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