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