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