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