1 /* CRIS-specific support for 32-bit ELF.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Contributed by Axis Communications AB.
4 Written by Hans-Peter Nilsson, based on elf32-fr30.c
5 PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
32 cris_elf_pcrel_reloc (bfd *, arelent *, asymbol *, void *,
33 asection *, bfd *, char **);
35 cris_elf_set_mach_from_flags (bfd *, unsigned long);
37 /* Forward declarations. */
38 static reloc_howto_type cris_elf_howto_table [] =
40 /* This reloc does nothing. */
41 HOWTO (R_CRIS_NONE, /* type */
43 2, /* size (0 = byte, 1 = short, 2 = long) */
45 FALSE, /* pc_relative */
47 complain_overflow_bitfield, /* complain_on_overflow */
48 bfd_elf_generic_reloc, /* special_function */
49 "R_CRIS_NONE", /* name */
50 FALSE, /* partial_inplace */
53 FALSE), /* pcrel_offset */
55 /* An 8 bit absolute relocation. */
56 HOWTO (R_CRIS_8, /* type */
58 0, /* size (0 = byte, 1 = short, 2 = long) */
60 FALSE, /* pc_relative */
62 complain_overflow_bitfield, /* complain_on_overflow */
63 bfd_elf_generic_reloc, /* special_function */
64 "R_CRIS_8", /* name */
65 FALSE, /* partial_inplace */
66 0x0000, /* src_mask */
67 0x00ff, /* dst_mask */
68 FALSE), /* pcrel_offset */
70 /* A 16 bit absolute relocation. */
71 HOWTO (R_CRIS_16, /* type */
73 1, /* size (0 = byte, 1 = short, 2 = long) */
75 FALSE, /* pc_relative */
77 complain_overflow_bitfield, /* complain_on_overflow */
78 bfd_elf_generic_reloc, /* special_function */
79 "R_CRIS_16", /* name */
80 FALSE, /* partial_inplace */
81 0x00000000, /* src_mask */
82 0x0000ffff, /* dst_mask */
83 FALSE), /* pcrel_offset */
85 /* A 32 bit absolute relocation. */
86 HOWTO (R_CRIS_32, /* type */
88 2, /* size (0 = byte, 1 = short, 2 = long) */
90 FALSE, /* pc_relative */
92 /* We don't want overflow complaints for 64-bit vma builds
93 for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
94 32-bit ELF) where sym=0xc0001234.
95 Don't do this for the PIC relocs, as we don't expect to
96 see them with large offsets. */
97 complain_overflow_dont, /* complain_on_overflow */
98 bfd_elf_generic_reloc, /* special_function */
99 "R_CRIS_32", /* name */
100 FALSE, /* partial_inplace */
101 0x00000000, /* src_mask */
102 0xffffffff, /* dst_mask */
103 FALSE), /* pcrel_offset */
105 /* An 8 bit PC-relative relocation. */
106 HOWTO (R_CRIS_8_PCREL, /* type */
108 0, /* size (0 = byte, 1 = short, 2 = long) */
110 TRUE, /* pc_relative */
112 complain_overflow_bitfield, /* complain_on_overflow */
113 cris_elf_pcrel_reloc, /* special_function */
114 "R_CRIS_8_PCREL", /* name */
115 FALSE, /* partial_inplace */
116 0x0000, /* src_mask */
117 0x00ff, /* dst_mask */
118 TRUE), /* pcrel_offset */
120 /* A 16 bit PC-relative relocation. */
121 HOWTO (R_CRIS_16_PCREL, /* type */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
125 TRUE, /* pc_relative */
127 complain_overflow_bitfield, /* complain_on_overflow */
128 cris_elf_pcrel_reloc, /* special_function */
129 "R_CRIS_16_PCREL", /* name */
130 FALSE, /* partial_inplace */
131 0x00000000, /* src_mask */
132 0x0000ffff, /* dst_mask */
133 TRUE), /* pcrel_offset */
135 /* A 32 bit PC-relative relocation. */
136 HOWTO (R_CRIS_32_PCREL, /* type */
138 2, /* size (0 = byte, 1 = short, 2 = long) */
140 TRUE, /* pc_relative */
142 complain_overflow_bitfield, /* complain_on_overflow */
143 cris_elf_pcrel_reloc, /* special_function */
144 "R_CRIS_32_PCREL", /* name */
145 FALSE, /* partial_inplace */
146 0x00000000, /* src_mask */
147 0xffffffff, /* dst_mask */
148 TRUE), /* pcrel_offset */
150 /* GNU extension to record C++ vtable hierarchy. */
151 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
155 FALSE, /* pc_relative */
157 complain_overflow_dont, /* complain_on_overflow */
158 NULL, /* special_function */
159 "R_CRIS_GNU_VTINHERIT", /* name */
160 FALSE, /* partial_inplace */
163 FALSE), /* pcrel_offset */
165 /* GNU extension to record C++ vtable member usage. */
166 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
170 FALSE, /* pc_relative */
172 complain_overflow_dont, /* complain_on_overflow */
173 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
174 "R_CRIS_GNU_VTENTRY", /* name */
175 FALSE, /* partial_inplace */
178 FALSE), /* pcrel_offset */
180 /* This is used only by the dynamic linker. The symbol should exist
181 both in the object being run and in some shared library. The
182 dynamic linker copies the data addressed by the symbol from the
183 shared library into the object, because the object being
184 run has to have the data at some particular address. */
185 HOWTO (R_CRIS_COPY, /* type */
187 2, /* size (0 = byte, 1 = short, 2 = long) */
189 FALSE, /* pc_relative */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 bfd_elf_generic_reloc, /* special_function */
193 "R_CRIS_COPY", /* name */
194 FALSE, /* partial_inplace */
197 FALSE), /* pcrel_offset */
199 /* Like R_CRIS_32, but used when setting global offset table entries. */
200 HOWTO (R_CRIS_GLOB_DAT, /* type */
202 2, /* size (0 = byte, 1 = short, 2 = long) */
204 FALSE, /* pc_relative */
206 complain_overflow_bitfield, /* complain_on_overflow */
207 bfd_elf_generic_reloc, /* special_function */
208 "R_CRIS_GLOB_DAT", /* name */
209 FALSE, /* partial_inplace */
211 0xffffffff, /* dst_mask */
212 FALSE), /* pcrel_offset */
214 /* Marks a procedure linkage table entry for a symbol. */
215 HOWTO (R_CRIS_JUMP_SLOT, /* type */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
219 FALSE, /* pc_relative */
221 complain_overflow_bitfield, /* complain_on_overflow */
222 bfd_elf_generic_reloc, /* special_function */
223 "R_CRIS_JUMP_SLOT", /* name */
224 FALSE, /* partial_inplace */
227 FALSE), /* pcrel_offset */
229 /* Used only by the dynamic linker. When the object is run, this
230 longword is set to the load address of the object, plus the
232 HOWTO (R_CRIS_RELATIVE, /* type */
234 2, /* size (0 = byte, 1 = short, 2 = long) */
236 FALSE, /* pc_relative */
238 complain_overflow_bitfield, /* complain_on_overflow */
239 bfd_elf_generic_reloc, /* special_function */
240 "R_CRIS_RELATIVE", /* name */
241 FALSE, /* partial_inplace */
243 0xffffffff, /* dst_mask */
244 FALSE), /* pcrel_offset */
246 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
247 HOWTO (R_CRIS_16_GOT, /* type */
249 1, /* size (0 = byte, 1 = short, 2 = long) */
251 FALSE, /* pc_relative */
253 complain_overflow_bitfield, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_CRIS_16_GOT", /* name */
256 FALSE, /* partial_inplace */
258 0xffff, /* dst_mask */
259 FALSE), /* pcrel_offset */
261 HOWTO (R_CRIS_32_GOT, /* type */
263 2, /* size (0 = byte, 1 = short, 2 = long) */
265 FALSE, /* pc_relative */
267 complain_overflow_bitfield, /* complain_on_overflow */
268 bfd_elf_generic_reloc, /* special_function */
269 "R_CRIS_32_GOT", /* name */
270 FALSE, /* partial_inplace */
272 0xffffffff, /* dst_mask */
273 FALSE), /* pcrel_offset */
275 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
276 the GOT table for the symbol. */
277 HOWTO (R_CRIS_16_GOTPLT, /* type */
279 1, /* size (0 = byte, 1 = short, 2 = long) */
281 FALSE, /* pc_relative */
283 complain_overflow_bitfield, /* complain_on_overflow */
284 bfd_elf_generic_reloc, /* special_function */
285 "R_CRIS_16_GOTPLT", /* name */
286 FALSE, /* partial_inplace */
288 0xffff, /* dst_mask */
289 FALSE), /* pcrel_offset */
291 HOWTO (R_CRIS_32_GOTPLT, /* type */
293 2, /* size (0 = byte, 1 = short, 2 = long) */
295 FALSE, /* pc_relative */
297 complain_overflow_bitfield, /* complain_on_overflow */
298 bfd_elf_generic_reloc, /* special_function */
299 "R_CRIS_32_GOTPLT", /* name */
300 FALSE, /* partial_inplace */
302 0xffffffff, /* dst_mask */
303 FALSE), /* pcrel_offset */
305 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
307 HOWTO (R_CRIS_32_GOTREL, /* type */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
311 FALSE, /* pc_relative */
313 complain_overflow_bitfield, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_CRIS_32_GOTREL", /* name */
316 FALSE, /* partial_inplace */
318 0xffffffff, /* dst_mask */
319 FALSE), /* pcrel_offset */
321 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
322 to create PLT entry for symbol. */
323 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
325 2, /* size (0 = byte, 1 = short, 2 = long) */
327 FALSE, /* pc_relative */
329 complain_overflow_bitfield, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* special_function */
331 "R_CRIS_32_PLT_GOTREL", /* name */
332 FALSE, /* partial_inplace */
334 0xffffffff, /* dst_mask */
335 FALSE), /* pcrel_offset */
337 /* A 32-bit offset from PC (location after the relocation) + addend to
338 entry for this symbol in PLT and request to create PLT entry for
340 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
344 TRUE, /* pc_relative */
346 complain_overflow_bitfield, /* complain_on_overflow */
347 cris_elf_pcrel_reloc, /* special_function */
348 "R_CRIS_32_PLT_PCREL", /* name */
349 FALSE, /* partial_inplace */
351 0xffffffff, /* dst_mask */
352 TRUE), /* pcrel_offset */
354 /* We don't handle these in any special manner and cross-format
355 linking is not supported; just recognize them enough to pass them
356 around. FIXME: do the same for most PIC relocs and add sanity
357 tests to actually refuse gracefully to handle these and PIC
358 relocs for cross-format linking. */
359 #define TLSHOWTO32(name) \
360 HOWTO (name, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, \
361 bfd_elf_generic_reloc, #name, FALSE, 0, 0xffffffff, FALSE)
362 #define TLSHOWTO16X(name, X) \
363 HOWTO (name, 0, 1, 16, FALSE, 0, complain_overflow_ ## X, \
364 bfd_elf_generic_reloc, #name, FALSE, 0, 0xffff, FALSE)
365 #define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
366 #define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
368 TLSHOWTO32 (R_CRIS_32_GOT_GD),
369 TLSHOWTO16 (R_CRIS_16_GOT_GD),
370 TLSHOWTO32 (R_CRIS_32_GD),
371 TLSHOWTO32 (R_CRIS_DTP),
372 TLSHOWTO32 (R_CRIS_32_DTPREL),
373 TLSHOWTO16S (R_CRIS_16_DTPREL),
374 TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
375 TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
376 TLSHOWTO32 (R_CRIS_32_TPREL),
377 TLSHOWTO16S (R_CRIS_16_TPREL),
378 TLSHOWTO32 (R_CRIS_DTPMOD),
379 TLSHOWTO32 (R_CRIS_32_IE)
382 /* Map BFD reloc types to CRIS ELF reloc types. */
384 struct cris_reloc_map
386 bfd_reloc_code_real_type bfd_reloc_val;
387 unsigned int cris_reloc_val;
390 static const struct cris_reloc_map cris_reloc_map [] =
392 { BFD_RELOC_NONE, R_CRIS_NONE },
393 { BFD_RELOC_8, R_CRIS_8 },
394 { BFD_RELOC_16, R_CRIS_16 },
395 { BFD_RELOC_32, R_CRIS_32 },
396 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
397 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
398 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
399 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
400 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
401 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
402 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
403 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
404 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
405 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
406 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
407 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
408 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
409 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
410 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
411 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
412 { BFD_RELOC_CRIS_32_GOT_GD, R_CRIS_32_GOT_GD },
413 { BFD_RELOC_CRIS_16_GOT_GD, R_CRIS_16_GOT_GD },
414 { BFD_RELOC_CRIS_32_GD, R_CRIS_32_GD },
415 { BFD_RELOC_CRIS_DTP, R_CRIS_DTP },
416 { BFD_RELOC_CRIS_32_DTPREL, R_CRIS_32_DTPREL },
417 { BFD_RELOC_CRIS_16_DTPREL, R_CRIS_16_DTPREL },
418 { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
419 { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
420 { BFD_RELOC_CRIS_32_TPREL, R_CRIS_32_TPREL },
421 { BFD_RELOC_CRIS_16_TPREL, R_CRIS_16_TPREL },
422 { BFD_RELOC_CRIS_DTPMOD, R_CRIS_DTPMOD },
423 { BFD_RELOC_CRIS_32_IE, R_CRIS_32_IE }
426 static reloc_howto_type *
427 cris_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
428 bfd_reloc_code_real_type code)
432 for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
433 if (cris_reloc_map [i].bfd_reloc_val == code)
434 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
439 static reloc_howto_type *
440 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
445 i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
447 if (cris_elf_howto_table[i].name != NULL
448 && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
449 return &cris_elf_howto_table[i];
454 /* Set the howto pointer for an CRIS ELF reloc. */
457 cris_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
459 Elf_Internal_Rela * dst)
461 enum elf_cris_reloc_type r_type;
463 r_type = ELF32_R_TYPE (dst->r_info);
464 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
465 cache_ptr->howto = & cris_elf_howto_table [r_type];
468 bfd_reloc_status_type
469 cris_elf_pcrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
470 arelent *reloc_entry,
472 void * data ATTRIBUTE_UNUSED,
473 asection *input_section,
475 char **error_message ATTRIBUTE_UNUSED)
477 /* By default (using only bfd_elf_generic_reloc when linking to
478 non-ELF formats) PC-relative relocs are relative to the beginning
479 of the reloc. CRIS PC-relative relocs are relative to the position
480 *after* the reloc because that's what pre-CRISv32 PC points to
481 after reading an insn field with that reloc. (For CRISv32, PC is
482 actually relative to the start of the insn, but we keep the old
483 definition.) Still, we use as much generic machinery as we can.
485 Only adjust when doing a final link. */
486 if (output_bfd == (bfd *) NULL)
487 reloc_entry->addend -= 1 << reloc_entry->howto->size;
490 bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
491 input_section, output_bfd, error_message);
494 /* Support for core dump NOTE sections.
495 The slightly unintuitive code layout is an attempt to keep at least
496 some similarities with other ports, hoping to simplify general
497 changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart. */
500 cris_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
505 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
506 switch (note->descsz)
511 case 202: /* Linux/CRISv32 */
513 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
516 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
525 switch (note->descsz)
530 case 214: /* Linux/CRIS */
532 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
535 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
544 /* Make a ".reg/999" section. */
545 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
546 size, note->descpos + offset);
550 cris_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
552 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
553 switch (note->descsz)
558 case 124: /* Linux/CRISv32 elf_prpsinfo */
559 elf_tdata (abfd)->core->program
560 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
561 elf_tdata (abfd)->core->command
562 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
565 switch (note->descsz)
570 case 124: /* Linux/CRIS elf_prpsinfo */
571 elf_tdata (abfd)->core->program
572 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
573 elf_tdata (abfd)->core->command
574 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
577 /* Note that for some reason, a spurious space is tacked
578 onto the end of the args in some (at least one anyway)
579 implementations, so strip it off if it exists. */
582 char *command = elf_tdata (abfd)->core->command;
583 int n = strlen (command);
585 if (0 < n && command[n - 1] == ' ')
586 command[n - 1] = '\0';
592 /* The name of the dynamic interpreter. This is put in the .interp
595 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
597 /* The size in bytes of an entry in the procedure linkage table. */
599 #define PLT_ENTRY_SIZE 20
600 #define PLT_ENTRY_SIZE_V32 26
602 /* The first entry in an absolute procedure linkage table looks like this. */
604 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
607 0x7e, 0x7e, /* push mof. */
608 0x7f, 0x0d, /* (dip [pc+]) */
609 0, 0, 0, 0, /* Replaced with address of .got + 4. */
610 0x30, 0x7a, /* move [...],mof */
611 0x7f, 0x0d, /* (dip [pc+]) */
612 0, 0, 0, 0, /* Replaced with address of .got + 8. */
613 0x30, 0x09 /* jump [...] */
616 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
618 0x84, 0xe2, /* subq 4,$sp */
619 0x6f, 0xfe, /* move.d 0,$acr */
620 0, 0, 0, 0, /* Replaced by address of .got + 4. */
621 0x7e, 0x7a, /* move $mof,[$sp] */
622 0x3f, 0x7a, /* move [$acr],$mof */
623 0x04, 0xf2, /* addq 4,acr */
624 0x6f, 0xfa, /* move.d [$acr],$acr */
625 0xbf, 0x09, /* jump $acr */
626 0xb0, 0x05, /* nop */
627 0, 0 /* Pad out to 26 bytes. */
630 /* Subsequent entries in an absolute procedure linkage table look like
633 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
635 0x7f, 0x0d, /* (dip [pc+]) */
636 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
637 0x30, 0x09, /* jump [...] */
638 0x3f, 0x7e, /* move [pc+],mof */
639 0, 0, 0, 0, /* Replaced with offset into relocation table. */
640 0x2f, 0xfe, /* add.d [pc+],pc */
642 0xff, 0xff /* Replaced with offset to start of .plt. */
645 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
647 0x6f, 0xfe, /* move.d 0,$acr */
648 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
649 0x6f, 0xfa, /* move.d [$acr],$acr */
650 0xbf, 0x09, /* jump $acr */
651 0xb0, 0x05, /* nop */
652 0x3f, 0x7e, /* move 0,mof */
653 0, 0, 0, 0, /* Replaced with offset into relocation table. */
654 0xbf, 0x0e, /* ba start_of_plt0_entry */
655 0, 0, 0, 0, /* Replaced with offset to plt0 entry. */
659 /* The first entry in a PIC procedure linkage table looks like this. */
661 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
663 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
664 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
665 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
666 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
669 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
671 0x84, 0xe2, /* subq 4,$sp */
672 0x04, 0x01, /* addoq 4,$r0,$acr */
673 0x7e, 0x7a, /* move $mof,[$sp] */
674 0x3f, 0x7a, /* move [$acr],$mof */
675 0x04, 0xf2, /* addq 4,$acr */
676 0x6f, 0xfa, /* move.d [$acr],$acr */
677 0xbf, 0x09, /* jump $acr */
678 0xb0, 0x05, /* nop */
679 0, 0, /* Pad out to 26 bytes. */
684 /* Subsequent entries in a PIC procedure linkage table look like this. */
686 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
688 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
689 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
690 0x30, 0x09, /* jump [...] */
691 0x3f, 0x7e, /* move [pc+],mof */
692 0, 0, 0, 0, /* Replaced with offset into relocation table. */
693 0x2f, 0xfe, /* add.d [pc+],pc */
694 0xec, 0xff, /* Replaced with offset to start of .plt. */
698 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
700 0x6f, 0x0d, /* addo.d 0,$r0,$acr */
701 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
702 0x6f, 0xfa, /* move.d [$acr],$acr */
703 0xbf, 0x09, /* jump $acr */
704 0xb0, 0x05, /* nop */
705 0x3f, 0x7e, /* move relocoffs,$mof */
706 0, 0, 0, 0, /* Replaced with offset into relocation table. */
707 0xbf, 0x0e, /* ba start_of_plt */
708 0, 0, 0, 0, /* Replaced with offset to start of .plt. */
712 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
713 (and most other PIC/shlib stuff). Check that we don't drift away
716 The CRIS linker, like the m68k and i386 linkers (and probably the rest
717 too) needs to keep track of the number of relocs that it decides to
718 copy in check_relocs for each symbol. This is so that it can discard
719 PC relative relocs if it doesn't need them when linking with
720 -Bsymbolic. We store the information in a field extending the regular
721 ELF linker hash table. */
723 /* This structure keeps track of the number of PC relative relocs we have
724 copied for a given symbol. */
726 struct elf_cris_pcrel_relocs_copied
729 struct elf_cris_pcrel_relocs_copied *next;
731 /* A section in dynobj. */
734 /* Number of relocs copied in this section. */
737 /* Example of reloc being copied, for message. */
738 enum elf_cris_reloc_type r_type;
741 /* CRIS ELF linker hash entry. */
743 struct elf_cris_link_hash_entry
745 struct elf_link_hash_entry root;
747 /* Number of PC relative relocs copied for this symbol. */
748 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
750 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
751 both a general GOT and a PLT-specific GOT entry for the same symbol,
752 when it is referenced both as a function and as a function pointer.
754 Number of GOTPLT references for a function. */
755 bfd_signed_vma gotplt_refcount;
757 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
758 (zero is never used as an index). FIXME: We should be able to fold
759 this with gotplt_refcount in a union, like the got and plt unions in
760 elf_link_hash_entry. */
761 bfd_size_type gotplt_offset;
763 /* The root.got.refcount is the sum of the regular reference counts
764 (this) and those members below. We have to keep a separate count
765 to track when we've found the first (or last) reference to a
766 regular got entry. The offset is in root.got.offset. */
767 bfd_signed_vma reg_got_refcount;
769 /* Similar to the above, the number of reloc references to this
770 symbols that need a R_CRIS_32_TPREL slot. The offset is in
771 root.got.offset, because this and .dtp_refcount can't validly
772 happen when there's also a regular GOT entry; that's invalid
773 input for which an error is emitted. */
774 bfd_signed_vma tprel_refcount;
776 /* Similar to the above, the number of reloc references to this
777 symbols that need a R_CRIS_DTP slot. The offset is in
778 root.got.offset; plus 4 if .tprel_refcount > 0. */
779 bfd_signed_vma dtp_refcount;
783 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *,
786 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *,
789 /* The local_got_refcounts and local_got_offsets are a multiple of
790 LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
791 refcount for GOT itself, see code), with the summary / group offset
792 for local symbols located at offset N, reference counts for
793 ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
794 relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
797 #define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
798 #define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
799 #define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
800 #define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
802 /* CRIS ELF linker hash table. */
804 struct elf_cris_link_hash_table
806 struct elf_link_hash_table root;
808 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
809 since we try and avoid creating GOTPLT:s when there's already a GOT.
810 Instead, we keep and update the next available index here. */
811 bfd_size_type next_gotplt_entry;
813 /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
814 been seen for any input; if != 0, then the constant-offset
815 R_CRIS_DTPMOD is needed for this DSO/executable. This turns
816 negative at relocation, so that we don't need an extra flag for
817 when the reloc is output. */
818 bfd_signed_vma dtpmod_refcount;
821 /* Traverse a CRIS ELF linker hash table. */
823 #define elf_cris_link_hash_traverse(table, func, info) \
824 (elf_link_hash_traverse \
826 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
829 /* Get the CRIS ELF linker hash table from a link_info structure. */
831 #define elf_cris_hash_table(p) \
832 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
833 == CRIS_ELF_DATA ? ((struct elf_cris_link_hash_table *) ((p)->hash)) : NULL)
835 /* Get the CRIS ELF linker hash entry from a regular hash entry (the
836 "parent class"). The .root reference is just a simple type
837 check on the argument. */
839 #define elf_cris_hash_entry(p) \
840 ((struct elf_cris_link_hash_entry *) (&(p)->root))
842 /* Create an entry in a CRIS ELF linker hash table. */
844 static struct bfd_hash_entry *
845 elf_cris_link_hash_newfunc (struct bfd_hash_entry *entry,
846 struct bfd_hash_table *table,
849 struct elf_cris_link_hash_entry *ret =
850 (struct elf_cris_link_hash_entry *) entry;
852 /* Allocate the structure if it has not already been allocated by a
854 if (ret == (struct elf_cris_link_hash_entry *) NULL)
855 ret = ((struct elf_cris_link_hash_entry *)
856 bfd_hash_allocate (table,
857 sizeof (struct elf_cris_link_hash_entry)));
858 if (ret == (struct elf_cris_link_hash_entry *) NULL)
859 return (struct bfd_hash_entry *) ret;
861 /* Call the allocation method of the superclass. */
862 ret = ((struct elf_cris_link_hash_entry *)
863 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
865 if (ret != (struct elf_cris_link_hash_entry *) NULL)
867 ret->pcrel_relocs_copied = NULL;
868 ret->gotplt_refcount = 0;
869 ret->gotplt_offset = 0;
870 ret->dtp_refcount = 0;
871 ret->tprel_refcount = 0;
872 ret->reg_got_refcount = 0;
875 return (struct bfd_hash_entry *) ret;
878 /* Create a CRIS ELF linker hash table. */
880 static struct bfd_link_hash_table *
881 elf_cris_link_hash_table_create (bfd *abfd)
883 struct elf_cris_link_hash_table *ret;
884 bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
886 ret = ((struct elf_cris_link_hash_table *) bfd_zmalloc (amt));
887 if (ret == (struct elf_cris_link_hash_table *) NULL)
890 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
891 elf_cris_link_hash_newfunc,
892 sizeof (struct elf_cris_link_hash_entry),
899 /* Initialize to skip over the first three entries in the gotplt; they
900 are used for run-time symbol evaluation. */
901 ret->next_gotplt_entry = 12;
903 return &ret->root.root;
906 /* Perform a single relocation. By default we use the standard BFD
907 routines, with a few tweaks. */
909 static bfd_reloc_status_type
910 cris_final_link_relocate (reloc_howto_type * howto,
912 asection * input_section,
914 Elf_Internal_Rela * rel,
917 bfd_reloc_status_type r;
918 enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
920 /* PC-relative relocations are relative to the position *after*
921 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
922 not a single byte, since PC must be 16-bit-aligned. */
925 /* Check that the 16-bit GOT relocs are positive. */
926 case R_CRIS_16_GOTPLT:
928 if ((bfd_signed_vma) relocation < 0)
929 return bfd_reloc_overflow;
932 case R_CRIS_32_PLT_PCREL:
933 case R_CRIS_32_PCREL:
937 case R_CRIS_16_PCREL:
945 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
946 contents, rel->r_offset,
947 relocation, rel->r_addend);
952 /* The number of errors left before we stop outputting reloc-specific
953 explanatory messages. By coincidence, this works nicely together
954 with the default number of messages you'll get from LD about
955 "relocation truncated to fit" messages before you get an
956 "additional relocation overflows omitted from the output". */
957 static int additional_relocation_error_msg_count = 10;
959 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
960 copied, for further comments. */
963 cris_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
964 struct bfd_link_info *info,
966 asection *input_section,
968 Elf_Internal_Rela *relocs,
969 Elf_Internal_Sym *local_syms,
970 asection **local_sections)
972 struct elf_cris_link_hash_table * htab;
974 Elf_Internal_Shdr *symtab_hdr;
975 struct elf_link_hash_entry **sym_hashes;
976 bfd_vma *local_got_offsets;
980 Elf_Internal_Rela *rel;
981 Elf_Internal_Rela *relend;
984 htab = elf_cris_hash_table (info);
988 dynobj = elf_hash_table (info)->dynobj;
989 local_got_offsets = elf_local_got_offsets (input_bfd);
990 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
991 sym_hashes = elf_sym_hashes (input_bfd);
992 relend = relocs + input_section->reloc_count;
1001 splt = bfd_get_linker_section (dynobj, ".plt");
1002 sgot = bfd_get_linker_section (dynobj, ".got");
1005 for (rel = relocs; rel < relend; rel ++)
1007 reloc_howto_type *howto;
1008 unsigned long r_symndx;
1009 Elf_Internal_Sym *sym;
1011 struct elf_link_hash_entry *h;
1013 bfd_reloc_status_type r;
1014 const char *symname = NULL;
1015 enum elf_cris_reloc_type r_type;
1017 r_type = ELF32_R_TYPE (rel->r_info);
1019 if ( r_type == R_CRIS_GNU_VTINHERIT
1020 || r_type == R_CRIS_GNU_VTENTRY)
1023 r_symndx = ELF32_R_SYM (rel->r_info);
1024 howto = cris_elf_howto_table + r_type;
1029 if (r_symndx < symtab_hdr->sh_info)
1031 sym = local_syms + r_symndx;
1032 sec = local_sections [r_symndx];
1033 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1035 symname = (bfd_elf_string_from_elf_section
1036 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1037 if (symname == NULL)
1038 symname = bfd_section_name (input_bfd, sec);
1042 bfd_boolean warned, ignored;
1043 bfd_boolean unresolved_reloc;
1045 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1046 r_symndx, symtab_hdr, sym_hashes,
1048 unresolved_reloc, warned, ignored);
1050 symname = h->root.root.string;
1052 if (unresolved_reloc
1053 /* Perhaps we should detect the cases that
1054 sec->output_section is expected to be NULL like i386 and
1055 m68k, but apparently (and according to elfxx-ia64.c) all
1056 valid cases are where the symbol is defined in a shared
1057 object which we link dynamically against. This includes
1058 PLT relocs for which we've created a PLT entry and other
1059 relocs for which we're prepared to create dynamic
1062 For now, new situations cause us to just err when
1063 sec->output_offset is NULL but the object with the symbol
1064 is *not* dynamically linked against. Thus this will
1065 automatically remind us so we can see if there are other
1066 valid cases we need to revisit. */
1067 && (sec->owner->flags & DYNAMIC) != 0)
1070 else if (h->root.type == bfd_link_hash_defined
1071 || h->root.type == bfd_link_hash_defweak)
1073 /* Here follow the cases where the relocation value must
1074 be zero (or when further handling is simplified when
1075 zero). I can't claim to understand the various
1076 conditions and they weren't described in the files
1077 where I copied them from (elf32-m68k.c and
1078 elf32-i386.c), but let's mention examples of where
1079 they happen. FIXME: Perhaps define and use a
1080 dynamic_symbol_p function like ia64.
1082 - When creating a shared library, we can have an
1083 ordinary relocation for a symbol defined in a shared
1084 library (perhaps the one we create). We then make
1085 the relocation value zero, as the value seen now will
1086 be added into the relocation addend in this shared
1087 library, but must be handled only at dynamic-link
1088 time. FIXME: Not sure this example covers the
1089 h->elf_link_hash_flags test, though it's there in
1092 && ((! info->symbolic && h->dynindx != -1)
1094 && (input_section->flags & SEC_ALLOC) != 0
1095 && (r_type == R_CRIS_8
1096 || r_type == R_CRIS_16
1097 || r_type == R_CRIS_32
1098 || r_type == R_CRIS_8_PCREL
1099 || r_type == R_CRIS_16_PCREL
1100 || r_type == R_CRIS_32_PCREL))
1102 else if (!info->relocatable && unresolved_reloc
1103 && (_bfd_elf_section_offset (output_bfd, info,
1109 (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1112 cris_elf_howto_table[r_type].name,
1114 bfd_set_error (bfd_error_bad_value);
1120 if (sec != NULL && discarded_section (sec))
1121 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1122 rel, 1, relend, howto, 0, contents);
1124 if (info->relocatable)
1129 case R_CRIS_16_GOTPLT:
1130 case R_CRIS_32_GOTPLT:
1131 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1132 but we require a PLT, and the PLT handling will take care of
1133 filling in the PLT-specific GOT entry. For the GOT offset,
1134 calculate it as we do when filling it in for the .got.plt
1135 section. If we don't have a PLT, punt to GOT handling. */
1137 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1140 = bfd_get_linker_section (dynobj, ".got.plt");
1143 BFD_ASSERT (h->dynindx != -1);
1144 BFD_ASSERT (sgotplt != NULL);
1147 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1149 relocation = got_offset;
1153 /* We didn't make a PLT entry for this symbol. Maybe everything is
1154 folded into the GOT. Other than folding, this happens when
1155 statically linking PIC code, or when using -Bsymbolic. Check
1156 that we instead have a GOT entry as done for us by
1157 elf_cris_adjust_dynamic_symbol, and drop through into the
1158 ordinary GOT cases. This must not happen for the
1159 executable, because any reference it does to a function
1160 that is satisfied by a DSO must generate a PLT. We assume
1161 these call-specific relocs don't address non-functions. */
1163 && (h->got.offset == (bfd_vma) -1
1167 && h->root.type == bfd_link_hash_undefweak)))))
1169 (*_bfd_error_handler)
1170 ((h->got.offset == (bfd_vma) -1)
1171 ? _("%B, section %A: No PLT nor GOT for relocation %s"
1172 " against symbol `%s'")
1173 : _("%B, section %A: No PLT for relocation %s"
1174 " against symbol `%s'"),
1177 cris_elf_howto_table[r_type].name,
1178 (symname != NULL && symname[0] != '\0'
1179 ? symname : _("[whose name is lost]")));
1181 /* FIXME: Perhaps blaming input is not the right thing to
1182 do; this is probably an internal error. But it is true
1183 that we didn't like that particular input. */
1184 bfd_set_error (bfd_error_bad_value);
1189 /* The size of the actual relocation is not used here; we only
1190 fill in the GOT table here. */
1196 /* Note that despite using RELA relocations, the .got contents
1197 is always filled in with the link-relative relocation
1198 value; the addend. */
1202 off = h->got.offset;
1203 BFD_ASSERT (off != (bfd_vma) -1);
1205 if (!elf_hash_table (info)->dynamic_sections_created
1208 || h->type == STT_FUNC
1211 && (info->symbolic || h->dynindx == -1)
1214 /* This wasn't checked above for ! info->shared, but
1215 must hold there if we get here; the symbol must
1216 be defined in the regular program or be undefweak
1217 or be a function or otherwise need a PLT. */
1218 BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1221 || h->type == STT_FUNC
1223 || h->root.type == bfd_link_hash_undefweak);
1225 /* This is actually a static link, or it is a
1226 -Bsymbolic link and the symbol is defined locally,
1227 or is undefweak, or the symbol was forced to be
1228 local because of a version file, or we're not
1229 creating a dynamic object. We must initialize this
1230 entry in the global offset table. Since the offset
1231 must always be a multiple of 4, we use the least
1232 significant bit to record whether we have
1233 initialized it already.
1235 If this GOT entry should be runtime-initialized, we
1236 will create a .rela.got relocation entry to
1237 initialize the value. This is done in the
1238 finish_dynamic_symbol routine. */
1243 bfd_put_32 (output_bfd, relocation,
1244 sgot->contents + off);
1251 BFD_ASSERT (local_got_offsets != NULL
1252 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1254 off = local_got_offsets[r_symndx];
1256 /* The offset must always be a multiple of 4. We use
1257 the least significant bit to record whether we have
1258 already generated the necessary reloc. */
1263 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1267 Elf_Internal_Rela outrel;
1270 if (srelgot == NULL)
1272 = bfd_get_linker_section (dynobj, ".rela.got");
1273 BFD_ASSERT (srelgot != NULL);
1275 outrel.r_offset = (sgot->output_section->vma
1276 + sgot->output_offset
1278 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1279 outrel.r_addend = relocation;
1280 loc = srelgot->contents;
1281 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1282 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1285 local_got_offsets[r_symndx] |= 1;
1289 relocation = sgot->output_offset + off;
1290 if (rel->r_addend != 0)
1292 /* We can't do anything for a relocation which is against
1293 a symbol *plus offset*. GOT holds relocations for
1294 symbols. Make this an error; the compiler isn't
1295 allowed to pass us these kinds of things. */
1297 (*_bfd_error_handler)
1298 (_("%B, section %A: relocation %s with non-zero addend %d"
1299 " against local symbol"),
1302 cris_elf_howto_table[r_type].name,
1305 (*_bfd_error_handler)
1306 (_("%B, section %A: relocation %s with non-zero addend %d"
1307 " against symbol `%s'"),
1310 cris_elf_howto_table[r_type].name,
1312 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1314 bfd_set_error (bfd_error_bad_value);
1320 case R_CRIS_32_GOTREL:
1321 /* This relocation must only be performed against local symbols.
1322 It's also ok when we link a program and the symbol is either
1323 defined in an ordinary (non-DSO) object or is undefined weak. */
1325 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1329 && h->root.type == bfd_link_hash_undefweak))))
1331 (*_bfd_error_handler)
1332 (_("%B, section %A: relocation %s is"
1333 " not allowed for global symbol: `%s'"),
1336 cris_elf_howto_table[r_type].name,
1338 bfd_set_error (bfd_error_bad_value);
1342 /* This can happen if we get a link error with the input ELF
1343 variant mismatching the output variant. Emit an error so
1344 it's noticed if it happens elsewhere. */
1347 (*_bfd_error_handler)
1348 (_("%B, section %A: relocation %s with no GOT created"),
1351 cris_elf_howto_table[r_type].name);
1352 bfd_set_error (bfd_error_bad_value);
1356 /* This relocation is like a PC-relative one, except the
1357 reference point is the location of GOT. Note that
1358 sgot->output_offset is not involved in this calculation. We
1359 always want the start of entire .got section, not the
1360 position after the reserved header. */
1361 relocation -= sgot->output_section->vma;
1364 case R_CRIS_32_PLT_PCREL:
1365 /* Relocation is to the entry for this symbol in the
1366 procedure linkage table. */
1368 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1369 without using the procedure linkage table. */
1370 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1373 if (h->plt.offset == (bfd_vma) -1
1376 /* We didn't make a PLT entry for this symbol. This
1377 happens when statically linking PIC code, or when
1378 using -Bsymbolic. */
1382 relocation = (splt->output_section->vma
1383 + splt->output_offset
1387 case R_CRIS_32_PLT_GOTREL:
1388 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1389 start of the .got section. See also comment at
1391 relocation -= sgot->output_section->vma;
1393 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1394 without using the procedure linkage table. */
1395 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1398 if (h->plt.offset == (bfd_vma) -1
1401 /* We didn't make a PLT entry for this symbol. This
1402 happens when statically linking PIC code, or when
1403 using -Bsymbolic. */
1407 relocation = (splt->output_section->vma
1408 + splt->output_offset
1410 - sgot->output_section->vma);
1413 case R_CRIS_8_PCREL:
1414 case R_CRIS_16_PCREL:
1415 case R_CRIS_32_PCREL:
1416 /* If the symbol was local, we need no shlib-specific handling. */
1417 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1418 || h->dynindx == -1)
1426 && r_symndx != STN_UNDEF
1427 && (input_section->flags & SEC_ALLOC) != 0
1428 && ((r_type != R_CRIS_8_PCREL
1429 && r_type != R_CRIS_16_PCREL
1430 && r_type != R_CRIS_32_PCREL)
1432 || (h != NULL && !h->def_regular))))
1434 Elf_Internal_Rela outrel;
1436 bfd_boolean skip, relocate;
1438 /* When generating a shared object, these relocations
1439 are copied into the output file to be resolved at run
1444 sreloc = _bfd_elf_get_dynamic_reloc_section
1445 (dynobj, input_section, /*rela?*/ TRUE);
1446 /* The section should have been created in cris_elf_check_relocs,
1447 but that function will not be called for objects which fail in
1448 cris_elf_merge_private_bfd_data. */
1451 bfd_set_error (bfd_error_bad_value);
1460 _bfd_elf_section_offset (output_bfd, info, input_section,
1462 if (outrel.r_offset == (bfd_vma) -1)
1464 else if (outrel.r_offset == (bfd_vma) -2
1465 /* For now, undefined weak symbols with non-default
1466 visibility (yielding 0), like exception info for
1467 discarded sections, will get a R_CRIS_NONE
1468 relocation rather than no relocation, because we
1469 notice too late that the symbol doesn't need a
1472 && h->root.type == bfd_link_hash_undefweak
1473 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
1474 skip = TRUE, relocate = TRUE;
1475 outrel.r_offset += (input_section->output_section->vma
1476 + input_section->output_offset);
1479 memset (&outrel, 0, sizeof outrel);
1480 /* h->dynindx may be -1 if the symbol was marked to
1483 && ((! info->symbolic && h->dynindx != -1)
1484 || !h->def_regular))
1486 BFD_ASSERT (h->dynindx != -1);
1487 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1488 outrel.r_addend = relocation + rel->r_addend;
1492 outrel.r_addend = relocation + rel->r_addend;
1494 if (r_type == R_CRIS_32)
1497 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1503 if (bfd_is_abs_section (sec))
1505 else if (sec == NULL || sec->owner == NULL)
1507 bfd_set_error (bfd_error_bad_value);
1514 /* We are turning this relocation into one
1515 against a section symbol. It would be
1516 proper to subtract the symbol's value,
1517 osec->vma, from the emitted reloc addend,
1518 but ld.so expects buggy relocs. */
1519 osec = sec->output_section;
1520 indx = elf_section_data (osec)->dynindx;
1523 osec = htab->root.text_index_section;
1524 indx = elf_section_data (osec)->dynindx;
1526 BFD_ASSERT (indx != 0);
1529 outrel.r_info = ELF32_R_INFO (indx, r_type);
1533 loc = sreloc->contents;
1534 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1535 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1537 /* This reloc will be computed at runtime, so there's no
1538 need to do anything now, except for R_CRIS_32 relocations
1539 that have been turned into R_CRIS_RELATIVE. */
1546 case R_CRIS_16_DTPREL:
1547 case R_CRIS_32_DTPREL:
1548 /* This relocation must only be performed against local
1549 symbols, or to sections that are not loadable. It's also
1550 ok when we link a program and the symbol is defined in an
1551 ordinary (non-DSO) object (if it's undefined there, we've
1552 already seen an error). */
1554 && (input_section->flags & SEC_ALLOC) != 0
1555 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1558 && h->root.type != bfd_link_hash_undefined)))
1560 (*_bfd_error_handler)
1561 ((h->root.type == bfd_link_hash_undefined)
1562 /* We shouldn't get here for GCC-emitted code. */
1563 ? _("%B, section %A: relocation %s has an undefined"
1564 " reference to `%s', perhaps a declaration mixup?")
1565 : ("%B, section %A: relocation %s is"
1566 " not allowed for `%s', a global symbol with default"
1567 " visibility, perhaps a declaration mixup?"),
1570 cris_elf_howto_table[r_type].name,
1571 symname != NULL && symname[0] != '\0'
1572 ? symname : _("[whose name is lost]"));
1573 bfd_set_error (bfd_error_bad_value);
1577 BFD_ASSERT ((input_section->flags & SEC_ALLOC) == 0
1578 || htab->dtpmod_refcount != 0);
1580 /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
1581 already done so. Note that we do this in .got.plt, not
1582 in .got, as .got.plt contains the first part, still the
1583 reloc is against .got, because the linker script directs
1584 (is required to direct) them both into .got. */
1585 if (htab->dtpmod_refcount > 0
1586 && (input_section->flags & SEC_ALLOC) != 0)
1588 asection *sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
1589 BFD_ASSERT (sgotplt != NULL);
1593 Elf_Internal_Rela outrel;
1596 if (srelgot == NULL)
1597 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1598 BFD_ASSERT (srelgot != NULL);
1599 loc = srelgot->contents;
1600 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1602 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
1603 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1604 outrel.r_offset = (sgotplt->output_section->vma
1605 + sgotplt->output_offset
1607 outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
1608 outrel.r_addend = 0;
1609 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1613 /* For an executable, the GOT entry contents is known. */
1614 bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
1615 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1618 /* Reverse the sign to mark that we've emitted the
1619 required GOT entry. */
1620 htab->dtpmod_refcount = - htab->dtpmod_refcount;
1623 /* The relocation is the offset from the start of the module
1624 TLS block to the (local) symbol. */
1625 relocation -= elf_hash_table (info)->tls_sec == NULL
1626 ? 0 : elf_hash_table (info)->tls_sec->vma;
1632 bfd_set_error (bfd_error_invalid_operation);
1634 /* We've already informed in cris_elf_check_relocs that
1635 this is an error. */
1640 case R_CRIS_16_GOT_GD:
1641 case R_CRIS_32_GOT_GD:
1642 if (rel->r_addend != 0)
1644 /* We can't do anything for a relocation which is against a
1645 symbol *plus offset*. The GOT holds relocations for
1646 symbols. Make this an error; the compiler isn't allowed
1647 to pass us these kinds of things. */
1648 (*_bfd_error_handler)
1649 (_("%B, section %A: relocation %s with non-zero addend %d"
1650 " against symbol `%s'"),
1653 cris_elf_howto_table[r_type].name,
1655 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1657 bfd_set_error (bfd_error_bad_value);
1662 && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1664 /* Known contents of the GOT. */
1667 /* The symbol is defined in the program, so just write
1668 (1, known_tpoffset) into the GOT. */
1669 relocation -= elf_hash_table (info)->tls_sec->vma;
1673 off = elf_cris_hash_entry (h)->tprel_refcount > 0
1674 ? h->got.offset + 4 : h->got.offset;
1678 off = local_got_offsets[r_symndx];
1679 if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1683 /* We use bit 1 of the offset as a flag for GOT entry with
1684 the R_CRIS_DTP reloc, setting it when we've emitted the
1685 GOT entry and reloc. Bit 0 is used for R_CRIS_32_TPREL
1694 local_got_offsets[r_symndx] |= 2;
1696 bfd_put_32 (output_bfd, 1, sgot->contents + off);
1697 bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
1702 relocation = sgot->output_offset + off
1703 + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1707 /* Not all parts of the GOT entry are known; emit a real
1712 off = elf_cris_hash_entry (h)->tprel_refcount > 0
1713 ? h->got.offset + 4 : h->got.offset;
1716 off = local_got_offsets[r_symndx];
1717 if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1721 /* See above re bit 1 and bit 0 usage. */
1724 Elf_Internal_Rela outrel;
1732 local_got_offsets[r_symndx] |= 2;
1734 /* Clear the target contents of the GOT (just as a
1735 gesture; it's already cleared on allocation): this
1736 relocation is not like the other dynrelocs. */
1737 bfd_put_32 (output_bfd, 0, sgot->contents + off);
1738 bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
1740 if (srelgot == NULL)
1741 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1742 BFD_ASSERT (srelgot != NULL);
1744 if (h != NULL && h->dynindx != -1)
1746 outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
1751 outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
1753 /* NULL if we had an error. */
1754 relocation -= elf_hash_table (info)->tls_sec == NULL
1755 ? 0 : elf_hash_table (info)->tls_sec->vma;
1758 outrel.r_offset = (sgot->output_section->vma
1759 + sgot->output_offset
1761 outrel.r_addend = relocation;
1762 loc = srelgot->contents;
1763 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1765 /* NULL if we had an error. */
1766 if (srelgot->contents != NULL)
1767 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1772 relocation = sgot->output_offset + off
1773 + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1776 /* The GOT-relative offset to the GOT entry is the
1777 relocation, or for R_CRIS_32_GD, the actual address of
1784 bfd_set_error (bfd_error_invalid_operation);
1786 /* We've already informed in cris_elf_check_relocs that
1787 this is an error. */
1792 case R_CRIS_32_GOT_TPREL:
1793 case R_CRIS_16_GOT_TPREL:
1794 if (rel->r_addend != 0)
1796 /* We can't do anything for a relocation which is
1797 against a symbol *plus offset*. GOT holds
1798 relocations for symbols. Make this an error; the
1799 compiler isn't allowed to pass us these kinds of
1801 (*_bfd_error_handler)
1802 (_("%B, section %A: relocation %s with non-zero addend %d"
1803 " against symbol `%s'"),
1806 cris_elf_howto_table[r_type].name,
1808 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1809 bfd_set_error (bfd_error_bad_value);
1814 && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1816 /* Known contents of the GOT. */
1819 /* The symbol is defined in the program, so just write
1820 the -prog_tls_size+known_tpoffset into the GOT. */
1821 relocation -= elf_hash_table (info)->tls_sec->vma;
1822 relocation -= elf_hash_table (info)->tls_size;
1825 off = h->got.offset;
1827 off = local_got_offsets[r_symndx];
1829 /* Bit 0 is used to mark whether we've emitted the required
1830 entry (and if needed R_CRIS_32_TPREL reloc). Bit 1
1831 is used similarly for R_CRIS_DTP, see above. */
1839 local_got_offsets[r_symndx] |= 1;
1841 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1846 relocation = sgot->output_offset + off
1847 + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1851 /* Emit a real relocation. */
1855 off = h->got.offset;
1857 off = local_got_offsets[r_symndx];
1859 /* See above re usage of bit 0 and 1. */
1862 Elf_Internal_Rela outrel;
1870 local_got_offsets[r_symndx] |= 1;
1872 if (srelgot == NULL)
1873 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1874 BFD_ASSERT (srelgot != NULL);
1876 if (h != NULL && h->dynindx != -1)
1878 outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
1883 outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
1885 /* NULL if we had an error. */
1886 relocation -= elf_hash_table (info)->tls_sec == NULL
1887 ? 0 : elf_hash_table (info)->tls_sec->vma;
1890 /* Just "define" the initial contents in some
1891 semi-logical way. */
1892 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1894 outrel.r_offset = (sgot->output_section->vma
1895 + sgot->output_offset
1897 outrel.r_addend = relocation;
1898 loc = srelgot->contents;
1899 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1900 /* NULL if we had an error. */
1901 if (srelgot->contents != NULL)
1902 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1907 relocation = sgot->output_offset + off
1908 + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1911 /* The GOT-relative offset to the GOT entry is the relocation,
1912 or for R_CRIS_32_GD, the actual address of the GOT entry. */
1915 case R_CRIS_16_TPREL:
1916 case R_CRIS_32_TPREL:
1917 /* This relocation must only be performed against symbols
1918 defined in an ordinary (non-DSO) object. */
1921 bfd_set_error (bfd_error_invalid_operation);
1923 /* We've already informed in cris_elf_check_relocs that
1924 this is an error. */
1929 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1930 && !(h->def_regular || ELF_COMMON_DEF_P (h))
1931 /* If it's undefined, then an error message has already
1933 && h->root.type != bfd_link_hash_undefined)
1935 (*_bfd_error_handler)
1936 (_("%B, section %A: relocation %s is"
1937 " not allowed for symbol: `%s'"
1938 " which is defined outside the program,"
1939 " perhaps a declaration mixup?"),
1942 cris_elf_howto_table[r_type].name,
1944 bfd_set_error (bfd_error_bad_value);
1948 /* NULL if we had an error. */
1949 relocation -= elf_hash_table (info)->tls_sec == NULL
1951 : (elf_hash_table (info)->tls_sec->vma
1952 + elf_hash_table (info)->tls_size);
1954 /* The TLS-relative offset is the relocation. */
1962 r = cris_final_link_relocate (howto, input_bfd, input_section,
1963 contents, rel, relocation);
1965 if (r != bfd_reloc_ok)
1967 const char * msg = (const char *) NULL;
1971 case bfd_reloc_overflow:
1972 r = info->callbacks->reloc_overflow
1973 (info, (h ? &h->root : NULL), symname, howto->name,
1974 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1975 if (additional_relocation_error_msg_count > 0)
1977 additional_relocation_error_msg_count--;
1980 case R_CRIS_16_GOTPLT:
1983 /* Not just TLS is involved here, so we make
1984 generation and message depend on -fPIC/-fpic
1986 case R_CRIS_16_GOT_TPREL:
1987 case R_CRIS_16_GOT_GD:
1988 (*_bfd_error_handler)
1989 (_("(too many global variables for -fpic:"
1990 " recompile with -fPIC)"));
1993 case R_CRIS_16_TPREL:
1994 case R_CRIS_16_DTPREL:
1995 (*_bfd_error_handler)
1996 (_("(thread-local data too big for -fpic or"
1997 " -msmall-tls: recompile with -fPIC or"
1998 " -mno-small-tls)"));
2001 /* No known cause for overflow for other relocs. */
2008 case bfd_reloc_undefined:
2009 r = info->callbacks->undefined_symbol
2010 (info, symname, input_bfd, input_section, rel->r_offset,
2014 case bfd_reloc_outofrange:
2015 msg = _("internal error: out of range error");
2018 case bfd_reloc_notsupported:
2019 msg = _("internal error: unsupported relocation error");
2022 case bfd_reloc_dangerous:
2023 msg = _("internal error: dangerous relocation");
2027 msg = _("internal error: unknown error");
2032 r = info->callbacks->warning
2033 (info, msg, symname, input_bfd, input_section, rel->r_offset);
2043 /* Finish up dynamic symbol handling. We set the contents of various
2044 dynamic sections here. */
2047 elf_cris_finish_dynamic_symbol (bfd *output_bfd,
2048 struct bfd_link_info *info,
2049 struct elf_link_hash_entry *h,
2050 Elf_Internal_Sym *sym)
2052 struct elf_cris_link_hash_table * htab;
2055 /* Where in the plt entry to put values. */
2056 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
2058 /* What offset to add to the distance to the first PLT entry for the
2059 value at plt_off3. */
2060 int plt_off3_value_bias = 4;
2062 /* Where in the PLT entry the call-dynlink-stub is (happens to be same
2063 for PIC and non-PIC for v32 and pre-v32). */
2064 int plt_stub_offset = 8;
2065 int plt_entry_size = PLT_ENTRY_SIZE;
2066 const bfd_byte *plt_entry = elf_cris_plt_entry;
2067 const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
2069 htab = elf_cris_hash_table (info);
2073 /* Adjust the various PLT entry offsets. */
2074 if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2078 plt_off3_value_bias = -2;
2079 plt_stub_offset = 12;
2080 plt_entry_size = PLT_ENTRY_SIZE_V32;
2081 plt_entry = elf_cris_plt_entry_v32;
2082 plt_pic_entry = elf_cris_pic_plt_entry_v32;
2085 dynobj = elf_hash_table (info)->dynobj;
2087 if (h->plt.offset != (bfd_vma) -1)
2094 bfd_vma gotplt_offset
2095 = elf_cris_hash_entry (h)->gotplt_offset;
2096 Elf_Internal_Rela rela;
2098 bfd_boolean has_gotplt = gotplt_offset != 0;
2100 /* Get the index in the .rela.plt relocations for the .got.plt
2101 entry that corresponds to this symbol.
2102 We have to count backwards here, and the result is only valid
2103 as an index into .rela.plt. We also have to undo the effect
2104 of the R_CRIS_DTPMOD entry at .got index 3 (offset 12 into
2105 .got.plt) for which gotplt_offset is adjusted, because while
2106 that entry goes into .got.plt, its relocation goes into
2107 .rela.got, not .rela.plt. (It's not PLT-specific; not to be
2108 processed as part of the runtime lazy .rela.plt relocation).
2109 FIXME: There be literal constants here... */
2110 bfd_vma rela_plt_index
2111 = (htab->dtpmod_refcount != 0
2112 ? gotplt_offset/4 - 2 - 3 : gotplt_offset/4 - 3);
2114 /* Get the offset into the .got table of the entry that corresponds
2115 to this function. Note that we embed knowledge that "incoming"
2116 .got goes after .got.plt in the output without padding (pointer
2117 aligned). However, that knowledge is present in several other
2122 : h->got.offset + htab->next_gotplt_entry);
2124 /* This symbol has an entry in the procedure linkage table. Set it
2127 BFD_ASSERT (h->dynindx != -1);
2129 splt = bfd_get_linker_section (dynobj, ".plt");
2130 sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2131 srela = bfd_get_linker_section (dynobj, ".rela.plt");
2132 BFD_ASSERT (splt != NULL && sgotplt != NULL
2133 && (! has_gotplt || srela != NULL));
2135 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2137 /* Fill in the entry in the procedure linkage table. */
2140 memcpy (splt->contents + h->plt.offset, plt_entry,
2143 /* We need to enter the absolute address of the GOT entry here. */
2144 bfd_put_32 (output_bfd, got_base + got_offset,
2145 splt->contents + h->plt.offset + plt_off1);
2149 memcpy (splt->contents + h->plt.offset, plt_pic_entry,
2151 bfd_put_32 (output_bfd, got_offset,
2152 splt->contents + h->plt.offset + plt_off1);
2155 /* Fill in the plt entry and make a relocation, if this is a "real"
2159 /* Fill in the offset to the reloc table. */
2160 bfd_put_32 (output_bfd,
2161 rela_plt_index * sizeof (Elf32_External_Rela),
2162 splt->contents + h->plt.offset + plt_off2);
2164 /* Fill in the offset to the first PLT entry, where to "jump". */
2165 bfd_put_32 (output_bfd,
2166 - (h->plt.offset + plt_off3 + plt_off3_value_bias),
2167 splt->contents + h->plt.offset + plt_off3);
2169 /* Fill in the entry in the global offset table with the address of
2170 the relocating stub. */
2171 bfd_put_32 (output_bfd,
2172 (splt->output_section->vma
2173 + splt->output_offset
2176 sgotplt->contents + got_offset);
2178 /* Fill in the entry in the .rela.plt section. */
2179 rela.r_offset = (sgotplt->output_section->vma
2180 + sgotplt->output_offset
2182 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
2184 loc = srela->contents + rela_plt_index * sizeof (Elf32_External_Rela);
2185 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2188 if (!h->def_regular)
2190 /* Mark the symbol as undefined, rather than as defined in
2191 the .plt section. Leave the value alone. */
2192 sym->st_shndx = SHN_UNDEF;
2194 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
2195 know whether resetting the value is significant; if it really
2196 is, rather than a quirk or bug in the sparc port, then I
2197 believe we'd see this elsewhere. */
2198 /* If the symbol is weak, we do need to clear the value.
2199 Otherwise, the PLT entry would provide a definition for
2200 the symbol even if the symbol wasn't defined anywhere,
2201 and so the symbol would never be NULL. */
2202 if (!h->ref_regular_nonweak)
2207 /* For an ordinary program, we emit .got relocs only for symbols that
2208 are in the dynamic-symbols table and are either defined by the
2209 program or are undefined weak symbols, or are function symbols
2210 where we do not output a PLT: the PLT reloc was output above and all
2211 references to the function symbol are redirected to the PLT. */
2212 if (h->got.offset != (bfd_vma) -1
2213 && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
2215 || (h->dynindx != -1
2216 && h->plt.offset == (bfd_vma) -1
2218 && h->root.type != bfd_link_hash_undefweak)))
2222 Elf_Internal_Rela rela;
2226 /* This symbol has an entry in the global offset table. Set it up. */
2228 sgot = bfd_get_linker_section (dynobj, ".got");
2229 srela = bfd_get_linker_section (dynobj, ".rela.got");
2230 BFD_ASSERT (sgot != NULL && srela != NULL);
2232 rela.r_offset = (sgot->output_section->vma
2233 + sgot->output_offset
2234 + (h->got.offset &~ (bfd_vma) 1));
2236 /* If this is a static link, or it is a -Bsymbolic link and the
2237 symbol is defined locally or was forced to be local because
2238 of a version file, we just want to emit a RELATIVE reloc.
2239 The entry in the global offset table will already have been
2240 initialized in the relocate_section function. */
2241 where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
2242 if (! elf_hash_table (info)->dynamic_sections_created
2244 && (info->symbolic || h->dynindx == -1)
2247 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
2248 rela.r_addend = bfd_get_signed_32 (output_bfd, where);
2252 bfd_put_32 (output_bfd, (bfd_vma) 0, where);
2253 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
2257 loc = srela->contents;
2258 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2259 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2265 Elf_Internal_Rela rela;
2268 /* This symbol needs a copy reloc. Set it up. */
2270 BFD_ASSERT (h->dynindx != -1
2271 && (h->root.type == bfd_link_hash_defined
2272 || h->root.type == bfd_link_hash_defweak));
2274 s = bfd_get_linker_section (dynobj, ".rela.bss");
2275 BFD_ASSERT (s != NULL);
2277 rela.r_offset = (h->root.u.def.value
2278 + h->root.u.def.section->output_section->vma
2279 + h->root.u.def.section->output_offset);
2280 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
2282 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2283 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2286 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2287 if (h == elf_hash_table (info)->hdynamic
2288 || h == elf_hash_table (info)->hgot)
2289 sym->st_shndx = SHN_ABS;
2294 /* Finish up the dynamic sections. Do *not* emit relocs here, as their
2295 offsets were changed, as part of -z combreloc handling, from those we
2299 elf_cris_finish_dynamic_sections (bfd *output_bfd,
2300 struct bfd_link_info *info)
2306 dynobj = elf_hash_table (info)->dynobj;
2308 sgot = bfd_get_linker_section (dynobj, ".got.plt");
2309 BFD_ASSERT (sgot != NULL);
2310 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2312 if (elf_hash_table (info)->dynamic_sections_created)
2315 Elf32_External_Dyn *dyncon, *dynconend;
2317 splt = bfd_get_linker_section (dynobj, ".plt");
2318 BFD_ASSERT (splt != NULL && sdyn != NULL);
2320 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2321 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2322 for (; dyncon < dynconend; dyncon++)
2324 Elf_Internal_Dyn dyn;
2327 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2335 s = bfd_get_section_by_name (output_bfd, ".got");
2336 BFD_ASSERT (s != NULL);
2337 dyn.d_un.d_ptr = s->vma;
2338 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2342 /* Yes, we *can* have a .plt and no .plt.rela, for instance
2343 if all symbols are found in the .got (not .got.plt). */
2344 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2345 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
2346 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2350 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2354 dyn.d_un.d_val = s->size;
2355 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2359 /* The procedure linkage table relocs (DT_JMPREL) should
2360 not be included in the overall relocs (DT_RELA).
2361 Therefore, we override the DT_RELASZ entry here to
2362 make it not include the JMPREL relocs. Since the
2363 linker script arranges for .rela.plt to follow all
2364 other relocation sections, we don't have to worry
2365 about changing the DT_RELA entry. */
2366 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2368 dyn.d_un.d_val -= s->size;
2369 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2374 /* Fill in the first entry in the procedure linkage table. */
2377 if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2380 memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
2381 PLT_ENTRY_SIZE_V32);
2384 memcpy (splt->contents, elf_cris_plt0_entry_v32,
2385 PLT_ENTRY_SIZE_V32);
2386 bfd_put_32 (output_bfd,
2387 sgot->output_section->vma
2388 + sgot->output_offset + 4,
2389 splt->contents + 4);
2391 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2392 = PLT_ENTRY_SIZE_V32;
2398 memcpy (splt->contents, elf_cris_pic_plt0_entry,
2402 memcpy (splt->contents, elf_cris_plt0_entry,
2404 bfd_put_32 (output_bfd,
2405 sgot->output_section->vma
2406 + sgot->output_offset + 4,
2407 splt->contents + 6);
2408 bfd_put_32 (output_bfd,
2409 sgot->output_section->vma
2410 + sgot->output_offset + 8,
2411 splt->contents + 14);
2413 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2420 /* Fill in the first three entries in the global offset table. */
2424 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2426 bfd_put_32 (output_bfd,
2427 sdyn->output_section->vma + sdyn->output_offset,
2429 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2430 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2433 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2438 /* Return the section that should be marked against GC for a given
2442 cris_elf_gc_mark_hook (asection *sec,
2443 struct bfd_link_info *info,
2444 Elf_Internal_Rela *rel,
2445 struct elf_link_hash_entry *h,
2446 Elf_Internal_Sym *sym)
2448 enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
2452 case R_CRIS_GNU_VTINHERIT:
2453 case R_CRIS_GNU_VTENTRY:
2460 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2463 /* Update the got entry reference counts for the section being removed. */
2466 cris_elf_gc_sweep_hook (bfd *abfd,
2467 struct bfd_link_info *info,
2469 const Elf_Internal_Rela *relocs)
2471 struct elf_cris_link_hash_table * htab;
2472 Elf_Internal_Shdr *symtab_hdr;
2473 struct elf_link_hash_entry **sym_hashes;
2474 bfd_signed_vma *local_got_refcounts;
2475 const Elf_Internal_Rela *rel, *relend;
2480 if (info->relocatable)
2483 dynobj = elf_hash_table (info)->dynobj;
2487 htab = elf_cris_hash_table (info);
2491 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2492 sym_hashes = elf_sym_hashes (abfd);
2493 local_got_refcounts = elf_local_got_refcounts (abfd);
2495 sgot = bfd_get_linker_section (dynobj, ".got");
2496 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2498 relend = relocs + sec->reloc_count;
2499 for (rel = relocs; rel < relend; rel++)
2501 unsigned long r_symndx;
2502 struct elf_link_hash_entry *h = NULL;
2503 bfd_signed_vma got_element_size = 4;
2504 bfd_signed_vma *specific_refcount = NULL;
2505 enum elf_cris_reloc_type r_type;
2507 r_symndx = ELF32_R_SYM (rel->r_info);
2508 if (r_symndx >= symtab_hdr->sh_info)
2510 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2511 while (h->root.type == bfd_link_hash_indirect
2512 || h->root.type == bfd_link_hash_warning)
2513 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2516 r_type = ELF32_R_TYPE (rel->r_info);
2521 case R_CRIS_16_GOTPLT:
2522 case R_CRIS_32_GOTPLT:
2523 specific_refcount = h != NULL
2524 ? &((struct elf_cris_link_hash_entry *) h)->reg_got_refcount
2525 : &local_got_refcounts[LGOT_REG_NDX (r_symndx)];
2529 case R_CRIS_32_GOT_GD:
2530 case R_CRIS_16_GOT_GD:
2531 got_element_size = 8;
2532 specific_refcount = h != NULL
2533 ? &((struct elf_cris_link_hash_entry *) h)->dtp_refcount
2534 : &local_got_refcounts[LGOT_DTP_NDX (r_symndx)];
2538 case R_CRIS_16_GOT_TPREL:
2539 case R_CRIS_32_GOT_TPREL:
2540 specific_refcount = h != NULL
2541 ? &((struct elf_cris_link_hash_entry *) h)->tprel_refcount
2542 : &local_got_refcounts[LGOT_TPREL_NDX (r_symndx)];
2553 case R_CRIS_16_GOT_TPREL:
2554 case R_CRIS_32_GOT_TPREL:
2555 case R_CRIS_32_GOT_GD:
2556 case R_CRIS_16_GOT_GD:
2561 /* If the counters are 0 when we got here, we've
2562 miscounted somehow somewhere, an internal error. */
2563 BFD_ASSERT (h->got.refcount > 0);
2566 BFD_ASSERT (*specific_refcount > 0);
2567 --*specific_refcount;
2568 if (*specific_refcount == 0)
2570 /* We don't need the .got entry any more. */
2571 sgot->size -= got_element_size;
2572 srelgot->size -= sizeof (Elf32_External_Rela);
2578 if (local_got_refcounts != NULL)
2580 /* If the counters are 0 when we got here, we've
2581 miscounted somehow somewhere, an internal error. */
2582 BFD_ASSERT (local_got_refcounts[r_symndx] > 0);
2583 --local_got_refcounts[r_symndx];
2585 BFD_ASSERT (*specific_refcount > 0);
2586 --*specific_refcount;
2587 if (*specific_refcount == 0)
2589 /* We don't need the .got entry any more. */
2590 sgot->size -= got_element_size;
2592 srelgot->size -= sizeof (Elf32_External_Rela);
2597 case R_CRIS_16_GOTPLT:
2598 case R_CRIS_32_GOTPLT:
2599 /* For local symbols, treat these like GOT relocs. */
2601 goto local_got_reloc;
2603 /* For global symbols, adjust the reloc-specific refcount. */
2604 elf_cris_hash_entry (h)->gotplt_refcount--;
2607 case R_CRIS_32_PLT_GOTREL:
2608 /* FIXME: We don't garbage-collect away the .got section. */
2609 if (local_got_refcounts != NULL)
2610 local_got_refcounts[-1]--;
2616 case R_CRIS_8_PCREL:
2617 case R_CRIS_16_PCREL:
2618 case R_CRIS_32_PCREL:
2619 case R_CRIS_32_PLT_PCREL:
2620 /* Negate the increment we did in cris_elf_check_relocs. */
2623 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2624 && h->plt.refcount > 0)
2629 case R_CRIS_32_DTPREL:
2630 /* This'd be a .dtpreld entry in e.g. debug info. */
2631 if ((sec->flags & SEC_ALLOC) == 0)
2634 case R_CRIS_16_DTPREL:
2635 htab->dtpmod_refcount--;
2636 if (htab->dtpmod_refcount == 0)
2637 htab->next_gotplt_entry -= 8;
2638 BFD_ASSERT (local_got_refcounts != NULL);
2639 local_got_refcounts[-1]--;
2650 /* The elf_backend_plt_sym_val hook function. */
2653 cris_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, const asection *plt,
2656 bfd_size_type plt_entry_size;
2657 bfd_size_type pltoffs;
2658 bfd *abfd = plt->owner;
2660 /* Same for CRIS and CRIS v32; see elf_cris_(|pic_)plt_entry(|_v32)[]. */
2661 bfd_size_type plt_entry_got_offset = 2;
2662 bfd_size_type plt_sec_size;
2663 bfd_size_type got_vma_for_dyn;
2666 /* FIXME: the .got section should be readily available also when
2667 we're not linking. */
2668 if ((got = bfd_get_section_by_name (abfd, ".got")) == NULL)
2669 return (bfd_vma) -1;
2671 plt_sec_size = bfd_section_size (plt->owner, plt);
2673 = (bfd_get_mach (abfd) == bfd_mach_cris_v32
2674 ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2676 /* Data in PLT is GOT-relative for DYN, but absolute for EXE. */
2677 got_vma_for_dyn = (abfd->flags & EXEC_P) ? 0 : got->vma;
2679 /* Because we can have merged GOT entries; a single .got entry for
2680 both GOT and the PLT part of the GOT (.got.plt), the index of the
2681 reloc in .rela.plt is not the same as the index in the PLT.
2682 Instead, we have to hunt down the GOT offset in the PLT that
2683 corresponds to that of this reloc. Unfortunately, we will only
2684 be called for the .rela.plt relocs, so we'll miss synthetic
2685 symbols for .plt entries with merged GOT entries. (FIXME:
2686 fixable by providing our own bfd_elf32_get_synthetic_symtab.
2687 Doesn't seem worthwile at time of this writing.) FIXME: we've
2688 gone from O(1) to O(N) (N number of PLT entries) for finding each
2689 PLT address. Shouldn't matter in practice though. */
2691 for (pltoffs = plt_entry_size;
2692 pltoffs < plt_sec_size;
2693 pltoffs += plt_entry_size)
2695 bfd_size_type got_offset;
2696 bfd_byte gotoffs_raw[4];
2698 if (!bfd_get_section_contents (abfd, (asection *) plt, gotoffs_raw,
2699 pltoffs + plt_entry_got_offset,
2700 sizeof (gotoffs_raw)))
2701 return (bfd_vma) -1;
2703 got_offset = bfd_get_32 (abfd, gotoffs_raw);
2704 if (got_offset + got_vma_for_dyn == rel->address)
2705 return plt->vma + pltoffs;
2708 /* While it's tempting to BFD_ASSERT that we shouldn't get here,
2709 that'd not be graceful behavior for invalid input. */
2710 return (bfd_vma) -1;
2713 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2714 entry but we found we will not create any. Called when we find we will
2715 not have any PLT for this symbol, by for example
2716 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2717 or elf_cris_size_dynamic_sections if no dynamic sections will be
2718 created (we're only linking static objects). */
2721 elf_cris_adjust_gotplt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2723 struct bfd_link_info *info = (struct bfd_link_info *) p;
2725 /* A GOTPLT reloc, when activated, is supposed to be included into
2726 the PLT refcount. */
2727 BFD_ASSERT (h->gotplt_refcount == 0
2728 || h->gotplt_refcount <= h->root.plt.refcount);
2730 /* If nobody wanted a GOTPLT with this symbol, we're done. */
2731 if (h->gotplt_refcount <= 0)
2734 if (h->reg_got_refcount > 0)
2736 /* There's a GOT entry for this symbol. Just adjust the refcounts.
2737 Probably not necessary at this stage, but keeping them accurate
2738 helps avoiding surprises later. */
2739 h->root.got.refcount += h->gotplt_refcount;
2740 h->reg_got_refcount += h->gotplt_refcount;
2741 h->gotplt_refcount = 0;
2745 /* No GOT entry for this symbol. We need to create one. */
2746 bfd *dynobj = elf_hash_table (info)->dynobj;
2750 BFD_ASSERT (dynobj != NULL);
2751 sgot = bfd_get_linker_section (dynobj, ".got");
2752 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2754 /* Put accurate refcounts there. */
2755 h->root.got.refcount += h->gotplt_refcount;
2756 h->reg_got_refcount = h->gotplt_refcount;
2758 h->gotplt_refcount = 0;
2760 /* We always have a .got and a .rela.got section if there were
2761 GOTPLT relocs in input. */
2762 BFD_ASSERT (sgot != NULL && srelgot != NULL);
2764 /* Allocate space in the .got section. */
2767 /* Allocate relocation space. */
2768 srelgot->size += sizeof (Elf32_External_Rela);
2774 /* Try to fold PLT entries with GOT entries. There are two cases when we
2777 - When all PLT references are GOTPLT references, and there are GOT
2778 references, and this is not the executable. We don't have to
2779 generate a PLT at all.
2781 - When there are both (ordinary) PLT references and GOT references,
2782 and this isn't the executable.
2783 We want to make the PLT reference use the ordinary GOT entry rather
2784 than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2785 since the GOT entry will have to be resolved at startup anyway.
2787 Though the latter case is handled when room for the PLT is allocated,
2790 By folding into the GOT, we may need a round-trip to a PLT in the
2791 executable for calls, a loss in performance. Still, losing a
2792 reloc is a win in size and at least in start-up time.
2794 Note that this function is called before symbols are forced local by
2795 version scripts. The differing cases are handled by
2796 elf_cris_hide_symbol. */
2799 elf_cris_try_fold_plt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2801 struct bfd_link_info *info = (struct bfd_link_info *) p;
2803 /* If there are no GOT references for this symbol, we can't fold any
2804 other reference so there's nothing to do. Likewise if there are no
2805 PLT references; GOTPLT references included. */
2806 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2809 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
2810 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2812 if (h->gotplt_refcount == h->root.plt.refcount)
2814 /* The only PLT references are GOTPLT references, and there are GOT
2815 references. Convert PLT to GOT references. */
2816 if (! elf_cris_adjust_gotplt_to_got (h, info))
2819 /* Clear the PLT references, so no PLT will be created. */
2820 h->root.plt.offset = (bfd_vma) -1;
2826 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2827 to use a GOT entry (and create one) rather than requiring a GOTPLT
2831 elf_cris_hide_symbol (struct bfd_link_info *info,
2832 struct elf_link_hash_entry *h,
2833 bfd_boolean force_local)
2835 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2837 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2840 /* Adjust a symbol defined by a dynamic object and referenced by a
2841 regular object. The current definition is in some section of the
2842 dynamic object, but we're not including those sections. We have to
2843 change the definition to something the rest of the link can
2847 elf_cris_adjust_dynamic_symbol (struct bfd_link_info *info,
2848 struct elf_link_hash_entry *h)
2850 struct elf_cris_link_hash_table * htab;
2853 bfd_size_type plt_entry_size;
2855 htab = elf_cris_hash_table (info);
2859 dynobj = elf_hash_table (info)->dynobj;
2861 /* Make sure we know what is going on here. */
2862 BFD_ASSERT (dynobj != NULL
2864 || h->u.weakdef != NULL
2867 && !h->def_regular)));
2870 = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2871 ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2873 /* If this is a function, put it in the procedure linkage table. We
2874 will fill in the contents of the procedure linkage table later,
2875 when we know the address of the .got section. */
2876 if (h->type == STT_FUNC
2879 /* If we link a program (not a DSO), we'll get rid of unnecessary
2880 PLT entries; we point to the actual symbols -- even for pic
2881 relocs, because a program built with -fpic should have the same
2882 result as one built without -fpic, specifically considering weak
2884 FIXME: m68k and i386 differ here, for unclear reasons. */
2888 /* This case can occur if we saw a PLT reloc in an input file,
2889 but the symbol was not defined by a dynamic object. In such
2890 a case, we don't actually need to build a procedure linkage
2891 table, and we can just do an absolute or PC reloc instead, or
2892 change a .got.plt index to a .got index for GOTPLT relocs. */
2893 BFD_ASSERT (h->needs_plt);
2895 h->plt.offset = (bfd_vma) -1;
2897 elf_cris_adjust_gotplt_to_got ((struct
2898 elf_cris_link_hash_entry *) h,
2902 /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2903 where a pointer-equivalent symbol was unimportant (i.e. more
2904 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2905 of the PLT. We can't for the executable, because the GOT
2906 entries will point to the PLT there (and be constant). */
2908 && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2912 /* GC or folding may have rendered this entry unused. */
2913 if (h->plt.refcount <= 0)
2916 h->plt.offset = (bfd_vma) -1;
2920 /* Make sure this symbol is output as a dynamic symbol. */
2921 if (h->dynindx == -1)
2923 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2927 s = bfd_get_linker_section (dynobj, ".plt");
2928 BFD_ASSERT (s != NULL);
2930 /* If this is the first .plt entry, make room for the special
2933 s->size += plt_entry_size;
2935 /* If this symbol is not defined in a regular file, and we are
2936 not generating a shared library, then set the symbol to this
2937 location in the .plt. */
2941 h->root.u.def.section = s;
2942 h->root.u.def.value = s->size;
2945 /* If there's already a GOT entry, use that, not a .got.plt. A
2946 GOT field still has a reference count when we get here; it's
2947 not yet changed to an offset. We can't do this for an
2948 executable, because then the reloc associated with the PLT
2949 would get a non-PLT reloc pointing to the PLT. FIXME: Move
2950 this to elf_cris_try_fold_plt_to_got. */
2951 if (info->shared && h->got.refcount > 0)
2953 h->got.refcount += h->plt.refcount;
2955 /* Mark the PLT offset to use the GOT entry by setting the low
2956 bit in the plt offset; it is always a multiple of
2957 plt_entry_size (which is at least a multiple of 2). */
2958 BFD_ASSERT ((s->size % plt_entry_size) == 0);
2960 /* Change the PLT refcount to an offset. */
2961 h->plt.offset = s->size;
2963 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2964 that the got entry should be used instead. */
2965 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2966 h)->gotplt_offset == 0);
2968 /* Make room for this entry. */
2969 s->size += plt_entry_size;
2974 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2975 h->plt.offset = s->size;
2977 /* Make room for this entry. */
2978 s->size += plt_entry_size;
2980 /* We also need to make an entry in the .got.plt section, which
2981 will be placed in the .got section by the linker script. */
2982 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2983 = htab->next_gotplt_entry;
2984 htab->next_gotplt_entry += 4;
2986 s = bfd_get_linker_section (dynobj, ".got.plt");
2987 BFD_ASSERT (s != NULL);
2990 /* We also need to make an entry in the .rela.plt section. */
2992 s = bfd_get_linker_section (dynobj, ".rela.plt");
2993 BFD_ASSERT (s != NULL);
2994 s->size += sizeof (Elf32_External_Rela);
2999 /* Reinitialize the plt offset now that it is not used as a reference
3001 h->plt.offset = (bfd_vma) -1;
3003 /* If this is a weak symbol, and there is a real definition, the
3004 processor independent code will have arranged for us to see the
3005 real definition first, and we can just use the same value. */
3006 if (h->u.weakdef != NULL)
3008 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3009 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3010 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3011 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3015 /* This is a reference to a symbol defined by a dynamic object which
3016 is not a function. */
3018 /* If we are creating a shared library, we must presume that the
3019 only references to the symbol are via the global offset table.
3020 For such cases we need not do anything here; the relocations will
3021 be handled correctly by relocate_section. */
3025 /* If there are no references to this symbol that do not use the
3026 GOT, we don't need to generate a copy reloc. */
3027 if (!h->non_got_ref)
3030 /* We must allocate the symbol in our .dynbss section, which will
3031 become part of the .bss section of the executable. There will be
3032 an entry for this symbol in the .dynsym section. The dynamic
3033 object will contain position independent code, so all references
3034 from the dynamic object to this symbol will go through the global
3035 offset table. The dynamic linker will use the .dynsym entry to
3036 determine the address it must put in the global offset table, so
3037 both the dynamic object and the regular object will refer to the
3038 same memory location for the variable. */
3040 s = bfd_get_linker_section (dynobj, ".dynbss");
3041 BFD_ASSERT (s != NULL);
3043 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
3044 copy the initial value out of the dynamic object and into the
3045 runtime process image. We need to remember the offset into the
3046 .rela.bss section we are going to use. */
3047 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3051 srel = bfd_get_linker_section (dynobj, ".rela.bss");
3052 BFD_ASSERT (srel != NULL);
3053 srel->size += sizeof (Elf32_External_Rela);
3057 return _bfd_elf_adjust_dynamic_copy (h, s);
3060 /* Adjust our "subclass" elements for an indirect symbol. */
3063 elf_cris_copy_indirect_symbol (struct bfd_link_info *info,
3064 struct elf_link_hash_entry *dir,
3065 struct elf_link_hash_entry *ind)
3067 struct elf_cris_link_hash_entry *edir, *eind;
3069 edir = (struct elf_cris_link_hash_entry *) dir;
3070 eind = (struct elf_cris_link_hash_entry *) ind;
3072 /* Only indirect symbols are replaced; we're not interested in
3073 updating any of EIND's fields for other symbols. */
3074 if (eind->root.root.type != bfd_link_hash_indirect)
3076 /* Still, we need to copy flags for e.g. weak definitions. */
3077 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3081 BFD_ASSERT (edir->gotplt_offset == 0 || eind->gotplt_offset == 0);
3083 #define XMOVOPZ(F, OP, Z) edir->F OP eind->F; eind->F = Z
3084 #define XMOVE(F) XMOVOPZ (F, +=, 0)
3085 if (eind->pcrel_relocs_copied != NULL)
3087 if (edir->pcrel_relocs_copied != NULL)
3089 struct elf_cris_pcrel_relocs_copied **pp;
3090 struct elf_cris_pcrel_relocs_copied *p;
3092 /* Add reloc counts against the indirect sym to the direct sym
3093 list. Merge any entries against the same section. */
3094 for (pp = &eind->pcrel_relocs_copied; *pp != NULL;)
3096 struct elf_cris_pcrel_relocs_copied *q;
3098 for (q = edir->pcrel_relocs_copied; q != NULL; q = q->next)
3099 if (q->section == p->section)
3101 q->count += p->count;
3108 *pp = edir->pcrel_relocs_copied;
3110 XMOVOPZ (pcrel_relocs_copied, =, NULL);
3112 XMOVE (gotplt_refcount);
3113 XMOVE (gotplt_offset);
3114 XMOVE (reg_got_refcount);
3115 XMOVE (tprel_refcount);
3116 XMOVE (dtp_refcount);
3120 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3123 /* Look through the relocs for a section during the first phase. */
3126 cris_elf_check_relocs (bfd *abfd,
3127 struct bfd_link_info *info,
3129 const Elf_Internal_Rela *relocs)
3131 struct elf_cris_link_hash_table * htab;
3133 Elf_Internal_Shdr *symtab_hdr;
3134 struct elf_link_hash_entry **sym_hashes;
3135 bfd_signed_vma *local_got_refcounts;
3136 const Elf_Internal_Rela *rel;
3137 const Elf_Internal_Rela *rel_end;
3142 if (info->relocatable)
3145 htab = elf_cris_hash_table (info);
3149 dynobj = elf_hash_table (info)->dynobj;
3150 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3151 sym_hashes = elf_sym_hashes (abfd);
3152 local_got_refcounts = elf_local_got_refcounts (abfd);
3158 rel_end = relocs + sec->reloc_count;
3159 for (rel = relocs; rel < rel_end; rel++)
3161 struct elf_link_hash_entry *h;
3162 unsigned long r_symndx;
3163 enum elf_cris_reloc_type r_type;
3164 bfd_signed_vma got_element_size = 4;
3165 unsigned long r_symndx_lgot = INT_MAX;
3167 r_symndx = ELF32_R_SYM (rel->r_info);
3168 if (r_symndx < symtab_hdr->sh_info)
3171 r_symndx_lgot = LGOT_REG_NDX (r_symndx);
3175 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3176 while (h->root.type == bfd_link_hash_indirect
3177 || h->root.type == bfd_link_hash_warning)
3178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3180 /* PR15323, ref flags aren't set for references in the same
3182 h->root.non_ir_ref = 1;
3185 r_type = ELF32_R_TYPE (rel->r_info);
3187 /* Some relocs require linker-created sections; we need to hang them
3188 on the first input bfd we found that contained dynamic relocs. */
3191 case R_CRIS_32_DTPREL:
3192 if ((sec->flags & SEC_ALLOC) == 0)
3193 /* This'd be a .dtpreld entry in e.g. debug info. We have
3194 several different switch statements below, but none of
3195 that is needed; we need no preparations for resolving
3196 R_CRIS_32_DTPREL into a non-allocated section (debug
3197 info), so let's just move on to the next
3201 case R_CRIS_16_DTPREL:
3202 /* The first .got.plt entry is right after the R_CRIS_DTPMOD
3203 entry at index 3. */
3204 if (htab->dtpmod_refcount == 0)
3205 htab->next_gotplt_entry += 8;
3207 htab->dtpmod_refcount++;
3212 case R_CRIS_16_GOT_GD:
3213 case R_CRIS_32_GOT_GD:
3214 case R_CRIS_32_GOT_TPREL:
3215 case R_CRIS_16_GOT_TPREL:
3218 case R_CRIS_32_GOTREL:
3219 case R_CRIS_32_PLT_GOTREL:
3220 case R_CRIS_32_PLT_PCREL:
3221 case R_CRIS_16_GOTPLT:
3222 case R_CRIS_32_GOTPLT:
3225 elf_hash_table (info)->dynobj = dynobj = abfd;
3227 /* We could handle this if we can get a handle on the
3228 output bfd in elf_cris_adjust_dynamic_symbol. Failing
3229 that, we must insist on dynobj being a specific mach. */
3230 if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
3232 (*_bfd_error_handler)
3233 (_("%B, section %A:\n v10/v32 compatible object %s"
3234 " must not contain a PIC relocation"),
3242 /* We may have a dynobj but no .got section, if machine-
3243 independent parts of the linker found a reason to create
3244 a dynobj. We want to create the .got section now, so we
3245 can assume it's always present whenever there's a dynobj.
3246 It's ok to call this function more than once. */
3247 if (!_bfd_elf_create_got_section (dynobj, info))
3250 sgot = bfd_get_linker_section (dynobj, ".got");
3253 if (local_got_refcounts == NULL)
3257 /* We use index local_got_refcounts[-1] to count all
3258 GOT-relative relocations that do not have explicit
3260 amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
3261 amt *= sizeof (bfd_signed_vma);
3262 local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
3263 if (local_got_refcounts == NULL)
3266 local_got_refcounts++;
3267 elf_local_got_refcounts (abfd) = local_got_refcounts;
3275 /* Some relocs require a global offset table (but perhaps not a
3276 specific GOT entry). */
3279 case R_CRIS_16_DTPREL:
3280 case R_CRIS_32_DTPREL:
3281 /* Not requesting .got.rela for an executable: the contents
3282 of the first entry is constant there. For a shared
3283 library, we need .got.rela for the R_CRIS_DTPMOD
3284 relocation at index 3. */
3291 case R_CRIS_16_GOT_GD:
3292 case R_CRIS_32_GOT_GD:
3293 case R_CRIS_32_GOT_TPREL:
3294 case R_CRIS_16_GOT_TPREL:
3297 /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
3298 entry only for local symbols. Unfortunately, we don't know
3299 until later on if there's a version script that forces the
3300 symbol local. We must have the .rela.got section in place
3301 before we know if the symbol looks global now, so we need
3302 to treat the reloc just like for R_CRIS_16_GOT and
3304 case R_CRIS_16_GOTPLT:
3305 case R_CRIS_32_GOTPLT:
3309 && (h != NULL || info->shared))
3311 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
3312 if (srelgot == NULL)
3314 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3315 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3317 srelgot = bfd_make_section_anyway_with_flags (dynobj,
3321 || !bfd_set_section_alignment (dynobj, srelgot, 2))
3331 /* Warn and error for invalid input. */
3335 case R_CRIS_32_TPREL:
3336 case R_CRIS_16_TPREL:
3340 (*_bfd_error_handler)
3341 (_("%B, section %A:\n relocation %s not valid"
3342 " in a shared object;"
3343 " typically an option mixup, recompile with -fPIC"),
3346 cris_elf_howto_table[r_type].name);
3347 /* Don't return FALSE here; we want messages for all of
3348 these and the error behavior is ungraceful
3358 case R_CRIS_16_GOT_GD:
3359 case R_CRIS_32_GOT_GD:
3360 /* These are requests for tls_index entries, run-time R_CRIS_DTP. */
3361 got_element_size = 8;
3362 r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
3365 case R_CRIS_16_DTPREL:
3366 case R_CRIS_32_DTPREL:
3367 /* These two just request for the constant-index
3368 module-local tls_index-sized GOT entry, which we add
3373 case R_CRIS_32_GOT_TPREL:
3374 case R_CRIS_16_GOT_TPREL:
3375 r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
3377 /* Those relocs also require that a DSO is of type
3378 Initial Exec. Like other targets, we don't reset this
3379 flag even if the relocs are GC:ed away. */
3381 info->flags |= DF_STATIC_TLS;
3384 /* Let's list the other assembler-generated TLS-relocs too,
3385 just to show that they're not forgotten. */
3386 case R_CRIS_16_TPREL:
3387 case R_CRIS_32_TPREL:
3394 case R_CRIS_16_GOTPLT:
3395 case R_CRIS_32_GOTPLT:
3396 /* Mark that we need a GOT entry if the PLT entry (and its GOT
3397 entry) is eliminated. We can only do this for a non-local
3401 elf_cris_hash_entry (h)->gotplt_refcount++;
3402 goto handle_gotplt_reloc;
3404 /* If h is NULL then this is a local symbol, and we must make a
3405 GOT entry for it, so handle it like a GOT reloc. */
3410 case R_CRIS_16_GOT_GD:
3411 case R_CRIS_32_GOT_GD:
3412 case R_CRIS_32_GOT_TPREL:
3413 case R_CRIS_16_GOT_TPREL:
3416 /* This symbol requires a global offset table entry. */
3419 if (h->got.refcount == 0)
3421 /* Make sure this symbol is output as a dynamic symbol. */
3422 if (h->dynindx == -1)
3424 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3429 /* Update the sum of reloc counts for this symbol. */
3436 if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
3438 /* Allocate space in the .got section. */
3439 sgot->size += got_element_size;
3440 /* Allocate relocation space. */
3441 srelgot->size += sizeof (Elf32_External_Rela);
3443 elf_cris_hash_entry (h)->reg_got_refcount++;
3447 case R_CRIS_16_GOT_GD:
3448 case R_CRIS_32_GOT_GD:
3449 if (elf_cris_hash_entry (h)->dtp_refcount == 0)
3451 /* Allocate space in the .got section. */
3452 sgot->size += got_element_size;
3453 /* Allocate relocation space. */
3454 srelgot->size += sizeof (Elf32_External_Rela);
3456 elf_cris_hash_entry (h)->dtp_refcount++;
3460 case R_CRIS_32_GOT_TPREL:
3461 case R_CRIS_16_GOT_TPREL:
3462 if (elf_cris_hash_entry (h)->tprel_refcount == 0)
3464 /* Allocate space in the .got section. */
3465 sgot->size += got_element_size;
3466 /* Allocate relocation space. */
3467 srelgot->size += sizeof (Elf32_External_Rela);
3469 elf_cris_hash_entry (h)->tprel_refcount++;
3479 /* This is a global offset table entry for a local symbol. */
3480 if (local_got_refcounts[r_symndx_lgot] == 0)
3482 sgot->size += got_element_size;
3485 /* If we are generating a shared object, we need
3486 to output a R_CRIS_RELATIVE reloc so that the
3487 dynamic linker can adjust this GOT entry.
3488 Similarly for non-regular got entries. */
3489 srelgot->size += sizeof (Elf32_External_Rela);
3492 /* Update the reloc-specific count. */
3493 local_got_refcounts[r_symndx_lgot]++;
3495 /* This one is the sum of all the others. */
3496 local_got_refcounts[r_symndx]++;
3500 case R_CRIS_16_DTPREL:
3501 case R_CRIS_32_DTPREL:
3502 case R_CRIS_32_GOTREL:
3503 /* This reference requires a global offset table.
3504 FIXME: The actual refcount isn't used currently; the .got
3505 section can't be removed if there were any references in the
3507 local_got_refcounts[-1]++;
3510 handle_gotplt_reloc:
3512 case R_CRIS_32_PLT_GOTREL:
3513 /* This reference requires a global offset table. */
3514 local_got_refcounts[-1]++;
3517 case R_CRIS_32_PLT_PCREL:
3518 /* This symbol requires a procedure linkage table entry. We
3519 actually build the entry in adjust_dynamic_symbol,
3520 because this might be a case of linking PIC code which is
3521 never referenced by a dynamic object, in which case we
3522 don't need to generate a procedure linkage table entry
3525 /* Beware: if we'd check for visibility of the symbol here
3526 (and not marking the need for a PLT when non-visible), we'd
3527 get into trouble with keeping handling consistent with
3528 regards to relocs found before definition and GOTPLT
3529 handling. Eliminable PLT entries will be dealt with later
3541 /* Let's help debug shared library creation. Any of these
3542 relocs *can* be used in shared libs, but pages containing
3543 them cannot be shared, so they're not appropriate for
3544 common use. Don't warn for sections we don't care about,
3545 such as debug sections or non-constant sections. We
3546 can't help tables of (global) function pointers, for
3547 example, though they must be emitted in a (writable) data
3548 section to avoid having impure text sections. */
3550 && (sec->flags & SEC_ALLOC) != 0
3551 && (sec->flags & SEC_READONLY) != 0)
3553 /* FIXME: How do we make this optionally a warning only? */
3554 (*_bfd_error_handler)
3555 (_("%B, section %A:\n relocation %s should not"
3556 " be used in a shared object; recompile with -fPIC"),
3559 cris_elf_howto_table[r_type].name);
3562 /* We don't need to handle relocs into sections not going into
3563 the "real" output. */
3564 if ((sec->flags & SEC_ALLOC) == 0)
3571 /* Make sure a plt entry is created for this symbol if it
3572 turns out to be a function defined by a dynamic object. */
3573 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3577 /* If we are creating a shared library and this is not a local
3578 symbol, we need to copy the reloc into the shared library.
3579 However when linking with -Bsymbolic and this is a global
3580 symbol which is defined in an object we are including in the
3581 link (i.e., DEF_REGULAR is set), then we can resolve the
3582 reloc directly. At this point we have not seen all the input
3583 files, so it is possible that DEF_REGULAR is not set now but
3584 will be set later (it is never cleared). In case of a weak
3585 definition, DEF_REGULAR may be cleared later by a strong
3586 definition in a shared library. We account for that
3587 possibility below by storing information in the relocs_copied
3588 field of the hash table entry. A similar situation occurs
3589 when creating shared libraries and symbol visibility changes
3590 render the symbol local. */
3592 /* No need to do anything if we're not creating a shared object. */
3596 /* We may need to create a reloc section in the dynobj and made room
3600 sreloc = _bfd_elf_make_dynamic_reloc_section
3601 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
3607 if (sec->flags & SEC_READONLY)
3608 info->flags |= DF_TEXTREL;
3610 sreloc->size += sizeof (Elf32_External_Rela);
3613 case R_CRIS_8_PCREL:
3614 case R_CRIS_16_PCREL:
3615 case R_CRIS_32_PCREL:
3620 /* Make sure a plt entry is created for this symbol if it
3621 turns out to be a function defined by a dynamic object. */
3622 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3626 /* If we are creating a shared library and this is not a local
3627 symbol, we need to copy the reloc into the shared library.
3628 However when linking with -Bsymbolic and this is a global
3629 symbol which is defined in an object we are including in the
3630 link (i.e., DEF_REGULAR is set), then we can resolve the
3631 reloc directly. At this point we have not seen all the input
3632 files, so it is possible that DEF_REGULAR is not set now but
3633 will be set later (it is never cleared). In case of a weak
3634 definition, DEF_REGULAR may be cleared later by a strong
3635 definition in a shared library. We account for that
3636 possibility below by storing information in the relocs_copied
3637 field of the hash table entry. A similar situation occurs
3638 when creating shared libraries and symbol visibility changes
3639 render the symbol local. */
3641 /* No need to do anything if we're not creating a shared object. */
3645 /* We don't need to handle relocs into sections not going into
3646 the "real" output. */
3647 if ((sec->flags & SEC_ALLOC) == 0)
3650 /* If the symbol is local, then we know already we can
3651 eliminate the reloc. */
3652 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3655 /* If this is with -Bsymbolic and the symbol isn't weak, and
3656 is defined by an ordinary object (the ones we include in
3657 this shared library) then we can also eliminate the
3658 reloc. See comment above for more eliminable cases which
3659 we can't identify at this time. */
3661 && h->root.type != bfd_link_hash_defweak
3665 /* We may need to create a reloc section in the dynobj and made room
3669 sreloc = _bfd_elf_make_dynamic_reloc_section
3670 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
3676 sreloc->size += sizeof (Elf32_External_Rela);
3678 /* We count the number of PC relative relocations we have
3679 entered for this symbol, so that we can discard them
3680 again if the symbol is later defined by a regular object.
3681 We know that h is really a pointer to an
3682 elf_cris_link_hash_entry. */
3684 struct elf_cris_link_hash_entry *eh;
3685 struct elf_cris_pcrel_relocs_copied *p;
3687 eh = elf_cris_hash_entry (h);
3689 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3690 if (p->section == sec)
3695 p = ((struct elf_cris_pcrel_relocs_copied *)
3696 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
3699 p->next = eh->pcrel_relocs_copied;
3700 eh->pcrel_relocs_copied = p;
3710 /* This relocation describes the C++ object vtable hierarchy.
3711 Reconstruct it for later use during GC. */
3712 case R_CRIS_GNU_VTINHERIT:
3713 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3717 /* This relocation describes which C++ vtable entries are actually
3718 used. Record for later use during GC. */
3719 case R_CRIS_GNU_VTENTRY:
3720 BFD_ASSERT (h != NULL);
3722 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3726 case R_CRIS_16_TPREL:
3727 case R_CRIS_32_TPREL:
3728 /* Already warned above, when necessary. */
3732 /* Other relocs do not appear here. */
3733 bfd_set_error (bfd_error_bad_value);
3741 /* Set the sizes of the dynamic sections. */
3744 elf_cris_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3745 struct bfd_link_info *info)
3747 struct elf_cris_link_hash_table * htab;
3753 htab = elf_cris_hash_table (info);
3757 dynobj = elf_hash_table (info)->dynobj;
3758 BFD_ASSERT (dynobj != NULL);
3760 if (elf_hash_table (info)->dynamic_sections_created)
3762 /* Set the contents of the .interp section to the interpreter. */
3763 if (info->executable)
3765 s = bfd_get_linker_section (dynobj, ".interp");
3766 BFD_ASSERT (s != NULL);
3767 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3768 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3773 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
3774 elf_cris_link_hash_traverse (htab, elf_cris_adjust_gotplt_to_got,
3777 /* We may have created entries in the .rela.got section.
3778 However, if we are not creating the dynamic sections, we will
3779 not actually use these entries. Reset the size of .rela.got,
3780 which will cause it to get stripped from the output file
3782 s = bfd_get_linker_section (dynobj, ".rela.got");
3787 /* If this is a -Bsymbolic shared link, then we need to discard all PC
3788 relative relocs against symbols defined in a regular object. We
3789 allocated space for them in the check_relocs routine, but we will not
3790 fill them in in the relocate_section routine. We also discard space
3791 for relocs that have become for local symbols due to symbol
3792 visibility changes. For programs, we discard space for relocs for
3793 symbols not referenced by any dynamic object. */
3795 elf_cris_link_hash_traverse (htab,
3796 elf_cris_discard_excess_dso_dynamics,
3799 elf_cris_link_hash_traverse (htab,
3800 elf_cris_discard_excess_program_dynamics,
3803 /* The check_relocs and adjust_dynamic_symbol entry points have
3804 determined the sizes of the various dynamic sections. Allocate
3808 for (s = dynobj->sections; s != NULL; s = s->next)
3812 if ((s->flags & SEC_LINKER_CREATED) == 0)
3815 /* It's OK to base decisions on the section name, because none
3816 of the dynobj section names depend upon the input files. */
3817 name = bfd_get_section_name (dynobj, s);
3819 if (strcmp (name, ".plt") == 0)
3821 /* Remember whether there is a PLT. */
3824 else if (strcmp (name, ".got.plt") == 0)
3826 /* The .got.plt contains the .got header as well as the
3827 actual .got.plt contents. The .got header may contain a
3828 R_CRIS_DTPMOD entry at index 3. */
3829 s->size += htab->dtpmod_refcount != 0
3832 else if (CONST_STRNEQ (name, ".rela"))
3834 if (strcmp (name, ".rela.got") == 0
3835 && htab->dtpmod_refcount != 0
3837 s->size += sizeof (Elf32_External_Rela);
3841 /* Remember whether there are any reloc sections other
3843 if (strcmp (name, ".rela.plt") != 0)
3846 /* We use the reloc_count field as a counter if we need
3847 to copy relocs into the output file. */
3851 else if (! CONST_STRNEQ (name, ".got")
3852 && strcmp (name, ".dynbss") != 0)
3854 /* It's not one of our sections, so don't allocate space. */
3860 /* If we don't need this section, strip it from the
3861 output file. This is mostly to handle .rela.bss and
3862 .rela.plt. We must create both sections in
3863 create_dynamic_sections, because they must be created
3864 before the linker maps input sections to output
3865 sections. The linker does that before
3866 adjust_dynamic_symbol is called, and it is that
3867 function which decides whether anything needs to go
3868 into these sections. */
3869 s->flags |= SEC_EXCLUDE;
3873 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3876 /* Allocate memory for the section contents. We use bfd_zalloc here
3877 in case unused entries are not reclaimed before the section's
3878 contents are written out. This should not happen, but this way
3879 if it does, we will not write out garbage. For reloc sections,
3880 this will make entries have the type R_CRIS_NONE. */
3881 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3882 if (s->contents == NULL)
3886 if (elf_hash_table (info)->dynamic_sections_created)
3888 /* Add some entries to the .dynamic section. We fill in the
3889 values later, in elf_cris_finish_dynamic_sections, but we
3890 must add the entries now so that we get the correct size for
3891 the .dynamic section. The DT_DEBUG entry is filled in by the
3892 dynamic linker and used by the debugger. */
3893 #define add_dynamic_entry(TAG, VAL) \
3894 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3898 if (!add_dynamic_entry (DT_DEBUG, 0))
3904 if (!add_dynamic_entry (DT_PLTGOT, 0)
3905 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3906 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3907 || !add_dynamic_entry (DT_JMPREL, 0))
3913 if (!add_dynamic_entry (DT_RELA, 0)
3914 || !add_dynamic_entry (DT_RELASZ, 0)
3915 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3919 if ((info->flags & DF_TEXTREL) != 0)
3921 if (!add_dynamic_entry (DT_TEXTREL, 0))
3923 info->flags |= DF_TEXTREL;
3926 #undef add_dynamic_entry
3931 /* This function is called via elf_cris_link_hash_traverse if we are
3932 creating a shared object. In the -Bsymbolic case, it discards the
3933 space allocated to copy PC relative relocs against symbols which
3934 are defined in regular objects. For the normal non-symbolic case,
3935 we also discard space for relocs that have become local due to
3936 symbol visibility changes. We allocated space for them in the
3937 check_relocs routine, but we won't fill them in in the
3938 relocate_section routine. */
3941 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *h,
3944 struct elf_cris_pcrel_relocs_copied *s;
3945 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3947 /* If a symbol has been forced local or we have found a regular
3948 definition for the symbolic link case, then we won't be needing
3950 if (h->root.def_regular
3951 && (h->root.forced_local
3954 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3957 = _bfd_elf_get_dynamic_reloc_section (elf_hash_table (info)
3961 sreloc->size -= s->count * sizeof (Elf32_External_Rela);
3966 /* If we have accounted for PC-relative relocs for read-only
3967 sections, now is the time to warn for them. We can't do it in
3968 cris_elf_check_relocs, because we don't know the status of all
3969 symbols at that time (and it's common to force symbols local
3972 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3973 if ((s->section->flags & SEC_READONLY) != 0)
3975 /* FIXME: How do we make this optionally a warning only? */
3976 (*_bfd_error_handler)
3977 (_("%B, section `%A', to symbol `%s':\n"
3978 " relocation %s should not be used"
3979 " in a shared object; recompile with -fPIC"),
3982 h->root.root.root.string,
3983 cris_elf_howto_table[s->r_type].name);
3985 info->flags |= DF_TEXTREL;
3991 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3992 creating a shared object. We discard space for relocs for symbols put
3993 in the .got, but which we found we do not have to resolve at run-time. */
3996 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *h,
3999 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4001 /* If we're not creating a shared library and have a symbol which is
4002 referred to by .got references, but the symbol is defined locally,
4003 (or rather, not defined by a DSO) then lose the reloc for the .got
4004 (don't allocate room for it). Likewise for relocs for something
4005 for which we create a PLT. */
4006 if (!h->root.def_dynamic
4007 || h->root.plt.refcount > 0)
4009 if (h->reg_got_refcount > 0
4010 /* The size of this section is only valid and in sync with the
4011 various reference counts if we do dynamic; don't decrement it
4013 && elf_hash_table (info)->dynamic_sections_created)
4015 bfd *dynobj = elf_hash_table (info)->dynobj;
4018 BFD_ASSERT (dynobj != NULL);
4020 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4022 BFD_ASSERT (srelgot != NULL);
4024 srelgot->size -= sizeof (Elf32_External_Rela);
4027 /* If the locally-defined symbol isn't used by a DSO, then we don't
4028 have to export it as a dynamic symbol. This was already done for
4029 functions; doing this for all symbols would presumably not
4030 introduce new problems. Of course we don't do this if we're
4031 exporting all dynamic symbols. */
4032 if (! info->export_dynamic
4033 && h->root.dynindx != -1
4034 && !h->root.def_dynamic
4035 && !h->root.ref_dynamic)
4037 h->root.dynindx = -1;
4038 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4039 h->root.dynstr_index);
4046 /* Reject a file depending on presence and expectation of prefixed
4047 underscores on symbols. */
4050 cris_elf_object_p (bfd *abfd)
4052 if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
4055 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
4056 return (bfd_get_symbol_leading_char (abfd) == '_');
4058 return (bfd_get_symbol_leading_char (abfd) == 0);
4061 /* Mark presence or absence of leading underscore. Set machine type
4062 flags from mach type. */
4065 cris_elf_final_write_processing (bfd *abfd,
4066 bfd_boolean linker ATTRIBUTE_UNUSED)
4068 unsigned long e_flags = elf_elfheader (abfd)->e_flags;
4070 e_flags &= ~EF_CRIS_UNDERSCORE;
4071 if (bfd_get_symbol_leading_char (abfd) == '_')
4072 e_flags |= EF_CRIS_UNDERSCORE;
4074 switch (bfd_get_mach (abfd))
4076 case bfd_mach_cris_v0_v10:
4077 e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
4080 case bfd_mach_cris_v10_v32:
4081 e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
4084 case bfd_mach_cris_v32:
4085 e_flags |= EF_CRIS_VARIANT_V32;
4089 _bfd_abort (__FILE__, __LINE__,
4090 _("Unexpected machine number"));
4093 elf_elfheader (abfd)->e_flags = e_flags;
4096 /* Set the mach type from e_flags value. */
4099 cris_elf_set_mach_from_flags (bfd *abfd,
4100 unsigned long flags)
4102 switch (flags & EF_CRIS_VARIANT_MASK)
4104 case EF_CRIS_VARIANT_ANY_V0_V10:
4105 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
4108 case EF_CRIS_VARIANT_V32:
4109 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
4112 case EF_CRIS_VARIANT_COMMON_V10_V32:
4113 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
4117 /* Since we don't recognize them, we obviously can't support them
4118 with this code; we'd have to require that all future handling
4119 would be optional. */
4120 bfd_set_error (bfd_error_wrong_format);
4127 /* Display the flags field. */
4130 cris_elf_print_private_bfd_data (bfd *abfd, void * ptr)
4132 FILE *file = (FILE *) ptr;
4134 BFD_ASSERT (abfd != NULL && ptr != NULL);
4136 _bfd_elf_print_private_bfd_data (abfd, ptr);
4138 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4140 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
4141 fprintf (file, _(" [symbols have a _ prefix]"));
4142 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
4143 == EF_CRIS_VARIANT_COMMON_V10_V32)
4144 fprintf (file, _(" [v10 and v32]"));
4145 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
4146 == EF_CRIS_VARIANT_V32)
4147 fprintf (file, _(" [v32]"));
4153 /* Don't mix files with and without a leading underscore. */
4156 cris_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4160 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4163 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4164 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4167 imach = bfd_get_mach (ibfd);
4169 if (! elf_flags_init (obfd))
4171 /* This happens when ld starts out with a 'blank' output file. */
4172 elf_flags_init (obfd) = TRUE;
4174 /* We ignore the linker-set mach, and instead set it according to
4175 the first input file. This would also happen if we could
4176 somehow filter out the OUTPUT_ARCH () setting from elf.sc.
4177 This allows us to keep the same linker config across
4178 cris(v0..v10) and crisv32. The drawback is that we can't force
4179 the output type, which might be a sane thing to do for a
4180 v10+v32 compatibility object. */
4181 if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
4185 if (bfd_get_symbol_leading_char (ibfd)
4186 != bfd_get_symbol_leading_char (obfd))
4188 (*_bfd_error_handler)
4189 (bfd_get_symbol_leading_char (ibfd) == '_'
4190 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
4191 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
4193 bfd_set_error (bfd_error_bad_value);
4197 omach = bfd_get_mach (obfd);
4201 /* We can get an incompatible combination only if either is
4202 bfd_mach_cris_v32, and the other one isn't compatible. */
4203 if ((imach == bfd_mach_cris_v32
4204 && omach != bfd_mach_cris_v10_v32)
4205 || (omach == bfd_mach_cris_v32
4206 && imach != bfd_mach_cris_v10_v32))
4208 (*_bfd_error_handler)
4209 ((imach == bfd_mach_cris_v32)
4210 ? _("%B contains CRIS v32 code, incompatible"
4211 " with previous objects")
4212 : _("%B contains non-CRIS-v32 code, incompatible"
4213 " with previous objects"),
4215 bfd_set_error (bfd_error_bad_value);
4219 /* We don't have to check the case where the input is compatible
4220 with v10 and v32, because the output is already known to be set
4221 to the other (compatible) mach. */
4222 if (omach == bfd_mach_cris_v10_v32
4223 && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
4230 /* Do side-effects of e_flags copying to obfd. */
4233 cris_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4235 /* Call the base function. */
4236 if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
4239 /* If output is big-endian for some obscure reason, stop here. */
4240 if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
4243 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4244 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4247 /* Do what we really came here for. */
4248 return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
4251 static enum elf_reloc_type_class
4252 elf_cris_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4253 const asection *rel_sec ATTRIBUTE_UNUSED,
4254 const Elf_Internal_Rela *rela)
4256 enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
4259 case R_CRIS_RELATIVE:
4260 return reloc_class_relative;
4261 case R_CRIS_JUMP_SLOT:
4262 return reloc_class_plt;
4264 return reloc_class_copy;
4266 return reloc_class_normal;
4270 /* The elf_backend_got_elt_size worker. For one symbol, we can have up to
4271 two GOT entries from three types with two different sizes. We handle
4272 it as a single entry, so we can use the regular offset-calculation
4276 elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
4277 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4278 struct elf_link_hash_entry *hr,
4280 unsigned long symndx)
4282 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
4285 /* We may have one regular GOT entry or up to two TLS GOT
4289 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4290 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
4292 BFD_ASSERT (local_got_refcounts != NULL);
4294 if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
4296 /* We can't have a variable referred to both as a regular
4297 variable and through TLS relocs. */
4298 BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
4299 && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
4303 if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
4306 if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
4311 struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
4312 if (hh->reg_got_refcount > 0)
4314 /* The actual error-on-input is emitted elsewhere. */
4315 BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
4319 if (hh->dtp_refcount > 0)
4322 if (hh->tprel_refcount > 0)
4326 /* We're only called when h->got.refcount is non-zero, so we must
4327 have a non-zero size. */
4328 BFD_ASSERT (eltsiz != 0);
4332 #define ELF_ARCH bfd_arch_cris
4333 #define ELF_TARGET_ID CRIS_ELF_DATA
4334 #define ELF_MACHINE_CODE EM_CRIS
4335 #define ELF_MAXPAGESIZE 0x2000
4337 #define TARGET_LITTLE_SYM cris_elf32_vec
4338 #define TARGET_LITTLE_NAME "elf32-cris"
4339 #define elf_symbol_leading_char 0
4341 #define elf_info_to_howto_rel NULL
4342 #define elf_info_to_howto cris_info_to_howto_rela
4343 #define elf_backend_relocate_section cris_elf_relocate_section
4344 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
4345 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
4346 #define elf_backend_plt_sym_val cris_elf_plt_sym_val
4347 #define elf_backend_check_relocs cris_elf_check_relocs
4348 #define elf_backend_grok_prstatus cris_elf_grok_prstatus
4349 #define elf_backend_grok_psinfo cris_elf_grok_psinfo
4351 #define elf_backend_can_gc_sections 1
4352 #define elf_backend_can_refcount 1
4354 #define elf_backend_object_p cris_elf_object_p
4355 #define elf_backend_final_write_processing \
4356 cris_elf_final_write_processing
4357 #define bfd_elf32_bfd_print_private_bfd_data \
4358 cris_elf_print_private_bfd_data
4359 #define bfd_elf32_bfd_merge_private_bfd_data \
4360 cris_elf_merge_private_bfd_data
4361 #define bfd_elf32_bfd_copy_private_bfd_data \
4362 cris_elf_copy_private_bfd_data
4364 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
4365 #define bfd_elf32_bfd_reloc_name_lookup cris_reloc_name_lookup
4367 #define bfd_elf32_bfd_link_hash_table_create \
4368 elf_cris_link_hash_table_create
4369 #define elf_backend_adjust_dynamic_symbol \
4370 elf_cris_adjust_dynamic_symbol
4371 #define elf_backend_copy_indirect_symbol \
4372 elf_cris_copy_indirect_symbol
4373 #define elf_backend_size_dynamic_sections \
4374 elf_cris_size_dynamic_sections
4375 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4376 #define elf_backend_finish_dynamic_symbol \
4377 elf_cris_finish_dynamic_symbol
4378 #define elf_backend_finish_dynamic_sections \
4379 elf_cris_finish_dynamic_sections
4380 #define elf_backend_create_dynamic_sections \
4381 _bfd_elf_create_dynamic_sections
4382 #define bfd_elf32_bfd_final_link \
4383 bfd_elf_gc_common_final_link
4384 #define elf_backend_hide_symbol elf_cris_hide_symbol
4385 #define elf_backend_reloc_type_class elf_cris_reloc_type_class
4387 #define elf_backend_want_got_plt 1
4388 #define elf_backend_plt_readonly 1
4389 #define elf_backend_want_plt_sym 0
4390 #define elf_backend_got_header_size 12
4391 #define elf_backend_got_elt_size elf_cris_got_elt_size
4393 /* Later, we my want to optimize RELA entries into REL entries for dynamic
4394 linking and libraries (if it's a win of any significance). Until then,
4395 take the easy route. */
4396 #define elf_backend_may_use_rel_p 0
4397 #define elf_backend_may_use_rela_p 1
4398 #define elf_backend_rela_normal 1
4400 #include "elf32-target.h"
4402 #undef TARGET_LITTLE_SYM
4403 #undef TARGET_LITTLE_NAME
4404 #undef elf_symbol_leading_char
4406 #define TARGET_LITTLE_SYM cris_elf32_us_vec
4407 #define TARGET_LITTLE_NAME "elf32-us-cris"
4408 #define elf_symbol_leading_char '_'
4410 #define elf32_bed elf32_us_cris_bed
4412 #include "elf32-target.h"