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