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