2001-03-26 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf32-cris.c
blob11dc077c645dcd7a7e2c86a438237265989e27cf
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_copies
62 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
63 static boolean elf_cris_adjust_gotplt_to_got
64 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
65 static boolean elf_cris_try_fold_plt_to_got
66 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
67 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
68 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
69 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
70 PARAMS ((bfd *));
71 static boolean elf_cris_adjust_dynamic_symbol
72 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
73 static boolean elf_cris_size_dynamic_sections
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static boolean elf_cris_finish_dynamic_symbol
76 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
77 Elf_Internal_Sym *));
78 static boolean elf_cris_finish_dynamic_sections
79 PARAMS ((bfd *, struct bfd_link_info *));
80 static void elf_cris_hide_symbol
81 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
83 static reloc_howto_type cris_elf_howto_table [] =
85 /* This reloc does nothing. */
86 HOWTO (R_CRIS_NONE, /* type */
87 0, /* rightshift */
88 2, /* size (0 = byte, 1 = short, 2 = long) */
89 32, /* bitsize */
90 false, /* pc_relative */
91 0, /* bitpos */
92 complain_overflow_bitfield, /* complain_on_overflow */
93 bfd_elf_generic_reloc, /* special_function */
94 "R_CRIS_NONE", /* name */
95 false, /* partial_inplace */
96 0, /* src_mask */
97 0, /* dst_mask */
98 false), /* pcrel_offset */
100 /* An 8 bit absolute relocation. */
101 HOWTO (R_CRIS_8, /* type */
102 0, /* rightshift */
103 0, /* size (0 = byte, 1 = short, 2 = long) */
104 8, /* bitsize */
105 false, /* pc_relative */
106 0, /* bitpos */
107 complain_overflow_bitfield, /* complain_on_overflow */
108 bfd_elf_generic_reloc, /* special_function */
109 "R_CRIS_8", /* name */
110 false, /* partial_inplace */
111 0x0000, /* src_mask */
112 0x00ff, /* dst_mask */
113 false), /* pcrel_offset */
115 /* A 16 bit absolute relocation. */
116 HOWTO (R_CRIS_16, /* type */
117 0, /* rightshift */
118 1, /* size (0 = byte, 1 = short, 2 = long) */
119 16, /* bitsize */
120 false, /* pc_relative */
121 0, /* bitpos */
122 complain_overflow_bitfield, /* complain_on_overflow */
123 bfd_elf_generic_reloc, /* special_function */
124 "R_CRIS_16", /* name */
125 false, /* partial_inplace */
126 0x00000000, /* src_mask */
127 0x0000ffff, /* dst_mask */
128 false), /* pcrel_offset */
130 /* A 32 bit absolute relocation. */
131 HOWTO (R_CRIS_32, /* type */
132 0, /* rightshift */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
134 32, /* bitsize */
135 false, /* pc_relative */
136 0, /* bitpos */
137 complain_overflow_bitfield, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_CRIS_32", /* name */
140 false, /* partial_inplace */
141 0x00000000, /* src_mask */
142 0xffffffff, /* dst_mask */
143 false), /* pcrel_offset */
145 /* An 8 bit PC-relative relocation. */
146 HOWTO (R_CRIS_8_PCREL, /* type */
147 0, /* rightshift */
148 0, /* size (0 = byte, 1 = short, 2 = long) */
149 8, /* bitsize */
150 true, /* pc_relative */
151 0, /* bitpos */
152 complain_overflow_bitfield, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_CRIS_8_PCREL", /* name */
155 false, /* partial_inplace */
156 0x0000, /* src_mask */
157 0x00ff, /* dst_mask */
158 true), /* pcrel_offset */
160 /* A 16 bit PC-relative relocation. */
161 HOWTO (R_CRIS_16_PCREL, /* type */
162 0, /* rightshift */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
164 16, /* bitsize */
165 true, /* pc_relative */
166 0, /* bitpos */
167 complain_overflow_bitfield, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_CRIS_16", /* name */
170 false, /* partial_inplace */
171 0x00000000, /* src_mask */
172 0x0000ffff, /* dst_mask */
173 true), /* pcrel_offset */
175 /* A 32 bit PC-relative relocation. */
176 HOWTO (R_CRIS_32_PCREL, /* type */
177 0, /* rightshift */
178 2, /* size (0 = byte, 1 = short, 2 = long) */
179 32, /* bitsize */
180 true, /* pc_relative */
181 0, /* bitpos */
182 complain_overflow_bitfield, /* complain_on_overflow */
183 bfd_elf_generic_reloc, /* special_function */
184 "R_CRIS_32", /* name */
185 false, /* partial_inplace */
186 0x00000000, /* src_mask */
187 0xffffffff, /* dst_mask */
188 true), /* pcrel_offset */
190 /* GNU extension to record C++ vtable hierarchy. */
191 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
192 0, /* rightshift */
193 2, /* size (0 = byte, 1 = short, 2 = long) */
194 0, /* bitsize */
195 false, /* pc_relative */
196 0, /* bitpos */
197 complain_overflow_dont, /* complain_on_overflow */
198 NULL, /* special_function */
199 "R_CRIS_GNU_VTINHERIT", /* name */
200 false, /* partial_inplace */
201 0, /* src_mask */
202 0, /* dst_mask */
203 false), /* pcrel_offset */
205 /* GNU extension to record C++ vtable member usage. */
206 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
207 0, /* rightshift */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
209 0, /* bitsize */
210 false, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_dont, /* complain_on_overflow */
213 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
214 "R_CRIS_GNU_VTENTRY", /* name */
215 false, /* partial_inplace */
216 0, /* src_mask */
217 0, /* dst_mask */
218 false), /* pcrel_offset */
220 /* This is used only by the dynamic linker. The symbol should exist
221 both in the object being run and in some shared library. The
222 dynamic linker copies the data addressed by the symbol from the
223 shared library into the object, because the object being
224 run has to have the data at some particular address. */
225 HOWTO (R_CRIS_COPY, /* type */
226 0, /* rightshift */
227 2, /* size (0 = byte, 1 = short, 2 = long) */
228 32, /* bitsize */
229 false, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_bitfield, /* complain_on_overflow */
232 bfd_elf_generic_reloc, /* special_function */
233 "R_CRIS_COPY", /* name */
234 false, /* partial_inplace */
235 0, /* src_mask */
236 0, /* dst_mask */
237 false), /* pcrel_offset */
239 /* Like R_CRIS_32, but used when setting global offset table entries. */
240 HOWTO (R_CRIS_GLOB_DAT, /* type */
241 0, /* rightshift */
242 2, /* size (0 = byte, 1 = short, 2 = long) */
243 32, /* bitsize */
244 false, /* pc_relative */
245 0, /* bitpos */
246 complain_overflow_bitfield, /* complain_on_overflow */
247 bfd_elf_generic_reloc, /* special_function */
248 "R_CRIS_GLOB_DAT", /* name */
249 false, /* partial_inplace */
250 0, /* src_mask */
251 0xffffffff, /* dst_mask */
252 false), /* pcrel_offset */
254 /* Marks a procedure linkage table entry for a symbol. */
255 HOWTO (R_CRIS_JUMP_SLOT, /* type */
256 0, /* rightshift */
257 2, /* size (0 = byte, 1 = short, 2 = long) */
258 32, /* bitsize */
259 false, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_bitfield, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* special_function */
263 "R_CRIS_JUMP_SLOT", /* name */
264 false, /* partial_inplace */
265 0, /* src_mask */
266 0, /* dst_mask */
267 false), /* pcrel_offset */
269 /* Used only by the dynamic linker. When the object is run, this
270 longword is set to the load address of the object, plus the
271 addend. */
272 HOWTO (R_CRIS_RELATIVE, /* type */
273 0, /* rightshift */
274 2, /* size (0 = byte, 1 = short, 2 = long) */
275 32, /* bitsize */
276 false, /* pc_relative */
277 0, /* bitpos */
278 complain_overflow_bitfield, /* complain_on_overflow */
279 bfd_elf_generic_reloc, /* special_function */
280 "R_CRIS_RELATIVE", /* name */
281 false, /* partial_inplace */
282 0, /* src_mask */
283 0xffffffff, /* dst_mask */
284 false), /* pcrel_offset */
286 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
287 HOWTO (R_CRIS_16_GOT, /* type */
288 0, /* rightshift */
289 1, /* size (0 = byte, 1 = short, 2 = long) */
290 16, /* bitsize */
291 false, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_CRIS_16_GOT", /* name */
296 false, /* partial_inplace */
297 0, /* src_mask */
298 0xffff, /* dst_mask */
299 false), /* pcrel_offset */
301 HOWTO (R_CRIS_32_GOT, /* type */
302 0, /* rightshift */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 32, /* bitsize */
305 false, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_bitfield, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_CRIS_32_GOT", /* name */
310 false, /* partial_inplace */
311 0, /* src_mask */
312 0xffffffff, /* dst_mask */
313 false), /* pcrel_offset */
315 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
316 the GOT table for the symbol. */
317 HOWTO (R_CRIS_16_GOTPLT, /* type */
318 0, /* rightshift */
319 1, /* size (0 = byte, 1 = short, 2 = long) */
320 16, /* bitsize */
321 false, /* pc_relative */
322 0, /* bitpos */
323 complain_overflow_bitfield, /* complain_on_overflow */
324 bfd_elf_generic_reloc, /* special_function */
325 "R_CRIS_16_GOTPLT", /* name */
326 false, /* partial_inplace */
327 0, /* src_mask */
328 0xffff, /* dst_mask */
329 false), /* pcrel_offset */
331 HOWTO (R_CRIS_32_GOTPLT, /* type */
332 0, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 32, /* bitsize */
335 false, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_bitfield, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_CRIS_32_GOTPLT", /* name */
340 false, /* partial_inplace */
341 0, /* src_mask */
342 0xffffffff, /* dst_mask */
343 false), /* pcrel_offset */
345 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
346 be necessary. */
347 HOWTO (R_CRIS_32_GOTREL, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 32, /* bitsize */
351 false, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_CRIS_32_GOTREL", /* name */
356 false, /* partial_inplace */
357 0, /* src_mask */
358 0xffffffff, /* dst_mask */
359 false), /* pcrel_offset */
361 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
362 to create PLT entry for symbol. */
363 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 32, /* bitsize */
367 false, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_CRIS_32_PLT_GOTREL", /* name */
372 false, /* partial_inplace */
373 0, /* src_mask */
374 0xffffffff, /* dst_mask */
375 false), /* pcrel_offset */
377 /* A 32-bit offset from PC (location after the relocation) + addend to
378 entry for this symbol in PLT and request to create PLT entry for
379 symbol. */
380 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
381 0, /* rightshift */
382 2, /* size (0 = byte, 1 = short, 2 = long) */
383 32, /* bitsize */
384 true, /* pc_relative */
385 0, /* bitpos */
386 complain_overflow_bitfield, /* complain_on_overflow */
387 bfd_elf_generic_reloc, /* special_function */
388 "R_CRIS_32_PLT_PCREL", /* name */
389 false, /* partial_inplace */
390 0, /* src_mask */
391 0xffffffff, /* dst_mask */
392 true) /* pcrel_offset */
395 /* Map BFD reloc types to CRIS ELF reloc types. */
397 struct cris_reloc_map
399 bfd_reloc_code_real_type bfd_reloc_val;
400 unsigned int cris_reloc_val;
403 static const struct cris_reloc_map cris_reloc_map [] =
405 { BFD_RELOC_NONE, R_CRIS_NONE },
406 { BFD_RELOC_8, R_CRIS_8 },
407 { BFD_RELOC_16, R_CRIS_16 },
408 { BFD_RELOC_32, R_CRIS_32 },
409 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
410 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
411 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
412 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
413 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
414 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
415 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
416 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
417 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
418 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
419 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
420 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
421 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
422 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
423 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
424 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
427 static reloc_howto_type *
428 cris_reloc_type_lookup (abfd, code)
429 bfd * abfd ATTRIBUTE_UNUSED;
430 bfd_reloc_code_real_type code;
432 unsigned int i;
434 for (i = sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]);
435 --i;)
436 if (cris_reloc_map [i].bfd_reloc_val == code)
437 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
439 return NULL;
442 /* Set the howto pointer for an CRIS ELF reloc. */
444 static void
445 cris_info_to_howto_rela (abfd, cache_ptr, dst)
446 bfd * abfd ATTRIBUTE_UNUSED;
447 arelent * cache_ptr;
448 Elf32_Internal_Rela * dst;
450 unsigned int r_type;
452 r_type = ELF32_R_TYPE (dst->r_info);
453 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
454 cache_ptr->howto = & cris_elf_howto_table [r_type];
457 /* The name of the dynamic interpreter. This is put in the .interp
458 section. */
460 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
462 /* The size in bytes of an entry in the procedure linkage table. */
464 #define PLT_ENTRY_SIZE 20
466 /* The first entry in an absolute procedure linkage table looks like this. */
468 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
470 0xfc, 0xe1,
471 0x7e, 0x7e, /* push mof. */
472 0x7f, 0x0d, /* (dip [pc+]) */
473 0, 0, 0, 0, /* Replaced with address of .got + 4. */
474 0x30, 0x7a, /* move [...],mof */
475 0x7f, 0x0d, /* (dip [pc+]) */
476 0, 0, 0, 0, /* Replaced with address of .got + 8. */
477 0x30, 0x09, /* jump [...] */
480 /* Subsequent entries in an absolute procedure linkage table look like
481 this. */
483 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
485 0x7f, 0x0d, /* (dip [pc+]) */
486 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
487 0x30, 0x09, /* jump [...] */
488 0x3f, 0x7e, /* move [pc+],mof */
489 0, 0, 0, 0, /* Replaced with offset into relocation table. */
490 0x2f, 0xfe, /* add.d [pc+],pc */
491 0xec, 0xff,
492 0xff, 0xff /* Replaced with offset to start of .plt. */
495 /* The first entry in a PIC procedure linkage table looks like this. */
497 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
499 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
500 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
501 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
502 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
505 /* Subsequent entries in a PIC procedure linkage table look like this. */
507 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
509 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
510 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
511 0x30, 0x09, /* jump [...] */
512 0x3f, 0x7e, /* move [pc+],mof */
513 0, 0, 0, 0, /* Replaced with offset into relocation table. */
514 0x2f, 0xfe, /* add.d [pc+],pc */
515 0xec, 0xff, /* Replaced with offset to start of .plt. */
516 0xff, 0xff
519 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
520 (and most other PIC/shlib stuff). Check that we don't drift away
521 without reason.
523 The CRIS linker, like the m68k and i386 linkers (and probably the rest
524 too) needs to keep track of the number of relocs that it decides to
525 copy in check_relocs for each symbol. This is so that it can discard
526 PC relative relocs if it doesn't need them when linking with
527 -Bsymbolic. We store the information in a field extending the regular
528 ELF linker hash table. */
530 /* This structure keeps track of the number of PC relative relocs we have
531 copied for a given symbol. */
533 struct elf_cris_pcrel_relocs_copied
535 /* Next section. */
536 struct elf_cris_pcrel_relocs_copied *next;
537 /* A section in dynobj. */
538 asection *section;
539 /* Number of relocs copied in this section. */
540 bfd_size_type count;
543 /* CRIS ELF linker hash entry. */
545 struct elf_cris_link_hash_entry
547 struct elf_link_hash_entry root;
549 /* Number of PC relative relocs copied for this symbol. */
550 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
552 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
553 both a general GOT and a PLT-specific GOT entry for the same symbol,
554 when it is referenced both as a function and as a function pointer.
556 Number of GOTPLT references for a function. */
557 bfd_signed_vma gotplt_refcount;
559 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
560 (zero is never used as an index). FIXME: We should be able to fold
561 this with gotplt_refcount in a union, like the got and plt unions in
562 elf_link_hash_entry. */
563 bfd_size_type gotplt_offset;
566 /* CRIS ELF linker hash table. */
568 struct elf_cris_link_hash_table
570 struct elf_link_hash_table root;
572 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
573 since we try and avoid creating GOTPLT:s when there's already a GOT.
574 Instead, we keep and update the next available index here. */
575 bfd_size_type next_gotplt_entry;
578 /* Traverse a CRIS ELF linker hash table. */
580 #define elf_cris_link_hash_traverse(table, func, info) \
581 (elf_link_hash_traverse \
582 (&(table)->root, \
583 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
584 (info)))
586 /* Get the CRIS ELF linker hash table from a link_info structure. */
588 #define elf_cris_hash_table(p) \
589 ((struct elf_cris_link_hash_table *) (p)->hash)
591 /* Create an entry in a CRIS ELF linker hash table. */
593 static struct bfd_hash_entry *
594 elf_cris_link_hash_newfunc (entry, table, string)
595 struct bfd_hash_entry *entry;
596 struct bfd_hash_table *table;
597 const char *string;
599 struct elf_cris_link_hash_entry *ret =
600 (struct elf_cris_link_hash_entry *) entry;
602 /* Allocate the structure if it has not already been allocated by a
603 subclass. */
604 if (ret == (struct elf_cris_link_hash_entry *) NULL)
605 ret = ((struct elf_cris_link_hash_entry *)
606 bfd_hash_allocate (table,
607 sizeof (struct elf_cris_link_hash_entry)));
608 if (ret == (struct elf_cris_link_hash_entry *) NULL)
609 return (struct bfd_hash_entry *) ret;
611 /* Call the allocation method of the superclass. */
612 ret = ((struct elf_cris_link_hash_entry *)
613 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
614 table, string));
615 if (ret != (struct elf_cris_link_hash_entry *) NULL)
617 ret->pcrel_relocs_copied = NULL;
618 ret->gotplt_refcount = 0;
619 ret->gotplt_offset = 0;
622 return (struct bfd_hash_entry *) ret;
625 /* Create a CRIS ELF linker hash table. */
627 static struct bfd_link_hash_table *
628 elf_cris_link_hash_table_create (abfd)
629 bfd *abfd;
631 struct elf_cris_link_hash_table *ret;
633 ret = ((struct elf_cris_link_hash_table *)
634 bfd_alloc (abfd, sizeof (struct elf_cris_link_hash_table)));
635 if (ret == (struct elf_cris_link_hash_table *) NULL)
636 return NULL;
638 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
639 elf_cris_link_hash_newfunc))
641 bfd_release (abfd, ret);
642 return NULL;
645 /* Initialize to skip over the first three entries in the gotplt; they
646 are used for run-time symbol evaluation. */
647 ret->next_gotplt_entry = 12;
649 return &ret->root.root;
652 /* Perform a single relocation. By default we use the standard BFD
653 routines, with a few tweaks. */
655 static bfd_reloc_status_type
656 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
657 relocation)
658 reloc_howto_type * howto;
659 bfd * input_bfd;
660 asection * input_section;
661 bfd_byte * contents;
662 Elf_Internal_Rela * rel;
663 bfd_vma relocation;
665 bfd_reloc_status_type r;
667 /* PC-relative relocations are relative to the position *after*
668 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
669 not a single byte, since PC must be 16-bit-aligned. */
670 switch (ELF32_R_TYPE (rel->r_info))
672 /* Check that the 16-bit GOT relocs are positive. */
673 case R_CRIS_16_GOTPLT:
674 case R_CRIS_16_GOT:
675 if ((bfd_signed_vma) relocation < 0)
676 return bfd_reloc_overflow;
677 break;
679 case R_CRIS_32_PLT_PCREL:
680 case R_CRIS_32_PCREL:
681 relocation -= 2;
682 /* Fall through. */
683 case R_CRIS_8_PCREL:
684 case R_CRIS_16_PCREL:
685 relocation -= 2;
686 break;
688 default:
689 break;
692 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
693 contents, rel->r_offset,
694 relocation, rel->r_addend);
695 return r;
698 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
699 copied, for further comments. */
701 static boolean
702 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
703 contents, relocs, local_syms, local_sections)
704 bfd * output_bfd ATTRIBUTE_UNUSED;
705 struct bfd_link_info * info;
706 bfd * input_bfd;
707 asection * input_section;
708 bfd_byte * contents;
709 Elf_Internal_Rela * relocs;
710 Elf_Internal_Sym * local_syms;
711 asection ** local_sections;
713 bfd *dynobj;
714 Elf_Internal_Shdr * symtab_hdr;
715 struct elf_link_hash_entry ** sym_hashes;
716 bfd_vma *local_got_offsets;
717 asection *sgot;
718 asection *splt;
719 asection *sreloc;
720 Elf_Internal_Rela * rel;
721 Elf_Internal_Rela * relend;
723 dynobj = elf_hash_table (info)->dynobj;
724 local_got_offsets = elf_local_got_offsets (input_bfd);
725 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
726 sym_hashes = elf_sym_hashes (input_bfd);
727 relend = relocs + input_section->reloc_count;
729 /* It seems this can happen with erroneous or unsupported input (mixing
730 a.out and elf in an archive, for example.) */
731 if (sym_hashes == NULL)
732 return false;
734 sgot = NULL;
735 splt = NULL;
736 sreloc = NULL;
738 if (dynobj != NULL)
740 splt = bfd_get_section_by_name (dynobj, ".plt");
741 sgot = bfd_get_section_by_name (dynobj, ".got");
744 for (rel = relocs; rel < relend; rel ++)
746 reloc_howto_type * howto;
747 unsigned long r_symndx;
748 Elf_Internal_Sym * sym;
749 asection * sec;
750 struct elf_link_hash_entry * h;
751 bfd_vma relocation;
752 bfd_reloc_status_type r;
753 const char * name = NULL;
754 int r_type;
756 r_type = ELF32_R_TYPE (rel->r_info);
758 if ( r_type == R_CRIS_GNU_VTINHERIT
759 || r_type == R_CRIS_GNU_VTENTRY)
760 continue;
762 r_symndx = ELF32_R_SYM (rel->r_info);
764 if (info->relocateable)
766 /* This is a relocateable link. We don't have to change
767 anything, unless the reloc is against a section symbol,
768 in which case we have to adjust according to where the
769 section symbol winds up in the output section. */
770 if (r_symndx < symtab_hdr->sh_info)
772 sym = local_syms + r_symndx;
774 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
776 sec = local_sections [r_symndx];
777 rel->r_addend += sec->output_offset + sym->st_value;
781 continue;
784 /* This is a final link. */
785 howto = cris_elf_howto_table + r_type;
786 h = NULL;
787 sym = NULL;
788 sec = NULL;
790 if (r_symndx < symtab_hdr->sh_info)
792 sym = local_syms + r_symndx;
793 sec = local_sections [r_symndx];
794 relocation = (sec->output_section->vma
795 + sec->output_offset
796 + sym->st_value);
798 name = bfd_elf_string_from_elf_section
799 (input_bfd, symtab_hdr->sh_link, sym->st_name);
800 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
802 else
804 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
806 while (h->root.type == bfd_link_hash_indirect
807 || h->root.type == bfd_link_hash_warning)
808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
810 name = h->root.root.string;
812 if (h->root.type == bfd_link_hash_defined
813 || h->root.type == bfd_link_hash_defweak)
815 sec = h->root.u.def.section;
817 /* Perhaps we should detect the cases that
818 sec->output_section is expected to be NULL like i386 and
819 m68k, but apparently (and according to elfxx-ia64.c) all
820 valid cases are where the symbol is defined in a shared
821 object which we link dynamically against. This includes
822 PLT relocs for which we've created a PLT entry and other
823 relocs for which we're prepared to create dynamic
824 relocations.
826 For now, new situations cause us to just err when
827 sec->output_offset is NULL but the object with the symbol
828 is *not* dynamically linked against. Thus this will
829 automatically remind us so we can see if there are other
830 valid cases we need to revisit. */
831 if ((sec->output_section == NULL
832 && (sec->owner->flags & DYNAMIC) != 0)
834 /* Here follow the cases where the relocation value must
835 be zero (or when further handling is simplified when
836 zero). I can't claim to understand the various
837 conditions and they weren't described in the files
838 where I copied them from (elf32-m68k.c and
839 elf32-i386.c), but let's mention examples of where
840 they happen. FIXME: Perhaps define and use a
841 dynamic_symbol_p function like ia64.
843 - When creating a shared library, we can have an
844 ordinary relocation for a symbol defined in a shared
845 library (perhaps the one we create). We then make
846 the relocation value zero, as the value seen now will
847 be added into the relocation addend in this shared
848 library, but must be handled only at dynamic-link
849 time. FIXME: Not sure this example covers the
850 h->elf_link_hash_flags test, though it's there in
851 other targets. */
852 || (info->shared
853 && ((! info->symbolic && h->dynindx != -1)
854 || (h->elf_link_hash_flags
855 & ELF_LINK_HASH_DEF_REGULAR) == 0)
856 && (input_section->flags & SEC_ALLOC) != 0
857 && (r_type == R_CRIS_8
858 || r_type == R_CRIS_16
859 || r_type == R_CRIS_32
860 || r_type == R_CRIS_8_PCREL
861 || r_type == R_CRIS_16_PCREL
862 || r_type == R_CRIS_32_PCREL)))
863 relocation = 0;
864 else if (sec->output_section != NULL)
865 relocation = (h->root.u.def.value
866 + sec->output_section->vma
867 + sec->output_offset);
868 else
870 if (input_bfd->my_archive)
871 (*_bfd_error_handler)
872 (_("%s(%s): unresolvable relocation %s against symbol `%s' from %s section"),
873 bfd_get_filename (bfd_my_archive (input_bfd)),
874 bfd_get_filename (input_bfd),
875 cris_elf_howto_table[r_type].name,
876 name,
877 bfd_get_section_name (input_bfd, input_section));
878 else
879 (*_bfd_error_handler)
880 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
881 bfd_get_filename (input_bfd),
882 cris_elf_howto_table[r_type].name,
883 name,
884 bfd_get_section_name (input_bfd, input_section));
885 bfd_set_error (bfd_error_bad_value);
886 return false;
889 else if (h->root.type == bfd_link_hash_undefweak)
890 relocation = 0;
891 else if (info->shared
892 && !info->symbolic
893 && !info->no_undefined
894 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
895 relocation = 0;
896 else
898 if (!(info->callbacks->undefined_symbol
899 (info, name, input_bfd,
900 input_section, rel->r_offset,
901 (!info->shared || info->no_undefined
902 || ELF_ST_VISIBILITY (h->other)))))
903 return false;
904 relocation = 0;
908 switch (r_type)
910 case R_CRIS_16_GOTPLT:
911 case R_CRIS_32_GOTPLT:
912 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
913 but we require a PLT, and the PLT handling will take care of
914 filling in the PLT-specific GOT entry. For the GOT offset,
915 calculate it as we do when filling it in for the .got.plt
916 section. If we don't have a PLT, punt to GOT handling. */
917 if (h != NULL
918 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
920 asection *sgotplt
921 = bfd_get_section_by_name (dynobj, ".got.plt");
922 bfd_vma got_offset;
924 BFD_ASSERT (h->dynindx != -1);
925 BFD_ASSERT (sgotplt != NULL);
927 got_offset
928 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
930 relocation = got_offset;
931 break;
934 /* We didn't make a PLT entry for this symbol, or everything is
935 folded into the GOT. Other than folding, this happens when
936 statically linking PIC code, or when using -Bsymbolic. Check
937 that we instead have a GOT entry as done for us by
938 elf_cris_adjust_dynamic_symbol, and drop through into the
939 ordinary GOT cases. */
940 if (h != NULL && h->got.offset == (bfd_vma) -1)
942 (*_bfd_error_handler)
943 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
944 bfd_get_filename (input_bfd),
945 cris_elf_howto_table[r_type].name,
946 name[0] != '\0' ? name : _("[whose name is lost]"),
947 bfd_get_section_name (input_bfd, input_section));
949 /* FIXME: Perhaps blaming input is not the right thing to
950 do; this is probably an internal error. But it is true
951 that we didn't like that particular input. */
952 bfd_set_error (bfd_error_bad_value);
953 return false;
955 /* Fall through. */
957 /* The size of the actual relocation is not used here; we only
958 fill in the GOT table here. */
959 case R_CRIS_16_GOT:
960 case R_CRIS_32_GOT:
962 bfd_vma off;
964 /* Note that despite using RELA relocations, the .got contents
965 is always filled in with the link-relative relocation
966 value; the addend. */
968 if (h != NULL)
970 off = h->got.offset;
971 BFD_ASSERT (off != (bfd_vma) -1);
973 if (!elf_hash_table (info)->dynamic_sections_created
974 || (info->shared
975 && (info->symbolic || h->dynindx == -1)
976 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
978 /* This is actually a static link, or it is a
979 -Bsymbolic link and the symbol is defined
980 locally, or the symbol was forced to be local
981 because of a version file. We must initialize
982 this entry in the global offset table. Since
983 the offset must always be a multiple of 4, we
984 use the least significant bit to record whether
985 we have initialized it already.
987 When doing a dynamic link, we create a .rela.got
988 relocation entry to initialize the value. This
989 is done in the finish_dynamic_symbol routine. */
990 if ((off & 1) != 0)
991 off &= ~1;
992 else
994 bfd_put_32 (output_bfd, relocation,
995 sgot->contents + off);
996 h->got.offset |= 1;
1000 else
1002 BFD_ASSERT (local_got_offsets != NULL
1003 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1005 off = local_got_offsets[r_symndx];
1007 /* The offset must always be a multiple of 4. We use
1008 the least significant bit to record whether we have
1009 already generated the necessary reloc. */
1010 if ((off & 1) != 0)
1011 off &= ~1;
1012 else
1014 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1016 if (info->shared)
1018 asection *srelgot;
1019 Elf_Internal_Rela outrel;
1021 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1022 BFD_ASSERT (srelgot != NULL);
1024 outrel.r_offset = (sgot->output_section->vma
1025 + sgot->output_offset
1026 + off);
1027 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1028 outrel.r_addend = relocation;
1029 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1030 (((Elf32_External_Rela *)
1031 srelgot->contents)
1032 + srelgot->reloc_count));
1033 ++srelgot->reloc_count;
1036 local_got_offsets[r_symndx] |= 1;
1040 relocation = sgot->output_offset + off;
1041 if (rel->r_addend != 0)
1043 /* We can't do anything for a relocation which is against
1044 a symbol *plus offset*. GOT holds relocations for
1045 symbols. Make this an error; the compiler isn't
1046 allowed to pass us these kinds of things. */
1047 if (h == NULL)
1048 (*_bfd_error_handler)
1049 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1050 bfd_get_filename (input_bfd),
1051 cris_elf_howto_table[r_type].name,
1052 rel->r_addend,
1053 bfd_get_section_name (input_bfd, input_section));
1054 else
1055 (*_bfd_error_handler)
1056 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1057 bfd_get_filename (input_bfd),
1058 cris_elf_howto_table[r_type].name,
1059 rel->r_addend,
1060 name[0] != '\0' ? name : _("[whose name is lost]"),
1061 bfd_get_section_name (input_bfd, input_section));
1063 bfd_set_error (bfd_error_bad_value);
1064 return false;
1067 break;
1069 case R_CRIS_32_GOTREL:
1070 /* This relocation must only be performed against local symbols. */
1071 if (h != NULL)
1073 (*_bfd_error_handler)
1074 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1075 bfd_get_filename (input_bfd),
1076 cris_elf_howto_table[r_type].name,
1077 name,
1078 bfd_get_section_name (input_bfd, input_section));
1079 bfd_set_error (bfd_error_bad_value);
1080 return false;
1083 /* This relocation is like a PC-relative one, except the
1084 reference point is the location of GOT. Note that
1085 sgot->output_offset is not involved in this calculation. We
1086 always want the start of entire .got section, not the
1087 position after the reserved header. */
1088 relocation -= sgot->output_section->vma;
1089 break;
1091 case R_CRIS_32_PLT_PCREL:
1092 /* Relocation is to the entry for this symbol in the
1093 procedure linkage table. */
1095 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1096 without using the procedure linkage table. */
1097 if (h == NULL)
1098 break;
1100 if (h->plt.offset == (bfd_vma) -1
1101 || splt == NULL)
1103 /* We didn't make a PLT entry for this symbol. This
1104 happens when statically linking PIC code, or when
1105 using -Bsymbolic. */
1106 break;
1109 relocation = (splt->output_section->vma
1110 + splt->output_offset
1111 + h->plt.offset);
1112 break;
1114 case R_CRIS_32_PLT_GOTREL:
1115 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1116 start of the .got section. See also comment at
1117 R_CRIS_32_GOT. */
1118 relocation -= sgot->output_section->vma;
1120 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1121 without using the procedure linkage table. */
1122 if (h == NULL)
1123 break;
1125 if (h->plt.offset == (bfd_vma) -1
1126 || splt == NULL)
1128 /* We didn't make a PLT entry for this symbol. This
1129 happens when statically linking PIC code, or when
1130 using -Bsymbolic. */
1131 break;
1134 relocation = (splt->output_section->vma
1135 + splt->output_offset
1136 + h->plt.offset
1137 - sgot->output_section->vma);
1138 break;
1140 case R_CRIS_8_PCREL:
1141 case R_CRIS_16_PCREL:
1142 case R_CRIS_32_PCREL:
1143 /* If the symbol was local, we need no shlib-specific handling. */
1144 if (h == NULL)
1145 break;
1147 /* Fall through. */
1148 case R_CRIS_8:
1149 case R_CRIS_16:
1150 case R_CRIS_32:
1151 if (info->shared
1152 && (input_section->flags & SEC_ALLOC) != 0
1153 && ((r_type != R_CRIS_8_PCREL
1154 && r_type != R_CRIS_16_PCREL
1155 && r_type != R_CRIS_32_PCREL)
1156 || (!info->symbolic
1157 || (h->elf_link_hash_flags
1158 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1160 Elf_Internal_Rela outrel;
1161 boolean skip, relocate;
1163 /* When generating a shared object, these relocations
1164 are copied into the output file to be resolved at run
1165 time. */
1167 if (sreloc == NULL)
1169 const char *name;
1171 name = (bfd_elf_string_from_elf_section
1172 (input_bfd,
1173 elf_elfheader (input_bfd)->e_shstrndx,
1174 elf_section_data (input_section)->rel_hdr.sh_name));
1175 if (name == NULL)
1176 return false;
1178 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1179 && strcmp (bfd_get_section_name (input_bfd,
1180 input_section),
1181 name + 5) == 0);
1183 sreloc = bfd_get_section_by_name (dynobj, name);
1185 /* That section should have been created in
1186 cris_elf_check_relocs, but that function will not be
1187 called for objects which fail in
1188 cris_elf_merge_private_bfd_data. */
1189 if (sreloc == NULL)
1191 (*_bfd_error_handler)
1192 (_("%s: Internal inconsistency; no relocation section %s"),
1193 bfd_get_filename (input_bfd),
1194 name);
1196 bfd_set_error (bfd_error_bad_value);
1197 return false;
1201 skip = false;
1203 if (elf_section_data (input_section)->stab_info == NULL)
1204 outrel.r_offset = rel->r_offset;
1205 else
1207 bfd_vma off;
1209 off = (_bfd_stab_section_offset
1210 (output_bfd, &elf_hash_table (info)->stab_info,
1211 input_section,
1212 &elf_section_data (input_section)->stab_info,
1213 rel->r_offset));
1214 if (off == (bfd_vma) -1)
1215 skip = true;
1216 outrel.r_offset = off;
1219 outrel.r_offset += (input_section->output_section->vma
1220 + input_section->output_offset);
1222 if (skip)
1224 memset (&outrel, 0, sizeof outrel);
1225 relocate = false;
1227 /* h->dynindx may be -1 if the symbol was marked to
1228 become local. */
1229 else if (h != NULL
1230 && ((! info->symbolic && h->dynindx != -1)
1231 || (h->elf_link_hash_flags
1232 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1234 BFD_ASSERT (h->dynindx != -1);
1235 relocate = false;
1236 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1237 outrel.r_addend = relocation + rel->r_addend;
1239 else
1241 if (r_type == R_CRIS_32)
1243 relocate = true;
1244 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1245 outrel.r_addend = relocation + rel->r_addend;
1247 else
1249 long indx;
1251 if (h == NULL)
1252 sec = local_sections[r_symndx];
1253 else
1255 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1256 || (h->root.type
1257 == bfd_link_hash_defweak));
1258 sec = h->root.u.def.section;
1260 if (sec != NULL && bfd_is_abs_section (sec))
1261 indx = 0;
1262 else if (sec == NULL || sec->owner == NULL)
1264 bfd_set_error (bfd_error_bad_value);
1265 return false;
1267 else
1269 asection *osec;
1271 osec = sec->output_section;
1272 indx = elf_section_data (osec)->dynindx;
1273 BFD_ASSERT (indx > 0);
1276 relocate = false;
1277 outrel.r_info = ELF32_R_INFO (indx, r_type);
1278 outrel.r_addend = relocation + rel->r_addend;
1282 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1283 (((Elf32_External_Rela *)
1284 sreloc->contents)
1285 + sreloc->reloc_count));
1286 ++sreloc->reloc_count;
1288 /* This reloc will be computed at runtime, so there's no
1289 need to do anything now, except for R_CRIS_32 relocations
1290 that have been turned into R_CRIS_RELATIVE. */
1291 if (!relocate)
1292 continue;
1295 break;
1298 r = cris_final_link_relocate (howto, input_bfd, input_section,
1299 contents, rel, relocation);
1301 if (r != bfd_reloc_ok)
1303 const char * msg = (const char *) NULL;
1305 switch (r)
1307 case bfd_reloc_overflow:
1308 r = info->callbacks->reloc_overflow
1309 (info, name, howto->name, (bfd_vma) 0,
1310 input_bfd, input_section, rel->r_offset);
1311 break;
1313 case bfd_reloc_undefined:
1314 r = info->callbacks->undefined_symbol
1315 (info, name, input_bfd, input_section, rel->r_offset,
1316 true);
1317 break;
1319 case bfd_reloc_outofrange:
1320 msg = _("internal error: out of range error");
1321 break;
1323 case bfd_reloc_notsupported:
1324 msg = _("internal error: unsupported relocation error");
1325 break;
1327 case bfd_reloc_dangerous:
1328 msg = _("internal error: dangerous relocation");
1329 break;
1331 default:
1332 msg = _("internal error: unknown error");
1333 break;
1336 if (msg)
1337 r = info->callbacks->warning
1338 (info, msg, name, input_bfd, input_section, rel->r_offset);
1340 if (! r)
1341 return false;
1345 return true;
1348 /* Finish up dynamic symbol handling. We set the contents of various
1349 dynamic sections here. */
1351 static boolean
1352 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1353 bfd *output_bfd;
1354 struct bfd_link_info *info;
1355 struct elf_link_hash_entry *h;
1356 Elf_Internal_Sym *sym;
1358 bfd *dynobj;
1359 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1361 dynobj = elf_hash_table (info)->dynobj;
1363 if (h->plt.offset != (bfd_vma) -1)
1365 asection *splt;
1366 asection *sgotplt;
1367 asection *sgot;
1368 asection *srela;
1369 bfd_vma got_base;
1371 bfd_vma gotplt_offset
1372 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1373 Elf_Internal_Rela rela;
1374 boolean has_gotplt = gotplt_offset != 0;
1376 /* Get the index in the procedure linkage table which
1377 corresponds to this symbol. This is the index of this symbol
1378 in all the symbols for which we are making plt entries. The
1379 first entry in the procedure linkage table is reserved. */
1380 /* We have to count backwards here, and the result is only valid as
1381 an index into .got.plt and its relocations. FIXME: Constants... */
1382 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1384 /* Get the offset into the .got table of the entry that corresponds
1385 to this function. Note that we embed knowledge that "incoming"
1386 .got goes after .got.plt in the output without padding (pointer
1387 aligned). However, that knowledge is present in several other
1388 places too, here and in elflink.h at least. */
1389 bfd_vma got_offset
1390 = (has_gotplt
1391 ? gotplt_offset
1392 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1394 /* This symbol has an entry in the procedure linkage table. Set it
1395 up. */
1397 BFD_ASSERT (h->dynindx != -1);
1399 splt = bfd_get_section_by_name (dynobj, ".plt");
1400 sgot = bfd_get_section_by_name (dynobj, ".got");
1401 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1402 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1403 BFD_ASSERT (splt != NULL && sgotplt != NULL
1404 && (! has_gotplt || srela != NULL));
1406 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1408 /* Fill in the entry in the procedure linkage table. */
1409 if (! info->shared)
1411 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1412 PLT_ENTRY_SIZE);
1414 /* We need to enter the absolute address of the GOT entry here. */
1415 bfd_put_32 (output_bfd, got_base + got_offset,
1416 splt->contents + h->plt.offset + plt_off1);
1418 else
1420 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1421 PLT_ENTRY_SIZE);
1422 bfd_put_32 (output_bfd, got_offset,
1423 splt->contents + h->plt.offset + plt_off1);
1426 /* Fill in the plt entry and make a relocation, if this is a "real"
1427 PLT entry. */
1428 if (has_gotplt)
1430 /* Fill in the offset into the reloc table. */
1431 bfd_put_32 (output_bfd,
1432 gotplt_index * sizeof (Elf32_External_Rela),
1433 splt->contents + h->plt.offset + plt_off2);
1435 /* Fill in the offset to the first PLT entry, where to "jump". */
1436 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1437 splt->contents + h->plt.offset + plt_off3);
1439 /* Fill in the entry in the global offset table with the address of
1440 the relocating stub. */
1441 bfd_put_32 (output_bfd,
1442 (splt->output_section->vma
1443 + splt->output_offset
1444 + h->plt.offset
1445 + 8),
1446 sgotplt->contents + got_offset);
1448 /* Fill in the entry in the .rela.plt section. */
1449 rela.r_offset = (sgotplt->output_section->vma
1450 + sgotplt->output_offset
1451 + got_offset);
1452 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1453 rela.r_addend = 0;
1454 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1455 ((Elf32_External_Rela *) srela->contents
1456 + gotplt_index));
1459 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1461 /* Mark the symbol as undefined, rather than as defined in
1462 the .plt section. Leave the value alone. */
1463 sym->st_shndx = SHN_UNDEF;
1465 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1466 know whether resetting the value is significant; if it really
1467 is, rather than a quirk or bug in the sparc port, then I
1468 believe we'd see this elsewhere. */
1469 /* If the symbol is weak, we do need to clear the value.
1470 Otherwise, the PLT entry would provide a definition for
1471 the symbol even if the symbol wasn't defined anywhere,
1472 and so the symbol would never be NULL. */
1473 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1474 == 0)
1475 sym->st_value = 0;
1479 if (h->got.offset != (bfd_vma) -1)
1481 asection *sgot;
1482 asection *srela;
1483 Elf_Internal_Rela rela;
1485 /* This symbol has an entry in the global offset table. Set it up. */
1487 sgot = bfd_get_section_by_name (dynobj, ".got");
1488 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1489 BFD_ASSERT (sgot != NULL && srela != NULL);
1491 rela.r_offset = (sgot->output_section->vma
1492 + sgot->output_offset
1493 + (h->got.offset &~ 1));
1495 /* If this is a static link, or it is a -Bsymbolic link and the
1496 symbol is defined locally or was forced to be local because
1497 of a version file, we just want to emit a RELATIVE reloc.
1498 The entry in the global offset table will already have been
1499 initialized in the relocate_section function. */
1500 if (! elf_hash_table (info)->dynamic_sections_created
1501 || (info->shared
1502 && (info->symbolic || h->dynindx == -1)
1503 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1505 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1506 rela.r_addend = bfd_get_signed_32 (output_bfd,
1507 (sgot->contents
1508 + (h->got.offset & ~1)));
1510 else
1512 bfd_put_32 (output_bfd, (bfd_vma) 0,
1513 sgot->contents + (h->got.offset & ~1));
1514 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1515 rela.r_addend = 0;
1518 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1519 ((Elf32_External_Rela *) srela->contents
1520 + srela->reloc_count));
1521 ++srela->reloc_count;
1524 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1526 asection *s;
1527 Elf_Internal_Rela rela;
1529 /* This symbol needs a copy reloc. Set it up. */
1531 BFD_ASSERT (h->dynindx != -1
1532 && (h->root.type == bfd_link_hash_defined
1533 || h->root.type == bfd_link_hash_defweak));
1535 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1536 ".rela.bss");
1537 BFD_ASSERT (s != NULL);
1539 rela.r_offset = (h->root.u.def.value
1540 + h->root.u.def.section->output_section->vma
1541 + h->root.u.def.section->output_offset);
1542 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1543 rela.r_addend = 0;
1544 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1545 ((Elf32_External_Rela *) s->contents
1546 + s->reloc_count));
1547 ++s->reloc_count;
1550 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1551 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1552 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1553 sym->st_shndx = SHN_ABS;
1555 return true;
1558 /* Finish up the dynamic sections. */
1560 static boolean
1561 elf_cris_finish_dynamic_sections (output_bfd, info)
1562 bfd *output_bfd;
1563 struct bfd_link_info *info;
1565 bfd *dynobj;
1566 asection *sgot;
1567 asection *sdyn;
1569 dynobj = elf_hash_table (info)->dynobj;
1571 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1572 BFD_ASSERT (sgot != NULL);
1573 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1575 if (elf_hash_table (info)->dynamic_sections_created)
1577 asection *splt;
1578 Elf32_External_Dyn *dyncon, *dynconend;
1580 splt = bfd_get_section_by_name (dynobj, ".plt");
1581 BFD_ASSERT (splt != NULL && sdyn != NULL);
1583 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1584 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1585 for (; dyncon < dynconend; dyncon++)
1587 Elf_Internal_Dyn dyn;
1588 asection *s;
1590 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1592 switch (dyn.d_tag)
1594 default:
1595 break;
1597 case DT_PLTGOT:
1598 s = bfd_get_section_by_name (output_bfd, ".got");
1599 BFD_ASSERT (s != NULL);
1600 dyn.d_un.d_ptr = s->vma;
1601 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1602 break;
1604 case DT_JMPREL:
1605 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1606 if all symbols are found in the .got (not .got.plt). */
1607 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1608 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1609 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1610 break;
1612 case DT_PLTRELSZ:
1613 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1614 if (s == NULL)
1615 dyn.d_un.d_val = 0;
1616 else if (s->_cooked_size != 0)
1617 dyn.d_un.d_val = s->_cooked_size;
1618 else
1619 dyn.d_un.d_val = s->_raw_size;
1620 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1621 break;
1623 case DT_RELASZ:
1624 /* The procedure linkage table relocs (DT_JMPREL) should
1625 not be included in the overall relocs (DT_RELA).
1626 Therefore, we override the DT_RELASZ entry here to
1627 make it not include the JMPREL relocs. Since the
1628 linker script arranges for .rela.plt to follow all
1629 other relocation sections, we don't have to worry
1630 about changing the DT_RELA entry. */
1631 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1632 if (s != NULL)
1634 if (s->_cooked_size != 0)
1635 dyn.d_un.d_val -= s->_cooked_size;
1636 else
1637 dyn.d_un.d_val -= s->_raw_size;
1639 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1640 break;
1644 /* Fill in the first entry in the procedure linkage table. */
1645 if (splt->_raw_size > 0)
1647 if (info->shared)
1648 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1649 else
1651 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1652 bfd_put_32 (output_bfd,
1653 sgot->output_section->vma + sgot->output_offset + 4,
1654 splt->contents + 6);
1655 bfd_put_32 (output_bfd,
1656 sgot->output_section->vma + sgot->output_offset + 8,
1657 splt->contents + 14);
1659 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1660 = PLT_ENTRY_SIZE;
1665 /* Fill in the first three entries in the global offset table. */
1666 if (sgot->_raw_size > 0)
1668 if (sdyn == NULL)
1669 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1670 else
1671 bfd_put_32 (output_bfd,
1672 sdyn->output_section->vma + sdyn->output_offset,
1673 sgot->contents);
1674 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1675 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1678 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1680 return true;
1683 /* Return the section that should be marked against GC for a given
1684 relocation. */
1686 static asection *
1687 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1688 bfd * abfd;
1689 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1690 Elf_Internal_Rela * rel;
1691 struct elf_link_hash_entry * h;
1692 Elf_Internal_Sym * sym;
1694 if (h != NULL)
1696 switch (ELF32_R_TYPE (rel->r_info))
1698 case R_CRIS_GNU_VTINHERIT:
1699 case R_CRIS_GNU_VTENTRY:
1700 break;
1702 default:
1703 switch (h->root.type)
1705 case bfd_link_hash_defined:
1706 case bfd_link_hash_defweak:
1707 return h->root.u.def.section;
1709 case bfd_link_hash_common:
1710 return h->root.u.c.p->section;
1712 default:
1713 break;
1717 else
1719 if (!(elf_bad_symtab (abfd)
1720 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1721 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1722 && sym->st_shndx != SHN_COMMON))
1724 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1728 return NULL;
1731 /* Update the got entry reference counts for the section being removed. */
1733 static boolean
1734 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1735 bfd * abfd ATTRIBUTE_UNUSED;
1736 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1737 asection * sec ATTRIBUTE_UNUSED;
1738 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1740 Elf_Internal_Shdr *symtab_hdr;
1741 struct elf_link_hash_entry **sym_hashes;
1742 bfd_signed_vma *local_got_refcounts;
1743 const Elf_Internal_Rela *rel, *relend;
1744 unsigned long r_symndx;
1745 struct elf_link_hash_entry *h;
1746 bfd *dynobj;
1747 asection *sgot;
1748 asection *srelgot;
1750 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1751 sym_hashes = elf_sym_hashes (abfd);
1752 local_got_refcounts = elf_local_got_refcounts (abfd);
1754 dynobj = elf_hash_table (info)->dynobj;
1755 if (dynobj == NULL)
1756 return true;
1758 sgot = bfd_get_section_by_name (dynobj, ".got");
1759 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1761 relend = relocs + sec->reloc_count;
1762 for (rel = relocs; rel < relend; rel++)
1764 switch (ELF32_R_TYPE (rel->r_info))
1766 case R_CRIS_16_GOT:
1767 case R_CRIS_32_GOT:
1768 r_symndx = ELF32_R_SYM (rel->r_info);
1769 if (r_symndx >= symtab_hdr->sh_info)
1771 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1772 if (h->got.refcount > 0)
1774 --h->got.refcount;
1775 if (h->got.refcount == 0)
1777 /* We don't need the .got entry any more. */
1778 sgot->_raw_size -= 4;
1779 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1782 break;
1785 local_got_reloc:
1786 if (local_got_refcounts != NULL)
1788 if (local_got_refcounts[r_symndx] > 0)
1790 --local_got_refcounts[r_symndx];
1791 if (local_got_refcounts[r_symndx] == 0)
1793 /* We don't need the .got entry any more. */
1794 sgot->_raw_size -= 4;
1795 if (info->shared)
1796 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1800 break;
1802 case R_CRIS_16_GOTPLT:
1803 case R_CRIS_32_GOTPLT:
1804 /* For local symbols, treat these like GOT relocs. */
1805 r_symndx = ELF32_R_SYM (rel->r_info);
1806 if (r_symndx < symtab_hdr->sh_info)
1807 goto local_got_reloc;
1809 case R_CRIS_32_PLT_GOTREL:
1810 /* FIXME: We don't garbage-collect away the .got section. */
1811 if (local_got_refcounts != NULL)
1812 local_got_refcounts[-1]--;
1813 /* Fall through. */
1815 case R_CRIS_8_PCREL:
1816 case R_CRIS_16_PCREL:
1817 case R_CRIS_32_PCREL:
1818 case R_CRIS_32_PLT_PCREL:
1819 r_symndx = ELF32_R_SYM (rel->r_info);
1820 if (r_symndx >= symtab_hdr->sh_info)
1822 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1823 if (h->plt.refcount > 0)
1824 --h->plt.refcount;
1826 break;
1828 default:
1829 break;
1833 return true;
1836 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1837 entry but we found we will not create any. Called when we find we will
1838 not have any PLT for this symbol, by elf_cris_adjust_dynamic_symbol
1839 when we're doing a proper dynamic link, or
1840 elf_cris_size_dynamic_sections if no dynamic sections will be created
1841 (we're only linking static objects). */
1843 static boolean
1844 elf_cris_adjust_gotplt_to_got (h, p)
1845 struct elf_cris_link_hash_entry *h;
1846 PTR p;
1848 struct bfd_link_info *info = (struct bfd_link_info *) p;
1849 bfd *dynobj = elf_hash_table (info)->dynobj;
1851 BFD_ASSERT (dynobj != NULL);
1853 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1854 if (h->gotplt_refcount <= 0)
1855 return true;
1857 if (h->root.got.refcount > 0)
1859 /* There's a GOT entry for this symbol. Just adjust the refcount.
1860 Probably not necessary at this stage, but keeping it accurate
1861 helps avoiding surprises later. */
1862 h->root.got.refcount += h->gotplt_refcount;
1863 h->gotplt_refcount = -1;
1865 else
1867 /* No GOT entry for this symbol. We need to create one.
1868 FIXME: This should be a *local* got, not a global one.
1869 Seriously. */
1870 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1871 asection *srelgot
1872 = bfd_get_section_by_name (dynobj, ".rela.got");
1874 /* Put an accurate refcount there. */
1875 h->root.got.refcount = h->gotplt_refcount;
1877 h->gotplt_refcount = -1;
1879 /* We always have a .got section when there are dynamic
1880 relocs. */
1881 BFD_ASSERT (sgot != NULL /* Surely have .got section. */);
1883 /* We might have had a PLT but with no GOT entry and
1884 further no GOT reloc section at all needed before.
1885 Add it. */
1886 if (srelgot == NULL)
1888 srelgot = bfd_make_section (dynobj, ".rela.got");
1890 if (srelgot == NULL
1891 || !bfd_set_section_flags (dynobj, srelgot,
1892 (SEC_ALLOC
1893 | SEC_LOAD
1894 | SEC_HAS_CONTENTS
1895 | SEC_IN_MEMORY
1896 | SEC_LINKER_CREATED
1897 | SEC_READONLY))
1898 || !bfd_set_section_alignment (dynobj, srelgot, 2))
1899 return false;
1902 /* Allocate space in the .got section. */
1903 sgot->_raw_size += 4;
1905 /* Allocate relocation space. */
1906 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1909 return true;
1912 /* Try to fold PLT entries with GOT entries. There are two cases when we
1913 want to do this:
1915 - When all PLT references are GOTPLT references, and there are GOT
1916 references. We don't have to generate a PLT at all.
1918 - When there are both (ordinary) PLT references and GOT references.
1919 We want to make the PLT reference use the ordinary GOT entry rather
1920 than a run-time dynamically resolved GOTPLT entry (since the GOT
1921 entry will have to be resolved at startup anyway).
1923 Though the latter case is handled when room for the PLT is allocated,
1924 not here.
1926 Note that this function is called before symbols are forced local by
1927 version scripts. The differing cases are handled by
1928 elf_cris_hide_symbol. */
1930 static boolean
1931 elf_cris_try_fold_plt_to_got (h, p)
1932 struct elf_cris_link_hash_entry *h;
1933 PTR p;
1935 struct bfd_link_info *info = (struct bfd_link_info *) p;
1937 /* If there are no GOT references for this symbol, we can't fold any
1938 other reference so there's nothing to do. Likewise if there are no
1939 PLT references; GOTPLT references included. */
1940 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1941 return true;
1943 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
1944 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1946 if (h->gotplt_refcount == h->root.plt.refcount)
1948 /* Th only PLT references are GOTPLT references, and there are GOT
1949 references. Convert PLT to GOT references. */
1950 if (! elf_cris_adjust_gotplt_to_got (h, info))
1951 return false;
1953 /* Clear the PLT references, so no PLT will be created. */
1954 h->root.plt.offset = (bfd_vma) -1;
1957 return true;
1960 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1961 to use a GOT entry (and create one) rather than requiring a GOTPLT
1962 entry. */
1964 static void
1965 elf_cris_hide_symbol (info, h)
1966 struct bfd_link_info *info;
1967 struct elf_link_hash_entry *h;
1969 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1971 _bfd_elf_link_hash_hide_symbol (info, h);
1974 /* Adjust a symbol defined by a dynamic object and referenced by a
1975 regular object. The current definition is in some section of the
1976 dynamic object, but we're not including those sections. We have to
1977 change the definition to something the rest of the link can
1978 understand. */
1980 static boolean
1981 elf_cris_adjust_dynamic_symbol (info, h)
1982 struct bfd_link_info *info;
1983 struct elf_link_hash_entry *h;
1985 bfd *dynobj;
1986 asection *s;
1987 unsigned int power_of_two;
1989 dynobj = elf_hash_table (info)->dynobj;
1991 /* Make sure we know what is going on here. */
1992 BFD_ASSERT (dynobj != NULL
1993 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1994 || h->weakdef != NULL
1995 || ((h->elf_link_hash_flags
1996 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1997 && (h->elf_link_hash_flags
1998 & ELF_LINK_HASH_REF_REGULAR) != 0
1999 && (h->elf_link_hash_flags
2000 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2002 /* If this is a function, put it in the procedure linkage table. We
2003 will fill in the contents of the procedure linkage table later,
2004 when we know the address of the .got section. */
2005 if (h->type == STT_FUNC
2006 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2008 if (! info->shared
2009 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2010 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2011 /* We must always create the plt entry if it was referenced by a
2012 PLT relocation. In this case we already recorded it as a
2013 dynamic symbol. */
2014 /* FIXME: m68k and i386 differ here, for unclear reasons. */
2015 && h->dynindx == -1)
2017 /* This case can occur if we saw a PLT reloc in an input file,
2018 but the symbol was never referred to by a dynamic object. In
2019 such a case, we don't actually need to build a procedure
2020 linkage table, and we can just do a PC reloc instead. */
2021 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2022 h->plt.offset = (bfd_vma) -1;
2024 return
2025 elf_cris_adjust_gotplt_to_got ((struct
2026 elf_cris_link_hash_entry *) h,
2027 info);
2030 /* If there are only GOT references and GOTPLT references to this
2031 PLT entry, get rid of the PLT. */
2032 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2033 h, info))
2034 return false;
2036 /* GC or folding may have rendered this entry unused. */
2037 if (h->plt.refcount <= 0)
2039 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2040 h->plt.offset = (bfd_vma) -1;
2041 return true;
2044 /* Make sure this symbol is output as a dynamic symbol. */
2045 if (h->dynindx == -1)
2047 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2048 return false;
2051 s = bfd_get_section_by_name (dynobj, ".plt");
2052 BFD_ASSERT (s != NULL);
2054 /* If this is the first .plt entry, make room for the special
2055 first entry. */
2056 if (s->_raw_size == 0)
2057 s->_raw_size += PLT_ENTRY_SIZE;
2059 /* If this symbol is not defined in a regular file, and we are
2060 not generating a shared library, then set the symbol to this
2061 location in the .plt. This is required to make function
2062 pointers compare as equal between the normal executable and
2063 the shared library. */
2064 if (!info->shared
2065 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2067 h->root.u.def.section = s;
2068 h->root.u.def.value = s->_raw_size;
2071 /* If there's already a GOT entry, use that, not a .got.plt. The
2072 GOT fields still have a reference count when we get here; it's
2073 not yet changed to offsets. */
2074 if (h->got.refcount > 0)
2076 h->got.refcount += h->plt.refcount;
2078 /* Mark the PLT offset to use the GOT entry by setting the low
2079 bit in the plt offset; it is always a multiple of
2080 pointer-size. */
2081 BFD_ASSERT ((s->_raw_size & 3) == 0);
2083 /* Change the PLT refcount to an offset. */
2084 h->plt.offset = s->_raw_size;
2086 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2087 that the got entry should be used instead. */
2088 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2089 h)->gotplt_offset == 0);
2091 /* Make room for this entry. */
2092 s->_raw_size += PLT_ENTRY_SIZE;
2094 return true;
2097 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2098 h->plt.offset = s->_raw_size;
2100 /* Make room for this entry. */
2101 s->_raw_size += PLT_ENTRY_SIZE;
2103 /* We also need to make an entry in the .got.plt section, which
2104 will be placed in the .got section by the linker script. */
2105 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2106 = elf_cris_hash_table (info)->next_gotplt_entry;
2107 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2109 s = bfd_get_section_by_name (dynobj, ".got.plt");
2110 BFD_ASSERT (s != NULL);
2111 s->_raw_size += 4;
2113 /* We also need to make an entry in the .rela.plt section. */
2115 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2116 BFD_ASSERT (s != NULL);
2117 s->_raw_size += sizeof (Elf32_External_Rela);
2119 return true;
2122 /* Reinitialize the plt offset now that it is not used as a reference
2123 count any more. */
2124 h->plt.offset = (bfd_vma) -1;
2126 /* If this is a weak symbol, and there is a real definition, the
2127 processor independent code will have arranged for us to see the
2128 real definition first, and we can just use the same value. */
2129 if (h->weakdef != NULL)
2131 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2132 || h->weakdef->root.type == bfd_link_hash_defweak);
2133 h->root.u.def.section = h->weakdef->root.u.def.section;
2134 h->root.u.def.value = h->weakdef->root.u.def.value;
2135 return true;
2138 /* This is a reference to a symbol defined by a dynamic object which
2139 is not a function. */
2141 /* If we are creating a shared library, we must presume that the
2142 only references to the symbol are via the global offset table.
2143 For such cases we need not do anything here; the relocations will
2144 be handled correctly by relocate_section. */
2145 if (info->shared)
2146 return true;
2148 /* If there are no references to this symbol that do not use the
2149 GOT, we don't need to generate a copy reloc. */
2150 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2151 return true;
2153 /* We must allocate the symbol in our .dynbss section, which will
2154 become part of the .bss section of the executable. There will be
2155 an entry for this symbol in the .dynsym section. The dynamic
2156 object will contain position independent code, so all references
2157 from the dynamic object to this symbol will go through the global
2158 offset table. The dynamic linker will use the .dynsym entry to
2159 determine the address it must put in the global offset table, so
2160 both the dynamic object and the regular object will refer to the
2161 same memory location for the variable. */
2163 s = bfd_get_section_by_name (dynobj, ".dynbss");
2164 BFD_ASSERT (s != NULL);
2166 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2167 copy the initial value out of the dynamic object and into the
2168 runtime process image. We need to remember the offset into the
2169 .rela.bss section we are going to use. */
2170 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2172 asection *srel;
2174 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2175 BFD_ASSERT (srel != NULL);
2176 srel->_raw_size += sizeof (Elf32_External_Rela);
2177 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2180 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2181 thing to copy; so do we. */
2183 /* We need to figure out the alignment required for this symbol. I
2184 have no idea how ELF linkers handle this. */
2185 power_of_two = bfd_log2 (h->size);
2186 if (power_of_two > 3)
2187 power_of_two = 3;
2189 /* Apply the required alignment. */
2190 s->_raw_size = BFD_ALIGN (s->_raw_size,
2191 (bfd_size_type) (1 << power_of_two));
2192 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2194 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2195 return false;
2198 /* Define the symbol as being at this point in the section. */
2199 h->root.u.def.section = s;
2200 h->root.u.def.value = s->_raw_size;
2202 /* Increment the section size to make room for the symbol. */
2203 s->_raw_size += h->size;
2205 return true;
2208 /* Look through the relocs for a section during the first phase. */
2210 static boolean
2211 cris_elf_check_relocs (abfd, info, sec, relocs)
2212 bfd *abfd;
2213 struct bfd_link_info *info;
2214 asection *sec;
2215 const Elf_Internal_Rela *relocs;
2217 bfd *dynobj;
2218 Elf_Internal_Shdr *symtab_hdr;
2219 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2220 bfd_signed_vma *local_got_refcounts;
2221 const Elf_Internal_Rela *rel;
2222 const Elf_Internal_Rela *rel_end;
2223 asection *sgot;
2224 asection *srelgot;
2225 asection *sreloc;
2227 if (info->relocateable)
2228 return true;
2230 dynobj = elf_hash_table (info)->dynobj;
2231 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2232 sym_hashes = elf_sym_hashes (abfd);
2233 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2234 local_got_refcounts = elf_local_got_refcounts (abfd);
2236 sgot = NULL;
2237 srelgot = NULL;
2238 sreloc = NULL;
2240 if (!elf_bad_symtab (abfd))
2241 sym_hashes_end -= symtab_hdr->sh_info;
2243 rel_end = relocs + sec->reloc_count;
2244 for (rel = relocs; rel < rel_end; rel++)
2246 struct elf_link_hash_entry *h;
2247 unsigned long r_symndx;
2248 enum elf_cris_reloc_type r_type;
2250 r_symndx = ELF32_R_SYM (rel->r_info);
2251 if (r_symndx < symtab_hdr->sh_info)
2252 h = NULL;
2253 else
2254 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2256 r_type = ELF32_R_TYPE (rel->r_info);
2258 /* Some relocs require linker-created sections; we need to hang them
2259 on the first input bfd we found that contained dynamic relocs. */
2260 switch (r_type)
2262 case R_CRIS_16_GOT:
2263 case R_CRIS_32_GOT:
2264 case R_CRIS_32_GOTREL:
2265 case R_CRIS_32_PLT_GOTREL:
2266 case R_CRIS_32_PLT_PCREL:
2267 case R_CRIS_16_GOTPLT:
2268 case R_CRIS_32_GOTPLT:
2269 if (dynobj == NULL)
2271 elf_hash_table (info)->dynobj = dynobj = abfd;
2273 /* Create the .got section, so we can assume it's always
2274 present whenever there's a dynobj. */
2275 if (!_bfd_elf_create_got_section (dynobj, info))
2276 return false;
2278 break;
2280 default:
2281 break;
2284 /* Some relocs require a global offset table (but perhaps not a
2285 specific GOT entry). */
2286 switch (ELF32_R_TYPE (rel->r_info))
2288 case R_CRIS_16_GOT:
2289 case R_CRIS_32_GOT:
2290 case R_CRIS_32_GOTREL:
2291 case R_CRIS_32_PLT_GOTREL:
2292 if (sgot == NULL)
2293 sgot = bfd_get_section_by_name (dynobj, ".got");
2295 if (local_got_refcounts == NULL)
2297 size_t size;
2299 /* We use index local_got_refcounts[-1] to count all
2300 GOT-relative relocations that do not have explicit
2301 GOT entries. */
2302 size = (symtab_hdr->sh_info + 1) * sizeof (bfd_signed_vma);
2303 local_got_refcounts = ((bfd_signed_vma *)
2304 bfd_alloc (abfd, size));
2305 if (local_got_refcounts == NULL)
2306 return false;
2307 memset (local_got_refcounts, -1, size);
2309 local_got_refcounts++;
2310 elf_local_got_refcounts (abfd) = local_got_refcounts;
2312 break;
2314 default:
2315 break;
2318 switch (ELF32_R_TYPE (rel->r_info))
2320 case R_CRIS_16_GOTPLT:
2321 case R_CRIS_32_GOTPLT:
2322 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2323 entry) is eliminated. We can only do this for a non-local
2324 symbol. */
2325 if (h != NULL)
2327 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2328 goto handle_gotplt_reloc;
2330 /* If h is NULL then this is a local symbol, and we must make a
2331 GOT entry for it, so handle it like a GOT reloc. */
2332 /* Fall through. */
2334 case R_CRIS_16_GOT:
2335 case R_CRIS_32_GOT:
2336 /* This symbol requires a global offset table entry. */
2338 if (srelgot == NULL
2339 && (h != NULL || info->shared))
2341 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2342 if (srelgot == NULL)
2344 srelgot = bfd_make_section (dynobj, ".rela.got");
2345 if (srelgot == NULL
2346 || !bfd_set_section_flags (dynobj, srelgot,
2347 (SEC_ALLOC
2348 | SEC_LOAD
2349 | SEC_HAS_CONTENTS
2350 | SEC_IN_MEMORY
2351 | SEC_LINKER_CREATED
2352 | SEC_READONLY))
2353 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2354 return false;
2358 if (h != NULL)
2360 if (h->got.refcount == -1)
2362 h->got.refcount = 1;
2364 /* Make sure this symbol is output as a dynamic symbol. */
2365 if (h->dynindx == -1)
2367 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2368 return false;
2371 /* Allocate space in the .got section. */
2372 sgot->_raw_size += 4;
2373 /* Allocate relocation space. */
2374 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2376 else
2377 h->got.refcount++;
2379 else
2381 /* This is a global offset table entry for a local symbol. */
2382 if (local_got_refcounts[r_symndx] == -1)
2384 local_got_refcounts[r_symndx] = 1;
2386 sgot->_raw_size += 4;
2387 if (info->shared)
2389 /* If we are generating a shared object, we need to
2390 output a R_CRIS_RELATIVE reloc so that the dynamic
2391 linker can adjust this GOT entry. */
2392 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2395 else
2396 local_got_refcounts[r_symndx]++;
2398 break;
2400 case R_CRIS_32_GOTREL:
2401 /* This reference requires a global offset table.
2402 FIXME: The actual refcount isn't used currently; the .got
2403 section can't be removed if there were any references in the
2404 input. */
2405 local_got_refcounts[-1]++;
2406 break;
2408 handle_gotplt_reloc:
2410 case R_CRIS_32_PLT_GOTREL:
2411 /* This reference requires a global offset table. */
2412 local_got_refcounts[-1]++;
2413 /* Fall through. */
2415 case R_CRIS_32_PLT_PCREL:
2416 /* This symbol requires a procedure linkage table entry. We
2417 actually build the entry in adjust_dynamic_symbol,
2418 because this might be a case of linking PIC code which is
2419 never referenced by a dynamic object, in which case we
2420 don't need to generate a procedure linkage table entry
2421 after all. */
2423 /* If this is a local symbol, we resolve it directly without
2424 creating a procedure linkage table entry. */
2425 if (h == NULL)
2426 continue;
2428 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2429 if (h->plt.refcount == -1)
2430 h->plt.refcount = 1;
2431 else
2432 h->plt.refcount++;
2433 break;
2435 case R_CRIS_8:
2436 case R_CRIS_16:
2437 case R_CRIS_32:
2438 /* Let's help debug shared library creation. Any of these
2439 relocs can be used in shared libs, but pages containing them
2440 cannot be shared. Don't warn for sections we don't care
2441 about, such as debug sections or non-constant sections. We
2442 can't help tables of (global) function pointers, for example,
2443 though they must be emitted in a data section to avoid having
2444 impure text sections. */
2445 if (info->shared
2446 && (sec->flags & SEC_ALLOC) != 0
2447 && (sec->flags & SEC_READONLY) != 0)
2449 /* FIXME: How do we make this optionally a warning only? */
2450 if (abfd->my_archive)
2451 (*_bfd_error_handler)
2452 (_("%s(%s), section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2453 bfd_get_filename (bfd_my_archive (abfd)),
2454 bfd_get_filename (abfd),
2455 sec->name,
2456 cris_elf_howto_table[ELF32_R_TYPE (rel->r_info)].name);
2457 else
2458 (*_bfd_error_handler)
2459 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2460 bfd_get_filename (abfd),
2461 sec->name,
2462 cris_elf_howto_table[ELF32_R_TYPE (rel->r_info)].name);
2464 /* Fall through. */
2466 case R_CRIS_8_PCREL:
2467 case R_CRIS_16_PCREL:
2468 case R_CRIS_32_PCREL:
2469 if (h != NULL)
2471 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2473 /* Make sure a plt entry is created for this symbol if it
2474 turns out to be a function defined by a dynamic object. */
2475 if (h->plt.refcount == -1)
2476 h->plt.refcount = 1;
2477 else
2478 h->plt.refcount++;
2481 /* If we are creating a shared library and this is not a local
2482 symbol, we need to copy the reloc into the shared library.
2483 However when linking with -Bsymbolic and this is a global
2484 symbol which is defined in an object we are including in the
2485 link (i.e., DEF_REGULAR is set), then we can resolve the
2486 reloc directly. At this point we have not seen all the input
2487 files, so it is possible that DEF_REGULAR is not set now but
2488 will be set later (it is never cleared). In case of a weak
2489 definition, DEF_REGULAR may be cleared later by a strong
2490 definition in a shared library. We account for that
2491 possibility below by storing information in the relocs_copied
2492 field of the hash table entry. A similar situation occurs
2493 when creating shared libraries and symbol visibility changes
2494 render the symbol local. */
2496 /* No need to do anything if we're not creating a shared object. */
2497 if (! info->shared)
2498 break;
2500 /* We don't need to handle relocs into sections not going into
2501 the "real" output. */
2502 if ((sec->flags & SEC_ALLOC) == 0)
2503 break;
2505 /* We can only eliminate PC-relative relocs. */
2506 if (r_type == R_CRIS_8_PCREL
2507 || r_type == R_CRIS_16_PCREL
2508 || r_type == R_CRIS_32_PCREL)
2510 /* If the symbol is local, then we can eliminate the reloc. */
2511 if (h == NULL)
2512 break;
2514 /* If this is with -Bsymbolic and the symbol isn't weak, and
2515 is defined by an ordinary object (the ones we include in
2516 this shared library) then we can also eliminate the
2517 reloc. See comment above for more eliminable cases which
2518 we can't identify at this time. */
2519 if (info->symbolic
2520 && h->root.type != bfd_link_hash_defweak
2521 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2522 break;
2525 /* We create a reloc section in dynobj and make room for this
2526 reloc. */
2527 if (sreloc == NULL)
2529 const char *name;
2531 name = (bfd_elf_string_from_elf_section
2532 (abfd,
2533 elf_elfheader (abfd)->e_shstrndx,
2534 elf_section_data (sec)->rel_hdr.sh_name));
2535 if (name == NULL)
2536 return false;
2538 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2539 && strcmp (bfd_get_section_name (abfd, sec),
2540 name + 5) == 0);
2542 sreloc = bfd_get_section_by_name (dynobj, name);
2543 if (sreloc == NULL)
2545 sreloc = bfd_make_section (dynobj, name);
2546 if (sreloc == NULL
2547 || !bfd_set_section_flags (dynobj, sreloc,
2548 (SEC_ALLOC
2549 | SEC_LOAD
2550 | SEC_HAS_CONTENTS
2551 | SEC_IN_MEMORY
2552 | SEC_LINKER_CREATED
2553 | SEC_READONLY))
2554 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2555 return false;
2559 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2561 /* If we are linking with -Bsymbolic, we count the number of PC
2562 relative relocations we have entered for this symbol, so that
2563 we can discard them again if the symbol is later defined by a
2564 regular object. We know that h is really a pointer to an
2565 elf_cris_link_hash_entry. */
2566 if ((ELF32_R_TYPE (rel->r_info) == R_CRIS_8_PCREL
2567 || ELF32_R_TYPE (rel->r_info) == R_CRIS_16_PCREL
2568 || ELF32_R_TYPE (rel->r_info) == R_CRIS_32_PCREL)
2569 && info->symbolic)
2571 struct elf_cris_link_hash_entry *eh;
2572 struct elf_cris_pcrel_relocs_copied *p;
2574 eh = (struct elf_cris_link_hash_entry *) h;
2576 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2577 if (p->section == sreloc)
2578 break;
2580 if (p == NULL)
2582 p = ((struct elf_cris_pcrel_relocs_copied *)
2583 bfd_alloc (dynobj, sizeof *p));
2584 if (p == NULL)
2585 return false;
2586 p->next = eh->pcrel_relocs_copied;
2587 eh->pcrel_relocs_copied = p;
2588 p->section = sreloc;
2589 p->count = 0;
2592 ++p->count;
2594 break;
2596 /* This relocation describes the C++ object vtable hierarchy.
2597 Reconstruct it for later use during GC. */
2598 case R_CRIS_GNU_VTINHERIT:
2599 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2600 return false;
2601 break;
2603 /* This relocation describes which C++ vtable entries are actually
2604 used. Record for later use during GC. */
2605 case R_CRIS_GNU_VTENTRY:
2606 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2607 return false;
2608 break;
2612 return true;
2615 /* Set the sizes of the dynamic sections. */
2617 static boolean
2618 elf_cris_size_dynamic_sections (output_bfd, info)
2619 bfd *output_bfd;
2620 struct bfd_link_info *info;
2622 bfd *dynobj;
2623 asection *s;
2624 boolean plt;
2625 boolean relocs;
2626 boolean reltext;
2628 dynobj = elf_hash_table (info)->dynobj;
2629 BFD_ASSERT (dynobj != NULL);
2631 if (elf_hash_table (info)->dynamic_sections_created)
2633 /* Set the contents of the .interp section to the interpreter. */
2634 if (!info->shared)
2636 s = bfd_get_section_by_name (dynobj, ".interp");
2637 BFD_ASSERT (s != NULL);
2638 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2639 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2642 else
2644 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2645 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2646 elf_cris_adjust_gotplt_to_got,
2647 (PTR) info);
2649 /* We may have created entries in the .rela.got section.
2650 However, if we are not creating the dynamic sections, we will
2651 not actually use these entries. Reset the size of .rela.got,
2652 which will cause it to get stripped from the output file
2653 below. */
2654 s = bfd_get_section_by_name (dynobj, ".rela.got");
2655 if (s != NULL)
2656 s->_raw_size = 0;
2659 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2660 relative relocs against symbols defined in a regular object. We
2661 allocated space for them in the check_relocs routine, but we will not
2662 fill them in in the relocate_section routine. We also discard space
2663 for relocs that have become for local symbols due to symbol
2664 visibility changes. */
2665 if (info->shared)
2666 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2667 elf_cris_discard_copies,
2668 (PTR) info);
2670 /* The check_relocs and adjust_dynamic_symbol entry points have
2671 determined the sizes of the various dynamic sections. Allocate
2672 memory for them. */
2673 plt = false;
2674 relocs = false;
2675 reltext = false;
2676 for (s = dynobj->sections; s != NULL; s = s->next)
2678 const char *name;
2679 boolean strip;
2681 if ((s->flags & SEC_LINKER_CREATED) == 0)
2682 continue;
2684 /* It's OK to base decisions on the section name, because none
2685 of the dynobj section names depend upon the input files. */
2686 name = bfd_get_section_name (dynobj, s);
2688 strip = false;
2690 if (strcmp (name, ".plt") == 0)
2692 if (s->_raw_size == 0)
2694 /* Strip this section if we don't need it; see the
2695 comment below. */
2696 strip = true;
2698 else
2700 /* Remember whether there is a PLT. */
2701 plt = true;
2704 else if (strncmp (name, ".rela", 5) == 0)
2706 if (s->_raw_size == 0)
2708 /* If we don't need this section, strip it from the
2709 output file. This is mostly to handle .rela.bss and
2710 .rela.plt. We must create both sections in
2711 create_dynamic_sections, because they must be created
2712 before the linker maps input sections to output
2713 sections. The linker does that before
2714 adjust_dynamic_symbol is called, and it is that
2715 function which decides whether anything needs to go
2716 into these sections. */
2717 strip = true;
2719 else
2721 asection *target;
2723 /* Remember whether there are any reloc sections other
2724 than .rela.plt. */
2725 if (strcmp (name, ".rela.plt") != 0)
2727 const char *outname;
2729 relocs = true;
2731 /* If this relocation section applies to a read only
2732 section, then we probably need a DT_TEXTREL entry.
2733 The entries in the .rela.plt section are actually
2734 associated with .got.plt, which we created ourselves
2735 and so know is not readonly. */
2736 outname = bfd_get_section_name (output_bfd,
2737 s->output_section);
2738 target
2739 = bfd_get_section_by_name (output_bfd,
2740 outname + strlen (".rela"));
2742 /* We have to test the .text section by name, becase for
2743 some reason it does not have SEC_READONLY set at this
2744 time. That flag is actually set in ldmain.c:main
2745 specifically for ".text" at a time long after this
2746 function is called. FIXME: This might be due to a
2747 general bug. FIXME: Have testcase for this. */
2748 if (target != NULL
2749 && (target->flags & SEC_ALLOC) != 0
2750 && ((target->flags & SEC_READONLY) != 0
2751 || strcmp (outname + strlen (".rela"),
2752 ".text") == 0))
2753 reltext = true;
2756 /* We use the reloc_count field as a counter if we need
2757 to copy relocs into the output file. */
2758 s->reloc_count = 0;
2761 else if (strncmp (name, ".got", 4) != 0)
2763 /* It's not one of our sections, so don't allocate space. */
2764 continue;
2767 if (strip)
2769 _bfd_strip_section_from_output (info, s);
2770 continue;
2773 /* Allocate memory for the section contents. We use bfd_zalloc here
2774 in case unused entries are not reclaimed before the section's
2775 contents are written out. This should not happen, but this way
2776 if it does, we will not write out garbage. For reloc sections,
2777 this will make entries have the type R_CRIS_NONE. */
2778 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2779 if (s->contents == NULL && s->_raw_size != 0)
2780 return false;
2783 if (elf_hash_table (info)->dynamic_sections_created)
2785 /* Add some entries to the .dynamic section. We fill in the
2786 values later, in elf_cris_finish_dynamic_sections, but we
2787 must add the entries now so that we get the correct size for
2788 the .dynamic section. The DT_DEBUG entry is filled in by the
2789 dynamic linker and used by the debugger. */
2790 if (!info->shared)
2792 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2793 return false;
2796 if (plt)
2798 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2799 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2800 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2801 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2802 return false;
2805 if (relocs)
2807 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2808 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2809 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2810 sizeof (Elf32_External_Rela)))
2811 return false;
2814 if (reltext)
2816 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2817 return false;
2818 info->flags |= DF_TEXTREL;
2822 return true;
2825 /* This function is called via elf_cris_link_hash_traverse if we are
2826 creating a shared object. In the -Bsymbolic case, it discards the
2827 space allocated to copy PC relative relocs against symbols which
2828 are defined in regular objects. For the normal non-symbolic case,
2829 we also discard space for relocs that have become local due to
2830 symbol visibility changes. We allocated space for them in the
2831 check_relocs routine, but we won't fill them in in the
2832 relocate_section routine. */
2834 static boolean
2835 elf_cris_discard_copies (h, inf)
2836 struct elf_cris_link_hash_entry *h;
2837 PTR inf;
2839 struct elf_cris_pcrel_relocs_copied *s;
2840 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2842 /* If a symbol has been forced local or we have found a regular
2843 definition for the symbolic link case, then we won't be needing
2844 any relocs. */
2845 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2846 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2847 || info->symbolic))
2849 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2850 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rel);
2853 return true;
2856 /* Reject a file depending on presence and expectation of prefixed
2857 underscores on symbols. */
2859 static boolean
2860 cris_elf_object_p (abfd)
2861 bfd *abfd;
2863 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2864 return (bfd_get_symbol_leading_char (abfd) == '_');
2865 else
2866 return (bfd_get_symbol_leading_char (abfd) == 0);
2869 /* Mark presence or absence of leading underscore. */
2871 static void
2872 cris_elf_final_write_processing (abfd, linker)
2873 bfd *abfd;
2874 boolean linker ATTRIBUTE_UNUSED;
2876 if (bfd_get_symbol_leading_char (abfd) == '_')
2877 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2878 else
2879 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2882 /* Display the flags field. */
2884 static boolean
2885 cris_elf_print_private_bfd_data (abfd, ptr)
2886 bfd *abfd;
2887 PTR ptr;
2889 FILE *file = (FILE *) ptr;
2891 BFD_ASSERT (abfd != NULL && ptr != NULL)
2893 _bfd_elf_print_private_bfd_data (abfd, ptr);
2895 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2897 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2898 fprintf (file, _(" [symbols have a _ prefix]"));
2900 fputc ('\n', file);
2901 return true;
2904 /* Don't mix files with and without a leading underscore. */
2906 static boolean
2907 cris_elf_merge_private_bfd_data (ibfd, obfd)
2908 bfd *ibfd;
2909 bfd *obfd;
2911 flagword old_flags, new_flags;
2913 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2914 return false;
2916 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2917 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2918 return true;
2920 if (! elf_flags_init (obfd))
2922 /* This happens when ld starts out with a 'blank' output file. */
2923 elf_flags_init (obfd) = true;
2925 /* Set flags according to current bfd_target. */
2926 cris_elf_final_write_processing (obfd, false);
2929 old_flags = elf_elfheader (obfd)->e_flags;
2930 new_flags = elf_elfheader (ibfd)->e_flags;
2932 /* Is this good or bad? We'll follow with other excluding flags. */
2933 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
2935 (*_bfd_error_handler)
2936 ((new_flags & EF_CRIS_UNDERSCORE)
2937 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
2938 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
2939 bfd_get_filename (ibfd));
2940 bfd_set_error (bfd_error_bad_value);
2941 return false;
2944 return true;
2947 #define ELF_ARCH bfd_arch_cris
2948 #define ELF_MACHINE_CODE EM_CRIS
2949 #define ELF_MAXPAGESIZE 0x2000
2951 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
2952 #define TARGET_LITTLE_NAME "elf32-cris"
2953 #define elf_symbol_leading_char 0
2955 #define elf_info_to_howto_rel NULL
2956 #define elf_info_to_howto cris_info_to_howto_rela
2957 #define elf_backend_relocate_section cris_elf_relocate_section
2958 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
2959 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
2960 #define elf_backend_check_relocs cris_elf_check_relocs
2962 #define elf_backend_can_gc_sections 1
2964 #define elf_backend_object_p cris_elf_object_p
2965 #define elf_backend_final_write_processing \
2966 cris_elf_final_write_processing
2967 #define bfd_elf32_bfd_print_private_bfd_data \
2968 cris_elf_print_private_bfd_data
2969 #define bfd_elf32_bfd_merge_private_bfd_data \
2970 cris_elf_merge_private_bfd_data
2972 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
2974 #define bfd_elf32_bfd_link_hash_table_create \
2975 elf_cris_link_hash_table_create
2976 #define elf_backend_adjust_dynamic_symbol \
2977 elf_cris_adjust_dynamic_symbol
2978 #define elf_backend_size_dynamic_sections \
2979 elf_cris_size_dynamic_sections
2980 #define elf_backend_finish_dynamic_symbol \
2981 elf_cris_finish_dynamic_symbol
2982 #define elf_backend_finish_dynamic_sections \
2983 elf_cris_finish_dynamic_sections
2984 #define elf_backend_create_dynamic_sections \
2985 _bfd_elf_create_dynamic_sections
2986 #define bfd_elf32_bfd_final_link \
2987 _bfd_elf32_gc_common_final_link
2988 #define elf_backend_hide_symbol elf_cris_hide_symbol
2990 #define elf_backend_want_got_plt 1
2991 #define elf_backend_plt_readonly 1
2992 #define elf_backend_want_plt_sym 0
2993 #define elf_backend_got_header_size 12
2994 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2996 /* Later, we my want to optimize RELA entries into REL entries for dynamic
2997 linking and libraries (if it's a win of any significance). Until then,
2998 take the easy route. */
2999 #define elf_backend_may_use_rel_p 0
3000 #define elf_backend_may_use_rela_p 1
3002 #include "elf32-target.h"
3004 #define INCLUDED_TARGET_FILE
3006 #undef TARGET_LITTLE_SYM
3007 #undef TARGET_LITTLE_NAME
3008 #undef elf_symbol_leading_char
3010 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3011 #define TARGET_LITTLE_NAME "elf32-us-cris"
3012 #define elf_symbol_leading_char '_'
3014 #include "elf32-target.h"