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