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