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