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