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