1 /* Or1k-specific support for 32-bit ELF.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Contributed for OR32 by Johan Rydberg, jrydberg@opencores.org
5 PIC parts added by Stefan Kristiansson, stefan.kristiansson@saunalahti.fi,
6 largely based on elf32-m32r.c and elf32-microblaze.c.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, see <http://www.gnu.org/licenses/>. */
28 #include "libiberty.h"
30 #define PLT_ENTRY_SIZE 20
32 #define PLT0_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(.got+4) */
33 #define PLT0_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(.got+4) */
34 #define PLT0_ENTRY_WORD2 0x85ec0004 /* l.lwz r15, 4(r12) <- *(.got+8)*/
35 #define PLT0_ENTRY_WORD3 0x44007800 /* l.jr r15 */
36 #define PLT0_ENTRY_WORD4 0x858c0000 /* l.lwz r12, 0(r12) */
38 #define PLT0_PIC_ENTRY_WORD0 0x85900004 /* l.lwz r12, 4(r16) */
39 #define PLT0_PIC_ENTRY_WORD1 0x85f00008 /* l.lwz r15, 8(r16) */
40 #define PLT0_PIC_ENTRY_WORD2 0x44007800 /* l.jr r15 */
41 #define PLT0_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
42 #define PLT0_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
44 #define PLT_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(got idx addr) */
45 #define PLT_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(got idx addr) */
46 #define PLT_ENTRY_WORD2 0x858c0000 /* l.lwz r12, 0(r12) */
47 #define PLT_ENTRY_WORD3 0x44006000 /* l.jr r12 */
48 #define PLT_ENTRY_WORD4 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
50 #define PLT_PIC_ENTRY_WORD0 0x85900000 /* l.lwz r12, 0(r16) <- index in got */
51 #define PLT_PIC_ENTRY_WORD1 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
52 #define PLT_PIC_ENTRY_WORD2 0x44006000 /* l.jr r12 */
53 #define PLT_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
54 #define PLT_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
56 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
58 static reloc_howto_type or1k_elf_howto_table[] =
60 /* This reloc does nothing. */
61 HOWTO (R_OR1K_NONE, /* type */
63 3, /* size (0 = byte, 1 = short, 2 = long) */
65 FALSE, /* pc_relative */
67 complain_overflow_dont, /* complain_on_overflow */
68 bfd_elf_generic_reloc, /* special_function */
69 "R_OR1K_NONE", /* name */
70 FALSE, /* partial_inplace */
73 FALSE), /* pcrel_offset */
77 2, /* size (0 = byte, 1 = short, 2 = long) */
79 FALSE, /* pc_relative */
81 complain_overflow_unsigned, /* complain_on_overflow */
82 bfd_elf_generic_reloc, /* special_function */
83 "R_OR1K_32", /* name */
84 FALSE, /* partial_inplace */
86 0xffffffff, /* dst_mask */
87 FALSE), /* pcrel_offset */
91 1, /* size (0 = byte, 1 = short, 2 = long) */
93 FALSE, /* pc_relative */
95 complain_overflow_unsigned, /* complain_on_overflow */
96 bfd_elf_generic_reloc, /* special_function */
97 "R_OR1K_16", /* name */
98 FALSE, /* partial_inplace */
100 0xffff, /* dst_mask */
101 FALSE), /* pcrel_offset */
105 0, /* size (0 = byte, 1 = short, 2 = long) */
107 FALSE, /* pc_relative */
109 complain_overflow_unsigned, /* complain_on_overflow */
110 bfd_elf_generic_reloc, /* special_function */
111 "R_OR1K_8", /* name */
112 FALSE, /* partial_inplace */
115 FALSE), /* pcrel_offset */
117 HOWTO (R_OR1K_LO_16_IN_INSN, /* type */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
121 FALSE, /* pc_relative */
123 complain_overflow_dont, /* complain_on_overflow */
124 bfd_elf_generic_reloc, /* special_function */
125 "R_OR1K_LO_16_IN_INSN", /* name */
126 FALSE, /* partial_inplace */
128 0x0000ffff, /* dst_mask */
129 FALSE), /* pcrel_offset */
131 HOWTO (R_OR1K_HI_16_IN_INSN, /* type */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
135 FALSE, /* pc_relative */
137 complain_overflow_dont, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_OR1K_HI_16_IN_INSN", /* name */
140 FALSE, /* partial_inplace */
142 0x0000ffff, /* dst_mask */
143 FALSE), /* pcrel_offset */
145 /* A PC relative 26 bit relocation, right shifted by 2. */
146 HOWTO (R_OR1K_INSN_REL_26, /* type */
148 2, /* size (0 = byte, 1 = short, 2 = long) */
150 TRUE, /* pc_relative */
152 complain_overflow_signed, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_OR1K_INSN_REL_26", /* name */
155 FALSE, /* partial_inplace */
157 0x03ffffff, /* dst_mask */
158 TRUE), /* pcrel_offset */
160 /* GNU extension to record C++ vtable hierarchy. */
161 HOWTO (R_OR1K_GNU_VTINHERIT, /* type */
163 2, /* size (0 = byte, 1 = short, 2 = long) */
165 FALSE, /* pc_relative */
167 complain_overflow_dont, /* complain_on_overflow */
168 NULL, /* special_function */
169 "R_OR1K_GNU_VTINHERIT", /* name */
170 FALSE, /* partial_inplace */
173 FALSE), /* pcrel_offset */
175 /* GNU extension to record C++ vtable member usage. */
176 HOWTO (R_OR1K_GNU_VTENTRY, /* type */
178 2, /* size (0 = byte, 1 = short, 2 = long) */
180 FALSE, /* pc_relative */
182 complain_overflow_dont, /* complain_on_overflow */
183 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
184 "R_OR1K_GNU_VTENTRY", /* name */
185 FALSE, /* partial_inplace */
188 FALSE), /* pcrel_offset */
190 HOWTO (R_OR1K_32_PCREL,
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 TRUE, /* pc_relative */
196 complain_overflow_signed, /* complain_on_overflow */
197 bfd_elf_generic_reloc, /* special_function */
198 "R_OR1K_32_PCREL", /* name */
199 FALSE, /* partial_inplace */
201 0xffffffff, /* dst_mask */
202 TRUE), /* pcrel_offset */
204 HOWTO (R_OR1K_16_PCREL,
206 1, /* size (0 = byte, 1 = short, 2 = long) */
208 TRUE, /* pc_relative */
210 complain_overflow_signed, /* complain_on_overflow */
211 bfd_elf_generic_reloc, /* special_function */
212 "R_OR1K_16_PCREL", /* name */
213 FALSE, /* partial_inplace */
215 0xffff, /* dst_mask */
216 TRUE), /* pcrel_offset */
218 HOWTO (R_OR1K_8_PCREL,
220 0, /* size (0 = byte, 1 = short, 2 = long) */
222 TRUE, /* pc_relative */
224 complain_overflow_signed, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_OR1K_8_PCREL", /* name */
227 FALSE, /* partial_inplace */
230 TRUE), /* pcrel_offset */
232 HOWTO (R_OR1K_GOTPC_HI16, /* Type. */
233 16, /* Rightshift. */
234 2, /* Size (0 = byte, 1 = short, 2 = long). */
236 TRUE, /* PC_relative. */
238 complain_overflow_dont, /* Complain on overflow. */
239 bfd_elf_generic_reloc, /* Special Function. */
240 "R_OR1K_GOTPC_HI16", /* Name. */
241 FALSE, /* Partial Inplace. */
242 0, /* Source Mask. */
243 0xffff, /* Dest Mask. */
244 TRUE), /* PC relative offset? */
246 HOWTO (R_OR1K_GOTPC_LO16, /* Type. */
248 2, /* Size (0 = byte, 1 = short, 2 = long). */
250 TRUE, /* PC_relative. */
252 complain_overflow_dont, /* Complain on overflow. */
253 bfd_elf_generic_reloc, /* Special Function. */
254 "R_OR1K_GOTPC_LO16", /* Name. */
255 FALSE, /* Partial Inplace. */
256 0, /* Source Mask. */
257 0xffff, /* Dest Mask. */
258 TRUE), /* PC relative offset? */
260 HOWTO (R_OR1K_GOT16, /* type */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
264 FALSE, /* pc_relative */
266 complain_overflow_signed, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_OR1K_GOT16", /* name */
269 FALSE, /* partial_inplace */
271 0xffff, /* dst_mask */
272 FALSE), /* pcrel_offset */
274 /* A 26 bit PLT relocation. Shifted by 2. */
275 HOWTO (R_OR1K_PLT26, /* Type. */
277 2, /* Size (0 = byte, 1 = short, 2 = long). */
279 TRUE, /* PC_relative. */
281 complain_overflow_dont, /* Complain on overflow. */
282 bfd_elf_generic_reloc,/* Special Function. */
283 "R_OR1K_PLT26", /* Name. */
284 FALSE, /* Partial Inplace. */
285 0, /* Source Mask. */
286 0x03ffffff, /* Dest Mask. */
287 TRUE), /* PC relative offset? */
289 HOWTO (R_OR1K_GOTOFF_HI16, /* type */
291 2, /* size (0 = byte, 1 = short, 2 = long) */
293 FALSE, /* pc_relative */
295 complain_overflow_dont, /* complain_on_overflow */
296 bfd_elf_generic_reloc, /* special_function */
297 "R_OR1K_GOTOFF_HI16", /* name */
298 FALSE, /* partial_inplace */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
303 HOWTO (R_OR1K_GOTOFF_LO16, /* type */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
307 FALSE, /* pc_relative */
309 complain_overflow_dont, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_OR1K_GOTOFF_LO16", /* name */
312 FALSE, /* partial_inplace */
314 0xffff, /* dst_mask */
315 FALSE), /* pcrel_offset */
317 HOWTO (R_OR1K_COPY, /* type */
319 2, /* size (0 = byte, 1 = short, 2 = long) */
321 FALSE, /* pc_relative */
323 complain_overflow_bitfield, /* complain_on_overflow */
324 bfd_elf_generic_reloc, /* special_function */
325 "R_OR1K_COPY", /* name */
326 FALSE, /* partial_inplace */
327 0xffffffff, /* src_mask */
328 0xffffffff, /* dst_mask */
329 FALSE), /* pcrel_offset */
331 HOWTO (R_OR1K_GLOB_DAT, /* type */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
335 FALSE, /* pc_relative */
337 complain_overflow_bitfield, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_OR1K_GLOB_DAT", /* name */
340 FALSE, /* partial_inplace */
341 0xffffffff, /* src_mask */
342 0xffffffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
345 HOWTO (R_OR1K_JMP_SLOT, /* type */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
349 FALSE, /* pc_relative */
351 complain_overflow_bitfield, /* complain_on_overflow */
352 bfd_elf_generic_reloc, /* special_function */
353 "R_OR1K_JMP_SLOT", /* name */
354 FALSE, /* partial_inplace */
355 0xffffffff, /* src_mask */
356 0xffffffff, /* dst_mask */
357 FALSE), /* pcrel_offset */
359 HOWTO (R_OR1K_RELATIVE, /* type */
361 2, /* size (0 = byte, 1 = short, 2 = long) */
363 FALSE, /* pc_relative */
365 complain_overflow_bitfield, /* complain_on_overflow */
366 bfd_elf_generic_reloc, /* special_function */
367 "R_OR1K_RELATIVE", /* name */
368 FALSE, /* partial_inplace */
369 0xffffffff, /* src_mask */
370 0xffffffff, /* dst_mask */
371 FALSE), /* pcrel_offset */
373 HOWTO (R_OR1K_TLS_GD_HI16, /* type */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
377 FALSE, /* pc_relative */
379 complain_overflow_dont, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_OR1K_TLS_GD_HI16", /* name */
382 FALSE, /* partial_inplace */
384 0xffff, /* dst_mask */
385 FALSE), /* pcrel_offset */
387 HOWTO (R_OR1K_TLS_GD_LO16, /* type */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
391 FALSE, /* pc_relative */
393 complain_overflow_dont, /* complain_on_overflow */
394 bfd_elf_generic_reloc, /* special_function */
395 "R_OR1K_TLS_GD_LO16", /* name */
396 FALSE, /* partial_inplace */
398 0xffff, /* dst_mask */
399 FALSE), /* pcrel_offset */
401 HOWTO (R_OR1K_TLS_LDM_HI16, /* type */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
405 FALSE, /* pc_relative */
407 complain_overflow_dont, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_OR1K_TLS_LDM_HI16", /* name */
410 FALSE, /* partial_inplace */
412 0xffff, /* dst_mask */
413 FALSE), /* pcrel_offset */
415 HOWTO (R_OR1K_TLS_LDM_LO16, /* type */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
419 FALSE, /* pc_relative */
421 complain_overflow_dont, /* complain_on_overflow */
422 bfd_elf_generic_reloc, /* special_function */
423 "R_OR1K_TLS_LDM_LO16", /* name */
424 FALSE, /* partial_inplace */
426 0xffff, /* dst_mask */
427 FALSE), /* pcrel_offset */
429 HOWTO (R_OR1K_TLS_LDO_HI16, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 FALSE, /* pc_relative */
435 complain_overflow_dont, /* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_OR1K_TLS_LDO_HI16", /* name */
438 FALSE, /* partial_inplace */
440 0xffff, /* dst_mask */
441 FALSE), /* pcrel_offset */
443 HOWTO (R_OR1K_TLS_LDO_LO16, /* type */
445 2, /* size (0 = byte, 1 = short, 2 = long) */
447 FALSE, /* pc_relative */
449 complain_overflow_dont, /* complain_on_overflow */
450 bfd_elf_generic_reloc, /* special_function */
451 "R_OR1K_TLS_LDO_LO16", /* name */
452 FALSE, /* partial_inplace */
454 0xffff, /* dst_mask */
455 FALSE), /* pcrel_offset */
457 HOWTO (R_OR1K_TLS_IE_HI16, /* type */
459 2, /* size (0 = byte, 1 = short, 2 = long) */
461 FALSE, /* pc_relative */
463 complain_overflow_dont, /* complain_on_overflow */
464 bfd_elf_generic_reloc, /* special_function */
465 "R_OR1K_TLS_IE_HI16", /* name */
466 FALSE, /* partial_inplace */
468 0xffff, /* dst_mask */
469 FALSE), /* pcrel_offset */
471 HOWTO (R_OR1K_TLS_IE_LO16, /* type */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
475 FALSE, /* pc_relative */
477 complain_overflow_dont, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_OR1K_TLS_IE_LO16", /* name */
480 FALSE, /* partial_inplace */
482 0xffff, /* dst_mask */
483 FALSE), /* pcrel_offset */
485 HOWTO (R_OR1K_TLS_LE_HI16, /* type */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
489 FALSE, /* pc_relative */
491 complain_overflow_dont, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_OR1K_TLS_LE_HI16", /* name */
494 FALSE, /* partial_inplace */
496 0xffff, /* dst_mask */
497 FALSE), /* pcrel_offset */
499 HOWTO (R_OR1K_TLS_LE_LO16, /* type */
501 2, /* size (0 = byte, 1 = short, 2 = long) */
503 FALSE, /* pc_relative */
505 complain_overflow_dont, /* complain_on_overflow */
506 bfd_elf_generic_reloc, /* special_function */
507 "R_OR1K_TLS_LE_LO16", /* name */
508 FALSE, /* partial_inplace */
510 0xffff, /* dst_mask */
511 FALSE), /* pcrel_offset */
515 /* Map BFD reloc types to Or1k ELF reloc types. */
517 struct or1k_reloc_map
519 bfd_reloc_code_real_type bfd_reloc_val;
520 unsigned int or1k_reloc_val;
523 static const struct or1k_reloc_map or1k_reloc_map[] =
525 { BFD_RELOC_NONE, R_OR1K_NONE },
526 { BFD_RELOC_32, R_OR1K_32 },
527 { BFD_RELOC_16, R_OR1K_16 },
528 { BFD_RELOC_8, R_OR1K_8 },
529 { BFD_RELOC_LO16, R_OR1K_LO_16_IN_INSN },
530 { BFD_RELOC_HI16, R_OR1K_HI_16_IN_INSN },
531 { BFD_RELOC_OR1K_REL_26, R_OR1K_INSN_REL_26 },
532 { BFD_RELOC_VTABLE_ENTRY, R_OR1K_GNU_VTENTRY },
533 { BFD_RELOC_VTABLE_INHERIT, R_OR1K_GNU_VTINHERIT },
534 { BFD_RELOC_32_PCREL, R_OR1K_32_PCREL },
535 { BFD_RELOC_16_PCREL, R_OR1K_16_PCREL },
536 { BFD_RELOC_8_PCREL, R_OR1K_8_PCREL },
537 { BFD_RELOC_OR1K_GOTPC_HI16, R_OR1K_GOTPC_HI16 },
538 { BFD_RELOC_OR1K_GOTPC_LO16, R_OR1K_GOTPC_LO16 },
539 { BFD_RELOC_OR1K_GOT16, R_OR1K_GOT16 },
540 { BFD_RELOC_OR1K_PLT26, R_OR1K_PLT26 },
541 { BFD_RELOC_OR1K_GOTOFF_HI16, R_OR1K_GOTOFF_HI16 },
542 { BFD_RELOC_OR1K_GOTOFF_LO16, R_OR1K_GOTOFF_LO16 },
543 { BFD_RELOC_OR1K_GLOB_DAT, R_OR1K_GLOB_DAT },
544 { BFD_RELOC_OR1K_COPY, R_OR1K_COPY },
545 { BFD_RELOC_OR1K_JMP_SLOT, R_OR1K_JMP_SLOT },
546 { BFD_RELOC_OR1K_RELATIVE, R_OR1K_RELATIVE },
547 { BFD_RELOC_OR1K_TLS_GD_HI16, R_OR1K_TLS_GD_HI16 },
548 { BFD_RELOC_OR1K_TLS_GD_LO16, R_OR1K_TLS_GD_LO16 },
549 { BFD_RELOC_OR1K_TLS_LDM_HI16, R_OR1K_TLS_LDM_HI16 },
550 { BFD_RELOC_OR1K_TLS_LDM_LO16, R_OR1K_TLS_LDM_LO16 },
551 { BFD_RELOC_OR1K_TLS_LDO_HI16, R_OR1K_TLS_LDO_HI16 },
552 { BFD_RELOC_OR1K_TLS_LDO_LO16, R_OR1K_TLS_LDO_LO16 },
553 { BFD_RELOC_OR1K_TLS_IE_HI16, R_OR1K_TLS_IE_HI16 },
554 { BFD_RELOC_OR1K_TLS_IE_LO16, R_OR1K_TLS_IE_LO16 },
555 { BFD_RELOC_OR1K_TLS_LE_HI16, R_OR1K_TLS_LE_HI16 },
556 { BFD_RELOC_OR1K_TLS_LE_LO16, R_OR1K_TLS_LE_LO16 },
559 #define TLS_UNKNOWN 0
566 /* ELF linker hash entry. */
567 struct elf_or1k_link_hash_entry
569 struct elf_link_hash_entry root;
571 /* Track dynamic relocs copied for this symbol. */
572 struct elf_dyn_relocs *dyn_relocs;
574 /* Track type of TLS access. */
575 unsigned char tls_type;
578 /* ELF object data. */
579 struct elf_or1k_obj_tdata
581 struct elf_obj_tdata root;
583 /* tls_type for each local got entry. */
584 unsigned char *local_tls_type;
587 #define elf_or1k_tdata(abfd) \
588 ((struct elf_or1k_obj_tdata *) (abfd)->tdata.any)
590 #define elf_or1k_local_tls_type(abfd) \
591 (elf_or1k_tdata (abfd)->local_tls_type)
593 /* ELF linker hash table. */
594 struct elf_or1k_link_hash_table
596 struct elf_link_hash_table root;
598 /* Small local sym to section mapping cache. */
599 struct sym_cache sym_sec;
602 /* Get the ELF linker hash table from a link_info structure. */
603 #define or1k_elf_hash_table(p) \
604 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
605 == OR1K_ELF_DATA ? ((struct elf_or1k_link_hash_table *) ((p)->hash)) : NULL)
608 elf_or1k_mkobject (bfd *abfd)
610 return bfd_elf_allocate_object (abfd, sizeof (struct elf_or1k_obj_tdata),
614 /* Create an entry in an or1k ELF linker hash table. */
616 static struct bfd_hash_entry *
617 or1k_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
618 struct bfd_hash_table *table,
621 struct elf_or1k_link_hash_entry *ret =
622 (struct elf_or1k_link_hash_entry *) entry;
624 /* Allocate the structure if it has not already been allocated by a
627 ret = bfd_hash_allocate (table,
628 sizeof (struct elf_or1k_link_hash_entry));
632 /* Call the allocation method of the superclass. */
633 ret = ((struct elf_or1k_link_hash_entry *)
634 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
638 struct elf_or1k_link_hash_entry *eh;
640 eh = (struct elf_or1k_link_hash_entry *) ret;
641 eh->dyn_relocs = NULL;
642 eh->tls_type = TLS_UNKNOWN;
645 return (struct bfd_hash_entry *) ret;
648 /* Create an or1k ELF linker hash table. */
650 static struct bfd_link_hash_table *
651 or1k_elf_link_hash_table_create (bfd *abfd)
653 struct elf_or1k_link_hash_table *ret;
654 bfd_size_type amt = sizeof (struct elf_or1k_link_hash_table);
656 ret = bfd_zmalloc (amt);
660 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
661 or1k_elf_link_hash_newfunc,
662 sizeof (struct elf_or1k_link_hash_entry),
669 return &ret->root.root;
672 static reloc_howto_type *
673 or1k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
674 bfd_reloc_code_real_type code)
678 for (i = ARRAY_SIZE (or1k_reloc_map); i--;)
679 if (or1k_reloc_map[i].bfd_reloc_val == code)
680 return & or1k_elf_howto_table[or1k_reloc_map[i].or1k_reloc_val];
685 static reloc_howto_type *
686 or1k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
692 i < (sizeof (or1k_elf_howto_table)
693 / sizeof (or1k_elf_howto_table[0]));
695 if (or1k_elf_howto_table[i].name != NULL
696 && strcasecmp (or1k_elf_howto_table[i].name, r_name) == 0)
697 return &or1k_elf_howto_table[i];
702 /* Set the howto pointer for an Or1k ELF reloc. */
705 or1k_info_to_howto_rela (bfd * abfd,
707 Elf_Internal_Rela * dst)
711 r_type = ELF32_R_TYPE (dst->r_info);
712 if (r_type >= (unsigned int) R_OR1K_max)
714 /* xgettext:c-format */
715 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
719 cache_ptr->howto = & or1k_elf_howto_table[r_type];
723 /* Return the relocation value for @tpoff relocations.. */
725 tpoff (struct bfd_link_info *info, bfd_vma address)
727 /* If tls_sec is NULL, we should have signalled an error already. */
728 if (elf_hash_table (info)->tls_sec == NULL)
731 /* The thread pointer on or1k stores the address after the TCB where
732 the data is, just compute the difference. No need to compensate
733 for the size of TCB. */
734 return (address - elf_hash_table (info)->tls_sec->vma);
737 /* Relocate an Or1k ELF section.
739 The RELOCATE_SECTION function is called by the new ELF backend linker
740 to handle the relocations for a section.
742 The relocs are always passed as Rela structures; if the section
743 actually uses Rel structures, the r_addend field will always be
746 This function is responsible for adjusting the section contents as
747 necessary, and (if using Rela relocs and generating a relocatable
748 output file) adjusting the reloc addend as necessary.
750 This function does not have to worry about setting the reloc
751 address or the reloc symbol index.
753 LOCAL_SYMS is a pointer to the swapped in local symbols.
755 LOCAL_SECTIONS is an array giving the section in the input file
756 corresponding to the st_shndx field of each local symbol.
758 The global hash table entry for the global symbols can be found
759 via elf_sym_hashes (input_bfd).
761 When generating relocatable output, this function must handle
762 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
763 going to be the section symbol corresponding to the output
764 section, which means that the addend must be adjusted
768 or1k_elf_relocate_section (bfd *output_bfd,
769 struct bfd_link_info *info,
771 asection *input_section,
773 Elf_Internal_Rela *relocs,
774 Elf_Internal_Sym *local_syms,
775 asection **local_sections)
777 Elf_Internal_Shdr *symtab_hdr;
778 struct elf_link_hash_entry **sym_hashes;
779 Elf_Internal_Rela *rel;
780 Elf_Internal_Rela *relend;
781 struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
784 bfd_vma *local_got_offsets;
790 dynobj = htab->root.dynobj;
791 local_got_offsets = elf_local_got_offsets (input_bfd);
793 sreloc = elf_section_data (input_section)->sreloc;
795 sgot = htab->root.sgot;
797 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
798 sym_hashes = elf_sym_hashes (input_bfd);
799 relend = relocs + input_section->reloc_count;
801 for (rel = relocs; rel < relend; rel++)
803 reloc_howto_type *howto;
804 unsigned long r_symndx;
805 Elf_Internal_Sym *sym;
807 struct elf_link_hash_entry *h;
809 bfd_reloc_status_type r;
810 const char *name = NULL;
813 r_type = ELF32_R_TYPE (rel->r_info);
814 r_symndx = ELF32_R_SYM (rel->r_info);
816 if (r_type == R_OR1K_GNU_VTINHERIT
817 || r_type == R_OR1K_GNU_VTENTRY)
820 if (r_type < 0 || r_type >= (int) R_OR1K_max)
822 bfd_set_error (bfd_error_bad_value);
826 howto = or1k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
831 if (r_symndx < symtab_hdr->sh_info)
833 sym = local_syms + r_symndx;
834 sec = local_sections[r_symndx];
835 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
837 name = bfd_elf_string_from_elf_section
838 (input_bfd, symtab_hdr->sh_link, sym->st_name);
839 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
843 bfd_boolean unresolved_reloc, warned, ignored;
845 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
846 r_symndx, symtab_hdr, sym_hashes,
848 unresolved_reloc, warned, ignored);
851 if (sec != NULL && discarded_section (sec))
852 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
853 rel, 1, relend, howto, 0, contents);
855 if (bfd_link_relocatable (info))
862 if (htab->root.splt != NULL && h != NULL
863 && h->plt.offset != (bfd_vma) -1)
865 relocation = (htab->root.splt->output_section->vma
866 + htab->root.splt->output_offset
873 /* Relocation is to the entry for this symbol in the global
875 BFD_ASSERT (sgot != NULL);
882 BFD_ASSERT (off != (bfd_vma) -1);
884 dyn = htab->root.dynamic_sections_created;
885 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
888 || (bfd_link_pic (info)
889 && SYMBOL_REFERENCES_LOCAL (info, h)))
891 /* This is actually a static link, or it is a
892 -Bsymbolic link and the symbol is defined
893 locally, or the symbol was forced to be local
894 because of a version file. We must initialize
895 this entry in the global offset table. Since the
896 offset must always be a multiple of 4, we use the
897 least significant bit to record whether we have
898 initialized it already.
900 When doing a dynamic link, we create a .rela.got
901 relocation entry to initialize the value. This
902 is done in the finish_dynamic_symbol routine. */
907 /* Write entry in GOT. */
908 bfd_put_32 (output_bfd, relocation,
909 sgot->contents + off);
910 /* Mark GOT entry as having been written. */
915 relocation = sgot->output_offset + off;
922 BFD_ASSERT (local_got_offsets != NULL
923 && local_got_offsets[r_symndx] != (bfd_vma) -1);
925 /* Get offset into GOT table. */
926 off = local_got_offsets[r_symndx];
928 /* The offset must always be a multiple of 4. We use
929 the least significant bit to record whether we have
930 already processed this entry. */
935 /* Write entry in GOT. */
936 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
937 if (bfd_link_pic (info))
940 Elf_Internal_Rela outrel;
942 /* We need to generate a R_OR1K_RELATIVE reloc
943 for the dynamic linker. */
944 srelgot = htab->root.srelgot;
945 BFD_ASSERT (srelgot != NULL);
947 outrel.r_offset = (sgot->output_section->vma
948 + sgot->output_offset
950 outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
951 outrel.r_addend = relocation;
952 loc = srelgot->contents;
953 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
954 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
955 ++srelgot->reloc_count;
958 local_got_offsets[r_symndx] |= 1;
960 relocation = sgot->output_offset + off;
963 /* Addend should be zero. */
964 if (rel->r_addend != 0)
966 (_("internal error: addend should be zero for %s"),
971 case R_OR1K_GOTOFF_LO16:
972 case R_OR1K_GOTOFF_HI16:
973 /* Relocation is offset from GOT. */
974 BFD_ASSERT (sgot != NULL);
976 -= (htab->root.hgot->root.u.def.value
977 + htab->root.hgot->root.u.def.section->output_offset
978 + htab->root.hgot->root.u.def.section->output_section->vma);
981 case R_OR1K_INSN_REL_26:
982 case R_OR1K_HI_16_IN_INSN:
983 case R_OR1K_LO_16_IN_INSN:
987 /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
988 from removed linkonce sections, or sections discarded by
990 if (r_symndx == STN_UNDEF
991 || (input_section->flags & SEC_ALLOC) == 0)
994 if ((bfd_link_pic (info)
996 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
997 || h->root.type != bfd_link_hash_undefweak)
998 && (howto->type != R_OR1K_INSN_REL_26
999 || !SYMBOL_CALLS_LOCAL (info, h)))
1000 || (!bfd_link_pic (info)
1006 || h->root.type == bfd_link_hash_undefweak
1007 || h->root.type == bfd_link_hash_undefined)))
1009 Elf_Internal_Rela outrel;
1013 /* When generating a shared object, these relocations
1014 are copied into the output file to be resolved at run
1017 BFD_ASSERT (sreloc != NULL);
1022 _bfd_elf_section_offset (output_bfd, info, input_section,
1024 if (outrel.r_offset == (bfd_vma) -1)
1026 else if (outrel.r_offset == (bfd_vma) -2)
1028 outrel.r_offset += (input_section->output_section->vma
1029 + input_section->output_offset);
1032 memset (&outrel, 0, sizeof outrel);
1033 /* h->dynindx may be -1 if the symbol was marked to
1036 && ((! info->symbolic && h->dynindx != -1)
1037 || !h->def_regular))
1039 BFD_ASSERT (h->dynindx != -1);
1040 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1041 outrel.r_addend = rel->r_addend;
1045 if (r_type == R_OR1K_32)
1047 outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1048 outrel.r_addend = relocation + rel->r_addend;
1054 (_("%pB: probably compiled without -fPIC?"),
1056 bfd_set_error (bfd_error_bad_value);
1061 loc = sreloc->contents;
1062 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1063 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1069 case R_OR1K_TLS_LDM_HI16:
1070 case R_OR1K_TLS_LDM_LO16:
1071 case R_OR1K_TLS_LDO_HI16:
1072 case R_OR1K_TLS_LDO_LO16:
1073 /* TODO: implement support for local dynamic. */
1076 (_("%pB: support for local dynamic not implemented"),
1078 bfd_set_error (bfd_error_bad_value);
1082 case R_OR1K_TLS_GD_HI16:
1083 case R_OR1K_TLS_GD_LO16:
1084 case R_OR1K_TLS_IE_HI16:
1085 case R_OR1K_TLS_IE_LO16:
1088 Elf_Internal_Rela rela;
1092 sreloc = bfd_get_section_by_name (dynobj, ".rela.got");
1094 /* Mark as TLS related GOT entry by setting
1095 bit 2 as well as bit 1. */
1098 gotoff = h->got.offset;
1103 gotoff = local_got_offsets[r_symndx];
1104 local_got_offsets[r_symndx] |= 3;
1107 /* Only process the relocation once. */
1110 relocation = sgot->output_offset + (gotoff & ~3);
1114 BFD_ASSERT (elf_hash_table (info)->hgot == NULL
1115 || elf_hash_table (info)->hgot->root.u.def.value == 0);
1117 /* Dynamic entries will require relocations. if we do not need
1118 them we will just use the default R_OR1K_NONE and
1119 not set anything. */
1120 dynamic = bfd_link_pic (info)
1121 || (sec && (sec->flags & SEC_ALLOC) != 0
1123 && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
1126 if (dynamic && (howto->type == R_OR1K_TLS_GD_HI16
1127 || howto->type == R_OR1K_TLS_GD_LO16))
1131 /* Add DTPMOD and DTPOFF GOT and rela entries. */
1132 for (i = 0; i < 2; ++i)
1134 rela.r_offset = sgot->output_section->vma +
1135 sgot->output_offset + gotoff + i*4;
1136 if (h != NULL && h->dynindx != -1)
1138 rela.r_info = ELF32_R_INFO (h->dynindx,
1139 (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1144 rela.r_info = ELF32_R_INFO (0,
1145 (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1146 rela.r_addend = tpoff (info, relocation);
1149 loc = sreloc->contents;
1150 loc += sreloc->reloc_count++ *
1151 sizeof (Elf32_External_Rela);
1153 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1154 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
1158 else if (howto->type == R_OR1K_TLS_GD_HI16
1159 || howto->type == R_OR1K_TLS_GD_LO16)
1161 bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
1162 bfd_put_32 (output_bfd, tpoff (info, relocation),
1163 sgot->contents + gotoff + 4);
1168 /* Add TPOFF GOT and rela entries. */
1169 rela.r_offset = sgot->output_section->vma +
1170 sgot->output_offset + gotoff;
1171 if (h != NULL && h->dynindx != -1)
1173 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_TLS_TPOFF);
1178 rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
1179 rela.r_addend = tpoff (info, relocation);
1182 loc = sreloc->contents;
1183 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1185 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1186 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
1191 bfd_put_32 (output_bfd, tpoff (info, relocation),
1192 sgot->contents + gotoff);
1194 relocation = sgot->output_offset + gotoff;
1197 case R_OR1K_TLS_LE_HI16:
1198 case R_OR1K_TLS_LE_LO16:
1200 /* Relocation is offset from TP. */
1201 relocation = tpoff (info, relocation);
1204 case R_OR1K_TLS_DTPMOD:
1205 case R_OR1K_TLS_DTPOFF:
1206 case R_OR1K_TLS_TPOFF:
1207 /* These are resolved dynamically on load and shouldn't
1208 be used as linker input. */
1211 (_("%pB: will not resolve runtime TLS relocation"),
1213 bfd_set_error (bfd_error_bad_value);
1219 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
1220 rel->r_offset, relocation, rel->r_addend);
1222 if (r != bfd_reloc_ok)
1224 const char *msg = NULL;
1228 case bfd_reloc_overflow:
1229 (*info->callbacks->reloc_overflow)
1230 (info, (h ? &h->root : NULL), name, howto->name,
1231 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1234 case bfd_reloc_undefined:
1235 (*info->callbacks->undefined_symbol)
1236 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1239 case bfd_reloc_outofrange:
1240 msg = _("internal error: out of range error");
1243 case bfd_reloc_notsupported:
1244 msg = _("internal error: unsupported relocation error");
1247 case bfd_reloc_dangerous:
1248 msg = _("internal error: dangerous relocation");
1252 msg = _("internal error: unknown error");
1257 (*info->callbacks->warning) (info, msg, name, input_bfd,
1258 input_section, rel->r_offset);
1265 /* Return the section that should be marked against GC for a given
1269 or1k_elf_gc_mark_hook (asection *sec,
1270 struct bfd_link_info *info,
1271 Elf_Internal_Rela *rel,
1272 struct elf_link_hash_entry *h,
1273 Elf_Internal_Sym *sym)
1276 switch (ELF32_R_TYPE (rel->r_info))
1278 case R_OR1K_GNU_VTINHERIT:
1279 case R_OR1K_GNU_VTENTRY:
1283 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1286 /* Look through the relocs for a section during the first phase. */
1289 or1k_elf_check_relocs (bfd *abfd,
1290 struct bfd_link_info *info,
1292 const Elf_Internal_Rela *relocs)
1294 Elf_Internal_Shdr *symtab_hdr;
1295 struct elf_link_hash_entry **sym_hashes;
1296 const Elf_Internal_Rela *rel;
1298 const Elf_Internal_Rela *rel_end;
1299 struct elf_or1k_link_hash_table *htab;
1301 asection *sreloc = NULL;
1303 if (bfd_link_relocatable (info))
1306 /* Don't do anything special with non-loaded, non-alloced sections.
1307 In particular, any relocs in such sections should not affect GOT
1308 and PLT reference counting (ie. we don't allow them to create GOT
1309 or PLT entries), there's no possibility or desire to optimize TLS
1310 relocs, and there's not much point in propagating relocs to shared
1311 libs that the dynamic linker won't relocate. */
1312 if ((sec->flags & SEC_ALLOC) == 0)
1315 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1316 sym_hashes = elf_sym_hashes (abfd);
1318 htab = or1k_elf_hash_table (info);
1322 dynobj = htab->root.dynobj;
1324 rel_end = relocs + sec->reloc_count;
1325 for (rel = relocs; rel < rel_end; rel++)
1327 struct elf_link_hash_entry *h;
1328 unsigned long r_symndx;
1329 unsigned char tls_type;
1331 r_symndx = ELF32_R_SYM (rel->r_info);
1332 if (r_symndx < symtab_hdr->sh_info)
1336 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1337 while (h->root.type == bfd_link_hash_indirect
1338 || h->root.type == bfd_link_hash_warning)
1339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1342 switch (ELF32_R_TYPE (rel->r_info))
1344 case R_OR1K_TLS_GD_HI16:
1345 case R_OR1K_TLS_GD_LO16:
1348 case R_OR1K_TLS_LDM_HI16:
1349 case R_OR1K_TLS_LDM_LO16:
1350 case R_OR1K_TLS_LDO_HI16:
1351 case R_OR1K_TLS_LDO_LO16:
1354 case R_OR1K_TLS_IE_HI16:
1355 case R_OR1K_TLS_IE_LO16:
1358 case R_OR1K_TLS_LE_HI16:
1359 case R_OR1K_TLS_LE_LO16:
1363 tls_type = TLS_NONE;
1366 /* Record TLS type. */
1368 ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
1371 unsigned char *local_tls_type;
1373 /* This is a TLS type record for a local symbol. */
1374 local_tls_type = (unsigned char *) elf_or1k_local_tls_type (abfd);
1375 if (local_tls_type == NULL)
1379 size = symtab_hdr->sh_info;
1380 local_tls_type = bfd_zalloc (abfd, size);
1381 if (local_tls_type == NULL)
1383 elf_or1k_local_tls_type (abfd) = local_tls_type;
1385 local_tls_type[r_symndx] = tls_type;
1388 switch (ELF32_R_TYPE (rel->r_info))
1390 /* This relocation describes the C++ object vtable hierarchy.
1391 Reconstruct it for later use during GC. */
1392 case R_OR1K_GNU_VTINHERIT:
1393 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1397 /* This relocation describes which C++ vtable entries are actually
1398 used. Record for later use during GC. */
1399 case R_OR1K_GNU_VTENTRY:
1400 BFD_ASSERT (h != NULL);
1402 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1406 /* This relocation requires .plt entry. */
1411 h->plt.refcount += 1;
1416 case R_OR1K_GOTOFF_HI16:
1417 case R_OR1K_GOTOFF_LO16:
1418 case R_OR1K_TLS_GD_HI16:
1419 case R_OR1K_TLS_GD_LO16:
1420 case R_OR1K_TLS_IE_HI16:
1421 case R_OR1K_TLS_IE_LO16:
1422 if (htab->root.sgot == NULL)
1425 htab->root.dynobj = dynobj = abfd;
1426 if (!_bfd_elf_create_got_section (dynobj, info))
1430 if (ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_HI16 &&
1431 ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_LO16)
1434 h->got.refcount += 1;
1437 bfd_signed_vma *local_got_refcounts;
1439 /* This is a global offset table entry for a local symbol. */
1440 local_got_refcounts = elf_local_got_refcounts (abfd);
1441 if (local_got_refcounts == NULL)
1445 size = symtab_hdr->sh_info;
1446 size *= sizeof (bfd_signed_vma);
1447 local_got_refcounts = bfd_zalloc (abfd, size);
1448 if (local_got_refcounts == NULL)
1450 elf_local_got_refcounts (abfd) = local_got_refcounts;
1452 local_got_refcounts[r_symndx] += 1;
1457 case R_OR1K_INSN_REL_26:
1458 case R_OR1K_HI_16_IN_INSN:
1459 case R_OR1K_LO_16_IN_INSN:
1463 if (h != NULL && !bfd_link_pic (info))
1465 /* We may need a copy reloc. */
1468 /* We may also need a .plt entry. */
1469 h->plt.refcount += 1;
1470 if (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26)
1471 h->pointer_equality_needed = 1;
1474 /* If we are creating a shared library, and this is a reloc
1475 against a global symbol, or a non PC relative reloc
1476 against a local symbol, then we need to copy the reloc
1477 into the shared library. However, if we are linking with
1478 -Bsymbolic, we do not need to copy a reloc against a
1479 global symbol which is defined in an object we are
1480 including in the link (i.e., DEF_REGULAR is set). At
1481 this point we have not seen all the input files, so it is
1482 possible that DEF_REGULAR is not set now but will be set
1483 later (it is never cleared). In case of a weak definition,
1484 DEF_REGULAR may be cleared later by a strong definition in
1485 a shared library. We account for that possibility below by
1486 storing information in the relocs_copied field of the hash
1487 table entry. A similar situation occurs when creating
1488 shared libraries and symbol visibility changes render the
1491 If on the other hand, we are creating an executable, we
1492 may need to keep relocations for symbols satisfied by a
1493 dynamic library if we manage to avoid copy relocs for the
1496 if ((bfd_link_pic (info)
1497 && (sec->flags & SEC_ALLOC) != 0
1498 && (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26
1500 && (!SYMBOLIC_BIND (info, h)
1501 || h->root.type == bfd_link_hash_defweak
1502 || !h->def_regular))))
1503 || (!bfd_link_pic (info)
1504 && (sec->flags & SEC_ALLOC) != 0
1506 && (h->root.type == bfd_link_hash_defweak
1507 || !h->def_regular)))
1509 struct elf_dyn_relocs *p;
1510 struct elf_dyn_relocs **head;
1512 /* When creating a shared object, we must copy these
1513 relocs into the output file. We create a reloc
1514 section in dynobj and make room for the reloc. */
1518 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1519 unsigned int shnam = _bfd_elf_single_rel_hdr (sec)->sh_name;
1521 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1525 if (strncmp (name, ".rela", 5) != 0
1526 || strcmp (bfd_get_section_name (abfd, sec),
1530 /* xgettext:c-format */
1531 (_("%pB: bad relocation section name `%s\'"),
1535 if (htab->root.dynobj == NULL)
1536 htab->root.dynobj = abfd;
1537 dynobj = htab->root.dynobj;
1539 sreloc = bfd_get_section_by_name (dynobj, name);
1542 sreloc = _bfd_elf_make_dynamic_reloc_section
1543 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
1548 elf_section_data (sec)->sreloc = sreloc;
1551 /* If this is a global symbol, we count the number of
1552 relocations we need for this symbol. */
1554 head = &((struct elf_or1k_link_hash_entry *) h)->dyn_relocs;
1557 /* Track dynamic relocs needed for local syms too.
1558 We really need local syms available to do this
1562 Elf_Internal_Sym *isym;
1565 isym = bfd_sym_from_r_symndx (&htab->sym_sec,
1570 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1574 vpp = &elf_section_data (s)->local_dynrel;
1575 head = (struct elf_dyn_relocs **) vpp;
1579 if (p == NULL || p->sec != sec)
1581 bfd_size_type amt = sizeof *p;
1582 p = ((struct elf_dyn_relocs *)
1583 bfd_alloc (htab->root.dynobj, amt));
1594 if (ELF32_R_TYPE (rel->r_info) == R_OR1K_INSN_REL_26)
1605 /* Finish up the dynamic sections. */
1608 or1k_elf_finish_dynamic_sections (bfd *output_bfd,
1609 struct bfd_link_info *info)
1612 asection *sdyn, *sgot;
1613 struct elf_or1k_link_hash_table *htab;
1615 htab = or1k_elf_hash_table (info);
1619 dynobj = htab->root.dynobj;
1621 sgot = htab->root.sgotplt;
1622 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1624 if (htab->root.dynamic_sections_created)
1627 Elf32_External_Dyn *dyncon, *dynconend;
1629 BFD_ASSERT (sgot != NULL && sdyn != NULL);
1631 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1632 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1634 for (; dyncon < dynconend; dyncon++)
1636 Elf_Internal_Dyn dyn;
1639 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1647 s = htab->root.sgotplt;
1648 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1652 s = htab->root.srelplt;
1653 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1657 s = htab->root.srelplt;
1658 dyn.d_un.d_val = s->size;
1661 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1665 /* Fill in the first entry in the procedure linkage table. */
1666 splt = htab->root.splt;
1667 if (splt && splt->size > 0)
1669 if (bfd_link_pic (info))
1671 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0,
1673 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1,
1674 splt->contents + 4);
1675 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2,
1676 splt->contents + 8);
1677 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3,
1678 splt->contents + 12);
1679 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4,
1680 splt->contents + 16);
1685 /* addr = .got + 4 */
1686 addr = sgot->output_section->vma + sgot->output_offset + 4;
1687 bfd_put_32 (output_bfd,
1688 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
1690 bfd_put_32 (output_bfd,
1691 PLT0_ENTRY_WORD1 | (addr & 0xffff),
1692 splt->contents + 4);
1693 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
1694 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
1695 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
1698 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
1702 /* Set the first entry in the global offset table to the address of
1703 the dynamic section. */
1704 if (sgot && sgot->size > 0)
1707 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1709 bfd_put_32 (output_bfd,
1710 sdyn->output_section->vma + sdyn->output_offset,
1712 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1715 if (htab->root.sgot && htab->root.sgot->size > 0)
1716 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize = 4;
1721 /* Finish up dynamic symbol handling. We set the contents of various
1722 dynamic sections here. */
1725 or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
1726 struct bfd_link_info *info,
1727 struct elf_link_hash_entry *h,
1728 Elf_Internal_Sym *sym)
1730 struct elf_or1k_link_hash_table *htab;
1733 htab = or1k_elf_hash_table (info);
1737 if (h->plt.offset != (bfd_vma) -1)
1746 Elf_Internal_Rela rela;
1748 /* This symbol has an entry in the procedure linkage table. Set
1750 BFD_ASSERT (h->dynindx != -1);
1752 splt = htab->root.splt;
1753 sgot = htab->root.sgotplt;
1754 srela = htab->root.srelplt;
1755 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1757 /* Get the index in the procedure linkage table which
1758 corresponds to this symbol. This is the index of this symbol
1759 in all the symbols for which we are making plt entries. The
1760 first entry in the procedure linkage table is reserved. */
1761 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1763 /* Get the offset into the .got table of the entry that
1764 corresponds to this function. Each .got entry is 4 bytes.
1765 The first three are reserved. */
1766 got_offset = (plt_index + 3) * 4;
1767 got_addr = got_offset;
1769 /* Fill in the entry in the procedure linkage table. */
1770 if (! bfd_link_pic (info))
1772 got_addr += htab->root.sgotplt->output_section->vma
1773 + htab->root.sgotplt->output_offset;
1774 bfd_put_32 (output_bfd, PLT_ENTRY_WORD0 | ((got_addr >> 16) & 0xffff),
1775 splt->contents + h->plt.offset);
1776 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1 | (got_addr & 0xffff),
1777 splt->contents + h->plt.offset + 4);
1778 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
1779 splt->contents + h->plt.offset + 8);
1780 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
1781 splt->contents + h->plt.offset + 12);
1782 bfd_put_32 (output_bfd, PLT_ENTRY_WORD4
1783 | plt_index * sizeof (Elf32_External_Rela),
1784 splt->contents + h->plt.offset + 16);
1788 bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD0 | (got_addr & 0xffff),
1789 splt->contents + h->plt.offset);
1790 bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD1
1791 | plt_index * sizeof (Elf32_External_Rela),
1792 splt->contents + h->plt.offset + 4);
1793 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
1794 splt->contents + h->plt.offset + 8);
1795 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
1796 splt->contents + h->plt.offset + 12);
1797 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
1798 splt->contents + h->plt.offset + 16);
1801 /* Fill in the entry in the global offset table. */
1802 bfd_put_32 (output_bfd,
1803 (splt->output_section->vma
1804 + splt->output_offset), /* Same offset. */
1805 sgot->contents + got_offset);
1807 /* Fill in the entry in the .rela.plt section. */
1808 rela.r_offset = (sgot->output_section->vma
1809 + sgot->output_offset
1811 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_JMP_SLOT);
1813 loc = srela->contents;
1814 loc += plt_index * sizeof (Elf32_External_Rela);
1815 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1817 if (!h->def_regular)
1819 /* Mark the symbol as undefined, rather than as defined in
1820 the .plt section. Leave the value alone. */
1821 sym->st_shndx = SHN_UNDEF;
1826 if (h->got.offset != (bfd_vma) -1
1827 && (h->got.offset & 2) == 0) /* Homemade TLS check. */
1831 Elf_Internal_Rela rela;
1833 /* This symbol has an entry in the global offset table. Set it
1835 sgot = htab->root.sgot;
1836 srela = htab->root.srelgot;
1837 BFD_ASSERT (sgot != NULL && srela != NULL);
1839 rela.r_offset = (sgot->output_section->vma
1840 + sgot->output_offset
1841 + (h->got.offset &~ 1));
1843 /* If this is a -Bsymbolic link, and the symbol is defined
1844 locally, we just want to emit a RELATIVE reloc. Likewise if
1845 the symbol was forced to be local because of a version file.
1846 The entry in the global offset table will already have been
1847 initialized in the relocate_section function. */
1848 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
1850 rela.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1851 rela.r_addend = (h->root.u.def.value
1852 + h->root.u.def.section->output_section->vma
1853 + h->root.u.def.section->output_offset);
1857 BFD_ASSERT ((h->got.offset & 1) == 0);
1858 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1859 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_GLOB_DAT);
1863 loc = srela->contents;
1864 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
1865 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1866 ++srela->reloc_count;
1872 Elf_Internal_Rela rela;
1874 /* This symbols needs a copy reloc. Set it up. */
1875 BFD_ASSERT (h->dynindx != -1
1876 && (h->root.type == bfd_link_hash_defined
1877 || h->root.type == bfd_link_hash_defweak));
1879 rela.r_offset = (h->root.u.def.value
1880 + h->root.u.def.section->output_section->vma
1881 + h->root.u.def.section->output_offset);
1882 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_COPY);
1884 if (h->root.u.def.section == htab->root.sdynrelro)
1885 s = htab->root.sreldynrelro;
1887 s = htab->root.srelbss;
1888 loc = s->contents + s->reloc_count * sizeof (Elf32_External_Rela);
1889 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1893 /* Mark some specially defined symbols as absolute. */
1894 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1895 || h == htab->root.hgot)
1896 sym->st_shndx = SHN_ABS;
1901 static enum elf_reloc_type_class
1902 or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
1903 const asection *rel_sec ATTRIBUTE_UNUSED,
1904 const Elf_Internal_Rela *rela)
1906 switch ((int) ELF32_R_TYPE (rela->r_info))
1908 case R_OR1K_RELATIVE: return reloc_class_relative;
1909 case R_OR1K_JMP_SLOT: return reloc_class_plt;
1910 case R_OR1K_COPY: return reloc_class_copy;
1911 default: return reloc_class_normal;
1915 /* Find dynamic relocs for H that apply to read-only sections. */
1918 readonly_dynrelocs (struct elf_link_hash_entry *h)
1920 struct elf_dyn_relocs *p;
1921 struct elf_or1k_link_hash_entry *eh = (struct elf_or1k_link_hash_entry *) h;
1923 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1925 asection *s = p->sec->output_section;
1927 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1933 /* Adjust a symbol defined by a dynamic object and referenced by a
1934 regular object. The current definition is in some section of the
1935 dynamic object, but we're not including those sections. We have to
1936 change the definition to something the rest of the link can
1940 or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1941 struct elf_link_hash_entry *h)
1943 struct elf_or1k_link_hash_table *htab;
1947 dynobj = elf_hash_table (info)->dynobj;
1949 /* Make sure we know what is going on here. */
1950 BFD_ASSERT (dynobj != NULL
1955 && !h->def_regular)));
1957 /* If this is a function, put it in the procedure linkage table. We
1958 will fill in the contents of the procedure linkage table later,
1959 when we know the address of the .got section. */
1960 if (h->type == STT_FUNC
1963 if (! bfd_link_pic (info)
1966 && h->root.type != bfd_link_hash_undefweak
1967 && h->root.type != bfd_link_hash_undefined)
1969 /* This case can occur if we saw a PLT reloc in an input
1970 file, but the symbol was never referred to by a dynamic
1971 object. In such a case, we don't actually need to build
1972 a procedure linkage table, and we can just do a PCREL
1974 h->plt.offset = (bfd_vma) -1;
1981 h->plt.offset = (bfd_vma) -1;
1983 /* If this is a weak symbol, and there is a real definition, the
1984 processor independent code will have arranged for us to see the
1985 real definition first, and we can just use the same value. */
1986 if (h->is_weakalias)
1988 struct elf_link_hash_entry *def = weakdef (h);
1989 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1990 h->root.u.def.section = def->root.u.def.section;
1991 h->root.u.def.value = def->root.u.def.value;
1995 /* This is a reference to a symbol defined by a dynamic object which
1996 is not a function. */
1998 /* If we are creating a shared library, we must presume that the
1999 only references to the symbol are via the global offset table.
2000 For such cases we need not do anything here; the relocations will
2001 be handled correctly by relocate_section. */
2002 if (bfd_link_pic (info))
2005 /* If there are no references to this symbol that do not use the
2006 GOT, we don't need to generate a copy reloc. */
2007 if (!h->non_got_ref)
2010 /* If -z nocopyreloc was given, we won't generate them either. */
2011 if (info->nocopyreloc)
2017 /* If we don't find any dynamic relocs in read-only sections, then
2018 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2019 if (!readonly_dynrelocs (h))
2025 /* We must allocate the symbol in our .dynbss section, which will
2026 become part of the .bss section of the executable. There will be
2027 an entry for this symbol in the .dynsym section. The dynamic
2028 object will contain position independent code, so all references
2029 from the dynamic object to this symbol will go through the global
2030 offset table. The dynamic linker will use the .dynsym entry to
2031 determine the address it must put in the global offset table, so
2032 both the dynamic object and the regular object will refer to the
2033 same memory location for the variable. */
2035 htab = or1k_elf_hash_table (info);
2039 /* We must generate a R_OR1K_COPY reloc to tell the dynamic linker
2040 to copy the initial value out of the dynamic object and into the
2041 runtime process image. We need to remember the offset into the
2042 .rela.bss section we are going to use. */
2043 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2045 s = htab->root.sdynrelro;
2046 srel = htab->root.sreldynrelro;
2050 s = htab->root.sdynbss;
2051 srel = htab->root.srelbss;
2053 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2055 srel->size += sizeof (Elf32_External_Rela);
2059 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2062 /* Allocate space in .plt, .got and associated reloc sections for
2066 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2068 struct bfd_link_info *info;
2069 struct elf_or1k_link_hash_table *htab;
2070 struct elf_or1k_link_hash_entry *eh;
2071 struct elf_dyn_relocs *p;
2073 if (h->root.type == bfd_link_hash_indirect)
2076 info = (struct bfd_link_info *) inf;
2077 htab = or1k_elf_hash_table (info);
2081 eh = (struct elf_or1k_link_hash_entry *) h;
2083 if (htab->root.dynamic_sections_created
2084 && h->plt.refcount > 0)
2086 /* Make sure this symbol is output as a dynamic symbol.
2087 Undefined weak syms won't yet be marked as dynamic. */
2088 if (h->dynindx == -1
2089 && !h->forced_local)
2091 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2095 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
2097 asection *s = htab->root.splt;
2099 /* If this is the first .plt entry, make room for the special
2102 s->size = PLT_ENTRY_SIZE;
2104 h->plt.offset = s->size;
2106 /* If this symbol is not defined in a regular file, and we are
2107 not generating a shared library, then set the symbol to this
2108 location in the .plt. This is required to make function
2109 pointers compare as equal between the normal executable and
2110 the shared library. */
2111 if (! bfd_link_pic (info)
2114 h->root.u.def.section = s;
2115 h->root.u.def.value = h->plt.offset;
2118 /* Make room for this entry. */
2119 s->size += PLT_ENTRY_SIZE;
2121 /* We also need to make an entry in the .got.plt section, which
2122 will be placed in the .got section by the linker script. */
2123 htab->root.sgotplt->size += 4;
2125 /* We also need to make an entry in the .rel.plt section. */
2126 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
2130 h->plt.offset = (bfd_vma) -1;
2136 h->plt.offset = (bfd_vma) -1;
2140 if (h->got.refcount > 0)
2144 unsigned char tls_type;
2146 /* Make sure this symbol is output as a dynamic symbol.
2147 Undefined weak syms won't yet be marked as dynamic. */
2148 if (h->dynindx == -1
2149 && !h->forced_local)
2151 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2155 s = htab->root.sgot;
2157 h->got.offset = s->size;
2159 tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
2161 /* TLS GD requires two GOT and two relocs. */
2162 if (tls_type == TLS_GD)
2166 dyn = htab->root.dynamic_sections_created;
2167 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
2169 if (tls_type == TLS_GD)
2170 htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2172 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2176 h->got.offset = (bfd_vma) -1;
2178 if (eh->dyn_relocs == NULL)
2181 /* In the shared -Bsymbolic case, discard space allocated for
2182 dynamic pc-relative relocs against symbols which turn out to be
2183 defined in regular objects. For the normal shared case, discard
2184 space for pc-relative relocs that have become local due to symbol
2185 visibility changes. */
2187 if (bfd_link_pic (info))
2189 if (SYMBOL_CALLS_LOCAL (info, h))
2191 struct elf_dyn_relocs **pp;
2193 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
2195 p->count -= p->pc_count;
2204 /* Also discard relocs on undefined weak syms with non-default
2206 if (eh->dyn_relocs != NULL
2207 && h->root.type == bfd_link_hash_undefweak)
2209 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2210 eh->dyn_relocs = NULL;
2212 /* Make sure undefined weak symbols are output as a dynamic
2214 else if (h->dynindx == -1
2215 && !h->forced_local)
2217 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2224 /* For the non-shared case, discard space for relocs against
2225 symbols which turn out to need copy relocs or are not
2231 || (htab->root.dynamic_sections_created
2232 && (h->root.type == bfd_link_hash_undefweak
2233 || h->root.type == bfd_link_hash_undefined))))
2235 /* Make sure this symbol is output as a dynamic symbol.
2236 Undefined weak syms won't yet be marked as dynamic. */
2237 if (h->dynindx == -1
2238 && !h->forced_local)
2240 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2244 /* If that succeeded, we know we'll be keeping all the
2246 if (h->dynindx != -1)
2250 eh->dyn_relocs = NULL;
2255 /* Finally, allocate space. */
2256 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2258 asection *sreloc = elf_section_data (p->sec)->sreloc;
2259 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2265 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2266 read-only sections. */
2269 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2273 if (h->root.type == bfd_link_hash_indirect)
2276 sec = readonly_dynrelocs (h);
2279 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2281 info->flags |= DF_TEXTREL;
2282 info->callbacks->minfo
2283 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2284 sec->owner, h->root.root.string, sec);
2286 /* Not an error, just cut short the traversal. */
2292 /* Set the sizes of the dynamic sections. */
2295 or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2296 struct bfd_link_info *info)
2298 struct elf_or1k_link_hash_table *htab;
2304 htab = or1k_elf_hash_table (info);
2308 dynobj = htab->root.dynobj;
2309 BFD_ASSERT (dynobj != NULL);
2311 if (htab->root.dynamic_sections_created)
2313 /* Set the contents of the .interp section to the interpreter. */
2314 if (bfd_link_executable (info) && !info->nointerp)
2316 s = bfd_get_section_by_name (dynobj, ".interp");
2317 BFD_ASSERT (s != NULL);
2318 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2319 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2323 /* Set up .got offsets for local syms, and space for local dynamic
2325 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2327 bfd_signed_vma *local_got;
2328 bfd_signed_vma *end_local_got;
2329 bfd_size_type locsymcount;
2330 Elf_Internal_Shdr *symtab_hdr;
2331 unsigned char *local_tls_type;
2334 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2337 for (s = ibfd->sections; s != NULL; s = s->next)
2339 struct elf_dyn_relocs *p;
2341 for (p = ((struct elf_dyn_relocs *)
2342 elf_section_data (s)->local_dynrel);
2346 if (! bfd_is_abs_section (p->sec)
2347 && bfd_is_abs_section (p->sec->output_section))
2349 /* Input section has been discarded, either because
2350 it is a copy of a linkonce section or due to
2351 linker script /DISCARD/, so we'll be discarding
2354 else if (p->count != 0)
2356 srel = elf_section_data (p->sec)->sreloc;
2357 srel->size += p->count * sizeof (Elf32_External_Rela);
2358 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2359 info->flags |= DF_TEXTREL;
2364 local_got = elf_local_got_refcounts (ibfd);
2368 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2369 locsymcount = symtab_hdr->sh_info;
2370 end_local_got = local_got + locsymcount;
2371 s = htab->root.sgot;
2372 srel = htab->root.srelgot;
2373 local_tls_type = (unsigned char *) elf_or1k_local_tls_type (ibfd);
2374 for (; local_got < end_local_got; ++local_got)
2378 *local_got = s->size;
2380 /* TLS GD requires two GOT and two relocs. */
2381 if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2385 if (bfd_link_pic (info))
2387 if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2388 srel->size += 2 * sizeof (Elf32_External_Rela);
2390 srel->size += sizeof (Elf32_External_Rela);
2395 *local_got = (bfd_vma) -1;
2402 /* Allocate global sym .plt and .got entries, and space for global
2403 sym dynamic relocs. */
2404 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
2406 /* We now have determined the sizes of the various dynamic sections.
2407 Allocate memory for them. */
2409 for (s = dynobj->sections; s != NULL; s = s->next)
2411 if ((s->flags & SEC_LINKER_CREATED) == 0)
2414 if (s == htab->root.splt
2415 || s == htab->root.sgot
2416 || s == htab->root.sgotplt
2417 || s == htab->root.sdynbss
2418 || s == htab->root.sdynrelro)
2420 /* Strip this section if we don't need it; see the
2423 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2425 if (s->size != 0 && s != htab->root.srelplt)
2428 /* We use the reloc_count field as a counter if we need
2429 to copy relocs into the output file. */
2433 /* It's not one of our sections, so don't allocate space. */
2438 /* If we don't need this section, strip it from the
2439 output file. This is mostly to handle .rela.bss and
2440 .rela.plt. We must create both sections in
2441 create_dynamic_sections, because they must be created
2442 before the linker maps input sections to output
2443 sections. The linker does that before
2444 adjust_dynamic_symbol is called, and it is that
2445 function which decides whether anything needs to go
2446 into these sections. */
2447 s->flags |= SEC_EXCLUDE;
2451 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2454 /* Allocate memory for the section contents. We use bfd_zalloc
2455 here in case unused entries are not reclaimed before the
2456 section's contents are written out. This should not happen,
2457 but this way if it does, we get a R_OR1K_NONE reloc instead
2459 s->contents = bfd_zalloc (dynobj, s->size);
2461 if (s->contents == NULL)
2465 if (htab->root.dynamic_sections_created)
2467 /* Add some entries to the .dynamic section. We fill in the
2468 values later, in or1k_elf_finish_dynamic_sections, but we
2469 must add the entries now so that we get the correct size for
2470 the .dynamic section. The DT_DEBUG entry is filled in by the
2471 dynamic linker and used by the debugger. */
2472 #define add_dynamic_entry(TAG, VAL) \
2473 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2475 if (bfd_link_executable (info))
2477 if (! add_dynamic_entry (DT_DEBUG, 0))
2481 if (htab->root.splt->size != 0)
2483 if (! add_dynamic_entry (DT_PLTGOT, 0)
2484 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2485 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2486 || ! add_dynamic_entry (DT_JMPREL, 0))
2492 if (! add_dynamic_entry (DT_RELA, 0)
2493 || ! add_dynamic_entry (DT_RELASZ, 0)
2494 || ! add_dynamic_entry (DT_RELAENT,
2495 sizeof (Elf32_External_Rela)))
2498 /* If any dynamic relocs apply to a read-only section,
2499 then we need a DT_TEXTREL entry. */
2500 if ((info->flags & DF_TEXTREL) == 0)
2501 elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
2503 if ((info->flags & DF_TEXTREL) != 0)
2505 if (! add_dynamic_entry (DT_TEXTREL, 0))
2511 #undef add_dynamic_entry
2515 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2518 or1k_elf_copy_indirect_symbol (struct bfd_link_info *info,
2519 struct elf_link_hash_entry *dir,
2520 struct elf_link_hash_entry *ind)
2522 struct elf_or1k_link_hash_entry * edir;
2523 struct elf_or1k_link_hash_entry * eind;
2525 edir = (struct elf_or1k_link_hash_entry *) dir;
2526 eind = (struct elf_or1k_link_hash_entry *) ind;
2528 if (eind->dyn_relocs != NULL)
2530 if (edir->dyn_relocs != NULL)
2532 struct elf_dyn_relocs **pp;
2533 struct elf_dyn_relocs *p;
2535 /* Add reloc counts against the indirect sym to the direct sym
2536 list. Merge any entries against the same section. */
2537 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2539 struct elf_dyn_relocs *q;
2541 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2542 if (q->sec == p->sec)
2544 q->pc_count += p->pc_count;
2545 q->count += p->count;
2552 *pp = edir->dyn_relocs;
2555 edir->dyn_relocs = eind->dyn_relocs;
2556 eind->dyn_relocs = NULL;
2559 if (ind->root.type == bfd_link_hash_indirect)
2561 if (dir->got.refcount <= 0)
2563 edir->tls_type = eind->tls_type;
2564 eind->tls_type = TLS_UNKNOWN;
2568 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2571 /* Set the right machine number. */
2574 or1k_elf_object_p (bfd *abfd)
2576 unsigned long mach = bfd_mach_or1k;
2578 if (elf_elfheader (abfd)->e_flags & EF_OR1K_NODELAY)
2579 mach = bfd_mach_or1knd;
2581 return bfd_default_set_arch_mach (abfd, bfd_arch_or1k, mach);
2584 /* Store the machine number in the flags field. */
2587 or1k_elf_final_write_processing (bfd *abfd,
2588 bfd_boolean linker ATTRIBUTE_UNUSED)
2590 switch (bfd_get_mach (abfd))
2595 case bfd_mach_or1knd:
2596 elf_elfheader (abfd)->e_flags |= EF_OR1K_NODELAY;
2602 or1k_elf_set_private_flags (bfd *abfd, flagword flags)
2604 BFD_ASSERT (!elf_flags_init (abfd)
2605 || elf_elfheader (abfd)->e_flags == flags);
2607 elf_elfheader (abfd)->e_flags = flags;
2608 elf_flags_init (abfd) = TRUE;
2612 /* Make sure all input files are consistent with respect to
2613 EF_OR1K_NODELAY flag setting. */
2616 elf32_or1k_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2618 bfd *obfd = info->output_bfd;
2622 in_flags = elf_elfheader (ibfd)->e_flags;
2623 out_flags = elf_elfheader (obfd)->e_flags;
2625 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2626 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2629 if (!elf_flags_init (obfd))
2631 elf_flags_init (obfd) = TRUE;
2632 elf_elfheader (obfd)->e_flags = in_flags;
2637 if (in_flags == out_flags)
2640 if ((in_flags & EF_OR1K_NODELAY) != (out_flags & EF_OR1K_NODELAY))
2643 (_("%pB: %s flag mismatch with previous modules"),
2644 ibfd, "EF_OR1K_NODELAY");
2646 bfd_set_error (bfd_error_bad_value);
2654 #define ELF_ARCH bfd_arch_or1k
2655 #define ELF_MACHINE_CODE EM_OR1K
2656 #define ELF_TARGET_ID OR1K_ELF_DATA
2657 #define ELF_MAXPAGESIZE 0x2000
2659 #define TARGET_BIG_SYM or1k_elf32_vec
2660 #define TARGET_BIG_NAME "elf32-or1k"
2662 #define elf_info_to_howto_rel NULL
2663 #define elf_info_to_howto or1k_info_to_howto_rela
2664 #define elf_backend_relocate_section or1k_elf_relocate_section
2665 #define elf_backend_gc_mark_hook or1k_elf_gc_mark_hook
2666 #define elf_backend_check_relocs or1k_elf_check_relocs
2667 #define elf_backend_reloc_type_class or1k_elf_reloc_type_class
2668 #define elf_backend_can_gc_sections 1
2669 #define elf_backend_rela_normal 1
2671 #define bfd_elf32_mkobject elf_or1k_mkobject
2673 #define bfd_elf32_bfd_merge_private_bfd_data elf32_or1k_merge_private_bfd_data
2674 #define bfd_elf32_bfd_set_private_flags or1k_elf_set_private_flags
2675 #define bfd_elf32_bfd_reloc_type_lookup or1k_reloc_type_lookup
2676 #define bfd_elf32_bfd_reloc_name_lookup or1k_reloc_name_lookup
2678 #define elf_backend_object_p or1k_elf_object_p
2679 #define elf_backend_final_write_processing or1k_elf_final_write_processing
2680 #define elf_backend_can_refcount 1
2682 #define elf_backend_plt_readonly 1
2683 #define elf_backend_want_got_plt 1
2684 #define elf_backend_want_plt_sym 0
2685 #define elf_backend_got_header_size 12
2686 #define elf_backend_dtrel_excludes_plt 1
2687 #define elf_backend_want_dynrelro 1
2689 #define bfd_elf32_bfd_link_hash_table_create or1k_elf_link_hash_table_create
2690 #define elf_backend_copy_indirect_symbol or1k_elf_copy_indirect_symbol
2691 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2692 #define elf_backend_finish_dynamic_sections or1k_elf_finish_dynamic_sections
2693 #define elf_backend_size_dynamic_sections or1k_elf_size_dynamic_sections
2694 #define elf_backend_adjust_dynamic_symbol or1k_elf_adjust_dynamic_symbol
2695 #define elf_backend_finish_dynamic_symbol or1k_elf_finish_dynamic_symbol
2697 #include "elf32-target.h"