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