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