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