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