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