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