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