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