* elf32-i386.c (elf_i386_relocate_section): Get the relocation
[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
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
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. We must initialize this entry in the
1206 global offset table. Since the offset must
1207 always be a multiple of 4, we use the least
1208 significant bit to record whether we have
1209 initialized it already.
1210
1211 When doing a dynamic link, we create a .rela.got
1212 relocation entry to initialize the value. This
1213 is done in the finish_dynamic_symbol routine. */
1214 if ((off & 1) != 0)
1215 off &= ~1;
1216 else
1217 {
1218 bfd_put_32 (output_bfd, relocation,
1219 sgot->contents + off);
1220 h->got_offset |= 1;
1221 }
1222 }
1223 }
1224 else
1225 {
1226 BFD_ASSERT (local_got_offsets != NULL
1227 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1228
1229 off = local_got_offsets[r_symndx];
1230
1231 /* The offset must always be a multiple of 4. We use
1232 the least significant bit to record whether we have
1233 already generated the necessary reloc. */
1234 if ((off & 1) != 0)
1235 off &= ~1;
1236 else
1237 {
1238 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1239
1240 if (info->shared)
1241 {
1242 asection *srelgot;
1243 Elf_Internal_Rela outrel;
1244
1245 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1246 BFD_ASSERT (srelgot != NULL);
1247
1248 outrel.r_offset = (sgot->output_section->vma
1249 + sgot->output_offset
1250 + off);
1251 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1252 outrel.r_addend = relocation;
1253 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1254 (((Elf32_External_Rela *)
1255 srelgot->contents)
1256 + srelgot->reloc_count));
1257 ++srelgot->reloc_count;
1258 }
1259
1260 local_got_offsets[r_symndx] |= 1;
1261 }
1262 }
1263
1264 relocation = sgot->output_offset + off;
1265 if (r_type == R_68K_GOT8O
1266 || r_type == R_68K_GOT16O
1267 || r_type == R_68K_GOT32O)
1268 {
1269 /* This relocation does not use the addend. */
1270 rel->r_addend = 0;
1271 }
1272 else
1273 relocation += sgot->output_section->vma;
1274 }
1275 break;
1276
1277 case R_68K_PLT8:
1278 case R_68K_PLT16:
1279 case R_68K_PLT32:
1280 /* Relocation is to the entry for this symbol in the
1281 procedure linkage table. */
1282
1283 /* Resolve a PLTxx reloc against a local symbol directly,
1284 without using the procedure linkage table. */
1285 if (h == NULL)
1286 break;
1287
1288 if (h->plt_offset == (bfd_vma) -1)
1289 {
1290 /* We didn't make a PLT entry for this symbol. This
1291 happens when statically linking PIC code, or when
1292 using -Bsymbolic. */
1293 break;
1294 }
1295
1296 if (splt == NULL)
1297 {
1298 splt = bfd_get_section_by_name (dynobj, ".plt");
1299 BFD_ASSERT (splt != NULL);
1300 }
1301
1302 relocation = (splt->output_section->vma
1303 + splt->output_offset
1304 + h->plt_offset);
1305 break;
1306
1307 case R_68K_PLT8O:
1308 case R_68K_PLT16O:
1309 case R_68K_PLT32O:
1310 /* Relocation is the offset of the entry for this symbol in
1311 the procedure linkage table. */
1312 BFD_ASSERT (h != NULL && h->plt_offset == (bfd_vma) -1);
1313
1314 if (splt == NULL)
1315 {
1316 splt = bfd_get_section_by_name (dynobj, ".plt");
1317 BFD_ASSERT (splt != NULL);
1318 }
1319
1320 relocation = h->plt_offset;
1321
1322 /* This relocation does not use the addend. */
1323 rel->r_addend = 0;
1324
1325 break;
1326
1327 case R_68K_PC8:
1328 case R_68K_PC16:
1329 case R_68K_PC32:
1330 if (h == NULL)
1331 break;
1332 /* Fall through. */
1333 case R_68K_8:
1334 case R_68K_16:
1335 case R_68K_32:
1336 if (info->shared
1337 && (input_section->flags & SEC_ALLOC) != 0
1338 && ((r_type != R_68K_PC8
1339 && r_type != R_68K_PC16
1340 && r_type != R_68K_PC32)
1341 || (!info->symbolic
1342 || (h->elf_link_hash_flags
1343 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1344 {
1345 Elf_Internal_Rela outrel;
1346 boolean skip, relocate;
1347
1348 /* When generating a shared object, these relocations
1349 are copied into the output file to be resolved at run
1350 time. */
1351
1352 if (sreloc == NULL)
1353 {
1354 const char *name;
1355
1356 name = (bfd_elf_string_from_elf_section
1357 (input_bfd,
1358 elf_elfheader (input_bfd)->e_shstrndx,
1359 elf_section_data (input_section)->rel_hdr.sh_name));
1360 if (name == NULL)
1361 return false;
1362
1363 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1364 && strcmp (bfd_get_section_name (input_bfd,
1365 input_section),
1366 name + 5) == 0);
1367
1368 sreloc = bfd_get_section_by_name (dynobj, name);
1369 BFD_ASSERT (sreloc != NULL);
1370 }
1371
1372 skip = false;
1373
1374 if (elf_section_data (input_section)->stab_info == NULL)
1375 outrel.r_offset = rel->r_offset;
1376 else
1377 {
1378 bfd_vma off;
1379
1380 off = (_bfd_stab_section_offset
1381 (output_bfd, &elf_hash_table (info)->stab_info,
1382 input_section,
1383 &elf_section_data (input_section)->stab_info,
1384 rel->r_offset));
1385 if (off == (bfd_vma) -1)
1386 skip = true;
1387 outrel.r_offset = off;
1388 }
1389
1390 outrel.r_offset += (input_section->output_section->vma
1391 + input_section->output_offset);
1392
1393 if (skip)
1394 {
1395 memset (&outrel, 0, sizeof outrel);
1396 relocate = false;
1397 }
1398 /* h->dynindx may be -1 if the symbol was marked to
1399 become local. */
1400 else if (h != NULL
1401 && ((! info->symbolic && h->dynindx != -1)
1402 || (h->elf_link_hash_flags
1403 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1404 {
1405 BFD_ASSERT (h->dynindx != -1);
1406 relocate = false;
1407 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1408 outrel.r_addend = relocation + rel->r_addend;
1409 }
1410 else
1411 {
1412 if (r_type == R_68K_32)
1413 {
1414 relocate = true;
1415 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1416 outrel.r_addend = relocation + rel->r_addend;
1417 }
1418 else
1419 {
1420 long indx;
1421
1422 if (h == NULL)
1423 sec = local_sections[r_symndx];
1424 else
1425 {
1426 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1427 || (h->root.type
1428 == bfd_link_hash_defweak));
1429 sec = h->root.u.def.section;
1430 }
1431 if (sec != NULL && bfd_is_abs_section (sec))
1432 indx = 0;
1433 else if (sec == NULL || sec->owner == NULL)
1434 {
1435 bfd_set_error (bfd_error_bad_value);
1436 return false;
1437 }
1438 else
1439 {
1440 asection *osec;
1441
1442 osec = sec->output_section;
1443 indx = elf_section_data (osec)->dynindx;
1444 if (indx == 0)
1445 abort ();
1446 }
1447
1448 relocate = false;
1449 outrel.r_info = ELF32_R_INFO (indx, r_type);
1450 outrel.r_addend = relocation + rel->r_addend;
1451 }
1452 }
1453
1454 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1455 (((Elf32_External_Rela *)
1456 sreloc->contents)
1457 + sreloc->reloc_count));
1458 ++sreloc->reloc_count;
1459
1460 /* This reloc will be computed at runtime, so there's no
1461 need to do anything now, except for R_68K_32
1462 relocations that have been turned into
1463 R_68K_RELATIVE. */
1464 if (!relocate)
1465 continue;
1466 }
1467
1468 break;
1469
1470 default:
1471 break;
1472 }
1473
1474 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1475 contents, rel->r_offset,
1476 relocation, rel->r_addend);
1477
1478 if (r != bfd_reloc_ok)
1479 {
1480 switch (r)
1481 {
1482 default:
1483 case bfd_reloc_outofrange:
1484 abort ();
1485 case bfd_reloc_overflow:
1486 {
1487 const char *name;
1488
1489 if (h != NULL)
1490 name = h->root.root.string;
1491 else
1492 {
1493 name = bfd_elf_string_from_elf_section (input_bfd,
1494 symtab_hdr->sh_link,
1495 sym->st_name);
1496 if (name == NULL)
1497 return false;
1498 if (*name == '\0')
1499 name = bfd_section_name (input_bfd, sec);
1500 }
1501 if (!(info->callbacks->reloc_overflow
1502 (info, name, howto->name, (bfd_vma) 0,
1503 input_bfd, input_section, rel->r_offset)))
1504 return false;
1505 }
1506 break;
1507 }
1508 }
1509 }
1510
1511 return true;
1512 }
1513
1514 /* Finish up dynamic symbol handling. We set the contents of various
1515 dynamic sections here. */
1516
1517 static boolean
1518 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1519 bfd *output_bfd;
1520 struct bfd_link_info *info;
1521 struct elf_link_hash_entry *h;
1522 Elf_Internal_Sym *sym;
1523 {
1524 bfd *dynobj;
1525
1526 dynobj = elf_hash_table (info)->dynobj;
1527
1528 if (h->plt_offset != (bfd_vma) -1)
1529 {
1530 asection *splt;
1531 asection *sgot;
1532 asection *srela;
1533 bfd_vma plt_index;
1534 bfd_vma got_offset;
1535 Elf_Internal_Rela rela;
1536
1537 /* This symbol has an entry in the procedure linkage table. Set
1538 it up. */
1539
1540 BFD_ASSERT (h->dynindx != -1);
1541
1542 splt = bfd_get_section_by_name (dynobj, ".plt");
1543 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1544 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1545 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1546
1547 /* Get the index in the procedure linkage table which
1548 corresponds to this symbol. This is the index of this symbol
1549 in all the symbols for which we are making plt entries. The
1550 first entry in the procedure linkage table is reserved. */
1551 plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
1552
1553 /* Get the offset into the .got table of the entry that
1554 corresponds to this function. Each .got entry is 4 bytes.
1555 The first three are reserved. */
1556 got_offset = (plt_index + 3) * 4;
1557
1558 /* Fill in the entry in the procedure linkage table. */
1559 memcpy (splt->contents + h->plt_offset, elf_m68k_plt_entry,
1560 PLT_ENTRY_SIZE);
1561 /* The offset is relative to the first extension word. */
1562 bfd_put_32 (output_bfd,
1563 (sgot->output_section->vma
1564 + sgot->output_offset
1565 + got_offset
1566 - (splt->output_section->vma
1567 + h->plt_offset + 2)),
1568 splt->contents + h->plt_offset + 4);
1569
1570 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1571 splt->contents + h->plt_offset + 10);
1572 bfd_put_32 (output_bfd, - (h->plt_offset + 16),
1573 splt->contents + h->plt_offset + 16);
1574
1575 /* Fill in the entry in the global offset table. */
1576 bfd_put_32 (output_bfd,
1577 (splt->output_section->vma
1578 + splt->output_offset
1579 + h->plt_offset
1580 + 8),
1581 sgot->contents + got_offset);
1582
1583 /* Fill in the entry in the .rela.plt section. */
1584 rela.r_offset = (sgot->output_section->vma
1585 + sgot->output_offset
1586 + got_offset);
1587 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1588 rela.r_addend = 0;
1589 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1590 ((Elf32_External_Rela *) srela->contents
1591 + plt_index));
1592
1593 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1594 {
1595 /* Mark the symbol as undefined, rather than as defined in
1596 the .plt section. Leave the value alone. */
1597 sym->st_shndx = SHN_UNDEF;
1598 }
1599 }
1600
1601 if (h->got_offset != (bfd_vma) -1)
1602 {
1603 asection *sgot;
1604 asection *srela;
1605 Elf_Internal_Rela rela;
1606
1607 /* This symbol has an entry in the global offset table. Set it
1608 up. */
1609
1610 BFD_ASSERT (h->dynindx != -1);
1611
1612 sgot = bfd_get_section_by_name (dynobj, ".got");
1613 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1614 BFD_ASSERT (sgot != NULL && srela != NULL);
1615
1616 rela.r_offset = (sgot->output_section->vma
1617 + sgot->output_offset
1618 + (h->got_offset &~ 1));
1619
1620 /* If this is a -Bsymbolic link, and the symbol is defined
1621 locally, we just want to emit a RELATIVE reloc. The entry in
1622 the global offset table will already have been initialized in
1623 the relocate_section function. */
1624 if (info->shared
1625 && info->symbolic
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.066758 seconds and 4 git commands to generate.