elf32-m68k.c revision 1.1.1.1.2.1 1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/m68k.h"
29 #include "opcode/m68k.h"
30
31 static bfd_boolean
32 elf_m68k_discard_copies (struct elf_link_hash_entry *, void *);
33
34 static reloc_howto_type howto_table[] =
35 {
36 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
37 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
38 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
39 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
40 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
41 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
42 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
43 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
44 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
45 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
46 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
47 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
48 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
49 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
50 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
51 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
52 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
53 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
54 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
55 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
56 HOWTO(R_68K_GLOB_DAT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_GLOB_DAT", FALSE, 0, 0xffffffff,FALSE),
57 HOWTO(R_68K_JMP_SLOT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_JMP_SLOT", FALSE, 0, 0xffffffff,FALSE),
58 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
59 /* GNU extension to record C++ vtable hierarchy. */
60 HOWTO (R_68K_GNU_VTINHERIT, /* type */
61 0, /* rightshift */
62 2, /* size (0 = byte, 1 = short, 2 = long) */
63 0, /* bitsize */
64 FALSE, /* pc_relative */
65 0, /* bitpos */
66 complain_overflow_dont, /* complain_on_overflow */
67 NULL, /* special_function */
68 "R_68K_GNU_VTINHERIT", /* name */
69 FALSE, /* partial_inplace */
70 0, /* src_mask */
71 0, /* dst_mask */
72 FALSE),
73 /* GNU extension to record C++ vtable member usage. */
74 HOWTO (R_68K_GNU_VTENTRY, /* type */
75 0, /* rightshift */
76 2, /* size (0 = byte, 1 = short, 2 = long) */
77 0, /* bitsize */
78 FALSE, /* pc_relative */
79 0, /* bitpos */
80 complain_overflow_dont, /* complain_on_overflow */
81 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
82 "R_68K_GNU_VTENTRY", /* name */
83 FALSE, /* partial_inplace */
84 0, /* src_mask */
85 0, /* dst_mask */
86 FALSE),
87
88 /* TLS general dynamic variable reference. */
89 HOWTO (R_68K_TLS_GD32, /* type */
90 0, /* rightshift */
91 2, /* size (0 = byte, 1 = short, 2 = long) */
92 32, /* bitsize */
93 FALSE, /* pc_relative */
94 0, /* bitpos */
95 complain_overflow_bitfield, /* complain_on_overflow */
96 bfd_elf_generic_reloc, /* special_function */
97 "R_68K_TLS_GD32", /* name */
98 FALSE, /* partial_inplace */
99 0, /* src_mask */
100 0xffffffff, /* dst_mask */
101 FALSE), /* pcrel_offset */
102
103 HOWTO (R_68K_TLS_GD16, /* type */
104 0, /* rightshift */
105 1, /* size (0 = byte, 1 = short, 2 = long) */
106 16, /* bitsize */
107 FALSE, /* pc_relative */
108 0, /* bitpos */
109 complain_overflow_signed, /* complain_on_overflow */
110 bfd_elf_generic_reloc, /* special_function */
111 "R_68K_TLS_GD16", /* name */
112 FALSE, /* partial_inplace */
113 0, /* src_mask */
114 0x0000ffff, /* dst_mask */
115 FALSE), /* pcrel_offset */
116
117 HOWTO (R_68K_TLS_GD8, /* type */
118 0, /* rightshift */
119 0, /* size (0 = byte, 1 = short, 2 = long) */
120 8, /* bitsize */
121 FALSE, /* pc_relative */
122 0, /* bitpos */
123 complain_overflow_signed, /* complain_on_overflow */
124 bfd_elf_generic_reloc, /* special_function */
125 "R_68K_TLS_GD8", /* name */
126 FALSE, /* partial_inplace */
127 0, /* src_mask */
128 0x000000ff, /* dst_mask */
129 FALSE), /* pcrel_offset */
130
131 /* TLS local dynamic variable reference. */
132 HOWTO (R_68K_TLS_LDM32, /* type */
133 0, /* rightshift */
134 2, /* size (0 = byte, 1 = short, 2 = long) */
135 32, /* bitsize */
136 FALSE, /* pc_relative */
137 0, /* bitpos */
138 complain_overflow_bitfield, /* complain_on_overflow */
139 bfd_elf_generic_reloc, /* special_function */
140 "R_68K_TLS_LDM32", /* name */
141 FALSE, /* partial_inplace */
142 0, /* src_mask */
143 0xffffffff, /* dst_mask */
144 FALSE), /* pcrel_offset */
145
146 HOWTO (R_68K_TLS_LDM16, /* type */
147 0, /* rightshift */
148 1, /* size (0 = byte, 1 = short, 2 = long) */
149 16, /* bitsize */
150 FALSE, /* pc_relative */
151 0, /* bitpos */
152 complain_overflow_signed, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_68K_TLS_LDM16", /* name */
155 FALSE, /* partial_inplace */
156 0, /* src_mask */
157 0x0000ffff, /* dst_mask */
158 FALSE), /* pcrel_offset */
159
160 HOWTO (R_68K_TLS_LDM8, /* type */
161 0, /* rightshift */
162 0, /* size (0 = byte, 1 = short, 2 = long) */
163 8, /* bitsize */
164 FALSE, /* pc_relative */
165 0, /* bitpos */
166 complain_overflow_signed, /* complain_on_overflow */
167 bfd_elf_generic_reloc, /* special_function */
168 "R_68K_TLS_LDM8", /* name */
169 FALSE, /* partial_inplace */
170 0, /* src_mask */
171 0x000000ff, /* dst_mask */
172 FALSE), /* pcrel_offset */
173
174 HOWTO (R_68K_TLS_LDO32, /* type */
175 0, /* rightshift */
176 2, /* size (0 = byte, 1 = short, 2 = long) */
177 32, /* bitsize */
178 FALSE, /* pc_relative */
179 0, /* bitpos */
180 complain_overflow_bitfield, /* complain_on_overflow */
181 bfd_elf_generic_reloc, /* special_function */
182 "R_68K_TLS_LDO32", /* name */
183 FALSE, /* partial_inplace */
184 0, /* src_mask */
185 0xffffffff, /* dst_mask */
186 FALSE), /* pcrel_offset */
187
188 HOWTO (R_68K_TLS_LDO16, /* type */
189 0, /* rightshift */
190 1, /* size (0 = byte, 1 = short, 2 = long) */
191 16, /* bitsize */
192 FALSE, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_signed, /* complain_on_overflow */
195 bfd_elf_generic_reloc, /* special_function */
196 "R_68K_TLS_LDO16", /* name */
197 FALSE, /* partial_inplace */
198 0, /* src_mask */
199 0x0000ffff, /* dst_mask */
200 FALSE), /* pcrel_offset */
201
202 HOWTO (R_68K_TLS_LDO8, /* type */
203 0, /* rightshift */
204 0, /* size (0 = byte, 1 = short, 2 = long) */
205 8, /* bitsize */
206 FALSE, /* pc_relative */
207 0, /* bitpos */
208 complain_overflow_signed, /* complain_on_overflow */
209 bfd_elf_generic_reloc, /* special_function */
210 "R_68K_TLS_LDO8", /* name */
211 FALSE, /* partial_inplace */
212 0, /* src_mask */
213 0x000000ff, /* dst_mask */
214 FALSE), /* pcrel_offset */
215
216 /* TLS initial execution variable reference. */
217 HOWTO (R_68K_TLS_IE32, /* type */
218 0, /* rightshift */
219 2, /* size (0 = byte, 1 = short, 2 = long) */
220 32, /* bitsize */
221 FALSE, /* pc_relative */
222 0, /* bitpos */
223 complain_overflow_bitfield, /* complain_on_overflow */
224 bfd_elf_generic_reloc, /* special_function */
225 "R_68K_TLS_IE32", /* name */
226 FALSE, /* partial_inplace */
227 0, /* src_mask */
228 0xffffffff, /* dst_mask */
229 FALSE), /* pcrel_offset */
230
231 HOWTO (R_68K_TLS_IE16, /* type */
232 0, /* rightshift */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
234 16, /* bitsize */
235 FALSE, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_signed, /* complain_on_overflow */
238 bfd_elf_generic_reloc, /* special_function */
239 "R_68K_TLS_IE16", /* name */
240 FALSE, /* partial_inplace */
241 0, /* src_mask */
242 0x0000ffff, /* dst_mask */
243 FALSE), /* pcrel_offset */
244
245 HOWTO (R_68K_TLS_IE8, /* type */
246 0, /* rightshift */
247 0, /* size (0 = byte, 1 = short, 2 = long) */
248 8, /* bitsize */
249 FALSE, /* pc_relative */
250 0, /* bitpos */
251 complain_overflow_signed, /* complain_on_overflow */
252 bfd_elf_generic_reloc, /* special_function */
253 "R_68K_TLS_IE8", /* name */
254 FALSE, /* partial_inplace */
255 0, /* src_mask */
256 0x000000ff, /* dst_mask */
257 FALSE), /* pcrel_offset */
258
259 /* TLS local execution variable reference. */
260 HOWTO (R_68K_TLS_LE32, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 32, /* bitsize */
264 FALSE, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_68K_TLS_LE32", /* name */
269 FALSE, /* partial_inplace */
270 0, /* src_mask */
271 0xffffffff, /* dst_mask */
272 FALSE), /* pcrel_offset */
273
274 HOWTO (R_68K_TLS_LE16, /* type */
275 0, /* rightshift */
276 1, /* size (0 = byte, 1 = short, 2 = long) */
277 16, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_signed, /* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_68K_TLS_LE16", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0x0000ffff, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 HOWTO (R_68K_TLS_LE8, /* type */
289 0, /* rightshift */
290 0, /* size (0 = byte, 1 = short, 2 = long) */
291 8, /* bitsize */
292 FALSE, /* pc_relative */
293 0, /* bitpos */
294 complain_overflow_signed, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_68K_TLS_LE8", /* name */
297 FALSE, /* partial_inplace */
298 0, /* src_mask */
299 0x000000ff, /* dst_mask */
300 FALSE), /* pcrel_offset */
301
302 /* TLS GD/LD dynamic relocations. */
303 HOWTO (R_68K_TLS_DTPMOD32, /* type */
304 0, /* rightshift */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
306 32, /* bitsize */
307 FALSE, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_dont, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_68K_TLS_DTPMOD32", /* name */
312 FALSE, /* partial_inplace */
313 0, /* src_mask */
314 0xffffffff, /* dst_mask */
315 FALSE), /* pcrel_offset */
316
317 HOWTO (R_68K_TLS_DTPREL32, /* type */
318 0, /* rightshift */
319 2, /* size (0 = byte, 1 = short, 2 = long) */
320 32, /* bitsize */
321 FALSE, /* pc_relative */
322 0, /* bitpos */
323 complain_overflow_dont, /* complain_on_overflow */
324 bfd_elf_generic_reloc, /* special_function */
325 "R_68K_TLS_DTPREL32", /* name */
326 FALSE, /* partial_inplace */
327 0, /* src_mask */
328 0xffffffff, /* dst_mask */
329 FALSE), /* pcrel_offset */
330
331 HOWTO (R_68K_TLS_TPREL32, /* type */
332 0, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 32, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_dont, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_68K_TLS_TPREL32", /* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0xffffffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
344 };
345
346 static void
347 rtype_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
348 {
349 unsigned int indx = ELF32_R_TYPE (dst->r_info);
350
351 if (indx >= (unsigned int) R_68K_max)
352 {
353 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
354 abfd, (int) indx);
355 indx = R_68K_NONE;
356 }
357 cache_ptr->howto = &howto_table[indx];
358 }
359
360 #define elf_info_to_howto rtype_to_howto
361
362 static const struct
363 {
364 bfd_reloc_code_real_type bfd_val;
365 int elf_val;
366 }
367 reloc_map[] =
368 {
369 { BFD_RELOC_NONE, R_68K_NONE },
370 { BFD_RELOC_32, R_68K_32 },
371 { BFD_RELOC_16, R_68K_16 },
372 { BFD_RELOC_8, R_68K_8 },
373 { BFD_RELOC_32_PCREL, R_68K_PC32 },
374 { BFD_RELOC_16_PCREL, R_68K_PC16 },
375 { BFD_RELOC_8_PCREL, R_68K_PC8 },
376 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
377 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
378 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
379 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
380 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
381 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
382 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
383 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
384 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
385 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
386 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
387 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
388 { BFD_RELOC_NONE, R_68K_COPY },
389 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
390 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
391 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
392 { BFD_RELOC_CTOR, R_68K_32 },
393 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
394 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
395 { BFD_RELOC_68K_TLS_GD32, R_68K_TLS_GD32 },
396 { BFD_RELOC_68K_TLS_GD16, R_68K_TLS_GD16 },
397 { BFD_RELOC_68K_TLS_GD8, R_68K_TLS_GD8 },
398 { BFD_RELOC_68K_TLS_LDM32, R_68K_TLS_LDM32 },
399 { BFD_RELOC_68K_TLS_LDM16, R_68K_TLS_LDM16 },
400 { BFD_RELOC_68K_TLS_LDM8, R_68K_TLS_LDM8 },
401 { BFD_RELOC_68K_TLS_LDO32, R_68K_TLS_LDO32 },
402 { BFD_RELOC_68K_TLS_LDO16, R_68K_TLS_LDO16 },
403 { BFD_RELOC_68K_TLS_LDO8, R_68K_TLS_LDO8 },
404 { BFD_RELOC_68K_TLS_IE32, R_68K_TLS_IE32 },
405 { BFD_RELOC_68K_TLS_IE16, R_68K_TLS_IE16 },
406 { BFD_RELOC_68K_TLS_IE8, R_68K_TLS_IE8 },
407 { BFD_RELOC_68K_TLS_LE32, R_68K_TLS_LE32 },
408 { BFD_RELOC_68K_TLS_LE16, R_68K_TLS_LE16 },
409 { BFD_RELOC_68K_TLS_LE8, R_68K_TLS_LE8 },
410 };
411
412 static reloc_howto_type *
413 reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
414 bfd_reloc_code_real_type code)
415 {
416 unsigned int i;
417 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
418 {
419 if (reloc_map[i].bfd_val == code)
420 return &howto_table[reloc_map[i].elf_val];
421 }
422 return 0;
423 }
424
425 static reloc_howto_type *
426 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
427 {
428 unsigned int i;
429
430 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
431 if (howto_table[i].name != NULL
432 && strcasecmp (howto_table[i].name, r_name) == 0)
433 return &howto_table[i];
434
435 return NULL;
436 }
437
438 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
439 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
440 #define ELF_ARCH bfd_arch_m68k
441 #define ELF_TARGET_ID M68K_ELF_DATA
442
443 /* Functions for the m68k ELF linker. */
445
446 /* The name of the dynamic interpreter. This is put in the .interp
447 section. */
448
449 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
450
451 /* Describes one of the various PLT styles. */
452
453 struct elf_m68k_plt_info
454 {
455 /* The size of each PLT entry. */
456 bfd_vma size;
457
458 /* The template for the first PLT entry. */
459 const bfd_byte *plt0_entry;
460
461 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
462 The comments by each member indicate the value that the relocation
463 is against. */
464 struct {
465 unsigned int got4; /* .got + 4 */
466 unsigned int got8; /* .got + 8 */
467 } plt0_relocs;
468
469 /* The template for a symbol's PLT entry. */
470 const bfd_byte *symbol_entry;
471
472 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
473 The comments by each member indicate the value that the relocation
474 is against. */
475 struct {
476 unsigned int got; /* the symbol's .got.plt entry */
477 unsigned int plt; /* .plt */
478 } symbol_relocs;
479
480 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
481 The stub starts with "move.l #relocoffset,%d0". */
482 bfd_vma symbol_resolve_entry;
483 };
484
485 /* The size in bytes of an entry in the procedure linkage table. */
486
487 #define PLT_ENTRY_SIZE 20
488
489 /* The first entry in a procedure linkage table looks like this. See
490 the SVR4 ABI m68k supplement to see how this works. */
491
492 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
493 {
494 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
495 0, 0, 0, 2, /* + (.got + 4) - . */
496 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
497 0, 0, 0, 2, /* + (.got + 8) - . */
498 0, 0, 0, 0 /* pad out to 20 bytes. */
499 };
500
501 /* Subsequent entries in a procedure linkage table look like this. */
502
503 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
504 {
505 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
506 0, 0, 0, 2, /* + (.got.plt entry) - . */
507 0x2f, 0x3c, /* move.l #offset,-(%sp) */
508 0, 0, 0, 0, /* + reloc index */
509 0x60, 0xff, /* bra.l .plt */
510 0, 0, 0, 0 /* + .plt - . */
511 };
512
513 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
514 PLT_ENTRY_SIZE,
515 elf_m68k_plt0_entry, { 4, 12 },
516 elf_m68k_plt_entry, { 4, 16 }, 8
517 };
518
519 #define ISAB_PLT_ENTRY_SIZE 24
520
521 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
522 {
523 0x20, 0x3c, /* move.l #offset,%d0 */
524 0, 0, 0, 0, /* + (.got + 4) - . */
525 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
526 0x20, 0x3c, /* move.l #offset,%d0 */
527 0, 0, 0, 0, /* + (.got + 8) - . */
528 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
529 0x4e, 0xd0, /* jmp (%a0) */
530 0x4e, 0x71 /* nop */
531 };
532
533 /* Subsequent entries in a procedure linkage table look like this. */
534
535 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
536 {
537 0x20, 0x3c, /* move.l #offset,%d0 */
538 0, 0, 0, 0, /* + (.got.plt entry) - . */
539 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
540 0x4e, 0xd0, /* jmp (%a0) */
541 0x2f, 0x3c, /* move.l #offset,-(%sp) */
542 0, 0, 0, 0, /* + reloc index */
543 0x60, 0xff, /* bra.l .plt */
544 0, 0, 0, 0 /* + .plt - . */
545 };
546
547 static const struct elf_m68k_plt_info elf_isab_plt_info = {
548 ISAB_PLT_ENTRY_SIZE,
549 elf_isab_plt0_entry, { 2, 12 },
550 elf_isab_plt_entry, { 2, 20 }, 12
551 };
552
553 #define ISAC_PLT_ENTRY_SIZE 24
554
555 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
556 {
557 0x20, 0x3c, /* move.l #offset,%d0 */
558 0, 0, 0, 0, /* replaced with .got + 4 - . */
559 0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
560 0x20, 0x3c, /* move.l #offset,%d0 */
561 0, 0, 0, 0, /* replaced with .got + 8 - . */
562 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
563 0x4e, 0xd0, /* jmp (%a0) */
564 0x4e, 0x71 /* nop */
565 };
566
567 /* Subsequent entries in a procedure linkage table look like this. */
568
569 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
570 {
571 0x20, 0x3c, /* move.l #offset,%d0 */
572 0, 0, 0, 0, /* replaced with (.got entry) - . */
573 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
574 0x4e, 0xd0, /* jmp (%a0) */
575 0x2f, 0x3c, /* move.l #offset,-(%sp) */
576 0, 0, 0, 0, /* replaced with offset into relocation table */
577 0x61, 0xff, /* bsr.l .plt */
578 0, 0, 0, 0 /* replaced with .plt - . */
579 };
580
581 static const struct elf_m68k_plt_info elf_isac_plt_info = {
582 ISAC_PLT_ENTRY_SIZE,
583 elf_isac_plt0_entry, { 2, 12},
584 elf_isac_plt_entry, { 2, 20 }, 12
585 };
586
587 #define CPU32_PLT_ENTRY_SIZE 24
588 /* Procedure linkage table entries for the cpu32 */
589 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
590 {
591 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
592 0, 0, 0, 2, /* + (.got + 4) - . */
593 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
594 0, 0, 0, 2, /* + (.got + 8) - . */
595 0x4e, 0xd1, /* jmp %a1@ */
596 0, 0, 0, 0, /* pad out to 24 bytes. */
597 0, 0
598 };
599
600 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
601 {
602 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
603 0, 0, 0, 2, /* + (.got.plt entry) - . */
604 0x4e, 0xd1, /* jmp %a1@ */
605 0x2f, 0x3c, /* move.l #offset,-(%sp) */
606 0, 0, 0, 0, /* + reloc index */
607 0x60, 0xff, /* bra.l .plt */
608 0, 0, 0, 0, /* + .plt - . */
609 0, 0
610 };
611
612 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
613 CPU32_PLT_ENTRY_SIZE,
614 elf_cpu32_plt0_entry, { 4, 12 },
615 elf_cpu32_plt_entry, { 4, 18 }, 10
616 };
617
618 /* The m68k linker needs to keep track of the number of relocs that it
619 decides to copy in check_relocs for each symbol. This is so that it
620 can discard PC relative relocs if it doesn't need them when linking
621 with -Bsymbolic. We store the information in a field extending the
622 regular ELF linker hash table. */
623
624 /* This structure keeps track of the number of PC relative relocs we have
625 copied for a given symbol. */
626
627 struct elf_m68k_pcrel_relocs_copied
628 {
629 /* Next section. */
630 struct elf_m68k_pcrel_relocs_copied *next;
631 /* A section in dynobj. */
632 asection *section;
633 /* Number of relocs copied in this section. */
634 bfd_size_type count;
635 };
636
637 /* Forward declaration. */
638 struct elf_m68k_got_entry;
639
640 /* m68k ELF linker hash entry. */
641
642 struct elf_m68k_link_hash_entry
643 {
644 struct elf_link_hash_entry root;
645
646 /* Number of PC relative relocs copied for this symbol. */
647 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
648
649 /* Key to got_entries. */
650 unsigned long got_entry_key;
651
652 /* List of GOT entries for this symbol. This list is build during
653 offset finalization and is used within elf_m68k_finish_dynamic_symbol
654 to traverse all GOT entries for a particular symbol.
655
656 ??? We could've used root.got.glist field instead, but having
657 a separate field is cleaner. */
658 struct elf_m68k_got_entry *glist;
659 };
660
661 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
662
663 /* Key part of GOT entry in hashtable. */
664 struct elf_m68k_got_entry_key
665 {
666 /* BFD in which this symbol was defined. NULL for global symbols. */
667 const bfd *bfd;
668
669 /* Symbol index. Either local symbol index or h->got_entry_key. */
670 unsigned long symndx;
671
672 /* Type is one of R_68K_GOT{8, 16, 32}O, R_68K_TLS_GD{8, 16, 32},
673 R_68K_TLS_LDM{8, 16, 32} or R_68K_TLS_IE{8, 16, 32}.
674
675 From perspective of hashtable key, only elf_m68k_got_reloc_type (type)
676 matters. That is, we distinguish between, say, R_68K_GOT16O
677 and R_68K_GOT32O when allocating offsets, but they are considered to be
678 the same when searching got->entries. */
679 enum elf_m68k_reloc_type type;
680 };
681
682 /* Size of the GOT offset suitable for relocation. */
683 enum elf_m68k_got_offset_size { R_8, R_16, R_32, R_LAST };
684
685 /* Entry of the GOT. */
686 struct elf_m68k_got_entry
687 {
688 /* GOT entries are put into a got->entries hashtable. This is the key. */
689 struct elf_m68k_got_entry_key key_;
690
691 /* GOT entry data. We need s1 before offset finalization and s2 after. */
692 union
693 {
694 struct
695 {
696 /* Number of times this entry is referenced. It is used to
697 filter out unnecessary GOT slots in elf_m68k_gc_sweep_hook. */
698 bfd_vma refcount;
699 } s1;
700
701 struct
702 {
703 /* Offset from the start of .got section. To calculate offset relative
704 to GOT pointer one should substract got->offset from this value. */
705 bfd_vma offset;
706
707 /* Pointer to the next GOT entry for this global symbol.
708 Symbols have at most one entry in one GOT, but might
709 have entries in more than one GOT.
710 Root of this list is h->glist.
711 NULL for local symbols. */
712 struct elf_m68k_got_entry *next;
713 } s2;
714 } u;
715 };
716
717 /* Return representative type for relocation R_TYPE.
718 This is used to avoid enumerating many relocations in comparisons,
719 switches etc. */
720
721 static enum elf_m68k_reloc_type
722 elf_m68k_reloc_got_type (enum elf_m68k_reloc_type r_type)
723 {
724 switch (r_type)
725 {
726 /* In most cases R_68K_GOTx relocations require the very same
727 handling as R_68K_GOT32O relocation. In cases when we need
728 to distinguish between the two, we use explicitly compare against
729 r_type. */
730 case R_68K_GOT32:
731 case R_68K_GOT16:
732 case R_68K_GOT8:
733 case R_68K_GOT32O:
734 case R_68K_GOT16O:
735 case R_68K_GOT8O:
736 return R_68K_GOT32O;
737
738 case R_68K_TLS_GD32:
739 case R_68K_TLS_GD16:
740 case R_68K_TLS_GD8:
741 return R_68K_TLS_GD32;
742
743 case R_68K_TLS_LDM32:
744 case R_68K_TLS_LDM16:
745 case R_68K_TLS_LDM8:
746 return R_68K_TLS_LDM32;
747
748 case R_68K_TLS_IE32:
749 case R_68K_TLS_IE16:
750 case R_68K_TLS_IE8:
751 return R_68K_TLS_IE32;
752
753 default:
754 BFD_ASSERT (FALSE);
755 return 0;
756 }
757 }
758
759 /* Return size of the GOT entry offset for relocation R_TYPE. */
760
761 static enum elf_m68k_got_offset_size
762 elf_m68k_reloc_got_offset_size (enum elf_m68k_reloc_type r_type)
763 {
764 switch (r_type)
765 {
766 case R_68K_GOT32: case R_68K_GOT16: case R_68K_GOT8:
767 case R_68K_GOT32O: case R_68K_TLS_GD32: case R_68K_TLS_LDM32:
768 case R_68K_TLS_IE32:
769 return R_32;
770
771 case R_68K_GOT16O: case R_68K_TLS_GD16: case R_68K_TLS_LDM16:
772 case R_68K_TLS_IE16:
773 return R_16;
774
775 case R_68K_GOT8O: case R_68K_TLS_GD8: case R_68K_TLS_LDM8:
776 case R_68K_TLS_IE8:
777 return R_8;
778
779 default:
780 BFD_ASSERT (FALSE);
781 return 0;
782 }
783 }
784
785 /* Return number of GOT entries we need to allocate in GOT for
786 relocation R_TYPE. */
787
788 static bfd_vma
789 elf_m68k_reloc_got_n_slots (enum elf_m68k_reloc_type r_type)
790 {
791 switch (elf_m68k_reloc_got_type (r_type))
792 {
793 case R_68K_GOT32O:
794 case R_68K_TLS_IE32:
795 return 1;
796
797 case R_68K_TLS_GD32:
798 case R_68K_TLS_LDM32:
799 return 2;
800
801 default:
802 BFD_ASSERT (FALSE);
803 return 0;
804 }
805 }
806
807 /* Return TRUE if relocation R_TYPE is a TLS one. */
808
809 static bfd_boolean
810 elf_m68k_reloc_tls_p (enum elf_m68k_reloc_type r_type)
811 {
812 switch (r_type)
813 {
814 case R_68K_TLS_GD32: case R_68K_TLS_GD16: case R_68K_TLS_GD8:
815 case R_68K_TLS_LDM32: case R_68K_TLS_LDM16: case R_68K_TLS_LDM8:
816 case R_68K_TLS_LDO32: case R_68K_TLS_LDO16: case R_68K_TLS_LDO8:
817 case R_68K_TLS_IE32: case R_68K_TLS_IE16: case R_68K_TLS_IE8:
818 case R_68K_TLS_LE32: case R_68K_TLS_LE16: case R_68K_TLS_LE8:
819 case R_68K_TLS_DTPMOD32: case R_68K_TLS_DTPREL32: case R_68K_TLS_TPREL32:
820 return TRUE;
821
822 default:
823 return FALSE;
824 }
825 }
826
827 /* Data structure representing a single GOT. */
828 struct elf_m68k_got
829 {
830 /* Hashtable of 'struct elf_m68k_got_entry's.
831 Starting size of this table is the maximum number of
832 R_68K_GOT8O entries. */
833 htab_t entries;
834
835 /* Number of R_x slots in this GOT. Some (e.g., TLS) entries require
836 several GOT slots.
837
838 n_slots[R_8] is the count of R_8 slots in this GOT.
839 n_slots[R_16] is the cumulative count of R_8 and R_16 slots
840 in this GOT.
841 n_slots[R_32] is the cumulative count of R_8, R_16 and R_32 slots
842 in this GOT. This is the total number of slots. */
843 bfd_vma n_slots[R_LAST];
844
845 /* Number of local (entry->key_.h == NULL) slots in this GOT.
846 This is only used to properly calculate size of .rela.got section;
847 see elf_m68k_partition_multi_got. */
848 bfd_vma local_n_slots;
849
850 /* Offset of this GOT relative to beginning of .got section. */
851 bfd_vma offset;
852 };
853
854 /* BFD and its GOT. This is an entry in multi_got->bfd2got hashtable. */
855 struct elf_m68k_bfd2got_entry
856 {
857 /* BFD. */
858 const bfd *bfd;
859
860 /* Assigned GOT. Before partitioning multi-GOT each BFD has its own
861 GOT structure. After partitioning several BFD's might [and often do]
862 share a single GOT. */
863 struct elf_m68k_got *got;
864 };
865
866 /* The main data structure holding all the pieces. */
867 struct elf_m68k_multi_got
868 {
869 /* Hashtable mapping each BFD to its GOT. If a BFD doesn't have an entry
870 here, then it doesn't need a GOT (this includes the case of a BFD
871 having an empty GOT).
872
873 ??? This hashtable can be replaced by an array indexed by bfd->id. */
874 htab_t bfd2got;
875
876 /* Next symndx to assign a global symbol.
877 h->got_entry_key is initialized from this counter. */
878 unsigned long global_symndx;
879 };
880
881 /* m68k ELF linker hash table. */
882
883 struct elf_m68k_link_hash_table
884 {
885 struct elf_link_hash_table root;
886
887 /* Small local sym cache. */
888 struct sym_cache sym_cache;
889
890 /* The PLT format used by this link, or NULL if the format has not
891 yet been chosen. */
892 const struct elf_m68k_plt_info *plt_info;
893
894 /* True, if GP is loaded within each function which uses it.
895 Set to TRUE when GOT negative offsets or multi-GOT is enabled. */
896 bfd_boolean local_gp_p;
897
898 /* Switch controlling use of negative offsets to double the size of GOTs. */
899 bfd_boolean use_neg_got_offsets_p;
900
901 /* Switch controlling generation of multiple GOTs. */
902 bfd_boolean allow_multigot_p;
903
904 /* Multi-GOT data structure. */
905 struct elf_m68k_multi_got multi_got_;
906 };
907
908 /* Get the m68k ELF linker hash table from a link_info structure. */
909
910 #define elf_m68k_hash_table(p) \
911 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
912 == M68K_ELF_DATA ? ((struct elf_m68k_link_hash_table *) ((p)->hash)) : NULL)
913
914 /* Shortcut to multi-GOT data. */
915 #define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
916
917 /* Create an entry in an m68k ELF linker hash table. */
918
919 static struct bfd_hash_entry *
920 elf_m68k_link_hash_newfunc (struct bfd_hash_entry *entry,
921 struct bfd_hash_table *table,
922 const char *string)
923 {
924 struct bfd_hash_entry *ret = entry;
925
926 /* Allocate the structure if it has not already been allocated by a
927 subclass. */
928 if (ret == NULL)
929 ret = bfd_hash_allocate (table,
930 sizeof (struct elf_m68k_link_hash_entry));
931 if (ret == NULL)
932 return ret;
933
934 /* Call the allocation method of the superclass. */
935 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
936 if (ret != NULL)
937 {
938 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
939 elf_m68k_hash_entry (ret)->got_entry_key = 0;
940 elf_m68k_hash_entry (ret)->glist = NULL;
941 }
942
943 return ret;
944 }
945
946 /* Create an m68k ELF linker hash table. */
947
948 static struct bfd_link_hash_table *
949 elf_m68k_link_hash_table_create (bfd *abfd)
950 {
951 struct elf_m68k_link_hash_table *ret;
952 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
953
954 ret = (struct elf_m68k_link_hash_table *) bfd_zmalloc (amt);
955 if (ret == (struct elf_m68k_link_hash_table *) NULL)
956 return NULL;
957
958 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
959 elf_m68k_link_hash_newfunc,
960 sizeof (struct elf_m68k_link_hash_entry),
961 M68K_ELF_DATA))
962 {
963 free (ret);
964 return NULL;
965 }
966
967 ret->multi_got_.global_symndx = 1;
968
969 return &ret->root.root;
970 }
971
972 /* Destruct local data. */
973
974 static void
975 elf_m68k_link_hash_table_free (struct bfd_link_hash_table *_htab)
976 {
977 struct elf_m68k_link_hash_table *htab;
978
979 htab = (struct elf_m68k_link_hash_table *) _htab;
980
981 if (htab->multi_got_.bfd2got != NULL)
982 {
983 htab_delete (htab->multi_got_.bfd2got);
984 htab->multi_got_.bfd2got = NULL;
985 }
986 _bfd_elf_link_hash_table_free (_htab);
987 }
988
989 /* Set the right machine number. */
990
991 static bfd_boolean
992 elf32_m68k_object_p (bfd *abfd)
993 {
994 unsigned int mach = 0;
995 unsigned features = 0;
996 flagword eflags = elf_elfheader (abfd)->e_flags;
997
998 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
999 features |= m68000;
1000 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1001 features |= cpu32;
1002 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1003 features |= fido_a;
1004 else
1005 {
1006 switch (eflags & EF_M68K_CF_ISA_MASK)
1007 {
1008 case EF_M68K_CF_ISA_A_NODIV:
1009 features |= mcfisa_a;
1010 break;
1011 case EF_M68K_CF_ISA_A:
1012 features |= mcfisa_a|mcfhwdiv;
1013 break;
1014 case EF_M68K_CF_ISA_A_PLUS:
1015 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
1016 break;
1017 case EF_M68K_CF_ISA_B_NOUSP:
1018 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
1019 break;
1020 case EF_M68K_CF_ISA_B:
1021 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
1022 break;
1023 case EF_M68K_CF_ISA_C:
1024 features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
1025 break;
1026 case EF_M68K_CF_ISA_C_NODIV:
1027 features |= mcfisa_a|mcfisa_c|mcfusp;
1028 break;
1029 }
1030 switch (eflags & EF_M68K_CF_MAC_MASK)
1031 {
1032 case EF_M68K_CF_MAC:
1033 features |= mcfmac;
1034 break;
1035 case EF_M68K_CF_EMAC:
1036 features |= mcfemac;
1037 break;
1038 }
1039 if (eflags & EF_M68K_CF_FLOAT)
1040 features |= cfloat;
1041 }
1042
1043 mach = bfd_m68k_features_to_mach (features);
1044 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
1045
1046 return TRUE;
1047 }
1048
1049 /* Somewhat reverse of elf32_m68k_object_p, this sets the e_flag
1050 field based on the machine number. */
1051
1052 static void
1053 elf_m68k_final_write_processing (bfd *abfd,
1054 bfd_boolean linker ATTRIBUTE_UNUSED)
1055 {
1056 int mach = bfd_get_mach (abfd);
1057 unsigned long e_flags = elf_elfheader (abfd)->e_flags;
1058
1059 if (!e_flags)
1060 {
1061 unsigned int arch_mask;
1062
1063 arch_mask = bfd_m68k_mach_to_features (mach);
1064
1065 if (arch_mask & m68000)
1066 e_flags = EF_M68K_M68000;
1067 else if (arch_mask & cpu32)
1068 e_flags = EF_M68K_CPU32;
1069 else if (arch_mask & fido_a)
1070 e_flags = EF_M68K_FIDO;
1071 else
1072 {
1073 switch (arch_mask
1074 & (mcfisa_a | mcfisa_aa | mcfisa_b | mcfisa_c | mcfhwdiv | mcfusp))
1075 {
1076 case mcfisa_a:
1077 e_flags |= EF_M68K_CF_ISA_A_NODIV;
1078 break;
1079 case mcfisa_a | mcfhwdiv:
1080 e_flags |= EF_M68K_CF_ISA_A;
1081 break;
1082 case mcfisa_a | mcfisa_aa | mcfhwdiv | mcfusp:
1083 e_flags |= EF_M68K_CF_ISA_A_PLUS;
1084 break;
1085 case mcfisa_a | mcfisa_b | mcfhwdiv:
1086 e_flags |= EF_M68K_CF_ISA_B_NOUSP;
1087 break;
1088 case mcfisa_a | mcfisa_b | mcfhwdiv | mcfusp:
1089 e_flags |= EF_M68K_CF_ISA_B;
1090 break;
1091 case mcfisa_a | mcfisa_c | mcfhwdiv | mcfusp:
1092 e_flags |= EF_M68K_CF_ISA_C;
1093 break;
1094 case mcfisa_a | mcfisa_c | mcfusp:
1095 e_flags |= EF_M68K_CF_ISA_C_NODIV;
1096 break;
1097 }
1098 if (arch_mask & mcfmac)
1099 e_flags |= EF_M68K_CF_MAC;
1100 else if (arch_mask & mcfemac)
1101 e_flags |= EF_M68K_CF_EMAC;
1102 if (arch_mask & cfloat)
1103 e_flags |= EF_M68K_CF_FLOAT | EF_M68K_CFV4E;
1104 }
1105 elf_elfheader (abfd)->e_flags = e_flags;
1106 }
1107 }
1108
1109 /* Keep m68k-specific flags in the ELF header. */
1110
1111 static bfd_boolean
1112 elf32_m68k_set_private_flags (bfd *abfd, flagword flags)
1113 {
1114 elf_elfheader (abfd)->e_flags = flags;
1115 elf_flags_init (abfd) = TRUE;
1116 return TRUE;
1117 }
1118
1119 /* Merge backend specific data from an object file to the output
1120 object file when linking. */
1121 static bfd_boolean
1122 elf32_m68k_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1123 {
1124 flagword out_flags;
1125 flagword in_flags;
1126 flagword out_isa;
1127 flagword in_isa;
1128 const bfd_arch_info_type *arch_info;
1129
1130 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1131 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1132 return FALSE;
1133
1134 /* Get the merged machine. This checks for incompatibility between
1135 Coldfire & non-Coldfire flags, incompability between different
1136 Coldfire ISAs, and incompability between different MAC types. */
1137 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
1138 if (!arch_info)
1139 return FALSE;
1140
1141 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
1142
1143 in_flags = elf_elfheader (ibfd)->e_flags;
1144 if (!elf_flags_init (obfd))
1145 {
1146 elf_flags_init (obfd) = TRUE;
1147 out_flags = in_flags;
1148 }
1149 else
1150 {
1151 out_flags = elf_elfheader (obfd)->e_flags;
1152 unsigned int variant_mask;
1153
1154 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1155 variant_mask = 0;
1156 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1157 variant_mask = 0;
1158 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1159 variant_mask = 0;
1160 else
1161 variant_mask = EF_M68K_CF_ISA_MASK;
1162
1163 in_isa = (in_flags & variant_mask);
1164 out_isa = (out_flags & variant_mask);
1165 if (in_isa > out_isa)
1166 out_flags ^= in_isa ^ out_isa;
1167 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
1168 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1169 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
1170 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
1171 out_flags = EF_M68K_FIDO;
1172 else
1173 out_flags |= in_flags ^ in_isa;
1174 }
1175 elf_elfheader (obfd)->e_flags = out_flags;
1176
1177 return TRUE;
1178 }
1179
1180 /* Display the flags field. */
1181
1182 static bfd_boolean
1183 elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
1184 {
1185 FILE *file = (FILE *) ptr;
1186 flagword eflags = elf_elfheader (abfd)->e_flags;
1187
1188 BFD_ASSERT (abfd != NULL && ptr != NULL);
1189
1190 /* Print normal ELF private data. */
1191 _bfd_elf_print_private_bfd_data (abfd, ptr);
1192
1193 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
1194
1195 /* xgettext:c-format */
1196 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1197
1198 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1199 fprintf (file, " [m68000]");
1200 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1201 fprintf (file, " [cpu32]");
1202 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1203 fprintf (file, " [fido]");
1204 else
1205 {
1206 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
1207 fprintf (file, " [cfv4e]");
1208
1209 if (eflags & EF_M68K_CF_ISA_MASK)
1210 {
1211 char const *isa = _("unknown");
1212 char const *mac = _("unknown");
1213 char const *additional = "";
1214
1215 switch (eflags & EF_M68K_CF_ISA_MASK)
1216 {
1217 case EF_M68K_CF_ISA_A_NODIV:
1218 isa = "A";
1219 additional = " [nodiv]";
1220 break;
1221 case EF_M68K_CF_ISA_A:
1222 isa = "A";
1223 break;
1224 case EF_M68K_CF_ISA_A_PLUS:
1225 isa = "A+";
1226 break;
1227 case EF_M68K_CF_ISA_B_NOUSP:
1228 isa = "B";
1229 additional = " [nousp]";
1230 break;
1231 case EF_M68K_CF_ISA_B:
1232 isa = "B";
1233 break;
1234 case EF_M68K_CF_ISA_C:
1235 isa = "C";
1236 break;
1237 case EF_M68K_CF_ISA_C_NODIV:
1238 isa = "C";
1239 additional = " [nodiv]";
1240 break;
1241 }
1242 fprintf (file, " [isa %s]%s", isa, additional);
1243
1244 if (eflags & EF_M68K_CF_FLOAT)
1245 fprintf (file, " [float]");
1246
1247 switch (eflags & EF_M68K_CF_MAC_MASK)
1248 {
1249 case 0:
1250 mac = NULL;
1251 break;
1252 case EF_M68K_CF_MAC:
1253 mac = "mac";
1254 break;
1255 case EF_M68K_CF_EMAC:
1256 mac = "emac";
1257 break;
1258 case EF_M68K_CF_EMAC_B:
1259 mac = "emac_b";
1260 break;
1261 }
1262 if (mac)
1263 fprintf (file, " [%s]", mac);
1264 }
1265 }
1266
1267 fputc ('\n', file);
1268
1269 return TRUE;
1270 }
1271
1272 /* Multi-GOT support implementation design:
1273
1274 Multi-GOT starts in check_relocs hook. There we scan all
1275 relocations of a BFD and build a local GOT (struct elf_m68k_got)
1276 for it. If a single BFD appears to require too many GOT slots with
1277 R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
1278 to user.
1279 After check_relocs has been invoked for each input BFD, we have
1280 constructed a GOT for each input BFD.
1281
1282 To minimize total number of GOTs required for a particular output BFD
1283 (as some environments support only 1 GOT per output object) we try
1284 to merge some of the GOTs to share an offset space. Ideally [and in most
1285 cases] we end up with a single GOT. In cases when there are too many
1286 restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
1287 several GOTs, assuming the environment can handle them.
1288
1289 Partitioning is done in elf_m68k_partition_multi_got. We start with
1290 an empty GOT and traverse bfd2got hashtable putting got_entries from
1291 local GOTs to the new 'big' one. We do that by constructing an
1292 intermediate GOT holding all the entries the local GOT has and the big
1293 GOT lacks. Then we check if there is room in the big GOT to accomodate
1294 all the entries from diff. On success we add those entries to the big
1295 GOT; on failure we start the new 'big' GOT and retry the adding of
1296 entries from the local GOT. Note that this retry will always succeed as
1297 each local GOT doesn't overflow the limits. After partitioning we
1298 end up with each bfd assigned one of the big GOTs. GOT entries in the
1299 big GOTs are initialized with GOT offsets. Note that big GOTs are
1300 positioned consequently in program space and represent a single huge GOT
1301 to the outside world.
1302
1303 After that we get to elf_m68k_relocate_section. There we
1304 adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
1305 relocations to refer to appropriate [assigned to current input_bfd]
1306 big GOT.
1307
1308 Notes:
1309
1310 GOT entry type: We have several types of GOT entries.
1311 * R_8 type is used in entries for symbols that have at least one
1312 R_68K_GOT8O or R_68K_TLS_*8 relocation. We can have at most 0x40
1313 such entries in one GOT.
1314 * R_16 type is used in entries for symbols that have at least one
1315 R_68K_GOT16O or R_68K_TLS_*16 relocation and no R_8 relocations.
1316 We can have at most 0x4000 such entries in one GOT.
1317 * R_32 type is used in all other cases. We can have as many
1318 such entries in one GOT as we'd like.
1319 When counting relocations we have to include the count of the smaller
1320 ranged relocations in the counts of the larger ranged ones in order
1321 to correctly detect overflow.
1322
1323 Sorting the GOT: In each GOT starting offsets are assigned to
1324 R_8 entries, which are followed by R_16 entries, and
1325 R_32 entries go at the end. See finalize_got_offsets for details.
1326
1327 Negative GOT offsets: To double usable offset range of GOTs we use
1328 negative offsets. As we assign entries with GOT offsets relative to
1329 start of .got section, the offset values are positive. They become
1330 negative only in relocate_section where got->offset value is
1331 subtracted from them.
1332
1333 3 special GOT entries: There are 3 special GOT entries used internally
1334 by loader. These entries happen to be placed to .got.plt section,
1335 so we don't do anything about them in multi-GOT support.
1336
1337 Memory management: All data except for hashtables
1338 multi_got->bfd2got and got->entries are allocated on
1339 elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
1340 to most functions), so we don't need to care to free them. At the
1341 moment of allocation hashtables are being linked into main data
1342 structure (multi_got), all pieces of which are reachable from
1343 elf_m68k_multi_got (info). We deallocate them in
1344 elf_m68k_link_hash_table_free. */
1345
1346 /* Initialize GOT. */
1347
1348 static void
1349 elf_m68k_init_got (struct elf_m68k_got *got)
1350 {
1351 got->entries = NULL;
1352 got->n_slots[R_8] = 0;
1353 got->n_slots[R_16] = 0;
1354 got->n_slots[R_32] = 0;
1355 got->local_n_slots = 0;
1356 got->offset = (bfd_vma) -1;
1357 }
1358
1359 /* Destruct GOT. */
1360
1361 static void
1362 elf_m68k_clear_got (struct elf_m68k_got *got)
1363 {
1364 if (got->entries != NULL)
1365 {
1366 htab_delete (got->entries);
1367 got->entries = NULL;
1368 }
1369 }
1370
1371 /* Create and empty GOT structure. INFO is the context where memory
1372 should be allocated. */
1373
1374 static struct elf_m68k_got *
1375 elf_m68k_create_empty_got (struct bfd_link_info *info)
1376 {
1377 struct elf_m68k_got *got;
1378
1379 got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
1380 if (got == NULL)
1381 return NULL;
1382
1383 elf_m68k_init_got (got);
1384
1385 return got;
1386 }
1387
1388 /* Initialize KEY. */
1389
1390 static void
1391 elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
1392 struct elf_link_hash_entry *h,
1393 const bfd *abfd, unsigned long symndx,
1394 enum elf_m68k_reloc_type reloc_type)
1395 {
1396 if (elf_m68k_reloc_got_type (reloc_type) == R_68K_TLS_LDM32)
1397 /* All TLS_LDM relocations share a single GOT entry. */
1398 {
1399 key->bfd = NULL;
1400 key->symndx = 0;
1401 }
1402 else if (h != NULL)
1403 /* Global symbols are identified with their got_entry_key. */
1404 {
1405 key->bfd = NULL;
1406 key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
1407 BFD_ASSERT (key->symndx != 0);
1408 }
1409 else
1410 /* Local symbols are identified by BFD they appear in and symndx. */
1411 {
1412 key->bfd = abfd;
1413 key->symndx = symndx;
1414 }
1415
1416 key->type = reloc_type;
1417 }
1418
1419 /* Calculate hash of got_entry.
1420 ??? Is it good? */
1421
1422 static hashval_t
1423 elf_m68k_got_entry_hash (const void *_entry)
1424 {
1425 const struct elf_m68k_got_entry_key *key;
1426
1427 key = &((const struct elf_m68k_got_entry *) _entry)->key_;
1428
1429 return (key->symndx
1430 + (key->bfd != NULL ? (int) key->bfd->id : -1)
1431 + elf_m68k_reloc_got_type (key->type));
1432 }
1433
1434 /* Check if two got entries are equal. */
1435
1436 static int
1437 elf_m68k_got_entry_eq (const void *_entry1, const void *_entry2)
1438 {
1439 const struct elf_m68k_got_entry_key *key1;
1440 const struct elf_m68k_got_entry_key *key2;
1441
1442 key1 = &((const struct elf_m68k_got_entry *) _entry1)->key_;
1443 key2 = &((const struct elf_m68k_got_entry *) _entry2)->key_;
1444
1445 return (key1->bfd == key2->bfd
1446 && key1->symndx == key2->symndx
1447 && (elf_m68k_reloc_got_type (key1->type)
1448 == elf_m68k_reloc_got_type (key2->type)));
1449 }
1450
1451 /* When using negative offsets, we allocate one extra R_8, one extra R_16
1452 and one extra R_32 slots to simplify handling of 2-slot entries during
1453 offset allocation -- hence -1 for R_8 slots and -2 for R_16 slots. */
1454
1455 /* Maximal number of R_8 slots in a single GOT. */
1456 #define ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT(INFO) \
1457 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1458 ? (0x40 - 1) \
1459 : 0x20)
1460
1461 /* Maximal number of R_8 and R_16 slots in a single GOT. */
1462 #define ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT(INFO) \
1463 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1464 ? (0x4000 - 2) \
1465 : 0x2000)
1466
1467 /* SEARCH - simply search the hashtable, don't insert new entries or fail when
1468 the entry cannot be found.
1469 FIND_OR_CREATE - search for an existing entry, but create new if there's
1470 no such.
1471 MUST_FIND - search for an existing entry and assert that it exist.
1472 MUST_CREATE - assert that there's no such entry and create new one. */
1473 enum elf_m68k_get_entry_howto
1474 {
1475 SEARCH,
1476 FIND_OR_CREATE,
1477 MUST_FIND,
1478 MUST_CREATE
1479 };
1480
1481 /* Get or create (depending on HOWTO) entry with KEY in GOT.
1482 INFO is context in which memory should be allocated (can be NULL if
1483 HOWTO is SEARCH or MUST_FIND). */
1484
1485 static struct elf_m68k_got_entry *
1486 elf_m68k_get_got_entry (struct elf_m68k_got *got,
1487 const struct elf_m68k_got_entry_key *key,
1488 enum elf_m68k_get_entry_howto howto,
1489 struct bfd_link_info *info)
1490 {
1491 struct elf_m68k_got_entry entry_;
1492 struct elf_m68k_got_entry *entry;
1493 void **ptr;
1494
1495 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1496
1497 if (got->entries == NULL)
1498 /* This is the first entry in ABFD. Initialize hashtable. */
1499 {
1500 if (howto == SEARCH)
1501 return NULL;
1502
1503 got->entries = htab_try_create (ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT
1504 (info),
1505 elf_m68k_got_entry_hash,
1506 elf_m68k_got_entry_eq, NULL);
1507 if (got->entries == NULL)
1508 {
1509 bfd_set_error (bfd_error_no_memory);
1510 return NULL;
1511 }
1512 }
1513
1514 entry_.key_ = *key;
1515 ptr = htab_find_slot (got->entries, &entry_, (howto != SEARCH
1516 ? INSERT : NO_INSERT));
1517 if (ptr == NULL)
1518 {
1519 if (howto == SEARCH)
1520 /* Entry not found. */
1521 return NULL;
1522
1523 /* We're out of memory. */
1524 bfd_set_error (bfd_error_no_memory);
1525 return NULL;
1526 }
1527
1528 if (*ptr == NULL)
1529 /* We didn't find the entry and we're asked to create a new one. */
1530 {
1531 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1532
1533 entry = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry));
1534 if (entry == NULL)
1535 return NULL;
1536
1537 /* Initialize new entry. */
1538 entry->key_ = *key;
1539
1540 entry->u.s1.refcount = 0;
1541
1542 /* Mark the entry as not initialized. */
1543 entry->key_.type = R_68K_max;
1544
1545 *ptr = entry;
1546 }
1547 else
1548 /* We found the entry. */
1549 {
1550 BFD_ASSERT (howto != MUST_CREATE);
1551
1552 entry = *ptr;
1553 }
1554
1555 return entry;
1556 }
1557
1558 /* Update GOT counters when merging entry of WAS type with entry of NEW type.
1559 Return the value to which ENTRY's type should be set. */
1560
1561 static enum elf_m68k_reloc_type
1562 elf_m68k_update_got_entry_type (struct elf_m68k_got *got,
1563 enum elf_m68k_reloc_type was,
1564 enum elf_m68k_reloc_type new_reloc)
1565 {
1566 enum elf_m68k_got_offset_size was_size;
1567 enum elf_m68k_got_offset_size new_size;
1568 bfd_vma n_slots;
1569
1570 if (was == R_68K_max)
1571 /* The type of the entry is not initialized yet. */
1572 {
1573 /* Update all got->n_slots counters, including n_slots[R_32]. */
1574 was_size = R_LAST;
1575
1576 was = new_reloc;
1577 }
1578 else
1579 {
1580 /* !!! We, probably, should emit an error rather then fail on assert
1581 in such a case. */
1582 BFD_ASSERT (elf_m68k_reloc_got_type (was)
1583 == elf_m68k_reloc_got_type (new_reloc));
1584
1585 was_size = elf_m68k_reloc_got_offset_size (was);
1586 }
1587
1588 new_size = elf_m68k_reloc_got_offset_size (new_reloc);
1589 n_slots = elf_m68k_reloc_got_n_slots (new_reloc);
1590
1591 while (was_size > new_size)
1592 {
1593 --was_size;
1594 got->n_slots[was_size] += n_slots;
1595 }
1596
1597 if (new_reloc > was)
1598 /* Relocations are ordered from bigger got offset size to lesser,
1599 so choose the relocation type with lesser offset size. */
1600 was = new_reloc;
1601
1602 return was;
1603 }
1604
1605 /* Update GOT counters when removing an entry of type TYPE. */
1606
1607 static void
1608 elf_m68k_remove_got_entry_type (struct elf_m68k_got *got,
1609 enum elf_m68k_reloc_type type)
1610 {
1611 enum elf_m68k_got_offset_size os;
1612 bfd_vma n_slots;
1613
1614 n_slots = elf_m68k_reloc_got_n_slots (type);
1615
1616 /* Decrese counter of slots with offset size corresponding to TYPE
1617 and all greater offset sizes. */
1618 for (os = elf_m68k_reloc_got_offset_size (type); os <= R_32; ++os)
1619 {
1620 BFD_ASSERT (got->n_slots[os] >= n_slots);
1621
1622 got->n_slots[os] -= n_slots;
1623 }
1624 }
1625
1626 /* Add new or update existing entry to GOT.
1627 H, ABFD, TYPE and SYMNDX is data for the entry.
1628 INFO is a context where memory should be allocated. */
1629
1630 static struct elf_m68k_got_entry *
1631 elf_m68k_add_entry_to_got (struct elf_m68k_got *got,
1632 struct elf_link_hash_entry *h,
1633 const bfd *abfd,
1634 enum elf_m68k_reloc_type reloc_type,
1635 unsigned long symndx,
1636 struct bfd_link_info *info)
1637 {
1638 struct elf_m68k_got_entry_key key_;
1639 struct elf_m68k_got_entry *entry;
1640
1641 if (h != NULL && elf_m68k_hash_entry (h)->got_entry_key == 0)
1642 elf_m68k_hash_entry (h)->got_entry_key
1643 = elf_m68k_multi_got (info)->global_symndx++;
1644
1645 elf_m68k_init_got_entry_key (&key_, h, abfd, symndx, reloc_type);
1646
1647 entry = elf_m68k_get_got_entry (got, &key_, FIND_OR_CREATE, info);
1648 if (entry == NULL)
1649 return NULL;
1650
1651 /* Determine entry's type and update got->n_slots counters. */
1652 entry->key_.type = elf_m68k_update_got_entry_type (got,
1653 entry->key_.type,
1654 reloc_type);
1655
1656 /* Update refcount. */
1657 ++entry->u.s1.refcount;
1658
1659 if (entry->u.s1.refcount == 1)
1660 /* We see this entry for the first time. */
1661 {
1662 if (entry->key_.bfd != NULL)
1663 got->local_n_slots += elf_m68k_reloc_got_n_slots (entry->key_.type);
1664 }
1665
1666 BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
1667
1668 if ((got->n_slots[R_8]
1669 > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1670 || (got->n_slots[R_16]
1671 > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1672 /* This BFD has too many relocation. */
1673 {
1674 if (got->n_slots[R_8] > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1675 (*_bfd_error_handler) (_("%B: GOT overflow: "
1676 "Number of relocations with 8-bit "
1677 "offset > %d"),
1678 abfd,
1679 ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info));
1680 else
1681 (*_bfd_error_handler) (_("%B: GOT overflow: "
1682 "Number of relocations with 8- or 16-bit "
1683 "offset > %d"),
1684 abfd,
1685 ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info));
1686
1687 return NULL;
1688 }
1689
1690 return entry;
1691 }
1692
1693 /* Compute the hash value of the bfd in a bfd2got hash entry. */
1694
1695 static hashval_t
1696 elf_m68k_bfd2got_entry_hash (const void *entry)
1697 {
1698 const struct elf_m68k_bfd2got_entry *e;
1699
1700 e = (const struct elf_m68k_bfd2got_entry *) entry;
1701
1702 return e->bfd->id;
1703 }
1704
1705 /* Check whether two hash entries have the same bfd. */
1706
1707 static int
1708 elf_m68k_bfd2got_entry_eq (const void *entry1, const void *entry2)
1709 {
1710 const struct elf_m68k_bfd2got_entry *e1;
1711 const struct elf_m68k_bfd2got_entry *e2;
1712
1713 e1 = (const struct elf_m68k_bfd2got_entry *) entry1;
1714 e2 = (const struct elf_m68k_bfd2got_entry *) entry2;
1715
1716 return e1->bfd == e2->bfd;
1717 }
1718
1719 /* Destruct a bfd2got entry. */
1720
1721 static void
1722 elf_m68k_bfd2got_entry_del (void *_entry)
1723 {
1724 struct elf_m68k_bfd2got_entry *entry;
1725
1726 entry = (struct elf_m68k_bfd2got_entry *) _entry;
1727
1728 BFD_ASSERT (entry->got != NULL);
1729 elf_m68k_clear_got (entry->got);
1730 }
1731
1732 /* Find existing or create new (depending on HOWTO) bfd2got entry in
1733 MULTI_GOT. ABFD is the bfd we need a GOT for. INFO is a context where
1734 memory should be allocated. */
1735
1736 static struct elf_m68k_bfd2got_entry *
1737 elf_m68k_get_bfd2got_entry (struct elf_m68k_multi_got *multi_got,
1738 const bfd *abfd,
1739 enum elf_m68k_get_entry_howto howto,
1740 struct bfd_link_info *info)
1741 {
1742 struct elf_m68k_bfd2got_entry entry_;
1743 void **ptr;
1744 struct elf_m68k_bfd2got_entry *entry;
1745
1746 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1747
1748 if (multi_got->bfd2got == NULL)
1749 /* This is the first GOT. Initialize bfd2got. */
1750 {
1751 if (howto == SEARCH)
1752 return NULL;
1753
1754 multi_got->bfd2got = htab_try_create (1, elf_m68k_bfd2got_entry_hash,
1755 elf_m68k_bfd2got_entry_eq,
1756 elf_m68k_bfd2got_entry_del);
1757 if (multi_got->bfd2got == NULL)
1758 {
1759 bfd_set_error (bfd_error_no_memory);
1760 return NULL;
1761 }
1762 }
1763
1764 entry_.bfd = abfd;
1765 ptr = htab_find_slot (multi_got->bfd2got, &entry_, (howto != SEARCH
1766 ? INSERT : NO_INSERT));
1767 if (ptr == NULL)
1768 {
1769 if (howto == SEARCH)
1770 /* Entry not found. */
1771 return NULL;
1772
1773 /* We're out of memory. */
1774 bfd_set_error (bfd_error_no_memory);
1775 return NULL;
1776 }
1777
1778 if (*ptr == NULL)
1779 /* Entry was not found. Create new one. */
1780 {
1781 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1782
1783 entry = ((struct elf_m68k_bfd2got_entry *)
1784 bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry)));
1785 if (entry == NULL)
1786 return NULL;
1787
1788 entry->bfd = abfd;
1789
1790 entry->got = elf_m68k_create_empty_got (info);
1791 if (entry->got == NULL)
1792 return NULL;
1793
1794 *ptr = entry;
1795 }
1796 else
1797 {
1798 BFD_ASSERT (howto != MUST_CREATE);
1799
1800 /* Return existing entry. */
1801 entry = *ptr;
1802 }
1803
1804 return entry;
1805 }
1806
1807 struct elf_m68k_can_merge_gots_arg
1808 {
1809 /* A current_got that we constructing a DIFF against. */
1810 struct elf_m68k_got *big;
1811
1812 /* GOT holding entries not present or that should be changed in
1813 BIG. */
1814 struct elf_m68k_got *diff;
1815
1816 /* Context where to allocate memory. */
1817 struct bfd_link_info *info;
1818
1819 /* Error flag. */
1820 bfd_boolean error_p;
1821 };
1822
1823 /* Process a single entry from the small GOT to see if it should be added
1824 or updated in the big GOT. */
1825
1826 static int
1827 elf_m68k_can_merge_gots_1 (void **_entry_ptr, void *_arg)
1828 {
1829 const struct elf_m68k_got_entry *entry1;
1830 struct elf_m68k_can_merge_gots_arg *arg;
1831 const struct elf_m68k_got_entry *entry2;
1832 enum elf_m68k_reloc_type type;
1833
1834 entry1 = (const struct elf_m68k_got_entry *) *_entry_ptr;
1835 arg = (struct elf_m68k_can_merge_gots_arg *) _arg;
1836
1837 entry2 = elf_m68k_get_got_entry (arg->big, &entry1->key_, SEARCH, NULL);
1838
1839 if (entry2 != NULL)
1840 /* We found an existing entry. Check if we should update it. */
1841 {
1842 type = elf_m68k_update_got_entry_type (arg->diff,
1843 entry2->key_.type,
1844 entry1->key_.type);
1845
1846 if (type == entry2->key_.type)
1847 /* ENTRY1 doesn't update data in ENTRY2. Skip it.
1848 To skip creation of difference entry we use the type,
1849 which we won't see in GOT entries for sure. */
1850 type = R_68K_max;
1851 }
1852 else
1853 /* We didn't find the entry. Add entry1 to DIFF. */
1854 {
1855 BFD_ASSERT (entry1->key_.type != R_68K_max);
1856
1857 type = elf_m68k_update_got_entry_type (arg->diff,
1858 R_68K_max, entry1->key_.type);
1859
1860 if (entry1->key_.bfd != NULL)
1861 arg->diff->local_n_slots += elf_m68k_reloc_got_n_slots (type);
1862 }
1863
1864 if (type != R_68K_max)
1865 /* Create an entry in DIFF. */
1866 {
1867 struct elf_m68k_got_entry *entry;
1868
1869 entry = elf_m68k_get_got_entry (arg->diff, &entry1->key_, MUST_CREATE,
1870 arg->info);
1871 if (entry == NULL)
1872 {
1873 arg->error_p = TRUE;
1874 return 0;
1875 }
1876
1877 entry->key_.type = type;
1878 }
1879
1880 return 1;
1881 }
1882
1883 /* Return TRUE if SMALL GOT can be added to BIG GOT without overflowing it.
1884 Construct DIFF GOT holding the entries which should be added or updated
1885 in BIG GOT to accumulate information from SMALL.
1886 INFO is the context where memory should be allocated. */
1887
1888 static bfd_boolean
1889 elf_m68k_can_merge_gots (struct elf_m68k_got *big,
1890 const struct elf_m68k_got *small,
1891 struct bfd_link_info *info,
1892 struct elf_m68k_got *diff)
1893 {
1894 struct elf_m68k_can_merge_gots_arg arg_;
1895
1896 BFD_ASSERT (small->offset == (bfd_vma) -1);
1897
1898 arg_.big = big;
1899 arg_.diff = diff;
1900 arg_.info = info;
1901 arg_.error_p = FALSE;
1902 htab_traverse_noresize (small->entries, elf_m68k_can_merge_gots_1, &arg_);
1903 if (arg_.error_p)
1904 {
1905 diff->offset = 0;
1906 return FALSE;
1907 }
1908
1909 /* Check for overflow. */
1910 if ((big->n_slots[R_8] + arg_.diff->n_slots[R_8]
1911 > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1912 || (big->n_slots[R_16] + arg_.diff->n_slots[R_16]
1913 > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1914 return FALSE;
1915
1916 return TRUE;
1917 }
1918
1919 struct elf_m68k_merge_gots_arg
1920 {
1921 /* The BIG got. */
1922 struct elf_m68k_got *big;
1923
1924 /* Context where memory should be allocated. */
1925 struct bfd_link_info *info;
1926
1927 /* Error flag. */
1928 bfd_boolean error_p;
1929 };
1930
1931 /* Process a single entry from DIFF got. Add or update corresponding
1932 entry in the BIG got. */
1933
1934 static int
1935 elf_m68k_merge_gots_1 (void **entry_ptr, void *_arg)
1936 {
1937 const struct elf_m68k_got_entry *from;
1938 struct elf_m68k_merge_gots_arg *arg;
1939 struct elf_m68k_got_entry *to;
1940
1941 from = (const struct elf_m68k_got_entry *) *entry_ptr;
1942 arg = (struct elf_m68k_merge_gots_arg *) _arg;
1943
1944 to = elf_m68k_get_got_entry (arg->big, &from->key_, FIND_OR_CREATE,
1945 arg->info);
1946 if (to == NULL)
1947 {
1948 arg->error_p = TRUE;
1949 return 0;
1950 }
1951
1952 BFD_ASSERT (to->u.s1.refcount == 0);
1953 /* All we need to merge is TYPE. */
1954 to->key_.type = from->key_.type;
1955
1956 return 1;
1957 }
1958
1959 /* Merge data from DIFF to BIG. INFO is context where memory should be
1960 allocated. */
1961
1962 static bfd_boolean
1963 elf_m68k_merge_gots (struct elf_m68k_got *big,
1964 struct elf_m68k_got *diff,
1965 struct bfd_link_info *info)
1966 {
1967 if (diff->entries != NULL)
1968 /* DIFF is not empty. Merge it into BIG GOT. */
1969 {
1970 struct elf_m68k_merge_gots_arg arg_;
1971
1972 /* Merge entries. */
1973 arg_.big = big;
1974 arg_.info = info;
1975 arg_.error_p = FALSE;
1976 htab_traverse_noresize (diff->entries, elf_m68k_merge_gots_1, &arg_);
1977 if (arg_.error_p)
1978 return FALSE;
1979
1980 /* Merge counters. */
1981 big->n_slots[R_8] += diff->n_slots[R_8];
1982 big->n_slots[R_16] += diff->n_slots[R_16];
1983 big->n_slots[R_32] += diff->n_slots[R_32];
1984 big->local_n_slots += diff->local_n_slots;
1985 }
1986 else
1987 /* DIFF is empty. */
1988 {
1989 BFD_ASSERT (diff->n_slots[R_8] == 0);
1990 BFD_ASSERT (diff->n_slots[R_16] == 0);
1991 BFD_ASSERT (diff->n_slots[R_32] == 0);
1992 BFD_ASSERT (diff->local_n_slots == 0);
1993 }
1994
1995 BFD_ASSERT (!elf_m68k_hash_table (info)->allow_multigot_p
1996 || ((big->n_slots[R_8]
1997 <= ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1998 && (big->n_slots[R_16]
1999 <= ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info))));
2000
2001 return TRUE;
2002 }
2003
2004 struct elf_m68k_finalize_got_offsets_arg
2005 {
2006 /* Ranges of the offsets for GOT entries.
2007 R_x entries receive offsets between offset1[R_x] and offset2[R_x].
2008 R_x is R_8, R_16 and R_32. */
2009 bfd_vma *offset1;
2010 bfd_vma *offset2;
2011
2012 /* Mapping from global symndx to global symbols.
2013 This is used to build lists of got entries for global symbols. */
2014 struct elf_m68k_link_hash_entry **symndx2h;
2015
2016 bfd_vma n_ldm_entries;
2017 };
2018
2019 /* Assign ENTRY an offset. Build list of GOT entries for global symbols
2020 along the way. */
2021
2022 static int
2023 elf_m68k_finalize_got_offsets_1 (void **entry_ptr, void *_arg)
2024 {
2025 struct elf_m68k_got_entry *entry;
2026 struct elf_m68k_finalize_got_offsets_arg *arg;
2027
2028 enum elf_m68k_got_offset_size got_offset_size;
2029 bfd_vma entry_size;
2030
2031 entry = (struct elf_m68k_got_entry *) *entry_ptr;
2032 arg = (struct elf_m68k_finalize_got_offsets_arg *) _arg;
2033
2034 /* This should be a fresh entry created in elf_m68k_can_merge_gots. */
2035 BFD_ASSERT (entry->u.s1.refcount == 0);
2036
2037 /* Get GOT offset size for the entry . */
2038 got_offset_size = elf_m68k_reloc_got_offset_size (entry->key_.type);
2039
2040 /* Calculate entry size in bytes. */
2041 entry_size = 4 * elf_m68k_reloc_got_n_slots (entry->key_.type);
2042
2043 /* Check if we should switch to negative range of the offsets. */
2044 if (arg->offset1[got_offset_size] + entry_size
2045 > arg->offset2[got_offset_size])
2046 {
2047 /* Verify that this is the only switch to negative range for
2048 got_offset_size. If this assertion fails, then we've miscalculated
2049 range for got_offset_size entries in
2050 elf_m68k_finalize_got_offsets. */
2051 BFD_ASSERT (arg->offset2[got_offset_size]
2052 != arg->offset2[-(int) got_offset_size - 1]);
2053
2054 /* Switch. */
2055 arg->offset1[got_offset_size] = arg->offset1[-(int) got_offset_size - 1];
2056 arg->offset2[got_offset_size] = arg->offset2[-(int) got_offset_size - 1];
2057
2058 /* Verify that now we have enough room for the entry. */
2059 BFD_ASSERT (arg->offset1[got_offset_size] + entry_size
2060 <= arg->offset2[got_offset_size]);
2061 }
2062
2063 /* Assign offset to entry. */
2064 entry->u.s2.offset = arg->offset1[got_offset_size];
2065 arg->offset1[got_offset_size] += entry_size;
2066
2067 if (entry->key_.bfd == NULL)
2068 /* Hook up this entry into the list of got_entries of H. */
2069 {
2070 struct elf_m68k_link_hash_entry *h;
2071
2072 h = arg->symndx2h[entry->key_.symndx];
2073 if (h != NULL)
2074 {
2075 entry->u.s2.next = h->glist;
2076 h->glist = entry;
2077 }
2078 else
2079 /* This should be the entry for TLS_LDM relocation then. */
2080 {
2081 BFD_ASSERT ((elf_m68k_reloc_got_type (entry->key_.type)
2082 == R_68K_TLS_LDM32)
2083 && entry->key_.symndx == 0);
2084
2085 ++arg->n_ldm_entries;
2086 }
2087 }
2088 else
2089 /* This entry is for local symbol. */
2090 entry->u.s2.next = NULL;
2091
2092 return 1;
2093 }
2094
2095 /* Assign offsets within GOT. USE_NEG_GOT_OFFSETS_P indicates if we
2096 should use negative offsets.
2097 Build list of GOT entries for global symbols along the way.
2098 SYMNDX2H is mapping from global symbol indices to actual
2099 global symbols.
2100 Return offset at which next GOT should start. */
2101
2102 static void
2103 elf_m68k_finalize_got_offsets (struct elf_m68k_got *got,
2104 bfd_boolean use_neg_got_offsets_p,
2105 struct elf_m68k_link_hash_entry **symndx2h,
2106 bfd_vma *final_offset, bfd_vma *n_ldm_entries)
2107 {
2108 struct elf_m68k_finalize_got_offsets_arg arg_;
2109 bfd_vma offset1_[2 * R_LAST];
2110 bfd_vma offset2_[2 * R_LAST];
2111 int i;
2112 bfd_vma start_offset;
2113
2114 BFD_ASSERT (got->offset != (bfd_vma) -1);
2115
2116 /* We set entry offsets relative to the .got section (and not the
2117 start of a particular GOT), so that we can use them in
2118 finish_dynamic_symbol without needing to know the GOT which they come
2119 from. */
2120
2121 /* Put offset1 in the middle of offset1_, same for offset2. */
2122 arg_.offset1 = offset1_ + R_LAST;
2123 arg_.offset2 = offset2_ + R_LAST;
2124
2125 start_offset = got->offset;
2126
2127 if (use_neg_got_offsets_p)
2128 /* Setup both negative and positive ranges for R_8, R_16 and R_32. */
2129 i = -(int) R_32 - 1;
2130 else
2131 /* Setup positives ranges for R_8, R_16 and R_32. */
2132 i = (int) R_8;
2133
2134 for (; i <= (int) R_32; ++i)
2135 {
2136 int j;
2137 size_t n;
2138
2139 /* Set beginning of the range of offsets I. */
2140 arg_.offset1[i] = start_offset;
2141
2142 /* Calculate number of slots that require I offsets. */
2143 j = (i >= 0) ? i : -i - 1;
2144 n = (j >= 1) ? got->n_slots[j - 1] : 0;
2145 n = got->n_slots[j] - n;
2146
2147 if (use_neg_got_offsets_p && n != 0)
2148 {
2149 if (i < 0)
2150 /* We first fill the positive side of the range, so we might
2151 end up with one empty slot at that side when we can't fit
2152 whole 2-slot entry. Account for that at negative side of
2153 the interval with one additional entry. */
2154 n = n / 2 + 1;
2155 else
2156 /* When the number of slots is odd, make positive side of the
2157 range one entry bigger. */
2158 n = (n + 1) / 2;
2159 }
2160
2161 /* N is the number of slots that require I offsets.
2162 Calculate length of the range for I offsets. */
2163 n = 4 * n;
2164
2165 /* Set end of the range. */
2166 arg_.offset2[i] = start_offset + n;
2167
2168 start_offset = arg_.offset2[i];
2169 }
2170
2171 if (!use_neg_got_offsets_p)
2172 /* Make sure that if we try to switch to negative offsets in
2173 elf_m68k_finalize_got_offsets_1, the assert therein will catch
2174 the bug. */
2175 for (i = R_8; i <= R_32; ++i)
2176 arg_.offset2[-i - 1] = arg_.offset2[i];
2177
2178 /* Setup got->offset. offset1[R_8] is either in the middle or at the
2179 beginning of GOT depending on use_neg_got_offsets_p. */
2180 got->offset = arg_.offset1[R_8];
2181
2182 arg_.symndx2h = symndx2h;
2183 arg_.n_ldm_entries = 0;
2184
2185 /* Assign offsets. */
2186 htab_traverse (got->entries, elf_m68k_finalize_got_offsets_1, &arg_);
2187
2188 /* Check offset ranges we have actually assigned. */
2189 for (i = (int) R_8; i <= (int) R_32; ++i)
2190 BFD_ASSERT (arg_.offset2[i] - arg_.offset1[i] <= 4);
2191
2192 *final_offset = start_offset;
2193 *n_ldm_entries = arg_.n_ldm_entries;
2194 }
2195
2196 struct elf_m68k_partition_multi_got_arg
2197 {
2198 /* The GOT we are adding entries to. Aka big got. */
2199 struct elf_m68k_got *current_got;
2200
2201 /* Offset to assign the next CURRENT_GOT. */
2202 bfd_vma offset;
2203
2204 /* Context where memory should be allocated. */
2205 struct bfd_link_info *info;
2206
2207 /* Total number of slots in the .got section.
2208 This is used to calculate size of the .got and .rela.got sections. */
2209 bfd_vma n_slots;
2210
2211 /* Difference in numbers of allocated slots in the .got section
2212 and necessary relocations in the .rela.got section.
2213 This is used to calculate size of the .rela.got section. */
2214 bfd_vma slots_relas_diff;
2215
2216 /* Error flag. */
2217 bfd_boolean error_p;
2218
2219 /* Mapping from global symndx to global symbols.
2220 This is used to build lists of got entries for global symbols. */
2221 struct elf_m68k_link_hash_entry **symndx2h;
2222 };
2223
2224 static void
2225 elf_m68k_partition_multi_got_2 (struct elf_m68k_partition_multi_got_arg *arg)
2226 {
2227 bfd_vma n_ldm_entries;
2228
2229 elf_m68k_finalize_got_offsets (arg->current_got,
2230 (elf_m68k_hash_table (arg->info)
2231 ->use_neg_got_offsets_p),
2232 arg->symndx2h,
2233 &arg->offset, &n_ldm_entries);
2234
2235 arg->n_slots += arg->current_got->n_slots[R_32];
2236
2237 if (!arg->info->shared)
2238 /* If we are generating a shared object, we need to
2239 output a R_68K_RELATIVE reloc so that the dynamic
2240 linker can adjust this GOT entry. Overwise we
2241 don't need space in .rela.got for local symbols. */
2242 arg->slots_relas_diff += arg->current_got->local_n_slots;
2243
2244 /* @LDM relocations require a 2-slot GOT entry, but only
2245 one relocation. Account for that. */
2246 arg->slots_relas_diff += n_ldm_entries;
2247
2248 BFD_ASSERT (arg->slots_relas_diff <= arg->n_slots);
2249 }
2250
2251
2252 /* Process a single BFD2GOT entry and either merge GOT to CURRENT_GOT
2253 or start a new CURRENT_GOT. */
2254
2255 static int
2256 elf_m68k_partition_multi_got_1 (void **_entry, void *_arg)
2257 {
2258 struct elf_m68k_bfd2got_entry *entry;
2259 struct elf_m68k_partition_multi_got_arg *arg;
2260 struct elf_m68k_got *got;
2261 struct elf_m68k_got diff_;
2262 struct elf_m68k_got *diff;
2263
2264 entry = (struct elf_m68k_bfd2got_entry *) *_entry;
2265 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2266
2267 got = entry->got;
2268 BFD_ASSERT (got != NULL);
2269 BFD_ASSERT (got->offset == (bfd_vma) -1);
2270
2271 diff = NULL;
2272
2273 if (arg->current_got != NULL)
2274 /* Construct diff. */
2275 {
2276 diff = &diff_;
2277 elf_m68k_init_got (diff);
2278
2279 if (!elf_m68k_can_merge_gots (arg->current_got, got, arg->info, diff))
2280 {
2281 if (diff->offset == 0)
2282 /* Offset set to 0 in the diff_ indicates an error. */
2283 {
2284 arg->error_p = TRUE;
2285 goto final_return;
2286 }
2287
2288 if (elf_m68k_hash_table (arg->info)->allow_multigot_p)
2289 {
2290 elf_m68k_clear_got (diff);
2291 /* Schedule to finish up current_got and start new one. */
2292 diff = NULL;
2293 }
2294 /* else
2295 Merge GOTs no matter what. If big GOT overflows,
2296 we'll fail in relocate_section due to truncated relocations.
2297
2298 ??? May be fail earlier? E.g., in can_merge_gots. */
2299 }
2300 }
2301 else
2302 /* Diff of got against empty current_got is got itself. */
2303 {
2304 /* Create empty current_got to put subsequent GOTs to. */
2305 arg->current_got = elf_m68k_create_empty_got (arg->info);
2306 if (arg->current_got == NULL)
2307 {
2308 arg->error_p = TRUE;
2309 goto final_return;
2310 }
2311
2312 arg->current_got->offset = arg->offset;
2313
2314 diff = got;
2315 }
2316
2317 if (diff != NULL)
2318 {
2319 if (!elf_m68k_merge_gots (arg->current_got, diff, arg->info))
2320 {
2321 arg->error_p = TRUE;
2322 goto final_return;
2323 }
2324
2325 /* Now we can free GOT. */
2326 elf_m68k_clear_got (got);
2327
2328 entry->got = arg->current_got;
2329 }
2330 else
2331 {
2332 /* Finish up current_got. */
2333 elf_m68k_partition_multi_got_2 (arg);
2334
2335 /* Schedule to start a new current_got. */
2336 arg->current_got = NULL;
2337
2338 /* Retry. */
2339 if (!elf_m68k_partition_multi_got_1 (_entry, _arg))
2340 {
2341 BFD_ASSERT (arg->error_p);
2342 goto final_return;
2343 }
2344 }
2345
2346 final_return:
2347 if (diff != NULL)
2348 elf_m68k_clear_got (diff);
2349
2350 return arg->error_p == FALSE ? 1 : 0;
2351 }
2352
2353 /* Helper function to build symndx2h mapping. */
2354
2355 static bfd_boolean
2356 elf_m68k_init_symndx2h_1 (struct elf_link_hash_entry *_h,
2357 void *_arg)
2358 {
2359 struct elf_m68k_link_hash_entry *h;
2360
2361 h = elf_m68k_hash_entry (_h);
2362
2363 if (h->got_entry_key != 0)
2364 /* H has at least one entry in the GOT. */
2365 {
2366 struct elf_m68k_partition_multi_got_arg *arg;
2367
2368 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2369
2370 BFD_ASSERT (arg->symndx2h[h->got_entry_key] == NULL);
2371 arg->symndx2h[h->got_entry_key] = h;
2372 }
2373
2374 return TRUE;
2375 }
2376
2377 /* Merge GOTs of some BFDs, assign offsets to GOT entries and build
2378 lists of GOT entries for global symbols.
2379 Calculate sizes of .got and .rela.got sections. */
2380
2381 static bfd_boolean
2382 elf_m68k_partition_multi_got (struct bfd_link_info *info)
2383 {
2384 struct elf_m68k_multi_got *multi_got;
2385 struct elf_m68k_partition_multi_got_arg arg_;
2386
2387 multi_got = elf_m68k_multi_got (info);
2388
2389 arg_.current_got = NULL;
2390 arg_.offset = 0;
2391 arg_.info = info;
2392 arg_.n_slots = 0;
2393 arg_.slots_relas_diff = 0;
2394 arg_.error_p = FALSE;
2395
2396 if (multi_got->bfd2got != NULL)
2397 {
2398 /* Initialize symndx2h mapping. */
2399 {
2400 arg_.symndx2h = bfd_zmalloc (multi_got->global_symndx
2401 * sizeof (*arg_.symndx2h));
2402 if (arg_.symndx2h == NULL)
2403 return FALSE;
2404
2405 elf_link_hash_traverse (elf_hash_table (info),
2406 elf_m68k_init_symndx2h_1, &arg_);
2407 }
2408
2409 /* Partition. */
2410 htab_traverse (multi_got->bfd2got, elf_m68k_partition_multi_got_1,
2411 &arg_);
2412 if (arg_.error_p)
2413 {
2414 free (arg_.symndx2h);
2415 arg_.symndx2h = NULL;
2416
2417 return FALSE;
2418 }
2419
2420 /* Finish up last current_got. */
2421 elf_m68k_partition_multi_got_2 (&arg_);
2422
2423 free (arg_.symndx2h);
2424 }
2425
2426 if (elf_hash_table (info)->dynobj != NULL)
2427 /* Set sizes of .got and .rela.got sections. */
2428 {
2429 asection *s;
2430
2431 s = bfd_get_linker_section (elf_hash_table (info)->dynobj, ".got");
2432 if (s != NULL)
2433 s->size = arg_.offset;
2434 else
2435 BFD_ASSERT (arg_.offset == 0);
2436
2437 BFD_ASSERT (arg_.slots_relas_diff <= arg_.n_slots);
2438 arg_.n_slots -= arg_.slots_relas_diff;
2439
2440 s = bfd_get_linker_section (elf_hash_table (info)->dynobj, ".rela.got");
2441 if (s != NULL)
2442 s->size = arg_.n_slots * sizeof (Elf32_External_Rela);
2443 else
2444 BFD_ASSERT (arg_.n_slots == 0);
2445 }
2446 else
2447 BFD_ASSERT (multi_got->bfd2got == NULL);
2448
2449 return TRUE;
2450 }
2451
2452 /* Specialized version of elf_m68k_get_got_entry that returns pointer
2453 to hashtable slot, thus allowing removal of entry via
2454 elf_m68k_remove_got_entry. */
2455
2456 static struct elf_m68k_got_entry **
2457 elf_m68k_find_got_entry_ptr (struct elf_m68k_got *got,
2458 struct elf_m68k_got_entry_key *key)
2459 {
2460 void **ptr;
2461 struct elf_m68k_got_entry entry_;
2462 struct elf_m68k_got_entry **entry_ptr;
2463
2464 entry_.key_ = *key;
2465 ptr = htab_find_slot (got->entries, &entry_, NO_INSERT);
2466 BFD_ASSERT (ptr != NULL);
2467
2468 entry_ptr = (struct elf_m68k_got_entry **) ptr;
2469
2470 return entry_ptr;
2471 }
2472
2473 /* Remove entry pointed to by ENTRY_PTR from GOT. */
2474
2475 static void
2476 elf_m68k_remove_got_entry (struct elf_m68k_got *got,
2477 struct elf_m68k_got_entry **entry_ptr)
2478 {
2479 struct elf_m68k_got_entry *entry;
2480
2481 entry = *entry_ptr;
2482
2483 /* Check that offsets have not been finalized yet. */
2484 BFD_ASSERT (got->offset == (bfd_vma) -1);
2485 /* Check that this entry is indeed unused. */
2486 BFD_ASSERT (entry->u.s1.refcount == 0);
2487
2488 elf_m68k_remove_got_entry_type (got, entry->key_.type);
2489
2490 if (entry->key_.bfd != NULL)
2491 got->local_n_slots -= elf_m68k_reloc_got_n_slots (entry->key_.type);
2492
2493 BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
2494
2495 htab_clear_slot (got->entries, (void **) entry_ptr);
2496 }
2497
2498 /* Copy any information related to dynamic linking from a pre-existing
2499 symbol to a newly created symbol. Also called to copy flags and
2500 other back-end info to a weakdef, in which case the symbol is not
2501 newly created and plt/got refcounts and dynamic indices should not
2502 be copied. */
2503
2504 static void
2505 elf_m68k_copy_indirect_symbol (struct bfd_link_info *info,
2506 struct elf_link_hash_entry *_dir,
2507 struct elf_link_hash_entry *_ind)
2508 {
2509 struct elf_m68k_link_hash_entry *dir;
2510 struct elf_m68k_link_hash_entry *ind;
2511
2512 _bfd_elf_link_hash_copy_indirect (info, _dir, _ind);
2513
2514 if (_ind->root.type != bfd_link_hash_indirect)
2515 return;
2516
2517 dir = elf_m68k_hash_entry (_dir);
2518 ind = elf_m68k_hash_entry (_ind);
2519
2520 /* Any absolute non-dynamic relocations against an indirect or weak
2521 definition will be against the target symbol. */
2522 _dir->non_got_ref |= _ind->non_got_ref;
2523
2524 /* We might have a direct symbol already having entries in the GOTs.
2525 Update its key only in case indirect symbol has GOT entries and
2526 assert that both indirect and direct symbols don't have GOT entries
2527 at the same time. */
2528 if (ind->got_entry_key != 0)
2529 {
2530 BFD_ASSERT (dir->got_entry_key == 0);
2531 /* Assert that GOTs aren't partioned yet. */
2532 BFD_ASSERT (ind->glist == NULL);
2533
2534 dir->got_entry_key = ind->got_entry_key;
2535 ind->got_entry_key = 0;
2536 }
2537 }
2538
2539 /* Look through the relocs for a section during the first phase, and
2540 allocate space in the global offset table or procedure linkage
2541 table. */
2542
2543 static bfd_boolean
2544 elf_m68k_check_relocs (bfd *abfd,
2545 struct bfd_link_info *info,
2546 asection *sec,
2547 const Elf_Internal_Rela *relocs)
2548 {
2549 bfd *dynobj;
2550 Elf_Internal_Shdr *symtab_hdr;
2551 struct elf_link_hash_entry **sym_hashes;
2552 const Elf_Internal_Rela *rel;
2553 const Elf_Internal_Rela *rel_end;
2554 asection *sgot;
2555 asection *srelgot;
2556 asection *sreloc;
2557 struct elf_m68k_got *got;
2558
2559 if (info->relocatable)
2560 return TRUE;
2561
2562 dynobj = elf_hash_table (info)->dynobj;
2563 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2564 sym_hashes = elf_sym_hashes (abfd);
2565
2566 sgot = NULL;
2567 srelgot = NULL;
2568 sreloc = NULL;
2569
2570 got = NULL;
2571
2572 rel_end = relocs + sec->reloc_count;
2573 for (rel = relocs; rel < rel_end; rel++)
2574 {
2575 unsigned long r_symndx;
2576 struct elf_link_hash_entry *h;
2577
2578 r_symndx = ELF32_R_SYM (rel->r_info);
2579
2580 if (r_symndx < symtab_hdr->sh_info)
2581 h = NULL;
2582 else
2583 {
2584 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2585 while (h->root.type == bfd_link_hash_indirect
2586 || h->root.type == bfd_link_hash_warning)
2587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2588 }
2589
2590 switch (ELF32_R_TYPE (rel->r_info))
2591 {
2592 case R_68K_GOT8:
2593 case R_68K_GOT16:
2594 case R_68K_GOT32:
2595 if (h != NULL
2596 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2597 break;
2598 /* Fall through. */
2599
2600 /* Relative GOT relocations. */
2601 case R_68K_GOT8O:
2602 case R_68K_GOT16O:
2603 case R_68K_GOT32O:
2604 /* Fall through. */
2605
2606 /* TLS relocations. */
2607 case R_68K_TLS_GD8:
2608 case R_68K_TLS_GD16:
2609 case R_68K_TLS_GD32:
2610 case R_68K_TLS_LDM8:
2611 case R_68K_TLS_LDM16:
2612 case R_68K_TLS_LDM32:
2613 case R_68K_TLS_IE8:
2614 case R_68K_TLS_IE16:
2615 case R_68K_TLS_IE32:
2616
2617 case R_68K_TLS_TPREL32:
2618 case R_68K_TLS_DTPREL32:
2619
2620 if (ELF32_R_TYPE (rel->r_info) == R_68K_TLS_TPREL32
2621 && info->shared)
2622 /* Do the special chorus for libraries with static TLS. */
2623 info->flags |= DF_STATIC_TLS;
2624
2625 /* This symbol requires a global offset table entry. */
2626
2627 if (dynobj == NULL)
2628 {
2629 /* Create the .got section. */
2630 elf_hash_table (info)->dynobj = dynobj = abfd;
2631 if (!_bfd_elf_create_got_section (dynobj, info))
2632 return FALSE;
2633 }
2634
2635 if (sgot == NULL)
2636 {
2637 sgot = bfd_get_linker_section (dynobj, ".got");
2638 BFD_ASSERT (sgot != NULL);
2639 }
2640
2641 if (srelgot == NULL
2642 && (h != NULL || info->shared))
2643 {
2644 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2645 if (srelgot == NULL)
2646 {
2647 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2648 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2649 | SEC_READONLY);
2650 srelgot = bfd_make_section_anyway_with_flags (dynobj,
2651 ".rela.got",
2652 flags);
2653 if (srelgot == NULL
2654 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2655 return FALSE;
2656 }
2657 }
2658
2659 if (got == NULL)
2660 {
2661 struct elf_m68k_bfd2got_entry *bfd2got_entry;
2662
2663 bfd2got_entry
2664 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2665 abfd, FIND_OR_CREATE, info);
2666 if (bfd2got_entry == NULL)
2667 return FALSE;
2668
2669 got = bfd2got_entry->got;
2670 BFD_ASSERT (got != NULL);
2671 }
2672
2673 {
2674 struct elf_m68k_got_entry *got_entry;
2675
2676 /* Add entry to got. */
2677 got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
2678 ELF32_R_TYPE (rel->r_info),
2679 r_symndx, info);
2680 if (got_entry == NULL)
2681 return FALSE;
2682
2683 if (got_entry->u.s1.refcount == 1)
2684 {
2685 /* Make sure this symbol is output as a dynamic symbol. */
2686 if (h != NULL
2687 && h->dynindx == -1
2688 && !h->forced_local)
2689 {
2690 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2691 return FALSE;
2692 }
2693 }
2694 }
2695
2696 break;
2697
2698 case R_68K_PLT8:
2699 case R_68K_PLT16:
2700 case R_68K_PLT32:
2701 /* This symbol requires a procedure linkage table entry. We
2702 actually build the entry in adjust_dynamic_symbol,
2703 because this might be a case of linking PIC code which is
2704 never referenced by a dynamic object, in which case we
2705 don't need to generate a procedure linkage table entry
2706 after all. */
2707
2708 /* If this is a local symbol, we resolve it directly without
2709 creating a procedure linkage table entry. */
2710 if (h == NULL)
2711 continue;
2712
2713 h->needs_plt = 1;
2714 h->plt.refcount++;
2715 break;
2716
2717 case R_68K_PLT8O:
2718 case R_68K_PLT16O:
2719 case R_68K_PLT32O:
2720 /* This symbol requires a procedure linkage table entry. */
2721
2722 if (h == NULL)
2723 {
2724 /* It does not make sense to have this relocation for a
2725 local symbol. FIXME: does it? How to handle it if
2726 it does make sense? */
2727 bfd_set_error (bfd_error_bad_value);
2728 return FALSE;
2729 }
2730
2731 /* Make sure this symbol is output as a dynamic symbol. */
2732 if (h->dynindx == -1
2733 && !h->forced_local)
2734 {
2735 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2736 return FALSE;
2737 }
2738
2739 h->needs_plt = 1;
2740 h->plt.refcount++;
2741 break;
2742
2743 case R_68K_PC8:
2744 case R_68K_PC16:
2745 case R_68K_PC32:
2746 /* If we are creating a shared library and this is not a local
2747 symbol, we need to copy the reloc into the shared library.
2748 However when linking with -Bsymbolic and this is a global
2749 symbol which is defined in an object we are including in the
2750 link (i.e., DEF_REGULAR is set), then we can resolve the
2751 reloc directly. At this point we have not seen all the input
2752 files, so it is possible that DEF_REGULAR is not set now but
2753 will be set later (it is never cleared). We account for that
2754 possibility below by storing information in the
2755 pcrel_relocs_copied field of the hash table entry. */
2756 if (!(info->shared
2757 && (sec->flags & SEC_ALLOC) != 0
2758 && h != NULL
2759 && (!info->symbolic
2760 || h->root.type == bfd_link_hash_defweak
2761 || !h->def_regular)))
2762 {
2763 if (h != NULL)
2764 {
2765 /* Make sure a plt entry is created for this symbol if
2766 it turns out to be a function defined by a dynamic
2767 object. */
2768 h->plt.refcount++;
2769 }
2770 break;
2771 }
2772 /* Fall through. */
2773 case R_68K_8:
2774 case R_68K_16:
2775 case R_68K_32:
2776 /* We don't need to handle relocs into sections not going into
2777 the "real" output. */
2778 if ((sec->flags & SEC_ALLOC) == 0)
2779 break;
2780
2781 if (h != NULL)
2782 {
2783 /* Make sure a plt entry is created for this symbol if it
2784 turns out to be a function defined by a dynamic object. */
2785 h->plt.refcount++;
2786
2787 if (info->executable)
2788 /* This symbol needs a non-GOT reference. */
2789 h->non_got_ref = 1;
2790 }
2791
2792 /* If we are creating a shared library, we need to copy the
2793 reloc into the shared library. */
2794 if (info->shared)
2795 {
2796 /* When creating a shared object, we must copy these
2797 reloc types into the output file. We create a reloc
2798 section in dynobj and make room for this reloc. */
2799 if (sreloc == NULL)
2800 {
2801 sreloc = _bfd_elf_make_dynamic_reloc_section
2802 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
2803
2804 if (sreloc == NULL)
2805 return FALSE;
2806 }
2807
2808 if (sec->flags & SEC_READONLY
2809 /* Don't set DF_TEXTREL yet for PC relative
2810 relocations, they might be discarded later. */
2811 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2812 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2813 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
2814 info->flags |= DF_TEXTREL;
2815
2816 sreloc->size += sizeof (Elf32_External_Rela);
2817
2818 /* We count the number of PC relative relocations we have
2819 entered for this symbol, so that we can discard them
2820 again if, in the -Bsymbolic case, the symbol is later
2821 defined by a regular object, or, in the normal shared
2822 case, the symbol is forced to be local. Note that this
2823 function is only called if we are using an m68kelf linker
2824 hash table, which means that h is really a pointer to an
2825 elf_m68k_link_hash_entry. */
2826 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2827 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2828 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
2829 {
2830 struct elf_m68k_pcrel_relocs_copied *p;
2831 struct elf_m68k_pcrel_relocs_copied **head;
2832
2833 if (h != NULL)
2834 {
2835 struct elf_m68k_link_hash_entry *eh
2836 = elf_m68k_hash_entry (h);
2837 head = &eh->pcrel_relocs_copied;
2838 }
2839 else
2840 {
2841 asection *s;
2842 void *vpp;
2843 Elf_Internal_Sym *isym;
2844
2845 isym = bfd_sym_from_r_symndx (&elf_m68k_hash_table (info)->sym_cache,
2846 abfd, r_symndx);
2847 if (isym == NULL)
2848 return FALSE;
2849
2850 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2851 if (s == NULL)
2852 s = sec;
2853
2854 vpp = &elf_section_data (s)->local_dynrel;
2855 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
2856 }
2857
2858 for (p = *head; p != NULL; p = p->next)
2859 if (p->section == sreloc)
2860 break;
2861
2862 if (p == NULL)
2863 {
2864 p = ((struct elf_m68k_pcrel_relocs_copied *)
2865 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2866 if (p == NULL)
2867 return FALSE;
2868 p->next = *head;
2869 *head = p;
2870 p->section = sreloc;
2871 p->count = 0;
2872 }
2873
2874 ++p->count;
2875 }
2876 }
2877
2878 break;
2879
2880 /* This relocation describes the C++ object vtable hierarchy.
2881 Reconstruct it for later use during GC. */
2882 case R_68K_GNU_VTINHERIT:
2883 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2884 return FALSE;
2885 break;
2886
2887 /* This relocation describes which C++ vtable entries are actually
2888 used. Record for later use during GC. */
2889 case R_68K_GNU_VTENTRY:
2890 BFD_ASSERT (h != NULL);
2891 if (h != NULL
2892 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2893 return FALSE;
2894 break;
2895
2896 default:
2897 break;
2898 }
2899 }
2900
2901 return TRUE;
2902 }
2903
2904 /* Return the section that should be marked against GC for a given
2905 relocation. */
2906
2907 static asection *
2908 elf_m68k_gc_mark_hook (asection *sec,
2909 struct bfd_link_info *info,
2910 Elf_Internal_Rela *rel,
2911 struct elf_link_hash_entry *h,
2912 Elf_Internal_Sym *sym)
2913 {
2914 if (h != NULL)
2915 switch (ELF32_R_TYPE (rel->r_info))
2916 {
2917 case R_68K_GNU_VTINHERIT:
2918 case R_68K_GNU_VTENTRY:
2919 return NULL;
2920 }
2921
2922 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2923 }
2924
2925 /* Update the got entry reference counts for the section being removed. */
2926
2927 static bfd_boolean
2928 elf_m68k_gc_sweep_hook (bfd *abfd,
2929 struct bfd_link_info *info,
2930 asection *sec,
2931 const Elf_Internal_Rela *relocs)
2932 {
2933 Elf_Internal_Shdr *symtab_hdr;
2934 struct elf_link_hash_entry **sym_hashes;
2935 const Elf_Internal_Rela *rel, *relend;
2936 bfd *dynobj;
2937 struct elf_m68k_got *got;
2938
2939 if (info->relocatable)
2940 return TRUE;
2941
2942 dynobj = elf_hash_table (info)->dynobj;
2943 if (dynobj == NULL)
2944 return TRUE;
2945
2946 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2947 sym_hashes = elf_sym_hashes (abfd);
2948 got = NULL;
2949
2950 relend = relocs + sec->reloc_count;
2951 for (rel = relocs; rel < relend; rel++)
2952 {
2953 unsigned long r_symndx;
2954 struct elf_link_hash_entry *h = NULL;
2955
2956 r_symndx = ELF32_R_SYM (rel->r_info);
2957 if (r_symndx >= symtab_hdr->sh_info)
2958 {
2959 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2960 while (h->root.type == bfd_link_hash_indirect
2961 || h->root.type == bfd_link_hash_warning)
2962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2963 }
2964
2965 switch (ELF32_R_TYPE (rel->r_info))
2966 {
2967 case R_68K_GOT8:
2968 case R_68K_GOT16:
2969 case R_68K_GOT32:
2970 if (h != NULL
2971 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2972 break;
2973
2974 /* FALLTHRU */
2975 case R_68K_GOT8O:
2976 case R_68K_GOT16O:
2977 case R_68K_GOT32O:
2978 /* Fall through. */
2979
2980 /* TLS relocations. */
2981 case R_68K_TLS_GD8:
2982 case R_68K_TLS_GD16:
2983 case R_68K_TLS_GD32:
2984 case R_68K_TLS_LDM8:
2985 case R_68K_TLS_LDM16:
2986 case R_68K_TLS_LDM32:
2987 case R_68K_TLS_IE8:
2988 case R_68K_TLS_IE16:
2989 case R_68K_TLS_IE32:
2990
2991 case R_68K_TLS_TPREL32:
2992 case R_68K_TLS_DTPREL32:
2993
2994 if (got == NULL)
2995 {
2996 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2997 abfd, MUST_FIND, NULL)->got;
2998 BFD_ASSERT (got != NULL);
2999 }
3000
3001 {
3002 struct elf_m68k_got_entry_key key_;
3003 struct elf_m68k_got_entry **got_entry_ptr;
3004 struct elf_m68k_got_entry *got_entry;
3005
3006 elf_m68k_init_got_entry_key (&key_, h, abfd, r_symndx,
3007 ELF32_R_TYPE (rel->r_info));
3008 got_entry_ptr = elf_m68k_find_got_entry_ptr (got, &key_);
3009
3010 got_entry = *got_entry_ptr;
3011
3012 if (got_entry->u.s1.refcount > 0)
3013 {
3014 --got_entry->u.s1.refcount;
3015
3016 if (got_entry->u.s1.refcount == 0)
3017 /* We don't need the .got entry any more. */
3018 elf_m68k_remove_got_entry (got, got_entry_ptr);
3019 }
3020 }
3021 break;
3022
3023 case R_68K_PLT8:
3024 case R_68K_PLT16:
3025 case R_68K_PLT32:
3026 case R_68K_PLT8O:
3027 case R_68K_PLT16O:
3028 case R_68K_PLT32O:
3029 case R_68K_PC8:
3030 case R_68K_PC16:
3031 case R_68K_PC32:
3032 case R_68K_8:
3033 case R_68K_16:
3034 case R_68K_32:
3035 if (h != NULL)
3036 {
3037 if (h->plt.refcount > 0)
3038 --h->plt.refcount;
3039 }
3040 break;
3041
3042 default:
3043 break;
3044 }
3045 }
3046
3047 return TRUE;
3048 }
3049
3050 /* 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->shared)
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;
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);
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 Elf_Internal_Rela *rela)
4743 {
4744 switch ((int) ELF32_R_TYPE (rela->r_info))
4745 {
4746 case R_68K_RELATIVE:
4747 return reloc_class_relative;
4748 case R_68K_JMP_SLOT:
4749 return reloc_class_plt;
4750 case R_68K_COPY:
4751 return reloc_class_copy;
4752 default:
4753 return reloc_class_normal;
4754 }
4755 }
4756
4757 /* Return address for Ith PLT stub in section PLT, for relocation REL
4758 or (bfd_vma) -1 if it should not be included. */
4759
4760 static bfd_vma
4761 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
4762 const arelent *rel ATTRIBUTE_UNUSED)
4763 {
4764 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
4765 }
4766
4767 /* Support for core dump NOTE sections. */
4768
4769 static bfd_boolean
4770 elf_m68k_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4771 {
4772 int offset;
4773 size_t size;
4774
4775 switch (note->descsz)
4776 {
4777 default:
4778 return FALSE;
4779
4780 case 154: /* Linux/m68k */
4781 /* pr_cursig */
4782 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4783
4784 /* pr_pid */
4785 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
4786
4787 /* pr_reg */
4788 offset = 70;
4789 size = 80;
4790
4791 break;
4792 }
4793
4794 /* Make a ".reg/999" section. */
4795 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4796 size, note->descpos + offset);
4797 }
4798
4799 static bfd_boolean
4800 elf_m68k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4801 {
4802 switch (note->descsz)
4803 {
4804 default:
4805 return FALSE;
4806
4807 case 124: /* Linux/m68k elf_prpsinfo. */
4808 elf_tdata (abfd)->core->pid
4809 = bfd_get_32 (abfd, note->descdata + 12);
4810 elf_tdata (abfd)->core->program
4811 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
4812 elf_tdata (abfd)->core->command
4813 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
4814 }
4815
4816 /* Note that for some reason, a spurious space is tacked
4817 onto the end of the args in some (at least one anyway)
4818 implementations, so strip it off if it exists. */
4819 {
4820 char *command = elf_tdata (abfd)->core->command;
4821 int n = strlen (command);
4822
4823 if (n > 0 && command[n - 1] == ' ')
4824 command[n - 1] = '\0';
4825 }
4826
4827 return TRUE;
4828 }
4829
4830 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
4831 #define TARGET_BIG_NAME "elf32-m68k"
4832 #define ELF_MACHINE_CODE EM_68K
4833 #define ELF_MAXPAGESIZE 0x2000
4834 #define elf_backend_create_dynamic_sections \
4835 _bfd_elf_create_dynamic_sections
4836 #define bfd_elf32_bfd_link_hash_table_create \
4837 elf_m68k_link_hash_table_create
4838 /* ??? Should it be this macro or bfd_elfNN_bfd_link_hash_table_create? */
4839 #define bfd_elf32_bfd_link_hash_table_free \
4840 elf_m68k_link_hash_table_free
4841 #define bfd_elf32_bfd_final_link bfd_elf_final_link
4842
4843 #define elf_backend_check_relocs elf_m68k_check_relocs
4844 #define elf_backend_always_size_sections \
4845 elf_m68k_always_size_sections
4846 #define elf_backend_adjust_dynamic_symbol \
4847 elf_m68k_adjust_dynamic_symbol
4848 #define elf_backend_size_dynamic_sections \
4849 elf_m68k_size_dynamic_sections
4850 #define elf_backend_final_write_processing elf_m68k_final_write_processing
4851 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4852 #define elf_backend_relocate_section elf_m68k_relocate_section
4853 #define elf_backend_finish_dynamic_symbol \
4854 elf_m68k_finish_dynamic_symbol
4855 #define elf_backend_finish_dynamic_sections \
4856 elf_m68k_finish_dynamic_sections
4857 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
4858 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
4859 #define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
4860 #define bfd_elf32_bfd_merge_private_bfd_data \
4861 elf32_m68k_merge_private_bfd_data
4862 #define bfd_elf32_bfd_set_private_flags \
4863 elf32_m68k_set_private_flags
4864 #define bfd_elf32_bfd_print_private_bfd_data \
4865 elf32_m68k_print_private_bfd_data
4866 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
4867 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
4868 #define elf_backend_object_p elf32_m68k_object_p
4869 #define elf_backend_grok_prstatus elf_m68k_grok_prstatus
4870 #define elf_backend_grok_psinfo elf_m68k_grok_psinfo
4871
4872 #define elf_backend_can_gc_sections 1
4873 #define elf_backend_can_refcount 1
4874 #define elf_backend_want_got_plt 1
4875 #define elf_backend_plt_readonly 1
4876 #define elf_backend_want_plt_sym 0
4877 #define elf_backend_got_header_size 12
4878 #define elf_backend_rela_normal 1
4879
4880 #include "elf32-target.h"
4881