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