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