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