daily update
[binutils.git] / bfd / elf32-cris.c
blob2c634740668de180405f62a44081bb3fa12729c8
1 /* CRIS-specific support for 32-bit ELF.
2 Copyright 2000, 2001, 2002, 2003 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 *, Elf_Internal_Rela *));
36 static bfd_boolean cris_elf_grok_prstatus
37 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
39 static bfd_boolean cris_elf_grok_psinfo
40 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
42 static bfd_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 bfd_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 ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
56 struct elf_link_hash_entry *, Elf_Internal_Sym *));
58 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
60 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
62 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
64 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
66 struct elf_cris_link_hash_entry;
67 static bfd_boolean elf_cris_discard_excess_dso_dynamics
68 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static bfd_boolean elf_cris_discard_excess_program_dynamics
70 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
71 static bfd_boolean elf_cris_adjust_gotplt_to_got
72 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
73 static bfd_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 bfd_boolean elf_cris_adjust_dynamic_symbol
80 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
81 static bfd_boolean cris_elf_check_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection *,
83 const Elf_Internal_Rela *));
85 static bfd_boolean elf_cris_size_dynamic_sections
86 PARAMS ((bfd *, struct bfd_link_info *));
87 static bfd_boolean elf_cris_finish_dynamic_symbol
88 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
89 Elf_Internal_Sym *));
90 static bfd_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 *, bfd_boolean));
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_PCREL", /* 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_PCREL", /* 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 Elf_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 bfd_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 bfd_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 (bfd_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_malloc (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 free (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 bfd_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 if (info->relocatable)
803 return TRUE;
805 dynobj = elf_hash_table (info)->dynobj;
806 local_got_offsets = elf_local_got_offsets (input_bfd);
807 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
808 sym_hashes = elf_sym_hashes (input_bfd);
809 relend = relocs + input_section->reloc_count;
811 sgot = NULL;
812 splt = NULL;
813 sreloc = NULL;
815 if (dynobj != NULL)
817 splt = bfd_get_section_by_name (dynobj, ".plt");
818 sgot = bfd_get_section_by_name (dynobj, ".got");
821 for (rel = relocs; rel < relend; rel ++)
823 reloc_howto_type *howto;
824 unsigned long r_symndx;
825 Elf_Internal_Sym *sym;
826 asection *sec;
827 struct elf_link_hash_entry *h;
828 bfd_vma relocation;
829 bfd_reloc_status_type r;
830 const char *symname = NULL;
831 int r_type;
833 r_type = ELF32_R_TYPE (rel->r_info);
835 if ( r_type == R_CRIS_GNU_VTINHERIT
836 || r_type == R_CRIS_GNU_VTENTRY)
837 continue;
839 /* This is a final link. */
840 r_symndx = ELF32_R_SYM (rel->r_info);
841 howto = cris_elf_howto_table + r_type;
842 h = NULL;
843 sym = NULL;
844 sec = NULL;
846 if (r_symndx < symtab_hdr->sh_info)
848 sym = local_syms + r_symndx;
849 sec = local_sections [r_symndx];
850 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
852 symname = (bfd_elf_string_from_elf_section
853 (input_bfd, symtab_hdr->sh_link, sym->st_name));
854 if (symname == NULL)
855 symname = bfd_section_name (input_bfd, sec);
857 else
859 bfd_boolean warned;
860 bfd_boolean unresolved_reloc;
862 RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx, symtab_hdr, relocation, sec, unresolved_reloc, info, warned);
864 if (unresolved_reloc
865 /* Perhaps we should detect the cases that
866 sec->output_section is expected to be NULL like i386 and
867 m68k, but apparently (and according to elfxx-ia64.c) all
868 valid cases are where the symbol is defined in a shared
869 object which we link dynamically against. This includes
870 PLT relocs for which we've created a PLT entry and other
871 relocs for which we're prepared to create dynamic
872 relocations.
874 For now, new situations cause us to just err when
875 sec->output_offset is NULL but the object with the symbol
876 is *not* dynamically linked against. Thus this will
877 automatically remind us so we can see if there are other
878 valid cases we need to revisit. */
879 && (sec->owner->flags & DYNAMIC) != 0)
880 relocation = 0;
882 else if (h->root.type == bfd_link_hash_defined
883 || h->root.type == bfd_link_hash_defweak)
885 /* Here follow the cases where the relocation value must
886 be zero (or when further handling is simplified when
887 zero). I can't claim to understand the various
888 conditions and they weren't described in the files
889 where I copied them from (elf32-m68k.c and
890 elf32-i386.c), but let's mention examples of where
891 they happen. FIXME: Perhaps define and use a
892 dynamic_symbol_p function like ia64.
894 - When creating a shared library, we can have an
895 ordinary relocation for a symbol defined in a shared
896 library (perhaps the one we create). We then make
897 the relocation value zero, as the value seen now will
898 be added into the relocation addend in this shared
899 library, but must be handled only at dynamic-link
900 time. FIXME: Not sure this example covers the
901 h->elf_link_hash_flags test, though it's there in
902 other targets. */
903 if (info->shared
904 && ((! info->symbolic && h->dynindx != -1)
905 || (h->elf_link_hash_flags
906 & ELF_LINK_HASH_DEF_REGULAR) == 0)
907 && (input_section->flags & SEC_ALLOC) != 0
908 && (r_type == R_CRIS_8
909 || r_type == R_CRIS_16
910 || r_type == R_CRIS_32
911 || r_type == R_CRIS_8_PCREL
912 || r_type == R_CRIS_16_PCREL
913 || r_type == R_CRIS_32_PCREL))
914 relocation = 0;
915 else if (unresolved_reloc)
917 _bfd_error_handler
918 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
919 bfd_archive_filename (input_bfd),
920 cris_elf_howto_table[r_type].name,
921 symname,
922 bfd_get_section_name (input_bfd, input_section));
923 bfd_set_error (bfd_error_bad_value);
924 return FALSE;
929 switch (r_type)
931 case R_CRIS_16_GOTPLT:
932 case R_CRIS_32_GOTPLT:
933 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
934 but we require a PLT, and the PLT handling will take care of
935 filling in the PLT-specific GOT entry. For the GOT offset,
936 calculate it as we do when filling it in for the .got.plt
937 section. If we don't have a PLT, punt to GOT handling. */
938 if (h != NULL
939 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
941 asection *sgotplt
942 = bfd_get_section_by_name (dynobj, ".got.plt");
943 bfd_vma got_offset;
945 BFD_ASSERT (h->dynindx != -1);
946 BFD_ASSERT (sgotplt != NULL);
948 got_offset
949 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
951 relocation = got_offset;
952 break;
955 /* We didn't make a PLT entry for this symbol. Maybe everything is
956 folded into the GOT. Other than folding, this happens when
957 statically linking PIC code, or when using -Bsymbolic. Check
958 that we instead have a GOT entry as done for us by
959 elf_cris_adjust_dynamic_symbol, and drop through into the
960 ordinary GOT cases. */
961 if (h != NULL && h->got.offset == (bfd_vma) -1)
963 (*_bfd_error_handler)
964 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
965 bfd_archive_filename (input_bfd),
966 cris_elf_howto_table[r_type].name,
967 symname[0] != '\0' ? symname : _("[whose name is lost]"),
968 bfd_get_section_name (input_bfd, input_section));
970 /* FIXME: Perhaps blaming input is not the right thing to
971 do; this is probably an internal error. But it is true
972 that we didn't like that particular input. */
973 bfd_set_error (bfd_error_bad_value);
974 return FALSE;
976 /* Fall through. */
978 /* The size of the actual relocation is not used here; we only
979 fill in the GOT table here. */
980 case R_CRIS_16_GOT:
981 case R_CRIS_32_GOT:
983 bfd_vma off;
985 /* Note that despite using RELA relocations, the .got contents
986 is always filled in with the link-relative relocation
987 value; the addend. */
989 if (h != NULL)
991 off = h->got.offset;
992 BFD_ASSERT (off != (bfd_vma) -1);
994 if (!elf_hash_table (info)->dynamic_sections_created
995 || (! info->shared
996 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
997 || (info->shared
998 && (info->symbolic || h->dynindx == -1)
999 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1001 /* This wasn't checked above for ! info->shared, but
1002 must hold there if we get here; the symbol must be
1003 defined in the regular program, or be undefweak. */
1004 BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1005 || info->shared
1006 || (h->elf_link_hash_flags
1007 & ELF_LINK_HASH_DEF_REGULAR) != 0
1008 || h->root.type == bfd_link_hash_undefweak);
1010 /* This is actually a static link, or it is a
1011 -Bsymbolic link and the symbol is defined locally,
1012 or is undefweak, or the symbol was forced to be
1013 local because of a version file, or we're not
1014 creating a dynamic object. We must initialize this
1015 entry in the global offset table. Since the offset
1016 must always be a multiple of 4, we use the least
1017 significant bit to record whether we have
1018 initialized it already.
1020 If this GOT entry should be runtime-initialized, we
1021 will create a .rela.got relocation entry to
1022 initialize the value. This is done in the
1023 finish_dynamic_symbol routine. */
1024 if ((off & 1) != 0)
1025 off &= ~1;
1026 else
1028 bfd_put_32 (output_bfd, relocation,
1029 sgot->contents + off);
1030 h->got.offset |= 1;
1034 else
1036 BFD_ASSERT (local_got_offsets != NULL
1037 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1039 off = local_got_offsets[r_symndx];
1041 /* The offset must always be a multiple of 4. We use
1042 the least significant bit to record whether we have
1043 already generated the necessary reloc. */
1044 if ((off & 1) != 0)
1045 off &= ~1;
1046 else
1048 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1050 if (info->shared)
1052 asection *s;
1053 Elf_Internal_Rela outrel;
1054 bfd_byte *loc;
1056 s = bfd_get_section_by_name (dynobj, ".rela.got");
1057 BFD_ASSERT (s != NULL);
1059 outrel.r_offset = (sgot->output_section->vma
1060 + sgot->output_offset
1061 + off);
1062 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1063 outrel.r_addend = relocation;
1064 loc = s->contents;
1065 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1066 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1069 local_got_offsets[r_symndx] |= 1;
1073 relocation = sgot->output_offset + off;
1074 if (rel->r_addend != 0)
1076 /* We can't do anything for a relocation which is against
1077 a symbol *plus offset*. GOT holds relocations for
1078 symbols. Make this an error; the compiler isn't
1079 allowed to pass us these kinds of things. */
1080 if (h == NULL)
1081 (*_bfd_error_handler)
1082 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1083 bfd_archive_filename (input_bfd),
1084 cris_elf_howto_table[r_type].name,
1085 rel->r_addend,
1086 bfd_get_section_name (input_bfd, input_section));
1087 else
1088 (*_bfd_error_handler)
1089 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1090 bfd_archive_filename (input_bfd),
1091 cris_elf_howto_table[r_type].name,
1092 rel->r_addend,
1093 symname[0] != '\0' ? symname : _("[whose name is lost]"),
1094 bfd_get_section_name (input_bfd, input_section));
1096 bfd_set_error (bfd_error_bad_value);
1097 return FALSE;
1100 break;
1102 case R_CRIS_32_GOTREL:
1103 /* This relocation must only be performed against local symbols.
1104 It's also ok when we link a program and the symbol is either
1105 defined in an ordinary (non-DSO) object or is undefined weak. */
1106 if (h != NULL
1107 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1108 && !(!info->shared
1109 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1110 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1111 && h->root.type == bfd_link_hash_undefweak))))
1113 (*_bfd_error_handler)
1114 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1115 bfd_archive_filename (input_bfd),
1116 cris_elf_howto_table[r_type].name,
1117 symname,
1118 bfd_get_section_name (input_bfd, input_section));
1119 bfd_set_error (bfd_error_bad_value);
1120 return FALSE;
1123 /* This can happen if we get a link error with the input ELF
1124 variant mismatching the output variant. Emit an error so
1125 it's noticed if it happens elsewhere. */
1126 if (sgot == NULL)
1128 (*_bfd_error_handler)
1129 (_("%s: relocation %s in section %s with no GOT created"),
1130 bfd_archive_filename (input_bfd),
1131 cris_elf_howto_table[r_type].name,
1132 bfd_get_section_name (input_bfd, input_section));
1133 bfd_set_error (bfd_error_bad_value);
1134 return FALSE;
1137 /* This relocation is like a PC-relative one, except the
1138 reference point is the location of GOT. Note that
1139 sgot->output_offset is not involved in this calculation. We
1140 always want the start of entire .got section, not the
1141 position after the reserved header. */
1142 relocation -= sgot->output_section->vma;
1143 break;
1145 case R_CRIS_32_PLT_PCREL:
1146 /* Relocation is to the entry for this symbol in the
1147 procedure linkage table. */
1149 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1150 without using the procedure linkage table. */
1151 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1152 break;
1154 if (h->plt.offset == (bfd_vma) -1
1155 || splt == NULL)
1157 /* We didn't make a PLT entry for this symbol. This
1158 happens when statically linking PIC code, or when
1159 using -Bsymbolic. */
1160 break;
1163 relocation = (splt->output_section->vma
1164 + splt->output_offset
1165 + h->plt.offset);
1166 break;
1168 case R_CRIS_32_PLT_GOTREL:
1169 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1170 start of the .got section. See also comment at
1171 R_CRIS_32_GOT. */
1172 relocation -= sgot->output_section->vma;
1174 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1175 without using the procedure linkage table. */
1176 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1177 break;
1179 if (h->plt.offset == (bfd_vma) -1
1180 || splt == NULL)
1182 /* We didn't make a PLT entry for this symbol. This
1183 happens when statically linking PIC code, or when
1184 using -Bsymbolic. */
1185 break;
1188 relocation = (splt->output_section->vma
1189 + splt->output_offset
1190 + h->plt.offset
1191 - sgot->output_section->vma);
1192 break;
1194 case R_CRIS_8_PCREL:
1195 case R_CRIS_16_PCREL:
1196 case R_CRIS_32_PCREL:
1197 /* If the symbol was local, we need no shlib-specific handling. */
1198 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1199 break;
1201 /* Fall through. */
1202 case R_CRIS_8:
1203 case R_CRIS_16:
1204 case R_CRIS_32:
1205 if (info->shared
1206 && r_symndx != 0
1207 && (input_section->flags & SEC_ALLOC) != 0
1208 && ((r_type != R_CRIS_8_PCREL
1209 && r_type != R_CRIS_16_PCREL
1210 && r_type != R_CRIS_32_PCREL)
1211 || (!info->symbolic
1212 || (h->elf_link_hash_flags
1213 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1215 Elf_Internal_Rela outrel;
1216 bfd_byte *loc;
1217 bfd_boolean skip, relocate;
1219 /* When generating a shared object, these relocations
1220 are copied into the output file to be resolved at run
1221 time. */
1223 if (sreloc == NULL)
1225 const char *name;
1227 name = (bfd_elf_string_from_elf_section
1228 (input_bfd,
1229 elf_elfheader (input_bfd)->e_shstrndx,
1230 elf_section_data (input_section)->rel_hdr.sh_name));
1231 if (name == NULL)
1232 return FALSE;
1234 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1235 && strcmp (bfd_get_section_name (input_bfd,
1236 input_section),
1237 name + 5) == 0);
1239 sreloc = bfd_get_section_by_name (dynobj, name);
1241 /* That section should have been created in
1242 cris_elf_check_relocs, but that function will not be
1243 called for objects which fail in
1244 cris_elf_merge_private_bfd_data. */
1245 if (sreloc == NULL)
1247 (*_bfd_error_handler)
1248 (_("%s: Internal inconsistency; no relocation section %s"),
1249 bfd_archive_filename (input_bfd),
1250 name);
1252 bfd_set_error (bfd_error_bad_value);
1253 return FALSE;
1257 skip = FALSE;
1258 relocate = FALSE;
1260 outrel.r_offset =
1261 _bfd_elf_section_offset (output_bfd, info, input_section,
1262 rel->r_offset);
1263 if (outrel.r_offset == (bfd_vma) -1)
1264 skip = TRUE;
1265 else if (outrel.r_offset == (bfd_vma) -2)
1266 skip = TRUE, relocate = TRUE;
1267 outrel.r_offset += (input_section->output_section->vma
1268 + input_section->output_offset);
1270 if (skip)
1271 memset (&outrel, 0, sizeof outrel);
1272 /* h->dynindx may be -1 if the symbol was marked to
1273 become local. */
1274 else if (h != NULL
1275 && ((! info->symbolic && h->dynindx != -1)
1276 || (h->elf_link_hash_flags
1277 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1279 BFD_ASSERT (h->dynindx != -1);
1280 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1281 outrel.r_addend = relocation + rel->r_addend;
1283 else
1285 if (r_type == R_CRIS_32)
1287 relocate = TRUE;
1288 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1289 outrel.r_addend = relocation + rel->r_addend;
1291 else
1293 long indx;
1295 if (bfd_is_abs_section (sec))
1296 indx = 0;
1297 else if (sec == NULL || sec->owner == NULL)
1299 bfd_set_error (bfd_error_bad_value);
1300 return FALSE;
1302 else
1304 asection *osec;
1306 osec = sec->output_section;
1307 indx = elf_section_data (osec)->dynindx;
1308 BFD_ASSERT (indx > 0);
1311 outrel.r_info = ELF32_R_INFO (indx, r_type);
1312 outrel.r_addend = relocation + rel->r_addend;
1316 loc = sreloc->contents;
1317 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1318 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1320 /* This reloc will be computed at runtime, so there's no
1321 need to do anything now, except for R_CRIS_32 relocations
1322 that have been turned into R_CRIS_RELATIVE. */
1323 if (!relocate)
1324 continue;
1327 break;
1330 r = cris_final_link_relocate (howto, input_bfd, input_section,
1331 contents, rel, relocation);
1333 if (r != bfd_reloc_ok)
1335 const char * msg = (const char *) NULL;
1337 switch (r)
1339 case bfd_reloc_overflow:
1340 r = info->callbacks->reloc_overflow
1341 (info, symname, howto->name, (bfd_vma) 0,
1342 input_bfd, input_section, rel->r_offset);
1343 break;
1345 case bfd_reloc_undefined:
1346 r = info->callbacks->undefined_symbol
1347 (info, symname, input_bfd, input_section, rel->r_offset,
1348 TRUE);
1349 break;
1351 case bfd_reloc_outofrange:
1352 msg = _("internal error: out of range error");
1353 break;
1355 case bfd_reloc_notsupported:
1356 msg = _("internal error: unsupported relocation error");
1357 break;
1359 case bfd_reloc_dangerous:
1360 msg = _("internal error: dangerous relocation");
1361 break;
1363 default:
1364 msg = _("internal error: unknown error");
1365 break;
1368 if (msg)
1369 r = info->callbacks->warning
1370 (info, msg, symname, input_bfd, input_section, rel->r_offset);
1372 if (! r)
1373 return FALSE;
1377 return TRUE;
1380 /* Finish up dynamic symbol handling. We set the contents of various
1381 dynamic sections here. */
1383 static bfd_boolean
1384 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1385 bfd *output_bfd;
1386 struct bfd_link_info *info;
1387 struct elf_link_hash_entry *h;
1388 Elf_Internal_Sym *sym;
1390 bfd *dynobj;
1391 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1393 dynobj = elf_hash_table (info)->dynobj;
1395 if (h->plt.offset != (bfd_vma) -1)
1397 asection *splt;
1398 asection *sgotplt;
1399 asection *sgot;
1400 asection *srela;
1401 bfd_vma got_base;
1403 bfd_vma gotplt_offset
1404 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1405 Elf_Internal_Rela rela;
1406 bfd_byte *loc;
1407 bfd_boolean has_gotplt = gotplt_offset != 0;
1409 /* Get the index in the procedure linkage table which
1410 corresponds to this symbol. This is the index of this symbol
1411 in all the symbols for which we are making plt entries. The
1412 first entry in the procedure linkage table is reserved. */
1413 /* We have to count backwards here, and the result is only valid as
1414 an index into .got.plt and its relocations. FIXME: Constants... */
1415 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1417 /* Get the offset into the .got table of the entry that corresponds
1418 to this function. Note that we embed knowledge that "incoming"
1419 .got goes after .got.plt in the output without padding (pointer
1420 aligned). However, that knowledge is present in several other
1421 places too, here and in elflink.h at least. */
1422 bfd_vma got_offset
1423 = (has_gotplt
1424 ? gotplt_offset
1425 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1427 /* This symbol has an entry in the procedure linkage table. Set it
1428 up. */
1430 BFD_ASSERT (h->dynindx != -1);
1432 splt = bfd_get_section_by_name (dynobj, ".plt");
1433 sgot = bfd_get_section_by_name (dynobj, ".got");
1434 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1435 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1436 BFD_ASSERT (splt != NULL && sgotplt != NULL
1437 && (! has_gotplt || srela != NULL));
1439 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1441 /* Fill in the entry in the procedure linkage table. */
1442 if (! info->shared)
1444 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1445 PLT_ENTRY_SIZE);
1447 /* We need to enter the absolute address of the GOT entry here. */
1448 bfd_put_32 (output_bfd, got_base + got_offset,
1449 splt->contents + h->plt.offset + plt_off1);
1451 else
1453 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1454 PLT_ENTRY_SIZE);
1455 bfd_put_32 (output_bfd, got_offset,
1456 splt->contents + h->plt.offset + plt_off1);
1459 /* Fill in the plt entry and make a relocation, if this is a "real"
1460 PLT entry. */
1461 if (has_gotplt)
1463 /* Fill in the offset into the reloc table. */
1464 bfd_put_32 (output_bfd,
1465 gotplt_index * sizeof (Elf32_External_Rela),
1466 splt->contents + h->plt.offset + plt_off2);
1468 /* Fill in the offset to the first PLT entry, where to "jump". */
1469 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1470 splt->contents + h->plt.offset + plt_off3);
1472 /* Fill in the entry in the global offset table with the address of
1473 the relocating stub. */
1474 bfd_put_32 (output_bfd,
1475 (splt->output_section->vma
1476 + splt->output_offset
1477 + h->plt.offset
1478 + 8),
1479 sgotplt->contents + got_offset);
1481 /* Fill in the entry in the .rela.plt section. */
1482 rela.r_offset = (sgotplt->output_section->vma
1483 + sgotplt->output_offset
1484 + got_offset);
1485 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1486 rela.r_addend = 0;
1487 loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
1488 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1491 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1493 /* Mark the symbol as undefined, rather than as defined in
1494 the .plt section. Leave the value alone. */
1495 sym->st_shndx = SHN_UNDEF;
1497 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1498 know whether resetting the value is significant; if it really
1499 is, rather than a quirk or bug in the sparc port, then I
1500 believe we'd see this elsewhere. */
1501 /* If the symbol is weak, we do need to clear the value.
1502 Otherwise, the PLT entry would provide a definition for
1503 the symbol even if the symbol wasn't defined anywhere,
1504 and so the symbol would never be NULL. */
1505 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1506 == 0)
1507 sym->st_value = 0;
1511 /* We don't emit .got relocs for symbols that aren't in the
1512 dynamic-symbols table for an ordinary program and are either defined
1513 by the program or are undefined weak symbols. */
1514 if (h->got.offset != (bfd_vma) -1
1515 && (info->shared
1516 || (h->dynindx != -1
1517 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1518 && h->root.type != bfd_link_hash_undefweak)))
1520 asection *sgot;
1521 asection *srela;
1522 Elf_Internal_Rela rela;
1523 bfd_byte *loc;
1524 bfd_byte *where;
1526 /* This symbol has an entry in the global offset table. Set it up. */
1528 sgot = bfd_get_section_by_name (dynobj, ".got");
1529 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1530 BFD_ASSERT (sgot != NULL && srela != NULL);
1532 rela.r_offset = (sgot->output_section->vma
1533 + sgot->output_offset
1534 + (h->got.offset &~ (bfd_vma) 1));
1536 /* If this is a static link, or it is a -Bsymbolic link and the
1537 symbol is defined locally or was forced to be local because
1538 of a version file, we just want to emit a RELATIVE reloc.
1539 The entry in the global offset table will already have been
1540 initialized in the relocate_section function. */
1541 where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1542 if (! elf_hash_table (info)->dynamic_sections_created
1543 || (info->shared
1544 && (info->symbolic || h->dynindx == -1)
1545 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1547 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1548 rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1550 else
1552 bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1553 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1554 rela.r_addend = 0;
1557 loc = srela->contents;
1558 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1559 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1562 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1564 asection *s;
1565 Elf_Internal_Rela rela;
1566 bfd_byte *loc;
1568 /* This symbol needs a copy reloc. Set it up. */
1570 BFD_ASSERT (h->dynindx != -1
1571 && (h->root.type == bfd_link_hash_defined
1572 || h->root.type == bfd_link_hash_defweak));
1574 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1575 ".rela.bss");
1576 BFD_ASSERT (s != NULL);
1578 rela.r_offset = (h->root.u.def.value
1579 + h->root.u.def.section->output_section->vma
1580 + h->root.u.def.section->output_offset);
1581 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1582 rela.r_addend = 0;
1583 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1584 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1587 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1588 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1589 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1590 sym->st_shndx = SHN_ABS;
1592 return TRUE;
1595 /* Finish up the dynamic sections. */
1597 static bfd_boolean
1598 elf_cris_finish_dynamic_sections (output_bfd, info)
1599 bfd *output_bfd;
1600 struct bfd_link_info *info;
1602 bfd *dynobj;
1603 asection *sgot;
1604 asection *sdyn;
1606 dynobj = elf_hash_table (info)->dynobj;
1608 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1609 BFD_ASSERT (sgot != NULL);
1610 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1612 if (elf_hash_table (info)->dynamic_sections_created)
1614 asection *splt;
1615 Elf32_External_Dyn *dyncon, *dynconend;
1617 splt = bfd_get_section_by_name (dynobj, ".plt");
1618 BFD_ASSERT (splt != NULL && sdyn != NULL);
1620 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1621 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1622 for (; dyncon < dynconend; dyncon++)
1624 Elf_Internal_Dyn dyn;
1625 asection *s;
1627 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1629 switch (dyn.d_tag)
1631 default:
1632 break;
1634 case DT_PLTGOT:
1635 s = bfd_get_section_by_name (output_bfd, ".got");
1636 BFD_ASSERT (s != NULL);
1637 dyn.d_un.d_ptr = s->vma;
1638 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1639 break;
1641 case DT_JMPREL:
1642 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1643 if all symbols are found in the .got (not .got.plt). */
1644 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1645 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1646 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1647 break;
1649 case DT_PLTRELSZ:
1650 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1651 if (s == NULL)
1652 dyn.d_un.d_val = 0;
1653 else if (s->_cooked_size != 0)
1654 dyn.d_un.d_val = s->_cooked_size;
1655 else
1656 dyn.d_un.d_val = s->_raw_size;
1657 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1658 break;
1660 case DT_RELASZ:
1661 /* The procedure linkage table relocs (DT_JMPREL) should
1662 not be included in the overall relocs (DT_RELA).
1663 Therefore, we override the DT_RELASZ entry here to
1664 make it not include the JMPREL relocs. Since the
1665 linker script arranges for .rela.plt to follow all
1666 other relocation sections, we don't have to worry
1667 about changing the DT_RELA entry. */
1668 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1669 if (s != NULL)
1671 if (s->_cooked_size != 0)
1672 dyn.d_un.d_val -= s->_cooked_size;
1673 else
1674 dyn.d_un.d_val -= s->_raw_size;
1676 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1677 break;
1681 /* Fill in the first entry in the procedure linkage table. */
1682 if (splt->_raw_size > 0)
1684 if (info->shared)
1685 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1686 else
1688 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1689 bfd_put_32 (output_bfd,
1690 sgot->output_section->vma + sgot->output_offset + 4,
1691 splt->contents + 6);
1692 bfd_put_32 (output_bfd,
1693 sgot->output_section->vma + sgot->output_offset + 8,
1694 splt->contents + 14);
1696 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1697 = PLT_ENTRY_SIZE;
1702 /* Fill in the first three entries in the global offset table. */
1703 if (sgot->_raw_size > 0)
1705 if (sdyn == NULL)
1706 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1707 else
1708 bfd_put_32 (output_bfd,
1709 sdyn->output_section->vma + sdyn->output_offset,
1710 sgot->contents);
1711 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1712 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1715 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1717 return TRUE;
1720 /* Return the section that should be marked against GC for a given
1721 relocation. */
1723 static asection *
1724 cris_elf_gc_mark_hook (sec, info, rel, h, sym)
1725 asection *sec;
1726 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1727 Elf_Internal_Rela *rel;
1728 struct elf_link_hash_entry *h;
1729 Elf_Internal_Sym *sym;
1731 if (h != NULL)
1733 switch (ELF32_R_TYPE (rel->r_info))
1735 case R_CRIS_GNU_VTINHERIT:
1736 case R_CRIS_GNU_VTENTRY:
1737 break;
1739 default:
1740 switch (h->root.type)
1742 case bfd_link_hash_defined:
1743 case bfd_link_hash_defweak:
1744 return h->root.u.def.section;
1746 case bfd_link_hash_common:
1747 return h->root.u.c.p->section;
1749 default:
1750 break;
1754 else
1755 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1757 return NULL;
1760 /* Update the got entry reference counts for the section being removed. */
1762 static bfd_boolean
1763 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1764 bfd *abfd ATTRIBUTE_UNUSED;
1765 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1766 asection *sec ATTRIBUTE_UNUSED;
1767 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1769 Elf_Internal_Shdr *symtab_hdr;
1770 struct elf_link_hash_entry **sym_hashes;
1771 bfd_signed_vma *local_got_refcounts;
1772 const Elf_Internal_Rela *rel, *relend;
1773 unsigned long r_symndx;
1774 struct elf_link_hash_entry *h;
1775 bfd *dynobj;
1776 asection *sgot;
1777 asection *srelgot;
1779 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1780 sym_hashes = elf_sym_hashes (abfd);
1781 local_got_refcounts = elf_local_got_refcounts (abfd);
1783 dynobj = elf_hash_table (info)->dynobj;
1784 if (dynobj == NULL)
1785 return TRUE;
1787 sgot = bfd_get_section_by_name (dynobj, ".got");
1788 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1790 relend = relocs + sec->reloc_count;
1791 for (rel = relocs; rel < relend; rel++)
1793 switch (ELF32_R_TYPE (rel->r_info))
1795 case R_CRIS_16_GOT:
1796 case R_CRIS_32_GOT:
1797 r_symndx = ELF32_R_SYM (rel->r_info);
1798 if (r_symndx >= symtab_hdr->sh_info)
1800 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1801 if (h->got.refcount > 0)
1803 --h->got.refcount;
1804 if (h->got.refcount == 0)
1806 /* We don't need the .got entry any more. */
1807 sgot->_raw_size -= 4;
1808 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1811 break;
1814 local_got_reloc:
1815 if (local_got_refcounts != NULL)
1817 if (local_got_refcounts[r_symndx] > 0)
1819 --local_got_refcounts[r_symndx];
1820 if (local_got_refcounts[r_symndx] == 0)
1822 /* We don't need the .got entry any more. */
1823 sgot->_raw_size -= 4;
1824 if (info->shared)
1825 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1829 break;
1831 case R_CRIS_16_GOTPLT:
1832 case R_CRIS_32_GOTPLT:
1833 /* For local symbols, treat these like GOT relocs. */
1834 r_symndx = ELF32_R_SYM (rel->r_info);
1835 if (r_symndx < symtab_hdr->sh_info)
1836 goto local_got_reloc;
1838 case R_CRIS_32_PLT_GOTREL:
1839 /* FIXME: We don't garbage-collect away the .got section. */
1840 if (local_got_refcounts != NULL)
1841 local_got_refcounts[-1]--;
1842 /* Fall through. */
1844 case R_CRIS_8_PCREL:
1845 case R_CRIS_16_PCREL:
1846 case R_CRIS_32_PCREL:
1847 case R_CRIS_32_PLT_PCREL:
1848 r_symndx = ELF32_R_SYM (rel->r_info);
1849 if (r_symndx >= symtab_hdr->sh_info)
1851 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1852 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1853 && h->plt.refcount > 0)
1854 --h->plt.refcount;
1856 break;
1858 default:
1859 break;
1863 return TRUE;
1866 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1867 entry but we found we will not create any. Called when we find we will
1868 not have any PLT for this symbol, by for example
1869 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1870 or elf_cris_size_dynamic_sections if no dynamic sections will be
1871 created (we're only linking static objects). */
1873 static bfd_boolean
1874 elf_cris_adjust_gotplt_to_got (h, p)
1875 struct elf_cris_link_hash_entry *h;
1876 PTR p;
1878 struct bfd_link_info *info = (struct bfd_link_info *) p;
1879 bfd *dynobj = elf_hash_table (info)->dynobj;
1881 BFD_ASSERT (dynobj != NULL);
1883 if (h->root.root.type == bfd_link_hash_warning)
1884 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
1886 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1887 if (h->gotplt_refcount <= 0)
1888 return TRUE;
1890 if (h->root.got.refcount > 0)
1892 /* There's a GOT entry for this symbol. Just adjust the refcount.
1893 Probably not necessary at this stage, but keeping it accurate
1894 helps avoiding surprises later. */
1895 h->root.got.refcount += h->gotplt_refcount;
1896 h->gotplt_refcount = -1;
1898 else
1900 /* No GOT entry for this symbol. We need to create one. */
1901 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1902 asection *srelgot
1903 = bfd_get_section_by_name (dynobj, ".rela.got");
1905 /* Put an accurate refcount there. */
1906 h->root.got.refcount = h->gotplt_refcount;
1908 h->gotplt_refcount = -1;
1910 /* We always have a .got and a .rela.got section if there were
1911 GOTPLT relocs in input. */
1912 BFD_ASSERT (sgot != NULL && srelgot != NULL);
1914 /* Allocate space in the .got section. */
1915 sgot->_raw_size += 4;
1917 /* Allocate relocation space. */
1918 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1921 return TRUE;
1924 /* Try to fold PLT entries with GOT entries. There are two cases when we
1925 want to do this:
1927 - When all PLT references are GOTPLT references, and there are GOT
1928 references. We don't have to generate a PLT at all.
1930 - When there are both (ordinary) PLT references and GOT references.
1931 We want to make the PLT reference use the ordinary GOT entry rather
1932 than a run-time dynamically resolved GOTPLT entry (since the GOT
1933 entry will have to be resolved at startup anyway).
1935 Though the latter case is handled when room for the PLT is allocated,
1936 not here.
1938 Note that this function is called before symbols are forced local by
1939 version scripts. The differing cases are handled by
1940 elf_cris_hide_symbol. */
1942 static bfd_boolean
1943 elf_cris_try_fold_plt_to_got (h, p)
1944 struct elf_cris_link_hash_entry *h;
1945 PTR p;
1947 struct bfd_link_info *info = (struct bfd_link_info *) p;
1949 /* If there are no GOT references for this symbol, we can't fold any
1950 other reference so there's nothing to do. Likewise if there are no
1951 PLT references; GOTPLT references included. */
1952 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1953 return TRUE;
1955 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
1956 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1958 if (h->gotplt_refcount == h->root.plt.refcount)
1960 /* The only PLT references are GOTPLT references, and there are GOT
1961 references. Convert PLT to GOT references. */
1962 if (! elf_cris_adjust_gotplt_to_got (h, info))
1963 return FALSE;
1965 /* Clear the PLT references, so no PLT will be created. */
1966 h->root.plt.offset = (bfd_vma) -1;
1969 return TRUE;
1972 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1973 to use a GOT entry (and create one) rather than requiring a GOTPLT
1974 entry. */
1976 static void
1977 elf_cris_hide_symbol (info, h, force_local)
1978 struct bfd_link_info *info;
1979 struct elf_link_hash_entry *h;
1980 bfd_boolean force_local;
1982 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1984 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1987 /* Adjust a symbol defined by a dynamic object and referenced by a
1988 regular object. The current definition is in some section of the
1989 dynamic object, but we're not including those sections. We have to
1990 change the definition to something the rest of the link can
1991 understand. */
1993 static bfd_boolean
1994 elf_cris_adjust_dynamic_symbol (info, h)
1995 struct bfd_link_info *info;
1996 struct elf_link_hash_entry *h;
1998 bfd *dynobj;
1999 asection *s;
2000 unsigned int power_of_two;
2002 dynobj = elf_hash_table (info)->dynobj;
2004 /* Make sure we know what is going on here. */
2005 BFD_ASSERT (dynobj != NULL
2006 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2007 || h->weakdef != NULL
2008 || ((h->elf_link_hash_flags
2009 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2010 && (h->elf_link_hash_flags
2011 & ELF_LINK_HASH_REF_REGULAR) != 0
2012 && (h->elf_link_hash_flags
2013 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2015 /* If this is a function, put it in the procedure linkage table. We
2016 will fill in the contents of the procedure linkage table later,
2017 when we know the address of the .got section. */
2018 if (h->type == STT_FUNC
2019 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2021 /* If we link a program (not a DSO), we'll get rid of unnecessary
2022 PLT entries; we point to the actual symbols -- even for pic
2023 relocs, because a program built with -fpic should have the same
2024 result as one built without -fpic, specifically considering weak
2025 symbols.
2026 FIXME: m68k and i386 differ here, for unclear reasons. */
2027 if (! info->shared
2028 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
2030 /* This case can occur if we saw a PLT reloc in an input file,
2031 but the symbol was not defined by a dynamic object. In such
2032 a case, we don't actually need to build a procedure linkage
2033 table, and we can just do an absolute or PC reloc instead, or
2034 change a .got.plt index to a .got index for GOTPLT relocs. */
2035 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2036 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2037 h->plt.offset = (bfd_vma) -1;
2038 return
2039 elf_cris_adjust_gotplt_to_got ((struct
2040 elf_cris_link_hash_entry *) h,
2041 info);
2044 /* If there are only GOT references and GOTPLT references to this
2045 PLT entry, get rid of the PLT. */
2046 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2047 h, info))
2048 return FALSE;
2050 /* GC or folding may have rendered this entry unused. */
2051 if (h->plt.refcount <= 0)
2053 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2054 h->plt.offset = (bfd_vma) -1;
2055 return TRUE;
2058 /* Make sure this symbol is output as a dynamic symbol. */
2059 if (h->dynindx == -1)
2061 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2062 return FALSE;
2065 s = bfd_get_section_by_name (dynobj, ".plt");
2066 BFD_ASSERT (s != NULL);
2068 /* If this is the first .plt entry, make room for the special
2069 first entry. */
2070 if (s->_raw_size == 0)
2071 s->_raw_size += PLT_ENTRY_SIZE;
2073 /* If this symbol is not defined in a regular file, and we are
2074 not generating a shared library, then set the symbol to this
2075 location in the .plt. */
2076 if (!info->shared
2077 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2079 h->root.u.def.section = s;
2080 h->root.u.def.value = s->_raw_size;
2083 /* If there's already a GOT entry, use that, not a .got.plt. A
2084 GOT field still has a reference count when we get here; it's
2085 not yet changed to an offset. */
2086 if (h->got.refcount > 0)
2088 h->got.refcount += h->plt.refcount;
2090 /* Mark the PLT offset to use the GOT entry by setting the low
2091 bit in the plt offset; it is always a multiple of
2092 pointer-size. */
2093 BFD_ASSERT ((s->_raw_size & 3) == 0);
2095 /* Change the PLT refcount to an offset. */
2096 h->plt.offset = s->_raw_size;
2098 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2099 that the got entry should be used instead. */
2100 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2101 h)->gotplt_offset == 0);
2103 /* Make room for this entry. */
2104 s->_raw_size += PLT_ENTRY_SIZE;
2106 return TRUE;
2109 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2110 h->plt.offset = s->_raw_size;
2112 /* Make room for this entry. */
2113 s->_raw_size += PLT_ENTRY_SIZE;
2115 /* We also need to make an entry in the .got.plt section, which
2116 will be placed in the .got section by the linker script. */
2117 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2118 = elf_cris_hash_table (info)->next_gotplt_entry;
2119 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2121 s = bfd_get_section_by_name (dynobj, ".got.plt");
2122 BFD_ASSERT (s != NULL);
2123 s->_raw_size += 4;
2125 /* We also need to make an entry in the .rela.plt section. */
2127 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2128 BFD_ASSERT (s != NULL);
2129 s->_raw_size += sizeof (Elf32_External_Rela);
2131 return TRUE;
2134 /* Reinitialize the plt offset now that it is not used as a reference
2135 count any more. */
2136 h->plt.offset = (bfd_vma) -1;
2138 /* If this is a weak symbol, and there is a real definition, the
2139 processor independent code will have arranged for us to see the
2140 real definition first, and we can just use the same value. */
2141 if (h->weakdef != NULL)
2143 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2144 || h->weakdef->root.type == bfd_link_hash_defweak);
2145 h->root.u.def.section = h->weakdef->root.u.def.section;
2146 h->root.u.def.value = h->weakdef->root.u.def.value;
2147 return TRUE;
2150 /* This is a reference to a symbol defined by a dynamic object which
2151 is not a function. */
2153 /* If we are creating a shared library, we must presume that the
2154 only references to the symbol are via the global offset table.
2155 For such cases we need not do anything here; the relocations will
2156 be handled correctly by relocate_section. */
2157 if (info->shared)
2158 return TRUE;
2160 /* If there are no references to this symbol that do not use the
2161 GOT, we don't need to generate a copy reloc. */
2162 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2163 return TRUE;
2165 /* We must allocate the symbol in our .dynbss section, which will
2166 become part of the .bss section of the executable. There will be
2167 an entry for this symbol in the .dynsym section. The dynamic
2168 object will contain position independent code, so all references
2169 from the dynamic object to this symbol will go through the global
2170 offset table. The dynamic linker will use the .dynsym entry to
2171 determine the address it must put in the global offset table, so
2172 both the dynamic object and the regular object will refer to the
2173 same memory location for the variable. */
2175 s = bfd_get_section_by_name (dynobj, ".dynbss");
2176 BFD_ASSERT (s != NULL);
2178 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2179 copy the initial value out of the dynamic object and into the
2180 runtime process image. We need to remember the offset into the
2181 .rela.bss section we are going to use. */
2182 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2184 asection *srel;
2186 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2187 BFD_ASSERT (srel != NULL);
2188 srel->_raw_size += sizeof (Elf32_External_Rela);
2189 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2192 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2193 thing to copy; so do we. */
2195 /* We need to figure out the alignment required for this symbol. I
2196 have no idea how ELF linkers handle this. */
2197 power_of_two = bfd_log2 (h->size);
2198 if (power_of_two > 3)
2199 power_of_two = 3;
2201 /* Apply the required alignment. */
2202 s->_raw_size = BFD_ALIGN (s->_raw_size,
2203 (bfd_size_type) (1 << power_of_two));
2204 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2206 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2207 return FALSE;
2210 /* Define the symbol as being at this point in the section. */
2211 h->root.u.def.section = s;
2212 h->root.u.def.value = s->_raw_size;
2214 /* Increment the section size to make room for the symbol. */
2215 s->_raw_size += h->size;
2217 return TRUE;
2220 /* Look through the relocs for a section during the first phase. */
2222 static bfd_boolean
2223 cris_elf_check_relocs (abfd, info, sec, relocs)
2224 bfd *abfd;
2225 struct bfd_link_info *info;
2226 asection *sec;
2227 const Elf_Internal_Rela *relocs;
2229 bfd *dynobj;
2230 Elf_Internal_Shdr *symtab_hdr;
2231 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2232 bfd_signed_vma *local_got_refcounts;
2233 const Elf_Internal_Rela *rel;
2234 const Elf_Internal_Rela *rel_end;
2235 asection *sgot;
2236 asection *srelgot;
2237 asection *sreloc;
2239 if (info->relocatable)
2240 return TRUE;
2242 dynobj = elf_hash_table (info)->dynobj;
2243 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2244 sym_hashes = elf_sym_hashes (abfd);
2245 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2246 local_got_refcounts = elf_local_got_refcounts (abfd);
2248 sgot = NULL;
2249 srelgot = NULL;
2250 sreloc = NULL;
2252 if (!elf_bad_symtab (abfd))
2253 sym_hashes_end -= symtab_hdr->sh_info;
2255 rel_end = relocs + sec->reloc_count;
2256 for (rel = relocs; rel < rel_end; rel++)
2258 struct elf_link_hash_entry *h;
2259 unsigned long r_symndx;
2260 enum elf_cris_reloc_type r_type;
2262 r_symndx = ELF32_R_SYM (rel->r_info);
2263 if (r_symndx < symtab_hdr->sh_info)
2264 h = NULL;
2265 else
2266 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2268 r_type = ELF32_R_TYPE (rel->r_info);
2270 /* Some relocs require linker-created sections; we need to hang them
2271 on the first input bfd we found that contained dynamic relocs. */
2272 switch (r_type)
2274 case R_CRIS_16_GOT:
2275 case R_CRIS_32_GOT:
2276 case R_CRIS_32_GOTREL:
2277 case R_CRIS_32_PLT_GOTREL:
2278 case R_CRIS_32_PLT_PCREL:
2279 case R_CRIS_16_GOTPLT:
2280 case R_CRIS_32_GOTPLT:
2281 if (dynobj == NULL)
2283 elf_hash_table (info)->dynobj = dynobj = abfd;
2285 /* Create the .got section, so we can assume it's always
2286 present whenever there's a dynobj. */
2287 if (!_bfd_elf_create_got_section (dynobj, info))
2288 return FALSE;
2290 break;
2292 default:
2293 break;
2296 /* Some relocs require a global offset table (but perhaps not a
2297 specific GOT entry). */
2298 switch (r_type)
2300 /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
2301 entry only for local symbols. Unfortunately, we don't know
2302 until later on if there's a version script that forces the
2303 symbol local. We must have the .rela.got section in place
2304 before we know if the symbol looks global now, so we need
2305 to treat the reloc just like for R_CRIS_16_GOT and
2306 R_CRIS_32_GOT. */
2307 case R_CRIS_16_GOTPLT:
2308 case R_CRIS_32_GOTPLT:
2309 case R_CRIS_16_GOT:
2310 case R_CRIS_32_GOT:
2311 if (srelgot == NULL
2312 && (h != NULL || info->shared))
2314 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2315 if (srelgot == NULL)
2317 srelgot = bfd_make_section (dynobj, ".rela.got");
2318 if (srelgot == NULL
2319 || !bfd_set_section_flags (dynobj, srelgot,
2320 (SEC_ALLOC
2321 | SEC_LOAD
2322 | SEC_HAS_CONTENTS
2323 | SEC_IN_MEMORY
2324 | SEC_LINKER_CREATED
2325 | SEC_READONLY))
2326 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2327 return FALSE;
2330 /* Fall through. */
2332 case R_CRIS_32_GOTREL:
2333 case R_CRIS_32_PLT_GOTREL:
2334 if (sgot == NULL)
2335 sgot = bfd_get_section_by_name (dynobj, ".got");
2337 if (local_got_refcounts == NULL)
2339 bfd_size_type amt;
2341 /* We use index local_got_refcounts[-1] to count all
2342 GOT-relative relocations that do not have explicit
2343 GOT entries. */
2344 amt = symtab_hdr->sh_info + 1;
2345 amt *= sizeof (bfd_signed_vma);
2346 local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2347 if (local_got_refcounts == NULL)
2348 return FALSE;
2350 local_got_refcounts++;
2351 elf_local_got_refcounts (abfd) = local_got_refcounts;
2353 break;
2355 default:
2356 break;
2359 switch (r_type)
2361 case R_CRIS_16_GOTPLT:
2362 case R_CRIS_32_GOTPLT:
2363 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2364 entry) is eliminated. We can only do this for a non-local
2365 symbol. */
2366 if (h != NULL)
2368 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2369 goto handle_gotplt_reloc;
2371 /* If h is NULL then this is a local symbol, and we must make a
2372 GOT entry for it, so handle it like a GOT reloc. */
2373 /* Fall through. */
2375 case R_CRIS_16_GOT:
2376 case R_CRIS_32_GOT:
2377 /* This symbol requires a global offset table entry. */
2378 if (h != NULL)
2380 if (h->got.refcount == 0)
2382 /* Make sure this symbol is output as a dynamic symbol. */
2383 if (h->dynindx == -1)
2385 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2386 return FALSE;
2389 /* Allocate space in the .got section. */
2390 sgot->_raw_size += 4;
2391 /* Allocate relocation space. */
2392 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2394 h->got.refcount++;
2396 else
2398 /* This is a global offset table entry for a local symbol. */
2399 if (local_got_refcounts[r_symndx] == 0)
2401 sgot->_raw_size += 4;
2402 if (info->shared)
2404 /* If we are generating a shared object, we need to
2405 output a R_CRIS_RELATIVE reloc so that the dynamic
2406 linker can adjust this GOT entry. */
2407 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2410 local_got_refcounts[r_symndx]++;
2412 break;
2414 case R_CRIS_32_GOTREL:
2415 /* This reference requires a global offset table.
2416 FIXME: The actual refcount isn't used currently; the .got
2417 section can't be removed if there were any references in the
2418 input. */
2419 local_got_refcounts[-1]++;
2420 break;
2422 handle_gotplt_reloc:
2424 case R_CRIS_32_PLT_GOTREL:
2425 /* This reference requires a global offset table. */
2426 local_got_refcounts[-1]++;
2427 /* Fall through. */
2429 case R_CRIS_32_PLT_PCREL:
2430 /* This symbol requires a procedure linkage table entry. We
2431 actually build the entry in adjust_dynamic_symbol,
2432 because this might be a case of linking PIC code which is
2433 never referenced by a dynamic object, in which case we
2434 don't need to generate a procedure linkage table entry
2435 after all. */
2437 /* If this is a local symbol, we resolve it directly without
2438 creating a procedure linkage table entry. */
2439 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2440 continue;
2442 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2443 h->plt.refcount++;
2444 break;
2446 case R_CRIS_8:
2447 case R_CRIS_16:
2448 case R_CRIS_32:
2449 /* Let's help debug shared library creation. Any of these
2450 relocs can be used in shared libs, but pages containing them
2451 cannot be shared. Don't warn for sections we don't care
2452 about, such as debug sections or non-constant sections. We
2453 can't help tables of (global) function pointers, for example,
2454 though they must be emitted in a data section to avoid having
2455 impure text sections. */
2456 if (info->shared
2457 && (sec->flags & SEC_ALLOC) != 0
2458 && (sec->flags & SEC_READONLY) != 0)
2460 /* FIXME: How do we make this optionally a warning only? */
2461 (*_bfd_error_handler)
2462 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2463 bfd_archive_filename (abfd),
2464 sec->name,
2465 cris_elf_howto_table[r_type].name);
2467 /* Fall through. */
2469 case R_CRIS_8_PCREL:
2470 case R_CRIS_16_PCREL:
2471 case R_CRIS_32_PCREL:
2472 if (h != NULL)
2474 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2476 /* Make sure a plt entry is created for this symbol if it
2477 turns out to be a function defined by a dynamic object. */
2478 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2479 h->plt.refcount++;
2482 /* If we are creating a shared library and this is not a local
2483 symbol, we need to copy the reloc into the shared library.
2484 However when linking with -Bsymbolic and this is a global
2485 symbol which is defined in an object we are including in the
2486 link (i.e., DEF_REGULAR is set), then we can resolve the
2487 reloc directly. At this point we have not seen all the input
2488 files, so it is possible that DEF_REGULAR is not set now but
2489 will be set later (it is never cleared). In case of a weak
2490 definition, DEF_REGULAR may be cleared later by a strong
2491 definition in a shared library. We account for that
2492 possibility below by storing information in the relocs_copied
2493 field of the hash table entry. A similar situation occurs
2494 when creating shared libraries and symbol visibility changes
2495 render the symbol local. */
2497 /* No need to do anything if we're not creating a shared object. */
2498 if (! info->shared)
2499 break;
2501 /* We don't need to handle relocs into sections not going into
2502 the "real" output. */
2503 if ((sec->flags & SEC_ALLOC) == 0)
2504 break;
2506 /* We can only eliminate PC-relative relocs. */
2507 if (r_type == R_CRIS_8_PCREL
2508 || r_type == R_CRIS_16_PCREL
2509 || r_type == R_CRIS_32_PCREL)
2511 /* If the symbol is local, then we can eliminate the reloc. */
2512 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2513 break;
2515 /* If this is with -Bsymbolic and the symbol isn't weak, and
2516 is defined by an ordinary object (the ones we include in
2517 this shared library) then we can also eliminate the
2518 reloc. See comment above for more eliminable cases which
2519 we can't identify at this time. */
2520 if (info->symbolic
2521 && h->root.type != bfd_link_hash_defweak
2522 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2523 break;
2526 /* We create a reloc section in dynobj and make room for this
2527 reloc. */
2528 if (sreloc == NULL)
2530 const char *name;
2532 name = (bfd_elf_string_from_elf_section
2533 (abfd,
2534 elf_elfheader (abfd)->e_shstrndx,
2535 elf_section_data (sec)->rel_hdr.sh_name));
2536 if (name == NULL)
2537 return FALSE;
2539 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2540 && strcmp (bfd_get_section_name (abfd, sec),
2541 name + 5) == 0);
2543 sreloc = bfd_get_section_by_name (dynobj, name);
2544 if (sreloc == NULL)
2546 sreloc = bfd_make_section (dynobj, name);
2547 if (sreloc == NULL
2548 || !bfd_set_section_flags (dynobj, sreloc,
2549 (SEC_ALLOC
2550 | SEC_LOAD
2551 | SEC_HAS_CONTENTS
2552 | SEC_IN_MEMORY
2553 | SEC_LINKER_CREATED
2554 | SEC_READONLY))
2555 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2556 return FALSE;
2558 if (sec->flags & SEC_READONLY)
2559 info->flags |= DF_TEXTREL;
2562 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2564 /* If we are linking with -Bsymbolic, we count the number of PC
2565 relative relocations we have entered for this symbol, so that
2566 we can discard them again if the symbol is later defined by a
2567 regular object. We know that h is really a pointer to an
2568 elf_cris_link_hash_entry. */
2569 if ((r_type == R_CRIS_8_PCREL
2570 || r_type == R_CRIS_16_PCREL
2571 || r_type == R_CRIS_32_PCREL)
2572 && info->symbolic)
2574 struct elf_cris_link_hash_entry *eh;
2575 struct elf_cris_pcrel_relocs_copied *p;
2577 eh = (struct elf_cris_link_hash_entry *) h;
2579 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2580 if (p->section == sreloc)
2581 break;
2583 if (p == NULL)
2585 p = ((struct elf_cris_pcrel_relocs_copied *)
2586 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2587 if (p == NULL)
2588 return FALSE;
2589 p->next = eh->pcrel_relocs_copied;
2590 eh->pcrel_relocs_copied = p;
2591 p->section = sreloc;
2592 p->count = 0;
2595 ++p->count;
2597 break;
2599 /* This relocation describes the C++ object vtable hierarchy.
2600 Reconstruct it for later use during GC. */
2601 case R_CRIS_GNU_VTINHERIT:
2602 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2603 return FALSE;
2604 break;
2606 /* This relocation describes which C++ vtable entries are actually
2607 used. Record for later use during GC. */
2608 case R_CRIS_GNU_VTENTRY:
2609 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2610 return FALSE;
2611 break;
2613 default:
2614 /* Other relocs do not appear here. */
2615 bfd_set_error (bfd_error_bad_value);
2616 return FALSE;
2620 return TRUE;
2623 /* Set the sizes of the dynamic sections. */
2625 static bfd_boolean
2626 elf_cris_size_dynamic_sections (output_bfd, info)
2627 bfd *output_bfd ATTRIBUTE_UNUSED;
2628 struct bfd_link_info *info;
2630 bfd *dynobj;
2631 asection *s;
2632 bfd_boolean plt;
2633 bfd_boolean relocs;
2635 dynobj = elf_hash_table (info)->dynobj;
2636 BFD_ASSERT (dynobj != NULL);
2638 if (elf_hash_table (info)->dynamic_sections_created)
2640 /* Set the contents of the .interp section to the interpreter. */
2641 if (info->executable)
2643 s = bfd_get_section_by_name (dynobj, ".interp");
2644 BFD_ASSERT (s != NULL);
2645 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2646 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2649 else
2651 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2652 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2653 elf_cris_adjust_gotplt_to_got,
2654 (PTR) info);
2656 /* We may have created entries in the .rela.got section.
2657 However, if we are not creating the dynamic sections, we will
2658 not actually use these entries. Reset the size of .rela.got,
2659 which will cause it to get stripped from the output file
2660 below. */
2661 s = bfd_get_section_by_name (dynobj, ".rela.got");
2662 if (s != NULL)
2663 s->_raw_size = 0;
2666 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2667 relative relocs against symbols defined in a regular object. We
2668 allocated space for them in the check_relocs routine, but we will not
2669 fill them in in the relocate_section routine. We also discard space
2670 for relocs that have become for local symbols due to symbol
2671 visibility changes. For programs, we discard space for relocs for
2672 symbols not referenced by any dynamic object. */
2673 if (info->shared)
2674 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2675 elf_cris_discard_excess_dso_dynamics,
2676 (PTR) info);
2677 else
2678 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2679 elf_cris_discard_excess_program_dynamics,
2680 (PTR) info);
2682 /* The check_relocs and adjust_dynamic_symbol entry points have
2683 determined the sizes of the various dynamic sections. Allocate
2684 memory for them. */
2685 plt = FALSE;
2686 relocs = FALSE;
2687 for (s = dynobj->sections; s != NULL; s = s->next)
2689 const char *name;
2690 bfd_boolean strip;
2692 if ((s->flags & SEC_LINKER_CREATED) == 0)
2693 continue;
2695 /* It's OK to base decisions on the section name, because none
2696 of the dynobj section names depend upon the input files. */
2697 name = bfd_get_section_name (dynobj, s);
2699 strip = FALSE;
2701 if (strcmp (name, ".plt") == 0)
2703 if (s->_raw_size == 0)
2705 /* Strip this section if we don't need it; see the
2706 comment below. */
2707 strip = TRUE;
2709 else
2711 /* Remember whether there is a PLT. */
2712 plt = TRUE;
2715 else if (strncmp (name, ".rela", 5) == 0)
2717 if (s->_raw_size == 0)
2719 /* If we don't need this section, strip it from the
2720 output file. This is mostly to handle .rela.bss and
2721 .rela.plt. We must create both sections in
2722 create_dynamic_sections, because they must be created
2723 before the linker maps input sections to output
2724 sections. The linker does that before
2725 adjust_dynamic_symbol is called, and it is that
2726 function which decides whether anything needs to go
2727 into these sections. */
2728 strip = TRUE;
2730 else
2732 /* Remember whether there are any reloc sections other
2733 than .rela.plt. */
2734 if (strcmp (name, ".rela.plt") != 0)
2735 relocs = TRUE;
2737 /* We use the reloc_count field as a counter if we need
2738 to copy relocs into the output file. */
2739 s->reloc_count = 0;
2742 else if (strncmp (name, ".got", 4) != 0)
2744 /* It's not one of our sections, so don't allocate space. */
2745 continue;
2748 if (strip)
2750 _bfd_strip_section_from_output (info, s);
2751 continue;
2754 /* Allocate memory for the section contents. We use bfd_zalloc here
2755 in case unused entries are not reclaimed before the section's
2756 contents are written out. This should not happen, but this way
2757 if it does, we will not write out garbage. For reloc sections,
2758 this will make entries have the type R_CRIS_NONE. */
2759 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2760 if (s->contents == NULL && s->_raw_size != 0)
2761 return FALSE;
2764 if (elf_hash_table (info)->dynamic_sections_created)
2766 /* Add some entries to the .dynamic section. We fill in the
2767 values later, in elf_cris_finish_dynamic_sections, but we
2768 must add the entries now so that we get the correct size for
2769 the .dynamic section. The DT_DEBUG entry is filled in by the
2770 dynamic linker and used by the debugger. */
2771 #define add_dynamic_entry(TAG, VAL) \
2772 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2774 if (!info->shared)
2776 if (!add_dynamic_entry (DT_DEBUG, 0))
2777 return FALSE;
2780 if (plt)
2782 if (!add_dynamic_entry (DT_PLTGOT, 0)
2783 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2784 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2785 || !add_dynamic_entry (DT_JMPREL, 0))
2786 return FALSE;
2789 if (relocs)
2791 if (!add_dynamic_entry (DT_RELA, 0)
2792 || !add_dynamic_entry (DT_RELASZ, 0)
2793 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2794 return FALSE;
2797 if ((info->flags & DF_TEXTREL) != 0)
2799 if (!add_dynamic_entry (DT_TEXTREL, 0))
2800 return FALSE;
2801 info->flags |= DF_TEXTREL;
2804 #undef add_dynamic_entry
2806 return TRUE;
2809 /* This function is called via elf_cris_link_hash_traverse if we are
2810 creating a shared object. In the -Bsymbolic case, it discards the
2811 space allocated to copy PC relative relocs against symbols which
2812 are defined in regular objects. For the normal non-symbolic case,
2813 we also discard space for relocs that have become local due to
2814 symbol visibility changes. We allocated space for them in the
2815 check_relocs routine, but we won't fill them in in the
2816 relocate_section routine. */
2818 static bfd_boolean
2819 elf_cris_discard_excess_dso_dynamics (h, inf)
2820 struct elf_cris_link_hash_entry *h;
2821 PTR inf;
2823 struct elf_cris_pcrel_relocs_copied *s;
2824 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2826 if (h->root.root.type == bfd_link_hash_warning)
2827 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2829 /* If a symbol has been forced local or we have found a regular
2830 definition for the symbolic link case, then we won't be needing
2831 any relocs. */
2832 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2833 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2834 || info->symbolic))
2836 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2837 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2840 return TRUE;
2843 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2844 creating a shared object. We discard space for relocs for symbols put
2845 in the .got, but which we found we do not have to resolve at run-time. */
2847 static bfd_boolean
2848 elf_cris_discard_excess_program_dynamics (h, inf)
2849 struct elf_cris_link_hash_entry *h;
2850 PTR inf;
2852 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2854 if (h->root.root.type == bfd_link_hash_warning)
2855 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2857 /* If we're not creating a shared library and have a symbol which is
2858 referred to by .got references, but the symbol is defined locally,
2859 (or rather, not not defined by a DSO) then lose the reloc for the
2860 .got (don't allocate room for it). */
2861 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
2863 if (h->root.got.refcount > 0
2864 /* The size of this section is only valid and in sync with the
2865 various reference counts if we do dynamic; don't decrement it
2866 otherwise. */
2867 && elf_hash_table (info)->dynamic_sections_created)
2869 bfd *dynobj = elf_hash_table (info)->dynobj;
2870 asection *srelgot;
2872 BFD_ASSERT (dynobj != NULL);
2874 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2876 BFD_ASSERT (srelgot != NULL);
2878 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2881 /* If the locally-defined symbol isn't used by a DSO, then we don't
2882 have to export it as a dynamic symbol. This was already done for
2883 functions; doing this for all symbols would presumably not
2884 introduce new problems. Of course we don't do this if we're
2885 exporting all dynamic symbols. */
2886 if (! info->export_dynamic
2887 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
2889 h->root.dynindx = -1;
2890 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2891 h->root.dynstr_index);
2895 return TRUE;
2898 /* Reject a file depending on presence and expectation of prefixed
2899 underscores on symbols. */
2901 static bfd_boolean
2902 cris_elf_object_p (abfd)
2903 bfd *abfd;
2905 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2906 return (bfd_get_symbol_leading_char (abfd) == '_');
2907 else
2908 return (bfd_get_symbol_leading_char (abfd) == 0);
2911 /* Mark presence or absence of leading underscore. */
2913 static void
2914 cris_elf_final_write_processing (abfd, linker)
2915 bfd *abfd;
2916 bfd_boolean linker ATTRIBUTE_UNUSED;
2918 if (bfd_get_symbol_leading_char (abfd) == '_')
2919 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2920 else
2921 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2924 /* Display the flags field. */
2926 static bfd_boolean
2927 cris_elf_print_private_bfd_data (abfd, ptr)
2928 bfd *abfd;
2929 PTR ptr;
2931 FILE *file = (FILE *) ptr;
2933 BFD_ASSERT (abfd != NULL && ptr != NULL)
2935 _bfd_elf_print_private_bfd_data (abfd, ptr);
2937 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2939 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2940 fprintf (file, _(" [symbols have a _ prefix]"));
2942 fputc ('\n', file);
2943 return TRUE;
2946 /* Don't mix files with and without a leading underscore. */
2948 static bfd_boolean
2949 cris_elf_merge_private_bfd_data (ibfd, obfd)
2950 bfd *ibfd;
2951 bfd *obfd;
2953 flagword old_flags, new_flags;
2955 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2956 return FALSE;
2958 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2959 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2960 return TRUE;
2962 if (! elf_flags_init (obfd))
2964 /* This happens when ld starts out with a 'blank' output file. */
2965 elf_flags_init (obfd) = TRUE;
2967 /* Set flags according to current bfd_target. */
2968 cris_elf_final_write_processing (obfd, FALSE);
2971 old_flags = elf_elfheader (obfd)->e_flags;
2972 new_flags = elf_elfheader (ibfd)->e_flags;
2974 /* Is this good or bad? We'll follow with other excluding flags. */
2975 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
2977 (*_bfd_error_handler)
2978 ((new_flags & EF_CRIS_UNDERSCORE)
2979 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
2980 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
2981 bfd_archive_filename (ibfd));
2982 bfd_set_error (bfd_error_bad_value);
2983 return FALSE;
2986 return TRUE;
2990 static enum elf_reloc_type_class
2991 elf_cris_reloc_type_class (rela)
2992 const Elf_Internal_Rela *rela;
2994 switch ((int) ELF32_R_TYPE (rela->r_info))
2996 case R_CRIS_RELATIVE:
2997 return reloc_class_relative;
2998 case R_CRIS_JUMP_SLOT:
2999 return reloc_class_plt;
3000 case R_CRIS_COPY:
3001 return reloc_class_copy;
3002 default:
3003 return reloc_class_normal;
3007 #define ELF_ARCH bfd_arch_cris
3008 #define ELF_MACHINE_CODE EM_CRIS
3009 #define ELF_MAXPAGESIZE 0x2000
3011 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3012 #define TARGET_LITTLE_NAME "elf32-cris"
3013 #define elf_symbol_leading_char 0
3015 #define elf_info_to_howto_rel NULL
3016 #define elf_info_to_howto cris_info_to_howto_rela
3017 #define elf_backend_relocate_section cris_elf_relocate_section
3018 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3019 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3020 #define elf_backend_check_relocs cris_elf_check_relocs
3021 #define elf_backend_grok_prstatus cris_elf_grok_prstatus
3022 #define elf_backend_grok_psinfo cris_elf_grok_psinfo
3024 #define elf_backend_can_gc_sections 1
3025 #define elf_backend_can_refcount 1
3027 #define elf_backend_object_p cris_elf_object_p
3028 #define elf_backend_final_write_processing \
3029 cris_elf_final_write_processing
3030 #define bfd_elf32_bfd_print_private_bfd_data \
3031 cris_elf_print_private_bfd_data
3032 #define bfd_elf32_bfd_merge_private_bfd_data \
3033 cris_elf_merge_private_bfd_data
3035 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3037 #define bfd_elf32_bfd_link_hash_table_create \
3038 elf_cris_link_hash_table_create
3039 #define elf_backend_adjust_dynamic_symbol \
3040 elf_cris_adjust_dynamic_symbol
3041 #define elf_backend_size_dynamic_sections \
3042 elf_cris_size_dynamic_sections
3043 #define elf_backend_finish_dynamic_symbol \
3044 elf_cris_finish_dynamic_symbol
3045 #define elf_backend_finish_dynamic_sections \
3046 elf_cris_finish_dynamic_sections
3047 #define elf_backend_create_dynamic_sections \
3048 _bfd_elf_create_dynamic_sections
3049 #define bfd_elf32_bfd_final_link \
3050 _bfd_elf32_gc_common_final_link
3051 #define elf_backend_hide_symbol elf_cris_hide_symbol
3052 #define elf_backend_reloc_type_class elf_cris_reloc_type_class
3054 #define elf_backend_want_got_plt 1
3055 #define elf_backend_plt_readonly 1
3056 #define elf_backend_want_plt_sym 0
3057 #define elf_backend_got_header_size 12
3059 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3060 linking and libraries (if it's a win of any significance). Until then,
3061 take the easy route. */
3062 #define elf_backend_may_use_rel_p 0
3063 #define elf_backend_may_use_rela_p 1
3064 #define elf_backend_rela_normal 1
3066 #include "elf32-target.h"
3068 #define INCLUDED_TARGET_FILE
3070 #undef TARGET_LITTLE_SYM
3071 #undef TARGET_LITTLE_NAME
3072 #undef elf_symbol_leading_char
3074 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3075 #define TARGET_LITTLE_NAME "elf32-us-cris"
3076 #define elf_symbol_leading_char '_'
3078 #include "elf32-target.h"