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