daily update
[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,
7fb9f789 3 2004, 2005, 2006, 2007, 2008 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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ae9a127f 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
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "elf/m68k.h"
266abb8f 28#include "opcode/m68k.h"
252b5132
RH
29
30static reloc_howto_type *reloc_type_lookup
31 PARAMS ((bfd *, bfd_reloc_code_real_type));
32static void rtype_to_howto
947216bf 33 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
252b5132
RH
34static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
37 PARAMS ((bfd *));
b34976b6 38static bfd_boolean elf_m68k_check_relocs
252b5132
RH
39 PARAMS ((bfd *, struct bfd_link_info *, asection *,
40 const Elf_Internal_Rela *));
b34976b6 41static bfd_boolean elf_m68k_adjust_dynamic_symbol
252b5132 42 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
b34976b6 43static bfd_boolean elf_m68k_size_dynamic_sections
252b5132 44 PARAMS ((bfd *, struct bfd_link_info *));
0cca5f05
AS
45static bfd_boolean elf_m68k_discard_copies
46 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 47static bfd_boolean elf_m68k_relocate_section
252b5132
RH
48 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
49 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
b34976b6 50static bfd_boolean elf_m68k_finish_dynamic_symbol
252b5132
RH
51 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
52 Elf_Internal_Sym *));
b34976b6 53static bfd_boolean elf_m68k_finish_dynamic_sections
252b5132
RH
54 PARAMS ((bfd *, struct bfd_link_info *));
55
b34976b6 56static bfd_boolean elf32_m68k_set_private_flags
9e1281c7 57 PARAMS ((bfd *, flagword));
b34976b6 58static bfd_boolean elf32_m68k_merge_private_bfd_data
9e1281c7 59 PARAMS ((bfd *, bfd *));
b34976b6 60static bfd_boolean elf32_m68k_print_private_bfd_data
9e1281c7 61 PARAMS ((bfd *, PTR));
aa91b392 62static enum elf_reloc_type_class elf32_m68k_reloc_type_class
f51e552e 63 PARAMS ((const Elf_Internal_Rela *));
9e1281c7 64
252b5132 65static reloc_howto_type howto_table[] = {
b34976b6
AM
66 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
67 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
68 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
69 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
70 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
71 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
72 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
73 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
74 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
75 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
76 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
77 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
78 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
79 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
80 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
81 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
82 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
83 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
84 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
85 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
86 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),
87 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),
88 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
ae9a127f 89 /* GNU extension to record C++ vtable hierarchy. */
252b5132
RH
90 HOWTO (R_68K_GNU_VTINHERIT, /* type */
91 0, /* rightshift */
92 2, /* size (0 = byte, 1 = short, 2 = long) */
93 0, /* bitsize */
b34976b6 94 FALSE, /* pc_relative */
252b5132
RH
95 0, /* bitpos */
96 complain_overflow_dont, /* complain_on_overflow */
97 NULL, /* special_function */
98 "R_68K_GNU_VTINHERIT", /* name */
b34976b6 99 FALSE, /* partial_inplace */
252b5132
RH
100 0, /* src_mask */
101 0, /* dst_mask */
b34976b6 102 FALSE),
ae9a127f 103 /* GNU extension to record C++ vtable member usage. */
252b5132
RH
104 HOWTO (R_68K_GNU_VTENTRY, /* type */
105 0, /* rightshift */
106 2, /* size (0 = byte, 1 = short, 2 = long) */
107 0, /* bitsize */
b34976b6 108 FALSE, /* pc_relative */
252b5132
RH
109 0, /* bitpos */
110 complain_overflow_dont, /* complain_on_overflow */
111 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
112 "R_68K_GNU_VTENTRY", /* name */
b34976b6 113 FALSE, /* partial_inplace */
252b5132
RH
114 0, /* src_mask */
115 0, /* dst_mask */
b34976b6 116 FALSE),
252b5132
RH
117};
118
119static void
120rtype_to_howto (abfd, cache_ptr, dst)
121089cb 121 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
122 arelent *cache_ptr;
123 Elf_Internal_Rela *dst;
124{
125 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
126 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
127}
128
129#define elf_info_to_howto rtype_to_howto
130
131static const struct
132{
133 bfd_reloc_code_real_type bfd_val;
134 int elf_val;
135} reloc_map[] = {
136 { BFD_RELOC_NONE, R_68K_NONE },
137 { BFD_RELOC_32, R_68K_32 },
138 { BFD_RELOC_16, R_68K_16 },
139 { BFD_RELOC_8, R_68K_8 },
140 { BFD_RELOC_32_PCREL, R_68K_PC32 },
141 { BFD_RELOC_16_PCREL, R_68K_PC16 },
142 { BFD_RELOC_8_PCREL, R_68K_PC8 },
143 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
144 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
145 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
146 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
147 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
148 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
149 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
150 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
151 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
152 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
153 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
154 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
155 { BFD_RELOC_NONE, R_68K_COPY },
156 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
157 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
158 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
159 { BFD_RELOC_CTOR, R_68K_32 },
160 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
161 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
162};
163
164static reloc_howto_type *
165reloc_type_lookup (abfd, code)
121089cb 166 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
167 bfd_reloc_code_real_type code;
168{
169 unsigned int i;
170 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
171 {
172 if (reloc_map[i].bfd_val == code)
173 return &howto_table[reloc_map[i].elf_val];
174 }
175 return 0;
176}
177
157090f7
AM
178static reloc_howto_type *
179reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
180{
181 unsigned int i;
182
183 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
184 if (howto_table[i].name != NULL
185 && strcasecmp (howto_table[i].name, r_name) == 0)
186 return &howto_table[i];
187
188 return NULL;
189}
190
252b5132 191#define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
157090f7 192#define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
252b5132 193#define ELF_ARCH bfd_arch_m68k
252b5132
RH
194\f
195/* Functions for the m68k ELF linker. */
196
197/* The name of the dynamic interpreter. This is put in the .interp
198 section. */
199
200#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
201
cc3e26be
RS
202/* Describes one of the various PLT styles. */
203
204struct elf_m68k_plt_info
205{
206 /* The size of each PLT entry. */
207 bfd_vma size;
208
209 /* The template for the first PLT entry. */
210 const bfd_byte *plt0_entry;
211
212 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
213 The comments by each member indicate the value that the relocation
214 is against. */
215 struct {
216 unsigned int got4; /* .got + 4 */
217 unsigned int got8; /* .got + 8 */
218 } plt0_relocs;
219
220 /* The template for a symbol's PLT entry. */
221 const bfd_byte *symbol_entry;
222
223 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
224 The comments by each member indicate the value that the relocation
225 is against. */
226 struct {
227 unsigned int got; /* the symbol's .got.plt entry */
228 unsigned int plt; /* .plt */
229 } symbol_relocs;
230
231 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
232 The stub starts with "move.l #relocoffset,%d0". */
233 bfd_vma symbol_resolve_entry;
234};
235
252b5132
RH
236/* The size in bytes of an entry in the procedure linkage table. */
237
238#define PLT_ENTRY_SIZE 20
239
240/* The first entry in a procedure linkage table looks like this. See
241 the SVR4 ABI m68k supplement to see how this works. */
242
243static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
244{
245 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
cc3e26be 246 0, 0, 0, 2, /* + (.got + 4) - . */
252b5132 247 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
cc3e26be 248 0, 0, 0, 2, /* + (.got + 8) - . */
252b5132
RH
249 0, 0, 0, 0 /* pad out to 20 bytes. */
250};
251
252/* Subsequent entries in a procedure linkage table look like this. */
253
254static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
255{
256 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
cc3e26be 257 0, 0, 0, 2, /* + (.got.plt entry) - . */
252b5132 258 0x2f, 0x3c, /* move.l #offset,-(%sp) */
cc3e26be 259 0, 0, 0, 0, /* + reloc index */
252b5132 260 0x60, 0xff, /* bra.l .plt */
cc3e26be 261 0, 0, 0, 0 /* + .plt - . */
252b5132
RH
262};
263
cc3e26be
RS
264static const struct elf_m68k_plt_info elf_m68k_plt_info = {
265 PLT_ENTRY_SIZE,
266 elf_m68k_plt0_entry, { 4, 12 },
267 elf_m68k_plt_entry, { 4, 16 }, 8
268};
238d258f 269
7fb9f789 270#define ISAB_PLT_ENTRY_SIZE 24
238d258f 271
cc3e26be 272static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
238d258f 273{
cc3e26be
RS
274 0x20, 0x3c, /* move.l #offset,%d0 */
275 0, 0, 0, 0, /* + (.got + 4) - . */
276 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
277 0x20, 0x3c, /* move.l #offset,%d0 */
278 0, 0, 0, 0, /* + (.got + 8) - . */
279 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
238d258f
NC
280 0x4e, 0xd0, /* jmp (%a0) */
281 0x4e, 0x71 /* nop */
282};
283
284/* Subsequent entries in a procedure linkage table look like this. */
285
cc3e26be 286static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
238d258f 287{
cc3e26be
RS
288 0x20, 0x3c, /* move.l #offset,%d0 */
289 0, 0, 0, 0, /* + (.got.plt entry) - . */
290 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
238d258f
NC
291 0x4e, 0xd0, /* jmp (%a0) */
292 0x2f, 0x3c, /* move.l #offset,-(%sp) */
cc3e26be 293 0, 0, 0, 0, /* + reloc index */
238d258f 294 0x60, 0xff, /* bra.l .plt */
cc3e26be 295 0, 0, 0, 0 /* + .plt - . */
238d258f
NC
296};
297
cc3e26be
RS
298static const struct elf_m68k_plt_info elf_isab_plt_info = {
299 ISAB_PLT_ENTRY_SIZE,
300 elf_isab_plt0_entry, { 2, 12 },
301 elf_isab_plt_entry, { 2, 20 }, 12
302};
9e1281c7 303
7fb9f789 304#define ISAC_PLT_ENTRY_SIZE 24
9a2e615a
NS
305
306static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
307{
308 0x20, 0x3c, /* move.l #offset,%d0 */
309 0, 0, 0, 0, /* replaced with .got + 4 - . */
310 0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
311 0x20, 0x3c, /* move.l #offset,%d0 */
312 0, 0, 0, 0, /* replaced with .got + 8 - . */
313 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
314 0x4e, 0xd0, /* jmp (%a0) */
315 0x4e, 0x71 /* nop */
316};
317
318/* Subsequent entries in a procedure linkage table look like this. */
319
320static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
321{
322 0x20, 0x3c, /* move.l #offset,%d0 */
323 0, 0, 0, 0, /* replaced with (.got entry) - . */
324 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
325 0x4e, 0xd0, /* jmp (%a0) */
326 0x2f, 0x3c, /* move.l #offset,-(%sp) */
327 0, 0, 0, 0, /* replaced with offset into relocation table */
328 0x61, 0xff, /* bsr.l .plt */
329 0, 0, 0, 0 /* replaced with .plt - . */
330};
331
332static const struct elf_m68k_plt_info elf_isac_plt_info = {
333 ISAC_PLT_ENTRY_SIZE,
334 elf_isac_plt0_entry, { 2, 12},
335 elf_isac_plt_entry, { 2, 20 }, 12
336};
337
cc3e26be 338#define CPU32_PLT_ENTRY_SIZE 24
9e1281c7 339/* Procedure linkage table entries for the cpu32 */
cc3e26be 340static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
9e1281c7 341{
6091b433 342 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
cc3e26be 343 0, 0, 0, 2, /* + (.got + 4) - . */
6091b433 344 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
cc3e26be 345 0, 0, 0, 2, /* + (.got + 8) - . */
6091b433 346 0x4e, 0xd1, /* jmp %a1@ */
9e1281c7
CM
347 0, 0, 0, 0, /* pad out to 24 bytes. */
348 0, 0
349};
350
cc3e26be 351static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
9e1281c7 352{
1ca42bad 353 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
cc3e26be 354 0, 0, 0, 2, /* + (.got.plt entry) - . */
1ca42bad 355 0x4e, 0xd1, /* jmp %a1@ */
9e1281c7 356 0x2f, 0x3c, /* move.l #offset,-(%sp) */
cc3e26be 357 0, 0, 0, 0, /* + reloc index */
9e1281c7 358 0x60, 0xff, /* bra.l .plt */
cc3e26be 359 0, 0, 0, 0, /* + .plt - . */
9e1281c7
CM
360 0, 0
361};
362
cc3e26be
RS
363static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
364 CPU32_PLT_ENTRY_SIZE,
365 elf_cpu32_plt0_entry, { 4, 12 },
366 elf_cpu32_plt_entry, { 4, 18 }, 10
367};
368
252b5132
RH
369/* The m68k linker needs to keep track of the number of relocs that it
370 decides to copy in check_relocs for each symbol. This is so that it
371 can discard PC relative relocs if it doesn't need them when linking
372 with -Bsymbolic. We store the information in a field extending the
373 regular ELF linker hash table. */
374
375/* This structure keeps track of the number of PC relative relocs we have
376 copied for a given symbol. */
377
378struct elf_m68k_pcrel_relocs_copied
379{
380 /* Next section. */
381 struct elf_m68k_pcrel_relocs_copied *next;
382 /* A section in dynobj. */
383 asection *section;
384 /* Number of relocs copied in this section. */
385 bfd_size_type count;
386};
387
7fb9f789
NC
388/* Forward declaration. */
389struct elf_m68k_got_entry;
390
252b5132
RH
391/* m68k ELF linker hash entry. */
392
393struct elf_m68k_link_hash_entry
394{
395 struct elf_link_hash_entry root;
396
397 /* Number of PC relative relocs copied for this symbol. */
398 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
7fb9f789
NC
399
400 /* Key to got_entries. */
401 unsigned long got_entry_key;
402
403 /* List of GOT entries for this symbol. This list is build during
404 offset finalization and is used within elf_m68k_finish_dynamic_symbol
405 to traverse all GOT entries for a particular symbol.
406
407 ??? We could've used root.got.glist field instead, but having
408 a separate field is cleaner. */
409 struct elf_m68k_got_entry *glist;
252b5132
RH
410};
411
0cca5f05
AS
412#define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
413
7fb9f789
NC
414/* Key part of GOT entry in hashtable. */
415struct elf_m68k_got_entry_key
416{
417 /* BFD in which this symbol was defined. NULL for global symbols. */
418 const bfd *bfd;
419
420 /* Symbol index. Either local symbol index or h->got_entry_key. */
421 unsigned long symndx;
422};
423
424/* Entry of the GOT. */
425struct elf_m68k_got_entry
426{
427 /* GOT entries are put into a got->entries hashtable. This is the key. */
428 struct elf_m68k_got_entry_key key_;
429
430 /* GOT entry data. We need s1 before offset finalization and s2 after. */
431 union
432 {
433 struct
434 {
435 /* Number of times this entry is referenced. It is used to
436 filter out unnecessary GOT slots in elf_m68k_gc_sweep_hook. */
437 bfd_vma refcount;
438
439 /* Type is one of R_68K_GOT8O, R_68K_GOT16O or R_68K_GOT32O. */
440 int type;
441 } s1;
442
443 struct
444 {
445 /* Offset from the start of .got section. To calculate offset relative
446 to GOT pointer one should substract got->offset from this value. */
447 bfd_vma offset;
448
449 /* Pointer to the next GOT entry for this global symbol.
450 Symbols have at most one entry in one GOT, but might
451 have entries in more than one GOT.
452 Root of this list is h->glist.
453 NULL for local symbols. */
454 struct elf_m68k_got_entry *next;
455 } s2;
456 } u;
457};
458
459/* Data structure representing a single GOT. */
460struct elf_m68k_got
461{
462 /* Hashtable of 'struct elf_m68k_got_entry's.
463 Starting size of this table is the maximum number of
464 R_68K_GOT8O entries. */
465 htab_t entries;
466
467 /* Number of R_68K_GOT8O entries in this GOT.
468 This is used to detect the overflow of number of such entries. */
469 bfd_vma rel_8o_n_entries;
470
471 /* Cumulative count of R_68K_GOT8O and R_68K_GOT16O entries in this GOT.
472 This is used to detect the overflow of number of such entries. */
473 bfd_vma rel_8o_16o_n_entries;
474
475 /* Number of local (entry->key_.h == NULL) entries in this GOT.
476 This is only used to properly calculate size of .rela.got section;
477 see elf_m68k_partition_multi_got. */
478 bfd_vma local_n_entries;
479
480 /* Offset of this GOT relative to beginning of .got section. */
481 bfd_vma offset;
482};
483
484/* BFD and its GOT. This is an entry in multi_got->bfd2got hashtable. */
485struct elf_m68k_bfd2got_entry
486{
487 /* BFD. */
488 const bfd *bfd;
489
490 /* Assigned GOT. Before partitioning multi-GOT each BFD has its own
491 GOT structure. After partitioning several BFD's might [and often do]
492 share a single GOT. */
493 struct elf_m68k_got *got;
494};
495
496/* The main data structure holding all the pieces. */
497struct elf_m68k_multi_got
498{
499 /* Hashtable mapping each BFD to its GOT. If a BFD doesn't have an entry
500 here, then it doesn't need a GOT (this includes the case of a BFD
501 having an empty GOT).
502
503 ??? This hashtable can be replaced by an array indexed by bfd->id. */
504 htab_t bfd2got;
505
506 /* Next symndx to assign a global symbol.
507 h->got_entry_key is initialized from this counter. */
508 unsigned long global_symndx;
509};
510
252b5132
RH
511/* m68k ELF linker hash table. */
512
513struct elf_m68k_link_hash_table
514{
515 struct elf_link_hash_table root;
b6152c34
AS
516
517 /* Small local sym to section mapping cache. */
518 struct sym_sec_cache sym_sec;
cc3e26be
RS
519
520 /* The PLT format used by this link, or NULL if the format has not
521 yet been chosen. */
522 const struct elf_m68k_plt_info *plt_info;
7fb9f789
NC
523
524 /* True, if GP is loaded within each function which uses it.
525 Set to TRUE when GOT negative offsets or multi-GOT is enabled. */
526 bfd_boolean local_gp_p;
527
528 /* Switch controlling use of negative offsets to double the size of GOTs. */
529 bfd_boolean use_neg_got_offsets_p;
530
531 /* Switch controlling generation of multiple GOTs. */
532 bfd_boolean allow_multigot_p;
533
534 /* Multi-GOT data structure. */
535 struct elf_m68k_multi_got multi_got_;
252b5132
RH
536};
537
252b5132
RH
538/* Get the m68k ELF linker hash table from a link_info structure. */
539
540#define elf_m68k_hash_table(p) \
541 ((struct elf_m68k_link_hash_table *) (p)->hash)
542
7fb9f789
NC
543/* Shortcut to multi-GOT data. */
544#define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
545
252b5132
RH
546/* Create an entry in an m68k ELF linker hash table. */
547
548static struct bfd_hash_entry *
549elf_m68k_link_hash_newfunc (entry, table, string)
550 struct bfd_hash_entry *entry;
551 struct bfd_hash_table *table;
552 const char *string;
553{
0cca5f05 554 struct bfd_hash_entry *ret = entry;
252b5132
RH
555
556 /* Allocate the structure if it has not already been allocated by a
557 subclass. */
0cca5f05
AS
558 if (ret == NULL)
559 ret = bfd_hash_allocate (table,
560 sizeof (struct elf_m68k_link_hash_entry));
561 if (ret == NULL)
562 return ret;
252b5132
RH
563
564 /* Call the allocation method of the superclass. */
0cca5f05
AS
565 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
566 if (ret != NULL)
7fb9f789
NC
567 {
568 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
569 elf_m68k_hash_entry (ret)->got_entry_key = 0;
570 elf_m68k_hash_entry (ret)->glist = NULL;
571 }
252b5132 572
0cca5f05 573 return ret;
252b5132
RH
574}
575
576/* Create an m68k ELF linker hash table. */
577
578static struct bfd_link_hash_table *
579elf_m68k_link_hash_table_create (abfd)
580 bfd *abfd;
581{
582 struct elf_m68k_link_hash_table *ret;
dc810e39 583 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
252b5132 584
e2d34d7d 585 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
252b5132
RH
586 if (ret == (struct elf_m68k_link_hash_table *) NULL)
587 return NULL;
588
66eb6687
AM
589 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
590 elf_m68k_link_hash_newfunc,
591 sizeof (struct elf_m68k_link_hash_entry)))
252b5132 592 {
e2d34d7d 593 free (ret);
252b5132
RH
594 return NULL;
595 }
596
b6152c34 597 ret->sym_sec.abfd = NULL;
cc3e26be 598 ret->plt_info = NULL;
7fb9f789
NC
599 ret->local_gp_p = FALSE;
600 ret->use_neg_got_offsets_p = FALSE;
601 ret->allow_multigot_p = FALSE;
602 ret->multi_got_.bfd2got = NULL;
603 ret->multi_got_.global_symndx = 1;
b6152c34 604
252b5132
RH
605 return &ret->root.root;
606}
607
7fb9f789
NC
608/* Destruct local data. */
609
610static void
611elf_m68k_link_hash_table_free (struct bfd_link_hash_table *_htab)
612{
613 struct elf_m68k_link_hash_table *htab;
614
615 htab = (struct elf_m68k_link_hash_table *) _htab;
616
617 if (htab->multi_got_.bfd2got != NULL)
618 {
619 htab_delete (htab->multi_got_.bfd2got);
620 htab->multi_got_.bfd2got = NULL;
621 }
622}
623
266abb8f
NS
624/* Set the right machine number. */
625
626static bfd_boolean
627elf32_m68k_object_p (bfd *abfd)
628{
629 unsigned int mach = 0;
630 unsigned features = 0;
631 flagword eflags = elf_elfheader (abfd)->e_flags;
632
425c6cb0 633 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
266abb8f 634 features |= m68000;
425c6cb0 635 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3bdcfdf4
KH
636 features |= cpu32;
637 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
638 features |= fido_a;
425c6cb0 639 else
266abb8f 640 {
c694fd50 641 switch (eflags & EF_M68K_CF_ISA_MASK)
266abb8f 642 {
c694fd50 643 case EF_M68K_CF_ISA_A_NODIV:
266abb8f
NS
644 features |= mcfisa_a;
645 break;
c694fd50 646 case EF_M68K_CF_ISA_A:
0b2e31dc
NS
647 features |= mcfisa_a|mcfhwdiv;
648 break;
c694fd50 649 case EF_M68K_CF_ISA_A_PLUS:
0b2e31dc
NS
650 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
651 break;
c694fd50 652 case EF_M68K_CF_ISA_B_NOUSP:
0b2e31dc
NS
653 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
654 break;
c694fd50 655 case EF_M68K_CF_ISA_B:
0b2e31dc
NS
656 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
657 break;
9a2e615a
NS
658 case EF_M68K_CF_ISA_C:
659 features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
660 break;
8d100c32
KH
661 case EF_M68K_CF_ISA_C_NODIV:
662 features |= mcfisa_a|mcfisa_c|mcfusp;
663 break;
266abb8f 664 }
c694fd50 665 switch (eflags & EF_M68K_CF_MAC_MASK)
266abb8f 666 {
c694fd50 667 case EF_M68K_CF_MAC:
266abb8f
NS
668 features |= mcfmac;
669 break;
c694fd50 670 case EF_M68K_CF_EMAC:
266abb8f
NS
671 features |= mcfemac;
672 break;
673 }
c694fd50 674 if (eflags & EF_M68K_CF_FLOAT)
266abb8f
NS
675 features |= cfloat;
676 }
677
678 mach = bfd_m68k_features_to_mach (features);
679 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
680
681 return TRUE;
682}
683
ae9a127f 684/* Keep m68k-specific flags in the ELF header. */
b34976b6 685static bfd_boolean
9e1281c7
CM
686elf32_m68k_set_private_flags (abfd, flags)
687 bfd *abfd;
688 flagword flags;
689{
690 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
691 elf_flags_init (abfd) = TRUE;
692 return TRUE;
9e1281c7
CM
693}
694
9e1281c7
CM
695/* Merge backend specific data from an object file to the output
696 object file when linking. */
b34976b6 697static bfd_boolean
9e1281c7
CM
698elf32_m68k_merge_private_bfd_data (ibfd, obfd)
699 bfd *ibfd;
700 bfd *obfd;
701{
702 flagword out_flags;
703 flagword in_flags;
a9d34880
RS
704 flagword out_isa;
705 flagword in_isa;
706 const bfd_arch_info_type *arch_info;
7fb9f789 707
9e1281c7
CM
708 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
709 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
266abb8f
NS
710 return FALSE;
711
a9d34880
RS
712 /* Get the merged machine. This checks for incompatibility between
713 Coldfire & non-Coldfire flags, incompability between different
714 Coldfire ISAs, and incompability between different MAC types. */
715 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
716 if (!arch_info)
717 return FALSE;
9e1281c7 718
7fb9f789
NC
719 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
720
721 in_flags = elf_elfheader (ibfd)->e_flags;
722 if (!elf_flags_init (obfd))
723 {
724 elf_flags_init (obfd) = TRUE;
725 out_flags = in_flags;
726 }
727 else
728 {
729 out_flags = elf_elfheader (obfd)->e_flags;
730 unsigned int variant_mask;
731
732 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
733 variant_mask = 0;
734 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
735 variant_mask = 0;
736 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
737 variant_mask = 0;
738 else
739 variant_mask = EF_M68K_CF_ISA_MASK;
740
741 in_isa = (in_flags & variant_mask);
742 out_isa = (out_flags & variant_mask);
743 if (in_isa > out_isa)
744 out_flags ^= in_isa ^ out_isa;
745 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
746 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
747 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
748 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
749 out_flags = EF_M68K_FIDO;
750 else
751 out_flags |= in_flags ^ in_isa;
752 }
753 elf_elfheader (obfd)->e_flags = out_flags;
754
755 return TRUE;
756}
757
758/* Display the flags field. */
759
760static bfd_boolean
761elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
762{
763 FILE *file = (FILE *) ptr;
764 flagword eflags = elf_elfheader (abfd)->e_flags;
765
766 BFD_ASSERT (abfd != NULL && ptr != NULL);
767
768 /* Print normal ELF private data. */
769 _bfd_elf_print_private_bfd_data (abfd, ptr);
770
771 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
772
773 /* xgettext:c-format */
774 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
775
776 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
777 fprintf (file, " [m68000]");
778 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
779 fprintf (file, " [cpu32]");
780 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
781 fprintf (file, " [fido]");
782 else
783 {
784 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
785 fprintf (file, " [cfv4e]");
786
787 if (eflags & EF_M68K_CF_ISA_MASK)
788 {
789 char const *isa = _("unknown");
790 char const *mac = _("unknown");
791 char const *additional = "";
792
793 switch (eflags & EF_M68K_CF_ISA_MASK)
794 {
795 case EF_M68K_CF_ISA_A_NODIV:
796 isa = "A";
797 additional = " [nodiv]";
798 break;
799 case EF_M68K_CF_ISA_A:
800 isa = "A";
801 break;
802 case EF_M68K_CF_ISA_A_PLUS:
803 isa = "A+";
804 break;
805 case EF_M68K_CF_ISA_B_NOUSP:
806 isa = "B";
807 additional = " [nousp]";
808 break;
809 case EF_M68K_CF_ISA_B:
810 isa = "B";
811 break;
812 case EF_M68K_CF_ISA_C:
813 isa = "C";
814 break;
815 case EF_M68K_CF_ISA_C_NODIV:
816 isa = "C";
817 additional = " [nodiv]";
818 break;
819 }
820 fprintf (file, " [isa %s]%s", isa, additional);
821
822 if (eflags & EF_M68K_CF_FLOAT)
823 fprintf (file, " [float]");
824
825 switch (eflags & EF_M68K_CF_MAC_MASK)
826 {
827 case 0:
828 mac = NULL;
829 break;
830 case EF_M68K_CF_MAC:
831 mac = "mac";
832 break;
833 case EF_M68K_CF_EMAC:
834 mac = "emac";
835 break;
836 }
837 if (mac)
838 fprintf (file, " [%s]", mac);
839 }
840 }
841
842 fputc ('\n', file);
843
844 return TRUE;
845}
846
847/* Multi-GOT support implementation design:
848
849 Multi-GOT starts in check_relocs hook. There we scan all
850 relocations of a BFD and build a local GOT (struct elf_m68k_got)
851 for it. If a single BFD appears to require too many GOT slots with
852 R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
853 to user.
854 After check_relocs has been invoked for each input BFD, we have
855 constructed a GOT for each input BFD.
856
857 To minimize total number of GOTs required for a particular output BFD
858 (as some environments support only 1 GOT per output object) we try
859 to merge some of the GOTs to share an offset space. Ideally [and in most
860 cases] we end up with a single GOT. In cases when there are too many
861 restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
862 several GOTs, assuming the environment can handle them.
863
864 Partitioning is done in elf_m68k_partition_multi_got. We start with
865 an empty GOT and traverse bfd2got hashtable putting got_entries from
866 local GOTs to the new 'big' one. We do that by constructing an
867 intermediate GOT holding all the entries the local GOT has and the big
868 GOT lacks. Then we check if there is room in the big GOT to accomodate
869 all the entries from diff. On success we add those entries to the big
870 GOT; on failure we start the new 'big' GOT and retry the adding of
871 entries from the local GOT. Note that this retry will always succeed as
872 each local GOT doesn't overflow the limits. After partitioning we
873 end up with each bfd assigned one of the big GOTs. GOT entries in the
874 big GOTs are initialized with GOT offsets. Note that big GOTs are
875 positioned consequently in program space and represent a single huge GOT
876 to the outside world.
877
878 After that we get to elf_m68k_relocate_section. There we
879 adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
880 relocations to refer to appropriate [assigned to current input_bfd]
881 big GOT.
882
883 Notes:
884
885 GOT entry type: We have 3 types of GOT entries.
886 * R_68K_GOT8O type is used in entries for symbols that have
887 at least one R_68K_GOT8O relocation. We can have at most 0x40
888 such entries in one GOT.
889 * R_68K_GOT16O type is used in entries for symbols that have
890 at least one R_68K_GOT16O relocation and no R_68K_GOT8O relocations.
891 We can have at most 0x4000 such entries in one GOT.
892 * R_68K_GOT32O type is used in all other cases. We can have as many
893 such entries in one GOT as we like.
894 When counting relocations we have to include the count of the smaller
895 ranged relocations in the counts of the larger ranged ones in order
896 to correctly detect overflow.
897
898 Sorting the GOT: In each GOT starting offsets are assigned to
899 R_68K_GOT8O entries, which are followed by R_68K_GOT16O entries, and
900 R_68K_GOT32O entries go at the end. See finalize_got_offsets for details.
901
902 Negative GOT offsets: To double usable offset range of GOTs we use
903 negative offsets. As we assign entries with GOT offsets relative to
904 start of .got section, the offset values are positive. They become
905 negative only in relocate_section where got->offset value is
906 subtracted from them.
907
908 3 special GOT entries: There are 3 special GOT entries used internally
909 by loader. These entries happen to be placed to .got.plt section,
910 so we don't do anything about them in multi-GOT support.
911
912 Memory management: All data except for hashtables
913 multi_got->bfd2got and got->entries are allocated on
914 elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
915 to most functions), so we don't need to care to free them. At the
916 moment of allocation hashtables are being linked into main data
917 structure (multi_got), all pieces of which are reachable from
918 elf_m68k_multi_got (info). We deallocate them in
919 elf_m68k_link_hash_table_free. */
920
921/* Initialize GOT. */
922
923static void
924elf_m68k_init_got (struct elf_m68k_got *got,
925 htab_t entries,
926 bfd_vma rel_8o_n_entries,
927 bfd_vma rel_8o_16o_n_entries,
928 bfd_vma local_n_entries,
929 bfd_vma offset)
930{
931 got->entries = entries;
932 got->rel_8o_n_entries = rel_8o_n_entries;
933 got->rel_8o_16o_n_entries = rel_8o_16o_n_entries;
934 got->local_n_entries = local_n_entries;
935 got->offset = offset;
936}
937
938/* Destruct GOT. */
939
940static void
941elf_m68k_clear_got (struct elf_m68k_got *got)
942{
943 if (got->entries != NULL)
944 {
945 htab_delete (got->entries);
946 got->entries = NULL;
947 }
948}
949
950/* Create and empty GOT structure. INFO is the context where memory
951 should be allocated. */
952
953static struct elf_m68k_got *
954elf_m68k_create_empty_got (struct bfd_link_info *info)
955{
956 struct elf_m68k_got *got;
957
958 got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
959 if (got == NULL)
960 return NULL;
961
962 elf_m68k_init_got (got, NULL, 0, 0, 0, (bfd_vma) -1);
963
964 return got;
965}
966
967/* Initialize KEY. */
968
969static void
970elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
971 struct elf_link_hash_entry *h,
972 const bfd *abfd, unsigned long symndx)
973{
974 if (h != NULL)
975 {
976 key->bfd = NULL;
977 key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
978 BFD_ASSERT (key->symndx != 0);
979 }
980 else
981 {
982 key->bfd = abfd;
983 key->symndx = symndx;
984 }
985}
986
987/* Calculate hash of got_entry.
988 ??? Is it good? */
989
990static hashval_t
991elf_m68k_got_entry_hash (const void *_entry)
992{
993 const struct elf_m68k_got_entry_key *key;
994
995 key = &((const struct elf_m68k_got_entry *) _entry)->key_;
996
997 return key->symndx + (key->bfd != NULL
998 ? (int) key->bfd->id
999 : -1);
1000}
1001
1002/* Check if two got entries are equal. */
1003
1004static int
1005elf_m68k_got_entry_eq (const void *_entry1, const void *_entry2)
1006{
1007 const struct elf_m68k_got_entry_key *key1;
1008 const struct elf_m68k_got_entry_key *key2;
1009
1010 key1 = &((const struct elf_m68k_got_entry *) _entry1)->key_;
1011 key2 = &((const struct elf_m68k_got_entry *) _entry2)->key_;
1012
1013 return (key1->bfd == key2->bfd
1014 && key1->symndx == key2->symndx);
1015}
1016
1017/* Maximal number of R_68K_GOT8O entries in a single GOT. */
1018#define ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT(INFO) \
1019 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1020 ? 0x40 \
1021 : 0x20)
1022
1023/* Maximal number of R_68K_GOT8O and R_68K_GOT16O entries in a single GOT. */
1024#define ELF_M68K_REL_8O_16O_MAX_N_ENTRIES_IN_GOT(INFO) \
1025 (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p \
1026 ? 0x4000 \
1027 : 0x2000)
1028
1029/* SEARCH - simply search the hashtable, don't insert new entries or fail when
1030 the entry cannot be found.
1031 FIND_OR_CREATE - search for an existing entry, but create new if there's
1032 no such.
1033 MUST_FIND - search for an existing entry and assert that it exist.
1034 MUST_CREATE - assert that there's no such entry and create new one. */
1035enum elf_m68k_get_entry_howto
1036 {
1037 SEARCH,
1038 FIND_OR_CREATE,
1039 MUST_FIND,
1040 MUST_CREATE
1041 };
1042
1043/* Get or create (depending on HOWTO) entry with KEY in GOT.
1044 INFO is context in which memory should be allocated (can be NULL if
1045 HOWTO is SEARCH or MUST_FIND). */
1046
1047static struct elf_m68k_got_entry *
1048elf_m68k_get_got_entry (struct elf_m68k_got *got,
1049 const struct elf_m68k_got_entry_key *key,
1050 enum elf_m68k_get_entry_howto howto,
1051 struct bfd_link_info *info)
1052{
1053 struct elf_m68k_got_entry entry_;
1054 struct elf_m68k_got_entry *entry;
1055 void **ptr;
1056
1057 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1058
1059 if (got->entries == NULL)
1060 /* This is the first entry in ABFD. Initialize hashtable. */
1061 {
1062 if (howto == SEARCH)
1063 return NULL;
1064
1065 got->entries = htab_try_create (ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT
1066 (info),
1067 elf_m68k_got_entry_hash,
1068 elf_m68k_got_entry_eq, NULL);
1069 if (got->entries == NULL)
1070 {
1071 bfd_set_error (bfd_error_no_memory);
1072 return NULL;
1073 }
1074 }
1075
1076 entry_.key_ = *key;
1077 ptr = htab_find_slot (got->entries, &entry_, (howto != SEARCH
1078 ? INSERT : NO_INSERT));
1079 if (ptr == NULL)
1080 {
1081 if (howto == SEARCH)
1082 /* Entry not found. */
1083 return NULL;
1084
1085 /* We're out of memory. */
1086 bfd_set_error (bfd_error_no_memory);
1087 return NULL;
1088 }
1089
1090 if (*ptr == NULL)
1091 /* We didn't find the entry and we're asked to create a new one. */
1092 {
1093 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1094
1095 entry = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry));
1096 if (entry == NULL)
1097 return NULL;
1098
1099 /* Initialize new entry. */
1100 entry->key_ = *key;
1101
1102 entry->u.s1.refcount = 0;
1103 entry->u.s1.type = R_68K_GOT32O;
1104
1105 *ptr = entry;
1106 }
1107 else
1108 /* We found the entry. */
1109 {
1110 BFD_ASSERT (howto != MUST_CREATE);
1111
1112 entry = *ptr;
1113 }
1114
1115 return entry;
1116}
1117
1118/* Update GOT counters when merging entry of WAS type with entry of NEW type.
1119 Return the value to which ENTRY's type should be set. */
1120
1121static int
1122elf_m68k_update_got_entry_type (struct elf_m68k_got *got, int was, int new)
1123{
1124 if (new == R_68K_GOT8O && was != R_68K_GOT8O)
1125 /* NEW overrides WAS. */
1126 {
1127 ++got->rel_8o_n_entries;
1128
1129 if (was != R_68K_GOT16O)
1130 /* Update this counter too. */
1131 ++got->rel_8o_16o_n_entries;
1132 }
1133 else if (new == R_68K_GOT16O && was != R_68K_GOT8O && was != R_68K_GOT16O)
1134 /* NEW overrides WAS. */
1135 ++got->rel_8o_16o_n_entries;
1136 else
1137 /* NEW doesn't override WAS. */
1138 new = was;
1139
1140 return new;
1141}
1142
1143/* Update GOT counters when removing an entry of type TYPE. */
1144
1145static void
1146elf_m68k_remove_got_entry_type (struct elf_m68k_got *got, int type)
1147{
1148 switch (type)
1149 {
1150 case R_68K_GOT8O:
1151 BFD_ASSERT (got->rel_8o_n_entries > 0);
1152
1153 --got->rel_8o_n_entries;
1154 /* FALLTHRU */
1155
1156 case R_68K_GOT16O:
1157 BFD_ASSERT (got->rel_8o_16o_n_entries >= got->rel_8o_n_entries);
1158
1159 --got->rel_8o_16o_n_entries;
1160 /* FALLTHRU */
1161
1162 case R_68K_GOT32O:
1163 break;
1164
1165 default:
1166 BFD_ASSERT (FALSE);
1167 }
1168}
1169
1170/* Add new or update existing entry to GOT.
1171 H, ABFD, TYPE and SYMNDX is data for the entry.
1172 INFO is a context where memory should be allocated. */
1173
1174static struct elf_m68k_got_entry *
1175elf_m68k_add_entry_to_got (struct elf_m68k_got *got,
1176 struct elf_link_hash_entry *h,
1177 const bfd *abfd,
1178 int type, unsigned long symndx,
1179 struct bfd_link_info *info)
1180{
1181 struct elf_m68k_got_entry_key key_;
1182 struct elf_m68k_got_entry *entry;
1183
1184 if (h != NULL && elf_m68k_hash_entry (h)->got_entry_key == 0)
1185 elf_m68k_hash_entry (h)->got_entry_key
1186 = elf_m68k_multi_got (info)->global_symndx++;
1187
1188 elf_m68k_init_got_entry_key (&key_, h, abfd, symndx);
1189
1190 entry = elf_m68k_get_got_entry (got, &key_, FIND_OR_CREATE, info);
1191 if (entry == NULL)
1192 return NULL;
1193
1194 /* Update refcount. */
1195 ++entry->u.s1.refcount;
1196
1197 if (entry->u.s1.refcount == 1)
1198 /* We see this entry for the first time. */
1199 {
1200 if (entry->key_.bfd != NULL)
1201 ++got->local_n_entries;
1202 }
1203
1204 /* Determine entry's type and update got->rel_*_n_entries counters. */
1205 entry->u.s1.type = elf_m68k_update_got_entry_type (got, entry->u.s1.type,
1206 type);
1207
1208 if ((got->rel_8o_n_entries
1209 > ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT (info))
1210 || (got->rel_8o_16o_n_entries
1211 > ELF_M68K_REL_8O_16O_MAX_N_ENTRIES_IN_GOT (info)))
1212 /* This BFD has too many relocation. */
1213 {
1214 if (got->rel_8o_n_entries
1215 > ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT (info))
1216 (*_bfd_error_handler) (_("%B: GOT overflow: "
1217 "Number of R_68K_GOT8O relocations > %d"),
1218 abfd,
1219 ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT (info));
1220 else
1221 (*_bfd_error_handler) (_("%B: GOT overflow: "
1222 "Number of R_68K_GOT8O and R_68K_GOT16O "
1223 "relocations > %d"),
1224 abfd,
1225 ELF_M68K_REL_8O_16O_MAX_N_ENTRIES_IN_GOT (info));
1226
1227 return NULL;
1228 }
1229
1230 return entry;
1231}
1232
1233/* Compute the hash value of the bfd in a bfd2got hash entry. */
1234
1235static hashval_t
1236elf_m68k_bfd2got_entry_hash (const void *entry)
1237{
1238 const struct elf_m68k_bfd2got_entry *e;
1239
1240 e = (const struct elf_m68k_bfd2got_entry *) entry;
1241
1242 return e->bfd->id;
1243}
1244
1245/* Check whether two hash entries have the same bfd. */
1246
1247static int
1248elf_m68k_bfd2got_entry_eq (const void *entry1, const void *entry2)
1249{
1250 const struct elf_m68k_bfd2got_entry *e1;
1251 const struct elf_m68k_bfd2got_entry *e2;
1252
1253 e1 = (const struct elf_m68k_bfd2got_entry *) entry1;
1254 e2 = (const struct elf_m68k_bfd2got_entry *) entry2;
1255
1256 return e1->bfd == e2->bfd;
1257}
1258
1259/* Destruct a bfd2got entry. */
1260
1261static void
1262elf_m68k_bfd2got_entry_del (void *_entry)
1263{
1264 struct elf_m68k_bfd2got_entry *entry;
1265
1266 entry = (struct elf_m68k_bfd2got_entry *) _entry;
1267
1268 BFD_ASSERT (entry->got != NULL);
1269 elf_m68k_clear_got (entry->got);
1270}
1271
1272/* Find existing or create new (depending on HOWTO) bfd2got entry in
1273 MULTI_GOT. ABFD is the bfd we need a GOT for. INFO is a context where
1274 memory should be allocated. */
1275
1276static struct elf_m68k_bfd2got_entry *
1277elf_m68k_get_bfd2got_entry (struct elf_m68k_multi_got *multi_got,
1278 const bfd *abfd,
1279 enum elf_m68k_get_entry_howto howto,
1280 struct bfd_link_info *info)
1281{
1282 struct elf_m68k_bfd2got_entry entry_;
1283 void **ptr;
1284 struct elf_m68k_bfd2got_entry *entry;
1285
1286 BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1287
1288 if (multi_got->bfd2got == NULL)
1289 /* This is the first GOT. Initialize bfd2got. */
1290 {
1291 if (howto == SEARCH)
1292 return NULL;
1293
1294 multi_got->bfd2got = htab_try_create (1, elf_m68k_bfd2got_entry_hash,
1295 elf_m68k_bfd2got_entry_eq,
1296 elf_m68k_bfd2got_entry_del);
1297 if (multi_got->bfd2got == NULL)
1298 {
1299 bfd_set_error (bfd_error_no_memory);
1300 return NULL;
1301 }
1302 }
1303
1304 entry_.bfd = abfd;
1305 ptr = htab_find_slot (multi_got->bfd2got, &entry_, (howto != SEARCH
1306 ? INSERT : NO_INSERT));
1307 if (ptr == NULL)
1308 {
1309 if (howto == SEARCH)
1310 /* Entry not found. */
1311 return NULL;
1312
1313 /* We're out of memory. */
1314 bfd_set_error (bfd_error_no_memory);
1315 return NULL;
1316 }
1317
1318 if (*ptr == NULL)
1319 /* Entry was not found. Create new one. */
1320 {
1321 BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1322
1323 entry = ((struct elf_m68k_bfd2got_entry *)
1324 bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry)));
1325 if (entry == NULL)
1326 return NULL;
1327
1328 entry->bfd = abfd;
1329
1330 entry->got = elf_m68k_create_empty_got (info);
1331 if (entry->got == NULL)
1332 return NULL;
1333
1334 *ptr = entry;
1335 }
1336 else
1337 {
1338 BFD_ASSERT (howto != MUST_CREATE);
1339
1340 /* Return existing entry. */
1341 entry = *ptr;
1342 }
1343
1344 return entry;
1345}
1346
1347struct elf_m68k_can_merge_gots_arg
1348{
1349 /* A current_got that we constructing a DIFF against. */
1350 struct elf_m68k_got *big;
1351
1352 /* GOT holding entries not present or that should be changed in
1353 BIG. */
1354 struct elf_m68k_got *diff;
1355
1356 /* Context where to allocate memory. */
1357 struct bfd_link_info *info;
1358
1359 /* Error flag. */
1360 bfd_boolean error_p;
1361};
1362
1363/* Process a single entry from the small GOT to see if it should be added
1364 or updated in the big GOT. */
1365
1366static int
1367elf_m68k_can_merge_gots_1 (void **_entry_ptr, void *_arg)
1368{
1369 const struct elf_m68k_got_entry *entry1;
1370 struct elf_m68k_can_merge_gots_arg *arg;
1371 const struct elf_m68k_got_entry *entry2;
1372 int type;
1373
1374 entry1 = (const struct elf_m68k_got_entry *) *_entry_ptr;
1375 arg = (struct elf_m68k_can_merge_gots_arg *) _arg;
1376
1377 entry2 = elf_m68k_get_got_entry (arg->big, &entry1->key_, SEARCH, NULL);
1378
1379 if (entry2 != NULL)
1380 {
1381 type = elf_m68k_update_got_entry_type (arg->diff, entry2->u.s1.type,
1382 entry1->u.s1.type);
1383
1384 if (type == entry2->u.s1.type)
1385 /* ENTRY1 doesn't update data in ENTRY2. Skip it.
1386 To skip creation of difference entry we use the type,
1387 which we won't see in GOT entries for sure. */
1388 type = R_68K_32;
1389 }
1390 else
1391 {
1392 BFD_ASSERT (entry1->u.s1.type != R_68K_32);
1393
1394 type = elf_m68k_update_got_entry_type (arg->diff, R_68K_GOT32O,
1395 entry1->u.s1.type);
1396
1397 /* Update local counter. */
1398 if (entry1->key_.bfd != NULL)
1399 ++arg->diff->local_n_entries;
1400 }
1401
1402 if (type != R_68K_32)
1403 /* Create an entry in DIFF. */
1404 {
1405 struct elf_m68k_got_entry *entry;
1406
1407 entry = elf_m68k_get_got_entry (arg->diff, &entry1->key_, MUST_CREATE,
1408 arg->info);
1409 if (entry == NULL)
1410 {
1411 arg->error_p = TRUE;
1412 return 0;
1413 }
1414
1415 entry->u.s1.type = type;
1416 }
1417
1418 return 1;
1419}
1420
1421/* Return TRUE if SMALL GOT can be added to BIG GOT without overflowing it.
1422 Construct DIFF GOT holding the entries which should be added or updated
1423 in BIG GOT to accumulate information from SMALL.
1424 INFO is the context where memory should be allocated. */
1425
1426static bfd_boolean
1427elf_m68k_can_merge_gots (struct elf_m68k_got *big,
1428 const struct elf_m68k_got *small,
1429 struct bfd_link_info *info,
1430 struct elf_m68k_got *diff)
1431{
1432 struct elf_m68k_can_merge_gots_arg arg_;
1433
1434 BFD_ASSERT (small->offset == (bfd_vma) -1);
1435
1436 arg_.big = big;
1437 arg_.diff = diff;
1438 arg_.info = info;
1439 arg_.error_p = FALSE;
1440 htab_traverse_noresize (small->entries, elf_m68k_can_merge_gots_1, &arg_);
1441 if (arg_.error_p)
1442 {
1443 diff->offset = 0;
1444 return FALSE;
1445 }
1446
1447 /* Check for overflow. */
1448 if ((big->rel_8o_n_entries + arg_.diff->rel_8o_n_entries
1449 > ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT (info))
1450 || (big->rel_8o_16o_n_entries + arg_.diff->rel_8o_16o_n_entries
1451 > ELF_M68K_REL_8O_16O_MAX_N_ENTRIES_IN_GOT (info)))
1452 return FALSE;
1453
1454 return TRUE;
1455}
1456
1457struct elf_m68k_merge_gots_arg
1458{
1459 /* The BIG got. */
1460 struct elf_m68k_got *big;
1461
1462 /* Context where memory should be allocated. */
1463 struct bfd_link_info *info;
1464
1465 /* Error flag. */
1466 bfd_boolean error_p;
1467};
1468
1469/* Process a single entry from DIFF got. Add or update corresponding
1470 entry in the BIG got. */
1471
1472static int
1473elf_m68k_merge_gots_1 (void **entry_ptr, void *_arg)
1474{
1475 const struct elf_m68k_got_entry *from;
1476 struct elf_m68k_merge_gots_arg *arg;
1477 struct elf_m68k_got_entry *to;
1478
1479 from = (const struct elf_m68k_got_entry *) *entry_ptr;
1480 arg = (struct elf_m68k_merge_gots_arg *) _arg;
1481
1482 to = elf_m68k_get_got_entry (arg->big, &from->key_, FIND_OR_CREATE,
1483 arg->info);
1484 if (to == NULL)
1485 {
1486 arg->error_p = TRUE;
1487 return 0;
1488 }
1489
1490 BFD_ASSERT (to->u.s1.refcount == 0);
1491 /* All we need to merge is TYPE. */
1492 to->u.s1.type = from->u.s1.type;
1493
1494 return 1;
1495}
1496
1497/* Merge data from DIFF to BIG. INFO is context where memory should be
1498 allocated. */
1499
1500static bfd_boolean
1501elf_m68k_merge_gots (struct elf_m68k_got *big,
1502 struct elf_m68k_got *diff,
1503 struct bfd_link_info *info)
1504{
1505 if (diff->entries != NULL)
1506 /* DIFF is not empty. Merge it into BIG GOT. */
1507 {
1508 struct elf_m68k_merge_gots_arg arg_;
1509
1510 /* Merge entries. */
1511 arg_.big = big;
1512 arg_.info = info;
1513 arg_.error_p = FALSE;
1514 htab_traverse_noresize (diff->entries, elf_m68k_merge_gots_1, &arg_);
1515 if (arg_.error_p)
1516 return FALSE;
1517
1518 /* Merge counters. */
1519 big->rel_8o_n_entries += diff->rel_8o_n_entries;
1520 big->rel_8o_16o_n_entries += diff->rel_8o_16o_n_entries;
1521 big->local_n_entries += diff->local_n_entries;
1522 }
1523 else
1524 /* DIFF is empty. */
1525 {
1526 BFD_ASSERT (diff->rel_8o_n_entries == 0);
1527 BFD_ASSERT (diff->rel_8o_16o_n_entries == 0);
1528 BFD_ASSERT (diff->local_n_entries == 0);
1529 }
1530
1531 BFD_ASSERT (!elf_m68k_hash_table (info)->allow_multigot_p
1532 || ((big->rel_8o_n_entries
1533 <= ELF_M68K_REL_8O_MAX_N_ENTRIES_IN_GOT (info))
1534 && (big->rel_8o_16o_n_entries
1535 <= ELF_M68K_REL_8O_16O_MAX_N_ENTRIES_IN_GOT (info))));
1536
1537 return TRUE;
1538}
1539
1540struct elf_m68k_finalize_got_offsets_arg
1541{
1542 /* Offset for the next R_68K_GOT8O entry. */
1543 bfd_vma rel_8o_offset;
1544
1545 /* Offset for the next R_68K_GOT16O entry. */
1546 bfd_vma rel_16o_offset;
1547
1548 /* Offset for the next R_68K_GOT32O entry. */
1549 bfd_vma rel_32o_offset;
1550
1551 /* Should we use negative (relative to GP) offsets for GOT entries. */
1552 bfd_boolean use_neg_got_offsets_p;
1553
1554 /* Offset of this GOT relative to .got section. */
1555 bfd_vma got_offset;
1556
1557 /* Mapping from global symndx to global symbols.
1558 This is used to build lists of got entries for global symbols. */
1559 struct elf_m68k_link_hash_entry **symndx2h;
1560};
1561
1562/* Assign ENTRY an offset. Build list of GOT entries for global symbols
1563 along the way. */
1564
1565static int
1566elf_m68k_finalize_got_offsets_1 (void **entry_ptr, void *_arg)
1567{
1568 struct elf_m68k_got_entry *entry;
1569 struct elf_m68k_finalize_got_offsets_arg *arg;
1570
1571 entry = (struct elf_m68k_got_entry *) *entry_ptr;
1572 arg = (struct elf_m68k_finalize_got_offsets_arg *) _arg;
1573
1574 /* This should be a fresh entry created in elf_m68k_can_merge_gots. */
1575 BFD_ASSERT (entry->u.s1.refcount == 0);
1576
1577 switch (entry->u.s1.type)
1578 {
1579 case R_68K_GOT8O:
1580 entry->u.s2.offset = arg->rel_8o_offset;
1581
1582 if (arg->use_neg_got_offsets_p)
1583 {
1584 if (arg->rel_8o_offset >= arg->got_offset)
1585 /* We've assigned a positive offset to this entry,
1586 next entry should get (-abs(offset) - 4). */
1587 arg->rel_8o_offset = (arg->got_offset
1588 - (arg->rel_8o_offset - arg->got_offset)
1589 - 4);
1590 else
1591 /* We've assigned a negative offset to this entry,
1592 next entry should get (+abs(offset) + 0). */
1593 arg->rel_8o_offset = (arg->got_offset
1594 + (arg->got_offset - arg->rel_8o_offset));
1595 }
1596 else
1597 /* Next entry will simply get next offset. */
1598 arg->rel_8o_offset += 4;
1599
1600 break;
1601
1602 case R_68K_GOT16O:
1603 entry->u.s2.offset = arg->rel_16o_offset;
1604
1605 if (arg->use_neg_got_offsets_p)
1606 {
1607 if (arg->rel_16o_offset >= arg->got_offset)
1608 /* We've assigned a positive offset to this entry,
1609 next entry should get (-abs(offset) - 4). */
1610 arg->rel_16o_offset = (arg->got_offset
1611 - (arg->rel_16o_offset - arg->got_offset)
1612 - 4);
1613 else
1614 /* We've assigned a negative offset to this entry,
1615 next entry should get (+abs(offset) + 0). */
1616 arg->rel_16o_offset = (arg->got_offset
1617 + (arg->got_offset - arg->rel_16o_offset));
1618 }
1619 else
1620 /* Next entry will simply get next offset. */
1621 arg->rel_16o_offset += 4;
1622
1623 break;
1624
1625 case R_68K_GOT32O:
1626 entry->u.s2.offset = arg->rel_32o_offset;
1627
1628 if (arg->use_neg_got_offsets_p)
1629 {
1630 if (arg->rel_32o_offset >= arg->got_offset)
1631 /* We've assigned a positive offset to this entry,
1632 next entry should get (-abs(offset) - 4). */
1633 arg->rel_32o_offset = (arg->got_offset
1634 - (arg->rel_32o_offset - arg->got_offset)
1635 - 4);
1636 else
1637 /* We've assigned a negative offset to this entry,
1638 next entry should get (+abs(offset) + 0). */
1639 arg->rel_32o_offset = (arg->got_offset
1640 + (arg->got_offset - arg->rel_32o_offset));
1641 }
1642 else
1643 /* Next entry will simply get next offset. */
1644 arg->rel_32o_offset += 4;
1645
1646 break;
1647
1648 default:
1649 BFD_ASSERT (FALSE);
1650 break;
1651 }
1652
1653 if (entry->key_.bfd == NULL)
1654 /* Hook up this entry into the list of got_entries of H. */
1655 {
1656 struct elf_m68k_link_hash_entry *h;
1657
1658 BFD_ASSERT (entry->key_.symndx != 0);
1659 h = arg->symndx2h[entry->key_.symndx];
1660 BFD_ASSERT (h != NULL);
1661
1662 entry->u.s2.next = h->glist;
1663 h->glist = entry;
1664 }
1665 else
1666 /* This entry is for local symbol. */
1667 entry->u.s2.next = NULL;
1668
1669 return 1;
1670}
1671
1672/* Assign offsets within GOT. USE_NEG_GOT_OFFSETS_P indicates if we
1673 should use negative offsets.
1674 Build list of GOT entries for global symbols along the way.
1675 SYMNDX2H is mapping from global symbol indices to actual
1676 global symbols. */
1677
1678static void
1679elf_m68k_finalize_got_offsets (struct elf_m68k_got *got,
1680 bfd_boolean use_neg_got_offsets_p,
1681 struct elf_m68k_link_hash_entry **symndx2h)
1682{
1683 struct elf_m68k_finalize_got_offsets_arg arg_;
1684
1685 BFD_ASSERT (got->offset != (bfd_vma) -1);
1686
1687 /* We set entry offsets relative to the .got section (and not the
1688 start of a particular GOT), so that we can use them in
1689 finish_dynamic_symbol without needing to know the GOT they come
1690 from. */
1691
1692 if (use_neg_got_offsets_p)
1693 {
1694 size_t n;
1695
1696 /* Put GOT pointer in the middle of GOT. */
1697 n = htab_elements (got->entries);
1698 if ((n & 1) == 0)
1699 /* Even number of GOT entries. */
1700 got->offset += 2 * n;
1701 else
1702 /* Odd number of GOT entries. */
1703 got->offset += 2 * (n - 1);
1704
1705 /* R_68K_GOT8O entries shall start at GOT offset. */
1706 arg_.rel_8o_offset = got->offset;
1707
1708 n = got->rel_8o_n_entries;
1709 if ((n & 1) == 0)
1710 /* Even number of R_68K_GOT8O entries.
1711 The last R_68K_GOT8O entry will be at
1712 (got->offset - 2 * n). Hence the first R_68K_GOT16O
1713 entry will be at offset ... */
1714 arg_.rel_16o_offset = got->offset + 2 * n;
1715 else
1716 /* Odd number of R_68K_GOT8O entries.
1717 The last R_68K_GOT8O entry will be at
1718 (got->offset + 2 * (n - 1)). Hence the first R_68K_GOT16O
1719 entry will be at offset ... */
1720 arg_.rel_16o_offset = got->offset - 2 * (n - 1) - 4;
1721
1722 n = got->rel_8o_16o_n_entries;
1723 if ((n & 1) == 0)
1724 /* Even number of R_68K_GOT8O and R_68K_GOT16O entries.
1725 The last R_68K_GOT8O entry will be at
1726 (got->offset - 2 * n). Hence the first R_68K_GOT32O
1727 entry will be at offset ... */
1728 arg_.rel_32o_offset = got->offset + 2 * n;
1729 else
1730 /* Odd number of R_68K_GOT8O and R_68K_GOT16O entries.
1731 The last R_68K_GOT16O entry will be at
1732 (got->offset + 2 * (n - 1)). Hence the first R_68K_GOT32O
1733 entry will be at offset ... */
1734 arg_.rel_32o_offset = got->offset - 2 * (n - 1) - 4;
1735
1736 arg_.use_neg_got_offsets_p = TRUE;
1737
1738 arg_.got_offset = got->offset;
1739 }
1740 else
1741 {
1742 arg_.rel_8o_offset = got->offset;
1743 arg_.rel_16o_offset = 4 * got->rel_8o_n_entries + got->offset;
1744 arg_.rel_32o_offset = 4 * got->rel_8o_16o_n_entries + got->offset;
1745
1746 arg_.use_neg_got_offsets_p = FALSE;
1747
1748 /* This shouldn't be used. */
1749 arg_.got_offset = (bfd_vma) -1;
1750 }
1751
1752 arg_.symndx2h = symndx2h;
1753
1754 htab_traverse (got->entries, elf_m68k_finalize_got_offsets_1, &arg_);
1755
1756 /* Calculate offset ranges we have actually assigned. */
1757 if (use_neg_got_offsets_p)
1758 {
1759 if (arg_.rel_8o_offset == (bfd_vma) -4
1760 || arg_.rel_8o_offset < got->offset)
1761 arg_.rel_8o_offset = 2 * (got->offset - arg_.rel_8o_offset) - 4;
1762 else
1763 arg_.rel_8o_offset = 2 * (arg_.rel_8o_offset - got->offset);
1764
1765 if (arg_.rel_16o_offset == (bfd_vma) -4
1766 || arg_.rel_16o_offset < got->offset)
1767 arg_.rel_16o_offset = 2 * (got->offset - arg_.rel_16o_offset) - 4;
1768 else
1769 arg_.rel_16o_offset = 2 * (arg_.rel_16o_offset - got->offset);
1770
1771 if (arg_.rel_32o_offset == (bfd_vma) -4
1772 || arg_.rel_32o_offset < got->offset)
1773 arg_.rel_32o_offset = 2 * (got->offset - arg_.rel_32o_offset) - 4;
1774 else
1775 arg_.rel_32o_offset = 2 * (arg_.rel_32o_offset - got->offset);
1776 }
1777 else
1778 {
1779 arg_.rel_8o_offset -= got->offset;
1780 arg_.rel_16o_offset -= got->offset;
1781 arg_.rel_32o_offset -= got->offset;
1782 }
1783
1784 /* These asserts check that we got counting of entries right. */
1785 BFD_ASSERT (arg_.rel_8o_offset == 4 * got->rel_8o_n_entries);
1786 BFD_ASSERT (arg_.rel_16o_offset == 4 * got->rel_8o_16o_n_entries);
1787 BFD_ASSERT (arg_.rel_32o_offset == 4 * htab_elements (got->entries));
1788}
1789
1790struct elf_m68k_partition_multi_got_arg
1791{
1792 /* The GOT we are adding entries to. Aka big got. */
1793 struct elf_m68k_got *current_got;
1794
1795 /* Offset to assign the next CURRENT_GOT. */
1796 bfd_vma offset;
1797
1798 /* Context where memory should be allocated. */
1799 struct bfd_link_info *info;
1800
1801 /* Total number of entries in the .got section.
1802 This is used to calculate size of the .got and .rela.got sections. */
1803 bfd_vma n_entries;
1804
1805 /* Total number of local entries in the .got section.
1806 This is used to calculate size of the .rela.got section. */
1807 bfd_vma local_n_entries;
1808
1809 /* Error flag. */
1810 bfd_boolean error_p;
1811
1812 /* Mapping from global symndx to global symbols.
1813 This is used to build lists of got entries for global symbols. */
1814 struct elf_m68k_link_hash_entry **symndx2h;
1815};
1816
1817/* Process a single BFD2GOT entry and either merge GOT to CURRENT_GOT
1818 or start a new CURRENT_GOT. */
1819
1820static int
1821elf_m68k_partition_multi_got_1 (void **_entry, void *_arg)
1822{
1823 struct elf_m68k_bfd2got_entry *entry;
1824 struct elf_m68k_partition_multi_got_arg *arg;
1825 struct elf_m68k_got *got;
1826 struct elf_m68k_got *current_got;
1827 struct elf_m68k_got diff_;
1828 struct elf_m68k_got *diff;
1829
1830 entry = (struct elf_m68k_bfd2got_entry *) *_entry;
1831 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
1832
1833 got = entry->got;
1834 BFD_ASSERT (got != NULL);
1835 BFD_ASSERT (got->offset == (bfd_vma) -1);
1836
1837 diff = NULL;
1838
1839 if (arg->current_got != NULL)
1840 /* Construct diff. */
1841 {
1842 diff = &diff_;
1843 elf_m68k_init_got (diff, NULL, 0, 0, 0, (bfd_vma) -1);
1844
1845 if (!elf_m68k_can_merge_gots (arg->current_got, got, arg->info, diff))
1846 {
1847 if (diff->offset == 0)
1848 /* Offset set to 0 in the diff_ indicates an error. */
1849 {
1850 arg->error_p = TRUE;
1851 goto final_return;
1852 }
1853
1854 if (elf_m68k_hash_table (arg->info)->allow_multigot_p)
1855 {
1856 elf_m68k_clear_got (diff);
1857 /* Schedule to finish up CURRENT_GOT and start new one. */
1858 diff = NULL;
1859 }
1860 /* else
1861 Merge GOTs no matter what. If big GOT overflows,
1862 we'll fail in relocate_section due to truncated relocations.
1863
1864 ??? May be fail earlier? E.g., in can_merge_gots. */
1865 }
1866 }
1867 else
1868 /* Diff of got against empty current_got is got itself. */
1869 {
1870 /* Create empty CURRENT_GOT to subsequent GOTs to. */
1871 arg->current_got = elf_m68k_create_empty_got (arg->info);
1872 if (arg->current_got == NULL)
1873 {
1874 arg->error_p = TRUE;
1875 goto final_return;
1876 }
1877
1878 arg->current_got->offset = arg->offset;
1879
1880 diff = got;
1881 }
1882
1883 current_got = arg->current_got;
1884
1885 if (diff != NULL)
1886 {
1887 if (!elf_m68k_merge_gots (current_got, diff, arg->info))
1888 {
1889 arg->error_p = TRUE;
1890 goto final_return;
1891 }
1892
1893 /* Now we can free GOT. */
1894 elf_m68k_clear_got (got);
1895
1896 entry->got = current_got;
1897 }
1898 else
1899 {
1900 /* Schedule to start a new current_got. */
1901 arg->current_got = NULL;
1902 arg->offset = (current_got->offset
1903 + 4 * htab_elements (current_got->entries));
1904
1905 /* Finish up current_got. */
1906 {
1907 elf_m68k_finalize_got_offsets (current_got,
1908 elf_m68k_hash_table (arg->info)
1909 ->use_neg_got_offsets_p,
1910 arg->symndx2h);
1911
1912 arg->n_entries += htab_elements (current_got->entries);
1913 arg->local_n_entries += current_got->local_n_entries;
1914
1915 BFD_ASSERT (arg->local_n_entries <= arg->n_entries);
1916 }
1917
1918 /* Retry. */
1919 if (!elf_m68k_partition_multi_got_1 (_entry, _arg))
1920 {
1921 BFD_ASSERT (arg->error_p);
1922 goto final_return;
1923 }
1924 }
1925
1926 final_return:
1927 if (diff != NULL)
1928 elf_m68k_clear_got (diff);
1929
1930 return arg->error_p == FALSE ? 1 : 0;
1931}
1932
1933/* Helper function to build symndx2h mapping. */
1934
1935static bfd_boolean
1936elf_m68k_init_symndx2h_1 (struct elf_link_hash_entry *_h,
1937 void *_arg)
1938{
1939 struct elf_m68k_link_hash_entry *h;
1940
1941 h = elf_m68k_hash_entry (_h);
1942
1943 if (h->got_entry_key != 0)
1944 /* H has at least one entry in the GOT. */
1945 {
1946 struct elf_m68k_partition_multi_got_arg *arg;
1947
1948 arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
1949
1950 BFD_ASSERT (arg->symndx2h[h->got_entry_key] == NULL);
1951 arg->symndx2h[h->got_entry_key] = h;
1952 }
1953
1954 return TRUE;
1955}
1956
1957/* Merge GOTs of some BFDs, assign offsets to GOT entries and build
1958 lists of GOT entries for global symbols.
1959 Calculate sizes of .got and .rela.got sections. */
1960
1961static bfd_boolean
1962elf_m68k_partition_multi_got (struct bfd_link_info *info)
1963{
1964 struct elf_m68k_multi_got *multi_got;
1965 struct elf_m68k_partition_multi_got_arg arg_;
1966
1967 multi_got = elf_m68k_multi_got (info);
1968
1969 arg_.current_got = NULL;
1970 arg_.offset = 0;
1971 arg_.info = info;
1972 arg_.n_entries = 0;
1973 arg_.local_n_entries = 0;
1974 arg_.error_p = FALSE;
1975
1976 if (multi_got->bfd2got != NULL)
1977 {
1978 /* Initialize symndx2h mapping. */
1979 {
1980 arg_.symndx2h = bfd_zmalloc (multi_got->global_symndx
1981 * sizeof (*arg_.symndx2h));
1982 if (arg_.symndx2h == NULL)
1983 return FALSE;
1984
1985 elf_link_hash_traverse (elf_hash_table (info),
1986 elf_m68k_init_symndx2h_1, &arg_);
1987 }
1988
1989 /* Partition. */
1990 htab_traverse (multi_got->bfd2got, elf_m68k_partition_multi_got_1,
1991 &arg_);
1992 if (arg_.error_p)
1993 {
1994 free (arg_.symndx2h);
1995 arg_.symndx2h = NULL;
1996
1997 return FALSE;
1998 }
1999
2000 /* Finish up last current_got. */
2001 {
2002 elf_m68k_finalize_got_offsets (arg_.current_got,
2003 elf_m68k_hash_table (info)
2004 ->use_neg_got_offsets_p, arg_.symndx2h);
2005
2006 arg_.n_entries += htab_elements (arg_.current_got->entries);
2007 arg_.local_n_entries += arg_.current_got->local_n_entries;
2008
2009 BFD_ASSERT (arg_.local_n_entries <= arg_.n_entries);
2010 }
2011
2012 free (arg_.symndx2h);
266abb8f 2013 }
7fb9f789
NC
2014
2015 if (elf_hash_table (info)->dynobj != NULL)
2016 /* Set sizes of .got and .rela.got sections. */
266abb8f 2017 {
7fb9f789 2018 asection *s;
425c6cb0 2019
7fb9f789
NC
2020 s = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got");
2021 if (s != NULL)
2022 s->size = arg_.n_entries * 4;
425c6cb0 2023 else
7fb9f789 2024 BFD_ASSERT (arg_.n_entries == 0);
425c6cb0 2025
7fb9f789
NC
2026 /* If we are generating a shared object, we need to
2027 output a R_68K_RELATIVE reloc so that the dynamic
2028 linker can adjust this GOT entry. Overwise we
2029 don't need space in .rela.got for local symbols. */
2030 if (!info->shared)
2031 {
2032 BFD_ASSERT (arg_.local_n_entries <= arg_.n_entries);
2033 arg_.n_entries -= arg_.local_n_entries;
2034 }
2035
2036 s = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".rela.got");
2037 if (s != NULL)
2038 s->size = arg_.n_entries * sizeof (Elf32_External_Rela);
3bdcfdf4 2039 else
7fb9f789 2040 BFD_ASSERT (arg_.n_entries == 0);
9e1281c7 2041 }
7fb9f789
NC
2042 else
2043 BFD_ASSERT (multi_got->bfd2got == NULL);
9e1281c7 2044
b34976b6 2045 return TRUE;
9e1281c7
CM
2046}
2047
7fb9f789
NC
2048/* Specialized version of elf_m68k_get_got_entry that returns pointer
2049 to hashtable slot, thus allowing removal of entry via
2050 elf_m68k_remove_got_entry. */
2051
2052static struct elf_m68k_got_entry **
2053elf_m68k_find_got_entry_ptr (struct elf_m68k_got *got,
2054 struct elf_m68k_got_entry_key *key)
9e1281c7 2055{
7fb9f789
NC
2056 void **ptr;
2057 struct elf_m68k_got_entry entry_;
2058 struct elf_m68k_got_entry **entry_ptr;
9e1281c7 2059
7fb9f789
NC
2060 entry_.key_ = *key;
2061 ptr = htab_find_slot (got->entries, &entry_, NO_INSERT);
2062 BFD_ASSERT (ptr != NULL);
9e1281c7 2063
7fb9f789 2064 entry_ptr = (struct elf_m68k_got_entry **) ptr;
9e1281c7 2065
7fb9f789
NC
2066 return entry_ptr;
2067}
9e1281c7 2068
7fb9f789 2069/* Remove entry pointed to by ENTRY_PTR from GOT. */
9e1281c7 2070
7fb9f789
NC
2071static void
2072elf_m68k_remove_got_entry (struct elf_m68k_got *got,
2073 struct elf_m68k_got_entry **entry_ptr)
2074{
2075 struct elf_m68k_got_entry *entry;
2076
2077 entry = *entry_ptr;
2078
2079 /* Check that offsets have not been finalized yet. */
2080 BFD_ASSERT (got->offset == (bfd_vma) -1);
2081 /* Check that this entry is indeed unused. */
2082 BFD_ASSERT (entry->u.s1.refcount == 0);
2083
2084 elf_m68k_remove_got_entry_type (got, entry->u.s1.type);
2085
2086 if (entry->key_.bfd != NULL)
2087 --got->local_n_entries;
2088
2089 htab_clear_slot (got->entries, (void **) entry_ptr);
2090}
2091
2092/* Copy any information related to dynamic linking from a pre-existing
2093 symbol to a newly created symbol. Also called to copy flags and
2094 other back-end info to a weakdef, in which case the symbol is not
2095 newly created and plt/got refcounts and dynamic indices should not
2096 be copied. */
2097
2098static void
2099elf_m68k_copy_indirect_symbol (struct bfd_link_info *info,
2100 struct elf_link_hash_entry *_dir,
2101 struct elf_link_hash_entry *_ind)
2102{
2103 struct elf_m68k_link_hash_entry *dir;
2104 struct elf_m68k_link_hash_entry *ind;
2105
2106 _bfd_elf_link_hash_copy_indirect (info, _dir, _ind);
2107
2108 if (_ind->root.type != bfd_link_hash_indirect)
2109 return;
2110
2111 dir = elf_m68k_hash_entry (_dir);
2112 ind = elf_m68k_hash_entry (_ind);
2113
2114 /* We might have a direct symbol already having entries in the GOTs.
2115 Update its key only in case indirect symbol has GOT entries and
2116 assert that both indirect and direct symbols don't have GOT entries
2117 at the same time. */
2118 if (ind->got_entry_key != 0)
266abb8f 2119 {
7fb9f789
NC
2120 BFD_ASSERT (dir->got_entry_key == 0);
2121 /* Assert that GOTs aren't partioned yet. */
2122 BFD_ASSERT (ind->glist == NULL);
425c6cb0 2123
7fb9f789
NC
2124 dir->got_entry_key = ind->got_entry_key;
2125 ind->got_entry_key = 0;
266abb8f 2126 }
9e1281c7 2127}
7fb9f789 2128
252b5132
RH
2129/* Look through the relocs for a section during the first phase, and
2130 allocate space in the global offset table or procedure linkage
2131 table. */
2132
b34976b6 2133static bfd_boolean
252b5132
RH
2134elf_m68k_check_relocs (abfd, info, sec, relocs)
2135 bfd *abfd;
2136 struct bfd_link_info *info;
2137 asection *sec;
2138 const Elf_Internal_Rela *relocs;
2139{
2140 bfd *dynobj;
2141 Elf_Internal_Shdr *symtab_hdr;
2142 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
2143 const Elf_Internal_Rela *rel;
2144 const Elf_Internal_Rela *rel_end;
2145 asection *sgot;
2146 asection *srelgot;
2147 asection *sreloc;
7fb9f789 2148 struct elf_m68k_got *got;
252b5132 2149
1049f94e 2150 if (info->relocatable)
b34976b6 2151 return TRUE;
252b5132
RH
2152
2153 dynobj = elf_hash_table (info)->dynobj;
2154 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2155 sym_hashes = elf_sym_hashes (abfd);
252b5132
RH
2156
2157 sgot = NULL;
2158 srelgot = NULL;
2159 sreloc = NULL;
2160
7fb9f789
NC
2161 got = NULL;
2162
252b5132
RH
2163 rel_end = relocs + sec->reloc_count;
2164 for (rel = relocs; rel < rel_end; rel++)
2165 {
2166 unsigned long r_symndx;
2167 struct elf_link_hash_entry *h;
2168
2169 r_symndx = ELF32_R_SYM (rel->r_info);
2170
2171 if (r_symndx < symtab_hdr->sh_info)
2172 h = NULL;
2173 else
973a3492
L
2174 {
2175 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2176 while (h->root.type == bfd_link_hash_indirect
2177 || h->root.type == bfd_link_hash_warning)
2178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2179 }
252b5132
RH
2180
2181 switch (ELF32_R_TYPE (rel->r_info))
2182 {
2183 case R_68K_GOT8:
2184 case R_68K_GOT16:
2185 case R_68K_GOT32:
2186 if (h != NULL
2187 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2188 break;
2189 /* Fall through. */
2190 case R_68K_GOT8O:
2191 case R_68K_GOT16O:
2192 case R_68K_GOT32O:
2193 /* This symbol requires a global offset table entry. */
2194
2195 if (dynobj == NULL)
2196 {
2197 /* Create the .got section. */
2198 elf_hash_table (info)->dynobj = dynobj = abfd;
2199 if (!_bfd_elf_create_got_section (dynobj, info))
b34976b6 2200 return FALSE;
252b5132
RH
2201 }
2202
2203 if (sgot == NULL)
2204 {
2205 sgot = bfd_get_section_by_name (dynobj, ".got");
2206 BFD_ASSERT (sgot != NULL);
2207 }
2208
2209 if (srelgot == NULL
2210 && (h != NULL || info->shared))
2211 {
2212 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2213 if (srelgot == NULL)
2214 {
3496cb2a
L
2215 srelgot = bfd_make_section_with_flags (dynobj,
2216 ".rela.got",
2217 (SEC_ALLOC
2218 | SEC_LOAD
2219 | SEC_HAS_CONTENTS
2220 | SEC_IN_MEMORY
2221 | SEC_LINKER_CREATED
2222 | SEC_READONLY));
252b5132 2223 if (srelgot == NULL
252b5132 2224 || !bfd_set_section_alignment (dynobj, srelgot, 2))
b34976b6 2225 return FALSE;
252b5132
RH
2226 }
2227 }
2228
7fb9f789 2229 if (got == NULL)
252b5132 2230 {
7fb9f789 2231 struct elf_m68k_bfd2got_entry *bfd2got_entry;
252b5132 2232
7fb9f789
NC
2233 bfd2got_entry
2234 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2235 abfd, FIND_OR_CREATE, info);
2236 if (bfd2got_entry == NULL)
2237 return FALSE;
252b5132 2238
7fb9f789
NC
2239 got = bfd2got_entry->got;
2240 BFD_ASSERT (got != NULL);
252b5132 2241 }
7fb9f789
NC
2242
2243 {
2244 struct elf_m68k_got_entry *got_entry;
2245
2246 /* Add entry to got. */
2247 got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
2248 ELF32_R_TYPE (rel->r_info),
2249 r_symndx, info);
2250 if (got_entry == NULL)
2251 return FALSE;
2252
2253 if (got_entry->u.s1.refcount == 1)
2254 {
2255 /* Make sure this symbol is output as a dynamic symbol. */
2256 if (h != NULL
2257 && h->dynindx == -1
2258 && !h->forced_local)
2259 {
2260 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2261 return FALSE;
2262 }
2263
2264 /* Allocate space in the .got section. */
2265 sgot->size += 4;
2266
2267 /* Allocate relocation space. */
2268 if (h != NULL
2269 || info->shared)
2270 srelgot->size += sizeof (Elf32_External_Rela);
2271 }
2272 }
2273
252b5132
RH
2274 break;
2275
2276 case R_68K_PLT8:
2277 case R_68K_PLT16:
2278 case R_68K_PLT32:
2279 /* This symbol requires a procedure linkage table entry. We
2280 actually build the entry in adjust_dynamic_symbol,
2281 because this might be a case of linking PIC code which is
2282 never referenced by a dynamic object, in which case we
2283 don't need to generate a procedure linkage table entry
2284 after all. */
2285
2286 /* If this is a local symbol, we resolve it directly without
2287 creating a procedure linkage table entry. */
2288 if (h == NULL)
2289 continue;
2290
f5385ebf 2291 h->needs_plt = 1;
51b64d56 2292 h->plt.refcount++;
252b5132
RH
2293 break;
2294
2295 case R_68K_PLT8O:
2296 case R_68K_PLT16O:
2297 case R_68K_PLT32O:
2298 /* This symbol requires a procedure linkage table entry. */
2299
2300 if (h == NULL)
2301 {
2302 /* It does not make sense to have this relocation for a
2303 local symbol. FIXME: does it? How to handle it if
2304 it does make sense? */
2305 bfd_set_error (bfd_error_bad_value);
b34976b6 2306 return FALSE;
252b5132
RH
2307 }
2308
2309 /* Make sure this symbol is output as a dynamic symbol. */
b6152c34 2310 if (h->dynindx == -1
f5385ebf 2311 && !h->forced_local)
252b5132 2312 {
c152c796 2313 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2314 return FALSE;
252b5132
RH
2315 }
2316
f5385ebf 2317 h->needs_plt = 1;
51b64d56 2318 h->plt.refcount++;
252b5132
RH
2319 break;
2320
2321 case R_68K_PC8:
2322 case R_68K_PC16:
2323 case R_68K_PC32:
2324 /* If we are creating a shared library and this is not a local
2325 symbol, we need to copy the reloc into the shared library.
2326 However when linking with -Bsymbolic and this is a global
2327 symbol which is defined in an object we are including in the
2328 link (i.e., DEF_REGULAR is set), then we can resolve the
2329 reloc directly. At this point we have not seen all the input
2330 files, so it is possible that DEF_REGULAR is not set now but
2331 will be set later (it is never cleared). We account for that
2332 possibility below by storing information in the
2333 pcrel_relocs_copied field of the hash table entry. */
2334 if (!(info->shared
2335 && (sec->flags & SEC_ALLOC) != 0
2336 && h != NULL
2337 && (!info->symbolic
b6152c34 2338 || h->root.type == bfd_link_hash_defweak
f5385ebf 2339 || !h->def_regular)))
252b5132
RH
2340 {
2341 if (h != NULL)
2342 {
2343 /* Make sure a plt entry is created for this symbol if
2344 it turns out to be a function defined by a dynamic
2345 object. */
51b64d56 2346 h->plt.refcount++;
252b5132
RH
2347 }
2348 break;
2349 }
2350 /* Fall through. */
2351 case R_68K_8:
2352 case R_68K_16:
2353 case R_68K_32:
2354 if (h != NULL)
2355 {
2356 /* Make sure a plt entry is created for this symbol if it
2357 turns out to be a function defined by a dynamic object. */
51b64d56 2358 h->plt.refcount++;
252b5132
RH
2359 }
2360
2361 /* If we are creating a shared library, we need to copy the
2362 reloc into the shared library. */
2363 if (info->shared
2364 && (sec->flags & SEC_ALLOC) != 0)
2365 {
2366 /* When creating a shared object, we must copy these
2367 reloc types into the output file. We create a reloc
2368 section in dynobj and make room for this reloc. */
2369 if (sreloc == NULL)
2370 {
2371 const char *name;
2372
2373 name = (bfd_elf_string_from_elf_section
2374 (abfd,
2375 elf_elfheader (abfd)->e_shstrndx,
2376 elf_section_data (sec)->rel_hdr.sh_name));
2377 if (name == NULL)
b34976b6 2378 return FALSE;
252b5132 2379
0112cd26 2380 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
252b5132
RH
2381 && strcmp (bfd_get_section_name (abfd, sec),
2382 name + 5) == 0);
2383
2384 sreloc = bfd_get_section_by_name (dynobj, name);
2385 if (sreloc == NULL)
2386 {
3496cb2a
L
2387 sreloc = bfd_make_section_with_flags (dynobj,
2388 name,
2389 (SEC_ALLOC
2390 | SEC_LOAD
2391 | SEC_HAS_CONTENTS
2392 | SEC_IN_MEMORY
2393 | SEC_LINKER_CREATED
2394 | SEC_READONLY));
252b5132 2395 if (sreloc == NULL
252b5132 2396 || !bfd_set_section_alignment (dynobj, sreloc, 2))
b34976b6 2397 return FALSE;
252b5132 2398 }
d2ff124f 2399 elf_section_data (sec)->sreloc = sreloc;
252b5132
RH
2400 }
2401
3e829b4a
AS
2402 if (sec->flags & SEC_READONLY
2403 /* Don't set DF_TEXTREL yet for PC relative
2404 relocations, they might be discarded later. */
2405 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2406 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2407 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
2408 info->flags |= DF_TEXTREL;
2409
eea6121a 2410 sreloc->size += sizeof (Elf32_External_Rela);
252b5132 2411
b6152c34
AS
2412 /* We count the number of PC relative relocations we have
2413 entered for this symbol, so that we can discard them
2414 again if, in the -Bsymbolic case, the symbol is later
2415 defined by a regular object, or, in the normal shared
2416 case, the symbol is forced to be local. Note that this
2417 function is only called if we are using an m68kelf linker
2418 hash table, which means that h is really a pointer to an
252b5132 2419 elf_m68k_link_hash_entry. */
b6152c34
AS
2420 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2421 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2422 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
252b5132 2423 {
252b5132 2424 struct elf_m68k_pcrel_relocs_copied *p;
b6152c34
AS
2425 struct elf_m68k_pcrel_relocs_copied **head;
2426
2427 if (h != NULL)
2428 {
2429 struct elf_m68k_link_hash_entry *eh
0cca5f05 2430 = elf_m68k_hash_entry (h);
b6152c34
AS
2431 head = &eh->pcrel_relocs_copied;
2432 }
2433 else
2434 {
2435 asection *s;
6edfbbad
DJ
2436 void *vpp;
2437
b6152c34
AS
2438 s = (bfd_section_from_r_symndx
2439 (abfd, &elf_m68k_hash_table (info)->sym_sec,
2440 sec, r_symndx));
2441 if (s == NULL)
2442 return FALSE;
252b5132 2443
6edfbbad
DJ
2444 vpp = &elf_section_data (s)->local_dynrel;
2445 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
b6152c34 2446 }
252b5132 2447
b6152c34 2448 for (p = *head; p != NULL; p = p->next)
252b5132
RH
2449 if (p->section == sreloc)
2450 break;
2451
2452 if (p == NULL)
2453 {
2454 p = ((struct elf_m68k_pcrel_relocs_copied *)
dc810e39 2455 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
252b5132 2456 if (p == NULL)
b34976b6 2457 return FALSE;
b6152c34
AS
2458 p->next = *head;
2459 *head = p;
252b5132
RH
2460 p->section = sreloc;
2461 p->count = 0;
2462 }
2463
2464 ++p->count;
2465 }
2466 }
2467
2468 break;
2469
2470 /* This relocation describes the C++ object vtable hierarchy.
2471 Reconstruct it for later use during GC. */
2472 case R_68K_GNU_VTINHERIT:
c152c796 2473 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 2474 return FALSE;
252b5132
RH
2475 break;
2476
2477 /* This relocation describes which C++ vtable entries are actually
2478 used. Record for later use during GC. */
2479 case R_68K_GNU_VTENTRY:
d17e0c6e
JB
2480 BFD_ASSERT (h != NULL);
2481 if (h != NULL
2482 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 2483 return FALSE;
252b5132
RH
2484 break;
2485
2486 default:
2487 break;
2488 }
2489 }
2490
b34976b6 2491 return TRUE;
252b5132
RH
2492}
2493
2494/* Return the section that should be marked against GC for a given
2495 relocation. */
2496
2497static asection *
07adf181
AM
2498elf_m68k_gc_mark_hook (asection *sec,
2499 struct bfd_link_info *info,
2500 Elf_Internal_Rela *rel,
2501 struct elf_link_hash_entry *h,
2502 Elf_Internal_Sym *sym)
252b5132
RH
2503{
2504 if (h != NULL)
07adf181
AM
2505 switch (ELF32_R_TYPE (rel->r_info))
2506 {
2507 case R_68K_GNU_VTINHERIT:
2508 case R_68K_GNU_VTENTRY:
2509 return NULL;
2510 }
2511
2512 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
2513}
2514
2515/* Update the got entry reference counts for the section being removed. */
2516
b34976b6 2517static bfd_boolean
07adf181
AM
2518elf_m68k_gc_sweep_hook (bfd *abfd,
2519 struct bfd_link_info *info,
2520 asection *sec,
2521 const Elf_Internal_Rela *relocs)
252b5132
RH
2522{
2523 Elf_Internal_Shdr *symtab_hdr;
2524 struct elf_link_hash_entry **sym_hashes;
252b5132 2525 const Elf_Internal_Rela *rel, *relend;
252b5132 2526 bfd *dynobj;
dd5724d5
AM
2527 asection *sgot;
2528 asection *srelgot;
7fb9f789 2529 struct elf_m68k_got *got;
252b5132 2530
7dda2462
TG
2531 if (info->relocatable)
2532 return TRUE;
2533
252b5132 2534 dynobj = elf_hash_table (info)->dynobj;
dd5724d5 2535 if (dynobj == NULL)
b34976b6 2536 return TRUE;
dd5724d5 2537
4f075348
KH
2538 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2539 sym_hashes = elf_sym_hashes (abfd);
4f075348 2540
dd5724d5
AM
2541 sgot = bfd_get_section_by_name (dynobj, ".got");
2542 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
7fb9f789 2543 got = NULL;
252b5132
RH
2544
2545 relend = relocs + sec->reloc_count;
2546 for (rel = relocs; rel < relend; rel++)
2547 {
4f075348 2548 unsigned long r_symndx;
3eb128b2
AM
2549 struct elf_link_hash_entry *h = NULL;
2550
2551 r_symndx = ELF32_R_SYM (rel->r_info);
2552 if (r_symndx >= symtab_hdr->sh_info)
2553 {
2554 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2555 while (h->root.type == bfd_link_hash_indirect
2556 || h->root.type == bfd_link_hash_warning)
2557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2558 }
4f075348 2559
252b5132
RH
2560 switch (ELF32_R_TYPE (rel->r_info))
2561 {
2562 case R_68K_GOT8:
2563 case R_68K_GOT16:
2564 case R_68K_GOT32:
7fb9f789
NC
2565 if (h != NULL
2566 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2567 break;
2568
2569 /* FALLTHRU */
252b5132
RH
2570 case R_68K_GOT8O:
2571 case R_68K_GOT16O:
2572 case R_68K_GOT32O:
7fb9f789 2573 if (got == NULL)
252b5132 2574 {
7fb9f789
NC
2575 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2576 abfd, MUST_FIND, NULL)->got;
2577 BFD_ASSERT (got != NULL);
252b5132 2578 }
7fb9f789
NC
2579
2580 {
2581 struct elf_m68k_got_entry_key key_;
2582 struct elf_m68k_got_entry **got_entry_ptr;
2583 struct elf_m68k_got_entry *got_entry;
2584
2585 elf_m68k_init_got_entry_key (&key_, h, abfd, r_symndx);
2586 got_entry_ptr = elf_m68k_find_got_entry_ptr (got, &key_);
2587
2588 got_entry = *got_entry_ptr;
2589
2590 if (got_entry->u.s1.refcount > 0)
2591 {
2592 --got_entry->u.s1.refcount;
2593
2594 if (got_entry->u.s1.refcount == 0)
2595 /* We don't need the .got entry any more. */
2596 elf_m68k_remove_got_entry (got, got_entry_ptr);
2597 }
2598 }
252b5132
RH
2599 break;
2600
2601 case R_68K_PLT8:
2602 case R_68K_PLT16:
2603 case R_68K_PLT32:
2604 case R_68K_PLT8O:
2605 case R_68K_PLT16O:
2606 case R_68K_PLT32O:
2607 case R_68K_PC8:
2608 case R_68K_PC16:
2609 case R_68K_PC32:
2610 case R_68K_8:
2611 case R_68K_16:
2612 case R_68K_32:
3eb128b2 2613 if (h != NULL)
252b5132 2614 {
252b5132
RH
2615 if (h->plt.refcount > 0)
2616 --h->plt.refcount;
2617 }
2618 break;
2619
2620 default:
2621 break;
2622 }
2623 }
2624
b34976b6 2625 return TRUE;
252b5132 2626}
cc3e26be
RS
2627\f
2628/* Return the type of PLT associated with OUTPUT_BFD. */
2629
2630static const struct elf_m68k_plt_info *
2631elf_m68k_get_plt_info (bfd *output_bfd)
2632{
2633 unsigned int features;
2634
2635 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
2636 if (features & cpu32)
2637 return &elf_cpu32_plt_info;
2638 if (features & mcfisa_b)
2639 return &elf_isab_plt_info;
9a2e615a
NS
2640 if (features & mcfisa_c)
2641 return &elf_isac_plt_info;
cc3e26be
RS
2642 return &elf_m68k_plt_info;
2643}
2644
2645/* This function is called after all the input files have been read,
2646 and the input sections have been assigned to output sections.
2647 It's a convenient place to determine the PLT style. */
2648
2649static bfd_boolean
2650elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2651{
7fb9f789
NC
2652 /* Bind input BFDs to GOTs and calculate sizes of .got and .rela.got
2653 sections. */
2654 if (!elf_m68k_partition_multi_got (info))
2655 return FALSE;
2656
cc3e26be
RS
2657 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
2658 return TRUE;
2659}
252b5132 2660
252b5132
RH
2661/* Adjust a symbol defined by a dynamic object and referenced by a
2662 regular object. The current definition is in some section of the
2663 dynamic object, but we're not including those sections. We have to
2664 change the definition to something the rest of the link can
2665 understand. */
2666
b34976b6 2667static bfd_boolean
252b5132
RH
2668elf_m68k_adjust_dynamic_symbol (info, h)
2669 struct bfd_link_info *info;
2670 struct elf_link_hash_entry *h;
2671{
cc3e26be 2672 struct elf_m68k_link_hash_table *htab;
252b5132
RH
2673 bfd *dynobj;
2674 asection *s;
252b5132 2675
cc3e26be 2676 htab = elf_m68k_hash_table (info);
252b5132
RH
2677 dynobj = elf_hash_table (info)->dynobj;
2678
2679 /* Make sure we know what is going on here. */
2680 BFD_ASSERT (dynobj != NULL
f5385ebf 2681 && (h->needs_plt
f6e332e6 2682 || h->u.weakdef != NULL
f5385ebf
AM
2683 || (h->def_dynamic
2684 && h->ref_regular
2685 && !h->def_regular)));
252b5132
RH
2686
2687 /* If this is a function, put it in the procedure linkage table. We
2688 will fill in the contents of the procedure linkage table later,
2689 when we know the address of the .got section. */
2690 if (h->type == STT_FUNC
f5385ebf 2691 || h->needs_plt)
252b5132 2692 {
9dfe8738
AS
2693 if ((h->plt.refcount <= 0
2694 || SYMBOL_CALLS_LOCAL (info, h)
2695 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2696 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
2697 /* We must always create the plt entry if it was referenced
2698 by a PLTxxO relocation. In this case we already recorded
2699 it as a dynamic symbol. */
2700 && h->dynindx == -1)
2701 {
2702 /* This case can occur if we saw a PLTxx reloc in an input
2703 file, but the symbol was never referred to by a dynamic
9dfe8738
AS
2704 object, or if all references were garbage collected. In
2705 such a case, we don't actually need to build a procedure
2706 linkage table, and we can just do a PCxx reloc instead. */
252b5132 2707 h->plt.offset = (bfd_vma) -1;
f5385ebf 2708 h->needs_plt = 0;
b34976b6 2709 return TRUE;
252b5132
RH
2710 }
2711
2712 /* Make sure this symbol is output as a dynamic symbol. */
b6152c34 2713 if (h->dynindx == -1
f5385ebf 2714 && !h->forced_local)
252b5132 2715 {
c152c796 2716 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2717 return FALSE;
252b5132
RH
2718 }
2719
2720 s = bfd_get_section_by_name (dynobj, ".plt");
2721 BFD_ASSERT (s != NULL);
2722
2723 /* If this is the first .plt entry, make room for the special
2724 first entry. */
eea6121a 2725 if (s->size == 0)
cc3e26be 2726 s->size = htab->plt_info->size;
252b5132
RH
2727
2728 /* If this symbol is not defined in a regular file, and we are
2729 not generating a shared library, then set the symbol to this
2730 location in the .plt. This is required to make function
2731 pointers compare as equal between the normal executable and
2732 the shared library. */
2733 if (!info->shared
f5385ebf 2734 && !h->def_regular)
252b5132
RH
2735 {
2736 h->root.u.def.section = s;
eea6121a 2737 h->root.u.def.value = s->size;
252b5132
RH
2738 }
2739
eea6121a 2740 h->plt.offset = s->size;
252b5132
RH
2741
2742 /* Make room for this entry. */
cc3e26be 2743 s->size += htab->plt_info->size;
252b5132
RH
2744
2745 /* We also need to make an entry in the .got.plt section, which
2746 will be placed in the .got section by the linker script. */
252b5132
RH
2747 s = bfd_get_section_by_name (dynobj, ".got.plt");
2748 BFD_ASSERT (s != NULL);
eea6121a 2749 s->size += 4;
252b5132
RH
2750
2751 /* We also need to make an entry in the .rela.plt section. */
252b5132
RH
2752 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2753 BFD_ASSERT (s != NULL);
eea6121a 2754 s->size += sizeof (Elf32_External_Rela);
252b5132 2755
b34976b6 2756 return TRUE;
252b5132
RH
2757 }
2758
2759 /* Reinitialize the plt offset now that it is not used as a reference
2760 count any more. */
2761 h->plt.offset = (bfd_vma) -1;
2762
2763 /* If this is a weak symbol, and there is a real definition, the
2764 processor independent code will have arranged for us to see the
2765 real definition first, and we can just use the same value. */
f6e332e6 2766 if (h->u.weakdef != NULL)
252b5132 2767 {
f6e332e6
AM
2768 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2769 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2770 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2771 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 2772 return TRUE;
252b5132
RH
2773 }
2774
2775 /* This is a reference to a symbol defined by a dynamic object which
2776 is not a function. */
2777
2778 /* If we are creating a shared library, we must presume that the
2779 only references to the symbol are via the global offset table.
2780 For such cases we need not do anything here; the relocations will
2781 be handled correctly by relocate_section. */
2782 if (info->shared)
b34976b6 2783 return TRUE;
252b5132 2784
909272ee
AM
2785 if (h->size == 0)
2786 {
2787 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2788 h->root.root.string);
2789 return TRUE;
2790 }
2791
252b5132
RH
2792 /* We must allocate the symbol in our .dynbss section, which will
2793 become part of the .bss section of the executable. There will be
2794 an entry for this symbol in the .dynsym section. The dynamic
2795 object will contain position independent code, so all references
2796 from the dynamic object to this symbol will go through the global
2797 offset table. The dynamic linker will use the .dynsym entry to
2798 determine the address it must put in the global offset table, so
2799 both the dynamic object and the regular object will refer to the
2800 same memory location for the variable. */
2801
2802 s = bfd_get_section_by_name (dynobj, ".dynbss");
2803 BFD_ASSERT (s != NULL);
2804
2805 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
2806 copy the initial value out of the dynamic object and into the
2807 runtime process image. We need to remember the offset into the
2808 .rela.bss section we are going to use. */
2809 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2810 {
2811 asection *srel;
2812
2813 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2814 BFD_ASSERT (srel != NULL);
eea6121a 2815 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 2816 h->needs_copy = 1;
252b5132
RH
2817 }
2818
027297b7 2819 return _bfd_elf_adjust_dynamic_copy (h, s);
252b5132
RH
2820}
2821
2822/* Set the sizes of the dynamic sections. */
2823
b34976b6 2824static bfd_boolean
252b5132 2825elf_m68k_size_dynamic_sections (output_bfd, info)
aa91b392 2826 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2827 struct bfd_link_info *info;
2828{
2829 bfd *dynobj;
2830 asection *s;
b34976b6
AM
2831 bfd_boolean plt;
2832 bfd_boolean relocs;
252b5132
RH
2833
2834 dynobj = elf_hash_table (info)->dynobj;
2835 BFD_ASSERT (dynobj != NULL);
2836
2837 if (elf_hash_table (info)->dynamic_sections_created)
2838 {
2839 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 2840 if (info->executable)
252b5132
RH
2841 {
2842 s = bfd_get_section_by_name (dynobj, ".interp");
2843 BFD_ASSERT (s != NULL);
eea6121a 2844 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
252b5132
RH
2845 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2846 }
2847 }
2848 else
2849 {
2850 /* We may have created entries in the .rela.got section.
2851 However, if we are not creating the dynamic sections, we will
2852 not actually use these entries. Reset the size of .rela.got,
2853 which will cause it to get stripped from the output file
2854 below. */
2855 s = bfd_get_section_by_name (dynobj, ".rela.got");
2856 if (s != NULL)
eea6121a 2857 s->size = 0;
252b5132
RH
2858 }
2859
b6152c34
AS
2860 /* If this is a -Bsymbolic shared link, then we need to discard all
2861 PC relative relocs against symbols defined in a regular object.
2862 For the normal shared case we discard the PC relative relocs
2863 against symbols that have become local due to visibility changes.
2864 We allocated space for them in the check_relocs routine, but we
2865 will not fill them in in the relocate_section routine. */
2866 if (info->shared)
0cca5f05
AS
2867 elf_link_hash_traverse (elf_hash_table (info),
2868 elf_m68k_discard_copies,
2869 (PTR) info);
252b5132
RH
2870
2871 /* The check_relocs and adjust_dynamic_symbol entry points have
2872 determined the sizes of the various dynamic sections. Allocate
2873 memory for them. */
b34976b6
AM
2874 plt = FALSE;
2875 relocs = FALSE;
252b5132
RH
2876 for (s = dynobj->sections; s != NULL; s = s->next)
2877 {
2878 const char *name;
252b5132
RH
2879
2880 if ((s->flags & SEC_LINKER_CREATED) == 0)
2881 continue;
2882
2883 /* It's OK to base decisions on the section name, because none
2884 of the dynobj section names depend upon the input files. */
2885 name = bfd_get_section_name (dynobj, s);
2886
252b5132
RH
2887 if (strcmp (name, ".plt") == 0)
2888 {
c456f082
AM
2889 /* Remember whether there is a PLT. */
2890 plt = s->size != 0;
252b5132 2891 }
0112cd26 2892 else if (CONST_STRNEQ (name, ".rela"))
252b5132 2893 {
c456f082 2894 if (s->size != 0)
252b5132 2895 {
b34976b6 2896 relocs = TRUE;
252b5132
RH
2897
2898 /* We use the reloc_count field as a counter if we need
2899 to copy relocs into the output file. */
2900 s->reloc_count = 0;
2901 }
2902 }
0112cd26 2903 else if (! CONST_STRNEQ (name, ".got")
c456f082 2904 && strcmp (name, ".dynbss") != 0)
252b5132
RH
2905 {
2906 /* It's not one of our sections, so don't allocate space. */
2907 continue;
2908 }
2909
c456f082 2910 if (s->size == 0)
252b5132 2911 {
c456f082
AM
2912 /* If we don't need this section, strip it from the
2913 output file. This is mostly to handle .rela.bss and
2914 .rela.plt. We must create both sections in
2915 create_dynamic_sections, because they must be created
2916 before the linker maps input sections to output
2917 sections. The linker does that before
2918 adjust_dynamic_symbol is called, and it is that
2919 function which decides whether anything needs to go
2920 into these sections. */
8423293d 2921 s->flags |= SEC_EXCLUDE;
252b5132
RH
2922 continue;
2923 }
2924
c456f082
AM
2925 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2926 continue;
2927
252b5132 2928 /* Allocate memory for the section contents. */
7a9af8c4
NC
2929 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
2930 Unused entries should be reclaimed before the section's contents
2931 are written out, but at the moment this does not happen. Thus in
2932 order to prevent writing out garbage, we initialise the section's
2933 contents to zero. */
eea6121a 2934 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c456f082 2935 if (s->contents == NULL)
b34976b6 2936 return FALSE;
252b5132
RH
2937 }
2938
2939 if (elf_hash_table (info)->dynamic_sections_created)
2940 {
2941 /* Add some entries to the .dynamic section. We fill in the
2942 values later, in elf_m68k_finish_dynamic_sections, but we
2943 must add the entries now so that we get the correct size for
2944 the .dynamic section. The DT_DEBUG entry is filled in by the
2945 dynamic linker and used by the debugger. */
dc810e39 2946#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2947 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2948
252b5132
RH
2949 if (!info->shared)
2950 {
dc810e39 2951 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2952 return FALSE;
252b5132
RH
2953 }
2954
2955 if (plt)
2956 {
dc810e39
AM
2957 if (!add_dynamic_entry (DT_PLTGOT, 0)
2958 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2959 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2960 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2961 return FALSE;
252b5132
RH
2962 }
2963
2964 if (relocs)
2965 {
dc810e39
AM
2966 if (!add_dynamic_entry (DT_RELA, 0)
2967 || !add_dynamic_entry (DT_RELASZ, 0)
2968 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
b34976b6 2969 return FALSE;
252b5132
RH
2970 }
2971
aa91b392 2972 if ((info->flags & DF_TEXTREL) != 0)
252b5132 2973 {
dc810e39 2974 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2975 return FALSE;
252b5132
RH
2976 }
2977 }
dc810e39 2978#undef add_dynamic_entry
252b5132 2979
b34976b6 2980 return TRUE;
252b5132
RH
2981}
2982
0cca5f05 2983/* This function is called via elf_link_hash_traverse if we are
b6152c34
AS
2984 creating a shared object. In the -Bsymbolic case it discards the
2985 space allocated to copy PC relative relocs against symbols which
3e829b4a 2986 are defined in regular objects. For the normal shared case, it
b6152c34
AS
2987 discards space for pc-relative relocs that have become local due to
2988 symbol visibility changes. We allocated space for them in the
2989 check_relocs routine, but we won't fill them in in the
3e829b4a
AS
2990 relocate_section routine.
2991
2992 We also check whether any of the remaining relocations apply
2993 against a readonly section, and set the DF_TEXTREL flag in this
2994 case. */
252b5132 2995
b34976b6 2996static bfd_boolean
b6152c34 2997elf_m68k_discard_copies (h, inf)
0cca5f05 2998 struct elf_link_hash_entry *h;
b6152c34 2999 PTR inf;
252b5132 3000{
b6152c34 3001 struct bfd_link_info *info = (struct bfd_link_info *) inf;
252b5132
RH
3002 struct elf_m68k_pcrel_relocs_copied *s;
3003
0cca5f05
AS
3004 if (h->root.type == bfd_link_hash_warning)
3005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e92d460e 3006
f5385ebf 3007 if (!h->def_regular
b6152c34 3008 || (!info->symbolic
f5385ebf 3009 && !h->forced_local))
3e829b4a
AS
3010 {
3011 if ((info->flags & DF_TEXTREL) == 0)
3012 {
3013 /* Look for relocations against read-only sections. */
0cca5f05
AS
3014 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3015 s != NULL;
3016 s = s->next)
3e829b4a
AS
3017 if ((s->section->flags & SEC_READONLY) != 0)
3018 {
3019 info->flags |= DF_TEXTREL;
3020 break;
3021 }
3022 }
0cca5f05 3023
3e829b4a
AS
3024 return TRUE;
3025 }
252b5132 3026
0cca5f05
AS
3027 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3028 s != NULL;
3029 s = s->next)
eea6121a 3030 s->section->size -= s->count * sizeof (Elf32_External_Rela);
252b5132 3031
b34976b6 3032 return TRUE;
252b5132
RH
3033}
3034
3035/* Relocate an M68K ELF section. */
3036
b34976b6 3037static bfd_boolean
252b5132
RH
3038elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
3039 contents, relocs, local_syms, local_sections)
3040 bfd *output_bfd;
3041 struct bfd_link_info *info;
3042 bfd *input_bfd;
3043 asection *input_section;
3044 bfd_byte *contents;
3045 Elf_Internal_Rela *relocs;
3046 Elf_Internal_Sym *local_syms;
3047 asection **local_sections;
3048{
3049 bfd *dynobj;
3050 Elf_Internal_Shdr *symtab_hdr;
3051 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
3052 asection *sgot;
3053 asection *splt;
3054 asection *sreloc;
7fb9f789 3055 struct elf_m68k_got *got;
252b5132
RH
3056 Elf_Internal_Rela *rel;
3057 Elf_Internal_Rela *relend;
3058
3059 dynobj = elf_hash_table (info)->dynobj;
3060 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3061 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
3062
3063 sgot = NULL;
3064 splt = NULL;
3065 sreloc = NULL;
3066
7fb9f789
NC
3067 got = NULL;
3068
252b5132
RH
3069 rel = relocs;
3070 relend = relocs + input_section->reloc_count;
3071 for (; rel < relend; rel++)
3072 {
3073 int r_type;
3074 reloc_howto_type *howto;
3075 unsigned long r_symndx;
3076 struct elf_link_hash_entry *h;
3077 Elf_Internal_Sym *sym;
3078 asection *sec;
3079 bfd_vma relocation;
44f745a6 3080 bfd_boolean unresolved_reloc;
252b5132
RH
3081 bfd_reloc_status_type r;
3082
3083 r_type = ELF32_R_TYPE (rel->r_info);
3084 if (r_type < 0 || r_type >= (int) R_68K_max)
3085 {
3086 bfd_set_error (bfd_error_bad_value);
b34976b6 3087 return FALSE;
252b5132
RH
3088 }
3089 howto = howto_table + r_type;
3090
3091 r_symndx = ELF32_R_SYM (rel->r_info);
3092
252b5132
RH
3093 h = NULL;
3094 sym = NULL;
3095 sec = NULL;
44f745a6 3096 unresolved_reloc = FALSE;
560e09e9 3097
252b5132
RH
3098 if (r_symndx < symtab_hdr->sh_info)
3099 {
3100 sym = local_syms + r_symndx;
3101 sec = local_sections[r_symndx];
8517fae7 3102 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
3103 }
3104 else
3105 {
560e09e9
NC
3106 bfd_boolean warned;
3107
b2a8e766
AM
3108 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3109 r_symndx, symtab_hdr, sym_hashes,
3110 h, sec, relocation,
3111 unresolved_reloc, warned);
252b5132
RH
3112 }
3113
ab96bf03
AM
3114 if (sec != NULL && elf_discarded_section (sec))
3115 {
3116 /* For relocs against symbols from removed linkonce sections,
3117 or sections discarded by a linker script, we just want the
3118 section contents zeroed. Avoid any special processing. */
3119 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
3120 rel->r_info = 0;
3121 rel->r_addend = 0;
3122 continue;
3123 }
3124
3125 if (info->relocatable)
3126 continue;
3127
252b5132
RH
3128 switch (r_type)
3129 {
3130 case R_68K_GOT8:
3131 case R_68K_GOT16:
3132 case R_68K_GOT32:
3133 /* Relocation is to the address of the entry for this symbol
3134 in the global offset table. */
3135 if (h != NULL
3136 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7fb9f789
NC
3137 {
3138 BFD_ASSERT (rel->r_addend == 0);
3139
3140 if (elf_m68k_hash_table (info)->local_gp_p)
3141 {
3142 bfd_vma sgot_output_offset;
3143 bfd_vma got_offset;
3144
3145 if (sgot == NULL)
3146 {
3147 sgot = bfd_get_section_by_name (dynobj, ".got");
3148
3149 if (sgot != NULL)
3150 sgot_output_offset = sgot->output_offset;
3151 else
3152 /* In this case we have a reference to
3153 _GLOBAL_OFFSET_TABLE_, but the GOT itself is
3154 empty.
3155 ??? Issue a warning? */
3156 sgot_output_offset = 0;
3157 }
3158 else
3159 sgot_output_offset = sgot->output_offset;
3160
3161 if (got == NULL)
3162 {
3163 struct elf_m68k_bfd2got_entry *bfd2got_entry;
3164
3165 bfd2got_entry
3166 = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3167 input_bfd, SEARCH, NULL);
3168
3169 if (bfd2got_entry != NULL)
3170 {
3171 got = bfd2got_entry->got;
3172 BFD_ASSERT (got != NULL);
3173
3174 got_offset = got->offset;
3175 }
3176 else
3177 /* In this case we have a reference to
3178 _GLOBAL_OFFSET_TABLE_, but no other references
3179 accessing any GOT entries.
3180 ??? Issue a warning? */
3181 got_offset = 0;
3182 }
3183 else
3184 got_offset = got->offset;
3185
3186 /* Adjust GOT pointer to point to the GOT
3187 assigned to input_bfd. */
3188 rel->r_addend = sgot_output_offset + got_offset;
3189 }
3190 else
3191 BFD_ASSERT (got == NULL || got->offset == 0);
3192
3193 break;
3194 }
252b5132
RH
3195 /* Fall through. */
3196 case R_68K_GOT8O:
3197 case R_68K_GOT16O:
3198 case R_68K_GOT32O:
3199 /* Relocation is the offset of the entry for this symbol in
3200 the global offset table. */
3201
3202 {
7fb9f789
NC
3203 struct elf_m68k_got_entry_key key_;
3204 bfd_vma *off_ptr;
252b5132
RH
3205 bfd_vma off;
3206
3207 if (sgot == NULL)
3208 {
3209 sgot = bfd_get_section_by_name (dynobj, ".got");
3210 BFD_ASSERT (sgot != NULL);
3211 }
3212
7fb9f789
NC
3213 if (got == NULL)
3214 {
3215 got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3216 input_bfd, MUST_FIND,
3217 NULL)->got;
3218 BFD_ASSERT (got != NULL);
3219 }
3220
3221 /* Get GOT offset for this symbol. */
3222 elf_m68k_init_got_entry_key (&key_, h, input_bfd, r_symndx);
3223 off_ptr = &elf_m68k_get_got_entry (got, &key_, MUST_FIND,
3224 NULL)->u.s2.offset;
3225 off = *off_ptr;
3226
252b5132
RH
3227 if (h != NULL)
3228 {
a5ad465a
AS
3229 bfd_boolean dyn;
3230
a5ad465a
AS
3231 dyn = elf_hash_table (info)->dynamic_sections_created;
3232 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
252b5132 3233 || (info->shared
a5ad465a
AS
3234 && (info->symbolic
3235 || h->dynindx == -1
f5385ebf
AM
3236 || h->forced_local)
3237 && h->def_regular))
252b5132
RH
3238 {
3239 /* This is actually a static link, or it is a
3240 -Bsymbolic link and the symbol is defined
3241 locally, or the symbol was forced to be local
3242 because of a version file.. We must initialize
3243 this entry in the global offset table. Since
3244 the offset must always be a multiple of 4, we
3245 use the least significant bit to record whether
3246 we have initialized it already.
3247
3248 When doing a dynamic link, we create a .rela.got
3249 relocation entry to initialize the value. This
3250 is done in the finish_dynamic_symbol routine. */
3251 if ((off & 1) != 0)
3252 off &= ~1;
3253 else
3254 {
3255 bfd_put_32 (output_bfd, relocation,
3256 sgot->contents + off);
7fb9f789 3257 *off_ptr |= 1;
252b5132
RH
3258 }
3259 }
44f745a6
AS
3260 else
3261 unresolved_reloc = FALSE;
252b5132
RH
3262 }
3263 else
3264 {
252b5132
RH
3265 /* The offset must always be a multiple of 4. We use
3266 the least significant bit to record whether we have
3267 already generated the necessary reloc. */
3268 if ((off & 1) != 0)
3269 off &= ~1;
3270 else
3271 {
3272 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3273
3274 if (info->shared)
3275 {
947216bf 3276 asection *s;
252b5132 3277 Elf_Internal_Rela outrel;
947216bf 3278 bfd_byte *loc;
252b5132 3279
947216bf
AM
3280 s = bfd_get_section_by_name (dynobj, ".rela.got");
3281 BFD_ASSERT (s != NULL);
252b5132
RH
3282
3283 outrel.r_offset = (sgot->output_section->vma
3284 + sgot->output_offset
3285 + off);
3286 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3287 outrel.r_addend = relocation;
947216bf
AM
3288 loc = s->contents;
3289 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3290 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
3291 }
3292
7fb9f789 3293 *off_ptr |= 1;
252b5132
RH
3294 }
3295 }
3296
252b5132
RH
3297 if (r_type == R_68K_GOT8O
3298 || r_type == R_68K_GOT16O
3299 || r_type == R_68K_GOT32O)
3300 {
7fb9f789
NC
3301 /* GOT pointer is adjusted to point to the start/middle
3302 of local GOT. Adjust the offset accordingly. */
3303 BFD_ASSERT (elf_m68k_hash_table (info)->use_neg_got_offsets_p
3304 || off >= got->offset);
3305
3306 if (elf_m68k_hash_table (info)->local_gp_p)
3307 relocation = off - got->offset;
3308 else
3309 {
3310 BFD_ASSERT (got->offset == 0);
3311 relocation = sgot->output_offset + off;
3312 }
3313
252b5132 3314 /* This relocation does not use the addend. */
7fb9f789 3315 BFD_ASSERT (rel->r_addend == 0);
252b5132
RH
3316 rel->r_addend = 0;
3317 }
3318 else
7fb9f789
NC
3319 relocation = (sgot->output_section->vma + sgot->output_offset
3320 + off);
252b5132
RH
3321 }
3322 break;
3323
3324 case R_68K_PLT8:
3325 case R_68K_PLT16:
3326 case R_68K_PLT32:
3327 /* Relocation is to the entry for this symbol in the
3328 procedure linkage table. */
3329
3330 /* Resolve a PLTxx reloc against a local symbol directly,
3331 without using the procedure linkage table. */
3332 if (h == NULL)
3333 break;
3334
3335 if (h->plt.offset == (bfd_vma) -1
3336 || !elf_hash_table (info)->dynamic_sections_created)
3337 {
3338 /* We didn't make a PLT entry for this symbol. This
3339 happens when statically linking PIC code, or when
3340 using -Bsymbolic. */
3341 break;
3342 }
3343
3344 if (splt == NULL)
3345 {
3346 splt = bfd_get_section_by_name (dynobj, ".plt");
3347 BFD_ASSERT (splt != NULL);
3348 }
3349
3350 relocation = (splt->output_section->vma
3351 + splt->output_offset
3352 + h->plt.offset);
44f745a6 3353 unresolved_reloc = FALSE;
252b5132
RH
3354 break;
3355
3356 case R_68K_PLT8O:
3357 case R_68K_PLT16O:
3358 case R_68K_PLT32O:
3359 /* Relocation is the offset of the entry for this symbol in
3360 the procedure linkage table. */
3361 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
3362
3363 if (splt == NULL)
3364 {
3365 splt = bfd_get_section_by_name (dynobj, ".plt");
3366 BFD_ASSERT (splt != NULL);
3367 }
3368
3369 relocation = h->plt.offset;
44f745a6 3370 unresolved_reloc = FALSE;
252b5132
RH
3371
3372 /* This relocation does not use the addend. */
3373 rel->r_addend = 0;
3374
3375 break;
3376
3377 case R_68K_PC8:
3378 case R_68K_PC16:
3379 case R_68K_PC32:
b6152c34
AS
3380 if (h == NULL
3381 || (info->shared
f5385ebf 3382 && h->forced_local))
252b5132
RH
3383 break;
3384 /* Fall through. */
3385 case R_68K_8:
3386 case R_68K_16:
3387 case R_68K_32:
3388 if (info->shared
ec338859 3389 && r_symndx != 0
252b5132 3390 && (input_section->flags & SEC_ALLOC) != 0
d2ff124f
AS
3391 && (h == NULL
3392 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3393 || h->root.type != bfd_link_hash_undefweak)
252b5132
RH
3394 && ((r_type != R_68K_PC8
3395 && r_type != R_68K_PC16
3396 && r_type != R_68K_PC32)
d2ff124f
AS
3397 || (h != NULL
3398 && h->dynindx != -1
3399 && (!info->symbolic
f5385ebf 3400 || !h->def_regular))))
252b5132
RH
3401 {
3402 Elf_Internal_Rela outrel;
947216bf 3403 bfd_byte *loc;
b34976b6 3404 bfd_boolean skip, relocate;
252b5132
RH
3405
3406 /* When generating a shared object, these relocations
3407 are copied into the output file to be resolved at run
3408 time. */
3409
b34976b6
AM
3410 skip = FALSE;
3411 relocate = FALSE;
252b5132 3412
c629eae0
JJ
3413 outrel.r_offset =
3414 _bfd_elf_section_offset (output_bfd, info, input_section,
3415 rel->r_offset);
3416 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3417 skip = TRUE;
0bb2d96a 3418 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3419 skip = TRUE, relocate = TRUE;
252b5132
RH
3420 outrel.r_offset += (input_section->output_section->vma
3421 + input_section->output_offset);
3422
3423 if (skip)
0bb2d96a 3424 memset (&outrel, 0, sizeof outrel);
252b5132 3425 else if (h != NULL
d2ff124f
AS
3426 && h->dynindx != -1
3427 && (r_type == R_68K_PC8
3428 || r_type == R_68K_PC16
3429 || r_type == R_68K_PC32
3430 || !info->shared
3431 || !info->symbolic
f5385ebf 3432 || !h->def_regular))
252b5132 3433 {
252b5132 3434 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
d2ff124f 3435 outrel.r_addend = rel->r_addend;
252b5132
RH
3436 }
3437 else
3438 {
d2ff124f 3439 /* This symbol is local, or marked to become local. */
74541ad4
AM
3440 outrel.r_addend = relocation + rel->r_addend;
3441
252b5132
RH
3442 if (r_type == R_68K_32)
3443 {
b34976b6 3444 relocate = TRUE;
252b5132 3445 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
252b5132
RH
3446 }
3447 else
3448 {
3449 long indx;
3450
8517fae7 3451 if (bfd_is_abs_section (sec))
252b5132
RH
3452 indx = 0;
3453 else if (sec == NULL || sec->owner == NULL)
3454 {
3455 bfd_set_error (bfd_error_bad_value);
b34976b6 3456 return FALSE;
252b5132
RH
3457 }
3458 else
3459 {
3460 asection *osec;
3461
74541ad4
AM
3462 /* We are turning this relocation into one
3463 against a section symbol. It would be
3464 proper to subtract the symbol's value,
3465 osec->vma, from the emitted reloc addend,
3466 but ld.so expects buggy relocs. */
252b5132
RH
3467 osec = sec->output_section;
3468 indx = elf_section_data (osec)->dynindx;
74541ad4
AM
3469 if (indx == 0)
3470 {
3471 struct elf_link_hash_table *htab;
3472 htab = elf_hash_table (info);
3473 osec = htab->text_index_section;
3474 indx = elf_section_data (osec)->dynindx;
3475 }
3476 BFD_ASSERT (indx != 0);
252b5132
RH
3477 }
3478
252b5132 3479 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
3480 }
3481 }
3482
d2ff124f
AS
3483 sreloc = elf_section_data (input_section)->sreloc;
3484 if (sreloc == NULL)
3485 abort ();
3486
947216bf
AM
3487 loc = sreloc->contents;
3488 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3489 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
3490
3491 /* This reloc will be computed at runtime, so there's no
3492 need to do anything now, except for R_68K_32
3493 relocations that have been turned into
3494 R_68K_RELATIVE. */
3495 if (!relocate)
3496 continue;
3497 }
3498
3499 break;
3500
3501 case R_68K_GNU_VTINHERIT:
3502 case R_68K_GNU_VTENTRY:
3503 /* These are no-ops in the end. */
3504 continue;
3505
3506 default:
3507 break;
3508 }
3509
44f745a6
AS
3510 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3511 because such sections are not SEC_ALLOC and thus ld.so will
3512 not process them. */
3513 if (unresolved_reloc
3514 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3515 && h->def_dynamic))
44f745a6
AS
3516 {
3517 (*_bfd_error_handler)
843fe662 3518 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3519 input_bfd,
3520 input_section,
44f745a6 3521 (long) rel->r_offset,
843fe662 3522 howto->name,
44f745a6
AS
3523 h->root.root.string);
3524 return FALSE;
3525 }
3526
252b5132
RH
3527 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3528 contents, rel->r_offset,
3529 relocation, rel->r_addend);
3530
3531 if (r != bfd_reloc_ok)
3532 {
44f745a6
AS
3533 const char *name;
3534
3535 if (h != NULL)
3536 name = h->root.root.string;
3537 else
252b5132 3538 {
44f745a6
AS
3539 name = bfd_elf_string_from_elf_section (input_bfd,
3540 symtab_hdr->sh_link,
3541 sym->st_name);
3542 if (name == NULL)
3543 return FALSE;
3544 if (*name == '\0')
3545 name = bfd_section_name (input_bfd, sec);
3546 }
252b5132 3547
44f745a6
AS
3548 if (r == bfd_reloc_overflow)
3549 {
3550 if (!(info->callbacks->reloc_overflow
dfeffb9f
L
3551 (info, (h ? &h->root : NULL), name, howto->name,
3552 (bfd_vma) 0, input_bfd, input_section,
3553 rel->r_offset)))
44f745a6
AS
3554 return FALSE;
3555 }
3556 else
3557 {
3558 (*_bfd_error_handler)
d003868e
AM
3559 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3560 input_bfd, input_section,
44f745a6
AS
3561 (long) rel->r_offset, name, (int) r);
3562 return FALSE;
252b5132
RH
3563 }
3564 }
3565 }
3566
b34976b6 3567 return TRUE;
252b5132
RH
3568}
3569
cc3e26be
RS
3570/* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
3571 into section SEC. */
3572
3573static void
3574elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
3575{
3576 /* Make VALUE PC-relative. */
3577 value -= sec->output_section->vma + offset;
3578
3579 /* Apply any in-place addend. */
3580 value += bfd_get_32 (sec->owner, sec->contents + offset);
3581
3582 bfd_put_32 (sec->owner, value, sec->contents + offset);
3583}
3584
252b5132
RH
3585/* Finish up dynamic symbol handling. We set the contents of various
3586 dynamic sections here. */
3587
b34976b6 3588static bfd_boolean
252b5132
RH
3589elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
3590 bfd *output_bfd;
3591 struct bfd_link_info *info;
3592 struct elf_link_hash_entry *h;
3593 Elf_Internal_Sym *sym;
3594{
3595 bfd *dynobj;
3596
3597 dynobj = elf_hash_table (info)->dynobj;
3598
3599 if (h->plt.offset != (bfd_vma) -1)
3600 {
cc3e26be 3601 const struct elf_m68k_plt_info *plt_info;
252b5132
RH
3602 asection *splt;
3603 asection *sgot;
3604 asection *srela;
3605 bfd_vma plt_index;
3606 bfd_vma got_offset;
3607 Elf_Internal_Rela rela;
947216bf 3608 bfd_byte *loc;
252b5132
RH
3609
3610 /* This symbol has an entry in the procedure linkage table. Set
3611 it up. */
3612
3613 BFD_ASSERT (h->dynindx != -1);
3614
cc3e26be 3615 plt_info = elf_m68k_hash_table (info)->plt_info;
252b5132
RH
3616 splt = bfd_get_section_by_name (dynobj, ".plt");
3617 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3618 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
3619 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3620
3621 /* Get the index in the procedure linkage table which
3622 corresponds to this symbol. This is the index of this symbol
3623 in all the symbols for which we are making plt entries. The
3624 first entry in the procedure linkage table is reserved. */
cc3e26be 3625 plt_index = (h->plt.offset / plt_info->size) - 1;
252b5132
RH
3626
3627 /* Get the offset into the .got table of the entry that
3628 corresponds to this function. Each .got entry is 4 bytes.
3629 The first three are reserved. */
3630 got_offset = (plt_index + 3) * 4;
3631
cc3e26be
RS
3632 memcpy (splt->contents + h->plt.offset,
3633 plt_info->symbol_entry,
3634 plt_info->size);
3635
3636 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
3637 (sgot->output_section->vma
3638 + sgot->output_offset
3639 + got_offset));
252b5132
RH
3640
3641 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
cc3e26be
RS
3642 splt->contents
3643 + h->plt.offset
3644 + plt_info->symbol_resolve_entry + 2);
3645
3646 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
3647 splt->output_section->vma);
252b5132
RH
3648
3649 /* Fill in the entry in the global offset table. */
3650 bfd_put_32 (output_bfd,
3651 (splt->output_section->vma
3652 + splt->output_offset
3653 + h->plt.offset
cc3e26be 3654 + plt_info->symbol_resolve_entry),
252b5132
RH
3655 sgot->contents + got_offset);
3656
3657 /* Fill in the entry in the .rela.plt section. */
3658 rela.r_offset = (sgot->output_section->vma
3659 + sgot->output_offset
3660 + got_offset);
3661 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
3662 rela.r_addend = 0;
947216bf
AM
3663 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3664 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132 3665
f5385ebf 3666 if (!h->def_regular)
252b5132
RH
3667 {
3668 /* Mark the symbol as undefined, rather than as defined in
3669 the .plt section. Leave the value alone. */
3670 sym->st_shndx = SHN_UNDEF;
3671 }
3672 }
3673
7fb9f789 3674 if (elf_m68k_hash_entry (h)->glist != NULL)
252b5132
RH
3675 {
3676 asection *sgot;
3677 asection *srela;
7fb9f789 3678 struct elf_m68k_got_entry *got_entry;
252b5132
RH
3679
3680 /* This symbol has an entry in the global offset table. Set it
3681 up. */
3682
3683 sgot = bfd_get_section_by_name (dynobj, ".got");
3684 srela = bfd_get_section_by_name (dynobj, ".rela.got");
3685 BFD_ASSERT (sgot != NULL && srela != NULL);
3686
7fb9f789
NC
3687 got_entry = elf_m68k_hash_entry (h)->glist;
3688
3689 while (got_entry != NULL)
252b5132 3690 {
7fb9f789
NC
3691 Elf_Internal_Rela rela;
3692 bfd_byte *loc;
3693
3694 rela.r_offset = (sgot->output_section->vma
3695 + sgot->output_offset
3696 + (got_entry->u.s2.offset &~ (bfd_vma) 1));
3697
3698 /* If this is a -Bsymbolic link, and the symbol is defined
3699 locally, we just want to emit a RELATIVE reloc. Likewise if
3700 the symbol was forced to be local because of a version file.
3701 The entry in the global offset table will already have been
3702 initialized in the relocate_section function. */
3703 if (info->shared
3704 && (info->symbolic
3705 || h->dynindx == -1
3706 || h->forced_local)
3707 && h->def_regular)
3708 {
3709 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3710 rela.r_addend = bfd_get_signed_32 (output_bfd,
3711 (sgot->contents
3712 + (got_entry->u.s2.offset
3713 &~ (bfd_vma) 1)));
3714 }
3715 else
3716 {
3717 bfd_put_32 (output_bfd, (bfd_vma) 0,
3718 sgot->contents + (got_entry->u.s2.offset
3719 &~ (bfd_vma) 1));
3720 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
3721 rela.r_addend = 0;
3722 }
252b5132 3723
7fb9f789
NC
3724 loc = srela->contents;
3725 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3726 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3727
3728 got_entry = got_entry->u.s2.next;
3729 }
252b5132
RH
3730 }
3731
f5385ebf 3732 if (h->needs_copy)
252b5132
RH
3733 {
3734 asection *s;
3735 Elf_Internal_Rela rela;
947216bf 3736 bfd_byte *loc;
252b5132
RH
3737
3738 /* This symbol needs a copy reloc. Set it up. */
3739
3740 BFD_ASSERT (h->dynindx != -1
3741 && (h->root.type == bfd_link_hash_defined
3742 || h->root.type == bfd_link_hash_defweak));
3743
3744 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3745 ".rela.bss");
3746 BFD_ASSERT (s != NULL);
3747
3748 rela.r_offset = (h->root.u.def.value
3749 + h->root.u.def.section->output_section->vma
3750 + h->root.u.def.section->output_offset);
3751 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
3752 rela.r_addend = 0;
947216bf
AM
3753 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3754 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
3755 }
3756
3757 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3758 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3759 || h == elf_hash_table (info)->hgot)
252b5132
RH
3760 sym->st_shndx = SHN_ABS;
3761
b34976b6 3762 return TRUE;
252b5132
RH
3763}
3764
3765/* Finish up the dynamic sections. */
3766
b34976b6 3767static bfd_boolean
252b5132
RH
3768elf_m68k_finish_dynamic_sections (output_bfd, info)
3769 bfd *output_bfd;
3770 struct bfd_link_info *info;
3771{
3772 bfd *dynobj;
3773 asection *sgot;
3774 asection *sdyn;
3775
3776 dynobj = elf_hash_table (info)->dynobj;
3777
3778 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3779 BFD_ASSERT (sgot != NULL);
3780 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3781
3782 if (elf_hash_table (info)->dynamic_sections_created)
3783 {
3784 asection *splt;
3785 Elf32_External_Dyn *dyncon, *dynconend;
3786
3787 splt = bfd_get_section_by_name (dynobj, ".plt");
3788 BFD_ASSERT (splt != NULL && sdyn != NULL);
3789
3790 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 3791 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
3792 for (; dyncon < dynconend; dyncon++)
3793 {
3794 Elf_Internal_Dyn dyn;
3795 const char *name;
3796 asection *s;
3797
3798 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3799
3800 switch (dyn.d_tag)
3801 {
3802 default:
3803 break;
3804
3805 case DT_PLTGOT:
3806 name = ".got";
3807 goto get_vma;
3808 case DT_JMPREL:
3809 name = ".rela.plt";
3810 get_vma:
3811 s = bfd_get_section_by_name (output_bfd, name);
3812 BFD_ASSERT (s != NULL);
3813 dyn.d_un.d_ptr = s->vma;
3814 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3815 break;
3816
3817 case DT_PLTRELSZ:
3818 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3819 BFD_ASSERT (s != NULL);
eea6121a 3820 dyn.d_un.d_val = s->size;
252b5132
RH
3821 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3822 break;
3823
3824 case DT_RELASZ:
3825 /* The procedure linkage table relocs (DT_JMPREL) should
3826 not be included in the overall relocs (DT_RELA).
3827 Therefore, we override the DT_RELASZ entry here to
3828 make it not include the JMPREL relocs. Since the
3829 linker script arranges for .rela.plt to follow all
3830 other relocation sections, we don't have to worry
3831 about changing the DT_RELA entry. */
3832 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3833 if (s != NULL)
eea6121a 3834 dyn.d_un.d_val -= s->size;
252b5132
RH
3835 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3836 break;
3837 }
3838 }
3839
3840 /* Fill in the first entry in the procedure linkage table. */
eea6121a 3841 if (splt->size > 0)
252b5132 3842 {
cc3e26be
RS
3843 const struct elf_m68k_plt_info *plt_info;
3844
3845 plt_info = elf_m68k_hash_table (info)->plt_info;
3846 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
3847
3848 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
3849 (sgot->output_section->vma
3850 + sgot->output_offset
3851 + 4));
3852
3853 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
3854 (sgot->output_section->vma
3855 + sgot->output_offset
3856 + 8));
3857
3858 elf_section_data (splt->output_section)->this_hdr.sh_entsize
3859 = plt_info->size;
252b5132 3860 }
252b5132
RH
3861 }
3862
3863 /* Fill in the first three entries in the global offset table. */
eea6121a 3864 if (sgot->size > 0)
252b5132
RH
3865 {
3866 if (sdyn == NULL)
3867 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3868 else
3869 bfd_put_32 (output_bfd,
3870 sdyn->output_section->vma + sdyn->output_offset,
3871 sgot->contents);
3872 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3873 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3874 }
3875
3876 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3877
b34976b6 3878 return TRUE;
252b5132
RH
3879}
3880
0752970e
NC
3881/* Given a .data section and a .emreloc in-memory section, store
3882 relocation information into the .emreloc section which can be
3883 used at runtime to relocate the section. This is called by the
3884 linker when the --embedded-relocs switch is used. This is called
3885 after the add_symbols entry point has been called for all the
3886 objects, and before the final_link entry point is called. */
3887
b34976b6 3888bfd_boolean
0752970e
NC
3889bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
3890 bfd *abfd;
3891 struct bfd_link_info *info;
3892 asection *datasec;
3893 asection *relsec;
3894 char **errmsg;
3895{
3896 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc
AM
3897 Elf_Internal_Sym *isymbuf = NULL;
3898 Elf_Internal_Rela *internal_relocs = NULL;
0752970e
NC
3899 Elf_Internal_Rela *irel, *irelend;
3900 bfd_byte *p;
dc810e39 3901 bfd_size_type amt;
0752970e 3902
1049f94e 3903 BFD_ASSERT (! info->relocatable);
0752970e
NC
3904
3905 *errmsg = NULL;
3906
3907 if (datasec->reloc_count == 0)
b34976b6 3908 return TRUE;
0752970e
NC
3909
3910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9ad5cbcf 3911
0752970e 3912 /* Get a copy of the native relocations. */
45d6a902 3913 internal_relocs = (_bfd_elf_link_read_relocs
0752970e
NC
3914 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3915 info->keep_memory));
3916 if (internal_relocs == NULL)
3917 goto error_return;
0752970e 3918
dc810e39
AM
3919 amt = (bfd_size_type) datasec->reloc_count * 12;
3920 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
0752970e
NC
3921 if (relsec->contents == NULL)
3922 goto error_return;
3923
3924 p = relsec->contents;
3925
3926 irelend = internal_relocs + datasec->reloc_count;
3927 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
3928 {
3929 asection *targetsec;
3930
3931 /* We are going to write a four byte longword into the runtime
3932 reloc section. The longword will be the address in the data
3933 section which must be relocated. It is followed by the name
3934 of the target section NUL-padded or truncated to 8
3935 characters. */
3936
3937 /* We can only relocate absolute longword relocs at run time. */
3938 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
3939 {
3940 *errmsg = _("unsupported reloc type");
3941 bfd_set_error (bfd_error_bad_value);
3942 goto error_return;
3943 }
3944
3945 /* Get the target section referred to by the reloc. */
3946 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3947 {
0752970e 3948 /* A local symbol. */
6cdc0ccc
AM
3949 Elf_Internal_Sym *isym;
3950
3951 /* Read this BFD's local symbols if we haven't done so already. */
3952 if (isymbuf == NULL)
3953 {
3954 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3955 if (isymbuf == NULL)
3956 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3957 symtab_hdr->sh_info, 0,
3958 NULL, NULL, NULL);
3959 if (isymbuf == NULL)
3960 goto error_return;
3961 }
0752970e 3962
6cdc0ccc
AM
3963 isym = isymbuf + ELF32_R_SYM (irel->r_info);
3964 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
0752970e
NC
3965 }
3966 else
3967 {
3968 unsigned long indx;
3969 struct elf_link_hash_entry *h;
3970
3971 /* An external symbol. */
3972 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3973 h = elf_sym_hashes (abfd)[indx];
3974 BFD_ASSERT (h != NULL);
3975 if (h->root.type == bfd_link_hash_defined
3976 || h->root.type == bfd_link_hash_defweak)
3977 targetsec = h->root.u.def.section;
3978 else
3979 targetsec = NULL;
3980 }
3981
3982 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
3983 memset (p + 4, 0, 8);
3984 if (targetsec != NULL)
f075ee0c 3985 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
0752970e 3986 }
c3668558 3987
6cdc0ccc
AM
3988 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3989 free (isymbuf);
3990 if (internal_relocs != NULL
3991 && elf_section_data (datasec)->relocs != internal_relocs)
3992 free (internal_relocs);
b34976b6 3993 return TRUE;
0752970e
NC
3994
3995error_return:
6cdc0ccc
AM
3996 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3997 free (isymbuf);
3998 if (internal_relocs != NULL
3999 && elf_section_data (datasec)->relocs != internal_relocs)
4000 free (internal_relocs);
b34976b6 4001 return FALSE;
0752970e
NC
4002}
4003
7fb9f789
NC
4004/* Set target options. */
4005
4006void
4007bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
4008{
4009 struct elf_m68k_link_hash_table *htab;
4010
4011 htab = elf_m68k_hash_table (info);
4012
4013 switch (got_handling)
4014 {
4015 case 0:
4016 /* --got=single. */
4017 htab->local_gp_p = FALSE;
4018 htab->use_neg_got_offsets_p = FALSE;
4019 htab->allow_multigot_p = FALSE;
4020 break;
4021
4022 case 1:
4023 /* --got=negative. */
4024 htab->local_gp_p = TRUE;
4025 htab->use_neg_got_offsets_p = TRUE;
4026 htab->allow_multigot_p = FALSE;
4027 break;
4028
4029 case 2:
4030 /* --got=multigot. */
4031 htab->local_gp_p = TRUE;
4032 htab->use_neg_got_offsets_p = TRUE;
4033 htab->allow_multigot_p = TRUE;
4034 break;
4035
4036 default:
4037 BFD_ASSERT (FALSE);
4038 }
4039}
4040
aa91b392 4041static enum elf_reloc_type_class
f51e552e
AM
4042elf32_m68k_reloc_type_class (rela)
4043 const Elf_Internal_Rela *rela;
aa91b392 4044{
f51e552e 4045 switch ((int) ELF32_R_TYPE (rela->r_info))
aa91b392
AS
4046 {
4047 case R_68K_RELATIVE:
4048 return reloc_class_relative;
4049 case R_68K_JMP_SLOT:
4050 return reloc_class_plt;
4051 case R_68K_COPY:
4052 return reloc_class_copy;
4053 default:
4054 return reloc_class_normal;
4055 }
4056}
4057
1715e0e3
AS
4058/* Return address for Ith PLT stub in section PLT, for relocation REL
4059 or (bfd_vma) -1 if it should not be included. */
4060
4061static bfd_vma
4062elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
4063 const arelent *rel ATTRIBUTE_UNUSED)
4064{
cc3e26be 4065 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
1715e0e3
AS
4066}
4067
252b5132
RH
4068#define TARGET_BIG_SYM bfd_elf32_m68k_vec
4069#define TARGET_BIG_NAME "elf32-m68k"
4070#define ELF_MACHINE_CODE EM_68K
4071#define ELF_MAXPAGESIZE 0x2000
4072#define elf_backend_create_dynamic_sections \
4073 _bfd_elf_create_dynamic_sections
4074#define bfd_elf32_bfd_link_hash_table_create \
4075 elf_m68k_link_hash_table_create
7fb9f789
NC
4076/* ??? Should it be this macro or bfd_elfNN_bfd_link_hash_table_create? */
4077#define bfd_elf32_bfd_link_hash_table_free \
4078 elf_m68k_link_hash_table_free
4079#define bfd_elf32_bfd_final_link bfd_elf_final_link
252b5132
RH
4080
4081#define elf_backend_check_relocs elf_m68k_check_relocs
cc3e26be
RS
4082#define elf_backend_always_size_sections \
4083 elf_m68k_always_size_sections
252b5132
RH
4084#define elf_backend_adjust_dynamic_symbol \
4085 elf_m68k_adjust_dynamic_symbol
4086#define elf_backend_size_dynamic_sections \
4087 elf_m68k_size_dynamic_sections
74541ad4 4088#define elf_backend_init_index_section _bfd_elf_init_1_index_section
252b5132
RH
4089#define elf_backend_relocate_section elf_m68k_relocate_section
4090#define elf_backend_finish_dynamic_symbol \
4091 elf_m68k_finish_dynamic_symbol
4092#define elf_backend_finish_dynamic_sections \
4093 elf_m68k_finish_dynamic_sections
4094#define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
4095#define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
7fb9f789 4096#define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
9e1281c7
CM
4097#define bfd_elf32_bfd_merge_private_bfd_data \
4098 elf32_m68k_merge_private_bfd_data
4099#define bfd_elf32_bfd_set_private_flags \
4100 elf32_m68k_set_private_flags
4101#define bfd_elf32_bfd_print_private_bfd_data \
4102 elf32_m68k_print_private_bfd_data
aa91b392 4103#define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
1715e0e3 4104#define elf_backend_plt_sym_val elf_m68k_plt_sym_val
266abb8f 4105#define elf_backend_object_p elf32_m68k_object_p
9e1281c7 4106
252b5132 4107#define elf_backend_can_gc_sections 1
51b64d56 4108#define elf_backend_can_refcount 1
252b5132
RH
4109#define elf_backend_want_got_plt 1
4110#define elf_backend_plt_readonly 1
4111#define elf_backend_want_plt_sym 0
4112#define elf_backend_got_header_size 12
b491616a 4113#define elf_backend_rela_normal 1
252b5132
RH
4114
4115#include "elf32-target.h"
This page took 0.661472 seconds and 4 git commands to generate.