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