Correct erroneous checkin, with no ChangeLog entry, of 13 May.
[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. */
584 BFD_ASSERT (dynobj != NULL);
585 BFD_ASSERT ((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
592 && (h->root.type == bfd_link_hash_defined
593 || h->root.type == bfd_link_hash_defweak)
594 && (h->root.u.def.section->owner == NULL
595 || ((elf_elfheader (h->root.u.def.section->owner)->e_type
596 == ET_DYN)
597 && (bfd_get_flavour (h->root.u.def.section->owner)
598 == bfd_target_elf_flavour)
599 && h->root.u.def.section->output_section == NULL))));
600
601 /* If this is a function, put it in the procedure linkage table. We
602 will fill in the contents of the procedure linkage table later,
603 when we know the address of the .got section. */
604 if (h->type == STT_FUNC
605 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
606 {
607 if (!elf_hash_table (info)->dynamic_sections_created)
608 {
609 /* This case can occur if we saw a PLT32 reloc in an input
610 file, but none of the input files were dynamic objects.
611 In such a case, we don't actually need to build a
612 procedure linkage table, and we can just do a PC32 reloc
613 instead. */
614 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
615 return true;
616 }
617
618 s = bfd_get_section_by_name (dynobj, ".plt");
619 BFD_ASSERT (s != NULL);
620
621 /* If this is the first .plt entry, make room for the special
622 first entry. */
623 if (s->_raw_size == 0)
624 s->_raw_size += PLT_ENTRY_SIZE;
625
626 /* If this symbol is not defined in a regular file, and we are
627 not generating a shared library, then set the symbol to this
628 location in the .plt. This is required to make function
629 pointers compare as equal between the normal executable and
630 the shared library. */
631 if (!info->shared
632 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
633 {
634 h->root.u.def.section = s;
635 h->root.u.def.value = s->_raw_size;
636 }
637
638 h->plt_offset = s->_raw_size;
639
640 /* Make room for this entry. */
641 s->_raw_size += PLT_ENTRY_SIZE;
642
643 /* We also need to make an entry in the .got.plt section, which
644 will be placed in the .got section by the linker script. */
645
646 s = bfd_get_section_by_name (dynobj, ".got.plt");
647 BFD_ASSERT (s != NULL);
648 s->_raw_size += 4;
649
650 /* We also need to make an entry in the .rela.plt section. */
651
652 s = bfd_get_section_by_name (dynobj, ".rela.plt");
653 BFD_ASSERT (s != NULL);
654 s->_raw_size += sizeof (Elf32_External_Rela);
655
656 return true;
657 }
658
659 /* If this is a weak symbol, and there is a real definition, the
660 processor independent code will have arranged for us to see the
661 real definition first, and we can just use the same value. */
662 if (h->weakdef != NULL)
663 {
664 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
665 || h->weakdef->root.type == bfd_link_hash_defweak);
666 h->root.u.def.section = h->weakdef->root.u.def.section;
667 h->root.u.def.value = h->weakdef->root.u.def.value;
668 return true;
669 }
670
671 /* This is a reference to a symbol defined by a dynamic object which
672 is not a function. */
673
674 /* If we are creating a shared library, we must presume that the
675 only references to the symbol are via the global offset table.
676 For such cases we need not do anything here; the relocations will
677 be handled correctly by relocate_section. */
678 if (info->shared)
679 return true;
680
681 /* We must allocate the symbol in our .dynbss section, which will
682 become part of the .bss section of the executable. There will be
683 an entry for this symbol in the .dynsym section. The dynamic
684 object will contain position independent code, so all references
685 from the dynamic object to this symbol will go through the global
686 offset table. The dynamic linker will use the .dynsym entry to
687 determine the address it must put in the global offset table, so
688 both the dynamic object and the regular object will refer to the
689 same memory location for the variable. */
690
691 s = bfd_get_section_by_name (dynobj, ".dynbss");
692 BFD_ASSERT (s != NULL);
693
694 /* If the symbol is currently defined in the .bss section of the
695 dynamic object, then it is OK to simply initialize it to zero.
696 If the symbol is in some other section, we must generate a
697 R_68K_COPY reloc to tell the dynamic linker to copy the initial
698 value out of the dynamic object and into the runtime process
699 image. We need to remember the offset into the .rela.bss section
700 we are going to use. */
701 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
702 {
703 asection *srel;
704
705 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
706 BFD_ASSERT (srel != NULL);
707 srel->_raw_size += sizeof (Elf32_External_Rela);
708 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
709 }
710
711 /* We need to figure out the alignment required for this symbol. I
712 have no idea how ELF linkers handle this. */
713 power_of_two = bfd_log2 (h->size);
714 if (power_of_two > 3)
715 power_of_two = 3;
716
717 /* Apply the required alignment. */
718 s->_raw_size = BFD_ALIGN (s->_raw_size,
719 (bfd_size_type) (1 << power_of_two));
720 if (power_of_two > bfd_get_section_alignment (dynobj, s))
721 {
722 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
723 return false;
724 }
725
726 /* Define the symbol as being at this point in the section. */
727 h->root.u.def.section = s;
728 h->root.u.def.value = s->_raw_size;
729
730 /* Increment the section size to make room for the symbol. */
731 s->_raw_size += h->size;
732
733 return true;
734}
735
736/* Set the sizes of the dynamic sections. */
737
738static boolean
739elf_m68k_size_dynamic_sections (output_bfd, info)
740 bfd *output_bfd;
741 struct bfd_link_info *info;
742{
743 bfd *dynobj;
744 asection *s;
745 boolean plt;
746 boolean relocs;
747 boolean reltext;
748
749 dynobj = elf_hash_table (info)->dynobj;
750 BFD_ASSERT (dynobj != NULL);
751
752 if (elf_hash_table (info)->dynamic_sections_created)
753 {
754 /* Set the contents of the .interp section to the interpreter. */
755 if (!info->shared)
756 {
757 s = bfd_get_section_by_name (dynobj, ".interp");
758 BFD_ASSERT (s != NULL);
759 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
760 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
761 }
762 }
763 else
764 {
765 /* We may have created entries in the .rela.got section.
766 However, if we are not creating the dynamic sections, we will
767 not actually use these entries. Reset the size of .rela.got,
768 which will cause it to get stripped from the output file
769 below. */
770 s = bfd_get_section_by_name (dynobj, ".rela.got");
771 if (s != NULL)
772 s->_raw_size = 0;
773 }
774
775 /* The check_relocs and adjust_dynamic_symbol entry points have
776 determined the sizes of the various dynamic sections. Allocate
777 memory for them. */
778 plt = false;
779 relocs = false;
780 reltext = false;
781 for (s = dynobj->sections; s != NULL; s = s->next)
782 {
783 const char *name;
784 boolean strip;
785
786 if ((s->flags & SEC_IN_MEMORY) == 0)
787 continue;
788
789 /* It's OK to base decisions on the section name, because none
790 of the dynobj section names depend upon the input files. */
791 name = bfd_get_section_name (dynobj, s);
792
793 strip = false;
794
795 if (strcmp (name, ".plt") == 0)
796 {
797 if (s->_raw_size == 0)
798 {
799 /* Strip this section if we don't need it; see the
800 comment below. */
801 strip = true;
802 }
803 else
804 {
805 /* Remember whether there is a PLT. */
806 plt = true;
807 }
808 }
809 else if (strncmp (name, ".rela", 5) == 0)
810 {
811 if (s->_raw_size == 0)
812 {
813 /* If we don't need this section, strip it from the
814 output file. This is mostly to handle .rela.bss and
815 .rela.plt. We must create both sections in
816 create_dynamic_sections, because they must be created
817 before the linker maps input sections to output
818 sections. The linker does that before
819 adjust_dynamic_symbol is called, and it is that
820 function which decides whether anything needs to go
821 into these sections. */
822 strip = true;
823 }
824 else
825 {
826 asection *target;
827
828 /* Remember whether there are any reloc sections other
829 than .rela.plt. */
830 if (strcmp (name, ".rela.plt") != 0)
831 {
832 relocs = true;
833
834 /* If this relocation section applies to a read only
835 section, then we probably need a DT_TEXTREL
836 entry. .rela.plt is actually associated with
837 .got.plt, which is never readonly. */
838 target = bfd_get_section_by_name (output_bfd, name + 5);
839 if (target != NULL
840 && (target->flags & SEC_READONLY) != 0)
841 reltext = true;
842 }
843
844 /* We use the reloc_count field as a counter if we need
845 to copy relocs into the output file. */
846 s->reloc_count = 0;
847 }
848 }
849 else if (strncmp (name, ".got", 4) != 0)
850 {
851 /* It's not one of our sections, so don't allocate space. */
852 continue;
853 }
854
855 if (strip)
856 {
857 asection **spp;
858
859 for (spp = &s->output_section->owner->sections;
860 *spp != s->output_section;
861 spp = &(*spp)->next)
862 ;
863 *spp = s->output_section->next;
864 --s->output_section->owner->section_count;
865
866 continue;
867 }
868
869 /* Allocate memory for the section contents. */
870 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
871 if (s->contents == NULL && s->_raw_size != 0)
872 {
873 bfd_set_error (bfd_error_no_memory);
874 return false;
875 }
876 }
877
878 if (elf_hash_table (info)->dynamic_sections_created)
879 {
880 /* Add some entries to the .dynamic section. We fill in the
881 values later, in elf_m68k_finish_dynamic_sections, but we
882 must add the entries now so that we get the correct size for
883 the .dynamic section. The DT_DEBUG entry is filled in by the
884 dynamic linker and used by the debugger. */
885 if (!info->shared)
886 {
887 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
888 return false;
889 }
890
891 if (plt)
892 {
893 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
894 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
895 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
896 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
897 return false;
898 }
899
900 if (relocs)
901 {
902 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
903 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
904 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
905 sizeof (Elf32_External_Rela)))
906 return false;
907 }
908
909 if (reltext)
910 {
911 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
912 return false;
913 }
914 }
915
916 /* If we are generating a shared library, we generate a section
917 symbol for each output section. These are local symbols, which
918 means that they must come first in the dynamic symbol table.
919 That means we must increment the dynamic symbol index of every
920 other dynamic symbol. */
921 if (info->shared)
922 {
923 int c, i;
924
925 c = bfd_count_sections (output_bfd);
926 elf_link_hash_traverse (elf_hash_table (info),
927 elf_m68k_adjust_dynindx,
928 (PTR) &c);
929 elf_hash_table (info)->dynsymcount += c;
930
931 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
932 {
933 elf_section_data (s)->dynindx = i;
934 /* These symbols will have no names, so we don't need to
935 fiddle with dynstr_index. */
936 }
937 }
938
939 return true;
940}
941
942/* Increment the index of a dynamic symbol by a given amount. Called
943 via elf_link_hash_traverse. */
944
945static boolean
946elf_m68k_adjust_dynindx (h, cparg)
947 struct elf_link_hash_entry *h;
948 PTR cparg;
949{
950 int *cp = (int *) cparg;
951
952 if (h->dynindx != -1)
953 h->dynindx += *cp;
954 return true;
955}
956
957/* Relocate an M68K ELF section. */
958
959static boolean
960elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
961 contents, relocs, local_syms, local_sections)
962 bfd *output_bfd;
963 struct bfd_link_info *info;
964 bfd *input_bfd;
965 asection *input_section;
966 bfd_byte *contents;
967 Elf_Internal_Rela *relocs;
968 Elf_Internal_Sym *local_syms;
969 asection **local_sections;
970{
971 bfd *dynobj;
972 Elf_Internal_Shdr *symtab_hdr;
973 struct elf_link_hash_entry **sym_hashes;
974 bfd_vma *local_got_offsets;
975 asection *sgot;
976 asection *sgotplt;
977 asection *splt;
978 asection *sreloc;
979 Elf_Internal_Rela *rel;
980 Elf_Internal_Rela *relend;
981
982 dynobj = elf_hash_table (info)->dynobj;
983 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
984 sym_hashes = elf_sym_hashes (input_bfd);
985 local_got_offsets = elf_local_got_offsets (input_bfd);
986
987 sgot = NULL;
988 sgotplt = NULL;
989 splt = NULL;
990 sreloc = NULL;
991
992 rel = relocs;
993 relend = relocs + input_section->reloc_count;
994 for (; rel < relend; rel++)
995 {
996 int r_type;
997 reloc_howto_type *howto;
998 long r_symndx;
999 struct elf_link_hash_entry *h;
1000 Elf_Internal_Sym *sym;
1001 asection *sec;
1002 bfd_vma relocation;
1003 bfd_reloc_status_type r;
1004
1005 r_type = ELF32_R_TYPE (rel->r_info);
1006 if (r_type < 0 || r_type >= (int) R_68K__max)
1007 {
1008 bfd_set_error (bfd_error_bad_value);
1009 return false;
1010 }
1011 howto = howto_table + r_type;
1012
1013 r_symndx = ELF32_R_SYM (rel->r_info);
1014
1015 if (info->relocateable)
1016 {
1017 /* This is a relocateable link. We don't have to change
1018 anything, unless the reloc is against a section symbol,
1019 in which case we have to adjust according to where the
1020 section symbol winds up in the output section. */
1021 if (r_symndx < symtab_hdr->sh_info)
1022 {
1023 sym = local_syms + r_symndx;
1024 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1025 {
1026 sec = local_sections[r_symndx];
1027 rel->r_addend += sec->output_offset + sym->st_value;
1028 }
1029 }
1030
1031 continue;
1032 }
1033
1034 /* This is a final link. */
1035 h = NULL;
1036 sym = NULL;
1037 sec = NULL;
1038 if (r_symndx < symtab_hdr->sh_info)
1039 {
1040 sym = local_syms + r_symndx;
1041 sec = local_sections[r_symndx];
1042 relocation = (sec->output_section->vma
1043 + sec->output_offset
1044 + sym->st_value);
1045 }
1046 else
1047 {
1048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1049 if (h->root.type == bfd_link_hash_defined
1050 || h->root.type == bfd_link_hash_defweak)
1051 {
1052 sec = h->root.u.def.section;
1053 if (((r_type == R_68K_PLT8
1054 || r_type == R_68K_PLT16
1055 || r_type == R_68K_PLT32
1056 || r_type == R_68K_PLT8O
1057 || r_type == R_68K_PLT16O
1058 || r_type == R_68K_PLT32O)
1059 && h->plt_offset != (bfd_vma) -1)
1060 || ((r_type == R_68K_GOT8O
1061 || r_type == R_68K_GOT16O
1062 || r_type == R_68K_GOT32O
1063 || ((r_type == R_68K_GOT8
1064 || r_type == R_68K_GOT16
1065 || r_type == R_68K_GOT32)
1066 && strcmp (h->root.root.string,
1067 "_GLOBAL_OFFSET_TABLE_") != 0))
1068 && elf_hash_table (info)->dynamic_sections_created)
1069 || (info->shared
1070 && (input_section->flags & SEC_ALLOC) != 0
1071 && (r_type == R_68K_8
1072 || r_type == R_68K_16
1073 || r_type == R_68K_32
1074 || r_type == R_68K_PC8
1075 || r_type == R_68K_PC16
1076 || r_type == R_68K_PC32)))
1077 {
1078 /* In these cases, we don't need the relocation
1079 value. We check specially because in some
1080 obscure cases sec->output_section will be NULL. */
1081 relocation = 0;
1082 }
1083 else
1084 relocation = (h->root.u.def.value
1085 + sec->output_section->vma
1086 + sec->output_offset);
1087 }
1088 else if (h->root.type == bfd_link_hash_undefweak)
1089 relocation = 0;
1090 else if (info->shared)
1091 relocation = 0;
1092 else
1093 {
1094 if (!(info->callbacks->undefined_symbol
1095 (info, h->root.root.string, input_bfd,
1096 input_section, rel->r_offset)))
1097 return false;
1098 relocation = 0;
1099 }
1100 }
1101
1102 switch (r_type)
1103 {
1104 case R_68K_GOT8:
1105 case R_68K_GOT16:
1106 case R_68K_GOT32:
1107 /* Relocation is to the entry for this symbol in the global
1108 offset table. */
1109 if (h != NULL
1110 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1111 break;
1112 /* Fall through. */
1113 case R_68K_GOT8O:
1114 case R_68K_GOT16O:
1115 case R_68K_GOT32O:
1116 /* Relocation is the offset of the entry for this symbol in
1117 the global offset table. */
1118
1119 if (sgot == NULL)
1120 {
1121 sgot = bfd_get_section_by_name (dynobj, ".got");
1122 BFD_ASSERT (sgot != NULL);
1123 }
1124
1125 if (sgotplt == NULL)
1126 {
1127 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1128 BFD_ASSERT (sgotplt != NULL);
1129 }
1130
1131 if (h != NULL)
1132 {
1133 bfd_vma off;
1134
1135 off = h->got_offset;
1136 BFD_ASSERT (off != (bfd_vma) -1);
1137
1138 if (!elf_hash_table (info)->dynamic_sections_created)
1139 {
1140 /* This is actually a static link. We must
1141 initialize this entry in the global offset table.
1142 Since the offset must always be a multiple of 4,
1143 we use the least significant bit to record
1144 whether we have initialized it already.
1145
1146 When doing a dynamic link, we create a .rela.got
1147 relocation entry to initialize the value. This
1148 is done in the finish_dynamic_symbol routine. */
1149 if ((off & 1) != 0)
1150 off &= ~1;
1151 else
1152 {
1153 bfd_put_32 (output_bfd, relocation,
1154 sgot->contents + off);
1155 h->got_offset |= 1;
1156 }
1157 }
1158
1159 relocation = sgot->output_offset + off;
1160 if (r_type == R_68K_GOT8O
1161 || r_type == R_68K_GOT16O
1162 || r_type == R_68K_GOT32O)
1163 relocation -= sgotplt->output_offset;
1164 }
1165 else
1166 {
1167 bfd_vma off;
1168
1169 BFD_ASSERT (local_got_offsets != NULL
1170 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1171
1172 off = local_got_offsets[r_symndx];
1173
1174 /* The offset must always be a multiple of 4. We use
1175 the least significant bit to record whether we have
1176 already generated the necessary reloc. */
1177 if ((off & 1) != 0)
1178 off &= ~1;
1179 else
1180 {
1181 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1182
1183 if (info->shared)
1184 {
1185 asection *srelgot;
1186 Elf_Internal_Rela outrel;
1187
1188 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1189 BFD_ASSERT (srelgot != NULL);
1190
1191 outrel.r_offset = (sgot->output_section->vma
1192 + sgot->output_offset
1193 + off);
1194 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1195 outrel.r_addend = 0;
1196 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1197 (((Elf32_External_Rela *)
1198 srelgot->contents)
1199 + srelgot->reloc_count));
1200 ++srelgot->reloc_count;
1201 }
1202
1203 local_got_offsets[r_symndx] |= 1;
1204 }
1205
1206 relocation = sgot->output_offset + off;
1207 if (r_type == R_68K_GOT8O
1208 || r_type == R_68K_GOT16O
1209 || r_type == R_68K_GOT32O)
1210 relocation -= sgotplt->output_offset;
1211 }
1212
1213 break;
1214
1215 case R_68K_PLT8:
1216 case R_68K_PLT16:
1217 case R_68K_PLT32:
1218 /* Relocation is to the entry for this symbol in the
1219 procedure linkage table. */
1220
1221 /* Resolve a PLT32 reloc against a local symbol directly,
1222 without using the procedure linkage table. */
1223 if (h == NULL)
1224 break;
1225
1226 if (h->plt_offset == (bfd_vma) -1)
1227 {
1228 /* We didn't make a PLT entry for this symbol. This
1229 happens when statically linking PIC code. */
1230 break;
1231 }
1232
1233 if (splt == NULL)
1234 {
1235 splt = bfd_get_section_by_name (dynobj, ".plt");
1236 BFD_ASSERT (splt != NULL);
1237 }
1238
1239 relocation = (splt->output_section->vma
1240 + splt->output_offset
1241 + h->plt_offset);
1242 break;
1243
1244 case R_68K_PLT8O:
1245 case R_68K_PLT16O:
1246 case R_68K_PLT32O:
1247 /* Relocation is the offset of the entry for this symbol in
1248 the procedure linkage table. */
1249 BFD_ASSERT (h != NULL);
1250
1251 if (h->plt_offset == (bfd_vma) -1)
1252 {
1253 /* We didn't make a PLT entry for this symbol. This
1254 happens when statically linking PIC code. */
1255 break;
1256 }
1257
1258 if (splt == NULL)
1259 {
1260 splt = bfd_get_section_by_name (dynobj, ".plt");
1261 BFD_ASSERT (splt != NULL);
1262 }
1263
1264 relocation = h->plt_offset;
1265 break;
1266
1267 case R_68K_8:
1268 case R_68K_16:
1269 case R_68K_32:
1270 case R_68K_PC8:
1271 case R_68K_PC16:
1272 case R_68K_PC32:
1273 if (info->shared
1274 && (input_section->flags & SEC_ALLOC) != 0)
1275 {
1276 Elf_Internal_Rela outrel;
1277
1278 /* When generating a shared object, these relocations
1279 are copied into the output file to be resolved at run
1280 time. */
1281
1282 if (sreloc == NULL)
1283 {
1284 const char *name;
1285
1286 name = (elf_string_from_elf_section
1287 (input_bfd,
1288 elf_elfheader (input_bfd)->e_shstrndx,
1289 elf_section_data (input_section)->rel_hdr.sh_name));
1290 if (name == NULL)
1291 return false;
1292
1293 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1294 && strcmp (bfd_get_section_name (input_bfd,
1295 input_section),
1296 name + 5) == 0);
1297
1298 sreloc = bfd_get_section_by_name (dynobj, name);
1299 BFD_ASSERT (sreloc != NULL);
1300 }
1301
1302 outrel.r_offset = (rel->r_offset
1303 + input_section->output_section->vma
1304 + input_section->output_offset);
1305 if (h != NULL)
1306 {
1307 BFD_ASSERT (h->dynindx != -1);
1308 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1309 outrel.r_addend = rel->r_addend;
1310 }
1311 else
1312 {
1313 if (r_type == R_68K_32)
1314 {
1315 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1316 outrel.r_addend = relocation + rel->r_addend;
1317 }
1318 else
1319 {
1320 long indx;
1321
1322 sym = local_syms + r_symndx;
1323
1324 BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1325
1326 sec = local_sections[r_symndx];
1327 if (sec != NULL && bfd_is_abs_section (sec))
1328 indx = 0;
1329 else if (sec == NULL || sec->owner == NULL)
1330 {
1331 bfd_set_error (bfd_error_bad_value);
1332 return false;
1333 }
1334 else
1335 {
1336 asection *osec;
1337
1338 osec = sec->output_section;
1339 indx = elf_section_data (osec)->dynindx;
1340 if (indx == 0)
1341 abort ();
1342 }
1343
1344 outrel.r_info = ELF32_R_INFO (indx, r_type);
1345 outrel.r_addend = relocation + rel->r_addend;
1346 }
1347 }
1348
1349 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1350 (((Elf32_External_Rela *)
1351 sreloc->contents)
1352 + sreloc->reloc_count));
1353 ++sreloc->reloc_count;
1354
1355 /* This reloc will be computed at runtime, so there's no
1356 need to do anything now. */
1357 continue;
1358 }
1359
1360 break;
1361
1362 default:
1363 break;
1364 }
1365
1366 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1367 contents, rel->r_offset,
1368 relocation, rel->r_addend);
1369
1370 if (r != bfd_reloc_ok)
1371 {
1372 switch (r)
1373 {
1374 default:
1375 case bfd_reloc_outofrange:
1376 abort ();
1377 case bfd_reloc_overflow:
1378 {
1379 const char *name;
1380
1381 if (h != NULL)
1382 name = h->root.root.string;
1383 else
1384 {
1385 name = elf_string_from_elf_section (input_bfd,
1386 symtab_hdr->sh_link,
1387 sym->st_name);
1388 if (name == NULL)
1389 return false;
1390 if (*name == '\0')
1391 name = bfd_section_name (input_bfd, sec);
1392 }
1393 if (!(info->callbacks->reloc_overflow
1394 (info, name, howto->name, (bfd_vma) 0,
1395 input_bfd, input_section, rel->r_offset)))
1396 return false;
1397 }
1398 break;
1399 }
1400 }
1401 }
1402
1403 return true;
1404}
1405
1406/* Finish up dynamic symbol handling. We set the contents of various
1407 dynamic sections here. */
1408
1409static boolean
1410elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1411 bfd *output_bfd;
1412 struct bfd_link_info *info;
1413 struct elf_link_hash_entry *h;
1414 Elf_Internal_Sym *sym;
1415{
1416 bfd *dynobj;
1417
1418 dynobj = elf_hash_table (info)->dynobj;
1419
1420 if (h->plt_offset != (bfd_vma) -1)
1421 {
1422 asection *splt;
1423 asection *sgot;
1424 asection *srela;
1425 bfd_vma plt_index;
1426 bfd_vma got_offset;
1427 Elf_Internal_Rela rela;
1428
1429 /* This symbol has an entry in the procedure linkage table. Set
1430 it up. */
1431
1432 BFD_ASSERT (h->dynindx != -1);
1433
1434 splt = bfd_get_section_by_name (dynobj, ".plt");
1435 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1436 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1437 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1438
1439 /* Get the index in the procedure linkage table which
1440 corresponds to this symbol. This is the index of this symbol
1441 in all the symbols for which we are making plt entries. The
1442 first entry in the procedure linkage table is reserved. */
1443 plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
1444
1445 /* Get the offset into the .got table of the entry that
1446 corresponds to this function. Each .got entry is 4 bytes.
1447 The first three are reserved. */
1448 got_offset = (plt_index + 3) * 4;
1449
1450 /* Fill in the entry in the procedure linkage table. */
1451 memcpy (splt->contents + h->plt_offset, elf_m68k_plt_entry,
1452 PLT_ENTRY_SIZE);
1453 /* The offset is relative to the first extension word. */
1454 bfd_put_32 (output_bfd,
1455 (sgot->output_section->vma
1456 + sgot->output_offset
1457 + got_offset
1458 - (splt->output_section->vma
1459 + h->plt_offset + 2)),
1460 splt->contents + h->plt_offset + 4);
1461
1462 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1463 splt->contents + h->plt_offset + 10);
1464 bfd_put_32 (output_bfd, - (h->plt_offset + 16),
1465 splt->contents + h->plt_offset + 16);
1466
1467 /* Fill in the entry in the global offset table. */
1468 bfd_put_32 (output_bfd,
1469 (splt->output_section->vma
1470 + splt->output_offset
1471 + h->plt_offset
1472 + 8),
1473 sgot->contents + got_offset);
1474
1475 /* Fill in the entry in the .rela.plt section. */
1476 rela.r_offset = (sgot->output_section->vma
1477 + sgot->output_offset
1478 + got_offset);
1479 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1480 rela.r_addend = 0;
1481 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1482 ((Elf32_External_Rela *) srela->contents
1483 + plt_index));
1484
1485 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1486 {
1487 /* Mark the symbol as undefined, rather than as defined in
1488 the .plt section. Leave the value alone. */
1489 sym->st_shndx = SHN_UNDEF;
1490 }
1491 }
1492
1493 if (h->got_offset != (bfd_vma) -1)
1494 {
1495 asection *sgot;
1496 asection *srela;
1497 Elf_Internal_Rela rela;
1498
1499 /* This symbol has an entry in the global offset table. Set it
1500 up. */
1501
1502 BFD_ASSERT (h->dynindx != -1);
1503
1504 sgot = bfd_get_section_by_name (dynobj, ".got");
1505 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1506 BFD_ASSERT (sgot != NULL && srela != NULL);
1507
1508 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1509
1510 rela.r_offset = (sgot->output_section->vma
1511 + sgot->output_offset
1512 + h->got_offset);
1513 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1514 rela.r_addend = 0;
1515 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1516 ((Elf32_External_Rela *) srela->contents
1517 + srela->reloc_count));
1518 ++srela->reloc_count;
1519 }
1520
1521 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1522 {
1523 asection *s;
1524 Elf_Internal_Rela rela;
1525
1526 /* This symbol needs a copy reloc. Set it up. */
1527
1528 BFD_ASSERT (h->dynindx != -1
1529 && (h->root.type == bfd_link_hash_defined
1530 || h->root.type == bfd_link_hash_defweak));
1531
1532 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1533 ".rela.bss");
1534 BFD_ASSERT (s != NULL);
1535
1536 rela.r_offset = (h->root.u.def.value
1537 + h->root.u.def.section->output_section->vma
1538 + h->root.u.def.section->output_offset);
1539 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
1540 rela.r_addend = 0;
1541 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1542 ((Elf32_External_Rela *) s->contents
1543 + s->reloc_count));
1544 ++s->reloc_count;
1545 }
1546
1547 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1548 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1549 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1550 sym->st_shndx = SHN_ABS;
1551
1552 return true;
1553}
1554
1555/* Finish up the dynamic sections. */
1556
1557static boolean
1558elf_m68k_finish_dynamic_sections (output_bfd, info)
1559 bfd *output_bfd;
1560 struct bfd_link_info *info;
1561{
1562 bfd *dynobj;
1563 asection *sgot;
1564 asection *sdyn;
1565
1566 dynobj = elf_hash_table (info)->dynobj;
1567
1568 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1569 BFD_ASSERT (sgot != NULL);
1570 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1571
1572 if (elf_hash_table (info)->dynamic_sections_created)
1573 {
1574 asection *splt;
1575 Elf32_External_Dyn *dyncon, *dynconend;
1576
1577 splt = bfd_get_section_by_name (dynobj, ".plt");
1578 BFD_ASSERT (splt != NULL && sdyn != NULL);
1579
1580 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1581 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1582 for (; dyncon < dynconend; dyncon++)
1583 {
1584 Elf_Internal_Dyn dyn;
1585 const char *name;
1586 asection *s;
1587
1588 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1589
1590 switch (dyn.d_tag)
1591 {
1592 default:
1593 break;
1594
1595 case DT_PLTGOT:
1596 name = ".got";
1597 goto get_vma;
1598 case DT_JMPREL:
1599 name = ".rela.plt";
1600 get_vma:
1601 s = bfd_get_section_by_name (output_bfd, name);
1602 BFD_ASSERT (s != NULL);
1603 dyn.d_un.d_ptr = s->vma;
1604 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1605 break;
1606
1607 case DT_PLTRELSZ:
1608 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1609 BFD_ASSERT (s != NULL);
1610 if (s->_cooked_size != 0)
1611 dyn.d_un.d_val = s->_cooked_size;
1612 else
1613 dyn.d_un.d_val = s->_raw_size;
1614 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1615 break;
1616
1617 case DT_RELASZ:
1618 /* My reading of the SVR4 ABI indicates that the
1619 procedure linkage table relocs (DT_JMPREL) should be
1620 included in the overall relocs (DT_RELA). This is
1621 what Solaris does. However, UnixWare can not handle
1622 that case. Therefore, we override the DT_RELASZ entry
1623 here to make it not include the JMPREL relocs. Since
1624 the linker script arranges for .rela.plt to follow all
1625 other relocation sections, we don't have to worry
1626 about changing the DT_RELA entry. */
1627 /* FIXME: This comment is from elf32-i386.c, what about
1628 the SVR4/m68k implementations? */
1629 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1630 if (s != NULL)
1631 {
1632 if (s->_cooked_size != 0)
1633 dyn.d_un.d_val -= s->_cooked_size;
1634 else
1635 dyn.d_un.d_val -= s->_raw_size;
1636 }
1637 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1638 break;
1639 }
1640 }
1641
1642 /* Fill in the first entry in the procedure linkage table. */
1643 if (splt->_raw_size > 0)
1644 {
1645 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
1646 bfd_put_32 (output_bfd,
1647 (sgot->output_section->vma
1648 + sgot->output_offset + 4
1649 - (splt->output_section->vma + 2)),
1650 splt->contents + 4);
1651 bfd_put_32 (output_bfd,
1652 (sgot->output_section->vma
1653 + sgot->output_offset + 8
1654 - (splt->output_section->vma + 10)),
1655 splt->contents + 12);
1656 }
1657
1658 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1659 = PLT_ENTRY_SIZE;
1660 }
1661
1662 /* Fill in the first three entries in the global offset table. */
1663 if (sgot->_raw_size > 0)
1664 {
1665 if (sdyn == NULL)
1666 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1667 else
1668 bfd_put_32 (output_bfd,
1669 sdyn->output_section->vma + sdyn->output_offset,
1670 sgot->contents);
1671 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1672 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1673 }
1674
1675 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1676
1677 return true;
1678}
1679
1680#define TARGET_BIG_SYM bfd_elf32_m68k_vec
1681#define TARGET_BIG_NAME "elf32-m68k"
1682#define ELF_MACHINE_CODE EM_68K
1683#define ELF_MAXPAGESIZE 0x2000
1684#define elf_backend_create_dynamic_sections \
1685 elf_m68k_create_dynamic_sections
1686#define elf_backend_check_relocs elf_m68k_check_relocs
1687#define elf_backend_adjust_dynamic_symbol \
1688 elf_m68k_adjust_dynamic_symbol
1689#define elf_backend_size_dynamic_sections \
1690 elf_m68k_size_dynamic_sections
1691#define elf_backend_relocate_section elf_m68k_relocate_section
1692#define elf_backend_finish_dynamic_symbol \
1693 elf_m68k_finish_dynamic_symbol
1694#define elf_backend_finish_dynamic_sections \
1695 elf_m68k_finish_dynamic_sections
1696
1697#include "elf32-target.h"
This page took 0.105947 seconds and 4 git commands to generate.