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