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