OSDN Git Service

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