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