Home | History | Annotate | Line # | Download | only in bfd
elf32-arm.c revision 1.11.12.1
      1 /* 32-bit ELF support for ARM
      2    Copyright (C) 1998-2018 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include <limits.h>
     23 
     24 #include "bfd.h"
     25 #include "bfd_stdint.h"
     26 #include "libiberty.h"
     27 #include "libbfd.h"
     28 #include "elf-bfd.h"
     29 #include "elf-nacl.h"
     30 #include "elf-vxworks.h"
     31 #include "elf/arm.h"
     32 
     33 /* Return the relocation section associated with NAME.  HTAB is the
     34    bfd's elf32_arm_link_hash_entry.  */
     35 #define RELOC_SECTION(HTAB, NAME) \
     36   ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
     37 
     38 /* Return size of a relocation entry.  HTAB is the bfd's
     39    elf32_arm_link_hash_entry.  */
     40 #define RELOC_SIZE(HTAB) \
     41   ((HTAB)->use_rel \
     42    ? sizeof (Elf32_External_Rel) \
     43    : sizeof (Elf32_External_Rela))
     44 
     45 /* Return function to swap relocations in.  HTAB is the bfd's
     46    elf32_arm_link_hash_entry.  */
     47 #define SWAP_RELOC_IN(HTAB) \
     48   ((HTAB)->use_rel \
     49    ? bfd_elf32_swap_reloc_in \
     50    : bfd_elf32_swap_reloca_in)
     51 
     52 /* Return function to swap relocations out.  HTAB is the bfd's
     53    elf32_arm_link_hash_entry.  */
     54 #define SWAP_RELOC_OUT(HTAB) \
     55   ((HTAB)->use_rel \
     56    ? bfd_elf32_swap_reloc_out \
     57    : bfd_elf32_swap_reloca_out)
     58 
     59 #define elf_info_to_howto		0
     60 #define elf_info_to_howto_rel		elf32_arm_info_to_howto
     61 
     62 #define ARM_ELF_ABI_VERSION		0
     63 #define ARM_ELF_OS_ABI_VERSION		ELFOSABI_ARM
     64 
     65 /* The Adjusted Place, as defined by AAELF.  */
     66 #define Pa(X) ((X) & 0xfffffffc)
     67 
     68 static bfd_boolean elf32_arm_write_section (bfd *output_bfd,
     69 					    struct bfd_link_info *link_info,
     70 					    asection *sec,
     71 					    bfd_byte *contents);
     72 
     73 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
     74    R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
     75    in that slot.  */
     76 
     77 static reloc_howto_type elf32_arm_howto_table_1[] =
     78 {
     79   /* No relocation.  */
     80   HOWTO (R_ARM_NONE,		/* type */
     81 	 0,			/* rightshift */
     82 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
     83 	 0,			/* bitsize */
     84 	 FALSE,			/* pc_relative */
     85 	 0,			/* bitpos */
     86 	 complain_overflow_dont,/* complain_on_overflow */
     87 	 bfd_elf_generic_reloc,	/* special_function */
     88 	 "R_ARM_NONE",		/* name */
     89 	 FALSE,			/* partial_inplace */
     90 	 0,			/* src_mask */
     91 	 0,			/* dst_mask */
     92 	 FALSE),		/* pcrel_offset */
     93 
     94   HOWTO (R_ARM_PC24,		/* type */
     95 	 2,			/* rightshift */
     96 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     97 	 24,			/* bitsize */
     98 	 TRUE,			/* pc_relative */
     99 	 0,			/* bitpos */
    100 	 complain_overflow_signed,/* complain_on_overflow */
    101 	 bfd_elf_generic_reloc,	/* special_function */
    102 	 "R_ARM_PC24",		/* name */
    103 	 FALSE,			/* partial_inplace */
    104 	 0x00ffffff,		/* src_mask */
    105 	 0x00ffffff,		/* dst_mask */
    106 	 TRUE),			/* pcrel_offset */
    107 
    108   /* 32 bit absolute */
    109   HOWTO (R_ARM_ABS32,		/* type */
    110 	 0,			/* rightshift */
    111 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    112 	 32,			/* bitsize */
    113 	 FALSE,			/* pc_relative */
    114 	 0,			/* bitpos */
    115 	 complain_overflow_bitfield,/* complain_on_overflow */
    116 	 bfd_elf_generic_reloc,	/* special_function */
    117 	 "R_ARM_ABS32",		/* name */
    118 	 FALSE,			/* partial_inplace */
    119 	 0xffffffff,		/* src_mask */
    120 	 0xffffffff,		/* dst_mask */
    121 	 FALSE),		/* pcrel_offset */
    122 
    123   /* standard 32bit pc-relative reloc */
    124   HOWTO (R_ARM_REL32,		/* type */
    125 	 0,			/* rightshift */
    126 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    127 	 32,			/* bitsize */
    128 	 TRUE,			/* pc_relative */
    129 	 0,			/* bitpos */
    130 	 complain_overflow_bitfield,/* complain_on_overflow */
    131 	 bfd_elf_generic_reloc,	/* special_function */
    132 	 "R_ARM_REL32",		/* name */
    133 	 FALSE,			/* partial_inplace */
    134 	 0xffffffff,		/* src_mask */
    135 	 0xffffffff,		/* dst_mask */
    136 	 TRUE),			/* pcrel_offset */
    137 
    138   /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
    139   HOWTO (R_ARM_LDR_PC_G0,	/* type */
    140 	 0,			/* rightshift */
    141 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    142 	 32,			/* bitsize */
    143 	 TRUE,			/* pc_relative */
    144 	 0,			/* bitpos */
    145 	 complain_overflow_dont,/* complain_on_overflow */
    146 	 bfd_elf_generic_reloc,	/* special_function */
    147 	 "R_ARM_LDR_PC_G0",     /* name */
    148 	 FALSE,			/* partial_inplace */
    149 	 0xffffffff,		/* src_mask */
    150 	 0xffffffff,		/* dst_mask */
    151 	 TRUE),			/* pcrel_offset */
    152 
    153    /* 16 bit absolute */
    154   HOWTO (R_ARM_ABS16,		/* type */
    155 	 0,			/* rightshift */
    156 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    157 	 16,			/* bitsize */
    158 	 FALSE,			/* pc_relative */
    159 	 0,			/* bitpos */
    160 	 complain_overflow_bitfield,/* complain_on_overflow */
    161 	 bfd_elf_generic_reloc,	/* special_function */
    162 	 "R_ARM_ABS16",		/* name */
    163 	 FALSE,			/* partial_inplace */
    164 	 0x0000ffff,		/* src_mask */
    165 	 0x0000ffff,		/* dst_mask */
    166 	 FALSE),		/* pcrel_offset */
    167 
    168   /* 12 bit absolute */
    169   HOWTO (R_ARM_ABS12,		/* type */
    170 	 0,			/* rightshift */
    171 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    172 	 12,			/* bitsize */
    173 	 FALSE,			/* pc_relative */
    174 	 0,			/* bitpos */
    175 	 complain_overflow_bitfield,/* complain_on_overflow */
    176 	 bfd_elf_generic_reloc,	/* special_function */
    177 	 "R_ARM_ABS12",		/* name */
    178 	 FALSE,			/* partial_inplace */
    179 	 0x00000fff,		/* src_mask */
    180 	 0x00000fff,		/* dst_mask */
    181 	 FALSE),		/* pcrel_offset */
    182 
    183   HOWTO (R_ARM_THM_ABS5,	/* type */
    184 	 6,			/* rightshift */
    185 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    186 	 5,			/* bitsize */
    187 	 FALSE,			/* pc_relative */
    188 	 0,			/* bitpos */
    189 	 complain_overflow_bitfield,/* complain_on_overflow */
    190 	 bfd_elf_generic_reloc,	/* special_function */
    191 	 "R_ARM_THM_ABS5",	/* name */
    192 	 FALSE,			/* partial_inplace */
    193 	 0x000007e0,		/* src_mask */
    194 	 0x000007e0,		/* dst_mask */
    195 	 FALSE),		/* pcrel_offset */
    196 
    197   /* 8 bit absolute */
    198   HOWTO (R_ARM_ABS8,		/* type */
    199 	 0,			/* rightshift */
    200 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    201 	 8,			/* bitsize */
    202 	 FALSE,			/* pc_relative */
    203 	 0,			/* bitpos */
    204 	 complain_overflow_bitfield,/* complain_on_overflow */
    205 	 bfd_elf_generic_reloc,	/* special_function */
    206 	 "R_ARM_ABS8",		/* name */
    207 	 FALSE,			/* partial_inplace */
    208 	 0x000000ff,		/* src_mask */
    209 	 0x000000ff,		/* dst_mask */
    210 	 FALSE),		/* pcrel_offset */
    211 
    212   HOWTO (R_ARM_SBREL32,		/* type */
    213 	 0,			/* rightshift */
    214 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    215 	 32,			/* bitsize */
    216 	 FALSE,			/* pc_relative */
    217 	 0,			/* bitpos */
    218 	 complain_overflow_dont,/* complain_on_overflow */
    219 	 bfd_elf_generic_reloc,	/* special_function */
    220 	 "R_ARM_SBREL32",	/* name */
    221 	 FALSE,			/* partial_inplace */
    222 	 0xffffffff,		/* src_mask */
    223 	 0xffffffff,		/* dst_mask */
    224 	 FALSE),		/* pcrel_offset */
    225 
    226   HOWTO (R_ARM_THM_CALL,	/* type */
    227 	 1,			/* rightshift */
    228 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    229 	 24,			/* bitsize */
    230 	 TRUE,			/* pc_relative */
    231 	 0,			/* bitpos */
    232 	 complain_overflow_signed,/* complain_on_overflow */
    233 	 bfd_elf_generic_reloc,	/* special_function */
    234 	 "R_ARM_THM_CALL",	/* name */
    235 	 FALSE,			/* partial_inplace */
    236 	 0x07ff2fff,		/* src_mask */
    237 	 0x07ff2fff,		/* dst_mask */
    238 	 TRUE),			/* pcrel_offset */
    239 
    240   HOWTO (R_ARM_THM_PC8,		/* type */
    241 	 1,			/* rightshift */
    242 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    243 	 8,			/* bitsize */
    244 	 TRUE,			/* pc_relative */
    245 	 0,			/* bitpos */
    246 	 complain_overflow_signed,/* complain_on_overflow */
    247 	 bfd_elf_generic_reloc,	/* special_function */
    248 	 "R_ARM_THM_PC8",	/* name */
    249 	 FALSE,			/* partial_inplace */
    250 	 0x000000ff,		/* src_mask */
    251 	 0x000000ff,		/* dst_mask */
    252 	 TRUE),			/* pcrel_offset */
    253 
    254   HOWTO (R_ARM_BREL_ADJ,	/* type */
    255 	 1,			/* rightshift */
    256 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    257 	 32,			/* bitsize */
    258 	 FALSE,			/* pc_relative */
    259 	 0,			/* bitpos */
    260 	 complain_overflow_signed,/* complain_on_overflow */
    261 	 bfd_elf_generic_reloc,	/* special_function */
    262 	 "R_ARM_BREL_ADJ",	/* name */
    263 	 FALSE,			/* partial_inplace */
    264 	 0xffffffff,		/* src_mask */
    265 	 0xffffffff,		/* dst_mask */
    266 	 FALSE),		/* pcrel_offset */
    267 
    268   HOWTO (R_ARM_TLS_DESC,	/* type */
    269 	 0,			/* rightshift */
    270 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    271 	 32,			/* bitsize */
    272 	 FALSE,			/* pc_relative */
    273 	 0,			/* bitpos */
    274 	 complain_overflow_bitfield,/* complain_on_overflow */
    275 	 bfd_elf_generic_reloc,	/* special_function */
    276 	 "R_ARM_TLS_DESC",	/* name */
    277 	 FALSE,			/* partial_inplace */
    278 	 0xffffffff,		/* src_mask */
    279 	 0xffffffff,		/* dst_mask */
    280 	 FALSE),		/* pcrel_offset */
    281 
    282   HOWTO (R_ARM_THM_SWI8,	/* type */
    283 	 0,			/* rightshift */
    284 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    285 	 0,			/* bitsize */
    286 	 FALSE,			/* pc_relative */
    287 	 0,			/* bitpos */
    288 	 complain_overflow_signed,/* complain_on_overflow */
    289 	 bfd_elf_generic_reloc,	/* special_function */
    290 	 "R_ARM_SWI8",		/* name */
    291 	 FALSE,			/* partial_inplace */
    292 	 0x00000000,		/* src_mask */
    293 	 0x00000000,		/* dst_mask */
    294 	 FALSE),		/* pcrel_offset */
    295 
    296   /* BLX instruction for the ARM.  */
    297   HOWTO (R_ARM_XPC25,		/* type */
    298 	 2,			/* rightshift */
    299 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    300 	 24,			/* bitsize */
    301 	 TRUE,			/* pc_relative */
    302 	 0,			/* bitpos */
    303 	 complain_overflow_signed,/* complain_on_overflow */
    304 	 bfd_elf_generic_reloc,	/* special_function */
    305 	 "R_ARM_XPC25",		/* name */
    306 	 FALSE,			/* partial_inplace */
    307 	 0x00ffffff,		/* src_mask */
    308 	 0x00ffffff,		/* dst_mask */
    309 	 TRUE),			/* pcrel_offset */
    310 
    311   /* BLX instruction for the Thumb.  */
    312   HOWTO (R_ARM_THM_XPC22,	/* type */
    313 	 2,			/* rightshift */
    314 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    315 	 24,			/* bitsize */
    316 	 TRUE,			/* pc_relative */
    317 	 0,			/* bitpos */
    318 	 complain_overflow_signed,/* complain_on_overflow */
    319 	 bfd_elf_generic_reloc,	/* special_function */
    320 	 "R_ARM_THM_XPC22",	/* name */
    321 	 FALSE,			/* partial_inplace */
    322 	 0x07ff2fff,		/* src_mask */
    323 	 0x07ff2fff,		/* dst_mask */
    324 	 TRUE),			/* pcrel_offset */
    325 
    326   /* Dynamic TLS relocations.  */
    327 
    328   HOWTO (R_ARM_TLS_DTPMOD32,	/* type */
    329 	 0,			/* rightshift */
    330 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    331 	 32,			/* bitsize */
    332 	 FALSE,			/* pc_relative */
    333 	 0,			/* bitpos */
    334 	 complain_overflow_bitfield,/* complain_on_overflow */
    335 	 bfd_elf_generic_reloc, /* special_function */
    336 	 "R_ARM_TLS_DTPMOD32",	/* name */
    337 	 TRUE,			/* partial_inplace */
    338 	 0xffffffff,		/* src_mask */
    339 	 0xffffffff,		/* dst_mask */
    340 	 FALSE),		/* pcrel_offset */
    341 
    342   HOWTO (R_ARM_TLS_DTPOFF32,	/* 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_bitfield,/* complain_on_overflow */
    349 	 bfd_elf_generic_reloc, /* special_function */
    350 	 "R_ARM_TLS_DTPOFF32",	/* name */
    351 	 TRUE,			/* partial_inplace */
    352 	 0xffffffff,		/* src_mask */
    353 	 0xffffffff,		/* dst_mask */
    354 	 FALSE),		/* pcrel_offset */
    355 
    356   HOWTO (R_ARM_TLS_TPOFF32,	/* type */
    357 	 0,			/* rightshift */
    358 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    359 	 32,			/* bitsize */
    360 	 FALSE,			/* pc_relative */
    361 	 0,			/* bitpos */
    362 	 complain_overflow_bitfield,/* complain_on_overflow */
    363 	 bfd_elf_generic_reloc, /* special_function */
    364 	 "R_ARM_TLS_TPOFF32",	/* name */
    365 	 TRUE,			/* partial_inplace */
    366 	 0xffffffff,		/* src_mask */
    367 	 0xffffffff,		/* dst_mask */
    368 	 FALSE),		/* pcrel_offset */
    369 
    370   /* Relocs used in ARM Linux */
    371 
    372   HOWTO (R_ARM_COPY,		/* type */
    373 	 0,			/* rightshift */
    374 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    375 	 32,			/* bitsize */
    376 	 FALSE,			/* pc_relative */
    377 	 0,			/* bitpos */
    378 	 complain_overflow_bitfield,/* complain_on_overflow */
    379 	 bfd_elf_generic_reloc, /* special_function */
    380 	 "R_ARM_COPY",		/* name */
    381 	 TRUE,			/* partial_inplace */
    382 	 0xffffffff,		/* src_mask */
    383 	 0xffffffff,		/* dst_mask */
    384 	 FALSE),		/* pcrel_offset */
    385 
    386   HOWTO (R_ARM_GLOB_DAT,	/* type */
    387 	 0,			/* rightshift */
    388 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    389 	 32,			/* bitsize */
    390 	 FALSE,			/* pc_relative */
    391 	 0,			/* bitpos */
    392 	 complain_overflow_bitfield,/* complain_on_overflow */
    393 	 bfd_elf_generic_reloc, /* special_function */
    394 	 "R_ARM_GLOB_DAT",	/* name */
    395 	 TRUE,			/* partial_inplace */
    396 	 0xffffffff,		/* src_mask */
    397 	 0xffffffff,		/* dst_mask */
    398 	 FALSE),		/* pcrel_offset */
    399 
    400   HOWTO (R_ARM_JUMP_SLOT,	/* type */
    401 	 0,			/* rightshift */
    402 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    403 	 32,			/* bitsize */
    404 	 FALSE,			/* pc_relative */
    405 	 0,			/* bitpos */
    406 	 complain_overflow_bitfield,/* complain_on_overflow */
    407 	 bfd_elf_generic_reloc, /* special_function */
    408 	 "R_ARM_JUMP_SLOT",	/* name */
    409 	 TRUE,			/* partial_inplace */
    410 	 0xffffffff,		/* src_mask */
    411 	 0xffffffff,		/* dst_mask */
    412 	 FALSE),		/* pcrel_offset */
    413 
    414   HOWTO (R_ARM_RELATIVE,	/* type */
    415 	 0,			/* rightshift */
    416 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    417 	 32,			/* bitsize */
    418 	 FALSE,			/* pc_relative */
    419 	 0,			/* bitpos */
    420 	 complain_overflow_bitfield,/* complain_on_overflow */
    421 	 bfd_elf_generic_reloc, /* special_function */
    422 	 "R_ARM_RELATIVE",	/* name */
    423 	 TRUE,			/* partial_inplace */
    424 	 0xffffffff,		/* src_mask */
    425 	 0xffffffff,		/* dst_mask */
    426 	 FALSE),		/* pcrel_offset */
    427 
    428   HOWTO (R_ARM_GOTOFF32,	/* type */
    429 	 0,			/* rightshift */
    430 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    431 	 32,			/* bitsize */
    432 	 FALSE,			/* pc_relative */
    433 	 0,			/* bitpos */
    434 	 complain_overflow_bitfield,/* complain_on_overflow */
    435 	 bfd_elf_generic_reloc, /* special_function */
    436 	 "R_ARM_GOTOFF32",	/* name */
    437 	 TRUE,			/* partial_inplace */
    438 	 0xffffffff,		/* src_mask */
    439 	 0xffffffff,		/* dst_mask */
    440 	 FALSE),		/* pcrel_offset */
    441 
    442   HOWTO (R_ARM_GOTPC,		/* type */
    443 	 0,			/* rightshift */
    444 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    445 	 32,			/* bitsize */
    446 	 TRUE,			/* pc_relative */
    447 	 0,			/* bitpos */
    448 	 complain_overflow_bitfield,/* complain_on_overflow */
    449 	 bfd_elf_generic_reloc, /* special_function */
    450 	 "R_ARM_GOTPC",		/* name */
    451 	 TRUE,			/* partial_inplace */
    452 	 0xffffffff,		/* src_mask */
    453 	 0xffffffff,		/* dst_mask */
    454 	 TRUE),			/* pcrel_offset */
    455 
    456   HOWTO (R_ARM_GOT32,		/* type */
    457 	 0,			/* rightshift */
    458 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    459 	 32,			/* bitsize */
    460 	 FALSE,			/* pc_relative */
    461 	 0,			/* bitpos */
    462 	 complain_overflow_bitfield,/* complain_on_overflow */
    463 	 bfd_elf_generic_reloc, /* special_function */
    464 	 "R_ARM_GOT32",		/* name */
    465 	 TRUE,			/* partial_inplace */
    466 	 0xffffffff,		/* src_mask */
    467 	 0xffffffff,		/* dst_mask */
    468 	 FALSE),		/* pcrel_offset */
    469 
    470   HOWTO (R_ARM_PLT32,		/* type */
    471 	 2,			/* rightshift */
    472 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    473 	 24,			/* bitsize */
    474 	 TRUE,			/* pc_relative */
    475 	 0,			/* bitpos */
    476 	 complain_overflow_bitfield,/* complain_on_overflow */
    477 	 bfd_elf_generic_reloc, /* special_function */
    478 	 "R_ARM_PLT32",		/* name */
    479 	 FALSE,			/* partial_inplace */
    480 	 0x00ffffff,		/* src_mask */
    481 	 0x00ffffff,		/* dst_mask */
    482 	 TRUE),			/* pcrel_offset */
    483 
    484   HOWTO (R_ARM_CALL,		/* type */
    485 	 2,			/* rightshift */
    486 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    487 	 24,			/* bitsize */
    488 	 TRUE,			/* pc_relative */
    489 	 0,			/* bitpos */
    490 	 complain_overflow_signed,/* complain_on_overflow */
    491 	 bfd_elf_generic_reloc,	/* special_function */
    492 	 "R_ARM_CALL",		/* name */
    493 	 FALSE,			/* partial_inplace */
    494 	 0x00ffffff,		/* src_mask */
    495 	 0x00ffffff,		/* dst_mask */
    496 	 TRUE),			/* pcrel_offset */
    497 
    498   HOWTO (R_ARM_JUMP24,		/* type */
    499 	 2,			/* rightshift */
    500 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    501 	 24,			/* bitsize */
    502 	 TRUE,			/* pc_relative */
    503 	 0,			/* bitpos */
    504 	 complain_overflow_signed,/* complain_on_overflow */
    505 	 bfd_elf_generic_reloc,	/* special_function */
    506 	 "R_ARM_JUMP24",	/* name */
    507 	 FALSE,			/* partial_inplace */
    508 	 0x00ffffff,		/* src_mask */
    509 	 0x00ffffff,		/* dst_mask */
    510 	 TRUE),			/* pcrel_offset */
    511 
    512   HOWTO (R_ARM_THM_JUMP24,	/* type */
    513 	 1,			/* rightshift */
    514 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    515 	 24,			/* bitsize */
    516 	 TRUE,			/* pc_relative */
    517 	 0,			/* bitpos */
    518 	 complain_overflow_signed,/* complain_on_overflow */
    519 	 bfd_elf_generic_reloc,	/* special_function */
    520 	 "R_ARM_THM_JUMP24",	/* name */
    521 	 FALSE,			/* partial_inplace */
    522 	 0x07ff2fff,		/* src_mask */
    523 	 0x07ff2fff,		/* dst_mask */
    524 	 TRUE),			/* pcrel_offset */
    525 
    526   HOWTO (R_ARM_BASE_ABS,	/* type */
    527 	 0,			/* rightshift */
    528 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    529 	 32,			/* bitsize */
    530 	 FALSE,			/* pc_relative */
    531 	 0,			/* bitpos */
    532 	 complain_overflow_dont,/* complain_on_overflow */
    533 	 bfd_elf_generic_reloc,	/* special_function */
    534 	 "R_ARM_BASE_ABS",	/* name */
    535 	 FALSE,			/* partial_inplace */
    536 	 0xffffffff,		/* src_mask */
    537 	 0xffffffff,		/* dst_mask */
    538 	 FALSE),		/* pcrel_offset */
    539 
    540   HOWTO (R_ARM_ALU_PCREL7_0,	/* type */
    541 	 0,			/* rightshift */
    542 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    543 	 12,			/* bitsize */
    544 	 TRUE,			/* pc_relative */
    545 	 0,			/* bitpos */
    546 	 complain_overflow_dont,/* complain_on_overflow */
    547 	 bfd_elf_generic_reloc,	/* special_function */
    548 	 "R_ARM_ALU_PCREL_7_0",	/* name */
    549 	 FALSE,			/* partial_inplace */
    550 	 0x00000fff,		/* src_mask */
    551 	 0x00000fff,		/* dst_mask */
    552 	 TRUE),			/* pcrel_offset */
    553 
    554   HOWTO (R_ARM_ALU_PCREL15_8,	/* type */
    555 	 0,			/* rightshift */
    556 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    557 	 12,			/* bitsize */
    558 	 TRUE,			/* pc_relative */
    559 	 8,			/* bitpos */
    560 	 complain_overflow_dont,/* complain_on_overflow */
    561 	 bfd_elf_generic_reloc,	/* special_function */
    562 	 "R_ARM_ALU_PCREL_15_8",/* name */
    563 	 FALSE,			/* partial_inplace */
    564 	 0x00000fff,		/* src_mask */
    565 	 0x00000fff,		/* dst_mask */
    566 	 TRUE),			/* pcrel_offset */
    567 
    568   HOWTO (R_ARM_ALU_PCREL23_15,	/* type */
    569 	 0,			/* rightshift */
    570 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    571 	 12,			/* bitsize */
    572 	 TRUE,			/* pc_relative */
    573 	 16,			/* bitpos */
    574 	 complain_overflow_dont,/* complain_on_overflow */
    575 	 bfd_elf_generic_reloc,	/* special_function */
    576 	 "R_ARM_ALU_PCREL_23_15",/* name */
    577 	 FALSE,			/* partial_inplace */
    578 	 0x00000fff,		/* src_mask */
    579 	 0x00000fff,		/* dst_mask */
    580 	 TRUE),			/* pcrel_offset */
    581 
    582   HOWTO (R_ARM_LDR_SBREL_11_0,	/* type */
    583 	 0,			/* rightshift */
    584 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    585 	 12,			/* bitsize */
    586 	 FALSE,			/* pc_relative */
    587 	 0,			/* bitpos */
    588 	 complain_overflow_dont,/* complain_on_overflow */
    589 	 bfd_elf_generic_reloc,	/* special_function */
    590 	 "R_ARM_LDR_SBREL_11_0",/* name */
    591 	 FALSE,			/* partial_inplace */
    592 	 0x00000fff,		/* src_mask */
    593 	 0x00000fff,		/* dst_mask */
    594 	 FALSE),		/* pcrel_offset */
    595 
    596   HOWTO (R_ARM_ALU_SBREL_19_12,	/* type */
    597 	 0,			/* rightshift */
    598 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    599 	 8,			/* bitsize */
    600 	 FALSE,			/* pc_relative */
    601 	 12,			/* bitpos */
    602 	 complain_overflow_dont,/* complain_on_overflow */
    603 	 bfd_elf_generic_reloc,	/* special_function */
    604 	 "R_ARM_ALU_SBREL_19_12",/* name */
    605 	 FALSE,			/* partial_inplace */
    606 	 0x000ff000,		/* src_mask */
    607 	 0x000ff000,		/* dst_mask */
    608 	 FALSE),		/* pcrel_offset */
    609 
    610   HOWTO (R_ARM_ALU_SBREL_27_20,	/* type */
    611 	 0,			/* rightshift */
    612 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    613 	 8,			/* bitsize */
    614 	 FALSE,			/* pc_relative */
    615 	 20,			/* bitpos */
    616 	 complain_overflow_dont,/* complain_on_overflow */
    617 	 bfd_elf_generic_reloc,	/* special_function */
    618 	 "R_ARM_ALU_SBREL_27_20",/* name */
    619 	 FALSE,			/* partial_inplace */
    620 	 0x0ff00000,		/* src_mask */
    621 	 0x0ff00000,		/* dst_mask */
    622 	 FALSE),		/* pcrel_offset */
    623 
    624   HOWTO (R_ARM_TARGET1,		/* type */
    625 	 0,			/* rightshift */
    626 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    627 	 32,			/* bitsize */
    628 	 FALSE,			/* pc_relative */
    629 	 0,			/* bitpos */
    630 	 complain_overflow_dont,/* complain_on_overflow */
    631 	 bfd_elf_generic_reloc,	/* special_function */
    632 	 "R_ARM_TARGET1",	/* name */
    633 	 FALSE,			/* partial_inplace */
    634 	 0xffffffff,		/* src_mask */
    635 	 0xffffffff,		/* dst_mask */
    636 	 FALSE),		/* pcrel_offset */
    637 
    638   HOWTO (R_ARM_ROSEGREL32,	/* type */
    639 	 0,			/* rightshift */
    640 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    641 	 32,			/* bitsize */
    642 	 FALSE,			/* pc_relative */
    643 	 0,			/* bitpos */
    644 	 complain_overflow_dont,/* complain_on_overflow */
    645 	 bfd_elf_generic_reloc,	/* special_function */
    646 	 "R_ARM_ROSEGREL32",	/* name */
    647 	 FALSE,			/* partial_inplace */
    648 	 0xffffffff,		/* src_mask */
    649 	 0xffffffff,		/* dst_mask */
    650 	 FALSE),		/* pcrel_offset */
    651 
    652   HOWTO (R_ARM_V4BX,		/* type */
    653 	 0,			/* rightshift */
    654 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    655 	 32,			/* bitsize */
    656 	 FALSE,			/* pc_relative */
    657 	 0,			/* bitpos */
    658 	 complain_overflow_dont,/* complain_on_overflow */
    659 	 bfd_elf_generic_reloc,	/* special_function */
    660 	 "R_ARM_V4BX",		/* name */
    661 	 FALSE,			/* partial_inplace */
    662 	 0xffffffff,		/* src_mask */
    663 	 0xffffffff,		/* dst_mask */
    664 	 FALSE),		/* pcrel_offset */
    665 
    666   HOWTO (R_ARM_TARGET2,		/* type */
    667 	 0,			/* rightshift */
    668 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    669 	 32,			/* bitsize */
    670 	 FALSE,			/* pc_relative */
    671 	 0,			/* bitpos */
    672 	 complain_overflow_signed,/* complain_on_overflow */
    673 	 bfd_elf_generic_reloc,	/* special_function */
    674 	 "R_ARM_TARGET2",	/* name */
    675 	 FALSE,			/* partial_inplace */
    676 	 0xffffffff,		/* src_mask */
    677 	 0xffffffff,		/* dst_mask */
    678 	 TRUE),			/* pcrel_offset */
    679 
    680   HOWTO (R_ARM_PREL31,		/* type */
    681 	 0,			/* rightshift */
    682 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    683 	 31,			/* bitsize */
    684 	 TRUE,			/* pc_relative */
    685 	 0,			/* bitpos */
    686 	 complain_overflow_signed,/* complain_on_overflow */
    687 	 bfd_elf_generic_reloc,	/* special_function */
    688 	 "R_ARM_PREL31",	/* name */
    689 	 FALSE,			/* partial_inplace */
    690 	 0x7fffffff,		/* src_mask */
    691 	 0x7fffffff,		/* dst_mask */
    692 	 TRUE),			/* pcrel_offset */
    693 
    694   HOWTO (R_ARM_MOVW_ABS_NC,	/* type */
    695 	 0,			/* rightshift */
    696 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    697 	 16,			/* bitsize */
    698 	 FALSE,			/* pc_relative */
    699 	 0,			/* bitpos */
    700 	 complain_overflow_dont,/* complain_on_overflow */
    701 	 bfd_elf_generic_reloc,	/* special_function */
    702 	 "R_ARM_MOVW_ABS_NC",	/* name */
    703 	 FALSE,			/* partial_inplace */
    704 	 0x000f0fff,		/* src_mask */
    705 	 0x000f0fff,		/* dst_mask */
    706 	 FALSE),		/* pcrel_offset */
    707 
    708   HOWTO (R_ARM_MOVT_ABS,	/* type */
    709 	 0,			/* rightshift */
    710 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    711 	 16,			/* bitsize */
    712 	 FALSE,			/* pc_relative */
    713 	 0,			/* bitpos */
    714 	 complain_overflow_bitfield,/* complain_on_overflow */
    715 	 bfd_elf_generic_reloc,	/* special_function */
    716 	 "R_ARM_MOVT_ABS",	/* name */
    717 	 FALSE,			/* partial_inplace */
    718 	 0x000f0fff,		/* src_mask */
    719 	 0x000f0fff,		/* dst_mask */
    720 	 FALSE),		/* pcrel_offset */
    721 
    722   HOWTO (R_ARM_MOVW_PREL_NC,	/* type */
    723 	 0,			/* rightshift */
    724 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    725 	 16,			/* bitsize */
    726 	 TRUE,			/* pc_relative */
    727 	 0,			/* bitpos */
    728 	 complain_overflow_dont,/* complain_on_overflow */
    729 	 bfd_elf_generic_reloc,	/* special_function */
    730 	 "R_ARM_MOVW_PREL_NC",	/* name */
    731 	 FALSE,			/* partial_inplace */
    732 	 0x000f0fff,		/* src_mask */
    733 	 0x000f0fff,		/* dst_mask */
    734 	 TRUE),			/* pcrel_offset */
    735 
    736   HOWTO (R_ARM_MOVT_PREL,	/* type */
    737 	 0,			/* rightshift */
    738 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    739 	 16,			/* bitsize */
    740 	 TRUE,			/* pc_relative */
    741 	 0,			/* bitpos */
    742 	 complain_overflow_bitfield,/* complain_on_overflow */
    743 	 bfd_elf_generic_reloc,	/* special_function */
    744 	 "R_ARM_MOVT_PREL",	/* name */
    745 	 FALSE,			/* partial_inplace */
    746 	 0x000f0fff,		/* src_mask */
    747 	 0x000f0fff,		/* dst_mask */
    748 	 TRUE),			/* pcrel_offset */
    749 
    750   HOWTO (R_ARM_THM_MOVW_ABS_NC,	/* type */
    751 	 0,			/* rightshift */
    752 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    753 	 16,			/* bitsize */
    754 	 FALSE,			/* pc_relative */
    755 	 0,			/* bitpos */
    756 	 complain_overflow_dont,/* complain_on_overflow */
    757 	 bfd_elf_generic_reloc,	/* special_function */
    758 	 "R_ARM_THM_MOVW_ABS_NC",/* name */
    759 	 FALSE,			/* partial_inplace */
    760 	 0x040f70ff,		/* src_mask */
    761 	 0x040f70ff,		/* dst_mask */
    762 	 FALSE),		/* pcrel_offset */
    763 
    764   HOWTO (R_ARM_THM_MOVT_ABS,	/* type */
    765 	 0,			/* rightshift */
    766 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    767 	 16,			/* bitsize */
    768 	 FALSE,			/* pc_relative */
    769 	 0,			/* bitpos */
    770 	 complain_overflow_bitfield,/* complain_on_overflow */
    771 	 bfd_elf_generic_reloc,	/* special_function */
    772 	 "R_ARM_THM_MOVT_ABS",	/* name */
    773 	 FALSE,			/* partial_inplace */
    774 	 0x040f70ff,		/* src_mask */
    775 	 0x040f70ff,		/* dst_mask */
    776 	 FALSE),		/* pcrel_offset */
    777 
    778   HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
    779 	 0,			/* rightshift */
    780 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    781 	 16,			/* bitsize */
    782 	 TRUE,			/* pc_relative */
    783 	 0,			/* bitpos */
    784 	 complain_overflow_dont,/* complain_on_overflow */
    785 	 bfd_elf_generic_reloc,	/* special_function */
    786 	 "R_ARM_THM_MOVW_PREL_NC",/* name */
    787 	 FALSE,			/* partial_inplace */
    788 	 0x040f70ff,		/* src_mask */
    789 	 0x040f70ff,		/* dst_mask */
    790 	 TRUE),			/* pcrel_offset */
    791 
    792   HOWTO (R_ARM_THM_MOVT_PREL,	/* type */
    793 	 0,			/* rightshift */
    794 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    795 	 16,			/* bitsize */
    796 	 TRUE,			/* pc_relative */
    797 	 0,			/* bitpos */
    798 	 complain_overflow_bitfield,/* complain_on_overflow */
    799 	 bfd_elf_generic_reloc,	/* special_function */
    800 	 "R_ARM_THM_MOVT_PREL",	/* name */
    801 	 FALSE,			/* partial_inplace */
    802 	 0x040f70ff,		/* src_mask */
    803 	 0x040f70ff,		/* dst_mask */
    804 	 TRUE),			/* pcrel_offset */
    805 
    806   HOWTO (R_ARM_THM_JUMP19,	/* type */
    807 	 1,			/* rightshift */
    808 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    809 	 19,			/* bitsize */
    810 	 TRUE,			/* pc_relative */
    811 	 0,			/* bitpos */
    812 	 complain_overflow_signed,/* complain_on_overflow */
    813 	 bfd_elf_generic_reloc, /* special_function */
    814 	 "R_ARM_THM_JUMP19",	/* name */
    815 	 FALSE,			/* partial_inplace */
    816 	 0x043f2fff,		/* src_mask */
    817 	 0x043f2fff,		/* dst_mask */
    818 	 TRUE),			/* pcrel_offset */
    819 
    820   HOWTO (R_ARM_THM_JUMP6,	/* type */
    821 	 1,			/* rightshift */
    822 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    823 	 6,			/* bitsize */
    824 	 TRUE,			/* pc_relative */
    825 	 0,			/* bitpos */
    826 	 complain_overflow_unsigned,/* complain_on_overflow */
    827 	 bfd_elf_generic_reloc,	/* special_function */
    828 	 "R_ARM_THM_JUMP6",	/* name */
    829 	 FALSE,			/* partial_inplace */
    830 	 0x02f8,		/* src_mask */
    831 	 0x02f8,		/* dst_mask */
    832 	 TRUE),			/* pcrel_offset */
    833 
    834   /* These are declared as 13-bit signed relocations because we can
    835      address -4095 .. 4095(base) by altering ADDW to SUBW or vice
    836      versa.  */
    837   HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
    838 	 0,			/* rightshift */
    839 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    840 	 13,			/* bitsize */
    841 	 TRUE,			/* pc_relative */
    842 	 0,			/* bitpos */
    843 	 complain_overflow_dont,/* complain_on_overflow */
    844 	 bfd_elf_generic_reloc,	/* special_function */
    845 	 "R_ARM_THM_ALU_PREL_11_0",/* name */
    846 	 FALSE,			/* partial_inplace */
    847 	 0xffffffff,		/* src_mask */
    848 	 0xffffffff,		/* dst_mask */
    849 	 TRUE),			/* pcrel_offset */
    850 
    851   HOWTO (R_ARM_THM_PC12,	/* type */
    852 	 0,			/* rightshift */
    853 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    854 	 13,			/* bitsize */
    855 	 TRUE,			/* pc_relative */
    856 	 0,			/* bitpos */
    857 	 complain_overflow_dont,/* complain_on_overflow */
    858 	 bfd_elf_generic_reloc,	/* special_function */
    859 	 "R_ARM_THM_PC12",	/* name */
    860 	 FALSE,			/* partial_inplace */
    861 	 0xffffffff,		/* src_mask */
    862 	 0xffffffff,		/* dst_mask */
    863 	 TRUE),			/* pcrel_offset */
    864 
    865   HOWTO (R_ARM_ABS32_NOI,	/* type */
    866 	 0,			/* rightshift */
    867 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    868 	 32,			/* bitsize */
    869 	 FALSE,			/* pc_relative */
    870 	 0,			/* bitpos */
    871 	 complain_overflow_dont,/* complain_on_overflow */
    872 	 bfd_elf_generic_reloc,	/* special_function */
    873 	 "R_ARM_ABS32_NOI",	/* name */
    874 	 FALSE,			/* partial_inplace */
    875 	 0xffffffff,		/* src_mask */
    876 	 0xffffffff,		/* dst_mask */
    877 	 FALSE),		/* pcrel_offset */
    878 
    879   HOWTO (R_ARM_REL32_NOI,	/* type */
    880 	 0,			/* rightshift */
    881 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    882 	 32,			/* bitsize */
    883 	 TRUE,			/* pc_relative */
    884 	 0,			/* bitpos */
    885 	 complain_overflow_dont,/* complain_on_overflow */
    886 	 bfd_elf_generic_reloc,	/* special_function */
    887 	 "R_ARM_REL32_NOI",	/* name */
    888 	 FALSE,			/* partial_inplace */
    889 	 0xffffffff,		/* src_mask */
    890 	 0xffffffff,		/* dst_mask */
    891 	 FALSE),		/* pcrel_offset */
    892 
    893   /* Group relocations.  */
    894 
    895   HOWTO (R_ARM_ALU_PC_G0_NC,	/* type */
    896 	 0,			/* rightshift */
    897 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    898 	 32,			/* bitsize */
    899 	 TRUE,			/* pc_relative */
    900 	 0,			/* bitpos */
    901 	 complain_overflow_dont,/* complain_on_overflow */
    902 	 bfd_elf_generic_reloc,	/* special_function */
    903 	 "R_ARM_ALU_PC_G0_NC",	/* name */
    904 	 FALSE,			/* partial_inplace */
    905 	 0xffffffff,		/* src_mask */
    906 	 0xffffffff,		/* dst_mask */
    907 	 TRUE),			/* pcrel_offset */
    908 
    909   HOWTO (R_ARM_ALU_PC_G0,	/* type */
    910 	 0,			/* rightshift */
    911 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    912 	 32,			/* bitsize */
    913 	 TRUE,			/* pc_relative */
    914 	 0,			/* bitpos */
    915 	 complain_overflow_dont,/* complain_on_overflow */
    916 	 bfd_elf_generic_reloc,	/* special_function */
    917 	 "R_ARM_ALU_PC_G0",	/* name */
    918 	 FALSE,			/* partial_inplace */
    919 	 0xffffffff,		/* src_mask */
    920 	 0xffffffff,		/* dst_mask */
    921 	 TRUE),			/* pcrel_offset */
    922 
    923   HOWTO (R_ARM_ALU_PC_G1_NC,	/* type */
    924 	 0,			/* rightshift */
    925 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    926 	 32,			/* bitsize */
    927 	 TRUE,			/* pc_relative */
    928 	 0,			/* bitpos */
    929 	 complain_overflow_dont,/* complain_on_overflow */
    930 	 bfd_elf_generic_reloc,	/* special_function */
    931 	 "R_ARM_ALU_PC_G1_NC",	/* name */
    932 	 FALSE,			/* partial_inplace */
    933 	 0xffffffff,		/* src_mask */
    934 	 0xffffffff,		/* dst_mask */
    935 	 TRUE),			/* pcrel_offset */
    936 
    937   HOWTO (R_ARM_ALU_PC_G1,	/* type */
    938 	 0,			/* rightshift */
    939 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    940 	 32,			/* bitsize */
    941 	 TRUE,			/* pc_relative */
    942 	 0,			/* bitpos */
    943 	 complain_overflow_dont,/* complain_on_overflow */
    944 	 bfd_elf_generic_reloc,	/* special_function */
    945 	 "R_ARM_ALU_PC_G1",	/* name */
    946 	 FALSE,			/* partial_inplace */
    947 	 0xffffffff,		/* src_mask */
    948 	 0xffffffff,		/* dst_mask */
    949 	 TRUE),			/* pcrel_offset */
    950 
    951   HOWTO (R_ARM_ALU_PC_G2,	/* type */
    952 	 0,			/* rightshift */
    953 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    954 	 32,			/* bitsize */
    955 	 TRUE,			/* pc_relative */
    956 	 0,			/* bitpos */
    957 	 complain_overflow_dont,/* complain_on_overflow */
    958 	 bfd_elf_generic_reloc,	/* special_function */
    959 	 "R_ARM_ALU_PC_G2",	/* name */
    960 	 FALSE,			/* partial_inplace */
    961 	 0xffffffff,		/* src_mask */
    962 	 0xffffffff,		/* dst_mask */
    963 	 TRUE),			/* pcrel_offset */
    964 
    965   HOWTO (R_ARM_LDR_PC_G1,	/* type */
    966 	 0,			/* rightshift */
    967 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    968 	 32,			/* bitsize */
    969 	 TRUE,			/* pc_relative */
    970 	 0,			/* bitpos */
    971 	 complain_overflow_dont,/* complain_on_overflow */
    972 	 bfd_elf_generic_reloc,	/* special_function */
    973 	 "R_ARM_LDR_PC_G1",	/* name */
    974 	 FALSE,			/* partial_inplace */
    975 	 0xffffffff,		/* src_mask */
    976 	 0xffffffff,		/* dst_mask */
    977 	 TRUE),			/* pcrel_offset */
    978 
    979   HOWTO (R_ARM_LDR_PC_G2,	/* type */
    980 	 0,			/* rightshift */
    981 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    982 	 32,			/* bitsize */
    983 	 TRUE,			/* pc_relative */
    984 	 0,			/* bitpos */
    985 	 complain_overflow_dont,/* complain_on_overflow */
    986 	 bfd_elf_generic_reloc,	/* special_function */
    987 	 "R_ARM_LDR_PC_G2",	/* name */
    988 	 FALSE,			/* partial_inplace */
    989 	 0xffffffff,		/* src_mask */
    990 	 0xffffffff,		/* dst_mask */
    991 	 TRUE),			/* pcrel_offset */
    992 
    993   HOWTO (R_ARM_LDRS_PC_G0,	/* type */
    994 	 0,			/* rightshift */
    995 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    996 	 32,			/* bitsize */
    997 	 TRUE,			/* pc_relative */
    998 	 0,			/* bitpos */
    999 	 complain_overflow_dont,/* complain_on_overflow */
   1000 	 bfd_elf_generic_reloc,	/* special_function */
   1001 	 "R_ARM_LDRS_PC_G0",	/* name */
   1002 	 FALSE,			/* partial_inplace */
   1003 	 0xffffffff,		/* src_mask */
   1004 	 0xffffffff,		/* dst_mask */
   1005 	 TRUE),			/* pcrel_offset */
   1006 
   1007   HOWTO (R_ARM_LDRS_PC_G1,	/* type */
   1008 	 0,			/* rightshift */
   1009 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1010 	 32,			/* bitsize */
   1011 	 TRUE,			/* pc_relative */
   1012 	 0,			/* bitpos */
   1013 	 complain_overflow_dont,/* complain_on_overflow */
   1014 	 bfd_elf_generic_reloc,	/* special_function */
   1015 	 "R_ARM_LDRS_PC_G1",	/* name */
   1016 	 FALSE,			/* partial_inplace */
   1017 	 0xffffffff,		/* src_mask */
   1018 	 0xffffffff,		/* dst_mask */
   1019 	 TRUE),			/* pcrel_offset */
   1020 
   1021   HOWTO (R_ARM_LDRS_PC_G2,	/* type */
   1022 	 0,			/* rightshift */
   1023 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1024 	 32,			/* bitsize */
   1025 	 TRUE,			/* pc_relative */
   1026 	 0,			/* bitpos */
   1027 	 complain_overflow_dont,/* complain_on_overflow */
   1028 	 bfd_elf_generic_reloc,	/* special_function */
   1029 	 "R_ARM_LDRS_PC_G2",	/* name */
   1030 	 FALSE,			/* partial_inplace */
   1031 	 0xffffffff,		/* src_mask */
   1032 	 0xffffffff,		/* dst_mask */
   1033 	 TRUE),			/* pcrel_offset */
   1034 
   1035   HOWTO (R_ARM_LDC_PC_G0,	/* type */
   1036 	 0,			/* rightshift */
   1037 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1038 	 32,			/* bitsize */
   1039 	 TRUE,			/* pc_relative */
   1040 	 0,			/* bitpos */
   1041 	 complain_overflow_dont,/* complain_on_overflow */
   1042 	 bfd_elf_generic_reloc,	/* special_function */
   1043 	 "R_ARM_LDC_PC_G0",	/* name */
   1044 	 FALSE,			/* partial_inplace */
   1045 	 0xffffffff,		/* src_mask */
   1046 	 0xffffffff,		/* dst_mask */
   1047 	 TRUE),			/* pcrel_offset */
   1048 
   1049   HOWTO (R_ARM_LDC_PC_G1,	/* type */
   1050 	 0,			/* rightshift */
   1051 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1052 	 32,			/* bitsize */
   1053 	 TRUE,			/* pc_relative */
   1054 	 0,			/* bitpos */
   1055 	 complain_overflow_dont,/* complain_on_overflow */
   1056 	 bfd_elf_generic_reloc,	/* special_function */
   1057 	 "R_ARM_LDC_PC_G1",	/* name */
   1058 	 FALSE,			/* partial_inplace */
   1059 	 0xffffffff,		/* src_mask */
   1060 	 0xffffffff,		/* dst_mask */
   1061 	 TRUE),			/* pcrel_offset */
   1062 
   1063   HOWTO (R_ARM_LDC_PC_G2,	/* type */
   1064 	 0,			/* rightshift */
   1065 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1066 	 32,			/* bitsize */
   1067 	 TRUE,			/* pc_relative */
   1068 	 0,			/* bitpos */
   1069 	 complain_overflow_dont,/* complain_on_overflow */
   1070 	 bfd_elf_generic_reloc,	/* special_function */
   1071 	 "R_ARM_LDC_PC_G2",	/* name */
   1072 	 FALSE,			/* partial_inplace */
   1073 	 0xffffffff,		/* src_mask */
   1074 	 0xffffffff,		/* dst_mask */
   1075 	 TRUE),			/* pcrel_offset */
   1076 
   1077   HOWTO (R_ARM_ALU_SB_G0_NC,	/* type */
   1078 	 0,			/* rightshift */
   1079 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1080 	 32,			/* bitsize */
   1081 	 TRUE,			/* pc_relative */
   1082 	 0,			/* bitpos */
   1083 	 complain_overflow_dont,/* complain_on_overflow */
   1084 	 bfd_elf_generic_reloc,	/* special_function */
   1085 	 "R_ARM_ALU_SB_G0_NC",	/* name */
   1086 	 FALSE,			/* partial_inplace */
   1087 	 0xffffffff,		/* src_mask */
   1088 	 0xffffffff,		/* dst_mask */
   1089 	 TRUE),			/* pcrel_offset */
   1090 
   1091   HOWTO (R_ARM_ALU_SB_G0,	/* type */
   1092 	 0,			/* rightshift */
   1093 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1094 	 32,			/* bitsize */
   1095 	 TRUE,			/* pc_relative */
   1096 	 0,			/* bitpos */
   1097 	 complain_overflow_dont,/* complain_on_overflow */
   1098 	 bfd_elf_generic_reloc,	/* special_function */
   1099 	 "R_ARM_ALU_SB_G0",	/* name */
   1100 	 FALSE,			/* partial_inplace */
   1101 	 0xffffffff,		/* src_mask */
   1102 	 0xffffffff,		/* dst_mask */
   1103 	 TRUE),			/* pcrel_offset */
   1104 
   1105   HOWTO (R_ARM_ALU_SB_G1_NC,	/* type */
   1106 	 0,			/* rightshift */
   1107 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1108 	 32,			/* bitsize */
   1109 	 TRUE,			/* pc_relative */
   1110 	 0,			/* bitpos */
   1111 	 complain_overflow_dont,/* complain_on_overflow */
   1112 	 bfd_elf_generic_reloc,	/* special_function */
   1113 	 "R_ARM_ALU_SB_G1_NC",	/* name */
   1114 	 FALSE,			/* partial_inplace */
   1115 	 0xffffffff,		/* src_mask */
   1116 	 0xffffffff,		/* dst_mask */
   1117 	 TRUE),			/* pcrel_offset */
   1118 
   1119   HOWTO (R_ARM_ALU_SB_G1,	/* type */
   1120 	 0,			/* rightshift */
   1121 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1122 	 32,			/* bitsize */
   1123 	 TRUE,			/* pc_relative */
   1124 	 0,			/* bitpos */
   1125 	 complain_overflow_dont,/* complain_on_overflow */
   1126 	 bfd_elf_generic_reloc,	/* special_function */
   1127 	 "R_ARM_ALU_SB_G1",	/* name */
   1128 	 FALSE,			/* partial_inplace */
   1129 	 0xffffffff,		/* src_mask */
   1130 	 0xffffffff,		/* dst_mask */
   1131 	 TRUE),			/* pcrel_offset */
   1132 
   1133   HOWTO (R_ARM_ALU_SB_G2,	/* type */
   1134 	 0,			/* rightshift */
   1135 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1136 	 32,			/* bitsize */
   1137 	 TRUE,			/* pc_relative */
   1138 	 0,			/* bitpos */
   1139 	 complain_overflow_dont,/* complain_on_overflow */
   1140 	 bfd_elf_generic_reloc,	/* special_function */
   1141 	 "R_ARM_ALU_SB_G2",	/* name */
   1142 	 FALSE,			/* partial_inplace */
   1143 	 0xffffffff,		/* src_mask */
   1144 	 0xffffffff,		/* dst_mask */
   1145 	 TRUE),			/* pcrel_offset */
   1146 
   1147   HOWTO (R_ARM_LDR_SB_G0,	/* type */
   1148 	 0,			/* rightshift */
   1149 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1150 	 32,			/* bitsize */
   1151 	 TRUE,			/* pc_relative */
   1152 	 0,			/* bitpos */
   1153 	 complain_overflow_dont,/* complain_on_overflow */
   1154 	 bfd_elf_generic_reloc,	/* special_function */
   1155 	 "R_ARM_LDR_SB_G0",	/* name */
   1156 	 FALSE,			/* partial_inplace */
   1157 	 0xffffffff,		/* src_mask */
   1158 	 0xffffffff,		/* dst_mask */
   1159 	 TRUE),			/* pcrel_offset */
   1160 
   1161   HOWTO (R_ARM_LDR_SB_G1,	/* type */
   1162 	 0,			/* rightshift */
   1163 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1164 	 32,			/* bitsize */
   1165 	 TRUE,			/* pc_relative */
   1166 	 0,			/* bitpos */
   1167 	 complain_overflow_dont,/* complain_on_overflow */
   1168 	 bfd_elf_generic_reloc,	/* special_function */
   1169 	 "R_ARM_LDR_SB_G1",	/* name */
   1170 	 FALSE,			/* partial_inplace */
   1171 	 0xffffffff,		/* src_mask */
   1172 	 0xffffffff,		/* dst_mask */
   1173 	 TRUE),			/* pcrel_offset */
   1174 
   1175   HOWTO (R_ARM_LDR_SB_G2,	/* type */
   1176 	 0,			/* rightshift */
   1177 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1178 	 32,			/* bitsize */
   1179 	 TRUE,			/* pc_relative */
   1180 	 0,			/* bitpos */
   1181 	 complain_overflow_dont,/* complain_on_overflow */
   1182 	 bfd_elf_generic_reloc,	/* special_function */
   1183 	 "R_ARM_LDR_SB_G2",	/* name */
   1184 	 FALSE,			/* partial_inplace */
   1185 	 0xffffffff,		/* src_mask */
   1186 	 0xffffffff,		/* dst_mask */
   1187 	 TRUE),			/* pcrel_offset */
   1188 
   1189   HOWTO (R_ARM_LDRS_SB_G0,	/* type */
   1190 	 0,			/* rightshift */
   1191 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1192 	 32,			/* bitsize */
   1193 	 TRUE,			/* pc_relative */
   1194 	 0,			/* bitpos */
   1195 	 complain_overflow_dont,/* complain_on_overflow */
   1196 	 bfd_elf_generic_reloc,	/* special_function */
   1197 	 "R_ARM_LDRS_SB_G0",	/* name */
   1198 	 FALSE,			/* partial_inplace */
   1199 	 0xffffffff,		/* src_mask */
   1200 	 0xffffffff,		/* dst_mask */
   1201 	 TRUE),			/* pcrel_offset */
   1202 
   1203   HOWTO (R_ARM_LDRS_SB_G1,	/* type */
   1204 	 0,			/* rightshift */
   1205 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1206 	 32,			/* bitsize */
   1207 	 TRUE,			/* pc_relative */
   1208 	 0,			/* bitpos */
   1209 	 complain_overflow_dont,/* complain_on_overflow */
   1210 	 bfd_elf_generic_reloc,	/* special_function */
   1211 	 "R_ARM_LDRS_SB_G1",	/* name */
   1212 	 FALSE,			/* partial_inplace */
   1213 	 0xffffffff,		/* src_mask */
   1214 	 0xffffffff,		/* dst_mask */
   1215 	 TRUE),			/* pcrel_offset */
   1216 
   1217   HOWTO (R_ARM_LDRS_SB_G2,	/* type */
   1218 	 0,			/* rightshift */
   1219 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1220 	 32,			/* bitsize */
   1221 	 TRUE,			/* pc_relative */
   1222 	 0,			/* bitpos */
   1223 	 complain_overflow_dont,/* complain_on_overflow */
   1224 	 bfd_elf_generic_reloc,	/* special_function */
   1225 	 "R_ARM_LDRS_SB_G2",	/* name */
   1226 	 FALSE,			/* partial_inplace */
   1227 	 0xffffffff,		/* src_mask */
   1228 	 0xffffffff,		/* dst_mask */
   1229 	 TRUE),			/* pcrel_offset */
   1230 
   1231   HOWTO (R_ARM_LDC_SB_G0,	/* type */
   1232 	 0,			/* rightshift */
   1233 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1234 	 32,			/* bitsize */
   1235 	 TRUE,			/* pc_relative */
   1236 	 0,			/* bitpos */
   1237 	 complain_overflow_dont,/* complain_on_overflow */
   1238 	 bfd_elf_generic_reloc,	/* special_function */
   1239 	 "R_ARM_LDC_SB_G0",	/* name */
   1240 	 FALSE,			/* partial_inplace */
   1241 	 0xffffffff,		/* src_mask */
   1242 	 0xffffffff,		/* dst_mask */
   1243 	 TRUE),			/* pcrel_offset */
   1244 
   1245   HOWTO (R_ARM_LDC_SB_G1,	/* type */
   1246 	 0,			/* rightshift */
   1247 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1248 	 32,			/* bitsize */
   1249 	 TRUE,			/* pc_relative */
   1250 	 0,			/* bitpos */
   1251 	 complain_overflow_dont,/* complain_on_overflow */
   1252 	 bfd_elf_generic_reloc,	/* special_function */
   1253 	 "R_ARM_LDC_SB_G1",	/* name */
   1254 	 FALSE,			/* partial_inplace */
   1255 	 0xffffffff,		/* src_mask */
   1256 	 0xffffffff,		/* dst_mask */
   1257 	 TRUE),			/* pcrel_offset */
   1258 
   1259   HOWTO (R_ARM_LDC_SB_G2,	/* type */
   1260 	 0,			/* rightshift */
   1261 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1262 	 32,			/* bitsize */
   1263 	 TRUE,			/* pc_relative */
   1264 	 0,			/* bitpos */
   1265 	 complain_overflow_dont,/* complain_on_overflow */
   1266 	 bfd_elf_generic_reloc,	/* special_function */
   1267 	 "R_ARM_LDC_SB_G2",	/* name */
   1268 	 FALSE,			/* partial_inplace */
   1269 	 0xffffffff,		/* src_mask */
   1270 	 0xffffffff,		/* dst_mask */
   1271 	 TRUE),			/* pcrel_offset */
   1272 
   1273   /* End of group relocations.  */
   1274 
   1275   HOWTO (R_ARM_MOVW_BREL_NC,	/* type */
   1276 	 0,			/* rightshift */
   1277 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1278 	 16,			/* bitsize */
   1279 	 FALSE,			/* pc_relative */
   1280 	 0,			/* bitpos */
   1281 	 complain_overflow_dont,/* complain_on_overflow */
   1282 	 bfd_elf_generic_reloc,	/* special_function */
   1283 	 "R_ARM_MOVW_BREL_NC",	/* name */
   1284 	 FALSE,			/* partial_inplace */
   1285 	 0x0000ffff,		/* src_mask */
   1286 	 0x0000ffff,		/* dst_mask */
   1287 	 FALSE),		/* pcrel_offset */
   1288 
   1289   HOWTO (R_ARM_MOVT_BREL,	/* type */
   1290 	 0,			/* rightshift */
   1291 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1292 	 16,			/* bitsize */
   1293 	 FALSE,			/* pc_relative */
   1294 	 0,			/* bitpos */
   1295 	 complain_overflow_bitfield,/* complain_on_overflow */
   1296 	 bfd_elf_generic_reloc,	/* special_function */
   1297 	 "R_ARM_MOVT_BREL",	/* name */
   1298 	 FALSE,			/* partial_inplace */
   1299 	 0x0000ffff,		/* src_mask */
   1300 	 0x0000ffff,		/* dst_mask */
   1301 	 FALSE),		/* pcrel_offset */
   1302 
   1303   HOWTO (R_ARM_MOVW_BREL,	/* type */
   1304 	 0,			/* rightshift */
   1305 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1306 	 16,			/* bitsize */
   1307 	 FALSE,			/* pc_relative */
   1308 	 0,			/* bitpos */
   1309 	 complain_overflow_dont,/* complain_on_overflow */
   1310 	 bfd_elf_generic_reloc,	/* special_function */
   1311 	 "R_ARM_MOVW_BREL",	/* name */
   1312 	 FALSE,			/* partial_inplace */
   1313 	 0x0000ffff,		/* src_mask */
   1314 	 0x0000ffff,		/* dst_mask */
   1315 	 FALSE),		/* pcrel_offset */
   1316 
   1317   HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
   1318 	 0,			/* rightshift */
   1319 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1320 	 16,			/* bitsize */
   1321 	 FALSE,			/* pc_relative */
   1322 	 0,			/* bitpos */
   1323 	 complain_overflow_dont,/* complain_on_overflow */
   1324 	 bfd_elf_generic_reloc,	/* special_function */
   1325 	 "R_ARM_THM_MOVW_BREL_NC",/* name */
   1326 	 FALSE,			/* partial_inplace */
   1327 	 0x040f70ff,		/* src_mask */
   1328 	 0x040f70ff,		/* dst_mask */
   1329 	 FALSE),		/* pcrel_offset */
   1330 
   1331   HOWTO (R_ARM_THM_MOVT_BREL,	/* type */
   1332 	 0,			/* rightshift */
   1333 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1334 	 16,			/* bitsize */
   1335 	 FALSE,			/* pc_relative */
   1336 	 0,			/* bitpos */
   1337 	 complain_overflow_bitfield,/* complain_on_overflow */
   1338 	 bfd_elf_generic_reloc,	/* special_function */
   1339 	 "R_ARM_THM_MOVT_BREL",	/* name */
   1340 	 FALSE,			/* partial_inplace */
   1341 	 0x040f70ff,		/* src_mask */
   1342 	 0x040f70ff,		/* dst_mask */
   1343 	 FALSE),		/* pcrel_offset */
   1344 
   1345   HOWTO (R_ARM_THM_MOVW_BREL,	/* type */
   1346 	 0,			/* rightshift */
   1347 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1348 	 16,			/* bitsize */
   1349 	 FALSE,			/* pc_relative */
   1350 	 0,			/* bitpos */
   1351 	 complain_overflow_dont,/* complain_on_overflow */
   1352 	 bfd_elf_generic_reloc,	/* special_function */
   1353 	 "R_ARM_THM_MOVW_BREL",	/* name */
   1354 	 FALSE,			/* partial_inplace */
   1355 	 0x040f70ff,		/* src_mask */
   1356 	 0x040f70ff,		/* dst_mask */
   1357 	 FALSE),		/* pcrel_offset */
   1358 
   1359   HOWTO (R_ARM_TLS_GOTDESC,	/* type */
   1360 	 0,			/* rightshift */
   1361 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1362 	 32,			/* bitsize */
   1363 	 FALSE,			/* pc_relative */
   1364 	 0,			/* bitpos */
   1365 	 complain_overflow_bitfield,/* complain_on_overflow */
   1366 	 NULL,			/* special_function */
   1367 	 "R_ARM_TLS_GOTDESC",	/* name */
   1368 	 TRUE,			/* partial_inplace */
   1369 	 0xffffffff,		/* src_mask */
   1370 	 0xffffffff,		/* dst_mask */
   1371 	 FALSE),		/* pcrel_offset */
   1372 
   1373   HOWTO (R_ARM_TLS_CALL,	/* type */
   1374 	 0,			/* rightshift */
   1375 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1376 	 24,			/* bitsize */
   1377 	 FALSE,			/* pc_relative */
   1378 	 0,			/* bitpos */
   1379 	 complain_overflow_dont,/* complain_on_overflow */
   1380 	 bfd_elf_generic_reloc,	/* special_function */
   1381 	 "R_ARM_TLS_CALL",	/* name */
   1382 	 FALSE,			/* partial_inplace */
   1383 	 0x00ffffff,		/* src_mask */
   1384 	 0x00ffffff,		/* dst_mask */
   1385 	 FALSE),		/* pcrel_offset */
   1386 
   1387   HOWTO (R_ARM_TLS_DESCSEQ,	/* type */
   1388 	 0,			/* rightshift */
   1389 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1390 	 0,			/* bitsize */
   1391 	 FALSE,			/* pc_relative */
   1392 	 0,			/* bitpos */
   1393 	 complain_overflow_bitfield,/* complain_on_overflow */
   1394 	 bfd_elf_generic_reloc,	/* special_function */
   1395 	 "R_ARM_TLS_DESCSEQ",	/* name */
   1396 	 FALSE,			/* partial_inplace */
   1397 	 0x00000000,		/* src_mask */
   1398 	 0x00000000,		/* dst_mask */
   1399 	 FALSE),		/* pcrel_offset */
   1400 
   1401   HOWTO (R_ARM_THM_TLS_CALL,	/* type */
   1402 	 0,			/* rightshift */
   1403 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1404 	 24,			/* bitsize */
   1405 	 FALSE,			/* pc_relative */
   1406 	 0,			/* bitpos */
   1407 	 complain_overflow_dont,/* complain_on_overflow */
   1408 	 bfd_elf_generic_reloc,	/* special_function */
   1409 	 "R_ARM_THM_TLS_CALL",	/* name */
   1410 	 FALSE,			/* partial_inplace */
   1411 	 0x07ff07ff,		/* src_mask */
   1412 	 0x07ff07ff,		/* dst_mask */
   1413 	 FALSE),		/* pcrel_offset */
   1414 
   1415   HOWTO (R_ARM_PLT32_ABS,	/* type */
   1416 	 0,			/* rightshift */
   1417 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1418 	 32,			/* bitsize */
   1419 	 FALSE,			/* pc_relative */
   1420 	 0,			/* bitpos */
   1421 	 complain_overflow_dont,/* complain_on_overflow */
   1422 	 bfd_elf_generic_reloc,	/* special_function */
   1423 	 "R_ARM_PLT32_ABS",	/* name */
   1424 	 FALSE,			/* partial_inplace */
   1425 	 0xffffffff,		/* src_mask */
   1426 	 0xffffffff,		/* dst_mask */
   1427 	 FALSE),		/* pcrel_offset */
   1428 
   1429   HOWTO (R_ARM_GOT_ABS,		/* type */
   1430 	 0,			/* rightshift */
   1431 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1432 	 32,			/* bitsize */
   1433 	 FALSE,			/* pc_relative */
   1434 	 0,			/* bitpos */
   1435 	 complain_overflow_dont,/* complain_on_overflow */
   1436 	 bfd_elf_generic_reloc,	/* special_function */
   1437 	 "R_ARM_GOT_ABS",	/* name */
   1438 	 FALSE,			/* partial_inplace */
   1439 	 0xffffffff,		/* src_mask */
   1440 	 0xffffffff,		/* dst_mask */
   1441 	 FALSE),			/* pcrel_offset */
   1442 
   1443   HOWTO (R_ARM_GOT_PREL,	/* type */
   1444 	 0,			/* rightshift */
   1445 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1446 	 32,			/* bitsize */
   1447 	 TRUE,			/* pc_relative */
   1448 	 0,			/* bitpos */
   1449 	 complain_overflow_dont,	/* complain_on_overflow */
   1450 	 bfd_elf_generic_reloc,	/* special_function */
   1451 	 "R_ARM_GOT_PREL",	/* name */
   1452 	 FALSE,			/* partial_inplace */
   1453 	 0xffffffff,		/* src_mask */
   1454 	 0xffffffff,		/* dst_mask */
   1455 	 TRUE),			/* pcrel_offset */
   1456 
   1457   HOWTO (R_ARM_GOT_BREL12,	/* type */
   1458 	 0,			/* rightshift */
   1459 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1460 	 12,			/* bitsize */
   1461 	 FALSE,			/* pc_relative */
   1462 	 0,			/* bitpos */
   1463 	 complain_overflow_bitfield,/* complain_on_overflow */
   1464 	 bfd_elf_generic_reloc,	/* special_function */
   1465 	 "R_ARM_GOT_BREL12",	/* name */
   1466 	 FALSE,			/* partial_inplace */
   1467 	 0x00000fff,		/* src_mask */
   1468 	 0x00000fff,		/* dst_mask */
   1469 	 FALSE),		/* pcrel_offset */
   1470 
   1471   HOWTO (R_ARM_GOTOFF12,	/* type */
   1472 	 0,			/* rightshift */
   1473 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1474 	 12,			/* bitsize */
   1475 	 FALSE,			/* pc_relative */
   1476 	 0,			/* bitpos */
   1477 	 complain_overflow_bitfield,/* complain_on_overflow */
   1478 	 bfd_elf_generic_reloc,	/* special_function */
   1479 	 "R_ARM_GOTOFF12",	/* name */
   1480 	 FALSE,			/* partial_inplace */
   1481 	 0x00000fff,		/* src_mask */
   1482 	 0x00000fff,		/* dst_mask */
   1483 	 FALSE),		/* pcrel_offset */
   1484 
   1485   EMPTY_HOWTO (R_ARM_GOTRELAX),	 /* reserved for future GOT-load optimizations */
   1486 
   1487   /* GNU extension to record C++ vtable member usage */
   1488   HOWTO (R_ARM_GNU_VTENTRY,	/* type */
   1489 	 0,			/* rightshift */
   1490 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1491 	 0,			/* bitsize */
   1492 	 FALSE,			/* pc_relative */
   1493 	 0,			/* bitpos */
   1494 	 complain_overflow_dont, /* complain_on_overflow */
   1495 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
   1496 	 "R_ARM_GNU_VTENTRY",	/* name */
   1497 	 FALSE,			/* partial_inplace */
   1498 	 0,			/* src_mask */
   1499 	 0,			/* dst_mask */
   1500 	 FALSE),		/* pcrel_offset */
   1501 
   1502   /* GNU extension to record C++ vtable hierarchy */
   1503   HOWTO (R_ARM_GNU_VTINHERIT, /* type */
   1504 	 0,			/* rightshift */
   1505 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1506 	 0,			/* bitsize */
   1507 	 FALSE,			/* pc_relative */
   1508 	 0,			/* bitpos */
   1509 	 complain_overflow_dont, /* complain_on_overflow */
   1510 	 NULL,			/* special_function */
   1511 	 "R_ARM_GNU_VTINHERIT", /* name */
   1512 	 FALSE,			/* partial_inplace */
   1513 	 0,			/* src_mask */
   1514 	 0,			/* dst_mask */
   1515 	 FALSE),		/* pcrel_offset */
   1516 
   1517   HOWTO (R_ARM_THM_JUMP11,	/* type */
   1518 	 1,			/* rightshift */
   1519 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1520 	 11,			/* bitsize */
   1521 	 TRUE,			/* pc_relative */
   1522 	 0,			/* bitpos */
   1523 	 complain_overflow_signed,	/* complain_on_overflow */
   1524 	 bfd_elf_generic_reloc,	/* special_function */
   1525 	 "R_ARM_THM_JUMP11",	/* name */
   1526 	 FALSE,			/* partial_inplace */
   1527 	 0x000007ff,		/* src_mask */
   1528 	 0x000007ff,		/* dst_mask */
   1529 	 TRUE),			/* pcrel_offset */
   1530 
   1531   HOWTO (R_ARM_THM_JUMP8,	/* type */
   1532 	 1,			/* rightshift */
   1533 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1534 	 8,			/* bitsize */
   1535 	 TRUE,			/* pc_relative */
   1536 	 0,			/* bitpos */
   1537 	 complain_overflow_signed,	/* complain_on_overflow */
   1538 	 bfd_elf_generic_reloc,	/* special_function */
   1539 	 "R_ARM_THM_JUMP8",	/* name */
   1540 	 FALSE,			/* partial_inplace */
   1541 	 0x000000ff,		/* src_mask */
   1542 	 0x000000ff,		/* dst_mask */
   1543 	 TRUE),			/* pcrel_offset */
   1544 
   1545   /* TLS relocations */
   1546   HOWTO (R_ARM_TLS_GD32,	/* type */
   1547 	 0,			/* rightshift */
   1548 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1549 	 32,			/* bitsize */
   1550 	 FALSE,			/* pc_relative */
   1551 	 0,			/* bitpos */
   1552 	 complain_overflow_bitfield,/* complain_on_overflow */
   1553 	 NULL,			/* special_function */
   1554 	 "R_ARM_TLS_GD32",	/* name */
   1555 	 TRUE,			/* partial_inplace */
   1556 	 0xffffffff,		/* src_mask */
   1557 	 0xffffffff,		/* dst_mask */
   1558 	 FALSE),		/* pcrel_offset */
   1559 
   1560   HOWTO (R_ARM_TLS_LDM32,	/* type */
   1561 	 0,			/* rightshift */
   1562 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1563 	 32,			/* bitsize */
   1564 	 FALSE,			/* pc_relative */
   1565 	 0,			/* bitpos */
   1566 	 complain_overflow_bitfield,/* complain_on_overflow */
   1567 	 bfd_elf_generic_reloc, /* special_function */
   1568 	 "R_ARM_TLS_LDM32",	/* name */
   1569 	 TRUE,			/* partial_inplace */
   1570 	 0xffffffff,		/* src_mask */
   1571 	 0xffffffff,		/* dst_mask */
   1572 	 FALSE),		/* pcrel_offset */
   1573 
   1574   HOWTO (R_ARM_TLS_LDO32,	/* type */
   1575 	 0,			/* rightshift */
   1576 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1577 	 32,			/* bitsize */
   1578 	 FALSE,			/* pc_relative */
   1579 	 0,			/* bitpos */
   1580 	 complain_overflow_bitfield,/* complain_on_overflow */
   1581 	 bfd_elf_generic_reloc, /* special_function */
   1582 	 "R_ARM_TLS_LDO32",	/* name */
   1583 	 TRUE,			/* partial_inplace */
   1584 	 0xffffffff,		/* src_mask */
   1585 	 0xffffffff,		/* dst_mask */
   1586 	 FALSE),		/* pcrel_offset */
   1587 
   1588   HOWTO (R_ARM_TLS_IE32,	/* type */
   1589 	 0,			/* rightshift */
   1590 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1591 	 32,			/* bitsize */
   1592 	 FALSE,			 /* pc_relative */
   1593 	 0,			/* bitpos */
   1594 	 complain_overflow_bitfield,/* complain_on_overflow */
   1595 	 NULL,			/* special_function */
   1596 	 "R_ARM_TLS_IE32",	/* name */
   1597 	 TRUE,			/* partial_inplace */
   1598 	 0xffffffff,		/* src_mask */
   1599 	 0xffffffff,		/* dst_mask */
   1600 	 FALSE),		/* pcrel_offset */
   1601 
   1602   HOWTO (R_ARM_TLS_LE32,	/* type */
   1603 	 0,			/* rightshift */
   1604 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1605 	 32,			/* bitsize */
   1606 	 FALSE,			/* pc_relative */
   1607 	 0,			/* bitpos */
   1608 	 complain_overflow_bitfield,/* complain_on_overflow */
   1609 	 NULL,			/* special_function */
   1610 	 "R_ARM_TLS_LE32",	/* name */
   1611 	 TRUE,			/* partial_inplace */
   1612 	 0xffffffff,		/* src_mask */
   1613 	 0xffffffff,		/* dst_mask */
   1614 	 FALSE),		/* pcrel_offset */
   1615 
   1616   HOWTO (R_ARM_TLS_LDO12,	/* type */
   1617 	 0,			/* rightshift */
   1618 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1619 	 12,			/* bitsize */
   1620 	 FALSE,			/* pc_relative */
   1621 	 0,			/* bitpos */
   1622 	 complain_overflow_bitfield,/* complain_on_overflow */
   1623 	 bfd_elf_generic_reloc,	/* special_function */
   1624 	 "R_ARM_TLS_LDO12",	/* name */
   1625 	 FALSE,			/* partial_inplace */
   1626 	 0x00000fff,		/* src_mask */
   1627 	 0x00000fff,		/* dst_mask */
   1628 	 FALSE),		/* pcrel_offset */
   1629 
   1630   HOWTO (R_ARM_TLS_LE12,	/* type */
   1631 	 0,			/* rightshift */
   1632 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1633 	 12,			/* bitsize */
   1634 	 FALSE,			/* pc_relative */
   1635 	 0,			/* bitpos */
   1636 	 complain_overflow_bitfield,/* complain_on_overflow */
   1637 	 bfd_elf_generic_reloc,	/* special_function */
   1638 	 "R_ARM_TLS_LE12",	/* name */
   1639 	 FALSE,			/* partial_inplace */
   1640 	 0x00000fff,		/* src_mask */
   1641 	 0x00000fff,		/* dst_mask */
   1642 	 FALSE),		/* pcrel_offset */
   1643 
   1644   HOWTO (R_ARM_TLS_IE12GP,	/* type */
   1645 	 0,			/* rightshift */
   1646 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1647 	 12,			/* bitsize */
   1648 	 FALSE,			/* pc_relative */
   1649 	 0,			/* bitpos */
   1650 	 complain_overflow_bitfield,/* complain_on_overflow */
   1651 	 bfd_elf_generic_reloc,	/* special_function */
   1652 	 "R_ARM_TLS_IE12GP",	/* name */
   1653 	 FALSE,			/* partial_inplace */
   1654 	 0x00000fff,		/* src_mask */
   1655 	 0x00000fff,		/* dst_mask */
   1656 	 FALSE),		/* pcrel_offset */
   1657 
   1658   /* 112-127 private relocations.  */
   1659   EMPTY_HOWTO (112),
   1660   EMPTY_HOWTO (113),
   1661   EMPTY_HOWTO (114),
   1662   EMPTY_HOWTO (115),
   1663   EMPTY_HOWTO (116),
   1664   EMPTY_HOWTO (117),
   1665   EMPTY_HOWTO (118),
   1666   EMPTY_HOWTO (119),
   1667   EMPTY_HOWTO (120),
   1668   EMPTY_HOWTO (121),
   1669   EMPTY_HOWTO (122),
   1670   EMPTY_HOWTO (123),
   1671   EMPTY_HOWTO (124),
   1672   EMPTY_HOWTO (125),
   1673   EMPTY_HOWTO (126),
   1674   EMPTY_HOWTO (127),
   1675 
   1676   /* R_ARM_ME_TOO, obsolete.  */
   1677   EMPTY_HOWTO (128),
   1678 
   1679   HOWTO (R_ARM_THM_TLS_DESCSEQ,	/* type */
   1680 	 0,			/* rightshift */
   1681 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1682 	 0,			/* bitsize */
   1683 	 FALSE,			/* pc_relative */
   1684 	 0,			/* bitpos */
   1685 	 complain_overflow_bitfield,/* complain_on_overflow */
   1686 	 bfd_elf_generic_reloc,	/* special_function */
   1687 	 "R_ARM_THM_TLS_DESCSEQ",/* name */
   1688 	 FALSE,			/* partial_inplace */
   1689 	 0x00000000,		/* src_mask */
   1690 	 0x00000000,		/* dst_mask */
   1691 	 FALSE),		/* pcrel_offset */
   1692   EMPTY_HOWTO (130),
   1693   EMPTY_HOWTO (131),
   1694   HOWTO (R_ARM_THM_ALU_ABS_G0_NC,/* type.  */
   1695 	 0,			/* rightshift.  */
   1696 	 1,			/* size (0 = byte, 1 = short, 2 = long).  */
   1697 	 16,			/* bitsize.  */
   1698 	 FALSE,			/* pc_relative.  */
   1699 	 0,			/* bitpos.  */
   1700 	 complain_overflow_bitfield,/* complain_on_overflow.  */
   1701 	 bfd_elf_generic_reloc,	/* special_function.  */
   1702 	 "R_ARM_THM_ALU_ABS_G0_NC",/* name.  */
   1703 	 FALSE,			/* partial_inplace.  */
   1704 	 0x00000000,		/* src_mask.  */
   1705 	 0x00000000,		/* dst_mask.  */
   1706 	 FALSE),		/* pcrel_offset.  */
   1707   HOWTO (R_ARM_THM_ALU_ABS_G1_NC,/* type.  */
   1708 	 0,			/* rightshift.  */
   1709 	 1,			/* size (0 = byte, 1 = short, 2 = long).  */
   1710 	 16,			/* bitsize.  */
   1711 	 FALSE,			/* pc_relative.  */
   1712 	 0,			/* bitpos.  */
   1713 	 complain_overflow_bitfield,/* complain_on_overflow.  */
   1714 	 bfd_elf_generic_reloc,	/* special_function.  */
   1715 	 "R_ARM_THM_ALU_ABS_G1_NC",/* name.  */
   1716 	 FALSE,			/* partial_inplace.  */
   1717 	 0x00000000,		/* src_mask.  */
   1718 	 0x00000000,		/* dst_mask.  */
   1719 	 FALSE),		/* pcrel_offset.  */
   1720   HOWTO (R_ARM_THM_ALU_ABS_G2_NC,/* type.  */
   1721 	 0,			/* rightshift.  */
   1722 	 1,			/* size (0 = byte, 1 = short, 2 = long).  */
   1723 	 16,			/* bitsize.  */
   1724 	 FALSE,			/* pc_relative.  */
   1725 	 0,			/* bitpos.  */
   1726 	 complain_overflow_bitfield,/* complain_on_overflow.  */
   1727 	 bfd_elf_generic_reloc,	/* special_function.  */
   1728 	 "R_ARM_THM_ALU_ABS_G2_NC",/* name.  */
   1729 	 FALSE,			/* partial_inplace.  */
   1730 	 0x00000000,		/* src_mask.  */
   1731 	 0x00000000,		/* dst_mask.  */
   1732 	 FALSE),		/* pcrel_offset.  */
   1733   HOWTO (R_ARM_THM_ALU_ABS_G3_NC,/* type.  */
   1734 	 0,			/* rightshift.  */
   1735 	 1,			/* size (0 = byte, 1 = short, 2 = long).  */
   1736 	 16,			/* bitsize.  */
   1737 	 FALSE,			/* pc_relative.  */
   1738 	 0,			/* bitpos.  */
   1739 	 complain_overflow_bitfield,/* complain_on_overflow.  */
   1740 	 bfd_elf_generic_reloc,	/* special_function.  */
   1741 	 "R_ARM_THM_ALU_ABS_G3_NC",/* name.  */
   1742 	 FALSE,			/* partial_inplace.  */
   1743 	 0x00000000,		/* src_mask.  */
   1744 	 0x00000000,		/* dst_mask.  */
   1745 	 FALSE),		/* pcrel_offset.  */
   1746 };
   1747 
   1748 /* 160 onwards: */
   1749 static reloc_howto_type elf32_arm_howto_table_2[1] =
   1750 {
   1751   HOWTO (R_ARM_IRELATIVE,	/* type */
   1752 	 0,			/* rightshift */
   1753 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1754 	 32,			/* bitsize */
   1755 	 FALSE,			/* pc_relative */
   1756 	 0,			/* bitpos */
   1757 	 complain_overflow_bitfield,/* complain_on_overflow */
   1758 	 bfd_elf_generic_reloc, /* special_function */
   1759 	 "R_ARM_IRELATIVE",	/* name */
   1760 	 TRUE,			/* partial_inplace */
   1761 	 0xffffffff,		/* src_mask */
   1762 	 0xffffffff,		/* dst_mask */
   1763 	 FALSE)			/* pcrel_offset */
   1764 };
   1765 
   1766 /* 249-255 extended, currently unused, relocations:  */
   1767 static reloc_howto_type elf32_arm_howto_table_3[4] =
   1768 {
   1769   HOWTO (R_ARM_RREL32,		/* type */
   1770 	 0,			/* rightshift */
   1771 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1772 	 0,			/* bitsize */
   1773 	 FALSE,			/* pc_relative */
   1774 	 0,			/* bitpos */
   1775 	 complain_overflow_dont,/* complain_on_overflow */
   1776 	 bfd_elf_generic_reloc,	/* special_function */
   1777 	 "R_ARM_RREL32",	/* name */
   1778 	 FALSE,			/* partial_inplace */
   1779 	 0,			/* src_mask */
   1780 	 0,			/* dst_mask */
   1781 	 FALSE),		/* pcrel_offset */
   1782 
   1783   HOWTO (R_ARM_RABS32,		/* type */
   1784 	 0,			/* rightshift */
   1785 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1786 	 0,			/* bitsize */
   1787 	 FALSE,			/* pc_relative */
   1788 	 0,			/* bitpos */
   1789 	 complain_overflow_dont,/* complain_on_overflow */
   1790 	 bfd_elf_generic_reloc,	/* special_function */
   1791 	 "R_ARM_RABS32",	/* name */
   1792 	 FALSE,			/* partial_inplace */
   1793 	 0,			/* src_mask */
   1794 	 0,			/* dst_mask */
   1795 	 FALSE),		/* pcrel_offset */
   1796 
   1797   HOWTO (R_ARM_RPC24,		/* type */
   1798 	 0,			/* rightshift */
   1799 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1800 	 0,			/* bitsize */
   1801 	 FALSE,			/* pc_relative */
   1802 	 0,			/* bitpos */
   1803 	 complain_overflow_dont,/* complain_on_overflow */
   1804 	 bfd_elf_generic_reloc,	/* special_function */
   1805 	 "R_ARM_RPC24",		/* name */
   1806 	 FALSE,			/* partial_inplace */
   1807 	 0,			/* src_mask */
   1808 	 0,			/* dst_mask */
   1809 	 FALSE),		/* pcrel_offset */
   1810 
   1811   HOWTO (R_ARM_RBASE,		/* type */
   1812 	 0,			/* rightshift */
   1813 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1814 	 0,			/* bitsize */
   1815 	 FALSE,			/* pc_relative */
   1816 	 0,			/* bitpos */
   1817 	 complain_overflow_dont,/* complain_on_overflow */
   1818 	 bfd_elf_generic_reloc,	/* special_function */
   1819 	 "R_ARM_RBASE",		/* name */
   1820 	 FALSE,			/* partial_inplace */
   1821 	 0,			/* src_mask */
   1822 	 0,			/* dst_mask */
   1823 	 FALSE)			/* pcrel_offset */
   1824 };
   1825 
   1826 static reloc_howto_type *
   1827 elf32_arm_howto_from_type (unsigned int r_type)
   1828 {
   1829   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
   1830     return &elf32_arm_howto_table_1[r_type];
   1831 
   1832   if (r_type == R_ARM_IRELATIVE)
   1833     return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
   1834 
   1835   if (r_type >= R_ARM_RREL32
   1836       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
   1837     return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
   1838 
   1839   return NULL;
   1840 }
   1841 
   1842 static void
   1843 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
   1844 			 Elf_Internal_Rela * elf_reloc)
   1845 {
   1846   unsigned int r_type;
   1847 
   1848   r_type = ELF32_R_TYPE (elf_reloc->r_info);
   1849   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
   1850 }
   1851 
   1852 struct elf32_arm_reloc_map
   1853   {
   1854     bfd_reloc_code_real_type  bfd_reloc_val;
   1855     unsigned char	      elf_reloc_val;
   1856   };
   1857 
   1858 /* All entries in this list must also be present in elf32_arm_howto_table.  */
   1859 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
   1860   {
   1861     {BFD_RELOC_NONE,		     R_ARM_NONE},
   1862     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
   1863     {BFD_RELOC_ARM_PCREL_CALL,	     R_ARM_CALL},
   1864     {BFD_RELOC_ARM_PCREL_JUMP,	     R_ARM_JUMP24},
   1865     {BFD_RELOC_ARM_PCREL_BLX,	     R_ARM_XPC25},
   1866     {BFD_RELOC_THUMB_PCREL_BLX,	     R_ARM_THM_XPC22},
   1867     {BFD_RELOC_32,		     R_ARM_ABS32},
   1868     {BFD_RELOC_32_PCREL,	     R_ARM_REL32},
   1869     {BFD_RELOC_8,		     R_ARM_ABS8},
   1870     {BFD_RELOC_16,		     R_ARM_ABS16},
   1871     {BFD_RELOC_ARM_OFFSET_IMM,	     R_ARM_ABS12},
   1872     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
   1873     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
   1874     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
   1875     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
   1876     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
   1877     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
   1878     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
   1879     {BFD_RELOC_ARM_GLOB_DAT,	     R_ARM_GLOB_DAT},
   1880     {BFD_RELOC_ARM_JUMP_SLOT,	     R_ARM_JUMP_SLOT},
   1881     {BFD_RELOC_ARM_RELATIVE,	     R_ARM_RELATIVE},
   1882     {BFD_RELOC_ARM_GOTOFF,	     R_ARM_GOTOFF32},
   1883     {BFD_RELOC_ARM_GOTPC,	     R_ARM_GOTPC},
   1884     {BFD_RELOC_ARM_GOT_PREL,	     R_ARM_GOT_PREL},
   1885     {BFD_RELOC_ARM_GOT32,	     R_ARM_GOT32},
   1886     {BFD_RELOC_ARM_PLT32,	     R_ARM_PLT32},
   1887     {BFD_RELOC_ARM_TARGET1,	     R_ARM_TARGET1},
   1888     {BFD_RELOC_ARM_ROSEGREL32,	     R_ARM_ROSEGREL32},
   1889     {BFD_RELOC_ARM_SBREL32,	     R_ARM_SBREL32},
   1890     {BFD_RELOC_ARM_PREL31,	     R_ARM_PREL31},
   1891     {BFD_RELOC_ARM_TARGET2,	     R_ARM_TARGET2},
   1892     {BFD_RELOC_ARM_PLT32,	     R_ARM_PLT32},
   1893     {BFD_RELOC_ARM_TLS_GOTDESC,	     R_ARM_TLS_GOTDESC},
   1894     {BFD_RELOC_ARM_TLS_CALL,	     R_ARM_TLS_CALL},
   1895     {BFD_RELOC_ARM_THM_TLS_CALL,     R_ARM_THM_TLS_CALL},
   1896     {BFD_RELOC_ARM_TLS_DESCSEQ,	     R_ARM_TLS_DESCSEQ},
   1897     {BFD_RELOC_ARM_THM_TLS_DESCSEQ,  R_ARM_THM_TLS_DESCSEQ},
   1898     {BFD_RELOC_ARM_TLS_DESC,	     R_ARM_TLS_DESC},
   1899     {BFD_RELOC_ARM_TLS_GD32,	     R_ARM_TLS_GD32},
   1900     {BFD_RELOC_ARM_TLS_LDO32,	     R_ARM_TLS_LDO32},
   1901     {BFD_RELOC_ARM_TLS_LDM32,	     R_ARM_TLS_LDM32},
   1902     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
   1903     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
   1904     {BFD_RELOC_ARM_TLS_TPOFF32,	     R_ARM_TLS_TPOFF32},
   1905     {BFD_RELOC_ARM_TLS_IE32,	     R_ARM_TLS_IE32},
   1906     {BFD_RELOC_ARM_TLS_LE32,	     R_ARM_TLS_LE32},
   1907     {BFD_RELOC_ARM_IRELATIVE,	     R_ARM_IRELATIVE},
   1908     {BFD_RELOC_VTABLE_INHERIT,	     R_ARM_GNU_VTINHERIT},
   1909     {BFD_RELOC_VTABLE_ENTRY,	     R_ARM_GNU_VTENTRY},
   1910     {BFD_RELOC_ARM_MOVW,	     R_ARM_MOVW_ABS_NC},
   1911     {BFD_RELOC_ARM_MOVT,	     R_ARM_MOVT_ABS},
   1912     {BFD_RELOC_ARM_MOVW_PCREL,	     R_ARM_MOVW_PREL_NC},
   1913     {BFD_RELOC_ARM_MOVT_PCREL,	     R_ARM_MOVT_PREL},
   1914     {BFD_RELOC_ARM_THUMB_MOVW,	     R_ARM_THM_MOVW_ABS_NC},
   1915     {BFD_RELOC_ARM_THUMB_MOVT,	     R_ARM_THM_MOVT_ABS},
   1916     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
   1917     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
   1918     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
   1919     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
   1920     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
   1921     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
   1922     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
   1923     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
   1924     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
   1925     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
   1926     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
   1927     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
   1928     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
   1929     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
   1930     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
   1931     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
   1932     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
   1933     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
   1934     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
   1935     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
   1936     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
   1937     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
   1938     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
   1939     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
   1940     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
   1941     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
   1942     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
   1943     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
   1944     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
   1945     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
   1946     {BFD_RELOC_ARM_V4BX,	     R_ARM_V4BX},
   1947     {BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC, R_ARM_THM_ALU_ABS_G3_NC},
   1948     {BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC, R_ARM_THM_ALU_ABS_G2_NC},
   1949     {BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC, R_ARM_THM_ALU_ABS_G1_NC},
   1950     {BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC, R_ARM_THM_ALU_ABS_G0_NC}
   1951   };
   1952 
   1953 static reloc_howto_type *
   1954 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1955 			     bfd_reloc_code_real_type code)
   1956 {
   1957   unsigned int i;
   1958 
   1959   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
   1960     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
   1961       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
   1962 
   1963   return NULL;
   1964 }
   1965 
   1966 static reloc_howto_type *
   1967 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1968 			     const char *r_name)
   1969 {
   1970   unsigned int i;
   1971 
   1972   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
   1973     if (elf32_arm_howto_table_1[i].name != NULL
   1974 	&& strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
   1975       return &elf32_arm_howto_table_1[i];
   1976 
   1977   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
   1978     if (elf32_arm_howto_table_2[i].name != NULL
   1979 	&& strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
   1980       return &elf32_arm_howto_table_2[i];
   1981 
   1982   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_3); i++)
   1983     if (elf32_arm_howto_table_3[i].name != NULL
   1984 	&& strcasecmp (elf32_arm_howto_table_3[i].name, r_name) == 0)
   1985       return &elf32_arm_howto_table_3[i];
   1986 
   1987   return NULL;
   1988 }
   1989 
   1990 /* Support for core dump NOTE sections.  */
   1991 
   1992 static bfd_boolean
   1993 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   1994 {
   1995   int offset;
   1996   size_t size;
   1997 
   1998   switch (note->descsz)
   1999     {
   2000       default:
   2001 	return FALSE;
   2002 
   2003       case 148:		/* Linux/ARM 32-bit.  */
   2004 	/* pr_cursig */
   2005 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   2006 
   2007 	/* pr_pid */
   2008 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
   2009 
   2010 	/* pr_reg */
   2011 	offset = 72;
   2012 	size = 72;
   2013 
   2014 	break;
   2015     }
   2016 
   2017   /* Make a ".reg/999" section.  */
   2018   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2019 					  size, note->descpos + offset);
   2020 }
   2021 
   2022 static bfd_boolean
   2023 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2024 {
   2025   switch (note->descsz)
   2026     {
   2027       default:
   2028 	return FALSE;
   2029 
   2030       case 124:		/* Linux/ARM elf_prpsinfo.  */
   2031 	elf_tdata (abfd)->core->pid
   2032 	 = bfd_get_32 (abfd, note->descdata + 12);
   2033 	elf_tdata (abfd)->core->program
   2034 	 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
   2035 	elf_tdata (abfd)->core->command
   2036 	 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
   2037     }
   2038 
   2039   /* Note that for some reason, a spurious space is tacked
   2040      onto the end of the args in some (at least one anyway)
   2041      implementations, so strip it off if it exists.  */
   2042   {
   2043     char *command = elf_tdata (abfd)->core->command;
   2044     int n = strlen (command);
   2045 
   2046     if (0 < n && command[n - 1] == ' ')
   2047       command[n - 1] = '\0';
   2048   }
   2049 
   2050   return TRUE;
   2051 }
   2052 
   2053 static char *
   2054 elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
   2055 				int note_type, ...)
   2056 {
   2057   switch (note_type)
   2058     {
   2059     default:
   2060       return NULL;
   2061 
   2062     case NT_PRPSINFO:
   2063       {
   2064 	char data[124];
   2065 	va_list ap;
   2066 
   2067 	va_start (ap, note_type);
   2068 	memset (data, 0, sizeof (data));
   2069 	strncpy (data + 28, va_arg (ap, const char *), 16);
   2070 	strncpy (data + 44, va_arg (ap, const char *), 80);
   2071 	va_end (ap);
   2072 
   2073 	return elfcore_write_note (abfd, buf, bufsiz,
   2074 				   "CORE", note_type, data, sizeof (data));
   2075       }
   2076 
   2077     case NT_PRSTATUS:
   2078       {
   2079 	char data[148];
   2080 	va_list ap;
   2081 	long pid;
   2082 	int cursig;
   2083 	const void *greg;
   2084 
   2085 	va_start (ap, note_type);
   2086 	memset (data, 0, sizeof (data));
   2087 	pid = va_arg (ap, long);
   2088 	bfd_put_32 (abfd, pid, data + 24);
   2089 	cursig = va_arg (ap, int);
   2090 	bfd_put_16 (abfd, cursig, data + 12);
   2091 	greg = va_arg (ap, const void *);
   2092 	memcpy (data + 72, greg, 72);
   2093 	va_end (ap);
   2094 
   2095 	return elfcore_write_note (abfd, buf, bufsiz,
   2096 				   "CORE", note_type, data, sizeof (data));
   2097       }
   2098     }
   2099 }
   2100 
   2101 #define TARGET_LITTLE_SYM		arm_elf32_le_vec
   2102 #define TARGET_LITTLE_NAME		"elf32-littlearm"
   2103 #define TARGET_BIG_SYM			arm_elf32_be_vec
   2104 #define TARGET_BIG_NAME			"elf32-bigarm"
   2105 
   2106 #define elf_backend_grok_prstatus	elf32_arm_nabi_grok_prstatus
   2107 #define elf_backend_grok_psinfo		elf32_arm_nabi_grok_psinfo
   2108 #define elf_backend_write_core_note	elf32_arm_nabi_write_core_note
   2109 
   2110 typedef unsigned long int insn32;
   2111 typedef unsigned short int insn16;
   2112 
   2113 /* In lieu of proper flags, assume all EABIv4 or later objects are
   2114    interworkable.  */
   2115 #define INTERWORK_FLAG(abfd)  \
   2116   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
   2117   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
   2118   || ((abfd)->flags & BFD_LINKER_CREATED))
   2119 
   2120 /* The linker script knows the section names for placement.
   2121    The entry_names are used to do simple name mangling on the stubs.
   2122    Given a function name, and its type, the stub can be found. The
   2123    name can be changed. The only requirement is the %s be present.  */
   2124 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
   2125 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
   2126 
   2127 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
   2128 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
   2129 
   2130 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
   2131 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
   2132 
   2133 #define STM32L4XX_ERRATUM_VENEER_SECTION_NAME ".text.stm32l4xx_veneer"
   2134 #define STM32L4XX_ERRATUM_VENEER_ENTRY_NAME   "__stm32l4xx_veneer_%x"
   2135 
   2136 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
   2137 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
   2138 
   2139 #define STUB_ENTRY_NAME   "__%s_veneer"
   2140 
   2141 #define CMSE_PREFIX "__acle_se_"
   2142 
   2143 /* The name of the dynamic interpreter.  This is put in the .interp
   2144    section.  */
   2145 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
   2146 
   2147 static const unsigned long tls_trampoline [] =
   2148 {
   2149   0xe08e0000,		/* add r0, lr, r0 */
   2150   0xe5901004,		/* ldr r1, [r0,#4] */
   2151   0xe12fff11,		/* bx  r1 */
   2152 };
   2153 
   2154 static const unsigned long dl_tlsdesc_lazy_trampoline [] =
   2155 {
   2156   0xe52d2004, /*	push    {r2}			*/
   2157   0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8]	*/
   2158   0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8]	*/
   2159   0xe79f2002, /* 1:   ldr     r2, [pc, r2]		*/
   2160   0xe081100f, /* 2:   add     r1, pc			*/
   2161   0xe12fff12, /*      bx      r2			*/
   2162   0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
   2163 				+ dl_tlsdesc_lazy_resolver(GOT)   */
   2164   0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
   2165 };
   2166 
   2167 #ifdef FOUR_WORD_PLT
   2168 
   2169 /* The first entry in a procedure linkage table looks like
   2170    this.  It is set up so that any shared library function that is
   2171    called before the relocation has been set up calls the dynamic
   2172    linker first.  */
   2173 static const bfd_vma elf32_arm_plt0_entry [] =
   2174 {
   2175   0xe52de004,		/* str   lr, [sp, #-4]! */
   2176   0xe59fe010,		/* ldr   lr, [pc, #16]  */
   2177   0xe08fe00e,		/* add   lr, pc, lr     */
   2178   0xe5bef008,		/* ldr   pc, [lr, #8]!  */
   2179 };
   2180 
   2181 /* Subsequent entries in a procedure linkage table look like
   2182    this.  */
   2183 static const bfd_vma elf32_arm_plt_entry [] =
   2184 {
   2185   0xe28fc600,		/* add   ip, pc, #NN	*/
   2186   0xe28cca00,		/* add	 ip, ip, #NN	*/
   2187   0xe5bcf000,		/* ldr	 pc, [ip, #NN]! */
   2188   0x00000000,		/* unused		*/
   2189 };
   2190 
   2191 #else /* not FOUR_WORD_PLT */
   2192 
   2193 /* The first entry in a procedure linkage table looks like
   2194    this.  It is set up so that any shared library function that is
   2195    called before the relocation has been set up calls the dynamic
   2196    linker first.  */
   2197 static const bfd_vma elf32_arm_plt0_entry [] =
   2198 {
   2199   0xe52de004,		/* str	 lr, [sp, #-4]! */
   2200   0xe59fe004,		/* ldr	 lr, [pc, #4]	*/
   2201   0xe08fe00e,		/* add	 lr, pc, lr	*/
   2202   0xe5bef008,		/* ldr	 pc, [lr, #8]!	*/
   2203   0x00000000,		/* &GOT[0] - .		*/
   2204 };
   2205 
   2206 /* By default subsequent entries in a procedure linkage table look like
   2207    this. Offsets that don't fit into 28 bits will cause link error.  */
   2208 static const bfd_vma elf32_arm_plt_entry_short [] =
   2209 {
   2210   0xe28fc600,		/* add   ip, pc, #0xNN00000 */
   2211   0xe28cca00,		/* add	 ip, ip, #0xNN000   */
   2212   0xe5bcf000,		/* ldr	 pc, [ip, #0xNNN]!  */
   2213 };
   2214 
   2215 /* When explicitly asked, we'll use this "long" entry format
   2216    which can cope with arbitrary displacements.  */
   2217 static const bfd_vma elf32_arm_plt_entry_long [] =
   2218 {
   2219   0xe28fc200,		/* add	 ip, pc, #0xN0000000 */
   2220   0xe28cc600,		/* add	 ip, ip, #0xNN00000  */
   2221   0xe28cca00,		/* add	 ip, ip, #0xNN000    */
   2222   0xe5bcf000,		/* ldr	 pc, [ip, #0xNNN]!   */
   2223 };
   2224 
   2225 static bfd_boolean elf32_arm_use_long_plt_entry = FALSE;
   2226 
   2227 #endif /* not FOUR_WORD_PLT */
   2228 
   2229 /* The first entry in a procedure linkage table looks like this.
   2230    It is set up so that any shared library function that is called before the
   2231    relocation has been set up calls the dynamic linker first.  */
   2232 static const bfd_vma elf32_thumb2_plt0_entry [] =
   2233 {
   2234   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
   2235      an instruction maybe encoded to one or two array elements.  */
   2236   0xf8dfb500,		/* push	   {lr}		 */
   2237   0x44fee008,		/* ldr.w   lr, [pc, #8]	 */
   2238 			/* add	   lr, pc	 */
   2239   0xff08f85e,		/* ldr.w   pc, [lr, #8]! */
   2240   0x00000000,		/* &GOT[0] - .		 */
   2241 };
   2242 
   2243 /* Subsequent entries in a procedure linkage table for thumb only target
   2244    look like this.  */
   2245 static const bfd_vma elf32_thumb2_plt_entry [] =
   2246 {
   2247   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
   2248      an instruction maybe encoded to one or two array elements.  */
   2249   0x0c00f240,		/* movw	   ip, #0xNNNN	  */
   2250   0x0c00f2c0,		/* movt	   ip, #0xNNNN	  */
   2251   0xf8dc44fc,		/* add	   ip, pc	  */
   2252   0xbf00f000		/* ldr.w   pc, [ip]	  */
   2253 			/* nop			  */
   2254 };
   2255 
   2256 /* The format of the first entry in the procedure linkage table
   2257    for a VxWorks executable.  */
   2258 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
   2259 {
   2260   0xe52dc008,		/* str	  ip,[sp,#-8]!			*/
   2261   0xe59fc000,		/* ldr	  ip,[pc]			*/
   2262   0xe59cf008,		/* ldr	  pc,[ip,#8]			*/
   2263   0x00000000,		/* .long  _GLOBAL_OFFSET_TABLE_		*/
   2264 };
   2265 
   2266 /* The format of subsequent entries in a VxWorks executable.  */
   2267 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
   2268 {
   2269   0xe59fc000,	      /* ldr	ip,[pc]			*/
   2270   0xe59cf000,	      /* ldr	pc,[ip]			*/
   2271   0x00000000,	      /* .long	@got				*/
   2272   0xe59fc000,	      /* ldr	ip,[pc]			*/
   2273   0xea000000,	      /* b	_PLT				*/
   2274   0x00000000,	      /* .long	@pltindex*sizeof(Elf32_Rela)	*/
   2275 };
   2276 
   2277 /* The format of entries in a VxWorks shared library.  */
   2278 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
   2279 {
   2280   0xe59fc000,	      /* ldr	ip,[pc]			*/
   2281   0xe79cf009,	      /* ldr	pc,[ip,r9]			*/
   2282   0x00000000,	      /* .long	@got				*/
   2283   0xe59fc000,	      /* ldr	ip,[pc]			*/
   2284   0xe599f008,	      /* ldr	pc,[r9,#8]			*/
   2285   0x00000000,	      /* .long	@pltindex*sizeof(Elf32_Rela)	*/
   2286 };
   2287 
   2288 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
   2289 #define PLT_THUMB_STUB_SIZE 4
   2290 static const bfd_vma elf32_arm_plt_thumb_stub [] =
   2291 {
   2292   0x4778,		/* bx pc */
   2293   0x46c0		/* nop   */
   2294 };
   2295 
   2296 /* The entries in a PLT when using a DLL-based target with multiple
   2297    address spaces.  */
   2298 static const bfd_vma elf32_arm_symbian_plt_entry [] =
   2299 {
   2300   0xe51ff004,	      /* ldr   pc, [pc, #-4] */
   2301   0x00000000,	      /* dcd   R_ARM_GLOB_DAT(X) */
   2302 };
   2303 
   2304 /* The first entry in a procedure linkage table looks like
   2305    this.  It is set up so that any shared library function that is
   2306    called before the relocation has been set up calls the dynamic
   2307    linker first.  */
   2308 static const bfd_vma elf32_arm_nacl_plt0_entry [] =
   2309 {
   2310   /* First bundle: */
   2311   0xe300c000,		/* movw	ip, #:lower16:&GOT[2]-.+8	*/
   2312   0xe340c000,		/* movt	ip, #:upper16:&GOT[2]-.+8	*/
   2313   0xe08cc00f,		/* add	ip, ip, pc			*/
   2314   0xe52dc008,		/* str	ip, [sp, #-8]!			*/
   2315   /* Second bundle: */
   2316   0xe3ccc103,		/* bic	ip, ip, #0xc0000000		*/
   2317   0xe59cc000,		/* ldr	ip, [ip]			*/
   2318   0xe3ccc13f,		/* bic	ip, ip, #0xc000000f		*/
   2319   0xe12fff1c,		/* bx	ip				*/
   2320   /* Third bundle: */
   2321   0xe320f000,		/* nop					*/
   2322   0xe320f000,		/* nop					*/
   2323   0xe320f000,		/* nop					*/
   2324   /* .Lplt_tail: */
   2325   0xe50dc004,		/* str	ip, [sp, #-4]			*/
   2326   /* Fourth bundle: */
   2327   0xe3ccc103,		/* bic	ip, ip, #0xc0000000		*/
   2328   0xe59cc000,		/* ldr	ip, [ip]			*/
   2329   0xe3ccc13f,		/* bic	ip, ip, #0xc000000f		*/
   2330   0xe12fff1c,		/* bx	ip				*/
   2331 };
   2332 #define ARM_NACL_PLT_TAIL_OFFSET	(11 * 4)
   2333 
   2334 /* Subsequent entries in a procedure linkage table look like this.  */
   2335 static const bfd_vma elf32_arm_nacl_plt_entry [] =
   2336 {
   2337   0xe300c000,		/* movw	ip, #:lower16:&GOT[n]-.+8	*/
   2338   0xe340c000,		/* movt	ip, #:upper16:&GOT[n]-.+8	*/
   2339   0xe08cc00f,		/* add	ip, ip, pc			*/
   2340   0xea000000,		/* b	.Lplt_tail			*/
   2341 };
   2342 
   2343 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
   2344 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
   2345 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
   2346 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
   2347 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
   2348 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
   2349 #define THM2_MAX_FWD_COND_BRANCH_OFFSET (((1 << 20) -2) + 4)
   2350 #define THM2_MAX_BWD_COND_BRANCH_OFFSET (-(1 << 20) + 4)
   2351 
   2352 enum stub_insn_type
   2353 {
   2354   THUMB16_TYPE = 1,
   2355   THUMB32_TYPE,
   2356   ARM_TYPE,
   2357   DATA_TYPE
   2358 };
   2359 
   2360 #define THUMB16_INSN(X)		{(X), THUMB16_TYPE, R_ARM_NONE, 0}
   2361 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
   2362    is inserted in arm_build_one_stub().  */
   2363 #define THUMB16_BCOND_INSN(X)	{(X), THUMB16_TYPE, R_ARM_NONE, 1}
   2364 #define THUMB32_INSN(X)		{(X), THUMB32_TYPE, R_ARM_NONE, 0}
   2365 #define THUMB32_MOVT(X)		{(X), THUMB32_TYPE, R_ARM_THM_MOVT_ABS, 0}
   2366 #define THUMB32_MOVW(X)		{(X), THUMB32_TYPE, R_ARM_THM_MOVW_ABS_NC, 0}
   2367 #define THUMB32_B_INSN(X, Z)	{(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
   2368 #define ARM_INSN(X)		{(X), ARM_TYPE, R_ARM_NONE, 0}
   2369 #define ARM_REL_INSN(X, Z)	{(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
   2370 #define DATA_WORD(X,Y,Z)	{(X), DATA_TYPE, (Y), (Z)}
   2371 
   2372 typedef struct
   2373 {
   2374   bfd_vma	       data;
   2375   enum stub_insn_type  type;
   2376   unsigned int	       r_type;
   2377   int		       reloc_addend;
   2378 }  insn_sequence;
   2379 
   2380 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
   2381    to reach the stub if necessary.  */
   2382 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
   2383 {
   2384   ARM_INSN (0xe51ff004),	    /* ldr   pc, [pc, #-4] */
   2385   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
   2386 };
   2387 
   2388 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
   2389    available.  */
   2390 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
   2391 {
   2392   ARM_INSN (0xe59fc000),	    /* ldr   ip, [pc, #0] */
   2393   ARM_INSN (0xe12fff1c),	    /* bx    ip */
   2394   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
   2395 };
   2396 
   2397 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
   2398 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
   2399 {
   2400   THUMB16_INSN (0xb401),	     /* push {r0} */
   2401   THUMB16_INSN (0x4802),	     /* ldr  r0, [pc, #8] */
   2402   THUMB16_INSN (0x4684),	     /* mov  ip, r0 */
   2403   THUMB16_INSN (0xbc01),	     /* pop  {r0} */
   2404   THUMB16_INSN (0x4760),	     /* bx   ip */
   2405   THUMB16_INSN (0xbf00),	     /* nop */
   2406   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
   2407 };
   2408 
   2409 /* Thumb -> Thumb long branch stub in thumb2 encoding.  Used on armv7.  */
   2410 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only[] =
   2411 {
   2412   THUMB32_INSN (0xf85ff000),	     /* ldr.w  pc, [pc, #-0] */
   2413   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(x) */
   2414 };
   2415 
   2416 /* Thumb -> Thumb long branch stub. Used for PureCode sections on Thumb2
   2417    M-profile architectures.  */
   2418 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only_pure[] =
   2419 {
   2420   THUMB32_MOVW (0xf2400c00),	     /* mov.w ip, R_ARM_MOVW_ABS_NC */
   2421   THUMB32_MOVT (0xf2c00c00),	     /* movt  ip, R_ARM_MOVT_ABS << 16 */
   2422   THUMB16_INSN (0x4760),	     /* bx   ip */
   2423 };
   2424 
   2425 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
   2426    allowed.  */
   2427 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
   2428 {
   2429   THUMB16_INSN (0x4778),	     /* bx   pc */
   2430   THUMB16_INSN (0x46c0),	     /* nop */
   2431   ARM_INSN (0xe59fc000),	     /* ldr  ip, [pc, #0] */
   2432   ARM_INSN (0xe12fff1c),	     /* bx   ip */
   2433   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
   2434 };
   2435 
   2436 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
   2437    available.  */
   2438 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
   2439 {
   2440   THUMB16_INSN (0x4778),	     /* bx   pc */
   2441   THUMB16_INSN (0x46c0),	     /* nop   */
   2442   ARM_INSN (0xe51ff004),	     /* ldr   pc, [pc, #-4] */
   2443   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
   2444 };
   2445 
   2446 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
   2447    one, when the destination is close enough.  */
   2448 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
   2449 {
   2450   THUMB16_INSN (0x4778),	     /* bx   pc */
   2451   THUMB16_INSN (0x46c0),	     /* nop   */
   2452   ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
   2453 };
   2454 
   2455 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
   2456    blx to reach the stub if necessary.  */
   2457 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
   2458 {
   2459   ARM_INSN (0xe59fc000),	     /* ldr   ip, [pc] */
   2460   ARM_INSN (0xe08ff00c),	     /* add   pc, pc, ip */
   2461   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
   2462 };
   2463 
   2464 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
   2465    blx to reach the stub if necessary.  We can not add into pc;
   2466    it is not guaranteed to mode switch (different in ARMv6 and
   2467    ARMv7).  */
   2468 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
   2469 {
   2470   ARM_INSN (0xe59fc004),	     /* ldr   ip, [pc, #4] */
   2471   ARM_INSN (0xe08fc00c),	     /* add   ip, pc, ip */
   2472   ARM_INSN (0xe12fff1c),	     /* bx    ip */
   2473   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
   2474 };
   2475 
   2476 /* V4T ARM -> ARM long branch stub, PIC.  */
   2477 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
   2478 {
   2479   ARM_INSN (0xe59fc004),	     /* ldr   ip, [pc, #4] */
   2480   ARM_INSN (0xe08fc00c),	     /* add   ip, pc, ip */
   2481   ARM_INSN (0xe12fff1c),	     /* bx    ip */
   2482   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
   2483 };
   2484 
   2485 /* V4T Thumb -> ARM long branch stub, PIC.  */
   2486 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
   2487 {
   2488   THUMB16_INSN (0x4778),	     /* bx   pc */
   2489   THUMB16_INSN (0x46c0),	     /* nop  */
   2490   ARM_INSN (0xe59fc000),	     /* ldr  ip, [pc, #0] */
   2491   ARM_INSN (0xe08cf00f),	     /* add  pc, ip, pc */
   2492   DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
   2493 };
   2494 
   2495 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
   2496    architectures.  */
   2497 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
   2498 {
   2499   THUMB16_INSN (0xb401),	     /* push {r0} */
   2500   THUMB16_INSN (0x4802),	     /* ldr  r0, [pc, #8] */
   2501   THUMB16_INSN (0x46fc),	     /* mov  ip, pc */
   2502   THUMB16_INSN (0x4484),	     /* add  ip, r0 */
   2503   THUMB16_INSN (0xbc01),	     /* pop  {r0} */
   2504   THUMB16_INSN (0x4760),	     /* bx   ip */
   2505   DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
   2506 };
   2507 
   2508 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
   2509    allowed.  */
   2510 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
   2511 {
   2512   THUMB16_INSN (0x4778),	     /* bx   pc */
   2513   THUMB16_INSN (0x46c0),	     /* nop */
   2514   ARM_INSN (0xe59fc004),	     /* ldr  ip, [pc, #4] */
   2515   ARM_INSN (0xe08fc00c),	     /* add   ip, pc, ip */
   2516   ARM_INSN (0xe12fff1c),	     /* bx   ip */
   2517   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
   2518 };
   2519 
   2520 /* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
   2521    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
   2522 static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
   2523 {
   2524   ARM_INSN (0xe59f1000),	     /* ldr   r1, [pc] */
   2525   ARM_INSN (0xe08ff001),	     /* add   pc, pc, r1 */
   2526   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
   2527 };
   2528 
   2529 /* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
   2530    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
   2531 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
   2532 {
   2533   THUMB16_INSN (0x4778),	     /* bx   pc */
   2534   THUMB16_INSN (0x46c0),	     /* nop */
   2535   ARM_INSN (0xe59f1000),	     /* ldr  r1, [pc, #0] */
   2536   ARM_INSN (0xe081f00f),	     /* add  pc, r1, pc */
   2537   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
   2538 };
   2539 
   2540 /* NaCl ARM -> ARM long branch stub.  */
   2541 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl[] =
   2542 {
   2543   ARM_INSN (0xe59fc00c),		/* ldr	ip, [pc, #12] */
   2544   ARM_INSN (0xe3ccc13f),		/* bic	ip, ip, #0xc000000f */
   2545   ARM_INSN (0xe12fff1c),		/* bx	ip */
   2546   ARM_INSN (0xe320f000),		/* nop */
   2547   ARM_INSN (0xe125be70),		/* bkpt	0x5be0 */
   2548   DATA_WORD (0, R_ARM_ABS32, 0),	/* dcd	R_ARM_ABS32(X) */
   2549   DATA_WORD (0, R_ARM_NONE, 0),		/* .word 0 */
   2550   DATA_WORD (0, R_ARM_NONE, 0),		/* .word 0 */
   2551 };
   2552 
   2553 /* NaCl ARM -> ARM long branch stub, PIC.  */
   2554 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl_pic[] =
   2555 {
   2556   ARM_INSN (0xe59fc00c),		/* ldr	ip, [pc, #12] */
   2557   ARM_INSN (0xe08cc00f),		/* add	ip, ip, pc */
   2558   ARM_INSN (0xe3ccc13f),		/* bic	ip, ip, #0xc000000f */
   2559   ARM_INSN (0xe12fff1c),		/* bx	ip */
   2560   ARM_INSN (0xe125be70),		/* bkpt	0x5be0 */
   2561   DATA_WORD (0, R_ARM_REL32, 8),	/* dcd	R_ARM_REL32(X+8) */
   2562   DATA_WORD (0, R_ARM_NONE, 0),		/* .word 0 */
   2563   DATA_WORD (0, R_ARM_NONE, 0),		/* .word 0 */
   2564 };
   2565 
   2566 /* Stub used for transition to secure state (aka SG veneer).  */
   2567 static const insn_sequence elf32_arm_stub_cmse_branch_thumb_only[] =
   2568 {
   2569   THUMB32_INSN (0xe97fe97f),		/* sg.  */
   2570   THUMB32_B_INSN (0xf000b800, -4),	/* b.w original_branch_dest.  */
   2571 };
   2572 
   2573 
   2574 /* Cortex-A8 erratum-workaround stubs.  */
   2575 
   2576 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
   2577    can't use a conditional branch to reach this stub).  */
   2578 
   2579 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
   2580 {
   2581   THUMB16_BCOND_INSN (0xd001),	       /* b<cond>.n true.  */
   2582   THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
   2583   THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
   2584 };
   2585 
   2586 /* Stub used for b.w and bl.w instructions.  */
   2587 
   2588 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
   2589 {
   2590   THUMB32_B_INSN (0xf000b800, -4)	/* b.w original_branch_dest.  */
   2591 };
   2592 
   2593 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
   2594 {
   2595   THUMB32_B_INSN (0xf000b800, -4)	/* b.w original_branch_dest.  */
   2596 };
   2597 
   2598 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
   2599    instruction (which switches to ARM mode) to point to this stub.  Jump to the
   2600    real destination using an ARM-mode branch.  */
   2601 
   2602 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
   2603 {
   2604   ARM_REL_INSN (0xea000000, -8)	/* b original_branch_dest.  */
   2605 };
   2606 
   2607 /* For each section group there can be a specially created linker section
   2608    to hold the stubs for that group.  The name of the stub section is based
   2609    upon the name of another section within that group with the suffix below
   2610    applied.
   2611 
   2612    PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
   2613    create what appeared to be a linker stub section when it actually
   2614    contained user code/data.  For example, consider this fragment:
   2615 
   2616      const char * stubborn_problems[] = { "np" };
   2617 
   2618    If this is compiled with "-fPIC -fdata-sections" then gcc produces a
   2619    section called:
   2620 
   2621      .data.rel.local.stubborn_problems
   2622 
   2623    This then causes problems in arm32_arm_build_stubs() as it triggers:
   2624 
   2625       // Ignore non-stub sections.
   2626       if (!strstr (stub_sec->name, STUB_SUFFIX))
   2627 	continue;
   2628 
   2629    And so the section would be ignored instead of being processed.  Hence
   2630    the change in definition of STUB_SUFFIX to a name that cannot be a valid
   2631    C identifier.  */
   2632 #define STUB_SUFFIX ".__stub"
   2633 
   2634 /* One entry per long/short branch stub defined above.  */
   2635 #define DEF_STUBS \
   2636   DEF_STUB(long_branch_any_any)	\
   2637   DEF_STUB(long_branch_v4t_arm_thumb) \
   2638   DEF_STUB(long_branch_thumb_only) \
   2639   DEF_STUB(long_branch_v4t_thumb_thumb)	\
   2640   DEF_STUB(long_branch_v4t_thumb_arm) \
   2641   DEF_STUB(short_branch_v4t_thumb_arm) \
   2642   DEF_STUB(long_branch_any_arm_pic) \
   2643   DEF_STUB(long_branch_any_thumb_pic) \
   2644   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
   2645   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
   2646   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
   2647   DEF_STUB(long_branch_thumb_only_pic) \
   2648   DEF_STUB(long_branch_any_tls_pic) \
   2649   DEF_STUB(long_branch_v4t_thumb_tls_pic) \
   2650   DEF_STUB(long_branch_arm_nacl) \
   2651   DEF_STUB(long_branch_arm_nacl_pic) \
   2652   DEF_STUB(cmse_branch_thumb_only) \
   2653   DEF_STUB(a8_veneer_b_cond) \
   2654   DEF_STUB(a8_veneer_b) \
   2655   DEF_STUB(a8_veneer_bl) \
   2656   DEF_STUB(a8_veneer_blx) \
   2657   DEF_STUB(long_branch_thumb2_only) \
   2658   DEF_STUB(long_branch_thumb2_only_pure)
   2659 
   2660 #define DEF_STUB(x) arm_stub_##x,
   2661 enum elf32_arm_stub_type
   2662 {
   2663   arm_stub_none,
   2664   DEF_STUBS
   2665   max_stub_type
   2666 };
   2667 #undef DEF_STUB
   2668 
   2669 /* Note the first a8_veneer type.  */
   2670 const unsigned arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond;
   2671 
   2672 typedef struct
   2673 {
   2674   const insn_sequence* template_sequence;
   2675   int template_size;
   2676 } stub_def;
   2677 
   2678 #define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
   2679 static const stub_def stub_definitions[] =
   2680 {
   2681   {NULL, 0},
   2682   DEF_STUBS
   2683 };
   2684 
   2685 struct elf32_arm_stub_hash_entry
   2686 {
   2687   /* Base hash table entry structure.  */
   2688   struct bfd_hash_entry root;
   2689 
   2690   /* The stub section.  */
   2691   asection *stub_sec;
   2692 
   2693   /* Offset within stub_sec of the beginning of this stub.  */
   2694   bfd_vma stub_offset;
   2695 
   2696   /* Given the symbol's value and its section we can determine its final
   2697      value when building the stubs (so the stub knows where to jump).  */
   2698   bfd_vma target_value;
   2699   asection *target_section;
   2700 
   2701   /* Same as above but for the source of the branch to the stub.  Used for
   2702      Cortex-A8 erratum workaround to patch it to branch to the stub.  As
   2703      such, source section does not need to be recorded since Cortex-A8 erratum
   2704      workaround stubs are only generated when both source and target are in the
   2705      same section.  */
   2706   bfd_vma source_value;
   2707 
   2708   /* The instruction which caused this stub to be generated (only valid for
   2709      Cortex-A8 erratum workaround stubs at present).  */
   2710   unsigned long orig_insn;
   2711 
   2712   /* The stub type.  */
   2713   enum elf32_arm_stub_type stub_type;
   2714   /* Its encoding size in bytes.  */
   2715   int stub_size;
   2716   /* Its template.  */
   2717   const insn_sequence *stub_template;
   2718   /* The size of the template (number of entries).  */
   2719   int stub_template_size;
   2720 
   2721   /* The symbol table entry, if any, that this was derived from.  */
   2722   struct elf32_arm_link_hash_entry *h;
   2723 
   2724   /* Type of branch.  */
   2725   enum arm_st_branch_type branch_type;
   2726 
   2727   /* Where this stub is being called from, or, in the case of combined
   2728      stub sections, the first input section in the group.  */
   2729   asection *id_sec;
   2730 
   2731   /* The name for the local symbol at the start of this stub.  The
   2732      stub name in the hash table has to be unique; this does not, so
   2733      it can be friendlier.  */
   2734   char *output_name;
   2735 };
   2736 
   2737 /* Used to build a map of a section.  This is required for mixed-endian
   2738    code/data.  */
   2739 
   2740 typedef struct elf32_elf_section_map
   2741 {
   2742   bfd_vma vma;
   2743   char type;
   2744 }
   2745 elf32_arm_section_map;
   2746 
   2747 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
   2748 
   2749 typedef enum
   2750 {
   2751   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
   2752   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
   2753   VFP11_ERRATUM_ARM_VENEER,
   2754   VFP11_ERRATUM_THUMB_VENEER
   2755 }
   2756 elf32_vfp11_erratum_type;
   2757 
   2758 typedef struct elf32_vfp11_erratum_list
   2759 {
   2760   struct elf32_vfp11_erratum_list *next;
   2761   bfd_vma vma;
   2762   union
   2763   {
   2764     struct
   2765     {
   2766       struct elf32_vfp11_erratum_list *veneer;
   2767       unsigned int vfp_insn;
   2768     } b;
   2769     struct
   2770     {
   2771       struct elf32_vfp11_erratum_list *branch;
   2772       unsigned int id;
   2773     } v;
   2774   } u;
   2775   elf32_vfp11_erratum_type type;
   2776 }
   2777 elf32_vfp11_erratum_list;
   2778 
   2779 /* Information about a STM32L4XX erratum veneer, or a branch to such a
   2780    veneer.  */
   2781 typedef enum
   2782 {
   2783   STM32L4XX_ERRATUM_BRANCH_TO_VENEER,
   2784   STM32L4XX_ERRATUM_VENEER
   2785 }
   2786 elf32_stm32l4xx_erratum_type;
   2787 
   2788 typedef struct elf32_stm32l4xx_erratum_list
   2789 {
   2790   struct elf32_stm32l4xx_erratum_list *next;
   2791   bfd_vma vma;
   2792   union
   2793   {
   2794     struct
   2795     {
   2796       struct elf32_stm32l4xx_erratum_list *veneer;
   2797       unsigned int insn;
   2798     } b;
   2799     struct
   2800     {
   2801       struct elf32_stm32l4xx_erratum_list *branch;
   2802       unsigned int id;
   2803     } v;
   2804   } u;
   2805   elf32_stm32l4xx_erratum_type type;
   2806 }
   2807 elf32_stm32l4xx_erratum_list;
   2808 
   2809 typedef enum
   2810 {
   2811   DELETE_EXIDX_ENTRY,
   2812   INSERT_EXIDX_CANTUNWIND_AT_END
   2813 }
   2814 arm_unwind_edit_type;
   2815 
   2816 /* A (sorted) list of edits to apply to an unwind table.  */
   2817 typedef struct arm_unwind_table_edit
   2818 {
   2819   arm_unwind_edit_type type;
   2820   /* Note: we sometimes want to insert an unwind entry corresponding to a
   2821      section different from the one we're currently writing out, so record the
   2822      (text) section this edit relates to here.  */
   2823   asection *linked_section;
   2824   unsigned int index;
   2825   struct arm_unwind_table_edit *next;
   2826 }
   2827 arm_unwind_table_edit;
   2828 
   2829 typedef struct _arm_elf_section_data
   2830 {
   2831   /* Information about mapping symbols.  */
   2832   struct bfd_elf_section_data elf;
   2833   unsigned int mapcount;
   2834   unsigned int mapsize;
   2835   elf32_arm_section_map *map;
   2836   /* Information about CPU errata.  */
   2837   unsigned int erratumcount;
   2838   elf32_vfp11_erratum_list *erratumlist;
   2839   unsigned int stm32l4xx_erratumcount;
   2840   elf32_stm32l4xx_erratum_list *stm32l4xx_erratumlist;
   2841   unsigned int additional_reloc_count;
   2842   /* Information about unwind tables.  */
   2843   union
   2844   {
   2845     /* Unwind info attached to a text section.  */
   2846     struct
   2847     {
   2848       asection *arm_exidx_sec;
   2849     } text;
   2850 
   2851     /* Unwind info attached to an .ARM.exidx section.  */
   2852     struct
   2853     {
   2854       arm_unwind_table_edit *unwind_edit_list;
   2855       arm_unwind_table_edit *unwind_edit_tail;
   2856     } exidx;
   2857   } u;
   2858 }
   2859 _arm_elf_section_data;
   2860 
   2861 #define elf32_arm_section_data(sec) \
   2862   ((_arm_elf_section_data *) elf_section_data (sec))
   2863 
   2864 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
   2865    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
   2866    so may be created multiple times: we use an array of these entries whilst
   2867    relaxing which we can refresh easily, then create stubs for each potentially
   2868    erratum-triggering instruction once we've settled on a solution.  */
   2869 
   2870 struct a8_erratum_fix
   2871 {
   2872   bfd *input_bfd;
   2873   asection *section;
   2874   bfd_vma offset;
   2875   bfd_vma target_offset;
   2876   unsigned long orig_insn;
   2877   char *stub_name;
   2878   enum elf32_arm_stub_type stub_type;
   2879   enum arm_st_branch_type branch_type;
   2880 };
   2881 
   2882 /* A table of relocs applied to branches which might trigger Cortex-A8
   2883    erratum.  */
   2884 
   2885 struct a8_erratum_reloc
   2886 {
   2887   bfd_vma from;
   2888   bfd_vma destination;
   2889   struct elf32_arm_link_hash_entry *hash;
   2890   const char *sym_name;
   2891   unsigned int r_type;
   2892   enum arm_st_branch_type branch_type;
   2893   bfd_boolean non_a8_stub;
   2894 };
   2895 
   2896 /* The size of the thread control block.  */
   2897 #define TCB_SIZE	8
   2898 
   2899 /* ARM-specific information about a PLT entry, over and above the usual
   2900    gotplt_union.  */
   2901 struct arm_plt_info
   2902 {
   2903   /* We reference count Thumb references to a PLT entry separately,
   2904      so that we can emit the Thumb trampoline only if needed.  */
   2905   bfd_signed_vma thumb_refcount;
   2906 
   2907   /* Some references from Thumb code may be eliminated by BL->BLX
   2908      conversion, so record them separately.  */
   2909   bfd_signed_vma maybe_thumb_refcount;
   2910 
   2911   /* How many of the recorded PLT accesses were from non-call relocations.
   2912      This information is useful when deciding whether anything takes the
   2913      address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
   2914      non-call references to the function should resolve directly to the
   2915      real runtime target.  */
   2916   unsigned int noncall_refcount;
   2917 
   2918   /* Since PLT entries have variable size if the Thumb prologue is
   2919      used, we need to record the index into .got.plt instead of
   2920      recomputing it from the PLT offset.  */
   2921   bfd_signed_vma got_offset;
   2922 };
   2923 
   2924 /* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
   2925 struct arm_local_iplt_info
   2926 {
   2927   /* The information that is usually found in the generic ELF part of
   2928      the hash table entry.  */
   2929   union gotplt_union root;
   2930 
   2931   /* The information that is usually found in the ARM-specific part of
   2932      the hash table entry.  */
   2933   struct arm_plt_info arm;
   2934 
   2935   /* A list of all potential dynamic relocations against this symbol.  */
   2936   struct elf_dyn_relocs *dyn_relocs;
   2937 };
   2938 
   2939 struct elf_arm_obj_tdata
   2940 {
   2941   struct elf_obj_tdata root;
   2942 
   2943   /* tls_type for each local got entry.  */
   2944   char *local_got_tls_type;
   2945 
   2946   /* GOTPLT entries for TLS descriptors.  */
   2947   bfd_vma *local_tlsdesc_gotent;
   2948 
   2949   /* Information for local symbols that need entries in .iplt.  */
   2950   struct arm_local_iplt_info **local_iplt;
   2951 
   2952   /* Zero to warn when linking objects with incompatible enum sizes.  */
   2953   int no_enum_size_warning;
   2954 
   2955   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
   2956   int no_wchar_size_warning;
   2957 };
   2958 
   2959 #define elf_arm_tdata(bfd) \
   2960   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
   2961 
   2962 #define elf32_arm_local_got_tls_type(bfd) \
   2963   (elf_arm_tdata (bfd)->local_got_tls_type)
   2964 
   2965 #define elf32_arm_local_tlsdesc_gotent(bfd) \
   2966   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
   2967 
   2968 #define elf32_arm_local_iplt(bfd) \
   2969   (elf_arm_tdata (bfd)->local_iplt)
   2970 
   2971 #define is_arm_elf(bfd) \
   2972   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2973    && elf_tdata (bfd) != NULL \
   2974    && elf_object_id (bfd) == ARM_ELF_DATA)
   2975 
   2976 static bfd_boolean
   2977 elf32_arm_mkobject (bfd *abfd)
   2978 {
   2979   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
   2980 				  ARM_ELF_DATA);
   2981 }
   2982 
   2983 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
   2984 
   2985 /* Arm ELF linker hash entry.  */
   2986 struct elf32_arm_link_hash_entry
   2987 {
   2988   struct elf_link_hash_entry root;
   2989 
   2990   /* Track dynamic relocs copied for this symbol.  */
   2991   struct elf_dyn_relocs *dyn_relocs;
   2992 
   2993   /* ARM-specific PLT information.  */
   2994   struct arm_plt_info plt;
   2995 
   2996 #define GOT_UNKNOWN	0
   2997 #define GOT_NORMAL	1
   2998 #define GOT_TLS_GD	2
   2999 #define GOT_TLS_IE	4
   3000 #define GOT_TLS_GDESC	8
   3001 #define GOT_TLS_GD_ANY_P(type)	((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
   3002   unsigned int tls_type : 8;
   3003 
   3004   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
   3005   unsigned int is_iplt : 1;
   3006 
   3007   unsigned int unused : 23;
   3008 
   3009   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
   3010      starting at the end of the jump table.  */
   3011   bfd_vma tlsdesc_got;
   3012 
   3013   /* The symbol marking the real symbol location for exported thumb
   3014      symbols with Arm stubs.  */
   3015   struct elf_link_hash_entry *export_glue;
   3016 
   3017   /* A pointer to the most recently used stub hash entry against this
   3018      symbol.  */
   3019   struct elf32_arm_stub_hash_entry *stub_cache;
   3020 };
   3021 
   3022 /* Traverse an arm ELF linker hash table.  */
   3023 #define elf32_arm_link_hash_traverse(table, func, info)			\
   3024   (elf_link_hash_traverse						\
   3025    (&(table)->root,							\
   3026     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
   3027     (info)))
   3028 
   3029 /* Get the ARM elf linker hash table from a link_info structure.  */
   3030 #define elf32_arm_hash_table(info) \
   3031   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
   3032   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
   3033 
   3034 #define arm_stub_hash_lookup(table, string, create, copy) \
   3035   ((struct elf32_arm_stub_hash_entry *) \
   3036    bfd_hash_lookup ((table), (string), (create), (copy)))
   3037 
   3038 /* Array to keep track of which stub sections have been created, and
   3039    information on stub grouping.  */
   3040 struct map_stub
   3041 {
   3042   /* This is the section to which stubs in the group will be
   3043      attached.  */
   3044   asection *link_sec;
   3045   /* The stub section.  */
   3046   asection *stub_sec;
   3047 };
   3048 
   3049 #define elf32_arm_compute_jump_table_size(htab) \
   3050   ((htab)->next_tls_desc_index * 4)
   3051 
   3052 /* ARM ELF linker hash table.  */
   3053 struct elf32_arm_link_hash_table
   3054 {
   3055   /* The main hash table.  */
   3056   struct elf_link_hash_table root;
   3057 
   3058   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
   3059   bfd_size_type thumb_glue_size;
   3060 
   3061   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
   3062   bfd_size_type arm_glue_size;
   3063 
   3064   /* The size in bytes of section containing the ARMv4 BX veneers.  */
   3065   bfd_size_type bx_glue_size;
   3066 
   3067   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
   3068      veneer has been populated.  */
   3069   bfd_vma bx_glue_offset[15];
   3070 
   3071   /* The size in bytes of the section containing glue for VFP11 erratum
   3072      veneers.  */
   3073   bfd_size_type vfp11_erratum_glue_size;
   3074 
   3075  /* The size in bytes of the section containing glue for STM32L4XX erratum
   3076      veneers.  */
   3077   bfd_size_type stm32l4xx_erratum_glue_size;
   3078 
   3079   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
   3080      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
   3081      elf32_arm_write_section().  */
   3082   struct a8_erratum_fix *a8_erratum_fixes;
   3083   unsigned int num_a8_erratum_fixes;
   3084 
   3085   /* An arbitrary input BFD chosen to hold the glue sections.  */
   3086   bfd * bfd_of_glue_owner;
   3087 
   3088   /* Nonzero to output a BE8 image.  */
   3089   int byteswap_code;
   3090 
   3091   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
   3092      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
   3093   int target1_is_rel;
   3094 
   3095   /* The relocation to use for R_ARM_TARGET2 relocations.  */
   3096   int target2_reloc;
   3097 
   3098   /* 0 = Ignore R_ARM_V4BX.
   3099      1 = Convert BX to MOV PC.
   3100      2 = Generate v4 interworing stubs.  */
   3101   int fix_v4bx;
   3102 
   3103   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
   3104   int fix_cortex_a8;
   3105 
   3106   /* Whether we should fix the ARM1176 BLX immediate issue.  */
   3107   int fix_arm1176;
   3108 
   3109   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
   3110   int use_blx;
   3111 
   3112   /* What sort of code sequences we should look for which may trigger the
   3113      VFP11 denorm erratum.  */
   3114   bfd_arm_vfp11_fix vfp11_fix;
   3115 
   3116   /* Global counter for the number of fixes we have emitted.  */
   3117   int num_vfp11_fixes;
   3118 
   3119   /* What sort of code sequences we should look for which may trigger the
   3120      STM32L4XX erratum.  */
   3121   bfd_arm_stm32l4xx_fix stm32l4xx_fix;
   3122 
   3123   /* Global counter for the number of fixes we have emitted.  */
   3124   int num_stm32l4xx_fixes;
   3125 
   3126   /* Nonzero to force PIC branch veneers.  */
   3127   int pic_veneer;
   3128 
   3129   /* The number of bytes in the initial entry in the PLT.  */
   3130   bfd_size_type plt_header_size;
   3131 
   3132   /* The number of bytes in the subsequent PLT etries.  */
   3133   bfd_size_type plt_entry_size;
   3134 
   3135   /* True if the target system is VxWorks.  */
   3136   int vxworks_p;
   3137 
   3138   /* True if the target system is Symbian OS.  */
   3139   int symbian_p;
   3140 
   3141   /* True if the target system is Native Client.  */
   3142   int nacl_p;
   3143 
   3144   /* True if the target uses REL relocations.  */
   3145   int use_rel;
   3146 
   3147   /* Nonzero if import library must be a secure gateway import library
   3148      as per ARMv8-M Security Extensions.  */
   3149   int cmse_implib;
   3150 
   3151   /* The import library whose symbols' address must remain stable in
   3152      the import library generated.  */
   3153   bfd *in_implib_bfd;
   3154 
   3155   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
   3156   bfd_vma next_tls_desc_index;
   3157 
   3158   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
   3159   bfd_vma num_tls_desc;
   3160 
   3161   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
   3162   asection *srelplt2;
   3163 
   3164   /* The offset into splt of the PLT entry for the TLS descriptor
   3165      resolver.  Special values are 0, if not necessary (or not found
   3166      to be necessary yet), and -1 if needed but not determined
   3167      yet.  */
   3168   bfd_vma dt_tlsdesc_plt;
   3169 
   3170   /* The offset into sgot of the GOT entry used by the PLT entry
   3171      above.  */
   3172   bfd_vma dt_tlsdesc_got;
   3173 
   3174   /* Offset in .plt section of tls_arm_trampoline.  */
   3175   bfd_vma tls_trampoline;
   3176 
   3177   /* Data for R_ARM_TLS_LDM32 relocations.  */
   3178   union
   3179   {
   3180     bfd_signed_vma refcount;
   3181     bfd_vma offset;
   3182   } tls_ldm_got;
   3183 
   3184   /* Small local sym cache.  */
   3185   struct sym_cache sym_cache;
   3186 
   3187   /* For convenience in allocate_dynrelocs.  */
   3188   bfd * obfd;
   3189 
   3190   /* The amount of space used by the reserved portion of the sgotplt
   3191      section, plus whatever space is used by the jump slots.  */
   3192   bfd_vma sgotplt_jump_table_size;
   3193 
   3194   /* The stub hash table.  */
   3195   struct bfd_hash_table stub_hash_table;
   3196 
   3197   /* Linker stub bfd.  */
   3198   bfd *stub_bfd;
   3199 
   3200   /* Linker call-backs.  */
   3201   asection * (*add_stub_section) (const char *, asection *, asection *,
   3202 				  unsigned int);
   3203   void (*layout_sections_again) (void);
   3204 
   3205   /* Array to keep track of which stub sections have been created, and
   3206      information on stub grouping.  */
   3207   struct map_stub *stub_group;
   3208 
   3209   /* Input stub section holding secure gateway veneers.  */
   3210   asection *cmse_stub_sec;
   3211 
   3212   /* Offset in cmse_stub_sec where new SG veneers (not in input import library)
   3213      start to be allocated.  */
   3214   bfd_vma new_cmse_stub_offset;
   3215 
   3216   /* Number of elements in stub_group.  */
   3217   unsigned int top_id;
   3218 
   3219   /* Assorted information used by elf32_arm_size_stubs.  */
   3220   unsigned int bfd_count;
   3221   unsigned int top_index;
   3222   asection **input_list;
   3223 };
   3224 
   3225 static inline int
   3226 ctz (unsigned int mask)
   3227 {
   3228 #if GCC_VERSION >= 3004
   3229   return __builtin_ctz (mask);
   3230 #else
   3231   unsigned int i;
   3232 
   3233   for (i = 0; i < 8 * sizeof (mask); i++)
   3234     {
   3235       if (mask & 0x1)
   3236 	break;
   3237       mask = (mask >> 1);
   3238     }
   3239   return i;
   3240 #endif
   3241 }
   3242 
   3243 #if !defined (__NetBSD__) || (__NetBSD_Version__ < 600000000)
   3244 static inline int
   3245 elf32_arm_popcount (unsigned int mask)
   3246 {
   3247 #if GCC_VERSION >= 3004
   3248   return __builtin_popcount (mask);
   3249 #else
   3250   unsigned int i;
   3251   int sum = 0;
   3252 
   3253   for (i = 0; i < 8 * sizeof (mask); i++)
   3254     {
   3255       if (mask & 0x1)
   3256 	sum++;
   3257       mask = (mask >> 1);
   3258     }
   3259   return sum;
   3260 #endif
   3261 }
   3262 #endif
   3263 
   3264 /* Create an entry in an ARM ELF linker hash table.  */
   3265 
   3266 static struct bfd_hash_entry *
   3267 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
   3268 			     struct bfd_hash_table * table,
   3269 			     const char * string)
   3270 {
   3271   struct elf32_arm_link_hash_entry * ret =
   3272     (struct elf32_arm_link_hash_entry *) entry;
   3273 
   3274   /* Allocate the structure if it has not already been allocated by a
   3275      subclass.  */
   3276   if (ret == NULL)
   3277     ret = (struct elf32_arm_link_hash_entry *)
   3278 	bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
   3279   if (ret == NULL)
   3280     return (struct bfd_hash_entry *) ret;
   3281 
   3282   /* Call the allocation method of the superclass.  */
   3283   ret = ((struct elf32_arm_link_hash_entry *)
   3284 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   3285 				     table, string));
   3286   if (ret != NULL)
   3287     {
   3288       ret->dyn_relocs = NULL;
   3289       ret->tls_type = GOT_UNKNOWN;
   3290       ret->tlsdesc_got = (bfd_vma) -1;
   3291       ret->plt.thumb_refcount = 0;
   3292       ret->plt.maybe_thumb_refcount = 0;
   3293       ret->plt.noncall_refcount = 0;
   3294       ret->plt.got_offset = -1;
   3295       ret->is_iplt = FALSE;
   3296       ret->export_glue = NULL;
   3297 
   3298       ret->stub_cache = NULL;
   3299     }
   3300 
   3301   return (struct bfd_hash_entry *) ret;
   3302 }
   3303 
   3304 /* Ensure that we have allocated bookkeeping structures for ABFD's local
   3305    symbols.  */
   3306 
   3307 static bfd_boolean
   3308 elf32_arm_allocate_local_sym_info (bfd *abfd)
   3309 {
   3310   if (elf_local_got_refcounts (abfd) == NULL)
   3311     {
   3312       bfd_size_type num_syms;
   3313       bfd_size_type size;
   3314       char *data;
   3315 
   3316       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
   3317       size = num_syms * (sizeof (bfd_signed_vma)
   3318 			 + sizeof (struct arm_local_iplt_info *)
   3319 			 + sizeof (bfd_vma)
   3320 			 + sizeof (char));
   3321       data = bfd_zalloc (abfd, size);
   3322       if (data == NULL)
   3323 	return FALSE;
   3324 
   3325       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
   3326       data += num_syms * sizeof (bfd_signed_vma);
   3327 
   3328       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
   3329       data += num_syms * sizeof (struct arm_local_iplt_info *);
   3330 
   3331       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
   3332       data += num_syms * sizeof (bfd_vma);
   3333 
   3334       elf32_arm_local_got_tls_type (abfd) = data;
   3335     }
   3336   return TRUE;
   3337 }
   3338 
   3339 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
   3340    to input bfd ABFD.  Create the information if it doesn't already exist.
   3341    Return null if an allocation fails.  */
   3342 
   3343 static struct arm_local_iplt_info *
   3344 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
   3345 {
   3346   struct arm_local_iplt_info **ptr;
   3347 
   3348   if (!elf32_arm_allocate_local_sym_info (abfd))
   3349     return NULL;
   3350 
   3351   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
   3352   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
   3353   if (*ptr == NULL)
   3354     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
   3355   return *ptr;
   3356 }
   3357 
   3358 /* Try to obtain PLT information for the symbol with index R_SYMNDX
   3359    in ABFD's symbol table.  If the symbol is global, H points to its
   3360    hash table entry, otherwise H is null.
   3361 
   3362    Return true if the symbol does have PLT information.  When returning
   3363    true, point *ROOT_PLT at the target-independent reference count/offset
   3364    union and *ARM_PLT at the ARM-specific information.  */
   3365 
   3366 static bfd_boolean
   3367 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_table *globals,
   3368 			struct elf32_arm_link_hash_entry *h,
   3369 			unsigned long r_symndx, union gotplt_union **root_plt,
   3370 			struct arm_plt_info **arm_plt)
   3371 {
   3372   struct arm_local_iplt_info *local_iplt;
   3373 
   3374   if (globals->root.splt == NULL && globals->root.iplt == NULL)
   3375     return FALSE;
   3376 
   3377   if (h != NULL)
   3378     {
   3379       *root_plt = &h->root.plt;
   3380       *arm_plt = &h->plt;
   3381       return TRUE;
   3382     }
   3383 
   3384   if (elf32_arm_local_iplt (abfd) == NULL)
   3385     return FALSE;
   3386 
   3387   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
   3388   if (local_iplt == NULL)
   3389     return FALSE;
   3390 
   3391   *root_plt = &local_iplt->root;
   3392   *arm_plt = &local_iplt->arm;
   3393   return TRUE;
   3394 }
   3395 
   3396 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
   3397    before it.  */
   3398 
   3399 static bfd_boolean
   3400 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
   3401 				  struct arm_plt_info *arm_plt)
   3402 {
   3403   struct elf32_arm_link_hash_table *htab;
   3404 
   3405   htab = elf32_arm_hash_table (info);
   3406   return (arm_plt->thumb_refcount != 0
   3407 	  || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0));
   3408 }
   3409 
   3410 /* Return a pointer to the head of the dynamic reloc list that should
   3411    be used for local symbol ISYM, which is symbol number R_SYMNDX in
   3412    ABFD's symbol table.  Return null if an error occurs.  */
   3413 
   3414 static struct elf_dyn_relocs **
   3415 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
   3416 				   Elf_Internal_Sym *isym)
   3417 {
   3418   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   3419     {
   3420       struct arm_local_iplt_info *local_iplt;
   3421 
   3422       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
   3423       if (local_iplt == NULL)
   3424 	return NULL;
   3425       return &local_iplt->dyn_relocs;
   3426     }
   3427   else
   3428     {
   3429       /* Track dynamic relocs needed for local syms too.
   3430 	 We really need local syms available to do this
   3431 	 easily.  Oh well.  */
   3432       asection *s;
   3433       void *vpp;
   3434 
   3435       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3436       if (s == NULL)
   3437 	abort ();
   3438 
   3439       vpp = &elf_section_data (s)->local_dynrel;
   3440       return (struct elf_dyn_relocs **) vpp;
   3441     }
   3442 }
   3443 
   3444 /* Initialize an entry in the stub hash table.  */
   3445 
   3446 static struct bfd_hash_entry *
   3447 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3448 		   struct bfd_hash_table *table,
   3449 		   const char *string)
   3450 {
   3451   /* Allocate the structure if it has not already been allocated by a
   3452      subclass.  */
   3453   if (entry == NULL)
   3454     {
   3455       entry = (struct bfd_hash_entry *)
   3456 	  bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
   3457       if (entry == NULL)
   3458 	return entry;
   3459     }
   3460 
   3461   /* Call the allocation method of the superclass.  */
   3462   entry = bfd_hash_newfunc (entry, table, string);
   3463   if (entry != NULL)
   3464     {
   3465       struct elf32_arm_stub_hash_entry *eh;
   3466 
   3467       /* Initialize the local fields.  */
   3468       eh = (struct elf32_arm_stub_hash_entry *) entry;
   3469       eh->stub_sec = NULL;
   3470       eh->stub_offset = (bfd_vma) -1;
   3471       eh->source_value = 0;
   3472       eh->target_value = 0;
   3473       eh->target_section = NULL;
   3474       eh->orig_insn = 0;
   3475       eh->stub_type = arm_stub_none;
   3476       eh->stub_size = 0;
   3477       eh->stub_template = NULL;
   3478       eh->stub_template_size = -1;
   3479       eh->h = NULL;
   3480       eh->id_sec = NULL;
   3481       eh->output_name = NULL;
   3482     }
   3483 
   3484   return entry;
   3485 }
   3486 
   3487 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
   3488    shortcuts to them in our hash table.  */
   3489 
   3490 static bfd_boolean
   3491 create_got_section (bfd *dynobj, struct bfd_link_info *info)
   3492 {
   3493   struct elf32_arm_link_hash_table *htab;
   3494 
   3495   htab = elf32_arm_hash_table (info);
   3496   if (htab == NULL)
   3497     return FALSE;
   3498 
   3499   /* BPABI objects never have a GOT, or associated sections.  */
   3500   if (htab->symbian_p)
   3501     return TRUE;
   3502 
   3503   if (! _bfd_elf_create_got_section (dynobj, info))
   3504     return FALSE;
   3505 
   3506   return TRUE;
   3507 }
   3508 
   3509 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
   3510 
   3511 static bfd_boolean
   3512 create_ifunc_sections (struct bfd_link_info *info)
   3513 {
   3514   struct elf32_arm_link_hash_table *htab;
   3515   const struct elf_backend_data *bed;
   3516   bfd *dynobj;
   3517   asection *s;
   3518   flagword flags;
   3519 
   3520   htab = elf32_arm_hash_table (info);
   3521   dynobj = htab->root.dynobj;
   3522   bed = get_elf_backend_data (dynobj);
   3523   flags = bed->dynamic_sec_flags;
   3524 
   3525   if (htab->root.iplt == NULL)
   3526     {
   3527       s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
   3528 					      flags | SEC_READONLY | SEC_CODE);
   3529       if (s == NULL
   3530 	  || !bfd_set_section_alignment (dynobj, s, bed->plt_alignment))
   3531 	return FALSE;
   3532       htab->root.iplt = s;
   3533     }
   3534 
   3535   if (htab->root.irelplt == NULL)
   3536     {
   3537       s = bfd_make_section_anyway_with_flags (dynobj,
   3538 					      RELOC_SECTION (htab, ".iplt"),
   3539 					      flags | SEC_READONLY);
   3540       if (s == NULL
   3541 	  || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
   3542 	return FALSE;
   3543       htab->root.irelplt = s;
   3544     }
   3545 
   3546   if (htab->root.igotplt == NULL)
   3547     {
   3548       s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
   3549       if (s == NULL
   3550 	  || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
   3551 	return FALSE;
   3552       htab->root.igotplt = s;
   3553     }
   3554   return TRUE;
   3555 }
   3556 
   3557 /* Determine if we're dealing with a Thumb only architecture.  */
   3558 
   3559 static bfd_boolean
   3560 using_thumb_only (struct elf32_arm_link_hash_table *globals)
   3561 {
   3562   int arch;
   3563   int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
   3564 					  Tag_CPU_arch_profile);
   3565 
   3566   if (profile)
   3567     return profile == 'M';
   3568 
   3569   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
   3570 
   3571   /* Force return logic to be reviewed for each new architecture.  */
   3572   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
   3573 
   3574   if (arch == TAG_CPU_ARCH_V6_M
   3575       || arch == TAG_CPU_ARCH_V6S_M
   3576       || arch == TAG_CPU_ARCH_V7E_M
   3577       || arch == TAG_CPU_ARCH_V8M_BASE
   3578       || arch == TAG_CPU_ARCH_V8M_MAIN)
   3579     return TRUE;
   3580 
   3581   return FALSE;
   3582 }
   3583 
   3584 /* Determine if we're dealing with a Thumb-2 object.  */
   3585 
   3586 static bfd_boolean
   3587 using_thumb2 (struct elf32_arm_link_hash_table *globals)
   3588 {
   3589   int arch;
   3590   int thumb_isa = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
   3591 					    Tag_THUMB_ISA_use);
   3592 
   3593   if (thumb_isa)
   3594     return thumb_isa == 2;
   3595 
   3596   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
   3597 
   3598   /* Force return logic to be reviewed for each new architecture.  */
   3599   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
   3600 
   3601   return (arch == TAG_CPU_ARCH_V6T2
   3602 	  || arch == TAG_CPU_ARCH_V7
   3603 	  || arch == TAG_CPU_ARCH_V7E_M
   3604 	  || arch == TAG_CPU_ARCH_V8
   3605 	  || arch == TAG_CPU_ARCH_V8R
   3606 	  || arch == TAG_CPU_ARCH_V8M_MAIN);
   3607 }
   3608 
   3609 /* Determine whether Thumb-2 BL instruction is available.  */
   3610 
   3611 static bfd_boolean
   3612 using_thumb2_bl (struct elf32_arm_link_hash_table *globals)
   3613 {
   3614   int arch =
   3615     bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
   3616 
   3617   /* Force return logic to be reviewed for each new architecture.  */
   3618   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
   3619 
   3620   /* Architecture was introduced after ARMv6T2 (eg. ARMv6-M).  */
   3621   return (arch == TAG_CPU_ARCH_V6T2
   3622 	  || arch >= TAG_CPU_ARCH_V7);
   3623 }
   3624 
   3625 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
   3626    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
   3627    hash table.  */
   3628 
   3629 static bfd_boolean
   3630 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   3631 {
   3632   struct elf32_arm_link_hash_table *htab;
   3633 
   3634   htab = elf32_arm_hash_table (info);
   3635   if (htab == NULL)
   3636     return FALSE;
   3637 
   3638   if (!htab->root.sgot && !create_got_section (dynobj, info))
   3639     return FALSE;
   3640 
   3641   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   3642     return FALSE;
   3643 
   3644   if (htab->vxworks_p)
   3645     {
   3646       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
   3647 	return FALSE;
   3648 
   3649       if (bfd_link_pic (info))
   3650 	{
   3651 	  htab->plt_header_size = 0;
   3652 	  htab->plt_entry_size
   3653 	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
   3654 	}
   3655       else
   3656 	{
   3657 	  htab->plt_header_size
   3658 	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
   3659 	  htab->plt_entry_size
   3660 	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
   3661 	}
   3662 
   3663       if (elf_elfheader (dynobj))
   3664 	elf_elfheader (dynobj)->e_ident[EI_CLASS] = ELFCLASS32;
   3665     }
   3666   else
   3667     {
   3668       /* PR ld/16017
   3669 	 Test for thumb only architectures.  Note - we cannot just call
   3670 	 using_thumb_only() as the attributes in the output bfd have not been
   3671 	 initialised at this point, so instead we use the input bfd.  */
   3672       bfd * saved_obfd = htab->obfd;
   3673 
   3674       htab->obfd = dynobj;
   3675       if (using_thumb_only (htab))
   3676 	{
   3677 	  htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
   3678 	  htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
   3679 	}
   3680       htab->obfd = saved_obfd;
   3681     }
   3682 
   3683   if (!htab->root.splt
   3684       || !htab->root.srelplt
   3685       || !htab->root.sdynbss
   3686       || (!bfd_link_pic (info) && !htab->root.srelbss))
   3687     abort ();
   3688 
   3689   return TRUE;
   3690 }
   3691 
   3692 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   3693 
   3694 static void
   3695 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
   3696 				struct elf_link_hash_entry *dir,
   3697 				struct elf_link_hash_entry *ind)
   3698 {
   3699   struct elf32_arm_link_hash_entry *edir, *eind;
   3700 
   3701   edir = (struct elf32_arm_link_hash_entry *) dir;
   3702   eind = (struct elf32_arm_link_hash_entry *) ind;
   3703 
   3704   if (eind->dyn_relocs != NULL)
   3705     {
   3706       if (edir->dyn_relocs != NULL)
   3707 	{
   3708 	  struct elf_dyn_relocs **pp;
   3709 	  struct elf_dyn_relocs *p;
   3710 
   3711 	  /* Add reloc counts against the indirect sym to the direct sym
   3712 	     list.  Merge any entries against the same section.  */
   3713 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   3714 	    {
   3715 	      struct elf_dyn_relocs *q;
   3716 
   3717 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   3718 		if (q->sec == p->sec)
   3719 		  {
   3720 		    q->pc_count += p->pc_count;
   3721 		    q->count += p->count;
   3722 		    *pp = p->next;
   3723 		    break;
   3724 		  }
   3725 	      if (q == NULL)
   3726 		pp = &p->next;
   3727 	    }
   3728 	  *pp = edir->dyn_relocs;
   3729 	}
   3730 
   3731       edir->dyn_relocs = eind->dyn_relocs;
   3732       eind->dyn_relocs = NULL;
   3733     }
   3734 
   3735   if (ind->root.type == bfd_link_hash_indirect)
   3736     {
   3737       /* Copy over PLT info.  */
   3738       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
   3739       eind->plt.thumb_refcount = 0;
   3740       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
   3741       eind->plt.maybe_thumb_refcount = 0;
   3742       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
   3743       eind->plt.noncall_refcount = 0;
   3744 
   3745       /* We should only allocate a function to .iplt once the final
   3746 	 symbol information is known.  */
   3747       BFD_ASSERT (!eind->is_iplt);
   3748 
   3749       if (dir->got.refcount <= 0)
   3750 	{
   3751 	  edir->tls_type = eind->tls_type;
   3752 	  eind->tls_type = GOT_UNKNOWN;
   3753 	}
   3754     }
   3755 
   3756   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   3757 }
   3758 
   3759 /* Destroy an ARM elf linker hash table.  */
   3760 
   3761 static void
   3762 elf32_arm_link_hash_table_free (bfd *obfd)
   3763 {
   3764   struct elf32_arm_link_hash_table *ret
   3765     = (struct elf32_arm_link_hash_table *) obfd->link.hash;
   3766 
   3767   bfd_hash_table_free (&ret->stub_hash_table);
   3768   _bfd_elf_link_hash_table_free (obfd);
   3769 }
   3770 
   3771 /* Create an ARM elf linker hash table.  */
   3772 
   3773 static struct bfd_link_hash_table *
   3774 elf32_arm_link_hash_table_create (bfd *abfd)
   3775 {
   3776   struct elf32_arm_link_hash_table *ret;
   3777   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
   3778 
   3779   ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
   3780   if (ret == NULL)
   3781     return NULL;
   3782 
   3783   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
   3784 				      elf32_arm_link_hash_newfunc,
   3785 				      sizeof (struct elf32_arm_link_hash_entry),
   3786 				      ARM_ELF_DATA))
   3787     {
   3788       free (ret);
   3789       return NULL;
   3790     }
   3791 
   3792   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
   3793   ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
   3794 #ifdef FOUR_WORD_PLT
   3795   ret->plt_header_size = 16;
   3796   ret->plt_entry_size = 16;
   3797 #else
   3798   ret->plt_header_size = 20;
   3799   ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
   3800 #endif
   3801   ret->use_rel = 1;
   3802   ret->obfd = abfd;
   3803 
   3804   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
   3805 			    sizeof (struct elf32_arm_stub_hash_entry)))
   3806     {
   3807       _bfd_elf_link_hash_table_free (abfd);
   3808       return NULL;
   3809     }
   3810   ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
   3811 
   3812   return &ret->root.root;
   3813 }
   3814 
   3815 /* Determine what kind of NOPs are available.  */
   3816 
   3817 static bfd_boolean
   3818 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
   3819 {
   3820   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
   3821 					     Tag_CPU_arch);
   3822 
   3823   /* Force return logic to be reviewed for each new architecture.  */
   3824   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
   3825 
   3826   return (arch == TAG_CPU_ARCH_V6T2
   3827 	  || arch == TAG_CPU_ARCH_V6K
   3828 	  || arch == TAG_CPU_ARCH_V7
   3829 	  || arch == TAG_CPU_ARCH_V8
   3830 	  || arch == TAG_CPU_ARCH_V8R);
   3831 }
   3832 
   3833 static bfd_boolean
   3834 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
   3835 {
   3836   switch (stub_type)
   3837     {
   3838     case arm_stub_long_branch_thumb_only:
   3839     case arm_stub_long_branch_thumb2_only:
   3840     case arm_stub_long_branch_thumb2_only_pure:
   3841     case arm_stub_long_branch_v4t_thumb_arm:
   3842     case arm_stub_short_branch_v4t_thumb_arm:
   3843     case arm_stub_long_branch_v4t_thumb_arm_pic:
   3844     case arm_stub_long_branch_v4t_thumb_tls_pic:
   3845     case arm_stub_long_branch_thumb_only_pic:
   3846     case arm_stub_cmse_branch_thumb_only:
   3847       return TRUE;
   3848     case arm_stub_none:
   3849       BFD_FAIL ();
   3850       return FALSE;
   3851       break;
   3852     default:
   3853       return FALSE;
   3854     }
   3855 }
   3856 
   3857 /* Determine the type of stub needed, if any, for a call.  */
   3858 
   3859 static enum elf32_arm_stub_type
   3860 arm_type_of_stub (struct bfd_link_info *info,
   3861 		  asection *input_sec,
   3862 		  const Elf_Internal_Rela *rel,
   3863 		  unsigned char st_type,
   3864 		  enum arm_st_branch_type *actual_branch_type,
   3865 		  struct elf32_arm_link_hash_entry *hash,
   3866 		  bfd_vma destination,
   3867 		  asection *sym_sec,
   3868 		  bfd *input_bfd,
   3869 		  const char *name)
   3870 {
   3871   bfd_vma location;
   3872   bfd_signed_vma branch_offset;
   3873   unsigned int r_type;
   3874   struct elf32_arm_link_hash_table * globals;
   3875   bfd_boolean thumb2, thumb2_bl, thumb_only;
   3876   enum elf32_arm_stub_type stub_type = arm_stub_none;
   3877   int use_plt = 0;
   3878   enum arm_st_branch_type branch_type = *actual_branch_type;
   3879   union gotplt_union *root_plt;
   3880   struct arm_plt_info *arm_plt;
   3881   int arch;
   3882   int thumb2_movw;
   3883 
   3884   if (branch_type == ST_BRANCH_LONG)
   3885     return stub_type;
   3886 
   3887   globals = elf32_arm_hash_table (info);
   3888   if (globals == NULL)
   3889     return stub_type;
   3890 
   3891   thumb_only = using_thumb_only (globals);
   3892   thumb2 = using_thumb2 (globals);
   3893   thumb2_bl = using_thumb2_bl (globals);
   3894 
   3895   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
   3896 
   3897   /* True for architectures that implement the thumb2 movw instruction.  */
   3898   thumb2_movw = thumb2 || (arch  == TAG_CPU_ARCH_V8M_BASE);
   3899 
   3900   /* Determine where the call point is.  */
   3901   location = (input_sec->output_offset
   3902 	      + input_sec->output_section->vma
   3903 	      + rel->r_offset);
   3904 
   3905   r_type = ELF32_R_TYPE (rel->r_info);
   3906 
   3907   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
   3908      are considering a function call relocation.  */
   3909   if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
   3910 		     || r_type == R_ARM_THM_JUMP19)
   3911       && branch_type == ST_BRANCH_TO_ARM)
   3912     branch_type = ST_BRANCH_TO_THUMB;
   3913 
   3914   /* For TLS call relocs, it is the caller's responsibility to provide
   3915      the address of the appropriate trampoline.  */
   3916   if (r_type != R_ARM_TLS_CALL
   3917       && r_type != R_ARM_THM_TLS_CALL
   3918       && elf32_arm_get_plt_info (input_bfd, globals, hash,
   3919 				 ELF32_R_SYM (rel->r_info), &root_plt,
   3920 				 &arm_plt)
   3921       && root_plt->offset != (bfd_vma) -1)
   3922     {
   3923       asection *splt;
   3924 
   3925       if (hash == NULL || hash->is_iplt)
   3926 	splt = globals->root.iplt;
   3927       else
   3928 	splt = globals->root.splt;
   3929       if (splt != NULL)
   3930 	{
   3931 	  use_plt = 1;
   3932 
   3933 	  /* Note when dealing with PLT entries: the main PLT stub is in
   3934 	     ARM mode, so if the branch is in Thumb mode, another
   3935 	     Thumb->ARM stub will be inserted later just before the ARM
   3936 	     PLT stub. If a long branch stub is needed, we'll add a
   3937 	     Thumb->Arm one and branch directly to the ARM PLT entry.
   3938 	     Here, we have to check if a pre-PLT Thumb->ARM stub
   3939 	     is needed and if it will be close enough.  */
   3940 
   3941 	  destination = (splt->output_section->vma
   3942 			 + splt->output_offset
   3943 			 + root_plt->offset);
   3944 	  st_type = STT_FUNC;
   3945 
   3946 	  /* Thumb branch/call to PLT: it can become a branch to ARM
   3947 	     or to Thumb. We must perform the same checks and
   3948 	     corrections as in elf32_arm_final_link_relocate.  */
   3949 	  if ((r_type == R_ARM_THM_CALL)
   3950 	      || (r_type == R_ARM_THM_JUMP24))
   3951 	    {
   3952 	      if (globals->use_blx
   3953 		  && r_type == R_ARM_THM_CALL
   3954 		  && !thumb_only)
   3955 		{
   3956 		  /* If the Thumb BLX instruction is available, convert
   3957 		     the BL to a BLX instruction to call the ARM-mode
   3958 		     PLT entry.  */
   3959 		  branch_type = ST_BRANCH_TO_ARM;
   3960 		}
   3961 	      else
   3962 		{
   3963 		  if (!thumb_only)
   3964 		    /* Target the Thumb stub before the ARM PLT entry.  */
   3965 		    destination -= PLT_THUMB_STUB_SIZE;
   3966 		  branch_type = ST_BRANCH_TO_THUMB;
   3967 		}
   3968 	    }
   3969 	  else
   3970 	    {
   3971 	      branch_type = ST_BRANCH_TO_ARM;
   3972 	    }
   3973 	}
   3974     }
   3975   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
   3976   BFD_ASSERT (st_type != STT_GNU_IFUNC);
   3977 
   3978   branch_offset = (bfd_signed_vma)(destination - location);
   3979 
   3980   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
   3981       || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
   3982     {
   3983       /* Handle cases where:
   3984 	 - this call goes too far (different Thumb/Thumb2 max
   3985 	   distance)
   3986 	 - it's a Thumb->Arm call and blx is not available, or it's a
   3987 	   Thumb->Arm branch (not bl). A stub is needed in this case,
   3988 	   but only if this call is not through a PLT entry. Indeed,
   3989 	   PLT stubs handle mode switching already.  */
   3990       if ((!thumb2_bl
   3991 	    && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
   3992 		|| (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
   3993 	  || (thumb2_bl
   3994 	      && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
   3995 		  || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
   3996 	  || (thumb2
   3997 	      && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
   3998 		  || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
   3999 	      && (r_type == R_ARM_THM_JUMP19))
   4000 	  || (branch_type == ST_BRANCH_TO_ARM
   4001 	      && (((r_type == R_ARM_THM_CALL
   4002 		    || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
   4003 		  || (r_type == R_ARM_THM_JUMP24)
   4004 		  || (r_type == R_ARM_THM_JUMP19))
   4005 	      && !use_plt))
   4006 	{
   4007 	  /* If we need to insert a Thumb-Thumb long branch stub to a
   4008 	     PLT, use one that branches directly to the ARM PLT
   4009 	     stub. If we pretended we'd use the pre-PLT Thumb->ARM
   4010 	     stub, undo this now.  */
   4011 	  if ((branch_type == ST_BRANCH_TO_THUMB) && use_plt && !thumb_only)
   4012 	    {
   4013 	      branch_type = ST_BRANCH_TO_ARM;
   4014 	      branch_offset += PLT_THUMB_STUB_SIZE;
   4015 	    }
   4016 
   4017 	  if (branch_type == ST_BRANCH_TO_THUMB)
   4018 	    {
   4019 	      /* Thumb to thumb.  */
   4020 	      if (!thumb_only)
   4021 		{
   4022 		  if (input_sec->flags & SEC_ELF_PURECODE)
   4023 		    _bfd_error_handler
   4024 		      (_("%B(%A): warning: long branch veneers used in"
   4025 			 " section with SHF_ARM_PURECODE section"
   4026 			 " attribute is only supported for M-profile"
   4027 			 " targets that implement the movw instruction."),
   4028 		       input_bfd, input_sec);
   4029 
   4030 		  stub_type = (bfd_link_pic (info) | globals->pic_veneer)
   4031 		    /* PIC stubs.  */
   4032 		    ? ((globals->use_blx
   4033 			&& (r_type == R_ARM_THM_CALL))
   4034 		       /* V5T and above. Stub starts with ARM code, so
   4035 			  we must be able to switch mode before
   4036 			  reaching it, which is only possible for 'bl'
   4037 			  (ie R_ARM_THM_CALL relocation).  */
   4038 		       ? arm_stub_long_branch_any_thumb_pic
   4039 		       /* On V4T, use Thumb code only.  */
   4040 		       : arm_stub_long_branch_v4t_thumb_thumb_pic)
   4041 
   4042 		    /* non-PIC stubs.  */
   4043 		    : ((globals->use_blx
   4044 			&& (r_type == R_ARM_THM_CALL))
   4045 		       /* V5T and above.  */
   4046 		       ? arm_stub_long_branch_any_any
   4047 		       /* V4T.  */
   4048 		       : arm_stub_long_branch_v4t_thumb_thumb);
   4049 		}
   4050 	      else
   4051 		{
   4052 		  if (thumb2_movw && (input_sec->flags & SEC_ELF_PURECODE))
   4053 		      stub_type = arm_stub_long_branch_thumb2_only_pure;
   4054 		  else
   4055 		    {
   4056 		      if (input_sec->flags & SEC_ELF_PURECODE)
   4057 			_bfd_error_handler
   4058 			  (_("%B(%A): warning: long branch veneers used in"
   4059 			     " section with SHF_ARM_PURECODE section"
   4060 			     " attribute is only supported for M-profile"
   4061 			     " targets that implement the movw instruction."),
   4062 			   input_bfd, input_sec);
   4063 
   4064 		      stub_type = (bfd_link_pic (info) | globals->pic_veneer)
   4065 			/* PIC stub.  */
   4066 			? arm_stub_long_branch_thumb_only_pic
   4067 			/* non-PIC stub.  */
   4068 			: (thumb2 ? arm_stub_long_branch_thumb2_only
   4069 				  : arm_stub_long_branch_thumb_only);
   4070 		    }
   4071 		}
   4072 	    }
   4073 	  else
   4074 	    {
   4075 	      if (input_sec->flags & SEC_ELF_PURECODE)
   4076 		_bfd_error_handler
   4077 		  (_("%B(%A): warning: long branch veneers used in"
   4078 		     " section with SHF_ARM_PURECODE section"
   4079 		     " attribute is only supported" " for M-profile"
   4080 		     " targets that implement the movw instruction."),
   4081 		   input_bfd, input_sec);
   4082 
   4083 	      /* Thumb to arm.  */
   4084 	      if (sym_sec != NULL
   4085 		  && sym_sec->owner != NULL
   4086 		  && !INTERWORK_FLAG (sym_sec->owner))
   4087 		{
   4088 		  _bfd_error_handler
   4089 		    (_("%B(%s): warning: interworking not enabled.\n"
   4090 		       "  first occurrence: %B: Thumb call to ARM"),
   4091 		     sym_sec->owner, name, input_bfd);
   4092 		}
   4093 
   4094 	      stub_type =
   4095 		(bfd_link_pic (info) | globals->pic_veneer)
   4096 		/* PIC stubs.  */
   4097 		? (r_type == R_ARM_THM_TLS_CALL
   4098 		   /* TLS PIC stubs.  */
   4099 		   ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
   4100 		      : arm_stub_long_branch_v4t_thumb_tls_pic)
   4101 		   : ((globals->use_blx && r_type == R_ARM_THM_CALL)
   4102 		      /* V5T PIC and above.  */
   4103 		      ? arm_stub_long_branch_any_arm_pic
   4104 		      /* V4T PIC stub.  */
   4105 		      : arm_stub_long_branch_v4t_thumb_arm_pic))
   4106 
   4107 		/* non-PIC stubs.  */
   4108 		: ((globals->use_blx && r_type == R_ARM_THM_CALL)
   4109 		   /* V5T and above.  */
   4110 		   ? arm_stub_long_branch_any_any
   4111 		   /* V4T.  */
   4112 		   : arm_stub_long_branch_v4t_thumb_arm);
   4113 
   4114 	      /* Handle v4t short branches.  */
   4115 	      if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
   4116 		  && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
   4117 		  && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
   4118 		stub_type = arm_stub_short_branch_v4t_thumb_arm;
   4119 	    }
   4120 	}
   4121     }
   4122   else if (r_type == R_ARM_CALL
   4123 	   || r_type == R_ARM_JUMP24
   4124 	   || r_type == R_ARM_PLT32
   4125 	   || r_type == R_ARM_TLS_CALL)
   4126     {
   4127       if (input_sec->flags & SEC_ELF_PURECODE)
   4128 	_bfd_error_handler
   4129 	  (_("%B(%A): warning: long branch veneers used in"
   4130 	     " section with SHF_ARM_PURECODE section"
   4131 	     " attribute is only supported for M-profile"
   4132 	     " targets that implement the movw instruction."),
   4133 	   input_bfd, input_sec);
   4134       if (branch_type == ST_BRANCH_TO_THUMB)
   4135 	{
   4136 	  /* Arm to thumb.  */
   4137 
   4138 	  if (sym_sec != NULL
   4139 	      && sym_sec->owner != NULL
   4140 	      && !INTERWORK_FLAG (sym_sec->owner))
   4141 	    {
   4142 	      _bfd_error_handler
   4143 		(_("%B(%s): warning: interworking not enabled.\n"
   4144 		   "  first occurrence: %B: ARM call to Thumb"),
   4145 		 sym_sec->owner, name, input_bfd);
   4146 	    }
   4147 
   4148 	  /* We have an extra 2-bytes reach because of
   4149 	     the mode change (bit 24 (H) of BLX encoding).  */
   4150 	  if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
   4151 	      || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
   4152 	      || (r_type == R_ARM_CALL && !globals->use_blx)
   4153 	      || (r_type == R_ARM_JUMP24)
   4154 	      || (r_type == R_ARM_PLT32))
   4155 	    {
   4156 	      stub_type = (bfd_link_pic (info) | globals->pic_veneer)
   4157 		/* PIC stubs.  */
   4158 		? ((globals->use_blx)
   4159 		   /* V5T and above.  */
   4160 		   ? arm_stub_long_branch_any_thumb_pic
   4161 		   /* V4T stub.  */
   4162 		   : arm_stub_long_branch_v4t_arm_thumb_pic)
   4163 
   4164 		/* non-PIC stubs.  */
   4165 		: ((globals->use_blx)
   4166 		   /* V5T and above.  */
   4167 		   ? arm_stub_long_branch_any_any
   4168 		   /* V4T.  */
   4169 		   : arm_stub_long_branch_v4t_arm_thumb);
   4170 	    }
   4171 	}
   4172       else
   4173 	{
   4174 	  /* Arm to arm.  */
   4175 	  if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
   4176 	      || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
   4177 	    {
   4178 	      stub_type =
   4179 		(bfd_link_pic (info) | globals->pic_veneer)
   4180 		/* PIC stubs.  */
   4181 		? (r_type == R_ARM_TLS_CALL
   4182 		   /* TLS PIC Stub.  */
   4183 		   ? arm_stub_long_branch_any_tls_pic
   4184 		   : (globals->nacl_p
   4185 		      ? arm_stub_long_branch_arm_nacl_pic
   4186 		      : arm_stub_long_branch_any_arm_pic))
   4187 		/* non-PIC stubs.  */
   4188 		: (globals->nacl_p
   4189 		   ? arm_stub_long_branch_arm_nacl
   4190 		   : arm_stub_long_branch_any_any);
   4191 	    }
   4192 	}
   4193     }
   4194 
   4195   /* If a stub is needed, record the actual destination type.  */
   4196   if (stub_type != arm_stub_none)
   4197     *actual_branch_type = branch_type;
   4198 
   4199   return stub_type;
   4200 }
   4201 
   4202 /* Build a name for an entry in the stub hash table.  */
   4203 
   4204 static char *
   4205 elf32_arm_stub_name (const asection *input_section,
   4206 		     const asection *sym_sec,
   4207 		     const struct elf32_arm_link_hash_entry *hash,
   4208 		     const Elf_Internal_Rela *rel,
   4209 		     enum elf32_arm_stub_type stub_type)
   4210 {
   4211   char *stub_name;
   4212   bfd_size_type len;
   4213 
   4214   if (hash)
   4215     {
   4216       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
   4217       stub_name = (char *) bfd_malloc (len);
   4218       if (stub_name != NULL)
   4219 	sprintf (stub_name, "%08x_%s+%x_%d",
   4220 		 input_section->id & 0xffffffff,
   4221 		 hash->root.root.root.string,
   4222 		 (int) rel->r_addend & 0xffffffff,
   4223 		 (int) stub_type);
   4224     }
   4225   else
   4226     {
   4227       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
   4228       stub_name = (char *) bfd_malloc (len);
   4229       if (stub_name != NULL)
   4230 	sprintf (stub_name, "%08x_%x:%x+%x_%d",
   4231 		 input_section->id & 0xffffffff,
   4232 		 sym_sec->id & 0xffffffff,
   4233 		 ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
   4234 		 || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
   4235 		 ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
   4236 		 (int) rel->r_addend & 0xffffffff,
   4237 		 (int) stub_type);
   4238     }
   4239 
   4240   return stub_name;
   4241 }
   4242 
   4243 /* Look up an entry in the stub hash.  Stub entries are cached because
   4244    creating the stub name takes a bit of time.  */
   4245 
   4246 static struct elf32_arm_stub_hash_entry *
   4247 elf32_arm_get_stub_entry (const asection *input_section,
   4248 			  const asection *sym_sec,
   4249 			  struct elf_link_hash_entry *hash,
   4250 			  const Elf_Internal_Rela *rel,
   4251 			  struct elf32_arm_link_hash_table *htab,
   4252 			  enum elf32_arm_stub_type stub_type)
   4253 {
   4254   struct elf32_arm_stub_hash_entry *stub_entry;
   4255   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
   4256   const asection *id_sec;
   4257 
   4258   if ((input_section->flags & SEC_CODE) == 0)
   4259     return NULL;
   4260 
   4261   /* If this input section is part of a group of sections sharing one
   4262      stub section, then use the id of the first section in the group.
   4263      Stub names need to include a section id, as there may well be
   4264      more than one stub used to reach say, printf, and we need to
   4265      distinguish between them.  */
   4266   BFD_ASSERT (input_section->id <= htab->top_id);
   4267   id_sec = htab->stub_group[input_section->id].link_sec;
   4268 
   4269   if (h != NULL && h->stub_cache != NULL
   4270       && h->stub_cache->h == h
   4271       && h->stub_cache->id_sec == id_sec
   4272       && h->stub_cache->stub_type == stub_type)
   4273     {
   4274       stub_entry = h->stub_cache;
   4275     }
   4276   else
   4277     {
   4278       char *stub_name;
   4279 
   4280       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
   4281       if (stub_name == NULL)
   4282 	return NULL;
   4283 
   4284       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
   4285 					stub_name, FALSE, FALSE);
   4286       if (h != NULL)
   4287 	h->stub_cache = stub_entry;
   4288 
   4289       free (stub_name);
   4290     }
   4291 
   4292   return stub_entry;
   4293 }
   4294 
   4295 /* Whether veneers of type STUB_TYPE require to be in a dedicated output
   4296    section.  */
   4297 
   4298 static bfd_boolean
   4299 arm_dedicated_stub_output_section_required (enum elf32_arm_stub_type stub_type)
   4300 {
   4301   if (stub_type >= max_stub_type)
   4302     abort ();  /* Should be unreachable.  */
   4303 
   4304   switch (stub_type)
   4305     {
   4306     case arm_stub_cmse_branch_thumb_only:
   4307       return TRUE;
   4308 
   4309     default:
   4310       return FALSE;
   4311     }
   4312 
   4313   abort ();  /* Should be unreachable.  */
   4314 }
   4315 
   4316 /* Required alignment (as a power of 2) for the dedicated section holding
   4317    veneers of type STUB_TYPE, or 0 if veneers of this type are interspersed
   4318    with input sections.  */
   4319 
   4320 static int
   4321 arm_dedicated_stub_output_section_required_alignment
   4322   (enum elf32_arm_stub_type stub_type)
   4323 {
   4324   if (stub_type >= max_stub_type)
   4325     abort ();  /* Should be unreachable.  */
   4326 
   4327   switch (stub_type)
   4328     {
   4329     /* Vectors of Secure Gateway veneers must be aligned on 32byte
   4330        boundary.  */
   4331     case arm_stub_cmse_branch_thumb_only:
   4332       return 5;
   4333 
   4334     default:
   4335       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
   4336       return 0;
   4337     }
   4338 
   4339   abort ();  /* Should be unreachable.  */
   4340 }
   4341 
   4342 /* Name of the dedicated output section to put veneers of type STUB_TYPE, or
   4343    NULL if veneers of this type are interspersed with input sections.  */
   4344 
   4345 static const char *
   4346 arm_dedicated_stub_output_section_name (enum elf32_arm_stub_type stub_type)
   4347 {
   4348   if (stub_type >= max_stub_type)
   4349     abort ();  /* Should be unreachable.  */
   4350 
   4351   switch (stub_type)
   4352     {
   4353     case arm_stub_cmse_branch_thumb_only:
   4354       return ".gnu.sgstubs";
   4355 
   4356     default:
   4357       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
   4358       return NULL;
   4359     }
   4360 
   4361   abort ();  /* Should be unreachable.  */
   4362 }
   4363 
   4364 /* If veneers of type STUB_TYPE should go in a dedicated output section,
   4365    returns the address of the hash table field in HTAB holding a pointer to the
   4366    corresponding input section.  Otherwise, returns NULL.  */
   4367 
   4368 static asection **
   4369 arm_dedicated_stub_input_section_ptr (struct elf32_arm_link_hash_table *htab,
   4370 				      enum elf32_arm_stub_type stub_type)
   4371 {
   4372   if (stub_type >= max_stub_type)
   4373     abort ();  /* Should be unreachable.  */
   4374 
   4375   switch (stub_type)
   4376     {
   4377     case arm_stub_cmse_branch_thumb_only:
   4378       return &htab->cmse_stub_sec;
   4379 
   4380     default:
   4381       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
   4382       return NULL;
   4383     }
   4384 
   4385   abort ();  /* Should be unreachable.  */
   4386 }
   4387 
   4388 /* Find or create a stub section to contain a stub of type STUB_TYPE.  SECTION
   4389    is the section that branch into veneer and can be NULL if stub should go in
   4390    a dedicated output section.  Returns a pointer to the stub section, and the
   4391    section to which the stub section will be attached (in *LINK_SEC_P).
   4392    LINK_SEC_P may be NULL.  */
   4393 
   4394 static asection *
   4395 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
   4396 				   struct elf32_arm_link_hash_table *htab,
   4397 				   enum elf32_arm_stub_type stub_type)
   4398 {
   4399   asection *link_sec, *out_sec, **stub_sec_p;
   4400   const char *stub_sec_prefix;
   4401   bfd_boolean dedicated_output_section =
   4402     arm_dedicated_stub_output_section_required (stub_type);
   4403   int align;
   4404 
   4405   if (dedicated_output_section)
   4406     {
   4407       bfd *output_bfd = htab->obfd;
   4408       const char *out_sec_name =
   4409 	arm_dedicated_stub_output_section_name (stub_type);
   4410       link_sec = NULL;
   4411       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
   4412       stub_sec_prefix = out_sec_name;
   4413       align = arm_dedicated_stub_output_section_required_alignment (stub_type);
   4414       out_sec = bfd_get_section_by_name (output_bfd, out_sec_name);
   4415       if (out_sec == NULL)
   4416 	{
   4417 	  _bfd_error_handler (_("No address assigned to the veneers output "
   4418 				"section %s"), out_sec_name);
   4419 	  return NULL;
   4420 	}
   4421     }
   4422   else
   4423     {
   4424       BFD_ASSERT (section->id <= htab->top_id);
   4425       link_sec = htab->stub_group[section->id].link_sec;
   4426       BFD_ASSERT (link_sec != NULL);
   4427       stub_sec_p = &htab->stub_group[section->id].stub_sec;
   4428       if (*stub_sec_p == NULL)
   4429 	stub_sec_p = &htab->stub_group[link_sec->id].stub_sec;
   4430       stub_sec_prefix = link_sec->name;
   4431       out_sec = link_sec->output_section;
   4432       align = htab->nacl_p ? 4 : 3;
   4433     }
   4434 
   4435   if (*stub_sec_p == NULL)
   4436     {
   4437       size_t namelen;
   4438       bfd_size_type len;
   4439       char *s_name;
   4440 
   4441       namelen = strlen (stub_sec_prefix);
   4442       len = namelen + sizeof (STUB_SUFFIX);
   4443       s_name = (char *) bfd_alloc (htab->stub_bfd, len);
   4444       if (s_name == NULL)
   4445 	return NULL;
   4446 
   4447       memcpy (s_name, stub_sec_prefix, namelen);
   4448       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4449       *stub_sec_p = (*htab->add_stub_section) (s_name, out_sec, link_sec,
   4450 					       align);
   4451       if (*stub_sec_p == NULL)
   4452 	return NULL;
   4453 
   4454       out_sec->flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
   4455 			| SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY
   4456 			| SEC_KEEP;
   4457     }
   4458 
   4459   if (!dedicated_output_section)
   4460     htab->stub_group[section->id].stub_sec = *stub_sec_p;
   4461 
   4462   if (link_sec_p)
   4463     *link_sec_p = link_sec;
   4464 
   4465   return *stub_sec_p;
   4466 }
   4467 
   4468 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4469    stub entry are initialised.  */
   4470 
   4471 static struct elf32_arm_stub_hash_entry *
   4472 elf32_arm_add_stub (const char *stub_name, asection *section,
   4473 		    struct elf32_arm_link_hash_table *htab,
   4474 		    enum elf32_arm_stub_type stub_type)
   4475 {
   4476   asection *link_sec;
   4477   asection *stub_sec;
   4478   struct elf32_arm_stub_hash_entry *stub_entry;
   4479 
   4480   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab,
   4481 						stub_type);
   4482   if (stub_sec == NULL)
   4483     return NULL;
   4484 
   4485   /* Enter this entry into the linker stub hash table.  */
   4486   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4487 				     TRUE, FALSE);
   4488   if (stub_entry == NULL)
   4489     {
   4490       if (section == NULL)
   4491 	section = stub_sec;
   4492       _bfd_error_handler (_("%B: cannot create stub entry %s"),
   4493 			  section->owner, stub_name);
   4494       return NULL;
   4495     }
   4496 
   4497   stub_entry->stub_sec = stub_sec;
   4498   stub_entry->stub_offset = (bfd_vma) -1;
   4499   stub_entry->id_sec = link_sec;
   4500 
   4501   return stub_entry;
   4502 }
   4503 
   4504 /* Store an Arm insn into an output section not processed by
   4505    elf32_arm_write_section.  */
   4506 
   4507 static void
   4508 put_arm_insn (struct elf32_arm_link_hash_table * htab,
   4509 	      bfd * output_bfd, bfd_vma val, void * ptr)
   4510 {
   4511   if (htab->byteswap_code != bfd_little_endian (output_bfd))
   4512     bfd_putl32 (val, ptr);
   4513   else
   4514     bfd_putb32 (val, ptr);
   4515 }
   4516 
   4517 /* Store a 16-bit Thumb insn into an output section not processed by
   4518    elf32_arm_write_section.  */
   4519 
   4520 static void
   4521 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
   4522 		bfd * output_bfd, bfd_vma val, void * ptr)
   4523 {
   4524   if (htab->byteswap_code != bfd_little_endian (output_bfd))
   4525     bfd_putl16 (val, ptr);
   4526   else
   4527     bfd_putb16 (val, ptr);
   4528 }
   4529 
   4530 /* Store a Thumb2 insn into an output section not processed by
   4531    elf32_arm_write_section.  */
   4532 
   4533 static void
   4534 put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
   4535 		 bfd * output_bfd, bfd_vma val, bfd_byte * ptr)
   4536 {
   4537   /* T2 instructions are 16-bit streamed.  */
   4538   if (htab->byteswap_code != bfd_little_endian (output_bfd))
   4539     {
   4540       bfd_putl16 ((val >> 16) & 0xffff, ptr);
   4541       bfd_putl16 ((val & 0xffff), ptr + 2);
   4542     }
   4543   else
   4544     {
   4545       bfd_putb16 ((val >> 16) & 0xffff, ptr);
   4546       bfd_putb16 ((val & 0xffff), ptr + 2);
   4547     }
   4548 }
   4549 
   4550 /* If it's possible to change R_TYPE to a more efficient access
   4551    model, return the new reloc type.  */
   4552 
   4553 static unsigned
   4554 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
   4555 			  struct elf_link_hash_entry *h)
   4556 {
   4557   int is_local = (h == NULL);
   4558 
   4559   if (bfd_link_pic (info)
   4560       || (h && h->root.type == bfd_link_hash_undefweak))
   4561     return r_type;
   4562 
   4563   /* We do not support relaxations for Old TLS models.  */
   4564   switch (r_type)
   4565     {
   4566     case R_ARM_TLS_GOTDESC:
   4567     case R_ARM_TLS_CALL:
   4568     case R_ARM_THM_TLS_CALL:
   4569     case R_ARM_TLS_DESCSEQ:
   4570     case R_ARM_THM_TLS_DESCSEQ:
   4571       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
   4572     }
   4573 
   4574   return r_type;
   4575 }
   4576 
   4577 static bfd_reloc_status_type elf32_arm_final_link_relocate
   4578   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
   4579    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
   4580    const char *, unsigned char, enum arm_st_branch_type,
   4581    struct elf_link_hash_entry *, bfd_boolean *, char **);
   4582 
   4583 static unsigned int
   4584 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
   4585 {
   4586   switch (stub_type)
   4587     {
   4588     case arm_stub_a8_veneer_b_cond:
   4589     case arm_stub_a8_veneer_b:
   4590     case arm_stub_a8_veneer_bl:
   4591       return 2;
   4592 
   4593     case arm_stub_long_branch_any_any:
   4594     case arm_stub_long_branch_v4t_arm_thumb:
   4595     case arm_stub_long_branch_thumb_only:
   4596     case arm_stub_long_branch_thumb2_only:
   4597     case arm_stub_long_branch_thumb2_only_pure:
   4598     case arm_stub_long_branch_v4t_thumb_thumb:
   4599     case arm_stub_long_branch_v4t_thumb_arm:
   4600     case arm_stub_short_branch_v4t_thumb_arm:
   4601     case arm_stub_long_branch_any_arm_pic:
   4602     case arm_stub_long_branch_any_thumb_pic:
   4603     case arm_stub_long_branch_v4t_thumb_thumb_pic:
   4604     case arm_stub_long_branch_v4t_arm_thumb_pic:
   4605     case arm_stub_long_branch_v4t_thumb_arm_pic:
   4606     case arm_stub_long_branch_thumb_only_pic:
   4607     case arm_stub_long_branch_any_tls_pic:
   4608     case arm_stub_long_branch_v4t_thumb_tls_pic:
   4609     case arm_stub_cmse_branch_thumb_only:
   4610     case arm_stub_a8_veneer_blx:
   4611       return 4;
   4612 
   4613     case arm_stub_long_branch_arm_nacl:
   4614     case arm_stub_long_branch_arm_nacl_pic:
   4615       return 16;
   4616 
   4617     default:
   4618       abort ();  /* Should be unreachable.  */
   4619     }
   4620 }
   4621 
   4622 /* Returns whether stubs of type STUB_TYPE take over the symbol they are
   4623    veneering (TRUE) or have their own symbol (FALSE).  */
   4624 
   4625 static bfd_boolean
   4626 arm_stub_sym_claimed (enum elf32_arm_stub_type stub_type)
   4627 {
   4628   if (stub_type >= max_stub_type)
   4629     abort ();  /* Should be unreachable.  */
   4630 
   4631   switch (stub_type)
   4632     {
   4633     case arm_stub_cmse_branch_thumb_only:
   4634       return TRUE;
   4635 
   4636     default:
   4637       return FALSE;
   4638     }
   4639 
   4640   abort ();  /* Should be unreachable.  */
   4641 }
   4642 
   4643 /* Returns the padding needed for the dedicated section used stubs of type
   4644    STUB_TYPE.  */
   4645 
   4646 static int
   4647 arm_dedicated_stub_section_padding (enum elf32_arm_stub_type stub_type)
   4648 {
   4649   if (stub_type >= max_stub_type)
   4650     abort ();  /* Should be unreachable.  */
   4651 
   4652   switch (stub_type)
   4653     {
   4654     case arm_stub_cmse_branch_thumb_only:
   4655       return 32;
   4656 
   4657     default:
   4658       return 0;
   4659     }
   4660 
   4661   abort ();  /* Should be unreachable.  */
   4662 }
   4663 
   4664 /* If veneers of type STUB_TYPE should go in a dedicated output section,
   4665    returns the address of the hash table field in HTAB holding the offset at
   4666    which new veneers should be layed out in the stub section.  */
   4667 
   4668 static bfd_vma*
   4669 arm_new_stubs_start_offset_ptr (struct elf32_arm_link_hash_table *htab,
   4670 				enum elf32_arm_stub_type stub_type)
   4671 {
   4672   switch (stub_type)
   4673     {
   4674     case arm_stub_cmse_branch_thumb_only:
   4675       return &htab->new_cmse_stub_offset;
   4676 
   4677     default:
   4678       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
   4679       return NULL;
   4680     }
   4681 }
   4682 
   4683 static bfd_boolean
   4684 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
   4685 		    void * in_arg)
   4686 {
   4687 #define MAXRELOCS 3
   4688   bfd_boolean removed_sg_veneer;
   4689   struct elf32_arm_stub_hash_entry *stub_entry;
   4690   struct elf32_arm_link_hash_table *globals;
   4691   struct bfd_link_info *info;
   4692   asection *stub_sec;
   4693   bfd *stub_bfd;
   4694   bfd_byte *loc;
   4695   bfd_vma sym_value;
   4696   int template_size;
   4697   int size;
   4698   const insn_sequence *template_sequence;
   4699   int i;
   4700   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
   4701   int stub_reloc_offset[MAXRELOCS] = {0, 0};
   4702   int nrelocs = 0;
   4703   int just_allocated = 0;
   4704 
   4705   /* Massage our args to the form they really have.  */
   4706   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
   4707   info = (struct bfd_link_info *) in_arg;
   4708 
   4709   globals = elf32_arm_hash_table (info);
   4710   if (globals == NULL)
   4711     return FALSE;
   4712 
   4713   stub_sec = stub_entry->stub_sec;
   4714 
   4715   if ((globals->fix_cortex_a8 < 0)
   4716       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
   4717     /* We have to do less-strictly-aligned fixes last.  */
   4718     return TRUE;
   4719 
   4720   /* Assign a slot at the end of section if none assigned yet.  */
   4721   if (stub_entry->stub_offset == (bfd_vma) -1)
   4722     {
   4723       stub_entry->stub_offset = stub_sec->size;
   4724       just_allocated = 1;
   4725     }
   4726   loc = stub_sec->contents + stub_entry->stub_offset;
   4727 
   4728   stub_bfd = stub_sec->owner;
   4729 
   4730   /* This is the address of the stub destination.  */
   4731   sym_value = (stub_entry->target_value
   4732 	       + stub_entry->target_section->output_offset
   4733 	       + stub_entry->target_section->output_section->vma);
   4734 
   4735   template_sequence = stub_entry->stub_template;
   4736   template_size = stub_entry->stub_template_size;
   4737 
   4738   size = 0;
   4739   for (i = 0; i < template_size; i++)
   4740     {
   4741       switch (template_sequence[i].type)
   4742 	{
   4743 	case THUMB16_TYPE:
   4744 	  {
   4745 	    bfd_vma data = (bfd_vma) template_sequence[i].data;
   4746 	    if (template_sequence[i].reloc_addend != 0)
   4747 	      {
   4748 		/* We've borrowed the reloc_addend field to mean we should
   4749 		   insert a condition code into this (Thumb-1 branch)
   4750 		   instruction.  See THUMB16_BCOND_INSN.  */
   4751 		BFD_ASSERT ((data & 0xff00) == 0xd000);
   4752 		data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
   4753 	      }
   4754 	    bfd_put_16 (stub_bfd, data, loc + size);
   4755 	    size += 2;
   4756 	  }
   4757 	  break;
   4758 
   4759 	case THUMB32_TYPE:
   4760 	  bfd_put_16 (stub_bfd,
   4761 		      (template_sequence[i].data >> 16) & 0xffff,
   4762 		      loc + size);
   4763 	  bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
   4764 		      loc + size + 2);
   4765 	  if (template_sequence[i].r_type != R_ARM_NONE)
   4766 	    {
   4767 	      stub_reloc_idx[nrelocs] = i;
   4768 	      stub_reloc_offset[nrelocs++] = size;
   4769 	    }
   4770 	  size += 4;
   4771 	  break;
   4772 
   4773 	case ARM_TYPE:
   4774 	  bfd_put_32 (stub_bfd, template_sequence[i].data,
   4775 		      loc + size);
   4776 	  /* Handle cases where the target is encoded within the
   4777 	     instruction.  */
   4778 	  if (template_sequence[i].r_type == R_ARM_JUMP24)
   4779 	    {
   4780 	      stub_reloc_idx[nrelocs] = i;
   4781 	      stub_reloc_offset[nrelocs++] = size;
   4782 	    }
   4783 	  size += 4;
   4784 	  break;
   4785 
   4786 	case DATA_TYPE:
   4787 	  bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
   4788 	  stub_reloc_idx[nrelocs] = i;
   4789 	  stub_reloc_offset[nrelocs++] = size;
   4790 	  size += 4;
   4791 	  break;
   4792 
   4793 	default:
   4794 	  BFD_FAIL ();
   4795 	  return FALSE;
   4796 	}
   4797     }
   4798 
   4799   if (just_allocated)
   4800     stub_sec->size += size;
   4801 
   4802   /* Stub size has already been computed in arm_size_one_stub. Check
   4803      consistency.  */
   4804   BFD_ASSERT (size == stub_entry->stub_size);
   4805 
   4806   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
   4807   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
   4808     sym_value |= 1;
   4809 
   4810   /* Assume non empty slots have at least one and at most MAXRELOCS entries
   4811      to relocate in each stub.  */
   4812   removed_sg_veneer =
   4813     (size == 0 && stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
   4814   BFD_ASSERT (removed_sg_veneer || (nrelocs != 0 && nrelocs <= MAXRELOCS));
   4815 
   4816   for (i = 0; i < nrelocs; i++)
   4817     {
   4818       Elf_Internal_Rela rel;
   4819       bfd_boolean unresolved_reloc;
   4820       char *error_message;
   4821       bfd_vma points_to =
   4822 	sym_value + template_sequence[stub_reloc_idx[i]].reloc_addend;
   4823 
   4824       rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
   4825       rel.r_info = ELF32_R_INFO (0,
   4826 				 template_sequence[stub_reloc_idx[i]].r_type);
   4827       rel.r_addend = 0;
   4828 
   4829       if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
   4830 	/* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
   4831 	   template should refer back to the instruction after the original
   4832 	   branch.  We use target_section as Cortex-A8 erratum workaround stubs
   4833 	   are only generated when both source and target are in the same
   4834 	   section.  */
   4835 	points_to = stub_entry->target_section->output_section->vma
   4836 		    + stub_entry->target_section->output_offset
   4837 		    + stub_entry->source_value;
   4838 
   4839       elf32_arm_final_link_relocate (elf32_arm_howto_from_type
   4840 	  (template_sequence[stub_reloc_idx[i]].r_type),
   4841 	   stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
   4842 	   points_to, info, stub_entry->target_section, "", STT_FUNC,
   4843 	   stub_entry->branch_type,
   4844 	   (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
   4845 	   &error_message);
   4846     }
   4847 
   4848   return TRUE;
   4849 #undef MAXRELOCS
   4850 }
   4851 
   4852 /* Calculate the template, template size and instruction size for a stub.
   4853    Return value is the instruction size.  */
   4854 
   4855 static unsigned int
   4856 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
   4857 			     const insn_sequence **stub_template,
   4858 			     int *stub_template_size)
   4859 {
   4860   const insn_sequence *template_sequence = NULL;
   4861   int template_size = 0, i;
   4862   unsigned int size;
   4863 
   4864   template_sequence = stub_definitions[stub_type].template_sequence;
   4865   if (stub_template)
   4866     *stub_template = template_sequence;
   4867 
   4868   template_size = stub_definitions[stub_type].template_size;
   4869   if (stub_template_size)
   4870     *stub_template_size = template_size;
   4871 
   4872   size = 0;
   4873   for (i = 0; i < template_size; i++)
   4874     {
   4875       switch (template_sequence[i].type)
   4876 	{
   4877 	case THUMB16_TYPE:
   4878 	  size += 2;
   4879 	  break;
   4880 
   4881 	case ARM_TYPE:
   4882 	case THUMB32_TYPE:
   4883 	case DATA_TYPE:
   4884 	  size += 4;
   4885 	  break;
   4886 
   4887 	default:
   4888 	  BFD_FAIL ();
   4889 	  return 0;
   4890 	}
   4891     }
   4892 
   4893   return size;
   4894 }
   4895 
   4896 /* As above, but don't actually build the stub.  Just bump offset so
   4897    we know stub section sizes.  */
   4898 
   4899 static bfd_boolean
   4900 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
   4901 		   void *in_arg ATTRIBUTE_UNUSED)
   4902 {
   4903   struct elf32_arm_stub_hash_entry *stub_entry;
   4904   const insn_sequence *template_sequence;
   4905   int template_size, size;
   4906 
   4907   /* Massage our args to the form they really have.  */
   4908   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
   4909 
   4910   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
   4911 	     && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
   4912 
   4913   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
   4914 				      &template_size);
   4915 
   4916   /* Initialized to -1.  Null size indicates an empty slot full of zeros.  */
   4917   if (stub_entry->stub_template_size)
   4918     {
   4919       stub_entry->stub_size = size;
   4920       stub_entry->stub_template = template_sequence;
   4921       stub_entry->stub_template_size = template_size;
   4922     }
   4923 
   4924   /* Already accounted for.  */
   4925   if (stub_entry->stub_offset != (bfd_vma) -1)
   4926     return TRUE;
   4927 
   4928   size = (size + 7) & ~7;
   4929   stub_entry->stub_sec->size += size;
   4930 
   4931   return TRUE;
   4932 }
   4933 
   4934 /* External entry points for sizing and building linker stubs.  */
   4935 
   4936 /* Set up various things so that we can make a list of input sections
   4937    for each output section included in the link.  Returns -1 on error,
   4938    0 when no stubs will be needed, and 1 on success.  */
   4939 
   4940 int
   4941 elf32_arm_setup_section_lists (bfd *output_bfd,
   4942 			       struct bfd_link_info *info)
   4943 {
   4944   bfd *input_bfd;
   4945   unsigned int bfd_count;
   4946   unsigned int top_id, top_index;
   4947   asection *section;
   4948   asection **input_list, **list;
   4949   bfd_size_type amt;
   4950   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
   4951 
   4952   if (htab == NULL)
   4953     return 0;
   4954   if (! is_elf_hash_table (htab))
   4955     return 0;
   4956 
   4957   /* Count the number of input BFDs and find the top input section id.  */
   4958   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
   4959        input_bfd != NULL;
   4960        input_bfd = input_bfd->link.next)
   4961     {
   4962       bfd_count += 1;
   4963       for (section = input_bfd->sections;
   4964 	   section != NULL;
   4965 	   section = section->next)
   4966 	{
   4967 	  if (top_id < section->id)
   4968 	    top_id = section->id;
   4969 	}
   4970     }
   4971   htab->bfd_count = bfd_count;
   4972 
   4973   amt = sizeof (struct map_stub) * (top_id + 1);
   4974   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
   4975   if (htab->stub_group == NULL)
   4976     return -1;
   4977   htab->top_id = top_id;
   4978 
   4979   /* We can't use output_bfd->section_count here to find the top output
   4980      section index as some sections may have been removed, and
   4981      _bfd_strip_section_from_output doesn't renumber the indices.  */
   4982   for (section = output_bfd->sections, top_index = 0;
   4983        section != NULL;
   4984        section = section->next)
   4985     {
   4986       if (top_index < section->index)
   4987 	top_index = section->index;
   4988     }
   4989 
   4990   htab->top_index = top_index;
   4991   amt = sizeof (asection *) * (top_index + 1);
   4992   input_list = (asection **) bfd_malloc (amt);
   4993   htab->input_list = input_list;
   4994   if (input_list == NULL)
   4995     return -1;
   4996 
   4997   /* For sections we aren't interested in, mark their entries with a
   4998      value we can check later.  */
   4999   list = input_list + top_index;
   5000   do
   5001     *list = bfd_abs_section_ptr;
   5002   while (list-- != input_list);
   5003 
   5004   for (section = output_bfd->sections;
   5005        section != NULL;
   5006        section = section->next)
   5007     {
   5008       if ((section->flags & SEC_CODE) != 0)
   5009 	input_list[section->index] = NULL;
   5010     }
   5011 
   5012   return 1;
   5013 }
   5014 
   5015 /* The linker repeatedly calls this function for each input section,
   5016    in the order that input sections are linked into output sections.
   5017    Build lists of input sections to determine groupings between which
   5018    we may insert linker stubs.  */
   5019 
   5020 void
   5021 elf32_arm_next_input_section (struct bfd_link_info *info,
   5022 			      asection *isec)
   5023 {
   5024   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
   5025 
   5026   if (htab == NULL)
   5027     return;
   5028 
   5029   if (isec->output_section->index <= htab->top_index)
   5030     {
   5031       asection **list = htab->input_list + isec->output_section->index;
   5032 
   5033       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
   5034 	{
   5035 	  /* Steal the link_sec pointer for our list.  */
   5036 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   5037 	  /* This happens to make the list in reverse order,
   5038 	     which we reverse later.  */
   5039 	  PREV_SEC (isec) = *list;
   5040 	  *list = isec;
   5041 	}
   5042     }
   5043 }
   5044 
   5045 /* See whether we can group stub sections together.  Grouping stub
   5046    sections may result in fewer stubs.  More importantly, we need to
   5047    put all .init* and .fini* stubs at the end of the .init or
   5048    .fini output sections respectively, because glibc splits the
   5049    _init and _fini functions into multiple parts.  Putting a stub in
   5050    the middle of a function is not a good idea.  */
   5051 
   5052 static void
   5053 group_sections (struct elf32_arm_link_hash_table *htab,
   5054 		bfd_size_type stub_group_size,
   5055 		bfd_boolean stubs_always_after_branch)
   5056 {
   5057   asection **list = htab->input_list;
   5058 
   5059   do
   5060     {
   5061       asection *tail = *list;
   5062       asection *head;
   5063 
   5064       if (tail == bfd_abs_section_ptr)
   5065 	continue;
   5066 
   5067       /* Reverse the list: we must avoid placing stubs at the
   5068 	 beginning of the section because the beginning of the text
   5069 	 section may be required for an interrupt vector in bare metal
   5070 	 code.  */
   5071 #define NEXT_SEC PREV_SEC
   5072       head = NULL;
   5073       while (tail != NULL)
   5074 	{
   5075 	  /* Pop from tail.  */
   5076 	  asection *item = tail;
   5077 	  tail = PREV_SEC (item);
   5078 
   5079 	  /* Push on head.  */
   5080 	  NEXT_SEC (item) = head;
   5081 	  head = item;
   5082 	}
   5083 
   5084       while (head != NULL)
   5085 	{
   5086 	  asection *curr;
   5087 	  asection *next;
   5088 	  bfd_vma stub_group_start = head->output_offset;
   5089 	  bfd_vma end_of_next;
   5090 
   5091 	  curr = head;
   5092 	  while (NEXT_SEC (curr) != NULL)
   5093 	    {
   5094 	      next = NEXT_SEC (curr);
   5095 	      end_of_next = next->output_offset + next->size;
   5096 	      if (end_of_next - stub_group_start >= stub_group_size)
   5097 		/* End of NEXT is too far from start, so stop.  */
   5098 		break;
   5099 	      /* Add NEXT to the group.  */
   5100 	      curr = next;
   5101 	    }
   5102 
   5103 	  /* OK, the size from the start to the start of CURR is less
   5104 	     than stub_group_size and thus can be handled by one stub
   5105 	     section.  (Or the head section is itself larger than
   5106 	     stub_group_size, in which case we may be toast.)
   5107 	     We should really be keeping track of the total size of
   5108 	     stubs added here, as stubs contribute to the final output
   5109 	     section size.  */
   5110 	  do
   5111 	    {
   5112 	      next = NEXT_SEC (head);
   5113 	      /* Set up this stub group.  */
   5114 	      htab->stub_group[head->id].link_sec = curr;
   5115 	    }
   5116 	  while (head != curr && (head = next) != NULL);
   5117 
   5118 	  /* But wait, there's more!  Input sections up to stub_group_size
   5119 	     bytes after the stub section can be handled by it too.  */
   5120 	  if (!stubs_always_after_branch)
   5121 	    {
   5122 	      stub_group_start = curr->output_offset + curr->size;
   5123 
   5124 	      while (next != NULL)
   5125 		{
   5126 		  end_of_next = next->output_offset + next->size;
   5127 		  if (end_of_next - stub_group_start >= stub_group_size)
   5128 		    /* End of NEXT is too far from stubs, so stop.  */
   5129 		    break;
   5130 		  /* Add NEXT to the stub group.  */
   5131 		  head = next;
   5132 		  next = NEXT_SEC (head);
   5133 		  htab->stub_group[head->id].link_sec = curr;
   5134 		}
   5135 	    }
   5136 	  head = next;
   5137 	}
   5138     }
   5139   while (list++ != htab->input_list + htab->top_index);
   5140 
   5141   free (htab->input_list);
   5142 #undef PREV_SEC
   5143 #undef NEXT_SEC
   5144 }
   5145 
   5146 /* Comparison function for sorting/searching relocations relating to Cortex-A8
   5147    erratum fix.  */
   5148 
   5149 static int
   5150 a8_reloc_compare (const void *a, const void *b)
   5151 {
   5152   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
   5153   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
   5154 
   5155   if (ra->from < rb->from)
   5156     return -1;
   5157   else if (ra->from > rb->from)
   5158     return 1;
   5159   else
   5160     return 0;
   5161 }
   5162 
   5163 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
   5164 						    const char *, char **);
   5165 
   5166 /* Helper function to scan code for sequences which might trigger the Cortex-A8
   5167    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
   5168    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
   5169    otherwise.  */
   5170 
   5171 static bfd_boolean
   5172 cortex_a8_erratum_scan (bfd *input_bfd,
   5173 			struct bfd_link_info *info,
   5174 			struct a8_erratum_fix **a8_fixes_p,
   5175 			unsigned int *num_a8_fixes_p,
   5176 			unsigned int *a8_fix_table_size_p,
   5177 			struct a8_erratum_reloc *a8_relocs,
   5178 			unsigned int num_a8_relocs,
   5179 			unsigned prev_num_a8_fixes,
   5180 			bfd_boolean *stub_changed_p)
   5181 {
   5182   asection *section;
   5183   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
   5184   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
   5185   unsigned int num_a8_fixes = *num_a8_fixes_p;
   5186   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
   5187 
   5188   if (htab == NULL)
   5189     return FALSE;
   5190 
   5191   for (section = input_bfd->sections;
   5192        section != NULL;
   5193        section = section->next)
   5194     {
   5195       bfd_byte *contents = NULL;
   5196       struct _arm_elf_section_data *sec_data;
   5197       unsigned int span;
   5198       bfd_vma base_vma;
   5199 
   5200       if (elf_section_type (section) != SHT_PROGBITS
   5201 	  || (elf_section_flags (section) & SHF_EXECINSTR) == 0
   5202 	  || (section->flags & SEC_EXCLUDE) != 0
   5203 	  || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   5204 	  || (section->output_section == bfd_abs_section_ptr))
   5205 	continue;
   5206 
   5207       base_vma = section->output_section->vma + section->output_offset;
   5208 
   5209       if (elf_section_data (section)->this_hdr.contents != NULL)
   5210 	contents = elf_section_data (section)->this_hdr.contents;
   5211       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
   5212 	return TRUE;
   5213 
   5214       sec_data = elf32_arm_section_data (section);
   5215 
   5216       for (span = 0; span < sec_data->mapcount; span++)
   5217 	{
   5218 	  unsigned int span_start = sec_data->map[span].vma;
   5219 	  unsigned int span_end = (span == sec_data->mapcount - 1)
   5220 	    ? section->size : sec_data->map[span + 1].vma;
   5221 	  unsigned int i;
   5222 	  char span_type = sec_data->map[span].type;
   5223 	  bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
   5224 
   5225 	  if (span_type != 't')
   5226 	    continue;
   5227 
   5228 	  /* Span is entirely within a single 4KB region: skip scanning.  */
   5229 	  if (((base_vma + span_start) & ~0xfff)
   5230 	      == ((base_vma + span_end) & ~0xfff))
   5231 	    continue;
   5232 
   5233 	  /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
   5234 
   5235 	       * The opcode is BLX.W, BL.W, B.W, Bcc.W
   5236 	       * The branch target is in the same 4KB region as the
   5237 		 first half of the branch.
   5238 	       * The instruction before the branch is a 32-bit
   5239 		 length non-branch instruction.  */
   5240 	  for (i = span_start; i < span_end;)
   5241 	    {
   5242 	      unsigned int insn = bfd_getl16 (&contents[i]);
   5243 	      bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
   5244 	      bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
   5245 
   5246 	      if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
   5247 		insn_32bit = TRUE;
   5248 
   5249 	      if (insn_32bit)
   5250 		{
   5251 		  /* Load the rest of the insn (in manual-friendly order).  */
   5252 		  insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
   5253 
   5254 		  /* Encoding T4: B<c>.W.  */
   5255 		  is_b = (insn & 0xf800d000) == 0xf0009000;
   5256 		  /* Encoding T1: BL<c>.W.  */
   5257 		  is_bl = (insn & 0xf800d000) == 0xf000d000;
   5258 		  /* Encoding T2: BLX<c>.W.  */
   5259 		  is_blx = (insn & 0xf800d000) == 0xf000c000;
   5260 		  /* Encoding T3: B<c>.W (not permitted in IT block).  */
   5261 		  is_bcc = (insn & 0xf800d000) == 0xf0008000
   5262 			   && (insn & 0x07f00000) != 0x03800000;
   5263 		}
   5264 
   5265 	      is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
   5266 
   5267 	      if (((base_vma + i) & 0xfff) == 0xffe
   5268 		  && insn_32bit
   5269 		  && is_32bit_branch
   5270 		  && last_was_32bit
   5271 		  && ! last_was_branch)
   5272 		{
   5273 		  bfd_signed_vma offset = 0;
   5274 		  bfd_boolean force_target_arm = FALSE;
   5275 		  bfd_boolean force_target_thumb = FALSE;
   5276 		  bfd_vma target;
   5277 		  enum elf32_arm_stub_type stub_type = arm_stub_none;
   5278 		  struct a8_erratum_reloc key, *found;
   5279 		  bfd_boolean use_plt = FALSE;
   5280 
   5281 		  key.from = base_vma + i;
   5282 		  found = (struct a8_erratum_reloc *)
   5283 		      bsearch (&key, a8_relocs, num_a8_relocs,
   5284 			       sizeof (struct a8_erratum_reloc),
   5285 			       &a8_reloc_compare);
   5286 
   5287 		  if (found)
   5288 		    {
   5289 		      char *error_message = NULL;
   5290 		      struct elf_link_hash_entry *entry;
   5291 
   5292 		      /* We don't care about the error returned from this
   5293 			 function, only if there is glue or not.  */
   5294 		      entry = find_thumb_glue (info, found->sym_name,
   5295 					       &error_message);
   5296 
   5297 		      if (entry)
   5298 			found->non_a8_stub = TRUE;
   5299 
   5300 		      /* Keep a simpler condition, for the sake of clarity.  */
   5301 		      if (htab->root.splt != NULL && found->hash != NULL
   5302 			  && found->hash->root.plt.offset != (bfd_vma) -1)
   5303 			use_plt = TRUE;
   5304 
   5305 		      if (found->r_type == R_ARM_THM_CALL)
   5306 			{
   5307 			  if (found->branch_type == ST_BRANCH_TO_ARM
   5308 			      || use_plt)
   5309 			    force_target_arm = TRUE;
   5310 			  else
   5311 			    force_target_thumb = TRUE;
   5312 			}
   5313 		    }
   5314 
   5315 		  /* Check if we have an offending branch instruction.  */
   5316 
   5317 		  if (found && found->non_a8_stub)
   5318 		    /* We've already made a stub for this instruction, e.g.
   5319 		       it's a long branch or a Thumb->ARM stub.  Assume that
   5320 		       stub will suffice to work around the A8 erratum (see
   5321 		       setting of always_after_branch above).  */
   5322 		    ;
   5323 		  else if (is_bcc)
   5324 		    {
   5325 		      offset = (insn & 0x7ff) << 1;
   5326 		      offset |= (insn & 0x3f0000) >> 4;
   5327 		      offset |= (insn & 0x2000) ? 0x40000 : 0;
   5328 		      offset |= (insn & 0x800) ? 0x80000 : 0;
   5329 		      offset |= (insn & 0x4000000) ? 0x100000 : 0;
   5330 		      if (offset & 0x100000)
   5331 			offset |= ~ ((bfd_signed_vma) 0xfffff);
   5332 		      stub_type = arm_stub_a8_veneer_b_cond;
   5333 		    }
   5334 		  else if (is_b || is_bl || is_blx)
   5335 		    {
   5336 		      int s = (insn & 0x4000000) != 0;
   5337 		      int j1 = (insn & 0x2000) != 0;
   5338 		      int j2 = (insn & 0x800) != 0;
   5339 		      int i1 = !(j1 ^ s);
   5340 		      int i2 = !(j2 ^ s);
   5341 
   5342 		      offset = (insn & 0x7ff) << 1;
   5343 		      offset |= (insn & 0x3ff0000) >> 4;
   5344 		      offset |= i2 << 22;
   5345 		      offset |= i1 << 23;
   5346 		      offset |= s << 24;
   5347 		      if (offset & 0x1000000)
   5348 			offset |= ~ ((bfd_signed_vma) 0xffffff);
   5349 
   5350 		      if (is_blx)
   5351 			offset &= ~ ((bfd_signed_vma) 3);
   5352 
   5353 		      stub_type = is_blx ? arm_stub_a8_veneer_blx :
   5354 			is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
   5355 		    }
   5356 
   5357 		  if (stub_type != arm_stub_none)
   5358 		    {
   5359 		      bfd_vma pc_for_insn = base_vma + i + 4;
   5360 
   5361 		      /* The original instruction is a BL, but the target is
   5362 			 an ARM instruction.  If we were not making a stub,
   5363 			 the BL would have been converted to a BLX.  Use the
   5364 			 BLX stub instead in that case.  */
   5365 		      if (htab->use_blx && force_target_arm
   5366 			  && stub_type == arm_stub_a8_veneer_bl)
   5367 			{
   5368 			  stub_type = arm_stub_a8_veneer_blx;
   5369 			  is_blx = TRUE;
   5370 			  is_bl = FALSE;
   5371 			}
   5372 		      /* Conversely, if the original instruction was
   5373 			 BLX but the target is Thumb mode, use the BL
   5374 			 stub.  */
   5375 		      else if (force_target_thumb
   5376 			       && stub_type == arm_stub_a8_veneer_blx)
   5377 			{
   5378 			  stub_type = arm_stub_a8_veneer_bl;
   5379 			  is_blx = FALSE;
   5380 			  is_bl = TRUE;
   5381 			}
   5382 
   5383 		      if (is_blx)
   5384 			pc_for_insn &= ~ ((bfd_vma) 3);
   5385 
   5386 		      /* If we found a relocation, use the proper destination,
   5387 			 not the offset in the (unrelocated) instruction.
   5388 			 Note this is always done if we switched the stub type
   5389 			 above.  */
   5390 		      if (found)
   5391 			offset =
   5392 			  (bfd_signed_vma) (found->destination - pc_for_insn);
   5393 
   5394 		      /* If the stub will use a Thumb-mode branch to a
   5395 			 PLT target, redirect it to the preceding Thumb
   5396 			 entry point.  */
   5397 		      if (stub_type != arm_stub_a8_veneer_blx && use_plt)
   5398 			offset -= PLT_THUMB_STUB_SIZE;
   5399 
   5400 		      target = pc_for_insn + offset;
   5401 
   5402 		      /* The BLX stub is ARM-mode code.  Adjust the offset to
   5403 			 take the different PC value (+8 instead of +4) into
   5404 			 account.  */
   5405 		      if (stub_type == arm_stub_a8_veneer_blx)
   5406 			offset += 4;
   5407 
   5408 		      if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
   5409 			{
   5410 			  char *stub_name = NULL;
   5411 
   5412 			  if (num_a8_fixes == a8_fix_table_size)
   5413 			    {
   5414 			      a8_fix_table_size *= 2;
   5415 			      a8_fixes = (struct a8_erratum_fix *)
   5416 				  bfd_realloc (a8_fixes,
   5417 					       sizeof (struct a8_erratum_fix)
   5418 					       * a8_fix_table_size);
   5419 			    }
   5420 
   5421 			  if (num_a8_fixes < prev_num_a8_fixes)
   5422 			    {
   5423 			      /* If we're doing a subsequent scan,
   5424 				 check if we've found the same fix as
   5425 				 before, and try and reuse the stub
   5426 				 name.  */
   5427 			      stub_name = a8_fixes[num_a8_fixes].stub_name;
   5428 			      if ((a8_fixes[num_a8_fixes].section != section)
   5429 				  || (a8_fixes[num_a8_fixes].offset != i))
   5430 				{
   5431 				  free (stub_name);
   5432 				  stub_name = NULL;
   5433 				  *stub_changed_p = TRUE;
   5434 				}
   5435 			    }
   5436 
   5437 			  if (!stub_name)
   5438 			    {
   5439 			      stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
   5440 			      if (stub_name != NULL)
   5441 				sprintf (stub_name, "%x:%x", section->id, i);
   5442 			    }
   5443 
   5444 			  a8_fixes[num_a8_fixes].input_bfd = input_bfd;
   5445 			  a8_fixes[num_a8_fixes].section = section;
   5446 			  a8_fixes[num_a8_fixes].offset = i;
   5447 			  a8_fixes[num_a8_fixes].target_offset =
   5448 			    target - base_vma;
   5449 			  a8_fixes[num_a8_fixes].orig_insn = insn;
   5450 			  a8_fixes[num_a8_fixes].stub_name = stub_name;
   5451 			  a8_fixes[num_a8_fixes].stub_type = stub_type;
   5452 			  a8_fixes[num_a8_fixes].branch_type =
   5453 			    is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
   5454 
   5455 			  num_a8_fixes++;
   5456 			}
   5457 		    }
   5458 		}
   5459 
   5460 	      i += insn_32bit ? 4 : 2;
   5461 	      last_was_32bit = insn_32bit;
   5462 	      last_was_branch = is_32bit_branch;
   5463 	    }
   5464 	}
   5465 
   5466       if (elf_section_data (section)->this_hdr.contents == NULL)
   5467 	free (contents);
   5468     }
   5469 
   5470   *a8_fixes_p = a8_fixes;
   5471   *num_a8_fixes_p = num_a8_fixes;
   5472   *a8_fix_table_size_p = a8_fix_table_size;
   5473 
   5474   return FALSE;
   5475 }
   5476 
   5477 /* Create or update a stub entry depending on whether the stub can already be
   5478    found in HTAB.  The stub is identified by:
   5479    - its type STUB_TYPE
   5480    - its source branch (note that several can share the same stub) whose
   5481      section and relocation (if any) are given by SECTION and IRELA
   5482      respectively
   5483    - its target symbol whose input section, hash, name, value and branch type
   5484      are given in SYM_SEC, HASH, SYM_NAME, SYM_VALUE and BRANCH_TYPE
   5485      respectively
   5486 
   5487    If found, the value of the stub's target symbol is updated from SYM_VALUE
   5488    and *NEW_STUB is set to FALSE.  Otherwise, *NEW_STUB is set to
   5489    TRUE and the stub entry is initialized.
   5490 
   5491    Returns the stub that was created or updated, or NULL if an error
   5492    occurred.  */
   5493 
   5494 static struct elf32_arm_stub_hash_entry *
   5495 elf32_arm_create_stub (struct elf32_arm_link_hash_table *htab,
   5496 		       enum elf32_arm_stub_type stub_type, asection *section,
   5497 		       Elf_Internal_Rela *irela, asection *sym_sec,
   5498 		       struct elf32_arm_link_hash_entry *hash, char *sym_name,
   5499 		       bfd_vma sym_value, enum arm_st_branch_type branch_type,
   5500 		       bfd_boolean *new_stub)
   5501 {
   5502   const asection *id_sec;
   5503   char *stub_name;
   5504   struct elf32_arm_stub_hash_entry *stub_entry;
   5505   unsigned int r_type;
   5506   bfd_boolean sym_claimed = arm_stub_sym_claimed (stub_type);
   5507 
   5508   BFD_ASSERT (stub_type != arm_stub_none);
   5509   *new_stub = FALSE;
   5510 
   5511   if (sym_claimed)
   5512     stub_name = sym_name;
   5513   else
   5514     {
   5515       BFD_ASSERT (irela);
   5516       BFD_ASSERT (section);
   5517       BFD_ASSERT (section->id <= htab->top_id);
   5518 
   5519       /* Support for grouping stub sections.  */
   5520       id_sec = htab->stub_group[section->id].link_sec;
   5521 
   5522       /* Get the name of this stub.  */
   5523       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela,
   5524 				       stub_type);
   5525       if (!stub_name)
   5526 	return NULL;
   5527     }
   5528 
   5529   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name, FALSE,
   5530 				     FALSE);
   5531   /* The proper stub has already been created, just update its value.  */
   5532   if (stub_entry != NULL)
   5533     {
   5534       if (!sym_claimed)
   5535 	free (stub_name);
   5536       stub_entry->target_value = sym_value;
   5537       return stub_entry;
   5538     }
   5539 
   5540   stub_entry = elf32_arm_add_stub (stub_name, section, htab, stub_type);
   5541   if (stub_entry == NULL)
   5542     {
   5543       if (!sym_claimed)
   5544 	free (stub_name);
   5545       return NULL;
   5546     }
   5547 
   5548   stub_entry->target_value = sym_value;
   5549   stub_entry->target_section = sym_sec;
   5550   stub_entry->stub_type = stub_type;
   5551   stub_entry->h = hash;
   5552   stub_entry->branch_type = branch_type;
   5553 
   5554   if (sym_claimed)
   5555     stub_entry->output_name = sym_name;
   5556   else
   5557     {
   5558       if (sym_name == NULL)
   5559 	sym_name = "unnamed";
   5560       stub_entry->output_name = (char *)
   5561 	bfd_alloc (htab->stub_bfd, sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
   5562 				   + strlen (sym_name));
   5563       if (stub_entry->output_name == NULL)
   5564 	{
   5565 	  free (stub_name);
   5566 	  return NULL;
   5567 	}
   5568 
   5569       /* For historical reasons, use the existing names for ARM-to-Thumb and
   5570 	 Thumb-to-ARM stubs.  */
   5571       r_type = ELF32_R_TYPE (irela->r_info);
   5572       if ((r_type == (unsigned int) R_ARM_THM_CALL
   5573 	   || r_type == (unsigned int) R_ARM_THM_JUMP24
   5574 	   || r_type == (unsigned int) R_ARM_THM_JUMP19)
   5575 	  && branch_type == ST_BRANCH_TO_ARM)
   5576 	sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
   5577       else if ((r_type == (unsigned int) R_ARM_CALL
   5578 		|| r_type == (unsigned int) R_ARM_JUMP24)
   5579 	       && branch_type == ST_BRANCH_TO_THUMB)
   5580 	sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
   5581       else
   5582 	sprintf (stub_entry->output_name, STUB_ENTRY_NAME, sym_name);
   5583     }
   5584 
   5585   *new_stub = TRUE;
   5586   return stub_entry;
   5587 }
   5588 
   5589 /* Scan symbols in INPUT_BFD to identify secure entry functions needing a
   5590    gateway veneer to transition from non secure to secure state and create them
   5591    accordingly.
   5592 
   5593    "ARMv8-M Security Extensions: Requirements on Development Tools" document
   5594    defines the conditions that govern Secure Gateway veneer creation for a
   5595    given symbol <SYM> as follows:
   5596    - it has function type
   5597    - it has non local binding
   5598    - a symbol named __acle_se_<SYM> (called special symbol) exists with the
   5599      same type, binding and value as <SYM> (called normal symbol).
   5600    An entry function can handle secure state transition itself in which case
   5601    its special symbol would have a different value from the normal symbol.
   5602 
   5603    OUT_ATTR gives the output attributes, SYM_HASHES the symbol index to hash
   5604    entry mapping while HTAB gives the name to hash entry mapping.
   5605    *CMSE_STUB_CREATED is increased by the number of secure gateway veneer
   5606    created.
   5607 
   5608    The return value gives whether a stub failed to be allocated.  */
   5609 
   5610 static bfd_boolean
   5611 cmse_scan (bfd *input_bfd, struct elf32_arm_link_hash_table *htab,
   5612 	   obj_attribute *out_attr, struct elf_link_hash_entry **sym_hashes,
   5613 	   int *cmse_stub_created)
   5614 {
   5615   const struct elf_backend_data *bed;
   5616   Elf_Internal_Shdr *symtab_hdr;
   5617   unsigned i, j, sym_count, ext_start;
   5618   Elf_Internal_Sym *cmse_sym, *local_syms;
   5619   struct elf32_arm_link_hash_entry *hash, *cmse_hash = NULL;
   5620   enum arm_st_branch_type branch_type;
   5621   char *sym_name, *lsym_name;
   5622   bfd_vma sym_value;
   5623   asection *section;
   5624   struct elf32_arm_stub_hash_entry *stub_entry;
   5625   bfd_boolean is_v8m, new_stub, cmse_invalid, ret = TRUE;
   5626 
   5627   bed = get_elf_backend_data (input_bfd);
   5628   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5629   sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
   5630   ext_start = symtab_hdr->sh_info;
   5631   is_v8m = (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
   5632 	    && out_attr[Tag_CPU_arch_profile].i == 'M');
   5633 
   5634   local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   5635   if (local_syms == NULL)
   5636     local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   5637 				       symtab_hdr->sh_info, 0, NULL, NULL,
   5638 				       NULL);
   5639   if (symtab_hdr->sh_info && local_syms == NULL)
   5640     return FALSE;
   5641 
   5642   /* Scan symbols.  */
   5643   for (i = 0; i < sym_count; i++)
   5644     {
   5645       cmse_invalid = FALSE;
   5646 
   5647       if (i < ext_start)
   5648 	{
   5649 	  cmse_sym = &local_syms[i];
   5650 	  /* Not a special symbol.  */
   5651 	  if (!ARM_GET_SYM_CMSE_SPCL (cmse_sym->st_target_internal))
   5652 	    continue;
   5653 	  sym_name = bfd_elf_string_from_elf_section (input_bfd,
   5654 						      symtab_hdr->sh_link,
   5655 						      cmse_sym->st_name);
   5656 	  /* Special symbol with local binding.  */
   5657 	  cmse_invalid = TRUE;
   5658 	}
   5659       else
   5660 	{
   5661 	  cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
   5662 	  sym_name = (char *) cmse_hash->root.root.root.string;
   5663 
   5664 	  /* Not a special symbol.  */
   5665 	  if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
   5666 	    continue;
   5667 
   5668 	  /* Special symbol has incorrect binding or type.  */
   5669 	  if ((cmse_hash->root.root.type != bfd_link_hash_defined
   5670 	       && cmse_hash->root.root.type != bfd_link_hash_defweak)
   5671 	      || cmse_hash->root.type != STT_FUNC)
   5672 	    cmse_invalid = TRUE;
   5673 	}
   5674 
   5675       if (!is_v8m)
   5676 	{
   5677 	  _bfd_error_handler (_("%B: Special symbol `%s' only allowed for "
   5678 				"ARMv8-M architecture or later."),
   5679 			      input_bfd, sym_name);
   5680 	  is_v8m = TRUE; /* Avoid multiple warning.  */
   5681 	  ret = FALSE;
   5682 	}
   5683 
   5684       if (cmse_invalid)
   5685 	{
   5686 	  _bfd_error_handler (_("%B: invalid special symbol `%s'."),
   5687 			      input_bfd, sym_name);
   5688 	  _bfd_error_handler (_("It must be a global or weak function "
   5689 				"symbol."));
   5690 	  ret = FALSE;
   5691 	  if (i < ext_start)
   5692 	    continue;
   5693 	}
   5694 
   5695       sym_name += strlen (CMSE_PREFIX);
   5696       hash = (struct elf32_arm_link_hash_entry *)
   5697 	elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
   5698 
   5699       /* No associated normal symbol or it is neither global nor weak.  */
   5700       if (!hash
   5701 	  || (hash->root.root.type != bfd_link_hash_defined
   5702 	      && hash->root.root.type != bfd_link_hash_defweak)
   5703 	  || hash->root.type != STT_FUNC)
   5704 	{
   5705 	  /* Initialize here to avoid warning about use of possibly
   5706 	     uninitialized variable.  */
   5707 	  j = 0;
   5708 
   5709 	  if (!hash)
   5710 	    {
   5711 	      /* Searching for a normal symbol with local binding.  */
   5712 	      for (; j < ext_start; j++)
   5713 		{
   5714 		  lsym_name =
   5715 		    bfd_elf_string_from_elf_section (input_bfd,
   5716 						     symtab_hdr->sh_link,
   5717 						     local_syms[j].st_name);
   5718 		  if (!strcmp (sym_name, lsym_name))
   5719 		    break;
   5720 		}
   5721 	    }
   5722 
   5723 	  if (hash || j < ext_start)
   5724 	    {
   5725 	      _bfd_error_handler
   5726 		(_("%B: invalid standard symbol `%s'."), input_bfd, sym_name);
   5727 	      _bfd_error_handler
   5728 		(_("It must be a global or weak function symbol."));
   5729 	    }
   5730 	  else
   5731 	    _bfd_error_handler
   5732 	      (_("%B: absent standard symbol `%s'."), input_bfd, sym_name);
   5733 	  ret = FALSE;
   5734 	  if (!hash)
   5735 	    continue;
   5736 	}
   5737 
   5738       sym_value = hash->root.root.u.def.value;
   5739       section = hash->root.root.u.def.section;
   5740 
   5741       if (cmse_hash->root.root.u.def.section != section)
   5742 	{
   5743 	  _bfd_error_handler
   5744 	    (_("%B: `%s' and its special symbol are in different sections."),
   5745 	     input_bfd, sym_name);
   5746 	  ret = FALSE;
   5747 	}
   5748       if (cmse_hash->root.root.u.def.value != sym_value)
   5749 	continue; /* Ignore: could be an entry function starting with SG.  */
   5750 
   5751 	/* If this section is a link-once section that will be discarded, then
   5752 	   don't create any stubs.  */
   5753       if (section->output_section == NULL)
   5754 	{
   5755 	  _bfd_error_handler
   5756 	    (_("%B: entry function `%s' not output."), input_bfd, sym_name);
   5757 	  continue;
   5758 	}
   5759 
   5760       if (hash->root.size == 0)
   5761 	{
   5762 	  _bfd_error_handler
   5763 	    (_("%B: entry function `%s' is empty."), input_bfd, sym_name);
   5764 	  ret = FALSE;
   5765 	}
   5766 
   5767       if (!ret)
   5768 	continue;
   5769       branch_type = ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
   5770       stub_entry
   5771 	= elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
   5772 				 NULL, NULL, section, hash, sym_name,
   5773 				 sym_value, branch_type, &new_stub);
   5774 
   5775       if (stub_entry == NULL)
   5776 	 ret = FALSE;
   5777       else
   5778 	{
   5779 	  BFD_ASSERT (new_stub);
   5780 	  (*cmse_stub_created)++;
   5781 	}
   5782     }
   5783 
   5784   if (!symtab_hdr->contents)
   5785     free (local_syms);
   5786   return ret;
   5787 }
   5788 
   5789 /* Return TRUE iff a symbol identified by its linker HASH entry is a secure
   5790    code entry function, ie can be called from non secure code without using a
   5791    veneer.  */
   5792 
   5793 static bfd_boolean
   5794 cmse_entry_fct_p (struct elf32_arm_link_hash_entry *hash)
   5795 {
   5796   bfd_byte contents[4];
   5797   uint32_t first_insn;
   5798   asection *section;
   5799   file_ptr offset;
   5800   bfd *abfd;
   5801 
   5802   /* Defined symbol of function type.  */
   5803   if (hash->root.root.type != bfd_link_hash_defined
   5804       && hash->root.root.type != bfd_link_hash_defweak)
   5805     return FALSE;
   5806   if (hash->root.type != STT_FUNC)
   5807     return FALSE;
   5808 
   5809   /* Read first instruction.  */
   5810   section = hash->root.root.u.def.section;
   5811   abfd = section->owner;
   5812   offset = hash->root.root.u.def.value - section->vma;
   5813   if (!bfd_get_section_contents (abfd, section, contents, offset,
   5814 				 sizeof (contents)))
   5815     return FALSE;
   5816 
   5817   first_insn = bfd_get_32 (abfd, contents);
   5818 
   5819   /* Starts by SG instruction.  */
   5820   return first_insn == 0xe97fe97f;
   5821 }
   5822 
   5823 /* Output the name (in symbol table) of the veneer GEN_ENTRY if it is a new
   5824    secure gateway veneers (ie. the veneers was not in the input import library)
   5825    and there is no output import library (GEN_INFO->out_implib_bfd is NULL.  */
   5826 
   5827 static bfd_boolean
   5828 arm_list_new_cmse_stub (struct bfd_hash_entry *gen_entry, void *gen_info)
   5829 {
   5830   struct elf32_arm_stub_hash_entry *stub_entry;
   5831   struct bfd_link_info *info;
   5832 
   5833   /* Massage our args to the form they really have.  */
   5834   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
   5835   info = (struct bfd_link_info *) gen_info;
   5836 
   5837   if (info->out_implib_bfd)
   5838     return TRUE;
   5839 
   5840   if (stub_entry->stub_type != arm_stub_cmse_branch_thumb_only)
   5841     return TRUE;
   5842 
   5843   if (stub_entry->stub_offset == (bfd_vma) -1)
   5844     _bfd_error_handler ("  %s", stub_entry->output_name);
   5845 
   5846   return TRUE;
   5847 }
   5848 
   5849 /* Set offset of each secure gateway veneers so that its address remain
   5850    identical to the one in the input import library referred by
   5851    HTAB->in_implib_bfd.  A warning is issued for veneers that disappeared
   5852    (present in input import library but absent from the executable being
   5853    linked) or if new veneers appeared and there is no output import library
   5854    (INFO->out_implib_bfd is NULL and *CMSE_STUB_CREATED is bigger than the
   5855    number of secure gateway veneers found in the input import library.
   5856 
   5857    The function returns whether an error occurred.  If no error occurred,
   5858    *CMSE_STUB_CREATED gives the number of SG veneers created by both cmse_scan
   5859    and this function and HTAB->new_cmse_stub_offset is set to the biggest
   5860    veneer observed set for new veneers to be layed out after.  */
   5861 
   5862 static bfd_boolean
   5863 set_cmse_veneer_addr_from_implib (struct bfd_link_info *info,
   5864 				  struct elf32_arm_link_hash_table *htab,
   5865 				  int *cmse_stub_created)
   5866 {
   5867   long symsize;
   5868   char *sym_name;
   5869   flagword flags;
   5870   long i, symcount;
   5871   bfd *in_implib_bfd;
   5872   asection *stub_out_sec;
   5873   bfd_boolean ret = TRUE;
   5874   Elf_Internal_Sym *intsym;
   5875   const char *out_sec_name;
   5876   bfd_size_type cmse_stub_size;
   5877   asymbol **sympp = NULL, *sym;
   5878   struct elf32_arm_link_hash_entry *hash;
   5879   const insn_sequence *cmse_stub_template;
   5880   struct elf32_arm_stub_hash_entry *stub_entry;
   5881   int cmse_stub_template_size, new_cmse_stubs_created = *cmse_stub_created;
   5882   bfd_vma veneer_value, stub_offset, next_cmse_stub_offset;
   5883   bfd_vma cmse_stub_array_start = (bfd_vma) -1, cmse_stub_sec_vma = 0;
   5884 
   5885   /* No input secure gateway import library.  */
   5886   if (!htab->in_implib_bfd)
   5887     return TRUE;
   5888 
   5889   in_implib_bfd = htab->in_implib_bfd;
   5890   if (!htab->cmse_implib)
   5891     {
   5892       _bfd_error_handler (_("%B: --in-implib only supported for Secure "
   5893 			    "Gateway import libraries."), in_implib_bfd);
   5894       return FALSE;
   5895     }
   5896 
   5897   /* Get symbol table size.  */
   5898   symsize = bfd_get_symtab_upper_bound (in_implib_bfd);
   5899   if (symsize < 0)
   5900     return FALSE;
   5901 
   5902   /* Read in the input secure gateway import library's symbol table.  */
   5903   sympp = (asymbol **) xmalloc (symsize);
   5904   symcount = bfd_canonicalize_symtab (in_implib_bfd, sympp);
   5905   if (symcount < 0)
   5906     {
   5907       ret = FALSE;
   5908       goto free_sym_buf;
   5909     }
   5910 
   5911   htab->new_cmse_stub_offset = 0;
   5912   cmse_stub_size =
   5913     find_stub_size_and_template (arm_stub_cmse_branch_thumb_only,
   5914 				 &cmse_stub_template,
   5915 				 &cmse_stub_template_size);
   5916   out_sec_name =
   5917     arm_dedicated_stub_output_section_name (arm_stub_cmse_branch_thumb_only);
   5918   stub_out_sec =
   5919     bfd_get_section_by_name (htab->obfd, out_sec_name);
   5920   if (stub_out_sec != NULL)
   5921     cmse_stub_sec_vma = stub_out_sec->vma;
   5922 
   5923   /* Set addresses of veneers mentionned in input secure gateway import
   5924      library's symbol table.  */
   5925   for (i = 0; i < symcount; i++)
   5926     {
   5927       sym = sympp[i];
   5928       flags = sym->flags;
   5929       sym_name = (char *) bfd_asymbol_name (sym);
   5930       intsym = &((elf_symbol_type *) sym)->internal_elf_sym;
   5931 
   5932       if (sym->section != bfd_abs_section_ptr
   5933 	  || !(flags & (BSF_GLOBAL | BSF_WEAK))
   5934 	  || (flags & BSF_FUNCTION) != BSF_FUNCTION
   5935 	  || (ARM_GET_SYM_BRANCH_TYPE (intsym->st_target_internal)
   5936 	      != ST_BRANCH_TO_THUMB))
   5937 	{
   5938 	  _bfd_error_handler (_("%B: invalid import library entry: `%s'."),
   5939 			      in_implib_bfd, sym_name);
   5940 	  _bfd_error_handler (_("Symbol should be absolute, global and "
   5941 				"refer to Thumb functions."));
   5942 	  ret = FALSE;
   5943 	  continue;
   5944 	}
   5945 
   5946       veneer_value = bfd_asymbol_value (sym);
   5947       stub_offset = veneer_value - cmse_stub_sec_vma;
   5948       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, sym_name,
   5949 					 FALSE, FALSE);
   5950       hash = (struct elf32_arm_link_hash_entry *)
   5951 	elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
   5952 
   5953       /* Stub entry should have been created by cmse_scan or the symbol be of
   5954 	 a secure function callable from non secure code.  */
   5955       if (!stub_entry && !hash)
   5956 	{
   5957 	  bfd_boolean new_stub;
   5958 
   5959 	  _bfd_error_handler
   5960 	    (_("Entry function `%s' disappeared from secure code."), sym_name);
   5961 	  hash = (struct elf32_arm_link_hash_entry *)
   5962 	    elf_link_hash_lookup (&(htab)->root, sym_name, TRUE, TRUE, TRUE);
   5963 	  stub_entry
   5964 	    = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
   5965 				     NULL, NULL, bfd_abs_section_ptr, hash,
   5966 				     sym_name, veneer_value,
   5967 				     ST_BRANCH_TO_THUMB, &new_stub);
   5968 	  if (stub_entry == NULL)
   5969 	    ret = FALSE;
   5970 	  else
   5971 	  {
   5972 	    BFD_ASSERT (new_stub);
   5973 	    new_cmse_stubs_created++;
   5974 	    (*cmse_stub_created)++;
   5975 	  }
   5976 	  stub_entry->stub_template_size = stub_entry->stub_size = 0;
   5977 	  stub_entry->stub_offset = stub_offset;
   5978 	}
   5979       /* Symbol found is not callable from non secure code.  */
   5980       else if (!stub_entry)
   5981 	{
   5982 	  if (!cmse_entry_fct_p (hash))
   5983 	    {
   5984 	      _bfd_error_handler (_("`%s' refers to a non entry function."),
   5985 				  sym_name);
   5986 	      ret = FALSE;
   5987 	    }
   5988 	  continue;
   5989 	}
   5990       else
   5991 	{
   5992 	  /* Only stubs for SG veneers should have been created.  */
   5993 	  BFD_ASSERT (stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
   5994 
   5995 	  /* Check visibility hasn't changed.  */
   5996 	  if (!!(flags & BSF_GLOBAL)
   5997 	      != (hash->root.root.type == bfd_link_hash_defined))
   5998 	    _bfd_error_handler
   5999 	      (_("%B: visibility of symbol `%s' has changed."), in_implib_bfd,
   6000 	       sym_name);
   6001 
   6002 	  stub_entry->stub_offset = stub_offset;
   6003 	}
   6004 
   6005       /* Size should match that of a SG veneer.  */
   6006       if (intsym->st_size != cmse_stub_size)
   6007 	{
   6008 	  _bfd_error_handler (_("%B: incorrect size for symbol `%s'."),
   6009 			      in_implib_bfd, sym_name);
   6010 	  ret = FALSE;
   6011 	}
   6012 
   6013       /* Previous veneer address is before current SG veneer section.  */
   6014       if (veneer_value < cmse_stub_sec_vma)
   6015 	{
   6016 	  /* Avoid offset underflow.  */
   6017 	  if (stub_entry)
   6018 	    stub_entry->stub_offset = 0;
   6019 	  stub_offset = 0;
   6020 	  ret = FALSE;
   6021 	}
   6022 
   6023       /* Complain if stub offset not a multiple of stub size.  */
   6024       if (stub_offset % cmse_stub_size)
   6025 	{
   6026 	  _bfd_error_handler
   6027 	    (_("Offset of veneer for entry function `%s' not a multiple of "
   6028 	       "its size."), sym_name);
   6029 	  ret = FALSE;
   6030 	}
   6031 
   6032       if (!ret)
   6033 	continue;
   6034 
   6035       new_cmse_stubs_created--;
   6036       if (veneer_value < cmse_stub_array_start)
   6037 	cmse_stub_array_start = veneer_value;
   6038       next_cmse_stub_offset = stub_offset + ((cmse_stub_size + 7) & ~7);
   6039       if (next_cmse_stub_offset > htab->new_cmse_stub_offset)
   6040 	htab->new_cmse_stub_offset = next_cmse_stub_offset;
   6041     }
   6042 
   6043   if (!info->out_implib_bfd && new_cmse_stubs_created != 0)
   6044     {
   6045       BFD_ASSERT (new_cmse_stubs_created > 0);
   6046       _bfd_error_handler
   6047 	(_("new entry function(s) introduced but no output import library "
   6048 	   "specified:"));
   6049       bfd_hash_traverse (&htab->stub_hash_table, arm_list_new_cmse_stub, info);
   6050     }
   6051 
   6052   if (cmse_stub_array_start != cmse_stub_sec_vma)
   6053     {
   6054       _bfd_error_handler
   6055 	(_("Start address of `%s' is different from previous link."),
   6056 	 out_sec_name);
   6057       ret = FALSE;
   6058     }
   6059 
   6060 free_sym_buf:
   6061   free (sympp);
   6062   return ret;
   6063 }
   6064 
   6065 /* Determine and set the size of the stub section for a final link.
   6066 
   6067    The basic idea here is to examine all the relocations looking for
   6068    PC-relative calls to a target that is unreachable with a "bl"
   6069    instruction.  */
   6070 
   6071 bfd_boolean
   6072 elf32_arm_size_stubs (bfd *output_bfd,
   6073 		      bfd *stub_bfd,
   6074 		      struct bfd_link_info *info,
   6075 		      bfd_signed_vma group_size,
   6076 		      asection * (*add_stub_section) (const char *, asection *,
   6077 						      asection *,
   6078 						      unsigned int),
   6079 		      void (*layout_sections_again) (void))
   6080 {
   6081   bfd_boolean ret = TRUE;
   6082   obj_attribute *out_attr;
   6083   int cmse_stub_created = 0;
   6084   bfd_size_type stub_group_size;
   6085   bfd_boolean m_profile, stubs_always_after_branch, first_veneer_scan = TRUE;
   6086   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
   6087   struct a8_erratum_fix *a8_fixes = NULL;
   6088   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
   6089   struct a8_erratum_reloc *a8_relocs = NULL;
   6090   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
   6091 
   6092   if (htab == NULL)
   6093     return FALSE;
   6094 
   6095   if (htab->fix_cortex_a8)
   6096     {
   6097       a8_fixes = (struct a8_erratum_fix *)
   6098 	  bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
   6099       a8_relocs = (struct a8_erratum_reloc *)
   6100 	  bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
   6101     }
   6102 
   6103   /* Propagate mach to stub bfd, because it may not have been
   6104      finalized when we created stub_bfd.  */
   6105   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
   6106 		     bfd_get_mach (output_bfd));
   6107 
   6108   /* Stash our params away.  */
   6109   htab->stub_bfd = stub_bfd;
   6110   htab->add_stub_section = add_stub_section;
   6111   htab->layout_sections_again = layout_sections_again;
   6112   stubs_always_after_branch = group_size < 0;
   6113 
   6114   out_attr = elf_known_obj_attributes_proc (output_bfd);
   6115   m_profile = out_attr[Tag_CPU_arch_profile].i == 'M';
   6116 
   6117   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
   6118      as the first half of a 32-bit branch straddling two 4K pages.  This is a
   6119      crude way of enforcing that.  */
   6120   if (htab->fix_cortex_a8)
   6121     stubs_always_after_branch = 1;
   6122 
   6123   if (group_size < 0)
   6124     stub_group_size = -group_size;
   6125   else
   6126     stub_group_size = group_size;
   6127 
   6128   if (stub_group_size == 1)
   6129     {
   6130       /* Default values.  */
   6131       /* Thumb branch range is +-4MB has to be used as the default
   6132 	 maximum size (a given section can contain both ARM and Thumb
   6133 	 code, so the worst case has to be taken into account).
   6134 
   6135 	 This value is 24K less than that, which allows for 2025
   6136 	 12-byte stubs.  If we exceed that, then we will fail to link.
   6137 	 The user will have to relink with an explicit group size
   6138 	 option.  */
   6139       stub_group_size = 4170000;
   6140     }
   6141 
   6142   group_sections (htab, stub_group_size, stubs_always_after_branch);
   6143 
   6144   /* If we're applying the cortex A8 fix, we need to determine the
   6145      program header size now, because we cannot change it later --
   6146      that could alter section placements.  Notice the A8 erratum fix
   6147      ends up requiring the section addresses to remain unchanged
   6148      modulo the page size.  That's something we cannot represent
   6149      inside BFD, and we don't want to force the section alignment to
   6150      be the page size.  */
   6151   if (htab->fix_cortex_a8)
   6152     (*htab->layout_sections_again) ();
   6153 
   6154   while (1)
   6155     {
   6156       bfd *input_bfd;
   6157       unsigned int bfd_indx;
   6158       asection *stub_sec;
   6159       enum elf32_arm_stub_type stub_type;
   6160       bfd_boolean stub_changed = FALSE;
   6161       unsigned prev_num_a8_fixes = num_a8_fixes;
   6162 
   6163       num_a8_fixes = 0;
   6164       for (input_bfd = info->input_bfds, bfd_indx = 0;
   6165 	   input_bfd != NULL;
   6166 	   input_bfd = input_bfd->link.next, bfd_indx++)
   6167 	{
   6168 	  Elf_Internal_Shdr *symtab_hdr;
   6169 	  asection *section;
   6170 	  Elf_Internal_Sym *local_syms = NULL;
   6171 
   6172 	  if (!is_arm_elf (input_bfd))
   6173 	    continue;
   6174 
   6175 	  num_a8_relocs = 0;
   6176 
   6177 	  /* We'll need the symbol table in a second.  */
   6178 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   6179 	  if (symtab_hdr->sh_info == 0)
   6180 	    continue;
   6181 
   6182 	  /* Limit scan of symbols to object file whose profile is
   6183 	     Microcontroller to not hinder performance in the general case.  */
   6184 	  if (m_profile && first_veneer_scan)
   6185 	    {
   6186 	      struct elf_link_hash_entry **sym_hashes;
   6187 
   6188 	      sym_hashes = elf_sym_hashes (input_bfd);
   6189 	      if (!cmse_scan (input_bfd, htab, out_attr, sym_hashes,
   6190 			      &cmse_stub_created))
   6191 		goto error_ret_free_local;
   6192 
   6193 	      if (cmse_stub_created != 0)
   6194 		stub_changed = TRUE;
   6195 	    }
   6196 
   6197 	  /* Walk over each section attached to the input bfd.  */
   6198 	  for (section = input_bfd->sections;
   6199 	       section != NULL;
   6200 	       section = section->next)
   6201 	    {
   6202 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   6203 
   6204 	      /* If there aren't any relocs, then there's nothing more
   6205 		 to do.  */
   6206 	      if ((section->flags & SEC_RELOC) == 0
   6207 		  || section->reloc_count == 0
   6208 		  || (section->flags & SEC_CODE) == 0)
   6209 		continue;
   6210 
   6211 	      /* If this section is a link-once section that will be
   6212 		 discarded, then don't create any stubs.  */
   6213 	      if (section->output_section == NULL
   6214 		  || section->output_section->owner != output_bfd)
   6215 		continue;
   6216 
   6217 	      /* Get the relocs.  */
   6218 	      internal_relocs
   6219 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL,
   6220 					     NULL, info->keep_memory);
   6221 	      if (internal_relocs == NULL)
   6222 		goto error_ret_free_local;
   6223 
   6224 	      /* Now examine each relocation.  */
   6225 	      irela = internal_relocs;
   6226 	      irelaend = irela + section->reloc_count;
   6227 	      for (; irela < irelaend; irela++)
   6228 		{
   6229 		  unsigned int r_type, r_indx;
   6230 		  asection *sym_sec;
   6231 		  bfd_vma sym_value;
   6232 		  bfd_vma destination;
   6233 		  struct elf32_arm_link_hash_entry *hash;
   6234 		  const char *sym_name;
   6235 		  unsigned char st_type;
   6236 		  enum arm_st_branch_type branch_type;
   6237 		  bfd_boolean created_stub = FALSE;
   6238 
   6239 		  r_type = ELF32_R_TYPE (irela->r_info);
   6240 		  r_indx = ELF32_R_SYM (irela->r_info);
   6241 
   6242 		  if (r_type >= (unsigned int) R_ARM_max)
   6243 		    {
   6244 		      bfd_set_error (bfd_error_bad_value);
   6245 		    error_ret_free_internal:
   6246 		      if (elf_section_data (section)->relocs == NULL)
   6247 			free (internal_relocs);
   6248 		    /* Fall through.  */
   6249 		    error_ret_free_local:
   6250 		      if (local_syms != NULL
   6251 			  && (symtab_hdr->contents
   6252 			      != (unsigned char *) local_syms))
   6253 			free (local_syms);
   6254 		      return FALSE;
   6255 		    }
   6256 
   6257 		  hash = NULL;
   6258 		  if (r_indx >= symtab_hdr->sh_info)
   6259 		    hash = elf32_arm_hash_entry
   6260 		      (elf_sym_hashes (input_bfd)
   6261 		       [r_indx - symtab_hdr->sh_info]);
   6262 
   6263 		  /* Only look for stubs on branch instructions, or
   6264 		     non-relaxed TLSCALL  */
   6265 		  if ((r_type != (unsigned int) R_ARM_CALL)
   6266 		      && (r_type != (unsigned int) R_ARM_THM_CALL)
   6267 		      && (r_type != (unsigned int) R_ARM_JUMP24)
   6268 		      && (r_type != (unsigned int) R_ARM_THM_JUMP19)
   6269 		      && (r_type != (unsigned int) R_ARM_THM_XPC22)
   6270 		      && (r_type != (unsigned int) R_ARM_THM_JUMP24)
   6271 		      && (r_type != (unsigned int) R_ARM_PLT32)
   6272 		      && !((r_type == (unsigned int) R_ARM_TLS_CALL
   6273 			    || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
   6274 			   && r_type == elf32_arm_tls_transition
   6275 			       (info, r_type, &hash->root)
   6276 			   && ((hash ? hash->tls_type
   6277 				: (elf32_arm_local_got_tls_type
   6278 				   (input_bfd)[r_indx]))
   6279 			       & GOT_TLS_GDESC) != 0))
   6280 		    continue;
   6281 
   6282 		  /* Now determine the call target, its name, value,
   6283 		     section.  */
   6284 		  sym_sec = NULL;
   6285 		  sym_value = 0;
   6286 		  destination = 0;
   6287 		  sym_name = NULL;
   6288 
   6289 		  if (r_type == (unsigned int) R_ARM_TLS_CALL
   6290 		      || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
   6291 		    {
   6292 		      /* A non-relaxed TLS call.  The target is the
   6293 			 plt-resident trampoline and nothing to do
   6294 			 with the symbol.  */
   6295 		      BFD_ASSERT (htab->tls_trampoline > 0);
   6296 		      sym_sec = htab->root.splt;
   6297 		      sym_value = htab->tls_trampoline;
   6298 		      hash = 0;
   6299 		      st_type = STT_FUNC;
   6300 		      branch_type = ST_BRANCH_TO_ARM;
   6301 		    }
   6302 		  else if (!hash)
   6303 		    {
   6304 		      /* It's a local symbol.  */
   6305 		      Elf_Internal_Sym *sym;
   6306 
   6307 		      if (local_syms == NULL)
   6308 			{
   6309 			  local_syms
   6310 			    = (Elf_Internal_Sym *) symtab_hdr->contents;
   6311 			  if (local_syms == NULL)
   6312 			    local_syms
   6313 			      = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   6314 						      symtab_hdr->sh_info, 0,
   6315 						      NULL, NULL, NULL);
   6316 			  if (local_syms == NULL)
   6317 			    goto error_ret_free_internal;
   6318 			}
   6319 
   6320 		      sym = local_syms + r_indx;
   6321 		      if (sym->st_shndx == SHN_UNDEF)
   6322 			sym_sec = bfd_und_section_ptr;
   6323 		      else if (sym->st_shndx == SHN_ABS)
   6324 			sym_sec = bfd_abs_section_ptr;
   6325 		      else if (sym->st_shndx == SHN_COMMON)
   6326 			sym_sec = bfd_com_section_ptr;
   6327 		      else
   6328 			sym_sec =
   6329 			  bfd_section_from_elf_index (input_bfd, sym->st_shndx);
   6330 
   6331 		      if (!sym_sec)
   6332 			/* This is an undefined symbol.  It can never
   6333 			   be resolved.  */
   6334 			continue;
   6335 
   6336 		      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
   6337 			sym_value = sym->st_value;
   6338 		      destination = (sym_value + irela->r_addend
   6339 				     + sym_sec->output_offset
   6340 				     + sym_sec->output_section->vma);
   6341 		      st_type = ELF_ST_TYPE (sym->st_info);
   6342 		      branch_type =
   6343 			ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
   6344 		      sym_name
   6345 			= bfd_elf_string_from_elf_section (input_bfd,
   6346 							   symtab_hdr->sh_link,
   6347 							   sym->st_name);
   6348 		    }
   6349 		  else
   6350 		    {
   6351 		      /* It's an external symbol.  */
   6352 		      while (hash->root.root.type == bfd_link_hash_indirect
   6353 			     || hash->root.root.type == bfd_link_hash_warning)
   6354 			hash = ((struct elf32_arm_link_hash_entry *)
   6355 				hash->root.root.u.i.link);
   6356 
   6357 		      if (hash->root.root.type == bfd_link_hash_defined
   6358 			  || hash->root.root.type == bfd_link_hash_defweak)
   6359 			{
   6360 			  sym_sec = hash->root.root.u.def.section;
   6361 			  sym_value = hash->root.root.u.def.value;
   6362 
   6363 			  struct elf32_arm_link_hash_table *globals =
   6364 						  elf32_arm_hash_table (info);
   6365 
   6366 			  /* For a destination in a shared library,
   6367 			     use the PLT stub as target address to
   6368 			     decide whether a branch stub is
   6369 			     needed.  */
   6370 			  if (globals != NULL
   6371 			      && globals->root.splt != NULL
   6372 			      && hash != NULL
   6373 			      && hash->root.plt.offset != (bfd_vma) -1)
   6374 			    {
   6375 			      sym_sec = globals->root.splt;
   6376 			      sym_value = hash->root.plt.offset;
   6377 			      if (sym_sec->output_section != NULL)
   6378 				destination = (sym_value
   6379 					       + sym_sec->output_offset
   6380 					       + sym_sec->output_section->vma);
   6381 			    }
   6382 			  else if (sym_sec->output_section != NULL)
   6383 			    destination = (sym_value + irela->r_addend
   6384 					   + sym_sec->output_offset
   6385 					   + sym_sec->output_section->vma);
   6386 			}
   6387 		      else if ((hash->root.root.type == bfd_link_hash_undefined)
   6388 			       || (hash->root.root.type == bfd_link_hash_undefweak))
   6389 			{
   6390 			  /* For a shared library, use the PLT stub as
   6391 			     target address to decide whether a long
   6392 			     branch stub is needed.
   6393 			     For absolute code, they cannot be handled.  */
   6394 			  struct elf32_arm_link_hash_table *globals =
   6395 			    elf32_arm_hash_table (info);
   6396 
   6397 			  if (globals != NULL
   6398 			      && globals->root.splt != NULL
   6399 			      && hash != NULL
   6400 			      && hash->root.plt.offset != (bfd_vma) -1)
   6401 			    {
   6402 			      sym_sec = globals->root.splt;
   6403 			      sym_value = hash->root.plt.offset;
   6404 			      if (sym_sec->output_section != NULL)
   6405 				destination = (sym_value
   6406 					       + sym_sec->output_offset
   6407 					       + sym_sec->output_section->vma);
   6408 			    }
   6409 			  else
   6410 			    continue;
   6411 			}
   6412 		      else
   6413 			{
   6414 			  bfd_set_error (bfd_error_bad_value);
   6415 			  goto error_ret_free_internal;
   6416 			}
   6417 		      st_type = hash->root.type;
   6418 		      branch_type =
   6419 			ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
   6420 		      sym_name = hash->root.root.root.string;
   6421 		    }
   6422 
   6423 		  do
   6424 		    {
   6425 		      bfd_boolean new_stub;
   6426 		      struct elf32_arm_stub_hash_entry *stub_entry;
   6427 
   6428 		      /* Determine what (if any) linker stub is needed.  */
   6429 		      stub_type = arm_type_of_stub (info, section, irela,
   6430 						    st_type, &branch_type,
   6431 						    hash, destination, sym_sec,
   6432 						    input_bfd, sym_name);
   6433 		      if (stub_type == arm_stub_none)
   6434 			break;
   6435 
   6436 		      /* We've either created a stub for this reloc already,
   6437 			 or we are about to.  */
   6438 		      stub_entry =
   6439 			elf32_arm_create_stub (htab, stub_type, section, irela,
   6440 					       sym_sec, hash,
   6441 					       (char *) sym_name, sym_value,
   6442 					       branch_type, &new_stub);
   6443 
   6444 		      created_stub = stub_entry != NULL;
   6445 		      if (!created_stub)
   6446 			goto error_ret_free_internal;
   6447 		      else if (!new_stub)
   6448 			break;
   6449 		      else
   6450 			stub_changed = TRUE;
   6451 		    }
   6452 		  while (0);
   6453 
   6454 		  /* Look for relocations which might trigger Cortex-A8
   6455 		     erratum.  */
   6456 		  if (htab->fix_cortex_a8
   6457 		      && (r_type == (unsigned int) R_ARM_THM_JUMP24
   6458 			  || r_type == (unsigned int) R_ARM_THM_JUMP19
   6459 			  || r_type == (unsigned int) R_ARM_THM_CALL
   6460 			  || r_type == (unsigned int) R_ARM_THM_XPC22))
   6461 		    {
   6462 		      bfd_vma from = section->output_section->vma
   6463 				     + section->output_offset
   6464 				     + irela->r_offset;
   6465 
   6466 		      if ((from & 0xfff) == 0xffe)
   6467 			{
   6468 			  /* Found a candidate.  Note we haven't checked the
   6469 			     destination is within 4K here: if we do so (and
   6470 			     don't create an entry in a8_relocs) we can't tell
   6471 			     that a branch should have been relocated when
   6472 			     scanning later.  */
   6473 			  if (num_a8_relocs == a8_reloc_table_size)
   6474 			    {
   6475 			      a8_reloc_table_size *= 2;
   6476 			      a8_relocs = (struct a8_erratum_reloc *)
   6477 				  bfd_realloc (a8_relocs,
   6478 					       sizeof (struct a8_erratum_reloc)
   6479 					       * a8_reloc_table_size);
   6480 			    }
   6481 
   6482 			  a8_relocs[num_a8_relocs].from = from;
   6483 			  a8_relocs[num_a8_relocs].destination = destination;
   6484 			  a8_relocs[num_a8_relocs].r_type = r_type;
   6485 			  a8_relocs[num_a8_relocs].branch_type = branch_type;
   6486 			  a8_relocs[num_a8_relocs].sym_name = sym_name;
   6487 			  a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
   6488 			  a8_relocs[num_a8_relocs].hash = hash;
   6489 
   6490 			  num_a8_relocs++;
   6491 			}
   6492 		    }
   6493 		}
   6494 
   6495 	      /* We're done with the internal relocs, free them.  */
   6496 	      if (elf_section_data (section)->relocs == NULL)
   6497 		free (internal_relocs);
   6498 	    }
   6499 
   6500 	  if (htab->fix_cortex_a8)
   6501 	    {
   6502 	      /* Sort relocs which might apply to Cortex-A8 erratum.  */
   6503 	      qsort (a8_relocs, num_a8_relocs,
   6504 		     sizeof (struct a8_erratum_reloc),
   6505 		     &a8_reloc_compare);
   6506 
   6507 	      /* Scan for branches which might trigger Cortex-A8 erratum.  */
   6508 	      if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
   6509 					  &num_a8_fixes, &a8_fix_table_size,
   6510 					  a8_relocs, num_a8_relocs,
   6511 					  prev_num_a8_fixes, &stub_changed)
   6512 		  != 0)
   6513 		goto error_ret_free_local;
   6514 	    }
   6515 
   6516 	  if (local_syms != NULL
   6517 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   6518 	    {
   6519 	      if (!info->keep_memory)
   6520 		free (local_syms);
   6521 	      else
   6522 		symtab_hdr->contents = (unsigned char *) local_syms;
   6523 	    }
   6524 	}
   6525 
   6526       if (first_veneer_scan
   6527 	  && !set_cmse_veneer_addr_from_implib (info, htab,
   6528 						&cmse_stub_created))
   6529 	ret = FALSE;
   6530 
   6531       if (prev_num_a8_fixes != num_a8_fixes)
   6532 	stub_changed = TRUE;
   6533 
   6534       if (!stub_changed)
   6535 	break;
   6536 
   6537       /* OK, we've added some stubs.  Find out the new size of the
   6538 	 stub sections.  */
   6539       for (stub_sec = htab->stub_bfd->sections;
   6540 	   stub_sec != NULL;
   6541 	   stub_sec = stub_sec->next)
   6542 	{
   6543 	  /* Ignore non-stub sections.  */
   6544 	  if (!strstr (stub_sec->name, STUB_SUFFIX))
   6545 	    continue;
   6546 
   6547 	  stub_sec->size = 0;
   6548 	}
   6549 
   6550       /* Add new SG veneers after those already in the input import
   6551 	 library.  */
   6552       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
   6553 	   stub_type++)
   6554 	{
   6555 	  bfd_vma *start_offset_p;
   6556 	  asection **stub_sec_p;
   6557 
   6558 	  start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
   6559 	  stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
   6560 	  if (start_offset_p == NULL)
   6561 	    continue;
   6562 
   6563 	  BFD_ASSERT (stub_sec_p != NULL);
   6564 	  if (*stub_sec_p != NULL)
   6565 	    (*stub_sec_p)->size = *start_offset_p;
   6566 	}
   6567 
   6568       /* Compute stub section size, considering padding.  */
   6569       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
   6570       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
   6571 	   stub_type++)
   6572 	{
   6573 	  int size, padding;
   6574 	  asection **stub_sec_p;
   6575 
   6576 	  padding = arm_dedicated_stub_section_padding (stub_type);
   6577 	  stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
   6578 	  /* Skip if no stub input section or no stub section padding
   6579 	     required.  */
   6580 	  if ((stub_sec_p != NULL && *stub_sec_p == NULL) || padding == 0)
   6581 	    continue;
   6582 	  /* Stub section padding required but no dedicated section.  */
   6583 	  BFD_ASSERT (stub_sec_p);
   6584 
   6585 	  size = (*stub_sec_p)->size;
   6586 	  size = (size + padding - 1) & ~(padding - 1);
   6587 	  (*stub_sec_p)->size = size;
   6588 	}
   6589 
   6590       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
   6591       if (htab->fix_cortex_a8)
   6592 	for (i = 0; i < num_a8_fixes; i++)
   6593 	  {
   6594 	    stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
   6595 			 a8_fixes[i].section, htab, a8_fixes[i].stub_type);
   6596 
   6597 	    if (stub_sec == NULL)
   6598 	      return FALSE;
   6599 
   6600 	    stub_sec->size
   6601 	      += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
   6602 					      NULL);
   6603 	  }
   6604 
   6605 
   6606       /* Ask the linker to do its stuff.  */
   6607       (*htab->layout_sections_again) ();
   6608       first_veneer_scan = FALSE;
   6609     }
   6610 
   6611   /* Add stubs for Cortex-A8 erratum fixes now.  */
   6612   if (htab->fix_cortex_a8)
   6613     {
   6614       for (i = 0; i < num_a8_fixes; i++)
   6615 	{
   6616 	  struct elf32_arm_stub_hash_entry *stub_entry;
   6617 	  char *stub_name = a8_fixes[i].stub_name;
   6618 	  asection *section = a8_fixes[i].section;
   6619 	  unsigned int section_id = a8_fixes[i].section->id;
   6620 	  asection *link_sec = htab->stub_group[section_id].link_sec;
   6621 	  asection *stub_sec = htab->stub_group[section_id].stub_sec;
   6622 	  const insn_sequence *template_sequence;
   6623 	  int template_size, size = 0;
   6624 
   6625 	  stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   6626 					     TRUE, FALSE);
   6627 	  if (stub_entry == NULL)
   6628 	    {
   6629 	      _bfd_error_handler (_("%B: cannot create stub entry %s"),
   6630 				  section->owner, stub_name);
   6631 	      return FALSE;
   6632 	    }
   6633 
   6634 	  stub_entry->stub_sec = stub_sec;
   6635 	  stub_entry->stub_offset = (bfd_vma) -1;
   6636 	  stub_entry->id_sec = link_sec;
   6637 	  stub_entry->stub_type = a8_fixes[i].stub_type;
   6638 	  stub_entry->source_value = a8_fixes[i].offset;
   6639 	  stub_entry->target_section = a8_fixes[i].section;
   6640 	  stub_entry->target_value = a8_fixes[i].target_offset;
   6641 	  stub_entry->orig_insn = a8_fixes[i].orig_insn;
   6642 	  stub_entry->branch_type = a8_fixes[i].branch_type;
   6643 
   6644 	  size = find_stub_size_and_template (a8_fixes[i].stub_type,
   6645 					      &template_sequence,
   6646 					      &template_size);
   6647 
   6648 	  stub_entry->stub_size = size;
   6649 	  stub_entry->stub_template = template_sequence;
   6650 	  stub_entry->stub_template_size = template_size;
   6651 	}
   6652 
   6653       /* Stash the Cortex-A8 erratum fix array for use later in
   6654 	 elf32_arm_write_section().  */
   6655       htab->a8_erratum_fixes = a8_fixes;
   6656       htab->num_a8_erratum_fixes = num_a8_fixes;
   6657     }
   6658   else
   6659     {
   6660       htab->a8_erratum_fixes = NULL;
   6661       htab->num_a8_erratum_fixes = 0;
   6662     }
   6663   return ret;
   6664 }
   6665 
   6666 /* Build all the stubs associated with the current output file.  The
   6667    stubs are kept in a hash table attached to the main linker hash
   6668    table.  We also set up the .plt entries for statically linked PIC
   6669    functions here.  This function is called via arm_elf_finish in the
   6670    linker.  */
   6671 
   6672 bfd_boolean
   6673 elf32_arm_build_stubs (struct bfd_link_info *info)
   6674 {
   6675   asection *stub_sec;
   6676   struct bfd_hash_table *table;
   6677   enum elf32_arm_stub_type stub_type;
   6678   struct elf32_arm_link_hash_table *htab;
   6679 
   6680   htab = elf32_arm_hash_table (info);
   6681   if (htab == NULL)
   6682     return FALSE;
   6683 
   6684   for (stub_sec = htab->stub_bfd->sections;
   6685        stub_sec != NULL;
   6686        stub_sec = stub_sec->next)
   6687     {
   6688       bfd_size_type size;
   6689 
   6690       /* Ignore non-stub sections.  */
   6691       if (!strstr (stub_sec->name, STUB_SUFFIX))
   6692 	continue;
   6693 
   6694       /* Allocate memory to hold the linker stubs.  Zeroing the stub sections
   6695 	 must at least be done for stub section requiring padding and for SG
   6696 	 veneers to ensure that a non secure code branching to a removed SG
   6697 	 veneer causes an error.  */
   6698       size = stub_sec->size;
   6699       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
   6700       if (stub_sec->contents == NULL && size != 0)
   6701 	return FALSE;
   6702 
   6703       stub_sec->size = 0;
   6704     }
   6705 
   6706   /* Add new SG veneers after those already in the input import library.  */
   6707   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
   6708     {
   6709       bfd_vma *start_offset_p;
   6710       asection **stub_sec_p;
   6711 
   6712       start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
   6713       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
   6714       if (start_offset_p == NULL)
   6715 	continue;
   6716 
   6717       BFD_ASSERT (stub_sec_p != NULL);
   6718       if (*stub_sec_p != NULL)
   6719 	(*stub_sec_p)->size = *start_offset_p;
   6720     }
   6721 
   6722   /* Build the stubs as directed by the stub hash table.  */
   6723   table = &htab->stub_hash_table;
   6724   bfd_hash_traverse (table, arm_build_one_stub, info);
   6725   if (htab->fix_cortex_a8)
   6726     {
   6727       /* Place the cortex a8 stubs last.  */
   6728       htab->fix_cortex_a8 = -1;
   6729       bfd_hash_traverse (table, arm_build_one_stub, info);
   6730     }
   6731 
   6732   return TRUE;
   6733 }
   6734 
   6735 /* Locate the Thumb encoded calling stub for NAME.  */
   6736 
   6737 static struct elf_link_hash_entry *
   6738 find_thumb_glue (struct bfd_link_info *link_info,
   6739 		 const char *name,
   6740 		 char **error_message)
   6741 {
   6742   char *tmp_name;
   6743   struct elf_link_hash_entry *hash;
   6744   struct elf32_arm_link_hash_table *hash_table;
   6745 
   6746   /* We need a pointer to the armelf specific hash table.  */
   6747   hash_table = elf32_arm_hash_table (link_info);
   6748   if (hash_table == NULL)
   6749     return NULL;
   6750 
   6751   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
   6752 				  + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
   6753 
   6754   BFD_ASSERT (tmp_name);
   6755 
   6756   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
   6757 
   6758   hash = elf_link_hash_lookup
   6759     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
   6760 
   6761   if (hash == NULL
   6762       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
   6763 		   tmp_name, name) == -1)
   6764     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
   6765 
   6766   free (tmp_name);
   6767 
   6768   return hash;
   6769 }
   6770 
   6771 /* Locate the ARM encoded calling stub for NAME.  */
   6772 
   6773 static struct elf_link_hash_entry *
   6774 find_arm_glue (struct bfd_link_info *link_info,
   6775 	       const char *name,
   6776 	       char **error_message)
   6777 {
   6778   char *tmp_name;
   6779   struct elf_link_hash_entry *myh;
   6780   struct elf32_arm_link_hash_table *hash_table;
   6781 
   6782   /* We need a pointer to the elfarm specific hash table.  */
   6783   hash_table = elf32_arm_hash_table (link_info);
   6784   if (hash_table == NULL)
   6785     return NULL;
   6786 
   6787   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
   6788 				  + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
   6789 
   6790   BFD_ASSERT (tmp_name);
   6791 
   6792   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
   6793 
   6794   myh = elf_link_hash_lookup
   6795     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
   6796 
   6797   if (myh == NULL
   6798       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
   6799 		   tmp_name, name) == -1)
   6800     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
   6801 
   6802   free (tmp_name);
   6803 
   6804   return myh;
   6805 }
   6806 
   6807 /* ARM->Thumb glue (static images):
   6808 
   6809    .arm
   6810    __func_from_arm:
   6811    ldr r12, __func_addr
   6812    bx  r12
   6813    __func_addr:
   6814    .word func    @ behave as if you saw a ARM_32 reloc.
   6815 
   6816    (v5t static images)
   6817    .arm
   6818    __func_from_arm:
   6819    ldr pc, __func_addr
   6820    __func_addr:
   6821    .word func    @ behave as if you saw a ARM_32 reloc.
   6822 
   6823    (relocatable images)
   6824    .arm
   6825    __func_from_arm:
   6826    ldr r12, __func_offset
   6827    add r12, r12, pc
   6828    bx  r12
   6829    __func_offset:
   6830    .word func - .   */
   6831 
   6832 #define ARM2THUMB_STATIC_GLUE_SIZE 12
   6833 static const insn32 a2t1_ldr_insn = 0xe59fc000;
   6834 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
   6835 static const insn32 a2t3_func_addr_insn = 0x00000001;
   6836 
   6837 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
   6838 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
   6839 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
   6840 
   6841 #define ARM2THUMB_PIC_GLUE_SIZE 16
   6842 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
   6843 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
   6844 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
   6845 
   6846 /* Thumb->ARM:				Thumb->(non-interworking aware) ARM
   6847 
   6848      .thumb				.thumb
   6849      .align 2				.align 2
   6850  __func_from_thumb:		    __func_from_thumb:
   6851      bx pc				push {r6, lr}
   6852      nop				ldr  r6, __func_addr
   6853      .arm				mov  lr, pc
   6854      b func				bx   r6
   6855 					.arm
   6856 				    ;; back_to_thumb
   6857 					ldmia r13! {r6, lr}
   6858 					bx    lr
   6859 				    __func_addr:
   6860 					.word	     func  */
   6861 
   6862 #define THUMB2ARM_GLUE_SIZE 8
   6863 static const insn16 t2a1_bx_pc_insn = 0x4778;
   6864 static const insn16 t2a2_noop_insn = 0x46c0;
   6865 static const insn32 t2a3_b_insn = 0xea000000;
   6866 
   6867 #define VFP11_ERRATUM_VENEER_SIZE 8
   6868 #define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
   6869 #define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
   6870 
   6871 #define ARM_BX_VENEER_SIZE 12
   6872 static const insn32 armbx1_tst_insn = 0xe3100001;
   6873 static const insn32 armbx2_moveq_insn = 0x01a0f000;
   6874 static const insn32 armbx3_bx_insn = 0xe12fff10;
   6875 
   6876 #ifndef ELFARM_NABI_C_INCLUDED
   6877 static void
   6878 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
   6879 {
   6880   asection * s;
   6881   bfd_byte * contents;
   6882 
   6883   if (size == 0)
   6884     {
   6885       /* Do not include empty glue sections in the output.  */
   6886       if (abfd != NULL)
   6887 	{
   6888 	  s = bfd_get_linker_section (abfd, name);
   6889 	  if (s != NULL)
   6890 	    s->flags |= SEC_EXCLUDE;
   6891 	}
   6892       return;
   6893     }
   6894 
   6895   BFD_ASSERT (abfd != NULL);
   6896 
   6897   s = bfd_get_linker_section (abfd, name);
   6898   BFD_ASSERT (s != NULL);
   6899 
   6900   contents = (bfd_byte *) bfd_alloc (abfd, size);
   6901 
   6902   BFD_ASSERT (s->size == size);
   6903   s->contents = contents;
   6904 }
   6905 
   6906 bfd_boolean
   6907 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
   6908 {
   6909   struct elf32_arm_link_hash_table * globals;
   6910 
   6911   globals = elf32_arm_hash_table (info);
   6912   BFD_ASSERT (globals != NULL);
   6913 
   6914   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
   6915 				   globals->arm_glue_size,
   6916 				   ARM2THUMB_GLUE_SECTION_NAME);
   6917 
   6918   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
   6919 				   globals->thumb_glue_size,
   6920 				   THUMB2ARM_GLUE_SECTION_NAME);
   6921 
   6922   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
   6923 				   globals->vfp11_erratum_glue_size,
   6924 				   VFP11_ERRATUM_VENEER_SECTION_NAME);
   6925 
   6926   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
   6927 				   globals->stm32l4xx_erratum_glue_size,
   6928 				   STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
   6929 
   6930   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
   6931 				   globals->bx_glue_size,
   6932 				   ARM_BX_GLUE_SECTION_NAME);
   6933 
   6934   return TRUE;
   6935 }
   6936 
   6937 /* Allocate space and symbols for calling a Thumb function from Arm mode.
   6938    returns the symbol identifying the stub.  */
   6939 
   6940 static struct elf_link_hash_entry *
   6941 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
   6942 			  struct elf_link_hash_entry * h)
   6943 {
   6944   const char * name = h->root.root.string;
   6945   asection * s;
   6946   char * tmp_name;
   6947   struct elf_link_hash_entry * myh;
   6948   struct bfd_link_hash_entry * bh;
   6949   struct elf32_arm_link_hash_table * globals;
   6950   bfd_vma val;
   6951   bfd_size_type size;
   6952 
   6953   globals = elf32_arm_hash_table (link_info);
   6954   BFD_ASSERT (globals != NULL);
   6955   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   6956 
   6957   s = bfd_get_linker_section
   6958     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
   6959 
   6960   BFD_ASSERT (s != NULL);
   6961 
   6962   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
   6963 				  + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
   6964 
   6965   BFD_ASSERT (tmp_name);
   6966 
   6967   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
   6968 
   6969   myh = elf_link_hash_lookup
   6970     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
   6971 
   6972   if (myh != NULL)
   6973     {
   6974       /* We've already seen this guy.  */
   6975       free (tmp_name);
   6976       return myh;
   6977     }
   6978 
   6979   /* The only trick here is using hash_table->arm_glue_size as the value.
   6980      Even though the section isn't allocated yet, this is where we will be
   6981      putting it.  The +1 on the value marks that the stub has not been
   6982      output yet - not that it is a Thumb function.  */
   6983   bh = NULL;
   6984   val = globals->arm_glue_size + 1;
   6985   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
   6986 				    tmp_name, BSF_GLOBAL, s, val,
   6987 				    NULL, TRUE, FALSE, &bh);
   6988 
   6989   myh = (struct elf_link_hash_entry *) bh;
   6990   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   6991   myh->forced_local = 1;
   6992 
   6993   free (tmp_name);
   6994 
   6995   if (bfd_link_pic (link_info)
   6996       || globals->root.is_relocatable_executable
   6997       || globals->pic_veneer)
   6998     size = ARM2THUMB_PIC_GLUE_SIZE;
   6999   else if (globals->use_blx)
   7000     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
   7001   else
   7002     size = ARM2THUMB_STATIC_GLUE_SIZE;
   7003 
   7004   s->size += size;
   7005   globals->arm_glue_size += size;
   7006 
   7007   return myh;
   7008 }
   7009 
   7010 /* Allocate space for ARMv4 BX veneers.  */
   7011 
   7012 static void
   7013 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
   7014 {
   7015   asection * s;
   7016   struct elf32_arm_link_hash_table *globals;
   7017   char *tmp_name;
   7018   struct elf_link_hash_entry *myh;
   7019   struct bfd_link_hash_entry *bh;
   7020   bfd_vma val;
   7021 
   7022   /* BX PC does not need a veneer.  */
   7023   if (reg == 15)
   7024     return;
   7025 
   7026   globals = elf32_arm_hash_table (link_info);
   7027   BFD_ASSERT (globals != NULL);
   7028   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   7029 
   7030   /* Check if this veneer has already been allocated.  */
   7031   if (globals->bx_glue_offset[reg])
   7032     return;
   7033 
   7034   s = bfd_get_linker_section
   7035     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
   7036 
   7037   BFD_ASSERT (s != NULL);
   7038 
   7039   /* Add symbol for veneer.  */
   7040   tmp_name = (char *)
   7041       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
   7042 
   7043   BFD_ASSERT (tmp_name);
   7044 
   7045   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
   7046 
   7047   myh = elf_link_hash_lookup
   7048     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
   7049 
   7050   BFD_ASSERT (myh == NULL);
   7051 
   7052   bh = NULL;
   7053   val = globals->bx_glue_size;
   7054   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
   7055 				    tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
   7056 				    NULL, TRUE, FALSE, &bh);
   7057 
   7058   myh = (struct elf_link_hash_entry *) bh;
   7059   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   7060   myh->forced_local = 1;
   7061 
   7062   s->size += ARM_BX_VENEER_SIZE;
   7063   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
   7064   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
   7065 }
   7066 
   7067 
   7068 /* Add an entry to the code/data map for section SEC.  */
   7069 
   7070 static void
   7071 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
   7072 {
   7073   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
   7074   unsigned int newidx;
   7075 
   7076   if (sec_data->map == NULL)
   7077     {
   7078       sec_data->map = (elf32_arm_section_map *)
   7079 	  bfd_malloc (sizeof (elf32_arm_section_map));
   7080       sec_data->mapcount = 0;
   7081       sec_data->mapsize = 1;
   7082     }
   7083 
   7084   newidx = sec_data->mapcount++;
   7085 
   7086   if (sec_data->mapcount > sec_data->mapsize)
   7087     {
   7088       sec_data->mapsize *= 2;
   7089       sec_data->map = (elf32_arm_section_map *)
   7090 	  bfd_realloc_or_free (sec_data->map, sec_data->mapsize
   7091 			       * sizeof (elf32_arm_section_map));
   7092     }
   7093 
   7094   if (sec_data->map)
   7095     {
   7096       sec_data->map[newidx].vma = vma;
   7097       sec_data->map[newidx].type = type;
   7098     }
   7099 }
   7100 
   7101 
   7102 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
   7103    veneers are handled for now.  */
   7104 
   7105 static bfd_vma
   7106 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
   7107 			     elf32_vfp11_erratum_list *branch,
   7108 			     bfd *branch_bfd,
   7109 			     asection *branch_sec,
   7110 			     unsigned int offset)
   7111 {
   7112   asection *s;
   7113   struct elf32_arm_link_hash_table *hash_table;
   7114   char *tmp_name;
   7115   struct elf_link_hash_entry *myh;
   7116   struct bfd_link_hash_entry *bh;
   7117   bfd_vma val;
   7118   struct _arm_elf_section_data *sec_data;
   7119   elf32_vfp11_erratum_list *newerr;
   7120 
   7121   hash_table = elf32_arm_hash_table (link_info);
   7122   BFD_ASSERT (hash_table != NULL);
   7123   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
   7124 
   7125   s = bfd_get_linker_section
   7126     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
   7127 
   7128   sec_data = elf32_arm_section_data (s);
   7129 
   7130   BFD_ASSERT (s != NULL);
   7131 
   7132   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
   7133 				  (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
   7134 
   7135   BFD_ASSERT (tmp_name);
   7136 
   7137   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
   7138 	   hash_table->num_vfp11_fixes);
   7139 
   7140   myh = elf_link_hash_lookup
   7141     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
   7142 
   7143   BFD_ASSERT (myh == NULL);
   7144 
   7145   bh = NULL;
   7146   val = hash_table->vfp11_erratum_glue_size;
   7147   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
   7148 				    tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
   7149 				    NULL, TRUE, FALSE, &bh);
   7150 
   7151   myh = (struct elf_link_hash_entry *) bh;
   7152   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   7153   myh->forced_local = 1;
   7154 
   7155   /* Link veneer back to calling location.  */
   7156   sec_data->erratumcount += 1;
   7157   newerr = (elf32_vfp11_erratum_list *)
   7158       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
   7159 
   7160   newerr->type = VFP11_ERRATUM_ARM_VENEER;
   7161   newerr->vma = -1;
   7162   newerr->u.v.branch = branch;
   7163   newerr->u.v.id = hash_table->num_vfp11_fixes;
   7164   branch->u.b.veneer = newerr;
   7165 
   7166   newerr->next = sec_data->erratumlist;
   7167   sec_data->erratumlist = newerr;
   7168 
   7169   /* A symbol for the return from the veneer.  */
   7170   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
   7171 	   hash_table->num_vfp11_fixes);
   7172 
   7173   myh = elf_link_hash_lookup
   7174     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
   7175 
   7176   if (myh != NULL)
   7177     abort ();
   7178 
   7179   bh = NULL;
   7180   val = offset + 4;
   7181   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
   7182 				    branch_sec, val, NULL, TRUE, FALSE, &bh);
   7183 
   7184   myh = (struct elf_link_hash_entry *) bh;
   7185   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   7186   myh->forced_local = 1;
   7187 
   7188   free (tmp_name);
   7189 
   7190   /* Generate a mapping symbol for the veneer section, and explicitly add an
   7191      entry for that symbol to the code/data map for the section.  */
   7192   if (hash_table->vfp11_erratum_glue_size == 0)
   7193     {
   7194       bh = NULL;
   7195       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
   7196 	 ever requires this erratum fix.  */
   7197       _bfd_generic_link_add_one_symbol (link_info,
   7198 					hash_table->bfd_of_glue_owner, "$a",
   7199 					BSF_LOCAL, s, 0, NULL,
   7200 					TRUE, FALSE, &bh);
   7201 
   7202       myh = (struct elf_link_hash_entry *) bh;
   7203       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
   7204       myh->forced_local = 1;
   7205 
   7206       /* The elf32_arm_init_maps function only cares about symbols from input
   7207 	 BFDs.  We must make a note of this generated mapping symbol
   7208 	 ourselves so that code byteswapping works properly in
   7209 	 elf32_arm_write_section.  */
   7210       elf32_arm_section_map_add (s, 'a', 0);
   7211     }
   7212 
   7213   s->size += VFP11_ERRATUM_VENEER_SIZE;
   7214   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
   7215   hash_table->num_vfp11_fixes++;
   7216 
   7217   /* The offset of the veneer.  */
   7218   return val;
   7219 }
   7220 
   7221 /* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
   7222    veneers need to be handled because used only in Cortex-M.  */
   7223 
   7224 static bfd_vma
   7225 record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
   7226 				 elf32_stm32l4xx_erratum_list *branch,
   7227 				 bfd *branch_bfd,
   7228 				 asection *branch_sec,
   7229 				 unsigned int offset,
   7230 				 bfd_size_type veneer_size)
   7231 {
   7232   asection *s;
   7233   struct elf32_arm_link_hash_table *hash_table;
   7234   char *tmp_name;
   7235   struct elf_link_hash_entry *myh;
   7236   struct bfd_link_hash_entry *bh;
   7237   bfd_vma val;
   7238   struct _arm_elf_section_data *sec_data;
   7239   elf32_stm32l4xx_erratum_list *newerr;
   7240 
   7241   hash_table = elf32_arm_hash_table (link_info);
   7242   BFD_ASSERT (hash_table != NULL);
   7243   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
   7244 
   7245   s = bfd_get_linker_section
   7246     (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
   7247 
   7248   BFD_ASSERT (s != NULL);
   7249 
   7250   sec_data = elf32_arm_section_data (s);
   7251 
   7252   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
   7253 				  (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
   7254 
   7255   BFD_ASSERT (tmp_name);
   7256 
   7257   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
   7258 	   hash_table->num_stm32l4xx_fixes);
   7259 
   7260   myh = elf_link_hash_lookup
   7261     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
   7262 
   7263   BFD_ASSERT (myh == NULL);
   7264 
   7265   bh = NULL;
   7266   val = hash_table->stm32l4xx_erratum_glue_size;
   7267   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
   7268 				    tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
   7269 				    NULL, TRUE, FALSE, &bh);
   7270 
   7271   myh = (struct elf_link_hash_entry *) bh;
   7272   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   7273   myh->forced_local = 1;
   7274 
   7275   /* Link veneer back to calling location.  */
   7276   sec_data->stm32l4xx_erratumcount += 1;
   7277   newerr = (elf32_stm32l4xx_erratum_list *)
   7278       bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
   7279 
   7280   newerr->type = STM32L4XX_ERRATUM_VENEER;
   7281   newerr->vma = -1;
   7282   newerr->u.v.branch = branch;
   7283   newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
   7284   branch->u.b.veneer = newerr;
   7285 
   7286   newerr->next = sec_data->stm32l4xx_erratumlist;
   7287   sec_data->stm32l4xx_erratumlist = newerr;
   7288 
   7289   /* A symbol for the return from the veneer.  */
   7290   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
   7291 	   hash_table->num_stm32l4xx_fixes);
   7292 
   7293   myh = elf_link_hash_lookup
   7294     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
   7295 
   7296   if (myh != NULL)
   7297     abort ();
   7298 
   7299   bh = NULL;
   7300   val = offset + 4;
   7301   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
   7302 				    branch_sec, val, NULL, TRUE, FALSE, &bh);
   7303 
   7304   myh = (struct elf_link_hash_entry *) bh;
   7305   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   7306   myh->forced_local = 1;
   7307 
   7308   free (tmp_name);
   7309 
   7310   /* Generate a mapping symbol for the veneer section, and explicitly add an
   7311      entry for that symbol to the code/data map for the section.  */
   7312   if (hash_table->stm32l4xx_erratum_glue_size == 0)
   7313     {
   7314       bh = NULL;
   7315       /* Creates a THUMB symbol since there is no other choice.  */
   7316       _bfd_generic_link_add_one_symbol (link_info,
   7317 					hash_table->bfd_of_glue_owner, "$t",
   7318 					BSF_LOCAL, s, 0, NULL,
   7319 					TRUE, FALSE, &bh);
   7320 
   7321       myh = (struct elf_link_hash_entry *) bh;
   7322       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
   7323       myh->forced_local = 1;
   7324 
   7325       /* The elf32_arm_init_maps function only cares about symbols from input
   7326 	 BFDs.  We must make a note of this generated mapping symbol
   7327 	 ourselves so that code byteswapping works properly in
   7328 	 elf32_arm_write_section.  */
   7329       elf32_arm_section_map_add (s, 't', 0);
   7330     }
   7331 
   7332   s->size += veneer_size;
   7333   hash_table->stm32l4xx_erratum_glue_size += veneer_size;
   7334   hash_table->num_stm32l4xx_fixes++;
   7335 
   7336   /* The offset of the veneer.  */
   7337   return val;
   7338 }
   7339 
   7340 #define ARM_GLUE_SECTION_FLAGS \
   7341   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
   7342    | SEC_READONLY | SEC_LINKER_CREATED)
   7343 
   7344 /* Create a fake section for use by the ARM backend of the linker.  */
   7345 
   7346 static bfd_boolean
   7347 arm_make_glue_section (bfd * abfd, const char * name)
   7348 {
   7349   asection * sec;
   7350 
   7351   sec = bfd_get_linker_section (abfd, name);
   7352   if (sec != NULL)
   7353     /* Already made.  */
   7354     return TRUE;
   7355 
   7356   sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
   7357 
   7358   if (sec == NULL
   7359       || !bfd_set_section_alignment (abfd, sec, 2))
   7360     return FALSE;
   7361 
   7362   /* Set the gc mark to prevent the section from being removed by garbage
   7363      collection, despite the fact that no relocs refer to this section.  */
   7364   sec->gc_mark = 1;
   7365 
   7366   return TRUE;
   7367 }
   7368 
   7369 /* Set size of .plt entries.  This function is called from the
   7370    linker scripts in ld/emultempl/{armelf}.em.  */
   7371 
   7372 void
   7373 bfd_elf32_arm_use_long_plt (void)
   7374 {
   7375   elf32_arm_use_long_plt_entry = TRUE;
   7376 }
   7377 
   7378 /* Add the glue sections to ABFD.  This function is called from the
   7379    linker scripts in ld/emultempl/{armelf}.em.  */
   7380 
   7381 bfd_boolean
   7382 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
   7383 					struct bfd_link_info *info)
   7384 {
   7385   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
   7386   bfd_boolean dostm32l4xx = globals
   7387     && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
   7388   bfd_boolean addglue;
   7389 
   7390   /* If we are only performing a partial
   7391      link do not bother adding the glue.  */
   7392   if (bfd_link_relocatable (info))
   7393     return TRUE;
   7394 
   7395   addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
   7396     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
   7397     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
   7398     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
   7399 
   7400   if (!dostm32l4xx)
   7401     return addglue;
   7402 
   7403   return addglue
   7404     && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
   7405 }
   7406 
   7407 /* Mark output sections of veneers needing a dedicated one with SEC_KEEP.  This
   7408    ensures they are not marked for deletion by
   7409    strip_excluded_output_sections () when veneers are going to be created
   7410    later.  Not doing so would trigger assert on empty section size in
   7411    lang_size_sections_1 ().  */
   7412 
   7413 void
   7414 bfd_elf32_arm_keep_private_stub_output_sections (struct bfd_link_info *info)
   7415 {
   7416   enum elf32_arm_stub_type stub_type;
   7417 
   7418   /* If we are only performing a partial
   7419      link do not bother adding the glue.  */
   7420   if (bfd_link_relocatable (info))
   7421     return;
   7422 
   7423   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
   7424     {
   7425       asection *out_sec;
   7426       const char *out_sec_name;
   7427 
   7428       if (!arm_dedicated_stub_output_section_required (stub_type))
   7429 	continue;
   7430 
   7431      out_sec_name = arm_dedicated_stub_output_section_name (stub_type);
   7432      out_sec = bfd_get_section_by_name (info->output_bfd, out_sec_name);
   7433      if (out_sec != NULL)
   7434 	out_sec->flags |= SEC_KEEP;
   7435     }
   7436 }
   7437 
   7438 /* Select a BFD to be used to hold the sections used by the glue code.
   7439    This function is called from the linker scripts in ld/emultempl/
   7440    {armelf/pe}.em.  */
   7441 
   7442 bfd_boolean
   7443 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
   7444 {
   7445   struct elf32_arm_link_hash_table *globals;
   7446 
   7447   /* If we are only performing a partial link
   7448      do not bother getting a bfd to hold the glue.  */
   7449   if (bfd_link_relocatable (info))
   7450     return TRUE;
   7451 
   7452   /* Make sure we don't attach the glue sections to a dynamic object.  */
   7453   BFD_ASSERT (!(abfd->flags & DYNAMIC));
   7454 
   7455   globals = elf32_arm_hash_table (info);
   7456   BFD_ASSERT (globals != NULL);
   7457 
   7458   if (globals->bfd_of_glue_owner != NULL)
   7459     return TRUE;
   7460 
   7461   /* Save the bfd for later use.  */
   7462   globals->bfd_of_glue_owner = abfd;
   7463 
   7464   return TRUE;
   7465 }
   7466 
   7467 static void
   7468 check_use_blx (struct elf32_arm_link_hash_table *globals)
   7469 {
   7470   int cpu_arch;
   7471 
   7472   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
   7473 				       Tag_CPU_arch);
   7474 
   7475   if (globals->fix_arm1176)
   7476     {
   7477       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
   7478 	globals->use_blx = 1;
   7479     }
   7480   else
   7481     {
   7482       if (cpu_arch > TAG_CPU_ARCH_V4T)
   7483 	globals->use_blx = 1;
   7484     }
   7485 }
   7486 
   7487 bfd_boolean
   7488 bfd_elf32_arm_process_before_allocation (bfd *abfd,
   7489 					 struct bfd_link_info *link_info)
   7490 {
   7491   Elf_Internal_Shdr *symtab_hdr;
   7492   Elf_Internal_Rela *internal_relocs = NULL;
   7493   Elf_Internal_Rela *irel, *irelend;
   7494   bfd_byte *contents = NULL;
   7495 
   7496   asection *sec;
   7497   struct elf32_arm_link_hash_table *globals;
   7498 
   7499   /* If we are only performing a partial link do not bother
   7500      to construct any glue.  */
   7501   if (bfd_link_relocatable (link_info))
   7502     return TRUE;
   7503 
   7504   /* Here we have a bfd that is to be included on the link.  We have a
   7505      hook to do reloc rummaging, before section sizes are nailed down.  */
   7506   globals = elf32_arm_hash_table (link_info);
   7507   BFD_ASSERT (globals != NULL);
   7508 
   7509   check_use_blx (globals);
   7510 
   7511   if (globals->byteswap_code && !bfd_big_endian (abfd))
   7512     {
   7513       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
   7514 			  abfd);
   7515       return FALSE;
   7516     }
   7517 
   7518   /* PR 5398: If we have not decided to include any loadable sections in
   7519      the output then we will not have a glue owner bfd.  This is OK, it
   7520      just means that there is nothing else for us to do here.  */
   7521   if (globals->bfd_of_glue_owner == NULL)
   7522     return TRUE;
   7523 
   7524   /* Rummage around all the relocs and map the glue vectors.  */
   7525   sec = abfd->sections;
   7526 
   7527   if (sec == NULL)
   7528     return TRUE;
   7529 
   7530   for (; sec != NULL; sec = sec->next)
   7531     {
   7532       if (sec->reloc_count == 0)
   7533 	continue;
   7534 
   7535       if ((sec->flags & SEC_EXCLUDE) != 0)
   7536 	continue;
   7537 
   7538       symtab_hdr = & elf_symtab_hdr (abfd);
   7539 
   7540       /* Load the relocs.  */
   7541       internal_relocs
   7542 	= _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
   7543 
   7544       if (internal_relocs == NULL)
   7545 	goto error_return;
   7546 
   7547       irelend = internal_relocs + sec->reloc_count;
   7548       for (irel = internal_relocs; irel < irelend; irel++)
   7549 	{
   7550 	  long r_type;
   7551 	  unsigned long r_index;
   7552 
   7553 	  struct elf_link_hash_entry *h;
   7554 
   7555 	  r_type = ELF32_R_TYPE (irel->r_info);
   7556 	  r_index = ELF32_R_SYM (irel->r_info);
   7557 
   7558 	  /* These are the only relocation types we care about.  */
   7559 	  if (   r_type != R_ARM_PC24
   7560 	      && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
   7561 	    continue;
   7562 
   7563 	  /* Get the section contents if we haven't done so already.  */
   7564 	  if (contents == NULL)
   7565 	    {
   7566 	      /* Get cached copy if it exists.  */
   7567 	      if (elf_section_data (sec)->this_hdr.contents != NULL)
   7568 		contents = elf_section_data (sec)->this_hdr.contents;
   7569 	      else
   7570 		{
   7571 		  /* Go get them off disk.  */
   7572 		  if (! bfd_malloc_and_get_section (abfd, sec, &contents))
   7573 		    goto error_return;
   7574 		}
   7575 	    }
   7576 
   7577 	  if (r_type == R_ARM_V4BX)
   7578 	    {
   7579 	      int reg;
   7580 
   7581 	      reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
   7582 	      record_arm_bx_glue (link_info, reg);
   7583 	      continue;
   7584 	    }
   7585 
   7586 	  /* If the relocation is not against a symbol it cannot concern us.  */
   7587 	  h = NULL;
   7588 
   7589 	  /* We don't care about local symbols.  */
   7590 	  if (r_index < symtab_hdr->sh_info)
   7591 	    continue;
   7592 
   7593 	  /* This is an external symbol.  */
   7594 	  r_index -= symtab_hdr->sh_info;
   7595 	  h = (struct elf_link_hash_entry *)
   7596 	    elf_sym_hashes (abfd)[r_index];
   7597 
   7598 	  /* If the relocation is against a static symbol it must be within
   7599 	     the current section and so cannot be a cross ARM/Thumb relocation.  */
   7600 	  if (h == NULL)
   7601 	    continue;
   7602 
   7603 	  /* If the call will go through a PLT entry then we do not need
   7604 	     glue.  */
   7605 	  if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
   7606 	    continue;
   7607 
   7608 	  switch (r_type)
   7609 	    {
   7610 	    case R_ARM_PC24:
   7611 	      /* This one is a call from arm code.  We need to look up
   7612 		 the target of the call.  If it is a thumb target, we
   7613 		 insert glue.  */
   7614 	      if (ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
   7615 		  == ST_BRANCH_TO_THUMB)
   7616 		record_arm_to_thumb_glue (link_info, h);
   7617 	      break;
   7618 
   7619 	    default:
   7620 	      abort ();
   7621 	    }
   7622 	}
   7623 
   7624       if (contents != NULL
   7625 	  && elf_section_data (sec)->this_hdr.contents != contents)
   7626 	free (contents);
   7627       contents = NULL;
   7628 
   7629       if (internal_relocs != NULL
   7630 	  && elf_section_data (sec)->relocs != internal_relocs)
   7631 	free (internal_relocs);
   7632       internal_relocs = NULL;
   7633     }
   7634 
   7635   return TRUE;
   7636 
   7637 error_return:
   7638   if (contents != NULL
   7639       && elf_section_data (sec)->this_hdr.contents != contents)
   7640     free (contents);
   7641   if (internal_relocs != NULL
   7642       && elf_section_data (sec)->relocs != internal_relocs)
   7643     free (internal_relocs);
   7644 
   7645   return FALSE;
   7646 }
   7647 #endif
   7648 
   7649 
   7650 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
   7651 
   7652 void
   7653 bfd_elf32_arm_init_maps (bfd *abfd)
   7654 {
   7655   Elf_Internal_Sym *isymbuf;
   7656   Elf_Internal_Shdr *hdr;
   7657   unsigned int i, localsyms;
   7658 
   7659   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
   7660   if (! is_arm_elf (abfd))
   7661     return;
   7662 
   7663   if ((abfd->flags & DYNAMIC) != 0)
   7664     return;
   7665 
   7666   hdr = & elf_symtab_hdr (abfd);
   7667   localsyms = hdr->sh_info;
   7668 
   7669   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
   7670      should contain the number of local symbols, which should come before any
   7671      global symbols.  Mapping symbols are always local.  */
   7672   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
   7673 				  NULL);
   7674 
   7675   /* No internal symbols read?  Skip this BFD.  */
   7676   if (isymbuf == NULL)
   7677     return;
   7678 
   7679   for (i = 0; i < localsyms; i++)
   7680     {
   7681       Elf_Internal_Sym *isym = &isymbuf[i];
   7682       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   7683       const char *name;
   7684 
   7685       if (sec != NULL
   7686 	  && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
   7687 	{
   7688 	  name = bfd_elf_string_from_elf_section (abfd,
   7689 	    hdr->sh_link, isym->st_name);
   7690 
   7691 	  if (bfd_is_arm_special_symbol_name (name,
   7692 					      BFD_ARM_SPECIAL_SYM_TYPE_MAP))
   7693 	    elf32_arm_section_map_add (sec, name[1], isym->st_value);
   7694 	}
   7695     }
   7696 }
   7697 
   7698 
   7699 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
   7700    say what they wanted.  */
   7701 
   7702 void
   7703 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
   7704 {
   7705   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   7706   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
   7707 
   7708   if (globals == NULL)
   7709     return;
   7710 
   7711   if (globals->fix_cortex_a8 == -1)
   7712     {
   7713       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
   7714       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
   7715 	  && (out_attr[Tag_CPU_arch_profile].i == 'A'
   7716 	      || out_attr[Tag_CPU_arch_profile].i == 0))
   7717 	globals->fix_cortex_a8 = 1;
   7718       else
   7719 	globals->fix_cortex_a8 = 0;
   7720     }
   7721 }
   7722 
   7723 
   7724 void
   7725 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
   7726 {
   7727   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   7728   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
   7729 
   7730   if (globals == NULL)
   7731     return;
   7732   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
   7733   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
   7734     {
   7735       switch (globals->vfp11_fix)
   7736 	{
   7737 	case BFD_ARM_VFP11_FIX_DEFAULT:
   7738 	case BFD_ARM_VFP11_FIX_NONE:
   7739 	  globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
   7740 	  break;
   7741 
   7742 	default:
   7743 	  /* Give a warning, but do as the user requests anyway.  */
   7744 	  _bfd_error_handler (_("%B: warning: selected VFP11 erratum "
   7745 	    "workaround is not necessary for target architecture"), obfd);
   7746 	}
   7747     }
   7748   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
   7749     /* For earlier architectures, we might need the workaround, but do not
   7750        enable it by default.  If users is running with broken hardware, they
   7751        must enable the erratum fix explicitly.  */
   7752     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
   7753 }
   7754 
   7755 void
   7756 bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
   7757 {
   7758   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   7759   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
   7760 
   7761   if (globals == NULL)
   7762     return;
   7763 
   7764   /* We assume only Cortex-M4 may require the fix.  */
   7765   if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
   7766       || out_attr[Tag_CPU_arch_profile].i != 'M')
   7767     {
   7768       if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
   7769 	/* Give a warning, but do as the user requests anyway.  */
   7770 	_bfd_error_handler
   7771 	  (_("%B: warning: selected STM32L4XX erratum "
   7772 	     "workaround is not necessary for target architecture"), obfd);
   7773     }
   7774 }
   7775 
   7776 enum bfd_arm_vfp11_pipe
   7777 {
   7778   VFP11_FMAC,
   7779   VFP11_LS,
   7780   VFP11_DS,
   7781   VFP11_BAD
   7782 };
   7783 
   7784 /* Return a VFP register number.  This is encoded as RX:X for single-precision
   7785    registers, or X:RX for double-precision registers, where RX is the group of
   7786    four bits in the instruction encoding and X is the single extension bit.
   7787    RX and X fields are specified using their lowest (starting) bit.  The return
   7788    value is:
   7789 
   7790      0...31: single-precision registers s0...s31
   7791      32...63: double-precision registers d0...d31.
   7792 
   7793    Although X should be zero for VFP11 (encoding d0...d15 only), we might
   7794    encounter VFP3 instructions, so we allow the full range for DP registers.  */
   7795 
   7796 static unsigned int
   7797 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
   7798 		     unsigned int x)
   7799 {
   7800   if (is_double)
   7801     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
   7802   else
   7803     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
   7804 }
   7805 
   7806 /* Set bits in *WMASK according to a register number REG as encoded by
   7807    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
   7808 
   7809 static void
   7810 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
   7811 {
   7812   if (reg < 32)
   7813     *wmask |= 1 << reg;
   7814   else if (reg < 48)
   7815     *wmask |= 3 << ((reg - 32) * 2);
   7816 }
   7817 
   7818 /* Return TRUE if WMASK overwrites anything in REGS.  */
   7819 
   7820 static bfd_boolean
   7821 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
   7822 {
   7823   int i;
   7824 
   7825   for (i = 0; i < numregs; i++)
   7826     {
   7827       unsigned int reg = regs[i];
   7828 
   7829       if (reg < 32 && (wmask & (1 << reg)) != 0)
   7830 	return TRUE;
   7831 
   7832       reg -= 32;
   7833 
   7834       if (reg >= 16)
   7835 	continue;
   7836 
   7837       if ((wmask & (3 << (reg * 2))) != 0)
   7838 	return TRUE;
   7839     }
   7840 
   7841   return FALSE;
   7842 }
   7843 
   7844 /* In this function, we're interested in two things: finding input registers
   7845    for VFP data-processing instructions, and finding the set of registers which
   7846    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
   7847    hold the written set, so FLDM etc. are easy to deal with (we're only
   7848    interested in 32 SP registers or 16 dp registers, due to the VFP version
   7849    implemented by the chip in question).  DP registers are marked by setting
   7850    both SP registers in the write mask).  */
   7851 
   7852 static enum bfd_arm_vfp11_pipe
   7853 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
   7854 			   int *numregs)
   7855 {
   7856   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
   7857   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
   7858 
   7859   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
   7860     {
   7861       unsigned int pqrs;
   7862       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
   7863       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
   7864 
   7865       pqrs = ((insn & 0x00800000) >> 20)
   7866 	   | ((insn & 0x00300000) >> 19)
   7867 	   | ((insn & 0x00000040) >> 6);
   7868 
   7869       switch (pqrs)
   7870 	{
   7871 	case 0: /* fmac[sd].  */
   7872 	case 1: /* fnmac[sd].  */
   7873 	case 2: /* fmsc[sd].  */
   7874 	case 3: /* fnmsc[sd].  */
   7875 	  vpipe = VFP11_FMAC;
   7876 	  bfd_arm_vfp11_write_mask (destmask, fd);
   7877 	  regs[0] = fd;
   7878 	  regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
   7879 	  regs[2] = fm;
   7880 	  *numregs = 3;
   7881 	  break;
   7882 
   7883 	case 4: /* fmul[sd].  */
   7884 	case 5: /* fnmul[sd].  */
   7885 	case 6: /* fadd[sd].  */
   7886 	case 7: /* fsub[sd].  */
   7887 	  vpipe = VFP11_FMAC;
   7888 	  goto vfp_binop;
   7889 
   7890 	case 8: /* fdiv[sd].  */
   7891 	  vpipe = VFP11_DS;
   7892 	  vfp_binop:
   7893 	  bfd_arm_vfp11_write_mask (destmask, fd);
   7894 	  regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
   7895 	  regs[1] = fm;
   7896 	  *numregs = 2;
   7897 	  break;
   7898 
   7899 	case 15: /* extended opcode.  */
   7900 	  {
   7901 	    unsigned int extn = ((insn >> 15) & 0x1e)
   7902 			      | ((insn >> 7) & 1);
   7903 
   7904 	    switch (extn)
   7905 	      {
   7906 	      case 0: /* fcpy[sd].  */
   7907 	      case 1: /* fabs[sd].  */
   7908 	      case 2: /* fneg[sd].  */
   7909 	      case 8: /* fcmp[sd].  */
   7910 	      case 9: /* fcmpe[sd].  */
   7911 	      case 10: /* fcmpz[sd].  */
   7912 	      case 11: /* fcmpez[sd].  */
   7913 	      case 16: /* fuito[sd].  */
   7914 	      case 17: /* fsito[sd].  */
   7915 	      case 24: /* ftoui[sd].  */
   7916 	      case 25: /* ftouiz[sd].  */
   7917 	      case 26: /* ftosi[sd].  */
   7918 	      case 27: /* ftosiz[sd].  */
   7919 		/* These instructions will not bounce due to underflow.  */
   7920 		*numregs = 0;
   7921 		vpipe = VFP11_FMAC;
   7922 		break;
   7923 
   7924 	      case 3: /* fsqrt[sd].  */
   7925 		/* fsqrt cannot underflow, but it can (perhaps) overwrite
   7926 		   registers to cause the erratum in previous instructions.  */
   7927 		bfd_arm_vfp11_write_mask (destmask, fd);
   7928 		vpipe = VFP11_DS;
   7929 		break;
   7930 
   7931 	      case 15: /* fcvt{ds,sd}.  */
   7932 		{
   7933 		  int rnum = 0;
   7934 
   7935 		  bfd_arm_vfp11_write_mask (destmask, fd);
   7936 
   7937 		  /* Only FCVTSD can underflow.  */
   7938 		  if ((insn & 0x100) != 0)
   7939 		    regs[rnum++] = fm;
   7940 
   7941 		  *numregs = rnum;
   7942 
   7943 		  vpipe = VFP11_FMAC;
   7944 		}
   7945 		break;
   7946 
   7947 	      default:
   7948 		return VFP11_BAD;
   7949 	      }
   7950 	  }
   7951 	  break;
   7952 
   7953 	default:
   7954 	  return VFP11_BAD;
   7955 	}
   7956     }
   7957   /* Two-register transfer.  */
   7958   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
   7959     {
   7960       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
   7961 
   7962       if ((insn & 0x100000) == 0)
   7963 	{
   7964 	  if (is_double)
   7965 	    bfd_arm_vfp11_write_mask (destmask, fm);
   7966 	  else
   7967 	    {
   7968 	      bfd_arm_vfp11_write_mask (destmask, fm);
   7969 	      bfd_arm_vfp11_write_mask (destmask, fm + 1);
   7970 	    }
   7971 	}
   7972 
   7973       vpipe = VFP11_LS;
   7974     }
   7975   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
   7976     {
   7977       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
   7978       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
   7979 
   7980       switch (puw)
   7981 	{
   7982 	case 0: /* Two-reg transfer.  We should catch these above.  */
   7983 	  abort ();
   7984 
   7985 	case 2: /* fldm[sdx].  */
   7986 	case 3:
   7987 	case 5:
   7988 	  {
   7989 	    unsigned int i, offset = insn & 0xff;
   7990 
   7991 	    if (is_double)
   7992 	      offset >>= 1;
   7993 
   7994 	    for (i = fd; i < fd + offset; i++)
   7995 	      bfd_arm_vfp11_write_mask (destmask, i);
   7996 	  }
   7997 	  break;
   7998 
   7999 	case 4: /* fld[sd].  */
   8000 	case 6:
   8001 	  bfd_arm_vfp11_write_mask (destmask, fd);
   8002 	  break;
   8003 
   8004 	default:
   8005 	  return VFP11_BAD;
   8006 	}
   8007 
   8008       vpipe = VFP11_LS;
   8009     }
   8010   /* Single-register transfer. Note L==0.  */
   8011   else if ((insn & 0x0f100e10) == 0x0e000a10)
   8012     {
   8013       unsigned int opcode = (insn >> 21) & 7;
   8014       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
   8015 
   8016       switch (opcode)
   8017 	{
   8018 	case 0: /* fmsr/fmdlr.  */
   8019 	case 1: /* fmdhr.  */
   8020 	  /* Mark fmdhr and fmdlr as writing to the whole of the DP
   8021 	     destination register.  I don't know if this is exactly right,
   8022 	     but it is the conservative choice.  */
   8023 	  bfd_arm_vfp11_write_mask (destmask, fn);
   8024 	  break;
   8025 
   8026 	case 7: /* fmxr.  */
   8027 	  break;
   8028 	}
   8029 
   8030       vpipe = VFP11_LS;
   8031     }
   8032 
   8033   return vpipe;
   8034 }
   8035 
   8036 
   8037 static int elf32_arm_compare_mapping (const void * a, const void * b);
   8038 
   8039 
   8040 /* Look for potentially-troublesome code sequences which might trigger the
   8041    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
   8042    (available from ARM) for details of the erratum.  A short version is
   8043    described in ld.texinfo.  */
   8044 
   8045 bfd_boolean
   8046 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
   8047 {
   8048   asection *sec;
   8049   bfd_byte *contents = NULL;
   8050   int state = 0;
   8051   int regs[3], numregs = 0;
   8052   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   8053   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
   8054 
   8055   if (globals == NULL)
   8056     return FALSE;
   8057 
   8058   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
   8059      The states transition as follows:
   8060 
   8061        0 -> 1 (vector) or 0 -> 2 (scalar)
   8062 	   A VFP FMAC-pipeline instruction has been seen. Fill
   8063 	   regs[0]..regs[numregs-1] with its input operands. Remember this
   8064 	   instruction in 'first_fmac'.
   8065 
   8066        1 -> 2
   8067 	   Any instruction, except for a VFP instruction which overwrites
   8068 	   regs[*].
   8069 
   8070        1 -> 3 [ -> 0 ]  or
   8071        2 -> 3 [ -> 0 ]
   8072 	   A VFP instruction has been seen which overwrites any of regs[*].
   8073 	   We must make a veneer!  Reset state to 0 before examining next
   8074 	   instruction.
   8075 
   8076        2 -> 0
   8077 	   If we fail to match anything in state 2, reset to state 0 and reset
   8078 	   the instruction pointer to the instruction after 'first_fmac'.
   8079 
   8080      If the VFP11 vector mode is in use, there must be at least two unrelated
   8081      instructions between anti-dependent VFP11 instructions to properly avoid
   8082      triggering the erratum, hence the use of the extra state 1.  */
   8083 
   8084   /* If we are only performing a partial link do not bother
   8085      to construct any glue.  */
   8086   if (bfd_link_relocatable (link_info))
   8087     return TRUE;
   8088 
   8089   /* Skip if this bfd does not correspond to an ELF image.  */
   8090   if (! is_arm_elf (abfd))
   8091     return TRUE;
   8092 
   8093   /* We should have chosen a fix type by the time we get here.  */
   8094   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
   8095 
   8096   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
   8097     return TRUE;
   8098 
   8099   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
   8100   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   8101     return TRUE;
   8102 
   8103   for (sec = abfd->sections; sec != NULL; sec = sec->next)
   8104     {
   8105       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
   8106       struct _arm_elf_section_data *sec_data;
   8107 
   8108       /* If we don't have executable progbits, we're not interested in this
   8109 	 section.  Also skip if section is to be excluded.  */
   8110       if (elf_section_type (sec) != SHT_PROGBITS
   8111 	  || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
   8112 	  || (sec->flags & SEC_EXCLUDE) != 0
   8113 	  || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8114 	  || sec->output_section == bfd_abs_section_ptr
   8115 	  || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
   8116 	continue;
   8117 
   8118       sec_data = elf32_arm_section_data (sec);
   8119 
   8120       if (sec_data->mapcount == 0)
   8121 	continue;
   8122 
   8123       if (elf_section_data (sec)->this_hdr.contents != NULL)
   8124 	contents = elf_section_data (sec)->this_hdr.contents;
   8125       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
   8126 	goto error_return;
   8127 
   8128       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
   8129 	     elf32_arm_compare_mapping);
   8130 
   8131       for (span = 0; span < sec_data->mapcount; span++)
   8132 	{
   8133 	  unsigned int span_start = sec_data->map[span].vma;
   8134 	  unsigned int span_end = (span == sec_data->mapcount - 1)
   8135 				  ? sec->size : sec_data->map[span + 1].vma;
   8136 	  char span_type = sec_data->map[span].type;
   8137 
   8138 	  /* FIXME: Only ARM mode is supported at present.  We may need to
   8139 	     support Thumb-2 mode also at some point.  */
   8140 	  if (span_type != 'a')
   8141 	    continue;
   8142 
   8143 	  for (i = span_start; i < span_end;)
   8144 	    {
   8145 	      unsigned int next_i = i + 4;
   8146 	      unsigned int insn = bfd_big_endian (abfd)
   8147 		? (contents[i] << 24)
   8148 		  | (contents[i + 1] << 16)
   8149 		  | (contents[i + 2] << 8)
   8150 		  | contents[i + 3]
   8151 		: (contents[i + 3] << 24)
   8152 		  | (contents[i + 2] << 16)
   8153 		  | (contents[i + 1] << 8)
   8154 		  | contents[i];
   8155 	      unsigned int writemask = 0;
   8156 	      enum bfd_arm_vfp11_pipe vpipe;
   8157 
   8158 	      switch (state)
   8159 		{
   8160 		case 0:
   8161 		  vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
   8162 						    &numregs);
   8163 		  /* I'm assuming the VFP11 erratum can trigger with denorm
   8164 		     operands on either the FMAC or the DS pipeline. This might
   8165 		     lead to slightly overenthusiastic veneer insertion.  */
   8166 		  if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
   8167 		    {
   8168 		      state = use_vector ? 1 : 2;
   8169 		      first_fmac = i;
   8170 		      veneer_of_insn = insn;
   8171 		    }
   8172 		  break;
   8173 
   8174 		case 1:
   8175 		  {
   8176 		    int other_regs[3], other_numregs;
   8177 		    vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
   8178 						      other_regs,
   8179 						      &other_numregs);
   8180 		    if (vpipe != VFP11_BAD
   8181 			&& bfd_arm_vfp11_antidependency (writemask, regs,
   8182 							 numregs))
   8183 		      state = 3;
   8184 		    else
   8185 		      state = 2;
   8186 		  }
   8187 		  break;
   8188 
   8189 		case 2:
   8190 		  {
   8191 		    int other_regs[3], other_numregs;
   8192 		    vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
   8193 						      other_regs,
   8194 						      &other_numregs);
   8195 		    if (vpipe != VFP11_BAD
   8196 			&& bfd_arm_vfp11_antidependency (writemask, regs,
   8197 							 numregs))
   8198 		      state = 3;
   8199 		    else
   8200 		      {
   8201 			state = 0;
   8202 			next_i = first_fmac + 4;
   8203 		      }
   8204 		  }
   8205 		  break;
   8206 
   8207 		case 3:
   8208 		  abort ();  /* Should be unreachable.  */
   8209 		}
   8210 
   8211 	      if (state == 3)
   8212 		{
   8213 		  elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
   8214 		      bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
   8215 
   8216 		  elf32_arm_section_data (sec)->erratumcount += 1;
   8217 
   8218 		  newerr->u.b.vfp_insn = veneer_of_insn;
   8219 
   8220 		  switch (span_type)
   8221 		    {
   8222 		    case 'a':
   8223 		      newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
   8224 		      break;
   8225 
   8226 		    default:
   8227 		      abort ();
   8228 		    }
   8229 
   8230 		  record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
   8231 					       first_fmac);
   8232 
   8233 		  newerr->vma = -1;
   8234 
   8235 		  newerr->next = sec_data->erratumlist;
   8236 		  sec_data->erratumlist = newerr;
   8237 
   8238 		  state = 0;
   8239 		}
   8240 
   8241 	      i = next_i;
   8242 	    }
   8243 	}
   8244 
   8245       if (contents != NULL
   8246 	  && elf_section_data (sec)->this_hdr.contents != contents)
   8247 	free (contents);
   8248       contents = NULL;
   8249     }
   8250 
   8251   return TRUE;
   8252 
   8253 error_return:
   8254   if (contents != NULL
   8255       && elf_section_data (sec)->this_hdr.contents != contents)
   8256     free (contents);
   8257 
   8258   return FALSE;
   8259 }
   8260 
   8261 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
   8262    after sections have been laid out, using specially-named symbols.  */
   8263 
   8264 void
   8265 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
   8266 					  struct bfd_link_info *link_info)
   8267 {
   8268   asection *sec;
   8269   struct elf32_arm_link_hash_table *globals;
   8270   char *tmp_name;
   8271 
   8272   if (bfd_link_relocatable (link_info))
   8273     return;
   8274 
   8275   /* Skip if this bfd does not correspond to an ELF image.  */
   8276   if (! is_arm_elf (abfd))
   8277     return;
   8278 
   8279   globals = elf32_arm_hash_table (link_info);
   8280   if (globals == NULL)
   8281     return;
   8282 
   8283   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
   8284 				  (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
   8285 
   8286   for (sec = abfd->sections; sec != NULL; sec = sec->next)
   8287     {
   8288       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
   8289       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
   8290 
   8291       for (; errnode != NULL; errnode = errnode->next)
   8292 	{
   8293 	  struct elf_link_hash_entry *myh;
   8294 	  bfd_vma vma;
   8295 
   8296 	  switch (errnode->type)
   8297 	    {
   8298 	    case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
   8299 	    case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
   8300 	      /* Find veneer symbol.  */
   8301 	      sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
   8302 		       errnode->u.b.veneer->u.v.id);
   8303 
   8304 	      myh = elf_link_hash_lookup
   8305 		(&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
   8306 
   8307 	      if (myh == NULL)
   8308 		_bfd_error_handler (_("%B: unable to find VFP11 veneer "
   8309 				      "`%s'"), abfd, tmp_name);
   8310 
   8311 	      vma = myh->root.u.def.section->output_section->vma
   8312 		    + myh->root.u.def.section->output_offset
   8313 		    + myh->root.u.def.value;
   8314 
   8315 	      errnode->u.b.veneer->vma = vma;
   8316 	      break;
   8317 
   8318 	    case VFP11_ERRATUM_ARM_VENEER:
   8319 	    case VFP11_ERRATUM_THUMB_VENEER:
   8320 	      /* Find return location.  */
   8321 	      sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
   8322 		       errnode->u.v.id);
   8323 
   8324 	      myh = elf_link_hash_lookup
   8325 		(&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
   8326 
   8327 	      if (myh == NULL)
   8328 		_bfd_error_handler (_("%B: unable to find VFP11 veneer "
   8329 				      "`%s'"), abfd, tmp_name);
   8330 
   8331 	      vma = myh->root.u.def.section->output_section->vma
   8332 		    + myh->root.u.def.section->output_offset
   8333 		    + myh->root.u.def.value;
   8334 
   8335 	      errnode->u.v.branch->vma = vma;
   8336 	      break;
   8337 
   8338 	    default:
   8339 	      abort ();
   8340 	    }
   8341 	}
   8342     }
   8343 
   8344   free (tmp_name);
   8345 }
   8346 
   8347 /* Find virtual-memory addresses for STM32L4XX erratum veneers and
   8348    return locations after sections have been laid out, using
   8349    specially-named symbols.  */
   8350 
   8351 void
   8352 bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
   8353 					      struct bfd_link_info *link_info)
   8354 {
   8355   asection *sec;
   8356   struct elf32_arm_link_hash_table *globals;
   8357   char *tmp_name;
   8358 
   8359   if (bfd_link_relocatable (link_info))
   8360     return;
   8361 
   8362   /* Skip if this bfd does not correspond to an ELF image.  */
   8363   if (! is_arm_elf (abfd))
   8364     return;
   8365 
   8366   globals = elf32_arm_hash_table (link_info);
   8367   if (globals == NULL)
   8368     return;
   8369 
   8370   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
   8371 				  (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
   8372 
   8373   for (sec = abfd->sections; sec != NULL; sec = sec->next)
   8374     {
   8375       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
   8376       elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
   8377 
   8378       for (; errnode != NULL; errnode = errnode->next)
   8379 	{
   8380 	  struct elf_link_hash_entry *myh;
   8381 	  bfd_vma vma;
   8382 
   8383 	  switch (errnode->type)
   8384 	    {
   8385 	    case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
   8386 	      /* Find veneer symbol.  */
   8387 	      sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
   8388 		       errnode->u.b.veneer->u.v.id);
   8389 
   8390 	      myh = elf_link_hash_lookup
   8391 		(&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
   8392 
   8393 	      if (myh == NULL)
   8394 		_bfd_error_handler (_("%B: unable to find STM32L4XX veneer "
   8395 				      "`%s'"), abfd, tmp_name);
   8396 
   8397 	      vma = myh->root.u.def.section->output_section->vma
   8398 		+ myh->root.u.def.section->output_offset
   8399 		+ myh->root.u.def.value;
   8400 
   8401 	      errnode->u.b.veneer->vma = vma;
   8402 	      break;
   8403 
   8404 	    case STM32L4XX_ERRATUM_VENEER:
   8405 	      /* Find return location.  */
   8406 	      sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
   8407 		       errnode->u.v.id);
   8408 
   8409 	      myh = elf_link_hash_lookup
   8410 		(&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
   8411 
   8412 	      if (myh == NULL)
   8413 		_bfd_error_handler (_("%B: unable to find STM32L4XX veneer "
   8414 				      "`%s'"), abfd, tmp_name);
   8415 
   8416 	      vma = myh->root.u.def.section->output_section->vma
   8417 		+ myh->root.u.def.section->output_offset
   8418 		+ myh->root.u.def.value;
   8419 
   8420 	      errnode->u.v.branch->vma = vma;
   8421 	      break;
   8422 
   8423 	    default:
   8424 	      abort ();
   8425 	    }
   8426 	}
   8427     }
   8428 
   8429   free (tmp_name);
   8430 }
   8431 
   8432 static inline bfd_boolean
   8433 is_thumb2_ldmia (const insn32 insn)
   8434 {
   8435   /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
   8436      1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
   8437   return (insn & 0xffd02000) == 0xe8900000;
   8438 }
   8439 
   8440 static inline bfd_boolean
   8441 is_thumb2_ldmdb (const insn32 insn)
   8442 {
   8443   /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
   8444      1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
   8445   return (insn & 0xffd02000) == 0xe9100000;
   8446 }
   8447 
   8448 static inline bfd_boolean
   8449 is_thumb2_vldm (const insn32 insn)
   8450 {
   8451   /* A6.5 Extension register load or store instruction
   8452      A7.7.229
   8453      We look for SP 32-bit and DP 64-bit registers.
   8454      Encoding T1 VLDM{mode}<c> <Rn>{!}, <list>
   8455      <list> is consecutive 64-bit registers
   8456      1110 - 110P - UDW1 - rrrr - vvvv - 1011 - iiii - iiii
   8457      Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
   8458      <list> is consecutive 32-bit registers
   8459      1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
   8460      if P==0 && U==1 && W==1 && Rn=1101 VPOP
   8461      if PUW=010 || PUW=011 || PUW=101 VLDM.  */
   8462   return
   8463     (((insn & 0xfe100f00) == 0xec100b00) ||
   8464      ((insn & 0xfe100f00) == 0xec100a00))
   8465     && /* (IA without !).  */
   8466     (((((insn << 7) >> 28) & 0xd) == 0x4)
   8467      /* (IA with !), includes VPOP (when reg number is SP).  */
   8468      || ((((insn << 7) >> 28) & 0xd) == 0x5)
   8469      /* (DB with !).  */
   8470      || ((((insn << 7) >> 28) & 0xd) == 0x9));
   8471 }
   8472 
   8473 /* STM STM32L4XX erratum : This function assumes that it receives an LDM or
   8474    VLDM opcode and:
   8475  - computes the number and the mode of memory accesses
   8476  - decides if the replacement should be done:
   8477    . replaces only if > 8-word accesses
   8478    . or (testing purposes only) replaces all accesses.  */
   8479 
   8480 static bfd_boolean
   8481 stm32l4xx_need_create_replacing_stub (const insn32 insn,
   8482 				      bfd_arm_stm32l4xx_fix stm32l4xx_fix)
   8483 {
   8484   int nb_words = 0;
   8485 
   8486   /* The field encoding the register list is the same for both LDMIA
   8487      and LDMDB encodings.  */
   8488   if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
   8489     nb_words = elf32_arm_popcount (insn & 0x0000ffff);
   8490   else if (is_thumb2_vldm (insn))
   8491    nb_words = (insn & 0xff);
   8492 
   8493   /* DEFAULT mode accounts for the real bug condition situation,
   8494      ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
   8495   return
   8496     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT) ? nb_words > 8 :
   8497     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL) ? TRUE : FALSE;
   8498 }
   8499 
   8500 /* Look for potentially-troublesome code sequences which might trigger
   8501    the STM STM32L4XX erratum.  */
   8502 
   8503 bfd_boolean
   8504 bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
   8505 				      struct bfd_link_info *link_info)
   8506 {
   8507   asection *sec;
   8508   bfd_byte *contents = NULL;
   8509   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   8510 
   8511   if (globals == NULL)
   8512     return FALSE;
   8513 
   8514   /* If we are only performing a partial link do not bother
   8515      to construct any glue.  */
   8516   if (bfd_link_relocatable (link_info))
   8517     return TRUE;
   8518 
   8519   /* Skip if this bfd does not correspond to an ELF image.  */
   8520   if (! is_arm_elf (abfd))
   8521     return TRUE;
   8522 
   8523   if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
   8524     return TRUE;
   8525 
   8526   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
   8527   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   8528     return TRUE;
   8529 
   8530   for (sec = abfd->sections; sec != NULL; sec = sec->next)
   8531     {
   8532       unsigned int i, span;
   8533       struct _arm_elf_section_data *sec_data;
   8534 
   8535       /* If we don't have executable progbits, we're not interested in this
   8536 	 section.  Also skip if section is to be excluded.  */
   8537       if (elf_section_type (sec) != SHT_PROGBITS
   8538 	  || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
   8539 	  || (sec->flags & SEC_EXCLUDE) != 0
   8540 	  || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8541 	  || sec->output_section == bfd_abs_section_ptr
   8542 	  || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
   8543 	continue;
   8544 
   8545       sec_data = elf32_arm_section_data (sec);
   8546 
   8547       if (sec_data->mapcount == 0)
   8548 	continue;
   8549 
   8550       if (elf_section_data (sec)->this_hdr.contents != NULL)
   8551 	contents = elf_section_data (sec)->this_hdr.contents;
   8552       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
   8553 	goto error_return;
   8554 
   8555       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
   8556 	     elf32_arm_compare_mapping);
   8557 
   8558       for (span = 0; span < sec_data->mapcount; span++)
   8559 	{
   8560 	  unsigned int span_start = sec_data->map[span].vma;
   8561 	  unsigned int span_end = (span == sec_data->mapcount - 1)
   8562 	    ? sec->size : sec_data->map[span + 1].vma;
   8563 	  char span_type = sec_data->map[span].type;
   8564 	  int itblock_current_pos = 0;
   8565 
   8566 	  /* Only Thumb2 mode need be supported with this CM4 specific
   8567 	     code, we should not encounter any arm mode eg span_type
   8568 	     != 'a'.  */
   8569 	  if (span_type != 't')
   8570 	    continue;
   8571 
   8572 	  for (i = span_start; i < span_end;)
   8573 	    {
   8574 	      unsigned int insn = bfd_get_16 (abfd, &contents[i]);
   8575 	      bfd_boolean insn_32bit = FALSE;
   8576 	      bfd_boolean is_ldm = FALSE;
   8577 	      bfd_boolean is_vldm = FALSE;
   8578 	      bfd_boolean is_not_last_in_it_block = FALSE;
   8579 
   8580 	      /* The first 16-bits of all 32-bit thumb2 instructions start
   8581 		 with opcode[15..13]=0b111 and the encoded op1 can be anything
   8582 		 except opcode[12..11]!=0b00.
   8583 		 See 32-bit Thumb instruction encoding.  */
   8584 	      if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
   8585 		insn_32bit = TRUE;
   8586 
   8587 	      /* Compute the predicate that tells if the instruction
   8588 		 is concerned by the IT block
   8589 		 - Creates an error if there is a ldm that is not
   8590 		   last in the IT block thus cannot be replaced
   8591 		 - Otherwise we can create a branch at the end of the
   8592 		   IT block, it will be controlled naturally by IT
   8593 		   with the proper pseudo-predicate
   8594 		 - So the only interesting predicate is the one that
   8595 		   tells that we are not on the last item of an IT
   8596 		   block.  */
   8597 	      if (itblock_current_pos != 0)
   8598 		  is_not_last_in_it_block = !!--itblock_current_pos;
   8599 
   8600 	      if (insn_32bit)
   8601 		{
   8602 		  /* Load the rest of the insn (in manual-friendly order).  */
   8603 		  insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
   8604 		  is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
   8605 		  is_vldm = is_thumb2_vldm (insn);
   8606 
   8607 		  /* Veneers are created for (v)ldm depending on
   8608 		     option flags and memory accesses conditions; but
   8609 		     if the instruction is not the last instruction of
   8610 		     an IT block, we cannot create a jump there, so we
   8611 		     bail out.  */
   8612 		    if ((is_ldm || is_vldm)
   8613 			&& stm32l4xx_need_create_replacing_stub
   8614 			(insn, globals->stm32l4xx_fix))
   8615 		      {
   8616 			if (is_not_last_in_it_block)
   8617 			  {
   8618 			    _bfd_error_handler
   8619 			      /* xgettext:c-format */
   8620 			      (_("%B(%A+%#x): error: multiple load detected"
   8621 				 " in non-last IT block instruction :"
   8622 				 " STM32L4XX veneer cannot be generated.\n"
   8623 				 "Use gcc option -mrestrict-it to generate"
   8624 				 " only one instruction per IT block.\n"),
   8625 			       abfd, sec, i);
   8626 			  }
   8627 			else
   8628 			  {
   8629 			    elf32_stm32l4xx_erratum_list *newerr =
   8630 			      (elf32_stm32l4xx_erratum_list *)
   8631 			      bfd_zmalloc
   8632 			      (sizeof (elf32_stm32l4xx_erratum_list));
   8633 
   8634 			    elf32_arm_section_data (sec)
   8635 			      ->stm32l4xx_erratumcount += 1;
   8636 			    newerr->u.b.insn = insn;
   8637 			    /* We create only thumb branches.  */
   8638 			    newerr->type =
   8639 			      STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
   8640 			    record_stm32l4xx_erratum_veneer
   8641 			      (link_info, newerr, abfd, sec,
   8642 			       i,
   8643 			       is_ldm ?
   8644 			       STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
   8645 			       STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
   8646 			    newerr->vma = -1;
   8647 			    newerr->next = sec_data->stm32l4xx_erratumlist;
   8648 			    sec_data->stm32l4xx_erratumlist = newerr;
   8649 			  }
   8650 		      }
   8651 		}
   8652 	      else
   8653 		{
   8654 		  /* A7.7.37 IT p208
   8655 		     IT blocks are only encoded in T1
   8656 		     Encoding T1: IT{x{y{z}}} <firstcond>
   8657 		     1 0 1 1 - 1 1 1 1 - firstcond - mask
   8658 		     if mask = '0000' then see 'related encodings'
   8659 		     We don't deal with UNPREDICTABLE, just ignore these.
   8660 		     There can be no nested IT blocks so an IT block
   8661 		     is naturally a new one for which it is worth
   8662 		     computing its size.  */
   8663 		  bfd_boolean is_newitblock = ((insn & 0xff00) == 0xbf00)
   8664 		    && ((insn & 0x000f) != 0x0000);
   8665 		  /* If we have a new IT block we compute its size.  */
   8666 		  if (is_newitblock)
   8667 		    {
   8668 		      /* Compute the number of instructions controlled
   8669 			 by the IT block, it will be used to decide
   8670 			 whether we are inside an IT block or not.  */
   8671 		      unsigned int mask = insn & 0x000f;
   8672 		      itblock_current_pos = 4 - ctz (mask);
   8673 		    }
   8674 		}
   8675 
   8676 	      i += insn_32bit ? 4 : 2;
   8677 	    }
   8678 	}
   8679 
   8680       if (contents != NULL
   8681 	  && elf_section_data (sec)->this_hdr.contents != contents)
   8682 	free (contents);
   8683       contents = NULL;
   8684     }
   8685 
   8686   return TRUE;
   8687 
   8688 error_return:
   8689   if (contents != NULL
   8690       && elf_section_data (sec)->this_hdr.contents != contents)
   8691     free (contents);
   8692 
   8693   return FALSE;
   8694 }
   8695 
   8696 /* Set target relocation values needed during linking.  */
   8697 
   8698 void
   8699 bfd_elf32_arm_set_target_params (struct bfd *output_bfd,
   8700 				 struct bfd_link_info *link_info,
   8701 				 struct elf32_arm_params *params)
   8702 {
   8703   struct elf32_arm_link_hash_table *globals;
   8704 
   8705   globals = elf32_arm_hash_table (link_info);
   8706   if (globals == NULL)
   8707     return;
   8708 
   8709   globals->target1_is_rel = params->target1_is_rel;
   8710   if (strcmp (params->target2_type, "rel") == 0)
   8711     globals->target2_reloc = R_ARM_REL32;
   8712   else if (strcmp (params->target2_type, "abs") == 0)
   8713     globals->target2_reloc = R_ARM_ABS32;
   8714   else if (strcmp (params->target2_type, "got-rel") == 0)
   8715     globals->target2_reloc = R_ARM_GOT_PREL;
   8716   else
   8717     {
   8718       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
   8719 			  params->target2_type);
   8720     }
   8721   globals->fix_v4bx = params->fix_v4bx;
   8722   globals->use_blx |= params->use_blx;
   8723   globals->vfp11_fix = params->vfp11_denorm_fix;
   8724   globals->stm32l4xx_fix = params->stm32l4xx_fix;
   8725   globals->pic_veneer = params->pic_veneer;
   8726   globals->fix_cortex_a8 = params->fix_cortex_a8;
   8727   globals->fix_arm1176 = params->fix_arm1176;
   8728   globals->cmse_implib = params->cmse_implib;
   8729   globals->in_implib_bfd = params->in_implib_bfd;
   8730 
   8731   BFD_ASSERT (is_arm_elf (output_bfd));
   8732   elf_arm_tdata (output_bfd)->no_enum_size_warning
   8733     = params->no_enum_size_warning;
   8734   elf_arm_tdata (output_bfd)->no_wchar_size_warning
   8735     = params->no_wchar_size_warning;
   8736 }
   8737 
   8738 /* Replace the target offset of a Thumb bl or b.w instruction.  */
   8739 
   8740 static void
   8741 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
   8742 {
   8743   bfd_vma upper;
   8744   bfd_vma lower;
   8745   int reloc_sign;
   8746 
   8747   BFD_ASSERT ((offset & 1) == 0);
   8748 
   8749   upper = bfd_get_16 (abfd, insn);
   8750   lower = bfd_get_16 (abfd, insn + 2);
   8751   reloc_sign = (offset < 0) ? 1 : 0;
   8752   upper = (upper & ~(bfd_vma) 0x7ff)
   8753 	  | ((offset >> 12) & 0x3ff)
   8754 	  | (reloc_sign << 10);
   8755   lower = (lower & ~(bfd_vma) 0x2fff)
   8756 	  | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
   8757 	  | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
   8758 	  | ((offset >> 1) & 0x7ff);
   8759   bfd_put_16 (abfd, upper, insn);
   8760   bfd_put_16 (abfd, lower, insn + 2);
   8761 }
   8762 
   8763 /* Thumb code calling an ARM function.  */
   8764 
   8765 static int
   8766 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
   8767 			 const char *		name,
   8768 			 bfd *			input_bfd,
   8769 			 bfd *			output_bfd,
   8770 			 asection *		input_section,
   8771 			 bfd_byte *		hit_data,
   8772 			 asection *		sym_sec,
   8773 			 bfd_vma		offset,
   8774 			 bfd_signed_vma		addend,
   8775 			 bfd_vma		val,
   8776 			 char **error_message)
   8777 {
   8778   asection * s = 0;
   8779   bfd_vma my_offset;
   8780   long int ret_offset;
   8781   struct elf_link_hash_entry * myh;
   8782   struct elf32_arm_link_hash_table * globals;
   8783 
   8784   myh = find_thumb_glue (info, name, error_message);
   8785   if (myh == NULL)
   8786     return FALSE;
   8787 
   8788   globals = elf32_arm_hash_table (info);
   8789   BFD_ASSERT (globals != NULL);
   8790   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   8791 
   8792   my_offset = myh->root.u.def.value;
   8793 
   8794   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
   8795 			      THUMB2ARM_GLUE_SECTION_NAME);
   8796 
   8797   BFD_ASSERT (s != NULL);
   8798   BFD_ASSERT (s->contents != NULL);
   8799   BFD_ASSERT (s->output_section != NULL);
   8800 
   8801   if ((my_offset & 0x01) == 0x01)
   8802     {
   8803       if (sym_sec != NULL
   8804 	  && sym_sec->owner != NULL
   8805 	  && !INTERWORK_FLAG (sym_sec->owner))
   8806 	{
   8807 	  _bfd_error_handler
   8808 	    (_("%B(%s): warning: interworking not enabled.\n"
   8809 	       "  first occurrence: %B: Thumb call to ARM"),
   8810 	     sym_sec->owner, name, input_bfd);
   8811 
   8812 	  return FALSE;
   8813 	}
   8814 
   8815       --my_offset;
   8816       myh->root.u.def.value = my_offset;
   8817 
   8818       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
   8819 		      s->contents + my_offset);
   8820 
   8821       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
   8822 		      s->contents + my_offset + 2);
   8823 
   8824       ret_offset =
   8825 	/* Address of destination of the stub.  */
   8826 	((bfd_signed_vma) val)
   8827 	- ((bfd_signed_vma)
   8828 	   /* Offset from the start of the current section
   8829 	      to the start of the stubs.  */
   8830 	   (s->output_offset
   8831 	    /* Offset of the start of this stub from the start of the stubs.  */
   8832 	    + my_offset
   8833 	    /* Address of the start of the current section.  */
   8834 	    + s->output_section->vma)
   8835 	   /* The branch instruction is 4 bytes into the stub.  */
   8836 	   + 4
   8837 	   /* ARM branches work from the pc of the instruction + 8.  */
   8838 	   + 8);
   8839 
   8840       put_arm_insn (globals, output_bfd,
   8841 		    (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
   8842 		    s->contents + my_offset + 4);
   8843     }
   8844 
   8845   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
   8846 
   8847   /* Now go back and fix up the original BL insn to point to here.  */
   8848   ret_offset =
   8849     /* Address of where the stub is located.  */
   8850     (s->output_section->vma + s->output_offset + my_offset)
   8851      /* Address of where the BL is located.  */
   8852     - (input_section->output_section->vma + input_section->output_offset
   8853        + offset)
   8854     /* Addend in the relocation.  */
   8855     - addend
   8856     /* Biassing for PC-relative addressing.  */
   8857     - 8;
   8858 
   8859   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
   8860 
   8861   return TRUE;
   8862 }
   8863 
   8864 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
   8865 
   8866 static struct elf_link_hash_entry *
   8867 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
   8868 			     const char *	    name,
   8869 			     bfd *		    input_bfd,
   8870 			     bfd *		    output_bfd,
   8871 			     asection *		    sym_sec,
   8872 			     bfd_vma		    val,
   8873 			     asection *		    s,
   8874 			     char **		    error_message)
   8875 {
   8876   bfd_vma my_offset;
   8877   long int ret_offset;
   8878   struct elf_link_hash_entry * myh;
   8879   struct elf32_arm_link_hash_table * globals;
   8880 
   8881   myh = find_arm_glue (info, name, error_message);
   8882   if (myh == NULL)
   8883     return NULL;
   8884 
   8885   globals = elf32_arm_hash_table (info);
   8886   BFD_ASSERT (globals != NULL);
   8887   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   8888 
   8889   my_offset = myh->root.u.def.value;
   8890 
   8891   if ((my_offset & 0x01) == 0x01)
   8892     {
   8893       if (sym_sec != NULL
   8894 	  && sym_sec->owner != NULL
   8895 	  && !INTERWORK_FLAG (sym_sec->owner))
   8896 	{
   8897 	  _bfd_error_handler
   8898 	    (_("%B(%s): warning: interworking not enabled.\n"
   8899 	       "  first occurrence: %B: arm call to thumb"),
   8900 	     sym_sec->owner, name, input_bfd);
   8901 	}
   8902 
   8903       --my_offset;
   8904       myh->root.u.def.value = my_offset;
   8905 
   8906       if (bfd_link_pic (info)
   8907 	  || globals->root.is_relocatable_executable
   8908 	  || globals->pic_veneer)
   8909 	{
   8910 	  /* For relocatable objects we can't use absolute addresses,
   8911 	     so construct the address from a relative offset.  */
   8912 	  /* TODO: If the offset is small it's probably worth
   8913 	     constructing the address with adds.  */
   8914 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
   8915 			s->contents + my_offset);
   8916 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
   8917 			s->contents + my_offset + 4);
   8918 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
   8919 			s->contents + my_offset + 8);
   8920 	  /* Adjust the offset by 4 for the position of the add,
   8921 	     and 8 for the pipeline offset.  */
   8922 	  ret_offset = (val - (s->output_offset
   8923 			       + s->output_section->vma
   8924 			       + my_offset + 12))
   8925 		       | 1;
   8926 	  bfd_put_32 (output_bfd, ret_offset,
   8927 		      s->contents + my_offset + 12);
   8928 	}
   8929       else if (globals->use_blx)
   8930 	{
   8931 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
   8932 			s->contents + my_offset);
   8933 
   8934 	  /* It's a thumb address.  Add the low order bit.  */
   8935 	  bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
   8936 		      s->contents + my_offset + 4);
   8937 	}
   8938       else
   8939 	{
   8940 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
   8941 			s->contents + my_offset);
   8942 
   8943 	  put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
   8944 			s->contents + my_offset + 4);
   8945 
   8946 	  /* It's a thumb address.  Add the low order bit.  */
   8947 	  bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
   8948 		      s->contents + my_offset + 8);
   8949 
   8950 	  my_offset += 12;
   8951 	}
   8952     }
   8953 
   8954   BFD_ASSERT (my_offset <= globals->arm_glue_size);
   8955 
   8956   return myh;
   8957 }
   8958 
   8959 /* Arm code calling a Thumb function.  */
   8960 
   8961 static int
   8962 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
   8963 			 const char *		name,
   8964 			 bfd *			input_bfd,
   8965 			 bfd *			output_bfd,
   8966 			 asection *		input_section,
   8967 			 bfd_byte *		hit_data,
   8968 			 asection *		sym_sec,
   8969 			 bfd_vma		offset,
   8970 			 bfd_signed_vma		addend,
   8971 			 bfd_vma		val,
   8972 			 char **error_message)
   8973 {
   8974   unsigned long int tmp;
   8975   bfd_vma my_offset;
   8976   asection * s;
   8977   long int ret_offset;
   8978   struct elf_link_hash_entry * myh;
   8979   struct elf32_arm_link_hash_table * globals;
   8980 
   8981   globals = elf32_arm_hash_table (info);
   8982   BFD_ASSERT (globals != NULL);
   8983   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   8984 
   8985   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
   8986 			      ARM2THUMB_GLUE_SECTION_NAME);
   8987   BFD_ASSERT (s != NULL);
   8988   BFD_ASSERT (s->contents != NULL);
   8989   BFD_ASSERT (s->output_section != NULL);
   8990 
   8991   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
   8992 				     sym_sec, val, s, error_message);
   8993   if (!myh)
   8994     return FALSE;
   8995 
   8996   my_offset = myh->root.u.def.value;
   8997   tmp = bfd_get_32 (input_bfd, hit_data);
   8998   tmp = tmp & 0xFF000000;
   8999 
   9000   /* Somehow these are both 4 too far, so subtract 8.  */
   9001   ret_offset = (s->output_offset
   9002 		+ my_offset
   9003 		+ s->output_section->vma
   9004 		- (input_section->output_offset
   9005 		   + input_section->output_section->vma
   9006 		   + offset + addend)
   9007 		- 8);
   9008 
   9009   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
   9010 
   9011   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
   9012 
   9013   return TRUE;
   9014 }
   9015 
   9016 /* Populate Arm stub for an exported Thumb function.  */
   9017 
   9018 static bfd_boolean
   9019 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
   9020 {
   9021   struct bfd_link_info * info = (struct bfd_link_info *) inf;
   9022   asection * s;
   9023   struct elf_link_hash_entry * myh;
   9024   struct elf32_arm_link_hash_entry *eh;
   9025   struct elf32_arm_link_hash_table * globals;
   9026   asection *sec;
   9027   bfd_vma val;
   9028   char *error_message;
   9029 
   9030   eh = elf32_arm_hash_entry (h);
   9031   /* Allocate stubs for exported Thumb functions on v4t.  */
   9032   if (eh->export_glue == NULL)
   9033     return TRUE;
   9034 
   9035   globals = elf32_arm_hash_table (info);
   9036   BFD_ASSERT (globals != NULL);
   9037   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   9038 
   9039   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
   9040 			      ARM2THUMB_GLUE_SECTION_NAME);
   9041   BFD_ASSERT (s != NULL);
   9042   BFD_ASSERT (s->contents != NULL);
   9043   BFD_ASSERT (s->output_section != NULL);
   9044 
   9045   sec = eh->export_glue->root.u.def.section;
   9046 
   9047   BFD_ASSERT (sec->output_section != NULL);
   9048 
   9049   val = eh->export_glue->root.u.def.value + sec->output_offset
   9050 	+ sec->output_section->vma;
   9051 
   9052   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
   9053 				     h->root.u.def.section->owner,
   9054 				     globals->obfd, sec, val, s,
   9055 				     &error_message);
   9056   BFD_ASSERT (myh);
   9057   return TRUE;
   9058 }
   9059 
   9060 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
   9061 
   9062 static bfd_vma
   9063 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
   9064 {
   9065   bfd_byte *p;
   9066   bfd_vma glue_addr;
   9067   asection *s;
   9068   struct elf32_arm_link_hash_table *globals;
   9069 
   9070   globals = elf32_arm_hash_table (info);
   9071   BFD_ASSERT (globals != NULL);
   9072   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
   9073 
   9074   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
   9075 			      ARM_BX_GLUE_SECTION_NAME);
   9076   BFD_ASSERT (s != NULL);
   9077   BFD_ASSERT (s->contents != NULL);
   9078   BFD_ASSERT (s->output_section != NULL);
   9079 
   9080   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
   9081 
   9082   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
   9083 
   9084   if ((globals->bx_glue_offset[reg] & 1) == 0)
   9085     {
   9086       p = s->contents + glue_addr;
   9087       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
   9088       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
   9089       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
   9090       globals->bx_glue_offset[reg] |= 1;
   9091     }
   9092 
   9093   return glue_addr + s->output_section->vma + s->output_offset;
   9094 }
   9095 
   9096 /* Generate Arm stubs for exported Thumb symbols.  */
   9097 static void
   9098 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
   9099 				  struct bfd_link_info *link_info)
   9100 {
   9101   struct elf32_arm_link_hash_table * globals;
   9102 
   9103   if (link_info == NULL)
   9104     /* Ignore this if we are not called by the ELF backend linker.  */
   9105     return;
   9106 
   9107   globals = elf32_arm_hash_table (link_info);
   9108   if (globals == NULL)
   9109     return;
   9110 
   9111   /* If blx is available then exported Thumb symbols are OK and there is
   9112      nothing to do.  */
   9113   if (globals->use_blx)
   9114     return;
   9115 
   9116   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
   9117 			  link_info);
   9118 }
   9119 
   9120 /* Reserve space for COUNT dynamic relocations in relocation selection
   9121    SRELOC.  */
   9122 
   9123 static void
   9124 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
   9125 			      bfd_size_type count)
   9126 {
   9127   struct elf32_arm_link_hash_table *htab;
   9128 
   9129   htab = elf32_arm_hash_table (info);
   9130   BFD_ASSERT (htab->root.dynamic_sections_created);
   9131   if (sreloc == NULL)
   9132     abort ();
   9133   sreloc->size += RELOC_SIZE (htab) * count;
   9134 }
   9135 
   9136 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
   9137    dynamic, the relocations should go in SRELOC, otherwise they should
   9138    go in the special .rel.iplt section.  */
   9139 
   9140 static void
   9141 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
   9142 			    bfd_size_type count)
   9143 {
   9144   struct elf32_arm_link_hash_table *htab;
   9145 
   9146   htab = elf32_arm_hash_table (info);
   9147   if (!htab->root.dynamic_sections_created)
   9148     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
   9149   else
   9150     {
   9151       BFD_ASSERT (sreloc != NULL);
   9152       sreloc->size += RELOC_SIZE (htab) * count;
   9153     }
   9154 }
   9155 
   9156 /* Add relocation REL to the end of relocation section SRELOC.  */
   9157 
   9158 static void
   9159 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
   9160 			asection *sreloc, Elf_Internal_Rela *rel)
   9161 {
   9162   bfd_byte *loc;
   9163   struct elf32_arm_link_hash_table *htab;
   9164 
   9165   htab = elf32_arm_hash_table (info);
   9166   if (!htab->root.dynamic_sections_created
   9167       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
   9168     sreloc = htab->root.irelplt;
   9169   if (sreloc == NULL)
   9170     abort ();
   9171   loc = sreloc->contents;
   9172   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
   9173   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
   9174     abort ();
   9175   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
   9176 }
   9177 
   9178 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
   9179    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
   9180    to .plt.  */
   9181 
   9182 static void
   9183 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
   9184 			      bfd_boolean is_iplt_entry,
   9185 			      union gotplt_union *root_plt,
   9186 			      struct arm_plt_info *arm_plt)
   9187 {
   9188   struct elf32_arm_link_hash_table *htab;
   9189   asection *splt;
   9190   asection *sgotplt;
   9191 
   9192   htab = elf32_arm_hash_table (info);
   9193 
   9194   if (is_iplt_entry)
   9195     {
   9196       splt = htab->root.iplt;
   9197       sgotplt = htab->root.igotplt;
   9198 
   9199       /* NaCl uses a special first entry in .iplt too.  */
   9200       if (htab->nacl_p && splt->size == 0)
   9201 	splt->size += htab->plt_header_size;
   9202 
   9203       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
   9204       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
   9205     }
   9206   else
   9207     {
   9208       splt = htab->root.splt;
   9209       sgotplt = htab->root.sgotplt;
   9210 
   9211       /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
   9212       elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
   9213 
   9214       /* If this is the first .plt entry, make room for the special
   9215 	 first entry.  */
   9216       if (splt->size == 0)
   9217 	splt->size += htab->plt_header_size;
   9218 
   9219       htab->next_tls_desc_index++;
   9220     }
   9221 
   9222   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
   9223   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
   9224     splt->size += PLT_THUMB_STUB_SIZE;
   9225   root_plt->offset = splt->size;
   9226   splt->size += htab->plt_entry_size;
   9227 
   9228   if (!htab->symbian_p)
   9229     {
   9230       /* We also need to make an entry in the .got.plt section, which
   9231 	 will be placed in the .got section by the linker script.  */
   9232       if (is_iplt_entry)
   9233 	arm_plt->got_offset = sgotplt->size;
   9234       else
   9235 	arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
   9236       sgotplt->size += 4;
   9237     }
   9238 }
   9239 
   9240 static bfd_vma
   9241 arm_movw_immediate (bfd_vma value)
   9242 {
   9243   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
   9244 }
   9245 
   9246 static bfd_vma
   9247 arm_movt_immediate (bfd_vma value)
   9248 {
   9249   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
   9250 }
   9251 
   9252 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
   9253    the entry lives in .iplt and resolves to (*SYM_VALUE)().
   9254    Otherwise, DYNINDX is the index of the symbol in the dynamic
   9255    symbol table and SYM_VALUE is undefined.
   9256 
   9257    ROOT_PLT points to the offset of the PLT entry from the start of its
   9258    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
   9259    bookkeeping information.
   9260 
   9261    Returns FALSE if there was a problem.  */
   9262 
   9263 static bfd_boolean
   9264 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
   9265 			      union gotplt_union *root_plt,
   9266 			      struct arm_plt_info *arm_plt,
   9267 			      int dynindx, bfd_vma sym_value)
   9268 {
   9269   struct elf32_arm_link_hash_table *htab;
   9270   asection *sgot;
   9271   asection *splt;
   9272   asection *srel;
   9273   bfd_byte *loc;
   9274   bfd_vma plt_index;
   9275   Elf_Internal_Rela rel;
   9276   bfd_vma plt_header_size;
   9277   bfd_vma got_header_size;
   9278 
   9279   htab = elf32_arm_hash_table (info);
   9280 
   9281   /* Pick the appropriate sections and sizes.  */
   9282   if (dynindx == -1)
   9283     {
   9284       splt = htab->root.iplt;
   9285       sgot = htab->root.igotplt;
   9286       srel = htab->root.irelplt;
   9287 
   9288       /* There are no reserved entries in .igot.plt, and no special
   9289 	 first entry in .iplt.  */
   9290       got_header_size = 0;
   9291       plt_header_size = 0;
   9292     }
   9293   else
   9294     {
   9295       splt = htab->root.splt;
   9296       sgot = htab->root.sgotplt;
   9297       srel = htab->root.srelplt;
   9298 
   9299       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
   9300       plt_header_size = htab->plt_header_size;
   9301     }
   9302   BFD_ASSERT (splt != NULL && srel != NULL);
   9303 
   9304   /* Fill in the entry in the procedure linkage table.  */
   9305   if (htab->symbian_p)
   9306     {
   9307       BFD_ASSERT (dynindx >= 0);
   9308       put_arm_insn (htab, output_bfd,
   9309 		    elf32_arm_symbian_plt_entry[0],
   9310 		    splt->contents + root_plt->offset);
   9311       bfd_put_32 (output_bfd,
   9312 		  elf32_arm_symbian_plt_entry[1],
   9313 		  splt->contents + root_plt->offset + 4);
   9314 
   9315       /* Fill in the entry in the .rel.plt section.  */
   9316       rel.r_offset = (splt->output_section->vma
   9317 		      + splt->output_offset
   9318 		      + root_plt->offset + 4);
   9319       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
   9320 
   9321       /* Get the index in the procedure linkage table which
   9322 	 corresponds to this symbol.  This is the index of this symbol
   9323 	 in all the symbols for which we are making plt entries.  The
   9324 	 first entry in the procedure linkage table is reserved.  */
   9325       plt_index = ((root_plt->offset - plt_header_size)
   9326 		   / htab->plt_entry_size);
   9327     }
   9328   else
   9329     {
   9330       bfd_vma got_offset, got_address, plt_address;
   9331       bfd_vma got_displacement, initial_got_entry;
   9332       bfd_byte * ptr;
   9333 
   9334       BFD_ASSERT (sgot != NULL);
   9335 
   9336       /* Get the offset into the .(i)got.plt table of the entry that
   9337 	 corresponds to this function.  */
   9338       got_offset = (arm_plt->got_offset & -2);
   9339 
   9340       /* Get the index in the procedure linkage table which
   9341 	 corresponds to this symbol.  This is the index of this symbol
   9342 	 in all the symbols for which we are making plt entries.
   9343 	 After the reserved .got.plt entries, all symbols appear in
   9344 	 the same order as in .plt.  */
   9345       plt_index = (got_offset - got_header_size) / 4;
   9346 
   9347       /* Calculate the address of the GOT entry.  */
   9348       got_address = (sgot->output_section->vma
   9349 		     + sgot->output_offset
   9350 		     + got_offset);
   9351 
   9352       /* ...and the address of the PLT entry.  */
   9353       plt_address = (splt->output_section->vma
   9354 		     + splt->output_offset
   9355 		     + root_plt->offset);
   9356 
   9357       ptr = splt->contents + root_plt->offset;
   9358       if (htab->vxworks_p && bfd_link_pic (info))
   9359 	{
   9360 	  unsigned int i;
   9361 	  bfd_vma val;
   9362 
   9363 	  for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
   9364 	    {
   9365 	      val = elf32_arm_vxworks_shared_plt_entry[i];
   9366 	      if (i == 2)
   9367 		val |= got_address - sgot->output_section->vma;
   9368 	      if (i == 5)
   9369 		val |= plt_index * RELOC_SIZE (htab);
   9370 	      if (i == 2 || i == 5)
   9371 		bfd_put_32 (output_bfd, val, ptr);
   9372 	      else
   9373 		put_arm_insn (htab, output_bfd, val, ptr);
   9374 	    }
   9375 	}
   9376       else if (htab->vxworks_p)
   9377 	{
   9378 	  unsigned int i;
   9379 	  bfd_vma val;
   9380 
   9381 	  for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
   9382 	    {
   9383 	      val = elf32_arm_vxworks_exec_plt_entry[i];
   9384 	      if (i == 2)
   9385 		val |= got_address;
   9386 	      if (i == 4)
   9387 		val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
   9388 	      if (i == 5)
   9389 		val |= plt_index * RELOC_SIZE (htab);
   9390 	      if (i == 2 || i == 5)
   9391 		bfd_put_32 (output_bfd, val, ptr);
   9392 	      else
   9393 		put_arm_insn (htab, output_bfd, val, ptr);
   9394 	    }
   9395 
   9396 	  loc = (htab->srelplt2->contents
   9397 		 + (plt_index * 2 + 1) * RELOC_SIZE (htab));
   9398 
   9399 	  /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
   9400 	     referencing the GOT for this PLT entry.  */
   9401 	  rel.r_offset = plt_address + 8;
   9402 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
   9403 	  rel.r_addend = got_offset;
   9404 	  SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
   9405 	  loc += RELOC_SIZE (htab);
   9406 
   9407 	  /* Create the R_ARM_ABS32 relocation referencing the
   9408 	     beginning of the PLT for this GOT entry.  */
   9409 	  rel.r_offset = got_address;
   9410 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
   9411 	  rel.r_addend = 0;
   9412 	  SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
   9413 	}
   9414       else if (htab->nacl_p)
   9415 	{
   9416 	  /* Calculate the displacement between the PLT slot and the
   9417 	     common tail that's part of the special initial PLT slot.  */
   9418 	  int32_t tail_displacement
   9419 	    = ((splt->output_section->vma + splt->output_offset
   9420 		+ ARM_NACL_PLT_TAIL_OFFSET)
   9421 	       - (plt_address + htab->plt_entry_size + 4));
   9422 	  BFD_ASSERT ((tail_displacement & 3) == 0);
   9423 	  tail_displacement >>= 2;
   9424 
   9425 	  BFD_ASSERT ((tail_displacement & 0xff000000) == 0
   9426 		      || (-tail_displacement & 0xff000000) == 0);
   9427 
   9428 	  /* Calculate the displacement between the PLT slot and the entry
   9429 	     in the GOT.  The offset accounts for the value produced by
   9430 	     adding to pc in the penultimate instruction of the PLT stub.  */
   9431 	  got_displacement = (got_address
   9432 			      - (plt_address + htab->plt_entry_size));
   9433 
   9434 	  /* NaCl does not support interworking at all.  */
   9435 	  BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
   9436 
   9437 	  put_arm_insn (htab, output_bfd,
   9438 			elf32_arm_nacl_plt_entry[0]
   9439 			| arm_movw_immediate (got_displacement),
   9440 			ptr + 0);
   9441 	  put_arm_insn (htab, output_bfd,
   9442 			elf32_arm_nacl_plt_entry[1]
   9443 			| arm_movt_immediate (got_displacement),
   9444 			ptr + 4);
   9445 	  put_arm_insn (htab, output_bfd,
   9446 			elf32_arm_nacl_plt_entry[2],
   9447 			ptr + 8);
   9448 	  put_arm_insn (htab, output_bfd,
   9449 			elf32_arm_nacl_plt_entry[3]
   9450 			| (tail_displacement & 0x00ffffff),
   9451 			ptr + 12);
   9452 	}
   9453       else if (using_thumb_only (htab))
   9454 	{
   9455 	  /* PR ld/16017: Generate thumb only PLT entries.  */
   9456 	  if (!using_thumb2 (htab))
   9457 	    {
   9458 	      /* FIXME: We ought to be able to generate thumb-1 PLT
   9459 		 instructions...  */
   9460 	      _bfd_error_handler (_("%B: Warning: thumb-1 mode PLT generation not currently supported"),
   9461 				  output_bfd);
   9462 	      return FALSE;
   9463 	    }
   9464 
   9465 	  /* Calculate the displacement between the PLT slot and the entry in
   9466 	     the GOT.  The 12-byte offset accounts for the value produced by
   9467 	     adding to pc in the 3rd instruction of the PLT stub.  */
   9468 	  got_displacement = got_address - (plt_address + 12);
   9469 
   9470 	  /* As we are using 32 bit instructions we have to use 'put_arm_insn'
   9471 	     instead of 'put_thumb_insn'.  */
   9472 	  put_arm_insn (htab, output_bfd,
   9473 			elf32_thumb2_plt_entry[0]
   9474 			| ((got_displacement & 0x000000ff) << 16)
   9475 			| ((got_displacement & 0x00000700) << 20)
   9476 			| ((got_displacement & 0x00000800) >>  1)
   9477 			| ((got_displacement & 0x0000f000) >> 12),
   9478 			ptr + 0);
   9479 	  put_arm_insn (htab, output_bfd,
   9480 			elf32_thumb2_plt_entry[1]
   9481 			| ((got_displacement & 0x00ff0000)      )
   9482 			| ((got_displacement & 0x07000000) <<  4)
   9483 			| ((got_displacement & 0x08000000) >> 17)
   9484 			| ((got_displacement & 0xf0000000) >> 28),
   9485 			ptr + 4);
   9486 	  put_arm_insn (htab, output_bfd,
   9487 			elf32_thumb2_plt_entry[2],
   9488 			ptr + 8);
   9489 	  put_arm_insn (htab, output_bfd,
   9490 			elf32_thumb2_plt_entry[3],
   9491 			ptr + 12);
   9492 	}
   9493       else
   9494 	{
   9495 	  /* Calculate the displacement between the PLT slot and the
   9496 	     entry in the GOT.  The eight-byte offset accounts for the
   9497 	     value produced by adding to pc in the first instruction
   9498 	     of the PLT stub.  */
   9499 	  got_displacement = got_address - (plt_address + 8);
   9500 
   9501 	  if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
   9502 	    {
   9503 	      put_thumb_insn (htab, output_bfd,
   9504 			      elf32_arm_plt_thumb_stub[0], ptr - 4);
   9505 	      put_thumb_insn (htab, output_bfd,
   9506 			      elf32_arm_plt_thumb_stub[1], ptr - 2);
   9507 	    }
   9508 
   9509 	  if (!elf32_arm_use_long_plt_entry)
   9510 	    {
   9511 	      BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
   9512 
   9513 	      put_arm_insn (htab, output_bfd,
   9514 			    elf32_arm_plt_entry_short[0]
   9515 			    | ((got_displacement & 0x0ff00000) >> 20),
   9516 			    ptr + 0);
   9517 	      put_arm_insn (htab, output_bfd,
   9518 			    elf32_arm_plt_entry_short[1]
   9519 			    | ((got_displacement & 0x000ff000) >> 12),
   9520 			    ptr+ 4);
   9521 	      put_arm_insn (htab, output_bfd,
   9522 			    elf32_arm_plt_entry_short[2]
   9523 			    | (got_displacement & 0x00000fff),
   9524 			    ptr + 8);
   9525 #ifdef FOUR_WORD_PLT
   9526 	      bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
   9527 #endif
   9528 	    }
   9529 	  else
   9530 	    {
   9531 	      put_arm_insn (htab, output_bfd,
   9532 			    elf32_arm_plt_entry_long[0]
   9533 			    | ((got_displacement & 0xf0000000) >> 28),
   9534 			    ptr + 0);
   9535 	      put_arm_insn (htab, output_bfd,
   9536 			    elf32_arm_plt_entry_long[1]
   9537 			    | ((got_displacement & 0x0ff00000) >> 20),
   9538 			    ptr + 4);
   9539 	      put_arm_insn (htab, output_bfd,
   9540 			    elf32_arm_plt_entry_long[2]
   9541 			    | ((got_displacement & 0x000ff000) >> 12),
   9542 			    ptr+ 8);
   9543 	      put_arm_insn (htab, output_bfd,
   9544 			    elf32_arm_plt_entry_long[3]
   9545 			    | (got_displacement & 0x00000fff),
   9546 			    ptr + 12);
   9547 	    }
   9548 	}
   9549 
   9550       /* Fill in the entry in the .rel(a).(i)plt section.  */
   9551       rel.r_offset = got_address;
   9552       rel.r_addend = 0;
   9553       if (dynindx == -1)
   9554 	{
   9555 	  /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
   9556 	     The dynamic linker or static executable then calls SYM_VALUE
   9557 	     to determine the correct run-time value of the .igot.plt entry.  */
   9558 	  rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
   9559 	  initial_got_entry = sym_value;
   9560 	}
   9561       else
   9562 	{
   9563 	  rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
   9564 	  initial_got_entry = (splt->output_section->vma
   9565 			       + splt->output_offset);
   9566 	}
   9567 
   9568       /* Fill in the entry in the global offset table.  */
   9569       bfd_put_32 (output_bfd, initial_got_entry,
   9570 		  sgot->contents + got_offset);
   9571     }
   9572 
   9573   if (dynindx == -1)
   9574     elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
   9575   else
   9576     {
   9577       loc = srel->contents + plt_index * RELOC_SIZE (htab);
   9578       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
   9579     }
   9580 
   9581   return TRUE;
   9582 }
   9583 
   9584 /* Some relocations map to different relocations depending on the
   9585    target.  Return the real relocation.  */
   9586 
   9587 static int
   9588 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
   9589 		     int r_type)
   9590 {
   9591   switch (r_type)
   9592     {
   9593     case R_ARM_TARGET1:
   9594       if (globals->target1_is_rel)
   9595 	return R_ARM_REL32;
   9596       else
   9597 	return R_ARM_ABS32;
   9598 
   9599     case R_ARM_TARGET2:
   9600       return globals->target2_reloc;
   9601 
   9602     default:
   9603       return r_type;
   9604     }
   9605 }
   9606 
   9607 /* Return the base VMA address which should be subtracted from real addresses
   9608    when resolving @dtpoff relocation.
   9609    This is PT_TLS segment p_vaddr.  */
   9610 
   9611 static bfd_vma
   9612 dtpoff_base (struct bfd_link_info *info)
   9613 {
   9614   /* If tls_sec is NULL, we should have signalled an error already.  */
   9615   if (elf_hash_table (info)->tls_sec == NULL)
   9616     return 0;
   9617   return elf_hash_table (info)->tls_sec->vma;
   9618 }
   9619 
   9620 /* Return the relocation value for @tpoff relocation
   9621    if STT_TLS virtual address is ADDRESS.  */
   9622 
   9623 static bfd_vma
   9624 tpoff (struct bfd_link_info *info, bfd_vma address)
   9625 {
   9626   struct elf_link_hash_table *htab = elf_hash_table (info);
   9627   bfd_vma base;
   9628 
   9629   /* If tls_sec is NULL, we should have signalled an error already.  */
   9630   if (htab->tls_sec == NULL)
   9631     return 0;
   9632   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
   9633   return address - htab->tls_sec->vma + base;
   9634 }
   9635 
   9636 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
   9637    VALUE is the relocation value.  */
   9638 
   9639 static bfd_reloc_status_type
   9640 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
   9641 {
   9642   if (value > 0xfff)
   9643     return bfd_reloc_overflow;
   9644 
   9645   value |= bfd_get_32 (abfd, data) & 0xfffff000;
   9646   bfd_put_32 (abfd, value, data);
   9647   return bfd_reloc_ok;
   9648 }
   9649 
   9650 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
   9651    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
   9652    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
   9653 
   9654    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
   9655    is to then call final_link_relocate.  Return other values in the
   9656    case of error.
   9657 
   9658    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
   9659    the pre-relaxed code.  It would be nice if the relocs were updated
   9660    to match the optimization.   */
   9661 
   9662 static bfd_reloc_status_type
   9663 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
   9664 		     bfd *input_bfd, asection *input_sec, bfd_byte *contents,
   9665 		     Elf_Internal_Rela *rel, unsigned long is_local)
   9666 {
   9667   unsigned long insn;
   9668 
   9669   switch (ELF32_R_TYPE (rel->r_info))
   9670     {
   9671     default:
   9672       return bfd_reloc_notsupported;
   9673 
   9674     case R_ARM_TLS_GOTDESC:
   9675       if (is_local)
   9676 	insn = 0;
   9677       else
   9678 	{
   9679 	  insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   9680 	  if (insn & 1)
   9681 	    insn -= 5; /* THUMB */
   9682 	  else
   9683 	    insn -= 8; /* ARM */
   9684 	}
   9685       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   9686       return bfd_reloc_continue;
   9687 
   9688     case R_ARM_THM_TLS_DESCSEQ:
   9689       /* Thumb insn.  */
   9690       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
   9691       if ((insn & 0xff78) == 0x4478)	  /* add rx, pc */
   9692 	{
   9693 	  if (is_local)
   9694 	    /* nop */
   9695 	    bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
   9696 	}
   9697       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
   9698 	{
   9699 	  if (is_local)
   9700 	    /* nop */
   9701 	    bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
   9702 	  else
   9703 	    /* ldr rx,[ry] */
   9704 	    bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
   9705 	}
   9706       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
   9707 	{
   9708 	  if (is_local)
   9709 	    /* nop */
   9710 	    bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
   9711 	  else
   9712 	    /* mov r0, rx */
   9713 	    bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
   9714 			contents + rel->r_offset);
   9715 	}
   9716       else
   9717 	{
   9718 	  if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
   9719 	    /* It's a 32 bit instruction, fetch the rest of it for
   9720 	       error generation.  */
   9721 	    insn = (insn << 16)
   9722 	      | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
   9723 	  _bfd_error_handler
   9724 	    /* xgettext:c-format */
   9725 	    (_("%B(%A+%#Lx): unexpected Thumb instruction '%#lx' in TLS trampoline"),
   9726 	     input_bfd, input_sec, rel->r_offset, insn);
   9727 	  return bfd_reloc_notsupported;
   9728 	}
   9729       break;
   9730 
   9731     case R_ARM_TLS_DESCSEQ:
   9732       /* arm insn.  */
   9733       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   9734       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
   9735 	{
   9736 	  if (is_local)
   9737 	    /* mov rx, ry */
   9738 	    bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
   9739 			contents + rel->r_offset);
   9740 	}
   9741       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
   9742 	{
   9743 	  if (is_local)
   9744 	    /* nop */
   9745 	    bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
   9746 	  else
   9747 	    /* ldr rx,[ry] */
   9748 	    bfd_put_32 (input_bfd, insn & 0xfffff000,
   9749 			contents + rel->r_offset);
   9750 	}
   9751       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
   9752 	{
   9753 	  if (is_local)
   9754 	    /* nop */
   9755 	    bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
   9756 	  else
   9757 	    /* mov r0, rx */
   9758 	    bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
   9759 			contents + rel->r_offset);
   9760 	}
   9761       else
   9762 	{
   9763 	  _bfd_error_handler
   9764 	    /* xgettext:c-format */
   9765 	    (_("%B(%A+%#Lx): unexpected ARM instruction '%#lx' in TLS trampoline"),
   9766 	     input_bfd, input_sec, rel->r_offset, insn);
   9767 	  return bfd_reloc_notsupported;
   9768 	}
   9769       break;
   9770 
   9771     case R_ARM_TLS_CALL:
   9772       /* GD->IE relaxation, turn the instruction into 'nop' or
   9773 	 'ldr r0, [pc,r0]'  */
   9774       insn = is_local ? 0xe1a00000 : 0xe79f0000;
   9775       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   9776       break;
   9777 
   9778     case R_ARM_THM_TLS_CALL:
   9779       /* GD->IE relaxation.  */
   9780       if (!is_local)
   9781 	/* add r0,pc; ldr r0, [r0]  */
   9782 	insn = 0x44786800;
   9783       else if (using_thumb2 (globals))
   9784 	/* nop.w */
   9785 	insn = 0xf3af8000;
   9786       else
   9787 	/* nop; nop */
   9788 	insn = 0xbf00bf00;
   9789 
   9790       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
   9791       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
   9792       break;
   9793     }
   9794   return bfd_reloc_ok;
   9795 }
   9796 
   9797 /* For a given value of n, calculate the value of G_n as required to
   9798    deal with group relocations.  We return it in the form of an
   9799    encoded constant-and-rotation, together with the final residual.  If n is
   9800    specified as less than zero, then final_residual is filled with the
   9801    input value and no further action is performed.  */
   9802 
   9803 static bfd_vma
   9804 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
   9805 {
   9806   int current_n;
   9807   bfd_vma g_n;
   9808   bfd_vma encoded_g_n = 0;
   9809   bfd_vma residual = value; /* Also known as Y_n.  */
   9810 
   9811   for (current_n = 0; current_n <= n; current_n++)
   9812     {
   9813       int shift;
   9814 
   9815       /* Calculate which part of the value to mask.  */
   9816       if (residual == 0)
   9817 	shift = 0;
   9818       else
   9819 	{
   9820 	  int msb;
   9821 
   9822 	  /* Determine the most significant bit in the residual and
   9823 	     align the resulting value to a 2-bit boundary.  */
   9824 	  for (msb = 30; msb >= 0; msb -= 2)
   9825 	    if (residual & (3 << msb))
   9826 	      break;
   9827 
   9828 	  /* The desired shift is now (msb - 6), or zero, whichever
   9829 	     is the greater.  */
   9830 	  shift = msb - 6;
   9831 	  if (shift < 0)
   9832 	    shift = 0;
   9833 	}
   9834 
   9835       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
   9836       g_n = residual & (0xff << shift);
   9837       encoded_g_n = (g_n >> shift)
   9838 		    | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
   9839 
   9840       /* Calculate the residual for the next time around.  */
   9841       residual &= ~g_n;
   9842     }
   9843 
   9844   *final_residual = residual;
   9845 
   9846   return encoded_g_n;
   9847 }
   9848 
   9849 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
   9850    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
   9851 
   9852 static int
   9853 identify_add_or_sub (bfd_vma insn)
   9854 {
   9855   int opcode = insn & 0x1e00000;
   9856 
   9857   if (opcode == 1 << 23) /* ADD */
   9858     return 1;
   9859 
   9860   if (opcode == 1 << 22) /* SUB */
   9861     return -1;
   9862 
   9863   return 0;
   9864 }
   9865 
   9866 /* Perform a relocation as part of a final link.  */
   9867 
   9868 static bfd_reloc_status_type
   9869 elf32_arm_final_link_relocate (reloc_howto_type *	    howto,
   9870 			       bfd *			    input_bfd,
   9871 			       bfd *			    output_bfd,
   9872 			       asection *		    input_section,
   9873 			       bfd_byte *		    contents,
   9874 			       Elf_Internal_Rela *	    rel,
   9875 			       bfd_vma			    value,
   9876 			       struct bfd_link_info *	    info,
   9877 			       asection *		    sym_sec,
   9878 			       const char *		    sym_name,
   9879 			       unsigned char		    st_type,
   9880 			       enum arm_st_branch_type	    branch_type,
   9881 			       struct elf_link_hash_entry * h,
   9882 			       bfd_boolean *		    unresolved_reloc_p,
   9883 			       char **			    error_message)
   9884 {
   9885   unsigned long			r_type = howto->type;
   9886   unsigned long			r_symndx;
   9887   bfd_byte *			hit_data = contents + rel->r_offset;
   9888   bfd_vma *			local_got_offsets;
   9889   bfd_vma *			local_tlsdesc_gotents;
   9890   asection *			sgot;
   9891   asection *			splt;
   9892   asection *			sreloc = NULL;
   9893   asection *			srelgot;
   9894   bfd_vma			addend;
   9895   bfd_signed_vma		signed_addend;
   9896   unsigned char			dynreloc_st_type;
   9897   bfd_vma			dynreloc_value;
   9898   struct elf32_arm_link_hash_table * globals;
   9899   struct elf32_arm_link_hash_entry *eh;
   9900   union gotplt_union	       *root_plt;
   9901   struct arm_plt_info	       *arm_plt;
   9902   bfd_vma			plt_offset;
   9903   bfd_vma			gotplt_offset;
   9904   bfd_boolean			has_iplt_entry;
   9905   bfd_boolean			resolved_to_zero;
   9906 
   9907   globals = elf32_arm_hash_table (info);
   9908   if (globals == NULL)
   9909     return bfd_reloc_notsupported;
   9910 
   9911   BFD_ASSERT (is_arm_elf (input_bfd));
   9912   BFD_ASSERT (howto != NULL);
   9913 
   9914   /* Some relocation types map to different relocations depending on the
   9915      target.  We pick the right one here.  */
   9916   r_type = arm_real_reloc_type (globals, r_type);
   9917 
   9918   /* It is possible to have linker relaxations on some TLS access
   9919      models.  Update our information here.  */
   9920   r_type = elf32_arm_tls_transition (info, r_type, h);
   9921 
   9922   if (r_type != howto->type)
   9923     howto = elf32_arm_howto_from_type (r_type);
   9924 
   9925   eh = (struct elf32_arm_link_hash_entry *) h;
   9926   sgot = globals->root.sgot;
   9927   local_got_offsets = elf_local_got_offsets (input_bfd);
   9928   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
   9929 
   9930   if (globals->root.dynamic_sections_created)
   9931     srelgot = globals->root.srelgot;
   9932   else
   9933     srelgot = NULL;
   9934 
   9935   r_symndx = ELF32_R_SYM (rel->r_info);
   9936 
   9937   if (globals->use_rel)
   9938     {
   9939       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
   9940 
   9941       if (addend & ((howto->src_mask + 1) >> 1))
   9942 	{
   9943 	  signed_addend = -1;
   9944 	  signed_addend &= ~ howto->src_mask;
   9945 	  signed_addend |= addend;
   9946 	}
   9947       else
   9948 	signed_addend = addend;
   9949     }
   9950   else
   9951     addend = signed_addend = rel->r_addend;
   9952 
   9953   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
   9954      are resolving a function call relocation.  */
   9955   if (using_thumb_only (globals)
   9956       && (r_type == R_ARM_THM_CALL
   9957 	  || r_type == R_ARM_THM_JUMP24)
   9958       && branch_type == ST_BRANCH_TO_ARM)
   9959     branch_type = ST_BRANCH_TO_THUMB;
   9960 
   9961   /* Record the symbol information that should be used in dynamic
   9962      relocations.  */
   9963   dynreloc_st_type = st_type;
   9964   dynreloc_value = value;
   9965   if (branch_type == ST_BRANCH_TO_THUMB)
   9966     dynreloc_value |= 1;
   9967 
   9968   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
   9969      VALUE appropriately for relocations that we resolve at link time.  */
   9970   has_iplt_entry = FALSE;
   9971   if (elf32_arm_get_plt_info (input_bfd, globals, eh, r_symndx, &root_plt,
   9972 			      &arm_plt)
   9973       && root_plt->offset != (bfd_vma) -1)
   9974     {
   9975       plt_offset = root_plt->offset;
   9976       gotplt_offset = arm_plt->got_offset;
   9977 
   9978       if (h == NULL || eh->is_iplt)
   9979 	{
   9980 	  has_iplt_entry = TRUE;
   9981 	  splt = globals->root.iplt;
   9982 
   9983 	  /* Populate .iplt entries here, because not all of them will
   9984 	     be seen by finish_dynamic_symbol.  The lower bit is set if
   9985 	     we have already populated the entry.  */
   9986 	  if (plt_offset & 1)
   9987 	    plt_offset--;
   9988 	  else
   9989 	    {
   9990 	      if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
   9991 						-1, dynreloc_value))
   9992 		root_plt->offset |= 1;
   9993 	      else
   9994 		return bfd_reloc_notsupported;
   9995 	    }
   9996 
   9997 	  /* Static relocations always resolve to the .iplt entry.  */
   9998 	  st_type = STT_FUNC;
   9999 	  value = (splt->output_section->vma
   10000 		   + splt->output_offset
   10001 		   + plt_offset);
   10002 	  branch_type = ST_BRANCH_TO_ARM;
   10003 
   10004 	  /* If there are non-call relocations that resolve to the .iplt
   10005 	     entry, then all dynamic ones must too.  */
   10006 	  if (arm_plt->noncall_refcount != 0)
   10007 	    {
   10008 	      dynreloc_st_type = st_type;
   10009 	      dynreloc_value = value;
   10010 	    }
   10011 	}
   10012       else
   10013 	/* We populate the .plt entry in finish_dynamic_symbol.  */
   10014 	splt = globals->root.splt;
   10015     }
   10016   else
   10017     {
   10018       splt = NULL;
   10019       plt_offset = (bfd_vma) -1;
   10020       gotplt_offset = (bfd_vma) -1;
   10021     }
   10022 
   10023   resolved_to_zero = (h != NULL
   10024 		      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   10025 
   10026   switch (r_type)
   10027     {
   10028     case R_ARM_NONE:
   10029       /* We don't need to find a value for this symbol.  It's just a
   10030 	 marker.  */
   10031       *unresolved_reloc_p = FALSE;
   10032       return bfd_reloc_ok;
   10033 
   10034     case R_ARM_ABS12:
   10035       if (!globals->vxworks_p)
   10036 	return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
   10037       /* Fall through.  */
   10038 
   10039     case R_ARM_PC24:
   10040     case R_ARM_ABS32:
   10041     case R_ARM_ABS32_NOI:
   10042     case R_ARM_REL32:
   10043     case R_ARM_REL32_NOI:
   10044     case R_ARM_CALL:
   10045     case R_ARM_JUMP24:
   10046     case R_ARM_XPC25:
   10047     case R_ARM_PREL31:
   10048     case R_ARM_PLT32:
   10049       /* Handle relocations which should use the PLT entry.  ABS32/REL32
   10050 	 will use the symbol's value, which may point to a PLT entry, but we
   10051 	 don't need to handle that here.  If we created a PLT entry, all
   10052 	 branches in this object should go to it, except if the PLT is too
   10053 	 far away, in which case a long branch stub should be inserted.  */
   10054       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
   10055 	   && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
   10056 	   && r_type != R_ARM_CALL
   10057 	   && r_type != R_ARM_JUMP24
   10058 	   && r_type != R_ARM_PLT32)
   10059 	  && plt_offset != (bfd_vma) -1)
   10060 	{
   10061 	  /* If we've created a .plt section, and assigned a PLT entry
   10062 	     to this function, it must either be a STT_GNU_IFUNC reference
   10063 	     or not be known to bind locally.  In other cases, we should
   10064 	     have cleared the PLT entry by now.  */
   10065 	  BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
   10066 
   10067 	  value = (splt->output_section->vma
   10068 		   + splt->output_offset
   10069 		   + plt_offset);
   10070 	  *unresolved_reloc_p = FALSE;
   10071 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   10072 					   contents, rel->r_offset, value,
   10073 					   rel->r_addend);
   10074 	}
   10075 
   10076       /* When generating a shared object or relocatable executable, these
   10077 	 relocations are copied into the output file to be resolved at
   10078 	 run time.  */
   10079       if ((bfd_link_pic (info)
   10080 	   || globals->root.is_relocatable_executable)
   10081 	  && (input_section->flags & SEC_ALLOC)
   10082 	  && !(globals->vxworks_p
   10083 	       && strcmp (input_section->output_section->name,
   10084 			  ".tls_vars") == 0)
   10085 	  && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
   10086 	      || !SYMBOL_CALLS_LOCAL (info, h))
   10087 	  && !(input_bfd == globals->stub_bfd
   10088 	       && strstr (input_section->name, STUB_SUFFIX))
   10089 	  && (h == NULL
   10090 	      || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   10091 		  && !resolved_to_zero)
   10092 	      || h->root.type != bfd_link_hash_undefweak)
   10093 	  && r_type != R_ARM_PC24
   10094 	  && r_type != R_ARM_CALL
   10095 	  && r_type != R_ARM_JUMP24
   10096 	  && r_type != R_ARM_PREL31
   10097 	  && r_type != R_ARM_PLT32)
   10098 	{
   10099 	  Elf_Internal_Rela outrel;
   10100 	  bfd_boolean skip, relocate;
   10101 
   10102 	  if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
   10103 	      && !h->def_regular)
   10104 	    {
   10105 	      char *v = _("shared object");
   10106 
   10107 	      if (bfd_link_executable (info))
   10108 		v = _("PIE executable");
   10109 
   10110 	      _bfd_error_handler
   10111 		(_("%B: relocation %s against external or undefined symbol `%s'"
   10112 		   " can not be used when making a %s; recompile with -fPIC"), input_bfd,
   10113 		 elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
   10114 	      return bfd_reloc_notsupported;
   10115 	    }
   10116 
   10117 	  *unresolved_reloc_p = FALSE;
   10118 
   10119 	  if (sreloc == NULL && globals->root.dynamic_sections_created)
   10120 	    {
   10121 	      sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
   10122 							   ! globals->use_rel);
   10123 
   10124 	      if (sreloc == NULL)
   10125 		return bfd_reloc_notsupported;
   10126 	    }
   10127 
   10128 	  skip = FALSE;
   10129 	  relocate = FALSE;
   10130 
   10131 	  outrel.r_addend = addend;
   10132 	  outrel.r_offset =
   10133 	    _bfd_elf_section_offset (output_bfd, info, input_section,
   10134 				     rel->r_offset);
   10135 	  if (outrel.r_offset == (bfd_vma) -1)
   10136 	    skip = TRUE;
   10137 	  else if (outrel.r_offset == (bfd_vma) -2)
   10138 	    skip = TRUE, relocate = TRUE;
   10139 	  outrel.r_offset += (input_section->output_section->vma
   10140 			      + input_section->output_offset);
   10141 
   10142 	  if (skip)
   10143 	    memset (&outrel, 0, sizeof outrel);
   10144 	  else if (h != NULL
   10145 		   && h->dynindx != -1
   10146 		   && (!bfd_link_pic (info)
   10147 		       || !(bfd_link_pie (info)
   10148 			    || SYMBOLIC_BIND (info, h))
   10149 		       || !h->def_regular))
   10150 	    outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   10151 	  else
   10152 	    {
   10153 	      int symbol;
   10154 
   10155 	      /* This symbol is local, or marked to become local.  */
   10156 	      BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI);
   10157 	      if (globals->symbian_p)
   10158 		{
   10159 		  asection *osec;
   10160 
   10161 		  /* On Symbian OS, the data segment and text segement
   10162 		     can be relocated independently.  Therefore, we
   10163 		     must indicate the segment to which this
   10164 		     relocation is relative.  The BPABI allows us to
   10165 		     use any symbol in the right segment; we just use
   10166 		     the section symbol as it is convenient.  (We
   10167 		     cannot use the symbol given by "h" directly as it
   10168 		     will not appear in the dynamic symbol table.)
   10169 
   10170 		     Note that the dynamic linker ignores the section
   10171 		     symbol value, so we don't subtract osec->vma
   10172 		     from the emitted reloc addend.  */
   10173 		  if (sym_sec)
   10174 		    osec = sym_sec->output_section;
   10175 		  else
   10176 		    osec = input_section->output_section;
   10177 		  symbol = elf_section_data (osec)->dynindx;
   10178 		  if (symbol == 0)
   10179 		    {
   10180 		      struct elf_link_hash_table *htab = elf_hash_table (info);
   10181 
   10182 		      if ((osec->flags & SEC_READONLY) == 0
   10183 			  && htab->data_index_section != NULL)
   10184 			osec = htab->data_index_section;
   10185 		      else
   10186 			osec = htab->text_index_section;
   10187 		      symbol = elf_section_data (osec)->dynindx;
   10188 		    }
   10189 		  BFD_ASSERT (symbol != 0);
   10190 		}
   10191 	      else
   10192 		/* On SVR4-ish systems, the dynamic loader cannot
   10193 		   relocate the text and data segments independently,
   10194 		   so the symbol does not matter.  */
   10195 		symbol = 0;
   10196 	      if (dynreloc_st_type == STT_GNU_IFUNC)
   10197 		/* We have an STT_GNU_IFUNC symbol that doesn't resolve
   10198 		   to the .iplt entry.  Instead, every non-call reference
   10199 		   must use an R_ARM_IRELATIVE relocation to obtain the
   10200 		   correct run-time address.  */
   10201 		outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
   10202 	      else
   10203 		outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
   10204 	      if (globals->use_rel)
   10205 		relocate = TRUE;
   10206 	      else
   10207 		outrel.r_addend += dynreloc_value;
   10208 	    }
   10209 
   10210 	  elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
   10211 
   10212 	  /* If this reloc is against an external symbol, we do not want to
   10213 	     fiddle with the addend.  Otherwise, we need to include the symbol
   10214 	     value so that it becomes an addend for the dynamic reloc.  */
   10215 	  if (! relocate)
   10216 	    return bfd_reloc_ok;
   10217 
   10218 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   10219 					   contents, rel->r_offset,
   10220 					   dynreloc_value, (bfd_vma) 0);
   10221 	}
   10222       else switch (r_type)
   10223 	{
   10224 	case R_ARM_ABS12:
   10225 	  return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
   10226 
   10227 	case R_ARM_XPC25:	  /* Arm BLX instruction.  */
   10228 	case R_ARM_CALL:
   10229 	case R_ARM_JUMP24:
   10230 	case R_ARM_PC24:	  /* Arm B/BL instruction.  */
   10231 	case R_ARM_PLT32:
   10232 	  {
   10233 	  struct elf32_arm_stub_hash_entry *stub_entry = NULL;
   10234 
   10235 	  if (r_type == R_ARM_XPC25)
   10236 	    {
   10237 	      /* Check for Arm calling Arm function.  */
   10238 	      /* FIXME: Should we translate the instruction into a BL
   10239 		 instruction instead ?  */
   10240 	      if (branch_type != ST_BRANCH_TO_THUMB)
   10241 		_bfd_error_handler
   10242 		  (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
   10243 		   input_bfd,
   10244 		   h ? h->root.root.string : "(local)");
   10245 	    }
   10246 	  else if (r_type == R_ARM_PC24)
   10247 	    {
   10248 	      /* Check for Arm calling Thumb function.  */
   10249 	      if (branch_type == ST_BRANCH_TO_THUMB)
   10250 		{
   10251 		  if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
   10252 					       output_bfd, input_section,
   10253 					       hit_data, sym_sec, rel->r_offset,
   10254 					       signed_addend, value,
   10255 					       error_message))
   10256 		    return bfd_reloc_ok;
   10257 		  else
   10258 		    return bfd_reloc_dangerous;
   10259 		}
   10260 	    }
   10261 
   10262 	  /* Check if a stub has to be inserted because the
   10263 	     destination is too far or we are changing mode.  */
   10264 	  if (   r_type == R_ARM_CALL
   10265 	      || r_type == R_ARM_JUMP24
   10266 	      || r_type == R_ARM_PLT32)
   10267 	    {
   10268 	      enum elf32_arm_stub_type stub_type = arm_stub_none;
   10269 	      struct elf32_arm_link_hash_entry *hash;
   10270 
   10271 	      hash = (struct elf32_arm_link_hash_entry *) h;
   10272 	      stub_type = arm_type_of_stub (info, input_section, rel,
   10273 					    st_type, &branch_type,
   10274 					    hash, value, sym_sec,
   10275 					    input_bfd, sym_name);
   10276 
   10277 	      if (stub_type != arm_stub_none)
   10278 		{
   10279 		  /* The target is out of reach, so redirect the
   10280 		     branch to the local stub for this function.  */
   10281 		  stub_entry = elf32_arm_get_stub_entry (input_section,
   10282 							 sym_sec, h,
   10283 							 rel, globals,
   10284 							 stub_type);
   10285 		  {
   10286 		    if (stub_entry != NULL)
   10287 		      value = (stub_entry->stub_offset
   10288 			       + stub_entry->stub_sec->output_offset
   10289 			       + stub_entry->stub_sec->output_section->vma);
   10290 
   10291 		    if (plt_offset != (bfd_vma) -1)
   10292 		      *unresolved_reloc_p = FALSE;
   10293 		  }
   10294 		}
   10295 	      else
   10296 		{
   10297 		  /* If the call goes through a PLT entry, make sure to
   10298 		     check distance to the right destination address.  */
   10299 		  if (plt_offset != (bfd_vma) -1)
   10300 		    {
   10301 		      value = (splt->output_section->vma
   10302 			       + splt->output_offset
   10303 			       + plt_offset);
   10304 		      *unresolved_reloc_p = FALSE;
   10305 		      /* The PLT entry is in ARM mode, regardless of the
   10306 			 target function.  */
   10307 		      branch_type = ST_BRANCH_TO_ARM;
   10308 		    }
   10309 		}
   10310 	    }
   10311 
   10312 	  /* The ARM ELF ABI says that this reloc is computed as: S - P + A
   10313 	     where:
   10314 	      S is the address of the symbol in the relocation.
   10315 	      P is address of the instruction being relocated.
   10316 	      A is the addend (extracted from the instruction) in bytes.
   10317 
   10318 	     S is held in 'value'.
   10319 	     P is the base address of the section containing the
   10320 	       instruction plus the offset of the reloc into that
   10321 	       section, ie:
   10322 		 (input_section->output_section->vma +
   10323 		  input_section->output_offset +
   10324 		  rel->r_offset).
   10325 	     A is the addend, converted into bytes, ie:
   10326 		 (signed_addend * 4)
   10327 
   10328 	     Note: None of these operations have knowledge of the pipeline
   10329 	     size of the processor, thus it is up to the assembler to
   10330 	     encode this information into the addend.  */
   10331 	  value -= (input_section->output_section->vma
   10332 		    + input_section->output_offset);
   10333 	  value -= rel->r_offset;
   10334 	  if (globals->use_rel)
   10335 	    value += (signed_addend << howto->size);
   10336 	  else
   10337 	    /* RELA addends do not have to be adjusted by howto->size.  */
   10338 	    value += signed_addend;
   10339 
   10340 	  signed_addend = value;
   10341 	  signed_addend >>= howto->rightshift;
   10342 
   10343 	  /* A branch to an undefined weak symbol is turned into a jump to
   10344 	     the next instruction unless a PLT entry will be created.
   10345 	     Do the same for local undefined symbols (but not for STN_UNDEF).
   10346 	     The jump to the next instruction is optimized as a NOP depending
   10347 	     on the architecture.  */
   10348 	  if (h ? (h->root.type == bfd_link_hash_undefweak
   10349 		   && plt_offset == (bfd_vma) -1)
   10350 	      : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
   10351 	    {
   10352 	      value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
   10353 
   10354 	      if (arch_has_arm_nop (globals))
   10355 		value |= 0x0320f000;
   10356 	      else
   10357 		value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
   10358 	    }
   10359 	  else
   10360 	    {
   10361 	      /* Perform a signed range check.  */
   10362 	      if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
   10363 		  || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
   10364 		return bfd_reloc_overflow;
   10365 
   10366 	      addend = (value & 2);
   10367 
   10368 	      value = (signed_addend & howto->dst_mask)
   10369 		| (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
   10370 
   10371 	      if (r_type == R_ARM_CALL)
   10372 		{
   10373 		  /* Set the H bit in the BLX instruction.  */
   10374 		  if (branch_type == ST_BRANCH_TO_THUMB)
   10375 		    {
   10376 		      if (addend)
   10377 			value |= (1 << 24);
   10378 		      else
   10379 			value &= ~(bfd_vma)(1 << 24);
   10380 		    }
   10381 
   10382 		  /* Select the correct instruction (BL or BLX).  */
   10383 		  /* Only if we are not handling a BL to a stub. In this
   10384 		     case, mode switching is performed by the stub.  */
   10385 		  if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
   10386 		    value |= (1 << 28);
   10387 		  else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
   10388 		    {
   10389 		      value &= ~(bfd_vma)(1 << 28);
   10390 		      value |= (1 << 24);
   10391 		    }
   10392 		}
   10393 	    }
   10394 	  }
   10395 	  break;
   10396 
   10397 	case R_ARM_ABS32:
   10398 	  value += addend;
   10399 	  if (branch_type == ST_BRANCH_TO_THUMB)
   10400 	    value |= 1;
   10401 	  break;
   10402 
   10403 	case R_ARM_ABS32_NOI:
   10404 	  value += addend;
   10405 	  break;
   10406 
   10407 	case R_ARM_REL32:
   10408 	  value += addend;
   10409 	  if (branch_type == ST_BRANCH_TO_THUMB)
   10410 	    value |= 1;
   10411 	  value -= (input_section->output_section->vma
   10412 		    + input_section->output_offset + rel->r_offset);
   10413 	  break;
   10414 
   10415 	case R_ARM_REL32_NOI:
   10416 	  value += addend;
   10417 	  value -= (input_section->output_section->vma
   10418 		    + input_section->output_offset + rel->r_offset);
   10419 	  break;
   10420 
   10421 	case R_ARM_PREL31:
   10422 	  value -= (input_section->output_section->vma
   10423 		    + input_section->output_offset + rel->r_offset);
   10424 	  value += signed_addend;
   10425 	  if (! h || h->root.type != bfd_link_hash_undefweak)
   10426 	    {
   10427 	      /* Check for overflow.  */
   10428 	      if ((value ^ (value >> 1)) & (1 << 30))
   10429 		return bfd_reloc_overflow;
   10430 	    }
   10431 	  value &= 0x7fffffff;
   10432 	  value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
   10433 	  if (branch_type == ST_BRANCH_TO_THUMB)
   10434 	    value |= 1;
   10435 	  break;
   10436 	}
   10437 
   10438       bfd_put_32 (input_bfd, value, hit_data);
   10439       return bfd_reloc_ok;
   10440 
   10441     case R_ARM_ABS8:
   10442       /* PR 16202: Refectch the addend using the correct size.  */
   10443       if (globals->use_rel)
   10444 	addend = bfd_get_8 (input_bfd, hit_data);
   10445       value += addend;
   10446 
   10447       /* There is no way to tell whether the user intended to use a signed or
   10448 	 unsigned addend.  When checking for overflow we accept either,
   10449 	 as specified by the AAELF.  */
   10450       if ((long) value > 0xff || (long) value < -0x80)
   10451 	return bfd_reloc_overflow;
   10452 
   10453       bfd_put_8 (input_bfd, value, hit_data);
   10454       return bfd_reloc_ok;
   10455 
   10456     case R_ARM_ABS16:
   10457       /* PR 16202: Refectch the addend using the correct size.  */
   10458       if (globals->use_rel)
   10459 	addend = bfd_get_16 (input_bfd, hit_data);
   10460       value += addend;
   10461 
   10462       /* See comment for R_ARM_ABS8.  */
   10463       if ((long) value > 0xffff || (long) value < -0x8000)
   10464 	return bfd_reloc_overflow;
   10465 
   10466       bfd_put_16 (input_bfd, value, hit_data);
   10467       return bfd_reloc_ok;
   10468 
   10469     case R_ARM_THM_ABS5:
   10470       /* Support ldr and str instructions for the thumb.  */
   10471       if (globals->use_rel)
   10472 	{
   10473 	  /* Need to refetch addend.  */
   10474 	  addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
   10475 	  /* ??? Need to determine shift amount from operand size.  */
   10476 	  addend >>= howto->rightshift;
   10477 	}
   10478       value += addend;
   10479 
   10480       /* ??? Isn't value unsigned?  */
   10481       if ((long) value > 0x1f || (long) value < -0x10)
   10482 	return bfd_reloc_overflow;
   10483 
   10484       /* ??? Value needs to be properly shifted into place first.  */
   10485       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
   10486       bfd_put_16 (input_bfd, value, hit_data);
   10487       return bfd_reloc_ok;
   10488 
   10489     case R_ARM_THM_ALU_PREL_11_0:
   10490       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
   10491       {
   10492 	bfd_vma insn;
   10493 	bfd_signed_vma relocation;
   10494 
   10495 	insn = (bfd_get_16 (input_bfd, hit_data) << 16)
   10496 	     | bfd_get_16 (input_bfd, hit_data + 2);
   10497 
   10498 	if (globals->use_rel)
   10499 	  {
   10500 	    signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
   10501 			  | ((insn & (1 << 26)) >> 15);
   10502 	    if (insn & 0xf00000)
   10503 	      signed_addend = -signed_addend;
   10504 	  }
   10505 
   10506 	relocation = value + signed_addend;
   10507 	relocation -= Pa (input_section->output_section->vma
   10508 			  + input_section->output_offset
   10509 			  + rel->r_offset);
   10510 
   10511 	/* PR 21523: Use an absolute value.  The user of this reloc will
   10512 	   have already selected an ADD or SUB insn appropriately.  */
   10513 	value = labs (relocation);
   10514 
   10515 	if (value >= 0x1000)
   10516 	  return bfd_reloc_overflow;
   10517 
   10518 	/* Destination is Thumb.  Force bit 0 to 1 to reflect this.  */
   10519 	if (branch_type == ST_BRANCH_TO_THUMB)
   10520 	  value |= 1;
   10521 
   10522 	insn = (insn & 0xfb0f8f00) | (value & 0xff)
   10523 	     | ((value & 0x700) << 4)
   10524 	     | ((value & 0x800) << 15);
   10525 	if (relocation < 0)
   10526 	  insn |= 0xa00000;
   10527 
   10528 	bfd_put_16 (input_bfd, insn >> 16, hit_data);
   10529 	bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
   10530 
   10531 	return bfd_reloc_ok;
   10532       }
   10533 
   10534     case R_ARM_THM_PC8:
   10535       /* PR 10073:  This reloc is not generated by the GNU toolchain,
   10536 	 but it is supported for compatibility with third party libraries
   10537 	 generated by other compilers, specifically the ARM/IAR.  */
   10538       {
   10539 	bfd_vma insn;
   10540 	bfd_signed_vma relocation;
   10541 
   10542 	insn = bfd_get_16 (input_bfd, hit_data);
   10543 
   10544 	if (globals->use_rel)
   10545 	  addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
   10546 
   10547 	relocation = value + addend;
   10548 	relocation -= Pa (input_section->output_section->vma
   10549 			  + input_section->output_offset
   10550 			  + rel->r_offset);
   10551 
   10552 	value = relocation;
   10553 
   10554 	/* We do not check for overflow of this reloc.  Although strictly
   10555 	   speaking this is incorrect, it appears to be necessary in order
   10556 	   to work with IAR generated relocs.  Since GCC and GAS do not
   10557 	   generate R_ARM_THM_PC8 relocs, the lack of a check should not be
   10558 	   a problem for them.  */
   10559 	value &= 0x3fc;
   10560 
   10561 	insn = (insn & 0xff00) | (value >> 2);
   10562 
   10563 	bfd_put_16 (input_bfd, insn, hit_data);
   10564 
   10565 	return bfd_reloc_ok;
   10566       }
   10567 
   10568     case R_ARM_THM_PC12:
   10569       /* Corresponds to: ldr.w reg, [pc, #offset].  */
   10570       {
   10571 	bfd_vma insn;
   10572 	bfd_signed_vma relocation;
   10573 
   10574 	insn = (bfd_get_16 (input_bfd, hit_data) << 16)
   10575 	     | bfd_get_16 (input_bfd, hit_data + 2);
   10576 
   10577 	if (globals->use_rel)
   10578 	  {
   10579 	    signed_addend = insn & 0xfff;
   10580 	    if (!(insn & (1 << 23)))
   10581 	      signed_addend = -signed_addend;
   10582 	  }
   10583 
   10584 	relocation = value + signed_addend;
   10585 	relocation -= Pa (input_section->output_section->vma
   10586 			  + input_section->output_offset
   10587 			  + rel->r_offset);
   10588 
   10589 	value = relocation;
   10590 
   10591 	if (value >= 0x1000)
   10592 	  return bfd_reloc_overflow;
   10593 
   10594 	insn = (insn & 0xff7ff000) | value;
   10595 	if (relocation >= 0)
   10596 	  insn |= (1 << 23);
   10597 
   10598 	bfd_put_16 (input_bfd, insn >> 16, hit_data);
   10599 	bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
   10600 
   10601 	return bfd_reloc_ok;
   10602       }
   10603 
   10604     case R_ARM_THM_XPC22:
   10605     case R_ARM_THM_CALL:
   10606     case R_ARM_THM_JUMP24:
   10607       /* Thumb BL (branch long instruction).  */
   10608       {
   10609 	bfd_vma relocation;
   10610 	bfd_vma reloc_sign;
   10611 	bfd_boolean overflow = FALSE;
   10612 	bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
   10613 	bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
   10614 	bfd_signed_vma reloc_signed_max;
   10615 	bfd_signed_vma reloc_signed_min;
   10616 	bfd_vma check;
   10617 	bfd_signed_vma signed_check;
   10618 	int bitsize;
   10619 	const int thumb2 = using_thumb2 (globals);
   10620 	const int thumb2_bl = using_thumb2_bl (globals);
   10621 
   10622 	/* A branch to an undefined weak symbol is turned into a jump to
   10623 	   the next instruction unless a PLT entry will be created.
   10624 	   The jump to the next instruction is optimized as a NOP.W for
   10625 	   Thumb-2 enabled architectures.  */
   10626 	if (h && h->root.type == bfd_link_hash_undefweak
   10627 	    && plt_offset == (bfd_vma) -1)
   10628 	  {
   10629 	    if (thumb2)
   10630 	      {
   10631 		bfd_put_16 (input_bfd, 0xf3af, hit_data);
   10632 		bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
   10633 	      }
   10634 	    else
   10635 	      {
   10636 		bfd_put_16 (input_bfd, 0xe000, hit_data);
   10637 		bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
   10638 	      }
   10639 	    return bfd_reloc_ok;
   10640 	  }
   10641 
   10642 	/* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
   10643 	   with Thumb-1) involving the J1 and J2 bits.  */
   10644 	if (globals->use_rel)
   10645 	  {
   10646 	    bfd_vma s = (upper_insn & (1 << 10)) >> 10;
   10647 	    bfd_vma upper = upper_insn & 0x3ff;
   10648 	    bfd_vma lower = lower_insn & 0x7ff;
   10649 	    bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
   10650 	    bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
   10651 	    bfd_vma i1 = j1 ^ s ? 0 : 1;
   10652 	    bfd_vma i2 = j2 ^ s ? 0 : 1;
   10653 
   10654 	    addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
   10655 	    /* Sign extend.  */
   10656 	    addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
   10657 
   10658 	    signed_addend = addend;
   10659 	  }
   10660 
   10661 	if (r_type == R_ARM_THM_XPC22)
   10662 	  {
   10663 	    /* Check for Thumb to Thumb call.  */
   10664 	    /* FIXME: Should we translate the instruction into a BL
   10665 	       instruction instead ?  */
   10666 	    if (branch_type == ST_BRANCH_TO_THUMB)
   10667 	      _bfd_error_handler
   10668 		(_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
   10669 		 input_bfd,
   10670 		 h ? h->root.root.string : "(local)");
   10671 	  }
   10672 	else
   10673 	  {
   10674 	    /* If it is not a call to Thumb, assume call to Arm.
   10675 	       If it is a call relative to a section name, then it is not a
   10676 	       function call at all, but rather a long jump.  Calls through
   10677 	       the PLT do not require stubs.  */
   10678 	    if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
   10679 	      {
   10680 		if (globals->use_blx && r_type == R_ARM_THM_CALL)
   10681 		  {
   10682 		    /* Convert BL to BLX.  */
   10683 		    lower_insn = (lower_insn & ~0x1000) | 0x0800;
   10684 		  }
   10685 		else if ((   r_type != R_ARM_THM_CALL)
   10686 			 && (r_type != R_ARM_THM_JUMP24))
   10687 		  {
   10688 		    if (elf32_thumb_to_arm_stub
   10689 			(info, sym_name, input_bfd, output_bfd, input_section,
   10690 			 hit_data, sym_sec, rel->r_offset, signed_addend, value,
   10691 			 error_message))
   10692 		      return bfd_reloc_ok;
   10693 		    else
   10694 		      return bfd_reloc_dangerous;
   10695 		  }
   10696 	      }
   10697 	    else if (branch_type == ST_BRANCH_TO_THUMB
   10698 		     && globals->use_blx
   10699 		     && r_type == R_ARM_THM_CALL)
   10700 	      {
   10701 		/* Make sure this is a BL.  */
   10702 		lower_insn |= 0x1800;
   10703 	      }
   10704 	  }
   10705 
   10706 	enum elf32_arm_stub_type stub_type = arm_stub_none;
   10707 	if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
   10708 	  {
   10709 	    /* Check if a stub has to be inserted because the destination
   10710 	       is too far.  */
   10711 	    struct elf32_arm_stub_hash_entry *stub_entry;
   10712 	    struct elf32_arm_link_hash_entry *hash;
   10713 
   10714 	    hash = (struct elf32_arm_link_hash_entry *) h;
   10715 
   10716 	    stub_type = arm_type_of_stub (info, input_section, rel,
   10717 					  st_type, &branch_type,
   10718 					  hash, value, sym_sec,
   10719 					  input_bfd, sym_name);
   10720 
   10721 	    if (stub_type != arm_stub_none)
   10722 	      {
   10723 		/* The target is out of reach or we are changing modes, so
   10724 		   redirect the branch to the local stub for this
   10725 		   function.  */
   10726 		stub_entry = elf32_arm_get_stub_entry (input_section,
   10727 						       sym_sec, h,
   10728 						       rel, globals,
   10729 						       stub_type);
   10730 		if (stub_entry != NULL)
   10731 		  {
   10732 		    value = (stub_entry->stub_offset
   10733 			     + stub_entry->stub_sec->output_offset
   10734 			     + stub_entry->stub_sec->output_section->vma);
   10735 
   10736 		    if (plt_offset != (bfd_vma) -1)
   10737 		      *unresolved_reloc_p = FALSE;
   10738 		  }
   10739 
   10740 		/* If this call becomes a call to Arm, force BLX.  */
   10741 		if (globals->use_blx && (r_type == R_ARM_THM_CALL))
   10742 		  {
   10743 		    if ((stub_entry
   10744 			 && !arm_stub_is_thumb (stub_entry->stub_type))
   10745 			|| branch_type != ST_BRANCH_TO_THUMB)
   10746 		      lower_insn = (lower_insn & ~0x1000) | 0x0800;
   10747 		  }
   10748 	      }
   10749 	  }
   10750 
   10751 	/* Handle calls via the PLT.  */
   10752 	if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
   10753 	  {
   10754 	    value = (splt->output_section->vma
   10755 		     + splt->output_offset
   10756 		     + plt_offset);
   10757 
   10758 	    if (globals->use_blx
   10759 		&& r_type == R_ARM_THM_CALL
   10760 		&& ! using_thumb_only (globals))
   10761 	      {
   10762 		/* If the Thumb BLX instruction is available, convert
   10763 		   the BL to a BLX instruction to call the ARM-mode
   10764 		   PLT entry.  */
   10765 		lower_insn = (lower_insn & ~0x1000) | 0x0800;
   10766 		branch_type = ST_BRANCH_TO_ARM;
   10767 	      }
   10768 	    else
   10769 	      {
   10770 		if (! using_thumb_only (globals))
   10771 		  /* Target the Thumb stub before the ARM PLT entry.  */
   10772 		  value -= PLT_THUMB_STUB_SIZE;
   10773 		branch_type = ST_BRANCH_TO_THUMB;
   10774 	      }
   10775 	    *unresolved_reloc_p = FALSE;
   10776 	  }
   10777 
   10778 	relocation = value + signed_addend;
   10779 
   10780 	relocation -= (input_section->output_section->vma
   10781 		       + input_section->output_offset
   10782 		       + rel->r_offset);
   10783 
   10784 	check = relocation >> howto->rightshift;
   10785 
   10786 	/* If this is a signed value, the rightshift just dropped
   10787 	   leading 1 bits (assuming twos complement).  */
   10788 	if ((bfd_signed_vma) relocation >= 0)
   10789 	  signed_check = check;
   10790 	else
   10791 	  signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
   10792 
   10793 	/* Calculate the permissable maximum and minimum values for
   10794 	   this relocation according to whether we're relocating for
   10795 	   Thumb-2 or not.  */
   10796 	bitsize = howto->bitsize;
   10797 	if (!thumb2_bl)
   10798 	  bitsize -= 2;
   10799 	reloc_signed_max = (1 << (bitsize - 1)) - 1;
   10800 	reloc_signed_min = ~reloc_signed_max;
   10801 
   10802 	/* Assumes two's complement.  */
   10803 	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
   10804 	  overflow = TRUE;
   10805 
   10806 	if ((lower_insn & 0x5000) == 0x4000)
   10807 	  /* For a BLX instruction, make sure that the relocation is rounded up
   10808 	     to a word boundary.  This follows the semantics of the instruction
   10809 	     which specifies that bit 1 of the target address will come from bit
   10810 	     1 of the base address.  */
   10811 	  relocation = (relocation + 2) & ~ 3;
   10812 
   10813 	/* Put RELOCATION back into the insn.  Assumes two's complement.
   10814 	   We use the Thumb-2 encoding, which is safe even if dealing with
   10815 	   a Thumb-1 instruction by virtue of our overflow check above.  */
   10816 	reloc_sign = (signed_check < 0) ? 1 : 0;
   10817 	upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
   10818 		     | ((relocation >> 12) & 0x3ff)
   10819 		     | (reloc_sign << 10);
   10820 	lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
   10821 		     | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
   10822 		     | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
   10823 		     | ((relocation >> 1) & 0x7ff);
   10824 
   10825 	/* Put the relocated value back in the object file:  */
   10826 	bfd_put_16 (input_bfd, upper_insn, hit_data);
   10827 	bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
   10828 
   10829 	return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
   10830       }
   10831       break;
   10832 
   10833     case R_ARM_THM_JUMP19:
   10834       /* Thumb32 conditional branch instruction.  */
   10835       {
   10836 	bfd_vma relocation;
   10837 	bfd_boolean overflow = FALSE;
   10838 	bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
   10839 	bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
   10840 	bfd_signed_vma reloc_signed_max = 0xffffe;
   10841 	bfd_signed_vma reloc_signed_min = -0x100000;
   10842 	bfd_signed_vma signed_check;
   10843 	enum elf32_arm_stub_type stub_type = arm_stub_none;
   10844 	struct elf32_arm_stub_hash_entry *stub_entry;
   10845 	struct elf32_arm_link_hash_entry *hash;
   10846 
   10847 	/* Need to refetch the addend, reconstruct the top three bits,
   10848 	   and squish the two 11 bit pieces together.  */
   10849 	if (globals->use_rel)
   10850 	  {
   10851 	    bfd_vma S     = (upper_insn & 0x0400) >> 10;
   10852 	    bfd_vma upper = (upper_insn & 0x003f);
   10853 	    bfd_vma J1    = (lower_insn & 0x2000) >> 13;
   10854 	    bfd_vma J2    = (lower_insn & 0x0800) >> 11;
   10855 	    bfd_vma lower = (lower_insn & 0x07ff);
   10856 
   10857 	    upper |= J1 << 6;
   10858 	    upper |= J2 << 7;
   10859 	    upper |= (!S) << 8;
   10860 	    upper -= 0x0100; /* Sign extend.  */
   10861 
   10862 	    addend = (upper << 12) | (lower << 1);
   10863 	    signed_addend = addend;
   10864 	  }
   10865 
   10866 	/* Handle calls via the PLT.  */
   10867 	if (plt_offset != (bfd_vma) -1)
   10868 	  {
   10869 	    value = (splt->output_section->vma
   10870 		     + splt->output_offset
   10871 		     + plt_offset);
   10872 	    /* Target the Thumb stub before the ARM PLT entry.  */
   10873 	    value -= PLT_THUMB_STUB_SIZE;
   10874 	    *unresolved_reloc_p = FALSE;
   10875 	  }
   10876 
   10877 	hash = (struct elf32_arm_link_hash_entry *)h;
   10878 
   10879 	stub_type = arm_type_of_stub (info, input_section, rel,
   10880 				      st_type, &branch_type,
   10881 				      hash, value, sym_sec,
   10882 				      input_bfd, sym_name);
   10883 	if (stub_type != arm_stub_none)
   10884 	  {
   10885 	    stub_entry = elf32_arm_get_stub_entry (input_section,
   10886 						   sym_sec, h,
   10887 						   rel, globals,
   10888 						   stub_type);
   10889 	    if (stub_entry != NULL)
   10890 	      {
   10891 		value = (stub_entry->stub_offset
   10892 			+ stub_entry->stub_sec->output_offset
   10893 			+ stub_entry->stub_sec->output_section->vma);
   10894 	      }
   10895 	  }
   10896 
   10897 	relocation = value + signed_addend;
   10898 	relocation -= (input_section->output_section->vma
   10899 		       + input_section->output_offset
   10900 		       + rel->r_offset);
   10901 	signed_check = (bfd_signed_vma) relocation;
   10902 
   10903 	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
   10904 	  overflow = TRUE;
   10905 
   10906 	/* Put RELOCATION back into the insn.  */
   10907 	{
   10908 	  bfd_vma S  = (relocation & 0x00100000) >> 20;
   10909 	  bfd_vma J2 = (relocation & 0x00080000) >> 19;
   10910 	  bfd_vma J1 = (relocation & 0x00040000) >> 18;
   10911 	  bfd_vma hi = (relocation & 0x0003f000) >> 12;
   10912 	  bfd_vma lo = (relocation & 0x00000ffe) >>  1;
   10913 
   10914 	  upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
   10915 	  lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
   10916 	}
   10917 
   10918 	/* Put the relocated value back in the object file:  */
   10919 	bfd_put_16 (input_bfd, upper_insn, hit_data);
   10920 	bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
   10921 
   10922 	return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
   10923       }
   10924 
   10925     case R_ARM_THM_JUMP11:
   10926     case R_ARM_THM_JUMP8:
   10927     case R_ARM_THM_JUMP6:
   10928       /* Thumb B (branch) instruction).  */
   10929       {
   10930 	bfd_signed_vma relocation;
   10931 	bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
   10932 	bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
   10933 	bfd_signed_vma signed_check;
   10934 
   10935 	/* CZB cannot jump backward.  */
   10936 	if (r_type == R_ARM_THM_JUMP6)
   10937 	  reloc_signed_min = 0;
   10938 
   10939 	if (globals->use_rel)
   10940 	  {
   10941 	    /* Need to refetch addend.  */
   10942 	    addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
   10943 	    if (addend & ((howto->src_mask + 1) >> 1))
   10944 	      {
   10945 		signed_addend = -1;
   10946 		signed_addend &= ~ howto->src_mask;
   10947 		signed_addend |= addend;
   10948 	      }
   10949 	    else
   10950 	      signed_addend = addend;
   10951 	    /* The value in the insn has been right shifted.  We need to
   10952 	       undo this, so that we can perform the address calculation
   10953 	       in terms of bytes.  */
   10954 	    signed_addend <<= howto->rightshift;
   10955 	  }
   10956 	relocation = value + signed_addend;
   10957 
   10958 	relocation -= (input_section->output_section->vma
   10959 		       + input_section->output_offset
   10960 		       + rel->r_offset);
   10961 
   10962 	relocation >>= howto->rightshift;
   10963 	signed_check = relocation;
   10964 
   10965 	if (r_type == R_ARM_THM_JUMP6)
   10966 	  relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
   10967 	else
   10968 	  relocation &= howto->dst_mask;
   10969 	relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
   10970 
   10971 	bfd_put_16 (input_bfd, relocation, hit_data);
   10972 
   10973 	/* Assumes two's complement.  */
   10974 	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
   10975 	  return bfd_reloc_overflow;
   10976 
   10977 	return bfd_reloc_ok;
   10978       }
   10979 
   10980     case R_ARM_ALU_PCREL7_0:
   10981     case R_ARM_ALU_PCREL15_8:
   10982     case R_ARM_ALU_PCREL23_15:
   10983       {
   10984 	bfd_vma insn;
   10985 	bfd_vma relocation;
   10986 
   10987 	insn = bfd_get_32 (input_bfd, hit_data);
   10988 	if (globals->use_rel)
   10989 	  {
   10990 	    /* Extract the addend.  */
   10991 	    addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
   10992 	    signed_addend = addend;
   10993 	  }
   10994 	relocation = value + signed_addend;
   10995 
   10996 	relocation -= (input_section->output_section->vma
   10997 		       + input_section->output_offset
   10998 		       + rel->r_offset);
   10999 	insn = (insn & ~0xfff)
   11000 	       | ((howto->bitpos << 7) & 0xf00)
   11001 	       | ((relocation >> howto->bitpos) & 0xff);
   11002 	bfd_put_32 (input_bfd, value, hit_data);
   11003       }
   11004       return bfd_reloc_ok;
   11005 
   11006     case R_ARM_GNU_VTINHERIT:
   11007     case R_ARM_GNU_VTENTRY:
   11008       return bfd_reloc_ok;
   11009 
   11010     case R_ARM_GOTOFF32:
   11011       /* Relocation is relative to the start of the
   11012 	 global offset table.  */
   11013 
   11014       BFD_ASSERT (sgot != NULL);
   11015       if (sgot == NULL)
   11016 	return bfd_reloc_notsupported;
   11017 
   11018       /* If we are addressing a Thumb function, we need to adjust the
   11019 	 address by one, so that attempts to call the function pointer will
   11020 	 correctly interpret it as Thumb code.  */
   11021       if (branch_type == ST_BRANCH_TO_THUMB)
   11022 	value += 1;
   11023 
   11024       /* Note that sgot->output_offset is not involved in this
   11025 	 calculation.  We always want the start of .got.  If we
   11026 	 define _GLOBAL_OFFSET_TABLE in a different way, as is
   11027 	 permitted by the ABI, we might have to change this
   11028 	 calculation.  */
   11029       value -= sgot->output_section->vma;
   11030       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11031 				       contents, rel->r_offset, value,
   11032 				       rel->r_addend);
   11033 
   11034     case R_ARM_GOTPC:
   11035       /* Use global offset table as symbol value.  */
   11036       BFD_ASSERT (sgot != NULL);
   11037 
   11038       if (sgot == NULL)
   11039 	return bfd_reloc_notsupported;
   11040 
   11041       *unresolved_reloc_p = FALSE;
   11042       value = sgot->output_section->vma;
   11043       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11044 				       contents, rel->r_offset, value,
   11045 				       rel->r_addend);
   11046 
   11047     case R_ARM_GOT32:
   11048     case R_ARM_GOT_PREL:
   11049       /* Relocation is to the entry for this symbol in the
   11050 	 global offset table.  */
   11051       if (sgot == NULL)
   11052 	return bfd_reloc_notsupported;
   11053 
   11054       if (dynreloc_st_type == STT_GNU_IFUNC
   11055 	  && plt_offset != (bfd_vma) -1
   11056 	  && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
   11057 	{
   11058 	  /* We have a relocation against a locally-binding STT_GNU_IFUNC
   11059 	     symbol, and the relocation resolves directly to the runtime
   11060 	     target rather than to the .iplt entry.  This means that any
   11061 	     .got entry would be the same value as the .igot.plt entry,
   11062 	     so there's no point creating both.  */
   11063 	  sgot = globals->root.igotplt;
   11064 	  value = sgot->output_offset + gotplt_offset;
   11065 	}
   11066       else if (h != NULL)
   11067 	{
   11068 	  bfd_vma off;
   11069 
   11070 	  off = h->got.offset;
   11071 	  BFD_ASSERT (off != (bfd_vma) -1);
   11072 	  if ((off & 1) != 0)
   11073 	    {
   11074 	      /* We have already processsed one GOT relocation against
   11075 		 this symbol.  */
   11076 	      off &= ~1;
   11077 	      if (globals->root.dynamic_sections_created
   11078 		  && !SYMBOL_REFERENCES_LOCAL (info, h))
   11079 		*unresolved_reloc_p = FALSE;
   11080 	    }
   11081 	  else
   11082 	    {
   11083 	      Elf_Internal_Rela outrel;
   11084 
   11085 	      if (h->dynindx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
   11086 		{
   11087 		  /* If the symbol doesn't resolve locally in a static
   11088 		     object, we have an undefined reference.  If the
   11089 		     symbol doesn't resolve locally in a dynamic object,
   11090 		     it should be resolved by the dynamic linker.  */
   11091 		  if (globals->root.dynamic_sections_created)
   11092 		    {
   11093 		      outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
   11094 		      *unresolved_reloc_p = FALSE;
   11095 		    }
   11096 		  else
   11097 		    outrel.r_info = 0;
   11098 		  outrel.r_addend = 0;
   11099 		}
   11100 	      else
   11101 		{
   11102 		  if (dynreloc_st_type == STT_GNU_IFUNC)
   11103 		    outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
   11104 		  else if (bfd_link_pic (info)
   11105 			   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   11106 			       || h->root.type != bfd_link_hash_undefweak))
   11107 		    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
   11108 		  else
   11109 		    outrel.r_info = 0;
   11110 		  outrel.r_addend = dynreloc_value;
   11111 		}
   11112 
   11113 	      /* The GOT entry is initialized to zero by default.
   11114 		 See if we should install a different value.  */
   11115 	      if (outrel.r_addend != 0
   11116 		  && (outrel.r_info == 0 || globals->use_rel))
   11117 		{
   11118 		  bfd_put_32 (output_bfd, outrel.r_addend,
   11119 			      sgot->contents + off);
   11120 		  outrel.r_addend = 0;
   11121 		}
   11122 
   11123 	      if (outrel.r_info != 0)
   11124 		{
   11125 		  outrel.r_offset = (sgot->output_section->vma
   11126 				     + sgot->output_offset
   11127 				     + off);
   11128 		  elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
   11129 		}
   11130 	      h->got.offset |= 1;
   11131 	    }
   11132 	  value = sgot->output_offset + off;
   11133 	}
   11134       else
   11135 	{
   11136 	  bfd_vma off;
   11137 
   11138 	  BFD_ASSERT (local_got_offsets != NULL
   11139 		      && local_got_offsets[r_symndx] != (bfd_vma) -1);
   11140 
   11141 	  off = local_got_offsets[r_symndx];
   11142 
   11143 	  /* The offset must always be a multiple of 4.  We use the
   11144 	     least significant bit to record whether we have already
   11145 	     generated the necessary reloc.  */
   11146 	  if ((off & 1) != 0)
   11147 	    off &= ~1;
   11148 	  else
   11149 	    {
   11150 	      if (globals->use_rel)
   11151 		bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
   11152 
   11153 	      if (bfd_link_pic (info) || dynreloc_st_type == STT_GNU_IFUNC)
   11154 		{
   11155 		  Elf_Internal_Rela outrel;
   11156 
   11157 		  outrel.r_addend = addend + dynreloc_value;
   11158 		  outrel.r_offset = (sgot->output_section->vma
   11159 				     + sgot->output_offset
   11160 				     + off);
   11161 		  if (dynreloc_st_type == STT_GNU_IFUNC)
   11162 		    outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
   11163 		  else
   11164 		    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
   11165 		  elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
   11166 		}
   11167 
   11168 	      local_got_offsets[r_symndx] |= 1;
   11169 	    }
   11170 
   11171 	  value = sgot->output_offset + off;
   11172 	}
   11173       if (r_type != R_ARM_GOT32)
   11174 	value += sgot->output_section->vma;
   11175 
   11176       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11177 				       contents, rel->r_offset, value,
   11178 				       rel->r_addend);
   11179 
   11180     case R_ARM_TLS_LDO32:
   11181       value = value - dtpoff_base (info);
   11182 
   11183       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11184 				       contents, rel->r_offset, value,
   11185 				       rel->r_addend);
   11186 
   11187     case R_ARM_TLS_LDM32:
   11188       {
   11189 	bfd_vma off;
   11190 
   11191 	if (sgot == NULL)
   11192 	  abort ();
   11193 
   11194 	off = globals->tls_ldm_got.offset;
   11195 
   11196 	if ((off & 1) != 0)
   11197 	  off &= ~1;
   11198 	else
   11199 	  {
   11200 	    /* If we don't know the module number, create a relocation
   11201 	       for it.  */
   11202 	    if (bfd_link_pic (info))
   11203 	      {
   11204 		Elf_Internal_Rela outrel;
   11205 
   11206 		if (srelgot == NULL)
   11207 		  abort ();
   11208 
   11209 		outrel.r_addend = 0;
   11210 		outrel.r_offset = (sgot->output_section->vma
   11211 				   + sgot->output_offset + off);
   11212 		outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
   11213 
   11214 		if (globals->use_rel)
   11215 		  bfd_put_32 (output_bfd, outrel.r_addend,
   11216 			      sgot->contents + off);
   11217 
   11218 		elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
   11219 	      }
   11220 	    else
   11221 	      bfd_put_32 (output_bfd, 1, sgot->contents + off);
   11222 
   11223 	    globals->tls_ldm_got.offset |= 1;
   11224 	  }
   11225 
   11226 	value = sgot->output_section->vma + sgot->output_offset + off
   11227 	  - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
   11228 
   11229 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11230 					 contents, rel->r_offset, value,
   11231 					 rel->r_addend);
   11232       }
   11233 
   11234     case R_ARM_TLS_CALL:
   11235     case R_ARM_THM_TLS_CALL:
   11236     case R_ARM_TLS_GD32:
   11237     case R_ARM_TLS_IE32:
   11238     case R_ARM_TLS_GOTDESC:
   11239     case R_ARM_TLS_DESCSEQ:
   11240     case R_ARM_THM_TLS_DESCSEQ:
   11241       {
   11242 	bfd_vma off, offplt;
   11243 	int indx = 0;
   11244 	char tls_type;
   11245 
   11246 	BFD_ASSERT (sgot != NULL);
   11247 
   11248 	if (h != NULL)
   11249 	  {
   11250 	    bfd_boolean dyn;
   11251 	    dyn = globals->root.dynamic_sections_created;
   11252 	    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   11253 						 bfd_link_pic (info),
   11254 						 h)
   11255 		&& (!bfd_link_pic (info)
   11256 		    || !SYMBOL_REFERENCES_LOCAL (info, h)))
   11257 	      {
   11258 		*unresolved_reloc_p = FALSE;
   11259 		indx = h->dynindx;
   11260 	      }
   11261 	    off = h->got.offset;
   11262 	    offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
   11263 	    tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
   11264 	  }
   11265 	else
   11266 	  {
   11267 	    BFD_ASSERT (local_got_offsets != NULL);
   11268 	    off = local_got_offsets[r_symndx];
   11269 	    offplt = local_tlsdesc_gotents[r_symndx];
   11270 	    tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
   11271 	  }
   11272 
   11273 	/* Linker relaxations happens from one of the
   11274 	   R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
   11275 	if (ELF32_R_TYPE(rel->r_info) != r_type)
   11276 	  tls_type = GOT_TLS_IE;
   11277 
   11278 	BFD_ASSERT (tls_type != GOT_UNKNOWN);
   11279 
   11280 	if ((off & 1) != 0)
   11281 	  off &= ~1;
   11282 	else
   11283 	  {
   11284 	    bfd_boolean need_relocs = FALSE;
   11285 	    Elf_Internal_Rela outrel;
   11286 	    int cur_off = off;
   11287 
   11288 	    /* The GOT entries have not been initialized yet.  Do it
   11289 	       now, and emit any relocations.  If both an IE GOT and a
   11290 	       GD GOT are necessary, we emit the GD first.  */
   11291 
   11292 	    if ((bfd_link_pic (info) || indx != 0)
   11293 		&& (h == NULL
   11294 		    || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   11295 			&& !resolved_to_zero)
   11296 		    || h->root.type != bfd_link_hash_undefweak))
   11297 	      {
   11298 		need_relocs = TRUE;
   11299 		BFD_ASSERT (srelgot != NULL);
   11300 	      }
   11301 
   11302 	    if (tls_type & GOT_TLS_GDESC)
   11303 	      {
   11304 		bfd_byte *loc;
   11305 
   11306 		/* We should have relaxed, unless this is an undefined
   11307 		   weak symbol.  */
   11308 		BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
   11309 			    || bfd_link_pic (info));
   11310 		BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
   11311 			    <= globals->root.sgotplt->size);
   11312 
   11313 		outrel.r_addend = 0;
   11314 		outrel.r_offset = (globals->root.sgotplt->output_section->vma
   11315 				   + globals->root.sgotplt->output_offset
   11316 				   + offplt
   11317 				   + globals->sgotplt_jump_table_size);
   11318 
   11319 		outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
   11320 		sreloc = globals->root.srelplt;
   11321 		loc = sreloc->contents;
   11322 		loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
   11323 		BFD_ASSERT (loc + RELOC_SIZE (globals)
   11324 			   <= sreloc->contents + sreloc->size);
   11325 
   11326 		SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
   11327 
   11328 		/* For globals, the first word in the relocation gets
   11329 		   the relocation index and the top bit set, or zero,
   11330 		   if we're binding now.  For locals, it gets the
   11331 		   symbol's offset in the tls section.  */
   11332 		bfd_put_32 (output_bfd,
   11333 			    !h ? value - elf_hash_table (info)->tls_sec->vma
   11334 			    : info->flags & DF_BIND_NOW ? 0
   11335 			    : 0x80000000 | ELF32_R_SYM (outrel.r_info),
   11336 			    globals->root.sgotplt->contents + offplt
   11337 			    + globals->sgotplt_jump_table_size);
   11338 
   11339 		/* Second word in the relocation is always zero.  */
   11340 		bfd_put_32 (output_bfd, 0,
   11341 			    globals->root.sgotplt->contents + offplt
   11342 			    + globals->sgotplt_jump_table_size + 4);
   11343 	      }
   11344 	    if (tls_type & GOT_TLS_GD)
   11345 	      {
   11346 		if (need_relocs)
   11347 		  {
   11348 		    outrel.r_addend = 0;
   11349 		    outrel.r_offset = (sgot->output_section->vma
   11350 				       + sgot->output_offset
   11351 				       + cur_off);
   11352 		    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
   11353 
   11354 		    if (globals->use_rel)
   11355 		      bfd_put_32 (output_bfd, outrel.r_addend,
   11356 				  sgot->contents + cur_off);
   11357 
   11358 		    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
   11359 
   11360 		    if (indx == 0)
   11361 		      bfd_put_32 (output_bfd, value - dtpoff_base (info),
   11362 				  sgot->contents + cur_off + 4);
   11363 		    else
   11364 		      {
   11365 			outrel.r_addend = 0;
   11366 			outrel.r_info = ELF32_R_INFO (indx,
   11367 						      R_ARM_TLS_DTPOFF32);
   11368 			outrel.r_offset += 4;
   11369 
   11370 			if (globals->use_rel)
   11371 			  bfd_put_32 (output_bfd, outrel.r_addend,
   11372 				      sgot->contents + cur_off + 4);
   11373 
   11374 			elf32_arm_add_dynreloc (output_bfd, info,
   11375 						srelgot, &outrel);
   11376 		      }
   11377 		  }
   11378 		else
   11379 		  {
   11380 		    /* If we are not emitting relocations for a
   11381 		       general dynamic reference, then we must be in a
   11382 		       static link or an executable link with the
   11383 		       symbol binding locally.  Mark it as belonging
   11384 		       to module 1, the executable.  */
   11385 		    bfd_put_32 (output_bfd, 1,
   11386 				sgot->contents + cur_off);
   11387 		    bfd_put_32 (output_bfd, value - dtpoff_base (info),
   11388 				sgot->contents + cur_off + 4);
   11389 		  }
   11390 
   11391 		cur_off += 8;
   11392 	      }
   11393 
   11394 	    if (tls_type & GOT_TLS_IE)
   11395 	      {
   11396 		if (need_relocs)
   11397 		  {
   11398 		    if (indx == 0)
   11399 		      outrel.r_addend = value - dtpoff_base (info);
   11400 		    else
   11401 		      outrel.r_addend = 0;
   11402 		    outrel.r_offset = (sgot->output_section->vma
   11403 				       + sgot->output_offset
   11404 				       + cur_off);
   11405 		    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
   11406 
   11407 		    if (globals->use_rel)
   11408 		      bfd_put_32 (output_bfd, outrel.r_addend,
   11409 				  sgot->contents + cur_off);
   11410 
   11411 		    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
   11412 		  }
   11413 		else
   11414 		  bfd_put_32 (output_bfd, tpoff (info, value),
   11415 			      sgot->contents + cur_off);
   11416 		cur_off += 4;
   11417 	      }
   11418 
   11419 	    if (h != NULL)
   11420 	      h->got.offset |= 1;
   11421 	    else
   11422 	      local_got_offsets[r_symndx] |= 1;
   11423 	  }
   11424 
   11425 	if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
   11426 	  off += 8;
   11427 	else if (tls_type & GOT_TLS_GDESC)
   11428 	  off = offplt;
   11429 
   11430 	if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
   11431 	    || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
   11432 	  {
   11433 	    bfd_signed_vma offset;
   11434 	    /* TLS stubs are arm mode.  The original symbol is a
   11435 	       data object, so branch_type is bogus.  */
   11436 	    branch_type = ST_BRANCH_TO_ARM;
   11437 	    enum elf32_arm_stub_type stub_type
   11438 	      = arm_type_of_stub (info, input_section, rel,
   11439 				  st_type, &branch_type,
   11440 				  (struct elf32_arm_link_hash_entry *)h,
   11441 				  globals->tls_trampoline, globals->root.splt,
   11442 				  input_bfd, sym_name);
   11443 
   11444 	    if (stub_type != arm_stub_none)
   11445 	      {
   11446 		struct elf32_arm_stub_hash_entry *stub_entry
   11447 		  = elf32_arm_get_stub_entry
   11448 		  (input_section, globals->root.splt, 0, rel,
   11449 		   globals, stub_type);
   11450 		offset = (stub_entry->stub_offset
   11451 			  + stub_entry->stub_sec->output_offset
   11452 			  + stub_entry->stub_sec->output_section->vma);
   11453 	      }
   11454 	    else
   11455 	      offset = (globals->root.splt->output_section->vma
   11456 			+ globals->root.splt->output_offset
   11457 			+ globals->tls_trampoline);
   11458 
   11459 	    if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
   11460 	      {
   11461 		unsigned long inst;
   11462 
   11463 		offset -= (input_section->output_section->vma
   11464 			   + input_section->output_offset
   11465 			   + rel->r_offset + 8);
   11466 
   11467 		inst = offset >> 2;
   11468 		inst &= 0x00ffffff;
   11469 		value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
   11470 	      }
   11471 	    else
   11472 	      {
   11473 		/* Thumb blx encodes the offset in a complicated
   11474 		   fashion.  */
   11475 		unsigned upper_insn, lower_insn;
   11476 		unsigned neg;
   11477 
   11478 		offset -= (input_section->output_section->vma
   11479 			   + input_section->output_offset
   11480 			   + rel->r_offset + 4);
   11481 
   11482 		if (stub_type != arm_stub_none
   11483 		    && arm_stub_is_thumb (stub_type))
   11484 		  {
   11485 		    lower_insn = 0xd000;
   11486 		  }
   11487 		else
   11488 		  {
   11489 		    lower_insn = 0xc000;
   11490 		    /* Round up the offset to a word boundary.  */
   11491 		    offset = (offset + 2) & ~2;
   11492 		  }
   11493 
   11494 		neg = offset < 0;
   11495 		upper_insn = (0xf000
   11496 			      | ((offset >> 12) & 0x3ff)
   11497 			      | (neg << 10));
   11498 		lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
   11499 			      | (((!((offset >> 22) & 1)) ^ neg) << 11)
   11500 			      | ((offset >> 1) & 0x7ff);
   11501 		bfd_put_16 (input_bfd, upper_insn, hit_data);
   11502 		bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
   11503 		return bfd_reloc_ok;
   11504 	      }
   11505 	  }
   11506 	/* These relocations needs special care, as besides the fact
   11507 	   they point somewhere in .gotplt, the addend must be
   11508 	   adjusted accordingly depending on the type of instruction
   11509 	   we refer to.  */
   11510 	else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
   11511 	  {
   11512 	    unsigned long data, insn;
   11513 	    unsigned thumb;
   11514 
   11515 	    data = bfd_get_32 (input_bfd, hit_data);
   11516 	    thumb = data & 1;
   11517 	    data &= ~1u;
   11518 
   11519 	    if (thumb)
   11520 	      {
   11521 		insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
   11522 		if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
   11523 		  insn = (insn << 16)
   11524 		    | bfd_get_16 (input_bfd,
   11525 				  contents + rel->r_offset - data + 2);
   11526 		if ((insn & 0xf800c000) == 0xf000c000)
   11527 		  /* bl/blx */
   11528 		  value = -6;
   11529 		else if ((insn & 0xffffff00) == 0x4400)
   11530 		  /* add */
   11531 		  value = -5;
   11532 		else
   11533 		  {
   11534 		    _bfd_error_handler
   11535 		      /* xgettext:c-format */
   11536 		      (_("%B(%A+%#Lx): unexpected Thumb instruction '%#lx' referenced by TLS_GOTDESC"),
   11537 		       input_bfd, input_section, rel->r_offset, insn);
   11538 		    return bfd_reloc_notsupported;
   11539 		  }
   11540 	      }
   11541 	    else
   11542 	      {
   11543 		insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
   11544 
   11545 		switch (insn >> 24)
   11546 		  {
   11547 		  case 0xeb:  /* bl */
   11548 		  case 0xfa:  /* blx */
   11549 		    value = -4;
   11550 		    break;
   11551 
   11552 		  case 0xe0:	/* add */
   11553 		    value = -8;
   11554 		    break;
   11555 
   11556 		  default:
   11557 		    _bfd_error_handler
   11558 		      /* xgettext:c-format */
   11559 		      (_("%B(%A+%#Lx): unexpected ARM instruction '%#lx' referenced by TLS_GOTDESC"),
   11560 		       input_bfd, input_section, rel->r_offset, insn);
   11561 		    return bfd_reloc_notsupported;
   11562 		  }
   11563 	      }
   11564 
   11565 	    value += ((globals->root.sgotplt->output_section->vma
   11566 		       + globals->root.sgotplt->output_offset + off)
   11567 		      - (input_section->output_section->vma
   11568 			 + input_section->output_offset
   11569 			 + rel->r_offset)
   11570 		      + globals->sgotplt_jump_table_size);
   11571 	  }
   11572 	else
   11573 	  value = ((globals->root.sgot->output_section->vma
   11574 		    + globals->root.sgot->output_offset + off)
   11575 		   - (input_section->output_section->vma
   11576 		      + input_section->output_offset + rel->r_offset));
   11577 
   11578 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11579 					 contents, rel->r_offset, value,
   11580 					 rel->r_addend);
   11581       }
   11582 
   11583     case R_ARM_TLS_LE32:
   11584       if (bfd_link_dll (info))
   11585 	{
   11586 	  _bfd_error_handler
   11587 	    /* xgettext:c-format */
   11588 	    (_("%B(%A+%#Lx): %s relocation not permitted in shared object"),
   11589 	     input_bfd, input_section, rel->r_offset, howto->name);
   11590 	  return bfd_reloc_notsupported;
   11591 	}
   11592       else
   11593 	value = tpoff (info, value);
   11594 
   11595       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   11596 				       contents, rel->r_offset, value,
   11597 				       rel->r_addend);
   11598 
   11599     case R_ARM_V4BX:
   11600       if (globals->fix_v4bx)
   11601 	{
   11602 	  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   11603 
   11604 	  /* Ensure that we have a BX instruction.  */
   11605 	  BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
   11606 
   11607 	  if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
   11608 	    {
   11609 	      /* Branch to veneer.  */
   11610 	      bfd_vma glue_addr;
   11611 	      glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
   11612 	      glue_addr -= input_section->output_section->vma
   11613 			   + input_section->output_offset
   11614 			   + rel->r_offset + 8;
   11615 	      insn = (insn & 0xf0000000) | 0x0a000000
   11616 		     | ((glue_addr >> 2) & 0x00ffffff);
   11617 	    }
   11618 	  else
   11619 	    {
   11620 	      /* Preserve Rm (lowest four bits) and the condition code
   11621 		 (highest four bits). Other bits encode MOV PC,Rm.  */
   11622 	      insn = (insn & 0xf000000f) | 0x01a0f000;
   11623 	    }
   11624 
   11625 	  bfd_put_32 (input_bfd, insn, hit_data);
   11626 	}
   11627       return bfd_reloc_ok;
   11628 
   11629     case R_ARM_MOVW_ABS_NC:
   11630     case R_ARM_MOVT_ABS:
   11631     case R_ARM_MOVW_PREL_NC:
   11632     case R_ARM_MOVT_PREL:
   11633     /* Until we properly support segment-base-relative addressing then
   11634        we assume the segment base to be zero, as for the group relocations.
   11635        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
   11636        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
   11637     case R_ARM_MOVW_BREL_NC:
   11638     case R_ARM_MOVW_BREL:
   11639     case R_ARM_MOVT_BREL:
   11640       {
   11641 	bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   11642 
   11643 	if (globals->use_rel)
   11644 	  {
   11645 	    addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
   11646 	    signed_addend = (addend ^ 0x8000) - 0x8000;
   11647 	  }
   11648 
   11649 	value += signed_addend;
   11650 
   11651 	if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
   11652 	  value -= (input_section->output_section->vma
   11653 		    + input_section->output_offset + rel->r_offset);
   11654 
   11655 	if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
   11656 	  return bfd_reloc_overflow;
   11657 
   11658 	if (branch_type == ST_BRANCH_TO_THUMB)
   11659 	  value |= 1;
   11660 
   11661 	if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
   11662 	    || r_type == R_ARM_MOVT_BREL)
   11663 	  value >>= 16;
   11664 
   11665 	insn &= 0xfff0f000;
   11666 	insn |= value & 0xfff;
   11667 	insn |= (value & 0xf000) << 4;
   11668 	bfd_put_32 (input_bfd, insn, hit_data);
   11669       }
   11670       return bfd_reloc_ok;
   11671 
   11672     case R_ARM_THM_MOVW_ABS_NC:
   11673     case R_ARM_THM_MOVT_ABS:
   11674     case R_ARM_THM_MOVW_PREL_NC:
   11675     case R_ARM_THM_MOVT_PREL:
   11676     /* Until we properly support segment-base-relative addressing then
   11677        we assume the segment base to be zero, as for the above relocations.
   11678        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
   11679        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
   11680        as R_ARM_THM_MOVT_ABS.  */
   11681     case R_ARM_THM_MOVW_BREL_NC:
   11682     case R_ARM_THM_MOVW_BREL:
   11683     case R_ARM_THM_MOVT_BREL:
   11684       {
   11685 	bfd_vma insn;
   11686 
   11687 	insn = bfd_get_16 (input_bfd, hit_data) << 16;
   11688 	insn |= bfd_get_16 (input_bfd, hit_data + 2);
   11689 
   11690 	if (globals->use_rel)
   11691 	  {
   11692 	    addend = ((insn >> 4)  & 0xf000)
   11693 		   | ((insn >> 15) & 0x0800)
   11694 		   | ((insn >> 4)  & 0x0700)
   11695 		   | (insn	   & 0x00ff);
   11696 	    signed_addend = (addend ^ 0x8000) - 0x8000;
   11697 	  }
   11698 
   11699 	value += signed_addend;
   11700 
   11701 	if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
   11702 	  value -= (input_section->output_section->vma
   11703 		    + input_section->output_offset + rel->r_offset);
   11704 
   11705 	if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
   11706 	  return bfd_reloc_overflow;
   11707 
   11708 	if (branch_type == ST_BRANCH_TO_THUMB)
   11709 	  value |= 1;
   11710 
   11711 	if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
   11712 	    || r_type == R_ARM_THM_MOVT_BREL)
   11713 	  value >>= 16;
   11714 
   11715 	insn &= 0xfbf08f00;
   11716 	insn |= (value & 0xf000) << 4;
   11717 	insn |= (value & 0x0800) << 15;
   11718 	insn |= (value & 0x0700) << 4;
   11719 	insn |= (value & 0x00ff);
   11720 
   11721 	bfd_put_16 (input_bfd, insn >> 16, hit_data);
   11722 	bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
   11723       }
   11724       return bfd_reloc_ok;
   11725 
   11726     case R_ARM_ALU_PC_G0_NC:
   11727     case R_ARM_ALU_PC_G1_NC:
   11728     case R_ARM_ALU_PC_G0:
   11729     case R_ARM_ALU_PC_G1:
   11730     case R_ARM_ALU_PC_G2:
   11731     case R_ARM_ALU_SB_G0_NC:
   11732     case R_ARM_ALU_SB_G1_NC:
   11733     case R_ARM_ALU_SB_G0:
   11734     case R_ARM_ALU_SB_G1:
   11735     case R_ARM_ALU_SB_G2:
   11736       {
   11737 	bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   11738 	bfd_vma pc = input_section->output_section->vma
   11739 		     + input_section->output_offset + rel->r_offset;
   11740 	/* sb is the origin of the *segment* containing the symbol.  */
   11741 	bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
   11742 	bfd_vma residual;
   11743 	bfd_vma g_n;
   11744 	bfd_signed_vma signed_value;
   11745 	int group = 0;
   11746 
   11747 	/* Determine which group of bits to select.  */
   11748 	switch (r_type)
   11749 	  {
   11750 	  case R_ARM_ALU_PC_G0_NC:
   11751 	  case R_ARM_ALU_PC_G0:
   11752 	  case R_ARM_ALU_SB_G0_NC:
   11753 	  case R_ARM_ALU_SB_G0:
   11754 	    group = 0;
   11755 	    break;
   11756 
   11757 	  case R_ARM_ALU_PC_G1_NC:
   11758 	  case R_ARM_ALU_PC_G1:
   11759 	  case R_ARM_ALU_SB_G1_NC:
   11760 	  case R_ARM_ALU_SB_G1:
   11761 	    group = 1;
   11762 	    break;
   11763 
   11764 	  case R_ARM_ALU_PC_G2:
   11765 	  case R_ARM_ALU_SB_G2:
   11766 	    group = 2;
   11767 	    break;
   11768 
   11769 	  default:
   11770 	    abort ();
   11771 	  }
   11772 
   11773 	/* If REL, extract the addend from the insn.  If RELA, it will
   11774 	   have already been fetched for us.  */
   11775 	if (globals->use_rel)
   11776 	  {
   11777 	    int negative;
   11778 	    bfd_vma constant = insn & 0xff;
   11779 	    bfd_vma rotation = (insn & 0xf00) >> 8;
   11780 
   11781 	    if (rotation == 0)
   11782 	      signed_addend = constant;
   11783 	    else
   11784 	      {
   11785 		/* Compensate for the fact that in the instruction, the
   11786 		   rotation is stored in multiples of 2 bits.  */
   11787 		rotation *= 2;
   11788 
   11789 		/* Rotate "constant" right by "rotation" bits.  */
   11790 		signed_addend = (constant >> rotation) |
   11791 				(constant << (8 * sizeof (bfd_vma) - rotation));
   11792 	      }
   11793 
   11794 	    /* Determine if the instruction is an ADD or a SUB.
   11795 	       (For REL, this determines the sign of the addend.)  */
   11796 	    negative = identify_add_or_sub (insn);
   11797 	    if (negative == 0)
   11798 	      {
   11799 		_bfd_error_handler
   11800 		  /* xgettext:c-format */
   11801 		  (_("%B(%A+%#Lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
   11802 		  input_bfd, input_section, rel->r_offset);
   11803 		return bfd_reloc_overflow;
   11804 	      }
   11805 
   11806 	    signed_addend *= negative;
   11807 	  }
   11808 
   11809 	/* Compute the value (X) to go in the place.  */
   11810 	if (r_type == R_ARM_ALU_PC_G0_NC
   11811 	    || r_type == R_ARM_ALU_PC_G1_NC
   11812 	    || r_type == R_ARM_ALU_PC_G0
   11813 	    || r_type == R_ARM_ALU_PC_G1
   11814 	    || r_type == R_ARM_ALU_PC_G2)
   11815 	  /* PC relative.  */
   11816 	  signed_value = value - pc + signed_addend;
   11817 	else
   11818 	  /* Section base relative.  */
   11819 	  signed_value = value - sb + signed_addend;
   11820 
   11821 	/* If the target symbol is a Thumb function, then set the
   11822 	   Thumb bit in the address.  */
   11823 	if (branch_type == ST_BRANCH_TO_THUMB)
   11824 	  signed_value |= 1;
   11825 
   11826 	/* Calculate the value of the relevant G_n, in encoded
   11827 	   constant-with-rotation format.  */
   11828 	g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
   11829 					  group, &residual);
   11830 
   11831 	/* Check for overflow if required.  */
   11832 	if ((r_type == R_ARM_ALU_PC_G0
   11833 	     || r_type == R_ARM_ALU_PC_G1
   11834 	     || r_type == R_ARM_ALU_PC_G2
   11835 	     || r_type == R_ARM_ALU_SB_G0
   11836 	     || r_type == R_ARM_ALU_SB_G1
   11837 	     || r_type == R_ARM_ALU_SB_G2) && residual != 0)
   11838 	  {
   11839 	    _bfd_error_handler
   11840 	      /* xgettext:c-format */
   11841 	      (_("%B(%A+%#Lx): Overflow whilst splitting %#Lx for group relocation %s"),
   11842 	       input_bfd, input_section, rel->r_offset,
   11843 	       signed_value < 0 ? -signed_value : signed_value, howto->name);
   11844 	    return bfd_reloc_overflow;
   11845 	  }
   11846 
   11847 	/* Mask out the value and the ADD/SUB part of the opcode; take care
   11848 	   not to destroy the S bit.  */
   11849 	insn &= 0xff1ff000;
   11850 
   11851 	/* Set the opcode according to whether the value to go in the
   11852 	   place is negative.  */
   11853 	if (signed_value < 0)
   11854 	  insn |= 1 << 22;
   11855 	else
   11856 	  insn |= 1 << 23;
   11857 
   11858 	/* Encode the offset.  */
   11859 	insn |= g_n;
   11860 
   11861 	bfd_put_32 (input_bfd, insn, hit_data);
   11862       }
   11863       return bfd_reloc_ok;
   11864 
   11865     case R_ARM_LDR_PC_G0:
   11866     case R_ARM_LDR_PC_G1:
   11867     case R_ARM_LDR_PC_G2:
   11868     case R_ARM_LDR_SB_G0:
   11869     case R_ARM_LDR_SB_G1:
   11870     case R_ARM_LDR_SB_G2:
   11871       {
   11872 	bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   11873 	bfd_vma pc = input_section->output_section->vma
   11874 		     + input_section->output_offset + rel->r_offset;
   11875 	/* sb is the origin of the *segment* containing the symbol.  */
   11876 	bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
   11877 	bfd_vma residual;
   11878 	bfd_signed_vma signed_value;
   11879 	int group = 0;
   11880 
   11881 	/* Determine which groups of bits to calculate.  */
   11882 	switch (r_type)
   11883 	  {
   11884 	  case R_ARM_LDR_PC_G0:
   11885 	  case R_ARM_LDR_SB_G0:
   11886 	    group = 0;
   11887 	    break;
   11888 
   11889 	  case R_ARM_LDR_PC_G1:
   11890 	  case R_ARM_LDR_SB_G1:
   11891 	    group = 1;
   11892 	    break;
   11893 
   11894 	  case R_ARM_LDR_PC_G2:
   11895 	  case R_ARM_LDR_SB_G2:
   11896 	    group = 2;
   11897 	    break;
   11898 
   11899 	  default:
   11900 	    abort ();
   11901 	  }
   11902 
   11903 	/* If REL, extract the addend from the insn.  If RELA, it will
   11904 	   have already been fetched for us.  */
   11905 	if (globals->use_rel)
   11906 	  {
   11907 	    int negative = (insn & (1 << 23)) ? 1 : -1;
   11908 	    signed_addend = negative * (insn & 0xfff);
   11909 	  }
   11910 
   11911 	/* Compute the value (X) to go in the place.  */
   11912 	if (r_type == R_ARM_LDR_PC_G0
   11913 	    || r_type == R_ARM_LDR_PC_G1
   11914 	    || r_type == R_ARM_LDR_PC_G2)
   11915 	  /* PC relative.  */
   11916 	  signed_value = value - pc + signed_addend;
   11917 	else
   11918 	  /* Section base relative.  */
   11919 	  signed_value = value - sb + signed_addend;
   11920 
   11921 	/* Calculate the value of the relevant G_{n-1} to obtain
   11922 	   the residual at that stage.  */
   11923 	calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
   11924 				    group - 1, &residual);
   11925 
   11926 	/* Check for overflow.  */
   11927 	if (residual >= 0x1000)
   11928 	  {
   11929 	    _bfd_error_handler
   11930 	      /* xgettext:c-format */
   11931 	      (_("%B(%A+%#Lx): Overflow whilst splitting %#Lx for group relocation %s"),
   11932 	       input_bfd, input_section, rel->r_offset,
   11933 	       signed_value < 0 ? -signed_value : signed_value, howto->name);
   11934 	    return bfd_reloc_overflow;
   11935 	  }
   11936 
   11937 	/* Mask out the value and U bit.  */
   11938 	insn &= 0xff7ff000;
   11939 
   11940 	/* Set the U bit if the value to go in the place is non-negative.  */
   11941 	if (signed_value >= 0)
   11942 	  insn |= 1 << 23;
   11943 
   11944 	/* Encode the offset.  */
   11945 	insn |= residual;
   11946 
   11947 	bfd_put_32 (input_bfd, insn, hit_data);
   11948       }
   11949       return bfd_reloc_ok;
   11950 
   11951     case R_ARM_LDRS_PC_G0:
   11952     case R_ARM_LDRS_PC_G1:
   11953     case R_ARM_LDRS_PC_G2:
   11954     case R_ARM_LDRS_SB_G0:
   11955     case R_ARM_LDRS_SB_G1:
   11956     case R_ARM_LDRS_SB_G2:
   11957       {
   11958 	bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   11959 	bfd_vma pc = input_section->output_section->vma
   11960 		     + input_section->output_offset + rel->r_offset;
   11961 	/* sb is the origin of the *segment* containing the symbol.  */
   11962 	bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
   11963 	bfd_vma residual;
   11964 	bfd_signed_vma signed_value;
   11965 	int group = 0;
   11966 
   11967 	/* Determine which groups of bits to calculate.  */
   11968 	switch (r_type)
   11969 	  {
   11970 	  case R_ARM_LDRS_PC_G0:
   11971 	  case R_ARM_LDRS_SB_G0:
   11972 	    group = 0;
   11973 	    break;
   11974 
   11975 	  case R_ARM_LDRS_PC_G1:
   11976 	  case R_ARM_LDRS_SB_G1:
   11977 	    group = 1;
   11978 	    break;
   11979 
   11980 	  case R_ARM_LDRS_PC_G2:
   11981 	  case R_ARM_LDRS_SB_G2:
   11982 	    group = 2;
   11983 	    break;
   11984 
   11985 	  default:
   11986 	    abort ();
   11987 	  }
   11988 
   11989 	/* If REL, extract the addend from the insn.  If RELA, it will
   11990 	   have already been fetched for us.  */
   11991 	if (globals->use_rel)
   11992 	  {
   11993 	    int negative = (insn & (1 << 23)) ? 1 : -1;
   11994 	    signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
   11995 	  }
   11996 
   11997 	/* Compute the value (X) to go in the place.  */
   11998 	if (r_type == R_ARM_LDRS_PC_G0
   11999 	    || r_type == R_ARM_LDRS_PC_G1
   12000 	    || r_type == R_ARM_LDRS_PC_G2)
   12001 	  /* PC relative.  */
   12002 	  signed_value = value - pc + signed_addend;
   12003 	else
   12004 	  /* Section base relative.  */
   12005 	  signed_value = value - sb + signed_addend;
   12006 
   12007 	/* Calculate the value of the relevant G_{n-1} to obtain
   12008 	   the residual at that stage.  */
   12009 	calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
   12010 				    group - 1, &residual);
   12011 
   12012 	/* Check for overflow.  */
   12013 	if (residual >= 0x100)
   12014 	  {
   12015 	    _bfd_error_handler
   12016 	      /* xgettext:c-format */
   12017 	      (_("%B(%A+%#Lx): Overflow whilst splitting %#Lx for group relocation %s"),
   12018 	       input_bfd, input_section, rel->r_offset,
   12019 	       signed_value < 0 ? -signed_value : signed_value, howto->name);
   12020 	    return bfd_reloc_overflow;
   12021 	  }
   12022 
   12023 	/* Mask out the value and U bit.  */
   12024 	insn &= 0xff7ff0f0;
   12025 
   12026 	/* Set the U bit if the value to go in the place is non-negative.  */
   12027 	if (signed_value >= 0)
   12028 	  insn |= 1 << 23;
   12029 
   12030 	/* Encode the offset.  */
   12031 	insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
   12032 
   12033 	bfd_put_32 (input_bfd, insn, hit_data);
   12034       }
   12035       return bfd_reloc_ok;
   12036 
   12037     case R_ARM_LDC_PC_G0:
   12038     case R_ARM_LDC_PC_G1:
   12039     case R_ARM_LDC_PC_G2:
   12040     case R_ARM_LDC_SB_G0:
   12041     case R_ARM_LDC_SB_G1:
   12042     case R_ARM_LDC_SB_G2:
   12043       {
   12044 	bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
   12045 	bfd_vma pc = input_section->output_section->vma
   12046 		     + input_section->output_offset + rel->r_offset;
   12047 	/* sb is the origin of the *segment* containing the symbol.  */
   12048 	bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
   12049 	bfd_vma residual;
   12050 	bfd_signed_vma signed_value;
   12051 	int group = 0;
   12052 
   12053 	/* Determine which groups of bits to calculate.  */
   12054 	switch (r_type)
   12055 	  {
   12056 	  case R_ARM_LDC_PC_G0:
   12057 	  case R_ARM_LDC_SB_G0:
   12058 	    group = 0;
   12059 	    break;
   12060 
   12061 	  case R_ARM_LDC_PC_G1:
   12062 	  case R_ARM_LDC_SB_G1:
   12063 	    group = 1;
   12064 	    break;
   12065 
   12066 	  case R_ARM_LDC_PC_G2:
   12067 	  case R_ARM_LDC_SB_G2:
   12068 	    group = 2;
   12069 	    break;
   12070 
   12071 	  default:
   12072 	    abort ();
   12073 	  }
   12074 
   12075 	/* If REL, extract the addend from the insn.  If RELA, it will
   12076 	   have already been fetched for us.  */
   12077 	if (globals->use_rel)
   12078 	  {
   12079 	    int negative = (insn & (1 << 23)) ? 1 : -1;
   12080 	    signed_addend = negative * ((insn & 0xff) << 2);
   12081 	  }
   12082 
   12083 	/* Compute the value (X) to go in the place.  */
   12084 	if (r_type == R_ARM_LDC_PC_G0
   12085 	    || r_type == R_ARM_LDC_PC_G1
   12086 	    || r_type == R_ARM_LDC_PC_G2)
   12087 	  /* PC relative.  */
   12088 	  signed_value = value - pc + signed_addend;
   12089 	else
   12090 	  /* Section base relative.  */
   12091 	  signed_value = value - sb + signed_addend;
   12092 
   12093 	/* Calculate the value of the relevant G_{n-1} to obtain
   12094 	   the residual at that stage.  */
   12095 	calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
   12096 				    group - 1, &residual);
   12097 
   12098 	/* Check for overflow.  (The absolute value to go in the place must be
   12099 	   divisible by four and, after having been divided by four, must
   12100 	   fit in eight bits.)  */
   12101 	if ((residual & 0x3) != 0 || residual >= 0x400)
   12102 	  {
   12103 	    _bfd_error_handler
   12104 	      /* xgettext:c-format */
   12105 	      (_("%B(%A+%#Lx): Overflow whilst splitting %#Lx for group relocation %s"),
   12106 	       input_bfd, input_section, rel->r_offset,
   12107 	       signed_value < 0 ? -signed_value : signed_value, howto->name);
   12108 	    return bfd_reloc_overflow;
   12109 	  }
   12110 
   12111 	/* Mask out the value and U bit.  */
   12112 	insn &= 0xff7fff00;
   12113 
   12114 	/* Set the U bit if the value to go in the place is non-negative.  */
   12115 	if (signed_value >= 0)
   12116 	  insn |= 1 << 23;
   12117 
   12118 	/* Encode the offset.  */
   12119 	insn |= residual >> 2;
   12120 
   12121 	bfd_put_32 (input_bfd, insn, hit_data);
   12122       }
   12123       return bfd_reloc_ok;
   12124 
   12125     case R_ARM_THM_ALU_ABS_G0_NC:
   12126     case R_ARM_THM_ALU_ABS_G1_NC:
   12127     case R_ARM_THM_ALU_ABS_G2_NC:
   12128     case R_ARM_THM_ALU_ABS_G3_NC:
   12129 	{
   12130 	    const int shift_array[4] = {0, 8, 16, 24};
   12131 	    bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
   12132 	    bfd_vma addr = value;
   12133 	    int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
   12134 
   12135 	    /* Compute address.  */
   12136 	    if (globals->use_rel)
   12137 		signed_addend = insn & 0xff;
   12138 	    addr += signed_addend;
   12139 	    if (branch_type == ST_BRANCH_TO_THUMB)
   12140 		addr |= 1;
   12141 	    /* Clean imm8 insn.  */
   12142 	    insn &= 0xff00;
   12143 	    /* And update with correct part of address.  */
   12144 	    insn |= (addr >> shift) & 0xff;
   12145 	    /* Update insn.  */
   12146 	    bfd_put_16 (input_bfd, insn, hit_data);
   12147 	}
   12148 
   12149 	*unresolved_reloc_p = FALSE;
   12150 	return bfd_reloc_ok;
   12151 
   12152     default:
   12153       return bfd_reloc_notsupported;
   12154     }
   12155 }
   12156 
   12157 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
   12158 static void
   12159 arm_add_to_rel (bfd *		   abfd,
   12160 		bfd_byte *	   address,
   12161 		reloc_howto_type * howto,
   12162 		bfd_signed_vma	   increment)
   12163 {
   12164   bfd_signed_vma addend;
   12165 
   12166   if (howto->type == R_ARM_THM_CALL
   12167       || howto->type == R_ARM_THM_JUMP24)
   12168     {
   12169       int upper_insn, lower_insn;
   12170       int upper, lower;
   12171 
   12172       upper_insn = bfd_get_16 (abfd, address);
   12173       lower_insn = bfd_get_16 (abfd, address + 2);
   12174       upper = upper_insn & 0x7ff;
   12175       lower = lower_insn & 0x7ff;
   12176 
   12177       addend = (upper << 12) | (lower << 1);
   12178       addend += increment;
   12179       addend >>= 1;
   12180 
   12181       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
   12182       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
   12183 
   12184       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
   12185       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
   12186     }
   12187   else
   12188     {
   12189       bfd_vma	     contents;
   12190 
   12191       contents = bfd_get_32 (abfd, address);
   12192 
   12193       /* Get the (signed) value from the instruction.  */
   12194       addend = contents & howto->src_mask;
   12195       if (addend & ((howto->src_mask + 1) >> 1))
   12196 	{
   12197 	  bfd_signed_vma mask;
   12198 
   12199 	  mask = -1;
   12200 	  mask &= ~ howto->src_mask;
   12201 	  addend |= mask;
   12202 	}
   12203 
   12204       /* Add in the increment, (which is a byte value).  */
   12205       switch (howto->type)
   12206 	{
   12207 	default:
   12208 	  addend += increment;
   12209 	  break;
   12210 
   12211 	case R_ARM_PC24:
   12212 	case R_ARM_PLT32:
   12213 	case R_ARM_CALL:
   12214 	case R_ARM_JUMP24:
   12215 	  addend <<= howto->size;
   12216 	  addend += increment;
   12217 
   12218 	  /* Should we check for overflow here ?  */
   12219 
   12220 	  /* Drop any undesired bits.  */
   12221 	  addend >>= howto->rightshift;
   12222 	  break;
   12223 	}
   12224 
   12225       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
   12226 
   12227       bfd_put_32 (abfd, contents, address);
   12228     }
   12229 }
   12230 
   12231 #define IS_ARM_TLS_RELOC(R_TYPE)	\
   12232   ((R_TYPE) == R_ARM_TLS_GD32		\
   12233    || (R_TYPE) == R_ARM_TLS_LDO32	\
   12234    || (R_TYPE) == R_ARM_TLS_LDM32	\
   12235    || (R_TYPE) == R_ARM_TLS_DTPOFF32	\
   12236    || (R_TYPE) == R_ARM_TLS_DTPMOD32	\
   12237    || (R_TYPE) == R_ARM_TLS_TPOFF32	\
   12238    || (R_TYPE) == R_ARM_TLS_LE32	\
   12239    || (R_TYPE) == R_ARM_TLS_IE32	\
   12240    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
   12241 
   12242 /* Specific set of relocations for the gnu tls dialect.  */
   12243 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)	\
   12244   ((R_TYPE) == R_ARM_TLS_GOTDESC	\
   12245    || (R_TYPE) == R_ARM_TLS_CALL	\
   12246    || (R_TYPE) == R_ARM_THM_TLS_CALL	\
   12247    || (R_TYPE) == R_ARM_TLS_DESCSEQ	\
   12248    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
   12249 
   12250 /* Relocate an ARM ELF section.  */
   12251 
   12252 static bfd_boolean
   12253 elf32_arm_relocate_section (bfd *		   output_bfd,
   12254 			    struct bfd_link_info * info,
   12255 			    bfd *		   input_bfd,
   12256 			    asection *		   input_section,
   12257 			    bfd_byte *		   contents,
   12258 			    Elf_Internal_Rela *	   relocs,
   12259 			    Elf_Internal_Sym *	   local_syms,
   12260 			    asection **		   local_sections)
   12261 {
   12262   Elf_Internal_Shdr *symtab_hdr;
   12263   struct elf_link_hash_entry **sym_hashes;
   12264   Elf_Internal_Rela *rel;
   12265   Elf_Internal_Rela *relend;
   12266   const char *name;
   12267   struct elf32_arm_link_hash_table * globals;
   12268 
   12269   globals = elf32_arm_hash_table (info);
   12270   if (globals == NULL)
   12271     return FALSE;
   12272 
   12273   symtab_hdr = & elf_symtab_hdr (input_bfd);
   12274   sym_hashes = elf_sym_hashes (input_bfd);
   12275 
   12276   rel = relocs;
   12277   relend = relocs + input_section->reloc_count;
   12278   for (; rel < relend; rel++)
   12279     {
   12280       int			   r_type;
   12281       reloc_howto_type *	   howto;
   12282       unsigned long		   r_symndx;
   12283       Elf_Internal_Sym *	   sym;
   12284       asection *		   sec;
   12285       struct elf_link_hash_entry * h;
   12286       bfd_vma			   relocation;
   12287       bfd_reloc_status_type	   r;
   12288       arelent			   bfd_reloc;
   12289       char			   sym_type;
   12290       bfd_boolean		   unresolved_reloc = FALSE;
   12291       char *error_message = NULL;
   12292 
   12293       r_symndx = ELF32_R_SYM (rel->r_info);
   12294       r_type   = ELF32_R_TYPE (rel->r_info);
   12295       r_type   = arm_real_reloc_type (globals, r_type);
   12296 
   12297       if (   r_type == R_ARM_GNU_VTENTRY
   12298 	  || r_type == R_ARM_GNU_VTINHERIT)
   12299 	continue;
   12300 
   12301       howto = bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
   12302 
   12303       if (howto == NULL)
   12304 	return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
   12305 
   12306       h = NULL;
   12307       sym = NULL;
   12308       sec = NULL;
   12309 
   12310       if (r_symndx < symtab_hdr->sh_info)
   12311 	{
   12312 	  sym = local_syms + r_symndx;
   12313 	  sym_type = ELF32_ST_TYPE (sym->st_info);
   12314 	  sec = local_sections[r_symndx];
   12315 
   12316 	  /* An object file might have a reference to a local
   12317 	     undefined symbol.  This is a daft object file, but we
   12318 	     should at least do something about it.  V4BX & NONE
   12319 	     relocations do not use the symbol and are explicitly
   12320 	     allowed to use the undefined symbol, so allow those.
   12321 	     Likewise for relocations against STN_UNDEF.  */
   12322 	  if (r_type != R_ARM_V4BX
   12323 	      && r_type != R_ARM_NONE
   12324 	      && r_symndx != STN_UNDEF
   12325 	      && bfd_is_und_section (sec)
   12326 	      && ELF_ST_BIND (sym->st_info) != STB_WEAK)
   12327 	    (*info->callbacks->undefined_symbol)
   12328 	      (info, bfd_elf_string_from_elf_section
   12329 	       (input_bfd, symtab_hdr->sh_link, sym->st_name),
   12330 	       input_bfd, input_section,
   12331 	       rel->r_offset, TRUE);
   12332 
   12333 	  if (globals->use_rel)
   12334 	    {
   12335 	      relocation = (sec->output_section->vma
   12336 			    + sec->output_offset
   12337 			    + sym->st_value);
   12338 	      if (!bfd_link_relocatable (info)
   12339 		  && (sec->flags & SEC_MERGE)
   12340 		  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   12341 		{
   12342 		  asection *msec;
   12343 		  bfd_vma addend, value;
   12344 
   12345 		  switch (r_type)
   12346 		    {
   12347 		    case R_ARM_MOVW_ABS_NC:
   12348 		    case R_ARM_MOVT_ABS:
   12349 		      value = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12350 		      addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
   12351 		      addend = (addend ^ 0x8000) - 0x8000;
   12352 		      break;
   12353 
   12354 		    case R_ARM_THM_MOVW_ABS_NC:
   12355 		    case R_ARM_THM_MOVT_ABS:
   12356 		      value = bfd_get_16 (input_bfd, contents + rel->r_offset)
   12357 			      << 16;
   12358 		      value |= bfd_get_16 (input_bfd,
   12359 					   contents + rel->r_offset + 2);
   12360 		      addend = ((value & 0xf7000) >> 4) | (value & 0xff)
   12361 			       | ((value & 0x04000000) >> 15);
   12362 		      addend = (addend ^ 0x8000) - 0x8000;
   12363 		      break;
   12364 
   12365 		    default:
   12366 		      if (howto->rightshift
   12367 			  || (howto->src_mask & (howto->src_mask + 1)))
   12368 			{
   12369 			  _bfd_error_handler
   12370 			    /* xgettext:c-format */
   12371 			    (_("%B(%A+%#Lx): %s relocation against SEC_MERGE section"),
   12372 			     input_bfd, input_section,
   12373 			     rel->r_offset, howto->name);
   12374 			  return FALSE;
   12375 			}
   12376 
   12377 		      value = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12378 
   12379 		      /* Get the (signed) value from the instruction.  */
   12380 		      addend = value & howto->src_mask;
   12381 		      if (addend & ((howto->src_mask + 1) >> 1))
   12382 			{
   12383 			  bfd_signed_vma mask;
   12384 
   12385 			  mask = -1;
   12386 			  mask &= ~ howto->src_mask;
   12387 			  addend |= mask;
   12388 			}
   12389 		      break;
   12390 		    }
   12391 
   12392 		  msec = sec;
   12393 		  addend =
   12394 		    _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
   12395 		    - relocation;
   12396 		  addend += msec->output_section->vma + msec->output_offset;
   12397 
   12398 		  /* Cases here must match those in the preceding
   12399 		     switch statement.  */
   12400 		  switch (r_type)
   12401 		    {
   12402 		    case R_ARM_MOVW_ABS_NC:
   12403 		    case R_ARM_MOVT_ABS:
   12404 		      value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
   12405 			      | (addend & 0xfff);
   12406 		      bfd_put_32 (input_bfd, value, contents + rel->r_offset);
   12407 		      break;
   12408 
   12409 		    case R_ARM_THM_MOVW_ABS_NC:
   12410 		    case R_ARM_THM_MOVT_ABS:
   12411 		      value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
   12412 			      | (addend & 0xff) | ((addend & 0x0800) << 15);
   12413 		      bfd_put_16 (input_bfd, value >> 16,
   12414 				  contents + rel->r_offset);
   12415 		      bfd_put_16 (input_bfd, value,
   12416 				  contents + rel->r_offset + 2);
   12417 		      break;
   12418 
   12419 		    default:
   12420 		      value = (value & ~ howto->dst_mask)
   12421 			      | (addend & howto->dst_mask);
   12422 		      bfd_put_32 (input_bfd, value, contents + rel->r_offset);
   12423 		      break;
   12424 		    }
   12425 		}
   12426 	    }
   12427 	  else
   12428 	    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   12429 	}
   12430       else
   12431 	{
   12432 	  bfd_boolean warned, ignored;
   12433 
   12434 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   12435 				   r_symndx, symtab_hdr, sym_hashes,
   12436 				   h, sec, relocation,
   12437 				   unresolved_reloc, warned, ignored);
   12438 
   12439 	  sym_type = h->type;
   12440 	}
   12441 
   12442       if (sec != NULL && discarded_section (sec))
   12443 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   12444 					 rel, 1, relend, howto, 0, contents);
   12445 
   12446       if (bfd_link_relocatable (info))
   12447 	{
   12448 	  /* This is a relocatable link.  We don't have to change
   12449 	     anything, unless the reloc is against a section symbol,
   12450 	     in which case we have to adjust according to where the
   12451 	     section symbol winds up in the output section.  */
   12452 	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   12453 	    {
   12454 	      if (globals->use_rel)
   12455 		arm_add_to_rel (input_bfd, contents + rel->r_offset,
   12456 				howto, (bfd_signed_vma) sec->output_offset);
   12457 	      else
   12458 		rel->r_addend += sec->output_offset;
   12459 	    }
   12460 	  continue;
   12461 	}
   12462 
   12463       if (h != NULL)
   12464 	name = h->root.root.string;
   12465       else
   12466 	{
   12467 	  name = (bfd_elf_string_from_elf_section
   12468 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   12469 	  if (name == NULL || *name == '\0')
   12470 	    name = bfd_section_name (input_bfd, sec);
   12471 	}
   12472 
   12473       if (r_symndx != STN_UNDEF
   12474 	  && r_type != R_ARM_NONE
   12475 	  && (h == NULL
   12476 	      || h->root.type == bfd_link_hash_defined
   12477 	      || h->root.type == bfd_link_hash_defweak)
   12478 	  && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
   12479 	{
   12480 	  _bfd_error_handler
   12481 	    ((sym_type == STT_TLS
   12482 	      /* xgettext:c-format */
   12483 	      ? _("%B(%A+%#Lx): %s used with TLS symbol %s")
   12484 	      /* xgettext:c-format */
   12485 	      : _("%B(%A+%#Lx): %s used with non-TLS symbol %s")),
   12486 	     input_bfd,
   12487 	     input_section,
   12488 	     rel->r_offset,
   12489 	     howto->name,
   12490 	     name);
   12491 	}
   12492 
   12493       /* We call elf32_arm_final_link_relocate unless we're completely
   12494 	 done, i.e., the relaxation produced the final output we want,
   12495 	 and we won't let anybody mess with it. Also, we have to do
   12496 	 addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
   12497 	 both in relaxed and non-relaxed cases.  */
   12498       if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
   12499 	  || (IS_ARM_TLS_GNU_RELOC (r_type)
   12500 	      && !((h ? elf32_arm_hash_entry (h)->tls_type :
   12501 		    elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
   12502 		   & GOT_TLS_GDESC)))
   12503 	{
   12504 	  r = elf32_arm_tls_relax (globals, input_bfd, input_section,
   12505 				   contents, rel, h == NULL);
   12506 	  /* This may have been marked unresolved because it came from
   12507 	     a shared library.  But we've just dealt with that.  */
   12508 	  unresolved_reloc = 0;
   12509 	}
   12510       else
   12511 	r = bfd_reloc_continue;
   12512 
   12513       if (r == bfd_reloc_continue)
   12514 	{
   12515 	  unsigned char branch_type =
   12516 	    h ? ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
   12517 	      : ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
   12518 
   12519 	  r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
   12520 					     input_section, contents, rel,
   12521 					     relocation, info, sec, name,
   12522 					     sym_type, branch_type, h,
   12523 					     &unresolved_reloc,
   12524 					     &error_message);
   12525 	}
   12526 
   12527       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   12528 	 because such sections are not SEC_ALLOC and thus ld.so will
   12529 	 not process them.  */
   12530       if (unresolved_reloc
   12531 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   12532 	       && h->def_dynamic)
   12533 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   12534 				      rel->r_offset) != (bfd_vma) -1)
   12535 	{
   12536 	  _bfd_error_handler
   12537 	    /* xgettext:c-format */
   12538 	    (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
   12539 	     input_bfd,
   12540 	     input_section,
   12541 	     rel->r_offset,
   12542 	     howto->name,
   12543 	     h->root.root.string);
   12544 	  return FALSE;
   12545 	}
   12546 
   12547       if (r != bfd_reloc_ok)
   12548 	{
   12549 	  switch (r)
   12550 	    {
   12551 	    case bfd_reloc_overflow:
   12552 	      /* If the overflowing reloc was to an undefined symbol,
   12553 		 we have already printed one error message and there
   12554 		 is no point complaining again.  */
   12555 	      if (!h || h->root.type != bfd_link_hash_undefined)
   12556 		(*info->callbacks->reloc_overflow)
   12557 		  (info, (h ? &h->root : NULL), name, howto->name,
   12558 		   (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   12559 	      break;
   12560 
   12561 	    case bfd_reloc_undefined:
   12562 	      (*info->callbacks->undefined_symbol)
   12563 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
   12564 	      break;
   12565 
   12566 	    case bfd_reloc_outofrange:
   12567 	      error_message = _("out of range");
   12568 	      goto common_error;
   12569 
   12570 	    case bfd_reloc_notsupported:
   12571 	      error_message = _("unsupported relocation");
   12572 	      goto common_error;
   12573 
   12574 	    case bfd_reloc_dangerous:
   12575 	      /* error_message should already be set.  */
   12576 	      goto common_error;
   12577 
   12578 	    default:
   12579 	      error_message = _("unknown error");
   12580 	      /* Fall through.  */
   12581 
   12582 	    common_error:
   12583 	      BFD_ASSERT (error_message != NULL);
   12584 	      (*info->callbacks->reloc_dangerous)
   12585 		(info, error_message, input_bfd, input_section, rel->r_offset);
   12586 	      break;
   12587 	    }
   12588 	}
   12589     }
   12590 
   12591   return TRUE;
   12592 }
   12593 
   12594 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
   12595    adds the edit to the start of the list.  (The list must be built in order of
   12596    ascending TINDEX: the function's callers are primarily responsible for
   12597    maintaining that condition).  */
   12598 
   12599 static void
   12600 add_unwind_table_edit (arm_unwind_table_edit **head,
   12601 		       arm_unwind_table_edit **tail,
   12602 		       arm_unwind_edit_type type,
   12603 		       asection *linked_section,
   12604 		       unsigned int tindex)
   12605 {
   12606   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
   12607       xmalloc (sizeof (arm_unwind_table_edit));
   12608 
   12609   new_edit->type = type;
   12610   new_edit->linked_section = linked_section;
   12611   new_edit->index = tindex;
   12612 
   12613   if (tindex > 0)
   12614     {
   12615       new_edit->next = NULL;
   12616 
   12617       if (*tail)
   12618 	(*tail)->next = new_edit;
   12619 
   12620       (*tail) = new_edit;
   12621 
   12622       if (!*head)
   12623 	(*head) = new_edit;
   12624     }
   12625   else
   12626     {
   12627       new_edit->next = *head;
   12628 
   12629       if (!*tail)
   12630 	*tail = new_edit;
   12631 
   12632       *head = new_edit;
   12633     }
   12634 }
   12635 
   12636 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
   12637 
   12638 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
   12639 static void
   12640 adjust_exidx_size(asection *exidx_sec, int adjust)
   12641 {
   12642   asection *out_sec;
   12643 
   12644   if (!exidx_sec->rawsize)
   12645     exidx_sec->rawsize = exidx_sec->size;
   12646 
   12647   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
   12648   out_sec = exidx_sec->output_section;
   12649   /* Adjust size of output section.  */
   12650   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
   12651 }
   12652 
   12653 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
   12654 static void
   12655 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
   12656 {
   12657   struct _arm_elf_section_data *exidx_arm_data;
   12658 
   12659   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
   12660   add_unwind_table_edit (
   12661     &exidx_arm_data->u.exidx.unwind_edit_list,
   12662     &exidx_arm_data->u.exidx.unwind_edit_tail,
   12663     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
   12664 
   12665   exidx_arm_data->additional_reloc_count++;
   12666 
   12667   adjust_exidx_size(exidx_sec, 8);
   12668 }
   12669 
   12670 /* Scan .ARM.exidx tables, and create a list describing edits which should be
   12671    made to those tables, such that:
   12672 
   12673      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
   12674      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
   12675 	codes which have been inlined into the index).
   12676 
   12677    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
   12678 
   12679    The edits are applied when the tables are written
   12680    (in elf32_arm_write_section).  */
   12681 
   12682 bfd_boolean
   12683 elf32_arm_fix_exidx_coverage (asection **text_section_order,
   12684 			      unsigned int num_text_sections,
   12685 			      struct bfd_link_info *info,
   12686 			      bfd_boolean merge_exidx_entries)
   12687 {
   12688   bfd *inp;
   12689   unsigned int last_second_word = 0, i;
   12690   asection *last_exidx_sec = NULL;
   12691   asection *last_text_sec = NULL;
   12692   int last_unwind_type = -1;
   12693 
   12694   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
   12695      text sections.  */
   12696   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
   12697     {
   12698       asection *sec;
   12699 
   12700       for (sec = inp->sections; sec != NULL; sec = sec->next)
   12701 	{
   12702 	  struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
   12703 	  Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
   12704 
   12705 	  if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
   12706 	    continue;
   12707 
   12708 	  if (elf_sec->linked_to)
   12709 	    {
   12710 	      Elf_Internal_Shdr *linked_hdr
   12711 		= &elf_section_data (elf_sec->linked_to)->this_hdr;
   12712 	      struct _arm_elf_section_data *linked_sec_arm_data
   12713 		= get_arm_elf_section_data (linked_hdr->bfd_section);
   12714 
   12715 	      if (linked_sec_arm_data == NULL)
   12716 		continue;
   12717 
   12718 	      /* Link this .ARM.exidx section back from the text section it
   12719 		 describes.  */
   12720 	      linked_sec_arm_data->u.text.arm_exidx_sec = sec;
   12721 	    }
   12722 	}
   12723     }
   12724 
   12725   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
   12726      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
   12727      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
   12728 
   12729   for (i = 0; i < num_text_sections; i++)
   12730     {
   12731       asection *sec = text_section_order[i];
   12732       asection *exidx_sec;
   12733       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
   12734       struct _arm_elf_section_data *exidx_arm_data;
   12735       bfd_byte *contents = NULL;
   12736       int deleted_exidx_bytes = 0;
   12737       bfd_vma j;
   12738       arm_unwind_table_edit *unwind_edit_head = NULL;
   12739       arm_unwind_table_edit *unwind_edit_tail = NULL;
   12740       Elf_Internal_Shdr *hdr;
   12741       bfd *ibfd;
   12742 
   12743       if (arm_data == NULL)
   12744 	continue;
   12745 
   12746       exidx_sec = arm_data->u.text.arm_exidx_sec;
   12747       if (exidx_sec == NULL)
   12748 	{
   12749 	  /* Section has no unwind data.  */
   12750 	  if (last_unwind_type == 0 || !last_exidx_sec)
   12751 	    continue;
   12752 
   12753 	  /* Ignore zero sized sections.  */
   12754 	  if (sec->size == 0)
   12755 	    continue;
   12756 
   12757 	  insert_cantunwind_after(last_text_sec, last_exidx_sec);
   12758 	  last_unwind_type = 0;
   12759 	  continue;
   12760 	}
   12761 
   12762       /* Skip /DISCARD/ sections.  */
   12763       if (bfd_is_abs_section (exidx_sec->output_section))
   12764 	continue;
   12765 
   12766       hdr = &elf_section_data (exidx_sec)->this_hdr;
   12767       if (hdr->sh_type != SHT_ARM_EXIDX)
   12768 	continue;
   12769 
   12770       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
   12771       if (exidx_arm_data == NULL)
   12772 	continue;
   12773 
   12774       ibfd = exidx_sec->owner;
   12775 
   12776       if (hdr->contents != NULL)
   12777 	contents = hdr->contents;
   12778       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
   12779 	/* An error?  */
   12780 	continue;
   12781 
   12782       if (last_unwind_type > 0)
   12783 	{
   12784 	  unsigned int first_word = bfd_get_32 (ibfd, contents);
   12785 	  /* Add cantunwind if first unwind item does not match section
   12786 	     start.  */
   12787 	  if (first_word != sec->vma)
   12788 	    {
   12789 	      insert_cantunwind_after (last_text_sec, last_exidx_sec);
   12790 	      last_unwind_type = 0;
   12791 	    }
   12792 	}
   12793 
   12794       for (j = 0; j < hdr->sh_size; j += 8)
   12795 	{
   12796 	  unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
   12797 	  int unwind_type;
   12798 	  int elide = 0;
   12799 
   12800 	  /* An EXIDX_CANTUNWIND entry.  */
   12801 	  if (second_word == 1)
   12802 	    {
   12803 	      if (last_unwind_type == 0)
   12804 		elide = 1;
   12805 	      unwind_type = 0;
   12806 	    }
   12807 	  /* Inlined unwinding data.  Merge if equal to previous.  */
   12808 	  else if ((second_word & 0x80000000) != 0)
   12809 	    {
   12810 	      if (merge_exidx_entries
   12811 		   && last_second_word == second_word && last_unwind_type == 1)
   12812 		elide = 1;
   12813 	      unwind_type = 1;
   12814 	      last_second_word = second_word;
   12815 	    }
   12816 	  /* Normal table entry.  In theory we could merge these too,
   12817 	     but duplicate entries are likely to be much less common.  */
   12818 	  else
   12819 	    unwind_type = 2;
   12820 
   12821 	  if (elide && !bfd_link_relocatable (info))
   12822 	    {
   12823 	      add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
   12824 				     DELETE_EXIDX_ENTRY, NULL, j / 8);
   12825 
   12826 	      deleted_exidx_bytes += 8;
   12827 	    }
   12828 
   12829 	  last_unwind_type = unwind_type;
   12830 	}
   12831 
   12832       /* Free contents if we allocated it ourselves.  */
   12833       if (contents != hdr->contents)
   12834 	free (contents);
   12835 
   12836       /* Record edits to be applied later (in elf32_arm_write_section).  */
   12837       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
   12838       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
   12839 
   12840       if (deleted_exidx_bytes > 0)
   12841 	adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
   12842 
   12843       last_exidx_sec = exidx_sec;
   12844       last_text_sec = sec;
   12845     }
   12846 
   12847   /* Add terminating CANTUNWIND entry.  */
   12848   if (!bfd_link_relocatable (info) && last_exidx_sec
   12849       && last_unwind_type != 0)
   12850     insert_cantunwind_after(last_text_sec, last_exidx_sec);
   12851 
   12852   return TRUE;
   12853 }
   12854 
   12855 static bfd_boolean
   12856 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
   12857 			       bfd *ibfd, const char *name)
   12858 {
   12859   asection *sec, *osec;
   12860 
   12861   sec = bfd_get_linker_section (ibfd, name);
   12862   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
   12863     return TRUE;
   12864 
   12865   osec = sec->output_section;
   12866   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
   12867     return TRUE;
   12868 
   12869   if (! bfd_set_section_contents (obfd, osec, sec->contents,
   12870 				  sec->output_offset, sec->size))
   12871     return FALSE;
   12872 
   12873   return TRUE;
   12874 }
   12875 
   12876 static bfd_boolean
   12877 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
   12878 {
   12879   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
   12880   asection *sec, *osec;
   12881 
   12882   if (globals == NULL)
   12883     return FALSE;
   12884 
   12885   /* Invoke the regular ELF backend linker to do all the work.  */
   12886   if (!bfd_elf_final_link (abfd, info))
   12887     return FALSE;
   12888 
   12889   /* Process stub sections (eg BE8 encoding, ...).  */
   12890   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
   12891   unsigned int i;
   12892   for (i=0; i<htab->top_id; i++)
   12893     {
   12894       sec = htab->stub_group[i].stub_sec;
   12895       /* Only process it once, in its link_sec slot.  */
   12896       if (sec && i == htab->stub_group[i].link_sec->id)
   12897 	{
   12898 	  osec = sec->output_section;
   12899 	  elf32_arm_write_section (abfd, info, sec, sec->contents);
   12900 	  if (! bfd_set_section_contents (abfd, osec, sec->contents,
   12901 					  sec->output_offset, sec->size))
   12902 	    return FALSE;
   12903 	}
   12904     }
   12905 
   12906   /* Write out any glue sections now that we have created all the
   12907      stubs.  */
   12908   if (globals->bfd_of_glue_owner != NULL)
   12909     {
   12910       if (! elf32_arm_output_glue_section (info, abfd,
   12911 					   globals->bfd_of_glue_owner,
   12912 					   ARM2THUMB_GLUE_SECTION_NAME))
   12913 	return FALSE;
   12914 
   12915       if (! elf32_arm_output_glue_section (info, abfd,
   12916 					   globals->bfd_of_glue_owner,
   12917 					   THUMB2ARM_GLUE_SECTION_NAME))
   12918 	return FALSE;
   12919 
   12920       if (! elf32_arm_output_glue_section (info, abfd,
   12921 					   globals->bfd_of_glue_owner,
   12922 					   VFP11_ERRATUM_VENEER_SECTION_NAME))
   12923 	return FALSE;
   12924 
   12925       if (! elf32_arm_output_glue_section (info, abfd,
   12926 					   globals->bfd_of_glue_owner,
   12927 					   STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
   12928 	return FALSE;
   12929 
   12930       if (! elf32_arm_output_glue_section (info, abfd,
   12931 					   globals->bfd_of_glue_owner,
   12932 					   ARM_BX_GLUE_SECTION_NAME))
   12933 	return FALSE;
   12934     }
   12935 
   12936   return TRUE;
   12937 }
   12938 
   12939 /* Return a best guess for the machine number based on the attributes.  */
   12940 
   12941 static unsigned int
   12942 bfd_arm_get_mach_from_attributes (bfd * abfd)
   12943 {
   12944   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
   12945 
   12946   switch (arch)
   12947     {
   12948     case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
   12949     case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
   12950     case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
   12951 
   12952     case TAG_CPU_ARCH_V5TE:
   12953       {
   12954 	char * name;
   12955 
   12956 	BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
   12957 	name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
   12958 
   12959 	if (name)
   12960 	  {
   12961 	    if (strcmp (name, "IWMMXT2") == 0)
   12962 	      return bfd_mach_arm_iWMMXt2;
   12963 
   12964 	    if (strcmp (name, "IWMMXT") == 0)
   12965 	      return bfd_mach_arm_iWMMXt;
   12966 
   12967 	    if (strcmp (name, "XSCALE") == 0)
   12968 	      {
   12969 		int wmmx;
   12970 
   12971 		BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
   12972 		wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
   12973 		switch (wmmx)
   12974 		  {
   12975 		  case 1: return bfd_mach_arm_iWMMXt;
   12976 		  case 2: return bfd_mach_arm_iWMMXt2;
   12977 		  default: return bfd_mach_arm_XScale;
   12978 		  }
   12979 	      }
   12980 	  }
   12981 
   12982 	return bfd_mach_arm_5TE;
   12983       }
   12984 
   12985     default:
   12986       return bfd_mach_arm_unknown;
   12987     }
   12988 }
   12989 
   12990 /* Set the right machine number.  */
   12991 
   12992 static bfd_boolean
   12993 elf32_arm_object_p (bfd *abfd)
   12994 {
   12995   unsigned int mach;
   12996 
   12997   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
   12998 
   12999   if (mach == bfd_mach_arm_unknown)
   13000     {
   13001       if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
   13002 	mach = bfd_mach_arm_ep9312;
   13003       else
   13004 	mach = bfd_arm_get_mach_from_attributes (abfd);
   13005     }
   13006 
   13007   bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
   13008   return TRUE;
   13009 }
   13010 
   13011 /* Function to keep ARM specific flags in the ELF header.  */
   13012 
   13013 static bfd_boolean
   13014 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
   13015 {
   13016   if (elf_flags_init (abfd)
   13017       && elf_elfheader (abfd)->e_flags != flags)
   13018     {
   13019       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
   13020 	{
   13021 	  if (flags & EF_ARM_INTERWORK)
   13022 	    _bfd_error_handler
   13023 	      (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
   13024 	       abfd);
   13025 	  else
   13026 	    _bfd_error_handler
   13027 	      (_("Warning: Clearing the interworking flag of %B due to outside request"),
   13028 	       abfd);
   13029 	}
   13030     }
   13031   else
   13032     {
   13033       elf_elfheader (abfd)->e_flags = flags;
   13034       elf_flags_init (abfd) = TRUE;
   13035     }
   13036 
   13037   return TRUE;
   13038 }
   13039 
   13040 /* Copy backend specific data from one object module to another.  */
   13041 
   13042 static bfd_boolean
   13043 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   13044 {
   13045   flagword in_flags;
   13046   flagword out_flags;
   13047 
   13048   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
   13049     return TRUE;
   13050 
   13051   in_flags  = elf_elfheader (ibfd)->e_flags;
   13052   out_flags = elf_elfheader (obfd)->e_flags;
   13053 
   13054   if (elf_flags_init (obfd)
   13055       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
   13056       && in_flags != out_flags)
   13057     {
   13058       /* Cannot mix APCS26 and APCS32 code.  */
   13059       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
   13060 	return FALSE;
   13061 
   13062       /* Cannot mix float APCS and non-float APCS code.  */
   13063       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
   13064 	return FALSE;
   13065 
   13066       /* If the src and dest have different interworking flags
   13067 	 then turn off the interworking bit.  */
   13068       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
   13069 	{
   13070 	  if (out_flags & EF_ARM_INTERWORK)
   13071 	    _bfd_error_handler
   13072 	      (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
   13073 	       obfd, ibfd);
   13074 
   13075 	  in_flags &= ~EF_ARM_INTERWORK;
   13076 	}
   13077 
   13078       /* Likewise for PIC, though don't warn for this case.  */
   13079       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
   13080 	in_flags &= ~EF_ARM_PIC;
   13081     }
   13082 
   13083   elf_elfheader (obfd)->e_flags = in_flags;
   13084   elf_flags_init (obfd) = TRUE;
   13085 
   13086   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
   13087 }
   13088 
   13089 /* Values for Tag_ABI_PCS_R9_use.  */
   13090 enum
   13091 {
   13092   AEABI_R9_V6,
   13093   AEABI_R9_SB,
   13094   AEABI_R9_TLS,
   13095   AEABI_R9_unused
   13096 };
   13097 
   13098 /* Values for Tag_ABI_PCS_RW_data.  */
   13099 enum
   13100 {
   13101   AEABI_PCS_RW_data_absolute,
   13102   AEABI_PCS_RW_data_PCrel,
   13103   AEABI_PCS_RW_data_SBrel,
   13104   AEABI_PCS_RW_data_unused
   13105 };
   13106 
   13107 /* Values for Tag_ABI_enum_size.  */
   13108 enum
   13109 {
   13110   AEABI_enum_unused,
   13111   AEABI_enum_short,
   13112   AEABI_enum_wide,
   13113   AEABI_enum_forced_wide
   13114 };
   13115 
   13116 /* Determine whether an object attribute tag takes an integer, a
   13117    string or both.  */
   13118 
   13119 static int
   13120 elf32_arm_obj_attrs_arg_type (int tag)
   13121 {
   13122   if (tag == Tag_compatibility)
   13123     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
   13124   else if (tag == Tag_nodefaults)
   13125     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
   13126   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
   13127     return ATTR_TYPE_FLAG_STR_VAL;
   13128   else if (tag < 32)
   13129     return ATTR_TYPE_FLAG_INT_VAL;
   13130   else
   13131     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
   13132 }
   13133 
   13134 /* The ABI defines that Tag_conformance should be emitted first, and that
   13135    Tag_nodefaults should be second (if either is defined).  This sets those
   13136    two positions, and bumps up the position of all the remaining tags to
   13137    compensate.  */
   13138 static int
   13139 elf32_arm_obj_attrs_order (int num)
   13140 {
   13141   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
   13142     return Tag_conformance;
   13143   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
   13144     return Tag_nodefaults;
   13145   if ((num - 2) < Tag_nodefaults)
   13146     return num - 2;
   13147   if ((num - 1) < Tag_conformance)
   13148     return num - 1;
   13149   return num;
   13150 }
   13151 
   13152 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
   13153 static bfd_boolean
   13154 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
   13155 {
   13156   if ((tag & 127) < 64)
   13157     {
   13158       _bfd_error_handler
   13159 	(_("%B: Unknown mandatory EABI object attribute %d"),
   13160 	 abfd, tag);
   13161       bfd_set_error (bfd_error_bad_value);
   13162       return FALSE;
   13163     }
   13164   else
   13165     {
   13166       _bfd_error_handler
   13167 	(_("Warning: %B: Unknown EABI object attribute %d"),
   13168 	 abfd, tag);
   13169       return TRUE;
   13170     }
   13171 }
   13172 
   13173 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
   13174    Returns -1 if no architecture could be read.  */
   13175 
   13176 static int
   13177 get_secondary_compatible_arch (bfd *abfd)
   13178 {
   13179   obj_attribute *attr =
   13180     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
   13181 
   13182   /* Note: the tag and its argument below are uleb128 values, though
   13183      currently-defined values fit in one byte for each.  */
   13184   if (attr->s
   13185       && attr->s[0] == Tag_CPU_arch
   13186       && (attr->s[1] & 128) != 128
   13187       && attr->s[2] == 0)
   13188    return attr->s[1];
   13189 
   13190   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
   13191   return -1;
   13192 }
   13193 
   13194 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
   13195    The tag is removed if ARCH is -1.  */
   13196 
   13197 static void
   13198 set_secondary_compatible_arch (bfd *abfd, int arch)
   13199 {
   13200   obj_attribute *attr =
   13201     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
   13202 
   13203   if (arch == -1)
   13204     {
   13205       attr->s = NULL;
   13206       return;
   13207     }
   13208 
   13209   /* Note: the tag and its argument below are uleb128 values, though
   13210      currently-defined values fit in one byte for each.  */
   13211   if (!attr->s)
   13212     attr->s = (char *) bfd_alloc (abfd, 3);
   13213   attr->s[0] = Tag_CPU_arch;
   13214   attr->s[1] = arch;
   13215   attr->s[2] = '\0';
   13216 }
   13217 
   13218 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
   13219    into account.  */
   13220 
   13221 static int
   13222 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
   13223 		      int newtag, int secondary_compat)
   13224 {
   13225 #define T(X) TAG_CPU_ARCH_##X
   13226   int tagl, tagh, result;
   13227   const int v6t2[] =
   13228     {
   13229       T(V6T2),   /* PRE_V4.  */
   13230       T(V6T2),   /* V4.  */
   13231       T(V6T2),   /* V4T.  */
   13232       T(V6T2),   /* V5T.  */
   13233       T(V6T2),   /* V5TE.  */
   13234       T(V6T2),   /* V5TEJ.  */
   13235       T(V6T2),   /* V6.  */
   13236       T(V7),     /* V6KZ.  */
   13237       T(V6T2)    /* V6T2.  */
   13238     };
   13239   const int v6k[] =
   13240     {
   13241       T(V6K),    /* PRE_V4.  */
   13242       T(V6K),    /* V4.  */
   13243       T(V6K),    /* V4T.  */
   13244       T(V6K),    /* V5T.  */
   13245       T(V6K),    /* V5TE.  */
   13246       T(V6K),    /* V5TEJ.  */
   13247       T(V6K),    /* V6.  */
   13248       T(V6KZ),   /* V6KZ.  */
   13249       T(V7),     /* V6T2.  */
   13250       T(V6K)     /* V6K.  */
   13251     };
   13252   const int v7[] =
   13253     {
   13254       T(V7),     /* PRE_V4.  */
   13255       T(V7),     /* V4.  */
   13256       T(V7),     /* V4T.  */
   13257       T(V7),     /* V5T.  */
   13258       T(V7),     /* V5TE.  */
   13259       T(V7),     /* V5TEJ.  */
   13260       T(V7),     /* V6.  */
   13261       T(V7),     /* V6KZ.  */
   13262       T(V7),     /* V6T2.  */
   13263       T(V7),     /* V6K.  */
   13264       T(V7)      /* V7.  */
   13265     };
   13266   const int v6_m[] =
   13267     {
   13268       -1,	 /* PRE_V4.  */
   13269       -1,	 /* V4.  */
   13270       T(V6K),    /* V4T.  */
   13271       T(V6K),    /* V5T.  */
   13272       T(V6K),    /* V5TE.  */
   13273       T(V6K),    /* V5TEJ.  */
   13274       T(V6K),    /* V6.  */
   13275       T(V6KZ),   /* V6KZ.  */
   13276       T(V7),     /* V6T2.  */
   13277       T(V6K),    /* V6K.  */
   13278       T(V7),     /* V7.  */
   13279       T(V6_M)    /* V6_M.  */
   13280     };
   13281   const int v6s_m[] =
   13282     {
   13283       -1,	 /* PRE_V4.  */
   13284       -1,	 /* V4.  */
   13285       T(V6K),    /* V4T.  */
   13286       T(V6K),    /* V5T.  */
   13287       T(V6K),    /* V5TE.  */
   13288       T(V6K),    /* V5TEJ.  */
   13289       T(V6K),    /* V6.  */
   13290       T(V6KZ),   /* V6KZ.  */
   13291       T(V7),     /* V6T2.  */
   13292       T(V6K),    /* V6K.  */
   13293       T(V7),     /* V7.  */
   13294       T(V6S_M),  /* V6_M.  */
   13295       T(V6S_M)   /* V6S_M.  */
   13296     };
   13297   const int v7e_m[] =
   13298     {
   13299       -1,	 /* PRE_V4.  */
   13300       -1,	 /* V4.  */
   13301       T(V7E_M),  /* V4T.  */
   13302       T(V7E_M),  /* V5T.  */
   13303       T(V7E_M),  /* V5TE.  */
   13304       T(V7E_M),  /* V5TEJ.  */
   13305       T(V7E_M),  /* V6.  */
   13306       T(V7E_M),  /* V6KZ.  */
   13307       T(V7E_M),  /* V6T2.  */
   13308       T(V7E_M),  /* V6K.  */
   13309       T(V7E_M),  /* V7.  */
   13310       T(V7E_M),  /* V6_M.  */
   13311       T(V7E_M),  /* V6S_M.  */
   13312       T(V7E_M)   /* V7E_M.  */
   13313     };
   13314   const int v8[] =
   13315     {
   13316       T(V8),		/* PRE_V4.  */
   13317       T(V8),		/* V4.  */
   13318       T(V8),		/* V4T.  */
   13319       T(V8),		/* V5T.  */
   13320       T(V8),		/* V5TE.  */
   13321       T(V8),		/* V5TEJ.  */
   13322       T(V8),		/* V6.  */
   13323       T(V8),		/* V6KZ.  */
   13324       T(V8),		/* V6T2.  */
   13325       T(V8),		/* V6K.  */
   13326       T(V8),		/* V7.  */
   13327       T(V8),		/* V6_M.  */
   13328       T(V8),		/* V6S_M.  */
   13329       T(V8),		/* V7E_M.  */
   13330       T(V8)		/* V8.  */
   13331     };
   13332   const int v8r[] =
   13333     {
   13334       T(V8R),		/* PRE_V4.  */
   13335       T(V8R),		/* V4.  */
   13336       T(V8R),		/* V4T.  */
   13337       T(V8R),		/* V5T.  */
   13338       T(V8R),		/* V5TE.  */
   13339       T(V8R),		/* V5TEJ.  */
   13340       T(V8R),		/* V6.  */
   13341       T(V8R),		/* V6KZ.  */
   13342       T(V8R),		/* V6T2.  */
   13343       T(V8R),		/* V6K.  */
   13344       T(V8R),		/* V7.  */
   13345       T(V8R),		/* V6_M.  */
   13346       T(V8R),		/* V6S_M.  */
   13347       T(V8R),		/* V7E_M.  */
   13348       T(V8),		/* V8.  */
   13349       T(V8R),		/* V8R.  */
   13350     };
   13351   const int v8m_baseline[] =
   13352     {
   13353       -1,		/* PRE_V4.  */
   13354       -1,		/* V4.  */
   13355       -1,		/* V4T.  */
   13356       -1,		/* V5T.  */
   13357       -1,		/* V5TE.  */
   13358       -1,		/* V5TEJ.  */
   13359       -1,		/* V6.  */
   13360       -1,		/* V6KZ.  */
   13361       -1,		/* V6T2.  */
   13362       -1,		/* V6K.  */
   13363       -1,		/* V7.  */
   13364       T(V8M_BASE),	/* V6_M.  */
   13365       T(V8M_BASE),	/* V6S_M.  */
   13366       -1,		/* V7E_M.  */
   13367       -1,		/* V8.  */
   13368       -1,		/* V8R.  */
   13369       T(V8M_BASE)	/* V8-M BASELINE.  */
   13370     };
   13371   const int v8m_mainline[] =
   13372     {
   13373       -1,		/* PRE_V4.  */
   13374       -1,		/* V4.  */
   13375       -1,		/* V4T.  */
   13376       -1,		/* V5T.  */
   13377       -1,		/* V5TE.  */
   13378       -1,		/* V5TEJ.  */
   13379       -1,		/* V6.  */
   13380       -1,		/* V6KZ.  */
   13381       -1,		/* V6T2.  */
   13382       -1,		/* V6K.  */
   13383       T(V8M_MAIN),	/* V7.  */
   13384       T(V8M_MAIN),	/* V6_M.  */
   13385       T(V8M_MAIN),	/* V6S_M.  */
   13386       T(V8M_MAIN),	/* V7E_M.  */
   13387       -1,		/* V8.  */
   13388       -1,		/* V8R.  */
   13389       T(V8M_MAIN),	/* V8-M BASELINE.  */
   13390       T(V8M_MAIN)	/* V8-M MAINLINE.  */
   13391     };
   13392   const int v4t_plus_v6_m[] =
   13393     {
   13394       -1,		/* PRE_V4.  */
   13395       -1,		/* V4.  */
   13396       T(V4T),		/* V4T.  */
   13397       T(V5T),		/* V5T.  */
   13398       T(V5TE),		/* V5TE.  */
   13399       T(V5TEJ),		/* V5TEJ.  */
   13400       T(V6),		/* V6.  */
   13401       T(V6KZ),		/* V6KZ.  */
   13402       T(V6T2),		/* V6T2.  */
   13403       T(V6K),		/* V6K.  */
   13404       T(V7),		/* V7.  */
   13405       T(V6_M),		/* V6_M.  */
   13406       T(V6S_M),		/* V6S_M.  */
   13407       T(V7E_M),		/* V7E_M.  */
   13408       T(V8),		/* V8.  */
   13409       -1,		/* V8R.  */
   13410       T(V8M_BASE),	/* V8-M BASELINE.  */
   13411       T(V8M_MAIN),	/* V8-M MAINLINE.  */
   13412       T(V4T_PLUS_V6_M)	/* V4T plus V6_M.  */
   13413     };
   13414   const int *comb[] =
   13415     {
   13416       v6t2,
   13417       v6k,
   13418       v7,
   13419       v6_m,
   13420       v6s_m,
   13421       v7e_m,
   13422       v8,
   13423       v8r,
   13424       v8m_baseline,
   13425       v8m_mainline,
   13426       /* Pseudo-architecture.  */
   13427       v4t_plus_v6_m
   13428     };
   13429 
   13430   /* Check we've not got a higher architecture than we know about.  */
   13431 
   13432   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
   13433     {
   13434       _bfd_error_handler (_("error: %B: Unknown CPU architecture"), ibfd);
   13435       return -1;
   13436     }
   13437 
   13438   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
   13439 
   13440   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
   13441       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
   13442     oldtag = T(V4T_PLUS_V6_M);
   13443 
   13444   /* And override the new tag if we have a Tag_also_compatible_with on the
   13445      input.  */
   13446 
   13447   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
   13448       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
   13449     newtag = T(V4T_PLUS_V6_M);
   13450 
   13451   tagl = (oldtag < newtag) ? oldtag : newtag;
   13452   result = tagh = (oldtag > newtag) ? oldtag : newtag;
   13453 
   13454   /* Architectures before V6KZ add features monotonically.  */
   13455   if (tagh <= TAG_CPU_ARCH_V6KZ)
   13456     return result;
   13457 
   13458   result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
   13459 
   13460   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
   13461      as the canonical version.  */
   13462   if (result == T(V4T_PLUS_V6_M))
   13463     {
   13464       result = T(V4T);
   13465       *secondary_compat_out = T(V6_M);
   13466     }
   13467   else
   13468     *secondary_compat_out = -1;
   13469 
   13470   if (result == -1)
   13471     {
   13472       _bfd_error_handler (_("error: %B: Conflicting CPU architectures %d/%d"),
   13473 			  ibfd, oldtag, newtag);
   13474       return -1;
   13475     }
   13476 
   13477   return result;
   13478 #undef T
   13479 }
   13480 
   13481 /* Query attributes object to see if integer divide instructions may be
   13482    present in an object.  */
   13483 static bfd_boolean
   13484 elf32_arm_attributes_accept_div (const obj_attribute *attr)
   13485 {
   13486   int arch = attr[Tag_CPU_arch].i;
   13487   int profile = attr[Tag_CPU_arch_profile].i;
   13488 
   13489   switch (attr[Tag_DIV_use].i)
   13490     {
   13491     case 0:
   13492       /* Integer divide allowed if instruction contained in archetecture.  */
   13493       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
   13494 	return TRUE;
   13495       else if (arch >= TAG_CPU_ARCH_V7E_M)
   13496 	return TRUE;
   13497       else
   13498 	return FALSE;
   13499 
   13500     case 1:
   13501       /* Integer divide explicitly prohibited.  */
   13502       return FALSE;
   13503 
   13504     default:
   13505       /* Unrecognised case - treat as allowing divide everywhere.  */
   13506     case 2:
   13507       /* Integer divide allowed in ARM state.  */
   13508       return TRUE;
   13509     }
   13510 }
   13511 
   13512 /* Query attributes object to see if integer divide instructions are
   13513    forbidden to be in the object.  This is not the inverse of
   13514    elf32_arm_attributes_accept_div.  */
   13515 static bfd_boolean
   13516 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
   13517 {
   13518   return attr[Tag_DIV_use].i == 1;
   13519 }
   13520 
   13521 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
   13522    are conflicting attributes.  */
   13523 
   13524 static bfd_boolean
   13525 elf32_arm_merge_eabi_attributes (bfd *ibfd, struct bfd_link_info *info)
   13526 {
   13527   bfd *obfd = info->output_bfd;
   13528   obj_attribute *in_attr;
   13529   obj_attribute *out_attr;
   13530   /* Some tags have 0 = don't care, 1 = strong requirement,
   13531      2 = weak requirement.  */
   13532   static const int order_021[3] = {0, 2, 1};
   13533   int i;
   13534   bfd_boolean result = TRUE;
   13535   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
   13536 
   13537   /* Skip the linker stubs file.  This preserves previous behavior
   13538      of accepting unknown attributes in the first input file - but
   13539      is that a bug?  */
   13540   if (ibfd->flags & BFD_LINKER_CREATED)
   13541     return TRUE;
   13542 
   13543   /* Skip any input that hasn't attribute section.
   13544      This enables to link object files without attribute section with
   13545      any others.  */
   13546   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
   13547     return TRUE;
   13548 
   13549   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   13550     {
   13551       /* This is the first object.  Copy the attributes.  */
   13552       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   13553 
   13554       out_attr = elf_known_obj_attributes_proc (obfd);
   13555 
   13556       /* Use the Tag_null value to indicate the attributes have been
   13557 	 initialized.  */
   13558       out_attr[0].i = 1;
   13559 
   13560       /* We do not output objects with Tag_MPextension_use_legacy - we move
   13561 	 the attribute's value to Tag_MPextension_use.  */
   13562       if (out_attr[Tag_MPextension_use_legacy].i != 0)
   13563 	{
   13564 	  if (out_attr[Tag_MPextension_use].i != 0
   13565 	      && out_attr[Tag_MPextension_use_legacy].i
   13566 		!= out_attr[Tag_MPextension_use].i)
   13567 	    {
   13568 	      _bfd_error_handler
   13569 		(_("Error: %B has both the current and legacy "
   13570 		   "Tag_MPextension_use attributes"), ibfd);
   13571 	      result = FALSE;
   13572 	    }
   13573 
   13574 	  out_attr[Tag_MPextension_use] =
   13575 	    out_attr[Tag_MPextension_use_legacy];
   13576 	  out_attr[Tag_MPextension_use_legacy].type = 0;
   13577 	  out_attr[Tag_MPextension_use_legacy].i = 0;
   13578 	}
   13579 
   13580       return result;
   13581     }
   13582 
   13583   in_attr = elf_known_obj_attributes_proc (ibfd);
   13584   out_attr = elf_known_obj_attributes_proc (obfd);
   13585   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
   13586   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
   13587     {
   13588       /* Ignore mismatches if the object doesn't use floating point or is
   13589 	 floating point ABI independent.  */
   13590       if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
   13591 	  || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
   13592 	      && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
   13593 	out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
   13594       else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
   13595 	       && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
   13596 	{
   13597 	  _bfd_error_handler
   13598 	    (_("error: %B uses VFP register arguments, %B does not"),
   13599 	     in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
   13600 	     in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
   13601 	  result = FALSE;
   13602 	}
   13603     }
   13604 
   13605   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
   13606     {
   13607       /* Merge this attribute with existing attributes.  */
   13608       switch (i)
   13609 	{
   13610 	case Tag_CPU_raw_name:
   13611 	case Tag_CPU_name:
   13612 	  /* These are merged after Tag_CPU_arch.  */
   13613 	  break;
   13614 
   13615 	case Tag_ABI_optimization_goals:
   13616 	case Tag_ABI_FP_optimization_goals:
   13617 	  /* Use the first value seen.  */
   13618 	  break;
   13619 
   13620 	case Tag_CPU_arch:
   13621 	  {
   13622 	    int secondary_compat = -1, secondary_compat_out = -1;
   13623 	    unsigned int saved_out_attr = out_attr[i].i;
   13624 	    int arch_attr;
   13625 	    static const char *name_table[] =
   13626 	      {
   13627 		/* These aren't real CPU names, but we can't guess
   13628 		   that from the architecture version alone.  */
   13629 		"Pre v4",
   13630 		"ARM v4",
   13631 		"ARM v4T",
   13632 		"ARM v5T",
   13633 		"ARM v5TE",
   13634 		"ARM v5TEJ",
   13635 		"ARM v6",
   13636 		"ARM v6KZ",
   13637 		"ARM v6T2",
   13638 		"ARM v6K",
   13639 		"ARM v7",
   13640 		"ARM v6-M",
   13641 		"ARM v6S-M",
   13642 		"ARM v8",
   13643 		"",
   13644 		"ARM v8-M.baseline",
   13645 		"ARM v8-M.mainline",
   13646 	    };
   13647 
   13648 	    /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
   13649 	    secondary_compat = get_secondary_compatible_arch (ibfd);
   13650 	    secondary_compat_out = get_secondary_compatible_arch (obfd);
   13651 	    arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
   13652 					      &secondary_compat_out,
   13653 					      in_attr[i].i,
   13654 					      secondary_compat);
   13655 
   13656 	    /* Return with error if failed to merge.  */
   13657 	    if (arch_attr == -1)
   13658 	      return FALSE;
   13659 
   13660 	    out_attr[i].i = arch_attr;
   13661 
   13662 	    set_secondary_compatible_arch (obfd, secondary_compat_out);
   13663 
   13664 	    /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
   13665 	    if (out_attr[i].i == saved_out_attr)
   13666 	      ; /* Leave the names alone.  */
   13667 	    else if (out_attr[i].i == in_attr[i].i)
   13668 	      {
   13669 		/* The output architecture has been changed to match the
   13670 		   input architecture.  Use the input names.  */
   13671 		out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
   13672 		  ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
   13673 		  : NULL;
   13674 		out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
   13675 		  ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
   13676 		  : NULL;
   13677 	      }
   13678 	    else
   13679 	      {
   13680 		out_attr[Tag_CPU_name].s = NULL;
   13681 		out_attr[Tag_CPU_raw_name].s = NULL;
   13682 	      }
   13683 
   13684 	    /* If we still don't have a value for Tag_CPU_name,
   13685 	       make one up now.  Tag_CPU_raw_name remains blank.  */
   13686 	    if (out_attr[Tag_CPU_name].s == NULL
   13687 		&& out_attr[i].i < ARRAY_SIZE (name_table))
   13688 	      out_attr[Tag_CPU_name].s =
   13689 		_bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
   13690 	  }
   13691 	  break;
   13692 
   13693 	case Tag_ARM_ISA_use:
   13694 	case Tag_THUMB_ISA_use:
   13695 	case Tag_WMMX_arch:
   13696 	case Tag_Advanced_SIMD_arch:
   13697 	  /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
   13698 	case Tag_ABI_FP_rounding:
   13699 	case Tag_ABI_FP_exceptions:
   13700 	case Tag_ABI_FP_user_exceptions:
   13701 	case Tag_ABI_FP_number_model:
   13702 	case Tag_FP_HP_extension:
   13703 	case Tag_CPU_unaligned_access:
   13704 	case Tag_T2EE_use:
   13705 	case Tag_MPextension_use:
   13706 	  /* Use the largest value specified.  */
   13707 	  if (in_attr[i].i > out_attr[i].i)
   13708 	    out_attr[i].i = in_attr[i].i;
   13709 	  break;
   13710 
   13711 	case Tag_ABI_align_preserved:
   13712 	case Tag_ABI_PCS_RO_data:
   13713 	  /* Use the smallest value specified.  */
   13714 	  if (in_attr[i].i < out_attr[i].i)
   13715 	    out_attr[i].i = in_attr[i].i;
   13716 	  break;
   13717 
   13718 	case Tag_ABI_align_needed:
   13719 	  if ((in_attr[i].i > 0 || out_attr[i].i > 0)
   13720 	      && (in_attr[Tag_ABI_align_preserved].i == 0
   13721 		  || out_attr[Tag_ABI_align_preserved].i == 0))
   13722 	    {
   13723 	      /* This error message should be enabled once all non-conformant
   13724 		 binaries in the toolchain have had the attributes set
   13725 		 properly.
   13726 	      _bfd_error_handler
   13727 		(_("error: %B: 8-byte data alignment conflicts with %B"),
   13728 		 obfd, ibfd);
   13729 	      result = FALSE; */
   13730 	    }
   13731 	  /* Fall through.  */
   13732 	case Tag_ABI_FP_denormal:
   13733 	case Tag_ABI_PCS_GOT_use:
   13734 	  /* Use the "greatest" from the sequence 0, 2, 1, or the largest
   13735 	     value if greater than 2 (for future-proofing).  */
   13736 	  if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
   13737 	      || (in_attr[i].i <= 2 && out_attr[i].i <= 2
   13738 		  && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
   13739 	    out_attr[i].i = in_attr[i].i;
   13740 	  break;
   13741 
   13742 	case Tag_Virtualization_use:
   13743 	  /* The virtualization tag effectively stores two bits of
   13744 	     information: the intended use of TrustZone (in bit 0), and the
   13745 	     intended use of Virtualization (in bit 1).  */
   13746 	  if (out_attr[i].i == 0)
   13747 	    out_attr[i].i = in_attr[i].i;
   13748 	  else if (in_attr[i].i != 0
   13749 		   && in_attr[i].i != out_attr[i].i)
   13750 	    {
   13751 	      if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
   13752 		out_attr[i].i = 3;
   13753 	      else
   13754 		{
   13755 		  _bfd_error_handler
   13756 		    (_("error: %B: unable to merge virtualization attributes "
   13757 		       "with %B"),
   13758 		     obfd, ibfd);
   13759 		  result = FALSE;
   13760 		}
   13761 	    }
   13762 	  break;
   13763 
   13764 	case Tag_CPU_arch_profile:
   13765 	  if (out_attr[i].i != in_attr[i].i)
   13766 	    {
   13767 	      /* 0 will merge with anything.
   13768 		 'A' and 'S' merge to 'A'.
   13769 		 'R' and 'S' merge to 'R'.
   13770 		 'M' and 'A|R|S' is an error.  */
   13771 	      if (out_attr[i].i == 0
   13772 		  || (out_attr[i].i == 'S'
   13773 		      && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
   13774 		out_attr[i].i = in_attr[i].i;
   13775 	      else if (in_attr[i].i == 0
   13776 		       || (in_attr[i].i == 'S'
   13777 			   && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
   13778 		; /* Do nothing.  */
   13779 	      else
   13780 		{
   13781 		  _bfd_error_handler
   13782 		    (_("error: %B: Conflicting architecture profiles %c/%c"),
   13783 		     ibfd,
   13784 		     in_attr[i].i ? in_attr[i].i : '0',
   13785 		     out_attr[i].i ? out_attr[i].i : '0');
   13786 		  result = FALSE;
   13787 		}
   13788 	    }
   13789 	  break;
   13790 
   13791 	case Tag_DSP_extension:
   13792 	  /* No need to change output value if any of:
   13793 	     - pre (<=) ARMv5T input architecture (do not have DSP)
   13794 	     - M input profile not ARMv7E-M and do not have DSP.  */
   13795 	  if (in_attr[Tag_CPU_arch].i <= 3
   13796 	      || (in_attr[Tag_CPU_arch_profile].i == 'M'
   13797 		  && in_attr[Tag_CPU_arch].i != 13
   13798 		  && in_attr[i].i == 0))
   13799 	    ; /* Do nothing.  */
   13800 	  /* Output value should be 0 if DSP part of architecture, ie.
   13801 	     - post (>=) ARMv5te architecture output
   13802 	     - A, R or S profile output or ARMv7E-M output architecture.  */
   13803 	  else if (out_attr[Tag_CPU_arch].i >= 4
   13804 		   && (out_attr[Tag_CPU_arch_profile].i == 'A'
   13805 		       || out_attr[Tag_CPU_arch_profile].i == 'R'
   13806 		       || out_attr[Tag_CPU_arch_profile].i == 'S'
   13807 		       || out_attr[Tag_CPU_arch].i == 13))
   13808 	    out_attr[i].i = 0;
   13809 	  /* Otherwise, DSP instructions are added and not part of output
   13810 	     architecture.  */
   13811 	  else
   13812 	    out_attr[i].i = 1;
   13813 	  break;
   13814 
   13815 	case Tag_FP_arch:
   13816 	    {
   13817 	      /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
   13818 		 the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
   13819 		 when it's 0.  It might mean absence of FP hardware if
   13820 		 Tag_FP_arch is zero.  */
   13821 
   13822 #define VFP_VERSION_COUNT 9
   13823 	      static const struct
   13824 	      {
   13825 		  int ver;
   13826 		  int regs;
   13827 	      } vfp_versions[VFP_VERSION_COUNT] =
   13828 		{
   13829 		  {0, 0},
   13830 		  {1, 16},
   13831 		  {2, 16},
   13832 		  {3, 32},
   13833 		  {3, 16},
   13834 		  {4, 32},
   13835 		  {4, 16},
   13836 		  {8, 32},
   13837 		  {8, 16}
   13838 		};
   13839 	      int ver;
   13840 	      int regs;
   13841 	      int newval;
   13842 
   13843 	      /* If the output has no requirement about FP hardware,
   13844 		 follow the requirement of the input.  */
   13845 	      if (out_attr[i].i == 0)
   13846 		{
   13847 		  /* This assert is still reasonable, we shouldn't
   13848 		     produce the suspicious build attribute
   13849 		     combination (See below for in_attr).  */
   13850 		  BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
   13851 		  out_attr[i].i = in_attr[i].i;
   13852 		  out_attr[Tag_ABI_HardFP_use].i
   13853 		    = in_attr[Tag_ABI_HardFP_use].i;
   13854 		  break;
   13855 		}
   13856 	      /* If the input has no requirement about FP hardware, do
   13857 		 nothing.  */
   13858 	      else if (in_attr[i].i == 0)
   13859 		{
   13860 		  /* We used to assert that Tag_ABI_HardFP_use was
   13861 		     zero here, but we should never assert when
   13862 		     consuming an object file that has suspicious
   13863 		     build attributes.  The single precision variant
   13864 		     of 'no FP architecture' is still 'no FP
   13865 		     architecture', so we just ignore the tag in this
   13866 		     case.  */
   13867 		  break;
   13868 		}
   13869 
   13870 	      /* Both the input and the output have nonzero Tag_FP_arch.
   13871 		 So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
   13872 
   13873 	      /* If both the input and the output have zero Tag_ABI_HardFP_use,
   13874 		 do nothing.  */
   13875 	      if (in_attr[Tag_ABI_HardFP_use].i == 0
   13876 		  && out_attr[Tag_ABI_HardFP_use].i == 0)
   13877 		;
   13878 	      /* If the input and the output have different Tag_ABI_HardFP_use,
   13879 		 the combination of them is 0 (implied by Tag_FP_arch).  */
   13880 	      else if (in_attr[Tag_ABI_HardFP_use].i
   13881 		       != out_attr[Tag_ABI_HardFP_use].i)
   13882 		out_attr[Tag_ABI_HardFP_use].i = 0;
   13883 
   13884 	      /* Now we can handle Tag_FP_arch.  */
   13885 
   13886 	      /* Values of VFP_VERSION_COUNT or more aren't defined, so just
   13887 		 pick the biggest.  */
   13888 	      if (in_attr[i].i >= VFP_VERSION_COUNT
   13889 		  && in_attr[i].i > out_attr[i].i)
   13890 		{
   13891 		  out_attr[i] = in_attr[i];
   13892 		  break;
   13893 		}
   13894 	      /* The output uses the superset of input features
   13895 		 (ISA version) and registers.  */
   13896 	      ver = vfp_versions[in_attr[i].i].ver;
   13897 	      if (ver < vfp_versions[out_attr[i].i].ver)
   13898 		ver = vfp_versions[out_attr[i].i].ver;
   13899 	      regs = vfp_versions[in_attr[i].i].regs;
   13900 	      if (regs < vfp_versions[out_attr[i].i].regs)
   13901 		regs = vfp_versions[out_attr[i].i].regs;
   13902 	      /* This assumes all possible supersets are also a valid
   13903 		 options.  */
   13904 	      for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
   13905 		{
   13906 		  if (regs == vfp_versions[newval].regs
   13907 		      && ver == vfp_versions[newval].ver)
   13908 		    break;
   13909 		}
   13910 	      out_attr[i].i = newval;
   13911 	    }
   13912 	  break;
   13913 	case Tag_PCS_config:
   13914 	  if (out_attr[i].i == 0)
   13915 	    out_attr[i].i = in_attr[i].i;
   13916 	  else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
   13917 	    {
   13918 	      /* It's sometimes ok to mix different configs, so this is only
   13919 		 a warning.  */
   13920 	      _bfd_error_handler
   13921 		(_("Warning: %B: Conflicting platform configuration"), ibfd);
   13922 	    }
   13923 	  break;
   13924 	case Tag_ABI_PCS_R9_use:
   13925 	  if (in_attr[i].i != out_attr[i].i
   13926 	      && out_attr[i].i != AEABI_R9_unused
   13927 	      && in_attr[i].i != AEABI_R9_unused)
   13928 	    {
   13929 	      _bfd_error_handler
   13930 		(_("error: %B: Conflicting use of R9"), ibfd);
   13931 	      result = FALSE;
   13932 	    }
   13933 	  if (out_attr[i].i == AEABI_R9_unused)
   13934 	    out_attr[i].i = in_attr[i].i;
   13935 	  break;
   13936 	case Tag_ABI_PCS_RW_data:
   13937 	  if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
   13938 	      && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
   13939 	      && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
   13940 	    {
   13941 	      _bfd_error_handler
   13942 		(_("error: %B: SB relative addressing conflicts with use of R9"),
   13943 		 ibfd);
   13944 	      result = FALSE;
   13945 	    }
   13946 	  /* Use the smallest value specified.  */
   13947 	  if (in_attr[i].i < out_attr[i].i)
   13948 	    out_attr[i].i = in_attr[i].i;
   13949 	  break;
   13950 	case Tag_ABI_PCS_wchar_t:
   13951 	  if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
   13952 	      && !elf_arm_tdata (obfd)->no_wchar_size_warning)
   13953 	    {
   13954 	      _bfd_error_handler
   13955 		(_("warning: %B uses %u-byte wchar_t yet the output is to use %u-byte wchar_t; use of wchar_t values across objects may fail"),
   13956 		 ibfd, in_attr[i].i, out_attr[i].i);
   13957 	    }
   13958 	  else if (in_attr[i].i && !out_attr[i].i)
   13959 	    out_attr[i].i = in_attr[i].i;
   13960 	  break;
   13961 	case Tag_ABI_enum_size:
   13962 	  if (in_attr[i].i != AEABI_enum_unused)
   13963 	    {
   13964 	      if (out_attr[i].i == AEABI_enum_unused
   13965 		  || out_attr[i].i == AEABI_enum_forced_wide)
   13966 		{
   13967 		  /* The existing object is compatible with anything.
   13968 		     Use whatever requirements the new object has.  */
   13969 		  out_attr[i].i = in_attr[i].i;
   13970 		}
   13971 	      else if (in_attr[i].i != AEABI_enum_forced_wide
   13972 		       && out_attr[i].i != in_attr[i].i
   13973 		       && !elf_arm_tdata (obfd)->no_enum_size_warning)
   13974 		{
   13975 		  static const char *aeabi_enum_names[] =
   13976 		    { "", "variable-size", "32-bit", "" };
   13977 		  const char *in_name =
   13978 		    in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
   13979 		    ? aeabi_enum_names[in_attr[i].i]
   13980 		    : "<unknown>";
   13981 		  const char *out_name =
   13982 		    out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
   13983 		    ? aeabi_enum_names[out_attr[i].i]
   13984 		    : "<unknown>";
   13985 		  _bfd_error_handler
   13986 		    (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
   13987 		     ibfd, in_name, out_name);
   13988 		}
   13989 	    }
   13990 	  break;
   13991 	case Tag_ABI_VFP_args:
   13992 	  /* Aready done.  */
   13993 	  break;
   13994 	case Tag_ABI_WMMX_args:
   13995 	  if (in_attr[i].i != out_attr[i].i)
   13996 	    {
   13997 	      _bfd_error_handler
   13998 		(_("error: %B uses iWMMXt register arguments, %B does not"),
   13999 		 ibfd, obfd);
   14000 	      result = FALSE;
   14001 	    }
   14002 	  break;
   14003 	case Tag_compatibility:
   14004 	  /* Merged in target-independent code.  */
   14005 	  break;
   14006 	case Tag_ABI_HardFP_use:
   14007 	  /* This is handled along with Tag_FP_arch.  */
   14008 	  break;
   14009 	case Tag_ABI_FP_16bit_format:
   14010 	  if (in_attr[i].i != 0 && out_attr[i].i != 0)
   14011 	    {
   14012 	      if (in_attr[i].i != out_attr[i].i)
   14013 		{
   14014 		  _bfd_error_handler
   14015 		    (_("error: fp16 format mismatch between %B and %B"),
   14016 		     ibfd, obfd);
   14017 		  result = FALSE;
   14018 		}
   14019 	    }
   14020 	  if (in_attr[i].i != 0)
   14021 	    out_attr[i].i = in_attr[i].i;
   14022 	  break;
   14023 
   14024 	case Tag_DIV_use:
   14025 	  /* A value of zero on input means that the divide instruction may
   14026 	     be used if available in the base architecture as specified via
   14027 	     Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
   14028 	     the user did not want divide instructions.  A value of 2
   14029 	     explicitly means that divide instructions were allowed in ARM
   14030 	     and Thumb state.  */
   14031 	  if (in_attr[i].i == out_attr[i].i)
   14032 	    /* Do nothing.  */ ;
   14033 	  else if (elf32_arm_attributes_forbid_div (in_attr)
   14034 		   && !elf32_arm_attributes_accept_div (out_attr))
   14035 	    out_attr[i].i = 1;
   14036 	  else if (elf32_arm_attributes_forbid_div (out_attr)
   14037 		   && elf32_arm_attributes_accept_div (in_attr))
   14038 	    out_attr[i].i = in_attr[i].i;
   14039 	  else if (in_attr[i].i == 2)
   14040 	    out_attr[i].i = in_attr[i].i;
   14041 	  break;
   14042 
   14043 	case Tag_MPextension_use_legacy:
   14044 	  /* We don't output objects with Tag_MPextension_use_legacy - we
   14045 	     move the value to Tag_MPextension_use.  */
   14046 	  if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
   14047 	    {
   14048 	      if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
   14049 		{
   14050 		  _bfd_error_handler
   14051 		    (_("%B has both the current and legacy "
   14052 		       "Tag_MPextension_use attributes"),
   14053 		     ibfd);
   14054 		  result = FALSE;
   14055 		}
   14056 	    }
   14057 
   14058 	  if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
   14059 	    out_attr[Tag_MPextension_use] = in_attr[i];
   14060 
   14061 	  break;
   14062 
   14063 	case Tag_nodefaults:
   14064 	  /* This tag is set if it exists, but the value is unused (and is
   14065 	     typically zero).  We don't actually need to do anything here -
   14066 	     the merge happens automatically when the type flags are merged
   14067 	     below.  */
   14068 	  break;
   14069 	case Tag_also_compatible_with:
   14070 	  /* Already done in Tag_CPU_arch.  */
   14071 	  break;
   14072 	case Tag_conformance:
   14073 	  /* Keep the attribute if it matches.  Throw it away otherwise.
   14074 	     No attribute means no claim to conform.  */
   14075 	  if (!in_attr[i].s || !out_attr[i].s
   14076 	      || strcmp (in_attr[i].s, out_attr[i].s) != 0)
   14077 	    out_attr[i].s = NULL;
   14078 	  break;
   14079 
   14080 	default:
   14081 	  result
   14082 	    = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
   14083 	}
   14084 
   14085       /* If out_attr was copied from in_attr then it won't have a type yet.  */
   14086       if (in_attr[i].type && !out_attr[i].type)
   14087 	out_attr[i].type = in_attr[i].type;
   14088     }
   14089 
   14090   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   14091   if (!_bfd_elf_merge_object_attributes (ibfd, info))
   14092     return FALSE;
   14093 
   14094   /* Check for any attributes not known on ARM.  */
   14095   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
   14096 
   14097   return result;
   14098 }
   14099 
   14100 
   14101 /* Return TRUE if the two EABI versions are incompatible.  */
   14102 
   14103 static bfd_boolean
   14104 elf32_arm_versions_compatible (unsigned iver, unsigned over)
   14105 {
   14106   /* v4 and v5 are the same spec before and after it was released,
   14107      so allow mixing them.  */
   14108   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
   14109       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
   14110     return TRUE;
   14111 
   14112   return (iver == over);
   14113 }
   14114 
   14115 /* Merge backend specific data from an object file to the output
   14116    object file when linking.  */
   14117 
   14118 static bfd_boolean
   14119 elf32_arm_merge_private_bfd_data (bfd *, struct bfd_link_info *);
   14120 
   14121 /* Display the flags field.  */
   14122 
   14123 static bfd_boolean
   14124 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
   14125 {
   14126   FILE * file = (FILE *) ptr;
   14127   unsigned long flags;
   14128 
   14129   BFD_ASSERT (abfd != NULL && ptr != NULL);
   14130 
   14131   /* Print normal ELF private data.  */
   14132   _bfd_elf_print_private_bfd_data (abfd, ptr);
   14133 
   14134   flags = elf_elfheader (abfd)->e_flags;
   14135   /* Ignore init flag - it may not be set, despite the flags field
   14136      containing valid data.  */
   14137 
   14138   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   14139 
   14140   switch (EF_ARM_EABI_VERSION (flags))
   14141     {
   14142     case EF_ARM_EABI_UNKNOWN:
   14143       /* The following flag bits are GNU extensions and not part of the
   14144 	 official ARM ELF extended ABI.  Hence they are only decoded if
   14145 	 the EABI version is not set.  */
   14146       if (flags & EF_ARM_INTERWORK)
   14147 	fprintf (file, _(" [interworking enabled]"));
   14148 
   14149       if (flags & EF_ARM_APCS_26)
   14150 	fprintf (file, " [APCS-26]");
   14151       else
   14152 	fprintf (file, " [APCS-32]");
   14153 
   14154       if (flags & EF_ARM_VFP_FLOAT)
   14155 	fprintf (file, _(" [VFP float format]"));
   14156       else if (flags & EF_ARM_MAVERICK_FLOAT)
   14157 	fprintf (file, _(" [Maverick float format]"));
   14158       else
   14159 	fprintf (file, _(" [FPA float format]"));
   14160 
   14161       if (flags & EF_ARM_APCS_FLOAT)
   14162 	fprintf (file, _(" [floats passed in float registers]"));
   14163 
   14164       if (flags & EF_ARM_PIC)
   14165 	fprintf (file, _(" [position independent]"));
   14166 
   14167       if (flags & EF_ARM_NEW_ABI)
   14168 	fprintf (file, _(" [new ABI]"));
   14169 
   14170       if (flags & EF_ARM_OLD_ABI)
   14171 	fprintf (file, _(" [old ABI]"));
   14172 
   14173       if (flags & EF_ARM_SOFT_FLOAT)
   14174 	fprintf (file, _(" [software FP]"));
   14175 
   14176       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
   14177 		 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
   14178 		 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
   14179 		 | EF_ARM_MAVERICK_FLOAT);
   14180       break;
   14181 
   14182     case EF_ARM_EABI_VER1:
   14183       fprintf (file, _(" [Version1 EABI]"));
   14184 
   14185       if (flags & EF_ARM_SYMSARESORTED)
   14186 	fprintf (file, _(" [sorted symbol table]"));
   14187       else
   14188 	fprintf (file, _(" [unsorted symbol table]"));
   14189 
   14190       flags &= ~ EF_ARM_SYMSARESORTED;
   14191       break;
   14192 
   14193     case EF_ARM_EABI_VER2:
   14194       fprintf (file, _(" [Version2 EABI]"));
   14195 
   14196       if (flags & EF_ARM_SYMSARESORTED)
   14197 	fprintf (file, _(" [sorted symbol table]"));
   14198       else
   14199 	fprintf (file, _(" [unsorted symbol table]"));
   14200 
   14201       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
   14202 	fprintf (file, _(" [dynamic symbols use segment index]"));
   14203 
   14204       if (flags & EF_ARM_MAPSYMSFIRST)
   14205 	fprintf (file, _(" [mapping symbols precede others]"));
   14206 
   14207       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
   14208 		 | EF_ARM_MAPSYMSFIRST);
   14209       break;
   14210 
   14211     case EF_ARM_EABI_VER3:
   14212       fprintf (file, _(" [Version3 EABI]"));
   14213       break;
   14214 
   14215     case EF_ARM_EABI_VER4:
   14216       fprintf (file, _(" [Version4 EABI]"));
   14217       goto eabi;
   14218 
   14219     case EF_ARM_EABI_VER5:
   14220       fprintf (file, _(" [Version5 EABI]"));
   14221 
   14222       if (flags & EF_ARM_ABI_FLOAT_SOFT)
   14223 	fprintf (file, _(" [soft-float ABI]"));
   14224 
   14225       if (flags & EF_ARM_ABI_FLOAT_HARD)
   14226 	fprintf (file, _(" [hard-float ABI]"));
   14227 
   14228       flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
   14229 
   14230     eabi:
   14231       if (flags & EF_ARM_BE8)
   14232 	fprintf (file, _(" [BE8]"));
   14233 
   14234       if (flags & EF_ARM_LE8)
   14235 	fprintf (file, _(" [LE8]"));
   14236 
   14237       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
   14238       break;
   14239 
   14240     default:
   14241       fprintf (file, _(" <EABI version unrecognised>"));
   14242       break;
   14243     }
   14244 
   14245   flags &= ~ EF_ARM_EABIMASK;
   14246 
   14247   if (flags & EF_ARM_RELEXEC)
   14248     fprintf (file, _(" [relocatable executable]"));
   14249 
   14250   flags &= ~EF_ARM_RELEXEC;
   14251 
   14252   if (flags)
   14253     fprintf (file, _("<Unrecognised flag bits set>"));
   14254 
   14255   fputc ('\n', file);
   14256 
   14257   return TRUE;
   14258 }
   14259 
   14260 static int
   14261 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
   14262 {
   14263   switch (ELF_ST_TYPE (elf_sym->st_info))
   14264     {
   14265     case STT_ARM_TFUNC:
   14266       return ELF_ST_TYPE (elf_sym->st_info);
   14267 
   14268     case STT_ARM_16BIT:
   14269       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
   14270 	 This allows us to distinguish between data used by Thumb instructions
   14271 	 and non-data (which is probably code) inside Thumb regions of an
   14272 	 executable.  */
   14273       if (type != STT_OBJECT && type != STT_TLS)
   14274 	return ELF_ST_TYPE (elf_sym->st_info);
   14275       break;
   14276 
   14277     default:
   14278       break;
   14279     }
   14280 
   14281   return type;
   14282 }
   14283 
   14284 static asection *
   14285 elf32_arm_gc_mark_hook (asection *sec,
   14286 			struct bfd_link_info *info,
   14287 			Elf_Internal_Rela *rel,
   14288 			struct elf_link_hash_entry *h,
   14289 			Elf_Internal_Sym *sym)
   14290 {
   14291   if (h != NULL)
   14292     switch (ELF32_R_TYPE (rel->r_info))
   14293       {
   14294       case R_ARM_GNU_VTINHERIT:
   14295       case R_ARM_GNU_VTENTRY:
   14296 	return NULL;
   14297       }
   14298 
   14299   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   14300 }
   14301 
   14302 /* Look through the relocs for a section during the first phase.  */
   14303 
   14304 static bfd_boolean
   14305 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
   14306 			asection *sec, const Elf_Internal_Rela *relocs)
   14307 {
   14308   Elf_Internal_Shdr *symtab_hdr;
   14309   struct elf_link_hash_entry **sym_hashes;
   14310   const Elf_Internal_Rela *rel;
   14311   const Elf_Internal_Rela *rel_end;
   14312   bfd *dynobj;
   14313   asection *sreloc;
   14314   struct elf32_arm_link_hash_table *htab;
   14315   bfd_boolean call_reloc_p;
   14316   bfd_boolean may_become_dynamic_p;
   14317   bfd_boolean may_need_local_target_p;
   14318   unsigned long nsyms;
   14319 
   14320   if (bfd_link_relocatable (info))
   14321     return TRUE;
   14322 
   14323   BFD_ASSERT (is_arm_elf (abfd));
   14324 
   14325   htab = elf32_arm_hash_table (info);
   14326   if (htab == NULL)
   14327     return FALSE;
   14328 
   14329   sreloc = NULL;
   14330 
   14331   /* Create dynamic sections for relocatable executables so that we can
   14332      copy relocations.  */
   14333   if (htab->root.is_relocatable_executable
   14334       && ! htab->root.dynamic_sections_created)
   14335     {
   14336       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   14337 	return FALSE;
   14338     }
   14339 
   14340   if (htab->root.dynobj == NULL)
   14341     htab->root.dynobj = abfd;
   14342   if (!create_ifunc_sections (info))
   14343     return FALSE;
   14344 
   14345   dynobj = htab->root.dynobj;
   14346 
   14347   symtab_hdr = & elf_symtab_hdr (abfd);
   14348   sym_hashes = elf_sym_hashes (abfd);
   14349   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   14350 
   14351   rel_end = relocs + sec->reloc_count;
   14352   for (rel = relocs; rel < rel_end; rel++)
   14353     {
   14354       Elf_Internal_Sym *isym;
   14355       struct elf_link_hash_entry *h;
   14356       struct elf32_arm_link_hash_entry *eh;
   14357       unsigned int r_symndx;
   14358       int r_type;
   14359 
   14360       r_symndx = ELF32_R_SYM (rel->r_info);
   14361       r_type = ELF32_R_TYPE (rel->r_info);
   14362       r_type = arm_real_reloc_type (htab, r_type);
   14363 
   14364       if (r_symndx >= nsyms
   14365 	  /* PR 9934: It is possible to have relocations that do not
   14366 	     refer to symbols, thus it is also possible to have an
   14367 	     object file containing relocations but no symbol table.  */
   14368 	  && (r_symndx > STN_UNDEF || nsyms > 0))
   14369 	{
   14370 	  _bfd_error_handler (_("%B: bad symbol index: %d"), abfd,
   14371 			      r_symndx);
   14372 	  return FALSE;
   14373 	}
   14374 
   14375       h = NULL;
   14376       isym = NULL;
   14377       if (nsyms > 0)
   14378 	{
   14379 	  if (r_symndx < symtab_hdr->sh_info)
   14380 	    {
   14381 	      /* A local symbol.  */
   14382 	      isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   14383 					    abfd, r_symndx);
   14384 	      if (isym == NULL)
   14385 		return FALSE;
   14386 	    }
   14387 	  else
   14388 	    {
   14389 	      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   14390 	      while (h->root.type == bfd_link_hash_indirect
   14391 		     || h->root.type == bfd_link_hash_warning)
   14392 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   14393 	    }
   14394 	}
   14395 
   14396       eh = (struct elf32_arm_link_hash_entry *) h;
   14397 
   14398       call_reloc_p = FALSE;
   14399       may_become_dynamic_p = FALSE;
   14400       may_need_local_target_p = FALSE;
   14401 
   14402       /* Could be done earlier, if h were already available.  */
   14403       r_type = elf32_arm_tls_transition (info, r_type, h);
   14404       switch (r_type)
   14405 	{
   14406 	  case R_ARM_GOT32:
   14407 	  case R_ARM_GOT_PREL:
   14408 	  case R_ARM_TLS_GD32:
   14409 	  case R_ARM_TLS_IE32:
   14410 	  case R_ARM_TLS_GOTDESC:
   14411 	  case R_ARM_TLS_DESCSEQ:
   14412 	  case R_ARM_THM_TLS_DESCSEQ:
   14413 	  case R_ARM_TLS_CALL:
   14414 	  case R_ARM_THM_TLS_CALL:
   14415 	    /* This symbol requires a global offset table entry.  */
   14416 	    {
   14417 	      int tls_type, old_tls_type;
   14418 
   14419 	      switch (r_type)
   14420 		{
   14421 		case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
   14422 
   14423 		case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
   14424 
   14425 		case R_ARM_TLS_GOTDESC:
   14426 		case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
   14427 		case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
   14428 		  tls_type = GOT_TLS_GDESC; break;
   14429 
   14430 		default: tls_type = GOT_NORMAL; break;
   14431 		}
   14432 
   14433 	      if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
   14434 		info->flags |= DF_STATIC_TLS;
   14435 
   14436 	      if (h != NULL)
   14437 		{
   14438 		  h->got.refcount++;
   14439 		  old_tls_type = elf32_arm_hash_entry (h)->tls_type;
   14440 		}
   14441 	      else
   14442 		{
   14443 		  /* This is a global offset table entry for a local symbol.  */
   14444 		  if (!elf32_arm_allocate_local_sym_info (abfd))
   14445 		    return FALSE;
   14446 		  elf_local_got_refcounts (abfd)[r_symndx] += 1;
   14447 		  old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
   14448 		}
   14449 
   14450 	      /* If a variable is accessed with both tls methods, two
   14451 		 slots may be created.  */
   14452 	      if (GOT_TLS_GD_ANY_P (old_tls_type)
   14453 		  && GOT_TLS_GD_ANY_P (tls_type))
   14454 		tls_type |= old_tls_type;
   14455 
   14456 	      /* We will already have issued an error message if there
   14457 		 is a TLS/non-TLS mismatch, based on the symbol
   14458 		 type.  So just combine any TLS types needed.  */
   14459 	      if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
   14460 		  && tls_type != GOT_NORMAL)
   14461 		tls_type |= old_tls_type;
   14462 
   14463 	      /* If the symbol is accessed in both IE and GDESC
   14464 		 method, we're able to relax. Turn off the GDESC flag,
   14465 		 without messing up with any other kind of tls types
   14466 		 that may be involved.  */
   14467 	      if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
   14468 		tls_type &= ~GOT_TLS_GDESC;
   14469 
   14470 	      if (old_tls_type != tls_type)
   14471 		{
   14472 		  if (h != NULL)
   14473 		    elf32_arm_hash_entry (h)->tls_type = tls_type;
   14474 		  else
   14475 		    elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
   14476 		}
   14477 	    }
   14478 	    /* Fall through.  */
   14479 
   14480 	  case R_ARM_TLS_LDM32:
   14481 	    if (r_type == R_ARM_TLS_LDM32)
   14482 		htab->tls_ldm_got.refcount++;
   14483 	    /* Fall through.  */
   14484 
   14485 	  case R_ARM_GOTOFF32:
   14486 	  case R_ARM_GOTPC:
   14487 	    if (htab->root.sgot == NULL
   14488 		&& !create_got_section (htab->root.dynobj, info))
   14489 	      return FALSE;
   14490 	    break;
   14491 
   14492 	  case R_ARM_PC24:
   14493 	  case R_ARM_PLT32:
   14494 	  case R_ARM_CALL:
   14495 	  case R_ARM_JUMP24:
   14496 	  case R_ARM_PREL31:
   14497 	  case R_ARM_THM_CALL:
   14498 	  case R_ARM_THM_JUMP24:
   14499 	  case R_ARM_THM_JUMP19:
   14500 	    call_reloc_p = TRUE;
   14501 	    may_need_local_target_p = TRUE;
   14502 	    break;
   14503 
   14504 	  case R_ARM_ABS12:
   14505 	    /* VxWorks uses dynamic R_ARM_ABS12 relocations for
   14506 	       ldr __GOTT_INDEX__ offsets.  */
   14507 	    if (!htab->vxworks_p)
   14508 	      {
   14509 		may_need_local_target_p = TRUE;
   14510 		break;
   14511 	      }
   14512 	    else goto jump_over;
   14513 
   14514 	    /* Fall through.  */
   14515 
   14516 	  case R_ARM_MOVW_ABS_NC:
   14517 	  case R_ARM_MOVT_ABS:
   14518 	  case R_ARM_THM_MOVW_ABS_NC:
   14519 	  case R_ARM_THM_MOVT_ABS:
   14520 	    if (bfd_link_pic (info))
   14521 	      {
   14522 		_bfd_error_handler
   14523 		  (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   14524 		   abfd, elf32_arm_howto_table_1[r_type].name,
   14525 		   (h) ? h->root.root.string : "a local symbol");
   14526 		bfd_set_error (bfd_error_bad_value);
   14527 		return FALSE;
   14528 	      }
   14529 
   14530 	    /* Fall through.  */
   14531 	  case R_ARM_ABS32:
   14532 	  case R_ARM_ABS32_NOI:
   14533 	jump_over:
   14534 	    if (h != NULL && bfd_link_executable (info))
   14535 	      {
   14536 		h->pointer_equality_needed = 1;
   14537 	      }
   14538 	    /* Fall through.  */
   14539 	  case R_ARM_REL32:
   14540 	  case R_ARM_REL32_NOI:
   14541 	  case R_ARM_MOVW_PREL_NC:
   14542 	  case R_ARM_MOVT_PREL:
   14543 	  case R_ARM_THM_MOVW_PREL_NC:
   14544 	  case R_ARM_THM_MOVT_PREL:
   14545 
   14546 	    /* Should the interworking branches be listed here?  */
   14547 	    if ((bfd_link_pic (info) || htab->root.is_relocatable_executable)
   14548 		&& (sec->flags & SEC_ALLOC) != 0)
   14549 	      {
   14550 		if (h == NULL
   14551 		    && elf32_arm_howto_from_type (r_type)->pc_relative)
   14552 		  {
   14553 		    /* In shared libraries and relocatable executables,
   14554 		       we treat local relative references as calls;
   14555 		       see the related SYMBOL_CALLS_LOCAL code in
   14556 		       allocate_dynrelocs.  */
   14557 		    call_reloc_p = TRUE;
   14558 		    may_need_local_target_p = TRUE;
   14559 		  }
   14560 		else
   14561 		  /* We are creating a shared library or relocatable
   14562 		     executable, and this is a reloc against a global symbol,
   14563 		     or a non-PC-relative reloc against a local symbol.
   14564 		     We may need to copy the reloc into the output.  */
   14565 		  may_become_dynamic_p = TRUE;
   14566 	      }
   14567 	    else
   14568 	      may_need_local_target_p = TRUE;
   14569 	    break;
   14570 
   14571 	/* This relocation describes the C++ object vtable hierarchy.
   14572 	   Reconstruct it for later use during GC.  */
   14573 	case R_ARM_GNU_VTINHERIT:
   14574 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   14575 	    return FALSE;
   14576 	  break;
   14577 
   14578 	/* This relocation describes which C++ vtable entries are actually
   14579 	   used.  Record for later use during GC.  */
   14580 	case R_ARM_GNU_VTENTRY:
   14581 	  BFD_ASSERT (h != NULL);
   14582 	  if (h != NULL
   14583 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   14584 	    return FALSE;
   14585 	  break;
   14586 	}
   14587 
   14588       if (h != NULL)
   14589 	{
   14590 	  if (call_reloc_p)
   14591 	    /* We may need a .plt entry if the function this reloc
   14592 	       refers to is in a different object, regardless of the
   14593 	       symbol's type.  We can't tell for sure yet, because
   14594 	       something later might force the symbol local.  */
   14595 	    h->needs_plt = 1;
   14596 	  else if (may_need_local_target_p)
   14597 	    /* If this reloc is in a read-only section, we might
   14598 	       need a copy reloc.  We can't check reliably at this
   14599 	       stage whether the section is read-only, as input
   14600 	       sections have not yet been mapped to output sections.
   14601 	       Tentatively set the flag for now, and correct in
   14602 	       adjust_dynamic_symbol.  */
   14603 	    h->non_got_ref = 1;
   14604 	}
   14605 
   14606       if (may_need_local_target_p
   14607 	  && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
   14608 	{
   14609 	  union gotplt_union *root_plt;
   14610 	  struct arm_plt_info *arm_plt;
   14611 	  struct arm_local_iplt_info *local_iplt;
   14612 
   14613 	  if (h != NULL)
   14614 	    {
   14615 	      root_plt = &h->plt;
   14616 	      arm_plt = &eh->plt;
   14617 	    }
   14618 	  else
   14619 	    {
   14620 	      local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
   14621 	      if (local_iplt == NULL)
   14622 		return FALSE;
   14623 	      root_plt = &local_iplt->root;
   14624 	      arm_plt = &local_iplt->arm;
   14625 	    }
   14626 
   14627 	  /* If the symbol is a function that doesn't bind locally,
   14628 	     this relocation will need a PLT entry.  */
   14629 	  if (root_plt->refcount != -1)
   14630 	    root_plt->refcount += 1;
   14631 
   14632 	  if (!call_reloc_p)
   14633 	    arm_plt->noncall_refcount++;
   14634 
   14635 	  /* It's too early to use htab->use_blx here, so we have to
   14636 	     record possible blx references separately from
   14637 	     relocs that definitely need a thumb stub.  */
   14638 
   14639 	  if (r_type == R_ARM_THM_CALL)
   14640 	    arm_plt->maybe_thumb_refcount += 1;
   14641 
   14642 	  if (r_type == R_ARM_THM_JUMP24
   14643 	      || r_type == R_ARM_THM_JUMP19)
   14644 	    arm_plt->thumb_refcount += 1;
   14645 	}
   14646 
   14647       if (may_become_dynamic_p)
   14648 	{
   14649 	  struct elf_dyn_relocs *p, **head;
   14650 
   14651 	  /* Create a reloc section in dynobj.  */
   14652 	  if (sreloc == NULL)
   14653 	    {
   14654 	      sreloc = _bfd_elf_make_dynamic_reloc_section
   14655 		(sec, dynobj, 2, abfd, ! htab->use_rel);
   14656 
   14657 	      if (sreloc == NULL)
   14658 		return FALSE;
   14659 
   14660 	      /* BPABI objects never have dynamic relocations mapped.  */
   14661 	      if (htab->symbian_p)
   14662 		{
   14663 		  flagword flags;
   14664 
   14665 		  flags = bfd_get_section_flags (dynobj, sreloc);
   14666 		  flags &= ~(SEC_LOAD | SEC_ALLOC);
   14667 		  bfd_set_section_flags (dynobj, sreloc, flags);
   14668 		}
   14669 	    }
   14670 
   14671 	  /* If this is a global symbol, count the number of
   14672 	     relocations we need for this symbol.  */
   14673 	  if (h != NULL)
   14674 	    head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
   14675 	  else
   14676 	    {
   14677 	      head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
   14678 	      if (head == NULL)
   14679 		return FALSE;
   14680 	    }
   14681 
   14682 	  p = *head;
   14683 	  if (p == NULL || p->sec != sec)
   14684 	    {
   14685 	      bfd_size_type amt = sizeof *p;
   14686 
   14687 	      p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
   14688 	      if (p == NULL)
   14689 		return FALSE;
   14690 	      p->next = *head;
   14691 	      *head = p;
   14692 	      p->sec = sec;
   14693 	      p->count = 0;
   14694 	      p->pc_count = 0;
   14695 	    }
   14696 
   14697 	  if (elf32_arm_howto_from_type (r_type)->pc_relative)
   14698 	    p->pc_count += 1;
   14699 	  p->count += 1;
   14700 	}
   14701     }
   14702 
   14703   return TRUE;
   14704 }
   14705 
   14706 static void
   14707 elf32_arm_update_relocs (asection *o,
   14708 			 struct bfd_elf_section_reloc_data *reldata)
   14709 {
   14710   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   14711   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   14712   const struct elf_backend_data *bed;
   14713   _arm_elf_section_data *eado;
   14714   struct bfd_link_order *p;
   14715   bfd_byte *erela_head, *erela;
   14716   Elf_Internal_Rela *irela_head, *irela;
   14717   Elf_Internal_Shdr *rel_hdr;
   14718   bfd *abfd;
   14719   unsigned int count;
   14720 
   14721   eado = get_arm_elf_section_data (o);
   14722 
   14723   if (!eado || eado->elf.this_hdr.sh_type != SHT_ARM_EXIDX)
   14724     return;
   14725 
   14726   abfd = o->owner;
   14727   bed = get_elf_backend_data (abfd);
   14728   rel_hdr = reldata->hdr;
   14729 
   14730   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
   14731     {
   14732       swap_in = bed->s->swap_reloc_in;
   14733       swap_out = bed->s->swap_reloc_out;
   14734     }
   14735   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
   14736     {
   14737       swap_in = bed->s->swap_reloca_in;
   14738       swap_out = bed->s->swap_reloca_out;
   14739     }
   14740   else
   14741     abort ();
   14742 
   14743   erela_head = rel_hdr->contents;
   14744   irela_head = (Elf_Internal_Rela *) bfd_zmalloc
   14745     ((NUM_SHDR_ENTRIES (rel_hdr) + 1) * sizeof (*irela_head));
   14746 
   14747   erela = erela_head;
   14748   irela = irela_head;
   14749   count = 0;
   14750 
   14751   for (p = o->map_head.link_order; p; p = p->next)
   14752     {
   14753       if (p->type == bfd_section_reloc_link_order
   14754 	  || p->type == bfd_symbol_reloc_link_order)
   14755 	{
   14756 	  (*swap_in) (abfd, erela, irela);
   14757 	  erela += rel_hdr->sh_entsize;
   14758 	  irela++;
   14759 	  count++;
   14760 	}
   14761       else if (p->type == bfd_indirect_link_order)
   14762 	{
   14763 	  struct bfd_elf_section_reloc_data *input_reldata;
   14764 	  arm_unwind_table_edit *edit_list, *edit_tail;
   14765 	  _arm_elf_section_data *eadi;
   14766 	  bfd_size_type j;
   14767 	  bfd_vma offset;
   14768 	  asection *i;
   14769 
   14770 	  i = p->u.indirect.section;
   14771 
   14772 	  eadi = get_arm_elf_section_data (i);
   14773 	  edit_list = eadi->u.exidx.unwind_edit_list;
   14774 	  edit_tail = eadi->u.exidx.unwind_edit_tail;
   14775 	  offset = o->vma + i->output_offset;
   14776 
   14777 	  if (eadi->elf.rel.hdr &&
   14778 	      eadi->elf.rel.hdr->sh_entsize == rel_hdr->sh_entsize)
   14779 	    input_reldata = &eadi->elf.rel;
   14780 	  else if (eadi->elf.rela.hdr &&
   14781 		   eadi->elf.rela.hdr->sh_entsize == rel_hdr->sh_entsize)
   14782 	    input_reldata = &eadi->elf.rela;
   14783 	  else
   14784 	    abort ();
   14785 
   14786 	  if (edit_list)
   14787 	    {
   14788 	      for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
   14789 		{
   14790 		  arm_unwind_table_edit *edit_node, *edit_next;
   14791 		  bfd_vma bias;
   14792 		  bfd_vma reloc_index;
   14793 
   14794 		  (*swap_in) (abfd, erela, irela);
   14795 		  reloc_index = (irela->r_offset - offset) / 8;
   14796 
   14797 		  bias = 0;
   14798 		  edit_node = edit_list;
   14799 		  for (edit_next = edit_list;
   14800 		       edit_next && edit_next->index <= reloc_index;
   14801 		       edit_next = edit_node->next)
   14802 		    {
   14803 		      bias++;
   14804 		      edit_node = edit_next;
   14805 		    }
   14806 
   14807 		  if (edit_node->type != DELETE_EXIDX_ENTRY
   14808 		      || edit_node->index != reloc_index)
   14809 		    {
   14810 		      irela->r_offset -= bias * 8;
   14811 		      irela++;
   14812 		      count++;
   14813 		    }
   14814 
   14815 		  erela += rel_hdr->sh_entsize;
   14816 		}
   14817 
   14818 	      if (edit_tail->type == INSERT_EXIDX_CANTUNWIND_AT_END)
   14819 		{
   14820 		  /* New relocation entity.  */
   14821 		  asection *text_sec = edit_tail->linked_section;
   14822 		  asection *text_out = text_sec->output_section;
   14823 		  bfd_vma exidx_offset = offset + i->size - 8;
   14824 
   14825 		  irela->r_addend = 0;
   14826 		  irela->r_offset = exidx_offset;
   14827 		  irela->r_info = ELF32_R_INFO
   14828 		    (text_out->target_index, R_ARM_PREL31);
   14829 		  irela++;
   14830 		  count++;
   14831 		}
   14832 	    }
   14833 	  else
   14834 	    {
   14835 	      for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
   14836 		{
   14837 		  (*swap_in) (abfd, erela, irela);
   14838 		  erela += rel_hdr->sh_entsize;
   14839 		  irela++;
   14840 		}
   14841 
   14842 	      count += NUM_SHDR_ENTRIES (input_reldata->hdr);
   14843 	    }
   14844 	}
   14845     }
   14846 
   14847   reldata->count = count;
   14848   rel_hdr->sh_size = count * rel_hdr->sh_entsize;
   14849 
   14850   erela = erela_head;
   14851   irela = irela_head;
   14852   while (count > 0)
   14853     {
   14854       (*swap_out) (abfd, irela, erela);
   14855       erela += rel_hdr->sh_entsize;
   14856       irela++;
   14857       count--;
   14858     }
   14859 
   14860   free (irela_head);
   14861 
   14862   /* Hashes are no longer valid.  */
   14863   free (reldata->hashes);
   14864   reldata->hashes = NULL;
   14865 }
   14866 
   14867 /* Unwinding tables are not referenced directly.  This pass marks them as
   14868    required if the corresponding code section is marked.  Similarly, ARMv8-M
   14869    secure entry functions can only be referenced by SG veneers which are
   14870    created after the GC process. They need to be marked in case they reside in
   14871    their own section (as would be the case if code was compiled with
   14872    -ffunction-sections).  */
   14873 
   14874 static bfd_boolean
   14875 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
   14876 				  elf_gc_mark_hook_fn gc_mark_hook)
   14877 {
   14878   bfd *sub;
   14879   Elf_Internal_Shdr **elf_shdrp;
   14880   asection *cmse_sec;
   14881   obj_attribute *out_attr;
   14882   Elf_Internal_Shdr *symtab_hdr;
   14883   unsigned i, sym_count, ext_start;
   14884   const struct elf_backend_data *bed;
   14885   struct elf_link_hash_entry **sym_hashes;
   14886   struct elf32_arm_link_hash_entry *cmse_hash;
   14887   bfd_boolean again, is_v8m, first_bfd_browse = TRUE;
   14888 
   14889   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
   14890 
   14891   out_attr = elf_known_obj_attributes_proc (info->output_bfd);
   14892   is_v8m = out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
   14893 	   && out_attr[Tag_CPU_arch_profile].i == 'M';
   14894 
   14895   /* Marking EH data may cause additional code sections to be marked,
   14896      requiring multiple passes.  */
   14897   again = TRUE;
   14898   while (again)
   14899     {
   14900       again = FALSE;
   14901       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14902 	{
   14903 	  asection *o;
   14904 
   14905 	  if (! is_arm_elf (sub))
   14906 	    continue;
   14907 
   14908 	  elf_shdrp = elf_elfsections (sub);
   14909 	  for (o = sub->sections; o != NULL; o = o->next)
   14910 	    {
   14911 	      Elf_Internal_Shdr *hdr;
   14912 
   14913 	      hdr = &elf_section_data (o)->this_hdr;
   14914 	      if (hdr->sh_type == SHT_ARM_EXIDX
   14915 		  && hdr->sh_link
   14916 		  && hdr->sh_link < elf_numsections (sub)
   14917 		  && !o->gc_mark
   14918 		  && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
   14919 		{
   14920 		  again = TRUE;
   14921 		  if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   14922 		    return FALSE;
   14923 		}
   14924 	    }
   14925 
   14926 	  /* Mark section holding ARMv8-M secure entry functions.  We mark all
   14927 	     of them so no need for a second browsing.  */
   14928 	  if (is_v8m && first_bfd_browse)
   14929 	    {
   14930 	      sym_hashes = elf_sym_hashes (sub);
   14931 	      bed = get_elf_backend_data (sub);
   14932 	      symtab_hdr = &elf_tdata (sub)->symtab_hdr;
   14933 	      sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
   14934 	      ext_start = symtab_hdr->sh_info;
   14935 
   14936 	      /* Scan symbols.  */
   14937 	      for (i = ext_start; i < sym_count; i++)
   14938 		{
   14939 		  cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
   14940 
   14941 		  /* Assume it is a special symbol.  If not, cmse_scan will
   14942 		     warn about it and user can do something about it.  */
   14943 		  if (ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
   14944 		    {
   14945 		      cmse_sec = cmse_hash->root.root.u.def.section;
   14946 		      if (!cmse_sec->gc_mark
   14947 			  && !_bfd_elf_gc_mark (info, cmse_sec, gc_mark_hook))
   14948 			return FALSE;
   14949 		    }
   14950 		}
   14951 	    }
   14952 	}
   14953       first_bfd_browse = FALSE;
   14954     }
   14955 
   14956   return TRUE;
   14957 }
   14958 
   14959 /* Treat mapping symbols as special target symbols.  */
   14960 
   14961 static bfd_boolean
   14962 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
   14963 {
   14964   return bfd_is_arm_special_symbol_name (sym->name,
   14965 					 BFD_ARM_SPECIAL_SYM_TYPE_ANY);
   14966 }
   14967 
   14968 /* This is a copy of elf_find_function() from elf.c except that
   14969    ARM mapping symbols are ignored when looking for function names
   14970    and STT_ARM_TFUNC is considered to a function type.  */
   14971 
   14972 static bfd_boolean
   14973 arm_elf_find_function (bfd *	     abfd ATTRIBUTE_UNUSED,
   14974 		       asymbol **    symbols,
   14975 		       asection *    section,
   14976 		       bfd_vma	     offset,
   14977 		       const char ** filename_ptr,
   14978 		       const char ** functionname_ptr)
   14979 {
   14980   const char * filename = NULL;
   14981   asymbol * func = NULL;
   14982   bfd_vma low_func = 0;
   14983   asymbol ** p;
   14984 
   14985   for (p = symbols; *p != NULL; p++)
   14986     {
   14987       elf_symbol_type *q;
   14988 
   14989       q = (elf_symbol_type *) *p;
   14990 
   14991       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
   14992 	{
   14993 	default:
   14994 	  break;
   14995 	case STT_FILE:
   14996 	  filename = bfd_asymbol_name (&q->symbol);
   14997 	  break;
   14998 	case STT_FUNC:
   14999 	case STT_ARM_TFUNC:
   15000 	case STT_NOTYPE:
   15001 	  /* Skip mapping symbols.  */
   15002 	  if ((q->symbol.flags & BSF_LOCAL)
   15003 	      && bfd_is_arm_special_symbol_name (q->symbol.name,
   15004 		    BFD_ARM_SPECIAL_SYM_TYPE_ANY))
   15005 	    continue;
   15006 	  /* Fall through.  */
   15007 	  if (bfd_get_section (&q->symbol) == section
   15008 	      && q->symbol.value >= low_func
   15009 	      && q->symbol.value <= offset)
   15010 	    {
   15011 	      func = (asymbol *) q;
   15012 	      low_func = q->symbol.value;
   15013 	    }
   15014 	  break;
   15015 	}
   15016     }
   15017 
   15018   if (func == NULL)
   15019     return FALSE;
   15020 
   15021   if (filename_ptr)
   15022     *filename_ptr = filename;
   15023   if (functionname_ptr)
   15024     *functionname_ptr = bfd_asymbol_name (func);
   15025 
   15026   return TRUE;
   15027 }
   15028 
   15029 
   15030 /* Find the nearest line to a particular section and offset, for error
   15031    reporting.   This code is a duplicate of the code in elf.c, except
   15032    that it uses arm_elf_find_function.  */
   15033 
   15034 static bfd_boolean
   15035 elf32_arm_find_nearest_line (bfd *	    abfd,
   15036 			     asymbol **	    symbols,
   15037 			     asection *	    section,
   15038 			     bfd_vma	    offset,
   15039 			     const char **  filename_ptr,
   15040 			     const char **  functionname_ptr,
   15041 			     unsigned int * line_ptr,
   15042 			     unsigned int * discriminator_ptr)
   15043 {
   15044   bfd_boolean found = FALSE;
   15045 
   15046   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   15047 				     filename_ptr, functionname_ptr,
   15048 				     line_ptr, discriminator_ptr,
   15049 				     dwarf_debug_sections, 0,
   15050 				     & elf_tdata (abfd)->dwarf2_find_line_info))
   15051     {
   15052       if (!*functionname_ptr)
   15053 	arm_elf_find_function (abfd, symbols, section, offset,
   15054 			       *filename_ptr ? NULL : filename_ptr,
   15055 			       functionname_ptr);
   15056 
   15057       return TRUE;
   15058     }
   15059 
   15060   /* Skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain
   15061      uses DWARF1.  */
   15062 
   15063   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   15064 					     & found, filename_ptr,
   15065 					     functionname_ptr, line_ptr,
   15066 					     & elf_tdata (abfd)->line_info))
   15067     return FALSE;
   15068 
   15069   if (found && (*functionname_ptr || *line_ptr))
   15070     return TRUE;
   15071 
   15072   if (symbols == NULL)
   15073     return FALSE;
   15074 
   15075   if (! arm_elf_find_function (abfd, symbols, section, offset,
   15076 			       filename_ptr, functionname_ptr))
   15077     return FALSE;
   15078 
   15079   *line_ptr = 0;
   15080   return TRUE;
   15081 }
   15082 
   15083 static bfd_boolean
   15084 elf32_arm_find_inliner_info (bfd *	    abfd,
   15085 			     const char **  filename_ptr,
   15086 			     const char **  functionname_ptr,
   15087 			     unsigned int * line_ptr)
   15088 {
   15089   bfd_boolean found;
   15090   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   15091 					 functionname_ptr, line_ptr,
   15092 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   15093   return found;
   15094 }
   15095 
   15096 /* Find dynamic relocs for H that apply to read-only sections.  */
   15097 
   15098 static asection *
   15099 readonly_dynrelocs (struct elf_link_hash_entry *h)
   15100 {
   15101   struct elf_dyn_relocs *p;
   15102 
   15103   for (p = elf32_arm_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
   15104     {
   15105       asection *s = p->sec->output_section;
   15106 
   15107       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   15108 	return p->sec;
   15109     }
   15110   return NULL;
   15111 }
   15112 
   15113 /* Adjust a symbol defined by a dynamic object and referenced by a
   15114    regular object.  The current definition is in some section of the
   15115    dynamic object, but we're not including those sections.  We have to
   15116    change the definition to something the rest of the link can
   15117    understand.  */
   15118 
   15119 static bfd_boolean
   15120 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
   15121 				 struct elf_link_hash_entry * h)
   15122 {
   15123   bfd * dynobj;
   15124   asection *s, *srel;
   15125   struct elf32_arm_link_hash_entry * eh;
   15126   struct elf32_arm_link_hash_table *globals;
   15127 
   15128   globals = elf32_arm_hash_table (info);
   15129   if (globals == NULL)
   15130     return FALSE;
   15131 
   15132   dynobj = elf_hash_table (info)->dynobj;
   15133 
   15134   /* Make sure we know what is going on here.  */
   15135   BFD_ASSERT (dynobj != NULL
   15136 	      && (h->needs_plt
   15137 		  || h->type == STT_GNU_IFUNC
   15138 		  || h->is_weakalias
   15139 		  || (h->def_dynamic
   15140 		      && h->ref_regular
   15141 		      && !h->def_regular)));
   15142 
   15143   eh = (struct elf32_arm_link_hash_entry *) h;
   15144 
   15145   /* If this is a function, put it in the procedure linkage table.  We
   15146      will fill in the contents of the procedure linkage table later,
   15147      when we know the address of the .got section.  */
   15148   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
   15149     {
   15150       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
   15151 	 symbol binds locally.  */
   15152       if (h->plt.refcount <= 0
   15153 	  || (h->type != STT_GNU_IFUNC
   15154 	      && (SYMBOL_CALLS_LOCAL (info, h)
   15155 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   15156 		      && h->root.type == bfd_link_hash_undefweak))))
   15157 	{
   15158 	  /* This case can occur if we saw a PLT32 reloc in an input
   15159 	     file, but the symbol was never referred to by a dynamic
   15160 	     object, or if all references were garbage collected.  In
   15161 	     such a case, we don't actually need to build a procedure
   15162 	     linkage table, and we can just do a PC24 reloc instead.  */
   15163 	  h->plt.offset = (bfd_vma) -1;
   15164 	  eh->plt.thumb_refcount = 0;
   15165 	  eh->plt.maybe_thumb_refcount = 0;
   15166 	  eh->plt.noncall_refcount = 0;
   15167 	  h->needs_plt = 0;
   15168 	}
   15169 
   15170       return TRUE;
   15171     }
   15172   else
   15173     {
   15174       /* It's possible that we incorrectly decided a .plt reloc was
   15175 	 needed for an R_ARM_PC24 or similar reloc to a non-function sym
   15176 	 in check_relocs.  We can't decide accurately between function
   15177 	 and non-function syms in check-relocs; Objects loaded later in
   15178 	 the link may change h->type.  So fix it now.  */
   15179       h->plt.offset = (bfd_vma) -1;
   15180       eh->plt.thumb_refcount = 0;
   15181       eh->plt.maybe_thumb_refcount = 0;
   15182       eh->plt.noncall_refcount = 0;
   15183     }
   15184 
   15185   /* If this is a weak symbol, and there is a real definition, the
   15186      processor independent code will have arranged for us to see the
   15187      real definition first, and we can just use the same value.  */
   15188   if (h->is_weakalias)
   15189     {
   15190       struct elf_link_hash_entry *def = weakdef (h);
   15191       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   15192       h->root.u.def.section = def->root.u.def.section;
   15193       h->root.u.def.value = def->root.u.def.value;
   15194       return TRUE;
   15195     }
   15196 
   15197   /* If there are no non-GOT references, we do not need a copy
   15198      relocation.  */
   15199   if (!h->non_got_ref)
   15200     return TRUE;
   15201 
   15202   /* This is a reference to a symbol defined by a dynamic object which
   15203      is not a function.  */
   15204 
   15205   /* If we are creating a shared library, we must presume that the
   15206      only references to the symbol are via the global offset table.
   15207      For such cases we need not do anything here; the relocations will
   15208      be handled correctly by relocate_section.  Relocatable executables
   15209      can reference data in shared objects directly, so we don't need to
   15210      do anything here.  */
   15211   if (bfd_link_pic (info) || globals->root.is_relocatable_executable)
   15212     return TRUE;
   15213 
   15214   /* We must allocate the symbol in our .dynbss section, which will
   15215      become part of the .bss section of the executable.  There will be
   15216      an entry for this symbol in the .dynsym section.  The dynamic
   15217      object will contain position independent code, so all references
   15218      from the dynamic object to this symbol will go through the global
   15219      offset table.  The dynamic linker will use the .dynsym entry to
   15220      determine the address it must put in the global offset table, so
   15221      both the dynamic object and the regular object will refer to the
   15222      same memory location for the variable.  */
   15223   /* If allowed, we must generate a R_ARM_COPY reloc to tell the dynamic
   15224      linker to copy the initial value out of the dynamic object and into
   15225      the runtime process image.  We need to remember the offset into the
   15226      .rel(a).bss section we are going to use.  */
   15227   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   15228     {
   15229       s = globals->root.sdynrelro;
   15230       srel = globals->root.sreldynrelro;
   15231     }
   15232   else
   15233     {
   15234       s = globals->root.sdynbss;
   15235       srel = globals->root.srelbss;
   15236     }
   15237   if (info->nocopyreloc == 0
   15238       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   15239       && h->size != 0)
   15240     {
   15241       elf32_arm_allocate_dynrelocs (info, srel, 1);
   15242       h->needs_copy = 1;
   15243     }
   15244 
   15245   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   15246 }
   15247 
   15248 /* Allocate space in .plt, .got and associated reloc sections for
   15249    dynamic relocs.  */
   15250 
   15251 static bfd_boolean
   15252 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
   15253 {
   15254   struct bfd_link_info *info;
   15255   struct elf32_arm_link_hash_table *htab;
   15256   struct elf32_arm_link_hash_entry *eh;
   15257   struct elf_dyn_relocs *p;
   15258 
   15259   if (h->root.type == bfd_link_hash_indirect)
   15260     return TRUE;
   15261 
   15262   eh = (struct elf32_arm_link_hash_entry *) h;
   15263 
   15264   info = (struct bfd_link_info *) inf;
   15265   htab = elf32_arm_hash_table (info);
   15266   if (htab == NULL)
   15267     return FALSE;
   15268 
   15269   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
   15270       && h->plt.refcount > 0)
   15271     {
   15272       /* Make sure this symbol is output as a dynamic symbol.
   15273 	 Undefined weak syms won't yet be marked as dynamic.  */
   15274       if (h->dynindx == -1 && !h->forced_local
   15275 	  && h->root.type == bfd_link_hash_undefweak)
   15276 	{
   15277 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   15278 	    return FALSE;
   15279 	}
   15280 
   15281       /* If the call in the PLT entry binds locally, the associated
   15282 	 GOT entry should use an R_ARM_IRELATIVE relocation instead of
   15283 	 the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
   15284 	 than the .plt section.  */
   15285       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
   15286 	{
   15287 	  eh->is_iplt = 1;
   15288 	  if (eh->plt.noncall_refcount == 0
   15289 	      && SYMBOL_REFERENCES_LOCAL (info, h))
   15290 	    /* All non-call references can be resolved directly.
   15291 	       This means that they can (and in some cases, must)
   15292 	       resolve directly to the run-time target, rather than
   15293 	       to the PLT.  That in turns means that any .got entry
   15294 	       would be equal to the .igot.plt entry, so there's
   15295 	       no point having both.  */
   15296 	    h->got.refcount = 0;
   15297 	}
   15298 
   15299       if (bfd_link_pic (info)
   15300 	  || eh->is_iplt
   15301 	  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
   15302 	{
   15303 	  elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
   15304 
   15305 	  /* If this symbol is not defined in a regular file, and we are
   15306 	     not generating a shared library, then set the symbol to this
   15307 	     location in the .plt.  This is required to make function
   15308 	     pointers compare as equal between the normal executable and
   15309 	     the shared library.  */
   15310 	  if (! bfd_link_pic (info)
   15311 	      && !h->def_regular)
   15312 	    {
   15313 	      h->root.u.def.section = htab->root.splt;
   15314 	      h->root.u.def.value = h->plt.offset;
   15315 
   15316 	      /* Make sure the function is not marked as Thumb, in case
   15317 		 it is the target of an ABS32 relocation, which will
   15318 		 point to the PLT entry.  */
   15319 	      ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
   15320 	    }
   15321 
   15322 	  /* VxWorks executables have a second set of relocations for
   15323 	     each PLT entry.  They go in a separate relocation section,
   15324 	     which is processed by the kernel loader.  */
   15325 	  if (htab->vxworks_p && !bfd_link_pic (info))
   15326 	    {
   15327 	      /* There is a relocation for the initial PLT entry:
   15328 		 an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
   15329 	      if (h->plt.offset == htab->plt_header_size)
   15330 		elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
   15331 
   15332 	      /* There are two extra relocations for each subsequent
   15333 		 PLT entry: an R_ARM_32 relocation for the GOT entry,
   15334 		 and an R_ARM_32 relocation for the PLT entry.  */
   15335 	      elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
   15336 	    }
   15337 	}
   15338       else
   15339 	{
   15340 	  h->plt.offset = (bfd_vma) -1;
   15341 	  h->needs_plt = 0;
   15342 	}
   15343     }
   15344   else
   15345     {
   15346       h->plt.offset = (bfd_vma) -1;
   15347       h->needs_plt = 0;
   15348     }
   15349 
   15350   eh = (struct elf32_arm_link_hash_entry *) h;
   15351   eh->tlsdesc_got = (bfd_vma) -1;
   15352 
   15353   if (h->got.refcount > 0)
   15354     {
   15355       asection *s;
   15356       bfd_boolean dyn;
   15357       int tls_type = elf32_arm_hash_entry (h)->tls_type;
   15358       int indx;
   15359 
   15360       /* Make sure this symbol is output as a dynamic symbol.
   15361 	 Undefined weak syms won't yet be marked as dynamic.  */
   15362       if (h->dynindx == -1 && !h->forced_local
   15363 	  && h->root.type == bfd_link_hash_undefweak)
   15364 	{
   15365 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   15366 	    return FALSE;
   15367 	}
   15368 
   15369       if (!htab->symbian_p)
   15370 	{
   15371 	  s = htab->root.sgot;
   15372 	  h->got.offset = s->size;
   15373 
   15374 	  if (tls_type == GOT_UNKNOWN)
   15375 	    abort ();
   15376 
   15377 	  if (tls_type == GOT_NORMAL)
   15378 	    /* Non-TLS symbols need one GOT slot.  */
   15379 	    s->size += 4;
   15380 	  else
   15381 	    {
   15382 	      if (tls_type & GOT_TLS_GDESC)
   15383 		{
   15384 		  /* R_ARM_TLS_DESC needs 2 GOT slots.  */
   15385 		  eh->tlsdesc_got
   15386 		    = (htab->root.sgotplt->size
   15387 		       - elf32_arm_compute_jump_table_size (htab));
   15388 		  htab->root.sgotplt->size += 8;
   15389 		  h->got.offset = (bfd_vma) -2;
   15390 		  /* plt.got_offset needs to know there's a TLS_DESC
   15391 		     reloc in the middle of .got.plt.  */
   15392 		  htab->num_tls_desc++;
   15393 		}
   15394 
   15395 	      if (tls_type & GOT_TLS_GD)
   15396 		{
   15397 		  /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  If
   15398 		     the symbol is both GD and GDESC, got.offset may
   15399 		     have been overwritten.  */
   15400 		  h->got.offset = s->size;
   15401 		  s->size += 8;
   15402 		}
   15403 
   15404 	      if (tls_type & GOT_TLS_IE)
   15405 		/* R_ARM_TLS_IE32 needs one GOT slot.  */
   15406 		s->size += 4;
   15407 	    }
   15408 
   15409 	  dyn = htab->root.dynamic_sections_created;
   15410 
   15411 	  indx = 0;
   15412 	  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   15413 					       bfd_link_pic (info),
   15414 					       h)
   15415 	      && (!bfd_link_pic (info)
   15416 		  || !SYMBOL_REFERENCES_LOCAL (info, h)))
   15417 	    indx = h->dynindx;
   15418 
   15419 	  if (tls_type != GOT_NORMAL
   15420 	      && (bfd_link_pic (info) || indx != 0)
   15421 	      && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   15422 		  || h->root.type != bfd_link_hash_undefweak))
   15423 	    {
   15424 	      if (tls_type & GOT_TLS_IE)
   15425 		elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15426 
   15427 	      if (tls_type & GOT_TLS_GD)
   15428 		elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15429 
   15430 	      if (tls_type & GOT_TLS_GDESC)
   15431 		{
   15432 		  elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
   15433 		  /* GDESC needs a trampoline to jump to.  */
   15434 		  htab->tls_trampoline = -1;
   15435 		}
   15436 
   15437 	      /* Only GD needs it.  GDESC just emits one relocation per
   15438 		 2 entries.  */
   15439 	      if ((tls_type & GOT_TLS_GD) && indx != 0)
   15440 		elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15441 	    }
   15442 	  else if (indx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
   15443 	    {
   15444 	      if (htab->root.dynamic_sections_created)
   15445 		/* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
   15446 		elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15447 	    }
   15448 	  else if (h->type == STT_GNU_IFUNC
   15449 		   && eh->plt.noncall_refcount == 0)
   15450 	    /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
   15451 	       they all resolve dynamically instead.  Reserve room for the
   15452 	       GOT entry's R_ARM_IRELATIVE relocation.  */
   15453 	    elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
   15454 	  else if (bfd_link_pic (info)
   15455 		   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   15456 		       || h->root.type != bfd_link_hash_undefweak))
   15457 	    /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
   15458 	    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15459 	}
   15460     }
   15461   else
   15462     h->got.offset = (bfd_vma) -1;
   15463 
   15464   /* Allocate stubs for exported Thumb functions on v4t.  */
   15465   if (!htab->use_blx && h->dynindx != -1
   15466       && h->def_regular
   15467       && ARM_GET_SYM_BRANCH_TYPE (h->target_internal) == ST_BRANCH_TO_THUMB
   15468       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   15469     {
   15470       struct elf_link_hash_entry * th;
   15471       struct bfd_link_hash_entry * bh;
   15472       struct elf_link_hash_entry * myh;
   15473       char name[1024];
   15474       asection *s;
   15475       bh = NULL;
   15476       /* Create a new symbol to regist the real location of the function.  */
   15477       s = h->root.u.def.section;
   15478       sprintf (name, "__real_%s", h->root.root.string);
   15479       _bfd_generic_link_add_one_symbol (info, s->owner,
   15480 					name, BSF_GLOBAL, s,
   15481 					h->root.u.def.value,
   15482 					NULL, TRUE, FALSE, &bh);
   15483 
   15484       myh = (struct elf_link_hash_entry *) bh;
   15485       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   15486       myh->forced_local = 1;
   15487       ARM_SET_SYM_BRANCH_TYPE (myh->target_internal, ST_BRANCH_TO_THUMB);
   15488       eh->export_glue = myh;
   15489       th = record_arm_to_thumb_glue (info, h);
   15490       /* Point the symbol at the stub.  */
   15491       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
   15492       ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
   15493       h->root.u.def.section = th->root.u.def.section;
   15494       h->root.u.def.value = th->root.u.def.value & ~1;
   15495     }
   15496 
   15497   if (eh->dyn_relocs == NULL)
   15498     return TRUE;
   15499 
   15500   /* In the shared -Bsymbolic case, discard space allocated for
   15501      dynamic pc-relative relocs against symbols which turn out to be
   15502      defined in regular objects.  For the normal shared case, discard
   15503      space for pc-relative relocs that have become local due to symbol
   15504      visibility changes.  */
   15505 
   15506   if (bfd_link_pic (info) || htab->root.is_relocatable_executable)
   15507     {
   15508       /* Relocs that use pc_count are PC-relative forms, which will appear
   15509 	 on something like ".long foo - ." or "movw REG, foo - .".  We want
   15510 	 calls to protected symbols to resolve directly to the function
   15511 	 rather than going via the plt.  If people want function pointer
   15512 	 comparisons to work as expected then they should avoid writing
   15513 	 assembly like ".long foo - .".  */
   15514       if (SYMBOL_CALLS_LOCAL (info, h))
   15515 	{
   15516 	  struct elf_dyn_relocs **pp;
   15517 
   15518 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   15519 	    {
   15520 	      p->count -= p->pc_count;
   15521 	      p->pc_count = 0;
   15522 	      if (p->count == 0)
   15523 		*pp = p->next;
   15524 	      else
   15525 		pp = &p->next;
   15526 	    }
   15527 	}
   15528 
   15529       if (htab->vxworks_p)
   15530 	{
   15531 	  struct elf_dyn_relocs **pp;
   15532 
   15533 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   15534 	    {
   15535 	      if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
   15536 		*pp = p->next;
   15537 	      else
   15538 		pp = &p->next;
   15539 	    }
   15540 	}
   15541 
   15542       /* Also discard relocs on undefined weak syms with non-default
   15543 	 visibility.  */
   15544       if (eh->dyn_relocs != NULL
   15545 	  && h->root.type == bfd_link_hash_undefweak)
   15546 	{
   15547 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   15548 	      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   15549 	    eh->dyn_relocs = NULL;
   15550 
   15551 	  /* Make sure undefined weak symbols are output as a dynamic
   15552 	     symbol in PIEs.  */
   15553 	  else if (h->dynindx == -1
   15554 		   && !h->forced_local)
   15555 	    {
   15556 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   15557 		return FALSE;
   15558 	    }
   15559 	}
   15560 
   15561       else if (htab->root.is_relocatable_executable && h->dynindx == -1
   15562 	       && h->root.type == bfd_link_hash_new)
   15563 	{
   15564 	  /* Output absolute symbols so that we can create relocations
   15565 	     against them.  For normal symbols we output a relocation
   15566 	     against the section that contains them.  */
   15567 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   15568 	    return FALSE;
   15569 	}
   15570 
   15571     }
   15572   else
   15573     {
   15574       /* For the non-shared case, discard space for relocs against
   15575 	 symbols which turn out to need copy relocs or are not
   15576 	 dynamic.  */
   15577 
   15578       if (!h->non_got_ref
   15579 	  && ((h->def_dynamic
   15580 	       && !h->def_regular)
   15581 	      || (htab->root.dynamic_sections_created
   15582 		  && (h->root.type == bfd_link_hash_undefweak
   15583 		      || h->root.type == bfd_link_hash_undefined))))
   15584 	{
   15585 	  /* Make sure this symbol is output as a dynamic symbol.
   15586 	     Undefined weak syms won't yet be marked as dynamic.  */
   15587 	  if (h->dynindx == -1 && !h->forced_local
   15588 	      && h->root.type == bfd_link_hash_undefweak)
   15589 	    {
   15590 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   15591 		return FALSE;
   15592 	    }
   15593 
   15594 	  /* If that succeeded, we know we'll be keeping all the
   15595 	     relocs.  */
   15596 	  if (h->dynindx != -1)
   15597 	    goto keep;
   15598 	}
   15599 
   15600       eh->dyn_relocs = NULL;
   15601 
   15602     keep: ;
   15603     }
   15604 
   15605   /* Finally, allocate space.  */
   15606   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   15607     {
   15608       asection *sreloc = elf_section_data (p->sec)->sreloc;
   15609       if (h->type == STT_GNU_IFUNC
   15610 	  && eh->plt.noncall_refcount == 0
   15611 	  && SYMBOL_REFERENCES_LOCAL (info, h))
   15612 	elf32_arm_allocate_irelocs (info, sreloc, p->count);
   15613       else
   15614 	elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
   15615     }
   15616 
   15617   return TRUE;
   15618 }
   15619 
   15620 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   15621    read-only sections.  */
   15622 
   15623 static bfd_boolean
   15624 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
   15625 {
   15626   asection *sec;
   15627 
   15628   if (h->root.type == bfd_link_hash_indirect)
   15629     return TRUE;
   15630 
   15631   sec = readonly_dynrelocs (h);
   15632   if (sec != NULL)
   15633     {
   15634       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
   15635 
   15636       info->flags |= DF_TEXTREL;
   15637       info->callbacks->minfo
   15638 	(_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
   15639 	 sec->owner, h->root.root.string, sec);
   15640 
   15641       /* Not an error, just cut short the traversal.  */
   15642       return FALSE;
   15643     }
   15644   return TRUE;
   15645 }
   15646 
   15647 void
   15648 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
   15649 				 int byteswap_code)
   15650 {
   15651   struct elf32_arm_link_hash_table *globals;
   15652 
   15653   globals = elf32_arm_hash_table (info);
   15654   if (globals == NULL)
   15655     return;
   15656 
   15657   globals->byteswap_code = byteswap_code;
   15658 }
   15659 
   15660 /* Set the sizes of the dynamic sections.  */
   15661 
   15662 static bfd_boolean
   15663 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
   15664 				 struct bfd_link_info * info)
   15665 {
   15666   bfd * dynobj;
   15667   asection * s;
   15668   bfd_boolean plt;
   15669   bfd_boolean relocs;
   15670   bfd *ibfd;
   15671   struct elf32_arm_link_hash_table *htab;
   15672 
   15673   htab = elf32_arm_hash_table (info);
   15674   if (htab == NULL)
   15675     return FALSE;
   15676 
   15677   dynobj = elf_hash_table (info)->dynobj;
   15678   BFD_ASSERT (dynobj != NULL);
   15679   check_use_blx (htab);
   15680 
   15681   if (elf_hash_table (info)->dynamic_sections_created)
   15682     {
   15683       /* Set the contents of the .interp section to the interpreter.  */
   15684       if (bfd_link_executable (info) && !info->nointerp)
   15685 	{
   15686 	  s = bfd_get_linker_section (dynobj, ".interp");
   15687 	  BFD_ASSERT (s != NULL);
   15688 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   15689 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   15690 	}
   15691     }
   15692 
   15693   /* Set up .got offsets for local syms, and space for local dynamic
   15694      relocs.  */
   15695   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   15696     {
   15697       bfd_signed_vma *local_got;
   15698       bfd_signed_vma *end_local_got;
   15699       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
   15700       char *local_tls_type;
   15701       bfd_vma *local_tlsdesc_gotent;
   15702       bfd_size_type locsymcount;
   15703       Elf_Internal_Shdr *symtab_hdr;
   15704       asection *srel;
   15705       bfd_boolean is_vxworks = htab->vxworks_p;
   15706       unsigned int symndx;
   15707 
   15708       if (! is_arm_elf (ibfd))
   15709 	continue;
   15710 
   15711       for (s = ibfd->sections; s != NULL; s = s->next)
   15712 	{
   15713 	  struct elf_dyn_relocs *p;
   15714 
   15715 	  for (p = (struct elf_dyn_relocs *)
   15716 		   elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   15717 	    {
   15718 	      if (!bfd_is_abs_section (p->sec)
   15719 		  && bfd_is_abs_section (p->sec->output_section))
   15720 		{
   15721 		  /* Input section has been discarded, either because
   15722 		     it is a copy of a linkonce section or due to
   15723 		     linker script /DISCARD/, so we'll be discarding
   15724 		     the relocs too.  */
   15725 		}
   15726 	      else if (is_vxworks
   15727 		       && strcmp (p->sec->output_section->name,
   15728 				  ".tls_vars") == 0)
   15729 		{
   15730 		  /* Relocations in vxworks .tls_vars sections are
   15731 		     handled specially by the loader.  */
   15732 		}
   15733 	      else if (p->count != 0)
   15734 		{
   15735 		  srel = elf_section_data (p->sec)->sreloc;
   15736 		  elf32_arm_allocate_dynrelocs (info, srel, p->count);
   15737 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   15738 		    info->flags |= DF_TEXTREL;
   15739 		}
   15740 	    }
   15741 	}
   15742 
   15743       local_got = elf_local_got_refcounts (ibfd);
   15744       if (!local_got)
   15745 	continue;
   15746 
   15747       symtab_hdr = & elf_symtab_hdr (ibfd);
   15748       locsymcount = symtab_hdr->sh_info;
   15749       end_local_got = local_got + locsymcount;
   15750       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
   15751       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
   15752       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
   15753       symndx = 0;
   15754       s = htab->root.sgot;
   15755       srel = htab->root.srelgot;
   15756       for (; local_got < end_local_got;
   15757 	   ++local_got, ++local_iplt_ptr, ++local_tls_type,
   15758 	   ++local_tlsdesc_gotent, ++symndx)
   15759 	{
   15760 	  *local_tlsdesc_gotent = (bfd_vma) -1;
   15761 	  local_iplt = *local_iplt_ptr;
   15762 	  if (local_iplt != NULL)
   15763 	    {
   15764 	      struct elf_dyn_relocs *p;
   15765 
   15766 	      if (local_iplt->root.refcount > 0)
   15767 		{
   15768 		  elf32_arm_allocate_plt_entry (info, TRUE,
   15769 						&local_iplt->root,
   15770 						&local_iplt->arm);
   15771 		  if (local_iplt->arm.noncall_refcount == 0)
   15772 		    /* All references to the PLT are calls, so all
   15773 		       non-call references can resolve directly to the
   15774 		       run-time target.  This means that the .got entry
   15775 		       would be the same as the .igot.plt entry, so there's
   15776 		       no point creating both.  */
   15777 		    *local_got = 0;
   15778 		}
   15779 	      else
   15780 		{
   15781 		  BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
   15782 		  local_iplt->root.offset = (bfd_vma) -1;
   15783 		}
   15784 
   15785 	      for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
   15786 		{
   15787 		  asection *psrel;
   15788 
   15789 		  psrel = elf_section_data (p->sec)->sreloc;
   15790 		  if (local_iplt->arm.noncall_refcount == 0)
   15791 		    elf32_arm_allocate_irelocs (info, psrel, p->count);
   15792 		  else
   15793 		    elf32_arm_allocate_dynrelocs (info, psrel, p->count);
   15794 		}
   15795 	    }
   15796 	  if (*local_got > 0)
   15797 	    {
   15798 	      Elf_Internal_Sym *isym;
   15799 
   15800 	      *local_got = s->size;
   15801 	      if (*local_tls_type & GOT_TLS_GD)
   15802 		/* TLS_GD relocs need an 8-byte structure in the GOT.  */
   15803 		s->size += 8;
   15804 	      if (*local_tls_type & GOT_TLS_GDESC)
   15805 		{
   15806 		  *local_tlsdesc_gotent = htab->root.sgotplt->size
   15807 		    - elf32_arm_compute_jump_table_size (htab);
   15808 		  htab->root.sgotplt->size += 8;
   15809 		  *local_got = (bfd_vma) -2;
   15810 		  /* plt.got_offset needs to know there's a TLS_DESC
   15811 		     reloc in the middle of .got.plt.  */
   15812 		  htab->num_tls_desc++;
   15813 		}
   15814 	      if (*local_tls_type & GOT_TLS_IE)
   15815 		s->size += 4;
   15816 
   15817 	      if (*local_tls_type & GOT_NORMAL)
   15818 		{
   15819 		  /* If the symbol is both GD and GDESC, *local_got
   15820 		     may have been overwritten.  */
   15821 		  *local_got = s->size;
   15822 		  s->size += 4;
   15823 		}
   15824 
   15825 	      isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
   15826 	      if (isym == NULL)
   15827 		return FALSE;
   15828 
   15829 	      /* If all references to an STT_GNU_IFUNC PLT are calls,
   15830 		 then all non-call references, including this GOT entry,
   15831 		 resolve directly to the run-time target.  */
   15832 	      if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   15833 		  && (local_iplt == NULL
   15834 		      || local_iplt->arm.noncall_refcount == 0))
   15835 		elf32_arm_allocate_irelocs (info, srel, 1);
   15836 	      else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC)
   15837 		{
   15838 		  if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC))
   15839 		      || *local_tls_type & GOT_TLS_GD)
   15840 		    elf32_arm_allocate_dynrelocs (info, srel, 1);
   15841 
   15842 		  if (bfd_link_pic (info) && *local_tls_type & GOT_TLS_GDESC)
   15843 		    {
   15844 		      elf32_arm_allocate_dynrelocs (info,
   15845 						    htab->root.srelplt, 1);
   15846 		      htab->tls_trampoline = -1;
   15847 		    }
   15848 		}
   15849 	    }
   15850 	  else
   15851 	    *local_got = (bfd_vma) -1;
   15852 	}
   15853     }
   15854 
   15855   if (htab->tls_ldm_got.refcount > 0)
   15856     {
   15857       /* Allocate two GOT entries and one dynamic relocation (if necessary)
   15858 	 for R_ARM_TLS_LDM32 relocations.  */
   15859       htab->tls_ldm_got.offset = htab->root.sgot->size;
   15860       htab->root.sgot->size += 8;
   15861       if (bfd_link_pic (info))
   15862 	elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
   15863     }
   15864   else
   15865     htab->tls_ldm_got.offset = -1;
   15866 
   15867   /* Allocate global sym .plt and .got entries, and space for global
   15868      sym dynamic relocs.  */
   15869   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
   15870 
   15871   /* Here we rummage through the found bfds to collect glue information.  */
   15872   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   15873     {
   15874       if (! is_arm_elf (ibfd))
   15875 	continue;
   15876 
   15877       /* Initialise mapping tables for code/data.  */
   15878       bfd_elf32_arm_init_maps (ibfd);
   15879 
   15880       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
   15881 	  || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
   15882 	  || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
   15883 	_bfd_error_handler (_("Errors encountered processing file %B"), ibfd);
   15884     }
   15885 
   15886   /* Allocate space for the glue sections now that we've sized them.  */
   15887   bfd_elf32_arm_allocate_interworking_sections (info);
   15888 
   15889   /* For every jump slot reserved in the sgotplt, reloc_count is
   15890      incremented.  However, when we reserve space for TLS descriptors,
   15891      it's not incremented, so in order to compute the space reserved
   15892      for them, it suffices to multiply the reloc count by the jump
   15893      slot size.  */
   15894   if (htab->root.srelplt)
   15895     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
   15896 
   15897   if (htab->tls_trampoline)
   15898     {
   15899       if (htab->root.splt->size == 0)
   15900 	htab->root.splt->size += htab->plt_header_size;
   15901 
   15902       htab->tls_trampoline = htab->root.splt->size;
   15903       htab->root.splt->size += htab->plt_entry_size;
   15904 
   15905       /* If we're not using lazy TLS relocations, don't generate the
   15906 	 PLT and GOT entries they require.  */
   15907       if (!(info->flags & DF_BIND_NOW))
   15908 	{
   15909 	  htab->dt_tlsdesc_got = htab->root.sgot->size;
   15910 	  htab->root.sgot->size += 4;
   15911 
   15912 	  htab->dt_tlsdesc_plt = htab->root.splt->size;
   15913 	  htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
   15914 	}
   15915     }
   15916 
   15917   /* The check_relocs and adjust_dynamic_symbol entry points have
   15918      determined the sizes of the various dynamic sections.  Allocate
   15919      memory for them.  */
   15920   plt = FALSE;
   15921   relocs = FALSE;
   15922   for (s = dynobj->sections; s != NULL; s = s->next)
   15923     {
   15924       const char * name;
   15925 
   15926       if ((s->flags & SEC_LINKER_CREATED) == 0)
   15927 	continue;
   15928 
   15929       /* It's OK to base decisions on the section name, because none
   15930 	 of the dynobj section names depend upon the input files.  */
   15931       name = bfd_get_section_name (dynobj, s);
   15932 
   15933       if (s == htab->root.splt)
   15934 	{
   15935 	  /* Remember whether there is a PLT.  */
   15936 	  plt = s->size != 0;
   15937 	}
   15938       else if (CONST_STRNEQ (name, ".rel"))
   15939 	{
   15940 	  if (s->size != 0)
   15941 	    {
   15942 	      /* Remember whether there are any reloc sections other
   15943 		 than .rel(a).plt and .rela.plt.unloaded.  */
   15944 	      if (s != htab->root.srelplt && s != htab->srelplt2)
   15945 		relocs = TRUE;
   15946 
   15947 	      /* We use the reloc_count field as a counter if we need
   15948 		 to copy relocs into the output file.  */
   15949 	      s->reloc_count = 0;
   15950 	    }
   15951 	}
   15952       else if (s != htab->root.sgot
   15953 	       && s != htab->root.sgotplt
   15954 	       && s != htab->root.iplt
   15955 	       && s != htab->root.igotplt
   15956 	       && s != htab->root.sdynbss
   15957 	       && s != htab->root.sdynrelro)
   15958 	{
   15959 	  /* It's not one of our sections, so don't allocate space.  */
   15960 	  continue;
   15961 	}
   15962 
   15963       if (s->size == 0)
   15964 	{
   15965 	  /* If we don't need this section, strip it from the
   15966 	     output file.  This is mostly to handle .rel(a).bss and
   15967 	     .rel(a).plt.  We must create both sections in
   15968 	     create_dynamic_sections, because they must be created
   15969 	     before the linker maps input sections to output
   15970 	     sections.  The linker does that before
   15971 	     adjust_dynamic_symbol is called, and it is that
   15972 	     function which decides whether anything needs to go
   15973 	     into these sections.  */
   15974 	  s->flags |= SEC_EXCLUDE;
   15975 	  continue;
   15976 	}
   15977 
   15978       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   15979 	continue;
   15980 
   15981       /* Allocate memory for the section contents.  */
   15982       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
   15983       if (s->contents == NULL)
   15984 	return FALSE;
   15985     }
   15986 
   15987   if (elf_hash_table (info)->dynamic_sections_created)
   15988     {
   15989       /* Add some entries to the .dynamic section.  We fill in the
   15990 	 values later, in elf32_arm_finish_dynamic_sections, but we
   15991 	 must add the entries now so that we get the correct size for
   15992 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   15993 	 dynamic linker and used by the debugger.  */
   15994 #define add_dynamic_entry(TAG, VAL) \
   15995   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   15996 
   15997      if (bfd_link_executable (info))
   15998 	{
   15999 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   16000 	    return FALSE;
   16001 	}
   16002 
   16003       if (plt)
   16004 	{
   16005 	  if (   !add_dynamic_entry (DT_PLTGOT, 0)
   16006 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   16007 	      || !add_dynamic_entry (DT_PLTREL,
   16008 				     htab->use_rel ? DT_REL : DT_RELA)
   16009 	      || !add_dynamic_entry (DT_JMPREL, 0))
   16010 	    return FALSE;
   16011 
   16012 	  if (htab->dt_tlsdesc_plt
   16013 	      && (!add_dynamic_entry (DT_TLSDESC_PLT,0)
   16014 		  || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
   16015 	    return FALSE;
   16016 	}
   16017 
   16018       if (relocs)
   16019 	{
   16020 	  if (htab->use_rel)
   16021 	    {
   16022 	      if (!add_dynamic_entry (DT_REL, 0)
   16023 		  || !add_dynamic_entry (DT_RELSZ, 0)
   16024 		  || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
   16025 		return FALSE;
   16026 	    }
   16027 	  else
   16028 	    {
   16029 	      if (!add_dynamic_entry (DT_RELA, 0)
   16030 		  || !add_dynamic_entry (DT_RELASZ, 0)
   16031 		  || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
   16032 		return FALSE;
   16033 	    }
   16034 	}
   16035 
   16036       /* If any dynamic relocs apply to a read-only section,
   16037 	 then we need a DT_TEXTREL entry.  */
   16038       if ((info->flags & DF_TEXTREL) == 0)
   16039 	elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
   16040 
   16041       if ((info->flags & DF_TEXTREL) != 0)
   16042 	{
   16043 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
   16044 	    return FALSE;
   16045 	}
   16046       if (htab->vxworks_p
   16047 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   16048 	return FALSE;
   16049     }
   16050 #undef add_dynamic_entry
   16051 
   16052   return TRUE;
   16053 }
   16054 
   16055 /* Size sections even though they're not dynamic.  We use it to setup
   16056    _TLS_MODULE_BASE_, if needed.  */
   16057 
   16058 static bfd_boolean
   16059 elf32_arm_always_size_sections (bfd *output_bfd,
   16060 				struct bfd_link_info *info)
   16061 {
   16062   asection *tls_sec;
   16063 
   16064   if (bfd_link_relocatable (info))
   16065     return TRUE;
   16066 
   16067   tls_sec = elf_hash_table (info)->tls_sec;
   16068 
   16069   if (tls_sec)
   16070     {
   16071       struct elf_link_hash_entry *tlsbase;
   16072 
   16073       tlsbase = elf_link_hash_lookup
   16074 	(elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
   16075 
   16076       if (tlsbase)
   16077 	{
   16078 	  struct bfd_link_hash_entry *bh = NULL;
   16079 	  const struct elf_backend_data *bed
   16080 	    = get_elf_backend_data (output_bfd);
   16081 
   16082 	  if (!(_bfd_generic_link_add_one_symbol
   16083 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
   16084 		 tls_sec, 0, NULL, FALSE,
   16085 		 bed->collect, &bh)))
   16086 	    return FALSE;
   16087 
   16088 	  tlsbase->type = STT_TLS;
   16089 	  tlsbase = (struct elf_link_hash_entry *)bh;
   16090 	  tlsbase->def_regular = 1;
   16091 	  tlsbase->other = STV_HIDDEN;
   16092 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
   16093 	}
   16094     }
   16095   return TRUE;
   16096 }
   16097 
   16098 /* Finish up dynamic symbol handling.  We set the contents of various
   16099    dynamic sections here.  */
   16100 
   16101 static bfd_boolean
   16102 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
   16103 				 struct bfd_link_info * info,
   16104 				 struct elf_link_hash_entry * h,
   16105 				 Elf_Internal_Sym * sym)
   16106 {
   16107   struct elf32_arm_link_hash_table *htab;
   16108   struct elf32_arm_link_hash_entry *eh;
   16109 
   16110   htab = elf32_arm_hash_table (info);
   16111   if (htab == NULL)
   16112     return FALSE;
   16113 
   16114   eh = (struct elf32_arm_link_hash_entry *) h;
   16115 
   16116   if (h->plt.offset != (bfd_vma) -1)
   16117     {
   16118       if (!eh->is_iplt)
   16119 	{
   16120 	  BFD_ASSERT (h->dynindx != -1);
   16121 	  if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
   16122 					      h->dynindx, 0))
   16123 	    return FALSE;
   16124 	}
   16125 
   16126       if (!h->def_regular)
   16127 	{
   16128 	  /* Mark the symbol as undefined, rather than as defined in
   16129 	     the .plt section.  */
   16130 	  sym->st_shndx = SHN_UNDEF;
   16131 	  /* If the symbol is weak we need to clear the value.
   16132 	     Otherwise, the PLT entry would provide a definition for
   16133 	     the symbol even if the symbol wasn't defined anywhere,
   16134 	     and so the symbol would never be NULL.  Leave the value if
   16135 	     there were any relocations where pointer equality matters
   16136 	     (this is a clue for the dynamic linker, to make function
   16137 	     pointer comparisons work between an application and shared
   16138 	     library).  */
   16139 	  if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
   16140 	    sym->st_value = 0;
   16141 	}
   16142       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
   16143 	{
   16144 	  /* At least one non-call relocation references this .iplt entry,
   16145 	     so the .iplt entry is the function's canonical address.  */
   16146 	  sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
   16147 	  ARM_SET_SYM_BRANCH_TYPE (sym->st_target_internal, ST_BRANCH_TO_ARM);
   16148 	  sym->st_shndx = (_bfd_elf_section_from_bfd_section
   16149 			   (output_bfd, htab->root.iplt->output_section));
   16150 	  sym->st_value = (h->plt.offset
   16151 			   + htab->root.iplt->output_section->vma
   16152 			   + htab->root.iplt->output_offset);
   16153 	}
   16154     }
   16155 
   16156   if (h->needs_copy)
   16157     {
   16158       asection * s;
   16159       Elf_Internal_Rela rel;
   16160 
   16161       /* This symbol needs a copy reloc.  Set it up.  */
   16162       BFD_ASSERT (h->dynindx != -1
   16163 		  && (h->root.type == bfd_link_hash_defined
   16164 		      || h->root.type == bfd_link_hash_defweak));
   16165 
   16166       rel.r_addend = 0;
   16167       rel.r_offset = (h->root.u.def.value
   16168 		      + h->root.u.def.section->output_section->vma
   16169 		      + h->root.u.def.section->output_offset);
   16170       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
   16171       if (h->root.u.def.section == htab->root.sdynrelro)
   16172 	s = htab->root.sreldynrelro;
   16173       else
   16174 	s = htab->root.srelbss;
   16175       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
   16176     }
   16177 
   16178   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
   16179      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
   16180      to the ".got" section.  */
   16181   if (h == htab->root.hdynamic
   16182       || (!htab->vxworks_p && h == htab->root.hgot))
   16183     sym->st_shndx = SHN_ABS;
   16184 
   16185   return TRUE;
   16186 }
   16187 
   16188 static void
   16189 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
   16190 		    void *contents,
   16191 		    const unsigned long *template, unsigned count)
   16192 {
   16193   unsigned ix;
   16194 
   16195   for (ix = 0; ix != count; ix++)
   16196     {
   16197       unsigned long insn = template[ix];
   16198 
   16199       /* Emit mov pc,rx if bx is not permitted.  */
   16200       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
   16201 	insn = (insn & 0xf000000f) | 0x01a0f000;
   16202       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
   16203     }
   16204 }
   16205 
   16206 /* Install the special first PLT entry for elf32-arm-nacl.  Unlike
   16207    other variants, NaCl needs this entry in a static executable's
   16208    .iplt too.  When we're handling that case, GOT_DISPLACEMENT is
   16209    zero.  For .iplt really only the last bundle is useful, and .iplt
   16210    could have a shorter first entry, with each individual PLT entry's
   16211    relative branch calculated differently so it targets the last
   16212    bundle instead of the instruction before it (labelled .Lplt_tail
   16213    above).  But it's simpler to keep the size and layout of PLT0
   16214    consistent with the dynamic case, at the cost of some dead code at
   16215    the start of .iplt and the one dead store to the stack at the start
   16216    of .Lplt_tail.  */
   16217 static void
   16218 arm_nacl_put_plt0 (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
   16219 		   asection *plt, bfd_vma got_displacement)
   16220 {
   16221   unsigned int i;
   16222 
   16223   put_arm_insn (htab, output_bfd,
   16224 		elf32_arm_nacl_plt0_entry[0]
   16225 		| arm_movw_immediate (got_displacement),
   16226 		plt->contents + 0);
   16227   put_arm_insn (htab, output_bfd,
   16228 		elf32_arm_nacl_plt0_entry[1]
   16229 		| arm_movt_immediate (got_displacement),
   16230 		plt->contents + 4);
   16231 
   16232   for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
   16233     put_arm_insn (htab, output_bfd,
   16234 		  elf32_arm_nacl_plt0_entry[i],
   16235 		  plt->contents + (i * 4));
   16236 }
   16237 
   16238 /* Finish up the dynamic sections.  */
   16239 
   16240 static bfd_boolean
   16241 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
   16242 {
   16243   bfd * dynobj;
   16244   asection * sgot;
   16245   asection * sdyn;
   16246   struct elf32_arm_link_hash_table *htab;
   16247 
   16248   htab = elf32_arm_hash_table (info);
   16249   if (htab == NULL)
   16250     return FALSE;
   16251 
   16252   dynobj = elf_hash_table (info)->dynobj;
   16253 
   16254   sgot = htab->root.sgotplt;
   16255   /* A broken linker script might have discarded the dynamic sections.
   16256      Catch this here so that we do not seg-fault later on.  */
   16257   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
   16258     return FALSE;
   16259   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   16260 
   16261   if (elf_hash_table (info)->dynamic_sections_created)
   16262     {
   16263       asection *splt;
   16264       Elf32_External_Dyn *dyncon, *dynconend;
   16265 
   16266       splt = htab->root.splt;
   16267       BFD_ASSERT (splt != NULL && sdyn != NULL);
   16268       BFD_ASSERT (htab->symbian_p || sgot != NULL);
   16269 
   16270       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   16271       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   16272 
   16273       for (; dyncon < dynconend; dyncon++)
   16274 	{
   16275 	  Elf_Internal_Dyn dyn;
   16276 	  const char * name;
   16277 	  asection * s;
   16278 
   16279 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   16280 
   16281 	  switch (dyn.d_tag)
   16282 	    {
   16283 	      unsigned int type;
   16284 
   16285 	    default:
   16286 	      if (htab->vxworks_p
   16287 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   16288 		bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16289 	      break;
   16290 
   16291 	    case DT_HASH:
   16292 	      name = ".hash";
   16293 	      goto get_vma_if_bpabi;
   16294 	    case DT_STRTAB:
   16295 	      name = ".dynstr";
   16296 	      goto get_vma_if_bpabi;
   16297 	    case DT_SYMTAB:
   16298 	      name = ".dynsym";
   16299 	      goto get_vma_if_bpabi;
   16300 	    case DT_VERSYM:
   16301 	      name = ".gnu.version";
   16302 	      goto get_vma_if_bpabi;
   16303 	    case DT_VERDEF:
   16304 	      name = ".gnu.version_d";
   16305 	      goto get_vma_if_bpabi;
   16306 	    case DT_VERNEED:
   16307 	      name = ".gnu.version_r";
   16308 	      goto get_vma_if_bpabi;
   16309 
   16310 	    case DT_PLTGOT:
   16311 	      name = htab->symbian_p ? ".got" : ".got.plt";
   16312 	      goto get_vma;
   16313 	    case DT_JMPREL:
   16314 	      name = RELOC_SECTION (htab, ".plt");
   16315 	    get_vma:
   16316 	      s = bfd_get_linker_section (dynobj, name);
   16317 	      if (s == NULL)
   16318 		{
   16319 		  _bfd_error_handler
   16320 		    (_("could not find section %s"), name);
   16321 		  bfd_set_error (bfd_error_invalid_operation);
   16322 		  return FALSE;
   16323 		}
   16324 	      if (!htab->symbian_p)
   16325 		dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   16326 	      else
   16327 		/* In the BPABI, tags in the PT_DYNAMIC section point
   16328 		   at the file offset, not the memory address, for the
   16329 		   convenience of the post linker.  */
   16330 		dyn.d_un.d_ptr = s->output_section->filepos + s->output_offset;
   16331 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16332 	      break;
   16333 
   16334 	    get_vma_if_bpabi:
   16335 	      if (htab->symbian_p)
   16336 		goto get_vma;
   16337 	      break;
   16338 
   16339 	    case DT_PLTRELSZ:
   16340 	      s = htab->root.srelplt;
   16341 	      BFD_ASSERT (s != NULL);
   16342 	      dyn.d_un.d_val = s->size;
   16343 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16344 	      break;
   16345 
   16346 	    case DT_RELSZ:
   16347 	    case DT_RELASZ:
   16348 	    case DT_REL:
   16349 	    case DT_RELA:
   16350 	      /* In the BPABI, the DT_REL tag must point at the file
   16351 		 offset, not the VMA, of the first relocation
   16352 		 section.  So, we use code similar to that in
   16353 		 elflink.c, but do not check for SHF_ALLOC on the
   16354 		 relocation section, since relocation sections are
   16355 		 never allocated under the BPABI.  PLT relocs are also
   16356 		 included.  */
   16357 	      if (htab->symbian_p)
   16358 		{
   16359 		  unsigned int i;
   16360 		  type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   16361 			  ? SHT_REL : SHT_RELA);
   16362 		  dyn.d_un.d_val = 0;
   16363 		  for (i = 1; i < elf_numsections (output_bfd); i++)
   16364 		    {
   16365 		      Elf_Internal_Shdr *hdr
   16366 			= elf_elfsections (output_bfd)[i];
   16367 		      if (hdr->sh_type == type)
   16368 			{
   16369 			  if (dyn.d_tag == DT_RELSZ
   16370 			      || dyn.d_tag == DT_RELASZ)
   16371 			    dyn.d_un.d_val += hdr->sh_size;
   16372 			  else if ((ufile_ptr) hdr->sh_offset
   16373 				   <= dyn.d_un.d_val - 1)
   16374 			    dyn.d_un.d_val = hdr->sh_offset;
   16375 			}
   16376 		    }
   16377 		  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16378 		}
   16379 	      break;
   16380 
   16381 	    case DT_TLSDESC_PLT:
   16382 	      s = htab->root.splt;
   16383 	      dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   16384 				+ htab->dt_tlsdesc_plt);
   16385 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16386 	      break;
   16387 
   16388 	    case DT_TLSDESC_GOT:
   16389 	      s = htab->root.sgot;
   16390 	      dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   16391 				+ htab->dt_tlsdesc_got);
   16392 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16393 	      break;
   16394 
   16395 	      /* Set the bottom bit of DT_INIT/FINI if the
   16396 		 corresponding function is Thumb.  */
   16397 	    case DT_INIT:
   16398 	      name = info->init_function;
   16399 	      goto get_sym;
   16400 	    case DT_FINI:
   16401 	      name = info->fini_function;
   16402 	    get_sym:
   16403 	      /* If it wasn't set by elf_bfd_final_link
   16404 		 then there is nothing to adjust.  */
   16405 	      if (dyn.d_un.d_val != 0)
   16406 		{
   16407 		  struct elf_link_hash_entry * eh;
   16408 
   16409 		  eh = elf_link_hash_lookup (elf_hash_table (info), name,
   16410 					     FALSE, FALSE, TRUE);
   16411 		  if (eh != NULL
   16412 		      && ARM_GET_SYM_BRANCH_TYPE (eh->target_internal)
   16413 			 == ST_BRANCH_TO_THUMB)
   16414 		    {
   16415 		      dyn.d_un.d_val |= 1;
   16416 		      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   16417 		    }
   16418 		}
   16419 	      break;
   16420 	    }
   16421 	}
   16422 
   16423       /* Fill in the first entry in the procedure linkage table.  */
   16424       if (splt->size > 0 && htab->plt_header_size)
   16425 	{
   16426 	  const bfd_vma *plt0_entry;
   16427 	  bfd_vma got_address, plt_address, got_displacement;
   16428 
   16429 	  /* Calculate the addresses of the GOT and PLT.  */
   16430 	  got_address = sgot->output_section->vma + sgot->output_offset;
   16431 	  plt_address = splt->output_section->vma + splt->output_offset;
   16432 
   16433 	  if (htab->vxworks_p)
   16434 	    {
   16435 	      /* The VxWorks GOT is relocated by the dynamic linker.
   16436 		 Therefore, we must emit relocations rather than simply
   16437 		 computing the values now.  */
   16438 	      Elf_Internal_Rela rel;
   16439 
   16440 	      plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
   16441 	      put_arm_insn (htab, output_bfd, plt0_entry[0],
   16442 			    splt->contents + 0);
   16443 	      put_arm_insn (htab, output_bfd, plt0_entry[1],
   16444 			    splt->contents + 4);
   16445 	      put_arm_insn (htab, output_bfd, plt0_entry[2],
   16446 			    splt->contents + 8);
   16447 	      bfd_put_32 (output_bfd, got_address, splt->contents + 12);
   16448 
   16449 	      /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
   16450 	      rel.r_offset = plt_address + 12;
   16451 	      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
   16452 	      rel.r_addend = 0;
   16453 	      SWAP_RELOC_OUT (htab) (output_bfd, &rel,
   16454 				     htab->srelplt2->contents);
   16455 	    }
   16456 	  else if (htab->nacl_p)
   16457 	    arm_nacl_put_plt0 (htab, output_bfd, splt,
   16458 			       got_address + 8 - (plt_address + 16));
   16459 	  else if (using_thumb_only (htab))
   16460 	    {
   16461 	      got_displacement = got_address - (plt_address + 12);
   16462 
   16463 	      plt0_entry = elf32_thumb2_plt0_entry;
   16464 	      put_arm_insn (htab, output_bfd, plt0_entry[0],
   16465 			    splt->contents + 0);
   16466 	      put_arm_insn (htab, output_bfd, plt0_entry[1],
   16467 			    splt->contents + 4);
   16468 	      put_arm_insn (htab, output_bfd, plt0_entry[2],
   16469 			    splt->contents + 8);
   16470 
   16471 	      bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
   16472 	    }
   16473 	  else
   16474 	    {
   16475 	      got_displacement = got_address - (plt_address + 16);
   16476 
   16477 	      plt0_entry = elf32_arm_plt0_entry;
   16478 	      put_arm_insn (htab, output_bfd, plt0_entry[0],
   16479 			    splt->contents + 0);
   16480 	      put_arm_insn (htab, output_bfd, plt0_entry[1],
   16481 			    splt->contents + 4);
   16482 	      put_arm_insn (htab, output_bfd, plt0_entry[2],
   16483 			    splt->contents + 8);
   16484 	      put_arm_insn (htab, output_bfd, plt0_entry[3],
   16485 			    splt->contents + 12);
   16486 
   16487 #ifdef FOUR_WORD_PLT
   16488 	      /* The displacement value goes in the otherwise-unused
   16489 		 last word of the second entry.  */
   16490 	      bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
   16491 #else
   16492 	      bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
   16493 #endif
   16494 	    }
   16495 	}
   16496 
   16497       /* UnixWare sets the entsize of .plt to 4, although that doesn't
   16498 	 really seem like the right value.  */
   16499       if (splt->output_section->owner == output_bfd)
   16500 	elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
   16501 
   16502       if (htab->dt_tlsdesc_plt)
   16503 	{
   16504 	  bfd_vma got_address
   16505 	    = sgot->output_section->vma + sgot->output_offset;
   16506 	  bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
   16507 				    + htab->root.sgot->output_offset);
   16508 	  bfd_vma plt_address
   16509 	    = splt->output_section->vma + splt->output_offset;
   16510 
   16511 	  arm_put_trampoline (htab, output_bfd,
   16512 			      splt->contents + htab->dt_tlsdesc_plt,
   16513 			      dl_tlsdesc_lazy_trampoline, 6);
   16514 
   16515 	  bfd_put_32 (output_bfd,
   16516 		      gotplt_address + htab->dt_tlsdesc_got
   16517 		      - (plt_address + htab->dt_tlsdesc_plt)
   16518 		      - dl_tlsdesc_lazy_trampoline[6],
   16519 		      splt->contents + htab->dt_tlsdesc_plt + 24);
   16520 	  bfd_put_32 (output_bfd,
   16521 		      got_address - (plt_address + htab->dt_tlsdesc_plt)
   16522 		      - dl_tlsdesc_lazy_trampoline[7],
   16523 		      splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
   16524 	}
   16525 
   16526       if (htab->tls_trampoline)
   16527 	{
   16528 	  arm_put_trampoline (htab, output_bfd,
   16529 			      splt->contents + htab->tls_trampoline,
   16530 			      tls_trampoline, 3);
   16531 #ifdef FOUR_WORD_PLT
   16532 	  bfd_put_32 (output_bfd, 0x00000000,
   16533 		      splt->contents + htab->tls_trampoline + 12);
   16534 #endif
   16535 	}
   16536 
   16537       if (htab->vxworks_p
   16538 	  && !bfd_link_pic (info)
   16539 	  && htab->root.splt->size > 0)
   16540 	{
   16541 	  /* Correct the .rel(a).plt.unloaded relocations.  They will have
   16542 	     incorrect symbol indexes.  */
   16543 	  int num_plts;
   16544 	  unsigned char *p;
   16545 
   16546 	  num_plts = ((htab->root.splt->size - htab->plt_header_size)
   16547 		      / htab->plt_entry_size);
   16548 	  p = htab->srelplt2->contents + RELOC_SIZE (htab);
   16549 
   16550 	  for (; num_plts; num_plts--)
   16551 	    {
   16552 	      Elf_Internal_Rela rel;
   16553 
   16554 	      SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
   16555 	      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
   16556 	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
   16557 	      p += RELOC_SIZE (htab);
   16558 
   16559 	      SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
   16560 	      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
   16561 	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
   16562 	      p += RELOC_SIZE (htab);
   16563 	    }
   16564 	}
   16565     }
   16566 
   16567   if (htab->nacl_p && htab->root.iplt != NULL && htab->root.iplt->size > 0)
   16568     /* NaCl uses a special first entry in .iplt too.  */
   16569     arm_nacl_put_plt0 (htab, output_bfd, htab->root.iplt, 0);
   16570 
   16571   /* Fill in the first three entries in the global offset table.  */
   16572   if (sgot)
   16573     {
   16574       if (sgot->size > 0)
   16575 	{
   16576 	  if (sdyn == NULL)
   16577 	    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   16578 	  else
   16579 	    bfd_put_32 (output_bfd,
   16580 			sdyn->output_section->vma + sdyn->output_offset,
   16581 			sgot->contents);
   16582 	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   16583 	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   16584 	}
   16585 
   16586       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   16587     }
   16588 
   16589   return TRUE;
   16590 }
   16591 
   16592 static void
   16593 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
   16594 {
   16595   Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
   16596   struct elf32_arm_link_hash_table *globals;
   16597   struct elf_segment_map *m;
   16598 
   16599   i_ehdrp = elf_elfheader (abfd);
   16600 
   16601   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
   16602     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
   16603   else
   16604     _bfd_elf_post_process_headers (abfd, link_info);
   16605   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
   16606 
   16607   if (link_info)
   16608     {
   16609       globals = elf32_arm_hash_table (link_info);
   16610       if (globals != NULL && globals->byteswap_code)
   16611 	i_ehdrp->e_flags |= EF_ARM_BE8;
   16612     }
   16613 
   16614   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
   16615       && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
   16616     {
   16617       int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
   16618       if (abi == AEABI_VFP_args_vfp)
   16619 	i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
   16620       else
   16621 	i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
   16622     }
   16623 
   16624   /* Scan segment to set p_flags attribute if it contains only sections with
   16625      SHF_ARM_PURECODE flag.  */
   16626   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   16627     {
   16628       unsigned int j;
   16629 
   16630       if (m->count == 0)
   16631 	continue;
   16632       for (j = 0; j < m->count; j++)
   16633 	{
   16634 	  if (!(elf_section_flags (m->sections[j]) & SHF_ARM_PURECODE))
   16635 	    break;
   16636 	}
   16637       if (j == m->count)
   16638 	{
   16639 	  m->p_flags = PF_X;
   16640 	  m->p_flags_valid = 1;
   16641 	}
   16642     }
   16643 }
   16644 
   16645 static enum elf_reloc_type_class
   16646 elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   16647 			    const asection *rel_sec ATTRIBUTE_UNUSED,
   16648 			    const Elf_Internal_Rela *rela)
   16649 {
   16650   switch ((int) ELF32_R_TYPE (rela->r_info))
   16651     {
   16652     case R_ARM_RELATIVE:
   16653       return reloc_class_relative;
   16654     case R_ARM_JUMP_SLOT:
   16655       return reloc_class_plt;
   16656     case R_ARM_COPY:
   16657       return reloc_class_copy;
   16658     case R_ARM_IRELATIVE:
   16659       return reloc_class_ifunc;
   16660     default:
   16661       return reloc_class_normal;
   16662     }
   16663 }
   16664 
   16665 static void
   16666 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
   16667 {
   16668   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
   16669 }
   16670 
   16671 /* Return TRUE if this is an unwinding table entry.  */
   16672 
   16673 static bfd_boolean
   16674 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
   16675 {
   16676   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
   16677 	  || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
   16678 }
   16679 
   16680 
   16681 /* Set the type and flags for an ARM section.  We do this by
   16682    the section name, which is a hack, but ought to work.  */
   16683 
   16684 static bfd_boolean
   16685 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
   16686 {
   16687   const char * name;
   16688 
   16689   name = bfd_get_section_name (abfd, sec);
   16690 
   16691   if (is_arm_elf_unwind_section_name (abfd, name))
   16692     {
   16693       hdr->sh_type = SHT_ARM_EXIDX;
   16694       hdr->sh_flags |= SHF_LINK_ORDER;
   16695     }
   16696 
   16697   if (sec->flags & SEC_ELF_PURECODE)
   16698     hdr->sh_flags |= SHF_ARM_PURECODE;
   16699 
   16700   return TRUE;
   16701 }
   16702 
   16703 /* Handle an ARM specific section when reading an object file.  This is
   16704    called when bfd_section_from_shdr finds a section with an unknown
   16705    type.  */
   16706 
   16707 static bfd_boolean
   16708 elf32_arm_section_from_shdr (bfd *abfd,
   16709 			     Elf_Internal_Shdr * hdr,
   16710 			     const char *name,
   16711 			     int shindex)
   16712 {
   16713   /* There ought to be a place to keep ELF backend specific flags, but
   16714      at the moment there isn't one.  We just keep track of the
   16715      sections by their name, instead.  Fortunately, the ABI gives
   16716      names for all the ARM specific sections, so we will probably get
   16717      away with this.  */
   16718   switch (hdr->sh_type)
   16719     {
   16720     case SHT_ARM_EXIDX:
   16721     case SHT_ARM_PREEMPTMAP:
   16722     case SHT_ARM_ATTRIBUTES:
   16723       break;
   16724 
   16725     default:
   16726       return FALSE;
   16727     }
   16728 
   16729   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   16730     return FALSE;
   16731 
   16732   return TRUE;
   16733 }
   16734 
   16735 static _arm_elf_section_data *
   16736 get_arm_elf_section_data (asection * sec)
   16737 {
   16738   if (sec && sec->owner && is_arm_elf (sec->owner))
   16739     return elf32_arm_section_data (sec);
   16740   else
   16741     return NULL;
   16742 }
   16743 
   16744 typedef struct
   16745 {
   16746   void *flaginfo;
   16747   struct bfd_link_info *info;
   16748   asection *sec;
   16749   int sec_shndx;
   16750   int (*func) (void *, const char *, Elf_Internal_Sym *,
   16751 	       asection *, struct elf_link_hash_entry *);
   16752 } output_arch_syminfo;
   16753 
   16754 enum map_symbol_type
   16755 {
   16756   ARM_MAP_ARM,
   16757   ARM_MAP_THUMB,
   16758   ARM_MAP_DATA
   16759 };
   16760 
   16761 
   16762 /* Output a single mapping symbol.  */
   16763 
   16764 static bfd_boolean
   16765 elf32_arm_output_map_sym (output_arch_syminfo *osi,
   16766 			  enum map_symbol_type type,
   16767 			  bfd_vma offset)
   16768 {
   16769   static const char *names[3] = {"$a", "$t", "$d"};
   16770   Elf_Internal_Sym sym;
   16771 
   16772   sym.st_value = osi->sec->output_section->vma
   16773 		 + osi->sec->output_offset
   16774 		 + offset;
   16775   sym.st_size = 0;
   16776   sym.st_other = 0;
   16777   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
   16778   sym.st_shndx = osi->sec_shndx;
   16779   sym.st_target_internal = 0;
   16780   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
   16781   return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
   16782 }
   16783 
   16784 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
   16785    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
   16786 
   16787 static bfd_boolean
   16788 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
   16789 			    bfd_boolean is_iplt_entry_p,
   16790 			    union gotplt_union *root_plt,
   16791 			    struct arm_plt_info *arm_plt)
   16792 {
   16793   struct elf32_arm_link_hash_table *htab;
   16794   bfd_vma addr, plt_header_size;
   16795 
   16796   if (root_plt->offset == (bfd_vma) -1)
   16797     return TRUE;
   16798 
   16799   htab = elf32_arm_hash_table (osi->info);
   16800   if (htab == NULL)
   16801     return FALSE;
   16802 
   16803   if (is_iplt_entry_p)
   16804     {
   16805       osi->sec = htab->root.iplt;
   16806       plt_header_size = 0;
   16807     }
   16808   else
   16809     {
   16810       osi->sec = htab->root.splt;
   16811       plt_header_size = htab->plt_header_size;
   16812     }
   16813   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
   16814 		    (osi->info->output_bfd, osi->sec->output_section));
   16815 
   16816   addr = root_plt->offset & -2;
   16817   if (htab->symbian_p)
   16818     {
   16819       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
   16820 	return FALSE;
   16821       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
   16822 	return FALSE;
   16823     }
   16824   else if (htab->vxworks_p)
   16825     {
   16826       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
   16827 	return FALSE;
   16828       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
   16829 	return FALSE;
   16830       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
   16831 	return FALSE;
   16832       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
   16833 	return FALSE;
   16834     }
   16835   else if (htab->nacl_p)
   16836     {
   16837       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
   16838 	return FALSE;
   16839     }
   16840   else if (using_thumb_only (htab))
   16841     {
   16842       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
   16843 	return FALSE;
   16844     }
   16845   else
   16846     {
   16847       bfd_boolean thumb_stub_p;
   16848 
   16849       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
   16850       if (thumb_stub_p)
   16851 	{
   16852 	  if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
   16853 	    return FALSE;
   16854 	}
   16855 #ifdef FOUR_WORD_PLT
   16856       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
   16857 	return FALSE;
   16858       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
   16859 	return FALSE;
   16860 #else
   16861       /* A three-word PLT with no Thumb thunk contains only Arm code,
   16862 	 so only need to output a mapping symbol for the first PLT entry and
   16863 	 entries with thumb thunks.  */
   16864       if (thumb_stub_p || addr == plt_header_size)
   16865 	{
   16866 	  if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
   16867 	    return FALSE;
   16868 	}
   16869 #endif
   16870     }
   16871 
   16872   return TRUE;
   16873 }
   16874 
   16875 /* Output mapping symbols for PLT entries associated with H.  */
   16876 
   16877 static bfd_boolean
   16878 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
   16879 {
   16880   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
   16881   struct elf32_arm_link_hash_entry *eh;
   16882 
   16883   if (h->root.type == bfd_link_hash_indirect)
   16884     return TRUE;
   16885 
   16886   if (h->root.type == bfd_link_hash_warning)
   16887     /* When warning symbols are created, they **replace** the "real"
   16888        entry in the hash table, thus we never get to see the real
   16889        symbol in a hash traversal.  So look at it now.  */
   16890     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   16891 
   16892   eh = (struct elf32_arm_link_hash_entry *) h;
   16893   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
   16894 				     &h->plt, &eh->plt);
   16895 }
   16896 
   16897 /* Bind a veneered symbol to its veneer identified by its hash entry
   16898    STUB_ENTRY.  The veneered location thus loose its symbol.  */
   16899 
   16900 static void
   16901 arm_stub_claim_sym (struct elf32_arm_stub_hash_entry *stub_entry)
   16902 {
   16903   struct elf32_arm_link_hash_entry *hash = stub_entry->h;
   16904 
   16905   BFD_ASSERT (hash);
   16906   hash->root.root.u.def.section = stub_entry->stub_sec;
   16907   hash->root.root.u.def.value = stub_entry->stub_offset;
   16908   hash->root.size = stub_entry->stub_size;
   16909 }
   16910 
   16911 /* Output a single local symbol for a generated stub.  */
   16912 
   16913 static bfd_boolean
   16914 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
   16915 			   bfd_vma offset, bfd_vma size)
   16916 {
   16917   Elf_Internal_Sym sym;
   16918 
   16919   sym.st_value = osi->sec->output_section->vma
   16920 		 + osi->sec->output_offset
   16921 		 + offset;
   16922   sym.st_size = size;
   16923   sym.st_other = 0;
   16924   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   16925   sym.st_shndx = osi->sec_shndx;
   16926   sym.st_target_internal = 0;
   16927   return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
   16928 }
   16929 
   16930 static bfd_boolean
   16931 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
   16932 		  void * in_arg)
   16933 {
   16934   struct elf32_arm_stub_hash_entry *stub_entry;
   16935   asection *stub_sec;
   16936   bfd_vma addr;
   16937   char *stub_name;
   16938   output_arch_syminfo *osi;
   16939   const insn_sequence *template_sequence;
   16940   enum stub_insn_type prev_type;
   16941   int size;
   16942   int i;
   16943   enum map_symbol_type sym_type;
   16944 
   16945   /* Massage our args to the form they really have.  */
   16946   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
   16947   osi = (output_arch_syminfo *) in_arg;
   16948 
   16949   stub_sec = stub_entry->stub_sec;
   16950 
   16951   /* Ensure this stub is attached to the current section being
   16952      processed.  */
   16953   if (stub_sec != osi->sec)
   16954     return TRUE;
   16955 
   16956   addr = (bfd_vma) stub_entry->stub_offset;
   16957   template_sequence = stub_entry->stub_template;
   16958 
   16959   if (arm_stub_sym_claimed (stub_entry->stub_type))
   16960     arm_stub_claim_sym (stub_entry);
   16961   else
   16962     {
   16963       stub_name = stub_entry->output_name;
   16964       switch (template_sequence[0].type)
   16965 	{
   16966 	case ARM_TYPE:
   16967 	  if (!elf32_arm_output_stub_sym (osi, stub_name, addr,
   16968 					  stub_entry->stub_size))
   16969 	    return FALSE;
   16970 	  break;
   16971 	case THUMB16_TYPE:
   16972 	case THUMB32_TYPE:
   16973 	  if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
   16974 					  stub_entry->stub_size))
   16975 	    return FALSE;
   16976 	  break;
   16977 	default:
   16978 	  BFD_FAIL ();
   16979 	  return 0;
   16980 	}
   16981     }
   16982 
   16983   prev_type = DATA_TYPE;
   16984   size = 0;
   16985   for (i = 0; i < stub_entry->stub_template_size; i++)
   16986     {
   16987       switch (template_sequence[i].type)
   16988 	{
   16989 	case ARM_TYPE:
   16990 	  sym_type = ARM_MAP_ARM;
   16991 	  break;
   16992 
   16993 	case THUMB16_TYPE:
   16994 	case THUMB32_TYPE:
   16995 	  sym_type = ARM_MAP_THUMB;
   16996 	  break;
   16997 
   16998 	case DATA_TYPE:
   16999 	  sym_type = ARM_MAP_DATA;
   17000 	  break;
   17001 
   17002 	default:
   17003 	  BFD_FAIL ();
   17004 	  return FALSE;
   17005 	}
   17006 
   17007       if (template_sequence[i].type != prev_type)
   17008 	{
   17009 	  prev_type = template_sequence[i].type;
   17010 	  if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
   17011 	    return FALSE;
   17012 	}
   17013 
   17014       switch (template_sequence[i].type)
   17015 	{
   17016 	case ARM_TYPE:
   17017 	case THUMB32_TYPE:
   17018 	  size += 4;
   17019 	  break;
   17020 
   17021 	case THUMB16_TYPE:
   17022 	  size += 2;
   17023 	  break;
   17024 
   17025 	case DATA_TYPE:
   17026 	  size += 4;
   17027 	  break;
   17028 
   17029 	default:
   17030 	  BFD_FAIL ();
   17031 	  return FALSE;
   17032 	}
   17033     }
   17034 
   17035   return TRUE;
   17036 }
   17037 
   17038 /* Output mapping symbols for linker generated sections,
   17039    and for those data-only sections that do not have a
   17040    $d.  */
   17041 
   17042 static bfd_boolean
   17043 elf32_arm_output_arch_local_syms (bfd *output_bfd,
   17044 				  struct bfd_link_info *info,
   17045 				  void *flaginfo,
   17046 				  int (*func) (void *, const char *,
   17047 					       Elf_Internal_Sym *,
   17048 					       asection *,
   17049 					       struct elf_link_hash_entry *))
   17050 {
   17051   output_arch_syminfo osi;
   17052   struct elf32_arm_link_hash_table *htab;
   17053   bfd_vma offset;
   17054   bfd_size_type size;
   17055   bfd *input_bfd;
   17056 
   17057   htab = elf32_arm_hash_table (info);
   17058   if (htab == NULL)
   17059     return FALSE;
   17060 
   17061   check_use_blx (htab);
   17062 
   17063   osi.flaginfo = flaginfo;
   17064   osi.info = info;
   17065   osi.func = func;
   17066 
   17067   /* Add a $d mapping symbol to data-only sections that
   17068      don't have any mapping symbol.  This may result in (harmless) redundant
   17069      mapping symbols.  */
   17070   for (input_bfd = info->input_bfds;
   17071        input_bfd != NULL;
   17072        input_bfd = input_bfd->link.next)
   17073     {
   17074       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
   17075 	for (osi.sec = input_bfd->sections;
   17076 	     osi.sec != NULL;
   17077 	     osi.sec = osi.sec->next)
   17078 	  {
   17079 	    if (osi.sec->output_section != NULL
   17080 		&& ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
   17081 		    != 0)
   17082 		&& (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
   17083 		   == SEC_HAS_CONTENTS
   17084 		&& get_arm_elf_section_data (osi.sec) != NULL
   17085 		&& get_arm_elf_section_data (osi.sec)->mapcount == 0
   17086 		&& osi.sec->size > 0
   17087 		&& (osi.sec->flags & SEC_EXCLUDE) == 0)
   17088 	      {
   17089 		osi.sec_shndx = _bfd_elf_section_from_bfd_section
   17090 		  (output_bfd, osi.sec->output_section);
   17091 		if (osi.sec_shndx != (int)SHN_BAD)
   17092 		  elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
   17093 	      }
   17094 	  }
   17095     }
   17096 
   17097   /* ARM->Thumb glue.  */
   17098   if (htab->arm_glue_size > 0)
   17099     {
   17100       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
   17101 					ARM2THUMB_GLUE_SECTION_NAME);
   17102 
   17103       osi.sec_shndx = _bfd_elf_section_from_bfd_section
   17104 	  (output_bfd, osi.sec->output_section);
   17105       if (bfd_link_pic (info) || htab->root.is_relocatable_executable
   17106 	  || htab->pic_veneer)
   17107 	size = ARM2THUMB_PIC_GLUE_SIZE;
   17108       else if (htab->use_blx)
   17109 	size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
   17110       else
   17111 	size = ARM2THUMB_STATIC_GLUE_SIZE;
   17112 
   17113       for (offset = 0; offset < htab->arm_glue_size; offset += size)
   17114 	{
   17115 	  elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
   17116 	  elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
   17117 	}
   17118     }
   17119 
   17120   /* Thumb->ARM glue.  */
   17121   if (htab->thumb_glue_size > 0)
   17122     {
   17123       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
   17124 					THUMB2ARM_GLUE_SECTION_NAME);
   17125 
   17126       osi.sec_shndx = _bfd_elf_section_from_bfd_section
   17127 	  (output_bfd, osi.sec->output_section);
   17128       size = THUMB2ARM_GLUE_SIZE;
   17129 
   17130       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
   17131 	{
   17132 	  elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
   17133 	  elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
   17134 	}
   17135     }
   17136 
   17137   /* ARMv4 BX veneers.  */
   17138   if (htab->bx_glue_size > 0)
   17139     {
   17140       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
   17141 					ARM_BX_GLUE_SECTION_NAME);
   17142 
   17143       osi.sec_shndx = _bfd_elf_section_from_bfd_section
   17144 	  (output_bfd, osi.sec->output_section);
   17145 
   17146       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
   17147     }
   17148 
   17149   /* Long calls stubs.  */
   17150   if (htab->stub_bfd && htab->stub_bfd->sections)
   17151     {
   17152       asection* stub_sec;
   17153 
   17154       for (stub_sec = htab->stub_bfd->sections;
   17155 	   stub_sec != NULL;
   17156 	   stub_sec = stub_sec->next)
   17157 	{
   17158 	  /* Ignore non-stub sections.  */
   17159 	  if (!strstr (stub_sec->name, STUB_SUFFIX))
   17160 	    continue;
   17161 
   17162 	  osi.sec = stub_sec;
   17163 
   17164 	  osi.sec_shndx = _bfd_elf_section_from_bfd_section
   17165 	    (output_bfd, osi.sec->output_section);
   17166 
   17167 	  bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
   17168 	}
   17169     }
   17170 
   17171   /* Finally, output mapping symbols for the PLT.  */
   17172   if (htab->root.splt && htab->root.splt->size > 0)
   17173     {
   17174       osi.sec = htab->root.splt;
   17175       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
   17176 		       (output_bfd, osi.sec->output_section));
   17177 
   17178       /* Output mapping symbols for the plt header.  SymbianOS does not have a
   17179 	 plt header.  */
   17180       if (htab->vxworks_p)
   17181 	{
   17182 	  /* VxWorks shared libraries have no PLT header.  */
   17183 	  if (!bfd_link_pic (info))
   17184 	    {
   17185 	      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
   17186 		return FALSE;
   17187 	      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
   17188 		return FALSE;
   17189 	    }
   17190 	}
   17191       else if (htab->nacl_p)
   17192 	{
   17193 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
   17194 	    return FALSE;
   17195 	}
   17196       else if (using_thumb_only (htab))
   17197 	{
   17198 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
   17199 	    return FALSE;
   17200 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
   17201 	    return FALSE;
   17202 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
   17203 	    return FALSE;
   17204 	}
   17205       else if (!htab->symbian_p)
   17206 	{
   17207 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
   17208 	    return FALSE;
   17209 #ifndef FOUR_WORD_PLT
   17210 	  if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
   17211 	    return FALSE;
   17212 #endif
   17213 	}
   17214     }
   17215   if (htab->nacl_p && htab->root.iplt && htab->root.iplt->size > 0)
   17216     {
   17217       /* NaCl uses a special first entry in .iplt too.  */
   17218       osi.sec = htab->root.iplt;
   17219       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
   17220 		       (output_bfd, osi.sec->output_section));
   17221       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
   17222 	return FALSE;
   17223     }
   17224   if ((htab->root.splt && htab->root.splt->size > 0)
   17225       || (htab->root.iplt && htab->root.iplt->size > 0))
   17226     {
   17227       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
   17228       for (input_bfd = info->input_bfds;
   17229 	   input_bfd != NULL;
   17230 	   input_bfd = input_bfd->link.next)
   17231 	{
   17232 	  struct arm_local_iplt_info **local_iplt;
   17233 	  unsigned int i, num_syms;
   17234 
   17235 	  local_iplt = elf32_arm_local_iplt (input_bfd);
   17236 	  if (local_iplt != NULL)
   17237 	    {
   17238 	      num_syms = elf_symtab_hdr (input_bfd).sh_info;
   17239 	      for (i = 0; i < num_syms; i++)
   17240 		if (local_iplt[i] != NULL
   17241 		    && !elf32_arm_output_plt_map_1 (&osi, TRUE,
   17242 						    &local_iplt[i]->root,
   17243 						    &local_iplt[i]->arm))
   17244 		  return FALSE;
   17245 	    }
   17246 	}
   17247     }
   17248   if (htab->dt_tlsdesc_plt != 0)
   17249     {
   17250       /* Mapping symbols for the lazy tls trampoline.  */
   17251       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
   17252 	return FALSE;
   17253 
   17254       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
   17255 				     htab->dt_tlsdesc_plt + 24))
   17256 	return FALSE;
   17257     }
   17258   if (htab->tls_trampoline != 0)
   17259     {
   17260       /* Mapping symbols for the tls trampoline.  */
   17261       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
   17262 	return FALSE;
   17263 #ifdef FOUR_WORD_PLT
   17264       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
   17265 				     htab->tls_trampoline + 12))
   17266 	return FALSE;
   17267 #endif
   17268     }
   17269 
   17270   return TRUE;
   17271 }
   17272 
   17273 /* Filter normal symbols of CMSE entry functions of ABFD to include in
   17274    the import library.  All SYMCOUNT symbols of ABFD can be examined
   17275    from their pointers in SYMS.  Pointers of symbols to keep should be
   17276    stored continuously at the beginning of that array.
   17277 
   17278    Returns the number of symbols to keep.  */
   17279 
   17280 static unsigned int
   17281 elf32_arm_filter_cmse_symbols (bfd *abfd ATTRIBUTE_UNUSED,
   17282 			       struct bfd_link_info *info,
   17283 			       asymbol **syms, long symcount)
   17284 {
   17285   size_t maxnamelen;
   17286   char *cmse_name;
   17287   long src_count, dst_count = 0;
   17288   struct elf32_arm_link_hash_table *htab;
   17289 
   17290   htab = elf32_arm_hash_table (info);
   17291   if (!htab->stub_bfd || !htab->stub_bfd->sections)
   17292     symcount = 0;
   17293 
   17294   maxnamelen = 128;
   17295   cmse_name = (char *) bfd_malloc (maxnamelen);
   17296   for (src_count = 0; src_count < symcount; src_count++)
   17297     {
   17298       struct elf32_arm_link_hash_entry *cmse_hash;
   17299       asymbol *sym;
   17300       flagword flags;
   17301       char *name;
   17302       size_t namelen;
   17303 
   17304       sym = syms[src_count];
   17305       flags = sym->flags;
   17306       name = (char *) bfd_asymbol_name (sym);
   17307 
   17308       if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
   17309 	continue;
   17310       if (!(flags & (BSF_GLOBAL | BSF_WEAK)))
   17311 	continue;
   17312 
   17313       namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
   17314       if (namelen > maxnamelen)
   17315 	{
   17316 	  cmse_name = (char *)
   17317 	    bfd_realloc (cmse_name, namelen);
   17318 	  maxnamelen = namelen;
   17319 	}
   17320       snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
   17321       cmse_hash = (struct elf32_arm_link_hash_entry *)
   17322 	elf_link_hash_lookup (&(htab)->root, cmse_name, FALSE, FALSE, TRUE);
   17323 
   17324       if (!cmse_hash
   17325 	  || (cmse_hash->root.root.type != bfd_link_hash_defined
   17326 	      && cmse_hash->root.root.type != bfd_link_hash_defweak)
   17327 	  || cmse_hash->root.type != STT_FUNC)
   17328 	continue;
   17329 
   17330       if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
   17331 	continue;
   17332 
   17333       syms[dst_count++] = sym;
   17334     }
   17335   free (cmse_name);
   17336 
   17337   syms[dst_count] = NULL;
   17338 
   17339   return dst_count;
   17340 }
   17341 
   17342 /* Filter symbols of ABFD to include in the import library.  All
   17343    SYMCOUNT symbols of ABFD can be examined from their pointers in
   17344    SYMS.  Pointers of symbols to keep should be stored continuously at
   17345    the beginning of that array.
   17346 
   17347    Returns the number of symbols to keep.  */
   17348 
   17349 static unsigned int
   17350 elf32_arm_filter_implib_symbols (bfd *abfd ATTRIBUTE_UNUSED,
   17351 				 struct bfd_link_info *info,
   17352 				 asymbol **syms, long symcount)
   17353 {
   17354   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
   17355 
   17356   /* Requirement 8 of "ARM v8-M Security Extensions: Requirements on
   17357      Development Tools" (ARM-ECM-0359818) mandates Secure Gateway import
   17358      library to be a relocatable object file.  */
   17359   BFD_ASSERT (!(bfd_get_file_flags (info->out_implib_bfd) & EXEC_P));
   17360   if (globals->cmse_implib)
   17361     return elf32_arm_filter_cmse_symbols (abfd, info, syms, symcount);
   17362   else
   17363     return _bfd_elf_filter_global_symbols (abfd, info, syms, symcount);
   17364 }
   17365 
   17366 /* Allocate target specific section data.  */
   17367 
   17368 static bfd_boolean
   17369 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
   17370 {
   17371   if (!sec->used_by_bfd)
   17372     {
   17373       _arm_elf_section_data *sdata;
   17374       bfd_size_type amt = sizeof (*sdata);
   17375 
   17376       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
   17377       if (sdata == NULL)
   17378 	return FALSE;
   17379       sec->used_by_bfd = sdata;
   17380     }
   17381 
   17382   return _bfd_elf_new_section_hook (abfd, sec);
   17383 }
   17384 
   17385 
   17386 /* Used to order a list of mapping symbols by address.  */
   17387 
   17388 static int
   17389 elf32_arm_compare_mapping (const void * a, const void * b)
   17390 {
   17391   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
   17392   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
   17393 
   17394   if (amap->vma > bmap->vma)
   17395     return 1;
   17396   else if (amap->vma < bmap->vma)
   17397     return -1;
   17398   else if (amap->type > bmap->type)
   17399     /* Ensure results do not depend on the host qsort for objects with
   17400        multiple mapping symbols at the same address by sorting on type
   17401        after vma.  */
   17402     return 1;
   17403   else if (amap->type < bmap->type)
   17404     return -1;
   17405   else
   17406     return 0;
   17407 }
   17408 
   17409 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
   17410 
   17411 static unsigned long
   17412 offset_prel31 (unsigned long addr, bfd_vma offset)
   17413 {
   17414   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
   17415 }
   17416 
   17417 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
   17418    relocations.  */
   17419 
   17420 static void
   17421 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
   17422 {
   17423   unsigned long first_word = bfd_get_32 (output_bfd, from);
   17424   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
   17425 
   17426   /* High bit of first word is supposed to be zero.  */
   17427   if ((first_word & 0x80000000ul) == 0)
   17428     first_word = offset_prel31 (first_word, offset);
   17429 
   17430   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
   17431      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
   17432   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
   17433     second_word = offset_prel31 (second_word, offset);
   17434 
   17435   bfd_put_32 (output_bfd, first_word, to);
   17436   bfd_put_32 (output_bfd, second_word, to + 4);
   17437 }
   17438 
   17439 /* Data for make_branch_to_a8_stub().  */
   17440 
   17441 struct a8_branch_to_stub_data
   17442 {
   17443   asection *writing_section;
   17444   bfd_byte *contents;
   17445 };
   17446 
   17447 
   17448 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
   17449    places for a particular section.  */
   17450 
   17451 static bfd_boolean
   17452 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
   17453 		       void *in_arg)
   17454 {
   17455   struct elf32_arm_stub_hash_entry *stub_entry;
   17456   struct a8_branch_to_stub_data *data;
   17457   bfd_byte *contents;
   17458   unsigned long branch_insn;
   17459   bfd_vma veneered_insn_loc, veneer_entry_loc;
   17460   bfd_signed_vma branch_offset;
   17461   bfd *abfd;
   17462   unsigned int loc;
   17463 
   17464   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
   17465   data = (struct a8_branch_to_stub_data *) in_arg;
   17466 
   17467   if (stub_entry->target_section != data->writing_section
   17468       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
   17469     return TRUE;
   17470 
   17471   contents = data->contents;
   17472 
   17473   /* We use target_section as Cortex-A8 erratum workaround stubs are only
   17474      generated when both source and target are in the same section.  */
   17475   veneered_insn_loc = stub_entry->target_section->output_section->vma
   17476 		      + stub_entry->target_section->output_offset
   17477 		      + stub_entry->source_value;
   17478 
   17479   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
   17480 		     + stub_entry->stub_sec->output_offset
   17481 		     + stub_entry->stub_offset;
   17482 
   17483   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
   17484     veneered_insn_loc &= ~3u;
   17485 
   17486   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
   17487 
   17488   abfd = stub_entry->target_section->owner;
   17489   loc = stub_entry->source_value;
   17490 
   17491   /* We attempt to avoid this condition by setting stubs_always_after_branch
   17492      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
   17493      This check is just to be on the safe side...  */
   17494   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
   17495     {
   17496       _bfd_error_handler (_("%B: error: Cortex-A8 erratum stub is "
   17497 			    "allocated in unsafe location"), abfd);
   17498       return FALSE;
   17499     }
   17500 
   17501   switch (stub_entry->stub_type)
   17502     {
   17503     case arm_stub_a8_veneer_b:
   17504     case arm_stub_a8_veneer_b_cond:
   17505       branch_insn = 0xf0009000;
   17506       goto jump24;
   17507 
   17508     case arm_stub_a8_veneer_blx:
   17509       branch_insn = 0xf000e800;
   17510       goto jump24;
   17511 
   17512     case arm_stub_a8_veneer_bl:
   17513       {
   17514 	unsigned int i1, j1, i2, j2, s;
   17515 
   17516 	branch_insn = 0xf000d000;
   17517 
   17518       jump24:
   17519 	if (branch_offset < -16777216 || branch_offset > 16777214)
   17520 	  {
   17521 	    /* There's not much we can do apart from complain if this
   17522 	       happens.  */
   17523 	    _bfd_error_handler (_("%B: error: Cortex-A8 erratum stub out "
   17524 				  "of range (input file too large)"), abfd);
   17525 	    return FALSE;
   17526 	  }
   17527 
   17528 	/* i1 = not(j1 eor s), so:
   17529 	   not i1 = j1 eor s
   17530 	   j1 = (not i1) eor s.  */
   17531 
   17532 	branch_insn |= (branch_offset >> 1) & 0x7ff;
   17533 	branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
   17534 	i2 = (branch_offset >> 22) & 1;
   17535 	i1 = (branch_offset >> 23) & 1;
   17536 	s = (branch_offset >> 24) & 1;
   17537 	j1 = (!i1) ^ s;
   17538 	j2 = (!i2) ^ s;
   17539 	branch_insn |= j2 << 11;
   17540 	branch_insn |= j1 << 13;
   17541 	branch_insn |= s << 26;
   17542       }
   17543       break;
   17544 
   17545     default:
   17546       BFD_FAIL ();
   17547       return FALSE;
   17548     }
   17549 
   17550   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[loc]);
   17551   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[loc + 2]);
   17552 
   17553   return TRUE;
   17554 }
   17555 
   17556 /* Beginning of stm32l4xx work-around.  */
   17557 
   17558 /* Functions encoding instructions necessary for the emission of the
   17559    fix-stm32l4xx-629360.
   17560    Encoding is extracted from the
   17561    ARM (C) Architecture Reference Manual
   17562    ARMv7-A and ARMv7-R edition
   17563    ARM DDI 0406C.b (ID072512).  */
   17564 
   17565 static inline bfd_vma
   17566 create_instruction_branch_absolute (int branch_offset)
   17567 {
   17568   /* A8.8.18 B (A8-334)
   17569      B target_address (Encoding T4).  */
   17570   /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
   17571   /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
   17572   /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
   17573 
   17574   int s = ((branch_offset & 0x1000000) >> 24);
   17575   int j1 = s ^ !((branch_offset & 0x800000) >> 23);
   17576   int j2 = s ^ !((branch_offset & 0x400000) >> 22);
   17577 
   17578   if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
   17579     BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
   17580 
   17581   bfd_vma patched_inst = 0xf0009000
   17582     | s << 26 /* S.  */
   17583     | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
   17584     | j1 << 13 /* J1.  */
   17585     | j2 << 11 /* J2.  */
   17586     | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
   17587 
   17588   return patched_inst;
   17589 }
   17590 
   17591 static inline bfd_vma
   17592 create_instruction_ldmia (int base_reg, int wback, int reg_mask)
   17593 {
   17594   /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
   17595      LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
   17596   bfd_vma patched_inst = 0xe8900000
   17597     | (/*W=*/wback << 21)
   17598     | (base_reg << 16)
   17599     | (reg_mask & 0x0000ffff);
   17600 
   17601   return patched_inst;
   17602 }
   17603 
   17604 static inline bfd_vma
   17605 create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
   17606 {
   17607   /* A8.8.60 LDMDB/LDMEA (A8-402)
   17608      LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
   17609   bfd_vma patched_inst = 0xe9100000
   17610     | (/*W=*/wback << 21)
   17611     | (base_reg << 16)
   17612     | (reg_mask & 0x0000ffff);
   17613 
   17614   return patched_inst;
   17615 }
   17616 
   17617 static inline bfd_vma
   17618 create_instruction_mov (int target_reg, int source_reg)
   17619 {
   17620   /* A8.8.103 MOV (register) (A8-486)
   17621      MOV Rd, Rm (Encoding T1).  */
   17622   bfd_vma patched_inst = 0x4600
   17623     | (target_reg & 0x7)
   17624     | ((target_reg & 0x8) >> 3) << 7
   17625     | (source_reg << 3);
   17626 
   17627   return patched_inst;
   17628 }
   17629 
   17630 static inline bfd_vma
   17631 create_instruction_sub (int target_reg, int source_reg, int value)
   17632 {
   17633   /* A8.8.221 SUB (immediate) (A8-708)
   17634      SUB Rd, Rn, #value (Encoding T3).  */
   17635   bfd_vma patched_inst = 0xf1a00000
   17636     | (target_reg << 8)
   17637     | (source_reg << 16)
   17638     | (/*S=*/0 << 20)
   17639     | ((value & 0x800) >> 11) << 26
   17640     | ((value & 0x700) >>  8) << 12
   17641     | (value & 0x0ff);
   17642 
   17643   return patched_inst;
   17644 }
   17645 
   17646 static inline bfd_vma
   17647 create_instruction_vldmia (int base_reg, int is_dp, int wback, int num_words,
   17648 			   int first_reg)
   17649 {
   17650   /* A8.8.332 VLDM (A8-922)
   17651      VLMD{MODE} Rn{!}, {list} (Encoding T1 or T2).  */
   17652   bfd_vma patched_inst = (is_dp ? 0xec900b00 : 0xec900a00)
   17653     | (/*W=*/wback << 21)
   17654     | (base_reg << 16)
   17655     | (num_words & 0x000000ff)
   17656     | (((unsigned)first_reg >> 1) & 0x0000000f) << 12
   17657     | (first_reg & 0x00000001) << 22;
   17658 
   17659   return patched_inst;
   17660 }
   17661 
   17662 static inline bfd_vma
   17663 create_instruction_vldmdb (int base_reg, int is_dp, int num_words,
   17664 			   int first_reg)
   17665 {
   17666   /* A8.8.332 VLDM (A8-922)
   17667      VLMD{MODE} Rn!, {} (Encoding T1 or T2).  */
   17668   bfd_vma patched_inst = (is_dp ? 0xed300b00 : 0xed300a00)
   17669     | (base_reg << 16)
   17670     | (num_words & 0x000000ff)
   17671     | (((unsigned)first_reg >>1 ) & 0x0000000f) << 12
   17672     | (first_reg & 0x00000001) << 22;
   17673 
   17674   return patched_inst;
   17675 }
   17676 
   17677 static inline bfd_vma
   17678 create_instruction_udf_w (int value)
   17679 {
   17680   /* A8.8.247 UDF (A8-758)
   17681      Undefined (Encoding T2).  */
   17682   bfd_vma patched_inst = 0xf7f0a000
   17683     | (value & 0x00000fff)
   17684     | (value & 0x000f0000) << 16;
   17685 
   17686   return patched_inst;
   17687 }
   17688 
   17689 static inline bfd_vma
   17690 create_instruction_udf (int value)
   17691 {
   17692   /* A8.8.247 UDF (A8-758)
   17693      Undefined (Encoding T1).  */
   17694   bfd_vma patched_inst = 0xde00
   17695     | (value & 0xff);
   17696 
   17697   return patched_inst;
   17698 }
   17699 
   17700 /* Functions writing an instruction in memory, returning the next
   17701    memory position to write to.  */
   17702 
   17703 static inline bfd_byte *
   17704 push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
   17705 		    bfd * output_bfd, bfd_byte *pt, insn32 insn)
   17706 {
   17707   put_thumb2_insn (htab, output_bfd, insn, pt);
   17708   return pt + 4;
   17709 }
   17710 
   17711 static inline bfd_byte *
   17712 push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
   17713 		    bfd * output_bfd, bfd_byte *pt, insn32 insn)
   17714 {
   17715   put_thumb_insn (htab, output_bfd, insn, pt);
   17716   return pt + 2;
   17717 }
   17718 
   17719 /* Function filling up a region in memory with T1 and T2 UDFs taking
   17720    care of alignment.  */
   17721 
   17722 static bfd_byte *
   17723 stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
   17724 			 bfd *			 output_bfd,
   17725 			 const bfd_byte * const	 base_stub_contents,
   17726 			 bfd_byte * const	 from_stub_contents,
   17727 			 const bfd_byte * const	 end_stub_contents)
   17728 {
   17729   bfd_byte *current_stub_contents = from_stub_contents;
   17730 
   17731   /* Fill the remaining of the stub with deterministic contents : UDF
   17732      instructions.
   17733      Check if realignment is needed on modulo 4 frontier using T1, to
   17734      further use T2.  */
   17735   if ((current_stub_contents < end_stub_contents)
   17736       && !((current_stub_contents - base_stub_contents) % 2)
   17737       && ((current_stub_contents - base_stub_contents) % 4))
   17738     current_stub_contents =
   17739       push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
   17740 			  create_instruction_udf (0));
   17741 
   17742   for (; current_stub_contents < end_stub_contents;)
   17743     current_stub_contents =
   17744       push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17745 			  create_instruction_udf_w (0));
   17746 
   17747   return current_stub_contents;
   17748 }
   17749 
   17750 /* Functions writing the stream of instructions equivalent to the
   17751    derived sequence for ldmia, ldmdb, vldm respectively.  */
   17752 
   17753 static void
   17754 stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
   17755 				       bfd * output_bfd,
   17756 				       const insn32 initial_insn,
   17757 				       const bfd_byte *const initial_insn_addr,
   17758 				       bfd_byte *const base_stub_contents)
   17759 {
   17760   int wback = (initial_insn & 0x00200000) >> 21;
   17761   int ri, rn = (initial_insn & 0x000F0000) >> 16;
   17762   int insn_all_registers = initial_insn & 0x0000ffff;
   17763   int insn_low_registers, insn_high_registers;
   17764   int usable_register_mask;
   17765   int nb_registers = elf32_arm_popcount (insn_all_registers);
   17766   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
   17767   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
   17768   bfd_byte *current_stub_contents = base_stub_contents;
   17769 
   17770   BFD_ASSERT (is_thumb2_ldmia (initial_insn));
   17771 
   17772   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
   17773      smaller than 8 registers load sequences that do not cause the
   17774      hardware issue.  */
   17775   if (nb_registers <= 8)
   17776     {
   17777       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
   17778       current_stub_contents =
   17779 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17780 			    initial_insn);
   17781 
   17782       /* B initial_insn_addr+4.  */
   17783       if (!restore_pc)
   17784 	current_stub_contents =
   17785 	  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17786 			      create_instruction_branch_absolute
   17787 			      (initial_insn_addr - current_stub_contents));
   17788 
   17789       /* Fill the remaining of the stub with deterministic contents.  */
   17790       current_stub_contents =
   17791 	stm32l4xx_fill_stub_udf (htab, output_bfd,
   17792 				 base_stub_contents, current_stub_contents,
   17793 				 base_stub_contents +
   17794 				 STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
   17795 
   17796       return;
   17797     }
   17798 
   17799   /* - reg_list[13] == 0.  */
   17800   BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
   17801 
   17802   /* - reg_list[14] & reg_list[15] != 1.  */
   17803   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
   17804 
   17805   /* - if (wback==1) reg_list[rn] == 0.  */
   17806   BFD_ASSERT (!wback || !restore_rn);
   17807 
   17808   /* - nb_registers > 8.  */
   17809   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
   17810 
   17811   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
   17812 
   17813   /* In the following algorithm, we split this wide LDM using 2 LDM insns:
   17814     - One with the 7 lowest registers (register mask 0x007F)
   17815       This LDM will finally contain between 2 and 7 registers
   17816     - One with the 7 highest registers (register mask 0xDF80)
   17817       This ldm will finally contain between 2 and 7 registers.  */
   17818   insn_low_registers = insn_all_registers & 0x007F;
   17819   insn_high_registers = insn_all_registers & 0xDF80;
   17820 
   17821   /* A spare register may be needed during this veneer to temporarily
   17822      handle the base register.  This register will be restored with the
   17823      last LDM operation.
   17824      The usable register may be any general purpose register (that
   17825      excludes PC, SP, LR : register mask is 0x1FFF).  */
   17826   usable_register_mask = 0x1FFF;
   17827 
   17828   /* Generate the stub function.  */
   17829   if (wback)
   17830     {
   17831       /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
   17832       current_stub_contents =
   17833 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17834 			    create_instruction_ldmia
   17835 			    (rn, /*wback=*/1, insn_low_registers));
   17836 
   17837       /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
   17838       current_stub_contents =
   17839 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17840 			    create_instruction_ldmia
   17841 			    (rn, /*wback=*/1, insn_high_registers));
   17842       if (!restore_pc)
   17843 	{
   17844 	  /* B initial_insn_addr+4.  */
   17845 	  current_stub_contents =
   17846 	    push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17847 				create_instruction_branch_absolute
   17848 				(initial_insn_addr - current_stub_contents));
   17849        }
   17850     }
   17851   else /* if (!wback).  */
   17852     {
   17853       ri = rn;
   17854 
   17855       /* If Rn is not part of the high-register-list, move it there.  */
   17856       if (!(insn_high_registers & (1 << rn)))
   17857 	{
   17858 	  /* Choose a Ri in the high-register-list that will be restored.  */
   17859 	  ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
   17860 
   17861 	  /* MOV Ri, Rn.  */
   17862 	  current_stub_contents =
   17863 	    push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
   17864 				create_instruction_mov (ri, rn));
   17865 	}
   17866 
   17867       /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
   17868       current_stub_contents =
   17869 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17870 			    create_instruction_ldmia
   17871 			    (ri, /*wback=*/1, insn_low_registers));
   17872 
   17873       /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
   17874       current_stub_contents =
   17875 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17876 			    create_instruction_ldmia
   17877 			    (ri, /*wback=*/0, insn_high_registers));
   17878 
   17879       if (!restore_pc)
   17880 	{
   17881 	  /* B initial_insn_addr+4.  */
   17882 	  current_stub_contents =
   17883 	    push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17884 				create_instruction_branch_absolute
   17885 				(initial_insn_addr - current_stub_contents));
   17886 	}
   17887     }
   17888 
   17889   /* Fill the remaining of the stub with deterministic contents.  */
   17890   current_stub_contents =
   17891     stm32l4xx_fill_stub_udf (htab, output_bfd,
   17892 			     base_stub_contents, current_stub_contents,
   17893 			     base_stub_contents +
   17894 			     STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
   17895 }
   17896 
   17897 static void
   17898 stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
   17899 				       bfd * output_bfd,
   17900 				       const insn32 initial_insn,
   17901 				       const bfd_byte *const initial_insn_addr,
   17902 				       bfd_byte *const base_stub_contents)
   17903 {
   17904   int wback = (initial_insn & 0x00200000) >> 21;
   17905   int ri, rn = (initial_insn & 0x000f0000) >> 16;
   17906   int insn_all_registers = initial_insn & 0x0000ffff;
   17907   int insn_low_registers, insn_high_registers;
   17908   int usable_register_mask;
   17909   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
   17910   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
   17911   int nb_registers = elf32_arm_popcount (insn_all_registers);
   17912   bfd_byte *current_stub_contents = base_stub_contents;
   17913 
   17914   BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
   17915 
   17916   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
   17917      smaller than 8 registers load sequences that do not cause the
   17918      hardware issue.  */
   17919   if (nb_registers <= 8)
   17920     {
   17921       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
   17922       current_stub_contents =
   17923 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17924 			    initial_insn);
   17925 
   17926       /* B initial_insn_addr+4.  */
   17927       current_stub_contents =
   17928 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17929 			    create_instruction_branch_absolute
   17930 			    (initial_insn_addr - current_stub_contents));
   17931 
   17932       /* Fill the remaining of the stub with deterministic contents.  */
   17933       current_stub_contents =
   17934 	stm32l4xx_fill_stub_udf (htab, output_bfd,
   17935 				 base_stub_contents, current_stub_contents,
   17936 				 base_stub_contents +
   17937 				 STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
   17938 
   17939       return;
   17940     }
   17941 
   17942   /* - reg_list[13] == 0.  */
   17943   BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
   17944 
   17945   /* - reg_list[14] & reg_list[15] != 1.  */
   17946   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
   17947 
   17948   /* - if (wback==1) reg_list[rn] == 0.  */
   17949   BFD_ASSERT (!wback || !restore_rn);
   17950 
   17951   /* - nb_registers > 8.  */
   17952   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
   17953 
   17954   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
   17955 
   17956   /* In the following algorithm, we split this wide LDM using 2 LDM insn:
   17957     - One with the 7 lowest registers (register mask 0x007F)
   17958       This LDM will finally contain between 2 and 7 registers
   17959     - One with the 7 highest registers (register mask 0xDF80)
   17960       This ldm will finally contain between 2 and 7 registers.  */
   17961   insn_low_registers = insn_all_registers & 0x007F;
   17962   insn_high_registers = insn_all_registers & 0xDF80;
   17963 
   17964   /* A spare register may be needed during this veneer to temporarily
   17965      handle the base register.  This register will be restored with
   17966      the last LDM operation.
   17967      The usable register may be any general purpose register (that excludes
   17968      PC, SP, LR : register mask is 0x1FFF).  */
   17969   usable_register_mask = 0x1FFF;
   17970 
   17971   /* Generate the stub function.  */
   17972   if (!wback && !restore_pc && !restore_rn)
   17973     {
   17974       /* Choose a Ri in the low-register-list that will be restored.  */
   17975       ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
   17976 
   17977       /* MOV Ri, Rn.  */
   17978       current_stub_contents =
   17979 	push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
   17980 			    create_instruction_mov (ri, rn));
   17981 
   17982       /* LDMDB Ri!, {R-high-register-list}.  */
   17983       current_stub_contents =
   17984 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17985 			    create_instruction_ldmdb
   17986 			    (ri, /*wback=*/1, insn_high_registers));
   17987 
   17988       /* LDMDB Ri, {R-low-register-list}.  */
   17989       current_stub_contents =
   17990 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17991 			    create_instruction_ldmdb
   17992 			    (ri, /*wback=*/0, insn_low_registers));
   17993 
   17994       /* B initial_insn_addr+4.  */
   17995       current_stub_contents =
   17996 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   17997 			    create_instruction_branch_absolute
   17998 			    (initial_insn_addr - current_stub_contents));
   17999     }
   18000   else if (wback && !restore_pc && !restore_rn)
   18001     {
   18002       /* LDMDB Rn!, {R-high-register-list}.  */
   18003       current_stub_contents =
   18004 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18005 			    create_instruction_ldmdb
   18006 			    (rn, /*wback=*/1, insn_high_registers));
   18007 
   18008       /* LDMDB Rn!, {R-low-register-list}.  */
   18009       current_stub_contents =
   18010 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18011 			    create_instruction_ldmdb
   18012 			    (rn, /*wback=*/1, insn_low_registers));
   18013 
   18014       /* B initial_insn_addr+4.  */
   18015       current_stub_contents =
   18016 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18017 			    create_instruction_branch_absolute
   18018 			    (initial_insn_addr - current_stub_contents));
   18019     }
   18020   else if (!wback && restore_pc && !restore_rn)
   18021     {
   18022       /* Choose a Ri in the high-register-list that will be restored.  */
   18023       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
   18024 
   18025       /* SUB Ri, Rn, #(4*nb_registers).  */
   18026       current_stub_contents =
   18027 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18028 			    create_instruction_sub (ri, rn, (4 * nb_registers)));
   18029 
   18030       /* LDMIA Ri!, {R-low-register-list}.  */
   18031       current_stub_contents =
   18032 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18033 			    create_instruction_ldmia
   18034 			    (ri, /*wback=*/1, insn_low_registers));
   18035 
   18036       /* LDMIA Ri, {R-high-register-list}.  */
   18037       current_stub_contents =
   18038 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18039 			    create_instruction_ldmia
   18040 			    (ri, /*wback=*/0, insn_high_registers));
   18041     }
   18042   else if (wback && restore_pc && !restore_rn)
   18043     {
   18044       /* Choose a Ri in the high-register-list that will be restored.  */
   18045       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
   18046 
   18047       /* SUB Rn, Rn, #(4*nb_registers)  */
   18048       current_stub_contents =
   18049 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18050 			    create_instruction_sub (rn, rn, (4 * nb_registers)));
   18051 
   18052       /* MOV Ri, Rn.  */
   18053       current_stub_contents =
   18054 	push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
   18055 			    create_instruction_mov (ri, rn));
   18056 
   18057       /* LDMIA Ri!, {R-low-register-list}.  */
   18058       current_stub_contents =
   18059 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18060 			    create_instruction_ldmia
   18061 			    (ri, /*wback=*/1, insn_low_registers));
   18062 
   18063       /* LDMIA Ri, {R-high-register-list}.  */
   18064       current_stub_contents =
   18065 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18066 			    create_instruction_ldmia
   18067 			    (ri, /*wback=*/0, insn_high_registers));
   18068     }
   18069   else if (!wback && !restore_pc && restore_rn)
   18070     {
   18071       ri = rn;
   18072       if (!(insn_low_registers & (1 << rn)))
   18073 	{
   18074 	  /* Choose a Ri in the low-register-list that will be restored.  */
   18075 	  ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
   18076 
   18077 	  /* MOV Ri, Rn.  */
   18078 	  current_stub_contents =
   18079 	    push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
   18080 				create_instruction_mov (ri, rn));
   18081 	}
   18082 
   18083       /* LDMDB Ri!, {R-high-register-list}.  */
   18084       current_stub_contents =
   18085 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18086 			    create_instruction_ldmdb
   18087 			    (ri, /*wback=*/1, insn_high_registers));
   18088 
   18089       /* LDMDB Ri, {R-low-register-list}.  */
   18090       current_stub_contents =
   18091 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18092 			    create_instruction_ldmdb
   18093 			    (ri, /*wback=*/0, insn_low_registers));
   18094 
   18095       /* B initial_insn_addr+4.  */
   18096       current_stub_contents =
   18097 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18098 			    create_instruction_branch_absolute
   18099 			    (initial_insn_addr - current_stub_contents));
   18100     }
   18101   else if (!wback && restore_pc && restore_rn)
   18102     {
   18103       ri = rn;
   18104       if (!(insn_high_registers & (1 << rn)))
   18105 	{
   18106 	  /* Choose a Ri in the high-register-list that will be restored.  */
   18107 	  ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
   18108 	}
   18109 
   18110       /* SUB Ri, Rn, #(4*nb_registers).  */
   18111       current_stub_contents =
   18112 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18113 			    create_instruction_sub (ri, rn, (4 * nb_registers)));
   18114 
   18115       /* LDMIA Ri!, {R-low-register-list}.  */
   18116       current_stub_contents =
   18117 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18118 			    create_instruction_ldmia
   18119 			    (ri, /*wback=*/1, insn_low_registers));
   18120 
   18121       /* LDMIA Ri, {R-high-register-list}.  */
   18122       current_stub_contents =
   18123 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18124 			    create_instruction_ldmia
   18125 			    (ri, /*wback=*/0, insn_high_registers));
   18126     }
   18127   else if (wback && restore_rn)
   18128     {
   18129       /* The assembler should not have accepted to encode this.  */
   18130       BFD_ASSERT (0 && "Cannot patch an instruction that has an "
   18131 	"undefined behavior.\n");
   18132     }
   18133 
   18134   /* Fill the remaining of the stub with deterministic contents.  */
   18135   current_stub_contents =
   18136     stm32l4xx_fill_stub_udf (htab, output_bfd,
   18137 			     base_stub_contents, current_stub_contents,
   18138 			     base_stub_contents +
   18139 			     STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
   18140 
   18141 }
   18142 
   18143 static void
   18144 stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
   18145 				      bfd * output_bfd,
   18146 				      const insn32 initial_insn,
   18147 				      const bfd_byte *const initial_insn_addr,
   18148 				      bfd_byte *const base_stub_contents)
   18149 {
   18150   int num_words = ((unsigned int) initial_insn << 24) >> 24;
   18151   bfd_byte *current_stub_contents = base_stub_contents;
   18152 
   18153   BFD_ASSERT (is_thumb2_vldm (initial_insn));
   18154 
   18155   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
   18156      smaller than 8 words load sequences that do not cause the
   18157      hardware issue.  */
   18158   if (num_words <= 8)
   18159     {
   18160       /* Untouched instruction.  */
   18161       current_stub_contents =
   18162 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18163 			    initial_insn);
   18164 
   18165       /* B initial_insn_addr+4.  */
   18166       current_stub_contents =
   18167 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18168 			    create_instruction_branch_absolute
   18169 			    (initial_insn_addr - current_stub_contents));
   18170     }
   18171   else
   18172     {
   18173       bfd_boolean is_dp = /* DP encoding.  */
   18174 	(initial_insn & 0xfe100f00) == 0xec100b00;
   18175       bfd_boolean is_ia_nobang = /* (IA without !).  */
   18176 	(((initial_insn << 7) >> 28) & 0xd) == 0x4;
   18177       bfd_boolean is_ia_bang = /* (IA with !) - includes VPOP.  */
   18178 	(((initial_insn << 7) >> 28) & 0xd) == 0x5;
   18179       bfd_boolean is_db_bang = /* (DB with !).  */
   18180 	(((initial_insn << 7) >> 28) & 0xd) == 0x9;
   18181       int base_reg = ((unsigned int) initial_insn << 12) >> 28;
   18182       /* d = UInt (Vd:D);.  */
   18183       int first_reg = ((((unsigned int) initial_insn << 16) >> 28) << 1)
   18184 	| (((unsigned int)initial_insn << 9) >> 31);
   18185 
   18186       /* Compute the number of 8-words chunks needed to split.  */
   18187       int chunks = (num_words % 8) ? (num_words / 8 + 1) : (num_words / 8);
   18188       int chunk;
   18189 
   18190       /* The test coverage has been done assuming the following
   18191 	 hypothesis that exactly one of the previous is_ predicates is
   18192 	 true.  */
   18193       BFD_ASSERT (    (is_ia_nobang ^ is_ia_bang ^ is_db_bang)
   18194 		  && !(is_ia_nobang & is_ia_bang & is_db_bang));
   18195 
   18196       /* We treat the cutting of the words in one pass for all
   18197 	 cases, then we emit the adjustments:
   18198 
   18199 	 vldm rx, {...}
   18200 	 -> vldm rx!, {8_words_or_less} for each needed 8_word
   18201 	 -> sub rx, rx, #size (list)
   18202 
   18203 	 vldm rx!, {...}
   18204 	 -> vldm rx!, {8_words_or_less} for each needed 8_word
   18205 	 This also handles vpop instruction (when rx is sp)
   18206 
   18207 	 vldmd rx!, {...}
   18208 	 -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
   18209       for (chunk = 0; chunk < chunks; ++chunk)
   18210 	{
   18211 	  bfd_vma new_insn = 0;
   18212 
   18213 	  if (is_ia_nobang || is_ia_bang)
   18214 	    {
   18215 	      new_insn = create_instruction_vldmia
   18216 		(base_reg,
   18217 		 is_dp,
   18218 		 /*wback= .  */1,
   18219 		 chunks - (chunk + 1) ?
   18220 		 8 : num_words - chunk * 8,
   18221 		 first_reg + chunk * 8);
   18222 	    }
   18223 	  else if (is_db_bang)
   18224 	    {
   18225 	      new_insn = create_instruction_vldmdb
   18226 		(base_reg,
   18227 		 is_dp,
   18228 		 chunks - (chunk + 1) ?
   18229 		 8 : num_words - chunk * 8,
   18230 		 first_reg + chunk * 8);
   18231 	    }
   18232 
   18233 	  if (new_insn)
   18234 	    current_stub_contents =
   18235 	      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18236 				  new_insn);
   18237 	}
   18238 
   18239       /* Only this case requires the base register compensation
   18240 	 subtract.  */
   18241       if (is_ia_nobang)
   18242 	{
   18243 	  current_stub_contents =
   18244 	    push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18245 				create_instruction_sub
   18246 				(base_reg, base_reg, 4*num_words));
   18247 	}
   18248 
   18249       /* B initial_insn_addr+4.  */
   18250       current_stub_contents =
   18251 	push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
   18252 			    create_instruction_branch_absolute
   18253 			    (initial_insn_addr - current_stub_contents));
   18254     }
   18255 
   18256   /* Fill the remaining of the stub with deterministic contents.  */
   18257   current_stub_contents =
   18258     stm32l4xx_fill_stub_udf (htab, output_bfd,
   18259 			     base_stub_contents, current_stub_contents,
   18260 			     base_stub_contents +
   18261 			     STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
   18262 }
   18263 
   18264 static void
   18265 stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
   18266 				 bfd * output_bfd,
   18267 				 const insn32 wrong_insn,
   18268 				 const bfd_byte *const wrong_insn_addr,
   18269 				 bfd_byte *const stub_contents)
   18270 {
   18271   if (is_thumb2_ldmia (wrong_insn))
   18272     stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
   18273 					   wrong_insn, wrong_insn_addr,
   18274 					   stub_contents);
   18275   else if (is_thumb2_ldmdb (wrong_insn))
   18276     stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
   18277 					   wrong_insn, wrong_insn_addr,
   18278 					   stub_contents);
   18279   else if (is_thumb2_vldm (wrong_insn))
   18280     stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
   18281 					  wrong_insn, wrong_insn_addr,
   18282 					  stub_contents);
   18283 }
   18284 
   18285 /* End of stm32l4xx work-around.  */
   18286 
   18287 
   18288 /* Do code byteswapping.  Return FALSE afterwards so that the section is
   18289    written out as normal.  */
   18290 
   18291 static bfd_boolean
   18292 elf32_arm_write_section (bfd *output_bfd,
   18293 			 struct bfd_link_info *link_info,
   18294 			 asection *sec,
   18295 			 bfd_byte *contents)
   18296 {
   18297   unsigned int mapcount, errcount;
   18298   _arm_elf_section_data *arm_data;
   18299   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
   18300   elf32_arm_section_map *map;
   18301   elf32_vfp11_erratum_list *errnode;
   18302   elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
   18303   bfd_vma ptr;
   18304   bfd_vma end;
   18305   bfd_vma offset = sec->output_section->vma + sec->output_offset;
   18306   bfd_byte tmp;
   18307   unsigned int i;
   18308 
   18309   if (globals == NULL)
   18310     return FALSE;
   18311 
   18312   /* If this section has not been allocated an _arm_elf_section_data
   18313      structure then we cannot record anything.  */
   18314   arm_data = get_arm_elf_section_data (sec);
   18315   if (arm_data == NULL)
   18316     return FALSE;
   18317 
   18318   mapcount = arm_data->mapcount;
   18319   map = arm_data->map;
   18320   errcount = arm_data->erratumcount;
   18321 
   18322   if (errcount != 0)
   18323     {
   18324       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
   18325 
   18326       for (errnode = arm_data->erratumlist; errnode != 0;
   18327 	   errnode = errnode->next)
   18328 	{
   18329 	  bfd_vma target = errnode->vma - offset;
   18330 
   18331 	  switch (errnode->type)
   18332 	    {
   18333 	    case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
   18334 	      {
   18335 		bfd_vma branch_to_veneer;
   18336 		/* Original condition code of instruction, plus bit mask for
   18337 		   ARM B instruction.  */
   18338 		unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
   18339 				  | 0x0a000000;
   18340 
   18341 		/* The instruction is before the label.  */
   18342 		target -= 4;
   18343 
   18344 		/* Above offset included in -4 below.  */
   18345 		branch_to_veneer = errnode->u.b.veneer->vma
   18346 				   - errnode->vma - 4;
   18347 
   18348 		if ((signed) branch_to_veneer < -(1 << 25)
   18349 		    || (signed) branch_to_veneer >= (1 << 25))
   18350 		  _bfd_error_handler (_("%B: error: VFP11 veneer out of "
   18351 					"range"), output_bfd);
   18352 
   18353 		insn |= (branch_to_veneer >> 2) & 0xffffff;
   18354 		contents[endianflip ^ target] = insn & 0xff;
   18355 		contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
   18356 		contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
   18357 		contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
   18358 	      }
   18359 	      break;
   18360 
   18361 	    case VFP11_ERRATUM_ARM_VENEER:
   18362 	      {
   18363 		bfd_vma branch_from_veneer;
   18364 		unsigned int insn;
   18365 
   18366 		/* Take size of veneer into account.  */
   18367 		branch_from_veneer = errnode->u.v.branch->vma
   18368 				     - errnode->vma - 12;
   18369 
   18370 		if ((signed) branch_from_veneer < -(1 << 25)
   18371 		    || (signed) branch_from_veneer >= (1 << 25))
   18372 		  _bfd_error_handler (_("%B: error: VFP11 veneer out of "
   18373 					"range"), output_bfd);
   18374 
   18375 		/* Original instruction.  */
   18376 		insn = errnode->u.v.branch->u.b.vfp_insn;
   18377 		contents[endianflip ^ target] = insn & 0xff;
   18378 		contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
   18379 		contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
   18380 		contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
   18381 
   18382 		/* Branch back to insn after original insn.  */
   18383 		insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
   18384 		contents[endianflip ^ (target + 4)] = insn & 0xff;
   18385 		contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
   18386 		contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
   18387 		contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
   18388 	      }
   18389 	      break;
   18390 
   18391 	    default:
   18392 	      abort ();
   18393 	    }
   18394 	}
   18395     }
   18396 
   18397   if (arm_data->stm32l4xx_erratumcount != 0)
   18398     {
   18399       for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
   18400 	   stm32l4xx_errnode != 0;
   18401 	   stm32l4xx_errnode = stm32l4xx_errnode->next)
   18402 	{
   18403 	  bfd_vma target = stm32l4xx_errnode->vma - offset;
   18404 
   18405 	  switch (stm32l4xx_errnode->type)
   18406 	    {
   18407 	    case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
   18408 	      {
   18409 		unsigned int insn;
   18410 		bfd_vma branch_to_veneer =
   18411 		  stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
   18412 
   18413 		if ((signed) branch_to_veneer < -(1 << 24)
   18414 		    || (signed) branch_to_veneer >= (1 << 24))
   18415 		  {
   18416 		    bfd_vma out_of_range =
   18417 		      ((signed) branch_to_veneer < -(1 << 24)) ?
   18418 		      - branch_to_veneer - (1 << 24) :
   18419 		      ((signed) branch_to_veneer >= (1 << 24)) ?
   18420 		      branch_to_veneer - (1 << 24) : 0;
   18421 
   18422 		    _bfd_error_handler
   18423 		      (_("%B(%#Lx): error: Cannot create STM32L4XX veneer. "
   18424 			 "Jump out of range by %Ld bytes. "
   18425 			 "Cannot encode branch instruction. "),
   18426 		       output_bfd,
   18427 		       stm32l4xx_errnode->vma - 4,
   18428 		       out_of_range);
   18429 		    continue;
   18430 		  }
   18431 
   18432 		insn = create_instruction_branch_absolute
   18433 		  (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
   18434 
   18435 		/* The instruction is before the label.  */
   18436 		target -= 4;
   18437 
   18438 		put_thumb2_insn (globals, output_bfd,
   18439 				 (bfd_vma) insn, contents + target);
   18440 	      }
   18441 	      break;
   18442 
   18443 	    case STM32L4XX_ERRATUM_VENEER:
   18444 	      {
   18445 		bfd_byte * veneer;
   18446 		bfd_byte * veneer_r;
   18447 		unsigned int insn;
   18448 
   18449 		veneer = contents + target;
   18450 		veneer_r = veneer
   18451 		  + stm32l4xx_errnode->u.b.veneer->vma
   18452 		  - stm32l4xx_errnode->vma - 4;
   18453 
   18454 		if ((signed) (veneer_r - veneer -
   18455 			      STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
   18456 			      STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
   18457 			      STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
   18458 			      STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
   18459 		    || (signed) (veneer_r - veneer) >= (1 << 24))
   18460 		  {
   18461 		    _bfd_error_handler (_("%B: error: Cannot create STM32L4XX "
   18462 					  "veneer."), output_bfd);
   18463 		     continue;
   18464 		  }
   18465 
   18466 		/* Original instruction.  */
   18467 		insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
   18468 
   18469 		stm32l4xx_create_replacing_stub
   18470 		  (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
   18471 	      }
   18472 	      break;
   18473 
   18474 	    default:
   18475 	      abort ();
   18476 	    }
   18477 	}
   18478     }
   18479 
   18480   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
   18481     {
   18482       arm_unwind_table_edit *edit_node
   18483 	= arm_data->u.exidx.unwind_edit_list;
   18484       /* Now, sec->size is the size of the section we will write.  The original
   18485 	 size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
   18486 	 markers) was sec->rawsize.  (This isn't the case if we perform no
   18487 	 edits, then rawsize will be zero and we should use size).  */
   18488       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
   18489       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
   18490       unsigned int in_index, out_index;
   18491       bfd_vma add_to_offsets = 0;
   18492 
   18493       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
   18494 	{
   18495 	  if (edit_node)
   18496 	    {
   18497 	      unsigned int edit_index = edit_node->index;
   18498 
   18499 	      if (in_index < edit_index && in_index * 8 < input_size)
   18500 		{
   18501 		  copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
   18502 				    contents + in_index * 8, add_to_offsets);
   18503 		  out_index++;
   18504 		  in_index++;
   18505 		}
   18506 	      else if (in_index == edit_index
   18507 		       || (in_index * 8 >= input_size
   18508 			   && edit_index == UINT_MAX))
   18509 		{
   18510 		  switch (edit_node->type)
   18511 		    {
   18512 		    case DELETE_EXIDX_ENTRY:
   18513 		      in_index++;
   18514 		      add_to_offsets += 8;
   18515 		      break;
   18516 
   18517 		    case INSERT_EXIDX_CANTUNWIND_AT_END:
   18518 		      {
   18519 			asection *text_sec = edit_node->linked_section;
   18520 			bfd_vma text_offset = text_sec->output_section->vma
   18521 					      + text_sec->output_offset
   18522 					      + text_sec->size;
   18523 			bfd_vma exidx_offset = offset + out_index * 8;
   18524 			unsigned long prel31_offset;
   18525 
   18526 			/* Note: this is meant to be equivalent to an
   18527 			   R_ARM_PREL31 relocation.  These synthetic
   18528 			   EXIDX_CANTUNWIND markers are not relocated by the
   18529 			   usual BFD method.  */
   18530 			prel31_offset = (text_offset - exidx_offset)
   18531 					& 0x7ffffffful;
   18532 			if (bfd_link_relocatable (link_info))
   18533 			  {
   18534 			    /* Here relocation for new EXIDX_CANTUNWIND is
   18535 			       created, so there is no need to
   18536 			       adjust offset by hand.  */
   18537 			    prel31_offset = text_sec->output_offset
   18538 					    + text_sec->size;
   18539 			  }
   18540 
   18541 			/* First address we can't unwind.  */
   18542 			bfd_put_32 (output_bfd, prel31_offset,
   18543 				    &edited_contents[out_index * 8]);
   18544 
   18545 			/* Code for EXIDX_CANTUNWIND.  */
   18546 			bfd_put_32 (output_bfd, 0x1,
   18547 				    &edited_contents[out_index * 8 + 4]);
   18548 
   18549 			out_index++;
   18550 			add_to_offsets -= 8;
   18551 		      }
   18552 		      break;
   18553 		    }
   18554 
   18555 		  edit_node = edit_node->next;
   18556 		}
   18557 	    }
   18558 	  else
   18559 	    {
   18560 	      /* No more edits, copy remaining entries verbatim.  */
   18561 	      copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
   18562 				contents + in_index * 8, add_to_offsets);
   18563 	      out_index++;
   18564 	      in_index++;
   18565 	    }
   18566 	}
   18567 
   18568       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
   18569 	bfd_set_section_contents (output_bfd, sec->output_section,
   18570 				  edited_contents,
   18571 				  (file_ptr) sec->output_offset, sec->size);
   18572 
   18573       return TRUE;
   18574     }
   18575 
   18576   /* Fix code to point to Cortex-A8 erratum stubs.  */
   18577   if (globals->fix_cortex_a8)
   18578     {
   18579       struct a8_branch_to_stub_data data;
   18580 
   18581       data.writing_section = sec;
   18582       data.contents = contents;
   18583 
   18584       bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
   18585 			 & data);
   18586     }
   18587 
   18588   if (mapcount == 0)
   18589     return FALSE;
   18590 
   18591   if (globals->byteswap_code)
   18592     {
   18593       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
   18594 
   18595       ptr = map[0].vma;
   18596       for (i = 0; i < mapcount; i++)
   18597 	{
   18598 	  if (i == mapcount - 1)
   18599 	    end = sec->size;
   18600 	  else
   18601 	    end = map[i + 1].vma;
   18602 
   18603 	  switch (map[i].type)
   18604 	    {
   18605 	    case 'a':
   18606 	      /* Byte swap code words.  */
   18607 	      while (ptr + 3 < end)
   18608 		{
   18609 		  tmp = contents[ptr];
   18610 		  contents[ptr] = contents[ptr + 3];
   18611 		  contents[ptr + 3] = tmp;
   18612 		  tmp = contents[ptr + 1];
   18613 		  contents[ptr + 1] = contents[ptr + 2];
   18614 		  contents[ptr + 2] = tmp;
   18615 		  ptr += 4;
   18616 		}
   18617 	      break;
   18618 
   18619 	    case 't':
   18620 	      /* Byte swap code halfwords.  */
   18621 	      while (ptr + 1 < end)
   18622 		{
   18623 		  tmp = contents[ptr];
   18624 		  contents[ptr] = contents[ptr + 1];
   18625 		  contents[ptr + 1] = tmp;
   18626 		  ptr += 2;
   18627 		}
   18628 	      break;
   18629 
   18630 	    case 'd':
   18631 	      /* Leave data alone.  */
   18632 	      break;
   18633 	    }
   18634 	  ptr = end;
   18635 	}
   18636     }
   18637 
   18638   free (map);
   18639   arm_data->mapcount = -1;
   18640   arm_data->mapsize = 0;
   18641   arm_data->map = NULL;
   18642 
   18643   return FALSE;
   18644 }
   18645 
   18646 /* Mangle thumb function symbols as we read them in.  */
   18647 
   18648 static bfd_boolean
   18649 elf32_arm_swap_symbol_in (bfd * abfd,
   18650 			  const void *psrc,
   18651 			  const void *pshn,
   18652 			  Elf_Internal_Sym *dst)
   18653 {
   18654   Elf_Internal_Shdr *symtab_hdr;
   18655   const char *name = NULL;
   18656 
   18657   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
   18658     return FALSE;
   18659   dst->st_target_internal = 0;
   18660 
   18661   /* New EABI objects mark thumb function symbols by setting the low bit of
   18662      the address.  */
   18663   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
   18664       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
   18665     {
   18666       if (dst->st_value & 1)
   18667 	{
   18668 	  dst->st_value &= ~(bfd_vma) 1;
   18669 	  ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal,
   18670 				   ST_BRANCH_TO_THUMB);
   18671 	}
   18672       else
   18673 	ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_ARM);
   18674     }
   18675   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
   18676     {
   18677       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
   18678       ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_THUMB);
   18679     }
   18680   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
   18681     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_LONG);
   18682   else
   18683     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_UNKNOWN);
   18684 
   18685   /* Mark CMSE special symbols.  */
   18686   symtab_hdr = & elf_symtab_hdr (abfd);
   18687   if (symtab_hdr->sh_size)
   18688     name = bfd_elf_sym_name (abfd, symtab_hdr, dst, NULL);
   18689   if (name && CONST_STRNEQ (name, CMSE_PREFIX))
   18690     ARM_SET_SYM_CMSE_SPCL (dst->st_target_internal);
   18691 
   18692   return TRUE;
   18693 }
   18694 
   18695 
   18696 /* Mangle thumb function symbols as we write them out.  */
   18697 
   18698 static void
   18699 elf32_arm_swap_symbol_out (bfd *abfd,
   18700 			   const Elf_Internal_Sym *src,
   18701 			   void *cdst,
   18702 			   void *shndx)
   18703 {
   18704   Elf_Internal_Sym newsym;
   18705 
   18706   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
   18707      of the address set, as per the new EABI.  We do this unconditionally
   18708      because objcopy does not set the elf header flags until after
   18709      it writes out the symbol table.  */
   18710   if (ARM_GET_SYM_BRANCH_TYPE (src->st_target_internal) == ST_BRANCH_TO_THUMB)
   18711     {
   18712       newsym = *src;
   18713       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
   18714 	newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
   18715       if (newsym.st_shndx != SHN_UNDEF)
   18716 	{
   18717 	  /* Do this only for defined symbols. At link type, the static
   18718 	     linker will simulate the work of dynamic linker of resolving
   18719 	     symbols and will carry over the thumbness of found symbols to
   18720 	     the output symbol table. It's not clear how it happens, but
   18721 	     the thumbness of undefined symbols can well be different at
   18722 	     runtime, and writing '1' for them will be confusing for users
   18723 	     and possibly for dynamic linker itself.
   18724 	  */
   18725 	  newsym.st_value |= 1;
   18726 	}
   18727 
   18728       src = &newsym;
   18729     }
   18730   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
   18731 }
   18732 
   18733 /* Add the PT_ARM_EXIDX program header.  */
   18734 
   18735 static bfd_boolean
   18736 elf32_arm_modify_segment_map (bfd *abfd,
   18737 			      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   18738 {
   18739   struct elf_segment_map *m;
   18740   asection *sec;
   18741 
   18742   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
   18743   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
   18744     {
   18745       /* If there is already a PT_ARM_EXIDX header, then we do not
   18746 	 want to add another one.  This situation arises when running
   18747 	 "strip"; the input binary already has the header.  */
   18748       m = elf_seg_map (abfd);
   18749       while (m && m->p_type != PT_ARM_EXIDX)
   18750 	m = m->next;
   18751       if (!m)
   18752 	{
   18753 	  m = (struct elf_segment_map *)
   18754 	      bfd_zalloc (abfd, sizeof (struct elf_segment_map));
   18755 	  if (m == NULL)
   18756 	    return FALSE;
   18757 	  m->p_type = PT_ARM_EXIDX;
   18758 	  m->count = 1;
   18759 	  m->sections[0] = sec;
   18760 
   18761 	  m->next = elf_seg_map (abfd);
   18762 	  elf_seg_map (abfd) = m;
   18763 	}
   18764     }
   18765 
   18766   return TRUE;
   18767 }
   18768 
   18769 /* We may add a PT_ARM_EXIDX program header.  */
   18770 
   18771 static int
   18772 elf32_arm_additional_program_headers (bfd *abfd,
   18773 				      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   18774 {
   18775   asection *sec;
   18776 
   18777   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
   18778   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
   18779     return 1;
   18780   else
   18781     return 0;
   18782 }
   18783 
   18784 /* Hook called by the linker routine which adds symbols from an object
   18785    file.  */
   18786 
   18787 static bfd_boolean
   18788 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   18789 			   Elf_Internal_Sym *sym, const char **namep,
   18790 			   flagword *flagsp, asection **secp, bfd_vma *valp)
   18791 {
   18792   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
   18793       && (abfd->flags & DYNAMIC) == 0
   18794       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
   18795     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
   18796 
   18797   if (elf32_arm_hash_table (info) == NULL)
   18798     return FALSE;
   18799 
   18800   if (elf32_arm_hash_table (info)->vxworks_p
   18801       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
   18802 				       flagsp, secp, valp))
   18803     return FALSE;
   18804 
   18805   return TRUE;
   18806 }
   18807 
   18808 /* We use this to override swap_symbol_in and swap_symbol_out.  */
   18809 const struct elf_size_info elf32_arm_size_info =
   18810 {
   18811   sizeof (Elf32_External_Ehdr),
   18812   sizeof (Elf32_External_Phdr),
   18813   sizeof (Elf32_External_Shdr),
   18814   sizeof (Elf32_External_Rel),
   18815   sizeof (Elf32_External_Rela),
   18816   sizeof (Elf32_External_Sym),
   18817   sizeof (Elf32_External_Dyn),
   18818   sizeof (Elf_External_Note),
   18819   4,
   18820   1,
   18821   32, 2,
   18822   ELFCLASS32, EV_CURRENT,
   18823   bfd_elf32_write_out_phdrs,
   18824   bfd_elf32_write_shdrs_and_ehdr,
   18825   bfd_elf32_checksum_contents,
   18826   bfd_elf32_write_relocs,
   18827   elf32_arm_swap_symbol_in,
   18828   elf32_arm_swap_symbol_out,
   18829   bfd_elf32_slurp_reloc_table,
   18830   bfd_elf32_slurp_symbol_table,
   18831   bfd_elf32_swap_dyn_in,
   18832   bfd_elf32_swap_dyn_out,
   18833   bfd_elf32_swap_reloc_in,
   18834   bfd_elf32_swap_reloc_out,
   18835   bfd_elf32_swap_reloca_in,
   18836   bfd_elf32_swap_reloca_out
   18837 };
   18838 
   18839 static bfd_vma
   18840 read_code32 (const bfd *abfd, const bfd_byte *addr)
   18841 {
   18842   /* V7 BE8 code is always little endian.  */
   18843   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
   18844     return bfd_getl32 (addr);
   18845 
   18846   return bfd_get_32 (abfd, addr);
   18847 }
   18848 
   18849 static bfd_vma
   18850 read_code16 (const bfd *abfd, const bfd_byte *addr)
   18851 {
   18852   /* V7 BE8 code is always little endian.  */
   18853   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
   18854     return bfd_getl16 (addr);
   18855 
   18856   return bfd_get_16 (abfd, addr);
   18857 }
   18858 
   18859 /* Return size of plt0 entry starting at ADDR
   18860    or (bfd_vma) -1 if size can not be determined.  */
   18861 
   18862 static bfd_vma
   18863 elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr)
   18864 {
   18865   bfd_vma first_word;
   18866   bfd_vma plt0_size;
   18867 
   18868   first_word = read_code32 (abfd, addr);
   18869 
   18870   if (first_word == elf32_arm_plt0_entry[0])
   18871     plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
   18872   else if (first_word == elf32_thumb2_plt0_entry[0])
   18873     plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
   18874   else
   18875     /* We don't yet handle this PLT format.  */
   18876     return (bfd_vma) -1;
   18877 
   18878   return plt0_size;
   18879 }
   18880 
   18881 /* Return size of plt entry starting at offset OFFSET
   18882    of plt section located at address START
   18883    or (bfd_vma) -1 if size can not be determined.  */
   18884 
   18885 static bfd_vma
   18886 elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset)
   18887 {
   18888   bfd_vma first_insn;
   18889   bfd_vma plt_size = 0;
   18890   const bfd_byte *addr = start + offset;
   18891 
   18892   /* PLT entry size if fixed on Thumb-only platforms.  */
   18893   if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
   18894       return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
   18895 
   18896   /* Respect Thumb stub if necessary.  */
   18897   if (read_code16 (abfd, addr) == elf32_arm_plt_thumb_stub[0])
   18898     {
   18899       plt_size += 2 * ARRAY_SIZE(elf32_arm_plt_thumb_stub);
   18900     }
   18901 
   18902   /* Strip immediate from first add.  */
   18903   first_insn = read_code32 (abfd, addr + plt_size) & 0xffffff00;
   18904 
   18905 #ifdef FOUR_WORD_PLT
   18906   if (first_insn == elf32_arm_plt_entry[0])
   18907     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
   18908 #else
   18909   if (first_insn == elf32_arm_plt_entry_long[0])
   18910     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
   18911   else if (first_insn == elf32_arm_plt_entry_short[0])
   18912     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
   18913 #endif
   18914   else
   18915     /* We don't yet handle this PLT format.  */
   18916     return (bfd_vma) -1;
   18917 
   18918   return plt_size;
   18919 }
   18920 
   18921 /* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
   18922 
   18923 static long
   18924 elf32_arm_get_synthetic_symtab (bfd *abfd,
   18925 			       long symcount ATTRIBUTE_UNUSED,
   18926 			       asymbol **syms ATTRIBUTE_UNUSED,
   18927 			       long dynsymcount,
   18928 			       asymbol **dynsyms,
   18929 			       asymbol **ret)
   18930 {
   18931   asection *relplt;
   18932   asymbol *s;
   18933   arelent *p;
   18934   long count, i, n;
   18935   size_t size;
   18936   Elf_Internal_Shdr *hdr;
   18937   char *names;
   18938   asection *plt;
   18939   bfd_vma offset;
   18940   bfd_byte *data;
   18941 
   18942   *ret = NULL;
   18943 
   18944   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   18945     return 0;
   18946 
   18947   if (dynsymcount <= 0)
   18948     return 0;
   18949 
   18950   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
   18951   if (relplt == NULL)
   18952     return 0;
   18953 
   18954   hdr = &elf_section_data (relplt)->this_hdr;
   18955   if (hdr->sh_link != elf_dynsymtab (abfd)
   18956       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   18957     return 0;
   18958 
   18959   plt = bfd_get_section_by_name (abfd, ".plt");
   18960   if (plt == NULL)
   18961     return 0;
   18962 
   18963   if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, TRUE))
   18964     return -1;
   18965 
   18966   data = plt->contents;
   18967   if (data == NULL)
   18968     {
   18969       if (!bfd_get_full_section_contents(abfd, (asection *) plt, &data) || data == NULL)
   18970 	return -1;
   18971       bfd_cache_section_contents((asection *) plt, data);
   18972     }
   18973 
   18974   count = relplt->size / hdr->sh_entsize;
   18975   size = count * sizeof (asymbol);
   18976   p = relplt->relocation;
   18977   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
   18978     {
   18979       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   18980       if (p->addend != 0)
   18981 	size += sizeof ("+0x") - 1 + 8;
   18982     }
   18983 
   18984   s = *ret = (asymbol *) bfd_malloc (size);
   18985   if (s == NULL)
   18986     return -1;
   18987 
   18988   offset = elf32_arm_plt0_size (abfd, data);
   18989   if (offset == (bfd_vma) -1)
   18990     return -1;
   18991 
   18992   names = (char *) (s + count);
   18993   p = relplt->relocation;
   18994   n = 0;
   18995   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
   18996     {
   18997       size_t len;
   18998 
   18999       bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset);
   19000       if (plt_size == (bfd_vma) -1)
   19001 	break;
   19002 
   19003       *s = **p->sym_ptr_ptr;
   19004       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   19005 	 we are defining a symbol, ensure one of them is set.  */
   19006       if ((s->flags & BSF_LOCAL) == 0)
   19007 	s->flags |= BSF_GLOBAL;
   19008       s->flags |= BSF_SYNTHETIC;
   19009       s->section = plt;
   19010       s->value = offset;
   19011       s->name = names;
   19012       s->udata.p = NULL;
   19013       len = strlen ((*p->sym_ptr_ptr)->name);
   19014       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   19015       names += len;
   19016       if (p->addend != 0)
   19017 	{
   19018 	  char buf[30], *a;
   19019 
   19020 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   19021 	  names += sizeof ("+0x") - 1;
   19022 	  bfd_sprintf_vma (abfd, buf, p->addend);
   19023 	  for (a = buf; *a == '0'; ++a)
   19024 	    ;
   19025 	  len = strlen (a);
   19026 	  memcpy (names, a, len);
   19027 	  names += len;
   19028 	}
   19029       memcpy (names, "@plt", sizeof ("@plt"));
   19030       names += sizeof ("@plt");
   19031       ++s, ++n;
   19032       offset += plt_size;
   19033     }
   19034 
   19035   return n;
   19036 }
   19037 
   19038 static bfd_boolean
   19039 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr * hdr)
   19040 {
   19041   if (hdr->sh_flags & SHF_ARM_PURECODE)
   19042     *flags |= SEC_ELF_PURECODE;
   19043   return TRUE;
   19044 }
   19045 
   19046 static flagword
   19047 elf32_arm_lookup_section_flags (char *flag_name)
   19048 {
   19049   if (!strcmp (flag_name, "SHF_ARM_PURECODE"))
   19050     return SHF_ARM_PURECODE;
   19051 
   19052   return SEC_NO_FLAGS;
   19053 }
   19054 
   19055 static unsigned int
   19056 elf32_arm_count_additional_relocs (asection *sec)
   19057 {
   19058   struct _arm_elf_section_data *arm_data;
   19059   arm_data = get_arm_elf_section_data (sec);
   19060 
   19061   return arm_data == NULL ? 0 : arm_data->additional_reloc_count;
   19062 }
   19063 
   19064 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
   19065    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
   19066    FALSE otherwise.  ISECTION is the best guess matching section from the
   19067    input bfd IBFD, but it might be NULL.  */
   19068 
   19069 static bfd_boolean
   19070 elf32_arm_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   19071 				       bfd *obfd ATTRIBUTE_UNUSED,
   19072 				       const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
   19073 				       Elf_Internal_Shdr *osection)
   19074 {
   19075   switch (osection->sh_type)
   19076     {
   19077     case SHT_ARM_EXIDX:
   19078       {
   19079 	Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   19080 	Elf_Internal_Shdr **iheaders = elf_elfsections (ibfd);
   19081 	unsigned i = 0;
   19082 
   19083 	osection->sh_flags = SHF_ALLOC | SHF_LINK_ORDER;
   19084 	osection->sh_info = 0;
   19085 
   19086 	/* The sh_link field must be set to the text section associated with
   19087 	   this index section.  Unfortunately the ARM EHABI does not specify
   19088 	   exactly how to determine this association.  Our caller does try
   19089 	   to match up OSECTION with its corresponding input section however
   19090 	   so that is a good first guess.  */
   19091 	if (isection != NULL
   19092 	    && osection->bfd_section != NULL
   19093 	    && isection->bfd_section != NULL
   19094 	    && isection->bfd_section->output_section != NULL
   19095 	    && isection->bfd_section->output_section == osection->bfd_section
   19096 	    && iheaders != NULL
   19097 	    && isection->sh_link > 0
   19098 	    && isection->sh_link < elf_numsections (ibfd)
   19099 	    && iheaders[isection->sh_link]->bfd_section != NULL
   19100 	    && iheaders[isection->sh_link]->bfd_section->output_section != NULL
   19101 	    )
   19102 	  {
   19103 	    for (i = elf_numsections (obfd); i-- > 0;)
   19104 	      if (oheaders[i]->bfd_section
   19105 		  == iheaders[isection->sh_link]->bfd_section->output_section)
   19106 		break;
   19107 	  }
   19108 
   19109 	if (i == 0)
   19110 	  {
   19111 	    /* Failing that we have to find a matching section ourselves.  If
   19112 	       we had the output section name available we could compare that
   19113 	       with input section names.  Unfortunately we don't.  So instead
   19114 	       we use a simple heuristic and look for the nearest executable
   19115 	       section before this one.  */
   19116 	    for (i = elf_numsections (obfd); i-- > 0;)
   19117 	      if (oheaders[i] == osection)
   19118 		break;
   19119 	    if (i == 0)
   19120 	      break;
   19121 
   19122 	    while (i-- > 0)
   19123 	      if (oheaders[i]->sh_type == SHT_PROGBITS
   19124 		  && (oheaders[i]->sh_flags & (SHF_ALLOC | SHF_EXECINSTR))
   19125 		  == (SHF_ALLOC | SHF_EXECINSTR))
   19126 		break;
   19127 	  }
   19128 
   19129 	if (i)
   19130 	  {
   19131 	    osection->sh_link = i;
   19132 	    /* If the text section was part of a group
   19133 	       then the index section should be too.  */
   19134 	    if (oheaders[i]->sh_flags & SHF_GROUP)
   19135 	      osection->sh_flags |= SHF_GROUP;
   19136 	    return TRUE;
   19137 	  }
   19138       }
   19139       break;
   19140 
   19141     case SHT_ARM_PREEMPTMAP:
   19142       osection->sh_flags = SHF_ALLOC;
   19143       break;
   19144 
   19145     case SHT_ARM_ATTRIBUTES:
   19146     case SHT_ARM_DEBUGOVERLAY:
   19147     case SHT_ARM_OVERLAYSECTION:
   19148     default:
   19149       break;
   19150     }
   19151 
   19152   return FALSE;
   19153 }
   19154 
   19155 /* Returns TRUE if NAME is an ARM mapping symbol.
   19156    Traditionally the symbols $a, $d and $t have been used.
   19157    The ARM ELF standard also defines $x (for A64 code).  It also allows a
   19158    period initiated suffix to be added to the symbol: "$[adtx]\.[:sym_char]+".
   19159    Other tools might also produce $b (Thumb BL), $f, $p, $m and $v, but we do
   19160    not support them here.  $t.x indicates the start of ThumbEE instructions.  */
   19161 
   19162 static bfd_boolean
   19163 is_arm_mapping_symbol (const char * name)
   19164 {
   19165   return name != NULL /* Paranoia.  */
   19166     && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
   19167 			 the mapping symbols could have acquired a prefix.
   19168 			 We do not support this here, since such symbols no
   19169 			 longer conform to the ARM ELF ABI.  */
   19170     && (name[1] == 'a' || name[1] == 'd' || name[1] == 't' || name[1] == 'x')
   19171     && (name[2] == 0 || name[2] == '.');
   19172   /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
   19173      any characters that follow the period are legal characters for the body
   19174      of a symbol's name.  For now we just assume that this is the case.  */
   19175 }
   19176 
   19177 /* Make sure that mapping symbols in object files are not removed via the
   19178    "strip --strip-unneeded" tool.  These symbols are needed in order to
   19179    correctly generate interworking veneers, and for byte swapping code
   19180    regions.  Once an object file has been linked, it is safe to remove the
   19181    symbols as they will no longer be needed.  */
   19182 
   19183 static void
   19184 elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
   19185 {
   19186   if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   19187       && sym->section != bfd_abs_section_ptr
   19188       && is_arm_mapping_symbol (sym->name))
   19189     sym->flags |= BSF_KEEP;
   19190 }
   19191 
   19192 #undef  elf_backend_copy_special_section_fields
   19193 #define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
   19194 
   19195 #define ELF_ARCH			bfd_arch_arm
   19196 #define ELF_TARGET_ID			ARM_ELF_DATA
   19197 #define ELF_MACHINE_CODE		EM_ARM
   19198 #ifdef __QNXTARGET__
   19199 #define ELF_MAXPAGESIZE			0x1000
   19200 #else
   19201 #define ELF_MAXPAGESIZE			0x10000
   19202 #endif
   19203 #define ELF_MINPAGESIZE			0x1000
   19204 #define ELF_COMMONPAGESIZE		0x1000
   19205 
   19206 #define bfd_elf32_mkobject			elf32_arm_mkobject
   19207 
   19208 #define bfd_elf32_bfd_copy_private_bfd_data	elf32_arm_copy_private_bfd_data
   19209 #define bfd_elf32_bfd_merge_private_bfd_data	elf32_arm_merge_private_bfd_data
   19210 #define bfd_elf32_bfd_set_private_flags		elf32_arm_set_private_flags
   19211 #define bfd_elf32_bfd_print_private_bfd_data	elf32_arm_print_private_bfd_data
   19212 #define bfd_elf32_bfd_link_hash_table_create	elf32_arm_link_hash_table_create
   19213 #define bfd_elf32_bfd_reloc_type_lookup		elf32_arm_reloc_type_lookup
   19214 #define bfd_elf32_bfd_reloc_name_lookup		elf32_arm_reloc_name_lookup
   19215 #define bfd_elf32_find_nearest_line		elf32_arm_find_nearest_line
   19216 #define bfd_elf32_find_inliner_info		elf32_arm_find_inliner_info
   19217 #define bfd_elf32_new_section_hook		elf32_arm_new_section_hook
   19218 #define bfd_elf32_bfd_is_target_special_symbol	elf32_arm_is_target_special_symbol
   19219 #define bfd_elf32_bfd_final_link		elf32_arm_final_link
   19220 #define bfd_elf32_get_synthetic_symtab	elf32_arm_get_synthetic_symtab
   19221 
   19222 #define elf_backend_get_symbol_type		elf32_arm_get_symbol_type
   19223 #define elf_backend_gc_mark_hook		elf32_arm_gc_mark_hook
   19224 #define elf_backend_gc_mark_extra_sections	elf32_arm_gc_mark_extra_sections
   19225 #define elf_backend_check_relocs		elf32_arm_check_relocs
   19226 #define elf_backend_update_relocs		elf32_arm_update_relocs
   19227 #define elf_backend_relocate_section		elf32_arm_relocate_section
   19228 #define elf_backend_write_section		elf32_arm_write_section
   19229 #define elf_backend_adjust_dynamic_symbol	elf32_arm_adjust_dynamic_symbol
   19230 #define elf_backend_create_dynamic_sections	elf32_arm_create_dynamic_sections
   19231 #define elf_backend_finish_dynamic_symbol	elf32_arm_finish_dynamic_symbol
   19232 #define elf_backend_finish_dynamic_sections	elf32_arm_finish_dynamic_sections
   19233 #define elf_backend_size_dynamic_sections	elf32_arm_size_dynamic_sections
   19234 #define elf_backend_always_size_sections	elf32_arm_always_size_sections
   19235 #define elf_backend_init_index_section		_bfd_elf_init_2_index_sections
   19236 #define elf_backend_post_process_headers	elf32_arm_post_process_headers
   19237 #define elf_backend_reloc_type_class		elf32_arm_reloc_type_class
   19238 #define elf_backend_object_p			elf32_arm_object_p
   19239 #define elf_backend_fake_sections		elf32_arm_fake_sections
   19240 #define elf_backend_section_from_shdr		elf32_arm_section_from_shdr
   19241 #define elf_backend_final_write_processing	elf32_arm_final_write_processing
   19242 #define elf_backend_copy_indirect_symbol	elf32_arm_copy_indirect_symbol
   19243 #define elf_backend_size_info			elf32_arm_size_info
   19244 #define elf_backend_modify_segment_map		elf32_arm_modify_segment_map
   19245 #define elf_backend_additional_program_headers	elf32_arm_additional_program_headers
   19246 #define elf_backend_output_arch_local_syms	elf32_arm_output_arch_local_syms
   19247 #define elf_backend_filter_implib_symbols	elf32_arm_filter_implib_symbols
   19248 #define elf_backend_begin_write_processing	elf32_arm_begin_write_processing
   19249 #define elf_backend_add_symbol_hook		elf32_arm_add_symbol_hook
   19250 #define elf_backend_count_additional_relocs	elf32_arm_count_additional_relocs
   19251 #define elf_backend_symbol_processing		elf32_arm_backend_symbol_processing
   19252 
   19253 #define elf_backend_can_refcount       1
   19254 #define elf_backend_can_gc_sections    1
   19255 #define elf_backend_plt_readonly       1
   19256 #define elf_backend_want_got_plt       1
   19257 #define elf_backend_want_plt_sym       0
   19258 #define elf_backend_want_dynrelro      1
   19259 #define elf_backend_may_use_rel_p      1
   19260 #define elf_backend_may_use_rela_p     0
   19261 #define elf_backend_default_use_rela_p 0
   19262 #define elf_backend_dtrel_excludes_plt 1
   19263 
   19264 #define elf_backend_got_header_size	12
   19265 #define elf_backend_extern_protected_data 1
   19266 
   19267 #undef	elf_backend_obj_attrs_vendor
   19268 #define elf_backend_obj_attrs_vendor		"aeabi"
   19269 #undef	elf_backend_obj_attrs_section
   19270 #define elf_backend_obj_attrs_section		".ARM.attributes"
   19271 #undef	elf_backend_obj_attrs_arg_type
   19272 #define elf_backend_obj_attrs_arg_type		elf32_arm_obj_attrs_arg_type
   19273 #undef	elf_backend_obj_attrs_section_type
   19274 #define elf_backend_obj_attrs_section_type	SHT_ARM_ATTRIBUTES
   19275 #define elf_backend_obj_attrs_order		elf32_arm_obj_attrs_order
   19276 #define elf_backend_obj_attrs_handle_unknown	elf32_arm_obj_attrs_handle_unknown
   19277 
   19278 #undef	elf_backend_section_flags
   19279 #define elf_backend_section_flags		elf32_arm_section_flags
   19280 #undef	elf_backend_lookup_section_flags_hook
   19281 #define elf_backend_lookup_section_flags_hook	elf32_arm_lookup_section_flags
   19282 
   19283 #define elf_backend_linux_prpsinfo32_ugid16	TRUE
   19284 
   19285 #include "elf32-target.h"
   19286 
   19287 /* Native Client targets.  */
   19288 
   19289 #undef	TARGET_LITTLE_SYM
   19290 #define TARGET_LITTLE_SYM		arm_elf32_nacl_le_vec
   19291 #undef	TARGET_LITTLE_NAME
   19292 #define TARGET_LITTLE_NAME		"elf32-littlearm-nacl"
   19293 #undef	TARGET_BIG_SYM
   19294 #define TARGET_BIG_SYM			arm_elf32_nacl_be_vec
   19295 #undef	TARGET_BIG_NAME
   19296 #define TARGET_BIG_NAME			"elf32-bigarm-nacl"
   19297 
   19298 /* Like elf32_arm_link_hash_table_create -- but overrides
   19299    appropriately for NaCl.  */
   19300 
   19301 static struct bfd_link_hash_table *
   19302 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
   19303 {
   19304   struct bfd_link_hash_table *ret;
   19305 
   19306   ret = elf32_arm_link_hash_table_create (abfd);
   19307   if (ret)
   19308     {
   19309       struct elf32_arm_link_hash_table *htab
   19310 	= (struct elf32_arm_link_hash_table *) ret;
   19311 
   19312       htab->nacl_p = 1;
   19313 
   19314       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
   19315       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
   19316     }
   19317   return ret;
   19318 }
   19319 
   19320 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
   19321    really need to use elf32_arm_modify_segment_map.  But we do it
   19322    anyway just to reduce gratuitous differences with the stock ARM backend.  */
   19323 
   19324 static bfd_boolean
   19325 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
   19326 {
   19327   return (elf32_arm_modify_segment_map (abfd, info)
   19328 	  && nacl_modify_segment_map (abfd, info));
   19329 }
   19330 
   19331 static void
   19332 elf32_arm_nacl_final_write_processing (bfd *abfd, bfd_boolean linker)
   19333 {
   19334   elf32_arm_final_write_processing (abfd, linker);
   19335   nacl_final_write_processing (abfd, linker);
   19336 }
   19337 
   19338 static bfd_vma
   19339 elf32_arm_nacl_plt_sym_val (bfd_vma i, const asection *plt,
   19340 			    const arelent *rel ATTRIBUTE_UNUSED)
   19341 {
   19342   return plt->vma
   19343     + 4 * (ARRAY_SIZE (elf32_arm_nacl_plt0_entry) +
   19344 	   i * ARRAY_SIZE (elf32_arm_nacl_plt_entry));
   19345 }
   19346 
   19347 #undef	elf32_bed
   19348 #define elf32_bed				elf32_arm_nacl_bed
   19349 #undef  bfd_elf32_bfd_link_hash_table_create
   19350 #define bfd_elf32_bfd_link_hash_table_create	\
   19351   elf32_arm_nacl_link_hash_table_create
   19352 #undef	elf_backend_plt_alignment
   19353 #define elf_backend_plt_alignment		4
   19354 #undef	elf_backend_modify_segment_map
   19355 #define	elf_backend_modify_segment_map		elf32_arm_nacl_modify_segment_map
   19356 #undef	elf_backend_modify_program_headers
   19357 #define	elf_backend_modify_program_headers	nacl_modify_program_headers
   19358 #undef  elf_backend_final_write_processing
   19359 #define elf_backend_final_write_processing	elf32_arm_nacl_final_write_processing
   19360 #undef bfd_elf32_get_synthetic_symtab
   19361 #undef  elf_backend_plt_sym_val
   19362 #define elf_backend_plt_sym_val			elf32_arm_nacl_plt_sym_val
   19363 #undef  elf_backend_copy_special_section_fields
   19364 
   19365 #undef	ELF_MINPAGESIZE
   19366 #undef	ELF_COMMONPAGESIZE
   19367 
   19368 
   19369 #include "elf32-target.h"
   19370 
   19371 /* Reset to defaults.  */
   19372 #undef	elf_backend_plt_alignment
   19373 #undef	elf_backend_modify_segment_map
   19374 #define elf_backend_modify_segment_map		elf32_arm_modify_segment_map
   19375 #undef	elf_backend_modify_program_headers
   19376 #undef  elf_backend_final_write_processing
   19377 #define elf_backend_final_write_processing	elf32_arm_final_write_processing
   19378 #undef	ELF_MINPAGESIZE
   19379 #define ELF_MINPAGESIZE			0x1000
   19380 #undef	ELF_COMMONPAGESIZE
   19381 #define ELF_COMMONPAGESIZE		0x1000
   19382 
   19383 
   19384 /* VxWorks Targets.  */
   19385 
   19386 #undef	TARGET_LITTLE_SYM
   19387 #define TARGET_LITTLE_SYM		arm_elf32_vxworks_le_vec
   19388 #undef	TARGET_LITTLE_NAME
   19389 #define TARGET_LITTLE_NAME		"elf32-littlearm-vxworks"
   19390 #undef	TARGET_BIG_SYM
   19391 #define TARGET_BIG_SYM			arm_elf32_vxworks_be_vec
   19392 #undef	TARGET_BIG_NAME
   19393 #define TARGET_BIG_NAME			"elf32-bigarm-vxworks"
   19394 
   19395 /* Like elf32_arm_link_hash_table_create -- but overrides
   19396    appropriately for VxWorks.  */
   19397 
   19398 static struct bfd_link_hash_table *
   19399 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
   19400 {
   19401   struct bfd_link_hash_table *ret;
   19402 
   19403   ret = elf32_arm_link_hash_table_create (abfd);
   19404   if (ret)
   19405     {
   19406       struct elf32_arm_link_hash_table *htab
   19407 	= (struct elf32_arm_link_hash_table *) ret;
   19408       htab->use_rel = 0;
   19409       htab->vxworks_p = 1;
   19410     }
   19411   return ret;
   19412 }
   19413 
   19414 static void
   19415 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
   19416 {
   19417   elf32_arm_final_write_processing (abfd, linker);
   19418   elf_vxworks_final_write_processing (abfd, linker);
   19419 }
   19420 
   19421 #undef  elf32_bed
   19422 #define elf32_bed elf32_arm_vxworks_bed
   19423 
   19424 #undef  bfd_elf32_bfd_link_hash_table_create
   19425 #define bfd_elf32_bfd_link_hash_table_create	elf32_arm_vxworks_link_hash_table_create
   19426 #undef  elf_backend_final_write_processing
   19427 #define elf_backend_final_write_processing	elf32_arm_vxworks_final_write_processing
   19428 #undef  elf_backend_emit_relocs
   19429 #define elf_backend_emit_relocs			elf_vxworks_emit_relocs
   19430 
   19431 #undef  elf_backend_may_use_rel_p
   19432 #define elf_backend_may_use_rel_p	0
   19433 #undef  elf_backend_may_use_rela_p
   19434 #define elf_backend_may_use_rela_p	1
   19435 #undef  elf_backend_default_use_rela_p
   19436 #define elf_backend_default_use_rela_p	1
   19437 #undef  elf_backend_want_plt_sym
   19438 #define elf_backend_want_plt_sym	1
   19439 #undef  ELF_MAXPAGESIZE
   19440 #define ELF_MAXPAGESIZE			0x1000
   19441 
   19442 #include "elf32-target.h"
   19443 
   19444 
   19445 /* Merge backend specific data from an object file to the output
   19446    object file when linking.  */
   19447 
   19448 static bfd_boolean
   19449 elf32_arm_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   19450 {
   19451   bfd *obfd = info->output_bfd;
   19452   flagword out_flags;
   19453   flagword in_flags;
   19454   bfd_boolean flags_compatible = TRUE;
   19455   asection *sec;
   19456 
   19457   /* Check if we have the same endianness.  */
   19458   if (! _bfd_generic_verify_endian_match (ibfd, info))
   19459     return FALSE;
   19460 
   19461   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
   19462     return TRUE;
   19463 
   19464   if (!elf32_arm_merge_eabi_attributes (ibfd, info))
   19465     return FALSE;
   19466 
   19467   /* The input BFD must have had its flags initialised.  */
   19468   /* The following seems bogus to me -- The flags are initialized in
   19469      the assembler but I don't think an elf_flags_init field is
   19470      written into the object.  */
   19471   /* BFD_ASSERT (elf_flags_init (ibfd)); */
   19472 
   19473   in_flags  = elf_elfheader (ibfd)->e_flags;
   19474   out_flags = elf_elfheader (obfd)->e_flags;
   19475 
   19476   /* In theory there is no reason why we couldn't handle this.  However
   19477      in practice it isn't even close to working and there is no real
   19478      reason to want it.  */
   19479   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
   19480       && !(ibfd->flags & DYNAMIC)
   19481       && (in_flags & EF_ARM_BE8))
   19482     {
   19483       _bfd_error_handler (_("error: %B is already in final BE8 format"),
   19484 			  ibfd);
   19485       return FALSE;
   19486     }
   19487 
   19488   if (!elf_flags_init (obfd))
   19489     {
   19490       /* If the input is the default architecture and had the default
   19491 	 flags then do not bother setting the flags for the output
   19492 	 architecture, instead allow future merges to do this.  If no
   19493 	 future merges ever set these flags then they will retain their
   19494 	 uninitialised values, which surprise surprise, correspond
   19495 	 to the default values.  */
   19496       if (bfd_get_arch_info (ibfd)->the_default
   19497 	  && elf_elfheader (ibfd)->e_flags == 0)
   19498 	return TRUE;
   19499 
   19500       elf_flags_init (obfd) = TRUE;
   19501       elf_elfheader (obfd)->e_flags = in_flags;
   19502 
   19503       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   19504 	  && bfd_get_arch_info (obfd)->the_default)
   19505 	return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
   19506 
   19507       return TRUE;
   19508     }
   19509 
   19510   /* Determine what should happen if the input ARM architecture
   19511      does not match the output ARM architecture.  */
   19512   if (! bfd_arm_merge_machines (ibfd, obfd))
   19513     return FALSE;
   19514 
   19515   /* Identical flags must be compatible.  */
   19516   if (in_flags == out_flags)
   19517     return TRUE;
   19518 
   19519   /* Check to see if the input BFD actually contains any sections.  If
   19520      not, its flags may not have been initialised either, but it
   19521      cannot actually cause any incompatiblity.  Do not short-circuit
   19522      dynamic objects; their section list may be emptied by
   19523     elf_link_add_object_symbols.
   19524 
   19525     Also check to see if there are no code sections in the input.
   19526     In this case there is no need to check for code specific flags.
   19527     XXX - do we need to worry about floating-point format compatability
   19528     in data sections ?  */
   19529   if (!(ibfd->flags & DYNAMIC))
   19530     {
   19531       bfd_boolean null_input_bfd = TRUE;
   19532       bfd_boolean only_data_sections = TRUE;
   19533 
   19534       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   19535 	{
   19536 	  /* Ignore synthetic glue sections.  */
   19537 	  if (strcmp (sec->name, ".glue_7")
   19538 	      && strcmp (sec->name, ".glue_7t"))
   19539 	    {
   19540 	      if ((bfd_get_section_flags (ibfd, sec)
   19541 		   & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   19542 		  == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   19543 		only_data_sections = FALSE;
   19544 
   19545 	      null_input_bfd = FALSE;
   19546 	      break;
   19547 	    }
   19548 	}
   19549 
   19550       if (null_input_bfd || only_data_sections)
   19551 	return TRUE;
   19552     }
   19553 
   19554   /* Complain about various flag mismatches.  */
   19555   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
   19556 				      EF_ARM_EABI_VERSION (out_flags)))
   19557     {
   19558       _bfd_error_handler
   19559 	(_("error: Source object %B has EABI version %d, but target %B has EABI version %d"),
   19560 	 ibfd, (in_flags & EF_ARM_EABIMASK) >> 24,
   19561 	 obfd, (out_flags & EF_ARM_EABIMASK) >> 24);
   19562       return FALSE;
   19563     }
   19564 
   19565   /* Not sure what needs to be checked for EABI versions >= 1.  */
   19566   /* VxWorks libraries do not use these flags.  */
   19567   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
   19568       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
   19569       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
   19570     {
   19571       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
   19572 	{
   19573 	  _bfd_error_handler
   19574 	    (_("error: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
   19575 	     ibfd, in_flags & EF_ARM_APCS_26 ? 26 : 32,
   19576 	     obfd, out_flags & EF_ARM_APCS_26 ? 26 : 32);
   19577 	  flags_compatible = FALSE;
   19578 	}
   19579 
   19580       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
   19581 	{
   19582 	  if (in_flags & EF_ARM_APCS_FLOAT)
   19583 	    _bfd_error_handler
   19584 	      (_("error: %B passes floats in float registers, whereas %B passes them in integer registers"),
   19585 	       ibfd, obfd);
   19586 	  else
   19587 	    _bfd_error_handler
   19588 	      (_("error: %B passes floats in integer registers, whereas %B passes them in float registers"),
   19589 	       ibfd, obfd);
   19590 
   19591 	  flags_compatible = FALSE;
   19592 	}
   19593 
   19594       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
   19595 	{
   19596 	  if (in_flags & EF_ARM_VFP_FLOAT)
   19597 	    _bfd_error_handler
   19598 	      (_("error: %B uses VFP instructions, whereas %B does not"),
   19599 	       ibfd, obfd);
   19600 	  else
   19601 	    _bfd_error_handler
   19602 	      (_("error: %B uses FPA instructions, whereas %B does not"),
   19603 	       ibfd, obfd);
   19604 
   19605 	  flags_compatible = FALSE;
   19606 	}
   19607 
   19608       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
   19609 	{
   19610 	  if (in_flags & EF_ARM_MAVERICK_FLOAT)
   19611 	    _bfd_error_handler
   19612 	      (_("error: %B uses Maverick instructions, whereas %B does not"),
   19613 	       ibfd, obfd);
   19614 	  else
   19615 	    _bfd_error_handler
   19616 	      (_("error: %B does not use Maverick instructions, whereas %B does"),
   19617 	       ibfd, obfd);
   19618 
   19619 	  flags_compatible = FALSE;
   19620 	}
   19621 
   19622 #ifdef EF_ARM_SOFT_FLOAT
   19623       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
   19624 	{
   19625 	  /* We can allow interworking between code that is VFP format
   19626 	     layout, and uses either soft float or integer regs for
   19627 	     passing floating point arguments and results.  We already
   19628 	     know that the APCS_FLOAT flags match; similarly for VFP
   19629 	     flags.  */
   19630 	  if ((in_flags & EF_ARM_APCS_FLOAT) != 0
   19631 	      || (in_flags & EF_ARM_VFP_FLOAT) == 0)
   19632 	    {
   19633 	      if (in_flags & EF_ARM_SOFT_FLOAT)
   19634 		_bfd_error_handler
   19635 		  (_("error: %B uses software FP, whereas %B uses hardware FP"),
   19636 		   ibfd, obfd);
   19637 	      else
   19638 		_bfd_error_handler
   19639 		  (_("error: %B uses hardware FP, whereas %B uses software FP"),
   19640 		   ibfd, obfd);
   19641 
   19642 	      flags_compatible = FALSE;
   19643 	    }
   19644 	}
   19645 #endif
   19646 
   19647       /* Interworking mismatch is only a warning.  */
   19648       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
   19649 	{
   19650 	  if (in_flags & EF_ARM_INTERWORK)
   19651 	    {
   19652 	      _bfd_error_handler
   19653 		(_("Warning: %B supports interworking, whereas %B does not"),
   19654 		 ibfd, obfd);
   19655 	    }
   19656 	  else
   19657 	    {
   19658 	      _bfd_error_handler
   19659 		(_("Warning: %B does not support interworking, whereas %B does"),
   19660 		 ibfd, obfd);
   19661 	    }
   19662 	}
   19663     }
   19664 
   19665   return flags_compatible;
   19666 }
   19667 
   19668 
   19669 /* Symbian OS Targets.  */
   19670 
   19671 #undef	TARGET_LITTLE_SYM
   19672 #define TARGET_LITTLE_SYM		arm_elf32_symbian_le_vec
   19673 #undef	TARGET_LITTLE_NAME
   19674 #define TARGET_LITTLE_NAME		"elf32-littlearm-symbian"
   19675 #undef	TARGET_BIG_SYM
   19676 #define TARGET_BIG_SYM			arm_elf32_symbian_be_vec
   19677 #undef	TARGET_BIG_NAME
   19678 #define TARGET_BIG_NAME			"elf32-bigarm-symbian"
   19679 
   19680 /* Like elf32_arm_link_hash_table_create -- but overrides
   19681    appropriately for Symbian OS.  */
   19682 
   19683 static struct bfd_link_hash_table *
   19684 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
   19685 {
   19686   struct bfd_link_hash_table *ret;
   19687 
   19688   ret = elf32_arm_link_hash_table_create (abfd);
   19689   if (ret)
   19690     {
   19691       struct elf32_arm_link_hash_table *htab
   19692 	= (struct elf32_arm_link_hash_table *)ret;
   19693       /* There is no PLT header for Symbian OS.  */
   19694       htab->plt_header_size = 0;
   19695       /* The PLT entries are each one instruction and one word.  */
   19696       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
   19697       htab->symbian_p = 1;
   19698       /* Symbian uses armv5t or above, so use_blx is always true.  */
   19699       htab->use_blx = 1;
   19700       htab->root.is_relocatable_executable = 1;
   19701     }
   19702   return ret;
   19703 }
   19704 
   19705 static const struct bfd_elf_special_section
   19706 elf32_arm_symbian_special_sections[] =
   19707 {
   19708   /* In a BPABI executable, the dynamic linking sections do not go in
   19709      the loadable read-only segment.  The post-linker may wish to
   19710      refer to these sections, but they are not part of the final
   19711      program image.  */
   19712   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  0 },
   19713   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  0 },
   19714   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  0 },
   19715   { STRING_COMMA_LEN (".got"),		 0, SHT_PROGBITS, 0 },
   19716   { STRING_COMMA_LEN (".hash"),		 0, SHT_HASH,	  0 },
   19717   /* These sections do not need to be writable as the SymbianOS
   19718      postlinker will arrange things so that no dynamic relocation is
   19719      required.  */
   19720   { STRING_COMMA_LEN (".init_array"),	 0, SHT_INIT_ARRAY,    SHF_ALLOC },
   19721   { STRING_COMMA_LEN (".fini_array"),	 0, SHT_FINI_ARRAY,    SHF_ALLOC },
   19722   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
   19723   { NULL,			      0, 0, 0,		       0 }
   19724 };
   19725 
   19726 static void
   19727 elf32_arm_symbian_begin_write_processing (bfd *abfd,
   19728 					  struct bfd_link_info *link_info)
   19729 {
   19730   /* BPABI objects are never loaded directly by an OS kernel; they are
   19731      processed by a postlinker first, into an OS-specific format.  If
   19732      the D_PAGED bit is set on the file, BFD will align segments on
   19733      page boundaries, so that an OS can directly map the file.  With
   19734      BPABI objects, that just results in wasted space.  In addition,
   19735      because we clear the D_PAGED bit, map_sections_to_segments will
   19736      recognize that the program headers should not be mapped into any
   19737      loadable segment.  */
   19738   abfd->flags &= ~D_PAGED;
   19739   elf32_arm_begin_write_processing (abfd, link_info);
   19740 }
   19741 
   19742 static bfd_boolean
   19743 elf32_arm_symbian_modify_segment_map (bfd *abfd,
   19744 				      struct bfd_link_info *info)
   19745 {
   19746   struct elf_segment_map *m;
   19747   asection *dynsec;
   19748 
   19749   /* BPABI shared libraries and executables should have a PT_DYNAMIC
   19750      segment.  However, because the .dynamic section is not marked
   19751      with SEC_LOAD, the generic ELF code will not create such a
   19752      segment.  */
   19753   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   19754   if (dynsec)
   19755     {
   19756       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   19757 	if (m->p_type == PT_DYNAMIC)
   19758 	  break;
   19759 
   19760       if (m == NULL)
   19761 	{
   19762 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   19763 	  m->next = elf_seg_map (abfd);
   19764 	  elf_seg_map (abfd) = m;
   19765 	}
   19766     }
   19767 
   19768   /* Also call the generic arm routine.  */
   19769   return elf32_arm_modify_segment_map (abfd, info);
   19770 }
   19771 
   19772 /* Return address for Ith PLT stub in section PLT, for relocation REL
   19773    or (bfd_vma) -1 if it should not be included.  */
   19774 
   19775 static bfd_vma
   19776 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
   19777 			       const arelent *rel ATTRIBUTE_UNUSED)
   19778 {
   19779   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
   19780 }
   19781 
   19782 #undef  elf32_bed
   19783 #define elf32_bed elf32_arm_symbian_bed
   19784 
   19785 /* The dynamic sections are not allocated on SymbianOS; the postlinker
   19786    will process them and then discard them.  */
   19787 #undef  ELF_DYNAMIC_SEC_FLAGS
   19788 #define ELF_DYNAMIC_SEC_FLAGS \
   19789   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
   19790 
   19791 #undef elf_backend_emit_relocs
   19792 
   19793 #undef  bfd_elf32_bfd_link_hash_table_create
   19794 #define bfd_elf32_bfd_link_hash_table_create	elf32_arm_symbian_link_hash_table_create
   19795 #undef  elf_backend_special_sections
   19796 #define elf_backend_special_sections		elf32_arm_symbian_special_sections
   19797 #undef  elf_backend_begin_write_processing
   19798 #define elf_backend_begin_write_processing	elf32_arm_symbian_begin_write_processing
   19799 #undef  elf_backend_final_write_processing
   19800 #define elf_backend_final_write_processing	elf32_arm_final_write_processing
   19801 
   19802 #undef  elf_backend_modify_segment_map
   19803 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
   19804 
   19805 /* There is no .got section for BPABI objects, and hence no header.  */
   19806 #undef  elf_backend_got_header_size
   19807 #define elf_backend_got_header_size 0
   19808 
   19809 /* Similarly, there is no .got.plt section.  */
   19810 #undef  elf_backend_want_got_plt
   19811 #define elf_backend_want_got_plt 0
   19812 
   19813 #undef  elf_backend_plt_sym_val
   19814 #define elf_backend_plt_sym_val		elf32_arm_symbian_plt_sym_val
   19815 
   19816 #undef  elf_backend_may_use_rel_p
   19817 #define elf_backend_may_use_rel_p	1
   19818 #undef  elf_backend_may_use_rela_p
   19819 #define elf_backend_may_use_rela_p	0
   19820 #undef  elf_backend_default_use_rela_p
   19821 #define elf_backend_default_use_rela_p	0
   19822 #undef  elf_backend_want_plt_sym
   19823 #define elf_backend_want_plt_sym	0
   19824 #undef  elf_backend_dtrel_excludes_plt
   19825 #define elf_backend_dtrel_excludes_plt	0
   19826 #undef  ELF_MAXPAGESIZE
   19827 #define ELF_MAXPAGESIZE			0x8000
   19828 
   19829 #include "elf32-target.h"
   19830