]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-m68k.c
Revert the value change of DT_ENCODING from 2003-01-17. Luckily (and
[thirdparty/binutils-gdb.git] / bfd / elf32-m68k.c
CommitLineData
252b5132 1/* Motorola 68k series support for 32-bit ELF
b2a8e766 2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
ab96bf03 3 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
252b5132 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
3e110533 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/m68k.h"
266abb8f 27#include "opcode/m68k.h"
252b5132
RH
28
29static reloc_howto_type *reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31static void rtype_to_howto
947216bf 32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
252b5132
RH
33static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
34 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
35static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
36 PARAMS ((bfd *));
b34976b6 37static bfd_boolean elf_m68k_check_relocs
252b5132
RH
38 PARAMS ((bfd *, struct bfd_link_info *, asection *,
39 const Elf_Internal_Rela *));
b34976b6 40static bfd_boolean elf_m68k_adjust_dynamic_symbol
252b5132 41 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
b34976b6 42static bfd_boolean elf_m68k_size_dynamic_sections
252b5132 43 PARAMS ((bfd *, struct bfd_link_info *));
0cca5f05
AS
44static bfd_boolean elf_m68k_discard_copies
45 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 46static bfd_boolean elf_m68k_relocate_section
252b5132
RH
47 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
b34976b6 49static bfd_boolean elf_m68k_finish_dynamic_symbol
252b5132
RH
50 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
51 Elf_Internal_Sym *));
b34976b6 52static bfd_boolean elf_m68k_finish_dynamic_sections
252b5132
RH
53 PARAMS ((bfd *, struct bfd_link_info *));
54
b34976b6 55static bfd_boolean elf32_m68k_set_private_flags
9e1281c7 56 PARAMS ((bfd *, flagword));
b34976b6 57static bfd_boolean elf32_m68k_merge_private_bfd_data
9e1281c7 58 PARAMS ((bfd *, bfd *));
b34976b6 59static bfd_boolean elf32_m68k_print_private_bfd_data
9e1281c7 60 PARAMS ((bfd *, PTR));
aa91b392 61static enum elf_reloc_type_class elf32_m68k_reloc_type_class
f51e552e 62 PARAMS ((const Elf_Internal_Rela *));
9e1281c7 63
252b5132 64static reloc_howto_type howto_table[] = {
b34976b6
AM
65 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
66 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
67 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
68 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
69 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
70 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
71 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
72 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
73 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
74 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
75 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
76 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
77 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
78 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
79 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
80 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
81 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
82 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
83 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
84 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
85 HOWTO(R_68K_GLOB_DAT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_GLOB_DAT", FALSE, 0, 0xffffffff,FALSE),
86 HOWTO(R_68K_JMP_SLOT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_JMP_SLOT", FALSE, 0, 0xffffffff,FALSE),
87 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
ae9a127f 88 /* GNU extension to record C++ vtable hierarchy. */
252b5132
RH
89 HOWTO (R_68K_GNU_VTINHERIT, /* type */
90 0, /* rightshift */
91 2, /* size (0 = byte, 1 = short, 2 = long) */
92 0, /* bitsize */
b34976b6 93 FALSE, /* pc_relative */
252b5132
RH
94 0, /* bitpos */
95 complain_overflow_dont, /* complain_on_overflow */
96 NULL, /* special_function */
97 "R_68K_GNU_VTINHERIT", /* name */
b34976b6 98 FALSE, /* partial_inplace */
252b5132
RH
99 0, /* src_mask */
100 0, /* dst_mask */
b34976b6 101 FALSE),
ae9a127f 102 /* GNU extension to record C++ vtable member usage. */
252b5132
RH
103 HOWTO (R_68K_GNU_VTENTRY, /* type */
104 0, /* rightshift */
105 2, /* size (0 = byte, 1 = short, 2 = long) */
106 0, /* bitsize */
b34976b6 107 FALSE, /* pc_relative */
252b5132
RH
108 0, /* bitpos */
109 complain_overflow_dont, /* complain_on_overflow */
110 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
111 "R_68K_GNU_VTENTRY", /* name */
b34976b6 112 FALSE, /* partial_inplace */
252b5132
RH
113 0, /* src_mask */
114 0, /* dst_mask */
b34976b6 115 FALSE),
252b5132
RH
116};
117
118static void
119rtype_to_howto (abfd, cache_ptr, dst)
121089cb 120 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
121 arelent *cache_ptr;
122 Elf_Internal_Rela *dst;
123{
124 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
125 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
126}
127
128#define elf_info_to_howto rtype_to_howto
129
130static const struct
131{
132 bfd_reloc_code_real_type bfd_val;
133 int elf_val;
134} reloc_map[] = {
135 { BFD_RELOC_NONE, R_68K_NONE },
136 { BFD_RELOC_32, R_68K_32 },
137 { BFD_RELOC_16, R_68K_16 },
138 { BFD_RELOC_8, R_68K_8 },
139 { BFD_RELOC_32_PCREL, R_68K_PC32 },
140 { BFD_RELOC_16_PCREL, R_68K_PC16 },
141 { BFD_RELOC_8_PCREL, R_68K_PC8 },
142 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
143 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
144 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
145 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
146 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
147 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
148 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
149 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
150 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
151 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
152 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
153 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
154 { BFD_RELOC_NONE, R_68K_COPY },
155 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
156 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
157 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
158 { BFD_RELOC_CTOR, R_68K_32 },
159 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
160 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
161};
162
163static reloc_howto_type *
164reloc_type_lookup (abfd, code)
121089cb 165 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
166 bfd_reloc_code_real_type code;
167{
168 unsigned int i;
169 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
170 {
171 if (reloc_map[i].bfd_val == code)
172 return &howto_table[reloc_map[i].elf_val];
173 }
174 return 0;
175}
176
157090f7
AM
177static reloc_howto_type *
178reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
179{
180 unsigned int i;
181
182 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
183 if (howto_table[i].name != NULL
184 && strcasecmp (howto_table[i].name, r_name) == 0)
185 return &howto_table[i];
186
187 return NULL;
188}
189
252b5132 190#define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
157090f7 191#define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
252b5132 192#define ELF_ARCH bfd_arch_m68k
252b5132
RH
193\f
194/* Functions for the m68k ELF linker. */
195
196/* The name of the dynamic interpreter. This is put in the .interp
197 section. */
198
199#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
200
cc3e26be
RS
201/* Describes one of the various PLT styles. */
202
203struct elf_m68k_plt_info
204{
205 /* The size of each PLT entry. */
206 bfd_vma size;
207
208 /* The template for the first PLT entry. */
209 const bfd_byte *plt0_entry;
210
211 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
212 The comments by each member indicate the value that the relocation
213 is against. */
214 struct {
215 unsigned int got4; /* .got + 4 */
216 unsigned int got8; /* .got + 8 */
217 } plt0_relocs;
218
219 /* The template for a symbol's PLT entry. */
220 const bfd_byte *symbol_entry;
221
222 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
223 The comments by each member indicate the value that the relocation
224 is against. */
225 struct {
226 unsigned int got; /* the symbol's .got.plt entry */
227 unsigned int plt; /* .plt */
228 } symbol_relocs;
229
230 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
231 The stub starts with "move.l #relocoffset,%d0". */
232 bfd_vma symbol_resolve_entry;
233};
234
252b5132
RH
235/* The size in bytes of an entry in the procedure linkage table. */
236
237#define PLT_ENTRY_SIZE 20
238
239/* The first entry in a procedure linkage table looks like this. See
240 the SVR4 ABI m68k supplement to see how this works. */
241
242static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
243{
244 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
cc3e26be 245 0, 0, 0, 2, /* + (.got + 4) - . */
252b5132 246 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
cc3e26be 247 0, 0, 0, 2, /* + (.got + 8) - . */
252b5132
RH
248 0, 0, 0, 0 /* pad out to 20 bytes. */
249};
250
251/* Subsequent entries in a procedure linkage table look like this. */
252
253static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
254{
255 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
cc3e26be 256 0, 0, 0, 2, /* + (.got.plt entry) - . */
252b5132 257 0x2f, 0x3c, /* move.l #offset,-(%sp) */
cc3e26be 258 0, 0, 0, 0, /* + reloc index */
252b5132 259 0x60, 0xff, /* bra.l .plt */
cc3e26be 260 0, 0, 0, 0 /* + .plt - . */
252b5132
RH
261};
262
cc3e26be
RS
263static const struct elf_m68k_plt_info elf_m68k_plt_info = {
264 PLT_ENTRY_SIZE,
265 elf_m68k_plt0_entry, { 4, 12 },
266 elf_m68k_plt_entry, { 4, 16 }, 8
267};
238d258f 268
cc3e26be 269#define ISAB_PLT_ENTRY_SIZE 24
238d258f 270
cc3e26be 271static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
238d258f 272{
cc3e26be
RS
273 0x20, 0x3c, /* move.l #offset,%d0 */
274 0, 0, 0, 0, /* + (.got + 4) - . */
275 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
276 0x20, 0x3c, /* move.l #offset,%d0 */
277 0, 0, 0, 0, /* + (.got + 8) - . */
278 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
238d258f
NC
279 0x4e, 0xd0, /* jmp (%a0) */
280 0x4e, 0x71 /* nop */
281};
282
283/* Subsequent entries in a procedure linkage table look like this. */
284
cc3e26be 285static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
238d258f 286{
cc3e26be
RS
287 0x20, 0x3c, /* move.l #offset,%d0 */
288 0, 0, 0, 0, /* + (.got.plt entry) - . */
289 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
238d258f
NC
290 0x4e, 0xd0, /* jmp (%a0) */
291 0x2f, 0x3c, /* move.l #offset,-(%sp) */
cc3e26be 292 0, 0, 0, 0, /* + reloc index */
238d258f 293 0x60, 0xff, /* bra.l .plt */
cc3e26be 294 0, 0, 0, 0 /* + .plt - . */
238d258f
NC
295};
296
cc3e26be
RS
297static const struct elf_m68k_plt_info elf_isab_plt_info = {
298 ISAB_PLT_ENTRY_SIZE,
299 elf_isab_plt0_entry, { 2, 12 },
300 elf_isab_plt_entry, { 2, 20 }, 12
301};
9e1281c7 302
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;
1218 unsigned int power_of_two;
1219
cc3e26be 1220 htab = elf_m68k_hash_table (info);
252b5132
RH
1221 dynobj = elf_hash_table (info)->dynobj;
1222
1223 /* Make sure we know what is going on here. */
1224 BFD_ASSERT (dynobj != NULL
f5385ebf 1225 && (h->needs_plt
f6e332e6 1226 || h->u.weakdef != NULL
f5385ebf
AM
1227 || (h->def_dynamic
1228 && h->ref_regular
1229 && !h->def_regular)));
252b5132
RH
1230
1231 /* If this is a function, put it in the procedure linkage table. We
1232 will fill in the contents of the procedure linkage table later,
1233 when we know the address of the .got section. */
1234 if (h->type == STT_FUNC
f5385ebf 1235 || h->needs_plt)
252b5132 1236 {
9dfe8738
AS
1237 if ((h->plt.refcount <= 0
1238 || SYMBOL_CALLS_LOCAL (info, h)
1239 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1240 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
1241 /* We must always create the plt entry if it was referenced
1242 by a PLTxxO relocation. In this case we already recorded
1243 it as a dynamic symbol. */
1244 && h->dynindx == -1)
1245 {
1246 /* This case can occur if we saw a PLTxx reloc in an input
1247 file, but the symbol was never referred to by a dynamic
9dfe8738
AS
1248 object, or if all references were garbage collected. In
1249 such a case, we don't actually need to build a procedure
1250 linkage table, and we can just do a PCxx reloc instead. */
252b5132 1251 h->plt.offset = (bfd_vma) -1;
f5385ebf 1252 h->needs_plt = 0;
b34976b6 1253 return TRUE;
252b5132
RH
1254 }
1255
1256 /* Make sure this symbol is output as a dynamic symbol. */
b6152c34 1257 if (h->dynindx == -1
f5385ebf 1258 && !h->forced_local)
252b5132 1259 {
c152c796 1260 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1261 return FALSE;
252b5132
RH
1262 }
1263
1264 s = bfd_get_section_by_name (dynobj, ".plt");
1265 BFD_ASSERT (s != NULL);
1266
1267 /* If this is the first .plt entry, make room for the special
1268 first entry. */
eea6121a 1269 if (s->size == 0)
cc3e26be 1270 s->size = htab->plt_info->size;
252b5132
RH
1271
1272 /* If this symbol is not defined in a regular file, and we are
1273 not generating a shared library, then set the symbol to this
1274 location in the .plt. This is required to make function
1275 pointers compare as equal between the normal executable and
1276 the shared library. */
1277 if (!info->shared
f5385ebf 1278 && !h->def_regular)
252b5132
RH
1279 {
1280 h->root.u.def.section = s;
eea6121a 1281 h->root.u.def.value = s->size;
252b5132
RH
1282 }
1283
eea6121a 1284 h->plt.offset = s->size;
252b5132
RH
1285
1286 /* Make room for this entry. */
cc3e26be 1287 s->size += htab->plt_info->size;
252b5132
RH
1288
1289 /* We also need to make an entry in the .got.plt section, which
1290 will be placed in the .got section by the linker script. */
252b5132
RH
1291 s = bfd_get_section_by_name (dynobj, ".got.plt");
1292 BFD_ASSERT (s != NULL);
eea6121a 1293 s->size += 4;
252b5132
RH
1294
1295 /* We also need to make an entry in the .rela.plt section. */
252b5132
RH
1296 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1297 BFD_ASSERT (s != NULL);
eea6121a 1298 s->size += sizeof (Elf32_External_Rela);
252b5132 1299
b34976b6 1300 return TRUE;
252b5132
RH
1301 }
1302
1303 /* Reinitialize the plt offset now that it is not used as a reference
1304 count any more. */
1305 h->plt.offset = (bfd_vma) -1;
1306
1307 /* If this is a weak symbol, and there is a real definition, the
1308 processor independent code will have arranged for us to see the
1309 real definition first, and we can just use the same value. */
f6e332e6 1310 if (h->u.weakdef != NULL)
252b5132 1311 {
f6e332e6
AM
1312 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1313 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1314 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1315 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 1316 return TRUE;
252b5132
RH
1317 }
1318
1319 /* This is a reference to a symbol defined by a dynamic object which
1320 is not a function. */
1321
1322 /* If we are creating a shared library, we must presume that the
1323 only references to the symbol are via the global offset table.
1324 For such cases we need not do anything here; the relocations will
1325 be handled correctly by relocate_section. */
1326 if (info->shared)
b34976b6 1327 return TRUE;
252b5132 1328
909272ee
AM
1329 if (h->size == 0)
1330 {
1331 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1332 h->root.root.string);
1333 return TRUE;
1334 }
1335
252b5132
RH
1336 /* We must allocate the symbol in our .dynbss section, which will
1337 become part of the .bss section of the executable. There will be
1338 an entry for this symbol in the .dynsym section. The dynamic
1339 object will contain position independent code, so all references
1340 from the dynamic object to this symbol will go through the global
1341 offset table. The dynamic linker will use the .dynsym entry to
1342 determine the address it must put in the global offset table, so
1343 both the dynamic object and the regular object will refer to the
1344 same memory location for the variable. */
1345
1346 s = bfd_get_section_by_name (dynobj, ".dynbss");
1347 BFD_ASSERT (s != NULL);
1348
1349 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1350 copy the initial value out of the dynamic object and into the
1351 runtime process image. We need to remember the offset into the
1352 .rela.bss section we are going to use. */
1353 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1354 {
1355 asection *srel;
1356
1357 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1358 BFD_ASSERT (srel != NULL);
eea6121a 1359 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 1360 h->needs_copy = 1;
252b5132
RH
1361 }
1362
1363 /* We need to figure out the alignment required for this symbol. I
1364 have no idea how ELF linkers handle this. */
1365 power_of_two = bfd_log2 (h->size);
1366 if (power_of_two > 3)
1367 power_of_two = 3;
1368
1369 /* Apply the required alignment. */
eea6121a 1370 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
252b5132
RH
1371 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1372 {
1373 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
b34976b6 1374 return FALSE;
252b5132
RH
1375 }
1376
1377 /* Define the symbol as being at this point in the section. */
1378 h->root.u.def.section = s;
eea6121a 1379 h->root.u.def.value = s->size;
252b5132
RH
1380
1381 /* Increment the section size to make room for the symbol. */
eea6121a 1382 s->size += h->size;
252b5132 1383
b34976b6 1384 return TRUE;
252b5132
RH
1385}
1386
1387/* Set the sizes of the dynamic sections. */
1388
b34976b6 1389static bfd_boolean
252b5132 1390elf_m68k_size_dynamic_sections (output_bfd, info)
aa91b392 1391 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
1392 struct bfd_link_info *info;
1393{
1394 bfd *dynobj;
1395 asection *s;
b34976b6
AM
1396 bfd_boolean plt;
1397 bfd_boolean relocs;
252b5132
RH
1398
1399 dynobj = elf_hash_table (info)->dynobj;
1400 BFD_ASSERT (dynobj != NULL);
1401
1402 if (elf_hash_table (info)->dynamic_sections_created)
1403 {
1404 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 1405 if (info->executable)
252b5132
RH
1406 {
1407 s = bfd_get_section_by_name (dynobj, ".interp");
1408 BFD_ASSERT (s != NULL);
eea6121a 1409 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
252b5132
RH
1410 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1411 }
1412 }
1413 else
1414 {
1415 /* We may have created entries in the .rela.got section.
1416 However, if we are not creating the dynamic sections, we will
1417 not actually use these entries. Reset the size of .rela.got,
1418 which will cause it to get stripped from the output file
1419 below. */
1420 s = bfd_get_section_by_name (dynobj, ".rela.got");
1421 if (s != NULL)
eea6121a 1422 s->size = 0;
252b5132
RH
1423 }
1424
b6152c34
AS
1425 /* If this is a -Bsymbolic shared link, then we need to discard all
1426 PC relative relocs against symbols defined in a regular object.
1427 For the normal shared case we discard the PC relative relocs
1428 against symbols that have become local due to visibility changes.
1429 We allocated space for them in the check_relocs routine, but we
1430 will not fill them in in the relocate_section routine. */
1431 if (info->shared)
0cca5f05
AS
1432 elf_link_hash_traverse (elf_hash_table (info),
1433 elf_m68k_discard_copies,
1434 (PTR) info);
252b5132
RH
1435
1436 /* The check_relocs and adjust_dynamic_symbol entry points have
1437 determined the sizes of the various dynamic sections. Allocate
1438 memory for them. */
b34976b6
AM
1439 plt = FALSE;
1440 relocs = FALSE;
252b5132
RH
1441 for (s = dynobj->sections; s != NULL; s = s->next)
1442 {
1443 const char *name;
252b5132
RH
1444
1445 if ((s->flags & SEC_LINKER_CREATED) == 0)
1446 continue;
1447
1448 /* It's OK to base decisions on the section name, because none
1449 of the dynobj section names depend upon the input files. */
1450 name = bfd_get_section_name (dynobj, s);
1451
252b5132
RH
1452 if (strcmp (name, ".plt") == 0)
1453 {
c456f082
AM
1454 /* Remember whether there is a PLT. */
1455 plt = s->size != 0;
252b5132 1456 }
0112cd26 1457 else if (CONST_STRNEQ (name, ".rela"))
252b5132 1458 {
c456f082 1459 if (s->size != 0)
252b5132 1460 {
b34976b6 1461 relocs = TRUE;
252b5132
RH
1462
1463 /* We use the reloc_count field as a counter if we need
1464 to copy relocs into the output file. */
1465 s->reloc_count = 0;
1466 }
1467 }
0112cd26 1468 else if (! CONST_STRNEQ (name, ".got")
c456f082 1469 && strcmp (name, ".dynbss") != 0)
252b5132
RH
1470 {
1471 /* It's not one of our sections, so don't allocate space. */
1472 continue;
1473 }
1474
c456f082 1475 if (s->size == 0)
252b5132 1476 {
c456f082
AM
1477 /* If we don't need this section, strip it from the
1478 output file. This is mostly to handle .rela.bss and
1479 .rela.plt. We must create both sections in
1480 create_dynamic_sections, because they must be created
1481 before the linker maps input sections to output
1482 sections. The linker does that before
1483 adjust_dynamic_symbol is called, and it is that
1484 function which decides whether anything needs to go
1485 into these sections. */
8423293d 1486 s->flags |= SEC_EXCLUDE;
252b5132
RH
1487 continue;
1488 }
1489
c456f082
AM
1490 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1491 continue;
1492
252b5132 1493 /* Allocate memory for the section contents. */
7a9af8c4
NC
1494 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1495 Unused entries should be reclaimed before the section's contents
1496 are written out, but at the moment this does not happen. Thus in
1497 order to prevent writing out garbage, we initialise the section's
1498 contents to zero. */
eea6121a 1499 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c456f082 1500 if (s->contents == NULL)
b34976b6 1501 return FALSE;
252b5132
RH
1502 }
1503
1504 if (elf_hash_table (info)->dynamic_sections_created)
1505 {
1506 /* Add some entries to the .dynamic section. We fill in the
1507 values later, in elf_m68k_finish_dynamic_sections, but we
1508 must add the entries now so that we get the correct size for
1509 the .dynamic section. The DT_DEBUG entry is filled in by the
1510 dynamic linker and used by the debugger. */
dc810e39 1511#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1512 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1513
252b5132
RH
1514 if (!info->shared)
1515 {
dc810e39 1516 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 1517 return FALSE;
252b5132
RH
1518 }
1519
1520 if (plt)
1521 {
dc810e39
AM
1522 if (!add_dynamic_entry (DT_PLTGOT, 0)
1523 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1524 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1525 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1526 return FALSE;
252b5132
RH
1527 }
1528
1529 if (relocs)
1530 {
dc810e39
AM
1531 if (!add_dynamic_entry (DT_RELA, 0)
1532 || !add_dynamic_entry (DT_RELASZ, 0)
1533 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
b34976b6 1534 return FALSE;
252b5132
RH
1535 }
1536
aa91b392 1537 if ((info->flags & DF_TEXTREL) != 0)
252b5132 1538 {
dc810e39 1539 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1540 return FALSE;
252b5132
RH
1541 }
1542 }
dc810e39 1543#undef add_dynamic_entry
252b5132 1544
b34976b6 1545 return TRUE;
252b5132
RH
1546}
1547
0cca5f05 1548/* This function is called via elf_link_hash_traverse if we are
b6152c34
AS
1549 creating a shared object. In the -Bsymbolic case it discards the
1550 space allocated to copy PC relative relocs against symbols which
3e829b4a 1551 are defined in regular objects. For the normal shared case, it
b6152c34
AS
1552 discards space for pc-relative relocs that have become local due to
1553 symbol visibility changes. We allocated space for them in the
1554 check_relocs routine, but we won't fill them in in the
3e829b4a
AS
1555 relocate_section routine.
1556
1557 We also check whether any of the remaining relocations apply
1558 against a readonly section, and set the DF_TEXTREL flag in this
1559 case. */
252b5132 1560
b34976b6 1561static bfd_boolean
b6152c34 1562elf_m68k_discard_copies (h, inf)
0cca5f05 1563 struct elf_link_hash_entry *h;
b6152c34 1564 PTR inf;
252b5132 1565{
b6152c34 1566 struct bfd_link_info *info = (struct bfd_link_info *) inf;
252b5132
RH
1567 struct elf_m68k_pcrel_relocs_copied *s;
1568
0cca5f05
AS
1569 if (h->root.type == bfd_link_hash_warning)
1570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e92d460e 1571
f5385ebf 1572 if (!h->def_regular
b6152c34 1573 || (!info->symbolic
f5385ebf 1574 && !h->forced_local))
3e829b4a
AS
1575 {
1576 if ((info->flags & DF_TEXTREL) == 0)
1577 {
1578 /* Look for relocations against read-only sections. */
0cca5f05
AS
1579 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1580 s != NULL;
1581 s = s->next)
3e829b4a
AS
1582 if ((s->section->flags & SEC_READONLY) != 0)
1583 {
1584 info->flags |= DF_TEXTREL;
1585 break;
1586 }
1587 }
0cca5f05 1588
3e829b4a
AS
1589 return TRUE;
1590 }
252b5132 1591
0cca5f05
AS
1592 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1593 s != NULL;
1594 s = s->next)
eea6121a 1595 s->section->size -= s->count * sizeof (Elf32_External_Rela);
252b5132 1596
b34976b6 1597 return TRUE;
252b5132
RH
1598}
1599
1600/* Relocate an M68K ELF section. */
1601
b34976b6 1602static bfd_boolean
252b5132
RH
1603elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1604 contents, relocs, local_syms, local_sections)
1605 bfd *output_bfd;
1606 struct bfd_link_info *info;
1607 bfd *input_bfd;
1608 asection *input_section;
1609 bfd_byte *contents;
1610 Elf_Internal_Rela *relocs;
1611 Elf_Internal_Sym *local_syms;
1612 asection **local_sections;
1613{
1614 bfd *dynobj;
1615 Elf_Internal_Shdr *symtab_hdr;
1616 struct elf_link_hash_entry **sym_hashes;
1617 bfd_vma *local_got_offsets;
1618 asection *sgot;
1619 asection *splt;
1620 asection *sreloc;
1621 Elf_Internal_Rela *rel;
1622 Elf_Internal_Rela *relend;
1623
1624 dynobj = elf_hash_table (info)->dynobj;
1625 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1626 sym_hashes = elf_sym_hashes (input_bfd);
1627 local_got_offsets = elf_local_got_offsets (input_bfd);
1628
1629 sgot = NULL;
1630 splt = NULL;
1631 sreloc = NULL;
1632
1633 rel = relocs;
1634 relend = relocs + input_section->reloc_count;
1635 for (; rel < relend; rel++)
1636 {
1637 int r_type;
1638 reloc_howto_type *howto;
1639 unsigned long r_symndx;
1640 struct elf_link_hash_entry *h;
1641 Elf_Internal_Sym *sym;
1642 asection *sec;
1643 bfd_vma relocation;
44f745a6 1644 bfd_boolean unresolved_reloc;
252b5132
RH
1645 bfd_reloc_status_type r;
1646
1647 r_type = ELF32_R_TYPE (rel->r_info);
1648 if (r_type < 0 || r_type >= (int) R_68K_max)
1649 {
1650 bfd_set_error (bfd_error_bad_value);
b34976b6 1651 return FALSE;
252b5132
RH
1652 }
1653 howto = howto_table + r_type;
1654
1655 r_symndx = ELF32_R_SYM (rel->r_info);
1656
252b5132
RH
1657 h = NULL;
1658 sym = NULL;
1659 sec = NULL;
44f745a6 1660 unresolved_reloc = FALSE;
560e09e9 1661
252b5132
RH
1662 if (r_symndx < symtab_hdr->sh_info)
1663 {
1664 sym = local_syms + r_symndx;
1665 sec = local_sections[r_symndx];
8517fae7 1666 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
1667 }
1668 else
1669 {
560e09e9
NC
1670 bfd_boolean warned;
1671
b2a8e766
AM
1672 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1673 r_symndx, symtab_hdr, sym_hashes,
1674 h, sec, relocation,
1675 unresolved_reloc, warned);
252b5132
RH
1676 }
1677
ab96bf03
AM
1678 if (sec != NULL && elf_discarded_section (sec))
1679 {
1680 /* For relocs against symbols from removed linkonce sections,
1681 or sections discarded by a linker script, we just want the
1682 section contents zeroed. Avoid any special processing. */
1683 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1684 rel->r_info = 0;
1685 rel->r_addend = 0;
1686 continue;
1687 }
1688
1689 if (info->relocatable)
1690 continue;
1691
252b5132
RH
1692 switch (r_type)
1693 {
1694 case R_68K_GOT8:
1695 case R_68K_GOT16:
1696 case R_68K_GOT32:
1697 /* Relocation is to the address of the entry for this symbol
1698 in the global offset table. */
1699 if (h != NULL
1700 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1701 break;
1702 /* Fall through. */
1703 case R_68K_GOT8O:
1704 case R_68K_GOT16O:
1705 case R_68K_GOT32O:
1706 /* Relocation is the offset of the entry for this symbol in
1707 the global offset table. */
1708
1709 {
1710 bfd_vma off;
1711
1712 if (sgot == NULL)
1713 {
1714 sgot = bfd_get_section_by_name (dynobj, ".got");
1715 BFD_ASSERT (sgot != NULL);
1716 }
1717
1718 if (h != NULL)
1719 {
a5ad465a
AS
1720 bfd_boolean dyn;
1721
252b5132
RH
1722 off = h->got.offset;
1723 BFD_ASSERT (off != (bfd_vma) -1);
1724
a5ad465a
AS
1725 dyn = elf_hash_table (info)->dynamic_sections_created;
1726 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
252b5132 1727 || (info->shared
a5ad465a
AS
1728 && (info->symbolic
1729 || h->dynindx == -1
f5385ebf
AM
1730 || h->forced_local)
1731 && h->def_regular))
252b5132
RH
1732 {
1733 /* This is actually a static link, or it is a
1734 -Bsymbolic link and the symbol is defined
1735 locally, or the symbol was forced to be local
1736 because of a version file.. We must initialize
1737 this entry in the global offset table. Since
1738 the offset must always be a multiple of 4, we
1739 use the least significant bit to record whether
1740 we have initialized it already.
1741
1742 When doing a dynamic link, we create a .rela.got
1743 relocation entry to initialize the value. This
1744 is done in the finish_dynamic_symbol routine. */
1745 if ((off & 1) != 0)
1746 off &= ~1;
1747 else
1748 {
1749 bfd_put_32 (output_bfd, relocation,
1750 sgot->contents + off);
1751 h->got.offset |= 1;
1752 }
1753 }
44f745a6
AS
1754 else
1755 unresolved_reloc = FALSE;
252b5132
RH
1756 }
1757 else
1758 {
1759 BFD_ASSERT (local_got_offsets != NULL
1760 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1761
1762 off = local_got_offsets[r_symndx];
1763
1764 /* The offset must always be a multiple of 4. We use
1765 the least significant bit to record whether we have
1766 already generated the necessary reloc. */
1767 if ((off & 1) != 0)
1768 off &= ~1;
1769 else
1770 {
1771 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1772
1773 if (info->shared)
1774 {
947216bf 1775 asection *s;
252b5132 1776 Elf_Internal_Rela outrel;
947216bf 1777 bfd_byte *loc;
252b5132 1778
947216bf
AM
1779 s = bfd_get_section_by_name (dynobj, ".rela.got");
1780 BFD_ASSERT (s != NULL);
252b5132
RH
1781
1782 outrel.r_offset = (sgot->output_section->vma
1783 + sgot->output_offset
1784 + off);
1785 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1786 outrel.r_addend = relocation;
947216bf
AM
1787 loc = s->contents;
1788 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1789 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
1790 }
1791
1792 local_got_offsets[r_symndx] |= 1;
1793 }
1794 }
1795
1796 relocation = sgot->output_offset + off;
1797 if (r_type == R_68K_GOT8O
1798 || r_type == R_68K_GOT16O
1799 || r_type == R_68K_GOT32O)
1800 {
1801 /* This relocation does not use the addend. */
1802 rel->r_addend = 0;
1803 }
1804 else
1805 relocation += sgot->output_section->vma;
1806 }
1807 break;
1808
1809 case R_68K_PLT8:
1810 case R_68K_PLT16:
1811 case R_68K_PLT32:
1812 /* Relocation is to the entry for this symbol in the
1813 procedure linkage table. */
1814
1815 /* Resolve a PLTxx reloc against a local symbol directly,
1816 without using the procedure linkage table. */
1817 if (h == NULL)
1818 break;
1819
1820 if (h->plt.offset == (bfd_vma) -1
1821 || !elf_hash_table (info)->dynamic_sections_created)
1822 {
1823 /* We didn't make a PLT entry for this symbol. This
1824 happens when statically linking PIC code, or when
1825 using -Bsymbolic. */
1826 break;
1827 }
1828
1829 if (splt == NULL)
1830 {
1831 splt = bfd_get_section_by_name (dynobj, ".plt");
1832 BFD_ASSERT (splt != NULL);
1833 }
1834
1835 relocation = (splt->output_section->vma
1836 + splt->output_offset
1837 + h->plt.offset);
44f745a6 1838 unresolved_reloc = FALSE;
252b5132
RH
1839 break;
1840
1841 case R_68K_PLT8O:
1842 case R_68K_PLT16O:
1843 case R_68K_PLT32O:
1844 /* Relocation is the offset of the entry for this symbol in
1845 the procedure linkage table. */
1846 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1847
1848 if (splt == NULL)
1849 {
1850 splt = bfd_get_section_by_name (dynobj, ".plt");
1851 BFD_ASSERT (splt != NULL);
1852 }
1853
1854 relocation = h->plt.offset;
44f745a6 1855 unresolved_reloc = FALSE;
252b5132
RH
1856
1857 /* This relocation does not use the addend. */
1858 rel->r_addend = 0;
1859
1860 break;
1861
1862 case R_68K_PC8:
1863 case R_68K_PC16:
1864 case R_68K_PC32:
b6152c34
AS
1865 if (h == NULL
1866 || (info->shared
f5385ebf 1867 && h->forced_local))
252b5132
RH
1868 break;
1869 /* Fall through. */
1870 case R_68K_8:
1871 case R_68K_16:
1872 case R_68K_32:
1873 if (info->shared
ec338859 1874 && r_symndx != 0
252b5132 1875 && (input_section->flags & SEC_ALLOC) != 0
d2ff124f
AS
1876 && (h == NULL
1877 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1878 || h->root.type != bfd_link_hash_undefweak)
252b5132
RH
1879 && ((r_type != R_68K_PC8
1880 && r_type != R_68K_PC16
1881 && r_type != R_68K_PC32)
d2ff124f
AS
1882 || (h != NULL
1883 && h->dynindx != -1
1884 && (!info->symbolic
f5385ebf 1885 || !h->def_regular))))
252b5132
RH
1886 {
1887 Elf_Internal_Rela outrel;
947216bf 1888 bfd_byte *loc;
b34976b6 1889 bfd_boolean skip, relocate;
252b5132
RH
1890
1891 /* When generating a shared object, these relocations
1892 are copied into the output file to be resolved at run
1893 time. */
1894
b34976b6
AM
1895 skip = FALSE;
1896 relocate = FALSE;
252b5132 1897
c629eae0
JJ
1898 outrel.r_offset =
1899 _bfd_elf_section_offset (output_bfd, info, input_section,
1900 rel->r_offset);
1901 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 1902 skip = TRUE;
0bb2d96a 1903 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 1904 skip = TRUE, relocate = TRUE;
252b5132
RH
1905 outrel.r_offset += (input_section->output_section->vma
1906 + input_section->output_offset);
1907
1908 if (skip)
0bb2d96a 1909 memset (&outrel, 0, sizeof outrel);
252b5132 1910 else if (h != NULL
d2ff124f
AS
1911 && h->dynindx != -1
1912 && (r_type == R_68K_PC8
1913 || r_type == R_68K_PC16
1914 || r_type == R_68K_PC32
1915 || !info->shared
1916 || !info->symbolic
f5385ebf 1917 || !h->def_regular))
252b5132 1918 {
252b5132 1919 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
d2ff124f 1920 outrel.r_addend = rel->r_addend;
252b5132
RH
1921 }
1922 else
1923 {
d2ff124f 1924 /* This symbol is local, or marked to become local. */
74541ad4
AM
1925 outrel.r_addend = relocation + rel->r_addend;
1926
252b5132
RH
1927 if (r_type == R_68K_32)
1928 {
b34976b6 1929 relocate = TRUE;
252b5132 1930 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
252b5132
RH
1931 }
1932 else
1933 {
1934 long indx;
1935
8517fae7 1936 if (bfd_is_abs_section (sec))
252b5132
RH
1937 indx = 0;
1938 else if (sec == NULL || sec->owner == NULL)
1939 {
1940 bfd_set_error (bfd_error_bad_value);
b34976b6 1941 return FALSE;
252b5132
RH
1942 }
1943 else
1944 {
1945 asection *osec;
1946
74541ad4
AM
1947 /* We are turning this relocation into one
1948 against a section symbol. It would be
1949 proper to subtract the symbol's value,
1950 osec->vma, from the emitted reloc addend,
1951 but ld.so expects buggy relocs. */
252b5132
RH
1952 osec = sec->output_section;
1953 indx = elf_section_data (osec)->dynindx;
74541ad4
AM
1954 if (indx == 0)
1955 {
1956 struct elf_link_hash_table *htab;
1957 htab = elf_hash_table (info);
1958 osec = htab->text_index_section;
1959 indx = elf_section_data (osec)->dynindx;
1960 }
1961 BFD_ASSERT (indx != 0);
252b5132
RH
1962 }
1963
252b5132 1964 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
1965 }
1966 }
1967
d2ff124f
AS
1968 sreloc = elf_section_data (input_section)->sreloc;
1969 if (sreloc == NULL)
1970 abort ();
1971
947216bf
AM
1972 loc = sreloc->contents;
1973 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1974 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
1975
1976 /* This reloc will be computed at runtime, so there's no
1977 need to do anything now, except for R_68K_32
1978 relocations that have been turned into
1979 R_68K_RELATIVE. */
1980 if (!relocate)
1981 continue;
1982 }
1983
1984 break;
1985
1986 case R_68K_GNU_VTINHERIT:
1987 case R_68K_GNU_VTENTRY:
1988 /* These are no-ops in the end. */
1989 continue;
1990
1991 default:
1992 break;
1993 }
1994
44f745a6
AS
1995 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1996 because such sections are not SEC_ALLOC and thus ld.so will
1997 not process them. */
1998 if (unresolved_reloc
1999 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 2000 && h->def_dynamic))
44f745a6
AS
2001 {
2002 (*_bfd_error_handler)
843fe662 2003 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
2004 input_bfd,
2005 input_section,
44f745a6 2006 (long) rel->r_offset,
843fe662 2007 howto->name,
44f745a6
AS
2008 h->root.root.string);
2009 return FALSE;
2010 }
2011
252b5132
RH
2012 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2013 contents, rel->r_offset,
2014 relocation, rel->r_addend);
2015
2016 if (r != bfd_reloc_ok)
2017 {
44f745a6
AS
2018 const char *name;
2019
2020 if (h != NULL)
2021 name = h->root.root.string;
2022 else
252b5132 2023 {
44f745a6
AS
2024 name = bfd_elf_string_from_elf_section (input_bfd,
2025 symtab_hdr->sh_link,
2026 sym->st_name);
2027 if (name == NULL)
2028 return FALSE;
2029 if (*name == '\0')
2030 name = bfd_section_name (input_bfd, sec);
2031 }
252b5132 2032
44f745a6
AS
2033 if (r == bfd_reloc_overflow)
2034 {
2035 if (!(info->callbacks->reloc_overflow
dfeffb9f
L
2036 (info, (h ? &h->root : NULL), name, howto->name,
2037 (bfd_vma) 0, input_bfd, input_section,
2038 rel->r_offset)))
44f745a6
AS
2039 return FALSE;
2040 }
2041 else
2042 {
2043 (*_bfd_error_handler)
d003868e
AM
2044 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2045 input_bfd, input_section,
44f745a6
AS
2046 (long) rel->r_offset, name, (int) r);
2047 return FALSE;
252b5132
RH
2048 }
2049 }
2050 }
2051
b34976b6 2052 return TRUE;
252b5132
RH
2053}
2054
cc3e26be
RS
2055/* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
2056 into section SEC. */
2057
2058static void
2059elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
2060{
2061 /* Make VALUE PC-relative. */
2062 value -= sec->output_section->vma + offset;
2063
2064 /* Apply any in-place addend. */
2065 value += bfd_get_32 (sec->owner, sec->contents + offset);
2066
2067 bfd_put_32 (sec->owner, value, sec->contents + offset);
2068}
2069
252b5132
RH
2070/* Finish up dynamic symbol handling. We set the contents of various
2071 dynamic sections here. */
2072
b34976b6 2073static bfd_boolean
252b5132
RH
2074elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2075 bfd *output_bfd;
2076 struct bfd_link_info *info;
2077 struct elf_link_hash_entry *h;
2078 Elf_Internal_Sym *sym;
2079{
2080 bfd *dynobj;
2081
2082 dynobj = elf_hash_table (info)->dynobj;
2083
2084 if (h->plt.offset != (bfd_vma) -1)
2085 {
cc3e26be 2086 const struct elf_m68k_plt_info *plt_info;
252b5132
RH
2087 asection *splt;
2088 asection *sgot;
2089 asection *srela;
2090 bfd_vma plt_index;
2091 bfd_vma got_offset;
2092 Elf_Internal_Rela rela;
947216bf 2093 bfd_byte *loc;
252b5132
RH
2094
2095 /* This symbol has an entry in the procedure linkage table. Set
2096 it up. */
2097
2098 BFD_ASSERT (h->dynindx != -1);
2099
cc3e26be 2100 plt_info = elf_m68k_hash_table (info)->plt_info;
252b5132
RH
2101 splt = bfd_get_section_by_name (dynobj, ".plt");
2102 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2103 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2104 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2105
2106 /* Get the index in the procedure linkage table which
2107 corresponds to this symbol. This is the index of this symbol
2108 in all the symbols for which we are making plt entries. The
2109 first entry in the procedure linkage table is reserved. */
cc3e26be 2110 plt_index = (h->plt.offset / plt_info->size) - 1;
252b5132
RH
2111
2112 /* Get the offset into the .got table of the entry that
2113 corresponds to this function. Each .got entry is 4 bytes.
2114 The first three are reserved. */
2115 got_offset = (plt_index + 3) * 4;
2116
cc3e26be
RS
2117 memcpy (splt->contents + h->plt.offset,
2118 plt_info->symbol_entry,
2119 plt_info->size);
2120
2121 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
2122 (sgot->output_section->vma
2123 + sgot->output_offset
2124 + got_offset));
252b5132
RH
2125
2126 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
cc3e26be
RS
2127 splt->contents
2128 + h->plt.offset
2129 + plt_info->symbol_resolve_entry + 2);
2130
2131 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
2132 splt->output_section->vma);
252b5132
RH
2133
2134 /* Fill in the entry in the global offset table. */
2135 bfd_put_32 (output_bfd,
2136 (splt->output_section->vma
2137 + splt->output_offset
2138 + h->plt.offset
cc3e26be 2139 + plt_info->symbol_resolve_entry),
252b5132
RH
2140 sgot->contents + got_offset);
2141
2142 /* Fill in the entry in the .rela.plt section. */
2143 rela.r_offset = (sgot->output_section->vma
2144 + sgot->output_offset
2145 + got_offset);
2146 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2147 rela.r_addend = 0;
947216bf
AM
2148 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2149 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132 2150
f5385ebf 2151 if (!h->def_regular)
252b5132
RH
2152 {
2153 /* Mark the symbol as undefined, rather than as defined in
2154 the .plt section. Leave the value alone. */
2155 sym->st_shndx = SHN_UNDEF;
2156 }
2157 }
2158
2159 if (h->got.offset != (bfd_vma) -1)
2160 {
2161 asection *sgot;
2162 asection *srela;
2163 Elf_Internal_Rela rela;
947216bf 2164 bfd_byte *loc;
252b5132
RH
2165
2166 /* This symbol has an entry in the global offset table. Set it
2167 up. */
2168
2169 sgot = bfd_get_section_by_name (dynobj, ".got");
2170 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2171 BFD_ASSERT (sgot != NULL && srela != NULL);
2172
2173 rela.r_offset = (sgot->output_section->vma
2174 + sgot->output_offset
dc810e39 2175 + (h->got.offset &~ (bfd_vma) 1));
252b5132
RH
2176
2177 /* If this is a -Bsymbolic link, and the symbol is defined
2178 locally, we just want to emit a RELATIVE reloc. Likewise if
2179 the symbol was forced to be local because of a version file.
2180 The entry in the global offset table will already have been
2181 initialized in the relocate_section function. */
2182 if (info->shared
a5ad465a
AS
2183 && (info->symbolic
2184 || h->dynindx == -1
f5385ebf
AM
2185 || h->forced_local)
2186 && h->def_regular)
252b5132
RH
2187 {
2188 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2189 rela.r_addend = bfd_get_signed_32 (output_bfd,
2190 (sgot->contents
dc810e39 2191 + (h->got.offset &~ (bfd_vma) 1)));
252b5132
RH
2192 }
2193 else
2194 {
2195 bfd_put_32 (output_bfd, (bfd_vma) 0,
dc810e39 2196 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
252b5132
RH
2197 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2198 rela.r_addend = 0;
2199 }
2200
947216bf
AM
2201 loc = srela->contents;
2202 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2203 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
2204 }
2205
f5385ebf 2206 if (h->needs_copy)
252b5132
RH
2207 {
2208 asection *s;
2209 Elf_Internal_Rela rela;
947216bf 2210 bfd_byte *loc;
252b5132
RH
2211
2212 /* This symbol needs a copy reloc. Set it up. */
2213
2214 BFD_ASSERT (h->dynindx != -1
2215 && (h->root.type == bfd_link_hash_defined
2216 || h->root.type == bfd_link_hash_defweak));
2217
2218 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2219 ".rela.bss");
2220 BFD_ASSERT (s != NULL);
2221
2222 rela.r_offset = (h->root.u.def.value
2223 + h->root.u.def.section->output_section->vma
2224 + h->root.u.def.section->output_offset);
2225 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2226 rela.r_addend = 0;
947216bf
AM
2227 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2228 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
2229 }
2230
2231 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2232 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 2233 || h == elf_hash_table (info)->hgot)
252b5132
RH
2234 sym->st_shndx = SHN_ABS;
2235
b34976b6 2236 return TRUE;
252b5132
RH
2237}
2238
2239/* Finish up the dynamic sections. */
2240
b34976b6 2241static bfd_boolean
252b5132
RH
2242elf_m68k_finish_dynamic_sections (output_bfd, info)
2243 bfd *output_bfd;
2244 struct bfd_link_info *info;
2245{
2246 bfd *dynobj;
2247 asection *sgot;
2248 asection *sdyn;
2249
2250 dynobj = elf_hash_table (info)->dynobj;
2251
2252 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2253 BFD_ASSERT (sgot != NULL);
2254 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2255
2256 if (elf_hash_table (info)->dynamic_sections_created)
2257 {
2258 asection *splt;
2259 Elf32_External_Dyn *dyncon, *dynconend;
2260
2261 splt = bfd_get_section_by_name (dynobj, ".plt");
2262 BFD_ASSERT (splt != NULL && sdyn != NULL);
2263
2264 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 2265 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
2266 for (; dyncon < dynconend; dyncon++)
2267 {
2268 Elf_Internal_Dyn dyn;
2269 const char *name;
2270 asection *s;
2271
2272 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2273
2274 switch (dyn.d_tag)
2275 {
2276 default:
2277 break;
2278
2279 case DT_PLTGOT:
2280 name = ".got";
2281 goto get_vma;
2282 case DT_JMPREL:
2283 name = ".rela.plt";
2284 get_vma:
2285 s = bfd_get_section_by_name (output_bfd, name);
2286 BFD_ASSERT (s != NULL);
2287 dyn.d_un.d_ptr = s->vma;
2288 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2289 break;
2290
2291 case DT_PLTRELSZ:
2292 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2293 BFD_ASSERT (s != NULL);
eea6121a 2294 dyn.d_un.d_val = s->size;
252b5132
RH
2295 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2296 break;
2297
2298 case DT_RELASZ:
2299 /* The procedure linkage table relocs (DT_JMPREL) should
2300 not be included in the overall relocs (DT_RELA).
2301 Therefore, we override the DT_RELASZ entry here to
2302 make it not include the JMPREL relocs. Since the
2303 linker script arranges for .rela.plt to follow all
2304 other relocation sections, we don't have to worry
2305 about changing the DT_RELA entry. */
2306 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2307 if (s != NULL)
eea6121a 2308 dyn.d_un.d_val -= s->size;
252b5132
RH
2309 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2310 break;
2311 }
2312 }
2313
2314 /* Fill in the first entry in the procedure linkage table. */
eea6121a 2315 if (splt->size > 0)
252b5132 2316 {
cc3e26be
RS
2317 const struct elf_m68k_plt_info *plt_info;
2318
2319 plt_info = elf_m68k_hash_table (info)->plt_info;
2320 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2321
2322 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
2323 (sgot->output_section->vma
2324 + sgot->output_offset
2325 + 4));
2326
2327 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
2328 (sgot->output_section->vma
2329 + sgot->output_offset
2330 + 8));
2331
2332 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2333 = plt_info->size;
252b5132 2334 }
252b5132
RH
2335 }
2336
2337 /* Fill in the first three entries in the global offset table. */
eea6121a 2338 if (sgot->size > 0)
252b5132
RH
2339 {
2340 if (sdyn == NULL)
2341 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2342 else
2343 bfd_put_32 (output_bfd,
2344 sdyn->output_section->vma + sdyn->output_offset,
2345 sgot->contents);
2346 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2347 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2348 }
2349
2350 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2351
b34976b6 2352 return TRUE;
252b5132
RH
2353}
2354
0752970e
NC
2355/* Given a .data section and a .emreloc in-memory section, store
2356 relocation information into the .emreloc section which can be
2357 used at runtime to relocate the section. This is called by the
2358 linker when the --embedded-relocs switch is used. This is called
2359 after the add_symbols entry point has been called for all the
2360 objects, and before the final_link entry point is called. */
2361
b34976b6 2362bfd_boolean
0752970e
NC
2363bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2364 bfd *abfd;
2365 struct bfd_link_info *info;
2366 asection *datasec;
2367 asection *relsec;
2368 char **errmsg;
2369{
2370 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc
AM
2371 Elf_Internal_Sym *isymbuf = NULL;
2372 Elf_Internal_Rela *internal_relocs = NULL;
0752970e
NC
2373 Elf_Internal_Rela *irel, *irelend;
2374 bfd_byte *p;
dc810e39 2375 bfd_size_type amt;
0752970e 2376
1049f94e 2377 BFD_ASSERT (! info->relocatable);
0752970e
NC
2378
2379 *errmsg = NULL;
2380
2381 if (datasec->reloc_count == 0)
b34976b6 2382 return TRUE;
0752970e
NC
2383
2384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9ad5cbcf 2385
0752970e 2386 /* Get a copy of the native relocations. */
45d6a902 2387 internal_relocs = (_bfd_elf_link_read_relocs
0752970e
NC
2388 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2389 info->keep_memory));
2390 if (internal_relocs == NULL)
2391 goto error_return;
0752970e 2392
dc810e39
AM
2393 amt = (bfd_size_type) datasec->reloc_count * 12;
2394 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
0752970e
NC
2395 if (relsec->contents == NULL)
2396 goto error_return;
2397
2398 p = relsec->contents;
2399
2400 irelend = internal_relocs + datasec->reloc_count;
2401 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2402 {
2403 asection *targetsec;
2404
2405 /* We are going to write a four byte longword into the runtime
2406 reloc section. The longword will be the address in the data
2407 section which must be relocated. It is followed by the name
2408 of the target section NUL-padded or truncated to 8
2409 characters. */
2410
2411 /* We can only relocate absolute longword relocs at run time. */
2412 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2413 {
2414 *errmsg = _("unsupported reloc type");
2415 bfd_set_error (bfd_error_bad_value);
2416 goto error_return;
2417 }
2418
2419 /* Get the target section referred to by the reloc. */
2420 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2421 {
0752970e 2422 /* A local symbol. */
6cdc0ccc
AM
2423 Elf_Internal_Sym *isym;
2424
2425 /* Read this BFD's local symbols if we haven't done so already. */
2426 if (isymbuf == NULL)
2427 {
2428 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2429 if (isymbuf == NULL)
2430 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2431 symtab_hdr->sh_info, 0,
2432 NULL, NULL, NULL);
2433 if (isymbuf == NULL)
2434 goto error_return;
2435 }
0752970e 2436
6cdc0ccc
AM
2437 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2438 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
0752970e
NC
2439 }
2440 else
2441 {
2442 unsigned long indx;
2443 struct elf_link_hash_entry *h;
2444
2445 /* An external symbol. */
2446 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2447 h = elf_sym_hashes (abfd)[indx];
2448 BFD_ASSERT (h != NULL);
2449 if (h->root.type == bfd_link_hash_defined
2450 || h->root.type == bfd_link_hash_defweak)
2451 targetsec = h->root.u.def.section;
2452 else
2453 targetsec = NULL;
2454 }
2455
2456 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2457 memset (p + 4, 0, 8);
2458 if (targetsec != NULL)
f075ee0c 2459 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
0752970e 2460 }
c3668558 2461
6cdc0ccc
AM
2462 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2463 free (isymbuf);
2464 if (internal_relocs != NULL
2465 && elf_section_data (datasec)->relocs != internal_relocs)
2466 free (internal_relocs);
b34976b6 2467 return TRUE;
0752970e
NC
2468
2469error_return:
6cdc0ccc
AM
2470 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2471 free (isymbuf);
2472 if (internal_relocs != NULL
2473 && elf_section_data (datasec)->relocs != internal_relocs)
2474 free (internal_relocs);
b34976b6 2475 return FALSE;
0752970e
NC
2476}
2477
aa91b392 2478static enum elf_reloc_type_class
f51e552e
AM
2479elf32_m68k_reloc_type_class (rela)
2480 const Elf_Internal_Rela *rela;
aa91b392 2481{
f51e552e 2482 switch ((int) ELF32_R_TYPE (rela->r_info))
aa91b392
AS
2483 {
2484 case R_68K_RELATIVE:
2485 return reloc_class_relative;
2486 case R_68K_JMP_SLOT:
2487 return reloc_class_plt;
2488 case R_68K_COPY:
2489 return reloc_class_copy;
2490 default:
2491 return reloc_class_normal;
2492 }
2493}
2494
1715e0e3
AS
2495/* Return address for Ith PLT stub in section PLT, for relocation REL
2496 or (bfd_vma) -1 if it should not be included. */
2497
2498static bfd_vma
2499elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2500 const arelent *rel ATTRIBUTE_UNUSED)
2501{
cc3e26be 2502 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
1715e0e3
AS
2503}
2504
252b5132
RH
2505#define TARGET_BIG_SYM bfd_elf32_m68k_vec
2506#define TARGET_BIG_NAME "elf32-m68k"
2507#define ELF_MACHINE_CODE EM_68K
2508#define ELF_MAXPAGESIZE 0x2000
2509#define elf_backend_create_dynamic_sections \
2510 _bfd_elf_create_dynamic_sections
2511#define bfd_elf32_bfd_link_hash_table_create \
2512 elf_m68k_link_hash_table_create
c152c796 2513#define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
252b5132
RH
2514
2515#define elf_backend_check_relocs elf_m68k_check_relocs
cc3e26be
RS
2516#define elf_backend_always_size_sections \
2517 elf_m68k_always_size_sections
252b5132
RH
2518#define elf_backend_adjust_dynamic_symbol \
2519 elf_m68k_adjust_dynamic_symbol
2520#define elf_backend_size_dynamic_sections \
2521 elf_m68k_size_dynamic_sections
74541ad4 2522#define elf_backend_init_index_section _bfd_elf_init_1_index_section
252b5132
RH
2523#define elf_backend_relocate_section elf_m68k_relocate_section
2524#define elf_backend_finish_dynamic_symbol \
2525 elf_m68k_finish_dynamic_symbol
2526#define elf_backend_finish_dynamic_sections \
2527 elf_m68k_finish_dynamic_sections
2528#define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2529#define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
9e1281c7
CM
2530#define bfd_elf32_bfd_merge_private_bfd_data \
2531 elf32_m68k_merge_private_bfd_data
2532#define bfd_elf32_bfd_set_private_flags \
2533 elf32_m68k_set_private_flags
2534#define bfd_elf32_bfd_print_private_bfd_data \
2535 elf32_m68k_print_private_bfd_data
aa91b392 2536#define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
1715e0e3 2537#define elf_backend_plt_sym_val elf_m68k_plt_sym_val
266abb8f 2538#define elf_backend_object_p elf32_m68k_object_p
9e1281c7 2539
252b5132 2540#define elf_backend_can_gc_sections 1
51b64d56 2541#define elf_backend_can_refcount 1
252b5132
RH
2542#define elf_backend_want_got_plt 1
2543#define elf_backend_plt_readonly 1
2544#define elf_backend_want_plt_sym 0
2545#define elf_backend_got_header_size 12
b491616a 2546#define elf_backend_rela_normal 1
252b5132
RH
2547
2548#include "elf32-target.h"