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