Remove spurious whitespace introduced by previous delta.
[deliverable/binutils-gdb.git] / bfd / elf32-metag.c
CommitLineData
a3c62988 1/* Meta support for 32-bit ELF
6f2750fe 2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
a3c62988
NC
3 Contributed by Imagination Technologies Ltd.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22#include "sysdep.h"
23#include "bfd.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf32-metag.h"
27#include "elf/metag.h"
28
29#define GOT_ENTRY_SIZE 4
30#define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
31
32/* ABI version:
33 0 - original
34 1 - with GOT offset */
35#define METAG_ELF_ABI_VERSION 1
36
37static const unsigned int plt0_entry[] =
38 {
39 0x02000005, /* MOVT D0Re0, #HI(GOT+4) */
40 0x02000000, /* ADD D0Re0, D0Re0, #LO(GOT+4) */
41 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
42 0xc600012a, /* GETD PC, [D0Re0+#4] */
43 0xa0fffffe /* NOP */
44 };
45
46static const unsigned int plt0_pic_entry[] =
47 {
48 0x82900001, /* ADDT A0.2, CPC0, #0 */
49 0x82100000, /* ADD A0.2, A0.2, #0 */
50 0xa3100c20, /* MOV D0Re0, A0.2 */
51 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
52 0xc600012a, /* GETD PC, [D0Re0+#4] */
53 };
54
55static const unsigned int plt_entry[] =
56 {
57 0x82100005, /* MOVT A0.2, #HI(GOT+off) */
58 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
59 0xc600806a, /* GETD PC, [A0.2] */
60 0x03000004, /* MOV D1Re0, #LO(offset) */
61 0xa0000000 /* B PLT0 */
62 };
63
64static const unsigned int plt_pic_entry[] =
65 {
66 0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */
67 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
68 0xc600806a, /* GETD PC, [A0.2] */
69 0x03000004, /* MOV D1Re0, #LO(offset) */
70 0xa0000000 /* B PLT0 */
71 };
72
73/* Variable names follow a coding style.
74 Please follow this (Apps Hungarian) style:
75
76 Structure/Variable Prefix
77 elf_link_hash_table "etab"
78 elf_link_hash_entry "eh"
79
80 elf_metag_link_hash_table "htab"
81 elf_metag_link_hash_entry "hh"
82
83 bfd_link_hash_table "btab"
84 bfd_link_hash_entry "bh"
85
86 bfd_hash_table containing stubs "bstab"
87 elf_metag_stub_hash_entry "hsh"
88
89 elf_metag_dyn_reloc_entry "hdh"
90
91 Always remember to use GNU Coding Style. */
92
93#define PLT_ENTRY_SIZE sizeof(plt_entry)
94
95static reloc_howto_type elf_metag_howto_table[] =
96{
97 /* High order 16 bit absolute. */
98 HOWTO (R_METAG_HIADDR16, /* type */
99 16, /* rightshift */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
101 16, /* bitsize */
102 FALSE, /* pc_relative */
103 3, /* bitpos */
104 complain_overflow_dont, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_METAG_HIADDR16", /* name */
107 FALSE, /* partial_inplace */
108 0, /* src_mask */
109 0x0007fff8, /* dst_mask */
110 FALSE), /* pcrel_offset */
111
112 /* Low order 16 bit absolute. */
113 HOWTO (R_METAG_LOADDR16, /* type */
114 0, /* rightshift */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
116 16, /* bitsize */
117 FALSE, /* pc_relative */
118 3, /* bitpos */
119 complain_overflow_dont,/* complain_on_overflow */
120 bfd_elf_generic_reloc, /* special_function */
121 "R_METAG_LOADDR16", /* name */
122 FALSE, /* partial_inplace */
123 0, /* src_mask */
124 0x0007fff8, /* dst_mask */
125 FALSE), /* pcrel_offset */
126
127 /* 32 bit absolute. */
128 HOWTO (R_METAG_ADDR32, /* type */
129 0, /* rightshift */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
131 32, /* bitsize */
132 FALSE, /* pc_relative */
133 0, /* bitpos */
134 complain_overflow_bitfield, /* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_METAG_ADDR32", /* name */
137 FALSE, /* partial_inplace */
138 0x00000000, /* src_mask */
139 0xffffffff, /* dst_mask */
140 FALSE), /* pcrel_offset */
141
142 /* No relocation. */
143 HOWTO (R_METAG_NONE, /* type */
144 0, /* rightshift */
6346d5ca 145 3, /* size (0 = byte, 1 = short, 2 = long) */
a3c62988
NC
146 0, /* bitsize */
147 FALSE, /* pc_relative */
148 0, /* bitpos */
149 complain_overflow_dont, /* complain_on_overflow */
150 bfd_elf_generic_reloc, /* special_function */
151 "R_METAG_NONE", /* name */
152 FALSE, /* partial_inplace */
153 0, /* src_mask */
154 0, /* dst_mask */
155 FALSE), /* pcrel_offset */
156
157 /* 19 bit pc relative */
158 HOWTO (R_METAG_RELBRANCH, /* type */
159 2, /* rightshift */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
161 19, /* bitsize */
162 TRUE, /* pc_relative */
163 5, /* bitpos */
164 complain_overflow_signed, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_METAG_RELBRANCH", /* name */
167 FALSE, /* partial_inplace */
168 0, /* src_mask */
169 0x00ffffe0, /* dst_mask */
170 FALSE), /* pcrel_offset */
171
172 /* GET/SET offset */
173 HOWTO (R_METAG_GETSETOFF, /* type */
174 0, /* rightshift */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
176 12, /* bitsize */
177 FALSE, /* pc_relative */
178 7, /* bitpos */
179 complain_overflow_dont, /* complain_on_overflow */
180 bfd_elf_generic_reloc, /* special_function */
181 "R_METAG_GETSETOFF", /* name */
182 FALSE, /* partial_inplace */
183 0, /* src_mask */
184 0, /* dst_mask */
185 FALSE), /* pcrel_offset */
186
187 EMPTY_HOWTO (6),
188 EMPTY_HOWTO (7),
189 EMPTY_HOWTO (8),
190 EMPTY_HOWTO (9),
191 EMPTY_HOWTO (10),
192 EMPTY_HOWTO (11),
193 EMPTY_HOWTO (12),
194 EMPTY_HOWTO (13),
195 EMPTY_HOWTO (14),
196 EMPTY_HOWTO (15),
197 EMPTY_HOWTO (16),
198 EMPTY_HOWTO (17),
199 EMPTY_HOWTO (18),
200 EMPTY_HOWTO (19),
201 EMPTY_HOWTO (20),
202 EMPTY_HOWTO (21),
203 EMPTY_HOWTO (22),
204 EMPTY_HOWTO (23),
205 EMPTY_HOWTO (24),
206 EMPTY_HOWTO (25),
207 EMPTY_HOWTO (26),
208 EMPTY_HOWTO (27),
209 EMPTY_HOWTO (28),
210 EMPTY_HOWTO (29),
211
212 HOWTO (R_METAG_GNU_VTINHERIT, /* type */
213 0, /* rightshift */
214 2, /* size (0 = byte, 1 = short, 2 = long) */
215 0, /* bitsize */
216 FALSE, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 NULL, /* special_function */
220 "R_METAG_GNU_VTINHERIT", /* name */
221 FALSE, /* partial_inplace */
222 0, /* src_mask */
223 0, /* dst_mask */
224 FALSE), /* pcrel_offset */
225
226 HOWTO (R_METAG_GNU_VTENTRY, /* type */
227 0, /* rightshift */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
229 0, /* bitsize */
230 FALSE, /* pc_relative */
231 0, /* bitpos */
232 complain_overflow_dont, /* complain_on_overflow */
233 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
234 "R_METAG_GNU_VTENTRY", /* name */
235 FALSE, /* partial_inplace */
236 0, /* src_mask */
237 0, /* dst_mask */
238 FALSE), /* pcrel_offset */
239
240 /* High order 16 bit GOT offset */
241 HOWTO (R_METAG_HI16_GOTOFF, /* type */
242 16, /* rightshift */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
244 16, /* bitsize */
245 FALSE, /* pc_relative */
246 3, /* bitpos */
247 complain_overflow_dont, /* complain_on_overflow */
248 bfd_elf_generic_reloc, /* special_function */
249 "R_METAG_HI16_GOTOFF", /* name */
250 FALSE, /* partial_inplace */
251 0, /* src_mask */
252 0x0007fff8, /* dst_mask */
253 FALSE), /* pcrel_offset */
254
255 /* Low order 16 bit GOT offset */
256 HOWTO (R_METAG_LO16_GOTOFF, /* type */
257 0, /* rightshift */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
259 16, /* bitsize */
260 FALSE, /* pc_relative */
261 3, /* bitpos */
262 complain_overflow_dont, /* complain_on_overflow */
263 bfd_elf_generic_reloc, /* special_function */
264 "R_METAG_LO16_GOTOFF", /* name */
265 FALSE, /* partial_inplace */
266 0, /* src_mask */
267 0x0007fff8, /* dst_mask */
268 FALSE), /* pcrel_offset */
269
270 /* GET/SET GOT offset */
271 HOWTO (R_METAG_GETSET_GOTOFF, /* type */
272 0, /* rightshift */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
274 12, /* bitsize */
275 FALSE, /* pc_relative */
276 7, /* bitpos */
277 complain_overflow_dont, /* complain_on_overflow */
278 bfd_elf_generic_reloc, /* special_function */
279 "R_METAG_GETSET_GOTOFF", /* name */
280 FALSE, /* partial_inplace */
281 0, /* src_mask */
282 0, /* dst_mask */
283 FALSE), /* pcrel_offset */
284
285 /* GET/SET GOT relative */
286 HOWTO (R_METAG_GETSET_GOT, /* type */
287 0, /* rightshift */
288 1, /* size (0 = byte, 1 = short, 2 = long) */
289 12, /* bitsize */
290 FALSE, /* pc_relative */
291 7, /* bitpos */
292 complain_overflow_dont, /* complain_on_overflow */
293 bfd_elf_generic_reloc, /* special_function */
294 "R_METAG_GETSET_GOT", /* name */
295 FALSE, /* partial_inplace */
296 0, /* src_mask */
297 0, /* dst_mask */
298 FALSE), /* pcrel_offset */
299
300 /* High order 16 bit GOT reference */
301 HOWTO (R_METAG_HI16_GOTPC, /* type */
302 16, /* rightshift */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 16, /* bitsize */
305 FALSE, /* pc_relative */
306 3, /* bitpos */
307 complain_overflow_dont, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_METAG_HI16_GOTPC", /* name */
310 FALSE, /* partial_inplace */
311 0, /* src_mask */
312 0x0007fff8, /* dst_mask */
313 FALSE), /* pcrel_offset */
314
315 /* Low order 16 bit GOT reference */
316 HOWTO (R_METAG_LO16_GOTPC, /* type */
317 0, /* rightshift */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
320 FALSE, /* pc_relative */
321 3, /* bitpos */
322 complain_overflow_dont, /* complain_on_overflow */
323 bfd_elf_generic_reloc, /* special_function */
324 "R_METAG_LO16_GOTPC", /* name */
325 FALSE, /* partial_inplace */
326 0, /* src_mask */
327 0x0007fff8, /* dst_mask */
328 FALSE), /* pcrel_offset */
329
330 /* High order 16 bit PLT */
331 HOWTO (R_METAG_HI16_PLT, /* type */
332 16, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 3, /* bitpos */
337 complain_overflow_dont, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_METAG_HI16_PLT", /* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0x0007fff8, /* dst_mask */
343 FALSE), /* pcrel_offset */
344
345 /* Low order 16 bit PLT */
346 HOWTO (R_METAG_LO16_PLT, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 3, /* bitpos */
352 complain_overflow_dont, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_METAG_LO16_PLT", /* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0xffffffff, /* dst_mask */
358 FALSE), /* pcrel_offset */
359
360 HOWTO (R_METAG_RELBRANCH_PLT, /* type */
361 2, /* rightshift */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
363 19, /* bitsize */
364 TRUE, /* pc_relative */
365 5, /* bitpos */
366 complain_overflow_signed, /* complain_on_overflow */
367 bfd_elf_generic_reloc, /* special_function */
368 "R_METAG_RELBRANCH_PLT", /* name */
369 FALSE, /* partial_inplace */
370 0, /* src_mask */
371 0x00ffffe0, /* dst_mask */
372 FALSE), /* pcrel_offset */
373
374 /* Dummy relocs used by the linker internally. */
375 HOWTO (R_METAG_GOTOFF, /* type */
376 0, /* rightshift */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
378 32, /* bitsize */
379 FALSE, /* pc_relative */
380 0, /* bitpos */
381 complain_overflow_bitfield, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 "R_METAG_GOTOFF", /* name */
384 FALSE, /* partial_inplace */
385 0xffffffff, /* src_mask */
386 0xffffffff, /* dst_mask */
387 FALSE), /* pcrel_offset */
388
389 HOWTO (R_METAG_PLT, /* type */
390 0, /* rightshift */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
392 32, /* bitsize */
393 FALSE, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_bitfield, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 "R_METAG_GOTOFF", /* name */
398 FALSE, /* partial_inplace */
399 0xffffffff, /* src_mask */
400 0xffffffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
402
403 /* This is used only by the dynamic linker. The symbol should exist
404 both in the object being run and in some shared library. The
405 dynamic linker copies the data addressed by the symbol from the
406 shared library into the object, because the object being
407 run has to have the data at some particular address. */
408 HOWTO (R_METAG_COPY, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 32, /* bitsize */
412 FALSE, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc, /* special_function */
416 "R_METAG_COPY", /* name */
417 FALSE, /* partial_inplace */
418 0xffffffff, /* src_mask */
419 0xffffffff, /* dst_mask */
420 FALSE), /* pcrel_offset */
421
422 /* Marks a procedure linkage table entry for a symbol. */
423 HOWTO (R_METAG_JMP_SLOT, /* type */
424 0, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 32, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* special_function */
431 "R_METAG_JMP_SLOT", /* name */
432 FALSE, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
436
437 /* Used only by the dynamic linker. When the object is run, this
438 longword is set to the load address of the object, plus the
439 addend. */
440 HOWTO (R_METAG_RELATIVE, /* type */
441 0, /* rightshift */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
443 32, /* bitsize */
444 FALSE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_bitfield, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* special_function */
448 "R_METAG_RELATIVE", /* name */
449 FALSE, /* partial_inplace */
450 0xffffffff, /* src_mask */
451 0xffffffff, /* dst_mask */
452 FALSE), /* pcrel_offset */
453
454 HOWTO (R_METAG_GLOB_DAT, /* type */
455 0, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 32, /* bitsize */
458 FALSE, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_bitfield, /* complain_on_overflow */
461 bfd_elf_generic_reloc, /* special_function */
462 "R_METAG_GLOB_DAT", /* name */
463 FALSE, /* partial_inplace */
464 0xffffffff, /* src_mask */
465 0xffffffff, /* dst_mask */
466 FALSE), /* pcrel_offset */
467
468 HOWTO (R_METAG_TLS_GD, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 16, /* bitsize */
472 FALSE, /* pc_relative */
473 3, /* bitpos */
474 complain_overflow_dont, /* complain_on_overflow */
475 bfd_elf_generic_reloc, /* special_function */
476 "R_METAG_TLS_GD", /* name */
477 FALSE, /* partial_inplace */
478 0, /* src_mask */
479 0x0007fff8, /* dst_mask */
480 FALSE), /* pcrel_offset */
481
482 HOWTO (R_METAG_TLS_LDM, /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
486 FALSE, /* pc_relative */
487 3, /* bitpos */
488 complain_overflow_bitfield, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 "R_METAG_TLS_LDM", /* name */
491 FALSE, /* partial_inplace */
492 0, /* src_mask */
493 0x0007fff8, /* dst_mask */
494 FALSE), /* pcrel_offset */
495
496 HOWTO (R_METAG_TLS_LDO_HI16, /* type */
497 16, /* rightshift */
498 2, /* size (0 = byte, 1 = short, 2 = long) */
499 16, /* bitsize */
500 FALSE, /* pc_relative */
501 3, /* bitpos */
502 complain_overflow_bitfield, /* complain_on_overflow */
503 bfd_elf_generic_reloc, /* special_function */
504 "R_METAG_TLS_LDO_HI16", /* name */
505 FALSE, /* partial_inplace */
506 0, /* src_mask */
507 0x0007fff8, /* dst_mask */
508 FALSE), /* pcrel_offset */
509
510 HOWTO (R_METAG_TLS_LDO_LO16, /* type */
511 0, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 16, /* bitsize */
514 FALSE, /* pc_relative */
515 3, /* bitpos */
516 complain_overflow_bitfield, /* complain_on_overflow */
517 bfd_elf_generic_reloc, /* special_function */
518 "R_METAG_TLS_LDO_LO16", /* name */
519 FALSE, /* partial_inplace */
520 0, /* src_mask */
521 0x0007fff8, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Dummy reloc used by the linker internally. */
525 HOWTO (R_METAG_TLS_LDO, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 16, /* bitsize */
529 FALSE, /* pc_relative */
530 3, /* bitpos */
531 complain_overflow_bitfield, /* complain_on_overflow */
532 bfd_elf_generic_reloc, /* special_function */
533 "R_METAG_TLS_LDO", /* name */
534 FALSE, /* partial_inplace */
535 0, /* src_mask */
536 0x0007fff8, /* dst_mask */
537 FALSE), /* pcrel_offset */
538
539 HOWTO (R_METAG_TLS_IE, /* type */
540 2, /* rightshift */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
542 12, /* bitsize */
543 FALSE, /* pc_relative */
544 7, /* bitpos */
545 complain_overflow_dont, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_METAG_TLS_IE", /* name */
548 FALSE, /* partial_inplace */
549 0, /* src_mask */
550 0x0007ff80, /* dst_mask */
551 FALSE), /* pcrel_offset */
552
553 /* Dummy reloc used by the linker internally. */
554 HOWTO (R_METAG_TLS_IENONPIC, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 16, /* bitsize */
558 FALSE, /* pc_relative */
559 3, /* bitpos */
560 complain_overflow_dont, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_METAG_TLS_IENONPIC", /* name */
563 FALSE, /* partial_inplace */
564 0, /* src_mask */
565 0x0007fff8, /* dst_mask */
566 FALSE), /* pcrel_offset */
567
568 HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */
569 16, /* rightshift */
570 2, /* size (0 = byte, 1 = short, 2 = long) */
571 16, /* bitsize */
572 FALSE, /* pc_relative */
573 3, /* bitpos */
574 complain_overflow_dont, /* complain_on_overflow */
575 bfd_elf_generic_reloc, /* special_function */
576 "R_METAG_TLS_IENONPIC_HI16", /* name */
577 FALSE, /* partial_inplace */
578 0, /* src_mask */
579 0x0007fff8, /* dst_mask */
580 FALSE), /* pcrel_offset */
581
582 HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 16, /* bitsize */
586 FALSE, /* pc_relative */
587 3, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_METAG_TLS_IENONPIC_LO16", /* name */
591 FALSE, /* partial_inplace */
592 0, /* src_mask */
593 0x0007fff8, /* dst_mask */
594 FALSE), /* pcrel_offset */
595
596 HOWTO (R_METAG_TLS_TPOFF, /* type */
597 0, /* rightshift */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
599 32, /* bitsize */
600 FALSE, /* pc_relative */
601 0, /* bitpos */
602 complain_overflow_bitfield, /* complain_on_overflow */
603 bfd_elf_generic_reloc, /* special_function */
604 "R_METAG_TLS_TPOFF", /* name */
605 FALSE, /* partial_inplace */
606 0, /* src_mask */
607 0xffffffff, /* dst_mask */
608 FALSE), /* pcrel_offset */
609
610 HOWTO (R_METAG_TLS_DTPMOD, /* type */
611 0, /* rightshift */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
613 32, /* bitsize */
614 FALSE, /* pc_relative */
615 0, /* bitpos */
616 complain_overflow_bitfield, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 "R_METAG_TLS_DTPMOD", /* name */
619 FALSE, /* partial_inplace */
620 0, /* src_mask */
621 0xffffffff, /* dst_mask */
622 FALSE), /* pcrel_offset */
623
624 HOWTO (R_METAG_TLS_DTPOFF, /* type */
625 0, /* rightshift */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
627 32, /* bitsize */
628 FALSE, /* pc_relative */
629 0, /* bitpos */
630 complain_overflow_bitfield, /* complain_on_overflow */
631 bfd_elf_generic_reloc, /* special_function */
632 "R_METAG_TLS_DTPOFF", /* name */
633 FALSE, /* partial_inplace */
634 0, /* src_mask */
635 0xffffffff, /* dst_mask */
636 FALSE), /* pcrel_offset */
637
638 /* Dummy reloc used by the linker internally. */
639 HOWTO (R_METAG_TLS_LE, /* type */
640 0, /* rightshift */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
642 32, /* bitsize */
643 FALSE, /* pc_relative */
644 0, /* bitpos */
645 complain_overflow_bitfield, /* complain_on_overflow */
646 bfd_elf_generic_reloc, /* special_function */
647 "R_METAG_TLS_LE", /* name */
648 FALSE, /* partial_inplace */
649 0, /* src_mask */
650 0xffffffff, /* dst_mask */
651 FALSE), /* pcrel_offset */
652
653 HOWTO (R_METAG_TLS_LE_HI16, /* type */
654 16, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
657 FALSE, /* pc_relative */
658 3, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_METAG_TLS_LE_HI16", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0x0007fff8, /* dst_mask */
665 FALSE), /* pcrel_offset */
666
667 HOWTO (R_METAG_TLS_LE_LO16, /* type */
668 0, /* rightshift */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
670 16, /* bitsize */
671 FALSE, /* pc_relative */
672 3, /* bitpos */
673 complain_overflow_dont, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* special_function */
675 "R_METAG_TLS_LE_LO16", /* name */
676 FALSE, /* partial_inplace */
677 0, /* src_mask */
678 0x0007fff8, /* dst_mask */
679 FALSE), /* pcrel_offset */
680
681};
682
683#define BRANCH_BITS 19
684
685/* The GOT is typically accessed using a [GS]ETD instruction. The size of the
686 immediate offset which can be used in such instructions therefore limits
687 the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
688 is pointing to the base of the GOT then the size is limited to the maximum
689 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
690 in a [GS]ETD instruction is signed, so by setting the base address register
691 to an offset of that 0x2000 byte maximum unsigned offset from the base of
692 the GOT we can use negative offsets in addition to positive. This
693 effectively doubles the usable GOT size to 0x4000 bytes. */
694#define GOT_REG_OFFSET 0x2000
695
696struct metag_reloc_map
697{
698 bfd_reloc_code_real_type bfd_reloc_val;
699 unsigned int metag_reloc_val;
700};
701
702static const struct metag_reloc_map metag_reloc_map [] =
703 {
704 { BFD_RELOC_NONE, R_METAG_NONE },
705 { BFD_RELOC_32, R_METAG_ADDR32 },
706 { BFD_RELOC_METAG_HIADDR16, R_METAG_HIADDR16 },
707 { BFD_RELOC_METAG_LOADDR16, R_METAG_LOADDR16 },
708 { BFD_RELOC_METAG_RELBRANCH, R_METAG_RELBRANCH },
709 { BFD_RELOC_METAG_GETSETOFF, R_METAG_GETSETOFF },
710 { BFD_RELOC_VTABLE_INHERIT, R_METAG_GNU_VTINHERIT },
711 { BFD_RELOC_VTABLE_ENTRY, R_METAG_GNU_VTENTRY },
712 { BFD_RELOC_METAG_REL8, R_METAG_REL8 },
713 { BFD_RELOC_METAG_REL16, R_METAG_REL16 },
714 { BFD_RELOC_METAG_HI16_GOTOFF, R_METAG_HI16_GOTOFF },
715 { BFD_RELOC_METAG_LO16_GOTOFF, R_METAG_LO16_GOTOFF },
716 { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF },
717 { BFD_RELOC_METAG_GETSET_GOT, R_METAG_GETSET_GOT },
718 { BFD_RELOC_METAG_HI16_GOTPC, R_METAG_HI16_GOTPC },
719 { BFD_RELOC_METAG_LO16_GOTPC, R_METAG_LO16_GOTPC },
720 { BFD_RELOC_METAG_HI16_PLT, R_METAG_HI16_PLT },
721 { BFD_RELOC_METAG_LO16_PLT, R_METAG_LO16_PLT },
722 { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT },
723 { BFD_RELOC_METAG_GOTOFF, R_METAG_GOTOFF },
724 { BFD_RELOC_METAG_PLT, R_METAG_PLT },
725 { BFD_RELOC_METAG_COPY, R_METAG_COPY },
726 { BFD_RELOC_METAG_JMP_SLOT, R_METAG_JMP_SLOT },
727 { BFD_RELOC_METAG_RELATIVE, R_METAG_RELATIVE },
728 { BFD_RELOC_METAG_GLOB_DAT, R_METAG_GLOB_DAT },
729 { BFD_RELOC_METAG_TLS_GD, R_METAG_TLS_GD },
730 { BFD_RELOC_METAG_TLS_LDM, R_METAG_TLS_LDM },
731 { BFD_RELOC_METAG_TLS_LDO_HI16, R_METAG_TLS_LDO_HI16 },
732 { BFD_RELOC_METAG_TLS_LDO_LO16, R_METAG_TLS_LDO_LO16 },
733 { BFD_RELOC_METAG_TLS_LDO, R_METAG_TLS_LDO },
734 { BFD_RELOC_METAG_TLS_IE, R_METAG_TLS_IE },
735 { BFD_RELOC_METAG_TLS_IENONPIC, R_METAG_TLS_IENONPIC },
736 { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 },
737 { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 },
738 { BFD_RELOC_METAG_TLS_TPOFF, R_METAG_TLS_TPOFF },
739 { BFD_RELOC_METAG_TLS_DTPMOD, R_METAG_TLS_DTPMOD },
740 { BFD_RELOC_METAG_TLS_DTPOFF, R_METAG_TLS_DTPOFF },
741 { BFD_RELOC_METAG_TLS_LE, R_METAG_TLS_LE },
742 { BFD_RELOC_METAG_TLS_LE_HI16, R_METAG_TLS_LE_HI16 },
743 { BFD_RELOC_METAG_TLS_LE_LO16, R_METAG_TLS_LE_LO16 },
744 };
745
746enum elf_metag_stub_type
747{
748 metag_stub_long_branch,
749 metag_stub_long_branch_shared,
750 metag_stub_none
751};
752
753struct elf_metag_stub_hash_entry
754{
755 /* Base hash table entry structure. */
756 struct bfd_hash_entry bh_root;
757
758 /* The stub section. */
759 asection *stub_sec;
760
761 /* Offset within stub_sec of the beginning of this stub. */
762 bfd_vma stub_offset;
763
764 /* Given the symbol's value and its section we can determine its final
765 value when building the stubs (so the stub knows where to jump. */
766 bfd_vma target_value;
767 asection *target_section;
768
769 enum elf_metag_stub_type stub_type;
770
771 /* The symbol table entry, if any, that this was derived from. */
772 struct elf_metag_link_hash_entry *hh;
773
774 /* And the reloc addend that this was derived from. */
775 bfd_vma addend;
776
777 /* Where this stub is being called from, or, in the case of combined
778 stub sections, the first input section in the group. */
779 asection *id_sec;
780};
781
782struct elf_metag_link_hash_entry
783{
784 struct elf_link_hash_entry eh;
785
786 /* A pointer to the most recently used stub hash entry against this
787 symbol. */
788 struct elf_metag_stub_hash_entry *hsh_cache;
789
790 /* Used to count relocations for delayed sizing of relocation
791 sections. */
792 struct elf_metag_dyn_reloc_entry {
793
794 /* Next relocation in the chain. */
795 struct elf_metag_dyn_reloc_entry *hdh_next;
796
797 /* The input section of the reloc. */
798 asection *sec;
799
800 /* Number of relocs copied in this section. */
801 bfd_size_type count;
802
803 /* Number of relative relocs copied for the input section. */
804 bfd_size_type relative_count;
805 } *dyn_relocs;
806
807 enum
808 {
809 GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8
810 } tls_type;
811};
812
813struct elf_metag_link_hash_table
814{
815 /* The main hash table. */
816 struct elf_link_hash_table etab;
817
818 /* The stub hash table. */
819 struct bfd_hash_table bstab;
820
821 /* Linker stub bfd. */
822 bfd *stub_bfd;
823
824 /* Linker call-backs. */
825 asection * (*add_stub_section) (const char *, asection *);
826 void (*layout_sections_again) (void);
827
828 /* Array to keep track of which stub sections have been created, and
829 information on stub grouping. */
830 struct map_stub
831 {
832 /* This is the section to which stubs in the group will be
833 attached. */
834 asection *link_sec;
835 /* The stub section. */
836 asection *stub_sec;
837 } *stub_group;
838
839 /* Assorted information used by elf_metag_size_stubs. */
840 unsigned int bfd_count;
7292b3ac 841 unsigned int top_index;
a3c62988
NC
842 asection **input_list;
843 Elf_Internal_Sym **all_local_syms;
844
845 /* Short-cuts to get to dynamic linker sections. */
846 asection *sgot;
847 asection *sgotplt;
848 asection *srelgot;
849 asection *splt;
850 asection *srelplt;
851 asection *sdynbss;
852 asection *srelbss;
853
854 /* Small local sym cache. */
855 struct sym_cache sym_cache;
856
857 /* Data for LDM relocations. */
858 union
859 {
860 bfd_signed_vma refcount;
861 bfd_vma offset;
862 } tls_ldm_got;
863};
864
865/* Return the base vma address which should be subtracted from the
866 real address when resolving a dtpoff relocation. This is PT_TLS
867 segment p_vaddr. */
868static bfd_vma
869dtpoff_base (struct bfd_link_info *info)
870{
871 /* If tls_sec is NULL, we should have signalled an error already. */
872 if (elf_hash_table (info)->tls_sec == NULL)
873 return 0;
874 return elf_hash_table (info)->tls_sec->vma;
875}
876
877/* Return the relocation value for R_METAG_TLS_IE */
878static bfd_vma
879tpoff (struct bfd_link_info *info, bfd_vma address)
880{
881 /* If tls_sec is NULL, we should have signalled an error already. */
882 if (elf_hash_table (info)->tls_sec == NULL)
883 return 0;
884 /* METAG TLS ABI is variant I and static TLS blocks start just after
885 tcbhead structure which has 2 pointer fields. */
886 return (address - elf_hash_table (info)->tls_sec->vma
887 + align_power ((bfd_vma) 8,
888 elf_hash_table (info)->tls_sec->alignment_power));
889}
890
891static void
892metag_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
893 arelent *cache_ptr,
894 Elf_Internal_Rela *dst)
895{
896 unsigned int r_type;
897
898 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
899 if (r_type >= (unsigned int) R_METAG_MAX)
900 {
64d29018 901 _bfd_error_handler (_("%B: invalid METAG reloc number: %d"), abfd, r_type);
5860e3f8
NC
902 r_type = 0;
903 }
a3c62988
NC
904 cache_ptr->howto = & elf_metag_howto_table [r_type];
905}
906
907static reloc_howto_type *
908metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
909 bfd_reloc_code_real_type code)
910{
911 unsigned int i;
912
913 for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++)
914 if (metag_reloc_map [i].bfd_reloc_val == code)
915 return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val];
916
917 return NULL;
918}
919
920static reloc_howto_type *
921metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
922 const char *r_name)
923{
924 unsigned int i;
925
926 for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++)
927 if (elf_metag_howto_table[i].name != NULL
928 && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0)
929 return &elf_metag_howto_table[i];
930
931 return NULL;
932}
933
934/* Various hash macros and functions. */
935#define metag_link_hash_table(p) \
936 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
937 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
938
939#define metag_elf_hash_entry(ent) \
940 ((struct elf_metag_link_hash_entry *)(ent))
941
942#define metag_stub_hash_entry(ent) \
943 ((struct elf_metag_stub_hash_entry *)(ent))
944
945#define metag_stub_hash_lookup(table, string, create, copy) \
946 ((struct elf_metag_stub_hash_entry *) \
947 bfd_hash_lookup ((table), (string), (create), (copy)))
948
949#define metag_elf_local_got_tls_type(abfd) \
950 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
951
952/* Assorted hash table functions. */
953
954/* Initialize an entry in the stub hash table. */
955
956static struct bfd_hash_entry *
957stub_hash_newfunc (struct bfd_hash_entry *entry,
958 struct bfd_hash_table *table,
959 const char *string)
960{
961 /* Allocate the structure if it has not already been allocated by a
962 subclass. */
963 if (entry == NULL)
964 {
965 entry = bfd_hash_allocate (table,
966 sizeof (struct elf_metag_stub_hash_entry));
967 if (entry == NULL)
968 return entry;
969 }
970
971 /* Call the allocation method of the superclass. */
972 entry = bfd_hash_newfunc (entry, table, string);
973 if (entry != NULL)
974 {
975 struct elf_metag_stub_hash_entry *hsh;
976
977 /* Initialize the local fields. */
978 hsh = (struct elf_metag_stub_hash_entry *) entry;
979 hsh->stub_sec = NULL;
980 hsh->stub_offset = 0;
981 hsh->target_value = 0;
982 hsh->target_section = NULL;
983 hsh->stub_type = metag_stub_long_branch;
984 hsh->hh = NULL;
985 hsh->id_sec = NULL;
986 }
987
988 return entry;
989}
990
991/* Initialize an entry in the link hash table. */
992
993static struct bfd_hash_entry *
994metag_link_hash_newfunc (struct bfd_hash_entry *entry,
995 struct bfd_hash_table *table,
996 const char *string)
997{
998 /* Allocate the structure if it has not already been allocated by a
999 subclass. */
1000 if (entry == NULL)
1001 {
1002 entry = bfd_hash_allocate (table,
1003 sizeof (struct elf_metag_link_hash_entry));
1004 if (entry == NULL)
1005 return entry;
1006 }
1007
1008 /* Call the allocation method of the superclass. */
1009 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1010 if (entry != NULL)
1011 {
1012 struct elf_metag_link_hash_entry *hh;
1013
1014 /* Initialize the local fields. */
1015 hh = (struct elf_metag_link_hash_entry *) entry;
1016 hh->hsh_cache = NULL;
1017 hh->dyn_relocs = NULL;
1018 hh->tls_type = GOT_UNKNOWN;
1019 }
1020
1021 return entry;
1022}
1023
68faa637
AM
1024/* Free the derived linker hash table. */
1025
1026static void
d495ab0d 1027elf_metag_link_hash_table_free (bfd *obfd)
68faa637
AM
1028{
1029 struct elf_metag_link_hash_table *htab
d495ab0d 1030 = (struct elf_metag_link_hash_table *) obfd->link.hash;
68faa637
AM
1031
1032 bfd_hash_table_free (&htab->bstab);
d495ab0d 1033 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
1034}
1035
a3c62988
NC
1036/* Create the derived linker hash table. The Meta ELF port uses the derived
1037 hash table to keep information specific to the Meta ELF linker (without
1038 using static variables). */
1039
1040static struct bfd_link_hash_table *
1041elf_metag_link_hash_table_create (bfd *abfd)
1042{
1043 struct elf_metag_link_hash_table *htab;
1044 bfd_size_type amt = sizeof (*htab);
1045
7bf52ea2 1046 htab = bfd_zmalloc (amt);
a3c62988
NC
1047 if (htab == NULL)
1048 return NULL;
1049
1050 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
1051 metag_link_hash_newfunc,
1052 sizeof (struct elf_metag_link_hash_entry),
1053 METAG_ELF_DATA))
1054 {
1055 free (htab);
1056 return NULL;
1057 }
1058
1059 /* Init the stub hash table too. */
1060 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
1061 sizeof (struct elf_metag_stub_hash_entry)))
d495ab0d
AM
1062 {
1063 _bfd_elf_link_hash_table_free (abfd);
1064 return NULL;
1065 }
1066 htab->etab.root.hash_table_free = elf_metag_link_hash_table_free;
a3c62988 1067
a3c62988
NC
1068 return &htab->etab.root;
1069}
1070
a3c62988
NC
1071/* Section name for stubs is the associated section name plus this
1072 string. */
1073#define STUB_SUFFIX ".stub"
1074
1075/* Build a name for an entry in the stub hash table. */
1076
1077static char *
1078metag_stub_name (const asection *input_section,
1079 const asection *sym_sec,
1080 const struct elf_metag_link_hash_entry *hh,
1081 const Elf_Internal_Rela *rel)
1082{
1083 char *stub_name;
1084 bfd_size_type len;
1085
1086 if (hh)
1087 {
1088 len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1;
1089 stub_name = bfd_malloc (len);
1090 if (stub_name != NULL)
1091 {
1092 sprintf (stub_name, "%08x_%s+%x",
1093 input_section->id & 0xffffffff,
1094 hh->eh.root.root.string,
1095 (int) rel->r_addend & 0xffffffff);
1096 }
1097 }
1098 else
1099 {
1100 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1101 stub_name = bfd_malloc (len);
1102 if (stub_name != NULL)
1103 {
1104 sprintf (stub_name, "%08x_%x:%x+%x",
1105 input_section->id & 0xffffffff,
1106 sym_sec->id & 0xffffffff,
1107 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1108 (int) rel->r_addend & 0xffffffff);
1109 }
1110 }
1111 return stub_name;
1112}
1113
1114/* Look up an entry in the stub hash. Stub entries are cached because
1115 creating the stub name takes a bit of time. */
1116
1117static struct elf_metag_stub_hash_entry *
1118metag_get_stub_entry (const asection *input_section,
1119 const asection *sym_sec,
1120 struct elf_metag_link_hash_entry *hh,
1121 const Elf_Internal_Rela *rel,
1122 struct elf_metag_link_hash_table *htab)
1123{
1124 struct elf_metag_stub_hash_entry *hsh;
1125 const asection *id_sec;
1126
1127 /* If this input section is part of a group of sections sharing one
1128 stub section, then use the id of the first section in the group.
1129 Stub names need to include a section id, as there may well be
1130 more than one stub used to reach say, printf, and we need to
1131 distinguish between them. */
1132 id_sec = htab->stub_group[input_section->id].link_sec;
1133
1134 if (hh != NULL && hh->hsh_cache != NULL
1135 && hh->hsh_cache->hh == hh
1136 && hh->hsh_cache->id_sec == id_sec)
1137 {
1138 hsh = hh->hsh_cache;
1139 }
1140 else
1141 {
1142 char *stub_name;
1143
1144 stub_name = metag_stub_name (id_sec, sym_sec, hh, rel);
1145 if (stub_name == NULL)
1146 return NULL;
1147
1148 hsh = metag_stub_hash_lookup (&htab->bstab,
1149 stub_name, FALSE, FALSE);
1150
1151 if (hh != NULL)
1152 hh->hsh_cache = hsh;
1153
1154 free (stub_name);
1155 }
1156
1157 return hsh;
1158}
1159
1160/* Add a new stub entry to the stub hash. Not all fields of the new
1161 stub entry are initialised. */
1162
1163static struct elf_metag_stub_hash_entry *
1164metag_add_stub (const char *stub_name,
1165 asection *section,
1166 struct elf_metag_link_hash_table *htab)
1167{
1168 asection *link_sec;
1169 asection *stub_sec;
1170 struct elf_metag_stub_hash_entry *hsh;
1171
1172 link_sec = htab->stub_group[section->id].link_sec;
1173 stub_sec = htab->stub_group[section->id].stub_sec;
1174 if (stub_sec == NULL)
1175 {
1176 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1177 if (stub_sec == NULL)
1178 {
1179 size_t namelen;
1180 bfd_size_type len;
1181 char *s_name;
1182
1183 namelen = strlen (link_sec->name);
1184 len = namelen + sizeof (STUB_SUFFIX);
1185 s_name = bfd_alloc (htab->stub_bfd, len);
1186 if (s_name == NULL)
1187 return NULL;
1188
1189 memcpy (s_name, link_sec->name, namelen);
1190 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1191
1192 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1193 if (stub_sec == NULL)
1194 return NULL;
1195 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1196 }
1197 htab->stub_group[section->id].stub_sec = stub_sec;
1198 }
1199
1200 /* Enter this entry into the linker stub hash table. */
1201 hsh = metag_stub_hash_lookup (&htab->bstab, stub_name,
1202 TRUE, FALSE);
1203 if (hsh == NULL)
1204 {
4eca0228
AM
1205 _bfd_error_handler (_("%B: cannot create stub entry %s"),
1206 section->owner, stub_name);
a3c62988
NC
1207 return NULL;
1208 }
1209
1210 hsh->stub_sec = stub_sec;
1211 hsh->stub_offset = 0;
1212 hsh->id_sec = link_sec;
1213 return hsh;
1214}
1215
1216/* Check a signed integer value can be represented in the given number
1217 of bits. */
1218
1219static bfd_boolean
1220within_signed_range (int value, unsigned int bits)
1221{
1222 int min_val = -(1 << (bits - 1));
1223 int max_val = (1 << (bits - 1)) - 1;
1224 return (value <= max_val) && (value >= min_val);
1225}
1226
1227/* Perform a relocation as part of a final link. */
1228
1229static bfd_reloc_status_type
1230metag_final_link_relocate (reloc_howto_type *howto,
1231 bfd *input_bfd,
1232 asection *input_section,
1233 bfd_byte *contents,
1234 Elf_Internal_Rela *rel,
1235 bfd_vma relocation,
1236 struct elf_metag_link_hash_entry *hh,
1237 struct elf_metag_link_hash_table *htab,
1238 asection *sym_sec)
1239{
1240 bfd_reloc_status_type r = bfd_reloc_ok;
1241 bfd_byte *hit_data = contents + rel->r_offset;
1242 int opcode, op_shift, op_extended, l1, l2;
1243 bfd_signed_vma srel, addend = rel->r_addend;
1244 struct elf_metag_stub_hash_entry *hsh = NULL;
1245 bfd_vma location;
1246
1247 /* Find out where we are and where we're going. */
1248 location = (rel->r_offset +
1249 input_section->output_offset +
1250 input_section->output_section->vma);
1251
1252 switch (howto->type)
1253 {
1254 case R_METAG_RELBRANCH:
1255 case R_METAG_RELBRANCH_PLT:
1256 /* Make it a pc relative offset. */
1257 relocation -= location;
1258 break;
1259 case R_METAG_TLS_GD:
1260 case R_METAG_TLS_IE:
1261 relocation -= elf_gp (input_section->output_section->owner);
1262 break;
1263 default:
1264 break;
1265 }
1266
1267 switch (howto->type)
1268 {
1269 case R_METAG_RELBRANCH_PLT:
1270 case R_METAG_RELBRANCH:
1271 opcode = bfd_get_32 (input_bfd, hit_data);
1272
1273 srel = (bfd_signed_vma) relocation;
1274 srel += addend;
1275
1276 /* If the branch is out of reach, then redirect the
1277 call to the local stub for this function. */
1278 if (srel > ((1 << (BRANCH_BITS + 1)) - 1) ||
1279 (srel < - (1 << (BRANCH_BITS + 1))))
1280 {
1281 if (sym_sec == NULL)
1282 break;
1283
1284 hsh = metag_get_stub_entry (input_section, sym_sec,
1285 hh, rel, htab);
1286 if (hsh == NULL)
1287 return bfd_reloc_undefined;
1288
1289 /* Munge up the value and addend so that we call the stub
1290 rather than the procedure directly. */
1291 srel = (hsh->stub_offset
1292 + hsh->stub_sec->output_offset
1293 + hsh->stub_sec->output_section->vma);
1294 srel -= location;
1295 }
1296
1297 srel = srel >> 2;
1298
1299 if (!within_signed_range (srel, BRANCH_BITS))
1300 {
1301 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1302 srel = 0;
1303 else
1304 return bfd_reloc_overflow;
1305 }
1306
1307 opcode &= ~(0x7ffff << 5);
1308 opcode |= ((srel & 0x7ffff) << 5);
1309
1310 bfd_put_32 (input_bfd, opcode, hit_data);
1311 break;
1312 case R_METAG_GETSETOFF:
1313 case R_METAG_GETSET_GOT:
1314 case R_METAG_GETSET_GOTOFF:
1315 opcode = bfd_get_32 (input_bfd, hit_data);
1316
1317 srel = (bfd_signed_vma) relocation;
1318 srel += addend;
1319
1320 /* Is this a standard or extended GET/SET? */
1321 if ((opcode & 0xf0000000) == 0xa0000000)
1322 {
1323 /* Extended GET/SET. */
1324 l1 = opcode & 0x2;
1325 l2 = opcode & 0x4;
1326 op_extended = 1;
1327 }
1328 else
1329 {
1330 /* Standard GET/SET. */
1331 l1 = opcode & 0x01000000;
1332 l2 = opcode & 0x04000000;
1333 op_extended = 0;
1334 }
1335
1336 /* Calculate the width of the GET/SET and how much we need to
1337 shift the result by. */
1338 if (l2)
1339 if (l1)
1340 op_shift = 3;
1341 else
1342 op_shift = 2;
1343 else
1344 if (l1)
1345 op_shift = 1;
1346 else
1347 op_shift = 0;
1348
1349 /* GET/SET offsets are scaled by the width of the transfer. */
1350 srel = srel >> op_shift;
1351
1352 /* Extended GET/SET has signed 12 bits of offset, standard has
1353 signed 6 bits. */
1354 if (op_extended)
1355 {
1356 if (!within_signed_range (srel, 12))
1357 {
1358 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1359 srel = 0;
1360 else
1361 return bfd_reloc_overflow;
1362 }
1363 opcode &= ~(0xfff << 7);
1364 opcode |= ((srel & 0xfff) << 7);
1365 }
1366 else
1367 {
1368 if (!within_signed_range (srel, 5))
1369 {
1370 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1371 srel = 0;
1372 else
1373 return bfd_reloc_overflow;
1374 }
1375 opcode &= ~(0x3f << 8);
1376 opcode |= ((srel & 0x3f) << 8);
1377 }
1378
1379 bfd_put_32 (input_bfd, opcode, hit_data);
1380 break;
1381 case R_METAG_TLS_GD:
1382 case R_METAG_TLS_LDM:
1383 opcode = bfd_get_32 (input_bfd, hit_data);
1384
1385 if ((bfd_signed_vma)relocation < 0)
1386 {
1387 /* sign extend immediate */
1388 if ((opcode & 0xf2000001) == 0x02000000)
1389 {
1390 /* ADD De.e,Dx.r,#I16 */
1391 /* set SE bit */
1392 opcode |= (1 << 1);
1393 } else
1394 return bfd_reloc_overflow;
1395 }
1396
1397 bfd_put_32 (input_bfd, opcode, hit_data);
1398
1399 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1400 contents, rel->r_offset,
1401 relocation, rel->r_addend);
1402 break;
1403 default:
1404 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1405 contents, rel->r_offset,
1406 relocation, rel->r_addend);
1407 }
1408
1409 return r;
1410}
1411
1412/* This is defined because R_METAG_NONE != 0...
1413 See RELOC_AGAINST_DISCARDED_SECTION for details. */
1414#define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1415 rel, relend, howto, contents) \
1416 { \
1417 _bfd_clear_contents (howto, input_bfd, input_section, \
1418 contents + rel->r_offset); \
1419 \
0e1862bb 1420 if (bfd_link_relocatable (info) \
a3c62988
NC
1421 && (input_section->flags & SEC_DEBUGGING)) \
1422 { \
1423 /* Only remove relocations in debug sections since other \
1424 sections may require relocations. */ \
1425 Elf_Internal_Shdr *rel_hdr; \
1426 \
1427 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1428 \
1429 /* Avoid empty output section. */ \
1430 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
1431 { \
1432 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1433 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
1434 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1435 \
1436 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
1437 \
1438 input_section->reloc_count--; \
1439 relend--; \
1440 rel--; \
1441 continue; \
1442 } \
1443 } \
1444 \
1445 rel->r_info = R_METAG_NONE; \
1446 rel->r_addend = 0; \
1447 continue; \
1448 }
1449
1450/* Relocate a META ELF section.
1451
1452The RELOCATE_SECTION function is called by the new ELF backend linker
1453to handle the relocations for a section.
1454
1455The relocs are always passed as Rela structures; if the section
1456actually uses Rel structures, the r_addend field will always be
1457zero.
1458
1459This function is responsible for adjusting the section contents as
1460necessary, and (if using Rela relocs and generating a relocatable
1461output file) adjusting the reloc addend as necessary.
1462
1463This function does not have to worry about setting the reloc
1464address or the reloc symbol index.
1465
1466LOCAL_SYMS is a pointer to the swapped in local symbols.
1467
1468LOCAL_SECTIONS is an array giving the section in the input file
1469corresponding to the st_shndx field of each local symbol.
1470
1471The global hash table entry for the global symbols can be found
1472via elf_sym_hashes (input_bfd).
1473
1474When generating relocatable output, this function must handle
1475STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1476going to be the section symbol corresponding to the output
1477section, which means that the addend must be adjusted
1478accordingly. */
1479
1480static bfd_boolean
1481elf_metag_relocate_section (bfd *output_bfd,
1482 struct bfd_link_info *info,
1483 bfd *input_bfd,
1484 asection *input_section,
1485 bfd_byte *contents,
1486 Elf_Internal_Rela *relocs,
1487 Elf_Internal_Sym *local_syms,
1488 asection **local_sections)
1489{
1490 bfd_vma *local_got_offsets;
1491 Elf_Internal_Shdr *symtab_hdr;
1492 struct elf_link_hash_entry **eh_syms;
1493 struct elf_metag_link_hash_table *htab;
1494 Elf_Internal_Rela *rel;
1495 Elf_Internal_Rela *relend;
1496 asection *sreloc;
1497
1498 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1499 eh_syms = elf_sym_hashes (input_bfd);
1500 relend = relocs + input_section->reloc_count;
1501
1502 htab = metag_link_hash_table (info);
1503 local_got_offsets = elf_local_got_offsets (input_bfd);
1504
1505 sreloc = NULL;
1506
1507 for (rel = relocs; rel < relend; rel ++)
1508 {
1509 reloc_howto_type *howto;
1510 unsigned long r_symndx;
1511 Elf_Internal_Sym *sym;
1512 asection *sec;
1513 struct elf_metag_link_hash_entry *hh;
1514 bfd_vma relocation;
1515 bfd_reloc_status_type r;
1516 const char *name;
1517 int r_type;
1518
1519 r_type = ELF32_R_TYPE (rel->r_info);
1520
1521 if (r_type == R_METAG_GNU_VTINHERIT
1522 || r_type == R_METAG_GNU_VTENTRY
1523 || r_type == R_METAG_NONE)
1524 continue;
1525
1526 r_symndx = ELF32_R_SYM (rel->r_info);
1527
1528 howto = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info);
1529 hh = NULL;
1530 sym = NULL;
1531 sec = NULL;
1532
1533 if (r_symndx < symtab_hdr->sh_info)
1534 {
1535 sym = local_syms + r_symndx;
1536 sec = local_sections [r_symndx];
1537 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1538
1539 name = bfd_elf_string_from_elf_section
1540 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1541 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1542 }
1543 else
1544 {
1545 struct elf_link_hash_entry *eh;
62d887d4 1546 bfd_boolean unresolved_reloc, warned, ignored;
a3c62988
NC
1547
1548 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1549 r_symndx, symtab_hdr, eh_syms,
1550 eh, sec, relocation,
62d887d4 1551 unresolved_reloc, warned, ignored);
a3c62988
NC
1552
1553 name = eh->root.root.string;
1554 hh = (struct elf_metag_link_hash_entry *) eh;
1555 }
1556
1557 if (sec != NULL && discarded_section (sec))
1558 METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1559 rel, relend, howto, contents);
1560
0e1862bb 1561 if (bfd_link_relocatable (info))
a3c62988
NC
1562 continue;
1563
1564 switch (r_type)
1565 {
1566 case R_METAG_ADDR32:
1567 case R_METAG_RELBRANCH:
1568 if ((input_section->flags & SEC_ALLOC) == 0)
1569 break;
1570
0e1862bb 1571 if ((bfd_link_pic (info)
a3c62988
NC
1572 && r_symndx != STN_UNDEF
1573 && (input_section->flags & SEC_ALLOC) != 0
1574 && (r_type != R_METAG_RELBRANCH
1575 || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
0e1862bb 1576 || (!bfd_link_pic (info)
a3c62988
NC
1577 && hh != NULL
1578 && hh->eh.dynindx != -1
1579 && !hh->eh.non_got_ref
1580 && ((hh->eh.def_dynamic
1581 && !hh->eh.def_regular)
1582 || hh->eh.root.type == bfd_link_hash_undefweak
1583 || hh->eh.root.type == bfd_link_hash_undefined)))
1584 {
1585 Elf_Internal_Rela outrel;
1586 bfd_boolean skip, relocate;
1587 bfd_byte *loc;
1588
1589 /* When generating a shared object, these relocations
1590 are copied into the output file to be resolved at run
1591 time. */
1592
1593 sreloc = elf_section_data (input_section)->sreloc;
1594 BFD_ASSERT (sreloc != NULL);
1595
1596 skip = FALSE;
1597 relocate = FALSE;
1598
1599 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1600 info,
1601 input_section,
1602 rel->r_offset);
1603 if (outrel.r_offset == (bfd_vma) -1)
1604 skip = TRUE;
1605 else if (outrel.r_offset == (bfd_vma) -2)
1606 skip = TRUE, relocate = TRUE;
1607 outrel.r_offset += (input_section->output_section->vma
1608 + input_section->output_offset);
1609
1610 if (skip)
1611 {
1612 memset (&outrel, 0, sizeof outrel);
1613 outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE);
1614 }
1615 else if (r_type == R_METAG_RELBRANCH)
1616 {
1617 BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1);
1618 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1619 outrel.r_addend = rel->r_addend;
1620 }
1621 else
1622 {
1623 /* h->dynindx may be -1 if this symbol was marked to
1624 become local. */
1625 if (hh == NULL
1626 || ((info->symbolic || hh->eh.dynindx == -1)
1627 && hh->eh.def_regular))
1628 {
1629 relocate = TRUE;
1630 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1631 outrel.r_addend = relocation + rel->r_addend;
1632 }
1633 else
1634 {
1635 BFD_ASSERT (hh->eh.dynindx != -1);
1636 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1637 outrel.r_addend = rel->r_addend;
1638 }
1639 }
1640
1641 loc = sreloc->contents;
1642 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
1643 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
1644 ++sreloc->reloc_count;
1645
1646 /* If this reloc is against an external symbol, we do
1647 not want to fiddle with the addend. Otherwise, we
1648 need to include the symbol value so that it becomes
1649 an addend for the dynamic reloc. */
1650 if (! relocate)
1651 continue;
1652 }
1653 break;
1654
1655 case R_METAG_RELBRANCH_PLT:
1656 /* Relocation is to the entry for this symbol in the
1657 procedure linkage table. */
1658
1659 if (hh == NULL)
1660 break;
1661
1662 if (hh->eh.forced_local)
1663 break;
1664
1665 if (hh->eh.plt.offset == (bfd_vma) -1 ||
1666 htab->splt == NULL)
1667 {
1668 /* We didn't make a PLT entry for this symbol. This
1669 happens when statically linking PIC code, or when
1670 using -Bsymbolic. */
1671 break;
1672 }
1673
1674 relocation = (htab->splt->output_section->vma
1675 + htab->splt->output_offset
1676 + hh->eh.plt.offset);
1677 break;
1678 case R_METAG_HI16_GOTPC:
1679 case R_METAG_LO16_GOTPC:
1680 BFD_ASSERT (htab->sgot != NULL);
1681
1682 relocation = (htab->sgot->output_section->vma +
1683 htab->sgot->output_offset);
1684 relocation += GOT_REG_OFFSET;
1685 relocation -= (input_section->output_section->vma
1686 + input_section->output_offset
1687 + rel->r_offset);
1688 break;
1689 case R_METAG_HI16_GOTOFF:
1690 case R_METAG_LO16_GOTOFF:
1691 case R_METAG_GETSET_GOTOFF:
1692 BFD_ASSERT (htab->sgot != NULL);
1693
1694 relocation -= (htab->sgot->output_section->vma +
1695 htab->sgot->output_offset);
1696 relocation -= GOT_REG_OFFSET;
1697 break;
1698 case R_METAG_GETSET_GOT:
1699 {
1700 bfd_vma off;
1701 bfd_boolean do_got = 0;
1702
1703 /* Relocation is to the entry for this symbol in the
1704 global offset table. */
1705 if (hh != NULL)
1706 {
1707 bfd_boolean dyn;
1708
1709 off = hh->eh.got.offset;
1710 dyn = htab->etab.dynamic_sections_created;
0e1862bb
L
1711 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1712 bfd_link_pic (info),
a3c62988
NC
1713 &hh->eh))
1714 {
1715 /* If we aren't going to call finish_dynamic_symbol,
1716 then we need to handle initialisation of the .got
1717 entry and create needed relocs here. Since the
1718 offset must always be a multiple of 4, we use the
1719 least significant bit to record whether we have
1720 initialised it already. */
1721 if ((off & 1) != 0)
1722 off &= ~1;
1723 else
1724 {
1725 hh->eh.got.offset |= 1;
1726 do_got = 1;
1727 }
1728 }
1729 }
1730 else
1731 {
1732 /* Local symbol case. */
1733 if (local_got_offsets == NULL)
1734 abort ();
1735
1736 off = local_got_offsets[r_symndx];
1737
1738 /* The offset must always be a multiple of 4. We use
1739 the least significant bit to record whether we have
1740 already generated the necessary reloc. */
1741 if ((off & 1) != 0)
1742 off &= ~1;
1743 else
1744 {
1745 local_got_offsets[r_symndx] |= 1;
1746 do_got = 1;
1747 }
1748 }
1749
1750 if (do_got)
1751 {
0e1862bb 1752 if (bfd_link_pic (info))
a3c62988
NC
1753 {
1754 /* Output a dynamic relocation for this GOT entry.
1755 In this case it is relative to the base of the
1756 object because the symbol index is zero. */
1757 Elf_Internal_Rela outrel;
1758 bfd_byte *loc;
1759 asection *s = htab->srelgot;
1760
1761 outrel.r_offset = (off
1762 + htab->sgot->output_offset
1763 + htab->sgot->output_section->vma);
1764 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1765 outrel.r_addend = relocation;
1766 loc = s->contents;
1767 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1768 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1769 }
1770 else
1771 bfd_put_32 (output_bfd, relocation,
1772 htab->sgot->contents + off);
1773 }
1774
1775 if (off >= (bfd_vma) -2)
1776 abort ();
1777
1778 relocation = off - GOT_REG_OFFSET;
1779 }
1780 break;
1781 case R_METAG_TLS_GD:
1782 case R_METAG_TLS_IE:
1783 {
1784 /* XXXMJF There is room here for optimisations. For example
1785 converting from GD->IE, etc. */
1786 bfd_vma off;
1787 int indx;
1788 char tls_type;
1789
1790 if (htab->sgot == NULL)
1791 abort();
1792
1793 indx = 0;
1794 if (hh != NULL)
1795 {
1796 bfd_boolean dyn;
1797 dyn = htab->etab.dynamic_sections_created;
1798
0e1862bb
L
1799 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1800 bfd_link_pic (info),
1801 &hh->eh)
1802 && (!bfd_link_pic (info)
a3c62988
NC
1803 || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
1804 {
1805 indx = hh->eh.dynindx;
1806 }
1807 off = hh->eh.got.offset;
1808 tls_type = hh->tls_type;
1809 }
1810 else
1811 {
1812 /* Local symbol case. */
1813 if (local_got_offsets == NULL)
1814 abort ();
1815
1816 off = local_got_offsets[r_symndx];
1817 tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx];
1818 }
1819
1820 if (tls_type == GOT_UNKNOWN)
1821 abort();
1822
1823 if ((off & 1) != 0)
1824 off &= ~1;
1825 else
1826 {
1827 bfd_boolean need_relocs = FALSE;
1828 Elf_Internal_Rela outrel;
1829 bfd_byte *loc = NULL;
1830 int cur_off = off;
1831
1832 /* The GOT entries have not been initialized yet. Do it
1833 now, and emit any relocations. If both an IE GOT and a
1834 GD GOT are necessary, we emit the GD first. */
1835
0e1862bb 1836 if ((bfd_link_pic (info) || indx != 0)
a3c62988
NC
1837 && (hh == NULL
1838 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
1839 || hh->eh.root.type != bfd_link_hash_undefweak))
1840 {
1841 need_relocs = TRUE;
1842 loc = htab->srelgot->contents;
1843 /* FIXME (CAO): Should this be reloc_count++ ? */
1844 loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela);
1845 }
1846
1847 if (tls_type & GOT_TLS_GD)
1848 {
1849 if (need_relocs)
1850 {
1851 outrel.r_offset = (cur_off
1852 + htab->sgot->output_section->vma
1853 + htab->sgot->output_offset);
1854 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD);
1855 outrel.r_addend = 0;
1856 bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off);
1857
1858 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1859 htab->srelgot->reloc_count++;
1860 loc += sizeof (Elf32_External_Rela);
1861
1862 if (indx == 0)
1863 bfd_put_32 (output_bfd, 0,
1864 htab->sgot->contents + cur_off + 4);
1865 else
1866 {
1867 bfd_put_32 (output_bfd, 0,
1868 htab->sgot->contents + cur_off + 4);
1869 outrel.r_info = ELF32_R_INFO (indx,
1870 R_METAG_TLS_DTPOFF);
1871 outrel.r_offset += 4;
1872 bfd_elf32_swap_reloca_out (output_bfd,
1873 &outrel, loc);
1874 htab->srelgot->reloc_count++;
1875 loc += sizeof (Elf32_External_Rela);
1876 }
1877 }
1878 else
1879 {
1880 /* We don't support changing the TLS model. */
1881 abort ();
1882 }
1883
1884 cur_off += 8;
1885 }
1886
1887 if (tls_type & GOT_TLS_IE)
1888 {
1889 if (need_relocs)
1890 {
1891 outrel.r_offset = (cur_off
1892 + htab->sgot->output_section->vma
1893 + htab->sgot->output_offset);
1894 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF);
1895
1896 if (indx == 0)
1897 outrel.r_addend = relocation - dtpoff_base (info);
1898 else
1899 outrel.r_addend = 0;
1900
1901 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1902 htab->srelgot->reloc_count++;
1903 loc += sizeof (Elf32_External_Rela);
1904 }
1905 else
1906 bfd_put_32 (output_bfd, tpoff (info, relocation),
1907 htab->sgot->contents + cur_off);
1908
1909 cur_off += 4;
1910 }
1911
1912 if (hh != NULL)
1913 hh->eh.got.offset |= 1;
1914 else
1915 local_got_offsets[r_symndx] |= 1;
1916 }
1917
1918 /* Add the base of the GOT to the relocation value. */
1919 relocation = off - GOT_REG_OFFSET;
1920
1921 break;
1922 }
1923
1924 case R_METAG_TLS_IENONPIC_HI16:
1925 case R_METAG_TLS_IENONPIC_LO16:
1926 case R_METAG_TLS_LE_HI16:
1927 case R_METAG_TLS_LE_LO16:
0e1862bb 1928 if (bfd_link_pic (info))
a3c62988 1929 {
4eca0228 1930 _bfd_error_handler
a3c62988
NC
1931 (_("%B(%A+0x%lx): R_METAG_TLS_LE/IENONPIC relocation not permitted in shared object"),
1932 input_bfd, input_section,
1933 (long) rel->r_offset, howto->name);
1934 return FALSE;
1935 }
1936 else
1937 relocation = tpoff (info, relocation);
1938 break;
1939 case R_METAG_TLS_LDO_HI16:
1940 case R_METAG_TLS_LDO_LO16:
0e1862bb 1941 if (! bfd_link_pic (info))
a3c62988
NC
1942 relocation = tpoff (info, relocation);
1943 else
1944 relocation -= dtpoff_base (info);
1945 break;
1946 case R_METAG_TLS_LDM:
1947 {
1948 bfd_vma off;
1949
1950 if (htab->sgot == NULL)
1951 abort();
1952 off = htab->tls_ldm_got.offset;
1953 if (off & 1)
1954 off &= ~1;
1955 else
1956 {
1957 Elf_Internal_Rela outrel;
1958 bfd_byte *loc;
1959
1960 outrel.r_offset = (off
1961 + htab->sgot->output_section->vma
1962 + htab->sgot->output_offset);
1963
1964 outrel.r_addend = 0;
1965 outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD);
1966 loc = htab->srelgot->contents;
1967 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1968 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1969 htab->tls_ldm_got.offset |= 1;
1970 }
1971
1972 relocation = off - GOT_REG_OFFSET;
1973 break;
1974 }
1975 default:
1976 break;
1977 }
1978
1979 r = metag_final_link_relocate (howto, input_bfd, input_section,
1980 contents, rel, relocation, hh, htab,
1981 sec);
1982
1983 if (r != bfd_reloc_ok)
1984 {
1985 const char * msg = (const char *) NULL;
1986
1987 switch (r)
1988 {
1989 case bfd_reloc_overflow:
1a72702b 1990 (*info->callbacks->reloc_overflow)
a3c62988
NC
1991 (info, (hh ? &hh->eh.root : NULL), name, howto->name,
1992 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1993 break;
1994
1995 case bfd_reloc_undefined:
1a72702b
AM
1996 (*info->callbacks->undefined_symbol)
1997 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
a3c62988
NC
1998 break;
1999
2000 case bfd_reloc_outofrange:
2001 msg = _("internal error: out of range error");
2002 break;
2003
2004 case bfd_reloc_notsupported:
2005 msg = _("internal error: unsupported relocation error");
2006 break;
2007
2008 case bfd_reloc_dangerous:
2009 msg = _("internal error: dangerous relocation");
2010 break;
2011
2012 default:
2013 msg = _("internal error: unknown error");
2014 break;
2015 }
2016
2017 if (msg)
1a72702b
AM
2018 (*info->callbacks->warning) (info, msg, name, input_bfd,
2019 input_section, rel->r_offset);
a3c62988
NC
2020 }
2021 }
2022
2023 return TRUE;
2024}
2025
2026/* Create the .plt and .got sections, and set up our hash table
2027 short-cuts to various dynamic sections. */
2028
2029static bfd_boolean
2030elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2031{
2032 struct elf_metag_link_hash_table *htab;
2033 struct elf_link_hash_entry *eh;
2034 struct bfd_link_hash_entry *bh;
2035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2036
2037 /* Don't try to create the .plt and .got twice. */
2038 htab = metag_link_hash_table (info);
2039 if (htab->splt != NULL)
2040 return TRUE;
2041
2042 /* Call the generic code to do most of the work. */
2043 if (! _bfd_elf_create_dynamic_sections (abfd, info))
2044 return FALSE;
2045
ad1fc92a 2046 htab->sgot = bfd_get_linker_section (abfd, ".got");
a3c62988
NC
2047 if (! htab->sgot)
2048 return FALSE;
2049
2050 htab->sgotplt = bfd_make_section_with_flags (abfd, ".got.plt",
2051 (SEC_ALLOC | SEC_LOAD |
2052 SEC_HAS_CONTENTS |
2053 SEC_IN_MEMORY |
2054 SEC_LINKER_CREATED));
2055 if (htab->sgotplt == NULL
2056 || !bfd_set_section_alignment (abfd, htab->sgotplt, 2))
2057 return FALSE;
2058
2059 /* Define the symbol __GLOBAL_OFFSET_TABLE__ at the start of the .got
2060 section. We don't do this in the linker script because we don't want
2061 to define the symbol if we are not creating a global offset table. */
2062 bh = NULL;
2063 if (!(_bfd_generic_link_add_one_symbol
2064 (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->sgot,
2065 (bfd_vma) 0, NULL, FALSE, bed->collect, &bh)))
2066 return FALSE;
2067 eh = (struct elf_link_hash_entry *) bh;
2068 eh->def_regular = 1;
2069 eh->type = STT_OBJECT;
2070 eh->other = STV_HIDDEN;
2071
0e1862bb 2072 if (! bfd_link_executable (info)
a3c62988
NC
2073 && ! bfd_elf_link_record_dynamic_symbol (info, eh))
2074 return FALSE;
2075
2076 elf_hash_table (info)->hgot = eh;
2077
ad1fc92a
NC
2078 htab->splt = bfd_get_linker_section (abfd, ".plt");
2079 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
a3c62988 2080
ad1fc92a 2081 htab->srelgot = bfd_get_linker_section (abfd, ".rela.got");
a3c62988 2082
ad1fc92a
NC
2083 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
2084 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
a3c62988
NC
2085
2086 return TRUE;
2087}
2088
2089/* Look through the relocs for a section during the first phase, and
2090 calculate needed space in the global offset table, procedure linkage
2091 table, and dynamic reloc sections. At this point we haven't
2092 necessarily read all the input files. */
2093
2094static bfd_boolean
2095elf_metag_check_relocs (bfd *abfd,
2096 struct bfd_link_info *info,
2097 asection *sec,
2098 const Elf_Internal_Rela *relocs)
2099{
2100 Elf_Internal_Shdr *symtab_hdr;
2101 struct elf_link_hash_entry **eh_syms;
2102 const Elf_Internal_Rela *rel;
2103 const Elf_Internal_Rela *rel_end;
2104 struct elf_metag_link_hash_table *htab;
2105 asection *sreloc;
2106 bfd *dynobj;
2107 int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
2108
0e1862bb 2109 if (bfd_link_relocatable (info))
a3c62988
NC
2110 return TRUE;
2111
2112 htab = metag_link_hash_table (info);
2113 dynobj = htab->etab.dynobj;
2114 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2115 eh_syms = elf_sym_hashes (abfd);
2116 sreloc = NULL;
2117
2118 if (htab == NULL)
2119 return FALSE;
2120
2121 rel_end = relocs + sec->reloc_count;
2122 for (rel = relocs; rel < rel_end; rel++)
2123 {
2124 int r_type;
2125 struct elf_metag_link_hash_entry *hh;
1a18f0ae 2126 Elf_Internal_Sym *isym;
a3c62988
NC
2127 unsigned long r_symndx;
2128
2129 r_symndx = ELF32_R_SYM (rel->r_info);
2130 r_type = ELF32_R_TYPE (rel->r_info);
2131 if (r_symndx < symtab_hdr->sh_info)
1a18f0ae
NC
2132 {
2133 /* A local symbol. */
2134 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2135 abfd, r_symndx);
2136 if (isym == NULL)
2137 return FALSE;
2138
2139 hh = NULL;
2140 }
a3c62988
NC
2141 else
2142 {
1a18f0ae
NC
2143 isym = NULL;
2144
a3c62988
NC
2145 hh = (struct elf_metag_link_hash_entry *)
2146 eh_syms[r_symndx - symtab_hdr->sh_info];
2147 while (hh->eh.root.type == bfd_link_hash_indirect
2148 || hh->eh.root.type == bfd_link_hash_warning)
2149 hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link;
81fbe831
AM
2150
2151 /* PR15323, ref flags aren't set for references in the same
2152 object. */
2153 hh->eh.root.non_ir_ref = 1;
a3c62988
NC
2154 }
2155
2156 /* Some relocs require a global offset table. */
2157 if (htab->sgot == NULL)
2158 {
2159 switch (r_type)
2160 {
2161 case R_METAG_TLS_GD:
2162 case R_METAG_TLS_LDM:
2163 case R_METAG_TLS_IE:
0e1862bb 2164 if (bfd_link_pic (info))
a3c62988
NC
2165 info->flags |= DF_STATIC_TLS;
2166 /* Fall through. */
2167
2168 case R_METAG_HI16_GOTOFF:
2169 case R_METAG_LO16_GOTOFF:
2170 case R_METAG_GETSET_GOTOFF:
2171 case R_METAG_GETSET_GOT:
2172 case R_METAG_HI16_GOTPC:
2173 case R_METAG_LO16_GOTPC:
2174 if (dynobj == NULL)
2175 htab->etab.dynobj = dynobj = abfd;
2176 if (!elf_metag_create_dynamic_sections (dynobj, info))
2177 return FALSE;
2178 break;
2179
2180 default:
2181 break;
2182 }
2183 }
2184
2185 switch (r_type)
2186 {
2187 case R_METAG_TLS_IE:
2188 case R_METAG_TLS_GD:
2189 case R_METAG_GETSET_GOT:
2190 switch (r_type)
2191 {
2192 default:
2193 tls_type = GOT_NORMAL;
2194 break;
2195 case R_METAG_TLS_IE:
2196 tls_type = GOT_TLS_IE;
2197 break;
2198 case R_METAG_TLS_GD:
2199 tls_type = GOT_TLS_GD;
2200 break;
2201 }
2202
2203 if (hh != NULL)
2204 {
2205 hh->eh.got.refcount += 1;
2206 old_tls_type = hh->tls_type;
2207 }
2208 else
2209 {
2210 bfd_signed_vma *local_got_refcounts;
2211
2212 /* This is a global offset table entry for a local
2213 symbol. */
2214 local_got_refcounts = elf_local_got_refcounts (abfd);
2215 if (local_got_refcounts == NULL)
2216 {
2217 bfd_size_type size;
2218
2219 size = symtab_hdr->sh_info;
2220 size *= sizeof (bfd_signed_vma);
2221 /* Add in space to store the local GOT TLS types. */
2222 size += symtab_hdr->sh_info;
2223 local_got_refcounts = ((bfd_signed_vma *)
2224 bfd_zalloc (abfd, size));
2225 if (local_got_refcounts == NULL)
2226 return FALSE;
2227 elf_local_got_refcounts (abfd) = local_got_refcounts;
2228 memset (metag_elf_local_got_tls_type (abfd),
2229 GOT_UNKNOWN, symtab_hdr->sh_info);
2230 }
2231 local_got_refcounts[r_symndx] += 1;
2232 old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx];
2233 }
2234
2235 if (old_tls_type != tls_type)
2236 {
2237 if (hh != NULL)
2238 {
2239 hh->tls_type = tls_type;
2240 }
2241 else
2242 {
2243 metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
2244 }
2245 }
2246
2247 break;
2248
2249 case R_METAG_TLS_LDM:
2250 metag_link_hash_table (info)->tls_ldm_got.refcount += 1;
2251 break;
2252
2253 case R_METAG_RELBRANCH_PLT:
2254 /* This symbol requires a procedure linkage table entry. We
2255 actually build the entry in adjust_dynamic_symbol,
2256 because this might be a case of linking PIC code without
2257 linking in any dynamic objects, in which case we don't
2258 need to generate a procedure linkage table after all. */
2259
2260 /* If this is a local symbol, we resolve it directly without
2261 creating a procedure linkage table entry. */
2262 if (hh == NULL)
2263 continue;
2264
2265 if (hh->eh.forced_local)
2266 break;
2267
2268 hh->eh.needs_plt = 1;
2269 hh->eh.plt.refcount += 1;
2270 break;
2271
a3c62988
NC
2272 case R_METAG_HIADDR16:
2273 case R_METAG_LOADDR16:
1a18f0ae
NC
2274 /* Let's help debug shared library creation. These relocs
2275 cannot be used in shared libs. Don't error out for
2276 sections we don't care about, such as debug sections or
2277 non-constant sections. */
0e1862bb 2278 if (bfd_link_pic (info)
1a18f0ae
NC
2279 && (sec->flags & SEC_ALLOC) != 0
2280 && (sec->flags & SEC_READONLY) != 0)
2281 {
2282 const char *name;
2283
2284 if (hh)
2285 name = hh->eh.root.root.string;
2286 else
2287 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
4eca0228 2288 _bfd_error_handler
1a18f0ae
NC
2289 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2290 abfd, elf_metag_howto_table[r_type].name, name);
2291 bfd_set_error (bfd_error_bad_value);
2292 return FALSE;
2293 }
2294
2295 /* Fall through. */
2296 case R_METAG_ADDR32:
a3c62988
NC
2297 case R_METAG_RELBRANCH:
2298 case R_METAG_GETSETOFF:
0e1862bb 2299 if (hh != NULL && !bfd_link_pic (info))
a3c62988
NC
2300 {
2301 hh->eh.non_got_ref = 1;
2302 hh->eh.plt.refcount += 1;
2303 }
2304
2305 /* If we are creating a shared library, and this is a reloc
2306 against a global symbol, or a non PC relative reloc
2307 against a local symbol, then we need to copy the reloc
2308 into the shared library. However, if we are linking with
2309 -Bsymbolic, we do not need to copy a reloc against a
2310 global symbol which is defined in an object we are
2311 including in the link (i.e., DEF_REGULAR is set). At
2312 this point we have not seen all the input files, so it is
2313 possible that DEF_REGULAR is not set now but will be set
2314 later (it is never cleared). We account for that
2315 possibility below by storing information in the
2316 dyn_relocs field of the hash table entry. A similar
2317 situation occurs when creating shared libraries and symbol
2318 visibility changes render the symbol local.
2319
2320 If on the other hand, we are creating an executable, we
2321 may need to keep relocations for symbols satisfied by a
2322 dynamic library if we manage to avoid copy relocs for the
2323 symbol. */
0e1862bb 2324 if ((bfd_link_pic (info)
a3c62988
NC
2325 && (sec->flags & SEC_ALLOC) != 0
2326 && (r_type != R_METAG_RELBRANCH
2327 || (hh != NULL
2328 && (! info->symbolic
2329 || hh->eh.root.type == bfd_link_hash_defweak
2330 || !hh->eh.def_regular))))
0e1862bb 2331 || (!bfd_link_pic (info)
a3c62988
NC
2332 && (sec->flags & SEC_ALLOC) != 0
2333 && hh != NULL
2334 && (hh->eh.root.type == bfd_link_hash_defweak
2335 || !hh->eh.def_regular)))
2336 {
2337 struct elf_metag_dyn_reloc_entry *hdh_p;
2338 struct elf_metag_dyn_reloc_entry **hdh_head;
2339
2340 if (dynobj == NULL)
2341 htab->etab.dynobj = dynobj = abfd;
2342
2343 /* When creating a shared object, we must copy these
2344 relocs into the output file. We create a reloc
2345 section in dynobj and make room for the reloc. */
2346 if (sreloc == NULL)
2347 {
2348 sreloc = _bfd_elf_make_dynamic_reloc_section
2349 (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE);
2350
2351 if (sreloc == NULL)
2352 {
2353 bfd_set_error (bfd_error_bad_value);
2354 return FALSE;
2355 }
2356
2357 elf_section_data (sec)->sreloc = sreloc;
2358 }
2359
2360 /* If this is a global symbol, we count the number of
2361 relocations we need for this symbol. */
2362 if (hh != NULL)
2363 hdh_head = &((struct elf_metag_link_hash_entry *) hh)->dyn_relocs;
2364 else
2365 {
2366 /* Track dynamic relocs needed for local syms too. */
2367 asection *sr;
2368 void *vpp;
a3c62988
NC
2369
2370 sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
2371 if (sr == NULL)
2372 sr = sec;
2373
2374 vpp = &elf_section_data (sr)->local_dynrel;
2375 hdh_head = (struct elf_metag_dyn_reloc_entry **) vpp;
2376 }
2377
2378 hdh_p = *hdh_head;
2379 if (hdh_p == NULL || hdh_p->sec != sec)
2380 {
2381 hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2382 bfd_alloc (dynobj, sizeof *hdh_p));
2383 if (hdh_p == NULL)
2384 return FALSE;
2385 hdh_p->hdh_next = *hdh_head;
2386 *hdh_head = hdh_p;
2387 hdh_p->sec = sec;
2388 hdh_p->count = 0;
2389 hdh_p->relative_count = 0;
2390 }
2391
2392 hdh_p->count += 1;
2393 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
2394 hdh_p->relative_count += 1;
2395 }
2396 break;
2397
2398 /* This relocation describes the C++ object vtable hierarchy.
2399 Reconstruct it for later use during GC. */
2400 case R_METAG_GNU_VTINHERIT:
2401 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh,
2402 rel->r_offset))
2403 return FALSE;
2404 break;
2405
2406 /* This relocation describes which C++ vtable entries are actually
2407 used. Record for later use during GC. */
2408 case R_METAG_GNU_VTENTRY:
2409 BFD_ASSERT (hh != NULL);
2410 if (hh != NULL
2411 && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend))
2412 return FALSE;
2413 break;
2414 }
2415 }
2416
2417 return TRUE;
2418}
2419
2420/* Copy the extra info we tack onto an elf_link_hash_entry. */
2421
2422static void
2423elf_metag_copy_indirect_symbol (struct bfd_link_info *info,
2424 struct elf_link_hash_entry *eh_dir,
2425 struct elf_link_hash_entry *eh_ind)
2426{
2427 struct elf_metag_link_hash_entry *hh_dir, *hh_ind;
2428
2429 hh_dir = metag_elf_hash_entry (eh_dir);
2430 hh_ind = metag_elf_hash_entry (eh_ind);
2431
2432 if (hh_ind->dyn_relocs != NULL)
2433 {
2434 if (hh_dir->dyn_relocs != NULL)
2435 {
2436 struct elf_metag_dyn_reloc_entry **hdh_pp;
2437 struct elf_metag_dyn_reloc_entry *hdh_p;
2438
2439 if (eh_ind->root.type == bfd_link_hash_indirect)
2440 abort ();
2441
2442 /* Add reloc counts against the weak sym to the strong sym
2443 list. Merge any entries against the same section. */
2444 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2445 {
2446 struct elf_metag_dyn_reloc_entry *hdh_q;
2447
2448 for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL;
2449 hdh_q = hdh_q->hdh_next)
2450 if (hdh_q->sec == hdh_p->sec)
2451 {
2452 hdh_q->relative_count += hdh_p->relative_count;
2453 hdh_q->count += hdh_p->count;
2454 *hdh_pp = hdh_p->hdh_next;
2455 break;
2456 }
2457 if (hdh_q == NULL)
2458 hdh_pp = &hdh_p->hdh_next;
2459 }
2460 *hdh_pp = hh_dir->dyn_relocs;
2461 }
2462
2463 hh_dir->dyn_relocs = hh_ind->dyn_relocs;
2464 hh_ind->dyn_relocs = NULL;
2465 }
2466
2467 if (eh_ind->root.type == bfd_link_hash_indirect
2468 && eh_dir->got.refcount <= 0)
2469 {
2470 hh_dir->tls_type = hh_ind->tls_type;
2471 hh_ind->tls_type = GOT_UNKNOWN;
2472 }
2473
2474 _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
2475}
2476
2477/* Adjust a symbol defined by a dynamic object and referenced by a
2478 regular object. The current definition is in some section of the
2479 dynamic object, but we're not including those sections. We have to
2480 change the definition to something the rest of the link can
2481 understand. */
2482
2483static bfd_boolean
2484elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info,
2485 struct elf_link_hash_entry *eh)
2486{
2487 struct elf_metag_link_hash_table *htab;
2488 struct elf_metag_link_hash_entry *hh;
2489 struct elf_metag_dyn_reloc_entry *hdh_p;
2490 asection *s;
2491
2492 /* If this is a function, put it in the procedure linkage table. We
2493 will fill in the contents of the procedure linkage table later,
2494 when we know the address of the .got section. */
2495 if (eh->type == STT_FUNC
2496 || eh->needs_plt)
2497 {
2498 if (eh->plt.refcount <= 0
2499 || SYMBOL_CALLS_LOCAL (info, eh)
2500 || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
2501 && eh->root.type == bfd_link_hash_undefweak))
2502 {
2503 /* This case can occur if we saw a PLT reloc in an input
2504 file, but the symbol was never referred to by a dynamic
2505 object. In such a case, we don't actually need to build
2506 a procedure linkage table, and we can just do a PCREL
2507 reloc instead. */
2508 eh->plt.offset = (bfd_vma) -1;
2509 eh->needs_plt = 0;
2510 }
2511
2512 return TRUE;
2513 }
2514 else
2515 eh->plt.offset = (bfd_vma) -1;
2516
2517 /* If this is a weak symbol, and there is a real definition, the
2518 processor independent code will have arranged for us to see the
2519 real definition first, and we can just use the same value. */
2520 if (eh->u.weakdef != NULL)
2521 {
2522 if (eh->u.weakdef->root.type != bfd_link_hash_defined
2523 && eh->u.weakdef->root.type != bfd_link_hash_defweak)
2524 abort ();
2525 eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
2526 eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
2527 eh->non_got_ref = eh->u.weakdef->non_got_ref;
2528 return TRUE;
2529 }
2530
2531 /* This is a reference to a symbol defined by a dynamic object which
2532 is not a function. */
2533
2534 /* If we are creating a shared library, we must presume that the
2535 only references to the symbol are via the global offset table.
2536 For such cases we need not do anything here; the relocations will
2537 be handled correctly by relocate_section. */
0e1862bb 2538 if (bfd_link_pic (info))
a3c62988
NC
2539 return TRUE;
2540
2541 /* If there are no references to this symbol that do not use the
2542 GOT, we don't need to generate a copy reloc. */
2543 if (!eh->non_got_ref)
2544 return TRUE;
2545
2546 /* If -z nocopyreloc was given, we won't generate them either. */
2547 if (info->nocopyreloc)
2548 {
2549 eh->non_got_ref = 0;
2550 return TRUE;
2551 }
2552
2553 hh = (struct elf_metag_link_hash_entry *) eh;
2554 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2555 {
2556 s = hdh_p->sec->output_section;
2557 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2558 break;
2559 }
2560
2561 /* If we didn't find any dynamic relocs in read-only sections, then
2562 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2563 if (hdh_p == NULL)
2564 {
2565 eh->non_got_ref = 0;
2566 return TRUE;
2567 }
2568
a3c62988
NC
2569 /* We must allocate the symbol in our .dynbss section, which will
2570 become part of the .bss section of the executable. There will be
2571 an entry for this symbol in the .dynsym section. The dynamic
2572 object will contain position independent code, so all references
2573 from the dynamic object to this symbol will go through the global
2574 offset table. The dynamic linker will use the .dynsym entry to
2575 determine the address it must put in the global offset table, so
2576 both the dynamic object and the regular object will refer to the
2577 same memory location for the variable. */
2578
2579 htab = metag_link_hash_table (info);
2580
2581 /* We must generate a COPY reloc to tell the dynamic linker to
2582 copy the initial value out of the dynamic object and into the
2583 runtime process image. */
34c67f14 2584 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
a3c62988
NC
2585 {
2586 htab->srelbss->size += sizeof (Elf32_External_Rela);
2587 eh->needs_copy = 1;
2588 }
2589
2590 s = htab->sdynbss;
2591
6cabe1ea 2592 return _bfd_elf_adjust_dynamic_copy (info, eh, s);
a3c62988
NC
2593}
2594
2595/* Allocate space in .plt, .got and associated reloc sections for
2596 global syms. */
2597
2598static bfd_boolean
2599allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2600{
2601 struct bfd_link_info *info;
2602 struct elf_metag_link_hash_table *htab;
2603 struct elf_metag_link_hash_entry *hh;
2604 struct elf_metag_dyn_reloc_entry *hdh_p;
2605
2606 if (eh->root.type == bfd_link_hash_indirect)
2607 return TRUE;
2608
2609 if (eh->root.type == bfd_link_hash_warning)
2610 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2611
2612 info = inf;
2613 htab = metag_link_hash_table (info);
2614
2615 if (htab->etab.dynamic_sections_created
2616 && eh->plt.refcount > 0)
2617 {
2618 /* Make sure this symbol is output as a dynamic symbol.
2619 Undefined weak syms won't yet be marked as dynamic. */
2620 if (eh->dynindx == -1
2621 && !eh->forced_local)
2622 {
2623 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2624 return FALSE;
2625 }
2626
0e1862bb 2627 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
a3c62988
NC
2628 {
2629 asection *s = htab->splt;
2630
2631 /* If this is the first .plt entry, make room for the special
2632 first entry. */
2633 if (s->size == 0)
2634 s->size += PLT_ENTRY_SIZE;
2635
2636 eh->plt.offset = s->size;
2637
2638 /* If this symbol is not defined in a regular file, and we are
2639 not generating a shared library, then set the symbol to this
2640 location in the .plt. This is required to make function
2641 pointers compare as equal between the normal executable and
2642 the shared library. */
0e1862bb 2643 if (! bfd_link_pic (info)
a3c62988
NC
2644 && !eh->def_regular)
2645 {
2646 eh->root.u.def.section = s;
2647 eh->root.u.def.value = eh->plt.offset;
2648 }
2649
2650 /* Make room for this entry. */
2651 s->size += PLT_ENTRY_SIZE;
2652
2653 /* We also need to make an entry in the .got.plt section, which
2654 will be placed in the .got section by the linker script. */
2655 htab->sgotplt->size += 4;
2656
2657 /* We also need to make an entry in the .rel.plt section. */
2658 htab->srelplt->size += sizeof (Elf32_External_Rela);
2659 }
2660 else
2661 {
2662 eh->plt.offset = (bfd_vma) -1;
2663 eh->needs_plt = 0;
2664 }
2665 }
2666 else
2667 {
2668 eh->plt.offset = (bfd_vma) -1;
2669 eh->needs_plt = 0;
2670 }
2671
2672 if (eh->got.refcount > 0)
2673 {
2674 asection *s;
2675 bfd_boolean dyn;
2676 int tls_type = metag_elf_hash_entry (eh)->tls_type;
2677
2678 /* Make sure this symbol is output as a dynamic symbol.
2679 Undefined weak syms won't yet be marked as dynamic. */
2680 if (eh->dynindx == -1
2681 && !eh->forced_local)
2682 {
2683 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2684 return FALSE;
2685 }
2686
2687 s = htab->sgot;
2688
2689 eh->got.offset = s->size;
2690 s->size += 4;
2691 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
2692 if (tls_type == GOT_TLS_GD)
2693 s->size += 4;
2694 dyn = htab->etab.dynamic_sections_created;
2695 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2696 R_METAG_TLS_GD needs one if local symbol and two if global. */
2697 if ((tls_type == GOT_TLS_GD && eh->dynindx == -1)
2698 || (tls_type == GOT_TLS_IE && dyn))
2699 htab->srelgot->size += sizeof (Elf32_External_Rela);
2700 else if (tls_type == GOT_TLS_GD)
2701 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
0e1862bb
L
2702 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2703 bfd_link_pic (info),
2704 eh))
a3c62988
NC
2705 htab->srelgot->size += sizeof (Elf32_External_Rela);
2706 }
2707 else
2708 eh->got.offset = (bfd_vma) -1;
2709
2710 hh = (struct elf_metag_link_hash_entry *) eh;
2711 if (hh->dyn_relocs == NULL)
2712 return TRUE;
2713
2714 /* If this is a -Bsymbolic shared link, then we need to discard all
2715 space allocated for dynamic pc-relative relocs against symbols
2716 defined in a regular object. For the normal shared case, discard
2717 space for relocs that have become local due to symbol visibility
2718 changes. */
0e1862bb 2719 if (bfd_link_pic (info))
a3c62988
NC
2720 {
2721 if (SYMBOL_CALLS_LOCAL (info, eh))
2722 {
2723 struct elf_metag_dyn_reloc_entry **hdh_pp;
2724
2725 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2726 {
2727 hdh_p->count -= hdh_p->relative_count;
2728 hdh_p->relative_count = 0;
2729 if (hdh_p->count == 0)
2730 *hdh_pp = hdh_p->hdh_next;
2731 else
2732 hdh_pp = &hdh_p->hdh_next;
2733 }
2734 }
2735
2736 /* Also discard relocs on undefined weak syms with non-default
2737 visibility. */
2738 if (hh->dyn_relocs != NULL
2739 && eh->root.type == bfd_link_hash_undefweak)
2740 {
2741 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2742 hh->dyn_relocs = NULL;
2743
2744 /* Make sure undefined weak symbols are output as a dynamic
2745 symbol in PIEs. */
2746 else if (eh->dynindx == -1
2747 && !eh->forced_local)
2748 {
2749 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2750 return FALSE;
2751 }
2752 }
2753 }
2754 else
2755 {
2756 /* For the non-shared case, discard space for relocs against
2757 symbols which turn out to need copy relocs or are not
2758 dynamic. */
2759 if (!eh->non_got_ref
2760 && ((eh->def_dynamic
2761 && !eh->def_regular)
2762 || (htab->etab.dynamic_sections_created
2763 && (eh->root.type == bfd_link_hash_undefweak
2764 || eh->root.type == bfd_link_hash_undefined))))
2765 {
2766 /* Make sure this symbol is output as a dynamic symbol.
2767 Undefined weak syms won't yet be marked as dynamic. */
2768 if (eh->dynindx == -1
2769 && !eh->forced_local)
2770 {
2771 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2772 return FALSE;
2773 }
2774
2775 /* If that succeeded, we know we'll be keeping all the
2776 relocs. */
2777 if (eh->dynindx != -1)
2778 goto keep;
2779 }
2780
2781 hh->dyn_relocs = NULL;
2782 return TRUE;
2783
2784 keep: ;
2785 }
2786
2787 /* Finally, allocate space. */
2788 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2789 {
2790 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2791 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2792 }
2793
2794 return TRUE;
2795}
2796
2797/* Find any dynamic relocs that apply to read-only sections. */
2798
2799static bfd_boolean
2800readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2801{
2802 struct elf_metag_link_hash_entry *hh;
2803 struct elf_metag_dyn_reloc_entry *hdh_p;
2804
2805 if (eh->root.type == bfd_link_hash_warning)
2806 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2807
2808 hh = (struct elf_metag_link_hash_entry *) eh;
2809 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2810 {
2811 asection *s = hdh_p->sec->output_section;
2812
2813 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2814 {
2815 struct bfd_link_info *info = inf;
2816
2817 info->flags |= DF_TEXTREL;
2818
2819 /* Not an error, just cut short the traversal. */
2820 return FALSE;
2821 }
2822 }
2823 return TRUE;
2824}
2825
2826/* Set the sizes of the dynamic sections. */
2827
2828static bfd_boolean
2829elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2830 struct bfd_link_info *info)
2831{
2832 struct elf_metag_link_hash_table *htab;
2833 bfd *dynobj;
2834 bfd *ibfd;
2835 asection *s;
2836 bfd_boolean relocs;
2837
2838 htab = metag_link_hash_table (info);
2839 dynobj = htab->etab.dynobj;
2840 if (dynobj == NULL)
2841 abort ();
2842
2843 if (htab->etab.dynamic_sections_created)
2844 {
2845 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 2846 if (bfd_link_executable (info) && !info->nointerp)
a3c62988 2847 {
ad1fc92a 2848 s = bfd_get_linker_section (dynobj, ".interp");
a3c62988
NC
2849 if (s == NULL)
2850 abort ();
2851 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2852 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2853 }
2854 }
2855
2856 /* Set up .got offsets for local syms, and space for local dynamic
2857 relocs. */
c72f2fb2 2858 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
a3c62988
NC
2859 {
2860 bfd_signed_vma *local_got;
2861 bfd_signed_vma *end_local_got;
2862 bfd_size_type locsymcount;
2863 Elf_Internal_Shdr *symtab_hdr;
2864 asection *srel;
2865 char *local_tls_type;
2866
2867 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2868 continue;
2869
2870 for (s = ibfd->sections; s != NULL; s = s->next)
2871 {
2872 struct elf_metag_dyn_reloc_entry *hdh_p;
2873
2874 for (hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2875 elf_section_data (s)->local_dynrel);
2876 hdh_p != NULL;
2877 hdh_p = hdh_p->hdh_next)
2878 {
2879 if (!bfd_is_abs_section (hdh_p->sec)
2880 && bfd_is_abs_section (hdh_p->sec->output_section))
2881 {
2882 /* Input section has been discarded, either because
2883 it is a copy of a linkonce section or due to
2884 linker script /DISCARD/, so we'll be discarding
2885 the relocs too. */
2886 }
2887 else if (hdh_p->count != 0)
2888 {
2889 srel = elf_section_data (hdh_p->sec)->sreloc;
2890 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2891 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2892 info->flags |= DF_TEXTREL;
2893 }
2894 }
2895 }
2896
2897 local_got = elf_local_got_refcounts (ibfd);
2898 if (!local_got)
2899 continue;
2900
2901 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2902 locsymcount = symtab_hdr->sh_info;
2903 end_local_got = local_got + locsymcount;
2904 local_tls_type = metag_elf_local_got_tls_type (ibfd);
2905 s = htab->sgot;
2906 srel = htab->srelgot;
2907 for (; local_got < end_local_got; ++local_got)
2908 {
2909 if (*local_got > 0)
2910 {
2911 *local_got = s->size;
2912 s->size += GOT_ENTRY_SIZE;
2913 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
2914 if (*local_tls_type == GOT_TLS_GD)
2915 s->size += 4;
0e1862bb 2916 if (bfd_link_pic (info))
a3c62988
NC
2917 srel->size += sizeof (Elf32_External_Rela);
2918 }
2919 else
2920 *local_got = (bfd_vma) -1;
2921 ++local_tls_type;
2922 }
2923 }
2924
2925 if (htab->tls_ldm_got.refcount > 0)
2926 {
2927 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2928 reloc. */
2929 htab->tls_ldm_got.offset = htab->sgot->size;
2930 htab->sgot->size += 8;
2931 htab->srelgot->size += sizeof (Elf32_External_Rela);
2932 }
2933 else
2934 htab->tls_ldm_got.offset = -1;
2935
2936 /* Allocate global sym .plt and .got entries, and space for global
2937 sym dynamic relocs. */
2938 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2939
2940 /* We now have determined the sizes of the various dynamic sections.
2941 Allocate memory for them. */
2942 relocs = FALSE;
2943 for (s = dynobj->sections; s != NULL; s = s->next)
2944 {
2945 bfd_boolean reloc_section = FALSE;
2946
2947 if ((s->flags & SEC_LINKER_CREATED) == 0)
2948 continue;
2949
2950 if (s == htab->splt
2951 || s == htab->sgot
2952 || s == htab->sgotplt
2953 || s == htab->sdynbss)
2954 {
2955 /* Strip this section if we don't need it; see the
2956 comment below. */
2957 }
2958 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2959 {
2960 if (s->size != 0 && s != htab->srelplt)
2961 relocs = TRUE;
2962
2963 /* We use the reloc_count field as a counter if we need
2964 to copy relocs into the output file. */
2965 s->reloc_count = 0;
2966 reloc_section = TRUE;
2967 }
2968 else
2969 {
2970 /* It's not one of our sections, so don't allocate space. */
2971 continue;
2972 }
2973
2974 if (s->size == 0)
2975 {
2976 /* If we don't need this section, strip it from the
2977 output file. This is mostly to handle .rela.bss and
2978 .rela.plt. We must create both sections in
2979 create_dynamic_sections, because they must be created
2980 before the linker maps input sections to output
2981 sections. The linker does that before
2982 adjust_dynamic_symbol is called, and it is that
2983 function which decides whether anything needs to go
2984 into these sections. */
2985 s->flags |= SEC_EXCLUDE;
2986 continue;
2987 }
2988
2989 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2990 continue;
2991
2992 /* Allocate memory for the section contents. */
2993 s->contents = bfd_zalloc (dynobj, s->size);
2994 if (s->contents == NULL)
2995 return FALSE;
2996 else if (reloc_section)
2997 {
2998 unsigned char *contents = s->contents;
2999 Elf32_External_Rela reloc;
3000
3001 /* Fill the reloc section with a R_METAG_NONE type reloc. */
3002 memset(&reloc, 0, sizeof(Elf32_External_Rela));
3003 reloc.r_info[0] = R_METAG_NONE;
3004 for (; contents < (s->contents + s->size);
3005 contents += sizeof(Elf32_External_Rela))
3006 {
3007 memcpy(contents, &reloc, sizeof(Elf32_External_Rela));
3008 }
3009 }
3010 }
3011
3012 if (htab->etab.dynamic_sections_created)
3013 {
3014 /* Add some entries to the .dynamic section. We fill in the
3015 values later, in elf_metag_finish_dynamic_sections, but we
3016 must add the entries now so that we get the correct size for
3017 the .dynamic section. The DT_DEBUG entry is filled in by the
3018 dynamic linker and used by the debugger. */
3019#define add_dynamic_entry(TAG, VAL) \
3020 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3021
3022 if (!add_dynamic_entry (DT_PLTGOT, 0))
3023 return FALSE;
3024
0e1862bb 3025 if (bfd_link_executable (info))
a3c62988
NC
3026 {
3027 if (!add_dynamic_entry (DT_DEBUG, 0))
3028 return FALSE;
3029 }
3030
3031 if (htab->srelplt->size != 0)
3032 {
3033 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3034 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3035 || !add_dynamic_entry (DT_JMPREL, 0))
3036 return FALSE;
3037 }
3038
3039 if (relocs)
3040 {
3041 if (!add_dynamic_entry (DT_RELA, 0)
3042 || !add_dynamic_entry (DT_RELASZ, 0)
3043 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3044 return FALSE;
3045
3046 /* If any dynamic relocs apply to a read-only section,
3047 then we need a DT_TEXTREL entry. */
3048 if ((info->flags & DF_TEXTREL) == 0)
3049 elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
3050
3051 if ((info->flags & DF_TEXTREL) != 0)
3052 {
3053 if (!add_dynamic_entry (DT_TEXTREL, 0))
3054 return FALSE;
3055 }
3056 }
3057 }
3058#undef add_dynamic_entry
3059
3060 return TRUE;
3061}
3062
3063/* Finish up dynamic symbol handling. We set the contents of various
3064 dynamic sections here. */
3065
3066static bfd_boolean
3067elf_metag_finish_dynamic_symbol (bfd *output_bfd,
3068 struct bfd_link_info *info,
3069 struct elf_link_hash_entry *eh,
3070 Elf_Internal_Sym *sym)
3071{
3072 struct elf_metag_link_hash_table *htab;
3073 Elf_Internal_Rela rel;
3074 bfd_byte *loc;
3075
3076 htab = metag_link_hash_table (info);
3077
3078 if (eh->plt.offset != (bfd_vma) -1)
3079 {
3080 asection *splt;
3081 asection *sgot;
3082 asection *srela;
3083
3084 bfd_vma plt_index;
3085 bfd_vma got_offset;
3086 bfd_vma got_entry;
3087
3088 if (eh->plt.offset & 1)
3089 abort ();
3090
3091 BFD_ASSERT (eh->dynindx != -1);
3092
3093 splt = htab->splt;
3094 sgot = htab->sgotplt;
3095 srela = htab->srelplt;
3096 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3097
3098 /* Get the index in the procedure linkage table which
3099 corresponds to this symbol. This is the index of this symbol
3100 in all the symbols for which we are making plt entries. The
3101 first entry in the procedure linkage table is reserved. */
3102 plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1;
3103
3104 /* Get the offset into the .got.plt table of the entry that
3105 corresponds to this function. */
3106 got_offset = plt_index * GOT_ENTRY_SIZE;
3107
3108 BFD_ASSERT (got_offset < (1 << 16));
3109
3110 got_entry = sgot->output_section->vma
3111 + sgot->output_offset
3112 + got_offset;
3113
3114 BFD_ASSERT (plt_index < (1 << 16));
3115
3116 /* Fill in the entry in the procedure linkage table. */
0e1862bb 3117 if (! bfd_link_pic (info))
a3c62988
NC
3118 {
3119 bfd_put_32 (output_bfd,
3120 (plt_entry[0]
3121 | (((got_entry >> 16) & 0xffff) << 3)),
3122 splt->contents + eh->plt.offset);
3123 bfd_put_32 (output_bfd,
3124 (plt_entry[1]
3125 | ((got_entry & 0xffff) << 3)),
3126 splt->contents + eh->plt.offset + 4);
3127 bfd_put_32 (output_bfd, plt_entry[2],
3128 splt->contents + eh->plt.offset + 8);
3129 bfd_put_32 (output_bfd,
3130 (plt_entry[3] | (plt_index << 3)),
3131 splt->contents + eh->plt.offset + 12);
3132 bfd_put_32 (output_bfd,
3133 (plt_entry[4]
3134 | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3135 splt->contents + eh->plt.offset + 16);
3136 }
3137 else
3138 {
3139 bfd_vma addr = got_entry - (splt->output_section->vma +
3140 splt->output_offset + eh->plt.offset);
3141
3142 bfd_put_32 (output_bfd,
3143 plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3144 splt->contents + eh->plt.offset);
3145 bfd_put_32 (output_bfd,
3146 plt_pic_entry[1] | ((addr & 0xffff) << 3),
3147 splt->contents + eh->plt.offset + 4);
3148 bfd_put_32 (output_bfd, plt_pic_entry[2],
3149 splt->contents + eh->plt.offset + 8);
3150 bfd_put_32 (output_bfd,
3151 (plt_pic_entry[3] | (plt_index << 3)),
3152 splt->contents + eh->plt.offset + 12);
3153 bfd_put_32 (output_bfd,
3154 (plt_pic_entry[4]
3155 + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3156 splt->contents + eh->plt.offset + 16);
3157 }
3158
3159 /* Fill in the entry in the global offset table. */
3160 bfd_put_32 (output_bfd,
3161 (splt->output_section->vma
3162 + splt->output_offset
3163 + eh->plt.offset
3164 + 12), /* offset within PLT entry */
3165 sgot->contents + got_offset);
3166
3167 /* Fill in the entry in the .rela.plt section. */
3168 rel.r_offset = (sgot->output_section->vma
3169 + sgot->output_offset
3170 + got_offset);
3171 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT);
3172 rel.r_addend = 0;
3173 loc = htab->srelplt->contents;
3174 loc += plt_index * sizeof(Elf32_External_Rela);
3175 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3176
3177 if (!eh->def_regular)
3178 {
3179 /* Mark the symbol as undefined, rather than as defined in
3180 the .plt section. Leave the value alone. */
3181 sym->st_shndx = SHN_UNDEF;
3182 }
3183 }
3184
3185 if (eh->got.offset != (bfd_vma) -1
3186 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
3187 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
3188 {
3189 /* This symbol has an entry in the global offset table. Set it
3190 up. */
3191
3192 rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
3193 + htab->sgot->output_offset
3194 + htab->sgot->output_section->vma);
3195
3196 /* If this is a -Bsymbolic link and the symbol is defined
3197 locally or was forced to be local because of a version file,
3198 we just want to emit a RELATIVE reloc. The entry in the
3199 global offset table will already have been initialized in the
3200 relocate_section function. */
0e1862bb 3201 if (bfd_link_pic (info)
a3c62988
NC
3202 && (info->symbolic || eh->dynindx == -1)
3203 && eh->def_regular)
3204 {
3205 rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
3206 rel.r_addend = (eh->root.u.def.value
3207 + eh->root.u.def.section->output_offset
3208 + eh->root.u.def.section->output_section->vma);
3209 }
3210 else
3211 {
3212 if ((eh->got.offset & 1) != 0)
3213 abort ();
3214 bfd_put_32 (output_bfd, 0, htab->sgot->contents + eh->got.offset);
3215 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT);
3216 rel.r_addend = 0;
3217 }
3218
3219 loc = htab->srelgot->contents;
3220 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3221 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3222 }
3223
3224 if (eh->needs_copy)
3225 {
3226 asection *s;
3227
3228 /* This symbol needs a copy reloc. Set it up. */
3229
3230 if (! (eh->dynindx != -1
3231 && (eh->root.type == bfd_link_hash_defined
3232 || eh->root.type == bfd_link_hash_defweak)))
3233 abort ();
3234
3235 s = htab->srelbss;
3236
3237 rel.r_offset = (eh->root.u.def.value
3238 + eh->root.u.def.section->output_offset
3239 + eh->root.u.def.section->output_section->vma);
3240 rel.r_addend = 0;
3241 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY);
3242 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3243 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3244 }
3245
3246 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3247 if (eh->root.root.string[0] == '_'
3248 && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
3249 || eh == htab->etab.hgot))
3250 {
3251 sym->st_shndx = SHN_ABS;
3252 }
3253
3254 return TRUE;
3255}
3256
3257/* Set the Meta ELF ABI version. */
3258
3259static void
df3ce959 3260elf_metag_post_process_headers (bfd * abfd, struct bfd_link_info * link_info)
a3c62988
NC
3261{
3262 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
3263
df3ce959 3264 _bfd_elf_post_process_headers (abfd, link_info);
a3c62988
NC
3265 i_ehdrp = elf_elfheader (abfd);
3266 i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION;
3267}
3268
3269/* Used to decide how to sort relocs in an optimal manner for the
3270 dynamic linker, before writing them out. */
3271
3272static enum elf_reloc_type_class
7e612e98
AM
3273elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3274 const asection *rel_sec ATTRIBUTE_UNUSED,
3275 const Elf_Internal_Rela *rela)
a3c62988
NC
3276{
3277 switch ((int) ELF32_R_TYPE (rela->r_info))
3278 {
3279 case R_METAG_RELATIVE:
3280 return reloc_class_relative;
3281 case R_METAG_JMP_SLOT:
3282 return reloc_class_plt;
3283 case R_METAG_COPY:
3284 return reloc_class_copy;
3285 default:
3286 return reloc_class_normal;
3287 }
3288}
3289
3290/* Finish up the dynamic sections. */
3291
3292static bfd_boolean
3293elf_metag_finish_dynamic_sections (bfd *output_bfd,
3294 struct bfd_link_info *info)
3295{
3296 bfd *dynobj;
3297 struct elf_metag_link_hash_table *htab;
3298 asection *sdyn;
3299
3300 htab = metag_link_hash_table (info);
3301 dynobj = htab->etab.dynobj;
3302
ad1fc92a 3303 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
a3c62988
NC
3304
3305 if (htab->etab.dynamic_sections_created)
3306 {
3307 asection *splt;
3308 Elf32_External_Dyn *dyncon, *dynconend;
3309
3310 if (sdyn == NULL)
3311 abort ();
3312
3313 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3314 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3315 for (; dyncon < dynconend; dyncon++)
3316 {
3317 Elf_Internal_Dyn dyn;
3318 asection *s;
3319
3320 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3321
3322 switch (dyn.d_tag)
3323 {
3324 default:
3325 continue;
3326
3327 case DT_PLTGOT:
4ade44b7
AM
3328 s = htab->sgot;
3329 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
a3c62988
NC
3330 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3331 break;
3332
3333 case DT_JMPREL:
4ade44b7
AM
3334 s = htab->srelplt;
3335 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
a3c62988
NC
3336 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3337 break;
3338
3339 case DT_PLTRELSZ:
3340 s = htab->srelplt;
3341 dyn.d_un.d_val = s->size;
3342 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3343 break;
3344
3345 case DT_RELASZ:
3346 /* Don't count procedure linkage table relocs in the
3347 overall reloc count. */
3348 if (htab->srelplt) {
3349 s = htab->srelplt;
3350 dyn.d_un.d_val -= s->size;
3351 }
3352 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3353 break;
3354
3355 case DT_RELA:
3356 /* We may not be using the standard ELF linker script.
3357 If .rela.plt is the first .rela section, we adjust
3358 DT_RELA to not include it. */
3359 if (htab->srelplt) {
3360 s = htab->srelplt;
3361 if (dyn.d_un.d_ptr == s->output_section->vma + s->output_offset)
3362 dyn.d_un.d_ptr += s->size;
3363 }
3364 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3365 break;
3366 }
3367
3368 }
3369
3370 /* Fill in the first entry in the procedure linkage table. */
3371 splt = htab->splt;
3372 if (splt && splt->size > 0)
3373 {
3374 unsigned long addr;
3375 /* addr = .got + 4 */
3376 addr = htab->sgot->output_section->vma +
3377 htab->sgot->output_offset + 4;
0e1862bb 3378 if (bfd_link_pic (info))
a3c62988
NC
3379 {
3380 addr -= splt->output_section->vma + splt->output_offset;
3381 bfd_put_32 (output_bfd,
3382 plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3383 splt->contents);
3384 bfd_put_32 (output_bfd,
3385 plt0_pic_entry[1] | ((addr & 0xffff) << 3),
3386 splt->contents + 4);
3387 bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8);
3388 bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12);
3389 bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16);
3390 }
3391 else
3392 {
3393 bfd_put_32 (output_bfd,
3394 plt0_entry[0] | (((addr >> 16) & 0xffff) << 3),
3395 splt->contents);
3396 bfd_put_32 (output_bfd,
3397 plt0_entry[1] | ((addr & 0xffff) << 3),
3398 splt->contents + 4);
3399 bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
3400 bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
3401 bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16);
3402 }
3403
3404 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3405 PLT_ENTRY_SIZE;
3406 }
3407 }
3408
3409 if (htab->sgot != NULL && htab->sgot->size != 0)
3410 {
3411 /* Fill in the first entry in the global offset table.
3412 We use it to point to our dynamic section, if we have one. */
3413 bfd_put_32 (output_bfd,
3414 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
3415 htab->sgot->contents);
3416
3417 /* The second entry is reserved for use by the dynamic linker. */
3418 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
3419
3420 /* Set .got entry size. */
3421 elf_section_data (htab->sgot->output_section)
3422 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3423 }
3424
3425 return TRUE;
3426}
3427
3428/* Return the section that should be marked against GC for a given
3429 relocation. */
3430
3431static asection *
3432elf_metag_gc_mark_hook (asection *sec,
3433 struct bfd_link_info *info,
3434 Elf_Internal_Rela *rela,
3435 struct elf_link_hash_entry *hh,
3436 Elf_Internal_Sym *sym)
3437{
3438 if (hh != NULL)
3439 switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
3440 {
3441 case R_METAG_GNU_VTINHERIT:
3442 case R_METAG_GNU_VTENTRY:
3443 return NULL;
3444 }
3445
3446 return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
3447}
3448
3449/* Update the got and plt entry reference counts for the section being
3450 removed. */
3451
3452static bfd_boolean
3453elf_metag_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
3454 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3455 asection *sec ATTRIBUTE_UNUSED,
3456 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
3457{
3458 Elf_Internal_Shdr *symtab_hdr;
3459 struct elf_link_hash_entry **eh_syms;
3460 bfd_signed_vma *local_got_refcounts;
3461 bfd_signed_vma *local_plt_refcounts;
3462 const Elf_Internal_Rela *rel, *relend;
3463
0e1862bb 3464 if (bfd_link_relocatable (info))
a3c62988
NC
3465 return TRUE;
3466
3467 elf_section_data (sec)->local_dynrel = NULL;
3468
3469 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3470 eh_syms = elf_sym_hashes (abfd);
3471 local_got_refcounts = elf_local_got_refcounts (abfd);
3472 local_plt_refcounts = local_got_refcounts;
3473 if (local_plt_refcounts != NULL)
3474 local_plt_refcounts += symtab_hdr->sh_info;
3475
3476 relend = relocs + sec->reloc_count;
3477 for (rel = relocs; rel < relend; rel++)
3478 {
3479 unsigned long r_symndx;
3480 unsigned int r_type;
3481 struct elf_link_hash_entry *eh = NULL;
3482
3483 r_symndx = ELF32_R_SYM (rel->r_info);
3484 if (r_symndx >= symtab_hdr->sh_info)
3485 {
3486 struct elf_metag_link_hash_entry *hh;
3487 struct elf_metag_dyn_reloc_entry **hdh_pp;
3488 struct elf_metag_dyn_reloc_entry *hdh_p;
3489
3490 eh = eh_syms[r_symndx - symtab_hdr->sh_info];
3491 while (eh->root.type == bfd_link_hash_indirect
3492 || eh->root.type == bfd_link_hash_warning)
3493 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3494 hh = (struct elf_metag_link_hash_entry *) eh;
3495
3496 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL;
3497 hdh_pp = &hdh_p->hdh_next)
3498 if (hdh_p->sec == sec)
3499 {
3500 /* Everything must go for SEC. */
3501 *hdh_pp = hdh_p->hdh_next;
3502 break;
3503 }
3504 }
3505
3506 r_type = ELF32_R_TYPE (rel->r_info);
3507 switch (r_type)
3508 {
3509 case R_METAG_TLS_LDM:
3510 if (metag_link_hash_table (info)->tls_ldm_got.refcount > 0)
3511 metag_link_hash_table (info)->tls_ldm_got.refcount -= 1;
3512 break;
3513 case R_METAG_TLS_IE:
3514 case R_METAG_TLS_GD:
3515 case R_METAG_GETSET_GOT:
3516 if (eh != NULL)
3517 {
3518 if (eh->got.refcount > 0)
3519 eh->got.refcount -= 1;
3520 }
3521 else if (local_got_refcounts != NULL)
3522 {
3523 if (local_got_refcounts[r_symndx] > 0)
3524 local_got_refcounts[r_symndx] -= 1;
3525 }
3526 break;
3527
3528 case R_METAG_RELBRANCH_PLT:
3529 if (eh != NULL)
3530 {
3531 if (eh->plt.refcount > 0)
3532 eh->plt.refcount -= 1;
3533 }
3534 break;
3535
3536 case R_METAG_ADDR32:
3537 case R_METAG_HIADDR16:
3538 case R_METAG_LOADDR16:
3539 case R_METAG_GETSETOFF:
3540 case R_METAG_RELBRANCH:
3541 if (eh != NULL)
3542 {
3543 struct elf_metag_link_hash_entry *hh;
3544 struct elf_metag_dyn_reloc_entry **hdh_pp;
3545 struct elf_metag_dyn_reloc_entry *hdh_p;
3546
0e1862bb 3547 if (!bfd_link_pic (info) && eh->plt.refcount > 0)
a3c62988
NC
3548 eh->plt.refcount -= 1;
3549
3550 hh = (struct elf_metag_link_hash_entry *) eh;
3551
3552 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL;
3553 hdh_pp = &hdh_p->hdh_next)
3554 if (hdh_p->sec == sec)
3555 {
3556 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
3557 hdh_p->relative_count -= 1;
3558 hdh_p->count -= 1;
3559 if (hdh_p->count == 0)
3560 *hdh_pp = hdh_p->hdh_next;
3561 break;
3562 }
3563 }
3564 break;
3565
3566 default:
3567 break;
3568 }
3569 }
3570
3571 return TRUE;
3572}
3573
3574/* Determine the type of stub needed, if any, for a call. */
3575
3576static enum elf_metag_stub_type
3577metag_type_of_stub (asection *input_sec,
3578 const Elf_Internal_Rela *rel,
3579 struct elf_metag_link_hash_entry *hh,
3580 bfd_vma destination,
3581 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3582{
3583 bfd_vma location;
3584 bfd_vma branch_offset;
3585 bfd_vma max_branch_offset;
3586
3587 if (hh != NULL &&
3588 !(hh->eh.root.type == bfd_link_hash_defined
3589 || hh->eh.root.type == bfd_link_hash_defweak))
3590 return metag_stub_none;
3591
3592 /* Determine where the call point is. */
3593 location = (input_sec->output_offset
3594 + input_sec->output_section->vma
3595 + rel->r_offset);
3596
3597 branch_offset = destination - location;
3598
3599 /* Determine if a long branch stub is needed. Meta branch offsets
3600 are signed 19 bits 4 byte aligned. */
3601 max_branch_offset = (1 << (BRANCH_BITS-1)) << 2;
3602
3603 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
3604 {
0e1862bb 3605 if (bfd_link_pic (info))
a3c62988
NC
3606 return metag_stub_long_branch_shared;
3607 else
3608 return metag_stub_long_branch;
3609 }
3610
3611 return metag_stub_none;
3612}
3613
3614#define MOVT_A0_3 0x82180005
3615#define JUMP_A0_3 0xac180003
3616
3617#define MOVT_A1LBP 0x83080005
3618#define ADD_A1LBP 0x83080000
3619
3620#define ADDT_A0_3_CPC 0x82980001
3621#define ADD_A0_3_A0_3 0x82180000
3622#define MOV_PC_A0_3 0xa3180ca0
3623
3624static bfd_boolean
3625metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3626{
3627 struct elf_metag_stub_hash_entry *hsh;
3628 asection *stub_sec;
3629 bfd *stub_bfd;
3630 bfd_byte *loc;
3631 bfd_vma sym_value;
3632 int size;
3633
3634 /* Massage our args to the form they really have. */
3635 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3636
3637 stub_sec = hsh->stub_sec;
3638
3639 /* Make a note of the offset within the stubs for this entry. */
3640 hsh->stub_offset = stub_sec->size;
3641 loc = stub_sec->contents + hsh->stub_offset;
3642
3643 stub_bfd = stub_sec->owner;
3644
3645 switch (hsh->stub_type)
3646 {
3647 case metag_stub_long_branch_shared:
3648 /* A PIC long branch stub is an ADDT and an ADD instruction used to
3649 calculate the jump target using A0.3 as a temporary. Then a MOV
3650 to PC carries out the jump. */
3651 sym_value = (hsh->target_value
3652 + hsh->target_section->output_offset
3653 + hsh->target_section->output_section->vma
3654 + hsh->addend);
3655
3656 sym_value -= (hsh->stub_offset
3657 + stub_sec->output_offset
3658 + stub_sec->output_section->vma);
3659
3660 bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3),
3661 loc);
3662
3663 bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3),
3664 loc + 4);
3665
3666 bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8);
3667
3668 size = 12;
3669 break;
3670 case metag_stub_long_branch:
3671 /* A standard long branch stub is a MOVT instruction followed by a
3672 JUMP instruction using the A0.3 register as a temporary. This is
3673 the same method used by the LDLK linker (patch.c). */
3674 sym_value = (hsh->target_value
3675 + hsh->target_section->output_offset
3676 + hsh->target_section->output_section->vma
3677 + hsh->addend);
3678
3679 bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3),
3680 loc);
3681
3682 bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4);
3683
3684 size = 8;
3685 break;
3686 default:
3687 BFD_FAIL ();
3688 return FALSE;
3689 }
3690
3691 stub_sec->size += size;
3692 return TRUE;
3693}
3694
3695/* As above, but don't actually build the stub. Just bump offset so
3696 we know stub section sizes. */
3697
3698static bfd_boolean
3699metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3700{
3701 struct elf_metag_stub_hash_entry *hsh;
3702 int size = 0;
3703
3704 /* Massage our args to the form they really have. */
3705 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3706
3707 if (hsh->stub_type == metag_stub_long_branch)
3708 size = 8;
3709 else if (hsh->stub_type == metag_stub_long_branch_shared)
3710 size = 12;
3711
3712 hsh->stub_sec->size += size;
3713 return TRUE;
3714}
3715
3716/* Set up various things so that we can make a list of input sections
3717 for each output section included in the link. Returns -1 on error,
3718 0 when no stubs will be needed, and 1 on success. */
3719
3720int
3721elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3722{
3723 bfd *input_bfd;
3724 unsigned int bfd_count;
7292b3ac 3725 unsigned int top_id, top_index;
a3c62988
NC
3726 asection *section;
3727 asection **input_list, **list;
3728 bfd_size_type amt;
3729 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3730
3731 /* Count the number of input BFDs and find the top input section id. */
3732 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3733 input_bfd != NULL;
c72f2fb2 3734 input_bfd = input_bfd->link.next)
a3c62988
NC
3735 {
3736 bfd_count += 1;
3737 for (section = input_bfd->sections;
3738 section != NULL;
3739 section = section->next)
3740 {
3741 if (top_id < section->id)
3742 top_id = section->id;
3743 }
3744 }
3745
3746 htab->bfd_count = bfd_count;
3747
3748 amt = sizeof (struct map_stub) * (top_id + 1);
3749 htab->stub_group = bfd_zmalloc (amt);
3750 if (htab->stub_group == NULL)
3751 return -1;
3752
3753 /* We can't use output_bfd->section_count here to find the top output
3754 section index as some sections may have been removed, and
3755 strip_excluded_output_sections doesn't renumber the indices. */
3756 for (section = output_bfd->sections, top_index = 0;
3757 section != NULL;
3758 section = section->next)
3759 {
3760 if (top_index < section->index)
3761 top_index = section->index;
3762 }
3763
3764 htab->top_index = top_index;
3765 amt = sizeof (asection *) * (top_index + 1);
3766 input_list = bfd_malloc (amt);
3767 htab->input_list = input_list;
3768 if (input_list == NULL)
3769 return -1;
3770
3771 /* For sections we aren't interested in, mark their entries with a
3772 value we can check later. */
3773 list = input_list + top_index;
3774 do
3775 *list = bfd_abs_section_ptr;
3776 while (list-- != input_list);
3777
3778 for (section = output_bfd->sections;
3779 section != NULL;
3780 section = section->next)
3781 {
3782 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3783 * have PC relative relocs in them but no code flag set. */
3784 if (((section->flags & SEC_CODE) != 0) ||
3785 strcmp(".ctors", section->name) ||
3786 strcmp(".dtors", section->name))
3787 input_list[section->index] = NULL;
3788 }
3789
3790 return 1;
3791}
3792
3793/* The linker repeatedly calls this function for each input section,
3794 in the order that input sections are linked into output sections.
3795 Build lists of input sections to determine groupings between which
3796 we may insert linker stubs. */
3797
3798void
3799elf_metag_next_input_section (struct bfd_link_info *info, asection *isec)
3800{
3801 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3802
3803 if (isec->output_section->index <= htab->top_index)
3804 {
3805 asection **list = htab->input_list + isec->output_section->index;
3806 if (*list != bfd_abs_section_ptr)
3807 {
3808 /* Steal the link_sec pointer for our list. */
3809#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3810 /* This happens to make the list in reverse order,
3811 which is what we want. */
3812 PREV_SEC (isec) = *list;
3813 *list = isec;
3814 }
3815 }
3816}
3817
3818/* See whether we can group stub sections together. Grouping stub
3819 sections may result in fewer stubs. More importantly, we need to
3820 put all .init* and .fini* stubs at the beginning of the .init or
3821 .fini output sections respectively, because glibc splits the
3822 _init and _fini functions into multiple parts. Putting a stub in
3823 the middle of a function is not a good idea. */
3824
3825static void
3826group_sections (struct elf_metag_link_hash_table *htab,
3827 bfd_size_type stub_group_size,
3828 bfd_boolean stubs_always_before_branch)
3829{
3830 asection **list = htab->input_list + htab->top_index;
3831 do
3832 {
3833 asection *tail = *list;
3834 if (tail == bfd_abs_section_ptr)
3835 continue;
3836 while (tail != NULL)
3837 {
3838 asection *curr;
3839 asection *prev;
3840 bfd_size_type total;
3841 bfd_boolean big_sec;
3842
3843 curr = tail;
3844 total = tail->size;
3845 big_sec = total >= stub_group_size;
3846
3847 while ((prev = PREV_SEC (curr)) != NULL
3848 && ((total += curr->output_offset - prev->output_offset)
3849 < stub_group_size))
3850 curr = prev;
3851
3852 /* OK, the size from the start of CURR to the end is less
3853 than stub_group_size bytes and thus can be handled by one stub
3854 section. (or the tail section is itself larger than
3855 stub_group_size bytes, in which case we may be toast.)
3856 We should really be keeping track of the total size of
3857 stubs added here, as stubs contribute to the final output
3858 section size. */
3859 do
3860 {
3861 prev = PREV_SEC (tail);
3862 /* Set up this stub group. */
3863 htab->stub_group[tail->id].link_sec = curr;
3864 }
3865 while (tail != curr && (tail = prev) != NULL);
3866
3867 /* But wait, there's more! Input sections up to stub_group_size
3868 bytes before the stub section can be handled by it too.
3869 Don't do this if we have a really large section after the
3870 stubs, as adding more stubs increases the chance that
3871 branches may not reach into the stub section. */
3872 if (!stubs_always_before_branch && !big_sec)
3873 {
3874 total = 0;
3875 while (prev != NULL
3876 && ((total += tail->output_offset - prev->output_offset)
3877 < stub_group_size))
3878 {
3879 tail = prev;
3880 prev = PREV_SEC (tail);
3881 htab->stub_group[tail->id].link_sec = curr;
3882 }
3883 }
3884 tail = prev;
3885 }
3886 }
3887 while (list-- != htab->input_list);
3888 free (htab->input_list);
3889#undef PREV_SEC
3890}
3891
3892/* Read in all local syms for all input bfds.
3893 Returns -1 on error, 0 otherwise. */
3894
3895static int
3896get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
3897 struct bfd_link_info *info)
3898{
3899 unsigned int bfd_indx;
3900 Elf_Internal_Sym *local_syms, **all_local_syms;
3901 int stub_changed = 0;
3902 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3903
3904 /* We want to read in symbol extension records only once. To do this
3905 we need to read in the local symbols in parallel and save them for
3906 later use; so hold pointers to the local symbols in an array. */
3907 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3908 all_local_syms = bfd_zmalloc (amt);
3909 htab->all_local_syms = all_local_syms;
3910 if (all_local_syms == NULL)
3911 return -1;
3912
3913 /* Walk over all the input BFDs, swapping in local symbols. */
3914 for (bfd_indx = 0;
3915 input_bfd != NULL;
c72f2fb2 3916 input_bfd = input_bfd->link.next, bfd_indx++)
a3c62988
NC
3917 {
3918 Elf_Internal_Shdr *symtab_hdr;
3919
3920 /* We'll need the symbol table in a second. */
3921 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3922 if (symtab_hdr->sh_info == 0)
3923 continue;
3924
3925 /* We need an array of the local symbols attached to the input bfd. */
3926 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3927 if (local_syms == NULL)
3928 {
3929 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3930 symtab_hdr->sh_info, 0,
3931 NULL, NULL, NULL);
3932 /* Cache them for elf_link_input_bfd. */
3933 symtab_hdr->contents = (unsigned char *) local_syms;
3934 }
3935 if (local_syms == NULL)
3936 return -1;
3937
3938 all_local_syms[bfd_indx] = local_syms;
3939 }
3940
3941 return stub_changed;
3942}
3943
3944/* Determine and set the size of the stub section for a final link.
3945
3946The basic idea here is to examine all the relocations looking for
3947PC-relative calls to a target that is unreachable with a "CALLR"
3948instruction. */
3949
3950/* See elf32-hppa.c and elf64-ppc.c. */
3951
3952bfd_boolean
3953elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd,
3954 struct bfd_link_info *info,
3955 bfd_signed_vma group_size,
3956 asection * (*add_stub_section) (const char *, asection *),
3957 void (*layout_sections_again) (void))
3958{
3959 bfd_size_type stub_group_size;
3960 bfd_boolean stubs_always_before_branch;
3961 bfd_boolean stub_changed;
3962 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3963
3964 /* Stash our params away. */
3965 htab->stub_bfd = stub_bfd;
3966 htab->add_stub_section = add_stub_section;
3967 htab->layout_sections_again = layout_sections_again;
3968 stubs_always_before_branch = group_size < 0;
3969 if (group_size < 0)
3970 stub_group_size = -group_size;
3971 else
3972 stub_group_size = group_size;
3973 if (stub_group_size == 1)
3974 {
3975 /* Default values. */
3976 /* FIXME: not sure what these values should be */
3977 if (stubs_always_before_branch)
3978 {
3979 stub_group_size = (1 << BRANCH_BITS);
3980 }
3981 else
3982 {
3983 stub_group_size = (1 << BRANCH_BITS);
3984 }
3985 }
3986
3987 group_sections (htab, stub_group_size, stubs_always_before_branch);
3988
3989 switch (get_local_syms (output_bfd, info->input_bfds, info))
3990 {
3991 default:
3992 if (htab->all_local_syms)
3993 goto error_ret_free_local;
3994 return FALSE;
3995
3996 case 0:
3997 stub_changed = FALSE;
3998 break;
3999
4000 case 1:
4001 stub_changed = TRUE;
4002 break;
4003 }
4004
4005 while (1)
4006 {
4007 bfd *input_bfd;
4008 unsigned int bfd_indx;
4009 asection *stub_sec;
4010
4011 for (input_bfd = info->input_bfds, bfd_indx = 0;
4012 input_bfd != NULL;
c72f2fb2 4013 input_bfd = input_bfd->link.next, bfd_indx++)
a3c62988
NC
4014 {
4015 Elf_Internal_Shdr *symtab_hdr;
4016 asection *section;
4017 Elf_Internal_Sym *local_syms;
4018
4019 /* We'll need the symbol table in a second. */
4020 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4021 if (symtab_hdr->sh_info == 0)
4022 continue;
4023
4024 local_syms = htab->all_local_syms[bfd_indx];
4025
4026 /* Walk over each section attached to the input bfd. */
4027 for (section = input_bfd->sections;
4028 section != NULL;
4029 section = section->next)
4030 {
4031 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4032
4033 /* If there aren't any relocs, then there's nothing more
4034 to do. */
4035 if ((section->flags & SEC_RELOC) == 0
4036 || section->reloc_count == 0)
4037 continue;
4038
4039 /* If this section is a link-once section that will be
4040 discarded, then don't create any stubs. */
4041 if (section->output_section == NULL
4042 || section->output_section->owner != output_bfd)
4043 continue;
4044
4045 /* Get the relocs. */
4046 internal_relocs
4047 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
4048 info->keep_memory);
4049 if (internal_relocs == NULL)
4050 goto error_ret_free_local;
4051
4052 /* Now examine each relocation. */
4053 irela = internal_relocs;
4054 irelaend = irela + section->reloc_count;
4055 for (; irela < irelaend; irela++)
4056 {
4057 unsigned int r_type, r_indx;
4058 enum elf_metag_stub_type stub_type;
4059 struct elf_metag_stub_hash_entry *hsh;
4060 asection *sym_sec;
4061 bfd_vma sym_value;
4062 bfd_vma destination;
4063 struct elf_metag_link_hash_entry *hh;
4064 char *stub_name;
4065 const asection *id_sec;
4066
4067 r_type = ELF32_R_TYPE (irela->r_info);
4068 r_indx = ELF32_R_SYM (irela->r_info);
4069
4070 if (r_type >= (unsigned int) R_METAG_MAX)
4071 {
4072 bfd_set_error (bfd_error_bad_value);
4073 error_ret_free_internal:
4074 if (elf_section_data (section)->relocs == NULL)
4075 free (internal_relocs);
4076 goto error_ret_free_local;
4077 }
4078
4079 /* Only look for stubs on CALLR and B instructions. */
4080 if (!(r_type == (unsigned int) R_METAG_RELBRANCH ||
4081 r_type == (unsigned int) R_METAG_RELBRANCH_PLT))
4082 continue;
4083
4084 /* Now determine the call target, its name, value,
4085 section. */
4086 sym_sec = NULL;
4087 sym_value = 0;
4088 destination = 0;
4089 hh = NULL;
4090 if (r_indx < symtab_hdr->sh_info)
4091 {
4092 /* It's a local symbol. */
4093 Elf_Internal_Sym *sym;
4094 Elf_Internal_Shdr *hdr;
4095 unsigned int shndx;
4096
4097 sym = local_syms + r_indx;
4098 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4099 sym_value = sym->st_value;
4100 shndx = sym->st_shndx;
4101 if (shndx < elf_numsections (input_bfd))
4102 {
4103 hdr = elf_elfsections (input_bfd)[shndx];
4104 sym_sec = hdr->bfd_section;
4105 destination = (sym_value + irela->r_addend
4106 + sym_sec->output_offset
4107 + sym_sec->output_section->vma);
4108 }
4109 }
4110 else
4111 {
4112 /* It's an external symbol. */
4113 int e_indx;
4114
4115 e_indx = r_indx - symtab_hdr->sh_info;
4116 hh = ((struct elf_metag_link_hash_entry *)
4117 elf_sym_hashes (input_bfd)[e_indx]);
4118
4119 while (hh->eh.root.type == bfd_link_hash_indirect
4120 || hh->eh.root.type == bfd_link_hash_warning)
4121 hh = ((struct elf_metag_link_hash_entry *)
4122 hh->eh.root.u.i.link);
4123
4124 if (hh->eh.root.type == bfd_link_hash_defined
4125 || hh->eh.root.type == bfd_link_hash_defweak)
4126 {
4127 sym_sec = hh->eh.root.u.def.section;
4128 sym_value = hh->eh.root.u.def.value;
4129 if (hh->eh.plt.offset != (bfd_vma) -1
4130 && hh->eh.dynindx != -1
4131 && r_type == (unsigned int) R_METAG_RELBRANCH_PLT)
4132 {
4133 sym_sec = htab->splt;
4134 sym_value = hh->eh.plt.offset;
4135 }
4136
4137 if (sym_sec->output_section != NULL)
4138 destination = (sym_value + irela->r_addend
4139 + sym_sec->output_offset
4140 + sym_sec->output_section->vma);
4141 else
4142 continue;
4143 }
4144 else if (hh->eh.root.type == bfd_link_hash_undefweak)
4145 {
0e1862bb 4146 if (! bfd_link_pic (info))
a3c62988
NC
4147 continue;
4148 }
4149 else if (hh->eh.root.type == bfd_link_hash_undefined)
4150 {
4151 if (! (info->unresolved_syms_in_objects == RM_IGNORE
4152 && (ELF_ST_VISIBILITY (hh->eh.other)
4153 == STV_DEFAULT)))
4154 continue;
4155 }
4156 else
4157 {
4158 bfd_set_error (bfd_error_bad_value);
4159 goto error_ret_free_internal;
4160 }
4161 }
4162
4163 /* Determine what (if any) linker stub is needed. */
4164 stub_type = metag_type_of_stub (section, irela, hh,
4165 destination, info);
4166 if (stub_type == metag_stub_none)
4167 continue;
4168
4169 /* Support for grouping stub sections. */
4170 id_sec = htab->stub_group[section->id].link_sec;
4171
4172 /* Get the name of this stub. */
4173 stub_name = metag_stub_name (id_sec, sym_sec, hh, irela);
4174 if (!stub_name)
4175 goto error_ret_free_internal;
4176
4177 hsh = metag_stub_hash_lookup (&htab->bstab,
4178 stub_name,
4179 FALSE, FALSE);
4180 if (hsh != NULL)
4181 {
4182 /* The proper stub has already been created. */
4183 free (stub_name);
4184 continue;
4185 }
4186
4187 hsh = metag_add_stub (stub_name, section, htab);
4188 if (hsh == NULL)
4189 {
4190 free (stub_name);
4191 goto error_ret_free_internal;
4192 }
4193 hsh->target_value = sym_value;
4194 hsh->target_section = sym_sec;
4195 hsh->stub_type = stub_type;
4196 hsh->hh = hh;
4197 hsh->addend = irela->r_addend;
4198 stub_changed = TRUE;
4199 }
4200
4201 /* We're done with the internal relocs, free them. */
4202 if (elf_section_data (section)->relocs == NULL)
4203 free (internal_relocs);
4204 }
4205 }
4206
4207 if (!stub_changed)
4208 break;
4209
4210 /* OK, we've added some stubs. Find out the new size of the
4211 stub sections. */
4212 for (stub_sec = htab->stub_bfd->sections;
4213 stub_sec != NULL;
4214 stub_sec = stub_sec->next)
4215 stub_sec->size = 0;
4216
4217 bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab);
4218
4219 /* Ask the linker to do its stuff. */
4220 (*htab->layout_sections_again) ();
4221 stub_changed = FALSE;
4222 }
4223
4224 free (htab->all_local_syms);
4225 return TRUE;
4226
4227 error_ret_free_local:
4228 free (htab->all_local_syms);
4229 return FALSE;
4230}
4231
4232/* Build all the stubs associated with the current output file. The
4233 stubs are kept in a hash table attached to the main linker hash
4234 table. This function is called via metagelf_finish in the linker. */
4235
4236bfd_boolean
4237elf_metag_build_stubs (struct bfd_link_info *info)
4238{
4239 asection *stub_sec;
4240 struct bfd_hash_table *table;
4241 struct elf_metag_link_hash_table *htab;
4242
4243 htab = metag_link_hash_table (info);
4244
4245 for (stub_sec = htab->stub_bfd->sections;
4246 stub_sec != NULL;
4247 stub_sec = stub_sec->next)
4248 {
4249 bfd_size_type size;
4250
4251 /* Allocate memory to hold the linker stubs. */
4252 size = stub_sec->size;
4253 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4254 if (stub_sec->contents == NULL && size != 0)
4255 return FALSE;
4256 stub_sec->size = 0;
4257 }
4258
4259 /* Build the stubs as directed by the stub hash table. */
4260 table = &htab->bstab;
4261 bfd_hash_traverse (table, metag_build_one_stub, info);
4262
4263 return TRUE;
4264}
4265
4266/* Return TRUE if SYM represents a local label symbol. */
4267
4268static bfd_boolean
4269elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
4270{
4271 if (name[0] == '$' && name[1] == 'L')
4272 return 1;
4273 return _bfd_elf_is_local_label_name (abfd, name);
4274}
4275
4276/* Return address for Ith PLT stub in section PLT, for relocation REL
4277 or (bfd_vma) -1 if it should not be included. */
4278
4279static bfd_vma
4280elf_metag_plt_sym_val (bfd_vma i, const asection *plt,
4281 const arelent *rel ATTRIBUTE_UNUSED)
4282{
4283 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4284}
4285
4286#define ELF_ARCH bfd_arch_metag
4287#define ELF_TARGET_ID METAG_ELF_DATA
4288#define ELF_MACHINE_CODE EM_METAG
4289#define ELF_MINPAGESIZE 0x1000
4290#define ELF_MAXPAGESIZE 0x4000
4291#define ELF_COMMONPAGESIZE 0x1000
4292
6d00b590 4293#define TARGET_LITTLE_SYM metag_elf32_vec
a3c62988
NC
4294#define TARGET_LITTLE_NAME "elf32-metag"
4295
4296#define elf_symbol_leading_char '_'
4297
4298#define elf_info_to_howto_rel NULL
4299#define elf_info_to_howto metag_info_to_howto_rela
4300
4301#define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
4302#define bfd_elf32_bfd_link_hash_table_create \
4303 elf_metag_link_hash_table_create
a3c62988
NC
4304#define elf_backend_relocate_section elf_metag_relocate_section
4305#define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
4306#define elf_backend_gc_sweep_hook elf_metag_gc_sweep_hook
4307#define elf_backend_check_relocs elf_metag_check_relocs
4308#define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
4309#define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
4310#define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
4311#define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
4312#define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
4313#define elf_backend_omit_section_dynsym \
4314 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4315#define elf_backend_post_process_headers elf_metag_post_process_headers
4316#define elf_backend_reloc_type_class elf_metag_reloc_type_class
4317#define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
4318#define elf_backend_plt_sym_val elf_metag_plt_sym_val
4319
4320#define elf_backend_can_gc_sections 1
4321#define elf_backend_can_refcount 1
4322#define elf_backend_got_header_size 12
4323#define elf_backend_rela_normal 1
4324#define elf_backend_want_got_sym 0
4325#define elf_backend_want_plt_sym 0
4326#define elf_backend_plt_readonly 1
4327
4328#define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4329#define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4330
4331#include "elf32-target.h"
This page took 0.709742 seconds and 4 git commands to generate.