]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-m68k.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / bfd / elf32-m68k.c
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 static reloc_howto_type *reloc_type_lookup
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void rtype_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static void rtype_to_howto_rel
31 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
32 static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
33 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
34 static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
35 PARAMS ((bfd *));
36 static boolean elf_m68k_check_relocs
37 PARAMS ((bfd *, struct bfd_link_info *, asection *,
38 const Elf_Internal_Rela *));
39 static boolean elf_m68k_adjust_dynamic_symbol
40 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
41 static boolean elf_m68k_adjust_dynindx
42 PARAMS ((struct elf_link_hash_entry *, PTR));
43 static boolean elf_m68k_size_dynamic_sections
44 PARAMS ((bfd *, struct bfd_link_info *));
45 static boolean elf_m68k_relocate_section
46 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
48 static boolean elf_m68k_finish_dynamic_symbol
49 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
50 Elf_Internal_Sym *));
51 static boolean elf_m68k_finish_dynamic_sections
52 PARAMS ((bfd *, struct bfd_link_info *));
53
54 /* elf32 m68k code, generated by elf.el */
55 enum reloc_type {
56 R_68K_NONE = 0,
57 R_68K_32 = 1,
58 R_68K_16 = 2,
59 R_68K_8 = 3,
60 R_68K_PC32 = 4,
61 R_68K_PC16 = 5,
62 R_68K_PC8 = 6,
63 R_68K_GOT32 = 7,
64 R_68K_GOT16 = 8,
65 R_68K_GOT8 = 9,
66 R_68K_GOT32O = 10,
67 R_68K_GOT16O = 11,
68 R_68K_GOT8O = 12,
69 R_68K_PLT32 = 13,
70 R_68K_PLT16 = 14,
71 R_68K_PLT8 = 15,
72 R_68K_PLT32O = 16,
73 R_68K_PLT16O = 17,
74 R_68K_PLT8O = 18,
75 R_68K_COPY = 19,
76 R_68K_GLOB_DAT = 20,
77 R_68K_JMP_SLOT = 21,
78 R_68K_RELATIVE = 22,
79 R_68K__max
80 };
81
82 static reloc_howto_type howto_table[] = {
83 HOWTO(R_68K_NONE, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", false, 0, 0x00000000,false),
84 HOWTO(R_68K_32, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", false, 0, 0xffffffff,false),
85 HOWTO(R_68K_16, 0, 1,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", false, 0, 0x0000ffff,false),
86 HOWTO(R_68K_8, 0, 0, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", false, 0, 0x000000ff,false),
87 HOWTO(R_68K_PC32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", false, 0, 0xffffffff,true),
88 HOWTO(R_68K_PC16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", false, 0, 0x0000ffff,true),
89 HOWTO(R_68K_PC8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", false, 0, 0x000000ff,true),
90 HOWTO(R_68K_GOT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", false, 0, 0xffffffff,true),
91 HOWTO(R_68K_GOT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", false, 0, 0x0000ffff,true),
92 HOWTO(R_68K_GOT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", false, 0, 0x000000ff,true),
93 HOWTO(R_68K_GOT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", false, 0, 0xffffffff,false),
94 HOWTO(R_68K_GOT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", false, 0, 0x0000ffff,false),
95 HOWTO(R_68K_GOT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", false, 0, 0x000000ff,false),
96 HOWTO(R_68K_PLT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", false, 0, 0xffffffff,true),
97 HOWTO(R_68K_PLT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", false, 0, 0x0000ffff,true),
98 HOWTO(R_68K_PLT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", false, 0, 0x000000ff,true),
99 HOWTO(R_68K_PLT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", false, 0, 0xffffffff,false),
100 HOWTO(R_68K_PLT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", false, 0, 0x0000ffff,false),
101 HOWTO(R_68K_PLT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", false, 0, 0x000000ff,false),
102 HOWTO(R_68K_COPY, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", false, 0, 0xffffffff,false),
103 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),
104 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),
105 HOWTO(R_68K_RELATIVE, 0, 2,32, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", false, 0, 0xffffffff,false),
106 };
107
108 static void
109 rtype_to_howto (abfd, cache_ptr, dst)
110 bfd *abfd;
111 arelent *cache_ptr;
112 Elf_Internal_Rela *dst;
113 {
114 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
115 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
116 }
117
118 static void
119 rtype_to_howto_rel (abfd, cache_ptr, dst)
120 bfd *abfd;
121 arelent *cache_ptr;
122 Elf_Internal_Rel *dst;
123 {
124 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
125 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
126 }
127
128 #define elf_info_to_howto rtype_to_howto
129 #define elf_info_to_howto_rel rtype_to_howto_rel
130
131 static const struct { unsigned char bfd_val, elf_val; } reloc_map[] = {
132 { BFD_RELOC_NONE, R_68K_NONE },
133 { BFD_RELOC_32, R_68K_32 },
134 { BFD_RELOC_16, R_68K_16 },
135 { BFD_RELOC_8, R_68K_8 },
136 { BFD_RELOC_32_PCREL, R_68K_PC32 },
137 { BFD_RELOC_16_PCREL, R_68K_PC16 },
138 { BFD_RELOC_8_PCREL, R_68K_PC8 },
139 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
140 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
141 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
142 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
143 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
144 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
145 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
146 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
147 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
148 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
149 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
150 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
151 { BFD_RELOC_NONE, R_68K_COPY },
152 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
153 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
154 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
155 { BFD_RELOC_CTOR, R_68K_32 },
156 };
157
158 static reloc_howto_type *
159 reloc_type_lookup (abfd, code)
160 bfd *abfd;
161 bfd_reloc_code_real_type code;
162 {
163 unsigned int i;
164 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
165 {
166 if (reloc_map[i].bfd_val == code)
167 return &howto_table[(int) reloc_map[i].elf_val];
168 }
169 return 0;
170 }
171
172 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
173 #define ELF_ARCH bfd_arch_m68k
174 /* end code generated by elf.el */
175
176 #define USE_RELA
177
178 \f
179 /* Functions for the m68k ELF linker. */
180
181 /* The name of the dynamic interpreter. This is put in the .interp
182 section. */
183
184 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
185
186 /* The size in bytes of an entry in the procedure linkage table. */
187
188 #define PLT_ENTRY_SIZE 20
189
190 /* The first entry in a procedure linkage table looks like this. See
191 the SVR4 ABI m68k supplement to see how this works. */
192
193 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
194 {
195 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
196 0, 0, 0, 0, /* replaced with offset to .got + 4. */
197 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
198 0, 0, 0, 0, /* replaced with offset to .got + 8. */
199 0, 0, 0, 0 /* pad out to 20 bytes. */
200 };
201
202 /* Subsequent entries in a procedure linkage table look like this. */
203
204 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
205 {
206 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
207 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
208 0x2f, 0x3c, /* move.l #offset,-(%sp) */
209 0, 0, 0, 0, /* replaced with offset into relocation table. */
210 0x60, 0xff, /* bra.l .plt */
211 0, 0, 0, 0 /* replaced with offset to start of .plt. */
212 };
213
214 /* The m68k linker needs to keep track of the number of relocs that it
215 decides to copy in check_relocs for each symbol. This is so that it
216 can discard PC relative relocs if it doesn't need them when linking
217 with -Bsymbolic. We store the information in a field extending the
218 regular ELF linker hash table. */
219
220 /* This structure keeps track of the number of PC relative relocs we have
221 copied for a given symbol. */
222
223 struct elf_m68k_pcrel_relocs_copied
224 {
225 /* Next section. */
226 struct elf_m68k_pcrel_relocs_copied *next;
227 /* A section in dynobj. */
228 asection *section;
229 /* Number of relocs copied in this section. */
230 bfd_size_type count;
231 };
232
233 /* m68k ELF linker hash entry. */
234
235 struct elf_m68k_link_hash_entry
236 {
237 struct elf_link_hash_entry root;
238
239 /* Number of PC relative relocs copied for this symbol. */
240 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
241 };
242
243 /* m68k ELF linker hash table. */
244
245 struct elf_m68k_link_hash_table
246 {
247 struct elf_link_hash_table root;
248 };
249
250 /* Declare this now that the above structures are defined. */
251
252 static boolean elf_m68k_discard_copies
253 PARAMS ((struct elf_m68k_link_hash_entry *, PTR));
254
255 /* Traverse an m68k ELF linker hash table. */
256
257 #define elf_m68k_link_hash_traverse(table, func, info) \
258 (elf_link_hash_traverse \
259 (&(table)->root, \
260 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
261 (info)))
262
263 /* Get the m68k ELF linker hash table from a link_info structure. */
264
265 #define elf_m68k_hash_table(p) \
266 ((struct elf_m68k_link_hash_table *) (p)->hash)
267
268 /* Create an entry in an m68k ELF linker hash table. */
269
270 static struct bfd_hash_entry *
271 elf_m68k_link_hash_newfunc (entry, table, string)
272 struct bfd_hash_entry *entry;
273 struct bfd_hash_table *table;
274 const char *string;
275 {
276 struct elf_m68k_link_hash_entry *ret =
277 (struct elf_m68k_link_hash_entry *) entry;
278
279 /* Allocate the structure if it has not already been allocated by a
280 subclass. */
281 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
282 ret = ((struct elf_m68k_link_hash_entry *)
283 bfd_hash_allocate (table,
284 sizeof (struct elf_m68k_link_hash_entry)));
285 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
286 return (struct bfd_hash_entry *) ret;
287
288 /* Call the allocation method of the superclass. */
289 ret = ((struct elf_m68k_link_hash_entry *)
290 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
291 table, string));
292 if (ret != (struct elf_m68k_link_hash_entry *) NULL)
293 {
294 ret->pcrel_relocs_copied = NULL;
295 }
296
297 return (struct bfd_hash_entry *) ret;
298 }
299
300 /* Create an m68k ELF linker hash table. */
301
302 static struct bfd_link_hash_table *
303 elf_m68k_link_hash_table_create (abfd)
304 bfd *abfd;
305 {
306 struct elf_m68k_link_hash_table *ret;
307
308 ret = ((struct elf_m68k_link_hash_table *)
309 bfd_alloc (abfd, sizeof (struct elf_m68k_link_hash_table)));
310 if (ret == (struct elf_m68k_link_hash_table *) NULL)
311 return NULL;
312
313 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
314 elf_m68k_link_hash_newfunc))
315 {
316 bfd_release (abfd, ret);
317 return NULL;
318 }
319
320 return &ret->root.root;
321 }
322
323 /* Look through the relocs for a section during the first phase, and
324 allocate space in the global offset table or procedure linkage
325 table. */
326
327 static boolean
328 elf_m68k_check_relocs (abfd, info, sec, relocs)
329 bfd *abfd;
330 struct bfd_link_info *info;
331 asection *sec;
332 const Elf_Internal_Rela *relocs;
333 {
334 bfd *dynobj;
335 Elf_Internal_Shdr *symtab_hdr;
336 struct elf_link_hash_entry **sym_hashes;
337 bfd_vma *local_got_offsets;
338 const Elf_Internal_Rela *rel;
339 const Elf_Internal_Rela *rel_end;
340 asection *sgot;
341 asection *srelgot;
342 asection *sreloc;
343
344 if (info->relocateable)
345 return true;
346
347 dynobj = elf_hash_table (info)->dynobj;
348 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
349 sym_hashes = elf_sym_hashes (abfd);
350 local_got_offsets = elf_local_got_offsets (abfd);
351
352 sgot = NULL;
353 srelgot = NULL;
354 sreloc = NULL;
355
356 rel_end = relocs + sec->reloc_count;
357 for (rel = relocs; rel < rel_end; rel++)
358 {
359 unsigned long r_symndx;
360 struct elf_link_hash_entry *h;
361
362 r_symndx = ELF32_R_SYM (rel->r_info);
363
364 if (r_symndx < symtab_hdr->sh_info)
365 h = NULL;
366 else
367 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
368
369 switch (ELF32_R_TYPE (rel->r_info))
370 {
371 case R_68K_GOT8:
372 case R_68K_GOT16:
373 case R_68K_GOT32:
374 if (h != NULL
375 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
376 break;
377 /* Fall through. */
378 case R_68K_GOT8O:
379 case R_68K_GOT16O:
380 case R_68K_GOT32O:
381 /* This symbol requires a global offset table entry. */
382
383 if (dynobj == NULL)
384 {
385 /* Create the .got section. */
386 elf_hash_table (info)->dynobj = dynobj = abfd;
387 if (!_bfd_elf_create_got_section (dynobj, info))
388 return false;
389 }
390
391 if (sgot == NULL)
392 {
393 sgot = bfd_get_section_by_name (dynobj, ".got");
394 BFD_ASSERT (sgot != NULL);
395 }
396
397 if (srelgot == NULL
398 && (h != NULL || info->shared))
399 {
400 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
401 if (srelgot == NULL)
402 {
403 srelgot = bfd_make_section (dynobj, ".rela.got");
404 if (srelgot == NULL
405 || !bfd_set_section_flags (dynobj, srelgot,
406 (SEC_ALLOC
407 | SEC_LOAD
408 | SEC_HAS_CONTENTS
409 | SEC_IN_MEMORY
410 | SEC_LINKER_CREATED
411 | SEC_READONLY))
412 || !bfd_set_section_alignment (dynobj, srelgot, 2))
413 return false;
414 }
415 }
416
417 if (h != NULL)
418 {
419 if (h->got.offset != (bfd_vma) -1)
420 {
421 /* We have already allocated space in the .got. */
422 break;
423 }
424 h->got.offset = sgot->_raw_size;
425
426 /* Make sure this symbol is output as a dynamic symbol. */
427 if (h->dynindx == -1)
428 {
429 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
430 return false;
431 }
432
433 srelgot->_raw_size += sizeof (Elf32_External_Rela);
434 }
435 else
436 {
437 /* This is a global offset table entry for a local
438 symbol. */
439 if (local_got_offsets == NULL)
440 {
441 size_t size;
442 register unsigned int i;
443
444 size = symtab_hdr->sh_info * sizeof (bfd_vma);
445 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
446 if (local_got_offsets == NULL)
447 return false;
448 elf_local_got_offsets (abfd) = local_got_offsets;
449 for (i = 0; i < symtab_hdr->sh_info; i++)
450 local_got_offsets[i] = (bfd_vma) -1;
451 }
452 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
453 {
454 /* We have already allocated space in the .got. */
455 break;
456 }
457 local_got_offsets[r_symndx] = sgot->_raw_size;
458
459 if (info->shared)
460 {
461 /* If we are generating a shared object, we need to
462 output a R_68K_RELATIVE reloc so that the dynamic
463 linker can adjust this GOT entry. */
464 srelgot->_raw_size += sizeof (Elf32_External_Rela);
465 }
466 }
467
468 sgot->_raw_size += 4;
469 break;
470
471 case R_68K_PLT8:
472 case R_68K_PLT16:
473 case R_68K_PLT32:
474 /* This symbol requires a procedure linkage table entry. We
475 actually build the entry in adjust_dynamic_symbol,
476 because this might be a case of linking PIC code which is
477 never referenced by a dynamic object, in which case we
478 don't need to generate a procedure linkage table entry
479 after all. */
480
481 /* If this is a local symbol, we resolve it directly without
482 creating a procedure linkage table entry. */
483 if (h == NULL)
484 continue;
485
486 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
487 break;
488
489 case R_68K_PLT8O:
490 case R_68K_PLT16O:
491 case R_68K_PLT32O:
492 /* This symbol requires a procedure linkage table entry. */
493
494 if (h == NULL)
495 {
496 /* It does not make sense to have this relocation for a
497 local symbol. FIXME: does it? How to handle it if
498 it does make sense? */
499 bfd_set_error (bfd_error_bad_value);
500 return false;
501 }
502
503 /* Make sure this symbol is output as a dynamic symbol. */
504 if (h->dynindx == -1)
505 {
506 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
507 return false;
508 }
509
510 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
511 break;
512
513 case R_68K_PC8:
514 case R_68K_PC16:
515 case R_68K_PC32:
516 /* If we are creating a shared library and this is not a local
517 symbol, we need to copy the reloc into the shared library.
518 However when linking with -Bsymbolic and this is a global
519 symbol which is defined in an object we are including in the
520 link (i.e., DEF_REGULAR is set), then we can resolve the
521 reloc directly. At this point we have not seen all the input
522 files, so it is possible that DEF_REGULAR is not set now but
523 will be set later (it is never cleared). We account for that
524 possibility below by storing information in the
525 pcrel_relocs_copied field of the hash table entry. */
526 if (!(info->shared
527 && (sec->flags & SEC_ALLOC) != 0
528 && h != NULL
529 && (!info->symbolic
530 || (h->elf_link_hash_flags
531 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
532 break;
533 /* Fall through. */
534 case R_68K_8:
535 case R_68K_16:
536 case R_68K_32:
537 /* If we are creating a shared library, we need to copy the
538 reloc into the shared library. */
539 if (info->shared
540 && (sec->flags & SEC_ALLOC) != 0)
541 {
542 /* When creating a shared object, we must copy these
543 reloc types into the output file. We create a reloc
544 section in dynobj and make room for this reloc. */
545 if (sreloc == NULL)
546 {
547 const char *name;
548
549 name = (bfd_elf_string_from_elf_section
550 (abfd,
551 elf_elfheader (abfd)->e_shstrndx,
552 elf_section_data (sec)->rel_hdr.sh_name));
553 if (name == NULL)
554 return false;
555
556 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
557 && strcmp (bfd_get_section_name (abfd, sec),
558 name + 5) == 0);
559
560 sreloc = bfd_get_section_by_name (dynobj, name);
561 if (sreloc == NULL)
562 {
563 sreloc = bfd_make_section (dynobj, name);
564 if (sreloc == NULL
565 || !bfd_set_section_flags (dynobj, sreloc,
566 (SEC_ALLOC
567 | SEC_LOAD
568 | SEC_HAS_CONTENTS
569 | SEC_IN_MEMORY
570 | SEC_LINKER_CREATED
571 | SEC_READONLY))
572 || !bfd_set_section_alignment (dynobj, sreloc, 2))
573 return false;
574 }
575 }
576
577 sreloc->_raw_size += sizeof (Elf32_External_Rela);
578
579 /* If we are linking with -Bsymbolic, we count the number of
580 PC relative relocations we have entered for this symbol,
581 so that we can discard them again if the symbol is later
582 defined by a regular object. Note that this function is
583 only called if we are using an m68kelf linker hash table,
584 which means that h is really a pointer to an
585 elf_m68k_link_hash_entry. */
586 if ((ELF32_R_TYPE (rel->r_info) == R_68K_PC8
587 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
588 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
589 && info->symbolic)
590 {
591 struct elf_m68k_link_hash_entry *eh;
592 struct elf_m68k_pcrel_relocs_copied *p;
593
594 eh = (struct elf_m68k_link_hash_entry *) h;
595
596 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
597 if (p->section == sreloc)
598 break;
599
600 if (p == NULL)
601 {
602 p = ((struct elf_m68k_pcrel_relocs_copied *)
603 bfd_alloc (dynobj, sizeof *p));
604 if (p == NULL)
605 return false;
606 p->next = eh->pcrel_relocs_copied;
607 eh->pcrel_relocs_copied = p;
608 p->section = sreloc;
609 p->count = 0;
610 }
611
612 ++p->count;
613 }
614 }
615
616 break;
617
618 default:
619 break;
620 }
621 }
622
623 return true;
624 }
625
626 /* Adjust a symbol defined by a dynamic object and referenced by a
627 regular object. The current definition is in some section of the
628 dynamic object, but we're not including those sections. We have to
629 change the definition to something the rest of the link can
630 understand. */
631
632 static boolean
633 elf_m68k_adjust_dynamic_symbol (info, h)
634 struct bfd_link_info *info;
635 struct elf_link_hash_entry *h;
636 {
637 bfd *dynobj;
638 asection *s;
639 unsigned int power_of_two;
640
641 dynobj = elf_hash_table (info)->dynobj;
642
643 /* Make sure we know what is going on here. */
644 BFD_ASSERT (dynobj != NULL
645 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
646 || h->weakdef != NULL
647 || ((h->elf_link_hash_flags
648 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
649 && (h->elf_link_hash_flags
650 & ELF_LINK_HASH_REF_REGULAR) != 0
651 && (h->elf_link_hash_flags
652 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
653
654 /* If this is a function, put it in the procedure linkage table. We
655 will fill in the contents of the procedure linkage table later,
656 when we know the address of the .got section. */
657 if (h->type == STT_FUNC
658 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
659 {
660 if (! info->shared
661 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
662 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
663 /* We must always create the plt entry if it was referenced
664 by a PLTxxO relocation. In this case we already recorded
665 it as a dynamic symbol. */
666 && h->dynindx == -1)
667 {
668 /* This case can occur if we saw a PLTxx reloc in an input
669 file, but the symbol was never referred to by a dynamic
670 object. In such a case, we don't actually need to build
671 a procedure linkage table, and we can just do a PCxx
672 reloc instead. */
673 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
674 return true;
675 }
676
677 /* Make sure this symbol is output as a dynamic symbol. */
678 if (h->dynindx == -1)
679 {
680 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
681 return false;
682 }
683
684 s = bfd_get_section_by_name (dynobj, ".plt");
685 BFD_ASSERT (s != NULL);
686
687 /* If this is the first .plt entry, make room for the special
688 first entry. */
689 if (s->_raw_size == 0)
690 s->_raw_size += PLT_ENTRY_SIZE;
691
692 /* If this symbol is not defined in a regular file, and we are
693 not generating a shared library, then set the symbol to this
694 location in the .plt. This is required to make function
695 pointers compare as equal between the normal executable and
696 the shared library. */
697 if (!info->shared
698 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
699 {
700 h->root.u.def.section = s;
701 h->root.u.def.value = s->_raw_size;
702 }
703
704 h->plt.offset = s->_raw_size;
705
706 /* Make room for this entry. */
707 s->_raw_size += PLT_ENTRY_SIZE;
708
709 /* We also need to make an entry in the .got.plt section, which
710 will be placed in the .got section by the linker script. */
711
712 s = bfd_get_section_by_name (dynobj, ".got.plt");
713 BFD_ASSERT (s != NULL);
714 s->_raw_size += 4;
715
716 /* We also need to make an entry in the .rela.plt section. */
717
718 s = bfd_get_section_by_name (dynobj, ".rela.plt");
719 BFD_ASSERT (s != NULL);
720 s->_raw_size += sizeof (Elf32_External_Rela);
721
722 return true;
723 }
724
725 /* If this is a weak symbol, and there is a real definition, the
726 processor independent code will have arranged for us to see the
727 real definition first, and we can just use the same value. */
728 if (h->weakdef != NULL)
729 {
730 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
731 || h->weakdef->root.type == bfd_link_hash_defweak);
732 h->root.u.def.section = h->weakdef->root.u.def.section;
733 h->root.u.def.value = h->weakdef->root.u.def.value;
734 return true;
735 }
736
737 /* This is a reference to a symbol defined by a dynamic object which
738 is not a function. */
739
740 /* If we are creating a shared library, we must presume that the
741 only references to the symbol are via the global offset table.
742 For such cases we need not do anything here; the relocations will
743 be handled correctly by relocate_section. */
744 if (info->shared)
745 return true;
746
747 /* We must allocate the symbol in our .dynbss section, which will
748 become part of the .bss section of the executable. There will be
749 an entry for this symbol in the .dynsym section. The dynamic
750 object will contain position independent code, so all references
751 from the dynamic object to this symbol will go through the global
752 offset table. The dynamic linker will use the .dynsym entry to
753 determine the address it must put in the global offset table, so
754 both the dynamic object and the regular object will refer to the
755 same memory location for the variable. */
756
757 s = bfd_get_section_by_name (dynobj, ".dynbss");
758 BFD_ASSERT (s != NULL);
759
760 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
761 copy the initial value out of the dynamic object and into the
762 runtime process image. We need to remember the offset into the
763 .rela.bss section we are going to use. */
764 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
765 {
766 asection *srel;
767
768 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
769 BFD_ASSERT (srel != NULL);
770 srel->_raw_size += sizeof (Elf32_External_Rela);
771 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
772 }
773
774 /* We need to figure out the alignment required for this symbol. I
775 have no idea how ELF linkers handle this. */
776 power_of_two = bfd_log2 (h->size);
777 if (power_of_two > 3)
778 power_of_two = 3;
779
780 /* Apply the required alignment. */
781 s->_raw_size = BFD_ALIGN (s->_raw_size,
782 (bfd_size_type) (1 << power_of_two));
783 if (power_of_two > bfd_get_section_alignment (dynobj, s))
784 {
785 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
786 return false;
787 }
788
789 /* Define the symbol as being at this point in the section. */
790 h->root.u.def.section = s;
791 h->root.u.def.value = s->_raw_size;
792
793 /* Increment the section size to make room for the symbol. */
794 s->_raw_size += h->size;
795
796 return true;
797 }
798
799 /* Set the sizes of the dynamic sections. */
800
801 static boolean
802 elf_m68k_size_dynamic_sections (output_bfd, info)
803 bfd *output_bfd;
804 struct bfd_link_info *info;
805 {
806 bfd *dynobj;
807 asection *s;
808 boolean plt;
809 boolean relocs;
810 boolean reltext;
811
812 dynobj = elf_hash_table (info)->dynobj;
813 BFD_ASSERT (dynobj != NULL);
814
815 if (elf_hash_table (info)->dynamic_sections_created)
816 {
817 /* Set the contents of the .interp section to the interpreter. */
818 if (!info->shared)
819 {
820 s = bfd_get_section_by_name (dynobj, ".interp");
821 BFD_ASSERT (s != NULL);
822 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
823 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
824 }
825 }
826 else
827 {
828 /* We may have created entries in the .rela.got section.
829 However, if we are not creating the dynamic sections, we will
830 not actually use these entries. Reset the size of .rela.got,
831 which will cause it to get stripped from the output file
832 below. */
833 s = bfd_get_section_by_name (dynobj, ".rela.got");
834 if (s != NULL)
835 s->_raw_size = 0;
836 }
837
838 /* If this is a -Bsymbolic shared link, then we need to discard all PC
839 relative relocs against symbols defined in a regular object. We
840 allocated space for them in the check_relocs routine, but we will not
841 fill them in in the relocate_section routine. */
842 if (info->shared && info->symbolic)
843 elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),
844 elf_m68k_discard_copies,
845 (PTR) NULL);
846
847 /* The check_relocs and adjust_dynamic_symbol entry points have
848 determined the sizes of the various dynamic sections. Allocate
849 memory for them. */
850 plt = false;
851 relocs = false;
852 reltext = false;
853 for (s = dynobj->sections; s != NULL; s = s->next)
854 {
855 const char *name;
856 boolean strip;
857
858 if ((s->flags & SEC_LINKER_CREATED) == 0)
859 continue;
860
861 /* It's OK to base decisions on the section name, because none
862 of the dynobj section names depend upon the input files. */
863 name = bfd_get_section_name (dynobj, s);
864
865 strip = false;
866
867 if (strcmp (name, ".plt") == 0)
868 {
869 if (s->_raw_size == 0)
870 {
871 /* Strip this section if we don't need it; see the
872 comment below. */
873 strip = true;
874 }
875 else
876 {
877 /* Remember whether there is a PLT. */
878 plt = true;
879 }
880 }
881 else if (strncmp (name, ".rela", 5) == 0)
882 {
883 if (s->_raw_size == 0)
884 {
885 /* If we don't need this section, strip it from the
886 output file. This is mostly to handle .rela.bss and
887 .rela.plt. We must create both sections in
888 create_dynamic_sections, because they must be created
889 before the linker maps input sections to output
890 sections. The linker does that before
891 adjust_dynamic_symbol is called, and it is that
892 function which decides whether anything needs to go
893 into these sections. */
894 strip = true;
895 }
896 else
897 {
898 asection *target;
899
900 /* Remember whether there are any reloc sections other
901 than .rela.plt. */
902 if (strcmp (name, ".rela.plt") != 0)
903 {
904 const char *outname;
905
906 relocs = true;
907
908 /* If this relocation section applies to a read only
909 section, then we probably need a DT_TEXTREL
910 entry. .rela.plt is actually associated with
911 .got.plt, which is never readonly. */
912 outname = bfd_get_section_name (output_bfd,
913 s->output_section);
914 target = bfd_get_section_by_name (output_bfd, outname + 5);
915 if (target != NULL
916 && (target->flags & SEC_READONLY) != 0
917 && (target->flags & SEC_ALLOC) != 0)
918 reltext = true;
919 }
920
921 /* We use the reloc_count field as a counter if we need
922 to copy relocs into the output file. */
923 s->reloc_count = 0;
924 }
925 }
926 else if (strncmp (name, ".got", 4) != 0)
927 {
928 /* It's not one of our sections, so don't allocate space. */
929 continue;
930 }
931
932 if (strip)
933 {
934 asection **spp;
935
936 for (spp = &s->output_section->owner->sections;
937 *spp != s->output_section;
938 spp = &(*spp)->next)
939 ;
940 *spp = s->output_section->next;
941 --s->output_section->owner->section_count;
942
943 continue;
944 }
945
946 /* Allocate memory for the section contents. */
947 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
948 if (s->contents == NULL && s->_raw_size != 0)
949 return false;
950 }
951
952 if (elf_hash_table (info)->dynamic_sections_created)
953 {
954 /* Add some entries to the .dynamic section. We fill in the
955 values later, in elf_m68k_finish_dynamic_sections, but we
956 must add the entries now so that we get the correct size for
957 the .dynamic section. The DT_DEBUG entry is filled in by the
958 dynamic linker and used by the debugger. */
959 if (!info->shared)
960 {
961 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
962 return false;
963 }
964
965 if (plt)
966 {
967 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
968 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
969 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
970 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
971 return false;
972 }
973
974 if (relocs)
975 {
976 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
977 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
978 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
979 sizeof (Elf32_External_Rela)))
980 return false;
981 }
982
983 if (reltext)
984 {
985 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
986 return false;
987 }
988 }
989
990 /* If we are generating a shared library, we generate a section
991 symbol for each output section for which we might need to copy
992 relocs. These are local symbols, which means that they must come
993 first in the dynamic symbol table. That means we must increment
994 the dynamic symbol index of every other dynamic symbol. */
995 if (info->shared)
996 {
997 int c;
998
999 c = 0;
1000 for (s = output_bfd->sections; s != NULL; s = s->next)
1001 {
1002 if ((s->flags & SEC_LINKER_CREATED) != 0
1003 || (s->flags & SEC_ALLOC) == 0)
1004 continue;
1005
1006 elf_section_data (s)->dynindx = c + 1;
1007
1008 /* These symbols will have no names, so we don't need to
1009 fiddle with dynstr_index. */
1010
1011 ++c;
1012 }
1013
1014 elf_link_hash_traverse (elf_hash_table (info),
1015 elf_m68k_adjust_dynindx,
1016 (PTR) &c);
1017 elf_hash_table (info)->dynsymcount += c;
1018 }
1019
1020 return true;
1021 }
1022
1023 /* Increment the index of a dynamic symbol by a given amount. Called
1024 via elf_link_hash_traverse. */
1025
1026 static boolean
1027 elf_m68k_adjust_dynindx (h, cparg)
1028 struct elf_link_hash_entry *h;
1029 PTR cparg;
1030 {
1031 int *cp = (int *) cparg;
1032
1033 if (h->dynindx != -1)
1034 h->dynindx += *cp;
1035 return true;
1036 }
1037
1038 /* This function is called via elf_m68k_link_hash_traverse if we are
1039 creating a shared object with -Bsymbolic. It discards the space
1040 allocated to copy PC relative relocs against symbols which are defined
1041 in regular objects. We allocated space for them in the check_relocs
1042 routine, but we won't fill them in in the relocate_section routine. */
1043
1044 /*ARGSUSED*/
1045 static boolean
1046 elf_m68k_discard_copies (h, ignore)
1047 struct elf_m68k_link_hash_entry *h;
1048 PTR ignore;
1049 {
1050 struct elf_m68k_pcrel_relocs_copied *s;
1051
1052 /* We only discard relocs for symbols defined in a regular object. */
1053 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1054 return true;
1055
1056 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1057 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
1058
1059 return true;
1060 }
1061
1062 /* Relocate an M68K ELF section. */
1063
1064 static boolean
1065 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1066 contents, relocs, local_syms, local_sections)
1067 bfd *output_bfd;
1068 struct bfd_link_info *info;
1069 bfd *input_bfd;
1070 asection *input_section;
1071 bfd_byte *contents;
1072 Elf_Internal_Rela *relocs;
1073 Elf_Internal_Sym *local_syms;
1074 asection **local_sections;
1075 {
1076 bfd *dynobj;
1077 Elf_Internal_Shdr *symtab_hdr;
1078 struct elf_link_hash_entry **sym_hashes;
1079 bfd_vma *local_got_offsets;
1080 asection *sgot;
1081 asection *splt;
1082 asection *sreloc;
1083 Elf_Internal_Rela *rel;
1084 Elf_Internal_Rela *relend;
1085
1086 dynobj = elf_hash_table (info)->dynobj;
1087 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1088 sym_hashes = elf_sym_hashes (input_bfd);
1089 local_got_offsets = elf_local_got_offsets (input_bfd);
1090
1091 sgot = NULL;
1092 splt = NULL;
1093 sreloc = NULL;
1094
1095 rel = relocs;
1096 relend = relocs + input_section->reloc_count;
1097 for (; rel < relend; rel++)
1098 {
1099 int r_type;
1100 reloc_howto_type *howto;
1101 unsigned long r_symndx;
1102 struct elf_link_hash_entry *h;
1103 Elf_Internal_Sym *sym;
1104 asection *sec;
1105 bfd_vma relocation;
1106 bfd_reloc_status_type r;
1107
1108 r_type = ELF32_R_TYPE (rel->r_info);
1109 if (r_type < 0 || r_type >= (int) R_68K__max)
1110 {
1111 bfd_set_error (bfd_error_bad_value);
1112 return false;
1113 }
1114 howto = howto_table + r_type;
1115
1116 r_symndx = ELF32_R_SYM (rel->r_info);
1117
1118 if (info->relocateable)
1119 {
1120 /* This is a relocateable link. We don't have to change
1121 anything, unless the reloc is against a section symbol,
1122 in which case we have to adjust according to where the
1123 section symbol winds up in the output section. */
1124 if (r_symndx < symtab_hdr->sh_info)
1125 {
1126 sym = local_syms + r_symndx;
1127 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1128 {
1129 sec = local_sections[r_symndx];
1130 rel->r_addend += sec->output_offset + sym->st_value;
1131 }
1132 }
1133
1134 continue;
1135 }
1136
1137 /* This is a final link. */
1138 h = NULL;
1139 sym = NULL;
1140 sec = NULL;
1141 if (r_symndx < symtab_hdr->sh_info)
1142 {
1143 sym = local_syms + r_symndx;
1144 sec = local_sections[r_symndx];
1145 relocation = (sec->output_section->vma
1146 + sec->output_offset
1147 + sym->st_value);
1148 }
1149 else
1150 {
1151 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1152 while (h->root.type == bfd_link_hash_indirect
1153 || h->root.type == bfd_link_hash_warning)
1154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1155 if (h->root.type == bfd_link_hash_defined
1156 || h->root.type == bfd_link_hash_defweak)
1157 {
1158 sec = h->root.u.def.section;
1159 if (((r_type == R_68K_PLT8
1160 || r_type == R_68K_PLT16
1161 || r_type == R_68K_PLT32
1162 || r_type == R_68K_PLT8O
1163 || r_type == R_68K_PLT16O
1164 || r_type == R_68K_PLT32O)
1165 && h->plt.offset != (bfd_vma) -1)
1166 || ((r_type == R_68K_GOT8O
1167 || r_type == R_68K_GOT16O
1168 || r_type == R_68K_GOT32O
1169 || ((r_type == R_68K_GOT8
1170 || r_type == R_68K_GOT16
1171 || r_type == R_68K_GOT32)
1172 && strcmp (h->root.root.string,
1173 "_GLOBAL_OFFSET_TABLE_") != 0))
1174 && elf_hash_table (info)->dynamic_sections_created
1175 && (! info->shared
1176 || (! info->symbolic && h->dynindx != -1)
1177 || (h->elf_link_hash_flags
1178 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1179 || (info->shared
1180 && ((! info->symbolic && h->dynindx != -1)
1181 || (h->elf_link_hash_flags
1182 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1183 && (input_section->flags & SEC_ALLOC) != 0
1184 && (r_type == R_68K_8
1185 || r_type == R_68K_16
1186 || r_type == R_68K_32
1187 || r_type == R_68K_PC8
1188 || r_type == R_68K_PC16
1189 || r_type == R_68K_PC32)))
1190 {
1191 /* In these cases, we don't need the relocation
1192 value. We check specially because in some
1193 obscure cases sec->output_section will be NULL. */
1194 relocation = 0;
1195 }
1196 else
1197 relocation = (h->root.u.def.value
1198 + sec->output_section->vma
1199 + sec->output_offset);
1200 }
1201 else if (h->root.type == bfd_link_hash_undefweak)
1202 relocation = 0;
1203 else if (info->shared && !info->symbolic)
1204 relocation = 0;
1205 else
1206 {
1207 if (!(info->callbacks->undefined_symbol
1208 (info, h->root.root.string, input_bfd,
1209 input_section, rel->r_offset)))
1210 return false;
1211 relocation = 0;
1212 }
1213 }
1214
1215 switch (r_type)
1216 {
1217 case R_68K_GOT8:
1218 case R_68K_GOT16:
1219 case R_68K_GOT32:
1220 /* Relocation is to the address of the entry for this symbol
1221 in the global offset table. */
1222 if (h != NULL
1223 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1224 break;
1225 /* Fall through. */
1226 case R_68K_GOT8O:
1227 case R_68K_GOT16O:
1228 case R_68K_GOT32O:
1229 /* Relocation is the offset of the entry for this symbol in
1230 the global offset table. */
1231
1232 {
1233 bfd_vma off;
1234
1235 if (sgot == NULL)
1236 {
1237 sgot = bfd_get_section_by_name (dynobj, ".got");
1238 BFD_ASSERT (sgot != NULL);
1239 }
1240
1241 if (h != NULL)
1242 {
1243 off = h->got.offset;
1244 BFD_ASSERT (off != (bfd_vma) -1);
1245
1246 if (!elf_hash_table (info)->dynamic_sections_created
1247 || (info->shared
1248 && (info->symbolic || h->dynindx == -1)
1249 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1250 {
1251 /* This is actually a static link, or it is a
1252 -Bsymbolic link and the symbol is defined
1253 locally, or the symbol was forced to be local
1254 because of a version file.. We must initialize
1255 this entry in the global offset table. Since
1256 the offset must always be a multiple of 4, we
1257 use the least significant bit to record whether
1258 we have initialized it already.
1259
1260 When doing a dynamic link, we create a .rela.got
1261 relocation entry to initialize the value. This
1262 is done in the finish_dynamic_symbol routine. */
1263 if ((off & 1) != 0)
1264 off &= ~1;
1265 else
1266 {
1267 bfd_put_32 (output_bfd, relocation,
1268 sgot->contents + off);
1269 h->got.offset |= 1;
1270 }
1271 }
1272 }
1273 else
1274 {
1275 BFD_ASSERT (local_got_offsets != NULL
1276 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1277
1278 off = local_got_offsets[r_symndx];
1279
1280 /* The offset must always be a multiple of 4. We use
1281 the least significant bit to record whether we have
1282 already generated the necessary reloc. */
1283 if ((off & 1) != 0)
1284 off &= ~1;
1285 else
1286 {
1287 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1288
1289 if (info->shared)
1290 {
1291 asection *srelgot;
1292 Elf_Internal_Rela outrel;
1293
1294 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1295 BFD_ASSERT (srelgot != NULL);
1296
1297 outrel.r_offset = (sgot->output_section->vma
1298 + sgot->output_offset
1299 + off);
1300 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1301 outrel.r_addend = relocation;
1302 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1303 (((Elf32_External_Rela *)
1304 srelgot->contents)
1305 + srelgot->reloc_count));
1306 ++srelgot->reloc_count;
1307 }
1308
1309 local_got_offsets[r_symndx] |= 1;
1310 }
1311 }
1312
1313 relocation = sgot->output_offset + off;
1314 if (r_type == R_68K_GOT8O
1315 || r_type == R_68K_GOT16O
1316 || r_type == R_68K_GOT32O)
1317 {
1318 /* This relocation does not use the addend. */
1319 rel->r_addend = 0;
1320 }
1321 else
1322 relocation += sgot->output_section->vma;
1323 }
1324 break;
1325
1326 case R_68K_PLT8:
1327 case R_68K_PLT16:
1328 case R_68K_PLT32:
1329 /* Relocation is to the entry for this symbol in the
1330 procedure linkage table. */
1331
1332 /* Resolve a PLTxx reloc against a local symbol directly,
1333 without using the procedure linkage table. */
1334 if (h == NULL)
1335 break;
1336
1337 if (h->plt.offset == (bfd_vma) -1)
1338 {
1339 /* We didn't make a PLT entry for this symbol. This
1340 happens when statically linking PIC code, or when
1341 using -Bsymbolic. */
1342 break;
1343 }
1344
1345 if (splt == NULL)
1346 {
1347 splt = bfd_get_section_by_name (dynobj, ".plt");
1348 BFD_ASSERT (splt != NULL);
1349 }
1350
1351 relocation = (splt->output_section->vma
1352 + splt->output_offset
1353 + h->plt.offset);
1354 break;
1355
1356 case R_68K_PLT8O:
1357 case R_68K_PLT16O:
1358 case R_68K_PLT32O:
1359 /* Relocation is the offset of the entry for this symbol in
1360 the procedure linkage table. */
1361 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1362
1363 if (splt == NULL)
1364 {
1365 splt = bfd_get_section_by_name (dynobj, ".plt");
1366 BFD_ASSERT (splt != NULL);
1367 }
1368
1369 relocation = h->plt.offset;
1370
1371 /* This relocation does not use the addend. */
1372 rel->r_addend = 0;
1373
1374 break;
1375
1376 case R_68K_PC8:
1377 case R_68K_PC16:
1378 case R_68K_PC32:
1379 if (h == NULL)
1380 break;
1381 /* Fall through. */
1382 case R_68K_8:
1383 case R_68K_16:
1384 case R_68K_32:
1385 if (info->shared
1386 && (input_section->flags & SEC_ALLOC) != 0
1387 && ((r_type != R_68K_PC8
1388 && r_type != R_68K_PC16
1389 && r_type != R_68K_PC32)
1390 || (!info->symbolic
1391 || (h->elf_link_hash_flags
1392 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1393 {
1394 Elf_Internal_Rela outrel;
1395 boolean skip, relocate;
1396
1397 /* When generating a shared object, these relocations
1398 are copied into the output file to be resolved at run
1399 time. */
1400
1401 if (sreloc == NULL)
1402 {
1403 const char *name;
1404
1405 name = (bfd_elf_string_from_elf_section
1406 (input_bfd,
1407 elf_elfheader (input_bfd)->e_shstrndx,
1408 elf_section_data (input_section)->rel_hdr.sh_name));
1409 if (name == NULL)
1410 return false;
1411
1412 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1413 && strcmp (bfd_get_section_name (input_bfd,
1414 input_section),
1415 name + 5) == 0);
1416
1417 sreloc = bfd_get_section_by_name (dynobj, name);
1418 BFD_ASSERT (sreloc != NULL);
1419 }
1420
1421 skip = false;
1422
1423 if (elf_section_data (input_section)->stab_info == NULL)
1424 outrel.r_offset = rel->r_offset;
1425 else
1426 {
1427 bfd_vma off;
1428
1429 off = (_bfd_stab_section_offset
1430 (output_bfd, &elf_hash_table (info)->stab_info,
1431 input_section,
1432 &elf_section_data (input_section)->stab_info,
1433 rel->r_offset));
1434 if (off == (bfd_vma) -1)
1435 skip = true;
1436 outrel.r_offset = off;
1437 }
1438
1439 outrel.r_offset += (input_section->output_section->vma
1440 + input_section->output_offset);
1441
1442 if (skip)
1443 {
1444 memset (&outrel, 0, sizeof outrel);
1445 relocate = false;
1446 }
1447 /* h->dynindx may be -1 if the symbol was marked to
1448 become local. */
1449 else if (h != NULL
1450 && ((! info->symbolic && h->dynindx != -1)
1451 || (h->elf_link_hash_flags
1452 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1453 {
1454 BFD_ASSERT (h->dynindx != -1);
1455 relocate = false;
1456 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1457 outrel.r_addend = relocation + rel->r_addend;
1458 }
1459 else
1460 {
1461 if (r_type == R_68K_32)
1462 {
1463 relocate = true;
1464 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1465 outrel.r_addend = relocation + rel->r_addend;
1466 }
1467 else
1468 {
1469 long indx;
1470
1471 if (h == NULL)
1472 sec = local_sections[r_symndx];
1473 else
1474 {
1475 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1476 || (h->root.type
1477 == bfd_link_hash_defweak));
1478 sec = h->root.u.def.section;
1479 }
1480 if (sec != NULL && bfd_is_abs_section (sec))
1481 indx = 0;
1482 else if (sec == NULL || sec->owner == NULL)
1483 {
1484 bfd_set_error (bfd_error_bad_value);
1485 return false;
1486 }
1487 else
1488 {
1489 asection *osec;
1490
1491 osec = sec->output_section;
1492 indx = elf_section_data (osec)->dynindx;
1493 BFD_ASSERT (indx > 0);
1494 }
1495
1496 relocate = false;
1497 outrel.r_info = ELF32_R_INFO (indx, r_type);
1498 outrel.r_addend = relocation + rel->r_addend;
1499 }
1500 }
1501
1502 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1503 (((Elf32_External_Rela *)
1504 sreloc->contents)
1505 + sreloc->reloc_count));
1506 ++sreloc->reloc_count;
1507
1508 /* This reloc will be computed at runtime, so there's no
1509 need to do anything now, except for R_68K_32
1510 relocations that have been turned into
1511 R_68K_RELATIVE. */
1512 if (!relocate)
1513 continue;
1514 }
1515
1516 break;
1517
1518 default:
1519 break;
1520 }
1521
1522 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1523 contents, rel->r_offset,
1524 relocation, rel->r_addend);
1525
1526 if (r != bfd_reloc_ok)
1527 {
1528 switch (r)
1529 {
1530 default:
1531 case bfd_reloc_outofrange:
1532 abort ();
1533 case bfd_reloc_overflow:
1534 {
1535 const char *name;
1536
1537 if (h != NULL)
1538 name = h->root.root.string;
1539 else
1540 {
1541 name = bfd_elf_string_from_elf_section (input_bfd,
1542 symtab_hdr->sh_link,
1543 sym->st_name);
1544 if (name == NULL)
1545 return false;
1546 if (*name == '\0')
1547 name = bfd_section_name (input_bfd, sec);
1548 }
1549 if (!(info->callbacks->reloc_overflow
1550 (info, name, howto->name, (bfd_vma) 0,
1551 input_bfd, input_section, rel->r_offset)))
1552 return false;
1553 }
1554 break;
1555 }
1556 }
1557 }
1558
1559 return true;
1560 }
1561
1562 /* Finish up dynamic symbol handling. We set the contents of various
1563 dynamic sections here. */
1564
1565 static boolean
1566 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1567 bfd *output_bfd;
1568 struct bfd_link_info *info;
1569 struct elf_link_hash_entry *h;
1570 Elf_Internal_Sym *sym;
1571 {
1572 bfd *dynobj;
1573
1574 dynobj = elf_hash_table (info)->dynobj;
1575
1576 if (h->plt.offset != (bfd_vma) -1)
1577 {
1578 asection *splt;
1579 asection *sgot;
1580 asection *srela;
1581 bfd_vma plt_index;
1582 bfd_vma got_offset;
1583 Elf_Internal_Rela rela;
1584
1585 /* This symbol has an entry in the procedure linkage table. Set
1586 it up. */
1587
1588 BFD_ASSERT (h->dynindx != -1);
1589
1590 splt = bfd_get_section_by_name (dynobj, ".plt");
1591 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1592 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1593 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1594
1595 /* Get the index in the procedure linkage table which
1596 corresponds to this symbol. This is the index of this symbol
1597 in all the symbols for which we are making plt entries. The
1598 first entry in the procedure linkage table is reserved. */
1599 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1600
1601 /* Get the offset into the .got table of the entry that
1602 corresponds to this function. Each .got entry is 4 bytes.
1603 The first three are reserved. */
1604 got_offset = (plt_index + 3) * 4;
1605
1606 /* Fill in the entry in the procedure linkage table. */
1607 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1608 PLT_ENTRY_SIZE);
1609 /* The offset is relative to the first extension word. */
1610 bfd_put_32 (output_bfd,
1611 (sgot->output_section->vma
1612 + sgot->output_offset
1613 + got_offset
1614 - (splt->output_section->vma
1615 + h->plt.offset + 2)),
1616 splt->contents + h->plt.offset + 4);
1617
1618 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1619 splt->contents + h->plt.offset + 10);
1620 bfd_put_32 (output_bfd, - (h->plt.offset + 16),
1621 splt->contents + h->plt.offset + 16);
1622
1623 /* Fill in the entry in the global offset table. */
1624 bfd_put_32 (output_bfd,
1625 (splt->output_section->vma
1626 + splt->output_offset
1627 + h->plt.offset
1628 + 8),
1629 sgot->contents + got_offset);
1630
1631 /* Fill in the entry in the .rela.plt section. */
1632 rela.r_offset = (sgot->output_section->vma
1633 + sgot->output_offset
1634 + got_offset);
1635 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1636 rela.r_addend = 0;
1637 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1638 ((Elf32_External_Rela *) srela->contents
1639 + plt_index));
1640
1641 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1642 {
1643 /* Mark the symbol as undefined, rather than as defined in
1644 the .plt section. Leave the value alone. */
1645 sym->st_shndx = SHN_UNDEF;
1646 }
1647 }
1648
1649 if (h->got.offset != (bfd_vma) -1)
1650 {
1651 asection *sgot;
1652 asection *srela;
1653 Elf_Internal_Rela rela;
1654
1655 /* This symbol has an entry in the global offset table. Set it
1656 up. */
1657
1658 sgot = bfd_get_section_by_name (dynobj, ".got");
1659 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1660 BFD_ASSERT (sgot != NULL && srela != NULL);
1661
1662 rela.r_offset = (sgot->output_section->vma
1663 + sgot->output_offset
1664 + (h->got.offset &~ 1));
1665
1666 /* If this is a -Bsymbolic link, and the symbol is defined
1667 locally, we just want to emit a RELATIVE reloc. Likewise if
1668 the symbol was forced to be local because of a version file.
1669 The entry in the global offset table will already have been
1670 initialized in the relocate_section function. */
1671 if (info->shared
1672 && (info->symbolic || h->dynindx == -1)
1673 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1674 {
1675 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1676 rela.r_addend = bfd_get_signed_32 (output_bfd,
1677 (sgot->contents
1678 + (h->got.offset & ~1)));
1679 }
1680 else
1681 {
1682 bfd_put_32 (output_bfd, (bfd_vma) 0,
1683 sgot->contents + (h->got.offset & ~1));
1684 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1685 rela.r_addend = 0;
1686 }
1687
1688 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1689 ((Elf32_External_Rela *) srela->contents
1690 + srela->reloc_count));
1691 ++srela->reloc_count;
1692 }
1693
1694 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1695 {
1696 asection *s;
1697 Elf_Internal_Rela rela;
1698
1699 /* This symbol needs a copy reloc. Set it up. */
1700
1701 BFD_ASSERT (h->dynindx != -1
1702 && (h->root.type == bfd_link_hash_defined
1703 || h->root.type == bfd_link_hash_defweak));
1704
1705 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1706 ".rela.bss");
1707 BFD_ASSERT (s != NULL);
1708
1709 rela.r_offset = (h->root.u.def.value
1710 + h->root.u.def.section->output_section->vma
1711 + h->root.u.def.section->output_offset);
1712 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
1713 rela.r_addend = 0;
1714 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1715 ((Elf32_External_Rela *) s->contents
1716 + s->reloc_count));
1717 ++s->reloc_count;
1718 }
1719
1720 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1721 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1722 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1723 sym->st_shndx = SHN_ABS;
1724
1725 return true;
1726 }
1727
1728 /* Finish up the dynamic sections. */
1729
1730 static boolean
1731 elf_m68k_finish_dynamic_sections (output_bfd, info)
1732 bfd *output_bfd;
1733 struct bfd_link_info *info;
1734 {
1735 bfd *dynobj;
1736 asection *sgot;
1737 asection *sdyn;
1738
1739 dynobj = elf_hash_table (info)->dynobj;
1740
1741 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1742 BFD_ASSERT (sgot != NULL);
1743 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1744
1745 if (elf_hash_table (info)->dynamic_sections_created)
1746 {
1747 asection *splt;
1748 Elf32_External_Dyn *dyncon, *dynconend;
1749
1750 splt = bfd_get_section_by_name (dynobj, ".plt");
1751 BFD_ASSERT (splt != NULL && sdyn != NULL);
1752
1753 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1754 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1755 for (; dyncon < dynconend; dyncon++)
1756 {
1757 Elf_Internal_Dyn dyn;
1758 const char *name;
1759 asection *s;
1760
1761 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1762
1763 switch (dyn.d_tag)
1764 {
1765 default:
1766 break;
1767
1768 case DT_PLTGOT:
1769 name = ".got";
1770 goto get_vma;
1771 case DT_JMPREL:
1772 name = ".rela.plt";
1773 get_vma:
1774 s = bfd_get_section_by_name (output_bfd, name);
1775 BFD_ASSERT (s != NULL);
1776 dyn.d_un.d_ptr = s->vma;
1777 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1778 break;
1779
1780 case DT_PLTRELSZ:
1781 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1782 BFD_ASSERT (s != NULL);
1783 if (s->_cooked_size != 0)
1784 dyn.d_un.d_val = s->_cooked_size;
1785 else
1786 dyn.d_un.d_val = s->_raw_size;
1787 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1788 break;
1789
1790 case DT_RELASZ:
1791 /* The procedure linkage table relocs (DT_JMPREL) should
1792 not be included in the overall relocs (DT_RELA).
1793 Therefore, we override the DT_RELASZ entry here to
1794 make it not include the JMPREL relocs. Since the
1795 linker script arranges for .rela.plt to follow all
1796 other relocation sections, we don't have to worry
1797 about changing the DT_RELA entry. */
1798 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1799 if (s != NULL)
1800 {
1801 if (s->_cooked_size != 0)
1802 dyn.d_un.d_val -= s->_cooked_size;
1803 else
1804 dyn.d_un.d_val -= s->_raw_size;
1805 }
1806 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1807 break;
1808 }
1809 }
1810
1811 /* Fill in the first entry in the procedure linkage table. */
1812 if (splt->_raw_size > 0)
1813 {
1814 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
1815 bfd_put_32 (output_bfd,
1816 (sgot->output_section->vma
1817 + sgot->output_offset + 4
1818 - (splt->output_section->vma + 2)),
1819 splt->contents + 4);
1820 bfd_put_32 (output_bfd,
1821 (sgot->output_section->vma
1822 + sgot->output_offset + 8
1823 - (splt->output_section->vma + 10)),
1824 splt->contents + 12);
1825 }
1826
1827 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1828 = PLT_ENTRY_SIZE;
1829 }
1830
1831 /* Fill in the first three entries in the global offset table. */
1832 if (sgot->_raw_size > 0)
1833 {
1834 if (sdyn == NULL)
1835 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1836 else
1837 bfd_put_32 (output_bfd,
1838 sdyn->output_section->vma + sdyn->output_offset,
1839 sgot->contents);
1840 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1841 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1842 }
1843
1844 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1845
1846 if (info->shared)
1847 {
1848 asection *sdynsym;
1849 asection *s;
1850 Elf_Internal_Sym sym;
1851 int c;
1852
1853 /* Set up the section symbols for the output sections. */
1854
1855 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1856 BFD_ASSERT (sdynsym != NULL);
1857
1858 sym.st_size = 0;
1859 sym.st_name = 0;
1860 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1861 sym.st_other = 0;
1862
1863 c = 0;
1864 for (s = output_bfd->sections; s != NULL; s = s->next)
1865 {
1866 int indx;
1867
1868 if (elf_section_data (s)->dynindx == 0)
1869 continue;
1870
1871 sym.st_value = s->vma;
1872
1873 indx = elf_section_data (s)->this_idx;
1874 BFD_ASSERT (indx > 0);
1875 sym.st_shndx = indx;
1876
1877 bfd_elf32_swap_symbol_out (output_bfd, &sym,
1878 (PTR) (((Elf32_External_Sym *)
1879 sdynsym->contents)
1880 + elf_section_data (s)->dynindx));
1881
1882 ++c;
1883 }
1884
1885 /* Set the sh_info field of the output .dynsym section to the
1886 index of the first global symbol. */
1887 elf_section_data (sdynsym->output_section)->this_hdr.sh_info = c + 1;
1888 }
1889
1890 return true;
1891 }
1892
1893 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
1894 #define TARGET_BIG_NAME "elf32-m68k"
1895 #define ELF_MACHINE_CODE EM_68K
1896 #define ELF_MAXPAGESIZE 0x2000
1897 #define elf_backend_create_dynamic_sections \
1898 _bfd_elf_create_dynamic_sections
1899 #define bfd_elf32_bfd_link_hash_table_create \
1900 elf_m68k_link_hash_table_create
1901 #define elf_backend_check_relocs elf_m68k_check_relocs
1902 #define elf_backend_adjust_dynamic_symbol \
1903 elf_m68k_adjust_dynamic_symbol
1904 #define elf_backend_size_dynamic_sections \
1905 elf_m68k_size_dynamic_sections
1906 #define elf_backend_relocate_section elf_m68k_relocate_section
1907 #define elf_backend_finish_dynamic_symbol \
1908 elf_m68k_finish_dynamic_symbol
1909 #define elf_backend_finish_dynamic_sections \
1910 elf_m68k_finish_dynamic_sections
1911 #define elf_backend_want_got_plt 1
1912 #define elf_backend_plt_readonly 1
1913 #define elf_backend_want_plt_sym 0
1914
1915 #include "elf32-target.h"