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