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