Home | History | Annotate | Line # | Download | only in bfd
elf32-m68k.c revision 1.15.2.1
      1 /* Motorola 68k series support for 32-bit ELF
      2    Copyright (C) 1993-2024 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 "bfd.h"
     23 #include "bfdlink.h"
     24 #include "libbfd.h"
     25 #include "elf-bfd.h"
     26 #include "elf/m68k.h"
     27 #include "opcode/m68k.h"
     28 #include "cpu-m68k.h"
     29 #include "elf32-m68k.h"
     30 
     31 static bool
     32 elf_m68k_discard_copies (struct elf_link_hash_entry *, void *);
     33 
     34 static reloc_howto_type howto_table[] =
     35 {
     36   HOWTO(R_68K_NONE,	  0, 0, 0, false,0, complain_overflow_dont,	bfd_elf_generic_reloc, "R_68K_NONE",	  false, 0, 0x00000000,false),
     37   HOWTO(R_68K_32,	  0, 4,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32",	  false, 0, 0xffffffff,false),
     38   HOWTO(R_68K_16,	  0, 2,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16",	  false, 0, 0x0000ffff,false),
     39   HOWTO(R_68K_8,	  0, 1, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8",	  false, 0, 0x000000ff,false),
     40   HOWTO(R_68K_PC32,	  0, 4,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32",	  false, 0, 0xffffffff,true),
     41   HOWTO(R_68K_PC16,	  0, 2,16, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PC16",	  false, 0, 0x0000ffff,true),
     42   HOWTO(R_68K_PC8,	  0, 1, 8, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PC8",	  false, 0, 0x000000ff,true),
     43   HOWTO(R_68K_GOT32,	  0, 4,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32",	  false, 0, 0xffffffff,true),
     44   HOWTO(R_68K_GOT16,	  0, 2,16, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_GOT16",	  false, 0, 0x0000ffff,true),
     45   HOWTO(R_68K_GOT8,	  0, 1, 8, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_GOT8",	  false, 0, 0x000000ff,true),
     46   HOWTO(R_68K_GOT32O,	  0, 4,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O",	  false, 0, 0xffffffff,false),
     47   HOWTO(R_68K_GOT16O,	  0, 2,16, false,0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_GOT16O",	  false, 0, 0x0000ffff,false),
     48   HOWTO(R_68K_GOT8O,	  0, 1, 8, false,0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_GOT8O",	  false, 0, 0x000000ff,false),
     49   HOWTO(R_68K_PLT32,	  0, 4,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32",	  false, 0, 0xffffffff,true),
     50   HOWTO(R_68K_PLT16,	  0, 2,16, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PLT16",	  false, 0, 0x0000ffff,true),
     51   HOWTO(R_68K_PLT8,	  0, 1, 8, true, 0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PLT8",	  false, 0, 0x000000ff,true),
     52   HOWTO(R_68K_PLT32O,	  0, 4,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O",	  false, 0, 0xffffffff,false),
     53   HOWTO(R_68K_PLT16O,	  0, 2,16, false,0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PLT16O",	  false, 0, 0x0000ffff,false),
     54   HOWTO(R_68K_PLT8O,	  0, 1, 8, false,0, complain_overflow_signed,	bfd_elf_generic_reloc, "R_68K_PLT8O",	  false, 0, 0x000000ff,false),
     55   HOWTO(R_68K_COPY,	  0, 0, 0, false,0, complain_overflow_dont,	bfd_elf_generic_reloc, "R_68K_COPY",	  false, 0, 0xffffffff,false),
     56   HOWTO(R_68K_GLOB_DAT,	  0, 4,32, false,0, complain_overflow_dont,	bfd_elf_generic_reloc, "R_68K_GLOB_DAT",  false, 0, 0xffffffff,false),
     57   HOWTO(R_68K_JMP_SLOT,	  0, 4,32, false,0, complain_overflow_dont,	bfd_elf_generic_reloc, "R_68K_JMP_SLOT",  false, 0, 0xffffffff,false),
     58   HOWTO(R_68K_RELATIVE,	  0, 4,32, false,0, complain_overflow_dont,	bfd_elf_generic_reloc, "R_68K_RELATIVE",  false, 0, 0xffffffff,false),
     59   /* GNU extension to record C++ vtable hierarchy.  */
     60   HOWTO (R_68K_GNU_VTINHERIT,	/* type */
     61 	 0,			/* rightshift */
     62 	 4,			/* size */
     63 	 0,			/* bitsize */
     64 	 false,			/* pc_relative */
     65 	 0,			/* bitpos */
     66 	 complain_overflow_dont, /* complain_on_overflow */
     67 	 NULL,			/* special_function */
     68 	 "R_68K_GNU_VTINHERIT",	/* name */
     69 	 false,			/* partial_inplace */
     70 	 0,			/* src_mask */
     71 	 0,			/* dst_mask */
     72 	 false),
     73   /* GNU extension to record C++ vtable member usage.  */
     74   HOWTO (R_68K_GNU_VTENTRY,	/* type */
     75 	 0,			/* rightshift */
     76 	 4,			/* size */
     77 	 0,			/* bitsize */
     78 	 false,			/* pc_relative */
     79 	 0,			/* bitpos */
     80 	 complain_overflow_dont, /* complain_on_overflow */
     81 	 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
     82 	 "R_68K_GNU_VTENTRY",	/* name */
     83 	 false,			/* partial_inplace */
     84 	 0,			/* src_mask */
     85 	 0,			/* dst_mask */
     86 	 false),
     87 
     88   /* TLS general dynamic variable reference.  */
     89   HOWTO (R_68K_TLS_GD32,	/* type */
     90 	 0,			/* rightshift */
     91 	 4,			/* size */
     92 	 32,			/* bitsize */
     93 	 false,			/* pc_relative */
     94 	 0,			/* bitpos */
     95 	 complain_overflow_bitfield, /* complain_on_overflow */
     96 	 bfd_elf_generic_reloc, /* special_function */
     97 	 "R_68K_TLS_GD32",	/* name */
     98 	 false,			/* partial_inplace */
     99 	 0,			/* src_mask */
    100 	 0xffffffff,		/* dst_mask */
    101 	 false),		/* pcrel_offset */
    102 
    103   HOWTO (R_68K_TLS_GD16,	/* type */
    104 	 0,			/* rightshift */
    105 	 2,			/* size */
    106 	 16,			/* bitsize */
    107 	 false,			/* pc_relative */
    108 	 0,			/* bitpos */
    109 	 complain_overflow_signed, /* complain_on_overflow */
    110 	 bfd_elf_generic_reloc, /* special_function */
    111 	 "R_68K_TLS_GD16",	/* name */
    112 	 false,			/* partial_inplace */
    113 	 0,			/* src_mask */
    114 	 0x0000ffff,		/* dst_mask */
    115 	 false),		/* pcrel_offset */
    116 
    117   HOWTO (R_68K_TLS_GD8,		/* type */
    118 	 0,			/* rightshift */
    119 	 1,			/* size */
    120 	 8,			/* bitsize */
    121 	 false,			/* pc_relative */
    122 	 0,			/* bitpos */
    123 	 complain_overflow_signed, /* complain_on_overflow */
    124 	 bfd_elf_generic_reloc, /* special_function */
    125 	 "R_68K_TLS_GD8",	/* name */
    126 	 false,			/* partial_inplace */
    127 	 0,			/* src_mask */
    128 	 0x000000ff,		/* dst_mask */
    129 	 false),		/* pcrel_offset */
    130 
    131   /* TLS local dynamic variable reference.  */
    132   HOWTO (R_68K_TLS_LDM32,	/* type */
    133 	 0,			/* rightshift */
    134 	 4,			/* size */
    135 	 32,			/* bitsize */
    136 	 false,			/* pc_relative */
    137 	 0,			/* bitpos */
    138 	 complain_overflow_bitfield, /* complain_on_overflow */
    139 	 bfd_elf_generic_reloc, /* special_function */
    140 	 "R_68K_TLS_LDM32",	/* name */
    141 	 false,			/* partial_inplace */
    142 	 0,			/* src_mask */
    143 	 0xffffffff,		/* dst_mask */
    144 	 false),		/* pcrel_offset */
    145 
    146   HOWTO (R_68K_TLS_LDM16,	/* type */
    147 	 0,			/* rightshift */
    148 	 2,			/* size */
    149 	 16,			/* bitsize */
    150 	 false,			/* pc_relative */
    151 	 0,			/* bitpos */
    152 	 complain_overflow_signed, /* complain_on_overflow */
    153 	 bfd_elf_generic_reloc, /* special_function */
    154 	 "R_68K_TLS_LDM16",	/* name */
    155 	 false,			/* partial_inplace */
    156 	 0,			/* src_mask */
    157 	 0x0000ffff,		/* dst_mask */
    158 	 false),		/* pcrel_offset */
    159 
    160   HOWTO (R_68K_TLS_LDM8,		/* type */
    161 	 0,			/* rightshift */
    162 	 1,			/* size */
    163 	 8,			/* bitsize */
    164 	 false,			/* pc_relative */
    165 	 0,			/* bitpos */
    166 	 complain_overflow_signed, /* complain_on_overflow */
    167 	 bfd_elf_generic_reloc, /* special_function */
    168 	 "R_68K_TLS_LDM8",	/* name */
    169 	 false,			/* partial_inplace */
    170 	 0,			/* src_mask */
    171 	 0x000000ff,		/* dst_mask */
    172 	 false),		/* pcrel_offset */
    173 
    174   HOWTO (R_68K_TLS_LDO32,	/* type */
    175 	 0,			/* rightshift */
    176 	 4,			/* size */
    177 	 32,			/* bitsize */
    178 	 false,			/* pc_relative */
    179 	 0,			/* bitpos */
    180 	 complain_overflow_bitfield, /* complain_on_overflow */
    181 	 bfd_elf_generic_reloc, /* special_function */
    182 	 "R_68K_TLS_LDO32",	/* name */
    183 	 false,			/* partial_inplace */
    184 	 0,			/* src_mask */
    185 	 0xffffffff,		/* dst_mask */
    186 	 false),		/* pcrel_offset */
    187 
    188   HOWTO (R_68K_TLS_LDO16,	/* type */
    189 	 0,			/* rightshift */
    190 	 2,			/* size */
    191 	 16,			/* bitsize */
    192 	 false,			/* pc_relative */
    193 	 0,			/* bitpos */
    194 	 complain_overflow_signed, /* complain_on_overflow */
    195 	 bfd_elf_generic_reloc, /* special_function */
    196 	 "R_68K_TLS_LDO16",	/* name */
    197 	 false,			/* partial_inplace */
    198 	 0,			/* src_mask */
    199 	 0x0000ffff,		/* dst_mask */
    200 	 false),		/* pcrel_offset */
    201 
    202   HOWTO (R_68K_TLS_LDO8,		/* type */
    203 	 0,			/* rightshift */
    204 	 1,			/* size */
    205 	 8,			/* bitsize */
    206 	 false,			/* pc_relative */
    207 	 0,			/* bitpos */
    208 	 complain_overflow_signed, /* complain_on_overflow */
    209 	 bfd_elf_generic_reloc, /* special_function */
    210 	 "R_68K_TLS_LDO8",	/* name */
    211 	 false,			/* partial_inplace */
    212 	 0,			/* src_mask */
    213 	 0x000000ff,		/* dst_mask */
    214 	 false),		/* pcrel_offset */
    215 
    216   /* TLS initial execution variable reference.  */
    217   HOWTO (R_68K_TLS_IE32,	/* type */
    218 	 0,			/* rightshift */
    219 	 4,			/* size */
    220 	 32,			/* bitsize */
    221 	 false,			/* pc_relative */
    222 	 0,			/* bitpos */
    223 	 complain_overflow_bitfield, /* complain_on_overflow */
    224 	 bfd_elf_generic_reloc, /* special_function */
    225 	 "R_68K_TLS_IE32",	/* name */
    226 	 false,			/* partial_inplace */
    227 	 0,			/* src_mask */
    228 	 0xffffffff,		/* dst_mask */
    229 	 false),		/* pcrel_offset */
    230 
    231   HOWTO (R_68K_TLS_IE16,	/* type */
    232 	 0,			/* rightshift */
    233 	 2,			/* size */
    234 	 16,			/* bitsize */
    235 	 false,			/* pc_relative */
    236 	 0,			/* bitpos */
    237 	 complain_overflow_signed, /* complain_on_overflow */
    238 	 bfd_elf_generic_reloc, /* special_function */
    239 	 "R_68K_TLS_IE16",	/* name */
    240 	 false,			/* partial_inplace */
    241 	 0,			/* src_mask */
    242 	 0x0000ffff,		/* dst_mask */
    243 	 false),		/* pcrel_offset */
    244 
    245   HOWTO (R_68K_TLS_IE8,		/* type */
    246 	 0,			/* rightshift */
    247 	 1,			/* size */
    248 	 8,			/* bitsize */
    249 	 false,			/* pc_relative */
    250 	 0,			/* bitpos */
    251 	 complain_overflow_signed, /* complain_on_overflow */
    252 	 bfd_elf_generic_reloc, /* special_function */
    253 	 "R_68K_TLS_IE8",	/* name */
    254 	 false,			/* partial_inplace */
    255 	 0,			/* src_mask */
    256 	 0x000000ff,		/* dst_mask */
    257 	 false),		/* pcrel_offset */
    258 
    259   /* TLS local execution variable reference.  */
    260   HOWTO (R_68K_TLS_LE32,	/* type */
    261 	 0,			/* rightshift */
    262 	 4,			/* size */
    263 	 32,			/* bitsize */
    264 	 false,			/* pc_relative */
    265 	 0,			/* bitpos */
    266 	 complain_overflow_bitfield, /* complain_on_overflow */
    267 	 bfd_elf_generic_reloc, /* special_function */
    268 	 "R_68K_TLS_LE32",	/* name */
    269 	 false,			/* partial_inplace */
    270 	 0,			/* src_mask */
    271 	 0xffffffff,		/* dst_mask */
    272 	 false),		/* pcrel_offset */
    273 
    274   HOWTO (R_68K_TLS_LE16,	/* type */
    275 	 0,			/* rightshift */
    276 	 2,			/* size */
    277 	 16,			/* bitsize */
    278 	 false,			/* pc_relative */
    279 	 0,			/* bitpos */
    280 	 complain_overflow_signed, /* complain_on_overflow */
    281 	 bfd_elf_generic_reloc, /* special_function */
    282 	 "R_68K_TLS_LE16",	/* name */
    283 	 false,			/* partial_inplace */
    284 	 0,			/* src_mask */
    285 	 0x0000ffff,		/* dst_mask */
    286 	 false),		/* pcrel_offset */
    287 
    288   HOWTO (R_68K_TLS_LE8,		/* type */
    289 	 0,			/* rightshift */
    290 	 1,			/* size */
    291 	 8,			/* bitsize */
    292 	 false,			/* pc_relative */
    293 	 0,			/* bitpos */
    294 	 complain_overflow_signed, /* complain_on_overflow */
    295 	 bfd_elf_generic_reloc, /* special_function */
    296 	 "R_68K_TLS_LE8",	/* name */
    297 	 false,			/* partial_inplace */
    298 	 0,			/* src_mask */
    299 	 0x000000ff,		/* dst_mask */
    300 	 false),		/* pcrel_offset */
    301 
    302   /* TLS GD/LD dynamic relocations.  */
    303   HOWTO (R_68K_TLS_DTPMOD32,	/* type */
    304 	 0,			/* rightshift */
    305 	 4,			/* size */
    306 	 32,			/* bitsize */
    307 	 false,			/* pc_relative */
    308 	 0,			/* bitpos */
    309 	 complain_overflow_dont, /* complain_on_overflow */
    310 	 bfd_elf_generic_reloc, /* special_function */
    311 	 "R_68K_TLS_DTPMOD32",	/* name */
    312 	 false,			/* partial_inplace */
    313 	 0,			/* src_mask */
    314 	 0xffffffff,		/* dst_mask */
    315 	 false),		/* pcrel_offset */
    316 
    317   HOWTO (R_68K_TLS_DTPREL32,	/* type */
    318 	 0,			/* rightshift */
    319 	 4,			/* size */
    320 	 32,			/* bitsize */
    321 	 false,			/* pc_relative */
    322 	 0,			/* bitpos */
    323 	 complain_overflow_dont, /* complain_on_overflow */
    324 	 bfd_elf_generic_reloc, /* special_function */
    325 	 "R_68K_TLS_DTPREL32",	/* name */
    326 	 false,			/* partial_inplace */
    327 	 0,			/* src_mask */
    328 	 0xffffffff,		/* dst_mask */
    329 	 false),		/* pcrel_offset */
    330 
    331   HOWTO (R_68K_TLS_TPREL32,	/* type */
    332 	 0,			/* rightshift */
    333 	 4,			/* size */
    334 	 32,			/* bitsize */
    335 	 false,			/* pc_relative */
    336 	 0,			/* bitpos */
    337 	 complain_overflow_dont, /* complain_on_overflow */
    338 	 bfd_elf_generic_reloc, /* special_function */
    339 	 "R_68K_TLS_TPREL32",	/* name */
    340 	 false,			/* partial_inplace */
    341 	 0,			/* src_mask */
    342 	 0xffffffff,		/* dst_mask */
    343 	 false),		/* pcrel_offset */
    344 };
    345 
    346 static bool
    347 rtype_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
    348 {
    349   unsigned int indx = ELF32_R_TYPE (dst->r_info);
    350 
    351   if (indx >= (unsigned int) R_68K_max)
    352     {
    353       /* xgettext:c-format */
    354       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    355 			  abfd, indx);
    356       bfd_set_error (bfd_error_bad_value);
    357       return false;
    358     }
    359   cache_ptr->howto = &howto_table[indx];
    360   return true;
    361 }
    362 
    363 #define elf_info_to_howto rtype_to_howto
    364 
    365 static const struct
    366 {
    367   bfd_reloc_code_real_type bfd_val;
    368   int elf_val;
    369 }
    370   reloc_map[] =
    371 {
    372   { BFD_RELOC_NONE, R_68K_NONE },
    373   { BFD_RELOC_32, R_68K_32 },
    374   { BFD_RELOC_16, R_68K_16 },
    375   { BFD_RELOC_8, R_68K_8 },
    376   { BFD_RELOC_32_PCREL, R_68K_PC32 },
    377   { BFD_RELOC_16_PCREL, R_68K_PC16 },
    378   { BFD_RELOC_8_PCREL, R_68K_PC8 },
    379   { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
    380   { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
    381   { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
    382   { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
    383   { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
    384   { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
    385   { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
    386   { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
    387   { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
    388   { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
    389   { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
    390   { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
    391   { BFD_RELOC_NONE, R_68K_COPY },
    392   { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
    393   { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
    394   { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
    395   { BFD_RELOC_CTOR, R_68K_32 },
    396   { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
    397   { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
    398   { BFD_RELOC_68K_TLS_GD32, R_68K_TLS_GD32 },
    399   { BFD_RELOC_68K_TLS_GD16, R_68K_TLS_GD16 },
    400   { BFD_RELOC_68K_TLS_GD8, R_68K_TLS_GD8 },
    401   { BFD_RELOC_68K_TLS_LDM32, R_68K_TLS_LDM32 },
    402   { BFD_RELOC_68K_TLS_LDM16, R_68K_TLS_LDM16 },
    403   { BFD_RELOC_68K_TLS_LDM8, R_68K_TLS_LDM8 },
    404   { BFD_RELOC_68K_TLS_LDO32, R_68K_TLS_LDO32 },
    405   { BFD_RELOC_68K_TLS_LDO16, R_68K_TLS_LDO16 },
    406   { BFD_RELOC_68K_TLS_LDO8, R_68K_TLS_LDO8 },
    407   { BFD_RELOC_68K_TLS_IE32, R_68K_TLS_IE32 },
    408   { BFD_RELOC_68K_TLS_IE16, R_68K_TLS_IE16 },
    409   { BFD_RELOC_68K_TLS_IE8, R_68K_TLS_IE8 },
    410   { BFD_RELOC_68K_TLS_LE32, R_68K_TLS_LE32 },
    411   { BFD_RELOC_68K_TLS_LE16, R_68K_TLS_LE16 },
    412   { BFD_RELOC_68K_TLS_LE8, R_68K_TLS_LE8 },
    413 };
    414 
    415 static reloc_howto_type *
    416 reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    417 		   bfd_reloc_code_real_type code)
    418 {
    419   unsigned int i;
    420   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
    421     {
    422       if (reloc_map[i].bfd_val == code)
    423 	return &howto_table[reloc_map[i].elf_val];
    424     }
    425   return 0;
    426 }
    427 
    428 static reloc_howto_type *
    429 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
    430 {
    431   unsigned int i;
    432 
    433   for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
    434     if (howto_table[i].name != NULL
    435 	&& strcasecmp (howto_table[i].name, r_name) == 0)
    436       return &howto_table[i];
    437 
    438   return NULL;
    439 }
    440 
    441 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
    442 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
    443 #define ELF_ARCH bfd_arch_m68k
    444 #define ELF_TARGET_ID M68K_ELF_DATA
    445 
    446 /* Functions for the m68k ELF linker.  */
    448 
    449 /* The name of the dynamic interpreter.  This is put in the .interp
    450    section.  */
    451 
    452 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
    453 
    454 /* Describes one of the various PLT styles.  */
    455 
    456 struct elf_m68k_plt_info
    457 {
    458   /* The size of each PLT entry.  */
    459   bfd_vma size;
    460 
    461   /* The template for the first PLT entry.  */
    462   const bfd_byte *plt0_entry;
    463 
    464   /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
    465      The comments by each member indicate the value that the relocation
    466      is against.  */
    467   struct {
    468     unsigned int got4; /* .got + 4 */
    469     unsigned int got8; /* .got + 8 */
    470   } plt0_relocs;
    471 
    472   /* The template for a symbol's PLT entry.  */
    473   const bfd_byte *symbol_entry;
    474 
    475   /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
    476      The comments by each member indicate the value that the relocation
    477      is against.  */
    478   struct {
    479     unsigned int got; /* the symbol's .got.plt entry */
    480     unsigned int plt; /* .plt */
    481   } symbol_relocs;
    482 
    483   /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
    484      The stub starts with "move.l #relocoffset,%d0".  */
    485   bfd_vma symbol_resolve_entry;
    486 };
    487 
    488 /* The size in bytes of an entry in the procedure linkage table.  */
    489 
    490 #define PLT_ENTRY_SIZE 20
    491 
    492 /* The first entry in a procedure linkage table looks like this.  See
    493    the SVR4 ABI m68k supplement to see how this works.  */
    494 
    495 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
    496 {
    497   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
    498   0, 0, 0, 2,		  /* + (.got + 4) - . */
    499   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
    500   0, 0, 0, 2,		  /* + (.got + 8) - . */
    501   0, 0, 0, 0		  /* pad out to 20 bytes.  */
    502 };
    503 
    504 /* Subsequent entries in a procedure linkage table look like this.  */
    505 
    506 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
    507 {
    508   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
    509   0, 0, 0, 2,		  /* + (.got.plt entry) - . */
    510   0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
    511   0, 0, 0, 0,		  /* + reloc index */
    512   0x60, 0xff,		  /* bra.l .plt */
    513   0, 0, 0, 0		  /* + .plt - . */
    514 };
    515 
    516 static const struct elf_m68k_plt_info elf_m68k_plt_info =
    517 {
    518   PLT_ENTRY_SIZE,
    519   elf_m68k_plt0_entry, { 4, 12 },
    520   elf_m68k_plt_entry, { 4, 16 }, 8
    521 };
    522 
    523 #define ISAB_PLT_ENTRY_SIZE 24
    524 
    525 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
    526 {
    527   0x20, 0x3c,		  /* move.l #offset,%d0 */
    528   0, 0, 0, 0,		  /* + (.got + 4) - . */
    529   0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
    530   0x20, 0x3c,		  /* move.l #offset,%d0 */
    531   0, 0, 0, 0,		  /* + (.got + 8) - . */
    532   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
    533   0x4e, 0xd0,		  /* jmp (%a0) */
    534   0x4e, 0x71		  /* nop */
    535 };
    536 
    537 /* Subsequent entries in a procedure linkage table look like this.  */
    538 
    539 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
    540 {
    541   0x20, 0x3c,		  /* move.l #offset,%d0 */
    542   0, 0, 0, 0,		  /* + (.got.plt entry) - . */
    543   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
    544   0x4e, 0xd0,		  /* jmp (%a0) */
    545   0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
    546   0, 0, 0, 0,		  /* + reloc index */
    547   0x60, 0xff,		  /* bra.l .plt */
    548   0, 0, 0, 0		  /* + .plt - . */
    549 };
    550 
    551 static const struct elf_m68k_plt_info elf_isab_plt_info =
    552 {
    553   ISAB_PLT_ENTRY_SIZE,
    554   elf_isab_plt0_entry, { 2, 12 },
    555   elf_isab_plt_entry, { 2, 20 }, 12
    556 };
    557 
    558 #define ISAC_PLT_ENTRY_SIZE 24
    559 
    560 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
    561 {
    562   0x20, 0x3c,		  /* move.l #offset,%d0 */
    563   0, 0, 0, 0,		  /* replaced with .got + 4 - . */
    564   0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
    565   0x20, 0x3c,		  /* move.l #offset,%d0 */
    566   0, 0, 0, 0,		  /* replaced with .got + 8 - . */
    567   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
    568   0x4e, 0xd0,		  /* jmp (%a0) */
    569   0x4e, 0x71		  /* nop */
    570 };
    571 
    572 /* Subsequent entries in a procedure linkage table look like this.  */
    573 
    574 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
    575 {
    576   0x20, 0x3c,		  /* move.l #offset,%d0 */
    577   0, 0, 0, 0,		  /* replaced with (.got entry) - . */
    578   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
    579   0x4e, 0xd0,		  /* jmp (%a0) */
    580   0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
    581   0, 0, 0, 0,		  /* replaced with offset into relocation table */
    582   0x61, 0xff,		  /* bsr.l .plt */
    583   0, 0, 0, 0		  /* replaced with .plt - . */
    584 };
    585 
    586 static const struct elf_m68k_plt_info elf_isac_plt_info =
    587 {
    588   ISAC_PLT_ENTRY_SIZE,
    589   elf_isac_plt0_entry, { 2, 12},
    590   elf_isac_plt_entry, { 2, 20 }, 12
    591 };
    592 
    593 #define CPU32_PLT_ENTRY_SIZE 24
    594 /* Procedure linkage table entries for the cpu32 */
    595 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
    596 {
    597   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
    598   0, 0, 0, 2,		  /* + (.got + 4) - . */
    599   0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
    600   0, 0, 0, 2,		  /* + (.got + 8) - . */
    601   0x4e, 0xd1,		  /* jmp %a1@ */
    602   0, 0, 0, 0,		  /* pad out to 24 bytes.  */
    603   0, 0
    604 };
    605 
    606 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
    607 {
    608   0x22, 0x7b, 0x01, 0x70,  /* moveal %pc@(0xc), %a1 */
    609   0, 0, 0, 2,		   /* + (.got.plt entry) - . */
    610   0x4e, 0xd1,		   /* jmp %a1@ */
    611   0x2f, 0x3c,		   /* move.l #offset,-(%sp) */
    612   0, 0, 0, 0,		   /* + reloc index */
    613   0x60, 0xff,		   /* bra.l .plt */
    614   0, 0, 0, 0,		   /* + .plt - . */
    615   0, 0
    616 };
    617 
    618 static const struct elf_m68k_plt_info elf_cpu32_plt_info =
    619 {
    620   CPU32_PLT_ENTRY_SIZE,
    621   elf_cpu32_plt0_entry, { 4, 12 },
    622   elf_cpu32_plt_entry, { 4, 18 }, 10
    623 };
    624 
    625 /* The m68k linker needs to keep track of the number of relocs that it
    626    decides to copy in check_relocs for each symbol.  This is so that it
    627    can discard PC relative relocs if it doesn't need them when linking
    628    with -Bsymbolic.  We store the information in a field extending the
    629    regular ELF linker hash table.  */
    630 
    631 /* This structure keeps track of the number of PC relative relocs we have
    632    copied for a given symbol.  */
    633 
    634 struct elf_m68k_pcrel_relocs_copied
    635 {
    636   /* Next section.  */
    637   struct elf_m68k_pcrel_relocs_copied *next;
    638   /* A section in dynobj.  */
    639   asection *section;
    640   /* Number of relocs copied in this section.  */
    641   bfd_size_type count;
    642 };
    643 
    644 /* Forward declaration.  */
    645 struct elf_m68k_got_entry;
    646 
    647 /* m68k ELF linker hash entry.  */
    648 
    649 struct elf_m68k_link_hash_entry
    650 {
    651   struct elf_link_hash_entry root;
    652 
    653   /* Number of PC relative relocs copied for this symbol.  */
    654   struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
    655 
    656   /* Key to got_entries.  */
    657   unsigned long got_entry_key;
    658 
    659   /* List of GOT entries for this symbol.  This list is build during
    660      offset finalization and is used within elf_m68k_finish_dynamic_symbol
    661      to traverse all GOT entries for a particular symbol.
    662 
    663      ??? We could've used root.got.glist field instead, but having
    664      a separate field is cleaner.  */
    665   struct elf_m68k_got_entry *glist;
    666 };
    667 
    668 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
    669 
    670 /* Key part of GOT entry in hashtable.  */
    671 struct elf_m68k_got_entry_key
    672 {
    673   /* BFD in which this symbol was defined.  NULL for global symbols.  */
    674   const bfd *bfd;
    675 
    676   /* Symbol index.  Either local symbol index or h->got_entry_key.  */
    677   unsigned long symndx;
    678 
    679   /* Type is one of R_68K_GOT{8, 16, 32}O, R_68K_TLS_GD{8, 16, 32},
    680      R_68K_TLS_LDM{8, 16, 32} or R_68K_TLS_IE{8, 16, 32}.
    681 
    682      From perspective of hashtable key, only elf_m68k_got_reloc_type (type)
    683      matters.  That is, we distinguish between, say, R_68K_GOT16O
    684      and R_68K_GOT32O when allocating offsets, but they are considered to be
    685      the same when searching got->entries.  */
    686   enum elf_m68k_reloc_type type;
    687 };
    688 
    689 /* Size of the GOT offset suitable for relocation.  */
    690 enum elf_m68k_got_offset_size { R_8, R_16, R_32, R_LAST };
    691 
    692 /* Entry of the GOT.  */
    693 struct elf_m68k_got_entry
    694 {
    695   /* GOT entries are put into a got->entries hashtable.  This is the key.  */
    696   struct elf_m68k_got_entry_key key_;
    697 
    698   /* GOT entry data.  We need s1 before offset finalization and s2 after.  */
    699   union
    700   {
    701     struct
    702     {
    703       /* Number of times this entry is referenced.  */
    704       bfd_vma refcount;
    705     } s1;
    706 
    707     struct
    708     {
    709       /* Offset from the start of .got section.  To calculate offset relative
    710 	 to GOT pointer one should subtract got->offset from this value.  */
    711       bfd_vma offset;
    712 
    713       /* Pointer to the next GOT entry for this global symbol.
    714 	 Symbols have at most one entry in one GOT, but might
    715 	 have entries in more than one GOT.
    716 	 Root of this list is h->glist.
    717 	 NULL for local symbols.  */
    718       struct elf_m68k_got_entry *next;
    719     } s2;
    720   } u;
    721 };
    722 
    723 /* Return representative type for relocation R_TYPE.
    724    This is used to avoid enumerating many relocations in comparisons,
    725    switches etc.  */
    726 
    727 static enum elf_m68k_reloc_type
    728 elf_m68k_reloc_got_type (enum elf_m68k_reloc_type r_type)
    729 {
    730   switch (r_type)
    731     {
    732       /* In most cases R_68K_GOTx relocations require the very same
    733 	 handling as R_68K_GOT32O relocation.  In cases when we need
    734 	 to distinguish between the two, we use explicitly compare against
    735 	 r_type.  */
    736     case R_68K_GOT32:
    737     case R_68K_GOT16:
    738     case R_68K_GOT8:
    739     case R_68K_GOT32O:
    740     case R_68K_GOT16O:
    741     case R_68K_GOT8O:
    742       return R_68K_GOT32O;
    743 
    744     case R_68K_TLS_GD32:
    745     case R_68K_TLS_GD16:
    746     case R_68K_TLS_GD8:
    747       return R_68K_TLS_GD32;
    748 
    749     case R_68K_TLS_LDM32:
    750     case R_68K_TLS_LDM16:
    751     case R_68K_TLS_LDM8:
    752       return R_68K_TLS_LDM32;
    753 
    754     case R_68K_TLS_IE32:
    755     case R_68K_TLS_IE16:
    756     case R_68K_TLS_IE8:
    757       return R_68K_TLS_IE32;
    758 
    759     default:
    760       BFD_ASSERT (false);
    761       return 0;
    762     }
    763 }
    764 
    765 /* Return size of the GOT entry offset for relocation R_TYPE.  */
    766 
    767 static enum elf_m68k_got_offset_size
    768 elf_m68k_reloc_got_offset_size (enum elf_m68k_reloc_type r_type)
    769 {
    770   switch (r_type)
    771     {
    772     case R_68K_GOT32: case R_68K_GOT16: case R_68K_GOT8:
    773     case R_68K_GOT32O: case R_68K_TLS_GD32: case R_68K_TLS_LDM32:
    774     case R_68K_TLS_IE32:
    775       return R_32;
    776 
    777     case R_68K_GOT16O: case R_68K_TLS_GD16: case R_68K_TLS_LDM16:
    778     case R_68K_TLS_IE16:
    779       return R_16;
    780 
    781     case R_68K_GOT8O: case R_68K_TLS_GD8: case R_68K_TLS_LDM8:
    782     case R_68K_TLS_IE8:
    783       return R_8;
    784 
    785     default:
    786       BFD_ASSERT (false);
    787       return 0;
    788     }
    789 }
    790 
    791 /* Return number of GOT entries we need to allocate in GOT for
    792    relocation R_TYPE.  */
    793 
    794 static bfd_vma
    795 elf_m68k_reloc_got_n_slots (enum elf_m68k_reloc_type r_type)
    796 {
    797   switch (elf_m68k_reloc_got_type (r_type))
    798     {
    799     case R_68K_GOT32O:
    800     case R_68K_TLS_IE32:
    801       return 1;
    802 
    803     case R_68K_TLS_GD32:
    804     case R_68K_TLS_LDM32:
    805       return 2;
    806 
    807     default:
    808       BFD_ASSERT (false);
    809       return 0;
    810     }
    811 }
    812 
    813 /* Return TRUE if relocation R_TYPE is a TLS one.  */
    814 
    815 static bool
    816 elf_m68k_reloc_tls_p (enum elf_m68k_reloc_type r_type)
    817 {
    818   switch (r_type)
    819     {
    820     case R_68K_TLS_GD32: case R_68K_TLS_GD16: case R_68K_TLS_GD8:
    821     case R_68K_TLS_LDM32: case R_68K_TLS_LDM16: case R_68K_TLS_LDM8:
    822     case R_68K_TLS_LDO32: case R_68K_TLS_LDO16: case R_68K_TLS_LDO8:
    823     case R_68K_TLS_IE32: case R_68K_TLS_IE16: case R_68K_TLS_IE8:
    824     case R_68K_TLS_LE32: case R_68K_TLS_LE16: case R_68K_TLS_LE8:
    825     case R_68K_TLS_DTPMOD32: case R_68K_TLS_DTPREL32: case R_68K_TLS_TPREL32:
    826       return true;
    827 
    828     default:
    829       return false;
    830     }
    831 }
    832 
    833 /* Data structure representing a single GOT.  */
    834 struct elf_m68k_got
    835 {
    836   /* Hashtable of 'struct elf_m68k_got_entry's.
    837      Starting size of this table is the maximum number of
    838      R_68K_GOT8O entries.  */
    839   htab_t entries;
    840 
    841   /* Number of R_x slots in this GOT.  Some (e.g., TLS) entries require
    842      several GOT slots.
    843 
    844      n_slots[R_8] is the count of R_8 slots in this GOT.
    845      n_slots[R_16] is the cumulative count of R_8 and R_16 slots
    846      in this GOT.
    847      n_slots[R_32] is the cumulative count of R_8, R_16 and R_32 slots
    848      in this GOT.  This is the total number of slots.  */
    849   bfd_vma n_slots[R_LAST];
    850 
    851   /* Number of local (entry->key_.h == NULL) slots in this GOT.
    852      This is only used to properly calculate size of .rela.got section;
    853      see elf_m68k_partition_multi_got.  */
    854   bfd_vma local_n_slots;
    855 
    856   /* Offset of this GOT relative to beginning of .got section.  */
    857   bfd_vma offset;
    858 };
    859 
    860 /* BFD and its GOT.  This is an entry in multi_got->bfd2got hashtable.  */
    861 struct elf_m68k_bfd2got_entry
    862 {
    863   /* BFD.  */
    864   const bfd *bfd;
    865 
    866   /* Assigned GOT.  Before partitioning multi-GOT each BFD has its own
    867      GOT structure.  After partitioning several BFD's might [and often do]
    868      share a single GOT.  */
    869   struct elf_m68k_got *got;
    870 };
    871 
    872 /* The main data structure holding all the pieces.  */
    873 struct elf_m68k_multi_got
    874 {
    875   /* Hashtable mapping each BFD to its GOT.  If a BFD doesn't have an entry
    876      here, then it doesn't need a GOT (this includes the case of a BFD
    877      having an empty GOT).
    878 
    879      ??? This hashtable can be replaced by an array indexed by bfd->id.  */
    880   htab_t bfd2got;
    881 
    882   /* Next symndx to assign a global symbol.
    883      h->got_entry_key is initialized from this counter.  */
    884   unsigned long global_symndx;
    885 };
    886 
    887 /* m68k ELF linker hash table.  */
    888 
    889 struct elf_m68k_link_hash_table
    890 {
    891   struct elf_link_hash_table root;
    892 
    893   /* The PLT format used by this link, or NULL if the format has not
    894      yet been chosen.  */
    895   const struct elf_m68k_plt_info *plt_info;
    896 
    897   /* True, if GP is loaded within each function which uses it.
    898      Set to TRUE when GOT negative offsets or multi-GOT is enabled.  */
    899   bool local_gp_p;
    900 
    901   /* Switch controlling use of negative offsets to double the size of GOTs.  */
    902   bool use_neg_got_offsets_p;
    903 
    904   /* Switch controlling generation of multiple GOTs.  */
    905   bool allow_multigot_p;
    906 
    907   /* Multi-GOT data structure.  */
    908   struct elf_m68k_multi_got multi_got_;
    909 };
    910 
    911 /* Get the m68k ELF linker hash table from a link_info structure.  */
    912 
    913 #define elf_m68k_hash_table(p) \
    914   ((is_elf_hash_table ((p)->hash)					\
    915     && elf_hash_table_id (elf_hash_table (p)) == M68K_ELF_DATA)		\
    916    ? (struct elf_m68k_link_hash_table *) (p)->hash : NULL)
    917 
    918 /* Shortcut to multi-GOT data.  */
    919 #define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
    920 
    921 /* Create an entry in an m68k ELF linker hash table.  */
    922 
    923 static struct bfd_hash_entry *
    924 elf_m68k_link_hash_newfunc (struct bfd_hash_entry *entry,
    925 			    struct bfd_hash_table *table,
    926 			    const char *string)
    927 {
    928   struct bfd_hash_entry *ret = entry;
    929 
    930   /* Allocate the structure if it has not already been allocated by a
    931      subclass.  */
    932   if (ret == NULL)
    933     ret = bfd_hash_allocate (table,
    934 			     sizeof (struct elf_m68k_link_hash_entry));
    935   if (ret == NULL)
    936     return ret;
    937 
    938   /* Call the allocation method of the superclass.  */
    939   ret = _bfd_elf_link_hash_newfunc (ret, table, string);
    940   if (ret != NULL)
    941     {
    942       elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
    943       elf_m68k_hash_entry (ret)->got_entry_key = 0;
    944       elf_m68k_hash_entry (ret)->glist = NULL;
    945     }
    946 
    947   return ret;
    948 }
    949 
    950 /* Destroy an m68k ELF linker hash table.  */
    951 
    952 static void
    953 elf_m68k_link_hash_table_free (bfd *obfd)
    954 {
    955   struct elf_m68k_link_hash_table *htab;
    956 
    957   htab = (struct elf_m68k_link_hash_table *) obfd->link.hash;
    958 
    959   if (htab->multi_got_.bfd2got != NULL)
    960     {
    961       htab_delete (htab->multi_got_.bfd2got);
    962       htab->multi_got_.bfd2got = NULL;
    963     }
    964   _bfd_elf_link_hash_table_free (obfd);
    965 }
    966 
    967 /* Create an m68k ELF linker hash table.  */
    968 
    969 static struct bfd_link_hash_table *
    970 elf_m68k_link_hash_table_create (bfd *abfd)
    971 {
    972   struct elf_m68k_link_hash_table *ret;
    973   size_t amt = sizeof (struct elf_m68k_link_hash_table);
    974 
    975   ret = (struct elf_m68k_link_hash_table *) bfd_zmalloc (amt);
    976   if (ret == (struct elf_m68k_link_hash_table *) NULL)
    977     return NULL;
    978 
    979   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
    980 				      elf_m68k_link_hash_newfunc,
    981 				      sizeof (struct elf_m68k_link_hash_entry),
    982 				      M68K_ELF_DATA))
    983     {
    984       free (ret);
    985       return NULL;
    986     }
    987   ret->root.root.hash_table_free = elf_m68k_link_hash_table_free;
    988 
    989   ret->multi_got_.global_symndx = 1;
    990 
    991   return &ret->root.root;
    992 }
    993 
    994 /* Set the right machine number.  */
    995 
    996 static bool
    997 elf32_m68k_object_p (bfd *abfd)
    998 {
    999   unsigned int mach = 0;
   1000   unsigned features = 0;
   1001   flagword eflags = elf_elfheader (abfd)->e_flags;
   1002 
   1003   if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
   1004     features |= m68000;
   1005   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
   1006     features |= cpu32;
   1007   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
   1008     features |= fido_a;
   1009   else
   1010     {
   1011       switch (eflags & EF_M68K_CF_ISA_MASK)
   1012 	{
   1013 	case EF_M68K_CF_ISA_A_NODIV:
   1014 	  features |= mcfisa_a;
   1015 	  break;
   1016 	case EF_M68K_CF_ISA_A:
   1017 	  features |= mcfisa_a|mcfhwdiv;
   1018 	  break;
   1019 	case EF_M68K_CF_ISA_A_PLUS:
   1020 	  features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
   1021 	  break;
   1022 	case EF_M68K_CF_ISA_B_NOUSP:
   1023 	  features |= mcfisa_a|mcfisa_b|mcfhwdiv;
   1024 	  break;
   1025 	case EF_M68K_CF_ISA_B:
   1026 	  features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
   1027 	  break;
   1028 	case EF_M68K_CF_ISA_C:
   1029 	  features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
   1030 	  break;
   1031 	case EF_M68K_CF_ISA_C_NODIV:
   1032 	  features |= mcfisa_a|mcfisa_c|mcfusp;
   1033 	  break;
   1034 	}
   1035       switch (eflags & EF_M68K_CF_MAC_MASK)
   1036 	{
   1037 	case EF_M68K_CF_MAC:
   1038 	  features |= mcfmac;
   1039 	  break;
   1040 	case EF_M68K_CF_EMAC:
   1041 	  features |= mcfemac;
   1042 	  break;
   1043 	}
   1044       if (eflags & EF_M68K_CF_FLOAT)
   1045 	features |= cfloat;
   1046     }
   1047 
   1048   mach = bfd_m68k_features_to_mach (features);
   1049   bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
   1050 
   1051   return true;
   1052 }
   1053 
   1054 /* Somewhat reverse of elf32_m68k_object_p, this sets the e_flag
   1055    field based on the machine number.  */
   1056 
   1057 static bool
   1058 elf_m68k_final_write_processing (bfd *abfd)
   1059 {
   1060   int mach = bfd_get_mach (abfd);
   1061   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
   1062 
   1063   if (!e_flags)
   1064     {
   1065       unsigned int arch_mask;
   1066 
   1067       arch_mask = bfd_m68k_mach_to_features (mach);
   1068 
   1069       if (arch_mask & m68000)
   1070 	e_flags = EF_M68K_M68000;
   1071       else if (arch_mask & cpu32)
   1072 	e_flags = EF_M68K_CPU32;
   1073       else if (arch_mask & fido_a)
   1074 	e_flags = EF_M68K_FIDO;
   1075       else
   1076 	{
   1077 	  switch (arch_mask
   1078 		  & (mcfisa_a | mcfisa_aa | mcfisa_b | mcfisa_c | mcfhwdiv | mcfusp))
   1079 	    {
   1080 	    case mcfisa_a:
   1081 	      e_flags |= EF_M68K_CF_ISA_A_NODIV;
   1082 	      break;
   1083 	    case mcfisa_a | mcfhwdiv:
   1084 	      e_flags |= EF_M68K_CF_ISA_A;
   1085 	      break;
   1086 	    case mcfisa_a | mcfisa_aa | mcfhwdiv | mcfusp:
   1087 	      e_flags |= EF_M68K_CF_ISA_A_PLUS;
   1088 	      break;
   1089 	    case mcfisa_a | mcfisa_b | mcfhwdiv:
   1090 	      e_flags |= EF_M68K_CF_ISA_B_NOUSP;
   1091 	      break;
   1092 	    case mcfisa_a | mcfisa_b | mcfhwdiv | mcfusp:
   1093 	      e_flags |= EF_M68K_CF_ISA_B;
   1094 	      break;
   1095 	    case mcfisa_a | mcfisa_c | mcfhwdiv | mcfusp:
   1096 	      e_flags |= EF_M68K_CF_ISA_C;
   1097 	      break;
   1098 	    case mcfisa_a | mcfisa_c | mcfusp:
   1099 	      e_flags |= EF_M68K_CF_ISA_C_NODIV;
   1100 	      break;
   1101 	    }
   1102 	  if (arch_mask & mcfmac)
   1103 	    e_flags |= EF_M68K_CF_MAC;
   1104 	  else if (arch_mask & mcfemac)
   1105 	    e_flags |= EF_M68K_CF_EMAC;
   1106 	  if (arch_mask & cfloat)
   1107 	    e_flags |= EF_M68K_CF_FLOAT | EF_M68K_CFV4E;
   1108 	}
   1109       elf_elfheader (abfd)->e_flags = e_flags;
   1110     }
   1111   return _bfd_elf_final_write_processing (abfd);
   1112 }
   1113 
   1114 /* Keep m68k-specific flags in the ELF header.  */
   1115 
   1116 static bool
   1117 elf32_m68k_set_private_flags (bfd *abfd, flagword flags)
   1118 {
   1119   elf_elfheader (abfd)->e_flags = flags;
   1120   elf_flags_init (abfd) = true;
   1121   return true;
   1122 }
   1123 
   1124 /* Merge object attributes from IBFD into OBFD.  Warn if
   1125    there are conflicting attributes. */
   1126 static bool
   1127 m68k_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
   1128 {
   1129   bfd *obfd = info->output_bfd;
   1130   obj_attribute *in_attr, *in_attrs;
   1131   obj_attribute *out_attr, *out_attrs;
   1132   bool ret = true;
   1133 
   1134   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   1135   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   1136 
   1137   in_attr = &in_attrs[Tag_GNU_M68K_ABI_FP];
   1138   out_attr = &out_attrs[Tag_GNU_M68K_ABI_FP];
   1139 
   1140   if (in_attr->i != out_attr->i)
   1141     {
   1142       int in_fp = in_attr->i & 3;
   1143       int out_fp = out_attr->i & 3;
   1144       static bfd *last_fp;
   1145 
   1146       if (in_fp == 0)
   1147 	;
   1148       else if (out_fp == 0)
   1149 	{
   1150 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   1151 	  out_attr->i ^= in_fp;
   1152 	  last_fp = ibfd;
   1153 	}
   1154       else if (out_fp == 1 && in_fp == 2)
   1155 	{
   1156 	  _bfd_error_handler
   1157 	    /* xgettext:c-format */
   1158 	    (_("%pB uses hard float, %pB uses soft float"),
   1159 	     last_fp, ibfd);
   1160 	  ret = false;
   1161 	}
   1162       else if (out_fp == 2 && in_fp == 1)
   1163 	{
   1164 	  _bfd_error_handler
   1165 	    /* xgettext:c-format */
   1166 	    (_("%pB uses hard float, %pB uses soft float"),
   1167 	     ibfd, last_fp);
   1168 	  ret = false;
   1169 	}
   1170     }
   1171 
   1172   if (!ret)
   1173     {
   1174       out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   1175       bfd_set_error (bfd_error_bad_value);
   1176       return false;
   1177     }
   1178 
   1179   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   1180   return _bfd_elf_merge_object_attributes (ibfd, info);
   1181 }
   1182 
   1183 /* Merge backend specific data from an object file to the output
   1184    object file when linking.  */
   1185 static bool
   1186 elf32_m68k_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   1187 {
   1188   bfd *obfd = info->output_bfd;
   1189   flagword out_flags;
   1190   flagword in_flags;
   1191   flagword out_isa;
   1192   flagword in_isa;
   1193   const bfd_arch_info_type *arch_info;
   1194 
   1195   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1196       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1197     /* PR 24523: For non-ELF files do not try to merge any private
   1198        data, but also do not prevent the link from succeeding.  */
   1199     return true;
   1200 
   1201   /* Get the merged machine.  This checks for incompatibility between
   1202      Coldfire & non-Coldfire flags, incompability between different
   1203      Coldfire ISAs, and incompability between different MAC types.  */
   1204   arch_info = bfd_arch_get_compatible (ibfd, obfd, false);
   1205   if (!arch_info)
   1206     return false;
   1207 
   1208   bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
   1209 
   1210   if (!m68k_elf_merge_obj_attributes (ibfd, info))
   1211     return false;
   1212 
   1213   in_flags = elf_elfheader (ibfd)->e_flags;
   1214   if (!elf_flags_init (obfd))
   1215     {
   1216       elf_flags_init (obfd) = true;
   1217       out_flags = in_flags;
   1218     }
   1219   else
   1220     {
   1221       out_flags = elf_elfheader (obfd)->e_flags;
   1222       unsigned int variant_mask;
   1223 
   1224       if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
   1225 	variant_mask = 0;
   1226       else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
   1227 	variant_mask = 0;
   1228       else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
   1229 	variant_mask = 0;
   1230       else
   1231 	variant_mask = EF_M68K_CF_ISA_MASK;
   1232 
   1233       in_isa = (in_flags & variant_mask);
   1234       out_isa = (out_flags & variant_mask);
   1235       if (in_isa > out_isa)
   1236 	out_flags ^= in_isa ^ out_isa;
   1237       if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
   1238 	   && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
   1239 	  || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
   1240 	      && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
   1241 	out_flags = EF_M68K_FIDO;
   1242       else
   1243       out_flags |= in_flags ^ in_isa;
   1244     }
   1245   elf_elfheader (obfd)->e_flags = out_flags;
   1246 
   1247   return true;
   1248 }
   1249 
   1250 /* Display the flags field.  */
   1251 
   1252 static bool
   1253 elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
   1254 {
   1255   FILE *file = (FILE *) ptr;
   1256   flagword eflags = elf_elfheader (abfd)->e_flags;
   1257 
   1258   BFD_ASSERT (abfd != NULL && ptr != NULL);
   1259 
   1260   /* Print normal ELF private data.  */
   1261   _bfd_elf_print_private_bfd_data (abfd, ptr);
   1262 
   1263   /* Ignore init flag - it may not be set, despite the flags field containing valid data.  */
   1264 
   1265   /* xgettext:c-format */
   1266   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   1267 
   1268   if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
   1269     fprintf (file, " [m68000]");
   1270   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
   1271     fprintf (file, " [cpu32]");
   1272   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
   1273     fprintf (file, " [fido]");
   1274   else
   1275     {
   1276       if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
   1277 	fprintf (file, " [cfv4e]");
   1278 
   1279       if (eflags & EF_M68K_CF_ISA_MASK)
   1280 	{
   1281 	  char const *isa = _("unknown");
   1282 	  char const *mac = _("unknown");
   1283 	  char const *additional = "";
   1284 
   1285 	  switch (eflags & EF_M68K_CF_ISA_MASK)
   1286 	    {
   1287 	    case EF_M68K_CF_ISA_A_NODIV:
   1288 	      isa = "A";
   1289 	      additional = " [nodiv]";
   1290 	      break;
   1291 	    case EF_M68K_CF_ISA_A:
   1292 	      isa = "A";
   1293 	      break;
   1294 	    case EF_M68K_CF_ISA_A_PLUS:
   1295 	      isa = "A+";
   1296 	      break;
   1297 	    case EF_M68K_CF_ISA_B_NOUSP:
   1298 	      isa = "B";
   1299 	      additional = " [nousp]";
   1300 	      break;
   1301 	    case EF_M68K_CF_ISA_B:
   1302 	      isa = "B";
   1303 	      break;
   1304 	    case EF_M68K_CF_ISA_C:
   1305 	      isa = "C";
   1306 	      break;
   1307 	    case EF_M68K_CF_ISA_C_NODIV:
   1308 	      isa = "C";
   1309 	      additional = " [nodiv]";
   1310 	      break;
   1311 	    }
   1312 	  fprintf (file, " [isa %s]%s", isa, additional);
   1313 
   1314 	  if (eflags & EF_M68K_CF_FLOAT)
   1315 	    fprintf (file, " [float]");
   1316 
   1317 	  switch (eflags & EF_M68K_CF_MAC_MASK)
   1318 	    {
   1319 	    case 0:
   1320 	      mac = NULL;
   1321 	      break;
   1322 	    case EF_M68K_CF_MAC:
   1323 	      mac = "mac";
   1324 	      break;
   1325 	    case EF_M68K_CF_EMAC:
   1326 	      mac = "emac";
   1327 	      break;
   1328 	    case EF_M68K_CF_EMAC_B:
   1329 	      mac = "emac_b";
   1330 	      break;
   1331 	    }
   1332 	  if (mac)
   1333 	    fprintf (file, " [%s]", mac);
   1334 	}
   1335     }
   1336 
   1337   fputc ('\n', file);
   1338 
   1339   return true;
   1340 }
   1341 
   1342 /* Multi-GOT support implementation design:
   1343 
   1344    Multi-GOT starts in check_relocs hook.  There we scan all
   1345    relocations of a BFD and build a local GOT (struct elf_m68k_got)
   1346    for it.  If a single BFD appears to require too many GOT slots with
   1347    R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
   1348    to user.
   1349    After check_relocs has been invoked for each input BFD, we have
   1350    constructed a GOT for each input BFD.
   1351 
   1352    To minimize total number of GOTs required for a particular output BFD
   1353    (as some environments support only 1 GOT per output object) we try
   1354    to merge some of the GOTs to share an offset space.  Ideally [and in most
   1355    cases] we end up with a single GOT.  In cases when there are too many
   1356    restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
   1357    several GOTs, assuming the environment can handle them.
   1358 
   1359    Partitioning is done in elf_m68k_partition_multi_got.  We start with
   1360    an empty GOT and traverse bfd2got hashtable putting got_entries from
   1361    local GOTs to the new 'big' one.  We do that by constructing an
   1362    intermediate GOT holding all the entries the local GOT has and the big
   1363    GOT lacks.  Then we check if there is room in the big GOT to accomodate
   1364    all the entries from diff.  On success we add those entries to the big
   1365    GOT; on failure we start the new 'big' GOT and retry the adding of
   1366    entries from the local GOT.  Note that this retry will always succeed as
   1367    each local GOT doesn't overflow the limits.  After partitioning we
   1368    end up with each bfd assigned one of the big GOTs.  GOT entries in the
   1369    big GOTs are initialized with GOT offsets.  Note that big GOTs are
   1370    positioned consequently in program space and represent a single huge GOT
   1371    to the outside world.
   1372 
   1373    After that we get to elf_m68k_relocate_section.  There we
   1374    adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
   1375    relocations to refer to appropriate [assigned to current input_bfd]
   1376    big GOT.
   1377 
   1378    Notes:
   1379 
   1380    GOT entry type: We have several types of GOT entries.
   1381    * R_8 type is used in entries for symbols that have at least one
   1382    R_68K_GOT8O or R_68K_TLS_*8 relocation.  We can have at most 0x40
   1383    such entries in one GOT.
   1384    * R_16 type is used in entries for symbols that have at least one
   1385    R_68K_GOT16O or R_68K_TLS_*16 relocation and no R_8 relocations.
   1386    We can have at most 0x4000 such entries in one GOT.
   1387    * R_32 type is used in all other cases.  We can have as many
   1388    such entries in one GOT as we'd like.
   1389    When counting relocations we have to include the count of the smaller
   1390    ranged relocations in the counts of the larger ranged ones in order
   1391    to correctly detect overflow.
   1392 
   1393    Sorting the GOT: In each GOT starting offsets are assigned to
   1394    R_8 entries, which are followed by R_16 entries, and
   1395    R_32 entries go at the end.  See finalize_got_offsets for details.
   1396 
   1397    Negative GOT offsets: To double usable offset range of GOTs we use
   1398    negative offsets.  As we assign entries with GOT offsets relative to
   1399    start of .got section, the offset values are positive.  They become
   1400    negative only in relocate_section where got->offset value is
   1401    subtracted from them.
   1402 
   1403    3 special GOT entries: There are 3 special GOT entries used internally
   1404    by loader.  These entries happen to be placed to .got.plt section,
   1405    so we don't do anything about them in multi-GOT support.
   1406 
   1407    Memory management: All data except for hashtables
   1408    multi_got->bfd2got and got->entries are allocated on
   1409    elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
   1410    to most functions), so we don't need to care to free them.  At the
   1411    moment of allocation hashtables are being linked into main data
   1412    structure (multi_got), all pieces of which are reachable from
   1413    elf_m68k_multi_got (info).  We deallocate them in
   1414    elf_m68k_link_hash_table_free.  */
   1415 
   1416 /* Initialize GOT.  */
   1417 
   1418 static void
   1419 elf_m68k_init_got (struct elf_m68k_got *got)
   1420 {
   1421   got->entries = NULL;
   1422   got->n_slots[R_8] = 0;
   1423   got->n_slots[R_16] = 0;
   1424   got->n_slots[R_32] = 0;
   1425   got->local_n_slots = 0;
   1426   got->offset = (bfd_vma) -1;
   1427 }
   1428 
   1429 /* Destruct GOT.  */
   1430 
   1431 static void
   1432 elf_m68k_clear_got (struct elf_m68k_got *got)
   1433 {
   1434   if (got->entries != NULL)
   1435     {
   1436       htab_delete (got->entries);
   1437       got->entries = NULL;
   1438     }
   1439 }
   1440 
   1441 /* Create and empty GOT structure.  INFO is the context where memory
   1442    should be allocated.  */
   1443 
   1444 static struct elf_m68k_got *
   1445 elf_m68k_create_empty_got (struct bfd_link_info *info)
   1446 {
   1447   struct elf_m68k_got *got;
   1448 
   1449   got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
   1450   if (got == NULL)
   1451     return NULL;
   1452 
   1453   elf_m68k_init_got (got);
   1454 
   1455   return got;
   1456 }
   1457 
   1458 /* Initialize KEY.  */
   1459 
   1460 static void
   1461 elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
   1462 			     struct elf_link_hash_entry *h,
   1463 			     const bfd *abfd, unsigned long symndx,
   1464 			     enum elf_m68k_reloc_type reloc_type)
   1465 {
   1466   if (elf_m68k_reloc_got_type (reloc_type) == R_68K_TLS_LDM32)
   1467     /* All TLS_LDM relocations share a single GOT entry.  */
   1468     {
   1469       key->bfd = NULL;
   1470       key->symndx = 0;
   1471     }
   1472   else if (h != NULL)
   1473     /* Global symbols are identified with their got_entry_key.  */
   1474     {
   1475       key->bfd = NULL;
   1476       key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
   1477       BFD_ASSERT (key->symndx != 0);
   1478     }
   1479   else
   1480     /* Local symbols are identified by BFD they appear in and symndx.  */
   1481     {
   1482       key->bfd = abfd;
   1483       key->symndx = symndx;
   1484     }
   1485 
   1486   key->type = reloc_type;
   1487 }
   1488 
   1489 /* Calculate hash of got_entry.
   1490    ??? Is it good?  */
   1491 
   1492 static hashval_t
   1493 elf_m68k_got_entry_hash (const void *_entry)
   1494 {
   1495   const struct elf_m68k_got_entry_key *key;
   1496 
   1497   key = &((const struct elf_m68k_got_entry *) _entry)->key_;
   1498 
   1499   return (key->symndx
   1500 	  + (key->bfd != NULL ? (int) key->bfd->id : -1)
   1501 	  + elf_m68k_reloc_got_type (key->type));
   1502 }
   1503 
   1504 /* Check if two got entries are equal.  */
   1505 
   1506 static int
   1507 elf_m68k_got_entry_eq (const void *_entry1, const void *_entry2)
   1508 {
   1509   const struct elf_m68k_got_entry_key *key1;
   1510   const struct elf_m68k_got_entry_key *key2;
   1511 
   1512   key1 = &((const struct elf_m68k_got_entry *) _entry1)->key_;
   1513   key2 = &((const struct elf_m68k_got_entry *) _entry2)->key_;
   1514 
   1515   return (key1->bfd == key2->bfd
   1516 	  && key1->symndx == key2->symndx
   1517 	  && (elf_m68k_reloc_got_type (key1->type)
   1518 	      == elf_m68k_reloc_got_type (key2->type)));
   1519 }
   1520 
   1521 /* When using negative offsets, we allocate one extra R_8, one extra R_16
   1522    and one extra R_32 slots to simplify handling of 2-slot entries during
   1523    offset allocation -- hence -1 for R_8 slots and -2 for R_16 slots.  */
   1524 
   1525 /* Maximal number of R_8 slots in a single GOT.  */
   1526 #define ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT(INFO)		\
   1527   (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p		\
   1528    ? (0x40 - 1)							\
   1529    : 0x20)
   1530 
   1531 /* Maximal number of R_8 and R_16 slots in a single GOT.  */
   1532 #define ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT(INFO)		\
   1533   (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p		\
   1534    ? (0x4000 - 2)						\
   1535    : 0x2000)
   1536 
   1537 /* SEARCH - simply search the hashtable, don't insert new entries or fail when
   1538    the entry cannot be found.
   1539    FIND_OR_CREATE - search for an existing entry, but create new if there's
   1540    no such.
   1541    MUST_FIND - search for an existing entry and assert that it exist.
   1542    MUST_CREATE - assert that there's no such entry and create new one.  */
   1543 enum elf_m68k_get_entry_howto
   1544   {
   1545     SEARCH,
   1546     FIND_OR_CREATE,
   1547     MUST_FIND,
   1548     MUST_CREATE
   1549   };
   1550 
   1551 /* Get or create (depending on HOWTO) entry with KEY in GOT.
   1552    INFO is context in which memory should be allocated (can be NULL if
   1553    HOWTO is SEARCH or MUST_FIND).  */
   1554 
   1555 static struct elf_m68k_got_entry *
   1556 elf_m68k_get_got_entry (struct elf_m68k_got *got,
   1557 			const struct elf_m68k_got_entry_key *key,
   1558 			enum elf_m68k_get_entry_howto howto,
   1559 			struct bfd_link_info *info)
   1560 {
   1561   struct elf_m68k_got_entry entry_;
   1562   struct elf_m68k_got_entry *entry;
   1563   void **ptr;
   1564 
   1565   BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
   1566 
   1567   if (got->entries == NULL)
   1568     /* This is the first entry in ABFD.  Initialize hashtable.  */
   1569     {
   1570       if (howto == SEARCH)
   1571 	return NULL;
   1572 
   1573       got->entries = htab_try_create (ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT
   1574 				      (info),
   1575 				      elf_m68k_got_entry_hash,
   1576 				      elf_m68k_got_entry_eq, NULL);
   1577       if (got->entries == NULL)
   1578 	{
   1579 	  bfd_set_error (bfd_error_no_memory);
   1580 	  return NULL;
   1581 	}
   1582     }
   1583 
   1584   entry_.key_ = *key;
   1585   ptr = htab_find_slot (got->entries, &entry_,
   1586 			(howto == SEARCH || howto == MUST_FIND ? NO_INSERT
   1587 			 : INSERT));
   1588   if (ptr == NULL)
   1589     {
   1590       if (howto == SEARCH)
   1591 	/* Entry not found.  */
   1592 	return NULL;
   1593 
   1594       if (howto == MUST_FIND)
   1595 	abort ();
   1596 
   1597       /* We're out of memory.  */
   1598       bfd_set_error (bfd_error_no_memory);
   1599       return NULL;
   1600     }
   1601 
   1602   if (*ptr == NULL)
   1603     /* We didn't find the entry and we're asked to create a new one.  */
   1604     {
   1605       if (howto == MUST_FIND)
   1606 	abort ();
   1607 
   1608       BFD_ASSERT (howto != SEARCH);
   1609 
   1610       entry = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry));
   1611       if (entry == NULL)
   1612 	return NULL;
   1613 
   1614       /* Initialize new entry.  */
   1615       entry->key_ = *key;
   1616 
   1617       entry->u.s1.refcount = 0;
   1618 
   1619       /* Mark the entry as not initialized.  */
   1620       entry->key_.type = R_68K_max;
   1621 
   1622       *ptr = entry;
   1623     }
   1624   else
   1625     /* We found the entry.  */
   1626     {
   1627       BFD_ASSERT (howto != MUST_CREATE);
   1628 
   1629       entry = *ptr;
   1630     }
   1631 
   1632   return entry;
   1633 }
   1634 
   1635 /* Update GOT counters when merging entry of WAS type with entry of NEW type.
   1636    Return the value to which ENTRY's type should be set.  */
   1637 
   1638 static enum elf_m68k_reloc_type
   1639 elf_m68k_update_got_entry_type (struct elf_m68k_got *got,
   1640 				enum elf_m68k_reloc_type was,
   1641 				enum elf_m68k_reloc_type new_reloc)
   1642 {
   1643   enum elf_m68k_got_offset_size was_size;
   1644   enum elf_m68k_got_offset_size new_size;
   1645   bfd_vma n_slots;
   1646 
   1647   if (was == R_68K_max)
   1648     /* The type of the entry is not initialized yet.  */
   1649     {
   1650       /* Update all got->n_slots counters, including n_slots[R_32].  */
   1651       was_size = R_LAST;
   1652 
   1653       was = new_reloc;
   1654     }
   1655   else
   1656     {
   1657       /* !!! We, probably, should emit an error rather then fail on assert
   1658 	 in such a case.  */
   1659       BFD_ASSERT (elf_m68k_reloc_got_type (was)
   1660 		  == elf_m68k_reloc_got_type (new_reloc));
   1661 
   1662       was_size = elf_m68k_reloc_got_offset_size (was);
   1663     }
   1664 
   1665   new_size = elf_m68k_reloc_got_offset_size (new_reloc);
   1666   n_slots = elf_m68k_reloc_got_n_slots (new_reloc);
   1667 
   1668   while (was_size > new_size)
   1669     {
   1670       --was_size;
   1671       got->n_slots[was_size] += n_slots;
   1672     }
   1673 
   1674   if (new_reloc > was)
   1675     /* Relocations are ordered from bigger got offset size to lesser,
   1676        so choose the relocation type with lesser offset size.  */
   1677     was = new_reloc;
   1678 
   1679   return was;
   1680 }
   1681 
   1682 /* Add new or update existing entry to GOT.
   1683    H, ABFD, TYPE and SYMNDX is data for the entry.
   1684    INFO is a context where memory should be allocated.  */
   1685 
   1686 static struct elf_m68k_got_entry *
   1687 elf_m68k_add_entry_to_got (struct elf_m68k_got *got,
   1688 			   struct elf_link_hash_entry *h,
   1689 			   const bfd *abfd,
   1690 			   enum elf_m68k_reloc_type reloc_type,
   1691 			   unsigned long symndx,
   1692 			   struct bfd_link_info *info)
   1693 {
   1694   struct elf_m68k_got_entry_key key_;
   1695   struct elf_m68k_got_entry *entry;
   1696 
   1697   if (h != NULL && elf_m68k_hash_entry (h)->got_entry_key == 0)
   1698     elf_m68k_hash_entry (h)->got_entry_key
   1699       = elf_m68k_multi_got (info)->global_symndx++;
   1700 
   1701   elf_m68k_init_got_entry_key (&key_, h, abfd, symndx, reloc_type);
   1702 
   1703   entry = elf_m68k_get_got_entry (got, &key_, FIND_OR_CREATE, info);
   1704   if (entry == NULL)
   1705     return NULL;
   1706 
   1707   /* Determine entry's type and update got->n_slots counters.  */
   1708   entry->key_.type = elf_m68k_update_got_entry_type (got,
   1709 						     entry->key_.type,
   1710 						     reloc_type);
   1711 
   1712   /* Update refcount.  */
   1713   ++entry->u.s1.refcount;
   1714 
   1715   if (entry->u.s1.refcount == 1)
   1716     /* We see this entry for the first time.  */
   1717     {
   1718       if (entry->key_.bfd != NULL)
   1719 	got->local_n_slots += elf_m68k_reloc_got_n_slots (entry->key_.type);
   1720     }
   1721 
   1722   BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
   1723 
   1724   if ((got->n_slots[R_8]
   1725        > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
   1726       || (got->n_slots[R_16]
   1727 	  > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
   1728     /* This BFD has too many relocation.  */
   1729     {
   1730       if (got->n_slots[R_8] > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
   1731 	/* xgettext:c-format */
   1732 	_bfd_error_handler (_("%pB: GOT overflow: "
   1733 			      "number of relocations with 8-bit "
   1734 			      "offset > %d"),
   1735 			    abfd,
   1736 			    ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info));
   1737       else
   1738 	/* xgettext:c-format */
   1739 	_bfd_error_handler (_("%pB: GOT overflow: "
   1740 			      "number of relocations with 8- or 16-bit "
   1741 			      "offset > %d"),
   1742 			    abfd,
   1743 			    ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info));
   1744 
   1745       return NULL;
   1746     }
   1747 
   1748   return entry;
   1749 }
   1750 
   1751 /* Compute the hash value of the bfd in a bfd2got hash entry.  */
   1752 
   1753 static hashval_t
   1754 elf_m68k_bfd2got_entry_hash (const void *entry)
   1755 {
   1756   const struct elf_m68k_bfd2got_entry *e;
   1757 
   1758   e = (const struct elf_m68k_bfd2got_entry *) entry;
   1759 
   1760   return e->bfd->id;
   1761 }
   1762 
   1763 /* Check whether two hash entries have the same bfd.  */
   1764 
   1765 static int
   1766 elf_m68k_bfd2got_entry_eq (const void *entry1, const void *entry2)
   1767 {
   1768   const struct elf_m68k_bfd2got_entry *e1;
   1769   const struct elf_m68k_bfd2got_entry *e2;
   1770 
   1771   e1 = (const struct elf_m68k_bfd2got_entry *) entry1;
   1772   e2 = (const struct elf_m68k_bfd2got_entry *) entry2;
   1773 
   1774   return e1->bfd == e2->bfd;
   1775 }
   1776 
   1777 /* Destruct a bfd2got entry.  */
   1778 
   1779 static void
   1780 elf_m68k_bfd2got_entry_del (void *_entry)
   1781 {
   1782   struct elf_m68k_bfd2got_entry *entry;
   1783 
   1784   entry = (struct elf_m68k_bfd2got_entry *) _entry;
   1785 
   1786   BFD_ASSERT (entry->got != NULL);
   1787   elf_m68k_clear_got (entry->got);
   1788 }
   1789 
   1790 /* Find existing or create new (depending on HOWTO) bfd2got entry in
   1791    MULTI_GOT.  ABFD is the bfd we need a GOT for.  INFO is a context where
   1792    memory should be allocated.  */
   1793 
   1794 static struct elf_m68k_bfd2got_entry *
   1795 elf_m68k_get_bfd2got_entry (struct elf_m68k_multi_got *multi_got,
   1796 			    const bfd *abfd,
   1797 			    enum elf_m68k_get_entry_howto howto,
   1798 			    struct bfd_link_info *info)
   1799 {
   1800   struct elf_m68k_bfd2got_entry entry_;
   1801   void **ptr;
   1802   struct elf_m68k_bfd2got_entry *entry;
   1803 
   1804   BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
   1805 
   1806   if (multi_got->bfd2got == NULL)
   1807     /* This is the first GOT.  Initialize bfd2got.  */
   1808     {
   1809       if (howto == SEARCH)
   1810 	return NULL;
   1811 
   1812       multi_got->bfd2got = htab_try_create (1, elf_m68k_bfd2got_entry_hash,
   1813 					    elf_m68k_bfd2got_entry_eq,
   1814 					    elf_m68k_bfd2got_entry_del);
   1815       if (multi_got->bfd2got == NULL)
   1816 	{
   1817 	  bfd_set_error (bfd_error_no_memory);
   1818 	  return NULL;
   1819 	}
   1820     }
   1821 
   1822   entry_.bfd = abfd;
   1823   ptr = htab_find_slot (multi_got->bfd2got, &entry_,
   1824 			(howto == SEARCH || howto == MUST_FIND ? NO_INSERT
   1825 			 : INSERT));
   1826   if (ptr == NULL)
   1827     {
   1828       if (howto == SEARCH)
   1829 	/* Entry not found.  */
   1830 	return NULL;
   1831 
   1832       if (howto == MUST_FIND)
   1833 	abort ();
   1834 
   1835       /* We're out of memory.  */
   1836       bfd_set_error (bfd_error_no_memory);
   1837       return NULL;
   1838     }
   1839 
   1840   if (*ptr == NULL)
   1841     /* Entry was not found.  Create new one.  */
   1842     {
   1843       if (howto == MUST_FIND)
   1844 	abort ();
   1845 
   1846       BFD_ASSERT (howto != SEARCH);
   1847 
   1848       entry = ((struct elf_m68k_bfd2got_entry *)
   1849 	       bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry)));
   1850       if (entry == NULL)
   1851 	return NULL;
   1852 
   1853       entry->bfd = abfd;
   1854 
   1855       entry->got = elf_m68k_create_empty_got (info);
   1856       if (entry->got == NULL)
   1857 	return NULL;
   1858 
   1859       *ptr = entry;
   1860     }
   1861   else
   1862     {
   1863       BFD_ASSERT (howto != MUST_CREATE);
   1864 
   1865       /* Return existing entry.  */
   1866       entry = *ptr;
   1867     }
   1868 
   1869   return entry;
   1870 }
   1871 
   1872 struct elf_m68k_can_merge_gots_arg
   1873 {
   1874   /* A current_got that we constructing a DIFF against.  */
   1875   struct elf_m68k_got *big;
   1876 
   1877   /* GOT holding entries not present or that should be changed in
   1878      BIG.  */
   1879   struct elf_m68k_got *diff;
   1880 
   1881   /* Context where to allocate memory.  */
   1882   struct bfd_link_info *info;
   1883 
   1884   /* Error flag.  */
   1885   bool error_p;
   1886 };
   1887 
   1888 /* Process a single entry from the small GOT to see if it should be added
   1889    or updated in the big GOT.  */
   1890 
   1891 static int
   1892 elf_m68k_can_merge_gots_1 (void **_entry_ptr, void *_arg)
   1893 {
   1894   const struct elf_m68k_got_entry *entry1;
   1895   struct elf_m68k_can_merge_gots_arg *arg;
   1896   const struct elf_m68k_got_entry *entry2;
   1897   enum elf_m68k_reloc_type type;
   1898 
   1899   entry1 = (const struct elf_m68k_got_entry *) *_entry_ptr;
   1900   arg = (struct elf_m68k_can_merge_gots_arg *) _arg;
   1901 
   1902   entry2 = elf_m68k_get_got_entry (arg->big, &entry1->key_, SEARCH, NULL);
   1903 
   1904   if (entry2 != NULL)
   1905     /* We found an existing entry.  Check if we should update it.  */
   1906     {
   1907       type = elf_m68k_update_got_entry_type (arg->diff,
   1908 					     entry2->key_.type,
   1909 					     entry1->key_.type);
   1910 
   1911       if (type == entry2->key_.type)
   1912 	/* ENTRY1 doesn't update data in ENTRY2.  Skip it.
   1913 	   To skip creation of difference entry we use the type,
   1914 	   which we won't see in GOT entries for sure.  */
   1915 	type = R_68K_max;
   1916     }
   1917   else
   1918     /* We didn't find the entry.  Add entry1 to DIFF.  */
   1919     {
   1920       BFD_ASSERT (entry1->key_.type != R_68K_max);
   1921 
   1922       type = elf_m68k_update_got_entry_type (arg->diff,
   1923 					     R_68K_max, entry1->key_.type);
   1924 
   1925       if (entry1->key_.bfd != NULL)
   1926 	arg->diff->local_n_slots += elf_m68k_reloc_got_n_slots (type);
   1927     }
   1928 
   1929   if (type != R_68K_max)
   1930     /* Create an entry in DIFF.  */
   1931     {
   1932       struct elf_m68k_got_entry *entry;
   1933 
   1934       entry = elf_m68k_get_got_entry (arg->diff, &entry1->key_, MUST_CREATE,
   1935 				      arg->info);
   1936       if (entry == NULL)
   1937 	{
   1938 	  arg->error_p = true;
   1939 	  return 0;
   1940 	}
   1941 
   1942       entry->key_.type = type;
   1943     }
   1944 
   1945   return 1;
   1946 }
   1947 
   1948 /* Return TRUE if SMALL GOT can be added to BIG GOT without overflowing it.
   1949    Construct DIFF GOT holding the entries which should be added or updated
   1950    in BIG GOT to accumulate information from SMALL.
   1951    INFO is the context where memory should be allocated.  */
   1952 
   1953 static bool
   1954 elf_m68k_can_merge_gots (struct elf_m68k_got *big,
   1955 			 const struct elf_m68k_got *small,
   1956 			 struct bfd_link_info *info,
   1957 			 struct elf_m68k_got *diff)
   1958 {
   1959   struct elf_m68k_can_merge_gots_arg arg_;
   1960 
   1961   BFD_ASSERT (small->offset == (bfd_vma) -1);
   1962 
   1963   arg_.big = big;
   1964   arg_.diff = diff;
   1965   arg_.info = info;
   1966   arg_.error_p = false;
   1967   htab_traverse_noresize (small->entries, elf_m68k_can_merge_gots_1, &arg_);
   1968   if (arg_.error_p)
   1969     {
   1970       diff->offset = 0;
   1971       return false;
   1972     }
   1973 
   1974   /* Check for overflow.  */
   1975   if ((big->n_slots[R_8] + arg_.diff->n_slots[R_8]
   1976        > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
   1977       || (big->n_slots[R_16] + arg_.diff->n_slots[R_16]
   1978 	  > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
   1979     return false;
   1980 
   1981   return true;
   1982 }
   1983 
   1984 struct elf_m68k_merge_gots_arg
   1985 {
   1986   /* The BIG got.  */
   1987   struct elf_m68k_got *big;
   1988 
   1989   /* Context where memory should be allocated.  */
   1990   struct bfd_link_info *info;
   1991 
   1992   /* Error flag.  */
   1993   bool error_p;
   1994 };
   1995 
   1996 /* Process a single entry from DIFF got.  Add or update corresponding
   1997    entry in the BIG got.  */
   1998 
   1999 static int
   2000 elf_m68k_merge_gots_1 (void **entry_ptr, void *_arg)
   2001 {
   2002   const struct elf_m68k_got_entry *from;
   2003   struct elf_m68k_merge_gots_arg *arg;
   2004   struct elf_m68k_got_entry *to;
   2005 
   2006   from = (const struct elf_m68k_got_entry *) *entry_ptr;
   2007   arg = (struct elf_m68k_merge_gots_arg *) _arg;
   2008 
   2009   to = elf_m68k_get_got_entry (arg->big, &from->key_, FIND_OR_CREATE,
   2010 			       arg->info);
   2011   if (to == NULL)
   2012     {
   2013       arg->error_p = true;
   2014       return 0;
   2015     }
   2016 
   2017   BFD_ASSERT (to->u.s1.refcount == 0);
   2018   /* All we need to merge is TYPE.  */
   2019   to->key_.type = from->key_.type;
   2020 
   2021   return 1;
   2022 }
   2023 
   2024 /* Merge data from DIFF to BIG.  INFO is context where memory should be
   2025    allocated.  */
   2026 
   2027 static bool
   2028 elf_m68k_merge_gots (struct elf_m68k_got *big,
   2029 		     struct elf_m68k_got *diff,
   2030 		     struct bfd_link_info *info)
   2031 {
   2032   if (diff->entries != NULL)
   2033     /* DIFF is not empty.  Merge it into BIG GOT.  */
   2034     {
   2035       struct elf_m68k_merge_gots_arg arg_;
   2036 
   2037       /* Merge entries.  */
   2038       arg_.big = big;
   2039       arg_.info = info;
   2040       arg_.error_p = false;
   2041       htab_traverse_noresize (diff->entries, elf_m68k_merge_gots_1, &arg_);
   2042       if (arg_.error_p)
   2043 	return false;
   2044 
   2045       /* Merge counters.  */
   2046       big->n_slots[R_8] += diff->n_slots[R_8];
   2047       big->n_slots[R_16] += diff->n_slots[R_16];
   2048       big->n_slots[R_32] += diff->n_slots[R_32];
   2049       big->local_n_slots += diff->local_n_slots;
   2050     }
   2051   else
   2052     /* DIFF is empty.  */
   2053     {
   2054       BFD_ASSERT (diff->n_slots[R_8] == 0);
   2055       BFD_ASSERT (diff->n_slots[R_16] == 0);
   2056       BFD_ASSERT (diff->n_slots[R_32] == 0);
   2057       BFD_ASSERT (diff->local_n_slots == 0);
   2058     }
   2059 
   2060   BFD_ASSERT (!elf_m68k_hash_table (info)->allow_multigot_p
   2061 	      || ((big->n_slots[R_8]
   2062 		   <= ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
   2063 		  && (big->n_slots[R_16]
   2064 		      <= ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info))));
   2065 
   2066   return true;
   2067 }
   2068 
   2069 struct elf_m68k_finalize_got_offsets_arg
   2070 {
   2071   /* Ranges of the offsets for GOT entries.
   2072      R_x entries receive offsets between offset1[R_x] and offset2[R_x].
   2073      R_x is R_8, R_16 and R_32.  */
   2074   bfd_vma *offset1;
   2075   bfd_vma *offset2;
   2076 
   2077   /* Mapping from global symndx to global symbols.
   2078      This is used to build lists of got entries for global symbols.  */
   2079   struct elf_m68k_link_hash_entry **symndx2h;
   2080 
   2081   bfd_vma n_ldm_entries;
   2082 };
   2083 
   2084 /* Assign ENTRY an offset.  Build list of GOT entries for global symbols
   2085    along the way.  */
   2086 
   2087 static int
   2088 elf_m68k_finalize_got_offsets_1 (void **entry_ptr, void *_arg)
   2089 {
   2090   struct elf_m68k_got_entry *entry;
   2091   struct elf_m68k_finalize_got_offsets_arg *arg;
   2092 
   2093   enum elf_m68k_got_offset_size got_offset_size;
   2094   bfd_vma entry_size;
   2095 
   2096   entry = (struct elf_m68k_got_entry *) *entry_ptr;
   2097   arg = (struct elf_m68k_finalize_got_offsets_arg *) _arg;
   2098 
   2099   /* This should be a fresh entry created in elf_m68k_can_merge_gots.  */
   2100   BFD_ASSERT (entry->u.s1.refcount == 0);
   2101 
   2102   /* Get GOT offset size for the entry .  */
   2103   got_offset_size = elf_m68k_reloc_got_offset_size (entry->key_.type);
   2104 
   2105   /* Calculate entry size in bytes.  */
   2106   entry_size = 4 * elf_m68k_reloc_got_n_slots (entry->key_.type);
   2107 
   2108   /* Check if we should switch to negative range of the offsets. */
   2109   if (arg->offset1[got_offset_size] + entry_size
   2110       > arg->offset2[got_offset_size])
   2111     {
   2112       /* Verify that this is the only switch to negative range for
   2113 	 got_offset_size.  If this assertion fails, then we've miscalculated
   2114 	 range for got_offset_size entries in
   2115 	 elf_m68k_finalize_got_offsets.  */
   2116       BFD_ASSERT (arg->offset2[got_offset_size]
   2117 		  != arg->offset2[-(int) got_offset_size - 1]);
   2118 
   2119       /* Switch.  */
   2120       arg->offset1[got_offset_size] = arg->offset1[-(int) got_offset_size - 1];
   2121       arg->offset2[got_offset_size] = arg->offset2[-(int) got_offset_size - 1];
   2122 
   2123       /* Verify that now we have enough room for the entry.  */
   2124       BFD_ASSERT (arg->offset1[got_offset_size] + entry_size
   2125 		  <= arg->offset2[got_offset_size]);
   2126     }
   2127 
   2128   /* Assign offset to entry.  */
   2129   entry->u.s2.offset = arg->offset1[got_offset_size];
   2130   arg->offset1[got_offset_size] += entry_size;
   2131 
   2132   if (entry->key_.bfd == NULL)
   2133     /* Hook up this entry into the list of got_entries of H.  */
   2134     {
   2135       struct elf_m68k_link_hash_entry *h;
   2136 
   2137       h = arg->symndx2h[entry->key_.symndx];
   2138       if (h != NULL)
   2139 	{
   2140 	  entry->u.s2.next = h->glist;
   2141 	  h->glist = entry;
   2142 	}
   2143       else
   2144 	/* This should be the entry for TLS_LDM relocation then.  */
   2145 	{
   2146 	  BFD_ASSERT ((elf_m68k_reloc_got_type (entry->key_.type)
   2147 		       == R_68K_TLS_LDM32)
   2148 		      && entry->key_.symndx == 0);
   2149 
   2150 	  ++arg->n_ldm_entries;
   2151 	}
   2152     }
   2153   else
   2154     /* This entry is for local symbol.  */
   2155     entry->u.s2.next = NULL;
   2156 
   2157   return 1;
   2158 }
   2159 
   2160 /* Assign offsets within GOT.  USE_NEG_GOT_OFFSETS_P indicates if we
   2161    should use negative offsets.
   2162    Build list of GOT entries for global symbols along the way.
   2163    SYMNDX2H is mapping from global symbol indices to actual
   2164    global symbols.
   2165    Return offset at which next GOT should start.  */
   2166 
   2167 static void
   2168 elf_m68k_finalize_got_offsets (struct elf_m68k_got *got,
   2169 			       bool use_neg_got_offsets_p,
   2170 			       struct elf_m68k_link_hash_entry **symndx2h,
   2171 			       bfd_vma *final_offset, bfd_vma *n_ldm_entries)
   2172 {
   2173   struct elf_m68k_finalize_got_offsets_arg arg_;
   2174   bfd_vma offset1_[2 * R_LAST];
   2175   bfd_vma offset2_[2 * R_LAST];
   2176   int i;
   2177   bfd_vma start_offset;
   2178 
   2179   BFD_ASSERT (got->offset != (bfd_vma) -1);
   2180 
   2181   /* We set entry offsets relative to the .got section (and not the
   2182      start of a particular GOT), so that we can use them in
   2183      finish_dynamic_symbol without needing to know the GOT which they come
   2184      from.  */
   2185 
   2186   /* Put offset1 in the middle of offset1_, same for offset2.  */
   2187   arg_.offset1 = offset1_ + R_LAST;
   2188   arg_.offset2 = offset2_ + R_LAST;
   2189 
   2190   start_offset = got->offset;
   2191 
   2192   if (use_neg_got_offsets_p)
   2193     /* Setup both negative and positive ranges for R_8, R_16 and R_32.  */
   2194     i = -(int) R_32 - 1;
   2195   else
   2196     /* Setup positives ranges for R_8, R_16 and R_32.  */
   2197     i = (int) R_8;
   2198 
   2199   for (; i <= (int) R_32; ++i)
   2200     {
   2201       int j;
   2202       size_t n;
   2203 
   2204       /* Set beginning of the range of offsets I.  */
   2205       arg_.offset1[i] = start_offset;
   2206 
   2207       /* Calculate number of slots that require I offsets.  */
   2208       j = (i >= 0) ? i : -i - 1;
   2209       n = (j >= 1) ? got->n_slots[j - 1] : 0;
   2210       n = got->n_slots[j] - n;
   2211 
   2212       if (use_neg_got_offsets_p && n != 0)
   2213 	{
   2214 	  if (i < 0)
   2215 	    /* We first fill the positive side of the range, so we might
   2216 	       end up with one empty slot at that side when we can't fit
   2217 	       whole 2-slot entry.  Account for that at negative side of
   2218 	       the interval with one additional entry.  */
   2219 	    n = n / 2 + 1;
   2220 	  else
   2221 	    /* When the number of slots is odd, make positive side of the
   2222 	       range one entry bigger.  */
   2223 	    n = (n + 1) / 2;
   2224 	}
   2225 
   2226       /* N is the number of slots that require I offsets.
   2227 	 Calculate length of the range for I offsets.  */
   2228       n = 4 * n;
   2229 
   2230       /* Set end of the range.  */
   2231       arg_.offset2[i] = start_offset + n;
   2232 
   2233       start_offset = arg_.offset2[i];
   2234     }
   2235 
   2236   if (!use_neg_got_offsets_p)
   2237     /* Make sure that if we try to switch to negative offsets in
   2238        elf_m68k_finalize_got_offsets_1, the assert therein will catch
   2239        the bug.  */
   2240     for (i = R_8; i <= R_32; ++i)
   2241       arg_.offset2[-i - 1] = arg_.offset2[i];
   2242 
   2243   /* Setup got->offset.  offset1[R_8] is either in the middle or at the
   2244      beginning of GOT depending on use_neg_got_offsets_p.  */
   2245   got->offset = arg_.offset1[R_8];
   2246 
   2247   arg_.symndx2h = symndx2h;
   2248   arg_.n_ldm_entries = 0;
   2249 
   2250   /* Assign offsets.  */
   2251   htab_traverse (got->entries, elf_m68k_finalize_got_offsets_1, &arg_);
   2252 
   2253   /* Check offset ranges we have actually assigned.  */
   2254   for (i = (int) R_8; i <= (int) R_32; ++i)
   2255     BFD_ASSERT (arg_.offset2[i] - arg_.offset1[i] <= 4);
   2256 
   2257   *final_offset = start_offset;
   2258   *n_ldm_entries = arg_.n_ldm_entries;
   2259 }
   2260 
   2261 struct elf_m68k_partition_multi_got_arg
   2262 {
   2263   /* The GOT we are adding entries to.  Aka big got.  */
   2264   struct elf_m68k_got *current_got;
   2265 
   2266   /* Offset to assign the next CURRENT_GOT.  */
   2267   bfd_vma offset;
   2268 
   2269   /* Context where memory should be allocated.  */
   2270   struct bfd_link_info *info;
   2271 
   2272   /* Total number of slots in the .got section.
   2273      This is used to calculate size of the .got and .rela.got sections.  */
   2274   bfd_vma n_slots;
   2275 
   2276   /* Difference in numbers of allocated slots in the .got section
   2277      and necessary relocations in the .rela.got section.
   2278      This is used to calculate size of the .rela.got section.  */
   2279   bfd_vma slots_relas_diff;
   2280 
   2281   /* Error flag.  */
   2282   bool error_p;
   2283 
   2284   /* Mapping from global symndx to global symbols.
   2285      This is used to build lists of got entries for global symbols.  */
   2286   struct elf_m68k_link_hash_entry **symndx2h;
   2287 };
   2288 
   2289 static void
   2290 elf_m68k_partition_multi_got_2 (struct elf_m68k_partition_multi_got_arg *arg)
   2291 {
   2292   bfd_vma n_ldm_entries;
   2293 
   2294   elf_m68k_finalize_got_offsets (arg->current_got,
   2295 				 (elf_m68k_hash_table (arg->info)
   2296 				  ->use_neg_got_offsets_p),
   2297 				 arg->symndx2h,
   2298 				 &arg->offset, &n_ldm_entries);
   2299 
   2300   arg->n_slots += arg->current_got->n_slots[R_32];
   2301 
   2302   if (!bfd_link_pic (arg->info))
   2303     /* If we are generating a shared object, we need to
   2304        output a R_68K_RELATIVE reloc so that the dynamic
   2305        linker can adjust this GOT entry.  Overwise we
   2306        don't need space in .rela.got for local symbols.  */
   2307     arg->slots_relas_diff += arg->current_got->local_n_slots;
   2308 
   2309   /* @LDM relocations require a 2-slot GOT entry, but only
   2310      one relocation.  Account for that.  */
   2311   arg->slots_relas_diff += n_ldm_entries;
   2312 
   2313   BFD_ASSERT (arg->slots_relas_diff <= arg->n_slots);
   2314 }
   2315 
   2316 
   2317 /* Process a single BFD2GOT entry and either merge GOT to CURRENT_GOT
   2318    or start a new CURRENT_GOT.  */
   2319 
   2320 static int
   2321 elf_m68k_partition_multi_got_1 (void **_entry, void *_arg)
   2322 {
   2323   struct elf_m68k_bfd2got_entry *entry;
   2324   struct elf_m68k_partition_multi_got_arg *arg;
   2325   struct elf_m68k_got *got;
   2326   struct elf_m68k_got diff_;
   2327   struct elf_m68k_got *diff;
   2328 
   2329   entry = (struct elf_m68k_bfd2got_entry *) *_entry;
   2330   arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
   2331 
   2332   got = entry->got;
   2333   BFD_ASSERT (got != NULL);
   2334   BFD_ASSERT (got->offset == (bfd_vma) -1);
   2335 
   2336   diff = NULL;
   2337 
   2338   if (arg->current_got != NULL)
   2339     /* Construct diff.  */
   2340     {
   2341       diff = &diff_;
   2342       elf_m68k_init_got (diff);
   2343 
   2344       if (!elf_m68k_can_merge_gots (arg->current_got, got, arg->info, diff))
   2345 	{
   2346 	  if (diff->offset == 0)
   2347 	    /* Offset set to 0 in the diff_ indicates an error.  */
   2348 	    {
   2349 	      arg->error_p = true;
   2350 	      goto final_return;
   2351 	    }
   2352 
   2353 	  if (elf_m68k_hash_table (arg->info)->allow_multigot_p)
   2354 	    {
   2355 	      elf_m68k_clear_got (diff);
   2356 	      /* Schedule to finish up current_got and start new one.  */
   2357 	      diff = NULL;
   2358 	    }
   2359 	  /* else
   2360 	     Merge GOTs no matter what.  If big GOT overflows,
   2361 	     we'll fail in relocate_section due to truncated relocations.
   2362 
   2363 	     ??? May be fail earlier?  E.g., in can_merge_gots.  */
   2364 	}
   2365     }
   2366   else
   2367     /* Diff of got against empty current_got is got itself.  */
   2368     {
   2369       /* Create empty current_got to put subsequent GOTs to.  */
   2370       arg->current_got = elf_m68k_create_empty_got (arg->info);
   2371       if (arg->current_got == NULL)
   2372 	{
   2373 	  arg->error_p = true;
   2374 	  goto final_return;
   2375 	}
   2376 
   2377       arg->current_got->offset = arg->offset;
   2378 
   2379       diff = got;
   2380     }
   2381 
   2382   if (diff != NULL)
   2383     {
   2384       if (!elf_m68k_merge_gots (arg->current_got, diff, arg->info))
   2385 	{
   2386 	  arg->error_p = true;
   2387 	  goto final_return;
   2388 	}
   2389 
   2390       /* Now we can free GOT.  */
   2391       elf_m68k_clear_got (got);
   2392 
   2393       entry->got = arg->current_got;
   2394     }
   2395   else
   2396     {
   2397       /* Finish up current_got.  */
   2398       elf_m68k_partition_multi_got_2 (arg);
   2399 
   2400       /* Schedule to start a new current_got.  */
   2401       arg->current_got = NULL;
   2402 
   2403       /* Retry.  */
   2404       if (!elf_m68k_partition_multi_got_1 (_entry, _arg))
   2405 	{
   2406 	  BFD_ASSERT (arg->error_p);
   2407 	  goto final_return;
   2408 	}
   2409     }
   2410 
   2411  final_return:
   2412   if (diff != NULL)
   2413     elf_m68k_clear_got (diff);
   2414 
   2415   return !arg->error_p;
   2416 }
   2417 
   2418 /* Helper function to build symndx2h mapping.  */
   2419 
   2420 static bool
   2421 elf_m68k_init_symndx2h_1 (struct elf_link_hash_entry *_h,
   2422 			  void *_arg)
   2423 {
   2424   struct elf_m68k_link_hash_entry *h;
   2425 
   2426   h = elf_m68k_hash_entry (_h);
   2427 
   2428   if (h->got_entry_key != 0)
   2429     /* H has at least one entry in the GOT.  */
   2430     {
   2431       struct elf_m68k_partition_multi_got_arg *arg;
   2432 
   2433       arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
   2434 
   2435       BFD_ASSERT (arg->symndx2h[h->got_entry_key] == NULL);
   2436       arg->symndx2h[h->got_entry_key] = h;
   2437     }
   2438 
   2439   return true;
   2440 }
   2441 
   2442 /* Merge GOTs of some BFDs, assign offsets to GOT entries and build
   2443    lists of GOT entries for global symbols.
   2444    Calculate sizes of .got and .rela.got sections.  */
   2445 
   2446 static bool
   2447 elf_m68k_partition_multi_got (struct bfd_link_info *info)
   2448 {
   2449   struct elf_m68k_multi_got *multi_got;
   2450   struct elf_m68k_partition_multi_got_arg arg_;
   2451 
   2452   multi_got = elf_m68k_multi_got (info);
   2453 
   2454   arg_.current_got = NULL;
   2455   arg_.offset = 0;
   2456   arg_.info = info;
   2457   arg_.n_slots = 0;
   2458   arg_.slots_relas_diff = 0;
   2459   arg_.error_p = false;
   2460 
   2461   if (multi_got->bfd2got != NULL)
   2462     {
   2463       /* Initialize symndx2h mapping.  */
   2464       {
   2465 	arg_.symndx2h = bfd_zmalloc (multi_got->global_symndx
   2466 				     * sizeof (*arg_.symndx2h));
   2467 	if (arg_.symndx2h == NULL)
   2468 	  return false;
   2469 
   2470 	elf_link_hash_traverse (elf_hash_table (info),
   2471 				elf_m68k_init_symndx2h_1, &arg_);
   2472       }
   2473 
   2474       /* Partition.  */
   2475       htab_traverse (multi_got->bfd2got, elf_m68k_partition_multi_got_1,
   2476 		     &arg_);
   2477       if (arg_.error_p)
   2478 	{
   2479 	  free (arg_.symndx2h);
   2480 	  arg_.symndx2h = NULL;
   2481 
   2482 	  return false;
   2483 	}
   2484 
   2485       /* Finish up last current_got.  */
   2486       elf_m68k_partition_multi_got_2 (&arg_);
   2487 
   2488       free (arg_.symndx2h);
   2489     }
   2490 
   2491   if (elf_hash_table (info)->dynobj != NULL)
   2492     /* Set sizes of .got and .rela.got sections.  */
   2493     {
   2494       asection *s;
   2495 
   2496       s = elf_hash_table (info)->sgot;
   2497       if (s != NULL)
   2498 	s->size = arg_.offset;
   2499       else
   2500 	BFD_ASSERT (arg_.offset == 0);
   2501 
   2502       BFD_ASSERT (arg_.slots_relas_diff <= arg_.n_slots);
   2503       arg_.n_slots -= arg_.slots_relas_diff;
   2504 
   2505       s = elf_hash_table (info)->srelgot;
   2506       if (s != NULL)
   2507 	s->size = arg_.n_slots * sizeof (Elf32_External_Rela);
   2508       else
   2509 	BFD_ASSERT (arg_.n_slots == 0);
   2510     }
   2511   else
   2512     BFD_ASSERT (multi_got->bfd2got == NULL);
   2513 
   2514   return true;
   2515 }
   2516 
   2517 /* Copy any information related to dynamic linking from a pre-existing
   2518    symbol to a newly created symbol.  Also called to copy flags and
   2519    other back-end info to a weakdef, in which case the symbol is not
   2520    newly created and plt/got refcounts and dynamic indices should not
   2521    be copied.  */
   2522 
   2523 static void
   2524 elf_m68k_copy_indirect_symbol (struct bfd_link_info *info,
   2525 			       struct elf_link_hash_entry *_dir,
   2526 			       struct elf_link_hash_entry *_ind)
   2527 {
   2528   struct elf_m68k_link_hash_entry *dir;
   2529   struct elf_m68k_link_hash_entry *ind;
   2530 
   2531   _bfd_elf_link_hash_copy_indirect (info, _dir, _ind);
   2532 
   2533   if (_ind->root.type != bfd_link_hash_indirect)
   2534     return;
   2535 
   2536   dir = elf_m68k_hash_entry (_dir);
   2537   ind = elf_m68k_hash_entry (_ind);
   2538 
   2539   /* Any absolute non-dynamic relocations against an indirect or weak
   2540      definition will be against the target symbol.  */
   2541   _dir->non_got_ref |= _ind->non_got_ref;
   2542 
   2543   /* We might have a direct symbol already having entries in the GOTs.
   2544      Update its key only in case indirect symbol has GOT entries and
   2545      assert that both indirect and direct symbols don't have GOT entries
   2546      at the same time.  */
   2547   if (ind->got_entry_key != 0)
   2548     {
   2549       BFD_ASSERT (dir->got_entry_key == 0);
   2550       /* Assert that GOTs aren't partitioned yet.  */
   2551       BFD_ASSERT (ind->glist == NULL);
   2552 
   2553       dir->got_entry_key = ind->got_entry_key;
   2554       ind->got_entry_key = 0;
   2555     }
   2556 }
   2557 
   2558 /* Look through the relocs for a section during the first phase, and
   2559    allocate space in the global offset table or procedure linkage
   2560    table.  */
   2561 
   2562 static bool
   2563 elf_m68k_check_relocs (bfd *abfd,
   2564 		       struct bfd_link_info *info,
   2565 		       asection *sec,
   2566 		       const Elf_Internal_Rela *relocs)
   2567 {
   2568   bfd *dynobj;
   2569   Elf_Internal_Shdr *symtab_hdr;
   2570   struct elf_link_hash_entry **sym_hashes;
   2571   const Elf_Internal_Rela *rel;
   2572   const Elf_Internal_Rela *rel_end;
   2573   asection *sreloc;
   2574   struct elf_m68k_got *got;
   2575 
   2576   if (bfd_link_relocatable (info))
   2577     return true;
   2578 
   2579   dynobj = elf_hash_table (info)->dynobj;
   2580   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2581   sym_hashes = elf_sym_hashes (abfd);
   2582 
   2583   sreloc = NULL;
   2584 
   2585   got = NULL;
   2586 
   2587   rel_end = relocs + sec->reloc_count;
   2588   for (rel = relocs; rel < rel_end; rel++)
   2589     {
   2590       unsigned long r_symndx;
   2591       struct elf_link_hash_entry *h;
   2592 
   2593       r_symndx = ELF32_R_SYM (rel->r_info);
   2594 
   2595       if (r_symndx < symtab_hdr->sh_info)
   2596 	h = NULL;
   2597       else
   2598 	{
   2599 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2600 	  while (h->root.type == bfd_link_hash_indirect
   2601 		 || h->root.type == bfd_link_hash_warning)
   2602 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2603 	}
   2604 
   2605       switch (ELF32_R_TYPE (rel->r_info))
   2606 	{
   2607 	case R_68K_GOT8:
   2608 	case R_68K_GOT16:
   2609 	case R_68K_GOT32:
   2610 	  if (h != NULL
   2611 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   2612 	    break;
   2613 	  /* Fall through.  */
   2614 
   2615 	  /* Relative GOT relocations.  */
   2616 	case R_68K_GOT8O:
   2617 	case R_68K_GOT16O:
   2618 	case R_68K_GOT32O:
   2619 	  /* Fall through.  */
   2620 
   2621 	  /* TLS relocations.  */
   2622 	case R_68K_TLS_GD8:
   2623 	case R_68K_TLS_GD16:
   2624 	case R_68K_TLS_GD32:
   2625 	case R_68K_TLS_LDM8:
   2626 	case R_68K_TLS_LDM16:
   2627 	case R_68K_TLS_LDM32:
   2628 	case R_68K_TLS_IE8:
   2629 	case R_68K_TLS_IE16:
   2630 	case R_68K_TLS_IE32:
   2631 
   2632 	case R_68K_TLS_TPREL32:
   2633 	case R_68K_TLS_DTPREL32:
   2634 
   2635 	  if (ELF32_R_TYPE (rel->r_info) == R_68K_TLS_TPREL32
   2636 	      && bfd_link_pic (info))
   2637 	    /* Do the special chorus for libraries with static TLS.  */
   2638 	    info->flags |= DF_STATIC_TLS;
   2639 
   2640 	  /* This symbol requires a global offset table entry.  */
   2641 
   2642 	  if (dynobj == NULL)
   2643 	    {
   2644 	      /* Create the .got section.  */
   2645 	      elf_hash_table (info)->dynobj = dynobj = abfd;
   2646 	      if (!_bfd_elf_create_got_section (dynobj, info))
   2647 		return false;
   2648 	    }
   2649 
   2650 	  if (got == NULL)
   2651 	    {
   2652 	      struct elf_m68k_bfd2got_entry *bfd2got_entry;
   2653 
   2654 	      bfd2got_entry
   2655 		= elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
   2656 					      abfd, FIND_OR_CREATE, info);
   2657 	      if (bfd2got_entry == NULL)
   2658 		return false;
   2659 
   2660 	      got = bfd2got_entry->got;
   2661 	      BFD_ASSERT (got != NULL);
   2662 	    }
   2663 
   2664 	  {
   2665 	    struct elf_m68k_got_entry *got_entry;
   2666 
   2667 	    /* Add entry to got.  */
   2668 	    got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
   2669 						   ELF32_R_TYPE (rel->r_info),
   2670 						   r_symndx, info);
   2671 	    if (got_entry == NULL)
   2672 	      return false;
   2673 
   2674 	    if (got_entry->u.s1.refcount == 1)
   2675 	      {
   2676 		/* Make sure this symbol is output as a dynamic symbol.  */
   2677 		if (h != NULL
   2678 		    && h->dynindx == -1
   2679 		    && !h->forced_local)
   2680 		  {
   2681 		    if (!bfd_elf_link_record_dynamic_symbol (info, h))
   2682 		      return false;
   2683 		  }
   2684 	      }
   2685 	  }
   2686 
   2687 	  break;
   2688 
   2689 	case R_68K_PLT8:
   2690 	case R_68K_PLT16:
   2691 	case R_68K_PLT32:
   2692 	  /* This symbol requires a procedure linkage table entry.  We
   2693 	     actually build the entry in adjust_dynamic_symbol,
   2694 	     because this might be a case of linking PIC code which is
   2695 	     never referenced by a dynamic object, in which case we
   2696 	     don't need to generate a procedure linkage table entry
   2697 	     after all.  */
   2698 
   2699 	  /* If this is a local symbol, we resolve it directly without
   2700 	     creating a procedure linkage table entry.  */
   2701 	  if (h == NULL)
   2702 	    continue;
   2703 
   2704 	  h->needs_plt = 1;
   2705 	  h->plt.refcount++;
   2706 	  break;
   2707 
   2708 	case R_68K_PLT8O:
   2709 	case R_68K_PLT16O:
   2710 	case R_68K_PLT32O:
   2711 	  /* This symbol requires a procedure linkage table entry.  */
   2712 
   2713 	  if (h == NULL)
   2714 	    {
   2715 	      /* It does not make sense to have this relocation for a
   2716 		 local symbol.  FIXME: does it?  How to handle it if
   2717 		 it does make sense?  */
   2718 	      bfd_set_error (bfd_error_bad_value);
   2719 	      return false;
   2720 	    }
   2721 
   2722 	  /* Make sure this symbol is output as a dynamic symbol.  */
   2723 	  if (h->dynindx == -1
   2724 	      && !h->forced_local)
   2725 	    {
   2726 	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
   2727 		return false;
   2728 	    }
   2729 
   2730 	  h->needs_plt = 1;
   2731 	  h->plt.refcount++;
   2732 	  break;
   2733 
   2734 	case R_68K_PC8:
   2735 	case R_68K_PC16:
   2736 	case R_68K_PC32:
   2737 	  /* If we are creating a shared library and this is not a local
   2738 	     symbol, we need to copy the reloc into the shared library.
   2739 	     However when linking with -Bsymbolic and this is a global
   2740 	     symbol which is defined in an object we are including in the
   2741 	     link (i.e., DEF_REGULAR is set), then we can resolve the
   2742 	     reloc directly.  At this point we have not seen all the input
   2743 	     files, so it is possible that DEF_REGULAR is not set now but
   2744 	     will be set later (it is never cleared).  We account for that
   2745 	     possibility below by storing information in the
   2746 	     pcrel_relocs_copied field of the hash table entry.  */
   2747 	  if (!(bfd_link_pic (info)
   2748 		&& (sec->flags & SEC_ALLOC) != 0
   2749 		&& h != NULL
   2750 		&& (!SYMBOLIC_BIND (info, h)
   2751 		    || h->root.type == bfd_link_hash_defweak
   2752 		    || !h->def_regular)))
   2753 	    {
   2754 	      if (h != NULL)
   2755 		{
   2756 		  /* Make sure a plt entry is created for this symbol if
   2757 		     it turns out to be a function defined by a dynamic
   2758 		     object.  */
   2759 		  h->plt.refcount++;
   2760 		}
   2761 	      break;
   2762 	    }
   2763 	  /* Fall through.  */
   2764 	case R_68K_8:
   2765 	case R_68K_16:
   2766 	case R_68K_32:
   2767 	  /* We don't need to handle relocs into sections not going into
   2768 	     the "real" output.  */
   2769 	  if ((sec->flags & SEC_ALLOC) == 0)
   2770 	      break;
   2771 
   2772 	  if (h != NULL)
   2773 	    {
   2774 	      /* Make sure a plt entry is created for this symbol if it
   2775 		 turns out to be a function defined by a dynamic object.  */
   2776 	      h->plt.refcount++;
   2777 
   2778 	      if (bfd_link_executable (info))
   2779 		/* This symbol needs a non-GOT reference.  */
   2780 		h->non_got_ref = 1;
   2781 	    }
   2782 
   2783 	  /* If we are creating a shared library, we need to copy the
   2784 	     reloc into the shared library.  */
   2785 	  if (bfd_link_pic (info)
   2786 	      && (h == NULL
   2787 		  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
   2788 	    {
   2789 	      /* When creating a shared object, we must copy these
   2790 		 reloc types into the output file.  We create a reloc
   2791 		 section in dynobj and make room for this reloc.  */
   2792 	      if (sreloc == NULL)
   2793 		{
   2794 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   2795 		    (sec, dynobj, 2, abfd, /*rela?*/ true);
   2796 
   2797 		  if (sreloc == NULL)
   2798 		    return false;
   2799 		}
   2800 
   2801 	      if (sec->flags & SEC_READONLY
   2802 		  /* Don't set DF_TEXTREL yet for PC relative
   2803 		     relocations, they might be discarded later.  */
   2804 		  && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
   2805 		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
   2806 		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
   2807 		{
   2808 		  if (bfd_link_textrel_check(info))
   2809 		    (*_bfd_error_handler)
   2810 		      (_("warning: dynamic relocation to `%s' in readonly section `%s'"),
   2811 		      h->root.root.string, sec->name);
   2812 		  info->flags |= DF_TEXTREL;
   2813 		}
   2814 
   2815 	      sreloc->size += sizeof (Elf32_External_Rela);
   2816 
   2817 	      /* We count the number of PC relative relocations we have
   2818 		 entered for this symbol, so that we can discard them
   2819 		 again if, in the -Bsymbolic case, the symbol is later
   2820 		 defined by a regular object, or, in the normal shared
   2821 		 case, the symbol is forced to be local.  Note that this
   2822 		 function is only called if we are using an m68kelf linker
   2823 		 hash table, which means that h is really a pointer to an
   2824 		 elf_m68k_link_hash_entry.  */
   2825 	      if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
   2826 		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
   2827 		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
   2828 		{
   2829 		  struct elf_m68k_pcrel_relocs_copied *p;
   2830 		  struct elf_m68k_pcrel_relocs_copied **head;
   2831 
   2832 		  if (h != NULL)
   2833 		    {
   2834 		      struct elf_m68k_link_hash_entry *eh
   2835 			= elf_m68k_hash_entry (h);
   2836 		      head = &eh->pcrel_relocs_copied;
   2837 		    }
   2838 		  else
   2839 		    {
   2840 		      asection *s;
   2841 		      void *vpp;
   2842 		      Elf_Internal_Sym *isym;
   2843 
   2844 		      isym = bfd_sym_from_r_symndx (&elf_m68k_hash_table (info)->root.sym_cache,
   2845 						    abfd, r_symndx);
   2846 		      if (isym == NULL)
   2847 			return false;
   2848 
   2849 		      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   2850 		      if (s == NULL)
   2851 			s = sec;
   2852 
   2853 		      vpp = &elf_section_data (s)->local_dynrel;
   2854 		      head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
   2855 		    }
   2856 
   2857 		  for (p = *head; p != NULL; p = p->next)
   2858 		    if (p->section == sreloc)
   2859 		      break;
   2860 
   2861 		  if (p == NULL)
   2862 		    {
   2863 		      p = ((struct elf_m68k_pcrel_relocs_copied *)
   2864 			   bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
   2865 		      if (p == NULL)
   2866 			return false;
   2867 		      p->next = *head;
   2868 		      *head = p;
   2869 		      p->section = sreloc;
   2870 		      p->count = 0;
   2871 		    }
   2872 
   2873 		  ++p->count;
   2874 		}
   2875 	    }
   2876 
   2877 	  break;
   2878 
   2879 	  /* This relocation describes the C++ object vtable hierarchy.
   2880 	     Reconstruct it for later use during GC.  */
   2881 	case R_68K_GNU_VTINHERIT:
   2882 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   2883 	    return false;
   2884 	  break;
   2885 
   2886 	  /* This relocation describes which C++ vtable entries are actually
   2887 	     used.  Record for later use during GC.  */
   2888 	case R_68K_GNU_VTENTRY:
   2889 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   2890 	    return false;
   2891 	  break;
   2892 
   2893 	default:
   2894 	  break;
   2895 	}
   2896     }
   2897 
   2898   return true;
   2899 }
   2900 
   2901 /* Return the section that should be marked against GC for a given
   2902    relocation.  */
   2903 
   2904 static asection *
   2905 elf_m68k_gc_mark_hook (asection *sec,
   2906 		       struct bfd_link_info *info,
   2907 		       Elf_Internal_Rela *rel,
   2908 		       struct elf_link_hash_entry *h,
   2909 		       Elf_Internal_Sym *sym)
   2910 {
   2911   if (h != NULL)
   2912     switch (ELF32_R_TYPE (rel->r_info))
   2913       {
   2914       case R_68K_GNU_VTINHERIT:
   2915       case R_68K_GNU_VTENTRY:
   2916 	return NULL;
   2917       }
   2918 
   2919   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   2920 }
   2921 
   2922 /* Return the type of PLT associated with OUTPUT_BFD.  */
   2924 
   2925 static const struct elf_m68k_plt_info *
   2926 elf_m68k_get_plt_info (bfd *output_bfd)
   2927 {
   2928   unsigned int features;
   2929 
   2930   features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
   2931   if (features & cpu32)
   2932     return &elf_cpu32_plt_info;
   2933   if (features & mcfisa_b)
   2934     return &elf_isab_plt_info;
   2935   if (features & mcfisa_c)
   2936     return &elf_isac_plt_info;
   2937   return &elf_m68k_plt_info;
   2938 }
   2939 
   2940 /* This function is called after all the input files have been read,
   2941    and the input sections have been assigned to output sections.
   2942    It's a convenient place to determine the PLT style.  */
   2943 
   2944 static bool
   2945 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
   2946 {
   2947   /* Bind input BFDs to GOTs and calculate sizes of .got and .rela.got
   2948      sections.  */
   2949   if (!elf_m68k_partition_multi_got (info))
   2950     return false;
   2951 
   2952   elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
   2953   return true;
   2954 }
   2955 
   2956 /* Adjust a symbol defined by a dynamic object and referenced by a
   2957    regular object.  The current definition is in some section of the
   2958    dynamic object, but we're not including those sections.  We have to
   2959    change the definition to something the rest of the link can
   2960    understand.  */
   2961 
   2962 static bool
   2963 elf_m68k_adjust_dynamic_symbol (struct bfd_link_info *info,
   2964 				struct elf_link_hash_entry *h)
   2965 {
   2966   struct elf_m68k_link_hash_table *htab;
   2967   bfd *dynobj;
   2968   asection *s;
   2969 
   2970   htab = elf_m68k_hash_table (info);
   2971   dynobj = htab->root.dynobj;
   2972 
   2973   /* Make sure we know what is going on here.  */
   2974   BFD_ASSERT (dynobj != NULL
   2975 	      && (h->needs_plt
   2976 		  || h->type == STT_GNU_IFUNC
   2977 		  || h->is_weakalias
   2978 		  || (h->def_dynamic
   2979 		      && h->ref_regular
   2980 		      && !h->def_regular)));
   2981 
   2982   /* If this is a function, put it in the procedure linkage table.  We
   2983      will fill in the contents of the procedure linkage table later,
   2984      when we know the address of the .got section.  */
   2985   if ((h->type == STT_FUNC || h->type == STT_GNU_IFUNC)
   2986       || h->needs_plt)
   2987     {
   2988       if ((h->plt.refcount <= 0
   2989 	   || SYMBOL_CALLS_LOCAL (info, h)
   2990 	   || ((ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2991 		|| UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   2992 	       && h->root.type == bfd_link_hash_undefweak))
   2993 	  /* We must always create the plt entry if it was referenced
   2994 	     by a PLTxxO relocation.  In this case we already recorded
   2995 	     it as a dynamic symbol.  */
   2996 	  && h->dynindx == -1)
   2997 	{
   2998 	  /* This case can occur if we saw a PLTxx reloc in an input
   2999 	     file, but the symbol was never referred to by a dynamic
   3000 	     object, or if all references were garbage collected.  In
   3001 	     such a case, we don't actually need to build a procedure
   3002 	     linkage table, and we can just do a PCxx reloc instead.  */
   3003 	  h->plt.offset = (bfd_vma) -1;
   3004 	  h->needs_plt = 0;
   3005 	  return true;
   3006 	}
   3007 
   3008       /* Make sure this symbol is output as a dynamic symbol.  */
   3009       if (h->dynindx == -1
   3010 	  && !h->forced_local)
   3011 	{
   3012 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   3013 	    return false;
   3014 	}
   3015 
   3016       s = htab->root.splt;
   3017       BFD_ASSERT (s != NULL);
   3018 
   3019       /* If this is the first .plt entry, make room for the special
   3020 	 first entry.  */
   3021       if (s->size == 0)
   3022 	s->size = htab->plt_info->size;
   3023 
   3024       /* If this symbol is not defined in a regular file, and we are
   3025 	 not generating a shared library, then set the symbol to this
   3026 	 location in the .plt.  This is required to make function
   3027 	 pointers compare as equal between the normal executable and
   3028 	 the shared library.  */
   3029       if (!bfd_link_pic (info)
   3030 	  && !h->def_regular)
   3031 	{
   3032 	  h->root.u.def.section = s;
   3033 	  h->root.u.def.value = s->size;
   3034 	}
   3035 
   3036       h->plt.offset = s->size;
   3037 
   3038       /* Make room for this entry.  */
   3039       s->size += htab->plt_info->size;
   3040 
   3041       /* We also need to make an entry in the .got.plt section, which
   3042 	 will be placed in the .got section by the linker script.  */
   3043       s = htab->root.sgotplt;
   3044       BFD_ASSERT (s != NULL);
   3045       s->size += 4;
   3046 
   3047       /* We also need to make an entry in the .rela.plt section.  */
   3048       s = htab->root.srelplt;
   3049       BFD_ASSERT (s != NULL);
   3050       s->size += sizeof (Elf32_External_Rela);
   3051 
   3052       return true;
   3053     }
   3054 
   3055   /* Reinitialize the plt offset now that it is not used as a reference
   3056      count any more.  */
   3057   h->plt.offset = (bfd_vma) -1;
   3058 
   3059   /* If this is a weak symbol, and there is a real definition, the
   3060      processor independent code will have arranged for us to see the
   3061      real definition first, and we can just use the same value.  */
   3062   if (h->is_weakalias)
   3063     {
   3064       struct elf_link_hash_entry *def = weakdef (h);
   3065       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   3066       h->root.u.def.section = def->root.u.def.section;
   3067       h->root.u.def.value = def->root.u.def.value;
   3068       return true;
   3069     }
   3070 
   3071   /* This is a reference to a symbol defined by a dynamic object which
   3072      is not a function.  */
   3073 
   3074   /* If we are creating a shared library, we must presume that the
   3075      only references to the symbol are via the global offset table.
   3076      For such cases we need not do anything here; the relocations will
   3077      be handled correctly by relocate_section.  */
   3078   if (bfd_link_pic (info))
   3079     return true;
   3080 
   3081   /* If there are no references to this symbol that do not use the
   3082      GOT, we don't need to generate a copy reloc.  */
   3083   if (!h->non_got_ref)
   3084     return true;
   3085 
   3086   /* We must allocate the symbol in our .dynbss section, which will
   3087      become part of the .bss section of the executable.  There will be
   3088      an entry for this symbol in the .dynsym section.  The dynamic
   3089      object will contain position independent code, so all references
   3090      from the dynamic object to this symbol will go through the global
   3091      offset table.  The dynamic linker will use the .dynsym entry to
   3092      determine the address it must put in the global offset table, so
   3093      both the dynamic object and the regular object will refer to the
   3094      same memory location for the variable.  */
   3095 
   3096   s = bfd_get_linker_section (dynobj, ".dynbss");
   3097   BFD_ASSERT (s != NULL);
   3098 
   3099   /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
   3100      copy the initial value out of the dynamic object and into the
   3101      runtime process image.  We need to remember the offset into the
   3102      .rela.bss section we are going to use.  */
   3103   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   3104     {
   3105       asection *srel;
   3106 
   3107       srel = bfd_get_linker_section (dynobj, ".rela.bss");
   3108       BFD_ASSERT (srel != NULL);
   3109       srel->size += sizeof (Elf32_External_Rela);
   3110       h->needs_copy = 1;
   3111     }
   3112 
   3113   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   3114 }
   3115 
   3116 /* Set the sizes of the dynamic sections.  */
   3117 
   3118 static bool
   3119 elf_m68k_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   3120 				struct bfd_link_info *info)
   3121 {
   3122   bfd *dynobj;
   3123   asection *s;
   3124   bool relocs;
   3125 
   3126   dynobj = elf_hash_table (info)->dynobj;
   3127   BFD_ASSERT (dynobj != NULL);
   3128 
   3129   if (elf_hash_table (info)->dynamic_sections_created)
   3130     {
   3131       /* Set the contents of the .interp section to the interpreter.  */
   3132       if (bfd_link_executable (info) && !info->nointerp)
   3133 	{
   3134 	  s = bfd_get_linker_section (dynobj, ".interp");
   3135 	  BFD_ASSERT (s != NULL);
   3136 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   3137 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   3138 	}
   3139     }
   3140   else
   3141     {
   3142       /* We may have created entries in the .rela.got section.
   3143 	 However, if we are not creating the dynamic sections, we will
   3144 	 not actually use these entries.  Reset the size of .rela.got,
   3145 	 which will cause it to get stripped from the output file
   3146 	 below.  */
   3147       s = elf_hash_table (info)->srelgot;
   3148       if (s != NULL)
   3149 	s->size = 0;
   3150     }
   3151 
   3152   /* If this is a -Bsymbolic shared link, then we need to discard all
   3153      PC relative relocs against symbols defined in a regular object.
   3154      For the normal shared case we discard the PC relative relocs
   3155      against symbols that have become local due to visibility changes.
   3156      We allocated space for them in the check_relocs routine, but we
   3157      will not fill them in in the relocate_section routine.  */
   3158   if (bfd_link_pic (info))
   3159     elf_link_hash_traverse (elf_hash_table (info),
   3160 			    elf_m68k_discard_copies,
   3161 			    info);
   3162 
   3163   /* The check_relocs and adjust_dynamic_symbol entry points have
   3164      determined the sizes of the various dynamic sections.  Allocate
   3165      memory for them.  */
   3166   relocs = false;
   3167   for (s = dynobj->sections; s != NULL; s = s->next)
   3168     {
   3169       const char *name;
   3170 
   3171       if ((s->flags & SEC_LINKER_CREATED) == 0)
   3172 	continue;
   3173 
   3174       /* It's OK to base decisions on the section name, because none
   3175 	 of the dynobj section names depend upon the input files.  */
   3176       name = bfd_section_name (s);
   3177 
   3178       if (strcmp (name, ".plt") == 0)
   3179 	{
   3180 	  /* Remember whether there is a PLT.  */
   3181 	  ;
   3182 	}
   3183       else if (startswith (name, ".rela"))
   3184 	{
   3185 	  if (s->size != 0)
   3186 	    {
   3187 	      relocs = true;
   3188 
   3189 	      /* We use the reloc_count field as a counter if we need
   3190 		 to copy relocs into the output file.  */
   3191 	      s->reloc_count = 0;
   3192 	    }
   3193 	}
   3194       else if (! startswith (name, ".got")
   3195 	       && strcmp (name, ".dynbss") != 0)
   3196 	{
   3197 	  /* It's not one of our sections, so don't allocate space.  */
   3198 	  continue;
   3199 	}
   3200 
   3201       if (s->size == 0)
   3202 	{
   3203 	  /* If we don't need this section, strip it from the
   3204 	     output file.  This is mostly to handle .rela.bss and
   3205 	     .rela.plt.  We must create both sections in
   3206 	     create_dynamic_sections, because they must be created
   3207 	     before the linker maps input sections to output
   3208 	     sections.  The linker does that before
   3209 	     adjust_dynamic_symbol is called, and it is that
   3210 	     function which decides whether anything needs to go
   3211 	     into these sections.  */
   3212 	  s->flags |= SEC_EXCLUDE;
   3213 	  continue;
   3214 	}
   3215 
   3216       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   3217 	continue;
   3218 
   3219       /* Allocate memory for the section contents.  */
   3220       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
   3221 	 Unused entries should be reclaimed before the section's contents
   3222 	 are written out, but at the moment this does not happen.  Thus in
   3223 	 order to prevent writing out garbage, we initialise the section's
   3224 	 contents to zero.  */
   3225       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   3226       if (s->contents == NULL)
   3227 	return false;
   3228     }
   3229 
   3230   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
   3231 }
   3232 
   3233 /* This function is called via elf_link_hash_traverse if we are
   3234    creating a shared object.  In the -Bsymbolic case it discards the
   3235    space allocated to copy PC relative relocs against symbols which
   3236    are defined in regular objects.  For the normal shared case, it
   3237    discards space for pc-relative relocs that have become local due to
   3238    symbol visibility changes.  We allocated space for them in the
   3239    check_relocs routine, but we won't fill them in in the
   3240    relocate_section routine.
   3241 
   3242    We also check whether any of the remaining relocations apply
   3243    against a readonly section, and set the DF_TEXTREL flag in this
   3244    case.  */
   3245 
   3246 static bool
   3247 elf_m68k_discard_copies (struct elf_link_hash_entry *h,
   3248 			 void * inf)
   3249 {
   3250   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   3251   struct elf_m68k_pcrel_relocs_copied *s;
   3252 
   3253   if (!SYMBOL_CALLS_LOCAL (info, h))
   3254     {
   3255       if ((info->flags & DF_TEXTREL) == 0)
   3256 	{
   3257 	  /* Look for relocations against read-only sections.  */
   3258 	  for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
   3259 	       s != NULL;
   3260 	       s = s->next)
   3261 	    if ((s->section->flags & SEC_READONLY) != 0)
   3262 	      {
   3263 		if (bfd_link_textrel_check(info))
   3264 		  (*_bfd_error_handler)
   3265 		    (_("warning: dynamic relocation to `%s' in readonly section `%s'"),
   3266 		    h->root.root.string, s->section->name);
   3267 		info->flags |= DF_TEXTREL;
   3268 		break;
   3269 	      }
   3270 	}
   3271 
   3272       /* Make sure undefined weak symbols are output as a dynamic symbol
   3273 	 in PIEs.  */
   3274       if (h->non_got_ref
   3275 	  && h->root.type == bfd_link_hash_undefweak
   3276 	  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3277 	  && h->dynindx == -1
   3278 	  && !h->forced_local)
   3279 	{
   3280 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   3281 	    return false;
   3282 	}
   3283 
   3284       return true;
   3285     }
   3286 
   3287   for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
   3288        s != NULL;
   3289        s = s->next)
   3290     s->section->size -= s->count * sizeof (Elf32_External_Rela);
   3291 
   3292   return true;
   3293 }
   3294 
   3295 
   3296 /* Install relocation RELA.  */
   3297 
   3298 static void
   3299 elf_m68k_install_rela (bfd *output_bfd,
   3300 		       asection *srela,
   3301 		       Elf_Internal_Rela *rela)
   3302 {
   3303   bfd_byte *loc;
   3304 
   3305   loc = srela->contents;
   3306   loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
   3307   bfd_elf32_swap_reloca_out (output_bfd, rela, loc);
   3308 }
   3309 
   3310 /* Find the base offsets for thread-local storage in this object,
   3311    for GD/LD and IE/LE respectively.  */
   3312 
   3313 #define DTP_OFFSET 0x8000
   3314 #define TP_OFFSET  0x7000
   3315 
   3316 static bfd_vma
   3317 dtpoff_base (struct bfd_link_info *info)
   3318 {
   3319   /* If tls_sec is NULL, we should have signalled an error already.  */
   3320   if (elf_hash_table (info)->tls_sec == NULL)
   3321     return 0;
   3322   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   3323 }
   3324 
   3325 static bfd_vma
   3326 tpoff_base (struct bfd_link_info *info)
   3327 {
   3328   /* If tls_sec is NULL, we should have signalled an error already.  */
   3329   if (elf_hash_table (info)->tls_sec == NULL)
   3330     return 0;
   3331   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   3332 }
   3333 
   3334 /* Output necessary relocation to handle a symbol during static link.
   3335    This function is called from elf_m68k_relocate_section.  */
   3336 
   3337 static void
   3338 elf_m68k_init_got_entry_static (struct bfd_link_info *info,
   3339 				bfd *output_bfd,
   3340 				enum elf_m68k_reloc_type r_type,
   3341 				asection *sgot,
   3342 				bfd_vma got_entry_offset,
   3343 				bfd_vma relocation)
   3344 {
   3345   switch (elf_m68k_reloc_got_type (r_type))
   3346     {
   3347     case R_68K_GOT32O:
   3348       bfd_put_32 (output_bfd, relocation, sgot->contents + got_entry_offset);
   3349       break;
   3350 
   3351     case R_68K_TLS_GD32:
   3352       /* We know the offset within the module,
   3353 	 put it into the second GOT slot.  */
   3354       bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
   3355 		  sgot->contents + got_entry_offset + 4);
   3356       /* FALLTHRU */
   3357 
   3358     case R_68K_TLS_LDM32:
   3359       /* Mark it as belonging to module 1, the executable.  */
   3360       bfd_put_32 (output_bfd, 1, sgot->contents + got_entry_offset);
   3361       break;
   3362 
   3363     case R_68K_TLS_IE32:
   3364       bfd_put_32 (output_bfd, relocation - tpoff_base (info),
   3365 		  sgot->contents + got_entry_offset);
   3366       break;
   3367 
   3368     default:
   3369       BFD_ASSERT (false);
   3370     }
   3371 }
   3372 
   3373 /* Output necessary relocation to handle a local symbol
   3374    during dynamic link.
   3375    This function is called either from elf_m68k_relocate_section
   3376    or from elf_m68k_finish_dynamic_symbol.  */
   3377 
   3378 static void
   3379 elf_m68k_init_got_entry_local_shared (struct bfd_link_info *info,
   3380 				      bfd *output_bfd,
   3381 				      enum elf_m68k_reloc_type r_type,
   3382 				      asection *sgot,
   3383 				      bfd_vma got_entry_offset,
   3384 				      bfd_vma relocation,
   3385 				      asection *srela)
   3386 {
   3387   Elf_Internal_Rela outrel;
   3388 
   3389   switch (elf_m68k_reloc_got_type (r_type))
   3390     {
   3391     case R_68K_GOT32O:
   3392       /* Emit RELATIVE relocation to initialize GOT slot
   3393 	 at run-time.  */
   3394       outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
   3395       outrel.r_addend = relocation;
   3396       break;
   3397 
   3398     case R_68K_TLS_GD32:
   3399       /* We know the offset within the module,
   3400 	 put it into the second GOT slot.  */
   3401       bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
   3402 		  sgot->contents + got_entry_offset + 4);
   3403       /* FALLTHRU */
   3404 
   3405     case R_68K_TLS_LDM32:
   3406       /* We don't know the module number,
   3407 	 create a relocation for it.  */
   3408       outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_DTPMOD32);
   3409       outrel.r_addend = 0;
   3410       break;
   3411 
   3412     case R_68K_TLS_IE32:
   3413       /* Emit TPREL relocation to initialize GOT slot
   3414 	 at run-time.  */
   3415       outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_TPREL32);
   3416       outrel.r_addend = relocation - elf_hash_table (info)->tls_sec->vma;
   3417       break;
   3418 
   3419     default:
   3420       BFD_ASSERT (false);
   3421     }
   3422 
   3423   /* Offset of the GOT entry.  */
   3424   outrel.r_offset = (sgot->output_section->vma
   3425 		     + sgot->output_offset
   3426 		     + got_entry_offset);
   3427 
   3428   /* Install one of the above relocations.  */
   3429   elf_m68k_install_rela (output_bfd, srela, &outrel);
   3430 
   3431   bfd_put_32 (output_bfd, outrel.r_addend, sgot->contents + got_entry_offset);
   3432 }
   3433 
   3434 /* Relocate an M68K ELF section.  */
   3435 
   3436 static int
   3437 elf_m68k_relocate_section (bfd *output_bfd,
   3438 			   struct bfd_link_info *info,
   3439 			   bfd *input_bfd,
   3440 			   asection *input_section,
   3441 			   bfd_byte *contents,
   3442 			   Elf_Internal_Rela *relocs,
   3443 			   Elf_Internal_Sym *local_syms,
   3444 			   asection **local_sections)
   3445 {
   3446   Elf_Internal_Shdr *symtab_hdr;
   3447   struct elf_link_hash_entry **sym_hashes;
   3448   asection *sgot;
   3449   asection *splt;
   3450   asection *sreloc;
   3451   asection *srela;
   3452   struct elf_m68k_got *got;
   3453   Elf_Internal_Rela *rel;
   3454   Elf_Internal_Rela *relend;
   3455 
   3456   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3457   sym_hashes = elf_sym_hashes (input_bfd);
   3458 
   3459   sgot = NULL;
   3460   splt = NULL;
   3461   sreloc = NULL;
   3462   srela = NULL;
   3463 
   3464   got = NULL;
   3465 
   3466   rel = relocs;
   3467   relend = relocs + input_section->reloc_count;
   3468   for (; rel < relend; rel++)
   3469     {
   3470       int r_type;
   3471       reloc_howto_type *howto;
   3472       unsigned long r_symndx;
   3473       struct elf_link_hash_entry *h;
   3474       Elf_Internal_Sym *sym;
   3475       asection *sec;
   3476       bfd_vma relocation;
   3477       bool unresolved_reloc;
   3478       bfd_reloc_status_type r;
   3479       bool resolved_to_zero;
   3480 
   3481       r_type = ELF32_R_TYPE (rel->r_info);
   3482       if (r_type < 0 || r_type >= (int) R_68K_max)
   3483 	{
   3484 	  bfd_set_error (bfd_error_bad_value);
   3485 	  return false;
   3486 	}
   3487       howto = howto_table + r_type;
   3488 
   3489       r_symndx = ELF32_R_SYM (rel->r_info);
   3490 
   3491       h = NULL;
   3492       sym = NULL;
   3493       sec = NULL;
   3494       unresolved_reloc = false;
   3495 
   3496       if (r_symndx < symtab_hdr->sh_info)
   3497 	{
   3498 	  sym = local_syms + r_symndx;
   3499 	  sec = local_sections[r_symndx];
   3500 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   3501 	}
   3502       else
   3503 	{
   3504 	  bool warned, ignored;
   3505 
   3506 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   3507 				   r_symndx, symtab_hdr, sym_hashes,
   3508 				   h, sec, relocation,
   3509 				   unresolved_reloc, warned, ignored);
   3510 	}
   3511 
   3512       if (sec != NULL && discarded_section (sec))
   3513 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   3514 					 rel, 1, relend, howto, 0, contents);
   3515 
   3516       if (bfd_link_relocatable (info))
   3517 	continue;
   3518 
   3519       resolved_to_zero = (h != NULL
   3520 			  && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   3521 
   3522       switch (r_type)
   3523 	{
   3524 	case R_68K_GOT8:
   3525 	case R_68K_GOT16:
   3526 	case R_68K_GOT32:
   3527 	  /* Relocation is to the address of the entry for this symbol
   3528 	     in the global offset table.  */
   3529 	  if (h != NULL
   3530 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   3531 	    {
   3532 	      if (elf_m68k_hash_table (info)->local_gp_p)
   3533 		{
   3534 		  bfd_vma sgot_output_offset;
   3535 		  bfd_vma got_offset;
   3536 
   3537 		  sgot = elf_hash_table (info)->sgot;
   3538 
   3539 		  if (sgot != NULL)
   3540 		    sgot_output_offset = sgot->output_offset;
   3541 		  else
   3542 		    /* In this case we have a reference to
   3543 		       _GLOBAL_OFFSET_TABLE_, but the GOT itself is
   3544 		       empty.
   3545 		       ??? Issue a warning?  */
   3546 		    sgot_output_offset = 0;
   3547 
   3548 		  if (got == NULL)
   3549 		    {
   3550 		      struct elf_m68k_bfd2got_entry *bfd2got_entry;
   3551 
   3552 		      bfd2got_entry
   3553 			= elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
   3554 						      input_bfd, SEARCH, NULL);
   3555 
   3556 		      if (bfd2got_entry != NULL)
   3557 			{
   3558 			  got = bfd2got_entry->got;
   3559 			  BFD_ASSERT (got != NULL);
   3560 
   3561 			  got_offset = got->offset;
   3562 			}
   3563 		      else
   3564 			/* In this case we have a reference to
   3565 			   _GLOBAL_OFFSET_TABLE_, but no other references
   3566 			   accessing any GOT entries.
   3567 			   ??? Issue a warning?  */
   3568 			got_offset = 0;
   3569 		    }
   3570 		  else
   3571 		    got_offset = got->offset;
   3572 
   3573 		  /* Adjust GOT pointer to point to the GOT
   3574 		     assigned to input_bfd.  */
   3575 		  rel->r_addend += sgot_output_offset + got_offset;
   3576 		}
   3577 	      else
   3578 		BFD_ASSERT (got == NULL || got->offset == 0);
   3579 
   3580 	      break;
   3581 	    }
   3582 	  /* Fall through.  */
   3583 	case R_68K_GOT8O:
   3584 	case R_68K_GOT16O:
   3585 	case R_68K_GOT32O:
   3586 
   3587 	case R_68K_TLS_LDM32:
   3588 	case R_68K_TLS_LDM16:
   3589 	case R_68K_TLS_LDM8:
   3590 
   3591 	case R_68K_TLS_GD8:
   3592 	case R_68K_TLS_GD16:
   3593 	case R_68K_TLS_GD32:
   3594 
   3595 	case R_68K_TLS_IE8:
   3596 	case R_68K_TLS_IE16:
   3597 	case R_68K_TLS_IE32:
   3598 
   3599 	  /* Relocation is the offset of the entry for this symbol in
   3600 	     the global offset table.  */
   3601 
   3602 	  {
   3603 	    struct elf_m68k_got_entry_key key_;
   3604 	    bfd_vma *off_ptr;
   3605 	    bfd_vma off;
   3606 
   3607 	    sgot = elf_hash_table (info)->sgot;
   3608 	    BFD_ASSERT (sgot != NULL);
   3609 
   3610 	    if (got == NULL)
   3611 	      got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
   3612 						input_bfd, MUST_FIND,
   3613 						NULL)->got;
   3614 
   3615 	    /* Get GOT offset for this symbol.  */
   3616 	    elf_m68k_init_got_entry_key (&key_, h, input_bfd, r_symndx,
   3617 					 r_type);
   3618 	    off_ptr = &elf_m68k_get_got_entry (got, &key_, MUST_FIND,
   3619 					       NULL)->u.s2.offset;
   3620 	    off = *off_ptr;
   3621 
   3622 	    /* The offset must always be a multiple of 4.  We use
   3623 	       the least significant bit to record whether we have
   3624 	       already generated the necessary reloc.  */
   3625 	    if ((off & 1) != 0)
   3626 	      off &= ~1;
   3627 	    else
   3628 	      {
   3629 		if (h != NULL
   3630 		    /* @TLSLDM relocations are bounded to the module, in
   3631 		       which the symbol is defined -- not to the symbol
   3632 		       itself.  */
   3633 		    && elf_m68k_reloc_got_type (r_type) != R_68K_TLS_LDM32)
   3634 		  {
   3635 		    bool dyn;
   3636 
   3637 		    dyn = elf_hash_table (info)->dynamic_sections_created;
   3638 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   3639 							  bfd_link_pic (info),
   3640 							  h)
   3641 			|| (bfd_link_pic (info)
   3642 			    && SYMBOL_REFERENCES_LOCAL (info, h))
   3643 			|| ((ELF_ST_VISIBILITY (h->other)
   3644 			     || resolved_to_zero)
   3645 			    && h->root.type == bfd_link_hash_undefweak))
   3646 		      {
   3647 			/* This is actually a static link, or it is a
   3648 			   -Bsymbolic link and the symbol is defined
   3649 			   locally, or the symbol was forced to be local
   3650 			   because of a version file.  We must initialize
   3651 			   this entry in the global offset table.  Since
   3652 			   the offset must always be a multiple of 4, we
   3653 			   use the least significant bit to record whether
   3654 			   we have initialized it already.
   3655 
   3656 			   When doing a dynamic link, we create a .rela.got
   3657 			   relocation entry to initialize the value.  This
   3658 			   is done in the finish_dynamic_symbol routine.  */
   3659 
   3660 			elf_m68k_init_got_entry_static (info,
   3661 							output_bfd,
   3662 							r_type,
   3663 							sgot,
   3664 							off,
   3665 							relocation);
   3666 
   3667 			*off_ptr |= 1;
   3668 		      }
   3669 		    else
   3670 		      unresolved_reloc = false;
   3671 		  }
   3672 		else if (bfd_link_pic (info)) /* && h == NULL */
   3673 		  /* Process local symbol during dynamic link.  */
   3674 		  {
   3675 		    srela = elf_hash_table (info)->srelgot;
   3676 		    BFD_ASSERT (srela != NULL);
   3677 
   3678 		    elf_m68k_init_got_entry_local_shared (info,
   3679 							  output_bfd,
   3680 							  r_type,
   3681 							  sgot,
   3682 							  off,
   3683 							  relocation,
   3684 							  srela);
   3685 
   3686 		    *off_ptr |= 1;
   3687 		  }
   3688 		else /* h == NULL && !bfd_link_pic (info) */
   3689 		  {
   3690 		    elf_m68k_init_got_entry_static (info,
   3691 						    output_bfd,
   3692 						    r_type,
   3693 						    sgot,
   3694 						    off,
   3695 						    relocation);
   3696 
   3697 		    *off_ptr |= 1;
   3698 		  }
   3699 	      }
   3700 
   3701 	    /* We don't use elf_m68k_reloc_got_type in the condition below
   3702 	       because this is the only place where difference between
   3703 	       R_68K_GOTx and R_68K_GOTxO relocations matters.  */
   3704 	    if (r_type == R_68K_GOT32O
   3705 		|| r_type == R_68K_GOT16O
   3706 		|| r_type == R_68K_GOT8O
   3707 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_GD32
   3708 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_LDM32
   3709 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_IE32)
   3710 	      {
   3711 		/* GOT pointer is adjusted to point to the start/middle
   3712 		   of local GOT.  Adjust the offset accordingly.  */
   3713 		BFD_ASSERT (elf_m68k_hash_table (info)->use_neg_got_offsets_p
   3714 			    || off >= got->offset);
   3715 
   3716 		if (elf_m68k_hash_table (info)->local_gp_p)
   3717 		  relocation = off - got->offset;
   3718 		else
   3719 		  {
   3720 		    BFD_ASSERT (got->offset == 0);
   3721 		    relocation = sgot->output_offset + off;
   3722 		  }
   3723 
   3724 		/* This relocation does not use the addend.  */
   3725 		rel->r_addend = 0;
   3726 	      }
   3727 	    else
   3728 	      relocation = (sgot->output_section->vma + sgot->output_offset
   3729 			    + off);
   3730 	  }
   3731 	  break;
   3732 
   3733 	case R_68K_TLS_LDO32:
   3734 	case R_68K_TLS_LDO16:
   3735 	case R_68K_TLS_LDO8:
   3736 	  relocation -= dtpoff_base (info);
   3737 	  break;
   3738 
   3739 	case R_68K_TLS_LE32:
   3740 	case R_68K_TLS_LE16:
   3741 	case R_68K_TLS_LE8:
   3742 	  if (bfd_link_dll (info))
   3743 	    {
   3744 	      _bfd_error_handler
   3745 		/* xgettext:c-format */
   3746 		(_("%pB(%pA+%#" PRIx64 "): "
   3747 		   "%s relocation not permitted in shared object"),
   3748 		 input_bfd, input_section, (uint64_t) rel->r_offset,
   3749 		 howto->name);
   3750 
   3751 	      return false;
   3752 	    }
   3753 	  else
   3754 	    relocation -= tpoff_base (info);
   3755 
   3756 	  break;
   3757 
   3758 	case R_68K_PLT8:
   3759 	case R_68K_PLT16:
   3760 	case R_68K_PLT32:
   3761 	  /* Relocation is to the entry for this symbol in the
   3762 	     procedure linkage table.  */
   3763 
   3764 	  /* Resolve a PLTxx reloc against a local symbol directly,
   3765 	     without using the procedure linkage table.  */
   3766 	  if (h == NULL)
   3767 	    break;
   3768 
   3769 	  if (h->plt.offset == (bfd_vma) -1
   3770 	      || !elf_hash_table (info)->dynamic_sections_created)
   3771 	    {
   3772 	      /* We didn't make a PLT entry for this symbol.  This
   3773 		 happens when statically linking PIC code, or when
   3774 		 using -Bsymbolic.  */
   3775 	      break;
   3776 	    }
   3777 
   3778 	  splt = elf_hash_table (info)->splt;
   3779 	  BFD_ASSERT (splt != NULL);
   3780 
   3781 	  relocation = (splt->output_section->vma
   3782 			+ splt->output_offset
   3783 			+ h->plt.offset);
   3784 	  unresolved_reloc = false;
   3785 	  break;
   3786 
   3787 	case R_68K_PLT8O:
   3788 	case R_68K_PLT16O:
   3789 	case R_68K_PLT32O:
   3790 	  /* Relocation is the offset of the entry for this symbol in
   3791 	     the procedure linkage table.  */
   3792 	  BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
   3793 
   3794 	  splt = elf_hash_table (info)->splt;
   3795 	  BFD_ASSERT (splt != NULL);
   3796 
   3797 	  relocation = h->plt.offset;
   3798 	  unresolved_reloc = false;
   3799 
   3800 	  /* This relocation does not use the addend.  */
   3801 	  rel->r_addend = 0;
   3802 
   3803 	  break;
   3804 
   3805 	case R_68K_8:
   3806 	case R_68K_16:
   3807 	case R_68K_32:
   3808 	case R_68K_PC8:
   3809 	case R_68K_PC16:
   3810 	case R_68K_PC32:
   3811 	  if (bfd_link_pic (info)
   3812 	      && r_symndx != STN_UNDEF
   3813 	      && (input_section->flags & SEC_ALLOC) != 0
   3814 	      && (h == NULL
   3815 		  || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3816 		      && !resolved_to_zero)
   3817 		  || h->root.type != bfd_link_hash_undefweak)
   3818 	      && ((r_type != R_68K_PC8
   3819 		   && r_type != R_68K_PC16
   3820 		   && r_type != R_68K_PC32)
   3821 		  || !SYMBOL_CALLS_LOCAL (info, h)))
   3822 	    {
   3823 	      Elf_Internal_Rela outrel;
   3824 	      bfd_byte *loc;
   3825 	      bool skip, relocate;
   3826 
   3827 	      /* When generating a shared object, these relocations
   3828 		 are copied into the output file to be resolved at run
   3829 		 time.  */
   3830 
   3831 	      skip = false;
   3832 	      relocate = false;
   3833 
   3834 	      outrel.r_offset =
   3835 		_bfd_elf_section_offset (output_bfd, info, input_section,
   3836 					 rel->r_offset);
   3837 	      if (outrel.r_offset == (bfd_vma) -1)
   3838 		skip = true;
   3839 	      else if (outrel.r_offset == (bfd_vma) -2)
   3840 		skip = true, relocate = true;
   3841 	      outrel.r_offset += (input_section->output_section->vma
   3842 				  + input_section->output_offset);
   3843 
   3844 	      if (skip)
   3845 		memset (&outrel, 0, sizeof outrel);
   3846 	      else if (h != NULL
   3847 		       && h->dynindx != -1
   3848 		       && (r_type == R_68K_PC8
   3849 			   || r_type == R_68K_PC16
   3850 			   || r_type == R_68K_PC32
   3851 			   || !bfd_link_pic (info)
   3852 			   || !SYMBOLIC_BIND (info, h)
   3853 			   || !h->def_regular))
   3854 		{
   3855 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   3856 		  outrel.r_addend = rel->r_addend;
   3857 		}
   3858 	      else
   3859 		{
   3860 		  /* This symbol is local, or marked to become local.  */
   3861 		  outrel.r_addend = relocation + rel->r_addend;
   3862 
   3863 		  if (r_type == R_68K_32)
   3864 		    {
   3865 		      relocate = true;
   3866 		      outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
   3867 		    }
   3868 		  else
   3869 		    {
   3870 		      long indx;
   3871 
   3872 		      if (bfd_is_abs_section (sec))
   3873 			indx = 0;
   3874 		      else if (sec == NULL || sec->owner == NULL)
   3875 			{
   3876 			  bfd_set_error (bfd_error_bad_value);
   3877 			  return false;
   3878 			}
   3879 		      else
   3880 			{
   3881 			  asection *osec;
   3882 
   3883 			  /* We are turning this relocation into one
   3884 			     against a section symbol.  It would be
   3885 			     proper to subtract the symbol's value,
   3886 			     osec->vma, from the emitted reloc addend,
   3887 			     but ld.so expects buggy relocs.  */
   3888 			  osec = sec->output_section;
   3889 			  indx = elf_section_data (osec)->dynindx;
   3890 			  if (indx == 0)
   3891 			    {
   3892 			      struct elf_link_hash_table *htab;
   3893 			      htab = elf_hash_table (info);
   3894 			      osec = htab->text_index_section;
   3895 			      indx = elf_section_data (osec)->dynindx;
   3896 			    }
   3897 			  BFD_ASSERT (indx != 0);
   3898 			}
   3899 
   3900 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
   3901 		    }
   3902 		}
   3903 
   3904 	      sreloc = elf_section_data (input_section)->sreloc;
   3905 	      if (sreloc == NULL)
   3906 		abort ();
   3907 
   3908 	      loc = sreloc->contents;
   3909 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
   3910 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   3911 
   3912 	      /* This reloc will be computed at runtime, so there's no
   3913 		 need to do anything now, except for R_68K_32
   3914 		 relocations that have been turned into
   3915 		 R_68K_RELATIVE.  */
   3916 	      if (!relocate)
   3917 		continue;
   3918 	    }
   3919 
   3920 	  break;
   3921 
   3922 	case R_68K_GNU_VTINHERIT:
   3923 	case R_68K_GNU_VTENTRY:
   3924 	  /* These are no-ops in the end.  */
   3925 	  continue;
   3926 
   3927 	default:
   3928 	  break;
   3929 	}
   3930 
   3931       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   3932 	 because such sections are not SEC_ALLOC and thus ld.so will
   3933 	 not process them.  */
   3934       if (unresolved_reloc
   3935 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   3936 	       && h->def_dynamic)
   3937 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   3938 				      rel->r_offset) != (bfd_vma) -1)
   3939 	{
   3940 	  _bfd_error_handler
   3941 	    /* xgettext:c-format */
   3942 	    (_("%pB(%pA+%#" PRIx64 "): "
   3943 	       "unresolvable %s relocation against symbol `%s'"),
   3944 	     input_bfd,
   3945 	     input_section,
   3946 	     (uint64_t) rel->r_offset,
   3947 	     howto->name,
   3948 	     h->root.root.string);
   3949 	  return false;
   3950 	}
   3951 
   3952       if (r_symndx != STN_UNDEF
   3953 	  && r_type != R_68K_NONE
   3954 	  && (h == NULL
   3955 	      || h->root.type == bfd_link_hash_defined
   3956 	      || h->root.type == bfd_link_hash_defweak))
   3957 	{
   3958 	  char sym_type;
   3959 
   3960 	  sym_type = (sym != NULL) ? ELF32_ST_TYPE (sym->st_info) : h->type;
   3961 
   3962 	  if (elf_m68k_reloc_tls_p (r_type) != (sym_type == STT_TLS))
   3963 	    {
   3964 	      const char *name;
   3965 
   3966 	      if (h != NULL)
   3967 		name = h->root.root.string;
   3968 	      else
   3969 		{
   3970 		  name = (bfd_elf_string_from_elf_section
   3971 			  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   3972 		  if (name == NULL || *name == '\0')
   3973 		    name = bfd_section_name (sec);
   3974 		}
   3975 
   3976 	      _bfd_error_handler
   3977 		((sym_type == STT_TLS
   3978 		  /* xgettext:c-format */
   3979 		  ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
   3980 		  /* xgettext:c-format */
   3981 		  : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
   3982 		 input_bfd,
   3983 		 input_section,
   3984 		 (uint64_t) rel->r_offset,
   3985 		 howto->name,
   3986 		 name);
   3987 	    }
   3988 	}
   3989 
   3990       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   3991 				    contents, rel->r_offset,
   3992 				    relocation, rel->r_addend);
   3993 
   3994       if (r != bfd_reloc_ok)
   3995 	{
   3996 	  const char *name;
   3997 
   3998 	  if (h != NULL)
   3999 	    name = h->root.root.string;
   4000 	  else
   4001 	    {
   4002 	      name = bfd_elf_string_from_elf_section (input_bfd,
   4003 						      symtab_hdr->sh_link,
   4004 						      sym->st_name);
   4005 	      if (name == NULL)
   4006 		return false;
   4007 	      if (*name == '\0')
   4008 		name = bfd_section_name (sec);
   4009 	    }
   4010 
   4011 	  if (r == bfd_reloc_overflow)
   4012 	    (*info->callbacks->reloc_overflow)
   4013 	      (info, (h ? &h->root : NULL), name, howto->name,
   4014 	       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   4015 	  else
   4016 	    {
   4017 	      _bfd_error_handler
   4018 		/* xgettext:c-format */
   4019 		(_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
   4020 		 input_bfd, input_section,
   4021 		 (uint64_t) rel->r_offset, name, (int) r);
   4022 	      return false;
   4023 	    }
   4024 	}
   4025     }
   4026 
   4027   return true;
   4028 }
   4029 
   4030 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
   4031    into section SEC.  */
   4032 
   4033 static void
   4034 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
   4035 {
   4036   /* Make VALUE PC-relative.  */
   4037   value -= sec->output_section->vma + offset;
   4038 
   4039   /* Apply any in-place addend.  */
   4040   value += bfd_get_32 (sec->owner, sec->contents + offset);
   4041 
   4042   bfd_put_32 (sec->owner, value, sec->contents + offset);
   4043 }
   4044 
   4045 /* Finish up dynamic symbol handling.  We set the contents of various
   4046    dynamic sections here.  */
   4047 
   4048 static bool
   4049 elf_m68k_finish_dynamic_symbol (bfd *output_bfd,
   4050 				struct bfd_link_info *info,
   4051 				struct elf_link_hash_entry *h,
   4052 				Elf_Internal_Sym *sym)
   4053 {
   4054   bfd *dynobj;
   4055 
   4056   dynobj = elf_hash_table (info)->dynobj;
   4057 
   4058   if (h->plt.offset != (bfd_vma) -1)
   4059     {
   4060       const struct elf_m68k_plt_info *plt_info;
   4061       asection *splt;
   4062       asection *sgot;
   4063       asection *srela;
   4064       bfd_vma plt_index;
   4065       bfd_vma got_offset;
   4066       Elf_Internal_Rela rela;
   4067       bfd_byte *loc;
   4068 
   4069       /* This symbol has an entry in the procedure linkage table.  Set
   4070 	 it up.  */
   4071 
   4072       BFD_ASSERT (h->dynindx != -1);
   4073 
   4074       plt_info = elf_m68k_hash_table (info)->plt_info;
   4075       splt = elf_hash_table (info)->splt;
   4076       sgot = elf_hash_table (info)->sgotplt;
   4077       srela = elf_hash_table (info)->srelplt;
   4078       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
   4079 
   4080       /* Get the index in the procedure linkage table which
   4081 	 corresponds to this symbol.  This is the index of this symbol
   4082 	 in all the symbols for which we are making plt entries.  The
   4083 	 first entry in the procedure linkage table is reserved.  */
   4084       plt_index = (h->plt.offset / plt_info->size) - 1;
   4085 
   4086       /* Get the offset into the .got table of the entry that
   4087 	 corresponds to this function.  Each .got entry is 4 bytes.
   4088 	 The first three are reserved.  */
   4089       got_offset = (plt_index + 3) * 4;
   4090 
   4091       memcpy (splt->contents + h->plt.offset,
   4092 	      plt_info->symbol_entry,
   4093 	      plt_info->size);
   4094 
   4095       elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
   4096 			     (sgot->output_section->vma
   4097 			      + sgot->output_offset
   4098 			      + got_offset));
   4099 
   4100       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
   4101 		  splt->contents
   4102 		  + h->plt.offset
   4103 		  + plt_info->symbol_resolve_entry + 2);
   4104 
   4105       elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
   4106 			     splt->output_section->vma);
   4107 
   4108       /* Fill in the entry in the global offset table.  */
   4109       bfd_put_32 (output_bfd,
   4110 		  (splt->output_section->vma
   4111 		   + splt->output_offset
   4112 		   + h->plt.offset
   4113 		   + plt_info->symbol_resolve_entry),
   4114 		  sgot->contents + got_offset);
   4115 
   4116       /* Fill in the entry in the .rela.plt section.  */
   4117       rela.r_offset = (sgot->output_section->vma
   4118 		       + sgot->output_offset
   4119 		       + got_offset);
   4120       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
   4121       rela.r_addend = 0;
   4122       loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
   4123       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   4124 
   4125       if (!h->def_regular)
   4126 	{
   4127 	  /* Mark the symbol as undefined, rather than as defined in
   4128 	     the .plt section.  Leave the value alone.  */
   4129 	  sym->st_shndx = SHN_UNDEF;
   4130 	}
   4131     }
   4132 
   4133   if (elf_m68k_hash_entry (h)->glist != NULL)
   4134     {
   4135       asection *sgot;
   4136       asection *srela;
   4137       struct elf_m68k_got_entry *got_entry;
   4138 
   4139       /* This symbol has an entry in the global offset table.  Set it
   4140 	 up.  */
   4141 
   4142       sgot = elf_hash_table (info)->sgot;
   4143       srela = elf_hash_table (info)->srelgot;
   4144       BFD_ASSERT (sgot != NULL && srela != NULL);
   4145 
   4146       got_entry = elf_m68k_hash_entry (h)->glist;
   4147 
   4148       while (got_entry != NULL)
   4149 	{
   4150 	  enum elf_m68k_reloc_type r_type;
   4151 	  bfd_vma got_entry_offset;
   4152 
   4153 	  r_type = got_entry->key_.type;
   4154 	  got_entry_offset = got_entry->u.s2.offset &~ (bfd_vma) 1;
   4155 
   4156 	  /* If this is a -Bsymbolic link, and the symbol is defined
   4157 	     locally, we just want to emit a RELATIVE reloc.  Likewise if
   4158 	     the symbol was forced to be local because of a version file.
   4159 	     The entry in the global offset table already have been
   4160 	     initialized in the relocate_section function.  */
   4161 	  if (bfd_link_pic (info)
   4162 	      && SYMBOL_REFERENCES_LOCAL (info, h))
   4163 	    {
   4164 	      bfd_vma relocation;
   4165 
   4166 	      relocation = bfd_get_signed_32 (output_bfd,
   4167 					      (sgot->contents
   4168 					       + got_entry_offset));
   4169 
   4170 	      /* Undo TP bias.  */
   4171 	      switch (elf_m68k_reloc_got_type (r_type))
   4172 		{
   4173 		case R_68K_GOT32O:
   4174 		case R_68K_TLS_LDM32:
   4175 		  break;
   4176 
   4177 		case R_68K_TLS_GD32:
   4178 		  /* The value for this relocation is actually put in
   4179 		     the second GOT slot.  */
   4180 		  relocation = bfd_get_signed_32 (output_bfd,
   4181 						  (sgot->contents
   4182 						   + got_entry_offset + 4));
   4183 		  relocation += dtpoff_base (info);
   4184 		  break;
   4185 
   4186 		case R_68K_TLS_IE32:
   4187 		  relocation += tpoff_base (info);
   4188 		  break;
   4189 
   4190 		default:
   4191 		  BFD_ASSERT (false);
   4192 		}
   4193 
   4194 	      elf_m68k_init_got_entry_local_shared (info,
   4195 						    output_bfd,
   4196 						    r_type,
   4197 						    sgot,
   4198 						    got_entry_offset,
   4199 						    relocation,
   4200 						    srela);
   4201 	    }
   4202 	  else
   4203 	    {
   4204 	      Elf_Internal_Rela rela;
   4205 
   4206 	      /* Put zeros to GOT slots that will be initialized
   4207 		 at run-time.  */
   4208 	      {
   4209 		bfd_vma n_slots;
   4210 
   4211 		n_slots = elf_m68k_reloc_got_n_slots (got_entry->key_.type);
   4212 		while (n_slots--)
   4213 		  bfd_put_32 (output_bfd, (bfd_vma) 0,
   4214 			      (sgot->contents + got_entry_offset
   4215 			       + 4 * n_slots));
   4216 	      }
   4217 
   4218 	      rela.r_addend = 0;
   4219 	      rela.r_offset = (sgot->output_section->vma
   4220 			       + sgot->output_offset
   4221 			       + got_entry_offset);
   4222 
   4223 	      switch (elf_m68k_reloc_got_type (r_type))
   4224 		{
   4225 		case R_68K_GOT32O:
   4226 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
   4227 		  elf_m68k_install_rela (output_bfd, srela, &rela);
   4228 		  break;
   4229 
   4230 		case R_68K_TLS_GD32:
   4231 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPMOD32);
   4232 		  elf_m68k_install_rela (output_bfd, srela, &rela);
   4233 
   4234 		  rela.r_offset += 4;
   4235 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPREL32);
   4236 		  elf_m68k_install_rela (output_bfd, srela, &rela);
   4237 		  break;
   4238 
   4239 		case R_68K_TLS_IE32:
   4240 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_TPREL32);
   4241 		  elf_m68k_install_rela (output_bfd, srela, &rela);
   4242 		  break;
   4243 
   4244 		default:
   4245 		  BFD_ASSERT (false);
   4246 		  break;
   4247 		}
   4248 	    }
   4249 
   4250 	  got_entry = got_entry->u.s2.next;
   4251 	}
   4252     }
   4253 
   4254   if (h->needs_copy)
   4255     {
   4256       asection *s;
   4257       Elf_Internal_Rela rela;
   4258       bfd_byte *loc;
   4259 
   4260       /* This symbol needs a copy reloc.  Set it up.  */
   4261 
   4262       BFD_ASSERT (h->dynindx != -1
   4263 		  && (h->root.type == bfd_link_hash_defined
   4264 		      || h->root.type == bfd_link_hash_defweak));
   4265 
   4266       s = bfd_get_linker_section (dynobj, ".rela.bss");
   4267       BFD_ASSERT (s != NULL);
   4268 
   4269       rela.r_offset = (h->root.u.def.value
   4270 		       + h->root.u.def.section->output_section->vma
   4271 		       + h->root.u.def.section->output_offset);
   4272       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
   4273       rela.r_addend = 0;
   4274       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   4275       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   4276     }
   4277 
   4278   return true;
   4279 }
   4280 
   4281 /* Finish up the dynamic sections.  */
   4282 
   4283 static bool
   4284 elf_m68k_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
   4285 {
   4286   bfd *dynobj;
   4287   asection *sgot;
   4288   asection *sdyn;
   4289 
   4290   dynobj = elf_hash_table (info)->dynobj;
   4291 
   4292   sgot = elf_hash_table (info)->sgotplt;
   4293   BFD_ASSERT (sgot != NULL);
   4294   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   4295 
   4296   if (elf_hash_table (info)->dynamic_sections_created)
   4297     {
   4298       asection *splt;
   4299       Elf32_External_Dyn *dyncon, *dynconend;
   4300 
   4301       splt = elf_hash_table (info)->splt;
   4302       BFD_ASSERT (splt != NULL && sdyn != NULL);
   4303 
   4304       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   4305       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   4306       for (; dyncon < dynconend; dyncon++)
   4307 	{
   4308 	  Elf_Internal_Dyn dyn;
   4309 	  asection *s;
   4310 
   4311 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   4312 
   4313 	  switch (dyn.d_tag)
   4314 	    {
   4315 	    default:
   4316 	      break;
   4317 
   4318 	    case DT_PLTGOT:
   4319 	      s = elf_hash_table (info)->sgotplt;
   4320 	      goto get_vma;
   4321 	    case DT_JMPREL:
   4322 	      s = elf_hash_table (info)->srelplt;
   4323 	    get_vma:
   4324 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   4325 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   4326 	      break;
   4327 
   4328 	    case DT_PLTRELSZ:
   4329 	      s = elf_hash_table (info)->srelplt;
   4330 	      dyn.d_un.d_val = s->size;
   4331 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   4332 	      break;
   4333 	    }
   4334 	}
   4335 
   4336       /* Fill in the first entry in the procedure linkage table.  */
   4337       if (splt->size > 0)
   4338 	{
   4339 	  const struct elf_m68k_plt_info *plt_info;
   4340 
   4341 	  plt_info = elf_m68k_hash_table (info)->plt_info;
   4342 	  memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
   4343 
   4344 	  elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
   4345 				 (sgot->output_section->vma
   4346 				  + sgot->output_offset
   4347 				  + 4));
   4348 
   4349 	  elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
   4350 				 (sgot->output_section->vma
   4351 				  + sgot->output_offset
   4352 				  + 8));
   4353 
   4354 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize
   4355 	    = plt_info->size;
   4356 	}
   4357     }
   4358 
   4359   /* Fill in the first three entries in the global offset table.  */
   4360   if (sgot->size > 0)
   4361     {
   4362       if (sdyn == NULL)
   4363 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   4364       else
   4365 	bfd_put_32 (output_bfd,
   4366 		    sdyn->output_section->vma + sdyn->output_offset,
   4367 		    sgot->contents);
   4368       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   4369       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   4370     }
   4371 
   4372   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   4373 
   4374   return true;
   4375 }
   4376 
   4377 /* Given a .data section and a .emreloc in-memory section, store
   4378    relocation information into the .emreloc section which can be
   4379    used at runtime to relocate the section.  This is called by the
   4380    linker when the --embedded-relocs switch is used.  This is called
   4381    after the add_symbols entry point has been called for all the
   4382    objects, and before the final_link entry point is called.  */
   4383 
   4384 bool
   4385 bfd_m68k_elf32_create_embedded_relocs (bfd *abfd, struct bfd_link_info *info,
   4386 				       asection *datasec, asection *relsec,
   4387 				       char **errmsg)
   4388 {
   4389   Elf_Internal_Shdr *symtab_hdr;
   4390   Elf_Internal_Sym *isymbuf = NULL;
   4391   Elf_Internal_Rela *internal_relocs = NULL;
   4392   Elf_Internal_Rela *irel, *irelend;
   4393   bfd_byte *p;
   4394   bfd_size_type amt;
   4395 
   4396   BFD_ASSERT (! bfd_link_relocatable (info));
   4397 
   4398   *errmsg = NULL;
   4399 
   4400   if (datasec->reloc_count == 0)
   4401     return true;
   4402 
   4403   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   4404 
   4405   /* Get a copy of the native relocations.  */
   4406   internal_relocs = (_bfd_elf_link_read_relocs
   4407 		     (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
   4408 		      info->keep_memory));
   4409   if (internal_relocs == NULL)
   4410     goto error_return;
   4411 
   4412   amt = (bfd_size_type) datasec->reloc_count * 12;
   4413   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
   4414   if (relsec->contents == NULL)
   4415     goto error_return;
   4416 
   4417   p = relsec->contents;
   4418 
   4419   irelend = internal_relocs + datasec->reloc_count;
   4420   for (irel = internal_relocs; irel < irelend; irel++, p += 12)
   4421     {
   4422       asection *targetsec;
   4423 
   4424       /* We are going to write a four byte longword into the runtime
   4425        reloc section.  The longword will be the address in the data
   4426        section which must be relocated.  It is followed by the name
   4427        of the target section NUL-padded or truncated to 8
   4428        characters.  */
   4429 
   4430       /* We can only relocate absolute longword relocs at run time.  */
   4431       if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
   4432 	{
   4433 	  *errmsg = _("unsupported relocation type");
   4434 	  bfd_set_error (bfd_error_bad_value);
   4435 	  goto error_return;
   4436 	}
   4437 
   4438       /* Get the target section referred to by the reloc.  */
   4439       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   4440 	{
   4441 	  /* A local symbol.  */
   4442 	  Elf_Internal_Sym *isym;
   4443 
   4444 	  /* Read this BFD's local symbols if we haven't done so already.  */
   4445 	  if (isymbuf == NULL)
   4446 	    {
   4447 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   4448 	      if (isymbuf == NULL)
   4449 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   4450 						symtab_hdr->sh_info, 0,
   4451 						NULL, NULL, NULL);
   4452 	      if (isymbuf == NULL)
   4453 		goto error_return;
   4454 	    }
   4455 
   4456 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
   4457 	  targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4458 	}
   4459       else
   4460 	{
   4461 	  unsigned long indx;
   4462 	  struct elf_link_hash_entry *h;
   4463 
   4464 	  /* An external symbol.  */
   4465 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   4466 	  h = elf_sym_hashes (abfd)[indx];
   4467 	  BFD_ASSERT (h != NULL);
   4468 	  if (h->root.type == bfd_link_hash_defined
   4469 	      || h->root.type == bfd_link_hash_defweak)
   4470 	    targetsec = h->root.u.def.section;
   4471 	  else
   4472 	    targetsec = NULL;
   4473 	}
   4474 
   4475       bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
   4476       memset (p + 4, 0, 8);
   4477       if (targetsec != NULL)
   4478 	strncpy ((char *) p + 4, targetsec->output_section->name, 8);
   4479     }
   4480 
   4481   if (symtab_hdr->contents != (unsigned char *) isymbuf)
   4482     free (isymbuf);
   4483   if (elf_section_data (datasec)->relocs != internal_relocs)
   4484     free (internal_relocs);
   4485   return true;
   4486 
   4487  error_return:
   4488   if (symtab_hdr->contents != (unsigned char *) isymbuf)
   4489     free (isymbuf);
   4490   if (elf_section_data (datasec)->relocs != internal_relocs)
   4491     free (internal_relocs);
   4492   return false;
   4493 }
   4494 
   4495 /* Set target options.  */
   4496 
   4497 void
   4498 bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
   4499 {
   4500   struct elf_m68k_link_hash_table *htab;
   4501   bool use_neg_got_offsets_p;
   4502   bool allow_multigot_p;
   4503   bool local_gp_p;
   4504 
   4505   switch (got_handling)
   4506     {
   4507     case 0:
   4508       /* --got=single.  */
   4509       local_gp_p = false;
   4510       use_neg_got_offsets_p = false;
   4511       allow_multigot_p = false;
   4512       break;
   4513 
   4514     case 1:
   4515       /* --got=negative.  */
   4516       local_gp_p = true;
   4517       use_neg_got_offsets_p = true;
   4518       allow_multigot_p = false;
   4519       break;
   4520 
   4521     case 2:
   4522       /* --got=multigot.  */
   4523       local_gp_p = true;
   4524       use_neg_got_offsets_p = true;
   4525       allow_multigot_p = true;
   4526       break;
   4527 
   4528     default:
   4529       BFD_ASSERT (false);
   4530       return;
   4531     }
   4532 
   4533   htab = elf_m68k_hash_table (info);
   4534   if (htab != NULL)
   4535     {
   4536       htab->local_gp_p = local_gp_p;
   4537       htab->use_neg_got_offsets_p = use_neg_got_offsets_p;
   4538       htab->allow_multigot_p = allow_multigot_p;
   4539     }
   4540 }
   4541 
   4542 static enum elf_reloc_type_class
   4543 elf32_m68k_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   4544 			     const asection *rel_sec ATTRIBUTE_UNUSED,
   4545 			     const Elf_Internal_Rela *rela)
   4546 {
   4547   switch ((int) ELF32_R_TYPE (rela->r_info))
   4548     {
   4549     case R_68K_RELATIVE:
   4550       return reloc_class_relative;
   4551     case R_68K_JMP_SLOT:
   4552       return reloc_class_plt;
   4553     case R_68K_COPY:
   4554       return reloc_class_copy;
   4555     default:
   4556       return reloc_class_normal;
   4557     }
   4558 }
   4559 
   4560 /* Return address for Ith PLT stub in section PLT, for relocation REL
   4561    or (bfd_vma) -1 if it should not be included.  */
   4562 
   4563 static bfd_vma
   4564 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
   4565 		      const arelent *rel ATTRIBUTE_UNUSED)
   4566 {
   4567   return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
   4568 }
   4569 
   4570 /* Support for core dump NOTE sections.  */
   4571 
   4572 static bool
   4573 elf_m68k_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   4574 {
   4575   int offset;
   4576   size_t size;
   4577 
   4578   switch (note->descsz)
   4579     {
   4580     default:
   4581       return false;
   4582 
   4583     case 154:		/* Linux/m68k */
   4584       /* pr_cursig */
   4585       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   4586 
   4587       /* pr_pid */
   4588       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
   4589 
   4590       /* pr_reg */
   4591       offset = 70;
   4592       size = 80;
   4593 
   4594       break;
   4595     }
   4596 
   4597   /* Make a ".reg/999" section.  */
   4598   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   4599 					  size, note->descpos + offset);
   4600 }
   4601 
   4602 static bool
   4603 elf_m68k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   4604 {
   4605   switch (note->descsz)
   4606     {
   4607     default:
   4608       return false;
   4609 
   4610     case 124:		/* Linux/m68k elf_prpsinfo.  */
   4611       elf_tdata (abfd)->core->pid
   4612 	= bfd_get_32 (abfd, note->descdata + 12);
   4613       elf_tdata (abfd)->core->program
   4614 	= _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
   4615       elf_tdata (abfd)->core->command
   4616 	= _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
   4617     }
   4618 
   4619   /* Note that for some reason, a spurious space is tacked
   4620      onto the end of the args in some (at least one anyway)
   4621      implementations, so strip it off if it exists.  */
   4622   {
   4623     char *command = elf_tdata (abfd)->core->command;
   4624     int n = strlen (command);
   4625 
   4626     if (n > 0 && command[n - 1] == ' ')
   4627       command[n - 1] = '\0';
   4628   }
   4629 
   4630   return true;
   4631 }
   4632 
   4633 #define TARGET_BIG_SYM			m68k_elf32_vec
   4634 #define TARGET_BIG_NAME			"elf32-m68k"
   4635 #define ELF_MACHINE_CODE		EM_68K
   4636 #define ELF_MAXPAGESIZE			0x2000
   4637 #define elf_backend_create_dynamic_sections \
   4638 					_bfd_elf_create_dynamic_sections
   4639 #define bfd_elf32_bfd_link_hash_table_create \
   4640 					elf_m68k_link_hash_table_create
   4641 #define bfd_elf32_bfd_final_link	bfd_elf_final_link
   4642 
   4643 #define elf_backend_check_relocs	elf_m68k_check_relocs
   4644 #define elf_backend_always_size_sections \
   4645 					elf_m68k_always_size_sections
   4646 #define elf_backend_adjust_dynamic_symbol \
   4647 					elf_m68k_adjust_dynamic_symbol
   4648 #define elf_backend_size_dynamic_sections \
   4649 					elf_m68k_size_dynamic_sections
   4650 #define elf_backend_final_write_processing	elf_m68k_final_write_processing
   4651 #define elf_backend_init_index_section	_bfd_elf_init_1_index_section
   4652 #define elf_backend_relocate_section	elf_m68k_relocate_section
   4653 #define elf_backend_finish_dynamic_symbol \
   4654 					elf_m68k_finish_dynamic_symbol
   4655 #define elf_backend_finish_dynamic_sections \
   4656 					elf_m68k_finish_dynamic_sections
   4657 #define elf_backend_gc_mark_hook	elf_m68k_gc_mark_hook
   4658 #define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
   4659 #define bfd_elf32_bfd_merge_private_bfd_data \
   4660 					elf32_m68k_merge_private_bfd_data
   4661 #define bfd_elf32_bfd_set_private_flags \
   4662 					elf32_m68k_set_private_flags
   4663 #define bfd_elf32_bfd_print_private_bfd_data \
   4664 					elf32_m68k_print_private_bfd_data
   4665 #define elf_backend_reloc_type_class	elf32_m68k_reloc_type_class
   4666 #define elf_backend_plt_sym_val		elf_m68k_plt_sym_val
   4667 #define elf_backend_object_p		elf32_m68k_object_p
   4668 #define elf_backend_grok_prstatus	elf_m68k_grok_prstatus
   4669 #define elf_backend_grok_psinfo		elf_m68k_grok_psinfo
   4670 
   4671 #define elf_backend_can_gc_sections 1
   4672 #define elf_backend_can_refcount 1
   4673 #define elf_backend_want_got_plt 1
   4674 #define elf_backend_plt_readonly 1
   4675 #define elf_backend_want_plt_sym 0
   4676 #define elf_backend_got_header_size	12
   4677 #define elf_backend_rela_normal		1
   4678 #define elf_backend_dtrel_excludes_plt	1
   4679 
   4680 #define elf_backend_linux_prpsinfo32_ugid16	true
   4681 
   4682 #include "elf32-target.h"
   4683