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