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