[PATCH 2/57][Arm][GAS] Add support for MVE instructions: vpst, vadd, vsub and vabd
[binutils-gdb.git] / bfd / elf32-m68k.c
blobe8d9ed5ce1451e5b541891a7d11bc8dfa1d94f31
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright (C) 1993-2019 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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"
29 static bfd_boolean
30 elf_m68k_discard_copies (struct elf_link_hash_entry *, void *);
32 static reloc_howto_type howto_table[] =
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),
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
344 static bfd_boolean
345 rtype_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
347 unsigned int indx = ELF32_R_TYPE (dst->r_info);
349 if (indx >= (unsigned int) R_68K_max)
351 /* xgettext:c-format */
352 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
353 abfd, indx);
354 bfd_set_error (bfd_error_bad_value);
355 return FALSE;
357 cache_ptr->howto = &howto_table[indx];
358 return TRUE;
361 #define elf_info_to_howto rtype_to_howto
363 static const struct
365 bfd_reloc_code_real_type bfd_val;
366 int elf_val;
368 reloc_map[] =
370 { BFD_RELOC_NONE, R_68K_NONE },
371 { BFD_RELOC_32, R_68K_32 },
372 { BFD_RELOC_16, R_68K_16 },
373 { BFD_RELOC_8, R_68K_8 },
374 { BFD_RELOC_32_PCREL, R_68K_PC32 },
375 { BFD_RELOC_16_PCREL, R_68K_PC16 },
376 { BFD_RELOC_8_PCREL, R_68K_PC8 },
377 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
378 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
379 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
380 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
381 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
382 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
383 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
384 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
385 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
386 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
387 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
388 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
389 { BFD_RELOC_NONE, R_68K_COPY },
390 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
391 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
392 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
393 { BFD_RELOC_CTOR, R_68K_32 },
394 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
395 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
396 { BFD_RELOC_68K_TLS_GD32, R_68K_TLS_GD32 },
397 { BFD_RELOC_68K_TLS_GD16, R_68K_TLS_GD16 },
398 { BFD_RELOC_68K_TLS_GD8, R_68K_TLS_GD8 },
399 { BFD_RELOC_68K_TLS_LDM32, R_68K_TLS_LDM32 },
400 { BFD_RELOC_68K_TLS_LDM16, R_68K_TLS_LDM16 },
401 { BFD_RELOC_68K_TLS_LDM8, R_68K_TLS_LDM8 },
402 { BFD_RELOC_68K_TLS_LDO32, R_68K_TLS_LDO32 },
403 { BFD_RELOC_68K_TLS_LDO16, R_68K_TLS_LDO16 },
404 { BFD_RELOC_68K_TLS_LDO8, R_68K_TLS_LDO8 },
405 { BFD_RELOC_68K_TLS_IE32, R_68K_TLS_IE32 },
406 { BFD_RELOC_68K_TLS_IE16, R_68K_TLS_IE16 },
407 { BFD_RELOC_68K_TLS_IE8, R_68K_TLS_IE8 },
408 { BFD_RELOC_68K_TLS_LE32, R_68K_TLS_LE32 },
409 { BFD_RELOC_68K_TLS_LE16, R_68K_TLS_LE16 },
410 { BFD_RELOC_68K_TLS_LE8, R_68K_TLS_LE8 },
413 static reloc_howto_type *
414 reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
415 bfd_reloc_code_real_type code)
417 unsigned int i;
418 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
420 if (reloc_map[i].bfd_val == code)
421 return &howto_table[reloc_map[i].elf_val];
423 return 0;
426 static reloc_howto_type *
427 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
429 unsigned int i;
431 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
432 if (howto_table[i].name != NULL
433 && strcasecmp (howto_table[i].name, r_name) == 0)
434 return &howto_table[i];
436 return NULL;
439 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
440 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
441 #define ELF_ARCH bfd_arch_m68k
442 #define ELF_TARGET_ID M68K_ELF_DATA
444 /* Functions for the m68k ELF linker. */
446 /* The name of the dynamic interpreter. This is put in the .interp
447 section. */
449 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
451 /* Describes one of the various PLT styles. */
453 struct elf_m68k_plt_info
455 /* The size of each PLT entry. */
456 bfd_vma size;
458 /* The template for the first PLT entry. */
459 const bfd_byte *plt0_entry;
461 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
462 The comments by each member indicate the value that the relocation
463 is against. */
464 struct {
465 unsigned int got4; /* .got + 4 */
466 unsigned int got8; /* .got + 8 */
467 } plt0_relocs;
469 /* The template for a symbol's PLT entry. */
470 const bfd_byte *symbol_entry;
472 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
473 The comments by each member indicate the value that the relocation
474 is against. */
475 struct {
476 unsigned int got; /* the symbol's .got.plt entry */
477 unsigned int plt; /* .plt */
478 } symbol_relocs;
480 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
481 The stub starts with "move.l #relocoffset,%d0". */
482 bfd_vma symbol_resolve_entry;
485 /* The size in bytes of an entry in the procedure linkage table. */
487 #define PLT_ENTRY_SIZE 20
489 /* The first entry in a procedure linkage table looks like this. See
490 the SVR4 ABI m68k supplement to see how this works. */
492 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
494 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
495 0, 0, 0, 2, /* + (.got + 4) - . */
496 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
497 0, 0, 0, 2, /* + (.got + 8) - . */
498 0, 0, 0, 0 /* pad out to 20 bytes. */
501 /* Subsequent entries in a procedure linkage table look like this. */
503 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
505 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
506 0, 0, 0, 2, /* + (.got.plt entry) - . */
507 0x2f, 0x3c, /* move.l #offset,-(%sp) */
508 0, 0, 0, 0, /* + reloc index */
509 0x60, 0xff, /* bra.l .plt */
510 0, 0, 0, 0 /* + .plt - . */
513 static const struct elf_m68k_plt_info elf_m68k_plt_info =
515 PLT_ENTRY_SIZE,
516 elf_m68k_plt0_entry, { 4, 12 },
517 elf_m68k_plt_entry, { 4, 16 }, 8
520 #define ISAB_PLT_ENTRY_SIZE 24
522 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
524 0x20, 0x3c, /* move.l #offset,%d0 */
525 0, 0, 0, 0, /* + (.got + 4) - . */
526 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
527 0x20, 0x3c, /* move.l #offset,%d0 */
528 0, 0, 0, 0, /* + (.got + 8) - . */
529 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
530 0x4e, 0xd0, /* jmp (%a0) */
531 0x4e, 0x71 /* nop */
534 /* Subsequent entries in a procedure linkage table look like this. */
536 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
538 0x20, 0x3c, /* move.l #offset,%d0 */
539 0, 0, 0, 0, /* + (.got.plt entry) - . */
540 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
541 0x4e, 0xd0, /* jmp (%a0) */
542 0x2f, 0x3c, /* move.l #offset,-(%sp) */
543 0, 0, 0, 0, /* + reloc index */
544 0x60, 0xff, /* bra.l .plt */
545 0, 0, 0, 0 /* + .plt - . */
548 static const struct elf_m68k_plt_info elf_isab_plt_info =
550 ISAB_PLT_ENTRY_SIZE,
551 elf_isab_plt0_entry, { 2, 12 },
552 elf_isab_plt_entry, { 2, 20 }, 12
555 #define ISAC_PLT_ENTRY_SIZE 24
557 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
559 0x20, 0x3c, /* move.l #offset,%d0 */
560 0, 0, 0, 0, /* replaced with .got + 4 - . */
561 0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
562 0x20, 0x3c, /* move.l #offset,%d0 */
563 0, 0, 0, 0, /* replaced with .got + 8 - . */
564 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
565 0x4e, 0xd0, /* jmp (%a0) */
566 0x4e, 0x71 /* nop */
569 /* Subsequent entries in a procedure linkage table look like this. */
571 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
573 0x20, 0x3c, /* move.l #offset,%d0 */
574 0, 0, 0, 0, /* replaced with (.got entry) - . */
575 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
576 0x4e, 0xd0, /* jmp (%a0) */
577 0x2f, 0x3c, /* move.l #offset,-(%sp) */
578 0, 0, 0, 0, /* replaced with offset into relocation table */
579 0x61, 0xff, /* bsr.l .plt */
580 0, 0, 0, 0 /* replaced with .plt - . */
583 static const struct elf_m68k_plt_info elf_isac_plt_info =
585 ISAC_PLT_ENTRY_SIZE,
586 elf_isac_plt0_entry, { 2, 12},
587 elf_isac_plt_entry, { 2, 20 }, 12
590 #define CPU32_PLT_ENTRY_SIZE 24
591 /* Procedure linkage table entries for the cpu32 */
592 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
594 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
595 0, 0, 0, 2, /* + (.got + 4) - . */
596 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
597 0, 0, 0, 2, /* + (.got + 8) - . */
598 0x4e, 0xd1, /* jmp %a1@ */
599 0, 0, 0, 0, /* pad out to 24 bytes. */
600 0, 0
603 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
605 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
606 0, 0, 0, 2, /* + (.got.plt entry) - . */
607 0x4e, 0xd1, /* jmp %a1@ */
608 0x2f, 0x3c, /* move.l #offset,-(%sp) */
609 0, 0, 0, 0, /* + reloc index */
610 0x60, 0xff, /* bra.l .plt */
611 0, 0, 0, 0, /* + .plt - . */
612 0, 0
615 static const struct elf_m68k_plt_info elf_cpu32_plt_info =
617 CPU32_PLT_ENTRY_SIZE,
618 elf_cpu32_plt0_entry, { 4, 12 },
619 elf_cpu32_plt_entry, { 4, 18 }, 10
622 /* The m68k linker needs to keep track of the number of relocs that it
623 decides to copy in check_relocs for each symbol. This is so that it
624 can discard PC relative relocs if it doesn't need them when linking
625 with -Bsymbolic. We store the information in a field extending the
626 regular ELF linker hash table. */
628 /* This structure keeps track of the number of PC relative relocs we have
629 copied for a given symbol. */
631 struct elf_m68k_pcrel_relocs_copied
633 /* Next section. */
634 struct elf_m68k_pcrel_relocs_copied *next;
635 /* A section in dynobj. */
636 asection *section;
637 /* Number of relocs copied in this section. */
638 bfd_size_type count;
641 /* Forward declaration. */
642 struct elf_m68k_got_entry;
644 /* m68k ELF linker hash entry. */
646 struct elf_m68k_link_hash_entry
648 struct elf_link_hash_entry root;
650 /* Number of PC relative relocs copied for this symbol. */
651 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
653 /* Key to got_entries. */
654 unsigned long got_entry_key;
656 /* List of GOT entries for this symbol. This list is build during
657 offset finalization and is used within elf_m68k_finish_dynamic_symbol
658 to traverse all GOT entries for a particular symbol.
660 ??? We could've used root.got.glist field instead, but having
661 a separate field is cleaner. */
662 struct elf_m68k_got_entry *glist;
665 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
667 /* Key part of GOT entry in hashtable. */
668 struct elf_m68k_got_entry_key
670 /* BFD in which this symbol was defined. NULL for global symbols. */
671 const bfd *bfd;
673 /* Symbol index. Either local symbol index or h->got_entry_key. */
674 unsigned long symndx;
676 /* Type is one of R_68K_GOT{8, 16, 32}O, R_68K_TLS_GD{8, 16, 32},
677 R_68K_TLS_LDM{8, 16, 32} or R_68K_TLS_IE{8, 16, 32}.
679 From perspective of hashtable key, only elf_m68k_got_reloc_type (type)
680 matters. That is, we distinguish between, say, R_68K_GOT16O
681 and R_68K_GOT32O when allocating offsets, but they are considered to be
682 the same when searching got->entries. */
683 enum elf_m68k_reloc_type type;
686 /* Size of the GOT offset suitable for relocation. */
687 enum elf_m68k_got_offset_size { R_8, R_16, R_32, R_LAST };
689 /* Entry of the GOT. */
690 struct elf_m68k_got_entry
692 /* GOT entries are put into a got->entries hashtable. This is the key. */
693 struct elf_m68k_got_entry_key key_;
695 /* GOT entry data. We need s1 before offset finalization and s2 after. */
696 union
698 struct
700 /* Number of times this entry is referenced. */
701 bfd_vma refcount;
702 } s1;
704 struct
706 /* Offset from the start of .got section. To calculate offset relative
707 to GOT pointer one should subtract got->offset from this value. */
708 bfd_vma offset;
710 /* Pointer to the next GOT entry for this global symbol.
711 Symbols have at most one entry in one GOT, but might
712 have entries in more than one GOT.
713 Root of this list is h->glist.
714 NULL for local symbols. */
715 struct elf_m68k_got_entry *next;
716 } s2;
717 } u;
720 /* Return representative type for relocation R_TYPE.
721 This is used to avoid enumerating many relocations in comparisons,
722 switches etc. */
724 static enum elf_m68k_reloc_type
725 elf_m68k_reloc_got_type (enum elf_m68k_reloc_type r_type)
727 switch (r_type)
729 /* In most cases R_68K_GOTx relocations require the very same
730 handling as R_68K_GOT32O relocation. In cases when we need
731 to distinguish between the two, we use explicitly compare against
732 r_type. */
733 case R_68K_GOT32:
734 case R_68K_GOT16:
735 case R_68K_GOT8:
736 case R_68K_GOT32O:
737 case R_68K_GOT16O:
738 case R_68K_GOT8O:
739 return R_68K_GOT32O;
741 case R_68K_TLS_GD32:
742 case R_68K_TLS_GD16:
743 case R_68K_TLS_GD8:
744 return R_68K_TLS_GD32;
746 case R_68K_TLS_LDM32:
747 case R_68K_TLS_LDM16:
748 case R_68K_TLS_LDM8:
749 return R_68K_TLS_LDM32;
751 case R_68K_TLS_IE32:
752 case R_68K_TLS_IE16:
753 case R_68K_TLS_IE8:
754 return R_68K_TLS_IE32;
756 default:
757 BFD_ASSERT (FALSE);
758 return 0;
762 /* Return size of the GOT entry offset for relocation R_TYPE. */
764 static enum elf_m68k_got_offset_size
765 elf_m68k_reloc_got_offset_size (enum elf_m68k_reloc_type r_type)
767 switch (r_type)
769 case R_68K_GOT32: case R_68K_GOT16: case R_68K_GOT8:
770 case R_68K_GOT32O: case R_68K_TLS_GD32: case R_68K_TLS_LDM32:
771 case R_68K_TLS_IE32:
772 return R_32;
774 case R_68K_GOT16O: case R_68K_TLS_GD16: case R_68K_TLS_LDM16:
775 case R_68K_TLS_IE16:
776 return R_16;
778 case R_68K_GOT8O: case R_68K_TLS_GD8: case R_68K_TLS_LDM8:
779 case R_68K_TLS_IE8:
780 return R_8;
782 default:
783 BFD_ASSERT (FALSE);
784 return 0;
788 /* Return number of GOT entries we need to allocate in GOT for
789 relocation R_TYPE. */
791 static bfd_vma
792 elf_m68k_reloc_got_n_slots (enum elf_m68k_reloc_type r_type)
794 switch (elf_m68k_reloc_got_type (r_type))
796 case R_68K_GOT32O:
797 case R_68K_TLS_IE32:
798 return 1;
800 case R_68K_TLS_GD32:
801 case R_68K_TLS_LDM32:
802 return 2;
804 default:
805 BFD_ASSERT (FALSE);
806 return 0;
810 /* Return TRUE if relocation R_TYPE is a TLS one. */
812 static bfd_boolean
813 elf_m68k_reloc_tls_p (enum elf_m68k_reloc_type r_type)
815 switch (r_type)
817 case R_68K_TLS_GD32: case R_68K_TLS_GD16: case R_68K_TLS_GD8:
818 case R_68K_TLS_LDM32: case R_68K_TLS_LDM16: case R_68K_TLS_LDM8:
819 case R_68K_TLS_LDO32: case R_68K_TLS_LDO16: case R_68K_TLS_LDO8:
820 case R_68K_TLS_IE32: case R_68K_TLS_IE16: case R_68K_TLS_IE8:
821 case R_68K_TLS_LE32: case R_68K_TLS_LE16: case R_68K_TLS_LE8:
822 case R_68K_TLS_DTPMOD32: case R_68K_TLS_DTPREL32: case R_68K_TLS_TPREL32:
823 return TRUE;
825 default:
826 return FALSE;
830 /* Data structure representing a single GOT. */
831 struct elf_m68k_got
833 /* Hashtable of 'struct elf_m68k_got_entry's.
834 Starting size of this table is the maximum number of
835 R_68K_GOT8O entries. */
836 htab_t entries;
838 /* Number of R_x slots in this GOT. Some (e.g., TLS) entries require
839 several GOT slots.
841 n_slots[R_8] is the count of R_8 slots in this GOT.
842 n_slots[R_16] is the cumulative count of R_8 and R_16 slots
843 in this GOT.
844 n_slots[R_32] is the cumulative count of R_8, R_16 and R_32 slots
845 in this GOT. This is the total number of slots. */
846 bfd_vma n_slots[R_LAST];
848 /* Number of local (entry->key_.h == NULL) slots in this GOT.
849 This is only used to properly calculate size of .rela.got section;
850 see elf_m68k_partition_multi_got. */
851 bfd_vma local_n_slots;
853 /* Offset of this GOT relative to beginning of .got section. */
854 bfd_vma offset;
857 /* BFD and its GOT. This is an entry in multi_got->bfd2got hashtable. */
858 struct elf_m68k_bfd2got_entry
860 /* BFD. */
861 const bfd *bfd;
863 /* Assigned GOT. Before partitioning multi-GOT each BFD has its own
864 GOT structure. After partitioning several BFD's might [and often do]
865 share a single GOT. */
866 struct elf_m68k_got *got;
869 /* The main data structure holding all the pieces. */
870 struct elf_m68k_multi_got
872 /* Hashtable mapping each BFD to its GOT. If a BFD doesn't have an entry
873 here, then it doesn't need a GOT (this includes the case of a BFD
874 having an empty GOT).
876 ??? This hashtable can be replaced by an array indexed by bfd->id. */
877 htab_t bfd2got;
879 /* Next symndx to assign a global symbol.
880 h->got_entry_key is initialized from this counter. */
881 unsigned long global_symndx;
884 /* m68k ELF linker hash table. */
886 struct elf_m68k_link_hash_table
888 struct elf_link_hash_table root;
890 /* Small local sym cache. */
891 struct sym_cache sym_cache;
893 /* The PLT format used by this link, or NULL if the format has not
894 yet been chosen. */
895 const struct elf_m68k_plt_info *plt_info;
897 /* True, if GP is loaded within each function which uses it.
898 Set to TRUE when GOT negative offsets or multi-GOT is enabled. */
899 bfd_boolean local_gp_p;
901 /* Switch controlling use of negative offsets to double the size of GOTs. */
902 bfd_boolean use_neg_got_offsets_p;
904 /* Switch controlling generation of multiple GOTs. */
905 bfd_boolean allow_multigot_p;
907 /* Multi-GOT data structure. */
908 struct elf_m68k_multi_got multi_got_;
911 /* Get the m68k ELF linker hash table from a link_info structure. */
913 #define elf_m68k_hash_table(p) \
914 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
915 == M68K_ELF_DATA ? ((struct elf_m68k_link_hash_table *) ((p)->hash)) : NULL)
917 /* Shortcut to multi-GOT data. */
918 #define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
920 /* Create an entry in an m68k ELF linker hash table. */
922 static struct bfd_hash_entry *
923 elf_m68k_link_hash_newfunc (struct bfd_hash_entry *entry,
924 struct bfd_hash_table *table,
925 const char *string)
927 struct bfd_hash_entry *ret = entry;
929 /* Allocate the structure if it has not already been allocated by a
930 subclass. */
931 if (ret == NULL)
932 ret = bfd_hash_allocate (table,
933 sizeof (struct elf_m68k_link_hash_entry));
934 if (ret == NULL)
935 return ret;
937 /* Call the allocation method of the superclass. */
938 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
939 if (ret != NULL)
941 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
942 elf_m68k_hash_entry (ret)->got_entry_key = 0;
943 elf_m68k_hash_entry (ret)->glist = NULL;
946 return ret;
949 /* Destroy an m68k ELF linker hash table. */
951 static void
952 elf_m68k_link_hash_table_free (bfd *obfd)
954 struct elf_m68k_link_hash_table *htab;
956 htab = (struct elf_m68k_link_hash_table *) obfd->link.hash;
958 if (htab->multi_got_.bfd2got != NULL)
960 htab_delete (htab->multi_got_.bfd2got);
961 htab->multi_got_.bfd2got = NULL;
963 _bfd_elf_link_hash_table_free (obfd);
966 /* Create an m68k ELF linker hash table. */
968 static struct bfd_link_hash_table *
969 elf_m68k_link_hash_table_create (bfd *abfd)
971 struct elf_m68k_link_hash_table *ret;
972 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
974 ret = (struct elf_m68k_link_hash_table *) bfd_zmalloc (amt);
975 if (ret == (struct elf_m68k_link_hash_table *) NULL)
976 return NULL;
978 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
979 elf_m68k_link_hash_newfunc,
980 sizeof (struct elf_m68k_link_hash_entry),
981 M68K_ELF_DATA))
983 free (ret);
984 return NULL;
986 ret->root.root.hash_table_free = elf_m68k_link_hash_table_free;
988 ret->multi_got_.global_symndx = 1;
990 return &ret->root.root;
993 /* Set the right machine number. */
995 static bfd_boolean
996 elf32_m68k_object_p (bfd *abfd)
998 unsigned int mach = 0;
999 unsigned features = 0;
1000 flagword eflags = elf_elfheader (abfd)->e_flags;
1002 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1003 features |= m68000;
1004 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1005 features |= cpu32;
1006 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1007 features |= fido_a;
1008 else
1010 switch (eflags & EF_M68K_CF_ISA_MASK)
1012 case EF_M68K_CF_ISA_A_NODIV:
1013 features |= mcfisa_a;
1014 break;
1015 case EF_M68K_CF_ISA_A:
1016 features |= mcfisa_a|mcfhwdiv;
1017 break;
1018 case EF_M68K_CF_ISA_A_PLUS:
1019 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
1020 break;
1021 case EF_M68K_CF_ISA_B_NOUSP:
1022 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
1023 break;
1024 case EF_M68K_CF_ISA_B:
1025 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
1026 break;
1027 case EF_M68K_CF_ISA_C:
1028 features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
1029 break;
1030 case EF_M68K_CF_ISA_C_NODIV:
1031 features |= mcfisa_a|mcfisa_c|mcfusp;
1032 break;
1034 switch (eflags & EF_M68K_CF_MAC_MASK)
1036 case EF_M68K_CF_MAC:
1037 features |= mcfmac;
1038 break;
1039 case EF_M68K_CF_EMAC:
1040 features |= mcfemac;
1041 break;
1043 if (eflags & EF_M68K_CF_FLOAT)
1044 features |= cfloat;
1047 mach = bfd_m68k_features_to_mach (features);
1048 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
1050 return TRUE;
1053 /* Somewhat reverse of elf32_m68k_object_p, this sets the e_flag
1054 field based on the machine number. */
1056 static void
1057 elf_m68k_final_write_processing (bfd *abfd,
1058 bfd_boolean linker ATTRIBUTE_UNUSED)
1060 int mach = bfd_get_mach (abfd);
1061 unsigned long e_flags = elf_elfheader (abfd)->e_flags;
1063 if (!e_flags)
1065 unsigned int arch_mask;
1067 arch_mask = bfd_m68k_mach_to_features (mach);
1069 if (arch_mask & m68000)
1070 e_flags = EF_M68K_M68000;
1071 else if (arch_mask & cpu32)
1072 e_flags = EF_M68K_CPU32;
1073 else if (arch_mask & fido_a)
1074 e_flags = EF_M68K_FIDO;
1075 else
1077 switch (arch_mask
1078 & (mcfisa_a | mcfisa_aa | mcfisa_b | mcfisa_c | mcfhwdiv | mcfusp))
1080 case mcfisa_a:
1081 e_flags |= EF_M68K_CF_ISA_A_NODIV;
1082 break;
1083 case mcfisa_a | mcfhwdiv:
1084 e_flags |= EF_M68K_CF_ISA_A;
1085 break;
1086 case mcfisa_a | mcfisa_aa | mcfhwdiv | mcfusp:
1087 e_flags |= EF_M68K_CF_ISA_A_PLUS;
1088 break;
1089 case mcfisa_a | mcfisa_b | mcfhwdiv:
1090 e_flags |= EF_M68K_CF_ISA_B_NOUSP;
1091 break;
1092 case mcfisa_a | mcfisa_b | mcfhwdiv | mcfusp:
1093 e_flags |= EF_M68K_CF_ISA_B;
1094 break;
1095 case mcfisa_a | mcfisa_c | mcfhwdiv | mcfusp:
1096 e_flags |= EF_M68K_CF_ISA_C;
1097 break;
1098 case mcfisa_a | mcfisa_c | mcfusp:
1099 e_flags |= EF_M68K_CF_ISA_C_NODIV;
1100 break;
1102 if (arch_mask & mcfmac)
1103 e_flags |= EF_M68K_CF_MAC;
1104 else if (arch_mask & mcfemac)
1105 e_flags |= EF_M68K_CF_EMAC;
1106 if (arch_mask & cfloat)
1107 e_flags |= EF_M68K_CF_FLOAT | EF_M68K_CFV4E;
1109 elf_elfheader (abfd)->e_flags = e_flags;
1113 /* Keep m68k-specific flags in the ELF header. */
1115 static bfd_boolean
1116 elf32_m68k_set_private_flags (bfd *abfd, flagword flags)
1118 elf_elfheader (abfd)->e_flags = flags;
1119 elf_flags_init (abfd) = TRUE;
1120 return TRUE;
1123 /* Merge backend specific data from an object file to the output
1124 object file when linking. */
1125 static bfd_boolean
1126 elf32_m68k_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
1128 bfd *obfd = info->output_bfd;
1129 flagword out_flags;
1130 flagword in_flags;
1131 flagword out_isa;
1132 flagword in_isa;
1133 const bfd_arch_info_type *arch_info;
1135 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1136 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1137 /* PR 24523: For non-ELF files do not try to merge any private
1138 data, but also do not prevent the link from succeeding. */
1139 return TRUE;
1141 /* Get the merged machine. This checks for incompatibility between
1142 Coldfire & non-Coldfire flags, incompability between different
1143 Coldfire ISAs, and incompability between different MAC types. */
1144 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
1145 if (!arch_info)
1146 return FALSE;
1148 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
1150 in_flags = elf_elfheader (ibfd)->e_flags;
1151 if (!elf_flags_init (obfd))
1153 elf_flags_init (obfd) = TRUE;
1154 out_flags = in_flags;
1156 else
1158 out_flags = elf_elfheader (obfd)->e_flags;
1159 unsigned int variant_mask;
1161 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1162 variant_mask = 0;
1163 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1164 variant_mask = 0;
1165 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1166 variant_mask = 0;
1167 else
1168 variant_mask = EF_M68K_CF_ISA_MASK;
1170 in_isa = (in_flags & variant_mask);
1171 out_isa = (out_flags & variant_mask);
1172 if (in_isa > out_isa)
1173 out_flags ^= in_isa ^ out_isa;
1174 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
1175 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1176 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
1177 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
1178 out_flags = EF_M68K_FIDO;
1179 else
1180 out_flags |= in_flags ^ in_isa;
1182 elf_elfheader (obfd)->e_flags = out_flags;
1184 return TRUE;
1187 /* Display the flags field. */
1189 static bfd_boolean
1190 elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
1192 FILE *file = (FILE *) ptr;
1193 flagword eflags = elf_elfheader (abfd)->e_flags;
1195 BFD_ASSERT (abfd != NULL && ptr != NULL);
1197 /* Print normal ELF private data. */
1198 _bfd_elf_print_private_bfd_data (abfd, ptr);
1200 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
1202 /* xgettext:c-format */
1203 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1205 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1206 fprintf (file, " [m68000]");
1207 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1208 fprintf (file, " [cpu32]");
1209 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1210 fprintf (file, " [fido]");
1211 else
1213 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
1214 fprintf (file, " [cfv4e]");
1216 if (eflags & EF_M68K_CF_ISA_MASK)
1218 char const *isa = _("unknown");
1219 char const *mac = _("unknown");
1220 char const *additional = "";
1222 switch (eflags & EF_M68K_CF_ISA_MASK)
1224 case EF_M68K_CF_ISA_A_NODIV:
1225 isa = "A";
1226 additional = " [nodiv]";
1227 break;
1228 case EF_M68K_CF_ISA_A:
1229 isa = "A";
1230 break;
1231 case EF_M68K_CF_ISA_A_PLUS:
1232 isa = "A+";
1233 break;
1234 case EF_M68K_CF_ISA_B_NOUSP:
1235 isa = "B";
1236 additional = " [nousp]";
1237 break;
1238 case EF_M68K_CF_ISA_B:
1239 isa = "B";
1240 break;
1241 case EF_M68K_CF_ISA_C:
1242 isa = "C";
1243 break;
1244 case EF_M68K_CF_ISA_C_NODIV:
1245 isa = "C";
1246 additional = " [nodiv]";
1247 break;
1249 fprintf (file, " [isa %s]%s", isa, additional);
1251 if (eflags & EF_M68K_CF_FLOAT)
1252 fprintf (file, " [float]");
1254 switch (eflags & EF_M68K_CF_MAC_MASK)
1256 case 0:
1257 mac = NULL;
1258 break;
1259 case EF_M68K_CF_MAC:
1260 mac = "mac";
1261 break;
1262 case EF_M68K_CF_EMAC:
1263 mac = "emac";
1264 break;
1265 case EF_M68K_CF_EMAC_B:
1266 mac = "emac_b";
1267 break;
1269 if (mac)
1270 fprintf (file, " [%s]", mac);
1274 fputc ('\n', file);
1276 return TRUE;
1279 /* Multi-GOT support implementation design:
1281 Multi-GOT starts in check_relocs hook. There we scan all
1282 relocations of a BFD and build a local GOT (struct elf_m68k_got)
1283 for it. If a single BFD appears to require too many GOT slots with
1284 R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
1285 to user.
1286 After check_relocs has been invoked for each input BFD, we have
1287 constructed a GOT for each input BFD.
1289 To minimize total number of GOTs required for a particular output BFD
1290 (as some environments support only 1 GOT per output object) we try
1291 to merge some of the GOTs to share an offset space. Ideally [and in most
1292 cases] we end up with a single GOT. In cases when there are too many
1293 restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
1294 several GOTs, assuming the environment can handle them.
1296 Partitioning is done in elf_m68k_partition_multi_got. We start with
1297 an empty GOT and traverse bfd2got hashtable putting got_entries from
1298 local GOTs to the new 'big' one. We do that by constructing an
1299 intermediate GOT holding all the entries the local GOT has and the big
1300 GOT lacks. Then we check if there is room in the big GOT to accomodate
1301 all the entries from diff. On success we add those entries to the big
1302 GOT; on failure we start the new 'big' GOT and retry the adding of
1303 entries from the local GOT. Note that this retry will always succeed as
1304 each local GOT doesn't overflow the limits. After partitioning we
1305 end up with each bfd assigned one of the big GOTs. GOT entries in the
1306 big GOTs are initialized with GOT offsets. Note that big GOTs are
1307 positioned consequently in program space and represent a single huge GOT
1308 to the outside world.
1310 After that we get to elf_m68k_relocate_section. There we
1311 adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
1312 relocations to refer to appropriate [assigned to current input_bfd]
1313 big GOT.
1315 Notes:
1317 GOT entry type: We have several types of GOT entries.
1318 * R_8 type is used in entries for symbols that have at least one
1319 R_68K_GOT8O or R_68K_TLS_*8 relocation. We can have at most 0x40
1320 such entries in one GOT.
1321 * R_16 type is used in entries for symbols that have at least one
1322 R_68K_GOT16O or R_68K_TLS_*16 relocation and no R_8 relocations.
1323 We can have at most 0x4000 such entries in one GOT.
1324 * R_32 type is used in all other cases. We can have as many
1325 such entries in one GOT as we'd like.
1326 When counting relocations we have to include the count of the smaller
1327 ranged relocations in the counts of the larger ranged ones in order
1328 to correctly detect overflow.
1330 Sorting the GOT: In each GOT starting offsets are assigned to
1331 R_8 entries, which are followed by R_16 entries, and
1332 R_32 entries go at the end. See finalize_got_offsets for details.
1334 Negative GOT offsets: To double usable offset range of GOTs we use
1335 negative offsets. As we assign entries with GOT offsets relative to
1336 start of .got section, the offset values are positive. They become
1337 negative only in relocate_section where got->offset value is
1338 subtracted from them.
1340 3 special GOT entries: There are 3 special GOT entries used internally
1341 by loader. These entries happen to be placed to .got.plt section,
1342 so we don't do anything about them in multi-GOT support.
1344 Memory management: All data except for hashtables
1345 multi_got->bfd2got and got->entries are allocated on
1346 elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
1347 to most functions), so we don't need to care to free them. At the
1348 moment of allocation hashtables are being linked into main data
1349 structure (multi_got), all pieces of which are reachable from
1350 elf_m68k_multi_got (info). We deallocate them in
1351 elf_m68k_link_hash_table_free. */
1353 /* Initialize GOT. */
1355 static void
1356 elf_m68k_init_got (struct elf_m68k_got *got)
1358 got->entries = NULL;
1359 got->n_slots[R_8] = 0;
1360 got->n_slots[R_16] = 0;
1361 got->n_slots[R_32] = 0;
1362 got->local_n_slots = 0;
1363 got->offset = (bfd_vma) -1;
1366 /* Destruct GOT. */
1368 static void
1369 elf_m68k_clear_got (struct elf_m68k_got *got)
1371 if (got->entries != NULL)
1373 htab_delete (got->entries);
1374 got->entries = NULL;
1378 /* Create and empty GOT structure. INFO is the context where memory
1379 should be allocated. */
1381 static struct elf_m68k_got *
1382 elf_m68k_create_empty_got (struct bfd_link_info *info)
1384 struct elf_m68k_got *got;
1386 got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
1387 if (got == NULL)
1388 return NULL;
1390 elf_m68k_init_got (got);
1392 return got;
1395 /* Initialize KEY. */
1397 static void
1398 elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
1399 struct elf_link_hash_entry *h,
1400 const bfd *abfd, unsigned long symndx,
1401 enum elf_m68k_reloc_type reloc_type)
1403 if (elf_m68k_reloc_got_type (reloc_type) == R_68K_TLS_LDM32)
1404 /* All TLS_LDM relocations share a single GOT entry. */
1406 key->bfd = NULL;
1407 key->symndx = 0;
1409 else if (h != NULL)
1410 /* Global symbols are identified with their got_entry_key. */
1412 key->bfd = NULL;
1413 key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
1414 BFD_ASSERT (key->symndx != 0);
1416 else
1417 /* Local symbols are identified by BFD they appear in and symndx. */
1419 key->bfd = abfd;
1420 key->symndx = symndx;
1423 key->type = reloc_type;
1426 /* Calculate hash of got_entry.
1427 ??? Is it good? */
1429 static hashval_t
1430 elf_m68k_got_entry_hash (const void *_entry)
1432 const struct elf_m68k_got_entry_key *key;
1434 key = &((const struct elf_m68k_got_entry *) _entry)->key_;
1436 return (key->symndx
1437 + (key->bfd != NULL ? (int) key->bfd->id : -1)
1438 + elf_m68k_reloc_got_type (key->type));
1441 /* Check if two got entries are equal. */
1443 static int
1444 elf_m68k_got_entry_eq (const void *_entry1, const void *_entry2)
1446 const struct elf_m68k_got_entry_key *key1;
1447 const struct elf_m68k_got_entry_key *key2;
1449 key1 = &((const struct elf_m68k_got_entry *) _entry1)->key_;
1450 key2 = &((const struct elf_m68k_got_entry *) _entry2)->key_;
1452 return (key1->bfd == key2->bfd
1453 && key1->symndx == key2->symndx
1454 && (elf_m68k_reloc_got_type (key1->type)
1455 == elf_m68k_reloc_got_type (key2->type)));
1458 /* When using negative offsets, we allocate one extra R_8, one extra R_16
1459 and one extra R_32 slots to simplify handling of 2-slot entries during
1460 offset allocation -- hence -1 for R_8 slots and -2 for R_16 slots. */
1462 /* Maximal number of R_8 slots in a single GOT. */
1463 #define ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT(INFO) \
1464 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1465 ? (0x40 - 1) \
1466 : 0x20)
1468 /* Maximal number of R_8 and R_16 slots in a single GOT. */
1469 #define ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT(INFO) \
1470 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1471 ? (0x4000 - 2) \
1472 : 0x2000)
1474 /* SEARCH - simply search the hashtable, don't insert new entries or fail when
1475 the entry cannot be found.
1476 FIND_OR_CREATE - search for an existing entry, but create new if there's
1477 no such.
1478 MUST_FIND - search for an existing entry and assert that it exist.
1479 MUST_CREATE - assert that there's no such entry and create new one. */
1480 enum elf_m68k_get_entry_howto
1482 SEARCH,
1483 FIND_OR_CREATE,
1484 MUST_FIND,
1485 MUST_CREATE
1488 /* Get or create (depending on HOWTO) entry with KEY in GOT.
1489 INFO is context in which memory should be allocated (can be NULL if
1490 HOWTO is SEARCH or MUST_FIND). */
1492 static struct elf_m68k_got_entry *
1493 elf_m68k_get_got_entry (struct elf_m68k_got *got,
1494 const struct elf_m68k_got_entry_key *key,
1495 enum elf_m68k_get_entry_howto howto,
1496 struct bfd_link_info *info)
1498 struct elf_m68k_got_entry entry_;
1499 struct elf_m68k_got_entry *entry;
1500 void **ptr;
1502 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1504 if (got->entries == NULL)
1505 /* This is the first entry in ABFD. Initialize hashtable. */
1507 if (howto == SEARCH)
1508 return NULL;
1510 got->entries = htab_try_create (ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT
1511 (info),
1512 elf_m68k_got_entry_hash,
1513 elf_m68k_got_entry_eq, NULL);
1514 if (got->entries == NULL)
1516 bfd_set_error (bfd_error_no_memory);
1517 return NULL;
1521 entry_.key_ = *key;
1522 ptr = htab_find_slot (got->entries, &entry_, (howto != SEARCH
1523 ? INSERT : NO_INSERT));
1524 if (ptr == NULL)
1526 if (howto == SEARCH)
1527 /* Entry not found. */
1528 return NULL;
1530 /* We're out of memory. */
1531 bfd_set_error (bfd_error_no_memory);
1532 return NULL;
1535 if (*ptr == NULL)
1536 /* We didn't find the entry and we're asked to create a new one. */
1538 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1540 entry = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry));
1541 if (entry == NULL)
1542 return NULL;
1544 /* Initialize new entry. */
1545 entry->key_ = *key;
1547 entry->u.s1.refcount = 0;
1549 /* Mark the entry as not initialized. */
1550 entry->key_.type = R_68K_max;
1552 *ptr = entry;
1554 else
1555 /* We found the entry. */
1557 BFD_ASSERT (howto != MUST_CREATE);
1559 entry = *ptr;
1562 return entry;
1565 /* Update GOT counters when merging entry of WAS type with entry of NEW type.
1566 Return the value to which ENTRY's type should be set. */
1568 static enum elf_m68k_reloc_type
1569 elf_m68k_update_got_entry_type (struct elf_m68k_got *got,
1570 enum elf_m68k_reloc_type was,
1571 enum elf_m68k_reloc_type new_reloc)
1573 enum elf_m68k_got_offset_size was_size;
1574 enum elf_m68k_got_offset_size new_size;
1575 bfd_vma n_slots;
1577 if (was == R_68K_max)
1578 /* The type of the entry is not initialized yet. */
1580 /* Update all got->n_slots counters, including n_slots[R_32]. */
1581 was_size = R_LAST;
1583 was = new_reloc;
1585 else
1587 /* !!! We, probably, should emit an error rather then fail on assert
1588 in such a case. */
1589 BFD_ASSERT (elf_m68k_reloc_got_type (was)
1590 == elf_m68k_reloc_got_type (new_reloc));
1592 was_size = elf_m68k_reloc_got_offset_size (was);
1595 new_size = elf_m68k_reloc_got_offset_size (new_reloc);
1596 n_slots = elf_m68k_reloc_got_n_slots (new_reloc);
1598 while (was_size > new_size)
1600 --was_size;
1601 got->n_slots[was_size] += n_slots;
1604 if (new_reloc > was)
1605 /* Relocations are ordered from bigger got offset size to lesser,
1606 so choose the relocation type with lesser offset size. */
1607 was = new_reloc;
1609 return was;
1612 /* Add new or update existing entry to GOT.
1613 H, ABFD, TYPE and SYMNDX is data for the entry.
1614 INFO is a context where memory should be allocated. */
1616 static struct elf_m68k_got_entry *
1617 elf_m68k_add_entry_to_got (struct elf_m68k_got *got,
1618 struct elf_link_hash_entry *h,
1619 const bfd *abfd,
1620 enum elf_m68k_reloc_type reloc_type,
1621 unsigned long symndx,
1622 struct bfd_link_info *info)
1624 struct elf_m68k_got_entry_key key_;
1625 struct elf_m68k_got_entry *entry;
1627 if (h != NULL && elf_m68k_hash_entry (h)->got_entry_key == 0)
1628 elf_m68k_hash_entry (h)->got_entry_key
1629 = elf_m68k_multi_got (info)->global_symndx++;
1631 elf_m68k_init_got_entry_key (&key_, h, abfd, symndx, reloc_type);
1633 entry = elf_m68k_get_got_entry (got, &key_, FIND_OR_CREATE, info);
1634 if (entry == NULL)
1635 return NULL;
1637 /* Determine entry's type and update got->n_slots counters. */
1638 entry->key_.type = elf_m68k_update_got_entry_type (got,
1639 entry->key_.type,
1640 reloc_type);
1642 /* Update refcount. */
1643 ++entry->u.s1.refcount;
1645 if (entry->u.s1.refcount == 1)
1646 /* We see this entry for the first time. */
1648 if (entry->key_.bfd != NULL)
1649 got->local_n_slots += elf_m68k_reloc_got_n_slots (entry->key_.type);
1652 BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
1654 if ((got->n_slots[R_8]
1655 > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1656 || (got->n_slots[R_16]
1657 > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1658 /* This BFD has too many relocation. */
1660 if (got->n_slots[R_8] > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1661 /* xgettext:c-format */
1662 _bfd_error_handler (_("%pB: GOT overflow: "
1663 "number of relocations with 8-bit "
1664 "offset > %d"),
1665 abfd,
1666 ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info));
1667 else
1668 /* xgettext:c-format */
1669 _bfd_error_handler (_("%pB: GOT overflow: "
1670 "number of relocations with 8- or 16-bit "
1671 "offset > %d"),
1672 abfd,
1673 ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info));
1675 return NULL;
1678 return entry;
1681 /* Compute the hash value of the bfd in a bfd2got hash entry. */
1683 static hashval_t
1684 elf_m68k_bfd2got_entry_hash (const void *entry)
1686 const struct elf_m68k_bfd2got_entry *e;
1688 e = (const struct elf_m68k_bfd2got_entry *) entry;
1690 return e->bfd->id;
1693 /* Check whether two hash entries have the same bfd. */
1695 static int
1696 elf_m68k_bfd2got_entry_eq (const void *entry1, const void *entry2)
1698 const struct elf_m68k_bfd2got_entry *e1;
1699 const struct elf_m68k_bfd2got_entry *e2;
1701 e1 = (const struct elf_m68k_bfd2got_entry *) entry1;
1702 e2 = (const struct elf_m68k_bfd2got_entry *) entry2;
1704 return e1->bfd == e2->bfd;
1707 /* Destruct a bfd2got entry. */
1709 static void
1710 elf_m68k_bfd2got_entry_del (void *_entry)
1712 struct elf_m68k_bfd2got_entry *entry;
1714 entry = (struct elf_m68k_bfd2got_entry *) _entry;
1716 BFD_ASSERT (entry->got != NULL);
1717 elf_m68k_clear_got (entry->got);
1720 /* Find existing or create new (depending on HOWTO) bfd2got entry in
1721 MULTI_GOT. ABFD is the bfd we need a GOT for. INFO is a context where
1722 memory should be allocated. */
1724 static struct elf_m68k_bfd2got_entry *
1725 elf_m68k_get_bfd2got_entry (struct elf_m68k_multi_got *multi_got,
1726 const bfd *abfd,
1727 enum elf_m68k_get_entry_howto howto,
1728 struct bfd_link_info *info)
1730 struct elf_m68k_bfd2got_entry entry_;
1731 void **ptr;
1732 struct elf_m68k_bfd2got_entry *entry;
1734 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1736 if (multi_got->bfd2got == NULL)
1737 /* This is the first GOT. Initialize bfd2got. */
1739 if (howto == SEARCH)
1740 return NULL;
1742 multi_got->bfd2got = htab_try_create (1, elf_m68k_bfd2got_entry_hash,
1743 elf_m68k_bfd2got_entry_eq,
1744 elf_m68k_bfd2got_entry_del);
1745 if (multi_got->bfd2got == NULL)
1747 bfd_set_error (bfd_error_no_memory);
1748 return NULL;
1752 entry_.bfd = abfd;
1753 ptr = htab_find_slot (multi_got->bfd2got, &entry_, (howto != SEARCH
1754 ? INSERT : NO_INSERT));
1755 if (ptr == NULL)
1757 if (howto == SEARCH)
1758 /* Entry not found. */
1759 return NULL;
1761 /* We're out of memory. */
1762 bfd_set_error (bfd_error_no_memory);
1763 return NULL;
1766 if (*ptr == NULL)
1767 /* Entry was not found. Create new one. */
1769 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1771 entry = ((struct elf_m68k_bfd2got_entry *)
1772 bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry)));
1773 if (entry == NULL)
1774 return NULL;
1776 entry->bfd = abfd;
1778 entry->got = elf_m68k_create_empty_got (info);
1779 if (entry->got == NULL)
1780 return NULL;
1782 *ptr = entry;
1784 else
1786 BFD_ASSERT (howto != MUST_CREATE);
1788 /* Return existing entry. */
1789 entry = *ptr;
1792 return entry;
1795 struct elf_m68k_can_merge_gots_arg
1797 /* A current_got that we constructing a DIFF against. */
1798 struct elf_m68k_got *big;
1800 /* GOT holding entries not present or that should be changed in
1801 BIG. */
1802 struct elf_m68k_got *diff;
1804 /* Context where to allocate memory. */
1805 struct bfd_link_info *info;
1807 /* Error flag. */
1808 bfd_boolean error_p;
1811 /* Process a single entry from the small GOT to see if it should be added
1812 or updated in the big GOT. */
1814 static int
1815 elf_m68k_can_merge_gots_1 (void **_entry_ptr, void *_arg)
1817 const struct elf_m68k_got_entry *entry1;
1818 struct elf_m68k_can_merge_gots_arg *arg;
1819 const struct elf_m68k_got_entry *entry2;
1820 enum elf_m68k_reloc_type type;
1822 entry1 = (const struct elf_m68k_got_entry *) *_entry_ptr;
1823 arg = (struct elf_m68k_can_merge_gots_arg *) _arg;
1825 entry2 = elf_m68k_get_got_entry (arg->big, &entry1->key_, SEARCH, NULL);
1827 if (entry2 != NULL)
1828 /* We found an existing entry. Check if we should update it. */
1830 type = elf_m68k_update_got_entry_type (arg->diff,
1831 entry2->key_.type,
1832 entry1->key_.type);
1834 if (type == entry2->key_.type)
1835 /* ENTRY1 doesn't update data in ENTRY2. Skip it.
1836 To skip creation of difference entry we use the type,
1837 which we won't see in GOT entries for sure. */
1838 type = R_68K_max;
1840 else
1841 /* We didn't find the entry. Add entry1 to DIFF. */
1843 BFD_ASSERT (entry1->key_.type != R_68K_max);
1845 type = elf_m68k_update_got_entry_type (arg->diff,
1846 R_68K_max, entry1->key_.type);
1848 if (entry1->key_.bfd != NULL)
1849 arg->diff->local_n_slots += elf_m68k_reloc_got_n_slots (type);
1852 if (type != R_68K_max)
1853 /* Create an entry in DIFF. */
1855 struct elf_m68k_got_entry *entry;
1857 entry = elf_m68k_get_got_entry (arg->diff, &entry1->key_, MUST_CREATE,
1858 arg->info);
1859 if (entry == NULL)
1861 arg->error_p = TRUE;
1862 return 0;
1865 entry->key_.type = type;
1868 return 1;
1871 /* Return TRUE if SMALL GOT can be added to BIG GOT without overflowing it.
1872 Construct DIFF GOT holding the entries which should be added or updated
1873 in BIG GOT to accumulate information from SMALL.
1874 INFO is the context where memory should be allocated. */
1876 static bfd_boolean
1877 elf_m68k_can_merge_gots (struct elf_m68k_got *big,
1878 const struct elf_m68k_got *small,
1879 struct bfd_link_info *info,
1880 struct elf_m68k_got *diff)
1882 struct elf_m68k_can_merge_gots_arg arg_;
1884 BFD_ASSERT (small->offset == (bfd_vma) -1);
1886 arg_.big = big;
1887 arg_.diff = diff;
1888 arg_.info = info;
1889 arg_.error_p = FALSE;
1890 htab_traverse_noresize (small->entries, elf_m68k_can_merge_gots_1, &arg_);
1891 if (arg_.error_p)
1893 diff->offset = 0;
1894 return FALSE;
1897 /* Check for overflow. */
1898 if ((big->n_slots[R_8] + arg_.diff->n_slots[R_8]
1899 > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1900 || (big->n_slots[R_16] + arg_.diff->n_slots[R_16]
1901 > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1902 return FALSE;
1904 return TRUE;
1907 struct elf_m68k_merge_gots_arg
1909 /* The BIG got. */
1910 struct elf_m68k_got *big;
1912 /* Context where memory should be allocated. */
1913 struct bfd_link_info *info;
1915 /* Error flag. */
1916 bfd_boolean error_p;
1919 /* Process a single entry from DIFF got. Add or update corresponding
1920 entry in the BIG got. */
1922 static int
1923 elf_m68k_merge_gots_1 (void **entry_ptr, void *_arg)
1925 const struct elf_m68k_got_entry *from;
1926 struct elf_m68k_merge_gots_arg *arg;
1927 struct elf_m68k_got_entry *to;
1929 from = (const struct elf_m68k_got_entry *) *entry_ptr;
1930 arg = (struct elf_m68k_merge_gots_arg *) _arg;
1932 to = elf_m68k_get_got_entry (arg->big, &from->key_, FIND_OR_CREATE,
1933 arg->info);
1934 if (to == NULL)
1936 arg->error_p = TRUE;
1937 return 0;
1940 BFD_ASSERT (to->u.s1.refcount == 0);
1941 /* All we need to merge is TYPE. */
1942 to->key_.type = from->key_.type;
1944 return 1;
1947 /* Merge data from DIFF to BIG. INFO is context where memory should be
1948 allocated. */
1950 static bfd_boolean
1951 elf_m68k_merge_gots (struct elf_m68k_got *big,
1952 struct elf_m68k_got *diff,
1953 struct bfd_link_info *info)
1955 if (diff->entries != NULL)
1956 /* DIFF is not empty. Merge it into BIG GOT. */
1958 struct elf_m68k_merge_gots_arg arg_;
1960 /* Merge entries. */
1961 arg_.big = big;
1962 arg_.info = info;
1963 arg_.error_p = FALSE;
1964 htab_traverse_noresize (diff->entries, elf_m68k_merge_gots_1, &arg_);
1965 if (arg_.error_p)
1966 return FALSE;
1968 /* Merge counters. */
1969 big->n_slots[R_8] += diff->n_slots[R_8];
1970 big->n_slots[R_16] += diff->n_slots[R_16];
1971 big->n_slots[R_32] += diff->n_slots[R_32];
1972 big->local_n_slots += diff->local_n_slots;
1974 else
1975 /* DIFF is empty. */
1977 BFD_ASSERT (diff->n_slots[R_8] == 0);
1978 BFD_ASSERT (diff->n_slots[R_16] == 0);
1979 BFD_ASSERT (diff->n_slots[R_32] == 0);
1980 BFD_ASSERT (diff->local_n_slots == 0);
1983 BFD_ASSERT (!elf_m68k_hash_table (info)->allow_multigot_p
1984 || ((big->n_slots[R_8]
1985 <= ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1986 && (big->n_slots[R_16]
1987 <= ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info))));
1989 return TRUE;
1992 struct elf_m68k_finalize_got_offsets_arg
1994 /* Ranges of the offsets for GOT entries.
1995 R_x entries receive offsets between offset1[R_x] and offset2[R_x].
1996 R_x is R_8, R_16 and R_32. */
1997 bfd_vma *offset1;
1998 bfd_vma *offset2;
2000 /* Mapping from global symndx to global symbols.
2001 This is used to build lists of got entries for global symbols. */
2002 struct elf_m68k_link_hash_entry **symndx2h;
2004 bfd_vma n_ldm_entries;
2007 /* Assign ENTRY an offset. Build list of GOT entries for global symbols
2008 along the way. */
2010 static int
2011 elf_m68k_finalize_got_offsets_1 (void **entry_ptr, void *_arg)
2013 struct elf_m68k_got_entry *entry;
2014 struct elf_m68k_finalize_got_offsets_arg *arg;
2016 enum elf_m68k_got_offset_size got_offset_size;
2017 bfd_vma entry_size;
2019 entry = (struct elf_m68k_got_entry *) *entry_ptr;
2020 arg = (struct elf_m68k_finalize_got_offsets_arg *) _arg;
2022 /* This should be a fresh entry created in elf_m68k_can_merge_gots. */
2023 BFD_ASSERT (entry->u.s1.refcount == 0);
2025 /* Get GOT offset size for the entry . */
2026 got_offset_size = elf_m68k_reloc_got_offset_size (entry->key_.type);
2028 /* Calculate entry size in bytes. */
2029 entry_size = 4 * elf_m68k_reloc_got_n_slots (entry->key_.type);
2031 /* Check if we should switch to negative range of the offsets. */
2032 if (arg->offset1[got_offset_size] + entry_size
2033 > arg->offset2[got_offset_size])
2035 /* Verify that this is the only switch to negative range for
2036 got_offset_size. If this assertion fails, then we've miscalculated
2037 range for got_offset_size entries in
2038 elf_m68k_finalize_got_offsets. */
2039 BFD_ASSERT (arg->offset2[got_offset_size]
2040 != arg->offset2[-(int) got_offset_size - 1]);
2042 /* Switch. */
2043 arg->offset1[got_offset_size] = arg->offset1[-(int) got_offset_size - 1];
2044 arg->offset2[got_offset_size] = arg->offset2[-(int) got_offset_size - 1];
2046 /* Verify that now we have enough room for the entry. */
2047 BFD_ASSERT (arg->offset1[got_offset_size] + entry_size
2048 <= arg->offset2[got_offset_size]);
2051 /* Assign offset to entry. */
2052 entry->u.s2.offset = arg->offset1[got_offset_size];
2053 arg->offset1[got_offset_size] += entry_size;
2055 if (entry->key_.bfd == NULL)
2056 /* Hook up this entry into the list of got_entries of H. */
2058 struct elf_m68k_link_hash_entry *h;
2060 h = arg->symndx2h[entry->key_.symndx];
2061 if (h != NULL)
2063 entry->u.s2.next = h->glist;
2064 h->glist = entry;
2066 else
2067 /* This should be the entry for TLS_LDM relocation then. */
2069 BFD_ASSERT ((elf_m68k_reloc_got_type (entry->key_.type)
2070 == R_68K_TLS_LDM32)
2071 && entry->key_.symndx == 0);
2073 ++arg->n_ldm_entries;
2076 else
2077 /* This entry is for local symbol. */
2078 entry->u.s2.next = NULL;
2080 return 1;
2083 /* Assign offsets within GOT. USE_NEG_GOT_OFFSETS_P indicates if we
2084 should use negative offsets.
2085 Build list of GOT entries for global symbols along the way.
2086 SYMNDX2H is mapping from global symbol indices to actual
2087 global symbols.
2088 Return offset at which next GOT should start. */
2090 static void
2091 elf_m68k_finalize_got_offsets (struct elf_m68k_got *got,
2092 bfd_boolean use_neg_got_offsets_p,
2093 struct elf_m68k_link_hash_entry **symndx2h,
2094 bfd_vma *final_offset, bfd_vma *n_ldm_entries)
2096 struct elf_m68k_finalize_got_offsets_arg arg_;
2097 bfd_vma offset1_[2 * R_LAST];
2098 bfd_vma offset2_[2 * R_LAST];
2099 int i;
2100 bfd_vma start_offset;
2102 BFD_ASSERT (got->offset != (bfd_vma) -1);
2104 /* We set entry offsets relative to the .got section (and not the
2105 start of a particular GOT), so that we can use them in
2106 finish_dynamic_symbol without needing to know the GOT which they come
2107 from. */
2109 /* Put offset1 in the middle of offset1_, same for offset2. */
2110 arg_.offset1 = offset1_ + R_LAST;
2111 arg_.offset2 = offset2_ + R_LAST;
2113 start_offset = got->offset;
2115 if (use_neg_got_offsets_p)
2116 /* Setup both negative and positive ranges for R_8, R_16 and R_32. */
2117 i = -(int) R_32 - 1;
2118 else
2119 /* Setup positives ranges for R_8, R_16 and R_32. */
2120 i = (int) R_8;
2122 for (; i <= (int) R_32; ++i)
2124 int j;
2125 size_t n;
2127 /* Set beginning of the range of offsets I. */
2128 arg_.offset1[i] = start_offset;
2130 /* Calculate number of slots that require I offsets. */
2131 j = (i >= 0) ? i : -i - 1;
2132 n = (j >= 1) ? got->n_slots[j - 1] : 0;
2133 n = got->n_slots[j] - n;
2135 if (use_neg_got_offsets_p && n != 0)
2137 if (i < 0)
2138 /* We first fill the positive side of the range, so we might
2139 end up with one empty slot at that side when we can't fit
2140 whole 2-slot entry. Account for that at negative side of
2141 the interval with one additional entry. */
2142 n = n / 2 + 1;
2143 else
2144 /* When the number of slots is odd, make positive side of the
2145 range one entry bigger. */
2146 n = (n + 1) / 2;
2149 /* N is the number of slots that require I offsets.
2150 Calculate length of the range for I offsets. */
2151 n = 4 * n;
2153 /* Set end of the range. */
2154 arg_.offset2[i] = start_offset + n;
2156 start_offset = arg_.offset2[i];
2159 if (!use_neg_got_offsets_p)
2160 /* Make sure that if we try to switch to negative offsets in
2161 elf_m68k_finalize_got_offsets_1, the assert therein will catch
2162 the bug. */
2163 for (i = R_8; i <= R_32; ++i)
2164 arg_.offset2[-i - 1] = arg_.offset2[i];
2166 /* Setup got->offset. offset1[R_8] is either in the middle or at the
2167 beginning of GOT depending on use_neg_got_offsets_p. */
2168 got->offset = arg_.offset1[R_8];
2170 arg_.symndx2h = symndx2h;
2171 arg_.n_ldm_entries = 0;
2173 /* Assign offsets. */
2174 htab_traverse (got->entries, elf_m68k_finalize_got_offsets_1, &arg_);
2176 /* Check offset ranges we have actually assigned. */
2177 for (i = (int) R_8; i <= (int) R_32; ++i)
2178 BFD_ASSERT (arg_.offset2[i] - arg_.offset1[i] <= 4);
2180 *final_offset = start_offset;
2181 *n_ldm_entries = arg_.n_ldm_entries;
2184 struct elf_m68k_partition_multi_got_arg
2186 /* The GOT we are adding entries to. Aka big got. */
2187 struct elf_m68k_got *current_got;
2189 /* Offset to assign the next CURRENT_GOT. */
2190 bfd_vma offset;
2192 /* Context where memory should be allocated. */
2193 struct bfd_link_info *info;
2195 /* Total number of slots in the .got section.
2196 This is used to calculate size of the .got and .rela.got sections. */
2197 bfd_vma n_slots;
2199 /* Difference in numbers of allocated slots in the .got section
2200 and necessary relocations in the .rela.got section.
2201 This is used to calculate size of the .rela.got section. */
2202 bfd_vma slots_relas_diff;
2204 /* Error flag. */
2205 bfd_boolean error_p;
2207 /* Mapping from global symndx to global symbols.
2208 This is used to build lists of got entries for global symbols. */
2209 struct elf_m68k_link_hash_entry **symndx2h;
2212 static void
2213 elf_m68k_partition_multi_got_2 (struct elf_m68k_partition_multi_got_arg *arg)
2215 bfd_vma n_ldm_entries;
2217 elf_m68k_finalize_got_offsets (arg->current_got,
2218 (elf_m68k_hash_table (arg->info)
2219 ->use_neg_got_offsets_p),
2220 arg->symndx2h,
2221 &arg->offset, &n_ldm_entries);
2223 arg->n_slots += arg->current_got->n_slots[R_32];
2225 if (!bfd_link_pic (arg->info))
2226 /* If we are generating a shared object, we need to
2227 output a R_68K_RELATIVE reloc so that the dynamic
2228 linker can adjust this GOT entry. Overwise we
2229 don't need space in .rela.got for local symbols. */
2230 arg->slots_relas_diff += arg->current_got->local_n_slots;
2232 /* @LDM relocations require a 2-slot GOT entry, but only
2233 one relocation. Account for that. */
2234 arg->slots_relas_diff += n_ldm_entries;
2236 BFD_ASSERT (arg->slots_relas_diff <= arg->n_slots);
2240 /* Process a single BFD2GOT entry and either merge GOT to CURRENT_GOT
2241 or start a new CURRENT_GOT. */
2243 static int
2244 elf_m68k_partition_multi_got_1 (void **_entry, void *_arg)
2246 struct elf_m68k_bfd2got_entry *entry;
2247 struct elf_m68k_partition_multi_got_arg *arg;
2248 struct elf_m68k_got *got;
2249 struct elf_m68k_got diff_;
2250 struct elf_m68k_got *diff;
2252 entry = (struct elf_m68k_bfd2got_entry *) *_entry;
2253 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2255 got = entry->got;
2256 BFD_ASSERT (got != NULL);
2257 BFD_ASSERT (got->offset == (bfd_vma) -1);
2259 diff = NULL;
2261 if (arg->current_got != NULL)
2262 /* Construct diff. */
2264 diff = &diff_;
2265 elf_m68k_init_got (diff);
2267 if (!elf_m68k_can_merge_gots (arg->current_got, got, arg->info, diff))
2269 if (diff->offset == 0)
2270 /* Offset set to 0 in the diff_ indicates an error. */
2272 arg->error_p = TRUE;
2273 goto final_return;
2276 if (elf_m68k_hash_table (arg->info)->allow_multigot_p)
2278 elf_m68k_clear_got (diff);
2279 /* Schedule to finish up current_got and start new one. */
2280 diff = NULL;
2282 /* else
2283 Merge GOTs no matter what. If big GOT overflows,
2284 we'll fail in relocate_section due to truncated relocations.
2286 ??? May be fail earlier? E.g., in can_merge_gots. */
2289 else
2290 /* Diff of got against empty current_got is got itself. */
2292 /* Create empty current_got to put subsequent GOTs to. */
2293 arg->current_got = elf_m68k_create_empty_got (arg->info);
2294 if (arg->current_got == NULL)
2296 arg->error_p = TRUE;
2297 goto final_return;
2300 arg->current_got->offset = arg->offset;
2302 diff = got;
2305 if (diff != NULL)
2307 if (!elf_m68k_merge_gots (arg->current_got, diff, arg->info))
2309 arg->error_p = TRUE;
2310 goto final_return;
2313 /* Now we can free GOT. */
2314 elf_m68k_clear_got (got);
2316 entry->got = arg->current_got;
2318 else
2320 /* Finish up current_got. */
2321 elf_m68k_partition_multi_got_2 (arg);
2323 /* Schedule to start a new current_got. */
2324 arg->current_got = NULL;
2326 /* Retry. */
2327 if (!elf_m68k_partition_multi_got_1 (_entry, _arg))
2329 BFD_ASSERT (arg->error_p);
2330 goto final_return;
2334 final_return:
2335 if (diff != NULL)
2336 elf_m68k_clear_got (diff);
2338 return !arg->error_p;
2341 /* Helper function to build symndx2h mapping. */
2343 static bfd_boolean
2344 elf_m68k_init_symndx2h_1 (struct elf_link_hash_entry *_h,
2345 void *_arg)
2347 struct elf_m68k_link_hash_entry *h;
2349 h = elf_m68k_hash_entry (_h);
2351 if (h->got_entry_key != 0)
2352 /* H has at least one entry in the GOT. */
2354 struct elf_m68k_partition_multi_got_arg *arg;
2356 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2358 BFD_ASSERT (arg->symndx2h[h->got_entry_key] == NULL);
2359 arg->symndx2h[h->got_entry_key] = h;
2362 return TRUE;
2365 /* Merge GOTs of some BFDs, assign offsets to GOT entries and build
2366 lists of GOT entries for global symbols.
2367 Calculate sizes of .got and .rela.got sections. */
2369 static bfd_boolean
2370 elf_m68k_partition_multi_got (struct bfd_link_info *info)
2372 struct elf_m68k_multi_got *multi_got;
2373 struct elf_m68k_partition_multi_got_arg arg_;
2375 multi_got = elf_m68k_multi_got (info);
2377 arg_.current_got = NULL;
2378 arg_.offset = 0;
2379 arg_.info = info;
2380 arg_.n_slots = 0;
2381 arg_.slots_relas_diff = 0;
2382 arg_.error_p = FALSE;
2384 if (multi_got->bfd2got != NULL)
2386 /* Initialize symndx2h mapping. */
2388 arg_.symndx2h = bfd_zmalloc (multi_got->global_symndx
2389 * sizeof (*arg_.symndx2h));
2390 if (arg_.symndx2h == NULL)
2391 return FALSE;
2393 elf_link_hash_traverse (elf_hash_table (info),
2394 elf_m68k_init_symndx2h_1, &arg_);
2397 /* Partition. */
2398 htab_traverse (multi_got->bfd2got, elf_m68k_partition_multi_got_1,
2399 &arg_);
2400 if (arg_.error_p)
2402 free (arg_.symndx2h);
2403 arg_.symndx2h = NULL;
2405 return FALSE;
2408 /* Finish up last current_got. */
2409 elf_m68k_partition_multi_got_2 (&arg_);
2411 free (arg_.symndx2h);
2414 if (elf_hash_table (info)->dynobj != NULL)
2415 /* Set sizes of .got and .rela.got sections. */
2417 asection *s;
2419 s = elf_hash_table (info)->sgot;
2420 if (s != NULL)
2421 s->size = arg_.offset;
2422 else
2423 BFD_ASSERT (arg_.offset == 0);
2425 BFD_ASSERT (arg_.slots_relas_diff <= arg_.n_slots);
2426 arg_.n_slots -= arg_.slots_relas_diff;
2428 s = elf_hash_table (info)->srelgot;
2429 if (s != NULL)
2430 s->size = arg_.n_slots * sizeof (Elf32_External_Rela);
2431 else
2432 BFD_ASSERT (arg_.n_slots == 0);
2434 else
2435 BFD_ASSERT (multi_got->bfd2got == NULL);
2437 return TRUE;
2440 /* Copy any information related to dynamic linking from a pre-existing
2441 symbol to a newly created symbol. Also called to copy flags and
2442 other back-end info to a weakdef, in which case the symbol is not
2443 newly created and plt/got refcounts and dynamic indices should not
2444 be copied. */
2446 static void
2447 elf_m68k_copy_indirect_symbol (struct bfd_link_info *info,
2448 struct elf_link_hash_entry *_dir,
2449 struct elf_link_hash_entry *_ind)
2451 struct elf_m68k_link_hash_entry *dir;
2452 struct elf_m68k_link_hash_entry *ind;
2454 _bfd_elf_link_hash_copy_indirect (info, _dir, _ind);
2456 if (_ind->root.type != bfd_link_hash_indirect)
2457 return;
2459 dir = elf_m68k_hash_entry (_dir);
2460 ind = elf_m68k_hash_entry (_ind);
2462 /* Any absolute non-dynamic relocations against an indirect or weak
2463 definition will be against the target symbol. */
2464 _dir->non_got_ref |= _ind->non_got_ref;
2466 /* We might have a direct symbol already having entries in the GOTs.
2467 Update its key only in case indirect symbol has GOT entries and
2468 assert that both indirect and direct symbols don't have GOT entries
2469 at the same time. */
2470 if (ind->got_entry_key != 0)
2472 BFD_ASSERT (dir->got_entry_key == 0);
2473 /* Assert that GOTs aren't partioned yet. */
2474 BFD_ASSERT (ind->glist == NULL);
2476 dir->got_entry_key = ind->got_entry_key;
2477 ind->got_entry_key = 0;
2481 /* Look through the relocs for a section during the first phase, and
2482 allocate space in the global offset table or procedure linkage
2483 table. */
2485 static bfd_boolean
2486 elf_m68k_check_relocs (bfd *abfd,
2487 struct bfd_link_info *info,
2488 asection *sec,
2489 const Elf_Internal_Rela *relocs)
2491 bfd *dynobj;
2492 Elf_Internal_Shdr *symtab_hdr;
2493 struct elf_link_hash_entry **sym_hashes;
2494 const Elf_Internal_Rela *rel;
2495 const Elf_Internal_Rela *rel_end;
2496 asection *sreloc;
2497 struct elf_m68k_got *got;
2499 if (bfd_link_relocatable (info))
2500 return TRUE;
2502 dynobj = elf_hash_table (info)->dynobj;
2503 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2504 sym_hashes = elf_sym_hashes (abfd);
2506 sreloc = NULL;
2508 got = NULL;
2510 rel_end = relocs + sec->reloc_count;
2511 for (rel = relocs; rel < rel_end; rel++)
2513 unsigned long r_symndx;
2514 struct elf_link_hash_entry *h;
2516 r_symndx = ELF32_R_SYM (rel->r_info);
2518 if (r_symndx < symtab_hdr->sh_info)
2519 h = NULL;
2520 else
2522 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2523 while (h->root.type == bfd_link_hash_indirect
2524 || h->root.type == bfd_link_hash_warning)
2525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2528 switch (ELF32_R_TYPE (rel->r_info))
2530 case R_68K_GOT8:
2531 case R_68K_GOT16:
2532 case R_68K_GOT32:
2533 if (h != NULL
2534 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2535 break;
2536 /* Fall through. */
2538 /* Relative GOT relocations. */
2539 case R_68K_GOT8O:
2540 case R_68K_GOT16O:
2541 case R_68K_GOT32O:
2542 /* Fall through. */
2544 /* TLS relocations. */
2545 case R_68K_TLS_GD8:
2546 case R_68K_TLS_GD16:
2547 case R_68K_TLS_GD32:
2548 case R_68K_TLS_LDM8:
2549 case R_68K_TLS_LDM16:
2550 case R_68K_TLS_LDM32:
2551 case R_68K_TLS_IE8:
2552 case R_68K_TLS_IE16:
2553 case R_68K_TLS_IE32:
2555 case R_68K_TLS_TPREL32:
2556 case R_68K_TLS_DTPREL32:
2558 if (ELF32_R_TYPE (rel->r_info) == R_68K_TLS_TPREL32
2559 && bfd_link_pic (info))
2560 /* Do the special chorus for libraries with static TLS. */
2561 info->flags |= DF_STATIC_TLS;
2563 /* This symbol requires a global offset table entry. */
2565 if (dynobj == NULL)
2567 /* Create the .got section. */
2568 elf_hash_table (info)->dynobj = dynobj = abfd;
2569 if (!_bfd_elf_create_got_section (dynobj, info))
2570 return FALSE;
2573 if (got == NULL)
2575 struct elf_m68k_bfd2got_entry *bfd2got_entry;
2577 bfd2got_entry
2578 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2579 abfd, FIND_OR_CREATE, info);
2580 if (bfd2got_entry == NULL)
2581 return FALSE;
2583 got = bfd2got_entry->got;
2584 BFD_ASSERT (got != NULL);
2588 struct elf_m68k_got_entry *got_entry;
2590 /* Add entry to got. */
2591 got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
2592 ELF32_R_TYPE (rel->r_info),
2593 r_symndx, info);
2594 if (got_entry == NULL)
2595 return FALSE;
2597 if (got_entry->u.s1.refcount == 1)
2599 /* Make sure this symbol is output as a dynamic symbol. */
2600 if (h != NULL
2601 && h->dynindx == -1
2602 && !h->forced_local)
2604 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2605 return FALSE;
2610 break;
2612 case R_68K_PLT8:
2613 case R_68K_PLT16:
2614 case R_68K_PLT32:
2615 /* This symbol requires a procedure linkage table entry. We
2616 actually build the entry in adjust_dynamic_symbol,
2617 because this might be a case of linking PIC code which is
2618 never referenced by a dynamic object, in which case we
2619 don't need to generate a procedure linkage table entry
2620 after all. */
2622 /* If this is a local symbol, we resolve it directly without
2623 creating a procedure linkage table entry. */
2624 if (h == NULL)
2625 continue;
2627 h->needs_plt = 1;
2628 h->plt.refcount++;
2629 break;
2631 case R_68K_PLT8O:
2632 case R_68K_PLT16O:
2633 case R_68K_PLT32O:
2634 /* This symbol requires a procedure linkage table entry. */
2636 if (h == NULL)
2638 /* It does not make sense to have this relocation for a
2639 local symbol. FIXME: does it? How to handle it if
2640 it does make sense? */
2641 bfd_set_error (bfd_error_bad_value);
2642 return FALSE;
2645 /* Make sure this symbol is output as a dynamic symbol. */
2646 if (h->dynindx == -1
2647 && !h->forced_local)
2649 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2650 return FALSE;
2653 h->needs_plt = 1;
2654 h->plt.refcount++;
2655 break;
2657 case R_68K_PC8:
2658 case R_68K_PC16:
2659 case R_68K_PC32:
2660 /* If we are creating a shared library and this is not a local
2661 symbol, we need to copy the reloc into the shared library.
2662 However when linking with -Bsymbolic and this is a global
2663 symbol which is defined in an object we are including in the
2664 link (i.e., DEF_REGULAR is set), then we can resolve the
2665 reloc directly. At this point we have not seen all the input
2666 files, so it is possible that DEF_REGULAR is not set now but
2667 will be set later (it is never cleared). We account for that
2668 possibility below by storing information in the
2669 pcrel_relocs_copied field of the hash table entry. */
2670 if (!(bfd_link_pic (info)
2671 && (sec->flags & SEC_ALLOC) != 0
2672 && h != NULL
2673 && (!SYMBOLIC_BIND (info, h)
2674 || h->root.type == bfd_link_hash_defweak
2675 || !h->def_regular)))
2677 if (h != NULL)
2679 /* Make sure a plt entry is created for this symbol if
2680 it turns out to be a function defined by a dynamic
2681 object. */
2682 h->plt.refcount++;
2684 break;
2686 /* Fall through. */
2687 case R_68K_8:
2688 case R_68K_16:
2689 case R_68K_32:
2690 /* We don't need to handle relocs into sections not going into
2691 the "real" output. */
2692 if ((sec->flags & SEC_ALLOC) == 0)
2693 break;
2695 if (h != NULL)
2697 /* Make sure a plt entry is created for this symbol if it
2698 turns out to be a function defined by a dynamic object. */
2699 h->plt.refcount++;
2701 if (bfd_link_executable (info))
2702 /* This symbol needs a non-GOT reference. */
2703 h->non_got_ref = 1;
2706 /* If we are creating a shared library, we need to copy the
2707 reloc into the shared library. */
2708 if (bfd_link_pic (info)
2709 && (h == NULL
2710 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
2712 /* When creating a shared object, we must copy these
2713 reloc types into the output file. We create a reloc
2714 section in dynobj and make room for this reloc. */
2715 if (sreloc == NULL)
2717 sreloc = _bfd_elf_make_dynamic_reloc_section
2718 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
2720 if (sreloc == NULL)
2721 return FALSE;
2724 if (sec->flags & SEC_READONLY
2725 /* Don't set DF_TEXTREL yet for PC relative
2726 relocations, they might be discarded later. */
2727 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2728 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2729 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
2730 info->flags |= DF_TEXTREL;
2732 sreloc->size += sizeof (Elf32_External_Rela);
2734 /* We count the number of PC relative relocations we have
2735 entered for this symbol, so that we can discard them
2736 again if, in the -Bsymbolic case, the symbol is later
2737 defined by a regular object, or, in the normal shared
2738 case, the symbol is forced to be local. Note that this
2739 function is only called if we are using an m68kelf linker
2740 hash table, which means that h is really a pointer to an
2741 elf_m68k_link_hash_entry. */
2742 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2743 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2744 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
2746 struct elf_m68k_pcrel_relocs_copied *p;
2747 struct elf_m68k_pcrel_relocs_copied **head;
2749 if (h != NULL)
2751 struct elf_m68k_link_hash_entry *eh
2752 = elf_m68k_hash_entry (h);
2753 head = &eh->pcrel_relocs_copied;
2755 else
2757 asection *s;
2758 void *vpp;
2759 Elf_Internal_Sym *isym;
2761 isym = bfd_sym_from_r_symndx (&elf_m68k_hash_table (info)->sym_cache,
2762 abfd, r_symndx);
2763 if (isym == NULL)
2764 return FALSE;
2766 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2767 if (s == NULL)
2768 s = sec;
2770 vpp = &elf_section_data (s)->local_dynrel;
2771 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
2774 for (p = *head; p != NULL; p = p->next)
2775 if (p->section == sreloc)
2776 break;
2778 if (p == NULL)
2780 p = ((struct elf_m68k_pcrel_relocs_copied *)
2781 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2782 if (p == NULL)
2783 return FALSE;
2784 p->next = *head;
2785 *head = p;
2786 p->section = sreloc;
2787 p->count = 0;
2790 ++p->count;
2794 break;
2796 /* This relocation describes the C++ object vtable hierarchy.
2797 Reconstruct it for later use during GC. */
2798 case R_68K_GNU_VTINHERIT:
2799 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2800 return FALSE;
2801 break;
2803 /* This relocation describes which C++ vtable entries are actually
2804 used. Record for later use during GC. */
2805 case R_68K_GNU_VTENTRY:
2806 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2807 return FALSE;
2808 break;
2810 default:
2811 break;
2815 return TRUE;
2818 /* Return the section that should be marked against GC for a given
2819 relocation. */
2821 static asection *
2822 elf_m68k_gc_mark_hook (asection *sec,
2823 struct bfd_link_info *info,
2824 Elf_Internal_Rela *rel,
2825 struct elf_link_hash_entry *h,
2826 Elf_Internal_Sym *sym)
2828 if (h != NULL)
2829 switch (ELF32_R_TYPE (rel->r_info))
2831 case R_68K_GNU_VTINHERIT:
2832 case R_68K_GNU_VTENTRY:
2833 return NULL;
2836 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2839 /* Return the type of PLT associated with OUTPUT_BFD. */
2841 static const struct elf_m68k_plt_info *
2842 elf_m68k_get_plt_info (bfd *output_bfd)
2844 unsigned int features;
2846 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
2847 if (features & cpu32)
2848 return &elf_cpu32_plt_info;
2849 if (features & mcfisa_b)
2850 return &elf_isab_plt_info;
2851 if (features & mcfisa_c)
2852 return &elf_isac_plt_info;
2853 return &elf_m68k_plt_info;
2856 /* This function is called after all the input files have been read,
2857 and the input sections have been assigned to output sections.
2858 It's a convenient place to determine the PLT style. */
2860 static bfd_boolean
2861 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2863 /* Bind input BFDs to GOTs and calculate sizes of .got and .rela.got
2864 sections. */
2865 if (!elf_m68k_partition_multi_got (info))
2866 return FALSE;
2868 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
2869 return TRUE;
2872 /* Adjust a symbol defined by a dynamic object and referenced by a
2873 regular object. The current definition is in some section of the
2874 dynamic object, but we're not including those sections. We have to
2875 change the definition to something the rest of the link can
2876 understand. */
2878 static bfd_boolean
2879 elf_m68k_adjust_dynamic_symbol (struct bfd_link_info *info,
2880 struct elf_link_hash_entry *h)
2882 struct elf_m68k_link_hash_table *htab;
2883 bfd *dynobj;
2884 asection *s;
2886 htab = elf_m68k_hash_table (info);
2887 dynobj = htab->root.dynobj;
2889 /* Make sure we know what is going on here. */
2890 BFD_ASSERT (dynobj != NULL
2891 && (h->needs_plt
2892 || h->is_weakalias
2893 || (h->def_dynamic
2894 && h->ref_regular
2895 && !h->def_regular)));
2897 /* If this is a function, put it in the procedure linkage table. We
2898 will fill in the contents of the procedure linkage table later,
2899 when we know the address of the .got section. */
2900 if (h->type == STT_FUNC
2901 || h->needs_plt)
2903 if ((h->plt.refcount <= 0
2904 || SYMBOL_CALLS_LOCAL (info, h)
2905 || ((ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2906 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
2907 && h->root.type == bfd_link_hash_undefweak))
2908 /* We must always create the plt entry if it was referenced
2909 by a PLTxxO relocation. In this case we already recorded
2910 it as a dynamic symbol. */
2911 && h->dynindx == -1)
2913 /* This case can occur if we saw a PLTxx reloc in an input
2914 file, but the symbol was never referred to by a dynamic
2915 object, or if all references were garbage collected. In
2916 such a case, we don't actually need to build a procedure
2917 linkage table, and we can just do a PCxx reloc instead. */
2918 h->plt.offset = (bfd_vma) -1;
2919 h->needs_plt = 0;
2920 return TRUE;
2923 /* Make sure this symbol is output as a dynamic symbol. */
2924 if (h->dynindx == -1
2925 && !h->forced_local)
2927 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2928 return FALSE;
2931 s = htab->root.splt;
2932 BFD_ASSERT (s != NULL);
2934 /* If this is the first .plt entry, make room for the special
2935 first entry. */
2936 if (s->size == 0)
2937 s->size = htab->plt_info->size;
2939 /* If this symbol is not defined in a regular file, and we are
2940 not generating a shared library, then set the symbol to this
2941 location in the .plt. This is required to make function
2942 pointers compare as equal between the normal executable and
2943 the shared library. */
2944 if (!bfd_link_pic (info)
2945 && !h->def_regular)
2947 h->root.u.def.section = s;
2948 h->root.u.def.value = s->size;
2951 h->plt.offset = s->size;
2953 /* Make room for this entry. */
2954 s->size += htab->plt_info->size;
2956 /* We also need to make an entry in the .got.plt section, which
2957 will be placed in the .got section by the linker script. */
2958 s = htab->root.sgotplt;
2959 BFD_ASSERT (s != NULL);
2960 s->size += 4;
2962 /* We also need to make an entry in the .rela.plt section. */
2963 s = htab->root.srelplt;
2964 BFD_ASSERT (s != NULL);
2965 s->size += sizeof (Elf32_External_Rela);
2967 return TRUE;
2970 /* Reinitialize the plt offset now that it is not used as a reference
2971 count any more. */
2972 h->plt.offset = (bfd_vma) -1;
2974 /* If this is a weak symbol, and there is a real definition, the
2975 processor independent code will have arranged for us to see the
2976 real definition first, and we can just use the same value. */
2977 if (h->is_weakalias)
2979 struct elf_link_hash_entry *def = weakdef (h);
2980 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2981 h->root.u.def.section = def->root.u.def.section;
2982 h->root.u.def.value = def->root.u.def.value;
2983 return TRUE;
2986 /* This is a reference to a symbol defined by a dynamic object which
2987 is not a function. */
2989 /* If we are creating a shared library, we must presume that the
2990 only references to the symbol are via the global offset table.
2991 For such cases we need not do anything here; the relocations will
2992 be handled correctly by relocate_section. */
2993 if (bfd_link_pic (info))
2994 return TRUE;
2996 /* If there are no references to this symbol that do not use the
2997 GOT, we don't need to generate a copy reloc. */
2998 if (!h->non_got_ref)
2999 return TRUE;
3001 /* We must allocate the symbol in our .dynbss section, which will
3002 become part of the .bss section of the executable. There will be
3003 an entry for this symbol in the .dynsym section. The dynamic
3004 object will contain position independent code, so all references
3005 from the dynamic object to this symbol will go through the global
3006 offset table. The dynamic linker will use the .dynsym entry to
3007 determine the address it must put in the global offset table, so
3008 both the dynamic object and the regular object will refer to the
3009 same memory location for the variable. */
3011 s = bfd_get_linker_section (dynobj, ".dynbss");
3012 BFD_ASSERT (s != NULL);
3014 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
3015 copy the initial value out of the dynamic object and into the
3016 runtime process image. We need to remember the offset into the
3017 .rela.bss section we are going to use. */
3018 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3020 asection *srel;
3022 srel = bfd_get_linker_section (dynobj, ".rela.bss");
3023 BFD_ASSERT (srel != NULL);
3024 srel->size += sizeof (Elf32_External_Rela);
3025 h->needs_copy = 1;
3028 return _bfd_elf_adjust_dynamic_copy (info, h, s);
3031 /* Set the sizes of the dynamic sections. */
3033 static bfd_boolean
3034 elf_m68k_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3035 struct bfd_link_info *info)
3037 bfd *dynobj;
3038 asection *s;
3039 bfd_boolean plt;
3040 bfd_boolean relocs;
3042 dynobj = elf_hash_table (info)->dynobj;
3043 BFD_ASSERT (dynobj != NULL);
3045 if (elf_hash_table (info)->dynamic_sections_created)
3047 /* Set the contents of the .interp section to the interpreter. */
3048 if (bfd_link_executable (info) && !info->nointerp)
3050 s = bfd_get_linker_section (dynobj, ".interp");
3051 BFD_ASSERT (s != NULL);
3052 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3053 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3056 else
3058 /* We may have created entries in the .rela.got section.
3059 However, if we are not creating the dynamic sections, we will
3060 not actually use these entries. Reset the size of .rela.got,
3061 which will cause it to get stripped from the output file
3062 below. */
3063 s = elf_hash_table (info)->srelgot;
3064 if (s != NULL)
3065 s->size = 0;
3068 /* If this is a -Bsymbolic shared link, then we need to discard all
3069 PC relative relocs against symbols defined in a regular object.
3070 For the normal shared case we discard the PC relative relocs
3071 against symbols that have become local due to visibility changes.
3072 We allocated space for them in the check_relocs routine, but we
3073 will not fill them in in the relocate_section routine. */
3074 if (bfd_link_pic (info))
3075 elf_link_hash_traverse (elf_hash_table (info),
3076 elf_m68k_discard_copies,
3077 info);
3079 /* The check_relocs and adjust_dynamic_symbol entry points have
3080 determined the sizes of the various dynamic sections. Allocate
3081 memory for them. */
3082 plt = FALSE;
3083 relocs = FALSE;
3084 for (s = dynobj->sections; s != NULL; s = s->next)
3086 const char *name;
3088 if ((s->flags & SEC_LINKER_CREATED) == 0)
3089 continue;
3091 /* It's OK to base decisions on the section name, because none
3092 of the dynobj section names depend upon the input files. */
3093 name = bfd_get_section_name (dynobj, s);
3095 if (strcmp (name, ".plt") == 0)
3097 /* Remember whether there is a PLT. */
3098 plt = s->size != 0;
3100 else if (CONST_STRNEQ (name, ".rela"))
3102 if (s->size != 0)
3104 relocs = TRUE;
3106 /* We use the reloc_count field as a counter if we need
3107 to copy relocs into the output file. */
3108 s->reloc_count = 0;
3111 else if (! CONST_STRNEQ (name, ".got")
3112 && strcmp (name, ".dynbss") != 0)
3114 /* It's not one of our sections, so don't allocate space. */
3115 continue;
3118 if (s->size == 0)
3120 /* If we don't need this section, strip it from the
3121 output file. This is mostly to handle .rela.bss and
3122 .rela.plt. We must create both sections in
3123 create_dynamic_sections, because they must be created
3124 before the linker maps input sections to output
3125 sections. The linker does that before
3126 adjust_dynamic_symbol is called, and it is that
3127 function which decides whether anything needs to go
3128 into these sections. */
3129 s->flags |= SEC_EXCLUDE;
3130 continue;
3133 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3134 continue;
3136 /* Allocate memory for the section contents. */
3137 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3138 Unused entries should be reclaimed before the section's contents
3139 are written out, but at the moment this does not happen. Thus in
3140 order to prevent writing out garbage, we initialise the section's
3141 contents to zero. */
3142 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3143 if (s->contents == NULL)
3144 return FALSE;
3147 if (elf_hash_table (info)->dynamic_sections_created)
3149 /* Add some entries to the .dynamic section. We fill in the
3150 values later, in elf_m68k_finish_dynamic_sections, but we
3151 must add the entries now so that we get the correct size for
3152 the .dynamic section. The DT_DEBUG entry is filled in by the
3153 dynamic linker and used by the debugger. */
3154 #define add_dynamic_entry(TAG, VAL) \
3155 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3157 if (bfd_link_executable (info))
3159 if (!add_dynamic_entry (DT_DEBUG, 0))
3160 return FALSE;
3163 if (plt)
3165 if (!add_dynamic_entry (DT_PLTGOT, 0)
3166 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3167 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3168 || !add_dynamic_entry (DT_JMPREL, 0))
3169 return FALSE;
3172 if (relocs)
3174 if (!add_dynamic_entry (DT_RELA, 0)
3175 || !add_dynamic_entry (DT_RELASZ, 0)
3176 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3177 return FALSE;
3180 if ((info->flags & DF_TEXTREL) != 0)
3182 if (!add_dynamic_entry (DT_TEXTREL, 0))
3183 return FALSE;
3186 #undef add_dynamic_entry
3188 return TRUE;
3191 /* This function is called via elf_link_hash_traverse if we are
3192 creating a shared object. In the -Bsymbolic case it discards the
3193 space allocated to copy PC relative relocs against symbols which
3194 are defined in regular objects. For the normal shared case, it
3195 discards space for pc-relative relocs that have become local due to
3196 symbol visibility changes. We allocated space for them in the
3197 check_relocs routine, but we won't fill them in in the
3198 relocate_section routine.
3200 We also check whether any of the remaining relocations apply
3201 against a readonly section, and set the DF_TEXTREL flag in this
3202 case. */
3204 static bfd_boolean
3205 elf_m68k_discard_copies (struct elf_link_hash_entry *h,
3206 void * inf)
3208 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3209 struct elf_m68k_pcrel_relocs_copied *s;
3211 if (!SYMBOL_CALLS_LOCAL (info, h))
3213 if ((info->flags & DF_TEXTREL) == 0)
3215 /* Look for relocations against read-only sections. */
3216 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3217 s != NULL;
3218 s = s->next)
3219 if ((s->section->flags & SEC_READONLY) != 0)
3221 info->flags |= DF_TEXTREL;
3222 break;
3226 /* Make sure undefined weak symbols are output as a dynamic symbol
3227 in PIEs. */
3228 if (h->non_got_ref
3229 && h->root.type == bfd_link_hash_undefweak
3230 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3231 && h->dynindx == -1
3232 && !h->forced_local)
3234 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3235 return FALSE;
3238 return TRUE;
3241 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3242 s != NULL;
3243 s = s->next)
3244 s->section->size -= s->count * sizeof (Elf32_External_Rela);
3246 return TRUE;
3250 /* Install relocation RELA. */
3252 static void
3253 elf_m68k_install_rela (bfd *output_bfd,
3254 asection *srela,
3255 Elf_Internal_Rela *rela)
3257 bfd_byte *loc;
3259 loc = srela->contents;
3260 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3261 bfd_elf32_swap_reloca_out (output_bfd, rela, loc);
3264 /* Find the base offsets for thread-local storage in this object,
3265 for GD/LD and IE/LE respectively. */
3267 #define DTP_OFFSET 0x8000
3268 #define TP_OFFSET 0x7000
3270 static bfd_vma
3271 dtpoff_base (struct bfd_link_info *info)
3273 /* If tls_sec is NULL, we should have signalled an error already. */
3274 if (elf_hash_table (info)->tls_sec == NULL)
3275 return 0;
3276 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
3279 static bfd_vma
3280 tpoff_base (struct bfd_link_info *info)
3282 /* If tls_sec is NULL, we should have signalled an error already. */
3283 if (elf_hash_table (info)->tls_sec == NULL)
3284 return 0;
3285 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
3288 /* Output necessary relocation to handle a symbol during static link.
3289 This function is called from elf_m68k_relocate_section. */
3291 static void
3292 elf_m68k_init_got_entry_static (struct bfd_link_info *info,
3293 bfd *output_bfd,
3294 enum elf_m68k_reloc_type r_type,
3295 asection *sgot,
3296 bfd_vma got_entry_offset,
3297 bfd_vma relocation)
3299 switch (elf_m68k_reloc_got_type (r_type))
3301 case R_68K_GOT32O:
3302 bfd_put_32 (output_bfd, relocation, sgot->contents + got_entry_offset);
3303 break;
3305 case R_68K_TLS_GD32:
3306 /* We know the offset within the module,
3307 put it into the second GOT slot. */
3308 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3309 sgot->contents + got_entry_offset + 4);
3310 /* FALLTHRU */
3312 case R_68K_TLS_LDM32:
3313 /* Mark it as belonging to module 1, the executable. */
3314 bfd_put_32 (output_bfd, 1, sgot->contents + got_entry_offset);
3315 break;
3317 case R_68K_TLS_IE32:
3318 bfd_put_32 (output_bfd, relocation - tpoff_base (info),
3319 sgot->contents + got_entry_offset);
3320 break;
3322 default:
3323 BFD_ASSERT (FALSE);
3327 /* Output necessary relocation to handle a local symbol
3328 during dynamic link.
3329 This function is called either from elf_m68k_relocate_section
3330 or from elf_m68k_finish_dynamic_symbol. */
3332 static void
3333 elf_m68k_init_got_entry_local_shared (struct bfd_link_info *info,
3334 bfd *output_bfd,
3335 enum elf_m68k_reloc_type r_type,
3336 asection *sgot,
3337 bfd_vma got_entry_offset,
3338 bfd_vma relocation,
3339 asection *srela)
3341 Elf_Internal_Rela outrel;
3343 switch (elf_m68k_reloc_got_type (r_type))
3345 case R_68K_GOT32O:
3346 /* Emit RELATIVE relocation to initialize GOT slot
3347 at run-time. */
3348 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3349 outrel.r_addend = relocation;
3350 break;
3352 case R_68K_TLS_GD32:
3353 /* We know the offset within the module,
3354 put it into the second GOT slot. */
3355 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3356 sgot->contents + got_entry_offset + 4);
3357 /* FALLTHRU */
3359 case R_68K_TLS_LDM32:
3360 /* We don't know the module number,
3361 create a relocation for it. */
3362 outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_DTPMOD32);
3363 outrel.r_addend = 0;
3364 break;
3366 case R_68K_TLS_IE32:
3367 /* Emit TPREL relocation to initialize GOT slot
3368 at run-time. */
3369 outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_TPREL32);
3370 outrel.r_addend = relocation - elf_hash_table (info)->tls_sec->vma;
3371 break;
3373 default:
3374 BFD_ASSERT (FALSE);
3377 /* Offset of the GOT entry. */
3378 outrel.r_offset = (sgot->output_section->vma
3379 + sgot->output_offset
3380 + got_entry_offset);
3382 /* Install one of the above relocations. */
3383 elf_m68k_install_rela (output_bfd, srela, &outrel);
3385 bfd_put_32 (output_bfd, outrel.r_addend, sgot->contents + got_entry_offset);
3388 /* Relocate an M68K ELF section. */
3390 static bfd_boolean
3391 elf_m68k_relocate_section (bfd *output_bfd,
3392 struct bfd_link_info *info,
3393 bfd *input_bfd,
3394 asection *input_section,
3395 bfd_byte *contents,
3396 Elf_Internal_Rela *relocs,
3397 Elf_Internal_Sym *local_syms,
3398 asection **local_sections)
3400 Elf_Internal_Shdr *symtab_hdr;
3401 struct elf_link_hash_entry **sym_hashes;
3402 asection *sgot;
3403 asection *splt;
3404 asection *sreloc;
3405 asection *srela;
3406 struct elf_m68k_got *got;
3407 Elf_Internal_Rela *rel;
3408 Elf_Internal_Rela *relend;
3410 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3411 sym_hashes = elf_sym_hashes (input_bfd);
3413 sgot = NULL;
3414 splt = NULL;
3415 sreloc = NULL;
3416 srela = NULL;
3418 got = NULL;
3420 rel = relocs;
3421 relend = relocs + input_section->reloc_count;
3422 for (; rel < relend; rel++)
3424 int r_type;
3425 reloc_howto_type *howto;
3426 unsigned long r_symndx;
3427 struct elf_link_hash_entry *h;
3428 Elf_Internal_Sym *sym;
3429 asection *sec;
3430 bfd_vma relocation;
3431 bfd_boolean unresolved_reloc;
3432 bfd_reloc_status_type r;
3433 bfd_boolean resolved_to_zero;
3435 r_type = ELF32_R_TYPE (rel->r_info);
3436 if (r_type < 0 || r_type >= (int) R_68K_max)
3438 bfd_set_error (bfd_error_bad_value);
3439 return FALSE;
3441 howto = howto_table + r_type;
3443 r_symndx = ELF32_R_SYM (rel->r_info);
3445 h = NULL;
3446 sym = NULL;
3447 sec = NULL;
3448 unresolved_reloc = FALSE;
3450 if (r_symndx < symtab_hdr->sh_info)
3452 sym = local_syms + r_symndx;
3453 sec = local_sections[r_symndx];
3454 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3456 else
3458 bfd_boolean warned, ignored;
3460 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3461 r_symndx, symtab_hdr, sym_hashes,
3462 h, sec, relocation,
3463 unresolved_reloc, warned, ignored);
3466 if (sec != NULL && discarded_section (sec))
3467 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3468 rel, 1, relend, howto, 0, contents);
3470 if (bfd_link_relocatable (info))
3471 continue;
3473 resolved_to_zero = (h != NULL
3474 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
3476 switch (r_type)
3478 case R_68K_GOT8:
3479 case R_68K_GOT16:
3480 case R_68K_GOT32:
3481 /* Relocation is to the address of the entry for this symbol
3482 in the global offset table. */
3483 if (h != NULL
3484 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3486 if (elf_m68k_hash_table (info)->local_gp_p)
3488 bfd_vma sgot_output_offset;
3489 bfd_vma got_offset;
3491 sgot = elf_hash_table (info)->sgot;
3493 if (sgot != NULL)
3494 sgot_output_offset = sgot->output_offset;
3495 else
3496 /* In this case we have a reference to
3497 _GLOBAL_OFFSET_TABLE_, but the GOT itself is
3498 empty.
3499 ??? Issue a warning? */
3500 sgot_output_offset = 0;
3502 if (got == NULL)
3504 struct elf_m68k_bfd2got_entry *bfd2got_entry;
3506 bfd2got_entry
3507 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3508 input_bfd, SEARCH, NULL);
3510 if (bfd2got_entry != NULL)
3512 got = bfd2got_entry->got;
3513 BFD_ASSERT (got != NULL);
3515 got_offset = got->offset;
3517 else
3518 /* In this case we have a reference to
3519 _GLOBAL_OFFSET_TABLE_, but no other references
3520 accessing any GOT entries.
3521 ??? Issue a warning? */
3522 got_offset = 0;
3524 else
3525 got_offset = got->offset;
3527 /* Adjust GOT pointer to point to the GOT
3528 assigned to input_bfd. */
3529 rel->r_addend += sgot_output_offset + got_offset;
3531 else
3532 BFD_ASSERT (got == NULL || got->offset == 0);
3534 break;
3536 /* Fall through. */
3537 case R_68K_GOT8O:
3538 case R_68K_GOT16O:
3539 case R_68K_GOT32O:
3541 case R_68K_TLS_LDM32:
3542 case R_68K_TLS_LDM16:
3543 case R_68K_TLS_LDM8:
3545 case R_68K_TLS_GD8:
3546 case R_68K_TLS_GD16:
3547 case R_68K_TLS_GD32:
3549 case R_68K_TLS_IE8:
3550 case R_68K_TLS_IE16:
3551 case R_68K_TLS_IE32:
3553 /* Relocation is the offset of the entry for this symbol in
3554 the global offset table. */
3557 struct elf_m68k_got_entry_key key_;
3558 bfd_vma *off_ptr;
3559 bfd_vma off;
3561 sgot = elf_hash_table (info)->sgot;
3562 BFD_ASSERT (sgot != NULL);
3564 if (got == NULL)
3566 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3567 input_bfd, MUST_FIND,
3568 NULL)->got;
3569 BFD_ASSERT (got != NULL);
3572 /* Get GOT offset for this symbol. */
3573 elf_m68k_init_got_entry_key (&key_, h, input_bfd, r_symndx,
3574 r_type);
3575 off_ptr = &elf_m68k_get_got_entry (got, &key_, MUST_FIND,
3576 NULL)->u.s2.offset;
3577 off = *off_ptr;
3579 /* The offset must always be a multiple of 4. We use
3580 the least significant bit to record whether we have
3581 already generated the necessary reloc. */
3582 if ((off & 1) != 0)
3583 off &= ~1;
3584 else
3586 if (h != NULL
3587 /* @TLSLDM relocations are bounded to the module, in
3588 which the symbol is defined -- not to the symbol
3589 itself. */
3590 && elf_m68k_reloc_got_type (r_type) != R_68K_TLS_LDM32)
3592 bfd_boolean dyn;
3594 dyn = elf_hash_table (info)->dynamic_sections_created;
3595 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3596 bfd_link_pic (info),
3598 || (bfd_link_pic (info)
3599 && SYMBOL_REFERENCES_LOCAL (info, h))
3600 || ((ELF_ST_VISIBILITY (h->other)
3601 || resolved_to_zero)
3602 && h->root.type == bfd_link_hash_undefweak))
3604 /* This is actually a static link, or it is a
3605 -Bsymbolic link and the symbol is defined
3606 locally, or the symbol was forced to be local
3607 because of a version file. We must initialize
3608 this entry in the global offset table. Since
3609 the offset must always be a multiple of 4, we
3610 use the least significant bit to record whether
3611 we have initialized it already.
3613 When doing a dynamic link, we create a .rela.got
3614 relocation entry to initialize the value. This
3615 is done in the finish_dynamic_symbol routine. */
3617 elf_m68k_init_got_entry_static (info,
3618 output_bfd,
3619 r_type,
3620 sgot,
3621 off,
3622 relocation);
3624 *off_ptr |= 1;
3626 else
3627 unresolved_reloc = FALSE;
3629 else if (bfd_link_pic (info)) /* && h == NULL */
3630 /* Process local symbol during dynamic link. */
3632 srela = elf_hash_table (info)->srelgot;
3633 BFD_ASSERT (srela != NULL);
3635 elf_m68k_init_got_entry_local_shared (info,
3636 output_bfd,
3637 r_type,
3638 sgot,
3639 off,
3640 relocation,
3641 srela);
3643 *off_ptr |= 1;
3645 else /* h == NULL && !bfd_link_pic (info) */
3647 elf_m68k_init_got_entry_static (info,
3648 output_bfd,
3649 r_type,
3650 sgot,
3651 off,
3652 relocation);
3654 *off_ptr |= 1;
3658 /* We don't use elf_m68k_reloc_got_type in the condition below
3659 because this is the only place where difference between
3660 R_68K_GOTx and R_68K_GOTxO relocations matters. */
3661 if (r_type == R_68K_GOT32O
3662 || r_type == R_68K_GOT16O
3663 || r_type == R_68K_GOT8O
3664 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_GD32
3665 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_LDM32
3666 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_IE32)
3668 /* GOT pointer is adjusted to point to the start/middle
3669 of local GOT. Adjust the offset accordingly. */
3670 BFD_ASSERT (elf_m68k_hash_table (info)->use_neg_got_offsets_p
3671 || off >= got->offset);
3673 if (elf_m68k_hash_table (info)->local_gp_p)
3674 relocation = off - got->offset;
3675 else
3677 BFD_ASSERT (got->offset == 0);
3678 relocation = sgot->output_offset + off;
3681 /* This relocation does not use the addend. */
3682 rel->r_addend = 0;
3684 else
3685 relocation = (sgot->output_section->vma + sgot->output_offset
3686 + off);
3688 break;
3690 case R_68K_TLS_LDO32:
3691 case R_68K_TLS_LDO16:
3692 case R_68K_TLS_LDO8:
3693 relocation -= dtpoff_base (info);
3694 break;
3696 case R_68K_TLS_LE32:
3697 case R_68K_TLS_LE16:
3698 case R_68K_TLS_LE8:
3699 if (bfd_link_dll (info))
3701 _bfd_error_handler
3702 /* xgettext:c-format */
3703 (_("%pB(%pA+%#" PRIx64 "): "
3704 "%s relocation not permitted in shared object"),
3705 input_bfd, input_section, (uint64_t) rel->r_offset,
3706 howto->name);
3708 return FALSE;
3710 else
3711 relocation -= tpoff_base (info);
3713 break;
3715 case R_68K_PLT8:
3716 case R_68K_PLT16:
3717 case R_68K_PLT32:
3718 /* Relocation is to the entry for this symbol in the
3719 procedure linkage table. */
3721 /* Resolve a PLTxx reloc against a local symbol directly,
3722 without using the procedure linkage table. */
3723 if (h == NULL)
3724 break;
3726 if (h->plt.offset == (bfd_vma) -1
3727 || !elf_hash_table (info)->dynamic_sections_created)
3729 /* We didn't make a PLT entry for this symbol. This
3730 happens when statically linking PIC code, or when
3731 using -Bsymbolic. */
3732 break;
3735 splt = elf_hash_table (info)->splt;
3736 BFD_ASSERT (splt != NULL);
3738 relocation = (splt->output_section->vma
3739 + splt->output_offset
3740 + h->plt.offset);
3741 unresolved_reloc = FALSE;
3742 break;
3744 case R_68K_PLT8O:
3745 case R_68K_PLT16O:
3746 case R_68K_PLT32O:
3747 /* Relocation is the offset of the entry for this symbol in
3748 the procedure linkage table. */
3749 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
3751 splt = elf_hash_table (info)->splt;
3752 BFD_ASSERT (splt != NULL);
3754 relocation = h->plt.offset;
3755 unresolved_reloc = FALSE;
3757 /* This relocation does not use the addend. */
3758 rel->r_addend = 0;
3760 break;
3762 case R_68K_8:
3763 case R_68K_16:
3764 case R_68K_32:
3765 case R_68K_PC8:
3766 case R_68K_PC16:
3767 case R_68K_PC32:
3768 if (bfd_link_pic (info)
3769 && r_symndx != STN_UNDEF
3770 && (input_section->flags & SEC_ALLOC) != 0
3771 && (h == NULL
3772 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3773 && !resolved_to_zero)
3774 || h->root.type != bfd_link_hash_undefweak)
3775 && ((r_type != R_68K_PC8
3776 && r_type != R_68K_PC16
3777 && r_type != R_68K_PC32)
3778 || !SYMBOL_CALLS_LOCAL (info, h)))
3780 Elf_Internal_Rela outrel;
3781 bfd_byte *loc;
3782 bfd_boolean skip, relocate;
3784 /* When generating a shared object, these relocations
3785 are copied into the output file to be resolved at run
3786 time. */
3788 skip = FALSE;
3789 relocate = FALSE;
3791 outrel.r_offset =
3792 _bfd_elf_section_offset (output_bfd, info, input_section,
3793 rel->r_offset);
3794 if (outrel.r_offset == (bfd_vma) -1)
3795 skip = TRUE;
3796 else if (outrel.r_offset == (bfd_vma) -2)
3797 skip = TRUE, relocate = TRUE;
3798 outrel.r_offset += (input_section->output_section->vma
3799 + input_section->output_offset);
3801 if (skip)
3802 memset (&outrel, 0, sizeof outrel);
3803 else if (h != NULL
3804 && h->dynindx != -1
3805 && (r_type == R_68K_PC8
3806 || r_type == R_68K_PC16
3807 || r_type == R_68K_PC32
3808 || !bfd_link_pic (info)
3809 || !SYMBOLIC_BIND (info, h)
3810 || !h->def_regular))
3812 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3813 outrel.r_addend = rel->r_addend;
3815 else
3817 /* This symbol is local, or marked to become local. */
3818 outrel.r_addend = relocation + rel->r_addend;
3820 if (r_type == R_68K_32)
3822 relocate = TRUE;
3823 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3825 else
3827 long indx;
3829 if (bfd_is_abs_section (sec))
3830 indx = 0;
3831 else if (sec == NULL || sec->owner == NULL)
3833 bfd_set_error (bfd_error_bad_value);
3834 return FALSE;
3836 else
3838 asection *osec;
3840 /* We are turning this relocation into one
3841 against a section symbol. It would be
3842 proper to subtract the symbol's value,
3843 osec->vma, from the emitted reloc addend,
3844 but ld.so expects buggy relocs. */
3845 osec = sec->output_section;
3846 indx = elf_section_data (osec)->dynindx;
3847 if (indx == 0)
3849 struct elf_link_hash_table *htab;
3850 htab = elf_hash_table (info);
3851 osec = htab->text_index_section;
3852 indx = elf_section_data (osec)->dynindx;
3854 BFD_ASSERT (indx != 0);
3857 outrel.r_info = ELF32_R_INFO (indx, r_type);
3861 sreloc = elf_section_data (input_section)->sreloc;
3862 if (sreloc == NULL)
3863 abort ();
3865 loc = sreloc->contents;
3866 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3867 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3869 /* This reloc will be computed at runtime, so there's no
3870 need to do anything now, except for R_68K_32
3871 relocations that have been turned into
3872 R_68K_RELATIVE. */
3873 if (!relocate)
3874 continue;
3877 break;
3879 case R_68K_GNU_VTINHERIT:
3880 case R_68K_GNU_VTENTRY:
3881 /* These are no-ops in the end. */
3882 continue;
3884 default:
3885 break;
3888 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3889 because such sections are not SEC_ALLOC and thus ld.so will
3890 not process them. */
3891 if (unresolved_reloc
3892 && !((input_section->flags & SEC_DEBUGGING) != 0
3893 && h->def_dynamic)
3894 && _bfd_elf_section_offset (output_bfd, info, input_section,
3895 rel->r_offset) != (bfd_vma) -1)
3897 _bfd_error_handler
3898 /* xgettext:c-format */
3899 (_("%pB(%pA+%#" PRIx64 "): "
3900 "unresolvable %s relocation against symbol `%s'"),
3901 input_bfd,
3902 input_section,
3903 (uint64_t) rel->r_offset,
3904 howto->name,
3905 h->root.root.string);
3906 return FALSE;
3909 if (r_symndx != STN_UNDEF
3910 && r_type != R_68K_NONE
3911 && (h == NULL
3912 || h->root.type == bfd_link_hash_defined
3913 || h->root.type == bfd_link_hash_defweak))
3915 char sym_type;
3917 sym_type = (sym != NULL) ? ELF32_ST_TYPE (sym->st_info) : h->type;
3919 if (elf_m68k_reloc_tls_p (r_type) != (sym_type == STT_TLS))
3921 const char *name;
3923 if (h != NULL)
3924 name = h->root.root.string;
3925 else
3927 name = (bfd_elf_string_from_elf_section
3928 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3929 if (name == NULL || *name == '\0')
3930 name = bfd_section_name (input_bfd, sec);
3933 _bfd_error_handler
3934 ((sym_type == STT_TLS
3935 /* xgettext:c-format */
3936 ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
3937 /* xgettext:c-format */
3938 : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
3939 input_bfd,
3940 input_section,
3941 (uint64_t) rel->r_offset,
3942 howto->name,
3943 name);
3947 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3948 contents, rel->r_offset,
3949 relocation, rel->r_addend);
3951 if (r != bfd_reloc_ok)
3953 const char *name;
3955 if (h != NULL)
3956 name = h->root.root.string;
3957 else
3959 name = bfd_elf_string_from_elf_section (input_bfd,
3960 symtab_hdr->sh_link,
3961 sym->st_name);
3962 if (name == NULL)
3963 return FALSE;
3964 if (*name == '\0')
3965 name = bfd_section_name (input_bfd, sec);
3968 if (r == bfd_reloc_overflow)
3969 (*info->callbacks->reloc_overflow)
3970 (info, (h ? &h->root : NULL), name, howto->name,
3971 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3972 else
3974 _bfd_error_handler
3975 /* xgettext:c-format */
3976 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3977 input_bfd, input_section,
3978 (uint64_t) rel->r_offset, name, (int) r);
3979 return FALSE;
3984 return TRUE;
3987 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
3988 into section SEC. */
3990 static void
3991 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
3993 /* Make VALUE PC-relative. */
3994 value -= sec->output_section->vma + offset;
3996 /* Apply any in-place addend. */
3997 value += bfd_get_32 (sec->owner, sec->contents + offset);
3999 bfd_put_32 (sec->owner, value, sec->contents + offset);
4002 /* Finish up dynamic symbol handling. We set the contents of various
4003 dynamic sections here. */
4005 static bfd_boolean
4006 elf_m68k_finish_dynamic_symbol (bfd *output_bfd,
4007 struct bfd_link_info *info,
4008 struct elf_link_hash_entry *h,
4009 Elf_Internal_Sym *sym)
4011 bfd *dynobj;
4013 dynobj = elf_hash_table (info)->dynobj;
4015 if (h->plt.offset != (bfd_vma) -1)
4017 const struct elf_m68k_plt_info *plt_info;
4018 asection *splt;
4019 asection *sgot;
4020 asection *srela;
4021 bfd_vma plt_index;
4022 bfd_vma got_offset;
4023 Elf_Internal_Rela rela;
4024 bfd_byte *loc;
4026 /* This symbol has an entry in the procedure linkage table. Set
4027 it up. */
4029 BFD_ASSERT (h->dynindx != -1);
4031 plt_info = elf_m68k_hash_table (info)->plt_info;
4032 splt = elf_hash_table (info)->splt;
4033 sgot = elf_hash_table (info)->sgotplt;
4034 srela = elf_hash_table (info)->srelplt;
4035 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
4037 /* Get the index in the procedure linkage table which
4038 corresponds to this symbol. This is the index of this symbol
4039 in all the symbols for which we are making plt entries. The
4040 first entry in the procedure linkage table is reserved. */
4041 plt_index = (h->plt.offset / plt_info->size) - 1;
4043 /* Get the offset into the .got table of the entry that
4044 corresponds to this function. Each .got entry is 4 bytes.
4045 The first three are reserved. */
4046 got_offset = (plt_index + 3) * 4;
4048 memcpy (splt->contents + h->plt.offset,
4049 plt_info->symbol_entry,
4050 plt_info->size);
4052 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
4053 (sgot->output_section->vma
4054 + sgot->output_offset
4055 + got_offset));
4057 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4058 splt->contents
4059 + h->plt.offset
4060 + plt_info->symbol_resolve_entry + 2);
4062 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
4063 splt->output_section->vma);
4065 /* Fill in the entry in the global offset table. */
4066 bfd_put_32 (output_bfd,
4067 (splt->output_section->vma
4068 + splt->output_offset
4069 + h->plt.offset
4070 + plt_info->symbol_resolve_entry),
4071 sgot->contents + got_offset);
4073 /* Fill in the entry in the .rela.plt section. */
4074 rela.r_offset = (sgot->output_section->vma
4075 + sgot->output_offset
4076 + got_offset);
4077 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
4078 rela.r_addend = 0;
4079 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
4080 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4082 if (!h->def_regular)
4084 /* Mark the symbol as undefined, rather than as defined in
4085 the .plt section. Leave the value alone. */
4086 sym->st_shndx = SHN_UNDEF;
4090 if (elf_m68k_hash_entry (h)->glist != NULL)
4092 asection *sgot;
4093 asection *srela;
4094 struct elf_m68k_got_entry *got_entry;
4096 /* This symbol has an entry in the global offset table. Set it
4097 up. */
4099 sgot = elf_hash_table (info)->sgot;
4100 srela = elf_hash_table (info)->srelgot;
4101 BFD_ASSERT (sgot != NULL && srela != NULL);
4103 got_entry = elf_m68k_hash_entry (h)->glist;
4105 while (got_entry != NULL)
4107 enum elf_m68k_reloc_type r_type;
4108 bfd_vma got_entry_offset;
4110 r_type = got_entry->key_.type;
4111 got_entry_offset = got_entry->u.s2.offset &~ (bfd_vma) 1;
4113 /* If this is a -Bsymbolic link, and the symbol is defined
4114 locally, we just want to emit a RELATIVE reloc. Likewise if
4115 the symbol was forced to be local because of a version file.
4116 The entry in the global offset table already have been
4117 initialized in the relocate_section function. */
4118 if (bfd_link_pic (info)
4119 && SYMBOL_REFERENCES_LOCAL (info, h))
4121 bfd_vma relocation;
4123 relocation = bfd_get_signed_32 (output_bfd,
4124 (sgot->contents
4125 + got_entry_offset));
4127 /* Undo TP bias. */
4128 switch (elf_m68k_reloc_got_type (r_type))
4130 case R_68K_GOT32O:
4131 case R_68K_TLS_LDM32:
4132 break;
4134 case R_68K_TLS_GD32:
4135 /* The value for this relocation is actually put in
4136 the second GOT slot. */
4137 relocation = bfd_get_signed_32 (output_bfd,
4138 (sgot->contents
4139 + got_entry_offset + 4));
4140 relocation += dtpoff_base (info);
4141 break;
4143 case R_68K_TLS_IE32:
4144 relocation += tpoff_base (info);
4145 break;
4147 default:
4148 BFD_ASSERT (FALSE);
4151 elf_m68k_init_got_entry_local_shared (info,
4152 output_bfd,
4153 r_type,
4154 sgot,
4155 got_entry_offset,
4156 relocation,
4157 srela);
4159 else
4161 Elf_Internal_Rela rela;
4163 /* Put zeros to GOT slots that will be initialized
4164 at run-time. */
4166 bfd_vma n_slots;
4168 n_slots = elf_m68k_reloc_got_n_slots (got_entry->key_.type);
4169 while (n_slots--)
4170 bfd_put_32 (output_bfd, (bfd_vma) 0,
4171 (sgot->contents + got_entry_offset
4172 + 4 * n_slots));
4175 rela.r_addend = 0;
4176 rela.r_offset = (sgot->output_section->vma
4177 + sgot->output_offset
4178 + got_entry_offset);
4180 switch (elf_m68k_reloc_got_type (r_type))
4182 case R_68K_GOT32O:
4183 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
4184 elf_m68k_install_rela (output_bfd, srela, &rela);
4185 break;
4187 case R_68K_TLS_GD32:
4188 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPMOD32);
4189 elf_m68k_install_rela (output_bfd, srela, &rela);
4191 rela.r_offset += 4;
4192 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPREL32);
4193 elf_m68k_install_rela (output_bfd, srela, &rela);
4194 break;
4196 case R_68K_TLS_IE32:
4197 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_TPREL32);
4198 elf_m68k_install_rela (output_bfd, srela, &rela);
4199 break;
4201 default:
4202 BFD_ASSERT (FALSE);
4203 break;
4207 got_entry = got_entry->u.s2.next;
4211 if (h->needs_copy)
4213 asection *s;
4214 Elf_Internal_Rela rela;
4215 bfd_byte *loc;
4217 /* This symbol needs a copy reloc. Set it up. */
4219 BFD_ASSERT (h->dynindx != -1
4220 && (h->root.type == bfd_link_hash_defined
4221 || h->root.type == bfd_link_hash_defweak));
4223 s = bfd_get_linker_section (dynobj, ".rela.bss");
4224 BFD_ASSERT (s != NULL);
4226 rela.r_offset = (h->root.u.def.value
4227 + h->root.u.def.section->output_section->vma
4228 + h->root.u.def.section->output_offset);
4229 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
4230 rela.r_addend = 0;
4231 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4232 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4235 return TRUE;
4238 /* Finish up the dynamic sections. */
4240 static bfd_boolean
4241 elf_m68k_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4243 bfd *dynobj;
4244 asection *sgot;
4245 asection *sdyn;
4247 dynobj = elf_hash_table (info)->dynobj;
4249 sgot = elf_hash_table (info)->sgotplt;
4250 BFD_ASSERT (sgot != NULL);
4251 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4253 if (elf_hash_table (info)->dynamic_sections_created)
4255 asection *splt;
4256 Elf32_External_Dyn *dyncon, *dynconend;
4258 splt = elf_hash_table (info)->splt;
4259 BFD_ASSERT (splt != NULL && sdyn != NULL);
4261 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4262 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4263 for (; dyncon < dynconend; dyncon++)
4265 Elf_Internal_Dyn dyn;
4266 asection *s;
4268 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4270 switch (dyn.d_tag)
4272 default:
4273 break;
4275 case DT_PLTGOT:
4276 s = elf_hash_table (info)->sgotplt;
4277 goto get_vma;
4278 case DT_JMPREL:
4279 s = elf_hash_table (info)->srelplt;
4280 get_vma:
4281 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4282 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4283 break;
4285 case DT_PLTRELSZ:
4286 s = elf_hash_table (info)->srelplt;
4287 dyn.d_un.d_val = s->size;
4288 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4289 break;
4293 /* Fill in the first entry in the procedure linkage table. */
4294 if (splt->size > 0)
4296 const struct elf_m68k_plt_info *plt_info;
4298 plt_info = elf_m68k_hash_table (info)->plt_info;
4299 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
4301 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
4302 (sgot->output_section->vma
4303 + sgot->output_offset
4304 + 4));
4306 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
4307 (sgot->output_section->vma
4308 + sgot->output_offset
4309 + 8));
4311 elf_section_data (splt->output_section)->this_hdr.sh_entsize
4312 = plt_info->size;
4316 /* Fill in the first three entries in the global offset table. */
4317 if (sgot->size > 0)
4319 if (sdyn == NULL)
4320 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4321 else
4322 bfd_put_32 (output_bfd,
4323 sdyn->output_section->vma + sdyn->output_offset,
4324 sgot->contents);
4325 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4326 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4329 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4331 return TRUE;
4334 /* Given a .data section and a .emreloc in-memory section, store
4335 relocation information into the .emreloc section which can be
4336 used at runtime to relocate the section. This is called by the
4337 linker when the --embedded-relocs switch is used. This is called
4338 after the add_symbols entry point has been called for all the
4339 objects, and before the final_link entry point is called. */
4341 bfd_boolean
4342 bfd_m68k_elf32_create_embedded_relocs (bfd *abfd, struct bfd_link_info *info,
4343 asection *datasec, asection *relsec,
4344 char **errmsg)
4346 Elf_Internal_Shdr *symtab_hdr;
4347 Elf_Internal_Sym *isymbuf = NULL;
4348 Elf_Internal_Rela *internal_relocs = NULL;
4349 Elf_Internal_Rela *irel, *irelend;
4350 bfd_byte *p;
4351 bfd_size_type amt;
4353 BFD_ASSERT (! bfd_link_relocatable (info));
4355 *errmsg = NULL;
4357 if (datasec->reloc_count == 0)
4358 return TRUE;
4360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4362 /* Get a copy of the native relocations. */
4363 internal_relocs = (_bfd_elf_link_read_relocs
4364 (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
4365 info->keep_memory));
4366 if (internal_relocs == NULL)
4367 goto error_return;
4369 amt = (bfd_size_type) datasec->reloc_count * 12;
4370 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
4371 if (relsec->contents == NULL)
4372 goto error_return;
4374 p = relsec->contents;
4376 irelend = internal_relocs + datasec->reloc_count;
4377 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
4379 asection *targetsec;
4381 /* We are going to write a four byte longword into the runtime
4382 reloc section. The longword will be the address in the data
4383 section which must be relocated. It is followed by the name
4384 of the target section NUL-padded or truncated to 8
4385 characters. */
4387 /* We can only relocate absolute longword relocs at run time. */
4388 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
4390 *errmsg = _("unsupported relocation type");
4391 bfd_set_error (bfd_error_bad_value);
4392 goto error_return;
4395 /* Get the target section referred to by the reloc. */
4396 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4398 /* A local symbol. */
4399 Elf_Internal_Sym *isym;
4401 /* Read this BFD's local symbols if we haven't done so already. */
4402 if (isymbuf == NULL)
4404 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4405 if (isymbuf == NULL)
4406 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4407 symtab_hdr->sh_info, 0,
4408 NULL, NULL, NULL);
4409 if (isymbuf == NULL)
4410 goto error_return;
4413 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4414 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4416 else
4418 unsigned long indx;
4419 struct elf_link_hash_entry *h;
4421 /* An external symbol. */
4422 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4423 h = elf_sym_hashes (abfd)[indx];
4424 BFD_ASSERT (h != NULL);
4425 if (h->root.type == bfd_link_hash_defined
4426 || h->root.type == bfd_link_hash_defweak)
4427 targetsec = h->root.u.def.section;
4428 else
4429 targetsec = NULL;
4432 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
4433 memset (p + 4, 0, 8);
4434 if (targetsec != NULL)
4435 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
4438 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4439 free (isymbuf);
4440 if (internal_relocs != NULL
4441 && elf_section_data (datasec)->relocs != internal_relocs)
4442 free (internal_relocs);
4443 return TRUE;
4445 error_return:
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 FALSE;
4454 /* Set target options. */
4456 void
4457 bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
4459 struct elf_m68k_link_hash_table *htab;
4460 bfd_boolean use_neg_got_offsets_p;
4461 bfd_boolean allow_multigot_p;
4462 bfd_boolean local_gp_p;
4464 switch (got_handling)
4466 case 0:
4467 /* --got=single. */
4468 local_gp_p = FALSE;
4469 use_neg_got_offsets_p = FALSE;
4470 allow_multigot_p = FALSE;
4471 break;
4473 case 1:
4474 /* --got=negative. */
4475 local_gp_p = TRUE;
4476 use_neg_got_offsets_p = TRUE;
4477 allow_multigot_p = FALSE;
4478 break;
4480 case 2:
4481 /* --got=multigot. */
4482 local_gp_p = TRUE;
4483 use_neg_got_offsets_p = TRUE;
4484 allow_multigot_p = TRUE;
4485 break;
4487 default:
4488 BFD_ASSERT (FALSE);
4489 return;
4492 htab = elf_m68k_hash_table (info);
4493 if (htab != NULL)
4495 htab->local_gp_p = local_gp_p;
4496 htab->use_neg_got_offsets_p = use_neg_got_offsets_p;
4497 htab->allow_multigot_p = allow_multigot_p;
4501 static enum elf_reloc_type_class
4502 elf32_m68k_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4503 const asection *rel_sec ATTRIBUTE_UNUSED,
4504 const Elf_Internal_Rela *rela)
4506 switch ((int) ELF32_R_TYPE (rela->r_info))
4508 case R_68K_RELATIVE:
4509 return reloc_class_relative;
4510 case R_68K_JMP_SLOT:
4511 return reloc_class_plt;
4512 case R_68K_COPY:
4513 return reloc_class_copy;
4514 default:
4515 return reloc_class_normal;
4519 /* Return address for Ith PLT stub in section PLT, for relocation REL
4520 or (bfd_vma) -1 if it should not be included. */
4522 static bfd_vma
4523 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
4524 const arelent *rel ATTRIBUTE_UNUSED)
4526 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
4529 /* Support for core dump NOTE sections. */
4531 static bfd_boolean
4532 elf_m68k_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4534 int offset;
4535 size_t size;
4537 switch (note->descsz)
4539 default:
4540 return FALSE;
4542 case 154: /* Linux/m68k */
4543 /* pr_cursig */
4544 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4546 /* pr_pid */
4547 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
4549 /* pr_reg */
4550 offset = 70;
4551 size = 80;
4553 break;
4556 /* Make a ".reg/999" section. */
4557 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4558 size, note->descpos + offset);
4561 static bfd_boolean
4562 elf_m68k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4564 switch (note->descsz)
4566 default:
4567 return FALSE;
4569 case 124: /* Linux/m68k elf_prpsinfo. */
4570 elf_tdata (abfd)->core->pid
4571 = bfd_get_32 (abfd, note->descdata + 12);
4572 elf_tdata (abfd)->core->program
4573 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
4574 elf_tdata (abfd)->core->command
4575 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
4578 /* Note that for some reason, a spurious space is tacked
4579 onto the end of the args in some (at least one anyway)
4580 implementations, so strip it off if it exists. */
4582 char *command = elf_tdata (abfd)->core->command;
4583 int n = strlen (command);
4585 if (n > 0 && command[n - 1] == ' ')
4586 command[n - 1] = '\0';
4589 return TRUE;
4592 #define TARGET_BIG_SYM m68k_elf32_vec
4593 #define TARGET_BIG_NAME "elf32-m68k"
4594 #define ELF_MACHINE_CODE EM_68K
4595 #define ELF_MAXPAGESIZE 0x2000
4596 #define elf_backend_create_dynamic_sections \
4597 _bfd_elf_create_dynamic_sections
4598 #define bfd_elf32_bfd_link_hash_table_create \
4599 elf_m68k_link_hash_table_create
4600 #define bfd_elf32_bfd_final_link bfd_elf_final_link
4602 #define elf_backend_check_relocs elf_m68k_check_relocs
4603 #define elf_backend_always_size_sections \
4604 elf_m68k_always_size_sections
4605 #define elf_backend_adjust_dynamic_symbol \
4606 elf_m68k_adjust_dynamic_symbol
4607 #define elf_backend_size_dynamic_sections \
4608 elf_m68k_size_dynamic_sections
4609 #define elf_backend_final_write_processing elf_m68k_final_write_processing
4610 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4611 #define elf_backend_relocate_section elf_m68k_relocate_section
4612 #define elf_backend_finish_dynamic_symbol \
4613 elf_m68k_finish_dynamic_symbol
4614 #define elf_backend_finish_dynamic_sections \
4615 elf_m68k_finish_dynamic_sections
4616 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
4617 #define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
4618 #define bfd_elf32_bfd_merge_private_bfd_data \
4619 elf32_m68k_merge_private_bfd_data
4620 #define bfd_elf32_bfd_set_private_flags \
4621 elf32_m68k_set_private_flags
4622 #define bfd_elf32_bfd_print_private_bfd_data \
4623 elf32_m68k_print_private_bfd_data
4624 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
4625 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
4626 #define elf_backend_object_p elf32_m68k_object_p
4627 #define elf_backend_grok_prstatus elf_m68k_grok_prstatus
4628 #define elf_backend_grok_psinfo elf_m68k_grok_psinfo
4630 #define elf_backend_can_gc_sections 1
4631 #define elf_backend_can_refcount 1
4632 #define elf_backend_want_got_plt 1
4633 #define elf_backend_plt_readonly 1
4634 #define elf_backend_want_plt_sym 0
4635 #define elf_backend_got_header_size 12
4636 #define elf_backend_rela_normal 1
4637 #define elf_backend_dtrel_excludes_plt 1
4639 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
4641 #include "elf32-target.h"