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