2001-08-31 Eric Christopher <echristo@redhat.com>
[binutils.git] / bfd / elf32-cris.c
bloba4b5e09dc10b1c5dab7d358ac030e30c35f9efd2
1 /* CRIS-specific support for 32-bit ELF.
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Axis Communications AB.
4 Written by Hans-Peter Nilsson, based on elf32-fr30.c
5 PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/cris.h"
29 /* Forward declarations. */
30 static reloc_howto_type * cris_reloc_type_lookup
31 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
33 static void cris_info_to_howto_rela
34 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
36 static boolean cris_elf_relocate_section
37 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
38 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
40 static bfd_reloc_status_type cris_final_link_relocate
41 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
42 Elf_Internal_Rela *, bfd_vma));
44 static boolean cris_elf_gc_sweep_hook
45 PARAMS ((bfd *, struct bfd_link_info *, asection *,
46 const Elf_Internal_Rela *));
48 static asection * cris_elf_gc_mark_hook
49 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
50 struct elf_link_hash_entry *, Elf_Internal_Sym *));
52 static boolean cris_elf_object_p PARAMS ((bfd *));
54 static void cris_elf_final_write_processing PARAMS ((bfd *, boolean));
56 static boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
58 static boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
60 struct elf_cris_link_hash_entry;
61 static boolean elf_cris_discard_excess_dso_dynamics
62 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
63 static boolean elf_cris_discard_excess_program_dynamics
64 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
65 static boolean elf_cris_adjust_gotplt_to_got
66 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
67 static boolean elf_cris_try_fold_plt_to_got
68 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
70 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
71 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
72 PARAMS ((bfd *));
73 static boolean elf_cris_adjust_dynamic_symbol
74 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static boolean cris_elf_check_relocs
76 PARAMS ((bfd *, struct bfd_link_info *, asection *,
77 const Elf_Internal_Rela *));
79 static boolean elf_cris_size_dynamic_sections
80 PARAMS ((bfd *, struct bfd_link_info *));
81 static boolean elf_cris_finish_dynamic_symbol
82 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
83 Elf_Internal_Sym *));
84 static boolean elf_cris_finish_dynamic_sections
85 PARAMS ((bfd *, struct bfd_link_info *));
86 static void elf_cris_hide_symbol
87 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
88 static enum elf_reloc_type_class elf_cris_reloc_type_class
89 PARAMS ((int));
91 static reloc_howto_type cris_elf_howto_table [] =
93 /* This reloc does nothing. */
94 HOWTO (R_CRIS_NONE, /* type */
95 0, /* rightshift */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
97 32, /* bitsize */
98 false, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_bitfield, /* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
102 "R_CRIS_NONE", /* name */
103 false, /* partial_inplace */
104 0, /* src_mask */
105 0, /* dst_mask */
106 false), /* pcrel_offset */
108 /* An 8 bit absolute relocation. */
109 HOWTO (R_CRIS_8, /* type */
110 0, /* rightshift */
111 0, /* size (0 = byte, 1 = short, 2 = long) */
112 8, /* bitsize */
113 false, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_bitfield, /* complain_on_overflow */
116 bfd_elf_generic_reloc, /* special_function */
117 "R_CRIS_8", /* name */
118 false, /* partial_inplace */
119 0x0000, /* src_mask */
120 0x00ff, /* dst_mask */
121 false), /* pcrel_offset */
123 /* A 16 bit absolute relocation. */
124 HOWTO (R_CRIS_16, /* type */
125 0, /* rightshift */
126 1, /* size (0 = byte, 1 = short, 2 = long) */
127 16, /* bitsize */
128 false, /* pc_relative */
129 0, /* bitpos */
130 complain_overflow_bitfield, /* complain_on_overflow */
131 bfd_elf_generic_reloc, /* special_function */
132 "R_CRIS_16", /* name */
133 false, /* partial_inplace */
134 0x00000000, /* src_mask */
135 0x0000ffff, /* dst_mask */
136 false), /* pcrel_offset */
138 /* A 32 bit absolute relocation. */
139 HOWTO (R_CRIS_32, /* type */
140 0, /* rightshift */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
142 32, /* bitsize */
143 false, /* pc_relative */
144 0, /* bitpos */
145 complain_overflow_bitfield, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
147 "R_CRIS_32", /* name */
148 false, /* partial_inplace */
149 0x00000000, /* src_mask */
150 0xffffffff, /* dst_mask */
151 false), /* pcrel_offset */
153 /* An 8 bit PC-relative relocation. */
154 HOWTO (R_CRIS_8_PCREL, /* type */
155 0, /* rightshift */
156 0, /* size (0 = byte, 1 = short, 2 = long) */
157 8, /* bitsize */
158 true, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_bitfield, /* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_CRIS_8_PCREL", /* name */
163 false, /* partial_inplace */
164 0x0000, /* src_mask */
165 0x00ff, /* dst_mask */
166 true), /* pcrel_offset */
168 /* A 16 bit PC-relative relocation. */
169 HOWTO (R_CRIS_16_PCREL, /* type */
170 0, /* rightshift */
171 1, /* size (0 = byte, 1 = short, 2 = long) */
172 16, /* bitsize */
173 true, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_bitfield, /* complain_on_overflow */
176 bfd_elf_generic_reloc, /* special_function */
177 "R_CRIS_16", /* name */
178 false, /* partial_inplace */
179 0x00000000, /* src_mask */
180 0x0000ffff, /* dst_mask */
181 true), /* pcrel_offset */
183 /* A 32 bit PC-relative relocation. */
184 HOWTO (R_CRIS_32_PCREL, /* type */
185 0, /* rightshift */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
187 32, /* bitsize */
188 true, /* pc_relative */
189 0, /* bitpos */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 bfd_elf_generic_reloc, /* special_function */
192 "R_CRIS_32", /* name */
193 false, /* partial_inplace */
194 0x00000000, /* src_mask */
195 0xffffffff, /* dst_mask */
196 true), /* pcrel_offset */
198 /* GNU extension to record C++ vtable hierarchy. */
199 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
200 0, /* rightshift */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
202 0, /* bitsize */
203 false, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_dont, /* complain_on_overflow */
206 NULL, /* special_function */
207 "R_CRIS_GNU_VTINHERIT", /* name */
208 false, /* partial_inplace */
209 0, /* src_mask */
210 0, /* dst_mask */
211 false), /* pcrel_offset */
213 /* GNU extension to record C++ vtable member usage. */
214 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
215 0, /* rightshift */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
217 0, /* bitsize */
218 false, /* pc_relative */
219 0, /* bitpos */
220 complain_overflow_dont, /* complain_on_overflow */
221 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
222 "R_CRIS_GNU_VTENTRY", /* name */
223 false, /* partial_inplace */
224 0, /* src_mask */
225 0, /* dst_mask */
226 false), /* pcrel_offset */
228 /* This is used only by the dynamic linker. The symbol should exist
229 both in the object being run and in some shared library. The
230 dynamic linker copies the data addressed by the symbol from the
231 shared library into the object, because the object being
232 run has to have the data at some particular address. */
233 HOWTO (R_CRIS_COPY, /* type */
234 0, /* rightshift */
235 2, /* size (0 = byte, 1 = short, 2 = long) */
236 32, /* bitsize */
237 false, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_bitfield, /* complain_on_overflow */
240 bfd_elf_generic_reloc, /* special_function */
241 "R_CRIS_COPY", /* name */
242 false, /* partial_inplace */
243 0, /* src_mask */
244 0, /* dst_mask */
245 false), /* pcrel_offset */
247 /* Like R_CRIS_32, but used when setting global offset table entries. */
248 HOWTO (R_CRIS_GLOB_DAT, /* type */
249 0, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 32, /* bitsize */
252 false, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_bitfield, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_CRIS_GLOB_DAT", /* name */
257 false, /* partial_inplace */
258 0, /* src_mask */
259 0xffffffff, /* dst_mask */
260 false), /* pcrel_offset */
262 /* Marks a procedure linkage table entry for a symbol. */
263 HOWTO (R_CRIS_JUMP_SLOT, /* type */
264 0, /* rightshift */
265 2, /* size (0 = byte, 1 = short, 2 = long) */
266 32, /* bitsize */
267 false, /* pc_relative */
268 0, /* bitpos */
269 complain_overflow_bitfield, /* complain_on_overflow */
270 bfd_elf_generic_reloc, /* special_function */
271 "R_CRIS_JUMP_SLOT", /* name */
272 false, /* partial_inplace */
273 0, /* src_mask */
274 0, /* dst_mask */
275 false), /* pcrel_offset */
277 /* Used only by the dynamic linker. When the object is run, this
278 longword is set to the load address of the object, plus the
279 addend. */
280 HOWTO (R_CRIS_RELATIVE, /* type */
281 0, /* rightshift */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
283 32, /* bitsize */
284 false, /* pc_relative */
285 0, /* bitpos */
286 complain_overflow_bitfield, /* complain_on_overflow */
287 bfd_elf_generic_reloc, /* special_function */
288 "R_CRIS_RELATIVE", /* name */
289 false, /* partial_inplace */
290 0, /* src_mask */
291 0xffffffff, /* dst_mask */
292 false), /* pcrel_offset */
294 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
295 HOWTO (R_CRIS_16_GOT, /* type */
296 0, /* rightshift */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
298 16, /* bitsize */
299 false, /* pc_relative */
300 0, /* bitpos */
301 complain_overflow_bitfield, /* complain_on_overflow */
302 bfd_elf_generic_reloc, /* special_function */
303 "R_CRIS_16_GOT", /* name */
304 false, /* partial_inplace */
305 0, /* src_mask */
306 0xffff, /* dst_mask */
307 false), /* pcrel_offset */
309 HOWTO (R_CRIS_32_GOT, /* type */
310 0, /* rightshift */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
312 32, /* bitsize */
313 false, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_bitfield, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_CRIS_32_GOT", /* name */
318 false, /* partial_inplace */
319 0, /* src_mask */
320 0xffffffff, /* dst_mask */
321 false), /* pcrel_offset */
323 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
324 the GOT table for the symbol. */
325 HOWTO (R_CRIS_16_GOTPLT, /* type */
326 0, /* rightshift */
327 1, /* size (0 = byte, 1 = short, 2 = long) */
328 16, /* bitsize */
329 false, /* pc_relative */
330 0, /* bitpos */
331 complain_overflow_bitfield, /* complain_on_overflow */
332 bfd_elf_generic_reloc, /* special_function */
333 "R_CRIS_16_GOTPLT", /* name */
334 false, /* partial_inplace */
335 0, /* src_mask */
336 0xffff, /* dst_mask */
337 false), /* pcrel_offset */
339 HOWTO (R_CRIS_32_GOTPLT, /* type */
340 0, /* rightshift */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
342 32, /* bitsize */
343 false, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_CRIS_32_GOTPLT", /* name */
348 false, /* partial_inplace */
349 0, /* src_mask */
350 0xffffffff, /* dst_mask */
351 false), /* pcrel_offset */
353 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
354 be necessary. */
355 HOWTO (R_CRIS_32_GOTREL, /* type */
356 0, /* rightshift */
357 2, /* size (0 = byte, 1 = short, 2 = long) */
358 32, /* bitsize */
359 false, /* pc_relative */
360 0, /* bitpos */
361 complain_overflow_bitfield, /* complain_on_overflow */
362 bfd_elf_generic_reloc, /* special_function */
363 "R_CRIS_32_GOTREL", /* name */
364 false, /* partial_inplace */
365 0, /* src_mask */
366 0xffffffff, /* dst_mask */
367 false), /* pcrel_offset */
369 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
370 to create PLT entry for symbol. */
371 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
372 0, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 32, /* bitsize */
375 false, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_bitfield, /* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_CRIS_32_PLT_GOTREL", /* name */
380 false, /* partial_inplace */
381 0, /* src_mask */
382 0xffffffff, /* dst_mask */
383 false), /* pcrel_offset */
385 /* A 32-bit offset from PC (location after the relocation) + addend to
386 entry for this symbol in PLT and request to create PLT entry for
387 symbol. */
388 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
389 0, /* rightshift */
390 2, /* size (0 = byte, 1 = short, 2 = long) */
391 32, /* bitsize */
392 true, /* pc_relative */
393 0, /* bitpos */
394 complain_overflow_bitfield, /* complain_on_overflow */
395 bfd_elf_generic_reloc, /* special_function */
396 "R_CRIS_32_PLT_PCREL", /* name */
397 false, /* partial_inplace */
398 0, /* src_mask */
399 0xffffffff, /* dst_mask */
400 true) /* pcrel_offset */
403 /* Map BFD reloc types to CRIS ELF reloc types. */
405 struct cris_reloc_map
407 bfd_reloc_code_real_type bfd_reloc_val;
408 unsigned int cris_reloc_val;
411 static const struct cris_reloc_map cris_reloc_map [] =
413 { BFD_RELOC_NONE, R_CRIS_NONE },
414 { BFD_RELOC_8, R_CRIS_8 },
415 { BFD_RELOC_16, R_CRIS_16 },
416 { BFD_RELOC_32, R_CRIS_32 },
417 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
418 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
419 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
420 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
421 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
422 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
423 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
424 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
425 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
426 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
427 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
428 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
429 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
430 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
431 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
432 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
435 static reloc_howto_type *
436 cris_reloc_type_lookup (abfd, code)
437 bfd * abfd ATTRIBUTE_UNUSED;
438 bfd_reloc_code_real_type code;
440 unsigned int i;
442 for (i = sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]);
443 --i;)
444 if (cris_reloc_map [i].bfd_reloc_val == code)
445 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
447 return NULL;
450 /* Set the howto pointer for an CRIS ELF reloc. */
452 static void
453 cris_info_to_howto_rela (abfd, cache_ptr, dst)
454 bfd * abfd ATTRIBUTE_UNUSED;
455 arelent * cache_ptr;
456 Elf32_Internal_Rela * dst;
458 unsigned int r_type;
460 r_type = ELF32_R_TYPE (dst->r_info);
461 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
462 cache_ptr->howto = & cris_elf_howto_table [r_type];
465 /* The name of the dynamic interpreter. This is put in the .interp
466 section. */
468 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
470 /* The size in bytes of an entry in the procedure linkage table. */
472 #define PLT_ENTRY_SIZE 20
474 /* The first entry in an absolute procedure linkage table looks like this. */
476 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
478 0xfc, 0xe1,
479 0x7e, 0x7e, /* push mof. */
480 0x7f, 0x0d, /* (dip [pc+]) */
481 0, 0, 0, 0, /* Replaced with address of .got + 4. */
482 0x30, 0x7a, /* move [...],mof */
483 0x7f, 0x0d, /* (dip [pc+]) */
484 0, 0, 0, 0, /* Replaced with address of .got + 8. */
485 0x30, 0x09, /* jump [...] */
488 /* Subsequent entries in an absolute procedure linkage table look like
489 this. */
491 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
493 0x7f, 0x0d, /* (dip [pc+]) */
494 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
495 0x30, 0x09, /* jump [...] */
496 0x3f, 0x7e, /* move [pc+],mof */
497 0, 0, 0, 0, /* Replaced with offset into relocation table. */
498 0x2f, 0xfe, /* add.d [pc+],pc */
499 0xec, 0xff,
500 0xff, 0xff /* Replaced with offset to start of .plt. */
503 /* The first entry in a PIC procedure linkage table looks like this. */
505 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
507 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
508 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
509 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
510 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
513 /* Subsequent entries in a PIC procedure linkage table look like this. */
515 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
517 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
518 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
519 0x30, 0x09, /* jump [...] */
520 0x3f, 0x7e, /* move [pc+],mof */
521 0, 0, 0, 0, /* Replaced with offset into relocation table. */
522 0x2f, 0xfe, /* add.d [pc+],pc */
523 0xec, 0xff, /* Replaced with offset to start of .plt. */
524 0xff, 0xff
527 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
528 (and most other PIC/shlib stuff). Check that we don't drift away
529 without reason.
531 The CRIS linker, like the m68k and i386 linkers (and probably the rest
532 too) needs to keep track of the number of relocs that it decides to
533 copy in check_relocs for each symbol. This is so that it can discard
534 PC relative relocs if it doesn't need them when linking with
535 -Bsymbolic. We store the information in a field extending the regular
536 ELF linker hash table. */
538 /* This structure keeps track of the number of PC relative relocs we have
539 copied for a given symbol. */
541 struct elf_cris_pcrel_relocs_copied
543 /* Next section. */
544 struct elf_cris_pcrel_relocs_copied *next;
545 /* A section in dynobj. */
546 asection *section;
547 /* Number of relocs copied in this section. */
548 bfd_size_type count;
551 /* CRIS ELF linker hash entry. */
553 struct elf_cris_link_hash_entry
555 struct elf_link_hash_entry root;
557 /* Number of PC relative relocs copied for this symbol. */
558 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
560 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
561 both a general GOT and a PLT-specific GOT entry for the same symbol,
562 when it is referenced both as a function and as a function pointer.
564 Number of GOTPLT references for a function. */
565 bfd_signed_vma gotplt_refcount;
567 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
568 (zero is never used as an index). FIXME: We should be able to fold
569 this with gotplt_refcount in a union, like the got and plt unions in
570 elf_link_hash_entry. */
571 bfd_size_type gotplt_offset;
574 /* CRIS ELF linker hash table. */
576 struct elf_cris_link_hash_table
578 struct elf_link_hash_table root;
580 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
581 since we try and avoid creating GOTPLT:s when there's already a GOT.
582 Instead, we keep and update the next available index here. */
583 bfd_size_type next_gotplt_entry;
586 /* Traverse a CRIS ELF linker hash table. */
588 #define elf_cris_link_hash_traverse(table, func, info) \
589 (elf_link_hash_traverse \
590 (&(table)->root, \
591 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
592 (info)))
594 /* Get the CRIS ELF linker hash table from a link_info structure. */
596 #define elf_cris_hash_table(p) \
597 ((struct elf_cris_link_hash_table *) (p)->hash)
599 /* Create an entry in a CRIS ELF linker hash table. */
601 static struct bfd_hash_entry *
602 elf_cris_link_hash_newfunc (entry, table, string)
603 struct bfd_hash_entry *entry;
604 struct bfd_hash_table *table;
605 const char *string;
607 struct elf_cris_link_hash_entry *ret =
608 (struct elf_cris_link_hash_entry *) entry;
610 /* Allocate the structure if it has not already been allocated by a
611 subclass. */
612 if (ret == (struct elf_cris_link_hash_entry *) NULL)
613 ret = ((struct elf_cris_link_hash_entry *)
614 bfd_hash_allocate (table,
615 sizeof (struct elf_cris_link_hash_entry)));
616 if (ret == (struct elf_cris_link_hash_entry *) NULL)
617 return (struct bfd_hash_entry *) ret;
619 /* Call the allocation method of the superclass. */
620 ret = ((struct elf_cris_link_hash_entry *)
621 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
622 table, string));
623 if (ret != (struct elf_cris_link_hash_entry *) NULL)
625 ret->pcrel_relocs_copied = NULL;
626 ret->gotplt_refcount = 0;
627 ret->gotplt_offset = 0;
630 return (struct bfd_hash_entry *) ret;
633 /* Create a CRIS ELF linker hash table. */
635 static struct bfd_link_hash_table *
636 elf_cris_link_hash_table_create (abfd)
637 bfd *abfd;
639 struct elf_cris_link_hash_table *ret;
641 ret = ((struct elf_cris_link_hash_table *)
642 bfd_alloc (abfd, sizeof (struct elf_cris_link_hash_table)));
643 if (ret == (struct elf_cris_link_hash_table *) NULL)
644 return NULL;
646 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
647 elf_cris_link_hash_newfunc))
649 bfd_release (abfd, ret);
650 return NULL;
653 /* Initialize to skip over the first three entries in the gotplt; they
654 are used for run-time symbol evaluation. */
655 ret->next_gotplt_entry = 12;
657 return &ret->root.root;
660 /* Perform a single relocation. By default we use the standard BFD
661 routines, with a few tweaks. */
663 static bfd_reloc_status_type
664 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
665 relocation)
666 reloc_howto_type * howto;
667 bfd * input_bfd;
668 asection * input_section;
669 bfd_byte * contents;
670 Elf_Internal_Rela * rel;
671 bfd_vma relocation;
673 bfd_reloc_status_type r;
675 /* PC-relative relocations are relative to the position *after*
676 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
677 not a single byte, since PC must be 16-bit-aligned. */
678 switch (ELF32_R_TYPE (rel->r_info))
680 /* Check that the 16-bit GOT relocs are positive. */
681 case R_CRIS_16_GOTPLT:
682 case R_CRIS_16_GOT:
683 if ((bfd_signed_vma) relocation < 0)
684 return bfd_reloc_overflow;
685 break;
687 case R_CRIS_32_PLT_PCREL:
688 case R_CRIS_32_PCREL:
689 relocation -= 2;
690 /* Fall through. */
691 case R_CRIS_8_PCREL:
692 case R_CRIS_16_PCREL:
693 relocation -= 2;
694 break;
696 default:
697 break;
700 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
701 contents, rel->r_offset,
702 relocation, rel->r_addend);
703 return r;
706 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
707 copied, for further comments. */
709 static boolean
710 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
711 contents, relocs, local_syms, local_sections)
712 bfd * output_bfd ATTRIBUTE_UNUSED;
713 struct bfd_link_info * info;
714 bfd * input_bfd;
715 asection * input_section;
716 bfd_byte * contents;
717 Elf_Internal_Rela * relocs;
718 Elf_Internal_Sym * local_syms;
719 asection ** local_sections;
721 bfd *dynobj;
722 Elf_Internal_Shdr * symtab_hdr;
723 struct elf_link_hash_entry ** sym_hashes;
724 bfd_vma *local_got_offsets;
725 asection *sgot;
726 asection *splt;
727 asection *sreloc;
728 Elf_Internal_Rela * rel;
729 Elf_Internal_Rela * relend;
731 dynobj = elf_hash_table (info)->dynobj;
732 local_got_offsets = elf_local_got_offsets (input_bfd);
733 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
734 sym_hashes = elf_sym_hashes (input_bfd);
735 relend = relocs + input_section->reloc_count;
737 /* It seems this can happen with erroneous or unsupported input (mixing
738 a.out and elf in an archive, for example.) */
739 if (sym_hashes == NULL)
740 return false;
742 sgot = NULL;
743 splt = NULL;
744 sreloc = NULL;
746 if (dynobj != NULL)
748 splt = bfd_get_section_by_name (dynobj, ".plt");
749 sgot = bfd_get_section_by_name (dynobj, ".got");
752 for (rel = relocs; rel < relend; rel ++)
754 reloc_howto_type * howto;
755 unsigned long r_symndx;
756 Elf_Internal_Sym * sym;
757 asection * sec;
758 struct elf_link_hash_entry * h;
759 bfd_vma relocation;
760 bfd_reloc_status_type r;
761 const char * name = NULL;
762 int r_type;
764 r_type = ELF32_R_TYPE (rel->r_info);
766 if ( r_type == R_CRIS_GNU_VTINHERIT
767 || r_type == R_CRIS_GNU_VTENTRY)
768 continue;
770 r_symndx = ELF32_R_SYM (rel->r_info);
772 if (info->relocateable)
774 /* This is a relocateable link. We don't have to change
775 anything, unless the reloc is against a section symbol,
776 in which case we have to adjust according to where the
777 section symbol winds up in the output section. */
778 if (r_symndx < symtab_hdr->sh_info)
780 sym = local_syms + r_symndx;
782 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
784 sec = local_sections [r_symndx];
785 rel->r_addend += sec->output_offset + sym->st_value;
789 continue;
792 /* This is a final link. */
793 howto = cris_elf_howto_table + r_type;
794 h = NULL;
795 sym = NULL;
796 sec = NULL;
798 if (r_symndx < symtab_hdr->sh_info)
800 sym = local_syms + r_symndx;
801 sec = local_sections [r_symndx];
802 relocation = (sec->output_section->vma
803 + sec->output_offset
804 + sym->st_value);
806 name = bfd_elf_string_from_elf_section
807 (input_bfd, symtab_hdr->sh_link, sym->st_name);
808 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
810 else
812 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
814 while (h->root.type == bfd_link_hash_indirect
815 || h->root.type == bfd_link_hash_warning)
816 h = (struct elf_link_hash_entry *) h->root.u.i.link;
818 name = h->root.root.string;
820 if (h->root.type == bfd_link_hash_defined
821 || h->root.type == bfd_link_hash_defweak)
823 sec = h->root.u.def.section;
825 /* Perhaps we should detect the cases that
826 sec->output_section is expected to be NULL like i386 and
827 m68k, but apparently (and according to elfxx-ia64.c) all
828 valid cases are where the symbol is defined in a shared
829 object which we link dynamically against. This includes
830 PLT relocs for which we've created a PLT entry and other
831 relocs for which we're prepared to create dynamic
832 relocations.
834 For now, new situations cause us to just err when
835 sec->output_offset is NULL but the object with the symbol
836 is *not* dynamically linked against. Thus this will
837 automatically remind us so we can see if there are other
838 valid cases we need to revisit. */
839 if ((sec->output_section == NULL
840 && (sec->owner->flags & DYNAMIC) != 0)
842 /* Here follow the cases where the relocation value must
843 be zero (or when further handling is simplified when
844 zero). I can't claim to understand the various
845 conditions and they weren't described in the files
846 where I copied them from (elf32-m68k.c and
847 elf32-i386.c), but let's mention examples of where
848 they happen. FIXME: Perhaps define and use a
849 dynamic_symbol_p function like ia64.
851 - When creating a shared library, we can have an
852 ordinary relocation for a symbol defined in a shared
853 library (perhaps the one we create). We then make
854 the relocation value zero, as the value seen now will
855 be added into the relocation addend in this shared
856 library, but must be handled only at dynamic-link
857 time. FIXME: Not sure this example covers the
858 h->elf_link_hash_flags test, though it's there in
859 other targets. */
860 || (info->shared
861 && ((! info->symbolic && h->dynindx != -1)
862 || (h->elf_link_hash_flags
863 & ELF_LINK_HASH_DEF_REGULAR) == 0)
864 && (input_section->flags & SEC_ALLOC) != 0
865 && (r_type == R_CRIS_8
866 || r_type == R_CRIS_16
867 || r_type == R_CRIS_32
868 || r_type == R_CRIS_8_PCREL
869 || r_type == R_CRIS_16_PCREL
870 || r_type == R_CRIS_32_PCREL)))
871 relocation = 0;
872 else if (sec->output_section != NULL)
873 relocation = (h->root.u.def.value
874 + sec->output_section->vma
875 + sec->output_offset);
876 else
878 if (input_bfd->my_archive)
879 (*_bfd_error_handler)
880 (_("%s(%s): unresolvable relocation %s against symbol `%s' from %s section"),
881 bfd_get_filename (bfd_my_archive (input_bfd)),
882 bfd_get_filename (input_bfd),
883 cris_elf_howto_table[r_type].name,
884 name,
885 bfd_get_section_name (input_bfd, input_section));
886 else
887 (*_bfd_error_handler)
888 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
889 bfd_get_filename (input_bfd),
890 cris_elf_howto_table[r_type].name,
891 name,
892 bfd_get_section_name (input_bfd, input_section));
893 bfd_set_error (bfd_error_bad_value);
894 return false;
897 else if (h->root.type == bfd_link_hash_undefweak)
898 relocation = 0;
899 else if (info->shared
900 && !info->symbolic
901 && !info->no_undefined
902 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
903 relocation = 0;
904 else
906 if (!(info->callbacks->undefined_symbol
907 (info, name, input_bfd,
908 input_section, rel->r_offset,
909 (!info->shared || info->no_undefined
910 || ELF_ST_VISIBILITY (h->other)))))
911 return false;
912 relocation = 0;
916 switch (r_type)
918 case R_CRIS_16_GOTPLT:
919 case R_CRIS_32_GOTPLT:
920 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
921 but we require a PLT, and the PLT handling will take care of
922 filling in the PLT-specific GOT entry. For the GOT offset,
923 calculate it as we do when filling it in for the .got.plt
924 section. If we don't have a PLT, punt to GOT handling. */
925 if (h != NULL
926 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
928 asection *sgotplt
929 = bfd_get_section_by_name (dynobj, ".got.plt");
930 bfd_vma got_offset;
932 BFD_ASSERT (h->dynindx != -1);
933 BFD_ASSERT (sgotplt != NULL);
935 got_offset
936 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
938 relocation = got_offset;
939 break;
942 /* We didn't make a PLT entry for this symbol. Maybe everything is
943 folded into the GOT. Other than folding, this happens when
944 statically linking PIC code, or when using -Bsymbolic. Check
945 that we instead have a GOT entry as done for us by
946 elf_cris_adjust_dynamic_symbol, and drop through into the
947 ordinary GOT cases. */
948 if (h != NULL && h->got.offset == (bfd_vma) -1)
950 (*_bfd_error_handler)
951 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
952 bfd_get_filename (input_bfd),
953 cris_elf_howto_table[r_type].name,
954 name[0] != '\0' ? name : _("[whose name is lost]"),
955 bfd_get_section_name (input_bfd, input_section));
957 /* FIXME: Perhaps blaming input is not the right thing to
958 do; this is probably an internal error. But it is true
959 that we didn't like that particular input. */
960 bfd_set_error (bfd_error_bad_value);
961 return false;
963 /* Fall through. */
965 /* The size of the actual relocation is not used here; we only
966 fill in the GOT table here. */
967 case R_CRIS_16_GOT:
968 case R_CRIS_32_GOT:
970 bfd_vma off;
972 /* Note that despite using RELA relocations, the .got contents
973 is always filled in with the link-relative relocation
974 value; the addend. */
976 if (h != NULL)
978 off = h->got.offset;
979 BFD_ASSERT (off != (bfd_vma) -1);
981 if (!elf_hash_table (info)->dynamic_sections_created
982 || (! info->shared && h->dynindx == -1)
983 || (info->shared
984 && (info->symbolic || h->dynindx == -1)
985 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
987 /* This wasn't checked above for ! info->shared, but
988 must hold there if we get here; the symbol must not
989 be used in, or defined by a DSO. (Note that
990 checking for ELF_LINK_HASH_DEF_REGULAR doesn't
991 catch all cases.) */
992 BFD_ASSERT (info->shared
993 || (h->elf_link_hash_flags
994 & (ELF_LINK_HASH_REF_DYNAMIC
995 | ELF_LINK_HASH_DEF_DYNAMIC)) == 0);
997 /* This is actually a static link, or it is a
998 -Bsymbolic link and the symbol is defined
999 locally, or the symbol was forced to be local
1000 because of a version file, or we're not creating a
1001 dynamic object and the symbol isn't referred to by
1002 a dynamic object. We must initialize
1003 this entry in the global offset table. Since
1004 the offset must always be a multiple of 4, we
1005 use the least significant bit to record whether
1006 we have initialized it already.
1008 When doing a dynamic link, we create a .rela.got
1009 relocation entry to initialize the value. This
1010 is done in the finish_dynamic_symbol routine. */
1011 if ((off & 1) != 0)
1012 off &= ~1;
1013 else
1015 bfd_put_32 (output_bfd, relocation,
1016 sgot->contents + off);
1017 h->got.offset |= 1;
1021 else
1023 BFD_ASSERT (local_got_offsets != NULL
1024 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1026 off = local_got_offsets[r_symndx];
1028 /* The offset must always be a multiple of 4. We use
1029 the least significant bit to record whether we have
1030 already generated the necessary reloc. */
1031 if ((off & 1) != 0)
1032 off &= ~1;
1033 else
1035 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1037 if (info->shared)
1039 asection *srelgot;
1040 Elf_Internal_Rela outrel;
1042 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1043 BFD_ASSERT (srelgot != NULL);
1045 outrel.r_offset = (sgot->output_section->vma
1046 + sgot->output_offset
1047 + off);
1048 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1049 outrel.r_addend = relocation;
1050 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1051 (((Elf32_External_Rela *)
1052 srelgot->contents)
1053 + srelgot->reloc_count));
1054 ++srelgot->reloc_count;
1057 local_got_offsets[r_symndx] |= 1;
1061 relocation = sgot->output_offset + off;
1062 if (rel->r_addend != 0)
1064 /* We can't do anything for a relocation which is against
1065 a symbol *plus offset*. GOT holds relocations for
1066 symbols. Make this an error; the compiler isn't
1067 allowed to pass us these kinds of things. */
1068 if (h == NULL)
1069 (*_bfd_error_handler)
1070 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1071 bfd_get_filename (input_bfd),
1072 cris_elf_howto_table[r_type].name,
1073 rel->r_addend,
1074 bfd_get_section_name (input_bfd, input_section));
1075 else
1076 (*_bfd_error_handler)
1077 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1078 bfd_get_filename (input_bfd),
1079 cris_elf_howto_table[r_type].name,
1080 rel->r_addend,
1081 name[0] != '\0' ? name : _("[whose name is lost]"),
1082 bfd_get_section_name (input_bfd, input_section));
1084 bfd_set_error (bfd_error_bad_value);
1085 return false;
1088 break;
1090 case R_CRIS_32_GOTREL:
1091 /* This relocation must only be performed against local symbols. */
1092 if (h != NULL)
1094 (*_bfd_error_handler)
1095 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1096 bfd_get_filename (input_bfd),
1097 cris_elf_howto_table[r_type].name,
1098 name,
1099 bfd_get_section_name (input_bfd, input_section));
1100 bfd_set_error (bfd_error_bad_value);
1101 return false;
1104 /* This relocation is like a PC-relative one, except the
1105 reference point is the location of GOT. Note that
1106 sgot->output_offset is not involved in this calculation. We
1107 always want the start of entire .got section, not the
1108 position after the reserved header. */
1109 relocation -= sgot->output_section->vma;
1110 break;
1112 case R_CRIS_32_PLT_PCREL:
1113 /* Relocation is to the entry for this symbol in the
1114 procedure linkage table. */
1116 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1117 without using the procedure linkage table. */
1118 if (h == NULL)
1119 break;
1121 if (h->plt.offset == (bfd_vma) -1
1122 || splt == NULL)
1124 /* We didn't make a PLT entry for this symbol. This
1125 happens when statically linking PIC code, or when
1126 using -Bsymbolic. */
1127 break;
1130 relocation = (splt->output_section->vma
1131 + splt->output_offset
1132 + h->plt.offset);
1133 break;
1135 case R_CRIS_32_PLT_GOTREL:
1136 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1137 start of the .got section. See also comment at
1138 R_CRIS_32_GOT. */
1139 relocation -= sgot->output_section->vma;
1141 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1142 without using the procedure linkage table. */
1143 if (h == NULL)
1144 break;
1146 if (h->plt.offset == (bfd_vma) -1
1147 || splt == NULL)
1149 /* We didn't make a PLT entry for this symbol. This
1150 happens when statically linking PIC code, or when
1151 using -Bsymbolic. */
1152 break;
1155 relocation = (splt->output_section->vma
1156 + splt->output_offset
1157 + h->plt.offset
1158 - sgot->output_section->vma);
1159 break;
1161 case R_CRIS_8_PCREL:
1162 case R_CRIS_16_PCREL:
1163 case R_CRIS_32_PCREL:
1164 /* If the symbol was local, we need no shlib-specific handling. */
1165 if (h == NULL)
1166 break;
1168 /* Fall through. */
1169 case R_CRIS_8:
1170 case R_CRIS_16:
1171 case R_CRIS_32:
1172 if (info->shared
1173 && (input_section->flags & SEC_ALLOC) != 0
1174 && ((r_type != R_CRIS_8_PCREL
1175 && r_type != R_CRIS_16_PCREL
1176 && r_type != R_CRIS_32_PCREL)
1177 || (!info->symbolic
1178 || (h->elf_link_hash_flags
1179 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1181 Elf_Internal_Rela outrel;
1182 boolean skip, relocate;
1184 /* When generating a shared object, these relocations
1185 are copied into the output file to be resolved at run
1186 time. */
1188 if (sreloc == NULL)
1190 const char *name;
1192 name = (bfd_elf_string_from_elf_section
1193 (input_bfd,
1194 elf_elfheader (input_bfd)->e_shstrndx,
1195 elf_section_data (input_section)->rel_hdr.sh_name));
1196 if (name == NULL)
1197 return false;
1199 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1200 && strcmp (bfd_get_section_name (input_bfd,
1201 input_section),
1202 name + 5) == 0);
1204 sreloc = bfd_get_section_by_name (dynobj, name);
1206 /* That section should have been created in
1207 cris_elf_check_relocs, but that function will not be
1208 called for objects which fail in
1209 cris_elf_merge_private_bfd_data. */
1210 if (sreloc == NULL)
1212 (*_bfd_error_handler)
1213 (_("%s: Internal inconsistency; no relocation section %s"),
1214 bfd_get_filename (input_bfd),
1215 name);
1217 bfd_set_error (bfd_error_bad_value);
1218 return false;
1222 skip = false;
1224 if (elf_section_data (input_section)->stab_info == NULL)
1225 outrel.r_offset = rel->r_offset;
1226 else
1228 bfd_vma off;
1230 off = (_bfd_stab_section_offset
1231 (output_bfd, &elf_hash_table (info)->stab_info,
1232 input_section,
1233 &elf_section_data (input_section)->stab_info,
1234 rel->r_offset));
1235 if (off == (bfd_vma) -1)
1236 skip = true;
1237 outrel.r_offset = off;
1240 outrel.r_offset += (input_section->output_section->vma
1241 + input_section->output_offset);
1243 if (skip)
1245 memset (&outrel, 0, sizeof outrel);
1246 relocate = false;
1248 /* h->dynindx may be -1 if the symbol was marked to
1249 become local. */
1250 else if (h != NULL
1251 && ((! info->symbolic && h->dynindx != -1)
1252 || (h->elf_link_hash_flags
1253 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1255 BFD_ASSERT (h->dynindx != -1);
1256 relocate = false;
1257 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1258 outrel.r_addend = relocation + rel->r_addend;
1260 else
1262 if (r_type == R_CRIS_32)
1264 relocate = true;
1265 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1266 outrel.r_addend = relocation + rel->r_addend;
1268 else
1270 long indx;
1272 if (h == NULL)
1273 sec = local_sections[r_symndx];
1274 else
1276 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1277 || (h->root.type
1278 == bfd_link_hash_defweak));
1279 sec = h->root.u.def.section;
1281 if (sec != NULL && bfd_is_abs_section (sec))
1282 indx = 0;
1283 else if (sec == NULL || sec->owner == NULL)
1285 bfd_set_error (bfd_error_bad_value);
1286 return false;
1288 else
1290 asection *osec;
1292 osec = sec->output_section;
1293 indx = elf_section_data (osec)->dynindx;
1294 BFD_ASSERT (indx > 0);
1297 relocate = false;
1298 outrel.r_info = ELF32_R_INFO (indx, r_type);
1299 outrel.r_addend = relocation + rel->r_addend;
1303 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1304 (((Elf32_External_Rela *)
1305 sreloc->contents)
1306 + sreloc->reloc_count));
1307 ++sreloc->reloc_count;
1309 /* This reloc will be computed at runtime, so there's no
1310 need to do anything now, except for R_CRIS_32 relocations
1311 that have been turned into R_CRIS_RELATIVE. */
1312 if (!relocate)
1313 continue;
1316 break;
1319 r = cris_final_link_relocate (howto, input_bfd, input_section,
1320 contents, rel, relocation);
1322 if (r != bfd_reloc_ok)
1324 const char * msg = (const char *) NULL;
1326 switch (r)
1328 case bfd_reloc_overflow:
1329 r = info->callbacks->reloc_overflow
1330 (info, name, howto->name, (bfd_vma) 0,
1331 input_bfd, input_section, rel->r_offset);
1332 break;
1334 case bfd_reloc_undefined:
1335 r = info->callbacks->undefined_symbol
1336 (info, name, input_bfd, input_section, rel->r_offset,
1337 true);
1338 break;
1340 case bfd_reloc_outofrange:
1341 msg = _("internal error: out of range error");
1342 break;
1344 case bfd_reloc_notsupported:
1345 msg = _("internal error: unsupported relocation error");
1346 break;
1348 case bfd_reloc_dangerous:
1349 msg = _("internal error: dangerous relocation");
1350 break;
1352 default:
1353 msg = _("internal error: unknown error");
1354 break;
1357 if (msg)
1358 r = info->callbacks->warning
1359 (info, msg, name, input_bfd, input_section, rel->r_offset);
1361 if (! r)
1362 return false;
1366 return true;
1369 /* Finish up dynamic symbol handling. We set the contents of various
1370 dynamic sections here. */
1372 static boolean
1373 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1374 bfd *output_bfd;
1375 struct bfd_link_info *info;
1376 struct elf_link_hash_entry *h;
1377 Elf_Internal_Sym *sym;
1379 bfd *dynobj;
1380 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1382 dynobj = elf_hash_table (info)->dynobj;
1384 if (h->plt.offset != (bfd_vma) -1)
1386 asection *splt;
1387 asection *sgotplt;
1388 asection *sgot;
1389 asection *srela;
1390 bfd_vma got_base;
1392 bfd_vma gotplt_offset
1393 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1394 Elf_Internal_Rela rela;
1395 boolean has_gotplt = gotplt_offset != 0;
1397 /* Get the index in the procedure linkage table which
1398 corresponds to this symbol. This is the index of this symbol
1399 in all the symbols for which we are making plt entries. The
1400 first entry in the procedure linkage table is reserved. */
1401 /* We have to count backwards here, and the result is only valid as
1402 an index into .got.plt and its relocations. FIXME: Constants... */
1403 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1405 /* Get the offset into the .got table of the entry that corresponds
1406 to this function. Note that we embed knowledge that "incoming"
1407 .got goes after .got.plt in the output without padding (pointer
1408 aligned). However, that knowledge is present in several other
1409 places too, here and in elflink.h at least. */
1410 bfd_vma got_offset
1411 = (has_gotplt
1412 ? gotplt_offset
1413 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1415 /* This symbol has an entry in the procedure linkage table. Set it
1416 up. */
1418 BFD_ASSERT (h->dynindx != -1);
1420 splt = bfd_get_section_by_name (dynobj, ".plt");
1421 sgot = bfd_get_section_by_name (dynobj, ".got");
1422 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1423 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1424 BFD_ASSERT (splt != NULL && sgotplt != NULL
1425 && (! has_gotplt || srela != NULL));
1427 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1429 /* Fill in the entry in the procedure linkage table. */
1430 if (! info->shared)
1432 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1433 PLT_ENTRY_SIZE);
1435 /* We need to enter the absolute address of the GOT entry here. */
1436 bfd_put_32 (output_bfd, got_base + got_offset,
1437 splt->contents + h->plt.offset + plt_off1);
1439 else
1441 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1442 PLT_ENTRY_SIZE);
1443 bfd_put_32 (output_bfd, got_offset,
1444 splt->contents + h->plt.offset + plt_off1);
1447 /* Fill in the plt entry and make a relocation, if this is a "real"
1448 PLT entry. */
1449 if (has_gotplt)
1451 /* Fill in the offset into the reloc table. */
1452 bfd_put_32 (output_bfd,
1453 gotplt_index * sizeof (Elf32_External_Rela),
1454 splt->contents + h->plt.offset + plt_off2);
1456 /* Fill in the offset to the first PLT entry, where to "jump". */
1457 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1458 splt->contents + h->plt.offset + plt_off3);
1460 /* Fill in the entry in the global offset table with the address of
1461 the relocating stub. */
1462 bfd_put_32 (output_bfd,
1463 (splt->output_section->vma
1464 + splt->output_offset
1465 + h->plt.offset
1466 + 8),
1467 sgotplt->contents + got_offset);
1469 /* Fill in the entry in the .rela.plt section. */
1470 rela.r_offset = (sgotplt->output_section->vma
1471 + sgotplt->output_offset
1472 + got_offset);
1473 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1474 rela.r_addend = 0;
1475 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1476 ((Elf32_External_Rela *) srela->contents
1477 + gotplt_index));
1480 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1482 /* Mark the symbol as undefined, rather than as defined in
1483 the .plt section. Leave the value alone. */
1484 sym->st_shndx = SHN_UNDEF;
1486 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1487 know whether resetting the value is significant; if it really
1488 is, rather than a quirk or bug in the sparc port, then I
1489 believe we'd see this elsewhere. */
1490 /* If the symbol is weak, we do need to clear the value.
1491 Otherwise, the PLT entry would provide a definition for
1492 the symbol even if the symbol wasn't defined anywhere,
1493 and so the symbol would never be NULL. */
1494 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1495 == 0)
1496 sym->st_value = 0;
1500 /* We don't emit .got relocs for symbols that aren't in the
1501 dynamic-symbols table for an ordinary program. */
1502 if (h->got.offset != (bfd_vma) -1
1503 && (info->shared || h->dynindx != -1))
1505 asection *sgot;
1506 asection *srela;
1507 Elf_Internal_Rela rela;
1509 /* This symbol has an entry in the global offset table. Set it up. */
1511 sgot = bfd_get_section_by_name (dynobj, ".got");
1512 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1513 BFD_ASSERT (sgot != NULL && srela != NULL);
1515 rela.r_offset = (sgot->output_section->vma
1516 + sgot->output_offset
1517 + (h->got.offset &~ 1));
1519 /* If this is a static link, or it is a -Bsymbolic link and the
1520 symbol is defined locally or was forced to be local because
1521 of a version file, we just want to emit a RELATIVE reloc.
1522 The entry in the global offset table will already have been
1523 initialized in the relocate_section function. */
1524 if (! elf_hash_table (info)->dynamic_sections_created
1525 || (info->shared
1526 && (info->symbolic || h->dynindx == -1)
1527 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1529 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1530 rela.r_addend = bfd_get_signed_32 (output_bfd,
1531 (sgot->contents
1532 + (h->got.offset & ~1)));
1534 else
1536 bfd_put_32 (output_bfd, (bfd_vma) 0,
1537 sgot->contents + (h->got.offset & ~1));
1538 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1539 rela.r_addend = 0;
1542 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1543 ((Elf32_External_Rela *) srela->contents
1544 + srela->reloc_count));
1545 ++srela->reloc_count;
1548 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1550 asection *s;
1551 Elf_Internal_Rela rela;
1553 /* This symbol needs a copy reloc. Set it up. */
1555 BFD_ASSERT (h->dynindx != -1
1556 && (h->root.type == bfd_link_hash_defined
1557 || h->root.type == bfd_link_hash_defweak));
1559 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1560 ".rela.bss");
1561 BFD_ASSERT (s != NULL);
1563 rela.r_offset = (h->root.u.def.value
1564 + h->root.u.def.section->output_section->vma
1565 + h->root.u.def.section->output_offset);
1566 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1567 rela.r_addend = 0;
1568 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1569 ((Elf32_External_Rela *) s->contents
1570 + s->reloc_count));
1571 ++s->reloc_count;
1574 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1575 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1576 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1577 sym->st_shndx = SHN_ABS;
1579 return true;
1582 /* Finish up the dynamic sections. */
1584 static boolean
1585 elf_cris_finish_dynamic_sections (output_bfd, info)
1586 bfd *output_bfd;
1587 struct bfd_link_info *info;
1589 bfd *dynobj;
1590 asection *sgot;
1591 asection *sdyn;
1593 dynobj = elf_hash_table (info)->dynobj;
1595 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1596 BFD_ASSERT (sgot != NULL);
1597 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1599 if (elf_hash_table (info)->dynamic_sections_created)
1601 asection *splt;
1602 Elf32_External_Dyn *dyncon, *dynconend;
1604 splt = bfd_get_section_by_name (dynobj, ".plt");
1605 BFD_ASSERT (splt != NULL && sdyn != NULL);
1607 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1608 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1609 for (; dyncon < dynconend; dyncon++)
1611 Elf_Internal_Dyn dyn;
1612 asection *s;
1614 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1616 switch (dyn.d_tag)
1618 default:
1619 break;
1621 case DT_PLTGOT:
1622 s = bfd_get_section_by_name (output_bfd, ".got");
1623 BFD_ASSERT (s != NULL);
1624 dyn.d_un.d_ptr = s->vma;
1625 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1626 break;
1628 case DT_JMPREL:
1629 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1630 if all symbols are found in the .got (not .got.plt). */
1631 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1632 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1633 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1634 break;
1636 case DT_PLTRELSZ:
1637 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1638 if (s == NULL)
1639 dyn.d_un.d_val = 0;
1640 else if (s->_cooked_size != 0)
1641 dyn.d_un.d_val = s->_cooked_size;
1642 else
1643 dyn.d_un.d_val = s->_raw_size;
1644 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1645 break;
1647 case DT_RELASZ:
1648 /* The procedure linkage table relocs (DT_JMPREL) should
1649 not be included in the overall relocs (DT_RELA).
1650 Therefore, we override the DT_RELASZ entry here to
1651 make it not include the JMPREL relocs. Since the
1652 linker script arranges for .rela.plt to follow all
1653 other relocation sections, we don't have to worry
1654 about changing the DT_RELA entry. */
1655 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1656 if (s != NULL)
1658 if (s->_cooked_size != 0)
1659 dyn.d_un.d_val -= s->_cooked_size;
1660 else
1661 dyn.d_un.d_val -= s->_raw_size;
1663 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1664 break;
1668 /* Fill in the first entry in the procedure linkage table. */
1669 if (splt->_raw_size > 0)
1671 if (info->shared)
1672 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1673 else
1675 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1676 bfd_put_32 (output_bfd,
1677 sgot->output_section->vma + sgot->output_offset + 4,
1678 splt->contents + 6);
1679 bfd_put_32 (output_bfd,
1680 sgot->output_section->vma + sgot->output_offset + 8,
1681 splt->contents + 14);
1683 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1684 = PLT_ENTRY_SIZE;
1689 /* Fill in the first three entries in the global offset table. */
1690 if (sgot->_raw_size > 0)
1692 if (sdyn == NULL)
1693 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1694 else
1695 bfd_put_32 (output_bfd,
1696 sdyn->output_section->vma + sdyn->output_offset,
1697 sgot->contents);
1698 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1699 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1702 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1704 return true;
1707 /* Return the section that should be marked against GC for a given
1708 relocation. */
1710 static asection *
1711 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1712 bfd * abfd;
1713 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1714 Elf_Internal_Rela * rel;
1715 struct elf_link_hash_entry * h;
1716 Elf_Internal_Sym * sym;
1718 if (h != NULL)
1720 switch (ELF32_R_TYPE (rel->r_info))
1722 case R_CRIS_GNU_VTINHERIT:
1723 case R_CRIS_GNU_VTENTRY:
1724 break;
1726 default:
1727 switch (h->root.type)
1729 case bfd_link_hash_defined:
1730 case bfd_link_hash_defweak:
1731 return h->root.u.def.section;
1733 case bfd_link_hash_common:
1734 return h->root.u.c.p->section;
1736 default:
1737 break;
1741 else
1743 if (!(elf_bad_symtab (abfd)
1744 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1745 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1746 && sym->st_shndx != SHN_COMMON))
1748 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1752 return NULL;
1755 /* Update the got entry reference counts for the section being removed. */
1757 static boolean
1758 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1759 bfd * abfd ATTRIBUTE_UNUSED;
1760 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1761 asection * sec ATTRIBUTE_UNUSED;
1762 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1764 Elf_Internal_Shdr *symtab_hdr;
1765 struct elf_link_hash_entry **sym_hashes;
1766 bfd_signed_vma *local_got_refcounts;
1767 const Elf_Internal_Rela *rel, *relend;
1768 unsigned long r_symndx;
1769 struct elf_link_hash_entry *h;
1770 bfd *dynobj;
1771 asection *sgot;
1772 asection *srelgot;
1774 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1775 sym_hashes = elf_sym_hashes (abfd);
1776 local_got_refcounts = elf_local_got_refcounts (abfd);
1778 dynobj = elf_hash_table (info)->dynobj;
1779 if (dynobj == NULL)
1780 return true;
1782 sgot = bfd_get_section_by_name (dynobj, ".got");
1783 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1785 relend = relocs + sec->reloc_count;
1786 for (rel = relocs; rel < relend; rel++)
1788 switch (ELF32_R_TYPE (rel->r_info))
1790 case R_CRIS_16_GOT:
1791 case R_CRIS_32_GOT:
1792 r_symndx = ELF32_R_SYM (rel->r_info);
1793 if (r_symndx >= symtab_hdr->sh_info)
1795 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1796 if (h->got.refcount > 0)
1798 --h->got.refcount;
1799 if (h->got.refcount == 0)
1801 /* We don't need the .got entry any more. */
1802 sgot->_raw_size -= 4;
1803 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1806 break;
1809 local_got_reloc:
1810 if (local_got_refcounts != NULL)
1812 if (local_got_refcounts[r_symndx] > 0)
1814 --local_got_refcounts[r_symndx];
1815 if (local_got_refcounts[r_symndx] == 0)
1817 /* We don't need the .got entry any more. */
1818 sgot->_raw_size -= 4;
1819 if (info->shared)
1820 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1824 break;
1826 case R_CRIS_16_GOTPLT:
1827 case R_CRIS_32_GOTPLT:
1828 /* For local symbols, treat these like GOT relocs. */
1829 r_symndx = ELF32_R_SYM (rel->r_info);
1830 if (r_symndx < symtab_hdr->sh_info)
1831 goto local_got_reloc;
1833 case R_CRIS_32_PLT_GOTREL:
1834 /* FIXME: We don't garbage-collect away the .got section. */
1835 if (local_got_refcounts != NULL)
1836 local_got_refcounts[-1]--;
1837 /* Fall through. */
1839 case R_CRIS_8_PCREL:
1840 case R_CRIS_16_PCREL:
1841 case R_CRIS_32_PCREL:
1842 case R_CRIS_32_PLT_PCREL:
1843 r_symndx = ELF32_R_SYM (rel->r_info);
1844 if (r_symndx >= symtab_hdr->sh_info)
1846 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1847 if (h->plt.refcount > 0)
1848 --h->plt.refcount;
1850 break;
1852 default:
1853 break;
1857 return true;
1860 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1861 entry but we found we will not create any. Called when we find we will
1862 not have any PLT for this symbol, by for example
1863 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1864 or elf_cris_size_dynamic_sections if no dynamic sections will be
1865 created (we're only linking static objects). */
1867 static boolean
1868 elf_cris_adjust_gotplt_to_got (h, p)
1869 struct elf_cris_link_hash_entry *h;
1870 PTR p;
1872 struct bfd_link_info *info = (struct bfd_link_info *) p;
1873 bfd *dynobj = elf_hash_table (info)->dynobj;
1875 BFD_ASSERT (dynobj != NULL);
1877 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1878 if (h->gotplt_refcount <= 0)
1879 return true;
1881 if (h->root.got.refcount > 0)
1883 /* There's a GOT entry for this symbol. Just adjust the refcount.
1884 Probably not necessary at this stage, but keeping it accurate
1885 helps avoiding surprises later. */
1886 h->root.got.refcount += h->gotplt_refcount;
1887 h->gotplt_refcount = -1;
1889 else
1891 /* No GOT entry for this symbol. We need to create one. */
1892 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1893 asection *srelgot
1894 = bfd_get_section_by_name (dynobj, ".rela.got");
1896 /* Put an accurate refcount there. */
1897 h->root.got.refcount = h->gotplt_refcount;
1899 h->gotplt_refcount = -1;
1901 /* We always have a .got section when there are dynamic
1902 relocs. */
1903 BFD_ASSERT (sgot != NULL /* Surely have .got section. */);
1905 /* We might have had a PLT but with no GOT entry and
1906 further no GOT reloc section at all needed before.
1907 Add it. */
1908 if (srelgot == NULL)
1910 srelgot = bfd_make_section (dynobj, ".rela.got");
1912 if (srelgot == NULL
1913 || !bfd_set_section_flags (dynobj, srelgot,
1914 (SEC_ALLOC
1915 | SEC_LOAD
1916 | SEC_HAS_CONTENTS
1917 | SEC_IN_MEMORY
1918 | SEC_LINKER_CREATED
1919 | SEC_READONLY))
1920 || !bfd_set_section_alignment (dynobj, srelgot, 2))
1921 return false;
1924 /* Allocate space in the .got section. */
1925 sgot->_raw_size += 4;
1927 /* Allocate relocation space. */
1928 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1931 return true;
1934 /* Try to fold PLT entries with GOT entries. There are two cases when we
1935 want to do this:
1937 - When all PLT references are GOTPLT references, and there are GOT
1938 references. We don't have to generate a PLT at all.
1940 - When there are both (ordinary) PLT references and GOT references.
1941 We want to make the PLT reference use the ordinary GOT entry rather
1942 than a run-time dynamically resolved GOTPLT entry (since the GOT
1943 entry will have to be resolved at startup anyway).
1945 Though the latter case is handled when room for the PLT is allocated,
1946 not here.
1948 Note that this function is called before symbols are forced local by
1949 version scripts. The differing cases are handled by
1950 elf_cris_hide_symbol. */
1952 static boolean
1953 elf_cris_try_fold_plt_to_got (h, p)
1954 struct elf_cris_link_hash_entry *h;
1955 PTR p;
1957 struct bfd_link_info *info = (struct bfd_link_info *) p;
1959 /* If there are no GOT references for this symbol, we can't fold any
1960 other reference so there's nothing to do. Likewise if there are no
1961 PLT references; GOTPLT references included. */
1962 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1963 return true;
1965 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
1966 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1968 if (h->gotplt_refcount == h->root.plt.refcount)
1970 /* The only PLT references are GOTPLT references, and there are GOT
1971 references. Convert PLT to GOT references. */
1972 if (! elf_cris_adjust_gotplt_to_got (h, info))
1973 return false;
1975 /* Clear the PLT references, so no PLT will be created. */
1976 h->root.plt.offset = (bfd_vma) -1;
1979 return true;
1982 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1983 to use a GOT entry (and create one) rather than requiring a GOTPLT
1984 entry. */
1986 static void
1987 elf_cris_hide_symbol (info, h)
1988 struct bfd_link_info *info;
1989 struct elf_link_hash_entry *h;
1991 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1993 _bfd_elf_link_hash_hide_symbol (info, h);
1996 /* Adjust a symbol defined by a dynamic object and referenced by a
1997 regular object. The current definition is in some section of the
1998 dynamic object, but we're not including those sections. We have to
1999 change the definition to something the rest of the link can
2000 understand. */
2002 static boolean
2003 elf_cris_adjust_dynamic_symbol (info, h)
2004 struct bfd_link_info *info;
2005 struct elf_link_hash_entry *h;
2007 bfd *dynobj;
2008 asection *s;
2009 unsigned int power_of_two;
2011 dynobj = elf_hash_table (info)->dynobj;
2013 /* Make sure we know what is going on here. */
2014 BFD_ASSERT (dynobj != NULL
2015 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2016 || h->weakdef != NULL
2017 || ((h->elf_link_hash_flags
2018 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2019 && (h->elf_link_hash_flags
2020 & ELF_LINK_HASH_REF_REGULAR) != 0
2021 && (h->elf_link_hash_flags
2022 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2024 /* If this is a function, put it in the procedure linkage table. We
2025 will fill in the contents of the procedure linkage table later,
2026 when we know the address of the .got section. */
2027 if (h->type == STT_FUNC
2028 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2030 if (! info->shared
2031 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2032 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2033 /* We must always create the plt entry if it was referenced by a
2034 PLT relocation. In this case we already recorded it as a
2035 dynamic symbol. */
2036 /* FIXME: m68k and i386 differ here, for unclear reasons. */
2037 && h->dynindx == -1)
2039 /* This case can occur if we saw a PLT reloc in an input file,
2040 but the symbol was never referred to by a dynamic object. In
2041 such a case, we don't actually need to build a procedure
2042 linkage table, and we can just do a PC reloc instead, or
2043 change a .got.plt index to a .got index for GOTPLT relocs. */
2044 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2045 h->plt.offset = (bfd_vma) -1;
2047 return
2048 elf_cris_adjust_gotplt_to_got ((struct
2049 elf_cris_link_hash_entry *) h,
2050 info);
2053 /* If there are only GOT references and GOTPLT references to this
2054 PLT entry, get rid of the PLT. */
2055 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2056 h, info))
2057 return false;
2059 /* GC or folding may have rendered this entry unused. */
2060 if (h->plt.refcount <= 0)
2062 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2063 h->plt.offset = (bfd_vma) -1;
2064 return true;
2067 /* Make sure this symbol is output as a dynamic symbol. */
2068 if (h->dynindx == -1)
2070 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2071 return false;
2074 s = bfd_get_section_by_name (dynobj, ".plt");
2075 BFD_ASSERT (s != NULL);
2077 /* If this is the first .plt entry, make room for the special
2078 first entry. */
2079 if (s->_raw_size == 0)
2080 s->_raw_size += PLT_ENTRY_SIZE;
2082 /* If this symbol is not defined in a regular file, and we are
2083 not generating a shared library, then set the symbol to this
2084 location in the .plt. This is required to make function
2085 pointers compare as equal between the normal executable and
2086 the shared library. */
2087 if (!info->shared
2088 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2090 h->root.u.def.section = s;
2091 h->root.u.def.value = s->_raw_size;
2094 /* If there's already a GOT entry, use that, not a .got.plt. A
2095 GOT field still has a reference count when we get here; it's
2096 not yet changed to an offset. */
2097 if (h->got.refcount > 0)
2099 h->got.refcount += h->plt.refcount;
2101 /* Mark the PLT offset to use the GOT entry by setting the low
2102 bit in the plt offset; it is always a multiple of
2103 pointer-size. */
2104 BFD_ASSERT ((s->_raw_size & 3) == 0);
2106 /* Change the PLT refcount to an offset. */
2107 h->plt.offset = s->_raw_size;
2109 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2110 that the got entry should be used instead. */
2111 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2112 h)->gotplt_offset == 0);
2114 /* Make room for this entry. */
2115 s->_raw_size += PLT_ENTRY_SIZE;
2117 return true;
2120 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2121 h->plt.offset = s->_raw_size;
2123 /* Make room for this entry. */
2124 s->_raw_size += PLT_ENTRY_SIZE;
2126 /* We also need to make an entry in the .got.plt section, which
2127 will be placed in the .got section by the linker script. */
2128 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2129 = elf_cris_hash_table (info)->next_gotplt_entry;
2130 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2132 s = bfd_get_section_by_name (dynobj, ".got.plt");
2133 BFD_ASSERT (s != NULL);
2134 s->_raw_size += 4;
2136 /* We also need to make an entry in the .rela.plt section. */
2138 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2139 BFD_ASSERT (s != NULL);
2140 s->_raw_size += sizeof (Elf32_External_Rela);
2142 return true;
2145 /* Reinitialize the plt offset now that it is not used as a reference
2146 count any more. */
2147 h->plt.offset = (bfd_vma) -1;
2149 /* If this is a weak symbol, and there is a real definition, the
2150 processor independent code will have arranged for us to see the
2151 real definition first, and we can just use the same value. */
2152 if (h->weakdef != NULL)
2154 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2155 || h->weakdef->root.type == bfd_link_hash_defweak);
2156 h->root.u.def.section = h->weakdef->root.u.def.section;
2157 h->root.u.def.value = h->weakdef->root.u.def.value;
2158 return true;
2161 /* This is a reference to a symbol defined by a dynamic object which
2162 is not a function. */
2164 /* If we are creating a shared library, we must presume that the
2165 only references to the symbol are via the global offset table.
2166 For such cases we need not do anything here; the relocations will
2167 be handled correctly by relocate_section. */
2168 if (info->shared)
2169 return true;
2171 /* If there are no references to this symbol that do not use the
2172 GOT, we don't need to generate a copy reloc. */
2173 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2174 return true;
2176 /* We must allocate the symbol in our .dynbss section, which will
2177 become part of the .bss section of the executable. There will be
2178 an entry for this symbol in the .dynsym section. The dynamic
2179 object will contain position independent code, so all references
2180 from the dynamic object to this symbol will go through the global
2181 offset table. The dynamic linker will use the .dynsym entry to
2182 determine the address it must put in the global offset table, so
2183 both the dynamic object and the regular object will refer to the
2184 same memory location for the variable. */
2186 s = bfd_get_section_by_name (dynobj, ".dynbss");
2187 BFD_ASSERT (s != NULL);
2189 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2190 copy the initial value out of the dynamic object and into the
2191 runtime process image. We need to remember the offset into the
2192 .rela.bss section we are going to use. */
2193 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2195 asection *srel;
2197 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2198 BFD_ASSERT (srel != NULL);
2199 srel->_raw_size += sizeof (Elf32_External_Rela);
2200 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2203 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2204 thing to copy; so do we. */
2206 /* We need to figure out the alignment required for this symbol. I
2207 have no idea how ELF linkers handle this. */
2208 power_of_two = bfd_log2 (h->size);
2209 if (power_of_two > 3)
2210 power_of_two = 3;
2212 /* Apply the required alignment. */
2213 s->_raw_size = BFD_ALIGN (s->_raw_size,
2214 (bfd_size_type) (1 << power_of_two));
2215 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2217 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2218 return false;
2221 /* Define the symbol as being at this point in the section. */
2222 h->root.u.def.section = s;
2223 h->root.u.def.value = s->_raw_size;
2225 /* Increment the section size to make room for the symbol. */
2226 s->_raw_size += h->size;
2228 return true;
2231 /* Look through the relocs for a section during the first phase. */
2233 static boolean
2234 cris_elf_check_relocs (abfd, info, sec, relocs)
2235 bfd *abfd;
2236 struct bfd_link_info *info;
2237 asection *sec;
2238 const Elf_Internal_Rela *relocs;
2240 bfd *dynobj;
2241 Elf_Internal_Shdr *symtab_hdr;
2242 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2243 bfd_signed_vma *local_got_refcounts;
2244 const Elf_Internal_Rela *rel;
2245 const Elf_Internal_Rela *rel_end;
2246 asection *sgot;
2247 asection *srelgot;
2248 asection *sreloc;
2250 if (info->relocateable)
2251 return true;
2253 dynobj = elf_hash_table (info)->dynobj;
2254 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2255 sym_hashes = elf_sym_hashes (abfd);
2256 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2257 local_got_refcounts = elf_local_got_refcounts (abfd);
2259 sgot = NULL;
2260 srelgot = NULL;
2261 sreloc = NULL;
2263 if (!elf_bad_symtab (abfd))
2264 sym_hashes_end -= symtab_hdr->sh_info;
2266 rel_end = relocs + sec->reloc_count;
2267 for (rel = relocs; rel < rel_end; rel++)
2269 struct elf_link_hash_entry *h;
2270 unsigned long r_symndx;
2271 enum elf_cris_reloc_type r_type;
2273 r_symndx = ELF32_R_SYM (rel->r_info);
2274 if (r_symndx < symtab_hdr->sh_info)
2275 h = NULL;
2276 else
2277 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2279 r_type = ELF32_R_TYPE (rel->r_info);
2281 /* Some relocs require linker-created sections; we need to hang them
2282 on the first input bfd we found that contained dynamic relocs. */
2283 switch (r_type)
2285 case R_CRIS_16_GOT:
2286 case R_CRIS_32_GOT:
2287 case R_CRIS_32_GOTREL:
2288 case R_CRIS_32_PLT_GOTREL:
2289 case R_CRIS_32_PLT_PCREL:
2290 case R_CRIS_16_GOTPLT:
2291 case R_CRIS_32_GOTPLT:
2292 if (dynobj == NULL)
2294 elf_hash_table (info)->dynobj = dynobj = abfd;
2296 /* Create the .got section, so we can assume it's always
2297 present whenever there's a dynobj. */
2298 if (!_bfd_elf_create_got_section (dynobj, info))
2299 return false;
2301 break;
2303 default:
2304 break;
2307 /* Some relocs require a global offset table (but perhaps not a
2308 specific GOT entry). */
2309 switch (r_type)
2311 case R_CRIS_16_GOT:
2312 case R_CRIS_32_GOT:
2313 case R_CRIS_32_GOTREL:
2314 case R_CRIS_32_PLT_GOTREL:
2315 if (sgot == NULL)
2316 sgot = bfd_get_section_by_name (dynobj, ".got");
2318 if (local_got_refcounts == NULL)
2320 size_t size;
2322 /* We use index local_got_refcounts[-1] to count all
2323 GOT-relative relocations that do not have explicit
2324 GOT entries. */
2325 size = (symtab_hdr->sh_info + 1) * sizeof (bfd_signed_vma);
2326 local_got_refcounts = ((bfd_signed_vma *)
2327 bfd_alloc (abfd, size));
2328 if (local_got_refcounts == NULL)
2329 return false;
2330 memset (local_got_refcounts, -1, size);
2332 local_got_refcounts++;
2333 elf_local_got_refcounts (abfd) = local_got_refcounts;
2335 break;
2337 default:
2338 break;
2341 switch (r_type)
2343 case R_CRIS_16_GOTPLT:
2344 case R_CRIS_32_GOTPLT:
2345 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2346 entry) is eliminated. We can only do this for a non-local
2347 symbol. */
2348 if (h != NULL)
2350 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2351 goto handle_gotplt_reloc;
2353 /* If h is NULL then this is a local symbol, and we must make a
2354 GOT entry for it, so handle it like a GOT reloc. */
2355 /* Fall through. */
2357 case R_CRIS_16_GOT:
2358 case R_CRIS_32_GOT:
2359 /* This symbol requires a global offset table entry. */
2361 if (srelgot == NULL
2362 && (h != NULL || info->shared))
2364 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2365 if (srelgot == NULL)
2367 srelgot = bfd_make_section (dynobj, ".rela.got");
2368 if (srelgot == NULL
2369 || !bfd_set_section_flags (dynobj, srelgot,
2370 (SEC_ALLOC
2371 | SEC_LOAD
2372 | SEC_HAS_CONTENTS
2373 | SEC_IN_MEMORY
2374 | SEC_LINKER_CREATED
2375 | SEC_READONLY))
2376 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2377 return false;
2381 if (h != NULL)
2383 if (h->got.refcount == -1)
2385 h->got.refcount = 1;
2387 /* Make sure this symbol is output as a dynamic symbol. */
2388 if (h->dynindx == -1)
2390 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2391 return false;
2394 /* Allocate space in the .got section. */
2395 sgot->_raw_size += 4;
2396 /* Allocate relocation space. */
2397 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2399 else
2400 h->got.refcount++;
2402 else
2404 /* This is a global offset table entry for a local symbol. */
2405 if (local_got_refcounts[r_symndx] == -1)
2407 local_got_refcounts[r_symndx] = 1;
2409 sgot->_raw_size += 4;
2410 if (info->shared)
2412 /* If we are generating a shared object, we need to
2413 output a R_CRIS_RELATIVE reloc so that the dynamic
2414 linker can adjust this GOT entry. */
2415 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2418 else
2419 local_got_refcounts[r_symndx]++;
2421 break;
2423 case R_CRIS_32_GOTREL:
2424 /* This reference requires a global offset table.
2425 FIXME: The actual refcount isn't used currently; the .got
2426 section can't be removed if there were any references in the
2427 input. */
2428 local_got_refcounts[-1]++;
2429 break;
2431 handle_gotplt_reloc:
2433 case R_CRIS_32_PLT_GOTREL:
2434 /* This reference requires a global offset table. */
2435 local_got_refcounts[-1]++;
2436 /* Fall through. */
2438 case R_CRIS_32_PLT_PCREL:
2439 /* This symbol requires a procedure linkage table entry. We
2440 actually build the entry in adjust_dynamic_symbol,
2441 because this might be a case of linking PIC code which is
2442 never referenced by a dynamic object, in which case we
2443 don't need to generate a procedure linkage table entry
2444 after all. */
2446 /* If this is a local symbol, we resolve it directly without
2447 creating a procedure linkage table entry. */
2448 if (h == NULL)
2449 continue;
2451 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2452 if (h->plt.refcount == -1)
2453 h->plt.refcount = 1;
2454 else
2455 h->plt.refcount++;
2456 break;
2458 case R_CRIS_8:
2459 case R_CRIS_16:
2460 case R_CRIS_32:
2461 /* Let's help debug shared library creation. Any of these
2462 relocs can be used in shared libs, but pages containing them
2463 cannot be shared. Don't warn for sections we don't care
2464 about, such as debug sections or non-constant sections. We
2465 can't help tables of (global) function pointers, for example,
2466 though they must be emitted in a data section to avoid having
2467 impure text sections. */
2468 if (info->shared
2469 && (sec->flags & SEC_ALLOC) != 0
2470 && (sec->flags & SEC_READONLY) != 0)
2472 /* FIXME: How do we make this optionally a warning only? */
2473 if (abfd->my_archive)
2474 (*_bfd_error_handler)
2475 (_("%s(%s), section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2476 bfd_get_filename (bfd_my_archive (abfd)),
2477 bfd_get_filename (abfd),
2478 sec->name,
2479 cris_elf_howto_table[r_type].name);
2480 else
2481 (*_bfd_error_handler)
2482 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2483 bfd_get_filename (abfd),
2484 sec->name,
2485 cris_elf_howto_table[r_type].name);
2487 /* Fall through. */
2489 case R_CRIS_8_PCREL:
2490 case R_CRIS_16_PCREL:
2491 case R_CRIS_32_PCREL:
2492 if (h != NULL)
2494 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2496 /* Make sure a plt entry is created for this symbol if it
2497 turns out to be a function defined by a dynamic object. */
2498 if (h->plt.refcount == -1)
2499 h->plt.refcount = 1;
2500 else
2501 h->plt.refcount++;
2504 /* If we are creating a shared library and this is not a local
2505 symbol, we need to copy the reloc into the shared library.
2506 However when linking with -Bsymbolic and this is a global
2507 symbol which is defined in an object we are including in the
2508 link (i.e., DEF_REGULAR is set), then we can resolve the
2509 reloc directly. At this point we have not seen all the input
2510 files, so it is possible that DEF_REGULAR is not set now but
2511 will be set later (it is never cleared). In case of a weak
2512 definition, DEF_REGULAR may be cleared later by a strong
2513 definition in a shared library. We account for that
2514 possibility below by storing information in the relocs_copied
2515 field of the hash table entry. A similar situation occurs
2516 when creating shared libraries and symbol visibility changes
2517 render the symbol local. */
2519 /* No need to do anything if we're not creating a shared object. */
2520 if (! info->shared)
2521 break;
2523 /* We don't need to handle relocs into sections not going into
2524 the "real" output. */
2525 if ((sec->flags & SEC_ALLOC) == 0)
2526 break;
2528 /* We can only eliminate PC-relative relocs. */
2529 if (r_type == R_CRIS_8_PCREL
2530 || r_type == R_CRIS_16_PCREL
2531 || r_type == R_CRIS_32_PCREL)
2533 /* If the symbol is local, then we can eliminate the reloc. */
2534 if (h == NULL)
2535 break;
2537 /* If this is with -Bsymbolic and the symbol isn't weak, and
2538 is defined by an ordinary object (the ones we include in
2539 this shared library) then we can also eliminate the
2540 reloc. See comment above for more eliminable cases which
2541 we can't identify at this time. */
2542 if (info->symbolic
2543 && h->root.type != bfd_link_hash_defweak
2544 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2545 break;
2548 /* We create a reloc section in dynobj and make room for this
2549 reloc. */
2550 if (sreloc == NULL)
2552 const char *name;
2554 name = (bfd_elf_string_from_elf_section
2555 (abfd,
2556 elf_elfheader (abfd)->e_shstrndx,
2557 elf_section_data (sec)->rel_hdr.sh_name));
2558 if (name == NULL)
2559 return false;
2561 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2562 && strcmp (bfd_get_section_name (abfd, sec),
2563 name + 5) == 0);
2565 sreloc = bfd_get_section_by_name (dynobj, name);
2566 if (sreloc == NULL)
2568 sreloc = bfd_make_section (dynobj, name);
2569 if (sreloc == NULL
2570 || !bfd_set_section_flags (dynobj, sreloc,
2571 (SEC_ALLOC
2572 | SEC_LOAD
2573 | SEC_HAS_CONTENTS
2574 | SEC_IN_MEMORY
2575 | SEC_LINKER_CREATED
2576 | SEC_READONLY))
2577 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2578 return false;
2580 if (sec->flags & SEC_READONLY)
2581 info->flags |= DF_TEXTREL;
2584 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2586 /* If we are linking with -Bsymbolic, we count the number of PC
2587 relative relocations we have entered for this symbol, so that
2588 we can discard them again if the symbol is later defined by a
2589 regular object. We know that h is really a pointer to an
2590 elf_cris_link_hash_entry. */
2591 if ((r_type == R_CRIS_8_PCREL
2592 || r_type == R_CRIS_16_PCREL
2593 || r_type == R_CRIS_32_PCREL)
2594 && info->symbolic)
2596 struct elf_cris_link_hash_entry *eh;
2597 struct elf_cris_pcrel_relocs_copied *p;
2599 eh = (struct elf_cris_link_hash_entry *) h;
2601 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2602 if (p->section == sreloc)
2603 break;
2605 if (p == NULL)
2607 p = ((struct elf_cris_pcrel_relocs_copied *)
2608 bfd_alloc (dynobj, sizeof *p));
2609 if (p == NULL)
2610 return false;
2611 p->next = eh->pcrel_relocs_copied;
2612 eh->pcrel_relocs_copied = p;
2613 p->section = sreloc;
2614 p->count = 0;
2617 ++p->count;
2619 break;
2621 /* This relocation describes the C++ object vtable hierarchy.
2622 Reconstruct it for later use during GC. */
2623 case R_CRIS_GNU_VTINHERIT:
2624 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2625 return false;
2626 break;
2628 /* This relocation describes which C++ vtable entries are actually
2629 used. Record for later use during GC. */
2630 case R_CRIS_GNU_VTENTRY:
2631 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2632 return false;
2633 break;
2635 default:
2636 /* Other relocs do not appear here. */
2637 bfd_set_error (bfd_error_bad_value);
2638 return false;
2642 return true;
2645 /* Set the sizes of the dynamic sections. */
2647 static boolean
2648 elf_cris_size_dynamic_sections (output_bfd, info)
2649 bfd *output_bfd ATTRIBUTE_UNUSED;
2650 struct bfd_link_info *info;
2652 bfd *dynobj;
2653 asection *s;
2654 boolean plt;
2655 boolean relocs;
2657 dynobj = elf_hash_table (info)->dynobj;
2658 BFD_ASSERT (dynobj != NULL);
2660 if (elf_hash_table (info)->dynamic_sections_created)
2662 /* Set the contents of the .interp section to the interpreter. */
2663 if (!info->shared)
2665 s = bfd_get_section_by_name (dynobj, ".interp");
2666 BFD_ASSERT (s != NULL);
2667 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2668 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2671 else
2673 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2674 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2675 elf_cris_adjust_gotplt_to_got,
2676 (PTR) info);
2678 /* We may have created entries in the .rela.got section.
2679 However, if we are not creating the dynamic sections, we will
2680 not actually use these entries. Reset the size of .rela.got,
2681 which will cause it to get stripped from the output file
2682 below. */
2683 s = bfd_get_section_by_name (dynobj, ".rela.got");
2684 if (s != NULL)
2685 s->_raw_size = 0;
2688 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2689 relative relocs against symbols defined in a regular object. We
2690 allocated space for them in the check_relocs routine, but we will not
2691 fill them in in the relocate_section routine. We also discard space
2692 for relocs that have become for local symbols due to symbol
2693 visibility changes. For programs, we discard space for relocs for
2694 symbols not referenced by any dynamic object. */
2695 if (info->shared)
2696 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2697 elf_cris_discard_excess_dso_dynamics,
2698 (PTR) info);
2699 else
2700 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2701 elf_cris_discard_excess_program_dynamics,
2702 (PTR) info);
2704 /* The check_relocs and adjust_dynamic_symbol entry points have
2705 determined the sizes of the various dynamic sections. Allocate
2706 memory for them. */
2707 plt = false;
2708 relocs = false;
2709 for (s = dynobj->sections; s != NULL; s = s->next)
2711 const char *name;
2712 boolean strip;
2714 if ((s->flags & SEC_LINKER_CREATED) == 0)
2715 continue;
2717 /* It's OK to base decisions on the section name, because none
2718 of the dynobj section names depend upon the input files. */
2719 name = bfd_get_section_name (dynobj, s);
2721 strip = false;
2723 if (strcmp (name, ".plt") == 0)
2725 if (s->_raw_size == 0)
2727 /* Strip this section if we don't need it; see the
2728 comment below. */
2729 strip = true;
2731 else
2733 /* Remember whether there is a PLT. */
2734 plt = true;
2737 else if (strncmp (name, ".rela", 5) == 0)
2739 if (s->_raw_size == 0)
2741 /* If we don't need this section, strip it from the
2742 output file. This is mostly to handle .rela.bss and
2743 .rela.plt. We must create both sections in
2744 create_dynamic_sections, because they must be created
2745 before the linker maps input sections to output
2746 sections. The linker does that before
2747 adjust_dynamic_symbol is called, and it is that
2748 function which decides whether anything needs to go
2749 into these sections. */
2750 strip = true;
2752 else
2754 /* Remember whether there are any reloc sections other
2755 than .rela.plt. */
2756 if (strcmp (name, ".rela.plt") != 0)
2757 relocs = true;
2759 /* We use the reloc_count field as a counter if we need
2760 to copy relocs into the output file. */
2761 s->reloc_count = 0;
2764 else if (strncmp (name, ".got", 4) != 0)
2766 /* It's not one of our sections, so don't allocate space. */
2767 continue;
2770 if (strip)
2772 _bfd_strip_section_from_output (info, s);
2773 continue;
2776 /* Allocate memory for the section contents. We use bfd_zalloc here
2777 in case unused entries are not reclaimed before the section's
2778 contents are written out. This should not happen, but this way
2779 if it does, we will not write out garbage. For reloc sections,
2780 this will make entries have the type R_CRIS_NONE. */
2781 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2782 if (s->contents == NULL && s->_raw_size != 0)
2783 return false;
2786 if (elf_hash_table (info)->dynamic_sections_created)
2788 /* Add some entries to the .dynamic section. We fill in the
2789 values later, in elf_cris_finish_dynamic_sections, but we
2790 must add the entries now so that we get the correct size for
2791 the .dynamic section. The DT_DEBUG entry is filled in by the
2792 dynamic linker and used by the debugger. */
2793 if (!info->shared)
2795 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2796 return false;
2799 if (plt)
2801 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2802 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2803 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2804 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2805 return false;
2808 if (relocs)
2810 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2811 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2812 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2813 sizeof (Elf32_External_Rela)))
2814 return false;
2817 if ((info->flags & DF_TEXTREL) != 0)
2819 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2820 return false;
2821 info->flags |= DF_TEXTREL;
2825 return true;
2828 /* This function is called via elf_cris_link_hash_traverse if we are
2829 creating a shared object. In the -Bsymbolic case, it discards the
2830 space allocated to copy PC relative relocs against symbols which
2831 are defined in regular objects. For the normal non-symbolic case,
2832 we also discard space for relocs that have become local due to
2833 symbol visibility changes. We allocated space for them in the
2834 check_relocs routine, but we won't fill them in in the
2835 relocate_section routine. */
2837 static boolean
2838 elf_cris_discard_excess_dso_dynamics (h, inf)
2839 struct elf_cris_link_hash_entry *h;
2840 PTR inf;
2842 struct elf_cris_pcrel_relocs_copied *s;
2843 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2845 /* If a symbol has been forced local or we have found a regular
2846 definition for the symbolic link case, then we won't be needing
2847 any relocs. */
2848 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2849 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2850 || info->symbolic))
2852 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2853 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2856 return true;
2859 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2860 creating a shared object. We discard space for relocs for symbols put
2861 in the .got, but which we found we do not have to resolve at run-time. */
2863 static boolean
2864 elf_cris_discard_excess_program_dynamics (h, inf)
2865 struct elf_cris_link_hash_entry *h;
2866 PTR inf;
2868 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2870 /* If we're not creating a shared library and have a symbol which is
2871 referred to by .got references, but the symbol is defined locally,
2872 (or rather, not referred to by a DSO and not defined by a DSO) then
2873 lose the reloc for the .got (don't allocate room for it). */
2874 if ((h->root.elf_link_hash_flags
2875 & (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_DEF_DYNAMIC)) == 0)
2877 if (h->root.got.refcount > 0
2878 /* The size of this section is only valid and in sync with the
2879 various reference counts if we do dynamic; don't decrement it
2880 otherwise. */
2881 && elf_hash_table (info)->dynamic_sections_created)
2883 bfd *dynobj = elf_hash_table (info)->dynobj;
2884 asection *srelgot;
2886 BFD_ASSERT (dynobj != NULL);
2888 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2890 BFD_ASSERT (srelgot != NULL);
2892 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2895 /* If the locally-defined symbol isn't used by a DSO, then we don't
2896 have to export it as a dynamic symbol. This was already done for
2897 functions; doing this for all symbols would presumably not
2898 introduce new problems. */
2899 h->root.dynindx = -1;
2902 return true;
2905 /* Reject a file depending on presence and expectation of prefixed
2906 underscores on symbols. */
2908 static boolean
2909 cris_elf_object_p (abfd)
2910 bfd *abfd;
2912 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2913 return (bfd_get_symbol_leading_char (abfd) == '_');
2914 else
2915 return (bfd_get_symbol_leading_char (abfd) == 0);
2918 /* Mark presence or absence of leading underscore. */
2920 static void
2921 cris_elf_final_write_processing (abfd, linker)
2922 bfd *abfd;
2923 boolean linker ATTRIBUTE_UNUSED;
2925 if (bfd_get_symbol_leading_char (abfd) == '_')
2926 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2927 else
2928 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2931 /* Display the flags field. */
2933 static boolean
2934 cris_elf_print_private_bfd_data (abfd, ptr)
2935 bfd *abfd;
2936 PTR ptr;
2938 FILE *file = (FILE *) ptr;
2940 BFD_ASSERT (abfd != NULL && ptr != NULL)
2942 _bfd_elf_print_private_bfd_data (abfd, ptr);
2944 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2946 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2947 fprintf (file, _(" [symbols have a _ prefix]"));
2949 fputc ('\n', file);
2950 return true;
2953 /* Don't mix files with and without a leading underscore. */
2955 static boolean
2956 cris_elf_merge_private_bfd_data (ibfd, obfd)
2957 bfd *ibfd;
2958 bfd *obfd;
2960 flagword old_flags, new_flags;
2962 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2963 return false;
2965 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2966 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2967 return true;
2969 if (! elf_flags_init (obfd))
2971 /* This happens when ld starts out with a 'blank' output file. */
2972 elf_flags_init (obfd) = true;
2974 /* Set flags according to current bfd_target. */
2975 cris_elf_final_write_processing (obfd, false);
2978 old_flags = elf_elfheader (obfd)->e_flags;
2979 new_flags = elf_elfheader (ibfd)->e_flags;
2981 /* Is this good or bad? We'll follow with other excluding flags. */
2982 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
2984 (*_bfd_error_handler)
2985 ((new_flags & EF_CRIS_UNDERSCORE)
2986 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
2987 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
2988 bfd_get_filename (ibfd));
2989 bfd_set_error (bfd_error_bad_value);
2990 return false;
2993 return true;
2997 static enum elf_reloc_type_class
2998 elf_cris_reloc_type_class (type)
2999 int type;
3001 switch (type)
3003 case R_CRIS_RELATIVE:
3004 return reloc_class_relative;
3005 case R_CRIS_JUMP_SLOT:
3006 return reloc_class_plt;
3007 case R_CRIS_COPY:
3008 return reloc_class_copy;
3009 default:
3010 return reloc_class_normal;
3014 #define ELF_ARCH bfd_arch_cris
3015 #define ELF_MACHINE_CODE EM_CRIS
3016 #define ELF_MAXPAGESIZE 0x2000
3018 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3019 #define TARGET_LITTLE_NAME "elf32-cris"
3020 #define elf_symbol_leading_char 0
3022 #define elf_info_to_howto_rel NULL
3023 #define elf_info_to_howto cris_info_to_howto_rela
3024 #define elf_backend_relocate_section cris_elf_relocate_section
3025 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3026 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3027 #define elf_backend_check_relocs cris_elf_check_relocs
3029 #define elf_backend_can_gc_sections 1
3031 #define elf_backend_object_p cris_elf_object_p
3032 #define elf_backend_final_write_processing \
3033 cris_elf_final_write_processing
3034 #define bfd_elf32_bfd_print_private_bfd_data \
3035 cris_elf_print_private_bfd_data
3036 #define bfd_elf32_bfd_merge_private_bfd_data \
3037 cris_elf_merge_private_bfd_data
3039 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3041 #define bfd_elf32_bfd_link_hash_table_create \
3042 elf_cris_link_hash_table_create
3043 #define elf_backend_adjust_dynamic_symbol \
3044 elf_cris_adjust_dynamic_symbol
3045 #define elf_backend_size_dynamic_sections \
3046 elf_cris_size_dynamic_sections
3047 #define elf_backend_finish_dynamic_symbol \
3048 elf_cris_finish_dynamic_symbol
3049 #define elf_backend_finish_dynamic_sections \
3050 elf_cris_finish_dynamic_sections
3051 #define elf_backend_create_dynamic_sections \
3052 _bfd_elf_create_dynamic_sections
3053 #define bfd_elf32_bfd_final_link \
3054 _bfd_elf32_gc_common_final_link
3055 #define elf_backend_hide_symbol elf_cris_hide_symbol
3056 #define elf_backend_reloc_type_class elf_cris_reloc_type_class
3058 #define elf_backend_want_got_plt 1
3059 #define elf_backend_plt_readonly 1
3060 #define elf_backend_want_plt_sym 0
3061 #define elf_backend_got_header_size 12
3062 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3064 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3065 linking and libraries (if it's a win of any significance). Until then,
3066 take the easy route. */
3067 #define elf_backend_may_use_rel_p 0
3068 #define elf_backend_may_use_rela_p 1
3070 #include "elf32-target.h"
3072 #define INCLUDED_TARGET_FILE
3074 #undef TARGET_LITTLE_SYM
3075 #undef TARGET_LITTLE_NAME
3076 #undef elf_symbol_leading_char
3078 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3079 #define TARGET_LITTLE_NAME "elf32-us-cris"
3080 #define elf_symbol_leading_char '_'
3082 #include "elf32-target.h"