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