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