* emultempl/elf32.em: For SEC_EXCLUDE sections, ensure that
[binutils.git] / bfd / elf32-cris.c
blobe57202b37dcc0d4fc21dd4f7b01277f1b41da8cd
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 *));
89 static reloc_howto_type cris_elf_howto_table [] =
91 /* This reloc does nothing. */
92 HOWTO (R_CRIS_NONE, /* type */
93 0, /* rightshift */
94 2, /* size (0 = byte, 1 = short, 2 = long) */
95 32, /* bitsize */
96 false, /* pc_relative */
97 0, /* bitpos */
98 complain_overflow_bitfield, /* complain_on_overflow */
99 bfd_elf_generic_reloc, /* special_function */
100 "R_CRIS_NONE", /* name */
101 false, /* partial_inplace */
102 0, /* src_mask */
103 0, /* dst_mask */
104 false), /* pcrel_offset */
106 /* An 8 bit absolute relocation. */
107 HOWTO (R_CRIS_8, /* type */
108 0, /* rightshift */
109 0, /* size (0 = byte, 1 = short, 2 = long) */
110 8, /* bitsize */
111 false, /* pc_relative */
112 0, /* bitpos */
113 complain_overflow_bitfield, /* complain_on_overflow */
114 bfd_elf_generic_reloc, /* special_function */
115 "R_CRIS_8", /* name */
116 false, /* partial_inplace */
117 0x0000, /* src_mask */
118 0x00ff, /* dst_mask */
119 false), /* pcrel_offset */
121 /* A 16 bit absolute relocation. */
122 HOWTO (R_CRIS_16, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
126 false, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_bitfield, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_CRIS_16", /* name */
131 false, /* partial_inplace */
132 0x00000000, /* src_mask */
133 0x0000ffff, /* dst_mask */
134 false), /* pcrel_offset */
136 /* A 32 bit absolute relocation. */
137 HOWTO (R_CRIS_32, /* type */
138 0, /* rightshift */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
140 32, /* bitsize */
141 false, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 bfd_elf_generic_reloc, /* special_function */
145 "R_CRIS_32", /* name */
146 false, /* partial_inplace */
147 0x00000000, /* src_mask */
148 0xffffffff, /* dst_mask */
149 false), /* pcrel_offset */
151 /* An 8 bit PC-relative relocation. */
152 HOWTO (R_CRIS_8_PCREL, /* type */
153 0, /* rightshift */
154 0, /* size (0 = byte, 1 = short, 2 = long) */
155 8, /* bitsize */
156 true, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_bitfield, /* complain_on_overflow */
159 bfd_elf_generic_reloc, /* special_function */
160 "R_CRIS_8_PCREL", /* name */
161 false, /* partial_inplace */
162 0x0000, /* src_mask */
163 0x00ff, /* dst_mask */
164 true), /* pcrel_offset */
166 /* A 16 bit PC-relative relocation. */
167 HOWTO (R_CRIS_16_PCREL, /* type */
168 0, /* rightshift */
169 1, /* size (0 = byte, 1 = short, 2 = long) */
170 16, /* bitsize */
171 true, /* pc_relative */
172 0, /* bitpos */
173 complain_overflow_bitfield, /* complain_on_overflow */
174 bfd_elf_generic_reloc, /* special_function */
175 "R_CRIS_16", /* name */
176 false, /* partial_inplace */
177 0x00000000, /* src_mask */
178 0x0000ffff, /* dst_mask */
179 true), /* pcrel_offset */
181 /* A 32 bit PC-relative relocation. */
182 HOWTO (R_CRIS_32_PCREL, /* type */
183 0, /* rightshift */
184 2, /* size (0 = byte, 1 = short, 2 = long) */
185 32, /* bitsize */
186 true, /* pc_relative */
187 0, /* bitpos */
188 complain_overflow_bitfield, /* complain_on_overflow */
189 bfd_elf_generic_reloc, /* special_function */
190 "R_CRIS_32", /* name */
191 false, /* partial_inplace */
192 0x00000000, /* src_mask */
193 0xffffffff, /* dst_mask */
194 true), /* pcrel_offset */
196 /* GNU extension to record C++ vtable hierarchy. */
197 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
198 0, /* rightshift */
199 2, /* size (0 = byte, 1 = short, 2 = long) */
200 0, /* bitsize */
201 false, /* pc_relative */
202 0, /* bitpos */
203 complain_overflow_dont, /* complain_on_overflow */
204 NULL, /* special_function */
205 "R_CRIS_GNU_VTINHERIT", /* name */
206 false, /* partial_inplace */
207 0, /* src_mask */
208 0, /* dst_mask */
209 false), /* pcrel_offset */
211 /* GNU extension to record C++ vtable member usage. */
212 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
213 0, /* rightshift */
214 2, /* size (0 = byte, 1 = short, 2 = long) */
215 0, /* bitsize */
216 false, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
220 "R_CRIS_GNU_VTENTRY", /* name */
221 false, /* partial_inplace */
222 0, /* src_mask */
223 0, /* dst_mask */
224 false), /* pcrel_offset */
226 /* This is used only by the dynamic linker. The symbol should exist
227 both in the object being run and in some shared library. The
228 dynamic linker copies the data addressed by the symbol from the
229 shared library into the object, because the object being
230 run has to have the data at some particular address. */
231 HOWTO (R_CRIS_COPY, /* type */
232 0, /* rightshift */
233 2, /* size (0 = byte, 1 = short, 2 = long) */
234 32, /* bitsize */
235 false, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_bitfield, /* complain_on_overflow */
238 bfd_elf_generic_reloc, /* special_function */
239 "R_CRIS_COPY", /* name */
240 false, /* partial_inplace */
241 0, /* src_mask */
242 0, /* dst_mask */
243 false), /* pcrel_offset */
245 /* Like R_CRIS_32, but used when setting global offset table entries. */
246 HOWTO (R_CRIS_GLOB_DAT, /* type */
247 0, /* rightshift */
248 2, /* size (0 = byte, 1 = short, 2 = long) */
249 32, /* bitsize */
250 false, /* pc_relative */
251 0, /* bitpos */
252 complain_overflow_bitfield, /* complain_on_overflow */
253 bfd_elf_generic_reloc, /* special_function */
254 "R_CRIS_GLOB_DAT", /* name */
255 false, /* partial_inplace */
256 0, /* src_mask */
257 0xffffffff, /* dst_mask */
258 false), /* pcrel_offset */
260 /* Marks a procedure linkage table entry for a symbol. */
261 HOWTO (R_CRIS_JUMP_SLOT, /* type */
262 0, /* rightshift */
263 2, /* size (0 = byte, 1 = short, 2 = long) */
264 32, /* bitsize */
265 false, /* pc_relative */
266 0, /* bitpos */
267 complain_overflow_bitfield, /* complain_on_overflow */
268 bfd_elf_generic_reloc, /* special_function */
269 "R_CRIS_JUMP_SLOT", /* name */
270 false, /* partial_inplace */
271 0, /* src_mask */
272 0, /* dst_mask */
273 false), /* pcrel_offset */
275 /* Used only by the dynamic linker. When the object is run, this
276 longword is set to the load address of the object, plus the
277 addend. */
278 HOWTO (R_CRIS_RELATIVE, /* type */
279 0, /* rightshift */
280 2, /* size (0 = byte, 1 = short, 2 = long) */
281 32, /* bitsize */
282 false, /* pc_relative */
283 0, /* bitpos */
284 complain_overflow_bitfield, /* complain_on_overflow */
285 bfd_elf_generic_reloc, /* special_function */
286 "R_CRIS_RELATIVE", /* name */
287 false, /* partial_inplace */
288 0, /* src_mask */
289 0xffffffff, /* dst_mask */
290 false), /* pcrel_offset */
292 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
293 HOWTO (R_CRIS_16_GOT, /* type */
294 0, /* rightshift */
295 1, /* size (0 = byte, 1 = short, 2 = long) */
296 16, /* bitsize */
297 false, /* pc_relative */
298 0, /* bitpos */
299 complain_overflow_bitfield, /* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_CRIS_16_GOT", /* name */
302 false, /* partial_inplace */
303 0, /* src_mask */
304 0xffff, /* dst_mask */
305 false), /* pcrel_offset */
307 HOWTO (R_CRIS_32_GOT, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 32, /* bitsize */
311 false, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_bitfield, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_CRIS_32_GOT", /* name */
316 false, /* partial_inplace */
317 0, /* src_mask */
318 0xffffffff, /* dst_mask */
319 false), /* pcrel_offset */
321 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
322 the GOT table for the symbol. */
323 HOWTO (R_CRIS_16_GOTPLT, /* type */
324 0, /* rightshift */
325 1, /* size (0 = byte, 1 = short, 2 = long) */
326 16, /* bitsize */
327 false, /* pc_relative */
328 0, /* bitpos */
329 complain_overflow_bitfield, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* special_function */
331 "R_CRIS_16_GOTPLT", /* name */
332 false, /* partial_inplace */
333 0, /* src_mask */
334 0xffff, /* dst_mask */
335 false), /* pcrel_offset */
337 HOWTO (R_CRIS_32_GOTPLT, /* type */
338 0, /* rightshift */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
340 32, /* bitsize */
341 false, /* pc_relative */
342 0, /* bitpos */
343 complain_overflow_bitfield, /* complain_on_overflow */
344 bfd_elf_generic_reloc, /* special_function */
345 "R_CRIS_32_GOTPLT", /* name */
346 false, /* partial_inplace */
347 0, /* src_mask */
348 0xffffffff, /* dst_mask */
349 false), /* pcrel_offset */
351 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
352 be necessary. */
353 HOWTO (R_CRIS_32_GOTREL, /* type */
354 0, /* rightshift */
355 2, /* size (0 = byte, 1 = short, 2 = long) */
356 32, /* bitsize */
357 false, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_bitfield, /* complain_on_overflow */
360 bfd_elf_generic_reloc, /* special_function */
361 "R_CRIS_32_GOTREL", /* name */
362 false, /* partial_inplace */
363 0, /* src_mask */
364 0xffffffff, /* dst_mask */
365 false), /* pcrel_offset */
367 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
368 to create PLT entry for symbol. */
369 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
370 0, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 32, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_bitfield, /* complain_on_overflow */
376 bfd_elf_generic_reloc, /* special_function */
377 "R_CRIS_32_PLT_GOTREL", /* name */
378 false, /* partial_inplace */
379 0, /* src_mask */
380 0xffffffff, /* dst_mask */
381 false), /* pcrel_offset */
383 /* A 32-bit offset from PC (location after the relocation) + addend to
384 entry for this symbol in PLT and request to create PLT entry for
385 symbol. */
386 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 32, /* bitsize */
390 true, /* pc_relative */
391 0, /* bitpos */
392 complain_overflow_bitfield, /* complain_on_overflow */
393 bfd_elf_generic_reloc, /* special_function */
394 "R_CRIS_32_PLT_PCREL", /* name */
395 false, /* partial_inplace */
396 0, /* src_mask */
397 0xffffffff, /* dst_mask */
398 true) /* pcrel_offset */
401 /* Map BFD reloc types to CRIS ELF reloc types. */
403 struct cris_reloc_map
405 bfd_reloc_code_real_type bfd_reloc_val;
406 unsigned int cris_reloc_val;
409 static const struct cris_reloc_map cris_reloc_map [] =
411 { BFD_RELOC_NONE, R_CRIS_NONE },
412 { BFD_RELOC_8, R_CRIS_8 },
413 { BFD_RELOC_16, R_CRIS_16 },
414 { BFD_RELOC_32, R_CRIS_32 },
415 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
416 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
417 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
418 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
419 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
420 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
421 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
422 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
423 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
424 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
425 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
426 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
427 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
428 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
429 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
430 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
433 static reloc_howto_type *
434 cris_reloc_type_lookup (abfd, code)
435 bfd * abfd ATTRIBUTE_UNUSED;
436 bfd_reloc_code_real_type code;
438 unsigned int i;
440 for (i = sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]);
441 --i;)
442 if (cris_reloc_map [i].bfd_reloc_val == code)
443 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
445 return NULL;
448 /* Set the howto pointer for an CRIS ELF reloc. */
450 static void
451 cris_info_to_howto_rela (abfd, cache_ptr, dst)
452 bfd * abfd ATTRIBUTE_UNUSED;
453 arelent * cache_ptr;
454 Elf32_Internal_Rela * dst;
456 unsigned int r_type;
458 r_type = ELF32_R_TYPE (dst->r_info);
459 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
460 cache_ptr->howto = & cris_elf_howto_table [r_type];
463 /* The name of the dynamic interpreter. This is put in the .interp
464 section. */
466 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
468 /* The size in bytes of an entry in the procedure linkage table. */
470 #define PLT_ENTRY_SIZE 20
472 /* The first entry in an absolute procedure linkage table looks like this. */
474 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
476 0xfc, 0xe1,
477 0x7e, 0x7e, /* push mof. */
478 0x7f, 0x0d, /* (dip [pc+]) */
479 0, 0, 0, 0, /* Replaced with address of .got + 4. */
480 0x30, 0x7a, /* move [...],mof */
481 0x7f, 0x0d, /* (dip [pc+]) */
482 0, 0, 0, 0, /* Replaced with address of .got + 8. */
483 0x30, 0x09, /* jump [...] */
486 /* Subsequent entries in an absolute procedure linkage table look like
487 this. */
489 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
491 0x7f, 0x0d, /* (dip [pc+]) */
492 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
493 0x30, 0x09, /* jump [...] */
494 0x3f, 0x7e, /* move [pc+],mof */
495 0, 0, 0, 0, /* Replaced with offset into relocation table. */
496 0x2f, 0xfe, /* add.d [pc+],pc */
497 0xec, 0xff,
498 0xff, 0xff /* Replaced with offset to start of .plt. */
501 /* The first entry in a PIC procedure linkage table looks like this. */
503 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
505 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
506 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
507 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
508 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
511 /* Subsequent entries in a PIC procedure linkage table look like this. */
513 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
515 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
516 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
517 0x30, 0x09, /* jump [...] */
518 0x3f, 0x7e, /* move [pc+],mof */
519 0, 0, 0, 0, /* Replaced with offset into relocation table. */
520 0x2f, 0xfe, /* add.d [pc+],pc */
521 0xec, 0xff, /* Replaced with offset to start of .plt. */
522 0xff, 0xff
525 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
526 (and most other PIC/shlib stuff). Check that we don't drift away
527 without reason.
529 The CRIS linker, like the m68k and i386 linkers (and probably the rest
530 too) needs to keep track of the number of relocs that it decides to
531 copy in check_relocs for each symbol. This is so that it can discard
532 PC relative relocs if it doesn't need them when linking with
533 -Bsymbolic. We store the information in a field extending the regular
534 ELF linker hash table. */
536 /* This structure keeps track of the number of PC relative relocs we have
537 copied for a given symbol. */
539 struct elf_cris_pcrel_relocs_copied
541 /* Next section. */
542 struct elf_cris_pcrel_relocs_copied *next;
543 /* A section in dynobj. */
544 asection *section;
545 /* Number of relocs copied in this section. */
546 bfd_size_type count;
549 /* CRIS ELF linker hash entry. */
551 struct elf_cris_link_hash_entry
553 struct elf_link_hash_entry root;
555 /* Number of PC relative relocs copied for this symbol. */
556 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
558 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
559 both a general GOT and a PLT-specific GOT entry for the same symbol,
560 when it is referenced both as a function and as a function pointer.
562 Number of GOTPLT references for a function. */
563 bfd_signed_vma gotplt_refcount;
565 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
566 (zero is never used as an index). FIXME: We should be able to fold
567 this with gotplt_refcount in a union, like the got and plt unions in
568 elf_link_hash_entry. */
569 bfd_size_type gotplt_offset;
572 /* CRIS ELF linker hash table. */
574 struct elf_cris_link_hash_table
576 struct elf_link_hash_table root;
578 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
579 since we try and avoid creating GOTPLT:s when there's already a GOT.
580 Instead, we keep and update the next available index here. */
581 bfd_size_type next_gotplt_entry;
584 /* Traverse a CRIS ELF linker hash table. */
586 #define elf_cris_link_hash_traverse(table, func, info) \
587 (elf_link_hash_traverse \
588 (&(table)->root, \
589 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
590 (info)))
592 /* Get the CRIS ELF linker hash table from a link_info structure. */
594 #define elf_cris_hash_table(p) \
595 ((struct elf_cris_link_hash_table *) (p)->hash)
597 /* Create an entry in a CRIS ELF linker hash table. */
599 static struct bfd_hash_entry *
600 elf_cris_link_hash_newfunc (entry, table, string)
601 struct bfd_hash_entry *entry;
602 struct bfd_hash_table *table;
603 const char *string;
605 struct elf_cris_link_hash_entry *ret =
606 (struct elf_cris_link_hash_entry *) entry;
608 /* Allocate the structure if it has not already been allocated by a
609 subclass. */
610 if (ret == (struct elf_cris_link_hash_entry *) NULL)
611 ret = ((struct elf_cris_link_hash_entry *)
612 bfd_hash_allocate (table,
613 sizeof (struct elf_cris_link_hash_entry)));
614 if (ret == (struct elf_cris_link_hash_entry *) NULL)
615 return (struct bfd_hash_entry *) ret;
617 /* Call the allocation method of the superclass. */
618 ret = ((struct elf_cris_link_hash_entry *)
619 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
620 table, string));
621 if (ret != (struct elf_cris_link_hash_entry *) NULL)
623 ret->pcrel_relocs_copied = NULL;
624 ret->gotplt_refcount = 0;
625 ret->gotplt_offset = 0;
628 return (struct bfd_hash_entry *) ret;
631 /* Create a CRIS ELF linker hash table. */
633 static struct bfd_link_hash_table *
634 elf_cris_link_hash_table_create (abfd)
635 bfd *abfd;
637 struct elf_cris_link_hash_table *ret;
639 ret = ((struct elf_cris_link_hash_table *)
640 bfd_alloc (abfd, sizeof (struct elf_cris_link_hash_table)));
641 if (ret == (struct elf_cris_link_hash_table *) NULL)
642 return NULL;
644 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
645 elf_cris_link_hash_newfunc))
647 bfd_release (abfd, ret);
648 return NULL;
651 /* Initialize to skip over the first three entries in the gotplt; they
652 are used for run-time symbol evaluation. */
653 ret->next_gotplt_entry = 12;
655 return &ret->root.root;
658 /* Perform a single relocation. By default we use the standard BFD
659 routines, with a few tweaks. */
661 static bfd_reloc_status_type
662 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
663 relocation)
664 reloc_howto_type * howto;
665 bfd * input_bfd;
666 asection * input_section;
667 bfd_byte * contents;
668 Elf_Internal_Rela * rel;
669 bfd_vma relocation;
671 bfd_reloc_status_type r;
673 /* PC-relative relocations are relative to the position *after*
674 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
675 not a single byte, since PC must be 16-bit-aligned. */
676 switch (ELF32_R_TYPE (rel->r_info))
678 /* Check that the 16-bit GOT relocs are positive. */
679 case R_CRIS_16_GOTPLT:
680 case R_CRIS_16_GOT:
681 if ((bfd_signed_vma) relocation < 0)
682 return bfd_reloc_overflow;
683 break;
685 case R_CRIS_32_PLT_PCREL:
686 case R_CRIS_32_PCREL:
687 relocation -= 2;
688 /* Fall through. */
689 case R_CRIS_8_PCREL:
690 case R_CRIS_16_PCREL:
691 relocation -= 2;
692 break;
694 default:
695 break;
698 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
699 contents, rel->r_offset,
700 relocation, rel->r_addend);
701 return r;
704 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
705 copied, for further comments. */
707 static boolean
708 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
709 contents, relocs, local_syms, local_sections)
710 bfd * output_bfd ATTRIBUTE_UNUSED;
711 struct bfd_link_info * info;
712 bfd * input_bfd;
713 asection * input_section;
714 bfd_byte * contents;
715 Elf_Internal_Rela * relocs;
716 Elf_Internal_Sym * local_syms;
717 asection ** local_sections;
719 bfd *dynobj;
720 Elf_Internal_Shdr * symtab_hdr;
721 struct elf_link_hash_entry ** sym_hashes;
722 bfd_vma *local_got_offsets;
723 asection *sgot;
724 asection *splt;
725 asection *sreloc;
726 Elf_Internal_Rela * rel;
727 Elf_Internal_Rela * relend;
729 dynobj = elf_hash_table (info)->dynobj;
730 local_got_offsets = elf_local_got_offsets (input_bfd);
731 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
732 sym_hashes = elf_sym_hashes (input_bfd);
733 relend = relocs + input_section->reloc_count;
735 /* It seems this can happen with erroneous or unsupported input (mixing
736 a.out and elf in an archive, for example.) */
737 if (sym_hashes == NULL)
738 return false;
740 sgot = NULL;
741 splt = NULL;
742 sreloc = NULL;
744 if (dynobj != NULL)
746 splt = bfd_get_section_by_name (dynobj, ".plt");
747 sgot = bfd_get_section_by_name (dynobj, ".got");
750 for (rel = relocs; rel < relend; rel ++)
752 reloc_howto_type * howto;
753 unsigned long r_symndx;
754 Elf_Internal_Sym * sym;
755 asection * sec;
756 struct elf_link_hash_entry * h;
757 bfd_vma relocation;
758 bfd_reloc_status_type r;
759 const char * name = NULL;
760 int r_type;
762 r_type = ELF32_R_TYPE (rel->r_info);
764 if ( r_type == R_CRIS_GNU_VTINHERIT
765 || r_type == R_CRIS_GNU_VTENTRY)
766 continue;
768 r_symndx = ELF32_R_SYM (rel->r_info);
770 if (info->relocateable)
772 /* This is a relocateable link. We don't have to change
773 anything, unless the reloc is against a section symbol,
774 in which case we have to adjust according to where the
775 section symbol winds up in the output section. */
776 if (r_symndx < symtab_hdr->sh_info)
778 sym = local_syms + r_symndx;
780 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
782 sec = local_sections [r_symndx];
783 rel->r_addend += sec->output_offset + sym->st_value;
787 continue;
790 /* This is a final link. */
791 howto = cris_elf_howto_table + r_type;
792 h = NULL;
793 sym = NULL;
794 sec = NULL;
796 if (r_symndx < symtab_hdr->sh_info)
798 sym = local_syms + r_symndx;
799 sec = local_sections [r_symndx];
800 relocation = (sec->output_section->vma
801 + sec->output_offset
802 + sym->st_value);
804 name = bfd_elf_string_from_elf_section
805 (input_bfd, symtab_hdr->sh_link, sym->st_name);
806 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
808 else
810 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
812 while (h->root.type == bfd_link_hash_indirect
813 || h->root.type == bfd_link_hash_warning)
814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
816 name = h->root.root.string;
818 if (h->root.type == bfd_link_hash_defined
819 || h->root.type == bfd_link_hash_defweak)
821 sec = h->root.u.def.section;
823 /* Perhaps we should detect the cases that
824 sec->output_section is expected to be NULL like i386 and
825 m68k, but apparently (and according to elfxx-ia64.c) all
826 valid cases are where the symbol is defined in a shared
827 object which we link dynamically against. This includes
828 PLT relocs for which we've created a PLT entry and other
829 relocs for which we're prepared to create dynamic
830 relocations.
832 For now, new situations cause us to just err when
833 sec->output_offset is NULL but the object with the symbol
834 is *not* dynamically linked against. Thus this will
835 automatically remind us so we can see if there are other
836 valid cases we need to revisit. */
837 if ((sec->output_section == NULL
838 && (sec->owner->flags & DYNAMIC) != 0)
840 /* Here follow the cases where the relocation value must
841 be zero (or when further handling is simplified when
842 zero). I can't claim to understand the various
843 conditions and they weren't described in the files
844 where I copied them from (elf32-m68k.c and
845 elf32-i386.c), but let's mention examples of where
846 they happen. FIXME: Perhaps define and use a
847 dynamic_symbol_p function like ia64.
849 - When creating a shared library, we can have an
850 ordinary relocation for a symbol defined in a shared
851 library (perhaps the one we create). We then make
852 the relocation value zero, as the value seen now will
853 be added into the relocation addend in this shared
854 library, but must be handled only at dynamic-link
855 time. FIXME: Not sure this example covers the
856 h->elf_link_hash_flags test, though it's there in
857 other targets. */
858 || (info->shared
859 && ((! info->symbolic && h->dynindx != -1)
860 || (h->elf_link_hash_flags
861 & ELF_LINK_HASH_DEF_REGULAR) == 0)
862 && (input_section->flags & SEC_ALLOC) != 0
863 && (r_type == R_CRIS_8
864 || r_type == R_CRIS_16
865 || r_type == R_CRIS_32
866 || r_type == R_CRIS_8_PCREL
867 || r_type == R_CRIS_16_PCREL
868 || r_type == R_CRIS_32_PCREL)))
869 relocation = 0;
870 else if (sec->output_section != NULL)
871 relocation = (h->root.u.def.value
872 + sec->output_section->vma
873 + sec->output_offset);
874 else
876 if (input_bfd->my_archive)
877 (*_bfd_error_handler)
878 (_("%s(%s): unresolvable relocation %s against symbol `%s' from %s section"),
879 bfd_get_filename (bfd_my_archive (input_bfd)),
880 bfd_get_filename (input_bfd),
881 cris_elf_howto_table[r_type].name,
882 name,
883 bfd_get_section_name (input_bfd, input_section));
884 else
885 (*_bfd_error_handler)
886 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
887 bfd_get_filename (input_bfd),
888 cris_elf_howto_table[r_type].name,
889 name,
890 bfd_get_section_name (input_bfd, input_section));
891 bfd_set_error (bfd_error_bad_value);
892 return false;
895 else if (h->root.type == bfd_link_hash_undefweak)
896 relocation = 0;
897 else if (info->shared
898 && !info->symbolic
899 && !info->no_undefined
900 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
901 relocation = 0;
902 else
904 if (!(info->callbacks->undefined_symbol
905 (info, name, input_bfd,
906 input_section, rel->r_offset,
907 (!info->shared || info->no_undefined
908 || ELF_ST_VISIBILITY (h->other)))))
909 return false;
910 relocation = 0;
914 switch (r_type)
916 case R_CRIS_16_GOTPLT:
917 case R_CRIS_32_GOTPLT:
918 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
919 but we require a PLT, and the PLT handling will take care of
920 filling in the PLT-specific GOT entry. For the GOT offset,
921 calculate it as we do when filling it in for the .got.plt
922 section. If we don't have a PLT, punt to GOT handling. */
923 if (h != NULL
924 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
926 asection *sgotplt
927 = bfd_get_section_by_name (dynobj, ".got.plt");
928 bfd_vma got_offset;
930 BFD_ASSERT (h->dynindx != -1);
931 BFD_ASSERT (sgotplt != NULL);
933 got_offset
934 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
936 relocation = got_offset;
937 break;
940 /* We didn't make a PLT entry for this symbol. Maybe everything is
941 folded into the GOT. Other than folding, this happens when
942 statically linking PIC code, or when using -Bsymbolic. Check
943 that we instead have a GOT entry as done for us by
944 elf_cris_adjust_dynamic_symbol, and drop through into the
945 ordinary GOT cases. */
946 if (h != NULL && h->got.offset == (bfd_vma) -1)
948 (*_bfd_error_handler)
949 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
950 bfd_get_filename (input_bfd),
951 cris_elf_howto_table[r_type].name,
952 name[0] != '\0' ? name : _("[whose name is lost]"),
953 bfd_get_section_name (input_bfd, input_section));
955 /* FIXME: Perhaps blaming input is not the right thing to
956 do; this is probably an internal error. But it is true
957 that we didn't like that particular input. */
958 bfd_set_error (bfd_error_bad_value);
959 return false;
961 /* Fall through. */
963 /* The size of the actual relocation is not used here; we only
964 fill in the GOT table here. */
965 case R_CRIS_16_GOT:
966 case R_CRIS_32_GOT:
968 bfd_vma off;
970 /* Note that despite using RELA relocations, the .got contents
971 is always filled in with the link-relative relocation
972 value; the addend. */
974 if (h != NULL)
976 off = h->got.offset;
977 BFD_ASSERT (off != (bfd_vma) -1);
979 if (!elf_hash_table (info)->dynamic_sections_created
980 || (! info->shared && h->dynindx == -1)
981 || (info->shared
982 && (info->symbolic || h->dynindx == -1)
983 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
985 /* This wasn't checked above for ! info->shared, but
986 must hold there if we get here; the symbol must not
987 be used in, or defined by a DSO. (Note that
988 checking for ELF_LINK_HASH_DEF_REGULAR doesn't
989 catch all cases.) */
990 BFD_ASSERT (info->shared
991 || (h->elf_link_hash_flags
992 & (ELF_LINK_HASH_REF_DYNAMIC
993 | ELF_LINK_HASH_DEF_DYNAMIC)) == 0);
995 /* This is actually a static link, or it is a
996 -Bsymbolic link and the symbol is defined
997 locally, or the symbol was forced to be local
998 because of a version file, or we're not creating a
999 dynamic object and the symbol isn't referred to by
1000 a dynamic object. We must initialize
1001 this entry in the global offset table. Since
1002 the offset must always be a multiple of 4, we
1003 use the least significant bit to record whether
1004 we have initialized it already.
1006 When doing a dynamic link, we create a .rela.got
1007 relocation entry to initialize the value. This
1008 is done in the finish_dynamic_symbol routine. */
1009 if ((off & 1) != 0)
1010 off &= ~1;
1011 else
1013 bfd_put_32 (output_bfd, relocation,
1014 sgot->contents + off);
1015 h->got.offset |= 1;
1019 else
1021 BFD_ASSERT (local_got_offsets != NULL
1022 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1024 off = local_got_offsets[r_symndx];
1026 /* The offset must always be a multiple of 4. We use
1027 the least significant bit to record whether we have
1028 already generated the necessary reloc. */
1029 if ((off & 1) != 0)
1030 off &= ~1;
1031 else
1033 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1035 if (info->shared)
1037 asection *srelgot;
1038 Elf_Internal_Rela outrel;
1040 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1041 BFD_ASSERT (srelgot != NULL);
1043 outrel.r_offset = (sgot->output_section->vma
1044 + sgot->output_offset
1045 + off);
1046 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1047 outrel.r_addend = relocation;
1048 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1049 (((Elf32_External_Rela *)
1050 srelgot->contents)
1051 + srelgot->reloc_count));
1052 ++srelgot->reloc_count;
1055 local_got_offsets[r_symndx] |= 1;
1059 relocation = sgot->output_offset + off;
1060 if (rel->r_addend != 0)
1062 /* We can't do anything for a relocation which is against
1063 a symbol *plus offset*. GOT holds relocations for
1064 symbols. Make this an error; the compiler isn't
1065 allowed to pass us these kinds of things. */
1066 if (h == NULL)
1067 (*_bfd_error_handler)
1068 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1069 bfd_get_filename (input_bfd),
1070 cris_elf_howto_table[r_type].name,
1071 rel->r_addend,
1072 bfd_get_section_name (input_bfd, input_section));
1073 else
1074 (*_bfd_error_handler)
1075 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1076 bfd_get_filename (input_bfd),
1077 cris_elf_howto_table[r_type].name,
1078 rel->r_addend,
1079 name[0] != '\0' ? name : _("[whose name is lost]"),
1080 bfd_get_section_name (input_bfd, input_section));
1082 bfd_set_error (bfd_error_bad_value);
1083 return false;
1086 break;
1088 case R_CRIS_32_GOTREL:
1089 /* This relocation must only be performed against local symbols. */
1090 if (h != NULL)
1092 (*_bfd_error_handler)
1093 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1094 bfd_get_filename (input_bfd),
1095 cris_elf_howto_table[r_type].name,
1096 name,
1097 bfd_get_section_name (input_bfd, input_section));
1098 bfd_set_error (bfd_error_bad_value);
1099 return false;
1102 /* This relocation is like a PC-relative one, except the
1103 reference point is the location of GOT. Note that
1104 sgot->output_offset is not involved in this calculation. We
1105 always want the start of entire .got section, not the
1106 position after the reserved header. */
1107 relocation -= sgot->output_section->vma;
1108 break;
1110 case R_CRIS_32_PLT_PCREL:
1111 /* Relocation is to the entry for this symbol in the
1112 procedure linkage table. */
1114 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1115 without using the procedure linkage table. */
1116 if (h == NULL)
1117 break;
1119 if (h->plt.offset == (bfd_vma) -1
1120 || splt == NULL)
1122 /* We didn't make a PLT entry for this symbol. This
1123 happens when statically linking PIC code, or when
1124 using -Bsymbolic. */
1125 break;
1128 relocation = (splt->output_section->vma
1129 + splt->output_offset
1130 + h->plt.offset);
1131 break;
1133 case R_CRIS_32_PLT_GOTREL:
1134 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1135 start of the .got section. See also comment at
1136 R_CRIS_32_GOT. */
1137 relocation -= sgot->output_section->vma;
1139 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1140 without using the procedure linkage table. */
1141 if (h == NULL)
1142 break;
1144 if (h->plt.offset == (bfd_vma) -1
1145 || splt == NULL)
1147 /* We didn't make a PLT entry for this symbol. This
1148 happens when statically linking PIC code, or when
1149 using -Bsymbolic. */
1150 break;
1153 relocation = (splt->output_section->vma
1154 + splt->output_offset
1155 + h->plt.offset
1156 - sgot->output_section->vma);
1157 break;
1159 case R_CRIS_8_PCREL:
1160 case R_CRIS_16_PCREL:
1161 case R_CRIS_32_PCREL:
1162 /* If the symbol was local, we need no shlib-specific handling. */
1163 if (h == NULL)
1164 break;
1166 /* Fall through. */
1167 case R_CRIS_8:
1168 case R_CRIS_16:
1169 case R_CRIS_32:
1170 if (info->shared
1171 && (input_section->flags & SEC_ALLOC) != 0
1172 && ((r_type != R_CRIS_8_PCREL
1173 && r_type != R_CRIS_16_PCREL
1174 && r_type != R_CRIS_32_PCREL)
1175 || (!info->symbolic
1176 || (h->elf_link_hash_flags
1177 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1179 Elf_Internal_Rela outrel;
1180 boolean skip, relocate;
1182 /* When generating a shared object, these relocations
1183 are copied into the output file to be resolved at run
1184 time. */
1186 if (sreloc == NULL)
1188 const char *name;
1190 name = (bfd_elf_string_from_elf_section
1191 (input_bfd,
1192 elf_elfheader (input_bfd)->e_shstrndx,
1193 elf_section_data (input_section)->rel_hdr.sh_name));
1194 if (name == NULL)
1195 return false;
1197 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1198 && strcmp (bfd_get_section_name (input_bfd,
1199 input_section),
1200 name + 5) == 0);
1202 sreloc = bfd_get_section_by_name (dynobj, name);
1204 /* That section should have been created in
1205 cris_elf_check_relocs, but that function will not be
1206 called for objects which fail in
1207 cris_elf_merge_private_bfd_data. */
1208 if (sreloc == NULL)
1210 (*_bfd_error_handler)
1211 (_("%s: Internal inconsistency; no relocation section %s"),
1212 bfd_get_filename (input_bfd),
1213 name);
1215 bfd_set_error (bfd_error_bad_value);
1216 return false;
1220 skip = false;
1222 if (elf_section_data (input_section)->stab_info == NULL)
1223 outrel.r_offset = rel->r_offset;
1224 else
1226 bfd_vma off;
1228 off = (_bfd_stab_section_offset
1229 (output_bfd, &elf_hash_table (info)->stab_info,
1230 input_section,
1231 &elf_section_data (input_section)->stab_info,
1232 rel->r_offset));
1233 if (off == (bfd_vma) -1)
1234 skip = true;
1235 outrel.r_offset = off;
1238 outrel.r_offset += (input_section->output_section->vma
1239 + input_section->output_offset);
1241 if (skip)
1243 memset (&outrel, 0, sizeof outrel);
1244 relocate = false;
1246 /* h->dynindx may be -1 if the symbol was marked to
1247 become local. */
1248 else if (h != NULL
1249 && ((! info->symbolic && h->dynindx != -1)
1250 || (h->elf_link_hash_flags
1251 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1253 BFD_ASSERT (h->dynindx != -1);
1254 relocate = false;
1255 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1256 outrel.r_addend = relocation + rel->r_addend;
1258 else
1260 if (r_type == R_CRIS_32)
1262 relocate = true;
1263 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1264 outrel.r_addend = relocation + rel->r_addend;
1266 else
1268 long indx;
1270 if (h == NULL)
1271 sec = local_sections[r_symndx];
1272 else
1274 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1275 || (h->root.type
1276 == bfd_link_hash_defweak));
1277 sec = h->root.u.def.section;
1279 if (sec != NULL && bfd_is_abs_section (sec))
1280 indx = 0;
1281 else if (sec == NULL || sec->owner == NULL)
1283 bfd_set_error (bfd_error_bad_value);
1284 return false;
1286 else
1288 asection *osec;
1290 osec = sec->output_section;
1291 indx = elf_section_data (osec)->dynindx;
1292 BFD_ASSERT (indx > 0);
1295 relocate = false;
1296 outrel.r_info = ELF32_R_INFO (indx, r_type);
1297 outrel.r_addend = relocation + rel->r_addend;
1301 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1302 (((Elf32_External_Rela *)
1303 sreloc->contents)
1304 + sreloc->reloc_count));
1305 ++sreloc->reloc_count;
1307 /* This reloc will be computed at runtime, so there's no
1308 need to do anything now, except for R_CRIS_32 relocations
1309 that have been turned into R_CRIS_RELATIVE. */
1310 if (!relocate)
1311 continue;
1314 break;
1317 r = cris_final_link_relocate (howto, input_bfd, input_section,
1318 contents, rel, relocation);
1320 if (r != bfd_reloc_ok)
1322 const char * msg = (const char *) NULL;
1324 switch (r)
1326 case bfd_reloc_overflow:
1327 r = info->callbacks->reloc_overflow
1328 (info, name, howto->name, (bfd_vma) 0,
1329 input_bfd, input_section, rel->r_offset);
1330 break;
1332 case bfd_reloc_undefined:
1333 r = info->callbacks->undefined_symbol
1334 (info, name, input_bfd, input_section, rel->r_offset,
1335 true);
1336 break;
1338 case bfd_reloc_outofrange:
1339 msg = _("internal error: out of range error");
1340 break;
1342 case bfd_reloc_notsupported:
1343 msg = _("internal error: unsupported relocation error");
1344 break;
1346 case bfd_reloc_dangerous:
1347 msg = _("internal error: dangerous relocation");
1348 break;
1350 default:
1351 msg = _("internal error: unknown error");
1352 break;
1355 if (msg)
1356 r = info->callbacks->warning
1357 (info, msg, name, input_bfd, input_section, rel->r_offset);
1359 if (! r)
1360 return false;
1364 return true;
1367 /* Finish up dynamic symbol handling. We set the contents of various
1368 dynamic sections here. */
1370 static boolean
1371 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1372 bfd *output_bfd;
1373 struct bfd_link_info *info;
1374 struct elf_link_hash_entry *h;
1375 Elf_Internal_Sym *sym;
1377 bfd *dynobj;
1378 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1380 dynobj = elf_hash_table (info)->dynobj;
1382 if (h->plt.offset != (bfd_vma) -1)
1384 asection *splt;
1385 asection *sgotplt;
1386 asection *sgot;
1387 asection *srela;
1388 bfd_vma got_base;
1390 bfd_vma gotplt_offset
1391 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1392 Elf_Internal_Rela rela;
1393 boolean has_gotplt = gotplt_offset != 0;
1395 /* Get the index in the procedure linkage table which
1396 corresponds to this symbol. This is the index of this symbol
1397 in all the symbols for which we are making plt entries. The
1398 first entry in the procedure linkage table is reserved. */
1399 /* We have to count backwards here, and the result is only valid as
1400 an index into .got.plt and its relocations. FIXME: Constants... */
1401 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1403 /* Get the offset into the .got table of the entry that corresponds
1404 to this function. Note that we embed knowledge that "incoming"
1405 .got goes after .got.plt in the output without padding (pointer
1406 aligned). However, that knowledge is present in several other
1407 places too, here and in elflink.h at least. */
1408 bfd_vma got_offset
1409 = (has_gotplt
1410 ? gotplt_offset
1411 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1413 /* This symbol has an entry in the procedure linkage table. Set it
1414 up. */
1416 BFD_ASSERT (h->dynindx != -1);
1418 splt = bfd_get_section_by_name (dynobj, ".plt");
1419 sgot = bfd_get_section_by_name (dynobj, ".got");
1420 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1421 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1422 BFD_ASSERT (splt != NULL && sgotplt != NULL
1423 && (! has_gotplt || srela != NULL));
1425 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1427 /* Fill in the entry in the procedure linkage table. */
1428 if (! info->shared)
1430 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1431 PLT_ENTRY_SIZE);
1433 /* We need to enter the absolute address of the GOT entry here. */
1434 bfd_put_32 (output_bfd, got_base + got_offset,
1435 splt->contents + h->plt.offset + plt_off1);
1437 else
1439 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1440 PLT_ENTRY_SIZE);
1441 bfd_put_32 (output_bfd, got_offset,
1442 splt->contents + h->plt.offset + plt_off1);
1445 /* Fill in the plt entry and make a relocation, if this is a "real"
1446 PLT entry. */
1447 if (has_gotplt)
1449 /* Fill in the offset into the reloc table. */
1450 bfd_put_32 (output_bfd,
1451 gotplt_index * sizeof (Elf32_External_Rela),
1452 splt->contents + h->plt.offset + plt_off2);
1454 /* Fill in the offset to the first PLT entry, where to "jump". */
1455 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1456 splt->contents + h->plt.offset + plt_off3);
1458 /* Fill in the entry in the global offset table with the address of
1459 the relocating stub. */
1460 bfd_put_32 (output_bfd,
1461 (splt->output_section->vma
1462 + splt->output_offset
1463 + h->plt.offset
1464 + 8),
1465 sgotplt->contents + got_offset);
1467 /* Fill in the entry in the .rela.plt section. */
1468 rela.r_offset = (sgotplt->output_section->vma
1469 + sgotplt->output_offset
1470 + got_offset);
1471 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1472 rela.r_addend = 0;
1473 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1474 ((Elf32_External_Rela *) srela->contents
1475 + gotplt_index));
1478 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1480 /* Mark the symbol as undefined, rather than as defined in
1481 the .plt section. Leave the value alone. */
1482 sym->st_shndx = SHN_UNDEF;
1484 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1485 know whether resetting the value is significant; if it really
1486 is, rather than a quirk or bug in the sparc port, then I
1487 believe we'd see this elsewhere. */
1488 /* If the symbol is weak, we do need to clear the value.
1489 Otherwise, the PLT entry would provide a definition for
1490 the symbol even if the symbol wasn't defined anywhere,
1491 and so the symbol would never be NULL. */
1492 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1493 == 0)
1494 sym->st_value = 0;
1498 /* We don't emit .got relocs for symbols that aren't in the
1499 dynamic-symbols table for an ordinary program. */
1500 if (h->got.offset != (bfd_vma) -1
1501 && (info->shared || h->dynindx != -1))
1503 asection *sgot;
1504 asection *srela;
1505 Elf_Internal_Rela rela;
1507 /* This symbol has an entry in the global offset table. Set it up. */
1509 sgot = bfd_get_section_by_name (dynobj, ".got");
1510 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1511 BFD_ASSERT (sgot != NULL && srela != NULL);
1513 rela.r_offset = (sgot->output_section->vma
1514 + sgot->output_offset
1515 + (h->got.offset &~ 1));
1517 /* If this is a static link, or it is a -Bsymbolic link and the
1518 symbol is defined locally or was forced to be local because
1519 of a version file, we just want to emit a RELATIVE reloc.
1520 The entry in the global offset table will already have been
1521 initialized in the relocate_section function. */
1522 if (! elf_hash_table (info)->dynamic_sections_created
1523 || (info->shared
1524 && (info->symbolic || h->dynindx == -1)
1525 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1527 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1528 rela.r_addend = bfd_get_signed_32 (output_bfd,
1529 (sgot->contents
1530 + (h->got.offset & ~1)));
1532 else
1534 bfd_put_32 (output_bfd, (bfd_vma) 0,
1535 sgot->contents + (h->got.offset & ~1));
1536 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1537 rela.r_addend = 0;
1540 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1541 ((Elf32_External_Rela *) srela->contents
1542 + srela->reloc_count));
1543 ++srela->reloc_count;
1546 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1548 asection *s;
1549 Elf_Internal_Rela rela;
1551 /* This symbol needs a copy reloc. Set it up. */
1553 BFD_ASSERT (h->dynindx != -1
1554 && (h->root.type == bfd_link_hash_defined
1555 || h->root.type == bfd_link_hash_defweak));
1557 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1558 ".rela.bss");
1559 BFD_ASSERT (s != NULL);
1561 rela.r_offset = (h->root.u.def.value
1562 + h->root.u.def.section->output_section->vma
1563 + h->root.u.def.section->output_offset);
1564 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1565 rela.r_addend = 0;
1566 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1567 ((Elf32_External_Rela *) s->contents
1568 + s->reloc_count));
1569 ++s->reloc_count;
1572 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1573 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1574 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1575 sym->st_shndx = SHN_ABS;
1577 return true;
1580 /* Finish up the dynamic sections. */
1582 static boolean
1583 elf_cris_finish_dynamic_sections (output_bfd, info)
1584 bfd *output_bfd;
1585 struct bfd_link_info *info;
1587 bfd *dynobj;
1588 asection *sgot;
1589 asection *sdyn;
1591 dynobj = elf_hash_table (info)->dynobj;
1593 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1594 BFD_ASSERT (sgot != NULL);
1595 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1597 if (elf_hash_table (info)->dynamic_sections_created)
1599 asection *splt;
1600 Elf32_External_Dyn *dyncon, *dynconend;
1602 splt = bfd_get_section_by_name (dynobj, ".plt");
1603 BFD_ASSERT (splt != NULL && sdyn != NULL);
1605 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1606 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1607 for (; dyncon < dynconend; dyncon++)
1609 Elf_Internal_Dyn dyn;
1610 asection *s;
1612 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1614 switch (dyn.d_tag)
1616 default:
1617 break;
1619 case DT_PLTGOT:
1620 s = bfd_get_section_by_name (output_bfd, ".got");
1621 BFD_ASSERT (s != NULL);
1622 dyn.d_un.d_ptr = s->vma;
1623 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1624 break;
1626 case DT_JMPREL:
1627 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1628 if all symbols are found in the .got (not .got.plt). */
1629 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1630 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1631 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1632 break;
1634 case DT_PLTRELSZ:
1635 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1636 if (s == NULL)
1637 dyn.d_un.d_val = 0;
1638 else if (s->_cooked_size != 0)
1639 dyn.d_un.d_val = s->_cooked_size;
1640 else
1641 dyn.d_un.d_val = s->_raw_size;
1642 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1643 break;
1645 case DT_RELASZ:
1646 /* The procedure linkage table relocs (DT_JMPREL) should
1647 not be included in the overall relocs (DT_RELA).
1648 Therefore, we override the DT_RELASZ entry here to
1649 make it not include the JMPREL relocs. Since the
1650 linker script arranges for .rela.plt to follow all
1651 other relocation sections, we don't have to worry
1652 about changing the DT_RELA entry. */
1653 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1654 if (s != NULL)
1656 if (s->_cooked_size != 0)
1657 dyn.d_un.d_val -= s->_cooked_size;
1658 else
1659 dyn.d_un.d_val -= s->_raw_size;
1661 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1662 break;
1666 /* Fill in the first entry in the procedure linkage table. */
1667 if (splt->_raw_size > 0)
1669 if (info->shared)
1670 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1671 else
1673 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1674 bfd_put_32 (output_bfd,
1675 sgot->output_section->vma + sgot->output_offset + 4,
1676 splt->contents + 6);
1677 bfd_put_32 (output_bfd,
1678 sgot->output_section->vma + sgot->output_offset + 8,
1679 splt->contents + 14);
1681 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1682 = PLT_ENTRY_SIZE;
1687 /* Fill in the first three entries in the global offset table. */
1688 if (sgot->_raw_size > 0)
1690 if (sdyn == NULL)
1691 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1692 else
1693 bfd_put_32 (output_bfd,
1694 sdyn->output_section->vma + sdyn->output_offset,
1695 sgot->contents);
1696 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1697 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1700 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1702 return true;
1705 /* Return the section that should be marked against GC for a given
1706 relocation. */
1708 static asection *
1709 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1710 bfd * abfd;
1711 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1712 Elf_Internal_Rela * rel;
1713 struct elf_link_hash_entry * h;
1714 Elf_Internal_Sym * sym;
1716 if (h != NULL)
1718 switch (ELF32_R_TYPE (rel->r_info))
1720 case R_CRIS_GNU_VTINHERIT:
1721 case R_CRIS_GNU_VTENTRY:
1722 break;
1724 default:
1725 switch (h->root.type)
1727 case bfd_link_hash_defined:
1728 case bfd_link_hash_defweak:
1729 return h->root.u.def.section;
1731 case bfd_link_hash_common:
1732 return h->root.u.c.p->section;
1734 default:
1735 break;
1739 else
1741 if (!(elf_bad_symtab (abfd)
1742 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1743 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1744 && sym->st_shndx != SHN_COMMON))
1746 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1750 return NULL;
1753 /* Update the got entry reference counts for the section being removed. */
1755 static boolean
1756 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1757 bfd * abfd ATTRIBUTE_UNUSED;
1758 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1759 asection * sec ATTRIBUTE_UNUSED;
1760 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1762 Elf_Internal_Shdr *symtab_hdr;
1763 struct elf_link_hash_entry **sym_hashes;
1764 bfd_signed_vma *local_got_refcounts;
1765 const Elf_Internal_Rela *rel, *relend;
1766 unsigned long r_symndx;
1767 struct elf_link_hash_entry *h;
1768 bfd *dynobj;
1769 asection *sgot;
1770 asection *srelgot;
1772 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1773 sym_hashes = elf_sym_hashes (abfd);
1774 local_got_refcounts = elf_local_got_refcounts (abfd);
1776 dynobj = elf_hash_table (info)->dynobj;
1777 if (dynobj == NULL)
1778 return true;
1780 sgot = bfd_get_section_by_name (dynobj, ".got");
1781 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1783 relend = relocs + sec->reloc_count;
1784 for (rel = relocs; rel < relend; rel++)
1786 switch (ELF32_R_TYPE (rel->r_info))
1788 case R_CRIS_16_GOT:
1789 case R_CRIS_32_GOT:
1790 r_symndx = ELF32_R_SYM (rel->r_info);
1791 if (r_symndx >= symtab_hdr->sh_info)
1793 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1794 if (h->got.refcount > 0)
1796 --h->got.refcount;
1797 if (h->got.refcount == 0)
1799 /* We don't need the .got entry any more. */
1800 sgot->_raw_size -= 4;
1801 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1804 break;
1807 local_got_reloc:
1808 if (local_got_refcounts != NULL)
1810 if (local_got_refcounts[r_symndx] > 0)
1812 --local_got_refcounts[r_symndx];
1813 if (local_got_refcounts[r_symndx] == 0)
1815 /* We don't need the .got entry any more. */
1816 sgot->_raw_size -= 4;
1817 if (info->shared)
1818 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1822 break;
1824 case R_CRIS_16_GOTPLT:
1825 case R_CRIS_32_GOTPLT:
1826 /* For local symbols, treat these like GOT relocs. */
1827 r_symndx = ELF32_R_SYM (rel->r_info);
1828 if (r_symndx < symtab_hdr->sh_info)
1829 goto local_got_reloc;
1831 case R_CRIS_32_PLT_GOTREL:
1832 /* FIXME: We don't garbage-collect away the .got section. */
1833 if (local_got_refcounts != NULL)
1834 local_got_refcounts[-1]--;
1835 /* Fall through. */
1837 case R_CRIS_8_PCREL:
1838 case R_CRIS_16_PCREL:
1839 case R_CRIS_32_PCREL:
1840 case R_CRIS_32_PLT_PCREL:
1841 r_symndx = ELF32_R_SYM (rel->r_info);
1842 if (r_symndx >= symtab_hdr->sh_info)
1844 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1845 if (h->plt.refcount > 0)
1846 --h->plt.refcount;
1848 break;
1850 default:
1851 break;
1855 return true;
1858 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1859 entry but we found we will not create any. Called when we find we will
1860 not have any PLT for this symbol, by for example
1861 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1862 or elf_cris_size_dynamic_sections if no dynamic sections will be
1863 created (we're only linking static objects). */
1865 static boolean
1866 elf_cris_adjust_gotplt_to_got (h, p)
1867 struct elf_cris_link_hash_entry *h;
1868 PTR p;
1870 struct bfd_link_info *info = (struct bfd_link_info *) p;
1871 bfd *dynobj = elf_hash_table (info)->dynobj;
1873 BFD_ASSERT (dynobj != NULL);
1875 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1876 if (h->gotplt_refcount <= 0)
1877 return true;
1879 if (h->root.got.refcount > 0)
1881 /* There's a GOT entry for this symbol. Just adjust the refcount.
1882 Probably not necessary at this stage, but keeping it accurate
1883 helps avoiding surprises later. */
1884 h->root.got.refcount += h->gotplt_refcount;
1885 h->gotplt_refcount = -1;
1887 else
1889 /* No GOT entry for this symbol. We need to create one. */
1890 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1891 asection *srelgot
1892 = bfd_get_section_by_name (dynobj, ".rela.got");
1894 /* Put an accurate refcount there. */
1895 h->root.got.refcount = h->gotplt_refcount;
1897 h->gotplt_refcount = -1;
1899 /* We always have a .got section when there are dynamic
1900 relocs. */
1901 BFD_ASSERT (sgot != NULL /* Surely have .got section. */);
1903 /* We might have had a PLT but with no GOT entry and
1904 further no GOT reloc section at all needed before.
1905 Add it. */
1906 if (srelgot == NULL)
1908 srelgot = bfd_make_section (dynobj, ".rela.got");
1910 if (srelgot == NULL
1911 || !bfd_set_section_flags (dynobj, srelgot,
1912 (SEC_ALLOC
1913 | SEC_LOAD
1914 | SEC_HAS_CONTENTS
1915 | SEC_IN_MEMORY
1916 | SEC_LINKER_CREATED
1917 | SEC_READONLY))
1918 || !bfd_set_section_alignment (dynobj, srelgot, 2))
1919 return false;
1922 /* Allocate space in the .got section. */
1923 sgot->_raw_size += 4;
1925 /* Allocate relocation space. */
1926 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1929 return true;
1932 /* Try to fold PLT entries with GOT entries. There are two cases when we
1933 want to do this:
1935 - When all PLT references are GOTPLT references, and there are GOT
1936 references. We don't have to generate a PLT at all.
1938 - When there are both (ordinary) PLT references and GOT references.
1939 We want to make the PLT reference use the ordinary GOT entry rather
1940 than a run-time dynamically resolved GOTPLT entry (since the GOT
1941 entry will have to be resolved at startup anyway).
1943 Though the latter case is handled when room for the PLT is allocated,
1944 not here.
1946 Note that this function is called before symbols are forced local by
1947 version scripts. The differing cases are handled by
1948 elf_cris_hide_symbol. */
1950 static boolean
1951 elf_cris_try_fold_plt_to_got (h, p)
1952 struct elf_cris_link_hash_entry *h;
1953 PTR p;
1955 struct bfd_link_info *info = (struct bfd_link_info *) p;
1957 /* If there are no GOT references for this symbol, we can't fold any
1958 other reference so there's nothing to do. Likewise if there are no
1959 PLT references; GOTPLT references included. */
1960 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1961 return true;
1963 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
1964 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1966 if (h->gotplt_refcount == h->root.plt.refcount)
1968 /* The only PLT references are GOTPLT references, and there are GOT
1969 references. Convert PLT to GOT references. */
1970 if (! elf_cris_adjust_gotplt_to_got (h, info))
1971 return false;
1973 /* Clear the PLT references, so no PLT will be created. */
1974 h->root.plt.offset = (bfd_vma) -1;
1977 return true;
1980 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1981 to use a GOT entry (and create one) rather than requiring a GOTPLT
1982 entry. */
1984 static void
1985 elf_cris_hide_symbol (info, h)
1986 struct bfd_link_info *info;
1987 struct elf_link_hash_entry *h;
1989 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1991 _bfd_elf_link_hash_hide_symbol (info, h);
1994 /* Adjust a symbol defined by a dynamic object and referenced by a
1995 regular object. The current definition is in some section of the
1996 dynamic object, but we're not including those sections. We have to
1997 change the definition to something the rest of the link can
1998 understand. */
2000 static boolean
2001 elf_cris_adjust_dynamic_symbol (info, h)
2002 struct bfd_link_info *info;
2003 struct elf_link_hash_entry *h;
2005 bfd *dynobj;
2006 asection *s;
2007 unsigned int power_of_two;
2009 dynobj = elf_hash_table (info)->dynobj;
2011 /* Make sure we know what is going on here. */
2012 BFD_ASSERT (dynobj != NULL
2013 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2014 || h->weakdef != NULL
2015 || ((h->elf_link_hash_flags
2016 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2017 && (h->elf_link_hash_flags
2018 & ELF_LINK_HASH_REF_REGULAR) != 0
2019 && (h->elf_link_hash_flags
2020 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2022 /* If this is a function, put it in the procedure linkage table. We
2023 will fill in the contents of the procedure linkage table later,
2024 when we know the address of the .got section. */
2025 if (h->type == STT_FUNC
2026 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2028 if (! info->shared
2029 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2030 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2031 /* We must always create the plt entry if it was referenced by a
2032 PLT relocation. In this case we already recorded it as a
2033 dynamic symbol. */
2034 /* FIXME: m68k and i386 differ here, for unclear reasons. */
2035 && h->dynindx == -1)
2037 /* This case can occur if we saw a PLT reloc in an input file,
2038 but the symbol was never referred to by a dynamic object. In
2039 such a case, we don't actually need to build a procedure
2040 linkage table, and we can just do a PC reloc instead, or
2041 change a .got.plt index to a .got index for GOTPLT relocs. */
2042 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2043 h->plt.offset = (bfd_vma) -1;
2045 return
2046 elf_cris_adjust_gotplt_to_got ((struct
2047 elf_cris_link_hash_entry *) h,
2048 info);
2051 /* If there are only GOT references and GOTPLT references to this
2052 PLT entry, get rid of the PLT. */
2053 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2054 h, info))
2055 return false;
2057 /* GC or folding may have rendered this entry unused. */
2058 if (h->plt.refcount <= 0)
2060 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2061 h->plt.offset = (bfd_vma) -1;
2062 return true;
2065 /* Make sure this symbol is output as a dynamic symbol. */
2066 if (h->dynindx == -1)
2068 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2069 return false;
2072 s = bfd_get_section_by_name (dynobj, ".plt");
2073 BFD_ASSERT (s != NULL);
2075 /* If this is the first .plt entry, make room for the special
2076 first entry. */
2077 if (s->_raw_size == 0)
2078 s->_raw_size += PLT_ENTRY_SIZE;
2080 /* If this symbol is not defined in a regular file, and we are
2081 not generating a shared library, then set the symbol to this
2082 location in the .plt. This is required to make function
2083 pointers compare as equal between the normal executable and
2084 the shared library. */
2085 if (!info->shared
2086 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2088 h->root.u.def.section = s;
2089 h->root.u.def.value = s->_raw_size;
2092 /* If there's already a GOT entry, use that, not a .got.plt. A
2093 GOT field still has a reference count when we get here; it's
2094 not yet changed to an offset. */
2095 if (h->got.refcount > 0)
2097 h->got.refcount += h->plt.refcount;
2099 /* Mark the PLT offset to use the GOT entry by setting the low
2100 bit in the plt offset; it is always a multiple of
2101 pointer-size. */
2102 BFD_ASSERT ((s->_raw_size & 3) == 0);
2104 /* Change the PLT refcount to an offset. */
2105 h->plt.offset = s->_raw_size;
2107 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2108 that the got entry should be used instead. */
2109 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2110 h)->gotplt_offset == 0);
2112 /* Make room for this entry. */
2113 s->_raw_size += PLT_ENTRY_SIZE;
2115 return true;
2118 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2119 h->plt.offset = s->_raw_size;
2121 /* Make room for this entry. */
2122 s->_raw_size += PLT_ENTRY_SIZE;
2124 /* We also need to make an entry in the .got.plt section, which
2125 will be placed in the .got section by the linker script. */
2126 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2127 = elf_cris_hash_table (info)->next_gotplt_entry;
2128 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2130 s = bfd_get_section_by_name (dynobj, ".got.plt");
2131 BFD_ASSERT (s != NULL);
2132 s->_raw_size += 4;
2134 /* We also need to make an entry in the .rela.plt section. */
2136 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2137 BFD_ASSERT (s != NULL);
2138 s->_raw_size += sizeof (Elf32_External_Rela);
2140 return true;
2143 /* Reinitialize the plt offset now that it is not used as a reference
2144 count any more. */
2145 h->plt.offset = (bfd_vma) -1;
2147 /* If this is a weak symbol, and there is a real definition, the
2148 processor independent code will have arranged for us to see the
2149 real definition first, and we can just use the same value. */
2150 if (h->weakdef != NULL)
2152 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2153 || h->weakdef->root.type == bfd_link_hash_defweak);
2154 h->root.u.def.section = h->weakdef->root.u.def.section;
2155 h->root.u.def.value = h->weakdef->root.u.def.value;
2156 return true;
2159 /* This is a reference to a symbol defined by a dynamic object which
2160 is not a function. */
2162 /* If we are creating a shared library, we must presume that the
2163 only references to the symbol are via the global offset table.
2164 For such cases we need not do anything here; the relocations will
2165 be handled correctly by relocate_section. */
2166 if (info->shared)
2167 return true;
2169 /* If there are no references to this symbol that do not use the
2170 GOT, we don't need to generate a copy reloc. */
2171 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2172 return true;
2174 /* We must allocate the symbol in our .dynbss section, which will
2175 become part of the .bss section of the executable. There will be
2176 an entry for this symbol in the .dynsym section. The dynamic
2177 object will contain position independent code, so all references
2178 from the dynamic object to this symbol will go through the global
2179 offset table. The dynamic linker will use the .dynsym entry to
2180 determine the address it must put in the global offset table, so
2181 both the dynamic object and the regular object will refer to the
2182 same memory location for the variable. */
2184 s = bfd_get_section_by_name (dynobj, ".dynbss");
2185 BFD_ASSERT (s != NULL);
2187 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2188 copy the initial value out of the dynamic object and into the
2189 runtime process image. We need to remember the offset into the
2190 .rela.bss section we are going to use. */
2191 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2193 asection *srel;
2195 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2196 BFD_ASSERT (srel != NULL);
2197 srel->_raw_size += sizeof (Elf32_External_Rela);
2198 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2201 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2202 thing to copy; so do we. */
2204 /* We need to figure out the alignment required for this symbol. I
2205 have no idea how ELF linkers handle this. */
2206 power_of_two = bfd_log2 (h->size);
2207 if (power_of_two > 3)
2208 power_of_two = 3;
2210 /* Apply the required alignment. */
2211 s->_raw_size = BFD_ALIGN (s->_raw_size,
2212 (bfd_size_type) (1 << power_of_two));
2213 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2215 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2216 return false;
2219 /* Define the symbol as being at this point in the section. */
2220 h->root.u.def.section = s;
2221 h->root.u.def.value = s->_raw_size;
2223 /* Increment the section size to make room for the symbol. */
2224 s->_raw_size += h->size;
2226 return true;
2229 /* Look through the relocs for a section during the first phase. */
2231 static boolean
2232 cris_elf_check_relocs (abfd, info, sec, relocs)
2233 bfd *abfd;
2234 struct bfd_link_info *info;
2235 asection *sec;
2236 const Elf_Internal_Rela *relocs;
2238 bfd *dynobj;
2239 Elf_Internal_Shdr *symtab_hdr;
2240 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2241 bfd_signed_vma *local_got_refcounts;
2242 const Elf_Internal_Rela *rel;
2243 const Elf_Internal_Rela *rel_end;
2244 asection *sgot;
2245 asection *srelgot;
2246 asection *sreloc;
2248 if (info->relocateable)
2249 return true;
2251 dynobj = elf_hash_table (info)->dynobj;
2252 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2253 sym_hashes = elf_sym_hashes (abfd);
2254 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2255 local_got_refcounts = elf_local_got_refcounts (abfd);
2257 sgot = NULL;
2258 srelgot = NULL;
2259 sreloc = NULL;
2261 if (!elf_bad_symtab (abfd))
2262 sym_hashes_end -= symtab_hdr->sh_info;
2264 rel_end = relocs + sec->reloc_count;
2265 for (rel = relocs; rel < rel_end; rel++)
2267 struct elf_link_hash_entry *h;
2268 unsigned long r_symndx;
2269 enum elf_cris_reloc_type r_type;
2271 r_symndx = ELF32_R_SYM (rel->r_info);
2272 if (r_symndx < symtab_hdr->sh_info)
2273 h = NULL;
2274 else
2275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2277 r_type = ELF32_R_TYPE (rel->r_info);
2279 /* Some relocs require linker-created sections; we need to hang them
2280 on the first input bfd we found that contained dynamic relocs. */
2281 switch (r_type)
2283 case R_CRIS_16_GOT:
2284 case R_CRIS_32_GOT:
2285 case R_CRIS_32_GOTREL:
2286 case R_CRIS_32_PLT_GOTREL:
2287 case R_CRIS_32_PLT_PCREL:
2288 case R_CRIS_16_GOTPLT:
2289 case R_CRIS_32_GOTPLT:
2290 if (dynobj == NULL)
2292 elf_hash_table (info)->dynobj = dynobj = abfd;
2294 /* Create the .got section, so we can assume it's always
2295 present whenever there's a dynobj. */
2296 if (!_bfd_elf_create_got_section (dynobj, info))
2297 return false;
2299 break;
2301 default:
2302 break;
2305 /* Some relocs require a global offset table (but perhaps not a
2306 specific GOT entry). */
2307 switch (r_type)
2309 case R_CRIS_16_GOT:
2310 case R_CRIS_32_GOT:
2311 case R_CRIS_32_GOTREL:
2312 case R_CRIS_32_PLT_GOTREL:
2313 if (sgot == NULL)
2314 sgot = bfd_get_section_by_name (dynobj, ".got");
2316 if (local_got_refcounts == NULL)
2318 size_t size;
2320 /* We use index local_got_refcounts[-1] to count all
2321 GOT-relative relocations that do not have explicit
2322 GOT entries. */
2323 size = (symtab_hdr->sh_info + 1) * sizeof (bfd_signed_vma);
2324 local_got_refcounts = ((bfd_signed_vma *)
2325 bfd_alloc (abfd, size));
2326 if (local_got_refcounts == NULL)
2327 return false;
2328 memset (local_got_refcounts, -1, size);
2330 local_got_refcounts++;
2331 elf_local_got_refcounts (abfd) = local_got_refcounts;
2333 break;
2335 default:
2336 break;
2339 switch (r_type)
2341 case R_CRIS_16_GOTPLT:
2342 case R_CRIS_32_GOTPLT:
2343 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2344 entry) is eliminated. We can only do this for a non-local
2345 symbol. */
2346 if (h != NULL)
2348 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2349 goto handle_gotplt_reloc;
2351 /* If h is NULL then this is a local symbol, and we must make a
2352 GOT entry for it, so handle it like a GOT reloc. */
2353 /* Fall through. */
2355 case R_CRIS_16_GOT:
2356 case R_CRIS_32_GOT:
2357 /* This symbol requires a global offset table entry. */
2359 if (srelgot == NULL
2360 && (h != NULL || info->shared))
2362 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2363 if (srelgot == NULL)
2365 srelgot = bfd_make_section (dynobj, ".rela.got");
2366 if (srelgot == NULL
2367 || !bfd_set_section_flags (dynobj, srelgot,
2368 (SEC_ALLOC
2369 | SEC_LOAD
2370 | SEC_HAS_CONTENTS
2371 | SEC_IN_MEMORY
2372 | SEC_LINKER_CREATED
2373 | SEC_READONLY))
2374 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2375 return false;
2379 if (h != NULL)
2381 if (h->got.refcount == -1)
2383 h->got.refcount = 1;
2385 /* Make sure this symbol is output as a dynamic symbol. */
2386 if (h->dynindx == -1)
2388 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2389 return false;
2392 /* Allocate space in the .got section. */
2393 sgot->_raw_size += 4;
2394 /* Allocate relocation space. */
2395 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2397 else
2398 h->got.refcount++;
2400 else
2402 /* This is a global offset table entry for a local symbol. */
2403 if (local_got_refcounts[r_symndx] == -1)
2405 local_got_refcounts[r_symndx] = 1;
2407 sgot->_raw_size += 4;
2408 if (info->shared)
2410 /* If we are generating a shared object, we need to
2411 output a R_CRIS_RELATIVE reloc so that the dynamic
2412 linker can adjust this GOT entry. */
2413 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2416 else
2417 local_got_refcounts[r_symndx]++;
2419 break;
2421 case R_CRIS_32_GOTREL:
2422 /* This reference requires a global offset table.
2423 FIXME: The actual refcount isn't used currently; the .got
2424 section can't be removed if there were any references in the
2425 input. */
2426 local_got_refcounts[-1]++;
2427 break;
2429 handle_gotplt_reloc:
2431 case R_CRIS_32_PLT_GOTREL:
2432 /* This reference requires a global offset table. */
2433 local_got_refcounts[-1]++;
2434 /* Fall through. */
2436 case R_CRIS_32_PLT_PCREL:
2437 /* This symbol requires a procedure linkage table entry. We
2438 actually build the entry in adjust_dynamic_symbol,
2439 because this might be a case of linking PIC code which is
2440 never referenced by a dynamic object, in which case we
2441 don't need to generate a procedure linkage table entry
2442 after all. */
2444 /* If this is a local symbol, we resolve it directly without
2445 creating a procedure linkage table entry. */
2446 if (h == NULL)
2447 continue;
2449 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2450 if (h->plt.refcount == -1)
2451 h->plt.refcount = 1;
2452 else
2453 h->plt.refcount++;
2454 break;
2456 case R_CRIS_8:
2457 case R_CRIS_16:
2458 case R_CRIS_32:
2459 /* Let's help debug shared library creation. Any of these
2460 relocs can be used in shared libs, but pages containing them
2461 cannot be shared. Don't warn for sections we don't care
2462 about, such as debug sections or non-constant sections. We
2463 can't help tables of (global) function pointers, for example,
2464 though they must be emitted in a data section to avoid having
2465 impure text sections. */
2466 if (info->shared
2467 && (sec->flags & SEC_ALLOC) != 0
2468 && (sec->flags & SEC_READONLY) != 0)
2470 /* FIXME: How do we make this optionally a warning only? */
2471 if (abfd->my_archive)
2472 (*_bfd_error_handler)
2473 (_("%s(%s), section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2474 bfd_get_filename (bfd_my_archive (abfd)),
2475 bfd_get_filename (abfd),
2476 sec->name,
2477 cris_elf_howto_table[r_type].name);
2478 else
2479 (*_bfd_error_handler)
2480 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2481 bfd_get_filename (abfd),
2482 sec->name,
2483 cris_elf_howto_table[r_type].name);
2485 /* Fall through. */
2487 case R_CRIS_8_PCREL:
2488 case R_CRIS_16_PCREL:
2489 case R_CRIS_32_PCREL:
2490 if (h != NULL)
2492 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2494 /* Make sure a plt entry is created for this symbol if it
2495 turns out to be a function defined by a dynamic object. */
2496 if (h->plt.refcount == -1)
2497 h->plt.refcount = 1;
2498 else
2499 h->plt.refcount++;
2502 /* If we are creating a shared library and this is not a local
2503 symbol, we need to copy the reloc into the shared library.
2504 However when linking with -Bsymbolic and this is a global
2505 symbol which is defined in an object we are including in the
2506 link (i.e., DEF_REGULAR is set), then we can resolve the
2507 reloc directly. At this point we have not seen all the input
2508 files, so it is possible that DEF_REGULAR is not set now but
2509 will be set later (it is never cleared). In case of a weak
2510 definition, DEF_REGULAR may be cleared later by a strong
2511 definition in a shared library. We account for that
2512 possibility below by storing information in the relocs_copied
2513 field of the hash table entry. A similar situation occurs
2514 when creating shared libraries and symbol visibility changes
2515 render the symbol local. */
2517 /* No need to do anything if we're not creating a shared object. */
2518 if (! info->shared)
2519 break;
2521 /* We don't need to handle relocs into sections not going into
2522 the "real" output. */
2523 if ((sec->flags & SEC_ALLOC) == 0)
2524 break;
2526 /* We can only eliminate PC-relative relocs. */
2527 if (r_type == R_CRIS_8_PCREL
2528 || r_type == R_CRIS_16_PCREL
2529 || r_type == R_CRIS_32_PCREL)
2531 /* If the symbol is local, then we can eliminate the reloc. */
2532 if (h == NULL)
2533 break;
2535 /* If this is with -Bsymbolic and the symbol isn't weak, and
2536 is defined by an ordinary object (the ones we include in
2537 this shared library) then we can also eliminate the
2538 reloc. See comment above for more eliminable cases which
2539 we can't identify at this time. */
2540 if (info->symbolic
2541 && h->root.type != bfd_link_hash_defweak
2542 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2543 break;
2546 /* We create a reloc section in dynobj and make room for this
2547 reloc. */
2548 if (sreloc == NULL)
2550 const char *name;
2552 name = (bfd_elf_string_from_elf_section
2553 (abfd,
2554 elf_elfheader (abfd)->e_shstrndx,
2555 elf_section_data (sec)->rel_hdr.sh_name));
2556 if (name == NULL)
2557 return false;
2559 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2560 && strcmp (bfd_get_section_name (abfd, sec),
2561 name + 5) == 0);
2563 sreloc = bfd_get_section_by_name (dynobj, name);
2564 if (sreloc == NULL)
2566 sreloc = bfd_make_section (dynobj, name);
2567 if (sreloc == NULL
2568 || !bfd_set_section_flags (dynobj, sreloc,
2569 (SEC_ALLOC
2570 | SEC_LOAD
2571 | SEC_HAS_CONTENTS
2572 | SEC_IN_MEMORY
2573 | SEC_LINKER_CREATED
2574 | SEC_READONLY))
2575 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2576 return false;
2580 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2582 /* If we are linking with -Bsymbolic, we count the number of PC
2583 relative relocations we have entered for this symbol, so that
2584 we can discard them again if the symbol is later defined by a
2585 regular object. We know that h is really a pointer to an
2586 elf_cris_link_hash_entry. */
2587 if ((r_type == R_CRIS_8_PCREL
2588 || r_type == R_CRIS_16_PCREL
2589 || r_type == R_CRIS_32_PCREL)
2590 && info->symbolic)
2592 struct elf_cris_link_hash_entry *eh;
2593 struct elf_cris_pcrel_relocs_copied *p;
2595 eh = (struct elf_cris_link_hash_entry *) h;
2597 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2598 if (p->section == sreloc)
2599 break;
2601 if (p == NULL)
2603 p = ((struct elf_cris_pcrel_relocs_copied *)
2604 bfd_alloc (dynobj, sizeof *p));
2605 if (p == NULL)
2606 return false;
2607 p->next = eh->pcrel_relocs_copied;
2608 eh->pcrel_relocs_copied = p;
2609 p->section = sreloc;
2610 p->count = 0;
2613 ++p->count;
2615 break;
2617 /* This relocation describes the C++ object vtable hierarchy.
2618 Reconstruct it for later use during GC. */
2619 case R_CRIS_GNU_VTINHERIT:
2620 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2621 return false;
2622 break;
2624 /* This relocation describes which C++ vtable entries are actually
2625 used. Record for later use during GC. */
2626 case R_CRIS_GNU_VTENTRY:
2627 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2628 return false;
2629 break;
2631 default:
2632 /* Other relocs do not appear here. */
2633 bfd_set_error (bfd_error_bad_value);
2634 return false;
2638 return true;
2641 /* Set the sizes of the dynamic sections. */
2643 static boolean
2644 elf_cris_size_dynamic_sections (output_bfd, info)
2645 bfd *output_bfd;
2646 struct bfd_link_info *info;
2648 bfd *dynobj;
2649 asection *s;
2650 boolean plt;
2651 boolean relocs;
2652 boolean reltext;
2654 dynobj = elf_hash_table (info)->dynobj;
2655 BFD_ASSERT (dynobj != NULL);
2657 if (elf_hash_table (info)->dynamic_sections_created)
2659 /* Set the contents of the .interp section to the interpreter. */
2660 if (!info->shared)
2662 s = bfd_get_section_by_name (dynobj, ".interp");
2663 BFD_ASSERT (s != NULL);
2664 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2665 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2668 else
2670 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2671 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2672 elf_cris_adjust_gotplt_to_got,
2673 (PTR) info);
2675 /* We may have created entries in the .rela.got section.
2676 However, if we are not creating the dynamic sections, we will
2677 not actually use these entries. Reset the size of .rela.got,
2678 which will cause it to get stripped from the output file
2679 below. */
2680 s = bfd_get_section_by_name (dynobj, ".rela.got");
2681 if (s != NULL)
2682 s->_raw_size = 0;
2685 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2686 relative relocs against symbols defined in a regular object. We
2687 allocated space for them in the check_relocs routine, but we will not
2688 fill them in in the relocate_section routine. We also discard space
2689 for relocs that have become for local symbols due to symbol
2690 visibility changes. For programs, we discard space for relocs for
2691 symbols not referenced by any dynamic object. */
2692 if (info->shared)
2693 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2694 elf_cris_discard_excess_dso_dynamics,
2695 (PTR) info);
2696 else
2697 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2698 elf_cris_discard_excess_program_dynamics,
2699 (PTR) info);
2701 /* The check_relocs and adjust_dynamic_symbol entry points have
2702 determined the sizes of the various dynamic sections. Allocate
2703 memory for them. */
2704 plt = false;
2705 relocs = false;
2706 reltext = false;
2707 for (s = dynobj->sections; s != NULL; s = s->next)
2709 const char *name;
2710 boolean strip;
2712 if ((s->flags & SEC_LINKER_CREATED) == 0)
2713 continue;
2715 /* It's OK to base decisions on the section name, because none
2716 of the dynobj section names depend upon the input files. */
2717 name = bfd_get_section_name (dynobj, s);
2719 strip = false;
2721 if (strcmp (name, ".plt") == 0)
2723 if (s->_raw_size == 0)
2725 /* Strip this section if we don't need it; see the
2726 comment below. */
2727 strip = true;
2729 else
2731 /* Remember whether there is a PLT. */
2732 plt = true;
2735 else if (strncmp (name, ".rela", 5) == 0)
2737 if (s->_raw_size == 0)
2739 /* If we don't need this section, strip it from the
2740 output file. This is mostly to handle .rela.bss and
2741 .rela.plt. We must create both sections in
2742 create_dynamic_sections, because they must be created
2743 before the linker maps input sections to output
2744 sections. The linker does that before
2745 adjust_dynamic_symbol is called, and it is that
2746 function which decides whether anything needs to go
2747 into these sections. */
2748 strip = true;
2750 else
2752 asection *target;
2754 /* Remember whether there are any reloc sections other
2755 than .rela.plt. */
2756 if (strcmp (name, ".rela.plt") != 0)
2758 const char *outname;
2760 relocs = true;
2762 /* If this relocation section applies to a read only
2763 section, then we probably need a DT_TEXTREL entry.
2764 The entries in the .rela.plt section are actually
2765 associated with .got.plt, which we created ourselves
2766 and so know is not readonly. */
2767 outname = bfd_get_section_name (output_bfd,
2768 s->output_section);
2769 target
2770 = bfd_get_section_by_name (output_bfd,
2771 outname + strlen (".rela"));
2773 /* We have to test the .text section by name, becase for
2774 some reason it does not have SEC_READONLY set at this
2775 time. That flag is actually set in ldmain.c:main
2776 specifically for ".text" at a time long after this
2777 function is called. FIXME: This might be due to a
2778 general bug. FIXME: Have testcase for this. */
2779 if (target != NULL
2780 && (target->flags & SEC_ALLOC) != 0
2781 && ((target->flags & SEC_READONLY) != 0
2782 || strcmp (outname + strlen (".rela"),
2783 ".text") == 0))
2784 reltext = true;
2787 /* We use the reloc_count field as a counter if we need
2788 to copy relocs into the output file. */
2789 s->reloc_count = 0;
2792 else if (strncmp (name, ".got", 4) != 0)
2794 /* It's not one of our sections, so don't allocate space. */
2795 continue;
2798 if (strip)
2800 _bfd_strip_section_from_output (info, s);
2801 continue;
2804 /* Allocate memory for the section contents. We use bfd_zalloc here
2805 in case unused entries are not reclaimed before the section's
2806 contents are written out. This should not happen, but this way
2807 if it does, we will not write out garbage. For reloc sections,
2808 this will make entries have the type R_CRIS_NONE. */
2809 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2810 if (s->contents == NULL && s->_raw_size != 0)
2811 return false;
2814 if (elf_hash_table (info)->dynamic_sections_created)
2816 /* Add some entries to the .dynamic section. We fill in the
2817 values later, in elf_cris_finish_dynamic_sections, but we
2818 must add the entries now so that we get the correct size for
2819 the .dynamic section. The DT_DEBUG entry is filled in by the
2820 dynamic linker and used by the debugger. */
2821 if (!info->shared)
2823 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2824 return false;
2827 if (plt)
2829 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2830 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2831 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2832 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2833 return false;
2836 if (relocs)
2838 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2839 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2840 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2841 sizeof (Elf32_External_Rela)))
2842 return false;
2845 if (reltext)
2847 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2848 return false;
2849 info->flags |= DF_TEXTREL;
2853 return true;
2856 /* This function is called via elf_cris_link_hash_traverse if we are
2857 creating a shared object. In the -Bsymbolic case, it discards the
2858 space allocated to copy PC relative relocs against symbols which
2859 are defined in regular objects. For the normal non-symbolic case,
2860 we also discard space for relocs that have become local due to
2861 symbol visibility changes. We allocated space for them in the
2862 check_relocs routine, but we won't fill them in in the
2863 relocate_section routine. */
2865 static boolean
2866 elf_cris_discard_excess_dso_dynamics (h, inf)
2867 struct elf_cris_link_hash_entry *h;
2868 PTR inf;
2870 struct elf_cris_pcrel_relocs_copied *s;
2871 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2873 /* If a symbol has been forced local or we have found a regular
2874 definition for the symbolic link case, then we won't be needing
2875 any relocs. */
2876 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2877 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2878 || info->symbolic))
2880 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2881 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2884 return true;
2887 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2888 creating a shared object. We discard space for relocs for symbols put
2889 in the .got, but which we found we do not have to resolve at run-time. */
2891 static boolean
2892 elf_cris_discard_excess_program_dynamics (h, inf)
2893 struct elf_cris_link_hash_entry *h;
2894 PTR inf;
2896 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2898 /* If we're not creating a shared library and have a symbol which is
2899 referred to by .got references, but the symbol is defined locally,
2900 (or rather, not referred to by a DSO and not defined by a DSO) then
2901 lose the reloc for the .got (don't allocate room for it). */
2902 if ((h->root.elf_link_hash_flags
2903 & (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_DEF_DYNAMIC)) == 0)
2905 if (h->root.got.refcount > 0
2906 /* The size of this section is only valid and in sync with the
2907 various reference counts if we do dynamic; don't decrement it
2908 otherwise. */
2909 && elf_hash_table (info)->dynamic_sections_created)
2911 bfd *dynobj = elf_hash_table (info)->dynobj;
2912 asection *srelgot;
2914 BFD_ASSERT (dynobj != NULL);
2916 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2918 BFD_ASSERT (srelgot != NULL);
2920 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2923 /* If the locally-defined symbol isn't used by a DSO, then we don't
2924 have to export it as a dynamic symbol. This was already done for
2925 functions; doing this for all symbols would presumably not
2926 introduce new problems. */
2927 h->root.dynindx = -1;
2930 return true;
2933 /* Reject a file depending on presence and expectation of prefixed
2934 underscores on symbols. */
2936 static boolean
2937 cris_elf_object_p (abfd)
2938 bfd *abfd;
2940 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2941 return (bfd_get_symbol_leading_char (abfd) == '_');
2942 else
2943 return (bfd_get_symbol_leading_char (abfd) == 0);
2946 /* Mark presence or absence of leading underscore. */
2948 static void
2949 cris_elf_final_write_processing (abfd, linker)
2950 bfd *abfd;
2951 boolean linker ATTRIBUTE_UNUSED;
2953 if (bfd_get_symbol_leading_char (abfd) == '_')
2954 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2955 else
2956 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2959 /* Display the flags field. */
2961 static boolean
2962 cris_elf_print_private_bfd_data (abfd, ptr)
2963 bfd *abfd;
2964 PTR ptr;
2966 FILE *file = (FILE *) ptr;
2968 BFD_ASSERT (abfd != NULL && ptr != NULL)
2970 _bfd_elf_print_private_bfd_data (abfd, ptr);
2972 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2974 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2975 fprintf (file, _(" [symbols have a _ prefix]"));
2977 fputc ('\n', file);
2978 return true;
2981 /* Don't mix files with and without a leading underscore. */
2983 static boolean
2984 cris_elf_merge_private_bfd_data (ibfd, obfd)
2985 bfd *ibfd;
2986 bfd *obfd;
2988 flagword old_flags, new_flags;
2990 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2991 return false;
2993 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2994 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2995 return true;
2997 if (! elf_flags_init (obfd))
2999 /* This happens when ld starts out with a 'blank' output file. */
3000 elf_flags_init (obfd) = true;
3002 /* Set flags according to current bfd_target. */
3003 cris_elf_final_write_processing (obfd, false);
3006 old_flags = elf_elfheader (obfd)->e_flags;
3007 new_flags = elf_elfheader (ibfd)->e_flags;
3009 /* Is this good or bad? We'll follow with other excluding flags. */
3010 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
3012 (*_bfd_error_handler)
3013 ((new_flags & EF_CRIS_UNDERSCORE)
3014 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3015 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3016 bfd_get_filename (ibfd));
3017 bfd_set_error (bfd_error_bad_value);
3018 return false;
3021 return true;
3024 #define ELF_ARCH bfd_arch_cris
3025 #define ELF_MACHINE_CODE EM_CRIS
3026 #define ELF_MAXPAGESIZE 0x2000
3028 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3029 #define TARGET_LITTLE_NAME "elf32-cris"
3030 #define elf_symbol_leading_char 0
3032 #define elf_info_to_howto_rel NULL
3033 #define elf_info_to_howto cris_info_to_howto_rela
3034 #define elf_backend_relocate_section cris_elf_relocate_section
3035 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3036 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3037 #define elf_backend_check_relocs cris_elf_check_relocs
3039 #define elf_backend_can_gc_sections 1
3041 #define elf_backend_object_p cris_elf_object_p
3042 #define elf_backend_final_write_processing \
3043 cris_elf_final_write_processing
3044 #define bfd_elf32_bfd_print_private_bfd_data \
3045 cris_elf_print_private_bfd_data
3046 #define bfd_elf32_bfd_merge_private_bfd_data \
3047 cris_elf_merge_private_bfd_data
3049 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3051 #define bfd_elf32_bfd_link_hash_table_create \
3052 elf_cris_link_hash_table_create
3053 #define elf_backend_adjust_dynamic_symbol \
3054 elf_cris_adjust_dynamic_symbol
3055 #define elf_backend_size_dynamic_sections \
3056 elf_cris_size_dynamic_sections
3057 #define elf_backend_finish_dynamic_symbol \
3058 elf_cris_finish_dynamic_symbol
3059 #define elf_backend_finish_dynamic_sections \
3060 elf_cris_finish_dynamic_sections
3061 #define elf_backend_create_dynamic_sections \
3062 _bfd_elf_create_dynamic_sections
3063 #define bfd_elf32_bfd_final_link \
3064 _bfd_elf32_gc_common_final_link
3065 #define elf_backend_hide_symbol elf_cris_hide_symbol
3067 #define elf_backend_want_got_plt 1
3068 #define elf_backend_plt_readonly 1
3069 #define elf_backend_want_plt_sym 0
3070 #define elf_backend_got_header_size 12
3071 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3073 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3074 linking and libraries (if it's a win of any significance). Until then,
3075 take the easy route. */
3076 #define elf_backend_may_use_rel_p 0
3077 #define elf_backend_may_use_rela_p 1
3079 #include "elf32-target.h"
3081 #define INCLUDED_TARGET_FILE
3083 #undef TARGET_LITTLE_SYM
3084 #undef TARGET_LITTLE_NAME
3085 #undef elf_symbol_leading_char
3087 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3088 #define TARGET_LITTLE_NAME "elf32-us-cris"
3089 #define elf_symbol_leading_char '_'
3091 #include "elf32-target.h"