Support for more than 64k ELF sections.
[binutils.git] / bfd / elf32-cris.c
blob6ee0d9cccf1190e28d01e201b28b4aa5ded55a0e
1 /* CRIS-specific support for 32-bit ELF.
2 Copyright 2000, 2001 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/cris.h"
29 /* Forward declarations. */
30 static reloc_howto_type * cris_reloc_type_lookup
31 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
33 static void cris_info_to_howto_rela
34 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
36 static boolean cris_elf_grok_prstatus
37 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
39 static boolean cris_elf_grok_psinfo
40 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
42 static boolean cris_elf_relocate_section
43 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
44 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
46 static bfd_reloc_status_type cris_final_link_relocate
47 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
48 Elf_Internal_Rela *, bfd_vma));
50 static boolean cris_elf_gc_sweep_hook
51 PARAMS ((bfd *, struct bfd_link_info *, asection *,
52 const Elf_Internal_Rela *));
54 static asection * cris_elf_gc_mark_hook
55 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
56 struct elf_link_hash_entry *, Elf_Internal_Sym *));
58 static boolean cris_elf_object_p PARAMS ((bfd *));
60 static void cris_elf_final_write_processing PARAMS ((bfd *, boolean));
62 static boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
64 static boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
66 struct elf_cris_link_hash_entry;
67 static boolean elf_cris_discard_excess_dso_dynamics
68 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static boolean elf_cris_discard_excess_program_dynamics
70 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
71 static boolean elf_cris_adjust_gotplt_to_got
72 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
73 static boolean elf_cris_try_fold_plt_to_got
74 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
75 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
76 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
77 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
78 PARAMS ((bfd *));
79 static boolean elf_cris_adjust_dynamic_symbol
80 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
81 static boolean cris_elf_check_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection *,
83 const Elf_Internal_Rela *));
85 static boolean elf_cris_size_dynamic_sections
86 PARAMS ((bfd *, struct bfd_link_info *));
87 static boolean elf_cris_finish_dynamic_symbol
88 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
89 Elf_Internal_Sym *));
90 static boolean elf_cris_finish_dynamic_sections
91 PARAMS ((bfd *, struct bfd_link_info *));
92 static void elf_cris_hide_symbol
93 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
94 static enum elf_reloc_type_class elf_cris_reloc_type_class
95 PARAMS ((const Elf_Internal_Rela *));
97 static reloc_howto_type cris_elf_howto_table [] =
99 /* This reloc does nothing. */
100 HOWTO (R_CRIS_NONE, /* type */
101 0, /* rightshift */
102 2, /* size (0 = byte, 1 = short, 2 = long) */
103 32, /* bitsize */
104 false, /* pc_relative */
105 0, /* bitpos */
106 complain_overflow_bitfield, /* complain_on_overflow */
107 bfd_elf_generic_reloc, /* special_function */
108 "R_CRIS_NONE", /* name */
109 false, /* partial_inplace */
110 0, /* src_mask */
111 0, /* dst_mask */
112 false), /* pcrel_offset */
114 /* An 8 bit absolute relocation. */
115 HOWTO (R_CRIS_8, /* type */
116 0, /* rightshift */
117 0, /* size (0 = byte, 1 = short, 2 = long) */
118 8, /* bitsize */
119 false, /* pc_relative */
120 0, /* bitpos */
121 complain_overflow_bitfield, /* complain_on_overflow */
122 bfd_elf_generic_reloc, /* special_function */
123 "R_CRIS_8", /* name */
124 false, /* partial_inplace */
125 0x0000, /* src_mask */
126 0x00ff, /* dst_mask */
127 false), /* pcrel_offset */
129 /* A 16 bit absolute relocation. */
130 HOWTO (R_CRIS_16, /* type */
131 0, /* rightshift */
132 1, /* size (0 = byte, 1 = short, 2 = long) */
133 16, /* bitsize */
134 false, /* pc_relative */
135 0, /* bitpos */
136 complain_overflow_bitfield, /* complain_on_overflow */
137 bfd_elf_generic_reloc, /* special_function */
138 "R_CRIS_16", /* name */
139 false, /* partial_inplace */
140 0x00000000, /* src_mask */
141 0x0000ffff, /* dst_mask */
142 false), /* pcrel_offset */
144 /* A 32 bit absolute relocation. */
145 HOWTO (R_CRIS_32, /* type */
146 0, /* rightshift */
147 2, /* size (0 = byte, 1 = short, 2 = long) */
148 32, /* bitsize */
149 false, /* pc_relative */
150 0, /* bitpos */
151 complain_overflow_bitfield, /* complain_on_overflow */
152 bfd_elf_generic_reloc, /* special_function */
153 "R_CRIS_32", /* name */
154 false, /* partial_inplace */
155 0x00000000, /* src_mask */
156 0xffffffff, /* dst_mask */
157 false), /* pcrel_offset */
159 /* An 8 bit PC-relative relocation. */
160 HOWTO (R_CRIS_8_PCREL, /* type */
161 0, /* rightshift */
162 0, /* size (0 = byte, 1 = short, 2 = long) */
163 8, /* bitsize */
164 true, /* pc_relative */
165 0, /* bitpos */
166 complain_overflow_bitfield, /* complain_on_overflow */
167 bfd_elf_generic_reloc, /* special_function */
168 "R_CRIS_8_PCREL", /* name */
169 false, /* partial_inplace */
170 0x0000, /* src_mask */
171 0x00ff, /* dst_mask */
172 true), /* pcrel_offset */
174 /* A 16 bit PC-relative relocation. */
175 HOWTO (R_CRIS_16_PCREL, /* type */
176 0, /* rightshift */
177 1, /* size (0 = byte, 1 = short, 2 = long) */
178 16, /* bitsize */
179 true, /* pc_relative */
180 0, /* bitpos */
181 complain_overflow_bitfield, /* complain_on_overflow */
182 bfd_elf_generic_reloc, /* special_function */
183 "R_CRIS_16", /* name */
184 false, /* partial_inplace */
185 0x00000000, /* src_mask */
186 0x0000ffff, /* dst_mask */
187 true), /* pcrel_offset */
189 /* A 32 bit PC-relative relocation. */
190 HOWTO (R_CRIS_32_PCREL, /* type */
191 0, /* rightshift */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
193 32, /* bitsize */
194 true, /* pc_relative */
195 0, /* bitpos */
196 complain_overflow_bitfield, /* complain_on_overflow */
197 bfd_elf_generic_reloc, /* special_function */
198 "R_CRIS_32", /* name */
199 false, /* partial_inplace */
200 0x00000000, /* src_mask */
201 0xffffffff, /* dst_mask */
202 true), /* pcrel_offset */
204 /* GNU extension to record C++ vtable hierarchy. */
205 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
206 0, /* rightshift */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
208 0, /* bitsize */
209 false, /* pc_relative */
210 0, /* bitpos */
211 complain_overflow_dont, /* complain_on_overflow */
212 NULL, /* special_function */
213 "R_CRIS_GNU_VTINHERIT", /* name */
214 false, /* partial_inplace */
215 0, /* src_mask */
216 0, /* dst_mask */
217 false), /* pcrel_offset */
219 /* GNU extension to record C++ vtable member usage. */
220 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
221 0, /* rightshift */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
223 0, /* bitsize */
224 false, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_dont, /* complain_on_overflow */
227 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
228 "R_CRIS_GNU_VTENTRY", /* name */
229 false, /* partial_inplace */
230 0, /* src_mask */
231 0, /* dst_mask */
232 false), /* pcrel_offset */
234 /* This is used only by the dynamic linker. The symbol should exist
235 both in the object being run and in some shared library. The
236 dynamic linker copies the data addressed by the symbol from the
237 shared library into the object, because the object being
238 run has to have the data at some particular address. */
239 HOWTO (R_CRIS_COPY, /* type */
240 0, /* rightshift */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
242 32, /* bitsize */
243 false, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 bfd_elf_generic_reloc, /* special_function */
247 "R_CRIS_COPY", /* name */
248 false, /* partial_inplace */
249 0, /* src_mask */
250 0, /* dst_mask */
251 false), /* pcrel_offset */
253 /* Like R_CRIS_32, but used when setting global offset table entries. */
254 HOWTO (R_CRIS_GLOB_DAT, /* type */
255 0, /* rightshift */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
257 32, /* bitsize */
258 false, /* pc_relative */
259 0, /* bitpos */
260 complain_overflow_bitfield, /* complain_on_overflow */
261 bfd_elf_generic_reloc, /* special_function */
262 "R_CRIS_GLOB_DAT", /* name */
263 false, /* partial_inplace */
264 0, /* src_mask */
265 0xffffffff, /* dst_mask */
266 false), /* pcrel_offset */
268 /* Marks a procedure linkage table entry for a symbol. */
269 HOWTO (R_CRIS_JUMP_SLOT, /* type */
270 0, /* rightshift */
271 2, /* size (0 = byte, 1 = short, 2 = long) */
272 32, /* bitsize */
273 false, /* pc_relative */
274 0, /* bitpos */
275 complain_overflow_bitfield, /* complain_on_overflow */
276 bfd_elf_generic_reloc, /* special_function */
277 "R_CRIS_JUMP_SLOT", /* name */
278 false, /* partial_inplace */
279 0, /* src_mask */
280 0, /* dst_mask */
281 false), /* pcrel_offset */
283 /* Used only by the dynamic linker. When the object is run, this
284 longword is set to the load address of the object, plus the
285 addend. */
286 HOWTO (R_CRIS_RELATIVE, /* type */
287 0, /* rightshift */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
289 32, /* bitsize */
290 false, /* pc_relative */
291 0, /* bitpos */
292 complain_overflow_bitfield, /* complain_on_overflow */
293 bfd_elf_generic_reloc, /* special_function */
294 "R_CRIS_RELATIVE", /* name */
295 false, /* partial_inplace */
296 0, /* src_mask */
297 0xffffffff, /* dst_mask */
298 false), /* pcrel_offset */
300 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
301 HOWTO (R_CRIS_16_GOT, /* type */
302 0, /* rightshift */
303 1, /* size (0 = byte, 1 = short, 2 = long) */
304 16, /* bitsize */
305 false, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_bitfield, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_CRIS_16_GOT", /* name */
310 false, /* partial_inplace */
311 0, /* src_mask */
312 0xffff, /* dst_mask */
313 false), /* pcrel_offset */
315 HOWTO (R_CRIS_32_GOT, /* type */
316 0, /* rightshift */
317 2, /* size (0 = byte, 1 = short, 2 = long) */
318 32, /* bitsize */
319 false, /* pc_relative */
320 0, /* bitpos */
321 complain_overflow_bitfield, /* complain_on_overflow */
322 bfd_elf_generic_reloc, /* special_function */
323 "R_CRIS_32_GOT", /* name */
324 false, /* partial_inplace */
325 0, /* src_mask */
326 0xffffffff, /* dst_mask */
327 false), /* pcrel_offset */
329 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
330 the GOT table for the symbol. */
331 HOWTO (R_CRIS_16_GOTPLT, /* type */
332 0, /* rightshift */
333 1, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 false, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_bitfield, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_CRIS_16_GOTPLT", /* name */
340 false, /* partial_inplace */
341 0, /* src_mask */
342 0xffff, /* dst_mask */
343 false), /* pcrel_offset */
345 HOWTO (R_CRIS_32_GOTPLT, /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 32, /* bitsize */
349 false, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_bitfield, /* complain_on_overflow */
352 bfd_elf_generic_reloc, /* special_function */
353 "R_CRIS_32_GOTPLT", /* name */
354 false, /* partial_inplace */
355 0, /* src_mask */
356 0xffffffff, /* dst_mask */
357 false), /* pcrel_offset */
359 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
360 be necessary. */
361 HOWTO (R_CRIS_32_GOTREL, /* type */
362 0, /* rightshift */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
364 32, /* bitsize */
365 false, /* pc_relative */
366 0, /* bitpos */
367 complain_overflow_bitfield, /* complain_on_overflow */
368 bfd_elf_generic_reloc, /* special_function */
369 "R_CRIS_32_GOTREL", /* name */
370 false, /* partial_inplace */
371 0, /* src_mask */
372 0xffffffff, /* dst_mask */
373 false), /* pcrel_offset */
375 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
376 to create PLT entry for symbol. */
377 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 32, /* bitsize */
381 false, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_bitfield, /* complain_on_overflow */
384 bfd_elf_generic_reloc, /* special_function */
385 "R_CRIS_32_PLT_GOTREL", /* name */
386 false, /* partial_inplace */
387 0, /* src_mask */
388 0xffffffff, /* dst_mask */
389 false), /* pcrel_offset */
391 /* A 32-bit offset from PC (location after the relocation) + addend to
392 entry for this symbol in PLT and request to create PLT entry for
393 symbol. */
394 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 32, /* bitsize */
398 true, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_bitfield, /* complain_on_overflow */
401 bfd_elf_generic_reloc, /* special_function */
402 "R_CRIS_32_PLT_PCREL", /* name */
403 false, /* partial_inplace */
404 0, /* src_mask */
405 0xffffffff, /* dst_mask */
406 true) /* pcrel_offset */
409 /* Map BFD reloc types to CRIS ELF reloc types. */
411 struct cris_reloc_map
413 bfd_reloc_code_real_type bfd_reloc_val;
414 unsigned int cris_reloc_val;
417 static const struct cris_reloc_map cris_reloc_map [] =
419 { BFD_RELOC_NONE, R_CRIS_NONE },
420 { BFD_RELOC_8, R_CRIS_8 },
421 { BFD_RELOC_16, R_CRIS_16 },
422 { BFD_RELOC_32, R_CRIS_32 },
423 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
424 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
425 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
426 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
427 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
428 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
429 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
430 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
431 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
432 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
433 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
434 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
435 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
436 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
437 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
438 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
441 static reloc_howto_type *
442 cris_reloc_type_lookup (abfd, code)
443 bfd * abfd ATTRIBUTE_UNUSED;
444 bfd_reloc_code_real_type code;
446 unsigned int i;
448 for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
449 if (cris_reloc_map [i].bfd_reloc_val == code)
450 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
452 return NULL;
455 /* Set the howto pointer for an CRIS ELF reloc. */
457 static void
458 cris_info_to_howto_rela (abfd, cache_ptr, dst)
459 bfd * abfd ATTRIBUTE_UNUSED;
460 arelent * cache_ptr;
461 Elf32_Internal_Rela * dst;
463 unsigned int r_type;
465 r_type = ELF32_R_TYPE (dst->r_info);
466 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
467 cache_ptr->howto = & cris_elf_howto_table [r_type];
470 /* Support for core dump NOTE sections. */
472 static boolean
473 cris_elf_grok_prstatus (abfd, note)
474 bfd *abfd;
475 Elf_Internal_Note *note;
477 int offset;
478 size_t raw_size;
480 switch (note->descsz)
482 default:
483 return false;
485 case 214: /* Linux/CRIS */
486 /* pr_cursig */
487 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
489 /* pr_pid */
490 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
492 /* pr_reg */
493 offset = 70;
494 raw_size = 140;
496 break;
499 /* Make a ".reg/999" section. */
500 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
501 raw_size, note->descpos + offset);
504 static boolean
505 cris_elf_grok_psinfo (abfd, note)
506 bfd *abfd;
507 Elf_Internal_Note *note;
509 switch (note->descsz)
511 default:
512 return false;
514 case 124: /* Linux/CRIS elf_prpsinfo */
515 elf_tdata (abfd)->core_program
516 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
517 elf_tdata (abfd)->core_command
518 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
521 /* Note that for some reason, a spurious space is tacked
522 onto the end of the args in some (at least one anyway)
523 implementations, so strip it off if it exists. */
526 char *command = elf_tdata (abfd)->core_command;
527 int n = strlen (command);
529 if (0 < n && command[n - 1] == ' ')
530 command[n - 1] = '\0';
533 return true;
536 /* The name of the dynamic interpreter. This is put in the .interp
537 section. */
539 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
541 /* The size in bytes of an entry in the procedure linkage table. */
543 #define PLT_ENTRY_SIZE 20
545 /* The first entry in an absolute procedure linkage table looks like this. */
547 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
549 0xfc, 0xe1,
550 0x7e, 0x7e, /* push mof. */
551 0x7f, 0x0d, /* (dip [pc+]) */
552 0, 0, 0, 0, /* Replaced with address of .got + 4. */
553 0x30, 0x7a, /* move [...],mof */
554 0x7f, 0x0d, /* (dip [pc+]) */
555 0, 0, 0, 0, /* Replaced with address of .got + 8. */
556 0x30, 0x09, /* jump [...] */
559 /* Subsequent entries in an absolute procedure linkage table look like
560 this. */
562 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
564 0x7f, 0x0d, /* (dip [pc+]) */
565 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
566 0x30, 0x09, /* jump [...] */
567 0x3f, 0x7e, /* move [pc+],mof */
568 0, 0, 0, 0, /* Replaced with offset into relocation table. */
569 0x2f, 0xfe, /* add.d [pc+],pc */
570 0xec, 0xff,
571 0xff, 0xff /* Replaced with offset to start of .plt. */
574 /* The first entry in a PIC procedure linkage table looks like this. */
576 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
578 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
579 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
580 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
581 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
584 /* Subsequent entries in a PIC procedure linkage table look like this. */
586 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
588 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
589 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
590 0x30, 0x09, /* jump [...] */
591 0x3f, 0x7e, /* move [pc+],mof */
592 0, 0, 0, 0, /* Replaced with offset into relocation table. */
593 0x2f, 0xfe, /* add.d [pc+],pc */
594 0xec, 0xff, /* Replaced with offset to start of .plt. */
595 0xff, 0xff
598 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
599 (and most other PIC/shlib stuff). Check that we don't drift away
600 without reason.
602 The CRIS linker, like the m68k and i386 linkers (and probably the rest
603 too) needs to keep track of the number of relocs that it decides to
604 copy in check_relocs for each symbol. This is so that it can discard
605 PC relative relocs if it doesn't need them when linking with
606 -Bsymbolic. We store the information in a field extending the regular
607 ELF linker hash table. */
609 /* This structure keeps track of the number of PC relative relocs we have
610 copied for a given symbol. */
612 struct elf_cris_pcrel_relocs_copied
614 /* Next section. */
615 struct elf_cris_pcrel_relocs_copied *next;
616 /* A section in dynobj. */
617 asection *section;
618 /* Number of relocs copied in this section. */
619 bfd_size_type count;
622 /* CRIS ELF linker hash entry. */
624 struct elf_cris_link_hash_entry
626 struct elf_link_hash_entry root;
628 /* Number of PC relative relocs copied for this symbol. */
629 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
631 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
632 both a general GOT and a PLT-specific GOT entry for the same symbol,
633 when it is referenced both as a function and as a function pointer.
635 Number of GOTPLT references for a function. */
636 bfd_signed_vma gotplt_refcount;
638 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
639 (zero is never used as an index). FIXME: We should be able to fold
640 this with gotplt_refcount in a union, like the got and plt unions in
641 elf_link_hash_entry. */
642 bfd_size_type gotplt_offset;
645 /* CRIS ELF linker hash table. */
647 struct elf_cris_link_hash_table
649 struct elf_link_hash_table root;
651 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
652 since we try and avoid creating GOTPLT:s when there's already a GOT.
653 Instead, we keep and update the next available index here. */
654 bfd_size_type next_gotplt_entry;
657 /* Traverse a CRIS ELF linker hash table. */
659 #define elf_cris_link_hash_traverse(table, func, info) \
660 (elf_link_hash_traverse \
661 (&(table)->root, \
662 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
663 (info)))
665 /* Get the CRIS ELF linker hash table from a link_info structure. */
667 #define elf_cris_hash_table(p) \
668 ((struct elf_cris_link_hash_table *) (p)->hash)
670 /* Create an entry in a CRIS ELF linker hash table. */
672 static struct bfd_hash_entry *
673 elf_cris_link_hash_newfunc (entry, table, string)
674 struct bfd_hash_entry *entry;
675 struct bfd_hash_table *table;
676 const char *string;
678 struct elf_cris_link_hash_entry *ret =
679 (struct elf_cris_link_hash_entry *) entry;
681 /* Allocate the structure if it has not already been allocated by a
682 subclass. */
683 if (ret == (struct elf_cris_link_hash_entry *) NULL)
684 ret = ((struct elf_cris_link_hash_entry *)
685 bfd_hash_allocate (table,
686 sizeof (struct elf_cris_link_hash_entry)));
687 if (ret == (struct elf_cris_link_hash_entry *) NULL)
688 return (struct bfd_hash_entry *) ret;
690 /* Call the allocation method of the superclass. */
691 ret = ((struct elf_cris_link_hash_entry *)
692 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
693 table, string));
694 if (ret != (struct elf_cris_link_hash_entry *) NULL)
696 ret->pcrel_relocs_copied = NULL;
697 ret->gotplt_refcount = 0;
698 ret->gotplt_offset = 0;
701 return (struct bfd_hash_entry *) ret;
704 /* Create a CRIS ELF linker hash table. */
706 static struct bfd_link_hash_table *
707 elf_cris_link_hash_table_create (abfd)
708 bfd *abfd;
710 struct elf_cris_link_hash_table *ret;
711 bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
713 ret = ((struct elf_cris_link_hash_table *) bfd_alloc (abfd, amt));
714 if (ret == (struct elf_cris_link_hash_table *) NULL)
715 return NULL;
717 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
718 elf_cris_link_hash_newfunc))
720 bfd_release (abfd, ret);
721 return NULL;
724 /* Initialize to skip over the first three entries in the gotplt; they
725 are used for run-time symbol evaluation. */
726 ret->next_gotplt_entry = 12;
728 return &ret->root.root;
731 /* Perform a single relocation. By default we use the standard BFD
732 routines, with a few tweaks. */
734 static bfd_reloc_status_type
735 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
736 relocation)
737 reloc_howto_type * howto;
738 bfd * input_bfd;
739 asection * input_section;
740 bfd_byte * contents;
741 Elf_Internal_Rela * rel;
742 bfd_vma relocation;
744 bfd_reloc_status_type r;
746 /* PC-relative relocations are relative to the position *after*
747 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
748 not a single byte, since PC must be 16-bit-aligned. */
749 switch (ELF32_R_TYPE (rel->r_info))
751 /* Check that the 16-bit GOT relocs are positive. */
752 case R_CRIS_16_GOTPLT:
753 case R_CRIS_16_GOT:
754 if ((bfd_signed_vma) relocation < 0)
755 return bfd_reloc_overflow;
756 break;
758 case R_CRIS_32_PLT_PCREL:
759 case R_CRIS_32_PCREL:
760 relocation -= 2;
761 /* Fall through. */
762 case R_CRIS_8_PCREL:
763 case R_CRIS_16_PCREL:
764 relocation -= 2;
765 break;
767 default:
768 break;
771 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
772 contents, rel->r_offset,
773 relocation, rel->r_addend);
774 return r;
777 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
778 copied, for further comments. */
780 static boolean
781 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
782 contents, relocs, local_syms, local_sections)
783 bfd * output_bfd ATTRIBUTE_UNUSED;
784 struct bfd_link_info * info;
785 bfd * input_bfd;
786 asection * input_section;
787 bfd_byte * contents;
788 Elf_Internal_Rela * relocs;
789 Elf_Internal_Sym * local_syms;
790 asection ** local_sections;
792 bfd *dynobj;
793 Elf_Internal_Shdr * symtab_hdr;
794 struct elf_link_hash_entry ** sym_hashes;
795 bfd_vma *local_got_offsets;
796 asection *sgot;
797 asection *splt;
798 asection *sreloc;
799 Elf_Internal_Rela * rel;
800 Elf_Internal_Rela * relend;
802 dynobj = elf_hash_table (info)->dynobj;
803 local_got_offsets = elf_local_got_offsets (input_bfd);
804 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
805 sym_hashes = elf_sym_hashes (input_bfd);
806 relend = relocs + input_section->reloc_count;
808 /* It seems this can happen with erroneous or unsupported input (mixing
809 a.out and elf in an archive, for example.) */
810 if (sym_hashes == NULL)
811 return false;
813 sgot = NULL;
814 splt = NULL;
815 sreloc = NULL;
817 if (dynobj != NULL)
819 splt = bfd_get_section_by_name (dynobj, ".plt");
820 sgot = bfd_get_section_by_name (dynobj, ".got");
823 for (rel = relocs; rel < relend; rel ++)
825 reloc_howto_type * howto;
826 unsigned long r_symndx;
827 Elf_Internal_Sym * sym;
828 asection * sec;
829 struct elf_link_hash_entry * h;
830 bfd_vma relocation;
831 bfd_reloc_status_type r;
832 const char * symname = NULL;
833 int r_type;
835 r_type = ELF32_R_TYPE (rel->r_info);
837 if ( r_type == R_CRIS_GNU_VTINHERIT
838 || r_type == R_CRIS_GNU_VTENTRY)
839 continue;
841 r_symndx = ELF32_R_SYM (rel->r_info);
843 if (info->relocateable)
845 /* This is a relocateable link. We don't have to change
846 anything, unless the reloc is against a section symbol,
847 in which case we have to adjust according to where the
848 section symbol winds up in the output section. */
849 if (r_symndx < symtab_hdr->sh_info)
851 sym = local_syms + r_symndx;
853 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
855 sec = local_sections [r_symndx];
856 rel->r_addend += sec->output_offset + sym->st_value;
860 continue;
863 /* This is a final link. */
864 howto = cris_elf_howto_table + r_type;
865 h = NULL;
866 sym = NULL;
867 sec = NULL;
869 if (r_symndx < symtab_hdr->sh_info)
871 sym = local_syms + r_symndx;
872 sec = local_sections [r_symndx];
873 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
875 symname = (bfd_elf_string_from_elf_section
876 (input_bfd, symtab_hdr->sh_link, sym->st_name));
877 if (symname == NULL)
878 symname = bfd_section_name (input_bfd, sec);
880 else
882 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
884 while (h->root.type == bfd_link_hash_indirect
885 || h->root.type == bfd_link_hash_warning)
886 h = (struct elf_link_hash_entry *) h->root.u.i.link;
888 symname = h->root.root.string;
890 if (h->root.type == bfd_link_hash_defined
891 || h->root.type == bfd_link_hash_defweak)
893 sec = h->root.u.def.section;
895 /* Perhaps we should detect the cases that
896 sec->output_section is expected to be NULL like i386 and
897 m68k, but apparently (and according to elfxx-ia64.c) all
898 valid cases are where the symbol is defined in a shared
899 object which we link dynamically against. This includes
900 PLT relocs for which we've created a PLT entry and other
901 relocs for which we're prepared to create dynamic
902 relocations.
904 For now, new situations cause us to just err when
905 sec->output_offset is NULL but the object with the symbol
906 is *not* dynamically linked against. Thus this will
907 automatically remind us so we can see if there are other
908 valid cases we need to revisit. */
909 if ((sec->output_section == NULL
910 && (sec->owner->flags & DYNAMIC) != 0)
912 /* Here follow the cases where the relocation value must
913 be zero (or when further handling is simplified when
914 zero). I can't claim to understand the various
915 conditions and they weren't described in the files
916 where I copied them from (elf32-m68k.c and
917 elf32-i386.c), but let's mention examples of where
918 they happen. FIXME: Perhaps define and use a
919 dynamic_symbol_p function like ia64.
921 - When creating a shared library, we can have an
922 ordinary relocation for a symbol defined in a shared
923 library (perhaps the one we create). We then make
924 the relocation value zero, as the value seen now will
925 be added into the relocation addend in this shared
926 library, but must be handled only at dynamic-link
927 time. FIXME: Not sure this example covers the
928 h->elf_link_hash_flags test, though it's there in
929 other targets. */
930 || (info->shared
931 && ((! info->symbolic && h->dynindx != -1)
932 || (h->elf_link_hash_flags
933 & ELF_LINK_HASH_DEF_REGULAR) == 0)
934 && (input_section->flags & SEC_ALLOC) != 0
935 && (r_type == R_CRIS_8
936 || r_type == R_CRIS_16
937 || r_type == R_CRIS_32
938 || r_type == R_CRIS_8_PCREL
939 || r_type == R_CRIS_16_PCREL
940 || r_type == R_CRIS_32_PCREL)))
941 relocation = 0;
942 else if (sec->output_section != NULL)
943 relocation = (h->root.u.def.value
944 + sec->output_section->vma
945 + sec->output_offset);
946 else
948 (*_bfd_error_handler)
949 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
950 bfd_archive_filename (input_bfd),
951 cris_elf_howto_table[r_type].name,
952 symname,
953 bfd_get_section_name (input_bfd, input_section));
954 bfd_set_error (bfd_error_bad_value);
955 return false;
958 else if (h->root.type == bfd_link_hash_undefweak)
959 relocation = 0;
960 else if (info->shared
961 && (!info->symbolic || info->allow_shlib_undefined)
962 && !info->no_undefined
963 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
964 relocation = 0;
965 else
967 if (!(info->callbacks->undefined_symbol
968 (info, symname, input_bfd,
969 input_section, rel->r_offset,
970 (!info->shared || info->no_undefined
971 || ELF_ST_VISIBILITY (h->other)))))
972 return false;
973 relocation = 0;
977 switch (r_type)
979 case R_CRIS_16_GOTPLT:
980 case R_CRIS_32_GOTPLT:
981 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
982 but we require a PLT, and the PLT handling will take care of
983 filling in the PLT-specific GOT entry. For the GOT offset,
984 calculate it as we do when filling it in for the .got.plt
985 section. If we don't have a PLT, punt to GOT handling. */
986 if (h != NULL
987 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
989 asection *sgotplt
990 = bfd_get_section_by_name (dynobj, ".got.plt");
991 bfd_vma got_offset;
993 BFD_ASSERT (h->dynindx != -1);
994 BFD_ASSERT (sgotplt != NULL);
996 got_offset
997 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
999 relocation = got_offset;
1000 break;
1003 /* We didn't make a PLT entry for this symbol. Maybe everything is
1004 folded into the GOT. Other than folding, this happens when
1005 statically linking PIC code, or when using -Bsymbolic. Check
1006 that we instead have a GOT entry as done for us by
1007 elf_cris_adjust_dynamic_symbol, and drop through into the
1008 ordinary GOT cases. */
1009 if (h != NULL && h->got.offset == (bfd_vma) -1)
1011 (*_bfd_error_handler)
1012 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
1013 bfd_archive_filename (input_bfd),
1014 cris_elf_howto_table[r_type].name,
1015 symname[0] != '\0' ? symname : _("[whose name is lost]"),
1016 bfd_get_section_name (input_bfd, input_section));
1018 /* FIXME: Perhaps blaming input is not the right thing to
1019 do; this is probably an internal error. But it is true
1020 that we didn't like that particular input. */
1021 bfd_set_error (bfd_error_bad_value);
1022 return false;
1024 /* Fall through. */
1026 /* The size of the actual relocation is not used here; we only
1027 fill in the GOT table here. */
1028 case R_CRIS_16_GOT:
1029 case R_CRIS_32_GOT:
1031 bfd_vma off;
1033 /* Note that despite using RELA relocations, the .got contents
1034 is always filled in with the link-relative relocation
1035 value; the addend. */
1037 if (h != NULL)
1039 off = h->got.offset;
1040 BFD_ASSERT (off != (bfd_vma) -1);
1042 if (!elf_hash_table (info)->dynamic_sections_created
1043 || (! info->shared && h->dynindx == -1)
1044 || (info->shared
1045 && (info->symbolic || h->dynindx == -1)
1046 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1048 /* This wasn't checked above for ! info->shared, but
1049 must hold there if we get here; the symbol must not
1050 be used in, or defined by a DSO. (Note that
1051 checking for ELF_LINK_HASH_DEF_REGULAR doesn't
1052 catch all cases.) */
1053 BFD_ASSERT (info->shared
1054 || (h->elf_link_hash_flags
1055 & (ELF_LINK_HASH_REF_DYNAMIC
1056 | ELF_LINK_HASH_DEF_DYNAMIC)) == 0);
1058 /* This is actually a static link, or it is a
1059 -Bsymbolic link and the symbol is defined
1060 locally, or the symbol was forced to be local
1061 because of a version file, or we're not creating a
1062 dynamic object and the symbol isn't referred to by
1063 a dynamic object. We must initialize
1064 this entry in the global offset table. Since
1065 the offset must always be a multiple of 4, we
1066 use the least significant bit to record whether
1067 we have initialized it already.
1069 When doing a dynamic link, we create a .rela.got
1070 relocation entry to initialize the value. This
1071 is done in the finish_dynamic_symbol routine. */
1072 if ((off & 1) != 0)
1073 off &= ~1;
1074 else
1076 bfd_put_32 (output_bfd, relocation,
1077 sgot->contents + off);
1078 h->got.offset |= 1;
1082 else
1084 BFD_ASSERT (local_got_offsets != NULL
1085 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1087 off = local_got_offsets[r_symndx];
1089 /* The offset must always be a multiple of 4. We use
1090 the least significant bit to record whether we have
1091 already generated the necessary reloc. */
1092 if ((off & 1) != 0)
1093 off &= ~1;
1094 else
1096 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1098 if (info->shared)
1100 asection *srelgot;
1101 Elf_Internal_Rela outrel;
1103 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1104 BFD_ASSERT (srelgot != NULL);
1106 outrel.r_offset = (sgot->output_section->vma
1107 + sgot->output_offset
1108 + off);
1109 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1110 outrel.r_addend = relocation;
1111 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1112 (((Elf32_External_Rela *)
1113 srelgot->contents)
1114 + srelgot->reloc_count));
1115 ++srelgot->reloc_count;
1118 local_got_offsets[r_symndx] |= 1;
1122 relocation = sgot->output_offset + off;
1123 if (rel->r_addend != 0)
1125 /* We can't do anything for a relocation which is against
1126 a symbol *plus offset*. GOT holds relocations for
1127 symbols. Make this an error; the compiler isn't
1128 allowed to pass us these kinds of things. */
1129 if (h == NULL)
1130 (*_bfd_error_handler)
1131 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1132 bfd_archive_filename (input_bfd),
1133 cris_elf_howto_table[r_type].name,
1134 rel->r_addend,
1135 bfd_get_section_name (input_bfd, input_section));
1136 else
1137 (*_bfd_error_handler)
1138 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1139 bfd_archive_filename (input_bfd),
1140 cris_elf_howto_table[r_type].name,
1141 rel->r_addend,
1142 symname[0] != '\0' ? symname : _("[whose name is lost]"),
1143 bfd_get_section_name (input_bfd, input_section));
1145 bfd_set_error (bfd_error_bad_value);
1146 return false;
1149 break;
1151 case R_CRIS_32_GOTREL:
1152 /* This relocation must only be performed against local symbols. */
1153 if (h != NULL)
1155 (*_bfd_error_handler)
1156 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1157 bfd_archive_filename (input_bfd),
1158 cris_elf_howto_table[r_type].name,
1159 symname,
1160 bfd_get_section_name (input_bfd, input_section));
1161 bfd_set_error (bfd_error_bad_value);
1162 return false;
1165 /* This relocation is like a PC-relative one, except the
1166 reference point is the location of GOT. Note that
1167 sgot->output_offset is not involved in this calculation. We
1168 always want the start of entire .got section, not the
1169 position after the reserved header. */
1170 relocation -= sgot->output_section->vma;
1171 break;
1173 case R_CRIS_32_PLT_PCREL:
1174 /* Relocation is to the entry for this symbol in the
1175 procedure linkage table. */
1177 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1178 without using the procedure linkage table. */
1179 if (h == NULL)
1180 break;
1182 if (h->plt.offset == (bfd_vma) -1
1183 || splt == NULL)
1185 /* We didn't make a PLT entry for this symbol. This
1186 happens when statically linking PIC code, or when
1187 using -Bsymbolic. */
1188 break;
1191 relocation = (splt->output_section->vma
1192 + splt->output_offset
1193 + h->plt.offset);
1194 break;
1196 case R_CRIS_32_PLT_GOTREL:
1197 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1198 start of the .got section. See also comment at
1199 R_CRIS_32_GOT. */
1200 relocation -= sgot->output_section->vma;
1202 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1203 without using the procedure linkage table. */
1204 if (h == NULL)
1205 break;
1207 if (h->plt.offset == (bfd_vma) -1
1208 || splt == NULL)
1210 /* We didn't make a PLT entry for this symbol. This
1211 happens when statically linking PIC code, or when
1212 using -Bsymbolic. */
1213 break;
1216 relocation = (splt->output_section->vma
1217 + splt->output_offset
1218 + h->plt.offset
1219 - sgot->output_section->vma);
1220 break;
1222 case R_CRIS_8_PCREL:
1223 case R_CRIS_16_PCREL:
1224 case R_CRIS_32_PCREL:
1225 /* If the symbol was local, we need no shlib-specific handling. */
1226 if (h == NULL)
1227 break;
1229 /* Fall through. */
1230 case R_CRIS_8:
1231 case R_CRIS_16:
1232 case R_CRIS_32:
1233 if (info->shared
1234 && r_symndx != 0
1235 && (input_section->flags & SEC_ALLOC) != 0
1236 && ((r_type != R_CRIS_8_PCREL
1237 && r_type != R_CRIS_16_PCREL
1238 && r_type != R_CRIS_32_PCREL)
1239 || (!info->symbolic
1240 || (h->elf_link_hash_flags
1241 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1243 Elf_Internal_Rela outrel;
1244 boolean skip, relocate;
1246 /* When generating a shared object, these relocations
1247 are copied into the output file to be resolved at run
1248 time. */
1250 if (sreloc == NULL)
1252 const char *name;
1254 name = (bfd_elf_string_from_elf_section
1255 (input_bfd,
1256 elf_elfheader (input_bfd)->e_shstrndx,
1257 elf_section_data (input_section)->rel_hdr.sh_name));
1258 if (name == NULL)
1259 return false;
1261 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1262 && strcmp (bfd_get_section_name (input_bfd,
1263 input_section),
1264 name + 5) == 0);
1266 sreloc = bfd_get_section_by_name (dynobj, name);
1268 /* That section should have been created in
1269 cris_elf_check_relocs, but that function will not be
1270 called for objects which fail in
1271 cris_elf_merge_private_bfd_data. */
1272 if (sreloc == NULL)
1274 (*_bfd_error_handler)
1275 (_("%s: Internal inconsistency; no relocation section %s"),
1276 bfd_archive_filename (input_bfd),
1277 name);
1279 bfd_set_error (bfd_error_bad_value);
1280 return false;
1284 skip = false;
1286 outrel.r_offset =
1287 _bfd_elf_section_offset (output_bfd, info, input_section,
1288 rel->r_offset);
1289 if (outrel.r_offset == (bfd_vma) -1)
1290 skip = true;
1291 outrel.r_offset += (input_section->output_section->vma
1292 + input_section->output_offset);
1294 if (skip)
1296 memset (&outrel, 0, sizeof outrel);
1297 relocate = false;
1299 /* h->dynindx may be -1 if the symbol was marked to
1300 become local. */
1301 else if (h != NULL
1302 && ((! info->symbolic && h->dynindx != -1)
1303 || (h->elf_link_hash_flags
1304 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1306 BFD_ASSERT (h->dynindx != -1);
1307 relocate = false;
1308 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1309 outrel.r_addend = relocation + rel->r_addend;
1311 else
1313 if (r_type == R_CRIS_32)
1315 relocate = true;
1316 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1317 outrel.r_addend = relocation + rel->r_addend;
1319 else
1321 long indx;
1323 if (h == NULL)
1324 sec = local_sections[r_symndx];
1325 else
1327 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1328 || (h->root.type
1329 == bfd_link_hash_defweak));
1330 sec = h->root.u.def.section;
1332 if (sec != NULL && bfd_is_abs_section (sec))
1333 indx = 0;
1334 else if (sec == NULL || sec->owner == NULL)
1336 bfd_set_error (bfd_error_bad_value);
1337 return false;
1339 else
1341 asection *osec;
1343 osec = sec->output_section;
1344 indx = elf_section_data (osec)->dynindx;
1345 BFD_ASSERT (indx > 0);
1348 relocate = false;
1349 outrel.r_info = ELF32_R_INFO (indx, r_type);
1350 outrel.r_addend = relocation + rel->r_addend;
1354 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1355 (((Elf32_External_Rela *)
1356 sreloc->contents)
1357 + sreloc->reloc_count));
1358 ++sreloc->reloc_count;
1360 /* This reloc will be computed at runtime, so there's no
1361 need to do anything now, except for R_CRIS_32 relocations
1362 that have been turned into R_CRIS_RELATIVE. */
1363 if (!relocate)
1364 continue;
1367 break;
1370 r = cris_final_link_relocate (howto, input_bfd, input_section,
1371 contents, rel, relocation);
1373 if (r != bfd_reloc_ok)
1375 const char * msg = (const char *) NULL;
1377 switch (r)
1379 case bfd_reloc_overflow:
1380 r = info->callbacks->reloc_overflow
1381 (info, symname, howto->name, (bfd_vma) 0,
1382 input_bfd, input_section, rel->r_offset);
1383 break;
1385 case bfd_reloc_undefined:
1386 r = info->callbacks->undefined_symbol
1387 (info, symname, input_bfd, input_section, rel->r_offset,
1388 true);
1389 break;
1391 case bfd_reloc_outofrange:
1392 msg = _("internal error: out of range error");
1393 break;
1395 case bfd_reloc_notsupported:
1396 msg = _("internal error: unsupported relocation error");
1397 break;
1399 case bfd_reloc_dangerous:
1400 msg = _("internal error: dangerous relocation");
1401 break;
1403 default:
1404 msg = _("internal error: unknown error");
1405 break;
1408 if (msg)
1409 r = info->callbacks->warning
1410 (info, msg, symname, input_bfd, input_section, rel->r_offset);
1412 if (! r)
1413 return false;
1417 return true;
1420 /* Finish up dynamic symbol handling. We set the contents of various
1421 dynamic sections here. */
1423 static boolean
1424 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1425 bfd *output_bfd;
1426 struct bfd_link_info *info;
1427 struct elf_link_hash_entry *h;
1428 Elf_Internal_Sym *sym;
1430 bfd *dynobj;
1431 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1433 dynobj = elf_hash_table (info)->dynobj;
1435 if (h->plt.offset != (bfd_vma) -1)
1437 asection *splt;
1438 asection *sgotplt;
1439 asection *sgot;
1440 asection *srela;
1441 bfd_vma got_base;
1443 bfd_vma gotplt_offset
1444 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1445 Elf_Internal_Rela rela;
1446 boolean has_gotplt = gotplt_offset != 0;
1448 /* Get the index in the procedure linkage table which
1449 corresponds to this symbol. This is the index of this symbol
1450 in all the symbols for which we are making plt entries. The
1451 first entry in the procedure linkage table is reserved. */
1452 /* We have to count backwards here, and the result is only valid as
1453 an index into .got.plt and its relocations. FIXME: Constants... */
1454 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1456 /* Get the offset into the .got table of the entry that corresponds
1457 to this function. Note that we embed knowledge that "incoming"
1458 .got goes after .got.plt in the output without padding (pointer
1459 aligned). However, that knowledge is present in several other
1460 places too, here and in elflink.h at least. */
1461 bfd_vma got_offset
1462 = (has_gotplt
1463 ? gotplt_offset
1464 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1466 /* This symbol has an entry in the procedure linkage table. Set it
1467 up. */
1469 BFD_ASSERT (h->dynindx != -1);
1471 splt = bfd_get_section_by_name (dynobj, ".plt");
1472 sgot = bfd_get_section_by_name (dynobj, ".got");
1473 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1474 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1475 BFD_ASSERT (splt != NULL && sgotplt != NULL
1476 && (! has_gotplt || srela != NULL));
1478 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1480 /* Fill in the entry in the procedure linkage table. */
1481 if (! info->shared)
1483 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1484 PLT_ENTRY_SIZE);
1486 /* We need to enter the absolute address of the GOT entry here. */
1487 bfd_put_32 (output_bfd, got_base + got_offset,
1488 splt->contents + h->plt.offset + plt_off1);
1490 else
1492 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1493 PLT_ENTRY_SIZE);
1494 bfd_put_32 (output_bfd, got_offset,
1495 splt->contents + h->plt.offset + plt_off1);
1498 /* Fill in the plt entry and make a relocation, if this is a "real"
1499 PLT entry. */
1500 if (has_gotplt)
1502 /* Fill in the offset into the reloc table. */
1503 bfd_put_32 (output_bfd,
1504 gotplt_index * sizeof (Elf32_External_Rela),
1505 splt->contents + h->plt.offset + plt_off2);
1507 /* Fill in the offset to the first PLT entry, where to "jump". */
1508 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1509 splt->contents + h->plt.offset + plt_off3);
1511 /* Fill in the entry in the global offset table with the address of
1512 the relocating stub. */
1513 bfd_put_32 (output_bfd,
1514 (splt->output_section->vma
1515 + splt->output_offset
1516 + h->plt.offset
1517 + 8),
1518 sgotplt->contents + got_offset);
1520 /* Fill in the entry in the .rela.plt section. */
1521 rela.r_offset = (sgotplt->output_section->vma
1522 + sgotplt->output_offset
1523 + got_offset);
1524 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1525 rela.r_addend = 0;
1526 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1527 ((Elf32_External_Rela *) srela->contents
1528 + gotplt_index));
1531 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1533 /* Mark the symbol as undefined, rather than as defined in
1534 the .plt section. Leave the value alone. */
1535 sym->st_shndx = SHN_UNDEF;
1537 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1538 know whether resetting the value is significant; if it really
1539 is, rather than a quirk or bug in the sparc port, then I
1540 believe we'd see this elsewhere. */
1541 /* If the symbol is weak, we do need to clear the value.
1542 Otherwise, the PLT entry would provide a definition for
1543 the symbol even if the symbol wasn't defined anywhere,
1544 and so the symbol would never be NULL. */
1545 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1546 == 0)
1547 sym->st_value = 0;
1551 /* We don't emit .got relocs for symbols that aren't in the
1552 dynamic-symbols table for an ordinary program. */
1553 if (h->got.offset != (bfd_vma) -1
1554 && (info->shared || h->dynindx != -1))
1556 asection *sgot;
1557 asection *srela;
1558 Elf_Internal_Rela rela;
1559 bfd_byte *where;
1561 /* This symbol has an entry in the global offset table. Set it up. */
1563 sgot = bfd_get_section_by_name (dynobj, ".got");
1564 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1565 BFD_ASSERT (sgot != NULL && srela != NULL);
1567 rela.r_offset = (sgot->output_section->vma
1568 + sgot->output_offset
1569 + (h->got.offset &~ (bfd_vma) 1));
1571 /* If this is a static link, or it is a -Bsymbolic link and the
1572 symbol is defined locally or was forced to be local because
1573 of a version file, we just want to emit a RELATIVE reloc.
1574 The entry in the global offset table will already have been
1575 initialized in the relocate_section function. */
1576 where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1577 if (! elf_hash_table (info)->dynamic_sections_created
1578 || (info->shared
1579 && (info->symbolic || h->dynindx == -1)
1580 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1582 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1583 rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1585 else
1587 bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1588 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1589 rela.r_addend = 0;
1592 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1593 ((Elf32_External_Rela *) srela->contents
1594 + srela->reloc_count));
1595 ++srela->reloc_count;
1598 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1600 asection *s;
1601 Elf_Internal_Rela rela;
1603 /* This symbol needs a copy reloc. Set it up. */
1605 BFD_ASSERT (h->dynindx != -1
1606 && (h->root.type == bfd_link_hash_defined
1607 || h->root.type == bfd_link_hash_defweak));
1609 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1610 ".rela.bss");
1611 BFD_ASSERT (s != NULL);
1613 rela.r_offset = (h->root.u.def.value
1614 + h->root.u.def.section->output_section->vma
1615 + h->root.u.def.section->output_offset);
1616 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1617 rela.r_addend = 0;
1618 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1619 ((Elf32_External_Rela *) s->contents
1620 + s->reloc_count));
1621 ++s->reloc_count;
1624 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1625 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1626 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1627 sym->st_shndx = SHN_ABS;
1629 return true;
1632 /* Finish up the dynamic sections. */
1634 static boolean
1635 elf_cris_finish_dynamic_sections (output_bfd, info)
1636 bfd *output_bfd;
1637 struct bfd_link_info *info;
1639 bfd *dynobj;
1640 asection *sgot;
1641 asection *sdyn;
1643 dynobj = elf_hash_table (info)->dynobj;
1645 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1646 BFD_ASSERT (sgot != NULL);
1647 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1649 if (elf_hash_table (info)->dynamic_sections_created)
1651 asection *splt;
1652 Elf32_External_Dyn *dyncon, *dynconend;
1654 splt = bfd_get_section_by_name (dynobj, ".plt");
1655 BFD_ASSERT (splt != NULL && sdyn != NULL);
1657 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1658 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1659 for (; dyncon < dynconend; dyncon++)
1661 Elf_Internal_Dyn dyn;
1662 asection *s;
1664 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1666 switch (dyn.d_tag)
1668 default:
1669 break;
1671 case DT_PLTGOT:
1672 s = bfd_get_section_by_name (output_bfd, ".got");
1673 BFD_ASSERT (s != NULL);
1674 dyn.d_un.d_ptr = s->vma;
1675 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1676 break;
1678 case DT_JMPREL:
1679 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1680 if all symbols are found in the .got (not .got.plt). */
1681 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1682 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1683 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1684 break;
1686 case DT_PLTRELSZ:
1687 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1688 if (s == NULL)
1689 dyn.d_un.d_val = 0;
1690 else if (s->_cooked_size != 0)
1691 dyn.d_un.d_val = s->_cooked_size;
1692 else
1693 dyn.d_un.d_val = s->_raw_size;
1694 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1695 break;
1697 case DT_RELASZ:
1698 /* The procedure linkage table relocs (DT_JMPREL) should
1699 not be included in the overall relocs (DT_RELA).
1700 Therefore, we override the DT_RELASZ entry here to
1701 make it not include the JMPREL relocs. Since the
1702 linker script arranges for .rela.plt to follow all
1703 other relocation sections, we don't have to worry
1704 about changing the DT_RELA entry. */
1705 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1706 if (s != NULL)
1708 if (s->_cooked_size != 0)
1709 dyn.d_un.d_val -= s->_cooked_size;
1710 else
1711 dyn.d_un.d_val -= s->_raw_size;
1713 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1714 break;
1718 /* Fill in the first entry in the procedure linkage table. */
1719 if (splt->_raw_size > 0)
1721 if (info->shared)
1722 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1723 else
1725 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1726 bfd_put_32 (output_bfd,
1727 sgot->output_section->vma + sgot->output_offset + 4,
1728 splt->contents + 6);
1729 bfd_put_32 (output_bfd,
1730 sgot->output_section->vma + sgot->output_offset + 8,
1731 splt->contents + 14);
1733 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1734 = PLT_ENTRY_SIZE;
1739 /* Fill in the first three entries in the global offset table. */
1740 if (sgot->_raw_size > 0)
1742 if (sdyn == NULL)
1743 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1744 else
1745 bfd_put_32 (output_bfd,
1746 sdyn->output_section->vma + sdyn->output_offset,
1747 sgot->contents);
1748 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1749 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1752 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1754 return true;
1757 /* Return the section that should be marked against GC for a given
1758 relocation. */
1760 static asection *
1761 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1762 bfd * abfd;
1763 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1764 Elf_Internal_Rela * rel;
1765 struct elf_link_hash_entry * h;
1766 Elf_Internal_Sym * sym;
1768 if (h != NULL)
1770 switch (ELF32_R_TYPE (rel->r_info))
1772 case R_CRIS_GNU_VTINHERIT:
1773 case R_CRIS_GNU_VTENTRY:
1774 break;
1776 default:
1777 switch (h->root.type)
1779 case bfd_link_hash_defined:
1780 case bfd_link_hash_defweak:
1781 return h->root.u.def.section;
1783 case bfd_link_hash_common:
1784 return h->root.u.c.p->section;
1786 default:
1787 break;
1791 else
1793 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1796 return NULL;
1799 /* Update the got entry reference counts for the section being removed. */
1801 static boolean
1802 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1803 bfd * abfd ATTRIBUTE_UNUSED;
1804 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1805 asection * sec ATTRIBUTE_UNUSED;
1806 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1808 Elf_Internal_Shdr *symtab_hdr;
1809 struct elf_link_hash_entry **sym_hashes;
1810 bfd_signed_vma *local_got_refcounts;
1811 const Elf_Internal_Rela *rel, *relend;
1812 unsigned long r_symndx;
1813 struct elf_link_hash_entry *h;
1814 bfd *dynobj;
1815 asection *sgot;
1816 asection *srelgot;
1818 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1819 sym_hashes = elf_sym_hashes (abfd);
1820 local_got_refcounts = elf_local_got_refcounts (abfd);
1822 dynobj = elf_hash_table (info)->dynobj;
1823 if (dynobj == NULL)
1824 return true;
1826 sgot = bfd_get_section_by_name (dynobj, ".got");
1827 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1829 relend = relocs + sec->reloc_count;
1830 for (rel = relocs; rel < relend; rel++)
1832 switch (ELF32_R_TYPE (rel->r_info))
1834 case R_CRIS_16_GOT:
1835 case R_CRIS_32_GOT:
1836 r_symndx = ELF32_R_SYM (rel->r_info);
1837 if (r_symndx >= symtab_hdr->sh_info)
1839 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1840 if (h->got.refcount > 0)
1842 --h->got.refcount;
1843 if (h->got.refcount == 0)
1845 /* We don't need the .got entry any more. */
1846 sgot->_raw_size -= 4;
1847 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1850 break;
1853 local_got_reloc:
1854 if (local_got_refcounts != NULL)
1856 if (local_got_refcounts[r_symndx] > 0)
1858 --local_got_refcounts[r_symndx];
1859 if (local_got_refcounts[r_symndx] == 0)
1861 /* We don't need the .got entry any more. */
1862 sgot->_raw_size -= 4;
1863 if (info->shared)
1864 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1868 break;
1870 case R_CRIS_16_GOTPLT:
1871 case R_CRIS_32_GOTPLT:
1872 /* For local symbols, treat these like GOT relocs. */
1873 r_symndx = ELF32_R_SYM (rel->r_info);
1874 if (r_symndx < symtab_hdr->sh_info)
1875 goto local_got_reloc;
1877 case R_CRIS_32_PLT_GOTREL:
1878 /* FIXME: We don't garbage-collect away the .got section. */
1879 if (local_got_refcounts != NULL)
1880 local_got_refcounts[-1]--;
1881 /* Fall through. */
1883 case R_CRIS_8_PCREL:
1884 case R_CRIS_16_PCREL:
1885 case R_CRIS_32_PCREL:
1886 case R_CRIS_32_PLT_PCREL:
1887 r_symndx = ELF32_R_SYM (rel->r_info);
1888 if (r_symndx >= symtab_hdr->sh_info)
1890 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1891 if (h->plt.refcount > 0)
1892 --h->plt.refcount;
1894 break;
1896 default:
1897 break;
1901 return true;
1904 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1905 entry but we found we will not create any. Called when we find we will
1906 not have any PLT for this symbol, by for example
1907 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1908 or elf_cris_size_dynamic_sections if no dynamic sections will be
1909 created (we're only linking static objects). */
1911 static boolean
1912 elf_cris_adjust_gotplt_to_got (h, p)
1913 struct elf_cris_link_hash_entry *h;
1914 PTR p;
1916 struct bfd_link_info *info = (struct bfd_link_info *) p;
1917 bfd *dynobj = elf_hash_table (info)->dynobj;
1919 BFD_ASSERT (dynobj != NULL);
1921 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1922 if (h->gotplt_refcount <= 0)
1923 return true;
1925 if (h->root.got.refcount > 0)
1927 /* There's a GOT entry for this symbol. Just adjust the refcount.
1928 Probably not necessary at this stage, but keeping it accurate
1929 helps avoiding surprises later. */
1930 h->root.got.refcount += h->gotplt_refcount;
1931 h->gotplt_refcount = -1;
1933 else
1935 /* No GOT entry for this symbol. We need to create one. */
1936 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1937 asection *srelgot
1938 = bfd_get_section_by_name (dynobj, ".rela.got");
1940 /* Put an accurate refcount there. */
1941 h->root.got.refcount = h->gotplt_refcount;
1943 h->gotplt_refcount = -1;
1945 /* We always have a .got section when there are dynamic
1946 relocs. */
1947 BFD_ASSERT (sgot != NULL /* Surely have .got section. */);
1949 /* We might have had a PLT but with no GOT entry and
1950 further no GOT reloc section at all needed before.
1951 Add it. */
1952 if (srelgot == NULL)
1954 srelgot = bfd_make_section (dynobj, ".rela.got");
1956 if (srelgot == NULL
1957 || !bfd_set_section_flags (dynobj, srelgot,
1958 (SEC_ALLOC
1959 | SEC_LOAD
1960 | SEC_HAS_CONTENTS
1961 | SEC_IN_MEMORY
1962 | SEC_LINKER_CREATED
1963 | SEC_READONLY))
1964 || !bfd_set_section_alignment (dynobj, srelgot, 2))
1965 return false;
1968 /* Allocate space in the .got section. */
1969 sgot->_raw_size += 4;
1971 /* Allocate relocation space. */
1972 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1975 return true;
1978 /* Try to fold PLT entries with GOT entries. There are two cases when we
1979 want to do this:
1981 - When all PLT references are GOTPLT references, and there are GOT
1982 references. We don't have to generate a PLT at all.
1984 - When there are both (ordinary) PLT references and GOT references.
1985 We want to make the PLT reference use the ordinary GOT entry rather
1986 than a run-time dynamically resolved GOTPLT entry (since the GOT
1987 entry will have to be resolved at startup anyway).
1989 Though the latter case is handled when room for the PLT is allocated,
1990 not here.
1992 Note that this function is called before symbols are forced local by
1993 version scripts. The differing cases are handled by
1994 elf_cris_hide_symbol. */
1996 static boolean
1997 elf_cris_try_fold_plt_to_got (h, p)
1998 struct elf_cris_link_hash_entry *h;
1999 PTR p;
2001 struct bfd_link_info *info = (struct bfd_link_info *) p;
2003 /* If there are no GOT references for this symbol, we can't fold any
2004 other reference so there's nothing to do. Likewise if there are no
2005 PLT references; GOTPLT references included. */
2006 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2007 return true;
2009 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
2010 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2012 if (h->gotplt_refcount == h->root.plt.refcount)
2014 /* The only PLT references are GOTPLT references, and there are GOT
2015 references. Convert PLT to GOT references. */
2016 if (! elf_cris_adjust_gotplt_to_got (h, info))
2017 return false;
2019 /* Clear the PLT references, so no PLT will be created. */
2020 h->root.plt.offset = (bfd_vma) -1;
2023 return true;
2026 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2027 to use a GOT entry (and create one) rather than requiring a GOTPLT
2028 entry. */
2030 static void
2031 elf_cris_hide_symbol (info, h)
2032 struct bfd_link_info *info;
2033 struct elf_link_hash_entry *h;
2035 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2037 _bfd_elf_link_hash_hide_symbol (info, h);
2040 /* Adjust a symbol defined by a dynamic object and referenced by a
2041 regular object. The current definition is in some section of the
2042 dynamic object, but we're not including those sections. We have to
2043 change the definition to something the rest of the link can
2044 understand. */
2046 static boolean
2047 elf_cris_adjust_dynamic_symbol (info, h)
2048 struct bfd_link_info *info;
2049 struct elf_link_hash_entry *h;
2051 bfd *dynobj;
2052 asection *s;
2053 unsigned int power_of_two;
2055 dynobj = elf_hash_table (info)->dynobj;
2057 /* Make sure we know what is going on here. */
2058 BFD_ASSERT (dynobj != NULL
2059 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2060 || h->weakdef != NULL
2061 || ((h->elf_link_hash_flags
2062 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2063 && (h->elf_link_hash_flags
2064 & ELF_LINK_HASH_REF_REGULAR) != 0
2065 && (h->elf_link_hash_flags
2066 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2068 /* If this is a function, put it in the procedure linkage table. We
2069 will fill in the contents of the procedure linkage table later,
2070 when we know the address of the .got section. */
2071 if (h->type == STT_FUNC
2072 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2074 if (! info->shared
2075 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2076 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2077 /* We must always create the plt entry if it was referenced by a
2078 PLT relocation. In this case we already recorded it as a
2079 dynamic symbol. */
2080 /* FIXME: m68k and i386 differ here, for unclear reasons. */
2081 && h->dynindx == -1)
2083 /* This case can occur if we saw a PLT reloc in an input file,
2084 but the symbol was never referred to by a dynamic object. In
2085 such a case, we don't actually need to build a procedure
2086 linkage table, and we can just do a PC reloc instead, or
2087 change a .got.plt index to a .got index for GOTPLT relocs. */
2088 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2089 h->plt.offset = (bfd_vma) -1;
2091 return
2092 elf_cris_adjust_gotplt_to_got ((struct
2093 elf_cris_link_hash_entry *) h,
2094 info);
2097 /* If there are only GOT references and GOTPLT references to this
2098 PLT entry, get rid of the PLT. */
2099 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2100 h, info))
2101 return false;
2103 /* GC or folding may have rendered this entry unused. */
2104 if (h->plt.refcount <= 0)
2106 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2107 h->plt.offset = (bfd_vma) -1;
2108 return true;
2111 /* Make sure this symbol is output as a dynamic symbol. */
2112 if (h->dynindx == -1)
2114 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2115 return false;
2118 s = bfd_get_section_by_name (dynobj, ".plt");
2119 BFD_ASSERT (s != NULL);
2121 /* If this is the first .plt entry, make room for the special
2122 first entry. */
2123 if (s->_raw_size == 0)
2124 s->_raw_size += PLT_ENTRY_SIZE;
2126 /* If this symbol is not defined in a regular file, and we are
2127 not generating a shared library, then set the symbol to this
2128 location in the .plt. This is required to make function
2129 pointers compare as equal between the normal executable and
2130 the shared library. */
2131 if (!info->shared
2132 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2134 h->root.u.def.section = s;
2135 h->root.u.def.value = s->_raw_size;
2138 /* If there's already a GOT entry, use that, not a .got.plt. A
2139 GOT field still has a reference count when we get here; it's
2140 not yet changed to an offset. */
2141 if (h->got.refcount > 0)
2143 h->got.refcount += h->plt.refcount;
2145 /* Mark the PLT offset to use the GOT entry by setting the low
2146 bit in the plt offset; it is always a multiple of
2147 pointer-size. */
2148 BFD_ASSERT ((s->_raw_size & 3) == 0);
2150 /* Change the PLT refcount to an offset. */
2151 h->plt.offset = s->_raw_size;
2153 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2154 that the got entry should be used instead. */
2155 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2156 h)->gotplt_offset == 0);
2158 /* Make room for this entry. */
2159 s->_raw_size += PLT_ENTRY_SIZE;
2161 return true;
2164 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2165 h->plt.offset = s->_raw_size;
2167 /* Make room for this entry. */
2168 s->_raw_size += PLT_ENTRY_SIZE;
2170 /* We also need to make an entry in the .got.plt section, which
2171 will be placed in the .got section by the linker script. */
2172 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2173 = elf_cris_hash_table (info)->next_gotplt_entry;
2174 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2176 s = bfd_get_section_by_name (dynobj, ".got.plt");
2177 BFD_ASSERT (s != NULL);
2178 s->_raw_size += 4;
2180 /* We also need to make an entry in the .rela.plt section. */
2182 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2183 BFD_ASSERT (s != NULL);
2184 s->_raw_size += sizeof (Elf32_External_Rela);
2186 return true;
2189 /* Reinitialize the plt offset now that it is not used as a reference
2190 count any more. */
2191 h->plt.offset = (bfd_vma) -1;
2193 /* If this is a weak symbol, and there is a real definition, the
2194 processor independent code will have arranged for us to see the
2195 real definition first, and we can just use the same value. */
2196 if (h->weakdef != NULL)
2198 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2199 || h->weakdef->root.type == bfd_link_hash_defweak);
2200 h->root.u.def.section = h->weakdef->root.u.def.section;
2201 h->root.u.def.value = h->weakdef->root.u.def.value;
2202 return true;
2205 /* This is a reference to a symbol defined by a dynamic object which
2206 is not a function. */
2208 /* If we are creating a shared library, we must presume that the
2209 only references to the symbol are via the global offset table.
2210 For such cases we need not do anything here; the relocations will
2211 be handled correctly by relocate_section. */
2212 if (info->shared)
2213 return true;
2215 /* If there are no references to this symbol that do not use the
2216 GOT, we don't need to generate a copy reloc. */
2217 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2218 return true;
2220 /* We must allocate the symbol in our .dynbss section, which will
2221 become part of the .bss section of the executable. There will be
2222 an entry for this symbol in the .dynsym section. The dynamic
2223 object will contain position independent code, so all references
2224 from the dynamic object to this symbol will go through the global
2225 offset table. The dynamic linker will use the .dynsym entry to
2226 determine the address it must put in the global offset table, so
2227 both the dynamic object and the regular object will refer to the
2228 same memory location for the variable. */
2230 s = bfd_get_section_by_name (dynobj, ".dynbss");
2231 BFD_ASSERT (s != NULL);
2233 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2234 copy the initial value out of the dynamic object and into the
2235 runtime process image. We need to remember the offset into the
2236 .rela.bss section we are going to use. */
2237 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2239 asection *srel;
2241 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2242 BFD_ASSERT (srel != NULL);
2243 srel->_raw_size += sizeof (Elf32_External_Rela);
2244 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2247 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2248 thing to copy; so do we. */
2250 /* We need to figure out the alignment required for this symbol. I
2251 have no idea how ELF linkers handle this. */
2252 power_of_two = bfd_log2 (h->size);
2253 if (power_of_two > 3)
2254 power_of_two = 3;
2256 /* Apply the required alignment. */
2257 s->_raw_size = BFD_ALIGN (s->_raw_size,
2258 (bfd_size_type) (1 << power_of_two));
2259 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2261 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2262 return false;
2265 /* Define the symbol as being at this point in the section. */
2266 h->root.u.def.section = s;
2267 h->root.u.def.value = s->_raw_size;
2269 /* Increment the section size to make room for the symbol. */
2270 s->_raw_size += h->size;
2272 return true;
2275 /* Look through the relocs for a section during the first phase. */
2277 static boolean
2278 cris_elf_check_relocs (abfd, info, sec, relocs)
2279 bfd *abfd;
2280 struct bfd_link_info *info;
2281 asection *sec;
2282 const Elf_Internal_Rela *relocs;
2284 bfd *dynobj;
2285 Elf_Internal_Shdr *symtab_hdr;
2286 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2287 bfd_signed_vma *local_got_refcounts;
2288 const Elf_Internal_Rela *rel;
2289 const Elf_Internal_Rela *rel_end;
2290 asection *sgot;
2291 asection *srelgot;
2292 asection *sreloc;
2294 if (info->relocateable)
2295 return true;
2297 dynobj = elf_hash_table (info)->dynobj;
2298 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2299 sym_hashes = elf_sym_hashes (abfd);
2300 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2301 local_got_refcounts = elf_local_got_refcounts (abfd);
2303 sgot = NULL;
2304 srelgot = NULL;
2305 sreloc = NULL;
2307 if (!elf_bad_symtab (abfd))
2308 sym_hashes_end -= symtab_hdr->sh_info;
2310 rel_end = relocs + sec->reloc_count;
2311 for (rel = relocs; rel < rel_end; rel++)
2313 struct elf_link_hash_entry *h;
2314 unsigned long r_symndx;
2315 enum elf_cris_reloc_type r_type;
2317 r_symndx = ELF32_R_SYM (rel->r_info);
2318 if (r_symndx < symtab_hdr->sh_info)
2319 h = NULL;
2320 else
2321 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2323 r_type = ELF32_R_TYPE (rel->r_info);
2325 /* Some relocs require linker-created sections; we need to hang them
2326 on the first input bfd we found that contained dynamic relocs. */
2327 switch (r_type)
2329 case R_CRIS_16_GOT:
2330 case R_CRIS_32_GOT:
2331 case R_CRIS_32_GOTREL:
2332 case R_CRIS_32_PLT_GOTREL:
2333 case R_CRIS_32_PLT_PCREL:
2334 case R_CRIS_16_GOTPLT:
2335 case R_CRIS_32_GOTPLT:
2336 if (dynobj == NULL)
2338 elf_hash_table (info)->dynobj = dynobj = abfd;
2340 /* Create the .got section, so we can assume it's always
2341 present whenever there's a dynobj. */
2342 if (!_bfd_elf_create_got_section (dynobj, info))
2343 return false;
2345 break;
2347 default:
2348 break;
2351 /* Some relocs require a global offset table (but perhaps not a
2352 specific GOT entry). */
2353 switch (r_type)
2355 case R_CRIS_16_GOT:
2356 case R_CRIS_32_GOT:
2357 case R_CRIS_32_GOTREL:
2358 case R_CRIS_32_PLT_GOTREL:
2359 if (sgot == NULL)
2360 sgot = bfd_get_section_by_name (dynobj, ".got");
2362 if (local_got_refcounts == NULL)
2364 bfd_size_type amt;
2366 /* We use index local_got_refcounts[-1] to count all
2367 GOT-relative relocations that do not have explicit
2368 GOT entries. */
2369 amt = symtab_hdr->sh_info + 1;
2370 amt *= sizeof (bfd_signed_vma);
2371 local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2372 if (local_got_refcounts == NULL)
2373 return false;
2375 local_got_refcounts++;
2376 elf_local_got_refcounts (abfd) = local_got_refcounts;
2378 break;
2380 default:
2381 break;
2384 switch (r_type)
2386 case R_CRIS_16_GOTPLT:
2387 case R_CRIS_32_GOTPLT:
2388 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2389 entry) is eliminated. We can only do this for a non-local
2390 symbol. */
2391 if (h != NULL)
2393 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2394 goto handle_gotplt_reloc;
2396 /* If h is NULL then this is a local symbol, and we must make a
2397 GOT entry for it, so handle it like a GOT reloc. */
2398 /* Fall through. */
2400 case R_CRIS_16_GOT:
2401 case R_CRIS_32_GOT:
2402 /* This symbol requires a global offset table entry. */
2404 if (srelgot == NULL
2405 && (h != NULL || info->shared))
2407 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2408 if (srelgot == NULL)
2410 srelgot = bfd_make_section (dynobj, ".rela.got");
2411 if (srelgot == NULL
2412 || !bfd_set_section_flags (dynobj, srelgot,
2413 (SEC_ALLOC
2414 | SEC_LOAD
2415 | SEC_HAS_CONTENTS
2416 | SEC_IN_MEMORY
2417 | SEC_LINKER_CREATED
2418 | SEC_READONLY))
2419 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2420 return false;
2424 if (h != NULL)
2426 if (h->got.refcount == 0)
2428 /* Make sure this symbol is output as a dynamic symbol. */
2429 if (h->dynindx == -1)
2431 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2432 return false;
2435 /* Allocate space in the .got section. */
2436 sgot->_raw_size += 4;
2437 /* Allocate relocation space. */
2438 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2440 h->got.refcount++;
2442 else
2444 /* This is a global offset table entry for a local symbol. */
2445 if (local_got_refcounts[r_symndx] == 0)
2447 sgot->_raw_size += 4;
2448 if (info->shared)
2450 /* If we are generating a shared object, we need to
2451 output a R_CRIS_RELATIVE reloc so that the dynamic
2452 linker can adjust this GOT entry. */
2453 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2456 local_got_refcounts[r_symndx]++;
2458 break;
2460 case R_CRIS_32_GOTREL:
2461 /* This reference requires a global offset table.
2462 FIXME: The actual refcount isn't used currently; the .got
2463 section can't be removed if there were any references in the
2464 input. */
2465 local_got_refcounts[-1]++;
2466 break;
2468 handle_gotplt_reloc:
2470 case R_CRIS_32_PLT_GOTREL:
2471 /* This reference requires a global offset table. */
2472 local_got_refcounts[-1]++;
2473 /* Fall through. */
2475 case R_CRIS_32_PLT_PCREL:
2476 /* This symbol requires a procedure linkage table entry. We
2477 actually build the entry in adjust_dynamic_symbol,
2478 because this might be a case of linking PIC code which is
2479 never referenced by a dynamic object, in which case we
2480 don't need to generate a procedure linkage table entry
2481 after all. */
2483 /* If this is a local symbol, we resolve it directly without
2484 creating a procedure linkage table entry. */
2485 if (h == NULL)
2486 continue;
2488 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2489 h->plt.refcount++;
2490 break;
2492 case R_CRIS_8:
2493 case R_CRIS_16:
2494 case R_CRIS_32:
2495 /* Let's help debug shared library creation. Any of these
2496 relocs can be used in shared libs, but pages containing them
2497 cannot be shared. Don't warn for sections we don't care
2498 about, such as debug sections or non-constant sections. We
2499 can't help tables of (global) function pointers, for example,
2500 though they must be emitted in a data section to avoid having
2501 impure text sections. */
2502 if (info->shared
2503 && (sec->flags & SEC_ALLOC) != 0
2504 && (sec->flags & SEC_READONLY) != 0)
2506 /* FIXME: How do we make this optionally a warning only? */
2507 (*_bfd_error_handler)
2508 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2509 bfd_archive_filename (abfd),
2510 sec->name,
2511 cris_elf_howto_table[r_type].name);
2513 /* Fall through. */
2515 case R_CRIS_8_PCREL:
2516 case R_CRIS_16_PCREL:
2517 case R_CRIS_32_PCREL:
2518 if (h != NULL)
2520 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2522 /* Make sure a plt entry is created for this symbol if it
2523 turns out to be a function defined by a dynamic object. */
2524 h->plt.refcount++;
2527 /* If we are creating a shared library and this is not a local
2528 symbol, we need to copy the reloc into the shared library.
2529 However when linking with -Bsymbolic and this is a global
2530 symbol which is defined in an object we are including in the
2531 link (i.e., DEF_REGULAR is set), then we can resolve the
2532 reloc directly. At this point we have not seen all the input
2533 files, so it is possible that DEF_REGULAR is not set now but
2534 will be set later (it is never cleared). In case of a weak
2535 definition, DEF_REGULAR may be cleared later by a strong
2536 definition in a shared library. We account for that
2537 possibility below by storing information in the relocs_copied
2538 field of the hash table entry. A similar situation occurs
2539 when creating shared libraries and symbol visibility changes
2540 render the symbol local. */
2542 /* No need to do anything if we're not creating a shared object. */
2543 if (! info->shared)
2544 break;
2546 /* We don't need to handle relocs into sections not going into
2547 the "real" output. */
2548 if ((sec->flags & SEC_ALLOC) == 0)
2549 break;
2551 /* We can only eliminate PC-relative relocs. */
2552 if (r_type == R_CRIS_8_PCREL
2553 || r_type == R_CRIS_16_PCREL
2554 || r_type == R_CRIS_32_PCREL)
2556 /* If the symbol is local, then we can eliminate the reloc. */
2557 if (h == NULL)
2558 break;
2560 /* If this is with -Bsymbolic and the symbol isn't weak, and
2561 is defined by an ordinary object (the ones we include in
2562 this shared library) then we can also eliminate the
2563 reloc. See comment above for more eliminable cases which
2564 we can't identify at this time. */
2565 if (info->symbolic
2566 && h->root.type != bfd_link_hash_defweak
2567 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2568 break;
2571 /* We create a reloc section in dynobj and make room for this
2572 reloc. */
2573 if (sreloc == NULL)
2575 const char *name;
2577 name = (bfd_elf_string_from_elf_section
2578 (abfd,
2579 elf_elfheader (abfd)->e_shstrndx,
2580 elf_section_data (sec)->rel_hdr.sh_name));
2581 if (name == NULL)
2582 return false;
2584 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2585 && strcmp (bfd_get_section_name (abfd, sec),
2586 name + 5) == 0);
2588 sreloc = bfd_get_section_by_name (dynobj, name);
2589 if (sreloc == NULL)
2591 sreloc = bfd_make_section (dynobj, name);
2592 if (sreloc == NULL
2593 || !bfd_set_section_flags (dynobj, sreloc,
2594 (SEC_ALLOC
2595 | SEC_LOAD
2596 | SEC_HAS_CONTENTS
2597 | SEC_IN_MEMORY
2598 | SEC_LINKER_CREATED
2599 | SEC_READONLY))
2600 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2601 return false;
2603 if (sec->flags & SEC_READONLY)
2604 info->flags |= DF_TEXTREL;
2607 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2609 /* If we are linking with -Bsymbolic, we count the number of PC
2610 relative relocations we have entered for this symbol, so that
2611 we can discard them again if the symbol is later defined by a
2612 regular object. We know that h is really a pointer to an
2613 elf_cris_link_hash_entry. */
2614 if ((r_type == R_CRIS_8_PCREL
2615 || r_type == R_CRIS_16_PCREL
2616 || r_type == R_CRIS_32_PCREL)
2617 && info->symbolic)
2619 struct elf_cris_link_hash_entry *eh;
2620 struct elf_cris_pcrel_relocs_copied *p;
2622 eh = (struct elf_cris_link_hash_entry *) h;
2624 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2625 if (p->section == sreloc)
2626 break;
2628 if (p == NULL)
2630 p = ((struct elf_cris_pcrel_relocs_copied *)
2631 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2632 if (p == NULL)
2633 return false;
2634 p->next = eh->pcrel_relocs_copied;
2635 eh->pcrel_relocs_copied = p;
2636 p->section = sreloc;
2637 p->count = 0;
2640 ++p->count;
2642 break;
2644 /* This relocation describes the C++ object vtable hierarchy.
2645 Reconstruct it for later use during GC. */
2646 case R_CRIS_GNU_VTINHERIT:
2647 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2648 return false;
2649 break;
2651 /* This relocation describes which C++ vtable entries are actually
2652 used. Record for later use during GC. */
2653 case R_CRIS_GNU_VTENTRY:
2654 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2655 return false;
2656 break;
2658 default:
2659 /* Other relocs do not appear here. */
2660 bfd_set_error (bfd_error_bad_value);
2661 return false;
2665 return true;
2668 /* Set the sizes of the dynamic sections. */
2670 static boolean
2671 elf_cris_size_dynamic_sections (output_bfd, info)
2672 bfd *output_bfd ATTRIBUTE_UNUSED;
2673 struct bfd_link_info *info;
2675 bfd *dynobj;
2676 asection *s;
2677 boolean plt;
2678 boolean relocs;
2680 dynobj = elf_hash_table (info)->dynobj;
2681 BFD_ASSERT (dynobj != NULL);
2683 if (elf_hash_table (info)->dynamic_sections_created)
2685 /* Set the contents of the .interp section to the interpreter. */
2686 if (!info->shared)
2688 s = bfd_get_section_by_name (dynobj, ".interp");
2689 BFD_ASSERT (s != NULL);
2690 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2691 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2694 else
2696 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2697 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2698 elf_cris_adjust_gotplt_to_got,
2699 (PTR) info);
2701 /* We may have created entries in the .rela.got section.
2702 However, if we are not creating the dynamic sections, we will
2703 not actually use these entries. Reset the size of .rela.got,
2704 which will cause it to get stripped from the output file
2705 below. */
2706 s = bfd_get_section_by_name (dynobj, ".rela.got");
2707 if (s != NULL)
2708 s->_raw_size = 0;
2711 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2712 relative relocs against symbols defined in a regular object. We
2713 allocated space for them in the check_relocs routine, but we will not
2714 fill them in in the relocate_section routine. We also discard space
2715 for relocs that have become for local symbols due to symbol
2716 visibility changes. For programs, we discard space for relocs for
2717 symbols not referenced by any dynamic object. */
2718 if (info->shared)
2719 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2720 elf_cris_discard_excess_dso_dynamics,
2721 (PTR) info);
2722 else
2723 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2724 elf_cris_discard_excess_program_dynamics,
2725 (PTR) info);
2727 /* The check_relocs and adjust_dynamic_symbol entry points have
2728 determined the sizes of the various dynamic sections. Allocate
2729 memory for them. */
2730 plt = false;
2731 relocs = false;
2732 for (s = dynobj->sections; s != NULL; s = s->next)
2734 const char *name;
2735 boolean strip;
2737 if ((s->flags & SEC_LINKER_CREATED) == 0)
2738 continue;
2740 /* It's OK to base decisions on the section name, because none
2741 of the dynobj section names depend upon the input files. */
2742 name = bfd_get_section_name (dynobj, s);
2744 strip = false;
2746 if (strcmp (name, ".plt") == 0)
2748 if (s->_raw_size == 0)
2750 /* Strip this section if we don't need it; see the
2751 comment below. */
2752 strip = true;
2754 else
2756 /* Remember whether there is a PLT. */
2757 plt = true;
2760 else if (strncmp (name, ".rela", 5) == 0)
2762 if (s->_raw_size == 0)
2764 /* If we don't need this section, strip it from the
2765 output file. This is mostly to handle .rela.bss and
2766 .rela.plt. We must create both sections in
2767 create_dynamic_sections, because they must be created
2768 before the linker maps input sections to output
2769 sections. The linker does that before
2770 adjust_dynamic_symbol is called, and it is that
2771 function which decides whether anything needs to go
2772 into these sections. */
2773 strip = true;
2775 else
2777 /* Remember whether there are any reloc sections other
2778 than .rela.plt. */
2779 if (strcmp (name, ".rela.plt") != 0)
2780 relocs = true;
2782 /* We use the reloc_count field as a counter if we need
2783 to copy relocs into the output file. */
2784 s->reloc_count = 0;
2787 else if (strncmp (name, ".got", 4) != 0)
2789 /* It's not one of our sections, so don't allocate space. */
2790 continue;
2793 if (strip)
2795 _bfd_strip_section_from_output (info, s);
2796 continue;
2799 /* Allocate memory for the section contents. We use bfd_zalloc here
2800 in case unused entries are not reclaimed before the section's
2801 contents are written out. This should not happen, but this way
2802 if it does, we will not write out garbage. For reloc sections,
2803 this will make entries have the type R_CRIS_NONE. */
2804 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2805 if (s->contents == NULL && s->_raw_size != 0)
2806 return false;
2809 if (elf_hash_table (info)->dynamic_sections_created)
2811 /* Add some entries to the .dynamic section. We fill in the
2812 values later, in elf_cris_finish_dynamic_sections, but we
2813 must add the entries now so that we get the correct size for
2814 the .dynamic section. The DT_DEBUG entry is filled in by the
2815 dynamic linker and used by the debugger. */
2816 #define add_dynamic_entry(TAG, VAL) \
2817 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2819 if (!info->shared)
2821 if (!add_dynamic_entry (DT_DEBUG, 0))
2822 return false;
2825 if (plt)
2827 if (!add_dynamic_entry (DT_PLTGOT, 0)
2828 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2829 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2830 || !add_dynamic_entry (DT_JMPREL, 0))
2831 return false;
2834 if (relocs)
2836 if (!add_dynamic_entry (DT_RELA, 0)
2837 || !add_dynamic_entry (DT_RELASZ, 0)
2838 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2839 return false;
2842 if ((info->flags & DF_TEXTREL) != 0)
2844 if (!add_dynamic_entry (DT_TEXTREL, 0))
2845 return false;
2846 info->flags |= DF_TEXTREL;
2849 #undef add_dynamic_entry
2851 return true;
2854 /* This function is called via elf_cris_link_hash_traverse if we are
2855 creating a shared object. In the -Bsymbolic case, it discards the
2856 space allocated to copy PC relative relocs against symbols which
2857 are defined in regular objects. For the normal non-symbolic case,
2858 we also discard space for relocs that have become local due to
2859 symbol visibility changes. We allocated space for them in the
2860 check_relocs routine, but we won't fill them in in the
2861 relocate_section routine. */
2863 static boolean
2864 elf_cris_discard_excess_dso_dynamics (h, inf)
2865 struct elf_cris_link_hash_entry *h;
2866 PTR inf;
2868 struct elf_cris_pcrel_relocs_copied *s;
2869 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2871 /* If a symbol has been forced local or we have found a regular
2872 definition for the symbolic link case, then we won't be needing
2873 any relocs. */
2874 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2875 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2876 || info->symbolic))
2878 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2879 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2882 return true;
2885 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2886 creating a shared object. We discard space for relocs for symbols put
2887 in the .got, but which we found we do not have to resolve at run-time. */
2889 static boolean
2890 elf_cris_discard_excess_program_dynamics (h, inf)
2891 struct elf_cris_link_hash_entry *h;
2892 PTR inf;
2894 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2896 /* If we're not creating a shared library and have a symbol which is
2897 referred to by .got references, but the symbol is defined locally,
2898 (or rather, not referred to by a DSO and not defined by a DSO) then
2899 lose the reloc for the .got (don't allocate room for it). */
2900 if ((h->root.elf_link_hash_flags
2901 & (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_DEF_DYNAMIC)) == 0)
2903 if (h->root.got.refcount > 0
2904 /* The size of this section is only valid and in sync with the
2905 various reference counts if we do dynamic; don't decrement it
2906 otherwise. */
2907 && elf_hash_table (info)->dynamic_sections_created)
2909 bfd *dynobj = elf_hash_table (info)->dynobj;
2910 asection *srelgot;
2912 BFD_ASSERT (dynobj != NULL);
2914 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2916 BFD_ASSERT (srelgot != NULL);
2918 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2921 /* If the locally-defined symbol isn't used by a DSO, then we don't
2922 have to export it as a dynamic symbol. This was already done for
2923 functions; doing this for all symbols would presumably not
2924 introduce new problems. */
2925 h->root.dynindx = -1;
2928 return true;
2931 /* Reject a file depending on presence and expectation of prefixed
2932 underscores on symbols. */
2934 static boolean
2935 cris_elf_object_p (abfd)
2936 bfd *abfd;
2938 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2939 return (bfd_get_symbol_leading_char (abfd) == '_');
2940 else
2941 return (bfd_get_symbol_leading_char (abfd) == 0);
2944 /* Mark presence or absence of leading underscore. */
2946 static void
2947 cris_elf_final_write_processing (abfd, linker)
2948 bfd *abfd;
2949 boolean linker ATTRIBUTE_UNUSED;
2951 if (bfd_get_symbol_leading_char (abfd) == '_')
2952 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2953 else
2954 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2957 /* Display the flags field. */
2959 static boolean
2960 cris_elf_print_private_bfd_data (abfd, ptr)
2961 bfd *abfd;
2962 PTR ptr;
2964 FILE *file = (FILE *) ptr;
2966 BFD_ASSERT (abfd != NULL && ptr != NULL)
2968 _bfd_elf_print_private_bfd_data (abfd, ptr);
2970 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2972 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2973 fprintf (file, _(" [symbols have a _ prefix]"));
2975 fputc ('\n', file);
2976 return true;
2979 /* Don't mix files with and without a leading underscore. */
2981 static boolean
2982 cris_elf_merge_private_bfd_data (ibfd, obfd)
2983 bfd *ibfd;
2984 bfd *obfd;
2986 flagword old_flags, new_flags;
2988 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2989 return false;
2991 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2992 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2993 return true;
2995 if (! elf_flags_init (obfd))
2997 /* This happens when ld starts out with a 'blank' output file. */
2998 elf_flags_init (obfd) = true;
3000 /* Set flags according to current bfd_target. */
3001 cris_elf_final_write_processing (obfd, false);
3004 old_flags = elf_elfheader (obfd)->e_flags;
3005 new_flags = elf_elfheader (ibfd)->e_flags;
3007 /* Is this good or bad? We'll follow with other excluding flags. */
3008 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
3010 (*_bfd_error_handler)
3011 ((new_flags & EF_CRIS_UNDERSCORE)
3012 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3013 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3014 bfd_archive_filename (ibfd));
3015 bfd_set_error (bfd_error_bad_value);
3016 return false;
3019 return true;
3023 static enum elf_reloc_type_class
3024 elf_cris_reloc_type_class (rela)
3025 const Elf_Internal_Rela *rela;
3027 switch ((int) ELF32_R_TYPE (rela->r_info))
3029 case R_CRIS_RELATIVE:
3030 return reloc_class_relative;
3031 case R_CRIS_JUMP_SLOT:
3032 return reloc_class_plt;
3033 case R_CRIS_COPY:
3034 return reloc_class_copy;
3035 default:
3036 return reloc_class_normal;
3040 #define ELF_ARCH bfd_arch_cris
3041 #define ELF_MACHINE_CODE EM_CRIS
3042 #define ELF_MAXPAGESIZE 0x2000
3044 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3045 #define TARGET_LITTLE_NAME "elf32-cris"
3046 #define elf_symbol_leading_char 0
3048 #define elf_info_to_howto_rel NULL
3049 #define elf_info_to_howto cris_info_to_howto_rela
3050 #define elf_backend_relocate_section cris_elf_relocate_section
3051 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3052 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3053 #define elf_backend_check_relocs cris_elf_check_relocs
3054 #define elf_backend_grok_prstatus cris_elf_grok_prstatus
3055 #define elf_backend_grok_psinfo cris_elf_grok_psinfo
3057 #define elf_backend_can_gc_sections 1
3058 #define elf_backend_can_refcount 1
3060 #define elf_backend_object_p cris_elf_object_p
3061 #define elf_backend_final_write_processing \
3062 cris_elf_final_write_processing
3063 #define bfd_elf32_bfd_print_private_bfd_data \
3064 cris_elf_print_private_bfd_data
3065 #define bfd_elf32_bfd_merge_private_bfd_data \
3066 cris_elf_merge_private_bfd_data
3068 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3070 #define bfd_elf32_bfd_link_hash_table_create \
3071 elf_cris_link_hash_table_create
3072 #define elf_backend_adjust_dynamic_symbol \
3073 elf_cris_adjust_dynamic_symbol
3074 #define elf_backend_size_dynamic_sections \
3075 elf_cris_size_dynamic_sections
3076 #define elf_backend_finish_dynamic_symbol \
3077 elf_cris_finish_dynamic_symbol
3078 #define elf_backend_finish_dynamic_sections \
3079 elf_cris_finish_dynamic_sections
3080 #define elf_backend_create_dynamic_sections \
3081 _bfd_elf_create_dynamic_sections
3082 #define bfd_elf32_bfd_final_link \
3083 _bfd_elf32_gc_common_final_link
3084 #define elf_backend_hide_symbol elf_cris_hide_symbol
3085 #define elf_backend_reloc_type_class elf_cris_reloc_type_class
3087 #define elf_backend_want_got_plt 1
3088 #define elf_backend_plt_readonly 1
3089 #define elf_backend_want_plt_sym 0
3090 #define elf_backend_got_header_size 12
3091 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3093 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3094 linking and libraries (if it's a win of any significance). Until then,
3095 take the easy route. */
3096 #define elf_backend_may_use_rel_p 0
3097 #define elf_backend_may_use_rela_p 1
3099 #include "elf32-target.h"
3101 #define INCLUDED_TARGET_FILE
3103 #undef TARGET_LITTLE_SYM
3104 #undef TARGET_LITTLE_NAME
3105 #undef elf_symbol_leading_char
3107 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3108 #define TARGET_LITTLE_NAME "elf32-us-cris"
3109 #define elf_symbol_leading_char '_'
3111 #include "elf32-target.h"