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