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