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