elf32-m68k.c revision 1.1.1.1.8.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, 2013
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 /* PR15323, ref flags aren't set for references in the same
2590 object. */
2591 h->root.non_ir_ref = 1;
2592 }
2593
2594 switch (ELF32_R_TYPE (rel->r_info))
2595 {
2596 case R_68K_GOT8:
2597 case R_68K_GOT16:
2598 case R_68K_GOT32:
2599 if (h != NULL
2600 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2601 break;
2602 /* Fall through. */
2603
2604 /* Relative GOT relocations. */
2605 case R_68K_GOT8O:
2606 case R_68K_GOT16O:
2607 case R_68K_GOT32O:
2608 /* Fall through. */
2609
2610 /* TLS relocations. */
2611 case R_68K_TLS_GD8:
2612 case R_68K_TLS_GD16:
2613 case R_68K_TLS_GD32:
2614 case R_68K_TLS_LDM8:
2615 case R_68K_TLS_LDM16:
2616 case R_68K_TLS_LDM32:
2617 case R_68K_TLS_IE8:
2618 case R_68K_TLS_IE16:
2619 case R_68K_TLS_IE32:
2620
2621 case R_68K_TLS_TPREL32:
2622 case R_68K_TLS_DTPREL32:
2623
2624 if (ELF32_R_TYPE (rel->r_info) == R_68K_TLS_TPREL32
2625 && info->shared)
2626 /* Do the special chorus for libraries with static TLS. */
2627 info->flags |= DF_STATIC_TLS;
2628
2629 /* This symbol requires a global offset table entry. */
2630
2631 if (dynobj == NULL)
2632 {
2633 /* Create the .got section. */
2634 elf_hash_table (info)->dynobj = dynobj = abfd;
2635 if (!_bfd_elf_create_got_section (dynobj, info))
2636 return FALSE;
2637 }
2638
2639 if (sgot == NULL)
2640 {
2641 sgot = bfd_get_linker_section (dynobj, ".got");
2642 BFD_ASSERT (sgot != NULL);
2643 }
2644
2645 if (srelgot == NULL
2646 && (h != NULL || info->shared))
2647 {
2648 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2649 if (srelgot == NULL)
2650 {
2651 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2652 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2653 | SEC_READONLY);
2654 srelgot = bfd_make_section_anyway_with_flags (dynobj,
2655 ".rela.got",
2656 flags);
2657 if (srelgot == NULL
2658 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2659 return FALSE;
2660 }
2661 }
2662
2663 if (got == NULL)
2664 {
2665 struct elf_m68k_bfd2got_entry *bfd2got_entry;
2666
2667 bfd2got_entry
2668 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2669 abfd, FIND_OR_CREATE, info);
2670 if (bfd2got_entry == NULL)
2671 return FALSE;
2672
2673 got = bfd2got_entry->got;
2674 BFD_ASSERT (got != NULL);
2675 }
2676
2677 {
2678 struct elf_m68k_got_entry *got_entry;
2679
2680 /* Add entry to got. */
2681 got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
2682 ELF32_R_TYPE (rel->r_info),
2683 r_symndx, info);
2684 if (got_entry == NULL)
2685 return FALSE;
2686
2687 if (got_entry->u.s1.refcount == 1)
2688 {
2689 /* Make sure this symbol is output as a dynamic symbol. */
2690 if (h != NULL
2691 && h->dynindx == -1
2692 && !h->forced_local)
2693 {
2694 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2695 return FALSE;
2696 }
2697 }
2698 }
2699
2700 break;
2701
2702 case R_68K_PLT8:
2703 case R_68K_PLT16:
2704 case R_68K_PLT32:
2705 /* This symbol requires a procedure linkage table entry. We
2706 actually build the entry in adjust_dynamic_symbol,
2707 because this might be a case of linking PIC code which is
2708 never referenced by a dynamic object, in which case we
2709 don't need to generate a procedure linkage table entry
2710 after all. */
2711
2712 /* If this is a local symbol, we resolve it directly without
2713 creating a procedure linkage table entry. */
2714 if (h == NULL)
2715 continue;
2716
2717 h->needs_plt = 1;
2718 h->plt.refcount++;
2719 break;
2720
2721 case R_68K_PLT8O:
2722 case R_68K_PLT16O:
2723 case R_68K_PLT32O:
2724 /* This symbol requires a procedure linkage table entry. */
2725
2726 if (h == NULL)
2727 {
2728 /* It does not make sense to have this relocation for a
2729 local symbol. FIXME: does it? How to handle it if
2730 it does make sense? */
2731 bfd_set_error (bfd_error_bad_value);
2732 return FALSE;
2733 }
2734
2735 /* Make sure this symbol is output as a dynamic symbol. */
2736 if (h->dynindx == -1
2737 && !h->forced_local)
2738 {
2739 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2740 return FALSE;
2741 }
2742
2743 h->needs_plt = 1;
2744 h->plt.refcount++;
2745 break;
2746
2747 case R_68K_PC8:
2748 case R_68K_PC16:
2749 case R_68K_PC32:
2750 /* If we are creating a shared library and this is not a local
2751 symbol, we need to copy the reloc into the shared library.
2752 However when linking with -Bsymbolic and this is a global
2753 symbol which is defined in an object we are including in the
2754 link (i.e., DEF_REGULAR is set), then we can resolve the
2755 reloc directly. At this point we have not seen all the input
2756 files, so it is possible that DEF_REGULAR is not set now but
2757 will be set later (it is never cleared). We account for that
2758 possibility below by storing information in the
2759 pcrel_relocs_copied field of the hash table entry. */
2760 if (!(info->shared
2761 && (sec->flags & SEC_ALLOC) != 0
2762 && h != NULL
2763 && (!info->symbolic
2764 || h->root.type == bfd_link_hash_defweak
2765 || !h->def_regular)))
2766 {
2767 if (h != NULL)
2768 {
2769 /* Make sure a plt entry is created for this symbol if
2770 it turns out to be a function defined by a dynamic
2771 object. */
2772 h->plt.refcount++;
2773 }
2774 break;
2775 }
2776 /* Fall through. */
2777 case R_68K_8:
2778 case R_68K_16:
2779 case R_68K_32:
2780 /* We don't need to handle relocs into sections not going into
2781 the "real" output. */
2782 if ((sec->flags & SEC_ALLOC) == 0)
2783 break;
2784
2785 if (h != NULL)
2786 {
2787 /* Make sure a plt entry is created for this symbol if it
2788 turns out to be a function defined by a dynamic object. */
2789 h->plt.refcount++;
2790
2791 if (info->executable)
2792 /* This symbol needs a non-GOT reference. */
2793 h->non_got_ref = 1;
2794 }
2795
2796 /* If we are creating a shared library, we need to copy the
2797 reloc into the shared library. */
2798 if (info->shared)
2799 {
2800 /* When creating a shared object, we must copy these
2801 reloc types into the output file. We create a reloc
2802 section in dynobj and make room for this reloc. */
2803 if (sreloc == NULL)
2804 {
2805 sreloc = _bfd_elf_make_dynamic_reloc_section
2806 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
2807
2808 if (sreloc == NULL)
2809 return FALSE;
2810 }
2811
2812 if (sec->flags & SEC_READONLY
2813 /* Don't set DF_TEXTREL yet for PC relative
2814 relocations, they might be discarded later. */
2815 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2816 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2817 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
2818 info->flags |= DF_TEXTREL;
2819
2820 sreloc->size += sizeof (Elf32_External_Rela);
2821
2822 /* We count the number of PC relative relocations we have
2823 entered for this symbol, so that we can discard them
2824 again if, in the -Bsymbolic case, the symbol is later
2825 defined by a regular object, or, in the normal shared
2826 case, the symbol is forced to be local. Note that this
2827 function is only called if we are using an m68kelf linker
2828 hash table, which means that h is really a pointer to an
2829 elf_m68k_link_hash_entry. */
2830 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2831 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2832 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
2833 {
2834 struct elf_m68k_pcrel_relocs_copied *p;
2835 struct elf_m68k_pcrel_relocs_copied **head;
2836
2837 if (h != NULL)
2838 {
2839 struct elf_m68k_link_hash_entry *eh
2840 = elf_m68k_hash_entry (h);
2841 head = &eh->pcrel_relocs_copied;
2842 }
2843 else
2844 {
2845 asection *s;
2846 void *vpp;
2847 Elf_Internal_Sym *isym;
2848
2849 isym = bfd_sym_from_r_symndx (&elf_m68k_hash_table (info)->sym_cache,
2850 abfd, r_symndx);
2851 if (isym == NULL)
2852 return FALSE;
2853
2854 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2855 if (s == NULL)
2856 s = sec;
2857
2858 vpp = &elf_section_data (s)->local_dynrel;
2859 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
2860 }
2861
2862 for (p = *head; p != NULL; p = p->next)
2863 if (p->section == sreloc)
2864 break;
2865
2866 if (p == NULL)
2867 {
2868 p = ((struct elf_m68k_pcrel_relocs_copied *)
2869 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2870 if (p == NULL)
2871 return FALSE;
2872 p->next = *head;
2873 *head = p;
2874 p->section = sreloc;
2875 p->count = 0;
2876 }
2877
2878 ++p->count;
2879 }
2880 }
2881
2882 break;
2883
2884 /* This relocation describes the C++ object vtable hierarchy.
2885 Reconstruct it for later use during GC. */
2886 case R_68K_GNU_VTINHERIT:
2887 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2888 return FALSE;
2889 break;
2890
2891 /* This relocation describes which C++ vtable entries are actually
2892 used. Record for later use during GC. */
2893 case R_68K_GNU_VTENTRY:
2894 BFD_ASSERT (h != NULL);
2895 if (h != NULL
2896 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2897 return FALSE;
2898 break;
2899
2900 default:
2901 break;
2902 }
2903 }
2904
2905 return TRUE;
2906 }
2907
2908 /* Return the section that should be marked against GC for a given
2909 relocation. */
2910
2911 static asection *
2912 elf_m68k_gc_mark_hook (asection *sec,
2913 struct bfd_link_info *info,
2914 Elf_Internal_Rela *rel,
2915 struct elf_link_hash_entry *h,
2916 Elf_Internal_Sym *sym)
2917 {
2918 if (h != NULL)
2919 switch (ELF32_R_TYPE (rel->r_info))
2920 {
2921 case R_68K_GNU_VTINHERIT:
2922 case R_68K_GNU_VTENTRY:
2923 return NULL;
2924 }
2925
2926 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2927 }
2928
2929 /* Update the got entry reference counts for the section being removed. */
2930
2931 static bfd_boolean
2932 elf_m68k_gc_sweep_hook (bfd *abfd,
2933 struct bfd_link_info *info,
2934 asection *sec,
2935 const Elf_Internal_Rela *relocs)
2936 {
2937 Elf_Internal_Shdr *symtab_hdr;
2938 struct elf_link_hash_entry **sym_hashes;
2939 const Elf_Internal_Rela *rel, *relend;
2940 bfd *dynobj;
2941 struct elf_m68k_got *got;
2942
2943 if (info->relocatable)
2944 return TRUE;
2945
2946 dynobj = elf_hash_table (info)->dynobj;
2947 if (dynobj == NULL)
2948 return TRUE;
2949
2950 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2951 sym_hashes = elf_sym_hashes (abfd);
2952 got = NULL;
2953
2954 relend = relocs + sec->reloc_count;
2955 for (rel = relocs; rel < relend; rel++)
2956 {
2957 unsigned long r_symndx;
2958 struct elf_link_hash_entry *h = NULL;
2959
2960 r_symndx = ELF32_R_SYM (rel->r_info);
2961 if (r_symndx >= symtab_hdr->sh_info)
2962 {
2963 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2964 while (h->root.type == bfd_link_hash_indirect
2965 || h->root.type == bfd_link_hash_warning)
2966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2967 }
2968
2969 switch (ELF32_R_TYPE (rel->r_info))
2970 {
2971 case R_68K_GOT8:
2972 case R_68K_GOT16:
2973 case R_68K_GOT32:
2974 if (h != NULL
2975 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2976 break;
2977
2978 /* FALLTHRU */
2979 case R_68K_GOT8O:
2980 case R_68K_GOT16O:
2981 case R_68K_GOT32O:
2982 /* Fall through. */
2983
2984 /* TLS relocations. */
2985 case R_68K_TLS_GD8:
2986 case R_68K_TLS_GD16:
2987 case R_68K_TLS_GD32:
2988 case R_68K_TLS_LDM8:
2989 case R_68K_TLS_LDM16:
2990 case R_68K_TLS_LDM32:
2991 case R_68K_TLS_IE8:
2992 case R_68K_TLS_IE16:
2993 case R_68K_TLS_IE32:
2994
2995 case R_68K_TLS_TPREL32:
2996 case R_68K_TLS_DTPREL32:
2997
2998 if (got == NULL)
2999 {
3000 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3001 abfd, MUST_FIND, NULL)->got;
3002 BFD_ASSERT (got != NULL);
3003 }
3004
3005 {
3006 struct elf_m68k_got_entry_key key_;
3007 struct elf_m68k_got_entry **got_entry_ptr;
3008 struct elf_m68k_got_entry *got_entry;
3009
3010 elf_m68k_init_got_entry_key (&key_, h, abfd, r_symndx,
3011 ELF32_R_TYPE (rel->r_info));
3012 got_entry_ptr = elf_m68k_find_got_entry_ptr (got, &key_);
3013
3014 got_entry = *got_entry_ptr;
3015
3016 if (got_entry->u.s1.refcount > 0)
3017 {
3018 --got_entry->u.s1.refcount;
3019
3020 if (got_entry->u.s1.refcount == 0)
3021 /* We don't need the .got entry any more. */
3022 elf_m68k_remove_got_entry (got, got_entry_ptr);
3023 }
3024 }
3025 break;
3026
3027 case R_68K_PLT8:
3028 case R_68K_PLT16:
3029 case R_68K_PLT32:
3030 case R_68K_PLT8O:
3031 case R_68K_PLT16O:
3032 case R_68K_PLT32O:
3033 case R_68K_PC8:
3034 case R_68K_PC16:
3035 case R_68K_PC32:
3036 case R_68K_8:
3037 case R_68K_16:
3038 case R_68K_32:
3039 if (h != NULL)
3040 {
3041 if (h->plt.refcount > 0)
3042 --h->plt.refcount;
3043 }
3044 break;
3045
3046 default:
3047 break;
3048 }
3049 }
3050
3051 return TRUE;
3052 }
3053
3054 /* Return the type of PLT associated with OUTPUT_BFD. */
3056
3057 static const struct elf_m68k_plt_info *
3058 elf_m68k_get_plt_info (bfd *output_bfd)
3059 {
3060 unsigned int features;
3061
3062 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
3063 if (features & cpu32)
3064 return &elf_cpu32_plt_info;
3065 if (features & mcfisa_b)
3066 return &elf_isab_plt_info;
3067 if (features & mcfisa_c)
3068 return &elf_isac_plt_info;
3069 return &elf_m68k_plt_info;
3070 }
3071
3072 /* This function is called after all the input files have been read,
3073 and the input sections have been assigned to output sections.
3074 It's a convenient place to determine the PLT style. */
3075
3076 static bfd_boolean
3077 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3078 {
3079 /* Bind input BFDs to GOTs and calculate sizes of .got and .rela.got
3080 sections. */
3081 if (!elf_m68k_partition_multi_got (info))
3082 return FALSE;
3083
3084 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
3085 return TRUE;
3086 }
3087
3088 /* Adjust a symbol defined by a dynamic object and referenced by a
3089 regular object. The current definition is in some section of the
3090 dynamic object, but we're not including those sections. We have to
3091 change the definition to something the rest of the link can
3092 understand. */
3093
3094 static bfd_boolean
3095 elf_m68k_adjust_dynamic_symbol (struct bfd_link_info *info,
3096 struct elf_link_hash_entry *h)
3097 {
3098 struct elf_m68k_link_hash_table *htab;
3099 bfd *dynobj;
3100 asection *s;
3101
3102 htab = elf_m68k_hash_table (info);
3103 dynobj = elf_hash_table (info)->dynobj;
3104
3105 /* Make sure we know what is going on here. */
3106 BFD_ASSERT (dynobj != NULL
3107 && (h->needs_plt
3108 || h->u.weakdef != NULL
3109 || (h->def_dynamic
3110 && h->ref_regular
3111 && !h->def_regular)));
3112
3113 /* If this is a function, put it in the procedure linkage table. We
3114 will fill in the contents of the procedure linkage table later,
3115 when we know the address of the .got section. */
3116 if (h->type == STT_FUNC
3117 || h->needs_plt)
3118 {
3119 if ((h->plt.refcount <= 0
3120 || SYMBOL_CALLS_LOCAL (info, h)
3121 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3122 && h->root.type == bfd_link_hash_undefweak))
3123 /* We must always create the plt entry if it was referenced
3124 by a PLTxxO relocation. In this case we already recorded
3125 it as a dynamic symbol. */
3126 && h->dynindx == -1)
3127 {
3128 /* This case can occur if we saw a PLTxx reloc in an input
3129 file, but the symbol was never referred to by a dynamic
3130 object, or if all references were garbage collected. In
3131 such a case, we don't actually need to build a procedure
3132 linkage table, and we can just do a PCxx reloc instead. */
3133 h->plt.offset = (bfd_vma) -1;
3134 h->needs_plt = 0;
3135 return TRUE;
3136 }
3137
3138 /* Make sure this symbol is output as a dynamic symbol. */
3139 if (h->dynindx == -1
3140 && !h->forced_local)
3141 {
3142 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3143 return FALSE;
3144 }
3145
3146 s = bfd_get_linker_section (dynobj, ".plt");
3147 BFD_ASSERT (s != NULL);
3148
3149 /* If this is the first .plt entry, make room for the special
3150 first entry. */
3151 if (s->size == 0)
3152 s->size = htab->plt_info->size;
3153
3154 /* If this symbol is not defined in a regular file, and we are
3155 not generating a shared library, then set the symbol to this
3156 location in the .plt. This is required to make function
3157 pointers compare as equal between the normal executable and
3158 the shared library. */
3159 if (!info->shared
3160 && !h->def_regular)
3161 {
3162 h->root.u.def.section = s;
3163 h->root.u.def.value = s->size;
3164 }
3165
3166 h->plt.offset = s->size;
3167
3168 /* Make room for this entry. */
3169 s->size += htab->plt_info->size;
3170
3171 /* We also need to make an entry in the .got.plt section, which
3172 will be placed in the .got section by the linker script. */
3173 s = bfd_get_linker_section (dynobj, ".got.plt");
3174 BFD_ASSERT (s != NULL);
3175 s->size += 4;
3176
3177 /* We also need to make an entry in the .rela.plt section. */
3178 s = bfd_get_linker_section (dynobj, ".rela.plt");
3179 BFD_ASSERT (s != NULL);
3180 s->size += sizeof (Elf32_External_Rela);
3181
3182 return TRUE;
3183 }
3184
3185 /* Reinitialize the plt offset now that it is not used as a reference
3186 count any more. */
3187 h->plt.offset = (bfd_vma) -1;
3188
3189 /* If this is a weak symbol, and there is a real definition, the
3190 processor independent code will have arranged for us to see the
3191 real definition first, and we can just use the same value. */
3192 if (h->u.weakdef != NULL)
3193 {
3194 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3195 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3196 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3197 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3198 return TRUE;
3199 }
3200
3201 /* This is a reference to a symbol defined by a dynamic object which
3202 is not a function. */
3203
3204 /* If we are creating a shared library, we must presume that the
3205 only references to the symbol are via the global offset table.
3206 For such cases we need not do anything here; the relocations will
3207 be handled correctly by relocate_section. */
3208 if (info->shared)
3209 return TRUE;
3210
3211 /* If there are no references to this symbol that do not use the
3212 GOT, we don't need to generate a copy reloc. */
3213 if (!h->non_got_ref)
3214 return TRUE;
3215
3216 /* We must allocate the symbol in our .dynbss section, which will
3217 become part of the .bss section of the executable. There will be
3218 an entry for this symbol in the .dynsym section. The dynamic
3219 object will contain position independent code, so all references
3220 from the dynamic object to this symbol will go through the global
3221 offset table. The dynamic linker will use the .dynsym entry to
3222 determine the address it must put in the global offset table, so
3223 both the dynamic object and the regular object will refer to the
3224 same memory location for the variable. */
3225
3226 s = bfd_get_linker_section (dynobj, ".dynbss");
3227 BFD_ASSERT (s != NULL);
3228
3229 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
3230 copy the initial value out of the dynamic object and into the
3231 runtime process image. We need to remember the offset into the
3232 .rela.bss section we are going to use. */
3233 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3234 {
3235 asection *srel;
3236
3237 srel = bfd_get_linker_section (dynobj, ".rela.bss");
3238 BFD_ASSERT (srel != NULL);
3239 srel->size += sizeof (Elf32_External_Rela);
3240 h->needs_copy = 1;
3241 }
3242
3243 return _bfd_elf_adjust_dynamic_copy (h, s);
3244 }
3245
3246 /* Set the sizes of the dynamic sections. */
3247
3248 static bfd_boolean
3249 elf_m68k_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3250 struct bfd_link_info *info)
3251 {
3252 bfd *dynobj;
3253 asection *s;
3254 bfd_boolean plt;
3255 bfd_boolean relocs;
3256
3257 dynobj = elf_hash_table (info)->dynobj;
3258 BFD_ASSERT (dynobj != NULL);
3259
3260 if (elf_hash_table (info)->dynamic_sections_created)
3261 {
3262 /* Set the contents of the .interp section to the interpreter. */
3263 if (info->executable)
3264 {
3265 s = bfd_get_linker_section (dynobj, ".interp");
3266 BFD_ASSERT (s != NULL);
3267 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3268 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3269 }
3270 }
3271 else
3272 {
3273 /* We may have created entries in the .rela.got section.
3274 However, if we are not creating the dynamic sections, we will
3275 not actually use these entries. Reset the size of .rela.got,
3276 which will cause it to get stripped from the output file
3277 below. */
3278 s = bfd_get_linker_section (dynobj, ".rela.got");
3279 if (s != NULL)
3280 s->size = 0;
3281 }
3282
3283 /* If this is a -Bsymbolic shared link, then we need to discard all
3284 PC relative relocs against symbols defined in a regular object.
3285 For the normal shared case we discard the PC relative relocs
3286 against symbols that have become local due to visibility changes.
3287 We allocated space for them in the check_relocs routine, but we
3288 will not fill them in in the relocate_section routine. */
3289 if (info->shared)
3290 elf_link_hash_traverse (elf_hash_table (info),
3291 elf_m68k_discard_copies,
3292 info);
3293
3294 /* The check_relocs and adjust_dynamic_symbol entry points have
3295 determined the sizes of the various dynamic sections. Allocate
3296 memory for them. */
3297 plt = FALSE;
3298 relocs = FALSE;
3299 for (s = dynobj->sections; s != NULL; s = s->next)
3300 {
3301 const char *name;
3302
3303 if ((s->flags & SEC_LINKER_CREATED) == 0)
3304 continue;
3305
3306 /* It's OK to base decisions on the section name, because none
3307 of the dynobj section names depend upon the input files. */
3308 name = bfd_get_section_name (dynobj, s);
3309
3310 if (strcmp (name, ".plt") == 0)
3311 {
3312 /* Remember whether there is a PLT. */
3313 plt = s->size != 0;
3314 }
3315 else if (CONST_STRNEQ (name, ".rela"))
3316 {
3317 if (s->size != 0)
3318 {
3319 relocs = TRUE;
3320
3321 /* We use the reloc_count field as a counter if we need
3322 to copy relocs into the output file. */
3323 s->reloc_count = 0;
3324 }
3325 }
3326 else if (! CONST_STRNEQ (name, ".got")
3327 && strcmp (name, ".dynbss") != 0)
3328 {
3329 /* It's not one of our sections, so don't allocate space. */
3330 continue;
3331 }
3332
3333 if (s->size == 0)
3334 {
3335 /* If we don't need this section, strip it from the
3336 output file. This is mostly to handle .rela.bss and
3337 .rela.plt. We must create both sections in
3338 create_dynamic_sections, because they must be created
3339 before the linker maps input sections to output
3340 sections. The linker does that before
3341 adjust_dynamic_symbol is called, and it is that
3342 function which decides whether anything needs to go
3343 into these sections. */
3344 s->flags |= SEC_EXCLUDE;
3345 continue;
3346 }
3347
3348 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3349 continue;
3350
3351 /* Allocate memory for the section contents. */
3352 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3353 Unused entries should be reclaimed before the section's contents
3354 are written out, but at the moment this does not happen. Thus in
3355 order to prevent writing out garbage, we initialise the section's
3356 contents to zero. */
3357 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3358 if (s->contents == NULL)
3359 return FALSE;
3360 }
3361
3362 if (elf_hash_table (info)->dynamic_sections_created)
3363 {
3364 /* Add some entries to the .dynamic section. We fill in the
3365 values later, in elf_m68k_finish_dynamic_sections, but we
3366 must add the entries now so that we get the correct size for
3367 the .dynamic section. The DT_DEBUG entry is filled in by the
3368 dynamic linker and used by the debugger. */
3369 #define add_dynamic_entry(TAG, VAL) \
3370 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3371
3372 if (info->executable)
3373 {
3374 if (!add_dynamic_entry (DT_DEBUG, 0))
3375 return FALSE;
3376 }
3377
3378 if (plt)
3379 {
3380 if (!add_dynamic_entry (DT_PLTGOT, 0)
3381 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3382 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3383 || !add_dynamic_entry (DT_JMPREL, 0))
3384 return FALSE;
3385 }
3386
3387 if (relocs)
3388 {
3389 if (!add_dynamic_entry (DT_RELA, 0)
3390 || !add_dynamic_entry (DT_RELASZ, 0)
3391 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3392 return FALSE;
3393 }
3394
3395 if ((info->flags & DF_TEXTREL) != 0)
3396 {
3397 if (!add_dynamic_entry (DT_TEXTREL, 0))
3398 return FALSE;
3399 }
3400 }
3401 #undef add_dynamic_entry
3402
3403 return TRUE;
3404 }
3405
3406 /* This function is called via elf_link_hash_traverse if we are
3407 creating a shared object. In the -Bsymbolic case it discards the
3408 space allocated to copy PC relative relocs against symbols which
3409 are defined in regular objects. For the normal shared case, it
3410 discards space for pc-relative relocs that have become local due to
3411 symbol visibility changes. We allocated space for them in the
3412 check_relocs routine, but we won't fill them in in the
3413 relocate_section routine.
3414
3415 We also check whether any of the remaining relocations apply
3416 against a readonly section, and set the DF_TEXTREL flag in this
3417 case. */
3418
3419 static bfd_boolean
3420 elf_m68k_discard_copies (struct elf_link_hash_entry *h,
3421 void * inf)
3422 {
3423 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3424 struct elf_m68k_pcrel_relocs_copied *s;
3425
3426 if (!SYMBOL_CALLS_LOCAL (info, h))
3427 {
3428 if ((info->flags & DF_TEXTREL) == 0)
3429 {
3430 /* Look for relocations against read-only sections. */
3431 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3432 s != NULL;
3433 s = s->next)
3434 if ((s->section->flags & SEC_READONLY) != 0)
3435 {
3436 info->flags |= DF_TEXTREL;
3437 break;
3438 }
3439 }
3440
3441 /* Make sure undefined weak symbols are output as a dynamic symbol
3442 in PIEs. */
3443 if (h->non_got_ref
3444 && h->root.type == bfd_link_hash_undefweak
3445 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3446 && h->dynindx == -1
3447 && !h->forced_local)
3448 {
3449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3450 return FALSE;
3451 }
3452
3453 return TRUE;
3454 }
3455
3456 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3457 s != NULL;
3458 s = s->next)
3459 s->section->size -= s->count * sizeof (Elf32_External_Rela);
3460
3461 return TRUE;
3462 }
3463
3464
3465 /* Install relocation RELA. */
3466
3467 static void
3468 elf_m68k_install_rela (bfd *output_bfd,
3469 asection *srela,
3470 Elf_Internal_Rela *rela)
3471 {
3472 bfd_byte *loc;
3473
3474 loc = srela->contents;
3475 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3476 bfd_elf32_swap_reloca_out (output_bfd, rela, loc);
3477 }
3478
3479 /* Find the base offsets for thread-local storage in this object,
3480 for GD/LD and IE/LE respectively. */
3481
3482 #define DTP_OFFSET 0x8000
3483 #define TP_OFFSET 0x7000
3484
3485 static bfd_vma
3486 dtpoff_base (struct bfd_link_info *info)
3487 {
3488 /* If tls_sec is NULL, we should have signalled an error already. */
3489 if (elf_hash_table (info)->tls_sec == NULL)
3490 return 0;
3491 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
3492 }
3493
3494 static bfd_vma
3495 tpoff_base (struct bfd_link_info *info)
3496 {
3497 /* If tls_sec is NULL, we should have signalled an error already. */
3498 if (elf_hash_table (info)->tls_sec == NULL)
3499 return 0;
3500 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
3501 }
3502
3503 /* Output necessary relocation to handle a symbol during static link.
3504 This function is called from elf_m68k_relocate_section. */
3505
3506 static void
3507 elf_m68k_init_got_entry_static (struct bfd_link_info *info,
3508 bfd *output_bfd,
3509 enum elf_m68k_reloc_type r_type,
3510 asection *sgot,
3511 bfd_vma got_entry_offset,
3512 bfd_vma relocation)
3513 {
3514 switch (elf_m68k_reloc_got_type (r_type))
3515 {
3516 case R_68K_GOT32O:
3517 bfd_put_32 (output_bfd, relocation, sgot->contents + got_entry_offset);
3518 break;
3519
3520 case R_68K_TLS_GD32:
3521 /* We know the offset within the module,
3522 put it into the second GOT slot. */
3523 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3524 sgot->contents + got_entry_offset + 4);
3525 /* FALLTHRU */
3526
3527 case R_68K_TLS_LDM32:
3528 /* Mark it as belonging to module 1, the executable. */
3529 bfd_put_32 (output_bfd, 1, sgot->contents + got_entry_offset);
3530 break;
3531
3532 case R_68K_TLS_IE32:
3533 bfd_put_32 (output_bfd, relocation - tpoff_base (info),
3534 sgot->contents + got_entry_offset);
3535 break;
3536
3537 default:
3538 BFD_ASSERT (FALSE);
3539 }
3540 }
3541
3542 /* Output necessary relocation to handle a local symbol
3543 during dynamic link.
3544 This function is called either from elf_m68k_relocate_section
3545 or from elf_m68k_finish_dynamic_symbol. */
3546
3547 static void
3548 elf_m68k_init_got_entry_local_shared (struct bfd_link_info *info,
3549 bfd *output_bfd,
3550 enum elf_m68k_reloc_type r_type,
3551 asection *sgot,
3552 bfd_vma got_entry_offset,
3553 bfd_vma relocation,
3554 asection *srela)
3555 {
3556 Elf_Internal_Rela outrel;
3557
3558 switch (elf_m68k_reloc_got_type (r_type))
3559 {
3560 case R_68K_GOT32O:
3561 /* Emit RELATIVE relocation to initialize GOT slot
3562 at run-time. */
3563 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3564 outrel.r_addend = relocation;
3565 break;
3566
3567 case R_68K_TLS_GD32:
3568 /* We know the offset within the module,
3569 put it into the second GOT slot. */
3570 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3571 sgot->contents + got_entry_offset + 4);
3572 /* FALLTHRU */
3573
3574 case R_68K_TLS_LDM32:
3575 /* We don't know the module number,
3576 create a relocation for it. */
3577 outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_DTPMOD32);
3578 outrel.r_addend = 0;
3579 break;
3580
3581 case R_68K_TLS_IE32:
3582 /* Emit TPREL relocation to initialize GOT slot
3583 at run-time. */
3584 outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_TPREL32);
3585 outrel.r_addend = relocation - elf_hash_table (info)->tls_sec->vma;
3586 break;
3587
3588 default:
3589 BFD_ASSERT (FALSE);
3590 }
3591
3592 /* Offset of the GOT entry. */
3593 outrel.r_offset = (sgot->output_section->vma
3594 + sgot->output_offset
3595 + got_entry_offset);
3596
3597 /* Install one of the above relocations. */
3598 elf_m68k_install_rela (output_bfd, srela, &outrel);
3599
3600 bfd_put_32 (output_bfd, outrel.r_addend, sgot->contents + got_entry_offset);
3601 }
3602
3603 /* Relocate an M68K ELF section. */
3604
3605 static bfd_boolean
3606 elf_m68k_relocate_section (bfd *output_bfd,
3607 struct bfd_link_info *info,
3608 bfd *input_bfd,
3609 asection *input_section,
3610 bfd_byte *contents,
3611 Elf_Internal_Rela *relocs,
3612 Elf_Internal_Sym *local_syms,
3613 asection **local_sections)
3614 {
3615 bfd *dynobj;
3616 Elf_Internal_Shdr *symtab_hdr;
3617 struct elf_link_hash_entry **sym_hashes;
3618 asection *sgot;
3619 asection *splt;
3620 asection *sreloc;
3621 asection *srela;
3622 struct elf_m68k_got *got;
3623 Elf_Internal_Rela *rel;
3624 Elf_Internal_Rela *relend;
3625
3626 dynobj = elf_hash_table (info)->dynobj;
3627 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3628 sym_hashes = elf_sym_hashes (input_bfd);
3629
3630 sgot = NULL;
3631 splt = NULL;
3632 sreloc = NULL;
3633 srela = NULL;
3634
3635 got = NULL;
3636
3637 rel = relocs;
3638 relend = relocs + input_section->reloc_count;
3639 for (; rel < relend; rel++)
3640 {
3641 int r_type;
3642 reloc_howto_type *howto;
3643 unsigned long r_symndx;
3644 struct elf_link_hash_entry *h;
3645 Elf_Internal_Sym *sym;
3646 asection *sec;
3647 bfd_vma relocation;
3648 bfd_boolean unresolved_reloc;
3649 bfd_reloc_status_type r;
3650
3651 r_type = ELF32_R_TYPE (rel->r_info);
3652 if (r_type < 0 || r_type >= (int) R_68K_max)
3653 {
3654 bfd_set_error (bfd_error_bad_value);
3655 return FALSE;
3656 }
3657 howto = howto_table + r_type;
3658
3659 r_symndx = ELF32_R_SYM (rel->r_info);
3660
3661 h = NULL;
3662 sym = NULL;
3663 sec = NULL;
3664 unresolved_reloc = FALSE;
3665
3666 if (r_symndx < symtab_hdr->sh_info)
3667 {
3668 sym = local_syms + r_symndx;
3669 sec = local_sections[r_symndx];
3670 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3671 }
3672 else
3673 {
3674 bfd_boolean warned, ignored;
3675
3676 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3677 r_symndx, symtab_hdr, sym_hashes,
3678 h, sec, relocation,
3679 unresolved_reloc, warned, ignored);
3680 }
3681
3682 if (sec != NULL && discarded_section (sec))
3683 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3684 rel, 1, relend, howto, 0, contents);
3685
3686 if (info->relocatable)
3687 continue;
3688
3689 switch (r_type)
3690 {
3691 case R_68K_GOT8:
3692 case R_68K_GOT16:
3693 case R_68K_GOT32:
3694 /* Relocation is to the address of the entry for this symbol
3695 in the global offset table. */
3696 if (h != NULL
3697 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3698 {
3699 if (elf_m68k_hash_table (info)->local_gp_p)
3700 {
3701 bfd_vma sgot_output_offset;
3702 bfd_vma got_offset;
3703
3704 if (sgot == NULL)
3705 {
3706 sgot = bfd_get_linker_section (dynobj, ".got");
3707
3708 if (sgot != NULL)
3709 sgot_output_offset = sgot->output_offset;
3710 else
3711 /* In this case we have a reference to
3712 _GLOBAL_OFFSET_TABLE_, but the GOT itself is
3713 empty.
3714 ??? Issue a warning? */
3715 sgot_output_offset = 0;
3716 }
3717 else
3718 sgot_output_offset = sgot->output_offset;
3719
3720 if (got == NULL)
3721 {
3722 struct elf_m68k_bfd2got_entry *bfd2got_entry;
3723
3724 bfd2got_entry
3725 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3726 input_bfd, SEARCH, NULL);
3727
3728 if (bfd2got_entry != NULL)
3729 {
3730 got = bfd2got_entry->got;
3731 BFD_ASSERT (got != NULL);
3732
3733 got_offset = got->offset;
3734 }
3735 else
3736 /* In this case we have a reference to
3737 _GLOBAL_OFFSET_TABLE_, but no other references
3738 accessing any GOT entries.
3739 ??? Issue a warning? */
3740 got_offset = 0;
3741 }
3742 else
3743 got_offset = got->offset;
3744
3745 /* Adjust GOT pointer to point to the GOT
3746 assigned to input_bfd. */
3747 rel->r_addend += sgot_output_offset + got_offset;
3748 }
3749 else
3750 BFD_ASSERT (got == NULL || got->offset == 0);
3751
3752 break;
3753 }
3754 /* Fall through. */
3755 case R_68K_GOT8O:
3756 case R_68K_GOT16O:
3757 case R_68K_GOT32O:
3758
3759 case R_68K_TLS_LDM32:
3760 case R_68K_TLS_LDM16:
3761 case R_68K_TLS_LDM8:
3762
3763 case R_68K_TLS_GD8:
3764 case R_68K_TLS_GD16:
3765 case R_68K_TLS_GD32:
3766
3767 case R_68K_TLS_IE8:
3768 case R_68K_TLS_IE16:
3769 case R_68K_TLS_IE32:
3770
3771 /* Relocation is the offset of the entry for this symbol in
3772 the global offset table. */
3773
3774 {
3775 struct elf_m68k_got_entry_key key_;
3776 bfd_vma *off_ptr;
3777 bfd_vma off;
3778
3779 if (sgot == NULL)
3780 {
3781 sgot = bfd_get_linker_section (dynobj, ".got");
3782 BFD_ASSERT (sgot != NULL);
3783 }
3784
3785 if (got == NULL)
3786 {
3787 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3788 input_bfd, MUST_FIND,
3789 NULL)->got;
3790 BFD_ASSERT (got != NULL);
3791 }
3792
3793 /* Get GOT offset for this symbol. */
3794 elf_m68k_init_got_entry_key (&key_, h, input_bfd, r_symndx,
3795 r_type);
3796 off_ptr = &elf_m68k_get_got_entry (got, &key_, MUST_FIND,
3797 NULL)->u.s2.offset;
3798 off = *off_ptr;
3799
3800 /* The offset must always be a multiple of 4. We use
3801 the least significant bit to record whether we have
3802 already generated the necessary reloc. */
3803 if ((off & 1) != 0)
3804 off &= ~1;
3805 else
3806 {
3807 if (h != NULL
3808 /* @TLSLDM relocations are bounded to the module, in
3809 which the symbol is defined -- not to the symbol
3810 itself. */
3811 && elf_m68k_reloc_got_type (r_type) != R_68K_TLS_LDM32)
3812 {
3813 bfd_boolean dyn;
3814
3815 dyn = elf_hash_table (info)->dynamic_sections_created;
3816 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3817 || (info->shared
3818 && SYMBOL_REFERENCES_LOCAL (info, h))
3819 || (ELF_ST_VISIBILITY (h->other)
3820 && h->root.type == bfd_link_hash_undefweak))
3821 {
3822 /* This is actually a static link, or it is a
3823 -Bsymbolic link and the symbol is defined
3824 locally, or the symbol was forced to be local
3825 because of a version file. We must initialize
3826 this entry in the global offset table. Since
3827 the offset must always be a multiple of 4, we
3828 use the least significant bit to record whether
3829 we have initialized it already.
3830
3831 When doing a dynamic link, we create a .rela.got
3832 relocation entry to initialize the value. This
3833 is done in the finish_dynamic_symbol routine. */
3834
3835 elf_m68k_init_got_entry_static (info,
3836 output_bfd,
3837 r_type,
3838 sgot,
3839 off,
3840 relocation);
3841
3842 *off_ptr |= 1;
3843 }
3844 else
3845 unresolved_reloc = FALSE;
3846 }
3847 else if (info->shared) /* && h == NULL */
3848 /* Process local symbol during dynamic link. */
3849 {
3850 if (srela == NULL)
3851 {
3852 srela = bfd_get_linker_section (dynobj, ".rela.got");
3853 BFD_ASSERT (srela != NULL);
3854 }
3855
3856 elf_m68k_init_got_entry_local_shared (info,
3857 output_bfd,
3858 r_type,
3859 sgot,
3860 off,
3861 relocation,
3862 srela);
3863
3864 *off_ptr |= 1;
3865 }
3866 else /* h == NULL && !info->shared */
3867 {
3868 elf_m68k_init_got_entry_static (info,
3869 output_bfd,
3870 r_type,
3871 sgot,
3872 off,
3873 relocation);
3874
3875 *off_ptr |= 1;
3876 }
3877 }
3878
3879 /* We don't use elf_m68k_reloc_got_type in the condition below
3880 because this is the only place where difference between
3881 R_68K_GOTx and R_68K_GOTxO relocations matters. */
3882 if (r_type == R_68K_GOT32O
3883 || r_type == R_68K_GOT16O
3884 || r_type == R_68K_GOT8O
3885 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_GD32
3886 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_LDM32
3887 || elf_m68k_reloc_got_type (r_type) == R_68K_TLS_IE32)
3888 {
3889 /* GOT pointer is adjusted to point to the start/middle
3890 of local GOT. Adjust the offset accordingly. */
3891 BFD_ASSERT (elf_m68k_hash_table (info)->use_neg_got_offsets_p
3892 || off >= got->offset);
3893
3894 if (elf_m68k_hash_table (info)->local_gp_p)
3895 relocation = off - got->offset;
3896 else
3897 {
3898 BFD_ASSERT (got->offset == 0);
3899 relocation = sgot->output_offset + off;
3900 }
3901
3902 /* This relocation does not use the addend. */
3903 rel->r_addend = 0;
3904 }
3905 else
3906 relocation = (sgot->output_section->vma + sgot->output_offset
3907 + off);
3908 }
3909 break;
3910
3911 case R_68K_TLS_LDO32:
3912 case R_68K_TLS_LDO16:
3913 case R_68K_TLS_LDO8:
3914 relocation -= dtpoff_base (info);
3915 break;
3916
3917 case R_68K_TLS_LE32:
3918 case R_68K_TLS_LE16:
3919 case R_68K_TLS_LE8:
3920 if (info->shared && !info->pie)
3921 {
3922 (*_bfd_error_handler)
3923 (_("%B(%A+0x%lx): R_68K_TLS_LE32 relocation not permitted "
3924 "in shared object"),
3925 input_bfd, input_section, (long) rel->r_offset, howto->name);
3926
3927 return FALSE;
3928 }
3929 else
3930 relocation -= tpoff_base (info);
3931
3932 break;
3933
3934 case R_68K_PLT8:
3935 case R_68K_PLT16:
3936 case R_68K_PLT32:
3937 /* Relocation is to the entry for this symbol in the
3938 procedure linkage table. */
3939
3940 /* Resolve a PLTxx reloc against a local symbol directly,
3941 without using the procedure linkage table. */
3942 if (h == NULL)
3943 break;
3944
3945 if (h->plt.offset == (bfd_vma) -1
3946 || !elf_hash_table (info)->dynamic_sections_created)
3947 {
3948 /* We didn't make a PLT entry for this symbol. This
3949 happens when statically linking PIC code, or when
3950 using -Bsymbolic. */
3951 break;
3952 }
3953
3954 if (splt == NULL)
3955 {
3956 splt = bfd_get_linker_section (dynobj, ".plt");
3957 BFD_ASSERT (splt != NULL);
3958 }
3959
3960 relocation = (splt->output_section->vma
3961 + splt->output_offset
3962 + h->plt.offset);
3963 unresolved_reloc = FALSE;
3964 break;
3965
3966 case R_68K_PLT8O:
3967 case R_68K_PLT16O:
3968 case R_68K_PLT32O:
3969 /* Relocation is the offset of the entry for this symbol in
3970 the procedure linkage table. */
3971 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
3972
3973 if (splt == NULL)
3974 {
3975 splt = bfd_get_linker_section (dynobj, ".plt");
3976 BFD_ASSERT (splt != NULL);
3977 }
3978
3979 relocation = h->plt.offset;
3980 unresolved_reloc = FALSE;
3981
3982 /* This relocation does not use the addend. */
3983 rel->r_addend = 0;
3984
3985 break;
3986
3987 case R_68K_8:
3988 case R_68K_16:
3989 case R_68K_32:
3990 case R_68K_PC8:
3991 case R_68K_PC16:
3992 case R_68K_PC32:
3993 if (info->shared
3994 && r_symndx != STN_UNDEF
3995 && (input_section->flags & SEC_ALLOC) != 0
3996 && (h == NULL
3997 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3998 || h->root.type != bfd_link_hash_undefweak)
3999 && ((r_type != R_68K_PC8
4000 && r_type != R_68K_PC16
4001 && r_type != R_68K_PC32)
4002 || !SYMBOL_CALLS_LOCAL (info, h)))
4003 {
4004 Elf_Internal_Rela outrel;
4005 bfd_byte *loc;
4006 bfd_boolean skip, relocate;
4007
4008 /* When generating a shared object, these relocations
4009 are copied into the output file to be resolved at run
4010 time. */
4011
4012 skip = FALSE;
4013 relocate = FALSE;
4014
4015 outrel.r_offset =
4016 _bfd_elf_section_offset (output_bfd, info, input_section,
4017 rel->r_offset);
4018 if (outrel.r_offset == (bfd_vma) -1)
4019 skip = TRUE;
4020 else if (outrel.r_offset == (bfd_vma) -2)
4021 skip = TRUE, relocate = TRUE;
4022 outrel.r_offset += (input_section->output_section->vma
4023 + input_section->output_offset);
4024
4025 if (skip)
4026 memset (&outrel, 0, sizeof outrel);
4027 else if (h != NULL
4028 && h->dynindx != -1
4029 && (r_type == R_68K_PC8
4030 || r_type == R_68K_PC16
4031 || r_type == R_68K_PC32
4032 || !info->shared
4033 || !info->symbolic
4034 || !h->def_regular))
4035 {
4036 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4037 outrel.r_addend = rel->r_addend;
4038 }
4039 else
4040 {
4041 /* This symbol is local, or marked to become local. */
4042 outrel.r_addend = relocation + rel->r_addend;
4043
4044 if (r_type == R_68K_32)
4045 {
4046 relocate = TRUE;
4047 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
4048 }
4049 else
4050 {
4051 long indx;
4052
4053 if (bfd_is_abs_section (sec))
4054 indx = 0;
4055 else if (sec == NULL || sec->owner == NULL)
4056 {
4057 bfd_set_error (bfd_error_bad_value);
4058 return FALSE;
4059 }
4060 else
4061 {
4062 asection *osec;
4063
4064 /* We are turning this relocation into one
4065 against a section symbol. It would be
4066 proper to subtract the symbol's value,
4067 osec->vma, from the emitted reloc addend,
4068 but ld.so expects buggy relocs. */
4069 osec = sec->output_section;
4070 indx = elf_section_data (osec)->dynindx;
4071 if (indx == 0)
4072 {
4073 struct elf_link_hash_table *htab;
4074 htab = elf_hash_table (info);
4075 osec = htab->text_index_section;
4076 indx = elf_section_data (osec)->dynindx;
4077 }
4078 BFD_ASSERT (indx != 0);
4079 }
4080
4081 outrel.r_info = ELF32_R_INFO (indx, r_type);
4082 }
4083 }
4084
4085 sreloc = elf_section_data (input_section)->sreloc;
4086 if (sreloc == NULL)
4087 abort ();
4088
4089 loc = sreloc->contents;
4090 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4091 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4092
4093 /* This reloc will be computed at runtime, so there's no
4094 need to do anything now, except for R_68K_32
4095 relocations that have been turned into
4096 R_68K_RELATIVE. */
4097 if (!relocate)
4098 continue;
4099 }
4100
4101 break;
4102
4103 case R_68K_GNU_VTINHERIT:
4104 case R_68K_GNU_VTENTRY:
4105 /* These are no-ops in the end. */
4106 continue;
4107
4108 default:
4109 break;
4110 }
4111
4112 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4113 because such sections are not SEC_ALLOC and thus ld.so will
4114 not process them. */
4115 if (unresolved_reloc
4116 && !((input_section->flags & SEC_DEBUGGING) != 0
4117 && h->def_dynamic)
4118 && _bfd_elf_section_offset (output_bfd, info, input_section,
4119 rel->r_offset) != (bfd_vma) -1)
4120 {
4121 (*_bfd_error_handler)
4122 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4123 input_bfd,
4124 input_section,
4125 (long) rel->r_offset,
4126 howto->name,
4127 h->root.root.string);
4128 return FALSE;
4129 }
4130
4131 if (r_symndx != STN_UNDEF
4132 && r_type != R_68K_NONE
4133 && (h == NULL
4134 || h->root.type == bfd_link_hash_defined
4135 || h->root.type == bfd_link_hash_defweak))
4136 {
4137 char sym_type;
4138
4139 sym_type = (sym != NULL) ? ELF32_ST_TYPE (sym->st_info) : h->type;
4140
4141 if (elf_m68k_reloc_tls_p (r_type) != (sym_type == STT_TLS))
4142 {
4143 const char *name;
4144
4145 if (h != NULL)
4146 name = h->root.root.string;
4147 else
4148 {
4149 name = (bfd_elf_string_from_elf_section
4150 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4151 if (name == NULL || *name == '\0')
4152 name = bfd_section_name (input_bfd, sec);
4153 }
4154
4155 (*_bfd_error_handler)
4156 ((sym_type == STT_TLS
4157 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
4158 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
4159 input_bfd,
4160 input_section,
4161 (long) rel->r_offset,
4162 howto->name,
4163 name);
4164 }
4165 }
4166
4167 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4168 contents, rel->r_offset,
4169 relocation, rel->r_addend);
4170
4171 if (r != bfd_reloc_ok)
4172 {
4173 const char *name;
4174
4175 if (h != NULL)
4176 name = h->root.root.string;
4177 else
4178 {
4179 name = bfd_elf_string_from_elf_section (input_bfd,
4180 symtab_hdr->sh_link,
4181 sym->st_name);
4182 if (name == NULL)
4183 return FALSE;
4184 if (*name == '\0')
4185 name = bfd_section_name (input_bfd, sec);
4186 }
4187
4188 if (r == bfd_reloc_overflow)
4189 {
4190 if (!(info->callbacks->reloc_overflow
4191 (info, (h ? &h->root : NULL), name, howto->name,
4192 (bfd_vma) 0, input_bfd, input_section,
4193 rel->r_offset)))
4194 return FALSE;
4195 }
4196 else
4197 {
4198 (*_bfd_error_handler)
4199 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4200 input_bfd, input_section,
4201 (long) rel->r_offset, name, (int) r);
4202 return FALSE;
4203 }
4204 }
4205 }
4206
4207 return TRUE;
4208 }
4209
4210 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
4211 into section SEC. */
4212
4213 static void
4214 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
4215 {
4216 /* Make VALUE PC-relative. */
4217 value -= sec->output_section->vma + offset;
4218
4219 /* Apply any in-place addend. */
4220 value += bfd_get_32 (sec->owner, sec->contents + offset);
4221
4222 bfd_put_32 (sec->owner, value, sec->contents + offset);
4223 }
4224
4225 /* Finish up dynamic symbol handling. We set the contents of various
4226 dynamic sections here. */
4227
4228 static bfd_boolean
4229 elf_m68k_finish_dynamic_symbol (bfd *output_bfd,
4230 struct bfd_link_info *info,
4231 struct elf_link_hash_entry *h,
4232 Elf_Internal_Sym *sym)
4233 {
4234 bfd *dynobj;
4235
4236 dynobj = elf_hash_table (info)->dynobj;
4237
4238 if (h->plt.offset != (bfd_vma) -1)
4239 {
4240 const struct elf_m68k_plt_info *plt_info;
4241 asection *splt;
4242 asection *sgot;
4243 asection *srela;
4244 bfd_vma plt_index;
4245 bfd_vma got_offset;
4246 Elf_Internal_Rela rela;
4247 bfd_byte *loc;
4248
4249 /* This symbol has an entry in the procedure linkage table. Set
4250 it up. */
4251
4252 BFD_ASSERT (h->dynindx != -1);
4253
4254 plt_info = elf_m68k_hash_table (info)->plt_info;
4255 splt = bfd_get_linker_section (dynobj, ".plt");
4256 sgot = bfd_get_linker_section (dynobj, ".got.plt");
4257 srela = bfd_get_linker_section (dynobj, ".rela.plt");
4258 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
4259
4260 /* Get the index in the procedure linkage table which
4261 corresponds to this symbol. This is the index of this symbol
4262 in all the symbols for which we are making plt entries. The
4263 first entry in the procedure linkage table is reserved. */
4264 plt_index = (h->plt.offset / plt_info->size) - 1;
4265
4266 /* Get the offset into the .got table of the entry that
4267 corresponds to this function. Each .got entry is 4 bytes.
4268 The first three are reserved. */
4269 got_offset = (plt_index + 3) * 4;
4270
4271 memcpy (splt->contents + h->plt.offset,
4272 plt_info->symbol_entry,
4273 plt_info->size);
4274
4275 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
4276 (sgot->output_section->vma
4277 + sgot->output_offset
4278 + got_offset));
4279
4280 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4281 splt->contents
4282 + h->plt.offset
4283 + plt_info->symbol_resolve_entry + 2);
4284
4285 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
4286 splt->output_section->vma);
4287
4288 /* Fill in the entry in the global offset table. */
4289 bfd_put_32 (output_bfd,
4290 (splt->output_section->vma
4291 + splt->output_offset
4292 + h->plt.offset
4293 + plt_info->symbol_resolve_entry),
4294 sgot->contents + got_offset);
4295
4296 /* Fill in the entry in the .rela.plt section. */
4297 rela.r_offset = (sgot->output_section->vma
4298 + sgot->output_offset
4299 + got_offset);
4300 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
4301 rela.r_addend = 0;
4302 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
4303 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4304
4305 if (!h->def_regular)
4306 {
4307 /* Mark the symbol as undefined, rather than as defined in
4308 the .plt section. Leave the value alone. */
4309 sym->st_shndx = SHN_UNDEF;
4310 }
4311 }
4312
4313 if (elf_m68k_hash_entry (h)->glist != NULL)
4314 {
4315 asection *sgot;
4316 asection *srela;
4317 struct elf_m68k_got_entry *got_entry;
4318
4319 /* This symbol has an entry in the global offset table. Set it
4320 up. */
4321
4322 sgot = bfd_get_linker_section (dynobj, ".got");
4323 srela = bfd_get_linker_section (dynobj, ".rela.got");
4324 BFD_ASSERT (sgot != NULL && srela != NULL);
4325
4326 got_entry = elf_m68k_hash_entry (h)->glist;
4327
4328 while (got_entry != NULL)
4329 {
4330 enum elf_m68k_reloc_type r_type;
4331 bfd_vma got_entry_offset;
4332
4333 r_type = got_entry->key_.type;
4334 got_entry_offset = got_entry->u.s2.offset &~ (bfd_vma) 1;
4335
4336 /* If this is a -Bsymbolic link, and the symbol is defined
4337 locally, we just want to emit a RELATIVE reloc. Likewise if
4338 the symbol was forced to be local because of a version file.
4339 The entry in the global offset table already have been
4340 initialized in the relocate_section function. */
4341 if (info->shared
4342 && SYMBOL_REFERENCES_LOCAL (info, h))
4343 {
4344 bfd_vma relocation;
4345
4346 relocation = bfd_get_signed_32 (output_bfd,
4347 (sgot->contents
4348 + got_entry_offset));
4349
4350 /* Undo TP bias. */
4351 switch (elf_m68k_reloc_got_type (r_type))
4352 {
4353 case R_68K_GOT32O:
4354 case R_68K_TLS_LDM32:
4355 break;
4356
4357 case R_68K_TLS_GD32:
4358 /* The value for this relocation is actually put in
4359 the second GOT slot. */
4360 relocation = bfd_get_signed_32 (output_bfd,
4361 (sgot->contents
4362 + got_entry_offset + 4));
4363 relocation += dtpoff_base (info);
4364 break;
4365
4366 case R_68K_TLS_IE32:
4367 relocation += tpoff_base (info);
4368 break;
4369
4370 default:
4371 BFD_ASSERT (FALSE);
4372 }
4373
4374 elf_m68k_init_got_entry_local_shared (info,
4375 output_bfd,
4376 r_type,
4377 sgot,
4378 got_entry_offset,
4379 relocation,
4380 srela);
4381 }
4382 else
4383 {
4384 Elf_Internal_Rela rela;
4385
4386 /* Put zeros to GOT slots that will be initialized
4387 at run-time. */
4388 {
4389 bfd_vma n_slots;
4390
4391 n_slots = elf_m68k_reloc_got_n_slots (got_entry->key_.type);
4392 while (n_slots--)
4393 bfd_put_32 (output_bfd, (bfd_vma) 0,
4394 (sgot->contents + got_entry_offset
4395 + 4 * n_slots));
4396 }
4397
4398 rela.r_addend = 0;
4399 rela.r_offset = (sgot->output_section->vma
4400 + sgot->output_offset
4401 + got_entry_offset);
4402
4403 switch (elf_m68k_reloc_got_type (r_type))
4404 {
4405 case R_68K_GOT32O:
4406 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
4407 elf_m68k_install_rela (output_bfd, srela, &rela);
4408 break;
4409
4410 case R_68K_TLS_GD32:
4411 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPMOD32);
4412 elf_m68k_install_rela (output_bfd, srela, &rela);
4413
4414 rela.r_offset += 4;
4415 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPREL32);
4416 elf_m68k_install_rela (output_bfd, srela, &rela);
4417 break;
4418
4419 case R_68K_TLS_IE32:
4420 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_TPREL32);
4421 elf_m68k_install_rela (output_bfd, srela, &rela);
4422 break;
4423
4424 default:
4425 BFD_ASSERT (FALSE);
4426 break;
4427 }
4428 }
4429
4430 got_entry = got_entry->u.s2.next;
4431 }
4432 }
4433
4434 if (h->needs_copy)
4435 {
4436 asection *s;
4437 Elf_Internal_Rela rela;
4438 bfd_byte *loc;
4439
4440 /* This symbol needs a copy reloc. Set it up. */
4441
4442 BFD_ASSERT (h->dynindx != -1
4443 && (h->root.type == bfd_link_hash_defined
4444 || h->root.type == bfd_link_hash_defweak));
4445
4446 s = bfd_get_linker_section (dynobj, ".rela.bss");
4447 BFD_ASSERT (s != NULL);
4448
4449 rela.r_offset = (h->root.u.def.value
4450 + h->root.u.def.section->output_section->vma
4451 + h->root.u.def.section->output_offset);
4452 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
4453 rela.r_addend = 0;
4454 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4455 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4456 }
4457
4458 return TRUE;
4459 }
4460
4461 /* Finish up the dynamic sections. */
4462
4463 static bfd_boolean
4464 elf_m68k_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4465 {
4466 bfd *dynobj;
4467 asection *sgot;
4468 asection *sdyn;
4469
4470 dynobj = elf_hash_table (info)->dynobj;
4471
4472 sgot = bfd_get_linker_section (dynobj, ".got.plt");
4473 BFD_ASSERT (sgot != NULL);
4474 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4475
4476 if (elf_hash_table (info)->dynamic_sections_created)
4477 {
4478 asection *splt;
4479 Elf32_External_Dyn *dyncon, *dynconend;
4480
4481 splt = bfd_get_linker_section (dynobj, ".plt");
4482 BFD_ASSERT (splt != NULL && sdyn != NULL);
4483
4484 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4485 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4486 for (; dyncon < dynconend; dyncon++)
4487 {
4488 Elf_Internal_Dyn dyn;
4489 const char *name;
4490 asection *s;
4491
4492 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4493
4494 switch (dyn.d_tag)
4495 {
4496 default:
4497 break;
4498
4499 case DT_PLTGOT:
4500 name = ".got";
4501 goto get_vma;
4502 case DT_JMPREL:
4503 name = ".rela.plt";
4504 get_vma:
4505 s = bfd_get_section_by_name (output_bfd, name);
4506 BFD_ASSERT (s != NULL);
4507 dyn.d_un.d_ptr = s->vma;
4508 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4509 break;
4510
4511 case DT_PLTRELSZ:
4512 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4513 BFD_ASSERT (s != NULL);
4514 dyn.d_un.d_val = s->size;
4515 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4516 break;
4517
4518 case DT_RELASZ:
4519 /* The procedure linkage table relocs (DT_JMPREL) should
4520 not be included in the overall relocs (DT_RELA).
4521 Therefore, we override the DT_RELASZ entry here to
4522 make it not include the JMPREL relocs. Since the
4523 linker script arranges for .rela.plt to follow all
4524 other relocation sections, we don't have to worry
4525 about changing the DT_RELA entry. */
4526 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4527 if (s != NULL)
4528 dyn.d_un.d_val -= s->size;
4529 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4530 break;
4531 }
4532 }
4533
4534 /* Fill in the first entry in the procedure linkage table. */
4535 if (splt->size > 0)
4536 {
4537 const struct elf_m68k_plt_info *plt_info;
4538
4539 plt_info = elf_m68k_hash_table (info)->plt_info;
4540 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
4541
4542 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
4543 (sgot->output_section->vma
4544 + sgot->output_offset
4545 + 4));
4546
4547 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
4548 (sgot->output_section->vma
4549 + sgot->output_offset
4550 + 8));
4551
4552 elf_section_data (splt->output_section)->this_hdr.sh_entsize
4553 = plt_info->size;
4554 }
4555 }
4556
4557 /* Fill in the first three entries in the global offset table. */
4558 if (sgot->size > 0)
4559 {
4560 if (sdyn == NULL)
4561 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4562 else
4563 bfd_put_32 (output_bfd,
4564 sdyn->output_section->vma + sdyn->output_offset,
4565 sgot->contents);
4566 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4567 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4568 }
4569
4570 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4571
4572 return TRUE;
4573 }
4574
4575 /* Given a .data section and a .emreloc in-memory section, store
4576 relocation information into the .emreloc section which can be
4577 used at runtime to relocate the section. This is called by the
4578 linker when the --embedded-relocs switch is used. This is called
4579 after the add_symbols entry point has been called for all the
4580 objects, and before the final_link entry point is called. */
4581
4582 bfd_boolean
4583 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
4584 bfd *abfd;
4585 struct bfd_link_info *info;
4586 asection *datasec;
4587 asection *relsec;
4588 char **errmsg;
4589 {
4590 Elf_Internal_Shdr *symtab_hdr;
4591 Elf_Internal_Sym *isymbuf = NULL;
4592 Elf_Internal_Rela *internal_relocs = NULL;
4593 Elf_Internal_Rela *irel, *irelend;
4594 bfd_byte *p;
4595 bfd_size_type amt;
4596
4597 BFD_ASSERT (! info->relocatable);
4598
4599 *errmsg = NULL;
4600
4601 if (datasec->reloc_count == 0)
4602 return TRUE;
4603
4604 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4605
4606 /* Get a copy of the native relocations. */
4607 internal_relocs = (_bfd_elf_link_read_relocs
4608 (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
4609 info->keep_memory));
4610 if (internal_relocs == NULL)
4611 goto error_return;
4612
4613 amt = (bfd_size_type) datasec->reloc_count * 12;
4614 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
4615 if (relsec->contents == NULL)
4616 goto error_return;
4617
4618 p = relsec->contents;
4619
4620 irelend = internal_relocs + datasec->reloc_count;
4621 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
4622 {
4623 asection *targetsec;
4624
4625 /* We are going to write a four byte longword into the runtime
4626 reloc section. The longword will be the address in the data
4627 section which must be relocated. It is followed by the name
4628 of the target section NUL-padded or truncated to 8
4629 characters. */
4630
4631 /* We can only relocate absolute longword relocs at run time. */
4632 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
4633 {
4634 *errmsg = _("unsupported reloc type");
4635 bfd_set_error (bfd_error_bad_value);
4636 goto error_return;
4637 }
4638
4639 /* Get the target section referred to by the reloc. */
4640 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4641 {
4642 /* A local symbol. */
4643 Elf_Internal_Sym *isym;
4644
4645 /* Read this BFD's local symbols if we haven't done so already. */
4646 if (isymbuf == NULL)
4647 {
4648 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4649 if (isymbuf == NULL)
4650 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4651 symtab_hdr->sh_info, 0,
4652 NULL, NULL, NULL);
4653 if (isymbuf == NULL)
4654 goto error_return;
4655 }
4656
4657 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4658 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4659 }
4660 else
4661 {
4662 unsigned long indx;
4663 struct elf_link_hash_entry *h;
4664
4665 /* An external symbol. */
4666 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4667 h = elf_sym_hashes (abfd)[indx];
4668 BFD_ASSERT (h != NULL);
4669 if (h->root.type == bfd_link_hash_defined
4670 || h->root.type == bfd_link_hash_defweak)
4671 targetsec = h->root.u.def.section;
4672 else
4673 targetsec = NULL;
4674 }
4675
4676 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
4677 memset (p + 4, 0, 8);
4678 if (targetsec != NULL)
4679 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
4680 }
4681
4682 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4683 free (isymbuf);
4684 if (internal_relocs != NULL
4685 && elf_section_data (datasec)->relocs != internal_relocs)
4686 free (internal_relocs);
4687 return TRUE;
4688
4689 error_return:
4690 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4691 free (isymbuf);
4692 if (internal_relocs != NULL
4693 && elf_section_data (datasec)->relocs != internal_relocs)
4694 free (internal_relocs);
4695 return FALSE;
4696 }
4697
4698 /* Set target options. */
4699
4700 void
4701 bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
4702 {
4703 struct elf_m68k_link_hash_table *htab;
4704 bfd_boolean use_neg_got_offsets_p;
4705 bfd_boolean allow_multigot_p;
4706 bfd_boolean local_gp_p;
4707
4708 switch (got_handling)
4709 {
4710 case 0:
4711 /* --got=single. */
4712 local_gp_p = FALSE;
4713 use_neg_got_offsets_p = FALSE;
4714 allow_multigot_p = FALSE;
4715 break;
4716
4717 case 1:
4718 /* --got=negative. */
4719 local_gp_p = TRUE;
4720 use_neg_got_offsets_p = TRUE;
4721 allow_multigot_p = FALSE;
4722 break;
4723
4724 case 2:
4725 /* --got=multigot. */
4726 local_gp_p = TRUE;
4727 use_neg_got_offsets_p = TRUE;
4728 allow_multigot_p = TRUE;
4729 break;
4730
4731 default:
4732 BFD_ASSERT (FALSE);
4733 return;
4734 }
4735
4736 htab = elf_m68k_hash_table (info);
4737 if (htab != NULL)
4738 {
4739 htab->local_gp_p = local_gp_p;
4740 htab->use_neg_got_offsets_p = use_neg_got_offsets_p;
4741 htab->allow_multigot_p = allow_multigot_p;
4742 }
4743 }
4744
4745 static enum elf_reloc_type_class
4746 elf32_m68k_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4747 const asection *rel_sec ATTRIBUTE_UNUSED,
4748 const Elf_Internal_Rela *rela)
4749 {
4750 switch ((int) ELF32_R_TYPE (rela->r_info))
4751 {
4752 case R_68K_RELATIVE:
4753 return reloc_class_relative;
4754 case R_68K_JMP_SLOT:
4755 return reloc_class_plt;
4756 case R_68K_COPY:
4757 return reloc_class_copy;
4758 default:
4759 return reloc_class_normal;
4760 }
4761 }
4762
4763 /* Return address for Ith PLT stub in section PLT, for relocation REL
4764 or (bfd_vma) -1 if it should not be included. */
4765
4766 static bfd_vma
4767 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
4768 const arelent *rel ATTRIBUTE_UNUSED)
4769 {
4770 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
4771 }
4772
4773 /* Support for core dump NOTE sections. */
4774
4775 static bfd_boolean
4776 elf_m68k_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4777 {
4778 int offset;
4779 size_t size;
4780
4781 switch (note->descsz)
4782 {
4783 default:
4784 return FALSE;
4785
4786 case 154: /* Linux/m68k */
4787 /* pr_cursig */
4788 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4789
4790 /* pr_pid */
4791 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
4792
4793 /* pr_reg */
4794 offset = 70;
4795 size = 80;
4796
4797 break;
4798 }
4799
4800 /* Make a ".reg/999" section. */
4801 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4802 size, note->descpos + offset);
4803 }
4804
4805 static bfd_boolean
4806 elf_m68k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4807 {
4808 switch (note->descsz)
4809 {
4810 default:
4811 return FALSE;
4812
4813 case 124: /* Linux/m68k elf_prpsinfo. */
4814 elf_tdata (abfd)->core->pid
4815 = bfd_get_32 (abfd, note->descdata + 12);
4816 elf_tdata (abfd)->core->program
4817 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
4818 elf_tdata (abfd)->core->command
4819 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
4820 }
4821
4822 /* Note that for some reason, a spurious space is tacked
4823 onto the end of the args in some (at least one anyway)
4824 implementations, so strip it off if it exists. */
4825 {
4826 char *command = elf_tdata (abfd)->core->command;
4827 int n = strlen (command);
4828
4829 if (n > 0 && command[n - 1] == ' ')
4830 command[n - 1] = '\0';
4831 }
4832
4833 return TRUE;
4834 }
4835
4836 /* Hook called by the linker routine which adds symbols from an object
4837 file. */
4838
4839 static bfd_boolean
4840 elf_m68k_add_symbol_hook (bfd *abfd,
4841 struct bfd_link_info *info,
4842 Elf_Internal_Sym *sym,
4843 const char **namep ATTRIBUTE_UNUSED,
4844 flagword *flagsp ATTRIBUTE_UNUSED,
4845 asection **secp ATTRIBUTE_UNUSED,
4846 bfd_vma *valp ATTRIBUTE_UNUSED)
4847 {
4848 if ((abfd->flags & DYNAMIC) == 0
4849 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4850 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4851 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4852
4853 return TRUE;
4854 }
4855
4856 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
4857 #define TARGET_BIG_NAME "elf32-m68k"
4858 #define ELF_MACHINE_CODE EM_68K
4859 #define ELF_MAXPAGESIZE 0x2000
4860 #define elf_backend_create_dynamic_sections \
4861 _bfd_elf_create_dynamic_sections
4862 #define bfd_elf32_bfd_link_hash_table_create \
4863 elf_m68k_link_hash_table_create
4864 /* ??? Should it be this macro or bfd_elfNN_bfd_link_hash_table_create? */
4865 #define bfd_elf32_bfd_link_hash_table_free \
4866 elf_m68k_link_hash_table_free
4867 #define bfd_elf32_bfd_final_link bfd_elf_final_link
4868
4869 #define elf_backend_check_relocs elf_m68k_check_relocs
4870 #define elf_backend_always_size_sections \
4871 elf_m68k_always_size_sections
4872 #define elf_backend_adjust_dynamic_symbol \
4873 elf_m68k_adjust_dynamic_symbol
4874 #define elf_backend_size_dynamic_sections \
4875 elf_m68k_size_dynamic_sections
4876 #define elf_backend_final_write_processing elf_m68k_final_write_processing
4877 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4878 #define elf_backend_relocate_section elf_m68k_relocate_section
4879 #define elf_backend_finish_dynamic_symbol \
4880 elf_m68k_finish_dynamic_symbol
4881 #define elf_backend_finish_dynamic_sections \
4882 elf_m68k_finish_dynamic_sections
4883 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
4884 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
4885 #define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
4886 #define bfd_elf32_bfd_merge_private_bfd_data \
4887 elf32_m68k_merge_private_bfd_data
4888 #define bfd_elf32_bfd_set_private_flags \
4889 elf32_m68k_set_private_flags
4890 #define bfd_elf32_bfd_print_private_bfd_data \
4891 elf32_m68k_print_private_bfd_data
4892 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
4893 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
4894 #define elf_backend_object_p elf32_m68k_object_p
4895 #define elf_backend_grok_prstatus elf_m68k_grok_prstatus
4896 #define elf_backend_grok_psinfo elf_m68k_grok_psinfo
4897 #define elf_backend_add_symbol_hook elf_m68k_add_symbol_hook
4898
4899 #define elf_backend_can_gc_sections 1
4900 #define elf_backend_can_refcount 1
4901 #define elf_backend_want_got_plt 1
4902 #define elf_backend_plt_readonly 1
4903 #define elf_backend_want_plt_sym 0
4904 #define elf_backend_got_header_size 12
4905 #define elf_backend_rela_normal 1
4906
4907 #include "elf32-target.h"
4908