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