]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-m68k.c
* pdp11-dis.c (print_insn_pdp11): Handle illegal instructions.
[thirdparty/binutils-gdb.git] / bfd / elf32-m68k.c
CommitLineData
252b5132 1/* Motorola 68k series support for 32-bit ELF
7898deda
NC
2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/m68k.h"
27
28static reloc_howto_type *reloc_type_lookup
29 PARAMS ((bfd *, bfd_reloc_code_real_type));
30static void rtype_to_howto
31 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
32static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
33 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
34static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
35 PARAMS ((bfd *));
36static boolean elf_m68k_check_relocs
37 PARAMS ((bfd *, struct bfd_link_info *, asection *,
38 const Elf_Internal_Rela *));
39static asection *elf_m68k_gc_mark_hook
40 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
41 struct elf_link_hash_entry *, Elf_Internal_Sym *));
42static boolean elf_m68k_gc_sweep_hook
43 PARAMS ((bfd *, struct bfd_link_info *, asection *,
44 const Elf_Internal_Rela *));
45static boolean elf_m68k_adjust_dynamic_symbol
46 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
252b5132
RH
47static boolean elf_m68k_size_dynamic_sections
48 PARAMS ((bfd *, struct bfd_link_info *));
49static boolean elf_m68k_relocate_section
50 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
51 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
52static boolean elf_m68k_finish_dynamic_symbol
53 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
54 Elf_Internal_Sym *));
55static boolean elf_m68k_finish_dynamic_sections
56 PARAMS ((bfd *, struct bfd_link_info *));
57
9e1281c7
CM
58static boolean elf32_m68k_set_private_flags
59 PARAMS ((bfd *, flagword));
60static boolean elf32_m68k_copy_private_bfd_data
61 PARAMS ((bfd *, bfd *));
62static boolean elf32_m68k_merge_private_bfd_data
63 PARAMS ((bfd *, bfd *));
64static boolean elf32_m68k_print_private_bfd_data
65 PARAMS ((bfd *, PTR));
aa91b392 66static enum elf_reloc_type_class elf32_m68k_reloc_type_class
f51e552e 67 PARAMS ((const Elf_Internal_Rela *));
9e1281c7 68
252b5132
RH
69static reloc_howto_type howto_table[] = {
70 HOWTO(R_68K_NONE, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", false, 0, 0x00000000,false),
71 HOWTO(R_68K_32, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", false, 0, 0xffffffff,false),
72 HOWTO(R_68K_16, 0, 1,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", false, 0, 0x0000ffff,false),
73 HOWTO(R_68K_8, 0, 0, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", false, 0, 0x000000ff,false),
74 HOWTO(R_68K_PC32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", false, 0, 0xffffffff,true),
75 HOWTO(R_68K_PC16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", false, 0, 0x0000ffff,true),
76 HOWTO(R_68K_PC8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", false, 0, 0x000000ff,true),
77 HOWTO(R_68K_GOT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", false, 0, 0xffffffff,true),
78 HOWTO(R_68K_GOT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", false, 0, 0x0000ffff,true),
79 HOWTO(R_68K_GOT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", false, 0, 0x000000ff,true),
80 HOWTO(R_68K_GOT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", false, 0, 0xffffffff,false),
81 HOWTO(R_68K_GOT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", false, 0, 0x0000ffff,false),
82 HOWTO(R_68K_GOT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", false, 0, 0x000000ff,false),
83 HOWTO(R_68K_PLT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", false, 0, 0xffffffff,true),
84 HOWTO(R_68K_PLT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", false, 0, 0x0000ffff,true),
85 HOWTO(R_68K_PLT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", false, 0, 0x000000ff,true),
86 HOWTO(R_68K_PLT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", false, 0, 0xffffffff,false),
87 HOWTO(R_68K_PLT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", false, 0, 0x0000ffff,false),
88 HOWTO(R_68K_PLT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", false, 0, 0x000000ff,false),
89 HOWTO(R_68K_COPY, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", false, 0, 0xffffffff,false),
90 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),
91 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),
92 HOWTO(R_68K_RELATIVE, 0, 2,32, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", false, 0, 0xffffffff,false),
93 /* GNU extension to record C++ vtable hierarchy */
94 HOWTO (R_68K_GNU_VTINHERIT, /* type */
95 0, /* rightshift */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
97 0, /* bitsize */
98 false, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_dont, /* complain_on_overflow */
101 NULL, /* special_function */
102 "R_68K_GNU_VTINHERIT", /* name */
103 false, /* partial_inplace */
104 0, /* src_mask */
105 0, /* dst_mask */
106 false),
107 /* GNU extension to record C++ vtable member usage */
108 HOWTO (R_68K_GNU_VTENTRY, /* type */
109 0, /* rightshift */
110 2, /* size (0 = byte, 1 = short, 2 = long) */
111 0, /* bitsize */
112 false, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_dont, /* complain_on_overflow */
115 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
116 "R_68K_GNU_VTENTRY", /* name */
117 false, /* partial_inplace */
118 0, /* src_mask */
119 0, /* dst_mask */
120 false),
121};
122
123static void
124rtype_to_howto (abfd, cache_ptr, dst)
121089cb 125 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
126 arelent *cache_ptr;
127 Elf_Internal_Rela *dst;
128{
129 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
130 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
131}
132
133#define elf_info_to_howto rtype_to_howto
134
135static const struct
136{
137 bfd_reloc_code_real_type bfd_val;
138 int elf_val;
139} reloc_map[] = {
140 { BFD_RELOC_NONE, R_68K_NONE },
141 { BFD_RELOC_32, R_68K_32 },
142 { BFD_RELOC_16, R_68K_16 },
143 { BFD_RELOC_8, R_68K_8 },
144 { BFD_RELOC_32_PCREL, R_68K_PC32 },
145 { BFD_RELOC_16_PCREL, R_68K_PC16 },
146 { BFD_RELOC_8_PCREL, R_68K_PC8 },
147 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
148 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
149 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
150 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
151 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
152 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
153 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
154 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
155 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
156 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
157 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
158 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
159 { BFD_RELOC_NONE, R_68K_COPY },
160 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
161 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
162 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
163 { BFD_RELOC_CTOR, R_68K_32 },
164 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
165 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
166};
167
168static reloc_howto_type *
169reloc_type_lookup (abfd, code)
121089cb 170 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
171 bfd_reloc_code_real_type code;
172{
173 unsigned int i;
174 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
175 {
176 if (reloc_map[i].bfd_val == code)
177 return &howto_table[reloc_map[i].elf_val];
178 }
179 return 0;
180}
181
182#define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
183#define ELF_ARCH bfd_arch_m68k
184/* end code generated by elf.el */
185
186#define USE_RELA
252b5132
RH
187\f
188/* Functions for the m68k ELF linker. */
189
190/* The name of the dynamic interpreter. This is put in the .interp
191 section. */
192
193#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
194
195/* The size in bytes of an entry in the procedure linkage table. */
196
197#define PLT_ENTRY_SIZE 20
198
199/* The first entry in a procedure linkage table looks like this. See
200 the SVR4 ABI m68k supplement to see how this works. */
201
202static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
203{
204 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
205 0, 0, 0, 0, /* replaced with offset to .got + 4. */
206 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
207 0, 0, 0, 0, /* replaced with offset to .got + 8. */
208 0, 0, 0, 0 /* pad out to 20 bytes. */
209};
210
211/* Subsequent entries in a procedure linkage table look like this. */
212
213static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
214{
215 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
216 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
217 0x2f, 0x3c, /* move.l #offset,-(%sp) */
218 0, 0, 0, 0, /* replaced with offset into relocation table. */
219 0x60, 0xff, /* bra.l .plt */
220 0, 0, 0, 0 /* replaced with offset to start of .plt. */
221};
222
9e1281c7
CM
223#define CPU32_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_CPU32)
224
225#define PLT_CPU32_ENTRY_SIZE 24
226/* Procedure linkage table entries for the cpu32 */
227static const bfd_byte elf_cpu32_plt0_entry[PLT_CPU32_ENTRY_SIZE] =
228{
6091b433 229 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
9e1281c7 230 0, 0, 0, 0, /* replaced with offset to .got + 4. */
6091b433 231 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
c3668558 232 0, 0, 0, 0, /* replace with offset to .got +8. */
6091b433 233 0x4e, 0xd1, /* jmp %a1@ */
9e1281c7
CM
234 0, 0, 0, 0, /* pad out to 24 bytes. */
235 0, 0
236};
237
238static const bfd_byte elf_cpu32_plt_entry[PLT_CPU32_ENTRY_SIZE] =
239{
1ca42bad 240 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
9e1281c7 241 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
1ca42bad 242 0x4e, 0xd1, /* jmp %a1@ */
9e1281c7
CM
243 0x2f, 0x3c, /* move.l #offset,-(%sp) */
244 0, 0, 0, 0, /* replaced with offset into relocation table. */
245 0x60, 0xff, /* bra.l .plt */
246 0, 0, 0, 0, /* replaced with offset to start of .plt. */
247 0, 0
248};
249
252b5132
RH
250/* The m68k linker needs to keep track of the number of relocs that it
251 decides to copy in check_relocs for each symbol. This is so that it
252 can discard PC relative relocs if it doesn't need them when linking
253 with -Bsymbolic. We store the information in a field extending the
254 regular ELF linker hash table. */
255
256/* This structure keeps track of the number of PC relative relocs we have
257 copied for a given symbol. */
258
259struct elf_m68k_pcrel_relocs_copied
260{
261 /* Next section. */
262 struct elf_m68k_pcrel_relocs_copied *next;
263 /* A section in dynobj. */
264 asection *section;
265 /* Number of relocs copied in this section. */
266 bfd_size_type count;
267};
268
269/* m68k ELF linker hash entry. */
270
271struct elf_m68k_link_hash_entry
272{
273 struct elf_link_hash_entry root;
274
275 /* Number of PC relative relocs copied for this symbol. */
276 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
277};
278
279/* m68k ELF linker hash table. */
280
281struct elf_m68k_link_hash_table
282{
283 struct elf_link_hash_table root;
284};
285
286/* Declare this now that the above structures are defined. */
287
288static boolean elf_m68k_discard_copies
289 PARAMS ((struct elf_m68k_link_hash_entry *, PTR));
290
291/* Traverse an m68k ELF linker hash table. */
292
293#define elf_m68k_link_hash_traverse(table, func, info) \
294 (elf_link_hash_traverse \
295 (&(table)->root, \
296 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
297 (info)))
298
299/* Get the m68k ELF linker hash table from a link_info structure. */
300
301#define elf_m68k_hash_table(p) \
302 ((struct elf_m68k_link_hash_table *) (p)->hash)
303
304/* Create an entry in an m68k ELF linker hash table. */
305
306static struct bfd_hash_entry *
307elf_m68k_link_hash_newfunc (entry, table, string)
308 struct bfd_hash_entry *entry;
309 struct bfd_hash_table *table;
310 const char *string;
311{
312 struct elf_m68k_link_hash_entry *ret =
313 (struct elf_m68k_link_hash_entry *) entry;
314
315 /* Allocate the structure if it has not already been allocated by a
316 subclass. */
317 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
318 ret = ((struct elf_m68k_link_hash_entry *)
319 bfd_hash_allocate (table,
320 sizeof (struct elf_m68k_link_hash_entry)));
321 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
322 return (struct bfd_hash_entry *) ret;
323
324 /* Call the allocation method of the superclass. */
325 ret = ((struct elf_m68k_link_hash_entry *)
326 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
327 table, string));
328 if (ret != (struct elf_m68k_link_hash_entry *) NULL)
329 {
330 ret->pcrel_relocs_copied = NULL;
331 }
332
333 return (struct bfd_hash_entry *) ret;
334}
335
336/* Create an m68k ELF linker hash table. */
337
338static struct bfd_link_hash_table *
339elf_m68k_link_hash_table_create (abfd)
340 bfd *abfd;
341{
342 struct elf_m68k_link_hash_table *ret;
dc810e39 343 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
252b5132 344
dc810e39 345 ret = (struct elf_m68k_link_hash_table *) bfd_alloc (abfd, amt);
252b5132
RH
346 if (ret == (struct elf_m68k_link_hash_table *) NULL)
347 return NULL;
348
349 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
350 elf_m68k_link_hash_newfunc))
351 {
352 bfd_release (abfd, ret);
353 return NULL;
354 }
355
356 return &ret->root.root;
357}
358
9e1281c7
CM
359/* Keep m68k-specific flags in the ELF header */
360static boolean
361elf32_m68k_set_private_flags (abfd, flags)
362 bfd *abfd;
363 flagword flags;
364{
365 elf_elfheader (abfd)->e_flags = flags;
366 elf_flags_init (abfd) = true;
367 return true;
368}
369
370/* Copy m68k-specific data from one module to another */
371static boolean
372elf32_m68k_copy_private_bfd_data (ibfd, obfd)
373 bfd *ibfd;
374 bfd *obfd;
375{
376 flagword in_flags;
377
378 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
379 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
380 return true;
c3668558 381
9e1281c7 382 in_flags = elf_elfheader (ibfd)->e_flags;
c3668558 383
9e1281c7
CM
384 elf_elfheader (obfd)->e_flags = in_flags;
385 elf_flags_init (obfd) = true;
c3668558 386
9e1281c7
CM
387 return true;
388}
389
9e1281c7
CM
390/* Merge backend specific data from an object file to the output
391 object file when linking. */
392static boolean
393elf32_m68k_merge_private_bfd_data (ibfd, obfd)
394 bfd *ibfd;
395 bfd *obfd;
396{
397 flagword out_flags;
398 flagword in_flags;
399
400 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
401 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
402 return true;
403
404 in_flags = elf_elfheader (ibfd)->e_flags;
405 out_flags = elf_elfheader (obfd)->e_flags;
406
407 if (!elf_flags_init (obfd))
408 {
409 elf_flags_init (obfd) = true;
410 elf_elfheader (obfd)->e_flags = in_flags;
411 }
412
413 return true;
414}
415
416/* Display the flags field */
417static boolean
418elf32_m68k_print_private_bfd_data (abfd, ptr)
419 bfd *abfd;
420 PTR ptr;
421{
422 FILE *file = (FILE *) ptr;
423
424 BFD_ASSERT (abfd != NULL && ptr != NULL);
425
426 /* Print normal ELF private data. */
427 _bfd_elf_print_private_bfd_data (abfd, ptr);
428
429 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
430
431 /* xgettext:c-format */
432 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
433
434 if (elf_elfheader (abfd)->e_flags & EF_CPU32)
435 fprintf (file, _ (" [cpu32]"));
436
437 fputc ('\n', file);
438
439 return true;
440}
252b5132
RH
441/* Look through the relocs for a section during the first phase, and
442 allocate space in the global offset table or procedure linkage
443 table. */
444
445static boolean
446elf_m68k_check_relocs (abfd, info, sec, relocs)
447 bfd *abfd;
448 struct bfd_link_info *info;
449 asection *sec;
450 const Elf_Internal_Rela *relocs;
451{
452 bfd *dynobj;
453 Elf_Internal_Shdr *symtab_hdr;
454 struct elf_link_hash_entry **sym_hashes;
455 bfd_signed_vma *local_got_refcounts;
456 const Elf_Internal_Rela *rel;
457 const Elf_Internal_Rela *rel_end;
458 asection *sgot;
459 asection *srelgot;
460 asection *sreloc;
461
462 if (info->relocateable)
463 return true;
464
465 dynobj = elf_hash_table (info)->dynobj;
466 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
467 sym_hashes = elf_sym_hashes (abfd);
468 local_got_refcounts = elf_local_got_refcounts (abfd);
469
470 sgot = NULL;
471 srelgot = NULL;
472 sreloc = NULL;
473
474 rel_end = relocs + sec->reloc_count;
475 for (rel = relocs; rel < rel_end; rel++)
476 {
477 unsigned long r_symndx;
478 struct elf_link_hash_entry *h;
479
480 r_symndx = ELF32_R_SYM (rel->r_info);
481
482 if (r_symndx < symtab_hdr->sh_info)
483 h = NULL;
484 else
485 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
486
487 switch (ELF32_R_TYPE (rel->r_info))
488 {
489 case R_68K_GOT8:
490 case R_68K_GOT16:
491 case R_68K_GOT32:
492 if (h != NULL
493 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
494 break;
495 /* Fall through. */
496 case R_68K_GOT8O:
497 case R_68K_GOT16O:
498 case R_68K_GOT32O:
499 /* This symbol requires a global offset table entry. */
500
501 if (dynobj == NULL)
502 {
503 /* Create the .got section. */
504 elf_hash_table (info)->dynobj = dynobj = abfd;
505 if (!_bfd_elf_create_got_section (dynobj, info))
506 return false;
507 }
508
509 if (sgot == NULL)
510 {
511 sgot = bfd_get_section_by_name (dynobj, ".got");
512 BFD_ASSERT (sgot != NULL);
513 }
514
515 if (srelgot == NULL
516 && (h != NULL || info->shared))
517 {
518 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
519 if (srelgot == NULL)
520 {
521 srelgot = bfd_make_section (dynobj, ".rela.got");
522 if (srelgot == NULL
523 || !bfd_set_section_flags (dynobj, srelgot,
524 (SEC_ALLOC
525 | SEC_LOAD
526 | SEC_HAS_CONTENTS
527 | SEC_IN_MEMORY
528 | SEC_LINKER_CREATED
529 | SEC_READONLY))
530 || !bfd_set_section_alignment (dynobj, srelgot, 2))
531 return false;
532 }
533 }
534
535 if (h != NULL)
536 {
51b64d56 537 if (h->got.refcount == 0)
252b5132 538 {
252b5132
RH
539 /* Make sure this symbol is output as a dynamic symbol. */
540 if (h->dynindx == -1)
541 {
542 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
543 return false;
544 }
545
546 /* Allocate space in the .got section. */
547 sgot->_raw_size += 4;
548 /* Allocate relocation space. */
549 srelgot->_raw_size += sizeof (Elf32_External_Rela);
550 }
51b64d56 551 h->got.refcount++;
252b5132
RH
552 }
553 else
554 {
555 /* This is a global offset table entry for a local symbol. */
556 if (local_got_refcounts == NULL)
557 {
dc810e39 558 bfd_size_type size;
252b5132 559
dc810e39
AM
560 size = symtab_hdr->sh_info;
561 size *= sizeof (bfd_signed_vma);
252b5132 562 local_got_refcounts = ((bfd_signed_vma *)
51b64d56 563 bfd_zalloc (abfd, size));
252b5132
RH
564 if (local_got_refcounts == NULL)
565 return false;
566 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 567 }
51b64d56 568 if (local_got_refcounts[r_symndx] == 0)
252b5132 569 {
252b5132
RH
570 sgot->_raw_size += 4;
571 if (info->shared)
572 {
573 /* If we are generating a shared object, we need to
574 output a R_68K_RELATIVE reloc so that the dynamic
575 linker can adjust this GOT entry. */
576 srelgot->_raw_size += sizeof (Elf32_External_Rela);
577 }
578 }
51b64d56 579 local_got_refcounts[r_symndx]++;
252b5132
RH
580 }
581 break;
582
583 case R_68K_PLT8:
584 case R_68K_PLT16:
585 case R_68K_PLT32:
586 /* This symbol requires a procedure linkage table entry. We
587 actually build the entry in adjust_dynamic_symbol,
588 because this might be a case of linking PIC code which is
589 never referenced by a dynamic object, in which case we
590 don't need to generate a procedure linkage table entry
591 after all. */
592
593 /* If this is a local symbol, we resolve it directly without
594 creating a procedure linkage table entry. */
595 if (h == NULL)
596 continue;
597
598 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
51b64d56 599 h->plt.refcount++;
252b5132
RH
600 break;
601
602 case R_68K_PLT8O:
603 case R_68K_PLT16O:
604 case R_68K_PLT32O:
605 /* This symbol requires a procedure linkage table entry. */
606
607 if (h == NULL)
608 {
609 /* It does not make sense to have this relocation for a
610 local symbol. FIXME: does it? How to handle it if
611 it does make sense? */
612 bfd_set_error (bfd_error_bad_value);
613 return false;
614 }
615
616 /* Make sure this symbol is output as a dynamic symbol. */
617 if (h->dynindx == -1)
618 {
619 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
620 return false;
621 }
622
623 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
51b64d56 624 h->plt.refcount++;
252b5132
RH
625 break;
626
627 case R_68K_PC8:
628 case R_68K_PC16:
629 case R_68K_PC32:
630 /* If we are creating a shared library and this is not a local
631 symbol, we need to copy the reloc into the shared library.
632 However when linking with -Bsymbolic and this is a global
633 symbol which is defined in an object we are including in the
634 link (i.e., DEF_REGULAR is set), then we can resolve the
635 reloc directly. At this point we have not seen all the input
636 files, so it is possible that DEF_REGULAR is not set now but
637 will be set later (it is never cleared). We account for that
638 possibility below by storing information in the
639 pcrel_relocs_copied field of the hash table entry. */
640 if (!(info->shared
641 && (sec->flags & SEC_ALLOC) != 0
642 && h != NULL
643 && (!info->symbolic
644 || (h->elf_link_hash_flags
645 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
646 {
647 if (h != NULL)
648 {
649 /* Make sure a plt entry is created for this symbol if
650 it turns out to be a function defined by a dynamic
651 object. */
51b64d56 652 h->plt.refcount++;
252b5132
RH
653 }
654 break;
655 }
656 /* Fall through. */
657 case R_68K_8:
658 case R_68K_16:
659 case R_68K_32:
660 if (h != NULL)
661 {
662 /* Make sure a plt entry is created for this symbol if it
663 turns out to be a function defined by a dynamic object. */
51b64d56 664 h->plt.refcount++;
252b5132
RH
665 }
666
667 /* If we are creating a shared library, we need to copy the
668 reloc into the shared library. */
669 if (info->shared
670 && (sec->flags & SEC_ALLOC) != 0)
671 {
672 /* When creating a shared object, we must copy these
673 reloc types into the output file. We create a reloc
674 section in dynobj and make room for this reloc. */
675 if (sreloc == NULL)
676 {
677 const char *name;
678
679 name = (bfd_elf_string_from_elf_section
680 (abfd,
681 elf_elfheader (abfd)->e_shstrndx,
682 elf_section_data (sec)->rel_hdr.sh_name));
683 if (name == NULL)
684 return false;
685
686 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
687 && strcmp (bfd_get_section_name (abfd, sec),
688 name + 5) == 0);
689
690 sreloc = bfd_get_section_by_name (dynobj, name);
691 if (sreloc == NULL)
692 {
693 sreloc = bfd_make_section (dynobj, name);
694 if (sreloc == NULL
695 || !bfd_set_section_flags (dynobj, sreloc,
696 (SEC_ALLOC
697 | SEC_LOAD
698 | SEC_HAS_CONTENTS
699 | SEC_IN_MEMORY
700 | SEC_LINKER_CREATED
701 | SEC_READONLY))
702 || !bfd_set_section_alignment (dynobj, sreloc, 2))
703 return false;
704 }
aa91b392
AS
705 if (sec->flags & SEC_READONLY)
706 info->flags |= DF_TEXTREL;
252b5132
RH
707 }
708
709 sreloc->_raw_size += sizeof (Elf32_External_Rela);
710
711 /* If we are linking with -Bsymbolic, we count the number of
712 PC relative relocations we have entered for this symbol,
713 so that we can discard them again if the symbol is later
714 defined by a regular object. Note that this function is
715 only called if we are using an m68kelf linker hash table,
716 which means that h is really a pointer to an
717 elf_m68k_link_hash_entry. */
718 if ((ELF32_R_TYPE (rel->r_info) == R_68K_PC8
719 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
720 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
721 && info->symbolic)
722 {
723 struct elf_m68k_link_hash_entry *eh;
724 struct elf_m68k_pcrel_relocs_copied *p;
725
726 eh = (struct elf_m68k_link_hash_entry *) h;
727
728 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
729 if (p->section == sreloc)
730 break;
731
732 if (p == NULL)
733 {
734 p = ((struct elf_m68k_pcrel_relocs_copied *)
dc810e39 735 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
252b5132
RH
736 if (p == NULL)
737 return false;
738 p->next = eh->pcrel_relocs_copied;
739 eh->pcrel_relocs_copied = p;
740 p->section = sreloc;
741 p->count = 0;
742 }
743
744 ++p->count;
745 }
746 }
747
748 break;
749
750 /* This relocation describes the C++ object vtable hierarchy.
751 Reconstruct it for later use during GC. */
752 case R_68K_GNU_VTINHERIT:
753 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
754 return false;
755 break;
756
757 /* This relocation describes which C++ vtable entries are actually
758 used. Record for later use during GC. */
759 case R_68K_GNU_VTENTRY:
760 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
761 return false;
762 break;
763
764 default:
765 break;
766 }
767 }
768
769 return true;
770}
771
772/* Return the section that should be marked against GC for a given
773 relocation. */
774
775static asection *
776elf_m68k_gc_mark_hook (abfd, info, rel, h, sym)
777 bfd *abfd;
121089cb 778 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
779 Elf_Internal_Rela *rel;
780 struct elf_link_hash_entry *h;
781 Elf_Internal_Sym *sym;
782{
783 if (h != NULL)
784 {
785 switch (ELF32_R_TYPE (rel->r_info))
786 {
787 case R_68K_GNU_VTINHERIT:
788 case R_68K_GNU_VTENTRY:
789 break;
790
791 default:
792 switch (h->root.type)
793 {
794 default:
795 break;
796
797 case bfd_link_hash_defined:
798 case bfd_link_hash_defweak:
799 return h->root.u.def.section;
800
801 case bfd_link_hash_common:
802 return h->root.u.c.p->section;
803 }
804 }
805 }
806 else
807 {
808 if (!(elf_bad_symtab (abfd)
809 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
810 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
811 && sym->st_shndx != SHN_COMMON))
812 {
813 return bfd_section_from_elf_index (abfd, sym->st_shndx);
814 }
815 }
816
817 return NULL;
818}
819
820/* Update the got entry reference counts for the section being removed. */
821
822static boolean
823elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
824 bfd *abfd;
825 struct bfd_link_info *info;
826 asection *sec;
827 const Elf_Internal_Rela *relocs;
828{
829 Elf_Internal_Shdr *symtab_hdr;
830 struct elf_link_hash_entry **sym_hashes;
831 bfd_signed_vma *local_got_refcounts;
832 const Elf_Internal_Rela *rel, *relend;
833 unsigned long r_symndx;
834 struct elf_link_hash_entry *h;
835 bfd *dynobj;
dd5724d5
AM
836 asection *sgot;
837 asection *srelgot;
252b5132
RH
838
839 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
840 sym_hashes = elf_sym_hashes (abfd);
841 local_got_refcounts = elf_local_got_refcounts (abfd);
842
843 dynobj = elf_hash_table (info)->dynobj;
dd5724d5
AM
844 if (dynobj == NULL)
845 return true;
846
847 sgot = bfd_get_section_by_name (dynobj, ".got");
848 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
252b5132
RH
849
850 relend = relocs + sec->reloc_count;
851 for (rel = relocs; rel < relend; rel++)
852 {
853 switch (ELF32_R_TYPE (rel->r_info))
854 {
855 case R_68K_GOT8:
856 case R_68K_GOT16:
857 case R_68K_GOT32:
858 case R_68K_GOT8O:
859 case R_68K_GOT16O:
860 case R_68K_GOT32O:
861 r_symndx = ELF32_R_SYM (rel->r_info);
862 if (r_symndx >= symtab_hdr->sh_info)
863 {
864 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
865 if (h->got.refcount > 0)
866 {
867 --h->got.refcount;
868 if (h->got.refcount == 0)
869 {
870 /* We don't need the .got entry any more. */
871 sgot->_raw_size -= 4;
872 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
873 }
874 }
875 }
dd5724d5 876 else if (local_got_refcounts != NULL)
252b5132
RH
877 {
878 if (local_got_refcounts[r_symndx] > 0)
879 {
880 --local_got_refcounts[r_symndx];
881 if (local_got_refcounts[r_symndx] == 0)
882 {
883 /* We don't need the .got entry any more. */
884 sgot->_raw_size -= 4;
885 if (info->shared)
886 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
887 }
888 }
889 }
890 break;
891
892 case R_68K_PLT8:
893 case R_68K_PLT16:
894 case R_68K_PLT32:
895 case R_68K_PLT8O:
896 case R_68K_PLT16O:
897 case R_68K_PLT32O:
898 case R_68K_PC8:
899 case R_68K_PC16:
900 case R_68K_PC32:
901 case R_68K_8:
902 case R_68K_16:
903 case R_68K_32:
904 r_symndx = ELF32_R_SYM (rel->r_info);
905 if (r_symndx >= symtab_hdr->sh_info)
906 {
907 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
908 if (h->plt.refcount > 0)
909 --h->plt.refcount;
910 }
911 break;
912
913 default:
914 break;
915 }
916 }
917
918 return true;
919}
920
252b5132
RH
921/* Adjust a symbol defined by a dynamic object and referenced by a
922 regular object. The current definition is in some section of the
923 dynamic object, but we're not including those sections. We have to
924 change the definition to something the rest of the link can
925 understand. */
926
927static boolean
928elf_m68k_adjust_dynamic_symbol (info, h)
929 struct bfd_link_info *info;
930 struct elf_link_hash_entry *h;
931{
932 bfd *dynobj;
933 asection *s;
934 unsigned int power_of_two;
935
936 dynobj = elf_hash_table (info)->dynobj;
937
938 /* Make sure we know what is going on here. */
939 BFD_ASSERT (dynobj != NULL
940 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
941 || h->weakdef != NULL
942 || ((h->elf_link_hash_flags
943 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
944 && (h->elf_link_hash_flags
945 & ELF_LINK_HASH_REF_REGULAR) != 0
946 && (h->elf_link_hash_flags
947 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
948
949 /* If this is a function, put it in the procedure linkage table. We
950 will fill in the contents of the procedure linkage table later,
951 when we know the address of the .got section. */
952 if (h->type == STT_FUNC
953 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
954 {
955 if (! info->shared
956 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
957 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
958 /* We must always create the plt entry if it was referenced
959 by a PLTxxO relocation. In this case we already recorded
960 it as a dynamic symbol. */
961 && h->dynindx == -1)
962 {
963 /* This case can occur if we saw a PLTxx reloc in an input
964 file, but the symbol was never referred to by a dynamic
965 object. In such a case, we don't actually need to build
966 a procedure linkage table, and we can just do a PCxx
967 reloc instead. */
968 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
969 h->plt.offset = (bfd_vma) -1;
970 return true;
971 }
972
973 /* GC may have rendered this entry unused. */
974 if (h->plt.refcount <= 0)
975 {
976 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
977 h->plt.offset = (bfd_vma) -1;
978 return true;
979 }
980
981 /* Make sure this symbol is output as a dynamic symbol. */
982 if (h->dynindx == -1)
983 {
984 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
985 return false;
986 }
987
988 s = bfd_get_section_by_name (dynobj, ".plt");
989 BFD_ASSERT (s != NULL);
990
991 /* If this is the first .plt entry, make room for the special
992 first entry. */
993 if (s->_raw_size == 0)
70f06c4a
AS
994 {
995 if (CPU32_FLAG (dynobj))
996 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
997 else
998 s->_raw_size += PLT_ENTRY_SIZE;
999 }
252b5132
RH
1000
1001 /* If this symbol is not defined in a regular file, and we are
1002 not generating a shared library, then set the symbol to this
1003 location in the .plt. This is required to make function
1004 pointers compare as equal between the normal executable and
1005 the shared library. */
1006 if (!info->shared
1007 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1008 {
1009 h->root.u.def.section = s;
1010 h->root.u.def.value = s->_raw_size;
1011 }
1012
1013 h->plt.offset = s->_raw_size;
1014
1015 /* Make room for this entry. */
9e1281c7
CM
1016 if (CPU32_FLAG (dynobj))
1017 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1018 else
1019 s->_raw_size += PLT_ENTRY_SIZE;
252b5132
RH
1020
1021 /* We also need to make an entry in the .got.plt section, which
1022 will be placed in the .got section by the linker script. */
1023
1024 s = bfd_get_section_by_name (dynobj, ".got.plt");
1025 BFD_ASSERT (s != NULL);
1026 s->_raw_size += 4;
1027
1028 /* We also need to make an entry in the .rela.plt section. */
1029
1030 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1031 BFD_ASSERT (s != NULL);
1032 s->_raw_size += sizeof (Elf32_External_Rela);
1033
1034 return true;
1035 }
1036
1037 /* Reinitialize the plt offset now that it is not used as a reference
1038 count any more. */
1039 h->plt.offset = (bfd_vma) -1;
1040
1041 /* If this is a weak symbol, and there is a real definition, the
1042 processor independent code will have arranged for us to see the
1043 real definition first, and we can just use the same value. */
1044 if (h->weakdef != NULL)
1045 {
1046 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1047 || h->weakdef->root.type == bfd_link_hash_defweak);
1048 h->root.u.def.section = h->weakdef->root.u.def.section;
1049 h->root.u.def.value = h->weakdef->root.u.def.value;
1050 return true;
1051 }
1052
1053 /* This is a reference to a symbol defined by a dynamic object which
1054 is not a function. */
1055
1056 /* If we are creating a shared library, we must presume that the
1057 only references to the symbol are via the global offset table.
1058 For such cases we need not do anything here; the relocations will
1059 be handled correctly by relocate_section. */
1060 if (info->shared)
1061 return true;
1062
1063 /* We must allocate the symbol in our .dynbss section, which will
1064 become part of the .bss section of the executable. There will be
1065 an entry for this symbol in the .dynsym section. The dynamic
1066 object will contain position independent code, so all references
1067 from the dynamic object to this symbol will go through the global
1068 offset table. The dynamic linker will use the .dynsym entry to
1069 determine the address it must put in the global offset table, so
1070 both the dynamic object and the regular object will refer to the
1071 same memory location for the variable. */
1072
1073 s = bfd_get_section_by_name (dynobj, ".dynbss");
1074 BFD_ASSERT (s != NULL);
1075
1076 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1077 copy the initial value out of the dynamic object and into the
1078 runtime process image. We need to remember the offset into the
1079 .rela.bss section we are going to use. */
1080 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1081 {
1082 asection *srel;
1083
1084 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1085 BFD_ASSERT (srel != NULL);
1086 srel->_raw_size += sizeof (Elf32_External_Rela);
1087 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1088 }
1089
1090 /* We need to figure out the alignment required for this symbol. I
1091 have no idea how ELF linkers handle this. */
1092 power_of_two = bfd_log2 (h->size);
1093 if (power_of_two > 3)
1094 power_of_two = 3;
1095
1096 /* Apply the required alignment. */
1097 s->_raw_size = BFD_ALIGN (s->_raw_size,
1098 (bfd_size_type) (1 << power_of_two));
1099 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1100 {
1101 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1102 return false;
1103 }
1104
1105 /* Define the symbol as being at this point in the section. */
1106 h->root.u.def.section = s;
1107 h->root.u.def.value = s->_raw_size;
1108
1109 /* Increment the section size to make room for the symbol. */
1110 s->_raw_size += h->size;
1111
1112 return true;
1113}
1114
1115/* Set the sizes of the dynamic sections. */
1116
1117static boolean
1118elf_m68k_size_dynamic_sections (output_bfd, info)
aa91b392 1119 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
1120 struct bfd_link_info *info;
1121{
1122 bfd *dynobj;
1123 asection *s;
1124 boolean plt;
1125 boolean relocs;
252b5132
RH
1126
1127 dynobj = elf_hash_table (info)->dynobj;
1128 BFD_ASSERT (dynobj != NULL);
1129
1130 if (elf_hash_table (info)->dynamic_sections_created)
1131 {
1132 /* Set the contents of the .interp section to the interpreter. */
1133 if (!info->shared)
1134 {
1135 s = bfd_get_section_by_name (dynobj, ".interp");
1136 BFD_ASSERT (s != NULL);
1137 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1138 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1139 }
1140 }
1141 else
1142 {
1143 /* We may have created entries in the .rela.got section.
1144 However, if we are not creating the dynamic sections, we will
1145 not actually use these entries. Reset the size of .rela.got,
1146 which will cause it to get stripped from the output file
1147 below. */
1148 s = bfd_get_section_by_name (dynobj, ".rela.got");
1149 if (s != NULL)
1150 s->_raw_size = 0;
1151 }
1152
1153 /* If this is a -Bsymbolic shared link, then we need to discard all PC
1154 relative relocs against symbols defined in a regular object. We
1155 allocated space for them in the check_relocs routine, but we will not
1156 fill them in in the relocate_section routine. */
1157 if (info->shared && info->symbolic)
1158 elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),
1159 elf_m68k_discard_copies,
1160 (PTR) NULL);
1161
1162 /* The check_relocs and adjust_dynamic_symbol entry points have
1163 determined the sizes of the various dynamic sections. Allocate
1164 memory for them. */
1165 plt = false;
1166 relocs = false;
252b5132
RH
1167 for (s = dynobj->sections; s != NULL; s = s->next)
1168 {
1169 const char *name;
1170 boolean strip;
1171
1172 if ((s->flags & SEC_LINKER_CREATED) == 0)
1173 continue;
1174
1175 /* It's OK to base decisions on the section name, because none
1176 of the dynobj section names depend upon the input files. */
1177 name = bfd_get_section_name (dynobj, s);
1178
1179 strip = false;
1180
1181 if (strcmp (name, ".plt") == 0)
1182 {
1183 if (s->_raw_size == 0)
1184 {
1185 /* Strip this section if we don't need it; see the
1186 comment below. */
1187 strip = true;
1188 }
1189 else
1190 {
1191 /* Remember whether there is a PLT. */
1192 plt = true;
1193 }
1194 }
1195 else if (strncmp (name, ".rela", 5) == 0)
1196 {
1197 if (s->_raw_size == 0)
1198 {
1199 /* If we don't need this section, strip it from the
1200 output file. This is mostly to handle .rela.bss and
1201 .rela.plt. We must create both sections in
1202 create_dynamic_sections, because they must be created
1203 before the linker maps input sections to output
1204 sections. The linker does that before
1205 adjust_dynamic_symbol is called, and it is that
1206 function which decides whether anything needs to go
1207 into these sections. */
1208 strip = true;
1209 }
1210 else
1211 {
aa91b392 1212 relocs = true;
252b5132
RH
1213
1214 /* We use the reloc_count field as a counter if we need
1215 to copy relocs into the output file. */
1216 s->reloc_count = 0;
1217 }
1218 }
1219 else if (strncmp (name, ".got", 4) != 0)
1220 {
1221 /* It's not one of our sections, so don't allocate space. */
1222 continue;
1223 }
1224
1225 if (strip)
1226 {
7f8d5fc9 1227 _bfd_strip_section_from_output (info, s);
252b5132
RH
1228 continue;
1229 }
1230
1231 /* Allocate memory for the section contents. */
7a9af8c4
NC
1232 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1233 Unused entries should be reclaimed before the section's contents
1234 are written out, but at the moment this does not happen. Thus in
1235 order to prevent writing out garbage, we initialise the section's
1236 contents to zero. */
1237 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
252b5132
RH
1238 if (s->contents == NULL && s->_raw_size != 0)
1239 return false;
1240 }
1241
1242 if (elf_hash_table (info)->dynamic_sections_created)
1243 {
1244 /* Add some entries to the .dynamic section. We fill in the
1245 values later, in elf_m68k_finish_dynamic_sections, but we
1246 must add the entries now so that we get the correct size for
1247 the .dynamic section. The DT_DEBUG entry is filled in by the
1248 dynamic linker and used by the debugger. */
dc810e39
AM
1249#define add_dynamic_entry(TAG, VAL) \
1250 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1251
252b5132
RH
1252 if (!info->shared)
1253 {
dc810e39 1254 if (!add_dynamic_entry (DT_DEBUG, 0))
252b5132
RH
1255 return false;
1256 }
1257
1258 if (plt)
1259 {
dc810e39
AM
1260 if (!add_dynamic_entry (DT_PLTGOT, 0)
1261 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1262 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1263 || !add_dynamic_entry (DT_JMPREL, 0))
252b5132
RH
1264 return false;
1265 }
1266
1267 if (relocs)
1268 {
dc810e39
AM
1269 if (!add_dynamic_entry (DT_RELA, 0)
1270 || !add_dynamic_entry (DT_RELASZ, 0)
1271 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
252b5132
RH
1272 return false;
1273 }
1274
aa91b392 1275 if ((info->flags & DF_TEXTREL) != 0)
252b5132 1276 {
dc810e39 1277 if (!add_dynamic_entry (DT_TEXTREL, 0))
252b5132
RH
1278 return false;
1279 }
1280 }
dc810e39 1281#undef add_dynamic_entry
252b5132 1282
252b5132
RH
1283 return true;
1284}
1285
1286/* This function is called via elf_m68k_link_hash_traverse if we are
1287 creating a shared object with -Bsymbolic. It discards the space
1288 allocated to copy PC relative relocs against symbols which are defined
1289 in regular objects. We allocated space for them in the check_relocs
1290 routine, but we won't fill them in in the relocate_section routine. */
1291
252b5132
RH
1292static boolean
1293elf_m68k_discard_copies (h, ignore)
1294 struct elf_m68k_link_hash_entry *h;
121089cb 1295 PTR ignore ATTRIBUTE_UNUSED;
252b5132
RH
1296{
1297 struct elf_m68k_pcrel_relocs_copied *s;
1298
1299 /* We only discard relocs for symbols defined in a regular object. */
1300 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1301 return true;
1302
1303 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1304 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
1305
1306 return true;
1307}
1308
1309/* Relocate an M68K ELF section. */
1310
1311static boolean
1312elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1313 contents, relocs, local_syms, local_sections)
1314 bfd *output_bfd;
1315 struct bfd_link_info *info;
1316 bfd *input_bfd;
1317 asection *input_section;
1318 bfd_byte *contents;
1319 Elf_Internal_Rela *relocs;
1320 Elf_Internal_Sym *local_syms;
1321 asection **local_sections;
1322{
1323 bfd *dynobj;
1324 Elf_Internal_Shdr *symtab_hdr;
1325 struct elf_link_hash_entry **sym_hashes;
1326 bfd_vma *local_got_offsets;
1327 asection *sgot;
1328 asection *splt;
1329 asection *sreloc;
1330 Elf_Internal_Rela *rel;
1331 Elf_Internal_Rela *relend;
1332
1333 dynobj = elf_hash_table (info)->dynobj;
1334 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1335 sym_hashes = elf_sym_hashes (input_bfd);
1336 local_got_offsets = elf_local_got_offsets (input_bfd);
1337
1338 sgot = NULL;
1339 splt = NULL;
1340 sreloc = NULL;
1341
1342 rel = relocs;
1343 relend = relocs + input_section->reloc_count;
1344 for (; rel < relend; rel++)
1345 {
1346 int r_type;
1347 reloc_howto_type *howto;
1348 unsigned long r_symndx;
1349 struct elf_link_hash_entry *h;
1350 Elf_Internal_Sym *sym;
1351 asection *sec;
1352 bfd_vma relocation;
1353 bfd_reloc_status_type r;
1354
1355 r_type = ELF32_R_TYPE (rel->r_info);
1356 if (r_type < 0 || r_type >= (int) R_68K_max)
1357 {
1358 bfd_set_error (bfd_error_bad_value);
1359 return false;
1360 }
1361 howto = howto_table + r_type;
1362
1363 r_symndx = ELF32_R_SYM (rel->r_info);
1364
1365 if (info->relocateable)
1366 {
1367 /* This is a relocateable link. We don't have to change
1368 anything, unless the reloc is against a section symbol,
1369 in which case we have to adjust according to where the
1370 section symbol winds up in the output section. */
1371 if (r_symndx < symtab_hdr->sh_info)
1372 {
1373 sym = local_syms + r_symndx;
1374 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1375 {
1376 sec = local_sections[r_symndx];
1377 rel->r_addend += sec->output_offset + sym->st_value;
1378 }
1379 }
1380
1381 continue;
1382 }
1383
1384 /* This is a final link. */
1385 h = NULL;
1386 sym = NULL;
1387 sec = NULL;
1388 if (r_symndx < symtab_hdr->sh_info)
1389 {
1390 sym = local_syms + r_symndx;
1391 sec = local_sections[r_symndx];
1392 relocation = (sec->output_section->vma
1393 + sec->output_offset
1394 + sym->st_value);
1395 }
1396 else
1397 {
1398 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1399 while (h->root.type == bfd_link_hash_indirect
1400 || h->root.type == bfd_link_hash_warning)
1401 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1402 if (h->root.type == bfd_link_hash_defined
1403 || h->root.type == bfd_link_hash_defweak)
1404 {
1405 sec = h->root.u.def.section;
1406 if (((r_type == R_68K_PLT8
1407 || r_type == R_68K_PLT16
1408 || r_type == R_68K_PLT32
1409 || r_type == R_68K_PLT8O
1410 || r_type == R_68K_PLT16O
1411 || r_type == R_68K_PLT32O)
1412 && h->plt.offset != (bfd_vma) -1
1413 && elf_hash_table (info)->dynamic_sections_created)
1414 || ((r_type == R_68K_GOT8O
1415 || r_type == R_68K_GOT16O
1416 || r_type == R_68K_GOT32O
1417 || ((r_type == R_68K_GOT8
1418 || r_type == R_68K_GOT16
1419 || r_type == R_68K_GOT32)
1420 && strcmp (h->root.root.string,
1421 "_GLOBAL_OFFSET_TABLE_") != 0))
1422 && elf_hash_table (info)->dynamic_sections_created
1423 && (! info->shared
1424 || (! info->symbolic && h->dynindx != -1)
1425 || (h->elf_link_hash_flags
1426 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1427 || (info->shared
1428 && ((! info->symbolic && h->dynindx != -1)
1429 || (h->elf_link_hash_flags
1430 & ELF_LINK_HASH_DEF_REGULAR) == 0)
95fc45e6
AS
1431 && ((input_section->flags & SEC_ALLOC) != 0
1432 /* DWARF will emit R_68K_32 relocations in its
1433 sections against symbols defined externally
1434 in shared libraries. We can't do anything
1435 with them here. */
1436 || ((input_section->flags & SEC_DEBUGGING) != 0
1437 && (h->elf_link_hash_flags
1438 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
252b5132
RH
1439 && (r_type == R_68K_8
1440 || r_type == R_68K_16
1441 || r_type == R_68K_32
1442 || r_type == R_68K_PC8
1443 || r_type == R_68K_PC16
1444 || r_type == R_68K_PC32)))
1445 {
1446 /* In these cases, we don't need the relocation
1447 value. We check specially because in some
1448 obscure cases sec->output_section will be NULL. */
1449 relocation = 0;
1450 }
1451 else
1452 relocation = (h->root.u.def.value
1453 + sec->output_section->vma
1454 + sec->output_offset);
1455 }
1456 else if (h->root.type == bfd_link_hash_undefweak)
1457 relocation = 0;
671bae9c
NC
1458 else if (info->shared
1459 && (!info->symbolic || info->allow_shlib_undefined)
3a27a730
L
1460 && !info->no_undefined
1461 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
252b5132
RH
1462 relocation = 0;
1463 else
1464 {
1465 if (!(info->callbacks->undefined_symbol
1466 (info, h->root.root.string, input_bfd,
5cc7c785 1467 input_section, rel->r_offset,
3a27a730
L
1468 (!info->shared || info->no_undefined
1469 || ELF_ST_VISIBILITY (h->other)))))
252b5132
RH
1470 return false;
1471 relocation = 0;
1472 }
1473 }
1474
1475 switch (r_type)
1476 {
1477 case R_68K_GOT8:
1478 case R_68K_GOT16:
1479 case R_68K_GOT32:
1480 /* Relocation is to the address of the entry for this symbol
1481 in the global offset table. */
1482 if (h != NULL
1483 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1484 break;
1485 /* Fall through. */
1486 case R_68K_GOT8O:
1487 case R_68K_GOT16O:
1488 case R_68K_GOT32O:
1489 /* Relocation is the offset of the entry for this symbol in
1490 the global offset table. */
1491
1492 {
1493 bfd_vma off;
1494
1495 if (sgot == NULL)
1496 {
1497 sgot = bfd_get_section_by_name (dynobj, ".got");
1498 BFD_ASSERT (sgot != NULL);
1499 }
1500
1501 if (h != NULL)
1502 {
1503 off = h->got.offset;
1504 BFD_ASSERT (off != (bfd_vma) -1);
1505
1506 if (!elf_hash_table (info)->dynamic_sections_created
1507 || (info->shared
1508 && (info->symbolic || h->dynindx == -1)
1509 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1510 {
1511 /* This is actually a static link, or it is a
1512 -Bsymbolic link and the symbol is defined
1513 locally, or the symbol was forced to be local
1514 because of a version file.. We must initialize
1515 this entry in the global offset table. Since
1516 the offset must always be a multiple of 4, we
1517 use the least significant bit to record whether
1518 we have initialized it already.
1519
1520 When doing a dynamic link, we create a .rela.got
1521 relocation entry to initialize the value. This
1522 is done in the finish_dynamic_symbol routine. */
1523 if ((off & 1) != 0)
1524 off &= ~1;
1525 else
1526 {
1527 bfd_put_32 (output_bfd, relocation,
1528 sgot->contents + off);
1529 h->got.offset |= 1;
1530 }
1531 }
1532 }
1533 else
1534 {
1535 BFD_ASSERT (local_got_offsets != NULL
1536 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1537
1538 off = local_got_offsets[r_symndx];
1539
1540 /* The offset must always be a multiple of 4. We use
1541 the least significant bit to record whether we have
1542 already generated the necessary reloc. */
1543 if ((off & 1) != 0)
1544 off &= ~1;
1545 else
1546 {
1547 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1548
1549 if (info->shared)
1550 {
1551 asection *srelgot;
1552 Elf_Internal_Rela outrel;
1553
1554 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1555 BFD_ASSERT (srelgot != NULL);
1556
1557 outrel.r_offset = (sgot->output_section->vma
1558 + sgot->output_offset
1559 + off);
1560 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1561 outrel.r_addend = relocation;
1562 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1563 (((Elf32_External_Rela *)
1564 srelgot->contents)
1565 + srelgot->reloc_count));
1566 ++srelgot->reloc_count;
1567 }
1568
1569 local_got_offsets[r_symndx] |= 1;
1570 }
1571 }
1572
1573 relocation = sgot->output_offset + off;
1574 if (r_type == R_68K_GOT8O
1575 || r_type == R_68K_GOT16O
1576 || r_type == R_68K_GOT32O)
1577 {
1578 /* This relocation does not use the addend. */
1579 rel->r_addend = 0;
1580 }
1581 else
1582 relocation += sgot->output_section->vma;
1583 }
1584 break;
1585
1586 case R_68K_PLT8:
1587 case R_68K_PLT16:
1588 case R_68K_PLT32:
1589 /* Relocation is to the entry for this symbol in the
1590 procedure linkage table. */
1591
1592 /* Resolve a PLTxx reloc against a local symbol directly,
1593 without using the procedure linkage table. */
1594 if (h == NULL)
1595 break;
1596
1597 if (h->plt.offset == (bfd_vma) -1
1598 || !elf_hash_table (info)->dynamic_sections_created)
1599 {
1600 /* We didn't make a PLT entry for this symbol. This
1601 happens when statically linking PIC code, or when
1602 using -Bsymbolic. */
1603 break;
1604 }
1605
1606 if (splt == NULL)
1607 {
1608 splt = bfd_get_section_by_name (dynobj, ".plt");
1609 BFD_ASSERT (splt != NULL);
1610 }
1611
1612 relocation = (splt->output_section->vma
1613 + splt->output_offset
1614 + h->plt.offset);
1615 break;
1616
1617 case R_68K_PLT8O:
1618 case R_68K_PLT16O:
1619 case R_68K_PLT32O:
1620 /* Relocation is the offset of the entry for this symbol in
1621 the procedure linkage table. */
1622 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1623
1624 if (splt == NULL)
1625 {
1626 splt = bfd_get_section_by_name (dynobj, ".plt");
1627 BFD_ASSERT (splt != NULL);
1628 }
1629
1630 relocation = h->plt.offset;
1631
1632 /* This relocation does not use the addend. */
1633 rel->r_addend = 0;
1634
1635 break;
1636
1637 case R_68K_PC8:
1638 case R_68K_PC16:
1639 case R_68K_PC32:
1640 if (h == NULL)
1641 break;
1642 /* Fall through. */
1643 case R_68K_8:
1644 case R_68K_16:
1645 case R_68K_32:
1646 if (info->shared
ec338859 1647 && r_symndx != 0
252b5132
RH
1648 && (input_section->flags & SEC_ALLOC) != 0
1649 && ((r_type != R_68K_PC8
1650 && r_type != R_68K_PC16
1651 && r_type != R_68K_PC32)
1652 || (!info->symbolic
1653 || (h->elf_link_hash_flags
1654 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1655 {
1656 Elf_Internal_Rela outrel;
1657 boolean skip, relocate;
1658
1659 /* When generating a shared object, these relocations
1660 are copied into the output file to be resolved at run
1661 time. */
1662
1663 if (sreloc == NULL)
1664 {
1665 const char *name;
1666
1667 name = (bfd_elf_string_from_elf_section
1668 (input_bfd,
1669 elf_elfheader (input_bfd)->e_shstrndx,
1670 elf_section_data (input_section)->rel_hdr.sh_name));
1671 if (name == NULL)
1672 return false;
1673
1674 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1675 && strcmp (bfd_get_section_name (input_bfd,
1676 input_section),
1677 name + 5) == 0);
1678
1679 sreloc = bfd_get_section_by_name (dynobj, name);
1680 BFD_ASSERT (sreloc != NULL);
1681 }
1682
1683 skip = false;
1684
1685 if (elf_section_data (input_section)->stab_info == NULL)
1686 outrel.r_offset = rel->r_offset;
1687 else
1688 {
1689 bfd_vma off;
1690
1691 off = (_bfd_stab_section_offset
1692 (output_bfd, &elf_hash_table (info)->stab_info,
1693 input_section,
1694 &elf_section_data (input_section)->stab_info,
1695 rel->r_offset));
1696 if (off == (bfd_vma) -1)
1697 skip = true;
1698 outrel.r_offset = off;
1699 }
1700
1701 outrel.r_offset += (input_section->output_section->vma
1702 + input_section->output_offset);
1703
1704 if (skip)
1705 {
1706 memset (&outrel, 0, sizeof outrel);
1707 relocate = false;
1708 }
1709 /* h->dynindx may be -1 if the symbol was marked to
1710 become local. */
1711 else if (h != NULL
1712 && ((! info->symbolic && h->dynindx != -1)
1713 || (h->elf_link_hash_flags
1714 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1715 {
1716 BFD_ASSERT (h->dynindx != -1);
1717 relocate = false;
1718 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1719 outrel.r_addend = relocation + rel->r_addend;
1720 }
1721 else
1722 {
1723 if (r_type == R_68K_32)
1724 {
1725 relocate = true;
1726 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1727 outrel.r_addend = relocation + rel->r_addend;
1728 }
1729 else
1730 {
1731 long indx;
1732
1733 if (h == NULL)
1734 sec = local_sections[r_symndx];
1735 else
1736 {
1737 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1738 || (h->root.type
1739 == bfd_link_hash_defweak));
1740 sec = h->root.u.def.section;
1741 }
1742 if (sec != NULL && bfd_is_abs_section (sec))
1743 indx = 0;
1744 else if (sec == NULL || sec->owner == NULL)
1745 {
1746 bfd_set_error (bfd_error_bad_value);
1747 return false;
1748 }
1749 else
1750 {
1751 asection *osec;
1752
1753 osec = sec->output_section;
1754 indx = elf_section_data (osec)->dynindx;
1755 BFD_ASSERT (indx > 0);
1756 }
1757
1758 relocate = false;
1759 outrel.r_info = ELF32_R_INFO (indx, r_type);
1760 outrel.r_addend = relocation + rel->r_addend;
1761 }
1762 }
1763
1764 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1765 (((Elf32_External_Rela *)
1766 sreloc->contents)
1767 + sreloc->reloc_count));
1768 ++sreloc->reloc_count;
1769
1770 /* This reloc will be computed at runtime, so there's no
1771 need to do anything now, except for R_68K_32
1772 relocations that have been turned into
1773 R_68K_RELATIVE. */
1774 if (!relocate)
1775 continue;
1776 }
1777
1778 break;
1779
1780 case R_68K_GNU_VTINHERIT:
1781 case R_68K_GNU_VTENTRY:
1782 /* These are no-ops in the end. */
1783 continue;
1784
1785 default:
1786 break;
1787 }
1788
1789 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1790 contents, rel->r_offset,
1791 relocation, rel->r_addend);
1792
1793 if (r != bfd_reloc_ok)
1794 {
1795 switch (r)
1796 {
1797 default:
1798 case bfd_reloc_outofrange:
1799 abort ();
1800 case bfd_reloc_overflow:
1801 {
1802 const char *name;
1803
1804 if (h != NULL)
1805 name = h->root.root.string;
1806 else
1807 {
1808 name = bfd_elf_string_from_elf_section (input_bfd,
1809 symtab_hdr->sh_link,
1810 sym->st_name);
1811 if (name == NULL)
1812 return false;
1813 if (*name == '\0')
1814 name = bfd_section_name (input_bfd, sec);
1815 }
1816 if (!(info->callbacks->reloc_overflow
1817 (info, name, howto->name, (bfd_vma) 0,
1818 input_bfd, input_section, rel->r_offset)))
1819 return false;
1820 }
1821 break;
1822 }
1823 }
1824 }
1825
1826 return true;
1827}
1828
1829/* Finish up dynamic symbol handling. We set the contents of various
1830 dynamic sections here. */
1831
1832static boolean
1833elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1834 bfd *output_bfd;
1835 struct bfd_link_info *info;
1836 struct elf_link_hash_entry *h;
1837 Elf_Internal_Sym *sym;
1838{
1839 bfd *dynobj;
9e1281c7 1840 int plt_off1, plt_off2, plt_off3;
252b5132
RH
1841
1842 dynobj = elf_hash_table (info)->dynobj;
1843
1844 if (h->plt.offset != (bfd_vma) -1)
1845 {
1846 asection *splt;
1847 asection *sgot;
1848 asection *srela;
1849 bfd_vma plt_index;
1850 bfd_vma got_offset;
1851 Elf_Internal_Rela rela;
1852
1853 /* This symbol has an entry in the procedure linkage table. Set
1854 it up. */
1855
1856 BFD_ASSERT (h->dynindx != -1);
1857
1858 splt = bfd_get_section_by_name (dynobj, ".plt");
1859 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1860 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1861 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1862
1863 /* Get the index in the procedure linkage table which
1864 corresponds to this symbol. This is the index of this symbol
1865 in all the symbols for which we are making plt entries. The
1866 first entry in the procedure linkage table is reserved. */
9e1281c7
CM
1867 if ( CPU32_FLAG (output_bfd))
1868 plt_index = h->plt.offset / PLT_CPU32_ENTRY_SIZE - 1;
1869 else
1870 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
252b5132
RH
1871
1872 /* Get the offset into the .got table of the entry that
1873 corresponds to this function. Each .got entry is 4 bytes.
1874 The first three are reserved. */
1875 got_offset = (plt_index + 3) * 4;
1876
9e1281c7
CM
1877 if ( CPU32_FLAG (output_bfd))
1878 {
1879 /* Fill in the entry in the procedure linkage table. */
1880 memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
1881 PLT_CPU32_ENTRY_SIZE);
1882 plt_off1 = 4;
1883 plt_off2 = 12;
1884 plt_off3 = 18;
1885 }
1886 else
1887 {
1888 /* Fill in the entry in the procedure linkage table. */
1889 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1890 PLT_ENTRY_SIZE);
1891 plt_off1 = 4;
1892 plt_off2 = 10;
1893 plt_off3 = 16;
1894 }
1895
252b5132
RH
1896 /* The offset is relative to the first extension word. */
1897 bfd_put_32 (output_bfd,
1898 (sgot->output_section->vma
1899 + sgot->output_offset
1900 + got_offset
1901 - (splt->output_section->vma
1902 + h->plt.offset + 2)),
9e1281c7 1903 splt->contents + h->plt.offset + plt_off1);
252b5132
RH
1904
1905 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
9e1281c7
CM
1906 splt->contents + h->plt.offset + plt_off2);
1907 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
1908 splt->contents + h->plt.offset + plt_off3);
252b5132
RH
1909
1910 /* Fill in the entry in the global offset table. */
1911 bfd_put_32 (output_bfd,
1912 (splt->output_section->vma
1913 + splt->output_offset
1914 + h->plt.offset
1915 + 8),
1916 sgot->contents + got_offset);
1917
1918 /* Fill in the entry in the .rela.plt section. */
1919 rela.r_offset = (sgot->output_section->vma
1920 + sgot->output_offset
1921 + got_offset);
1922 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1923 rela.r_addend = 0;
1924 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1925 ((Elf32_External_Rela *) srela->contents
1926 + plt_index));
1927
1928 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1929 {
1930 /* Mark the symbol as undefined, rather than as defined in
1931 the .plt section. Leave the value alone. */
1932 sym->st_shndx = SHN_UNDEF;
1933 }
1934 }
1935
1936 if (h->got.offset != (bfd_vma) -1)
1937 {
1938 asection *sgot;
1939 asection *srela;
1940 Elf_Internal_Rela rela;
1941
1942 /* This symbol has an entry in the global offset table. Set it
1943 up. */
1944
1945 sgot = bfd_get_section_by_name (dynobj, ".got");
1946 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1947 BFD_ASSERT (sgot != NULL && srela != NULL);
1948
1949 rela.r_offset = (sgot->output_section->vma
1950 + sgot->output_offset
dc810e39 1951 + (h->got.offset &~ (bfd_vma) 1));
252b5132
RH
1952
1953 /* If this is a -Bsymbolic link, and the symbol is defined
1954 locally, we just want to emit a RELATIVE reloc. Likewise if
1955 the symbol was forced to be local because of a version file.
1956 The entry in the global offset table will already have been
1957 initialized in the relocate_section function. */
1958 if (info->shared
1959 && (info->symbolic || h->dynindx == -1)
1960 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1961 {
1962 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1963 rela.r_addend = bfd_get_signed_32 (output_bfd,
1964 (sgot->contents
dc810e39 1965 + (h->got.offset &~ (bfd_vma) 1)));
252b5132
RH
1966 }
1967 else
1968 {
1969 bfd_put_32 (output_bfd, (bfd_vma) 0,
dc810e39 1970 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
252b5132
RH
1971 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1972 rela.r_addend = 0;
1973 }
1974
1975 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1976 ((Elf32_External_Rela *) srela->contents
1977 + srela->reloc_count));
1978 ++srela->reloc_count;
1979 }
1980
1981 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1982 {
1983 asection *s;
1984 Elf_Internal_Rela rela;
1985
1986 /* This symbol needs a copy reloc. Set it up. */
1987
1988 BFD_ASSERT (h->dynindx != -1
1989 && (h->root.type == bfd_link_hash_defined
1990 || h->root.type == bfd_link_hash_defweak));
1991
1992 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1993 ".rela.bss");
1994 BFD_ASSERT (s != NULL);
1995
1996 rela.r_offset = (h->root.u.def.value
1997 + h->root.u.def.section->output_section->vma
1998 + h->root.u.def.section->output_offset);
1999 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2000 rela.r_addend = 0;
2001 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2002 ((Elf32_External_Rela *) s->contents
2003 + s->reloc_count));
2004 ++s->reloc_count;
2005 }
2006
2007 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2008 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2009 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2010 sym->st_shndx = SHN_ABS;
2011
2012 return true;
2013}
2014
2015/* Finish up the dynamic sections. */
2016
2017static boolean
2018elf_m68k_finish_dynamic_sections (output_bfd, info)
2019 bfd *output_bfd;
2020 struct bfd_link_info *info;
2021{
2022 bfd *dynobj;
2023 asection *sgot;
2024 asection *sdyn;
2025
2026 dynobj = elf_hash_table (info)->dynobj;
2027
2028 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2029 BFD_ASSERT (sgot != NULL);
2030 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2031
2032 if (elf_hash_table (info)->dynamic_sections_created)
2033 {
2034 asection *splt;
2035 Elf32_External_Dyn *dyncon, *dynconend;
2036
2037 splt = bfd_get_section_by_name (dynobj, ".plt");
2038 BFD_ASSERT (splt != NULL && sdyn != NULL);
2039
2040 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2041 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2042 for (; dyncon < dynconend; dyncon++)
2043 {
2044 Elf_Internal_Dyn dyn;
2045 const char *name;
2046 asection *s;
2047
2048 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2049
2050 switch (dyn.d_tag)
2051 {
2052 default:
2053 break;
2054
2055 case DT_PLTGOT:
2056 name = ".got";
2057 goto get_vma;
2058 case DT_JMPREL:
2059 name = ".rela.plt";
2060 get_vma:
2061 s = bfd_get_section_by_name (output_bfd, name);
2062 BFD_ASSERT (s != NULL);
2063 dyn.d_un.d_ptr = s->vma;
2064 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2065 break;
2066
2067 case DT_PLTRELSZ:
2068 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2069 BFD_ASSERT (s != NULL);
2070 if (s->_cooked_size != 0)
2071 dyn.d_un.d_val = s->_cooked_size;
2072 else
2073 dyn.d_un.d_val = s->_raw_size;
2074 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2075 break;
2076
2077 case DT_RELASZ:
2078 /* The procedure linkage table relocs (DT_JMPREL) should
2079 not be included in the overall relocs (DT_RELA).
2080 Therefore, we override the DT_RELASZ entry here to
2081 make it not include the JMPREL relocs. Since the
2082 linker script arranges for .rela.plt to follow all
2083 other relocation sections, we don't have to worry
2084 about changing the DT_RELA entry. */
2085 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2086 if (s != NULL)
2087 {
2088 if (s->_cooked_size != 0)
2089 dyn.d_un.d_val -= s->_cooked_size;
2090 else
2091 dyn.d_un.d_val -= s->_raw_size;
2092 }
2093 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2094 break;
2095 }
2096 }
2097
2098 /* Fill in the first entry in the procedure linkage table. */
2099 if (splt->_raw_size > 0)
2100 {
9e1281c7
CM
2101 if (!CPU32_FLAG (output_bfd))
2102 {
2103 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2104 bfd_put_32 (output_bfd,
2105 (sgot->output_section->vma
2106 + sgot->output_offset + 4
2107 - (splt->output_section->vma + 2)),
2108 splt->contents + 4);
2109 bfd_put_32 (output_bfd,
2110 (sgot->output_section->vma
2111 + sgot->output_offset + 8
2112 - (splt->output_section->vma + 10)),
2113 splt->contents + 12);
c3668558 2114 elf_section_data (splt->output_section)->this_hdr.sh_entsize
9e1281c7
CM
2115 = PLT_ENTRY_SIZE;
2116 }
2117 else /* cpu32 */
2118 {
2119 memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2120 bfd_put_32 (output_bfd,
2121 (sgot->output_section->vma
2122 + sgot->output_offset + 4
2123 - (splt->output_section->vma + 2)),
2124 splt->contents + 4);
2125 bfd_put_32 (output_bfd,
2126 (sgot->output_section->vma
2127 + sgot->output_offset + 8
2128 - (splt->output_section->vma + 10)),
6091b433 2129 splt->contents + 12);
c3668558 2130 elf_section_data (splt->output_section)->this_hdr.sh_entsize
9e1281c7
CM
2131 = PLT_CPU32_ENTRY_SIZE;
2132 }
252b5132 2133 }
252b5132
RH
2134 }
2135
2136 /* Fill in the first three entries in the global offset table. */
2137 if (sgot->_raw_size > 0)
2138 {
2139 if (sdyn == NULL)
2140 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2141 else
2142 bfd_put_32 (output_bfd,
2143 sdyn->output_section->vma + sdyn->output_offset,
2144 sgot->contents);
2145 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2146 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2147 }
2148
2149 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2150
252b5132
RH
2151 return true;
2152}
2153
0752970e
NC
2154/* Given a .data section and a .emreloc in-memory section, store
2155 relocation information into the .emreloc section which can be
2156 used at runtime to relocate the section. This is called by the
2157 linker when the --embedded-relocs switch is used. This is called
2158 after the add_symbols entry point has been called for all the
2159 objects, and before the final_link entry point is called. */
2160
2161boolean
2162bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2163 bfd *abfd;
2164 struct bfd_link_info *info;
2165 asection *datasec;
2166 asection *relsec;
2167 char **errmsg;
2168{
2169 Elf_Internal_Shdr *symtab_hdr;
2170 Elf32_External_Sym *extsyms;
2171 Elf32_External_Sym *free_extsyms = NULL;
2172 Elf_Internal_Rela *internal_relocs;
2173 Elf_Internal_Rela *free_relocs = NULL;
2174 Elf_Internal_Rela *irel, *irelend;
2175 bfd_byte *p;
dc810e39 2176 bfd_size_type amt;
0752970e
NC
2177
2178 BFD_ASSERT (! info->relocateable);
2179
2180 *errmsg = NULL;
2181
2182 if (datasec->reloc_count == 0)
2183 return true;
2184
2185 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2186 /* Read this BFD's symbols if we haven't done so already, or get the cached
2187 copy if it exists. */
2188 if (symtab_hdr->contents != NULL)
2189 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2190 else
2191 {
2192 /* Go get them off disk. */
2193 if (info->keep_memory)
dc810e39 2194 extsyms = (Elf32_External_Sym *) bfd_alloc (abfd, symtab_hdr->sh_size);
0752970e 2195 else
dc810e39 2196 extsyms = (Elf32_External_Sym *) bfd_malloc (symtab_hdr->sh_size);
0752970e
NC
2197 if (extsyms == NULL)
2198 goto error_return;
2199 if (! info->keep_memory)
2200 free_extsyms = extsyms;
2201 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
dc810e39 2202 || (bfd_bread (extsyms, symtab_hdr->sh_size, abfd)
0752970e
NC
2203 != symtab_hdr->sh_size))
2204 goto error_return;
2205 if (info->keep_memory)
973ffd63 2206 symtab_hdr->contents = (unsigned char *) extsyms;
0752970e
NC
2207 }
2208
2209 /* Get a copy of the native relocations. */
2210 internal_relocs = (_bfd_elf32_link_read_relocs
2211 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2212 info->keep_memory));
2213 if (internal_relocs == NULL)
2214 goto error_return;
2215 if (! info->keep_memory)
2216 free_relocs = internal_relocs;
2217
dc810e39
AM
2218 amt = (bfd_size_type) datasec->reloc_count * 12;
2219 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
0752970e
NC
2220 if (relsec->contents == NULL)
2221 goto error_return;
2222
2223 p = relsec->contents;
2224
2225 irelend = internal_relocs + datasec->reloc_count;
2226 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2227 {
2228 asection *targetsec;
2229
2230 /* We are going to write a four byte longword into the runtime
2231 reloc section. The longword will be the address in the data
2232 section which must be relocated. It is followed by the name
2233 of the target section NUL-padded or truncated to 8
2234 characters. */
2235
2236 /* We can only relocate absolute longword relocs at run time. */
2237 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2238 {
2239 *errmsg = _("unsupported reloc type");
2240 bfd_set_error (bfd_error_bad_value);
2241 goto error_return;
2242 }
2243
2244 /* Get the target section referred to by the reloc. */
2245 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2246 {
2247 Elf_Internal_Sym isym;
2248
2249 /* A local symbol. */
2250 bfd_elf32_swap_symbol_in (abfd,
2251 extsyms + ELF32_R_SYM (irel->r_info),
2252 &isym);
2253
2254 targetsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2255 }
2256 else
2257 {
2258 unsigned long indx;
2259 struct elf_link_hash_entry *h;
2260
2261 /* An external symbol. */
2262 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2263 h = elf_sym_hashes (abfd)[indx];
2264 BFD_ASSERT (h != NULL);
2265 if (h->root.type == bfd_link_hash_defined
2266 || h->root.type == bfd_link_hash_defweak)
2267 targetsec = h->root.u.def.section;
2268 else
2269 targetsec = NULL;
2270 }
2271
2272 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2273 memset (p + 4, 0, 8);
2274 if (targetsec != NULL)
2275 strncpy (p + 4, targetsec->output_section->name, 8);
2276 }
c3668558 2277
0752970e
NC
2278 if (free_extsyms != NULL)
2279 free (free_extsyms);
2280 if (free_relocs != NULL)
2281 free (free_relocs);
2282 return true;
2283
2284error_return:
2285 if (free_extsyms != NULL)
2286 free (free_extsyms);
2287 if (free_relocs != NULL)
2288 free (free_relocs);
2289 return false;
2290}
2291
aa91b392 2292static enum elf_reloc_type_class
f51e552e
AM
2293elf32_m68k_reloc_type_class (rela)
2294 const Elf_Internal_Rela *rela;
aa91b392 2295{
f51e552e 2296 switch ((int) ELF32_R_TYPE (rela->r_info))
aa91b392
AS
2297 {
2298 case R_68K_RELATIVE:
2299 return reloc_class_relative;
2300 case R_68K_JMP_SLOT:
2301 return reloc_class_plt;
2302 case R_68K_COPY:
2303 return reloc_class_copy;
2304 default:
2305 return reloc_class_normal;
2306 }
2307}
2308
252b5132
RH
2309#define TARGET_BIG_SYM bfd_elf32_m68k_vec
2310#define TARGET_BIG_NAME "elf32-m68k"
2311#define ELF_MACHINE_CODE EM_68K
2312#define ELF_MAXPAGESIZE 0x2000
2313#define elf_backend_create_dynamic_sections \
2314 _bfd_elf_create_dynamic_sections
2315#define bfd_elf32_bfd_link_hash_table_create \
2316 elf_m68k_link_hash_table_create
2317#define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2318
2319#define elf_backend_check_relocs elf_m68k_check_relocs
2320#define elf_backend_adjust_dynamic_symbol \
2321 elf_m68k_adjust_dynamic_symbol
2322#define elf_backend_size_dynamic_sections \
2323 elf_m68k_size_dynamic_sections
2324#define elf_backend_relocate_section elf_m68k_relocate_section
2325#define elf_backend_finish_dynamic_symbol \
2326 elf_m68k_finish_dynamic_symbol
2327#define elf_backend_finish_dynamic_sections \
2328 elf_m68k_finish_dynamic_sections
2329#define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2330#define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
9e1281c7
CM
2331#define bfd_elf32_bfd_copy_private_bfd_data \
2332 elf32_m68k_copy_private_bfd_data
2333#define bfd_elf32_bfd_merge_private_bfd_data \
2334 elf32_m68k_merge_private_bfd_data
2335#define bfd_elf32_bfd_set_private_flags \
2336 elf32_m68k_set_private_flags
2337#define bfd_elf32_bfd_print_private_bfd_data \
2338 elf32_m68k_print_private_bfd_data
aa91b392 2339#define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
9e1281c7 2340
252b5132 2341#define elf_backend_can_gc_sections 1
51b64d56 2342#define elf_backend_can_refcount 1
252b5132
RH
2343#define elf_backend_want_got_plt 1
2344#define elf_backend_plt_readonly 1
2345#define elf_backend_want_plt_sym 0
2346#define elf_backend_got_header_size 12
252b5132
RH
2347
2348#include "elf32-target.h"