* elf-bfd.h: Add prototypes for _bfd_elfcore_make_pseudosection
[binutils.git] / bfd / elf32-cris.c
bloba679c4fd465f70b6680303062f01dbf460bbe153
1 /* CRIS-specific support for 32-bit ELF.
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Axis Communications AB.
4 Written by Hans-Peter Nilsson, based on elf32-fr30.c
5 PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/cris.h"
29 /* Forward declarations. */
30 static reloc_howto_type * cris_reloc_type_lookup
31 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
33 static void cris_info_to_howto_rela
34 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
36 static boolean cris_elf_relocate_section
37 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
38 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
40 static bfd_reloc_status_type cris_final_link_relocate
41 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
42 Elf_Internal_Rela *, bfd_vma));
44 static boolean cris_elf_gc_sweep_hook
45 PARAMS ((bfd *, struct bfd_link_info *, asection *,
46 const Elf_Internal_Rela *));
48 static asection * cris_elf_gc_mark_hook
49 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
50 struct elf_link_hash_entry *, Elf_Internal_Sym *));
52 static boolean cris_elf_object_p PARAMS ((bfd *));
54 static void cris_elf_final_write_processing PARAMS ((bfd *, boolean));
56 static boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
58 static boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
60 struct elf_cris_link_hash_entry;
61 static boolean elf_cris_discard_excess_dso_dynamics
62 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
63 static boolean elf_cris_discard_excess_program_dynamics
64 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
65 static boolean elf_cris_adjust_gotplt_to_got
66 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
67 static boolean elf_cris_try_fold_plt_to_got
68 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
70 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
71 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
72 PARAMS ((bfd *));
73 static boolean elf_cris_adjust_dynamic_symbol
74 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static boolean elf_cris_size_dynamic_sections
76 PARAMS ((bfd *, struct bfd_link_info *));
77 static boolean elf_cris_finish_dynamic_symbol
78 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
79 Elf_Internal_Sym *));
80 static boolean elf_cris_finish_dynamic_sections
81 PARAMS ((bfd *, struct bfd_link_info *));
82 static void elf_cris_hide_symbol
83 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
85 static reloc_howto_type cris_elf_howto_table [] =
87 /* This reloc does nothing. */
88 HOWTO (R_CRIS_NONE, /* type */
89 0, /* rightshift */
90 2, /* size (0 = byte, 1 = short, 2 = long) */
91 32, /* bitsize */
92 false, /* pc_relative */
93 0, /* bitpos */
94 complain_overflow_bitfield, /* complain_on_overflow */
95 bfd_elf_generic_reloc, /* special_function */
96 "R_CRIS_NONE", /* name */
97 false, /* partial_inplace */
98 0, /* src_mask */
99 0, /* dst_mask */
100 false), /* pcrel_offset */
102 /* An 8 bit absolute relocation. */
103 HOWTO (R_CRIS_8, /* type */
104 0, /* rightshift */
105 0, /* size (0 = byte, 1 = short, 2 = long) */
106 8, /* bitsize */
107 false, /* pc_relative */
108 0, /* bitpos */
109 complain_overflow_bitfield, /* complain_on_overflow */
110 bfd_elf_generic_reloc, /* special_function */
111 "R_CRIS_8", /* name */
112 false, /* partial_inplace */
113 0x0000, /* src_mask */
114 0x00ff, /* dst_mask */
115 false), /* pcrel_offset */
117 /* A 16 bit absolute relocation. */
118 HOWTO (R_CRIS_16, /* type */
119 0, /* rightshift */
120 1, /* size (0 = byte, 1 = short, 2 = long) */
121 16, /* bitsize */
122 false, /* pc_relative */
123 0, /* bitpos */
124 complain_overflow_bitfield, /* complain_on_overflow */
125 bfd_elf_generic_reloc, /* special_function */
126 "R_CRIS_16", /* name */
127 false, /* partial_inplace */
128 0x00000000, /* src_mask */
129 0x0000ffff, /* dst_mask */
130 false), /* pcrel_offset */
132 /* A 32 bit absolute relocation. */
133 HOWTO (R_CRIS_32, /* type */
134 0, /* rightshift */
135 2, /* size (0 = byte, 1 = short, 2 = long) */
136 32, /* bitsize */
137 false, /* pc_relative */
138 0, /* bitpos */
139 complain_overflow_bitfield, /* complain_on_overflow */
140 bfd_elf_generic_reloc, /* special_function */
141 "R_CRIS_32", /* name */
142 false, /* partial_inplace */
143 0x00000000, /* src_mask */
144 0xffffffff, /* dst_mask */
145 false), /* pcrel_offset */
147 /* An 8 bit PC-relative relocation. */
148 HOWTO (R_CRIS_8_PCREL, /* type */
149 0, /* rightshift */
150 0, /* size (0 = byte, 1 = short, 2 = long) */
151 8, /* bitsize */
152 true, /* pc_relative */
153 0, /* bitpos */
154 complain_overflow_bitfield, /* complain_on_overflow */
155 bfd_elf_generic_reloc, /* special_function */
156 "R_CRIS_8_PCREL", /* name */
157 false, /* partial_inplace */
158 0x0000, /* src_mask */
159 0x00ff, /* dst_mask */
160 true), /* pcrel_offset */
162 /* A 16 bit PC-relative relocation. */
163 HOWTO (R_CRIS_16_PCREL, /* type */
164 0, /* rightshift */
165 1, /* size (0 = byte, 1 = short, 2 = long) */
166 16, /* bitsize */
167 true, /* pc_relative */
168 0, /* bitpos */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 bfd_elf_generic_reloc, /* special_function */
171 "R_CRIS_16", /* name */
172 false, /* partial_inplace */
173 0x00000000, /* src_mask */
174 0x0000ffff, /* dst_mask */
175 true), /* pcrel_offset */
177 /* A 32 bit PC-relative relocation. */
178 HOWTO (R_CRIS_32_PCREL, /* type */
179 0, /* rightshift */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
181 32, /* bitsize */
182 true, /* pc_relative */
183 0, /* bitpos */
184 complain_overflow_bitfield, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_CRIS_32", /* name */
187 false, /* partial_inplace */
188 0x00000000, /* src_mask */
189 0xffffffff, /* dst_mask */
190 true), /* pcrel_offset */
192 /* GNU extension to record C++ vtable hierarchy. */
193 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
194 0, /* rightshift */
195 2, /* size (0 = byte, 1 = short, 2 = long) */
196 0, /* bitsize */
197 false, /* pc_relative */
198 0, /* bitpos */
199 complain_overflow_dont, /* complain_on_overflow */
200 NULL, /* special_function */
201 "R_CRIS_GNU_VTINHERIT", /* name */
202 false, /* partial_inplace */
203 0, /* src_mask */
204 0, /* dst_mask */
205 false), /* pcrel_offset */
207 /* GNU extension to record C++ vtable member usage. */
208 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
209 0, /* rightshift */
210 2, /* size (0 = byte, 1 = short, 2 = long) */
211 0, /* bitsize */
212 false, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_dont, /* complain_on_overflow */
215 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
216 "R_CRIS_GNU_VTENTRY", /* name */
217 false, /* partial_inplace */
218 0, /* src_mask */
219 0, /* dst_mask */
220 false), /* pcrel_offset */
222 /* This is used only by the dynamic linker. The symbol should exist
223 both in the object being run and in some shared library. The
224 dynamic linker copies the data addressed by the symbol from the
225 shared library into the object, because the object being
226 run has to have the data at some particular address. */
227 HOWTO (R_CRIS_COPY, /* type */
228 0, /* rightshift */
229 2, /* size (0 = byte, 1 = short, 2 = long) */
230 32, /* bitsize */
231 false, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_bitfield, /* complain_on_overflow */
234 bfd_elf_generic_reloc, /* special_function */
235 "R_CRIS_COPY", /* name */
236 false, /* partial_inplace */
237 0, /* src_mask */
238 0, /* dst_mask */
239 false), /* pcrel_offset */
241 /* Like R_CRIS_32, but used when setting global offset table entries. */
242 HOWTO (R_CRIS_GLOB_DAT, /* type */
243 0, /* rightshift */
244 2, /* size (0 = byte, 1 = short, 2 = long) */
245 32, /* bitsize */
246 false, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_bitfield, /* complain_on_overflow */
249 bfd_elf_generic_reloc, /* special_function */
250 "R_CRIS_GLOB_DAT", /* name */
251 false, /* partial_inplace */
252 0, /* src_mask */
253 0xffffffff, /* dst_mask */
254 false), /* pcrel_offset */
256 /* Marks a procedure linkage table entry for a symbol. */
257 HOWTO (R_CRIS_JUMP_SLOT, /* type */
258 0, /* rightshift */
259 2, /* size (0 = byte, 1 = short, 2 = long) */
260 32, /* bitsize */
261 false, /* pc_relative */
262 0, /* bitpos */
263 complain_overflow_bitfield, /* complain_on_overflow */
264 bfd_elf_generic_reloc, /* special_function */
265 "R_CRIS_JUMP_SLOT", /* name */
266 false, /* partial_inplace */
267 0, /* src_mask */
268 0, /* dst_mask */
269 false), /* pcrel_offset */
271 /* Used only by the dynamic linker. When the object is run, this
272 longword is set to the load address of the object, plus the
273 addend. */
274 HOWTO (R_CRIS_RELATIVE, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 false, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield, /* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_CRIS_RELATIVE", /* name */
283 false, /* partial_inplace */
284 0, /* src_mask */
285 0xffffffff, /* dst_mask */
286 false), /* pcrel_offset */
288 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
289 HOWTO (R_CRIS_16_GOT, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 false, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_bitfield, /* complain_on_overflow */
296 bfd_elf_generic_reloc, /* special_function */
297 "R_CRIS_16_GOT", /* name */
298 false, /* partial_inplace */
299 0, /* src_mask */
300 0xffff, /* dst_mask */
301 false), /* pcrel_offset */
303 HOWTO (R_CRIS_32_GOT, /* type */
304 0, /* rightshift */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
306 32, /* bitsize */
307 false, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_bitfield, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_CRIS_32_GOT", /* name */
312 false, /* partial_inplace */
313 0, /* src_mask */
314 0xffffffff, /* dst_mask */
315 false), /* pcrel_offset */
317 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
318 the GOT table for the symbol. */
319 HOWTO (R_CRIS_16_GOTPLT, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
323 false, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_CRIS_16_GOTPLT", /* name */
328 false, /* partial_inplace */
329 0, /* src_mask */
330 0xffff, /* dst_mask */
331 false), /* pcrel_offset */
333 HOWTO (R_CRIS_32_GOTPLT, /* type */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 32, /* bitsize */
337 false, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_CRIS_32_GOTPLT", /* name */
342 false, /* partial_inplace */
343 0, /* src_mask */
344 0xffffffff, /* dst_mask */
345 false), /* pcrel_offset */
347 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
348 be necessary. */
349 HOWTO (R_CRIS_32_GOTREL, /* type */
350 0, /* rightshift */
351 2, /* size (0 = byte, 1 = short, 2 = long) */
352 32, /* bitsize */
353 false, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_bitfield, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_CRIS_32_GOTREL", /* name */
358 false, /* partial_inplace */
359 0, /* src_mask */
360 0xffffffff, /* dst_mask */
361 false), /* pcrel_offset */
363 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
364 to create PLT entry for symbol. */
365 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
366 0, /* rightshift */
367 2, /* size (0 = byte, 1 = short, 2 = long) */
368 32, /* bitsize */
369 false, /* pc_relative */
370 0, /* bitpos */
371 complain_overflow_bitfield, /* complain_on_overflow */
372 bfd_elf_generic_reloc, /* special_function */
373 "R_CRIS_32_PLT_GOTREL", /* name */
374 false, /* partial_inplace */
375 0, /* src_mask */
376 0xffffffff, /* dst_mask */
377 false), /* pcrel_offset */
379 /* A 32-bit offset from PC (location after the relocation) + addend to
380 entry for this symbol in PLT and request to create PLT entry for
381 symbol. */
382 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
383 0, /* rightshift */
384 2, /* size (0 = byte, 1 = short, 2 = long) */
385 32, /* bitsize */
386 true, /* pc_relative */
387 0, /* bitpos */
388 complain_overflow_bitfield, /* complain_on_overflow */
389 bfd_elf_generic_reloc, /* special_function */
390 "R_CRIS_32_PLT_PCREL", /* name */
391 false, /* partial_inplace */
392 0, /* src_mask */
393 0xffffffff, /* dst_mask */
394 true) /* pcrel_offset */
397 /* Map BFD reloc types to CRIS ELF reloc types. */
399 struct cris_reloc_map
401 bfd_reloc_code_real_type bfd_reloc_val;
402 unsigned int cris_reloc_val;
405 static const struct cris_reloc_map cris_reloc_map [] =
407 { BFD_RELOC_NONE, R_CRIS_NONE },
408 { BFD_RELOC_8, R_CRIS_8 },
409 { BFD_RELOC_16, R_CRIS_16 },
410 { BFD_RELOC_32, R_CRIS_32 },
411 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
412 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
413 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
414 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
415 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
416 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
417 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
418 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
419 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
420 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
421 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
422 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
423 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
424 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
425 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
426 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
429 static reloc_howto_type *
430 cris_reloc_type_lookup (abfd, code)
431 bfd * abfd ATTRIBUTE_UNUSED;
432 bfd_reloc_code_real_type code;
434 unsigned int i;
436 for (i = sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]);
437 --i;)
438 if (cris_reloc_map [i].bfd_reloc_val == code)
439 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
441 return NULL;
444 /* Set the howto pointer for an CRIS ELF reloc. */
446 static void
447 cris_info_to_howto_rela (abfd, cache_ptr, dst)
448 bfd * abfd ATTRIBUTE_UNUSED;
449 arelent * cache_ptr;
450 Elf32_Internal_Rela * dst;
452 unsigned int r_type;
454 r_type = ELF32_R_TYPE (dst->r_info);
455 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
456 cache_ptr->howto = & cris_elf_howto_table [r_type];
459 /* The name of the dynamic interpreter. This is put in the .interp
460 section. */
462 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
464 /* The size in bytes of an entry in the procedure linkage table. */
466 #define PLT_ENTRY_SIZE 20
468 /* The first entry in an absolute procedure linkage table looks like this. */
470 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
472 0xfc, 0xe1,
473 0x7e, 0x7e, /* push mof. */
474 0x7f, 0x0d, /* (dip [pc+]) */
475 0, 0, 0, 0, /* Replaced with address of .got + 4. */
476 0x30, 0x7a, /* move [...],mof */
477 0x7f, 0x0d, /* (dip [pc+]) */
478 0, 0, 0, 0, /* Replaced with address of .got + 8. */
479 0x30, 0x09, /* jump [...] */
482 /* Subsequent entries in an absolute procedure linkage table look like
483 this. */
485 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
487 0x7f, 0x0d, /* (dip [pc+]) */
488 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
489 0x30, 0x09, /* jump [...] */
490 0x3f, 0x7e, /* move [pc+],mof */
491 0, 0, 0, 0, /* Replaced with offset into relocation table. */
492 0x2f, 0xfe, /* add.d [pc+],pc */
493 0xec, 0xff,
494 0xff, 0xff /* Replaced with offset to start of .plt. */
497 /* The first entry in a PIC procedure linkage table looks like this. */
499 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
501 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
502 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
503 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
504 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
507 /* Subsequent entries in a PIC procedure linkage table look like this. */
509 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
511 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
512 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
513 0x30, 0x09, /* jump [...] */
514 0x3f, 0x7e, /* move [pc+],mof */
515 0, 0, 0, 0, /* Replaced with offset into relocation table. */
516 0x2f, 0xfe, /* add.d [pc+],pc */
517 0xec, 0xff, /* Replaced with offset to start of .plt. */
518 0xff, 0xff
521 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
522 (and most other PIC/shlib stuff). Check that we don't drift away
523 without reason.
525 The CRIS linker, like the m68k and i386 linkers (and probably the rest
526 too) needs to keep track of the number of relocs that it decides to
527 copy in check_relocs for each symbol. This is so that it can discard
528 PC relative relocs if it doesn't need them when linking with
529 -Bsymbolic. We store the information in a field extending the regular
530 ELF linker hash table. */
532 /* This structure keeps track of the number of PC relative relocs we have
533 copied for a given symbol. */
535 struct elf_cris_pcrel_relocs_copied
537 /* Next section. */
538 struct elf_cris_pcrel_relocs_copied *next;
539 /* A section in dynobj. */
540 asection *section;
541 /* Number of relocs copied in this section. */
542 bfd_size_type count;
545 /* CRIS ELF linker hash entry. */
547 struct elf_cris_link_hash_entry
549 struct elf_link_hash_entry root;
551 /* Number of PC relative relocs copied for this symbol. */
552 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
554 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
555 both a general GOT and a PLT-specific GOT entry for the same symbol,
556 when it is referenced both as a function and as a function pointer.
558 Number of GOTPLT references for a function. */
559 bfd_signed_vma gotplt_refcount;
561 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
562 (zero is never used as an index). FIXME: We should be able to fold
563 this with gotplt_refcount in a union, like the got and plt unions in
564 elf_link_hash_entry. */
565 bfd_size_type gotplt_offset;
568 /* CRIS ELF linker hash table. */
570 struct elf_cris_link_hash_table
572 struct elf_link_hash_table root;
574 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
575 since we try and avoid creating GOTPLT:s when there's already a GOT.
576 Instead, we keep and update the next available index here. */
577 bfd_size_type next_gotplt_entry;
580 /* Traverse a CRIS ELF linker hash table. */
582 #define elf_cris_link_hash_traverse(table, func, info) \
583 (elf_link_hash_traverse \
584 (&(table)->root, \
585 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
586 (info)))
588 /* Get the CRIS ELF linker hash table from a link_info structure. */
590 #define elf_cris_hash_table(p) \
591 ((struct elf_cris_link_hash_table *) (p)->hash)
593 /* Create an entry in a CRIS ELF linker hash table. */
595 static struct bfd_hash_entry *
596 elf_cris_link_hash_newfunc (entry, table, string)
597 struct bfd_hash_entry *entry;
598 struct bfd_hash_table *table;
599 const char *string;
601 struct elf_cris_link_hash_entry *ret =
602 (struct elf_cris_link_hash_entry *) entry;
604 /* Allocate the structure if it has not already been allocated by a
605 subclass. */
606 if (ret == (struct elf_cris_link_hash_entry *) NULL)
607 ret = ((struct elf_cris_link_hash_entry *)
608 bfd_hash_allocate (table,
609 sizeof (struct elf_cris_link_hash_entry)));
610 if (ret == (struct elf_cris_link_hash_entry *) NULL)
611 return (struct bfd_hash_entry *) ret;
613 /* Call the allocation method of the superclass. */
614 ret = ((struct elf_cris_link_hash_entry *)
615 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
616 table, string));
617 if (ret != (struct elf_cris_link_hash_entry *) NULL)
619 ret->pcrel_relocs_copied = NULL;
620 ret->gotplt_refcount = 0;
621 ret->gotplt_offset = 0;
624 return (struct bfd_hash_entry *) ret;
627 /* Create a CRIS ELF linker hash table. */
629 static struct bfd_link_hash_table *
630 elf_cris_link_hash_table_create (abfd)
631 bfd *abfd;
633 struct elf_cris_link_hash_table *ret;
635 ret = ((struct elf_cris_link_hash_table *)
636 bfd_alloc (abfd, sizeof (struct elf_cris_link_hash_table)));
637 if (ret == (struct elf_cris_link_hash_table *) NULL)
638 return NULL;
640 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
641 elf_cris_link_hash_newfunc))
643 bfd_release (abfd, ret);
644 return NULL;
647 /* Initialize to skip over the first three entries in the gotplt; they
648 are used for run-time symbol evaluation. */
649 ret->next_gotplt_entry = 12;
651 return &ret->root.root;
654 /* Perform a single relocation. By default we use the standard BFD
655 routines, with a few tweaks. */
657 static bfd_reloc_status_type
658 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
659 relocation)
660 reloc_howto_type * howto;
661 bfd * input_bfd;
662 asection * input_section;
663 bfd_byte * contents;
664 Elf_Internal_Rela * rel;
665 bfd_vma relocation;
667 bfd_reloc_status_type r;
669 /* PC-relative relocations are relative to the position *after*
670 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
671 not a single byte, since PC must be 16-bit-aligned. */
672 switch (ELF32_R_TYPE (rel->r_info))
674 /* Check that the 16-bit GOT relocs are positive. */
675 case R_CRIS_16_GOTPLT:
676 case R_CRIS_16_GOT:
677 if ((bfd_signed_vma) relocation < 0)
678 return bfd_reloc_overflow;
679 break;
681 case R_CRIS_32_PLT_PCREL:
682 case R_CRIS_32_PCREL:
683 relocation -= 2;
684 /* Fall through. */
685 case R_CRIS_8_PCREL:
686 case R_CRIS_16_PCREL:
687 relocation -= 2;
688 break;
690 default:
691 break;
694 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
695 contents, rel->r_offset,
696 relocation, rel->r_addend);
697 return r;
700 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
701 copied, for further comments. */
703 static boolean
704 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
705 contents, relocs, local_syms, local_sections)
706 bfd * output_bfd ATTRIBUTE_UNUSED;
707 struct bfd_link_info * info;
708 bfd * input_bfd;
709 asection * input_section;
710 bfd_byte * contents;
711 Elf_Internal_Rela * relocs;
712 Elf_Internal_Sym * local_syms;
713 asection ** local_sections;
715 bfd *dynobj;
716 Elf_Internal_Shdr * symtab_hdr;
717 struct elf_link_hash_entry ** sym_hashes;
718 bfd_vma *local_got_offsets;
719 asection *sgot;
720 asection *splt;
721 asection *sreloc;
722 Elf_Internal_Rela * rel;
723 Elf_Internal_Rela * relend;
725 dynobj = elf_hash_table (info)->dynobj;
726 local_got_offsets = elf_local_got_offsets (input_bfd);
727 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
728 sym_hashes = elf_sym_hashes (input_bfd);
729 relend = relocs + input_section->reloc_count;
731 /* It seems this can happen with erroneous or unsupported input (mixing
732 a.out and elf in an archive, for example.) */
733 if (sym_hashes == NULL)
734 return false;
736 sgot = NULL;
737 splt = NULL;
738 sreloc = NULL;
740 if (dynobj != NULL)
742 splt = bfd_get_section_by_name (dynobj, ".plt");
743 sgot = bfd_get_section_by_name (dynobj, ".got");
746 for (rel = relocs; rel < relend; rel ++)
748 reloc_howto_type * howto;
749 unsigned long r_symndx;
750 Elf_Internal_Sym * sym;
751 asection * sec;
752 struct elf_link_hash_entry * h;
753 bfd_vma relocation;
754 bfd_reloc_status_type r;
755 const char * name = NULL;
756 int r_type;
758 r_type = ELF32_R_TYPE (rel->r_info);
760 if ( r_type == R_CRIS_GNU_VTINHERIT
761 || r_type == R_CRIS_GNU_VTENTRY)
762 continue;
764 r_symndx = ELF32_R_SYM (rel->r_info);
766 if (info->relocateable)
768 /* This is a relocateable link. We don't have to change
769 anything, unless the reloc is against a section symbol,
770 in which case we have to adjust according to where the
771 section symbol winds up in the output section. */
772 if (r_symndx < symtab_hdr->sh_info)
774 sym = local_syms + r_symndx;
776 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
778 sec = local_sections [r_symndx];
779 rel->r_addend += sec->output_offset + sym->st_value;
783 continue;
786 /* This is a final link. */
787 howto = cris_elf_howto_table + r_type;
788 h = NULL;
789 sym = NULL;
790 sec = NULL;
792 if (r_symndx < symtab_hdr->sh_info)
794 sym = local_syms + r_symndx;
795 sec = local_sections [r_symndx];
796 relocation = (sec->output_section->vma
797 + sec->output_offset
798 + sym->st_value);
800 name = bfd_elf_string_from_elf_section
801 (input_bfd, symtab_hdr->sh_link, sym->st_name);
802 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
804 else
806 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
808 while (h->root.type == bfd_link_hash_indirect
809 || h->root.type == bfd_link_hash_warning)
810 h = (struct elf_link_hash_entry *) h->root.u.i.link;
812 name = h->root.root.string;
814 if (h->root.type == bfd_link_hash_defined
815 || h->root.type == bfd_link_hash_defweak)
817 sec = h->root.u.def.section;
819 /* Perhaps we should detect the cases that
820 sec->output_section is expected to be NULL like i386 and
821 m68k, but apparently (and according to elfxx-ia64.c) all
822 valid cases are where the symbol is defined in a shared
823 object which we link dynamically against. This includes
824 PLT relocs for which we've created a PLT entry and other
825 relocs for which we're prepared to create dynamic
826 relocations.
828 For now, new situations cause us to just err when
829 sec->output_offset is NULL but the object with the symbol
830 is *not* dynamically linked against. Thus this will
831 automatically remind us so we can see if there are other
832 valid cases we need to revisit. */
833 if ((sec->output_section == NULL
834 && (sec->owner->flags & DYNAMIC) != 0)
836 /* Here follow the cases where the relocation value must
837 be zero (or when further handling is simplified when
838 zero). I can't claim to understand the various
839 conditions and they weren't described in the files
840 where I copied them from (elf32-m68k.c and
841 elf32-i386.c), but let's mention examples of where
842 they happen. FIXME: Perhaps define and use a
843 dynamic_symbol_p function like ia64.
845 - When creating a shared library, we can have an
846 ordinary relocation for a symbol defined in a shared
847 library (perhaps the one we create). We then make
848 the relocation value zero, as the value seen now will
849 be added into the relocation addend in this shared
850 library, but must be handled only at dynamic-link
851 time. FIXME: Not sure this example covers the
852 h->elf_link_hash_flags test, though it's there in
853 other targets. */
854 || (info->shared
855 && ((! info->symbolic && h->dynindx != -1)
856 || (h->elf_link_hash_flags
857 & ELF_LINK_HASH_DEF_REGULAR) == 0)
858 && (input_section->flags & SEC_ALLOC) != 0
859 && (r_type == R_CRIS_8
860 || r_type == R_CRIS_16
861 || r_type == R_CRIS_32
862 || r_type == R_CRIS_8_PCREL
863 || r_type == R_CRIS_16_PCREL
864 || r_type == R_CRIS_32_PCREL)))
865 relocation = 0;
866 else if (sec->output_section != NULL)
867 relocation = (h->root.u.def.value
868 + sec->output_section->vma
869 + sec->output_offset);
870 else
872 if (input_bfd->my_archive)
873 (*_bfd_error_handler)
874 (_("%s(%s): unresolvable relocation %s against symbol `%s' from %s section"),
875 bfd_get_filename (bfd_my_archive (input_bfd)),
876 bfd_get_filename (input_bfd),
877 cris_elf_howto_table[r_type].name,
878 name,
879 bfd_get_section_name (input_bfd, input_section));
880 else
881 (*_bfd_error_handler)
882 (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
883 bfd_get_filename (input_bfd),
884 cris_elf_howto_table[r_type].name,
885 name,
886 bfd_get_section_name (input_bfd, input_section));
887 bfd_set_error (bfd_error_bad_value);
888 return false;
891 else if (h->root.type == bfd_link_hash_undefweak)
892 relocation = 0;
893 else if (info->shared
894 && !info->symbolic
895 && !info->no_undefined
896 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
897 relocation = 0;
898 else
900 if (!(info->callbacks->undefined_symbol
901 (info, name, input_bfd,
902 input_section, rel->r_offset,
903 (!info->shared || info->no_undefined
904 || ELF_ST_VISIBILITY (h->other)))))
905 return false;
906 relocation = 0;
910 switch (r_type)
912 case R_CRIS_16_GOTPLT:
913 case R_CRIS_32_GOTPLT:
914 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
915 but we require a PLT, and the PLT handling will take care of
916 filling in the PLT-specific GOT entry. For the GOT offset,
917 calculate it as we do when filling it in for the .got.plt
918 section. If we don't have a PLT, punt to GOT handling. */
919 if (h != NULL
920 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
922 asection *sgotplt
923 = bfd_get_section_by_name (dynobj, ".got.plt");
924 bfd_vma got_offset;
926 BFD_ASSERT (h->dynindx != -1);
927 BFD_ASSERT (sgotplt != NULL);
929 got_offset
930 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
932 relocation = got_offset;
933 break;
936 /* We didn't make a PLT entry for this symbol. Maybe everything is
937 folded into the GOT. Other than folding, this happens when
938 statically linking PIC code, or when using -Bsymbolic. Check
939 that we instead have a GOT entry as done for us by
940 elf_cris_adjust_dynamic_symbol, and drop through into the
941 ordinary GOT cases. */
942 if (h != NULL && h->got.offset == (bfd_vma) -1)
944 (*_bfd_error_handler)
945 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
946 bfd_get_filename (input_bfd),
947 cris_elf_howto_table[r_type].name,
948 name[0] != '\0' ? name : _("[whose name is lost]"),
949 bfd_get_section_name (input_bfd, input_section));
951 /* FIXME: Perhaps blaming input is not the right thing to
952 do; this is probably an internal error. But it is true
953 that we didn't like that particular input. */
954 bfd_set_error (bfd_error_bad_value);
955 return false;
957 /* Fall through. */
959 /* The size of the actual relocation is not used here; we only
960 fill in the GOT table here. */
961 case R_CRIS_16_GOT:
962 case R_CRIS_32_GOT:
964 bfd_vma off;
966 /* Note that despite using RELA relocations, the .got contents
967 is always filled in with the link-relative relocation
968 value; the addend. */
970 if (h != NULL)
972 off = h->got.offset;
973 BFD_ASSERT (off != (bfd_vma) -1);
975 if (!elf_hash_table (info)->dynamic_sections_created
976 || (! info->shared && h->dynindx == -1)
977 || (info->shared
978 && (info->symbolic || h->dynindx == -1)
979 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
981 /* This wasn't checked above for ! info->shared, but
982 must hold there if we get here; the symbol must not
983 be used in, or defined by a DSO. (Note that
984 checking for ELF_LINK_HASH_DEF_REGULAR doesn't
985 catch all cases.) */
986 BFD_ASSERT (info->shared
987 || (h->elf_link_hash_flags
988 & (ELF_LINK_HASH_REF_DYNAMIC
989 | ELF_LINK_HASH_DEF_DYNAMIC)) == 0);
991 /* This is actually a static link, or it is a
992 -Bsymbolic link and the symbol is defined
993 locally, or the symbol was forced to be local
994 because of a version file, or we're not creating a
995 dynamic object and the symbol isn't referred to by
996 a dynamic object. We must initialize
997 this entry in the global offset table. Since
998 the offset must always be a multiple of 4, we
999 use the least significant bit to record whether
1000 we have initialized it already.
1002 When doing a dynamic link, we create a .rela.got
1003 relocation entry to initialize the value. This
1004 is done in the finish_dynamic_symbol routine. */
1005 if ((off & 1) != 0)
1006 off &= ~1;
1007 else
1009 bfd_put_32 (output_bfd, relocation,
1010 sgot->contents + off);
1011 h->got.offset |= 1;
1015 else
1017 BFD_ASSERT (local_got_offsets != NULL
1018 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1020 off = local_got_offsets[r_symndx];
1022 /* The offset must always be a multiple of 4. We use
1023 the least significant bit to record whether we have
1024 already generated the necessary reloc. */
1025 if ((off & 1) != 0)
1026 off &= ~1;
1027 else
1029 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1031 if (info->shared)
1033 asection *srelgot;
1034 Elf_Internal_Rela outrel;
1036 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1037 BFD_ASSERT (srelgot != NULL);
1039 outrel.r_offset = (sgot->output_section->vma
1040 + sgot->output_offset
1041 + off);
1042 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1043 outrel.r_addend = relocation;
1044 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1045 (((Elf32_External_Rela *)
1046 srelgot->contents)
1047 + srelgot->reloc_count));
1048 ++srelgot->reloc_count;
1051 local_got_offsets[r_symndx] |= 1;
1055 relocation = sgot->output_offset + off;
1056 if (rel->r_addend != 0)
1058 /* We can't do anything for a relocation which is against
1059 a symbol *plus offset*. GOT holds relocations for
1060 symbols. Make this an error; the compiler isn't
1061 allowed to pass us these kinds of things. */
1062 if (h == NULL)
1063 (*_bfd_error_handler)
1064 (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1065 bfd_get_filename (input_bfd),
1066 cris_elf_howto_table[r_type].name,
1067 rel->r_addend,
1068 bfd_get_section_name (input_bfd, input_section));
1069 else
1070 (*_bfd_error_handler)
1071 (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1072 bfd_get_filename (input_bfd),
1073 cris_elf_howto_table[r_type].name,
1074 rel->r_addend,
1075 name[0] != '\0' ? name : _("[whose name is lost]"),
1076 bfd_get_section_name (input_bfd, input_section));
1078 bfd_set_error (bfd_error_bad_value);
1079 return false;
1082 break;
1084 case R_CRIS_32_GOTREL:
1085 /* This relocation must only be performed against local symbols. */
1086 if (h != NULL)
1088 (*_bfd_error_handler)
1089 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1090 bfd_get_filename (input_bfd),
1091 cris_elf_howto_table[r_type].name,
1092 name,
1093 bfd_get_section_name (input_bfd, input_section));
1094 bfd_set_error (bfd_error_bad_value);
1095 return false;
1098 /* This relocation is like a PC-relative one, except the
1099 reference point is the location of GOT. Note that
1100 sgot->output_offset is not involved in this calculation. We
1101 always want the start of entire .got section, not the
1102 position after the reserved header. */
1103 relocation -= sgot->output_section->vma;
1104 break;
1106 case R_CRIS_32_PLT_PCREL:
1107 /* Relocation is to the entry for this symbol in the
1108 procedure linkage table. */
1110 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1111 without using the procedure linkage table. */
1112 if (h == NULL)
1113 break;
1115 if (h->plt.offset == (bfd_vma) -1
1116 || splt == NULL)
1118 /* We didn't make a PLT entry for this symbol. This
1119 happens when statically linking PIC code, or when
1120 using -Bsymbolic. */
1121 break;
1124 relocation = (splt->output_section->vma
1125 + splt->output_offset
1126 + h->plt.offset);
1127 break;
1129 case R_CRIS_32_PLT_GOTREL:
1130 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1131 start of the .got section. See also comment at
1132 R_CRIS_32_GOT. */
1133 relocation -= sgot->output_section->vma;
1135 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1136 without using the procedure linkage table. */
1137 if (h == NULL)
1138 break;
1140 if (h->plt.offset == (bfd_vma) -1
1141 || splt == NULL)
1143 /* We didn't make a PLT entry for this symbol. This
1144 happens when statically linking PIC code, or when
1145 using -Bsymbolic. */
1146 break;
1149 relocation = (splt->output_section->vma
1150 + splt->output_offset
1151 + h->plt.offset
1152 - sgot->output_section->vma);
1153 break;
1155 case R_CRIS_8_PCREL:
1156 case R_CRIS_16_PCREL:
1157 case R_CRIS_32_PCREL:
1158 /* If the symbol was local, we need no shlib-specific handling. */
1159 if (h == NULL)
1160 break;
1162 /* Fall through. */
1163 case R_CRIS_8:
1164 case R_CRIS_16:
1165 case R_CRIS_32:
1166 if (info->shared
1167 && (input_section->flags & SEC_ALLOC) != 0
1168 && ((r_type != R_CRIS_8_PCREL
1169 && r_type != R_CRIS_16_PCREL
1170 && r_type != R_CRIS_32_PCREL)
1171 || (!info->symbolic
1172 || (h->elf_link_hash_flags
1173 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1175 Elf_Internal_Rela outrel;
1176 boolean skip, relocate;
1178 /* When generating a shared object, these relocations
1179 are copied into the output file to be resolved at run
1180 time. */
1182 if (sreloc == NULL)
1184 const char *name;
1186 name = (bfd_elf_string_from_elf_section
1187 (input_bfd,
1188 elf_elfheader (input_bfd)->e_shstrndx,
1189 elf_section_data (input_section)->rel_hdr.sh_name));
1190 if (name == NULL)
1191 return false;
1193 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1194 && strcmp (bfd_get_section_name (input_bfd,
1195 input_section),
1196 name + 5) == 0);
1198 sreloc = bfd_get_section_by_name (dynobj, name);
1200 /* That section should have been created in
1201 cris_elf_check_relocs, but that function will not be
1202 called for objects which fail in
1203 cris_elf_merge_private_bfd_data. */
1204 if (sreloc == NULL)
1206 (*_bfd_error_handler)
1207 (_("%s: Internal inconsistency; no relocation section %s"),
1208 bfd_get_filename (input_bfd),
1209 name);
1211 bfd_set_error (bfd_error_bad_value);
1212 return false;
1216 skip = false;
1218 if (elf_section_data (input_section)->stab_info == NULL)
1219 outrel.r_offset = rel->r_offset;
1220 else
1222 bfd_vma off;
1224 off = (_bfd_stab_section_offset
1225 (output_bfd, &elf_hash_table (info)->stab_info,
1226 input_section,
1227 &elf_section_data (input_section)->stab_info,
1228 rel->r_offset));
1229 if (off == (bfd_vma) -1)
1230 skip = true;
1231 outrel.r_offset = off;
1234 outrel.r_offset += (input_section->output_section->vma
1235 + input_section->output_offset);
1237 if (skip)
1239 memset (&outrel, 0, sizeof outrel);
1240 relocate = false;
1242 /* h->dynindx may be -1 if the symbol was marked to
1243 become local. */
1244 else if (h != NULL
1245 && ((! info->symbolic && h->dynindx != -1)
1246 || (h->elf_link_hash_flags
1247 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1249 BFD_ASSERT (h->dynindx != -1);
1250 relocate = false;
1251 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1252 outrel.r_addend = relocation + rel->r_addend;
1254 else
1256 if (r_type == R_CRIS_32)
1258 relocate = true;
1259 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1260 outrel.r_addend = relocation + rel->r_addend;
1262 else
1264 long indx;
1266 if (h == NULL)
1267 sec = local_sections[r_symndx];
1268 else
1270 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1271 || (h->root.type
1272 == bfd_link_hash_defweak));
1273 sec = h->root.u.def.section;
1275 if (sec != NULL && bfd_is_abs_section (sec))
1276 indx = 0;
1277 else if (sec == NULL || sec->owner == NULL)
1279 bfd_set_error (bfd_error_bad_value);
1280 return false;
1282 else
1284 asection *osec;
1286 osec = sec->output_section;
1287 indx = elf_section_data (osec)->dynindx;
1288 BFD_ASSERT (indx > 0);
1291 relocate = false;
1292 outrel.r_info = ELF32_R_INFO (indx, r_type);
1293 outrel.r_addend = relocation + rel->r_addend;
1297 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1298 (((Elf32_External_Rela *)
1299 sreloc->contents)
1300 + sreloc->reloc_count));
1301 ++sreloc->reloc_count;
1303 /* This reloc will be computed at runtime, so there's no
1304 need to do anything now, except for R_CRIS_32 relocations
1305 that have been turned into R_CRIS_RELATIVE. */
1306 if (!relocate)
1307 continue;
1310 break;
1313 r = cris_final_link_relocate (howto, input_bfd, input_section,
1314 contents, rel, relocation);
1316 if (r != bfd_reloc_ok)
1318 const char * msg = (const char *) NULL;
1320 switch (r)
1322 case bfd_reloc_overflow:
1323 r = info->callbacks->reloc_overflow
1324 (info, name, howto->name, (bfd_vma) 0,
1325 input_bfd, input_section, rel->r_offset);
1326 break;
1328 case bfd_reloc_undefined:
1329 r = info->callbacks->undefined_symbol
1330 (info, name, input_bfd, input_section, rel->r_offset,
1331 true);
1332 break;
1334 case bfd_reloc_outofrange:
1335 msg = _("internal error: out of range error");
1336 break;
1338 case bfd_reloc_notsupported:
1339 msg = _("internal error: unsupported relocation error");
1340 break;
1342 case bfd_reloc_dangerous:
1343 msg = _("internal error: dangerous relocation");
1344 break;
1346 default:
1347 msg = _("internal error: unknown error");
1348 break;
1351 if (msg)
1352 r = info->callbacks->warning
1353 (info, msg, name, input_bfd, input_section, rel->r_offset);
1355 if (! r)
1356 return false;
1360 return true;
1363 /* Finish up dynamic symbol handling. We set the contents of various
1364 dynamic sections here. */
1366 static boolean
1367 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1368 bfd *output_bfd;
1369 struct bfd_link_info *info;
1370 struct elf_link_hash_entry *h;
1371 Elf_Internal_Sym *sym;
1373 bfd *dynobj;
1374 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1376 dynobj = elf_hash_table (info)->dynobj;
1378 if (h->plt.offset != (bfd_vma) -1)
1380 asection *splt;
1381 asection *sgotplt;
1382 asection *sgot;
1383 asection *srela;
1384 bfd_vma got_base;
1386 bfd_vma gotplt_offset
1387 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1388 Elf_Internal_Rela rela;
1389 boolean has_gotplt = gotplt_offset != 0;
1391 /* Get the index in the procedure linkage table which
1392 corresponds to this symbol. This is the index of this symbol
1393 in all the symbols for which we are making plt entries. The
1394 first entry in the procedure linkage table is reserved. */
1395 /* We have to count backwards here, and the result is only valid as
1396 an index into .got.plt and its relocations. FIXME: Constants... */
1397 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1399 /* Get the offset into the .got table of the entry that corresponds
1400 to this function. Note that we embed knowledge that "incoming"
1401 .got goes after .got.plt in the output without padding (pointer
1402 aligned). However, that knowledge is present in several other
1403 places too, here and in elflink.h at least. */
1404 bfd_vma got_offset
1405 = (has_gotplt
1406 ? gotplt_offset
1407 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1409 /* This symbol has an entry in the procedure linkage table. Set it
1410 up. */
1412 BFD_ASSERT (h->dynindx != -1);
1414 splt = bfd_get_section_by_name (dynobj, ".plt");
1415 sgot = bfd_get_section_by_name (dynobj, ".got");
1416 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1417 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1418 BFD_ASSERT (splt != NULL && sgotplt != NULL
1419 && (! has_gotplt || srela != NULL));
1421 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1423 /* Fill in the entry in the procedure linkage table. */
1424 if (! info->shared)
1426 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1427 PLT_ENTRY_SIZE);
1429 /* We need to enter the absolute address of the GOT entry here. */
1430 bfd_put_32 (output_bfd, got_base + got_offset,
1431 splt->contents + h->plt.offset + plt_off1);
1433 else
1435 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1436 PLT_ENTRY_SIZE);
1437 bfd_put_32 (output_bfd, got_offset,
1438 splt->contents + h->plt.offset + plt_off1);
1441 /* Fill in the plt entry and make a relocation, if this is a "real"
1442 PLT entry. */
1443 if (has_gotplt)
1445 /* Fill in the offset into the reloc table. */
1446 bfd_put_32 (output_bfd,
1447 gotplt_index * sizeof (Elf32_External_Rela),
1448 splt->contents + h->plt.offset + plt_off2);
1450 /* Fill in the offset to the first PLT entry, where to "jump". */
1451 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1452 splt->contents + h->plt.offset + plt_off3);
1454 /* Fill in the entry in the global offset table with the address of
1455 the relocating stub. */
1456 bfd_put_32 (output_bfd,
1457 (splt->output_section->vma
1458 + splt->output_offset
1459 + h->plt.offset
1460 + 8),
1461 sgotplt->contents + got_offset);
1463 /* Fill in the entry in the .rela.plt section. */
1464 rela.r_offset = (sgotplt->output_section->vma
1465 + sgotplt->output_offset
1466 + got_offset);
1467 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1468 rela.r_addend = 0;
1469 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1470 ((Elf32_External_Rela *) srela->contents
1471 + gotplt_index));
1474 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1476 /* Mark the symbol as undefined, rather than as defined in
1477 the .plt section. Leave the value alone. */
1478 sym->st_shndx = SHN_UNDEF;
1480 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1481 know whether resetting the value is significant; if it really
1482 is, rather than a quirk or bug in the sparc port, then I
1483 believe we'd see this elsewhere. */
1484 /* If the symbol is weak, we do need to clear the value.
1485 Otherwise, the PLT entry would provide a definition for
1486 the symbol even if the symbol wasn't defined anywhere,
1487 and so the symbol would never be NULL. */
1488 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1489 == 0)
1490 sym->st_value = 0;
1494 /* We don't emit .got relocs for symbols that aren't in the
1495 dynamic-symbols table for an ordinary program. */
1496 if (h->got.offset != (bfd_vma) -1
1497 && (info->shared || h->dynindx != -1))
1499 asection *sgot;
1500 asection *srela;
1501 Elf_Internal_Rela rela;
1503 /* This symbol has an entry in the global offset table. Set it up. */
1505 sgot = bfd_get_section_by_name (dynobj, ".got");
1506 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1507 BFD_ASSERT (sgot != NULL && srela != NULL);
1509 rela.r_offset = (sgot->output_section->vma
1510 + sgot->output_offset
1511 + (h->got.offset &~ 1));
1513 /* If this is a static link, or it is a -Bsymbolic link and the
1514 symbol is defined locally or was forced to be local because
1515 of a version file, we just want to emit a RELATIVE reloc.
1516 The entry in the global offset table will already have been
1517 initialized in the relocate_section function. */
1518 if (! elf_hash_table (info)->dynamic_sections_created
1519 || (info->shared
1520 && (info->symbolic || h->dynindx == -1)
1521 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1523 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1524 rela.r_addend = bfd_get_signed_32 (output_bfd,
1525 (sgot->contents
1526 + (h->got.offset & ~1)));
1528 else
1530 bfd_put_32 (output_bfd, (bfd_vma) 0,
1531 sgot->contents + (h->got.offset & ~1));
1532 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1533 rela.r_addend = 0;
1536 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1537 ((Elf32_External_Rela *) srela->contents
1538 + srela->reloc_count));
1539 ++srela->reloc_count;
1542 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1544 asection *s;
1545 Elf_Internal_Rela rela;
1547 /* This symbol needs a copy reloc. Set it up. */
1549 BFD_ASSERT (h->dynindx != -1
1550 && (h->root.type == bfd_link_hash_defined
1551 || h->root.type == bfd_link_hash_defweak));
1553 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1554 ".rela.bss");
1555 BFD_ASSERT (s != NULL);
1557 rela.r_offset = (h->root.u.def.value
1558 + h->root.u.def.section->output_section->vma
1559 + h->root.u.def.section->output_offset);
1560 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1561 rela.r_addend = 0;
1562 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1563 ((Elf32_External_Rela *) s->contents
1564 + s->reloc_count));
1565 ++s->reloc_count;
1568 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1569 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1570 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1571 sym->st_shndx = SHN_ABS;
1573 return true;
1576 /* Finish up the dynamic sections. */
1578 static boolean
1579 elf_cris_finish_dynamic_sections (output_bfd, info)
1580 bfd *output_bfd;
1581 struct bfd_link_info *info;
1583 bfd *dynobj;
1584 asection *sgot;
1585 asection *sdyn;
1587 dynobj = elf_hash_table (info)->dynobj;
1589 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1590 BFD_ASSERT (sgot != NULL);
1591 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1593 if (elf_hash_table (info)->dynamic_sections_created)
1595 asection *splt;
1596 Elf32_External_Dyn *dyncon, *dynconend;
1598 splt = bfd_get_section_by_name (dynobj, ".plt");
1599 BFD_ASSERT (splt != NULL && sdyn != NULL);
1601 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1602 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1603 for (; dyncon < dynconend; dyncon++)
1605 Elf_Internal_Dyn dyn;
1606 asection *s;
1608 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1610 switch (dyn.d_tag)
1612 default:
1613 break;
1615 case DT_PLTGOT:
1616 s = bfd_get_section_by_name (output_bfd, ".got");
1617 BFD_ASSERT (s != NULL);
1618 dyn.d_un.d_ptr = s->vma;
1619 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1620 break;
1622 case DT_JMPREL:
1623 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1624 if all symbols are found in the .got (not .got.plt). */
1625 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1626 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1627 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1628 break;
1630 case DT_PLTRELSZ:
1631 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1632 if (s == NULL)
1633 dyn.d_un.d_val = 0;
1634 else 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;
1638 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1639 break;
1641 case DT_RELASZ:
1642 /* The procedure linkage table relocs (DT_JMPREL) should
1643 not be included in the overall relocs (DT_RELA).
1644 Therefore, we override the DT_RELASZ entry here to
1645 make it not include the JMPREL relocs. Since the
1646 linker script arranges for .rela.plt to follow all
1647 other relocation sections, we don't have to worry
1648 about changing the DT_RELA entry. */
1649 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1650 if (s != NULL)
1652 if (s->_cooked_size != 0)
1653 dyn.d_un.d_val -= s->_cooked_size;
1654 else
1655 dyn.d_un.d_val -= s->_raw_size;
1657 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1658 break;
1662 /* Fill in the first entry in the procedure linkage table. */
1663 if (splt->_raw_size > 0)
1665 if (info->shared)
1666 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1667 else
1669 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1670 bfd_put_32 (output_bfd,
1671 sgot->output_section->vma + sgot->output_offset + 4,
1672 splt->contents + 6);
1673 bfd_put_32 (output_bfd,
1674 sgot->output_section->vma + sgot->output_offset + 8,
1675 splt->contents + 14);
1677 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1678 = PLT_ENTRY_SIZE;
1683 /* Fill in the first three entries in the global offset table. */
1684 if (sgot->_raw_size > 0)
1686 if (sdyn == NULL)
1687 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1688 else
1689 bfd_put_32 (output_bfd,
1690 sdyn->output_section->vma + sdyn->output_offset,
1691 sgot->contents);
1692 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1693 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1696 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1698 return true;
1701 /* Return the section that should be marked against GC for a given
1702 relocation. */
1704 static asection *
1705 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1706 bfd * abfd;
1707 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1708 Elf_Internal_Rela * rel;
1709 struct elf_link_hash_entry * h;
1710 Elf_Internal_Sym * sym;
1712 if (h != NULL)
1714 switch (ELF32_R_TYPE (rel->r_info))
1716 case R_CRIS_GNU_VTINHERIT:
1717 case R_CRIS_GNU_VTENTRY:
1718 break;
1720 default:
1721 switch (h->root.type)
1723 case bfd_link_hash_defined:
1724 case bfd_link_hash_defweak:
1725 return h->root.u.def.section;
1727 case bfd_link_hash_common:
1728 return h->root.u.c.p->section;
1730 default:
1731 break;
1735 else
1737 if (!(elf_bad_symtab (abfd)
1738 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1739 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1740 && sym->st_shndx != SHN_COMMON))
1742 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1746 return NULL;
1749 /* Update the got entry reference counts for the section being removed. */
1751 static boolean
1752 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1753 bfd * abfd ATTRIBUTE_UNUSED;
1754 struct bfd_link_info * info ATTRIBUTE_UNUSED;
1755 asection * sec ATTRIBUTE_UNUSED;
1756 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1758 Elf_Internal_Shdr *symtab_hdr;
1759 struct elf_link_hash_entry **sym_hashes;
1760 bfd_signed_vma *local_got_refcounts;
1761 const Elf_Internal_Rela *rel, *relend;
1762 unsigned long r_symndx;
1763 struct elf_link_hash_entry *h;
1764 bfd *dynobj;
1765 asection *sgot;
1766 asection *srelgot;
1768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1769 sym_hashes = elf_sym_hashes (abfd);
1770 local_got_refcounts = elf_local_got_refcounts (abfd);
1772 dynobj = elf_hash_table (info)->dynobj;
1773 if (dynobj == NULL)
1774 return true;
1776 sgot = bfd_get_section_by_name (dynobj, ".got");
1777 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1779 relend = relocs + sec->reloc_count;
1780 for (rel = relocs; rel < relend; rel++)
1782 switch (ELF32_R_TYPE (rel->r_info))
1784 case R_CRIS_16_GOT:
1785 case R_CRIS_32_GOT:
1786 r_symndx = ELF32_R_SYM (rel->r_info);
1787 if (r_symndx >= symtab_hdr->sh_info)
1789 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1790 if (h->got.refcount > 0)
1792 --h->got.refcount;
1793 if (h->got.refcount == 0)
1795 /* We don't need the .got entry any more. */
1796 sgot->_raw_size -= 4;
1797 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1800 break;
1803 local_got_reloc:
1804 if (local_got_refcounts != NULL)
1806 if (local_got_refcounts[r_symndx] > 0)
1808 --local_got_refcounts[r_symndx];
1809 if (local_got_refcounts[r_symndx] == 0)
1811 /* We don't need the .got entry any more. */
1812 sgot->_raw_size -= 4;
1813 if (info->shared)
1814 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1818 break;
1820 case R_CRIS_16_GOTPLT:
1821 case R_CRIS_32_GOTPLT:
1822 /* For local symbols, treat these like GOT relocs. */
1823 r_symndx = ELF32_R_SYM (rel->r_info);
1824 if (r_symndx < symtab_hdr->sh_info)
1825 goto local_got_reloc;
1827 case R_CRIS_32_PLT_GOTREL:
1828 /* FIXME: We don't garbage-collect away the .got section. */
1829 if (local_got_refcounts != NULL)
1830 local_got_refcounts[-1]--;
1831 /* Fall through. */
1833 case R_CRIS_8_PCREL:
1834 case R_CRIS_16_PCREL:
1835 case R_CRIS_32_PCREL:
1836 case R_CRIS_32_PLT_PCREL:
1837 r_symndx = ELF32_R_SYM (rel->r_info);
1838 if (r_symndx >= symtab_hdr->sh_info)
1840 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1841 if (h->plt.refcount > 0)
1842 --h->plt.refcount;
1844 break;
1846 default:
1847 break;
1851 return true;
1854 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1855 entry but we found we will not create any. Called when we find we will
1856 not have any PLT for this symbol, by for example
1857 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1858 or elf_cris_size_dynamic_sections if no dynamic sections will be
1859 created (we're only linking static objects). */
1861 static boolean
1862 elf_cris_adjust_gotplt_to_got (h, p)
1863 struct elf_cris_link_hash_entry *h;
1864 PTR p;
1866 struct bfd_link_info *info = (struct bfd_link_info *) p;
1867 bfd *dynobj = elf_hash_table (info)->dynobj;
1869 BFD_ASSERT (dynobj != NULL);
1871 /* If nobody wanted a GOTPLT with this symbol, we're done. */
1872 if (h->gotplt_refcount <= 0)
1873 return true;
1875 if (h->root.got.refcount > 0)
1877 /* There's a GOT entry for this symbol. Just adjust the refcount.
1878 Probably not necessary at this stage, but keeping it accurate
1879 helps avoiding surprises later. */
1880 h->root.got.refcount += h->gotplt_refcount;
1881 h->gotplt_refcount = -1;
1883 else
1885 /* No GOT entry for this symbol. We need to create one. */
1886 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1887 asection *srelgot
1888 = bfd_get_section_by_name (dynobj, ".rela.got");
1890 /* Put an accurate refcount there. */
1891 h->root.got.refcount = h->gotplt_refcount;
1893 h->gotplt_refcount = -1;
1895 /* We always have a .got section when there are dynamic
1896 relocs. */
1897 BFD_ASSERT (sgot != NULL /* Surely have .got section. */);
1899 /* We might have had a PLT but with no GOT entry and
1900 further no GOT reloc section at all needed before.
1901 Add it. */
1902 if (srelgot == NULL)
1904 srelgot = bfd_make_section (dynobj, ".rela.got");
1906 if (srelgot == NULL
1907 || !bfd_set_section_flags (dynobj, srelgot,
1908 (SEC_ALLOC
1909 | SEC_LOAD
1910 | SEC_HAS_CONTENTS
1911 | SEC_IN_MEMORY
1912 | SEC_LINKER_CREATED
1913 | SEC_READONLY))
1914 || !bfd_set_section_alignment (dynobj, srelgot, 2))
1915 return false;
1918 /* Allocate space in the .got section. */
1919 sgot->_raw_size += 4;
1921 /* Allocate relocation space. */
1922 srelgot->_raw_size += sizeof (Elf32_External_Rela);
1925 return true;
1928 /* Try to fold PLT entries with GOT entries. There are two cases when we
1929 want to do this:
1931 - When all PLT references are GOTPLT references, and there are GOT
1932 references. We don't have to generate a PLT at all.
1934 - When there are both (ordinary) PLT references and GOT references.
1935 We want to make the PLT reference use the ordinary GOT entry rather
1936 than a run-time dynamically resolved GOTPLT entry (since the GOT
1937 entry will have to be resolved at startup anyway).
1939 Though the latter case is handled when room for the PLT is allocated,
1940 not here.
1942 Note that this function is called before symbols are forced local by
1943 version scripts. The differing cases are handled by
1944 elf_cris_hide_symbol. */
1946 static boolean
1947 elf_cris_try_fold_plt_to_got (h, p)
1948 struct elf_cris_link_hash_entry *h;
1949 PTR p;
1951 struct bfd_link_info *info = (struct bfd_link_info *) p;
1953 /* If there are no GOT references for this symbol, we can't fold any
1954 other reference so there's nothing to do. Likewise if there are no
1955 PLT references; GOTPLT references included. */
1956 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1957 return true;
1959 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
1960 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1962 if (h->gotplt_refcount == h->root.plt.refcount)
1964 /* The only PLT references are GOTPLT references, and there are GOT
1965 references. Convert PLT to GOT references. */
1966 if (! elf_cris_adjust_gotplt_to_got (h, info))
1967 return false;
1969 /* Clear the PLT references, so no PLT will be created. */
1970 h->root.plt.offset = (bfd_vma) -1;
1973 return true;
1976 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1977 to use a GOT entry (and create one) rather than requiring a GOTPLT
1978 entry. */
1980 static void
1981 elf_cris_hide_symbol (info, h)
1982 struct bfd_link_info *info;
1983 struct elf_link_hash_entry *h;
1985 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1987 _bfd_elf_link_hash_hide_symbol (info, h);
1990 /* Adjust a symbol defined by a dynamic object and referenced by a
1991 regular object. The current definition is in some section of the
1992 dynamic object, but we're not including those sections. We have to
1993 change the definition to something the rest of the link can
1994 understand. */
1996 static boolean
1997 elf_cris_adjust_dynamic_symbol (info, h)
1998 struct bfd_link_info *info;
1999 struct elf_link_hash_entry *h;
2001 bfd *dynobj;
2002 asection *s;
2003 unsigned int power_of_two;
2005 dynobj = elf_hash_table (info)->dynobj;
2007 /* Make sure we know what is going on here. */
2008 BFD_ASSERT (dynobj != NULL
2009 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2010 || h->weakdef != NULL
2011 || ((h->elf_link_hash_flags
2012 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2013 && (h->elf_link_hash_flags
2014 & ELF_LINK_HASH_REF_REGULAR) != 0
2015 && (h->elf_link_hash_flags
2016 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2018 /* If this is a function, put it in the procedure linkage table. We
2019 will fill in the contents of the procedure linkage table later,
2020 when we know the address of the .got section. */
2021 if (h->type == STT_FUNC
2022 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2024 if (! info->shared
2025 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2026 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2027 /* We must always create the plt entry if it was referenced by a
2028 PLT relocation. In this case we already recorded it as a
2029 dynamic symbol. */
2030 /* FIXME: m68k and i386 differ here, for unclear reasons. */
2031 && h->dynindx == -1)
2033 /* This case can occur if we saw a PLT reloc in an input file,
2034 but the symbol was never referred to by a dynamic object. In
2035 such a case, we don't actually need to build a procedure
2036 linkage table, and we can just do a PC reloc instead, or
2037 change a .got.plt index to a .got index for GOTPLT relocs. */
2038 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2039 h->plt.offset = (bfd_vma) -1;
2041 return
2042 elf_cris_adjust_gotplt_to_got ((struct
2043 elf_cris_link_hash_entry *) h,
2044 info);
2047 /* If there are only GOT references and GOTPLT references to this
2048 PLT entry, get rid of the PLT. */
2049 if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2050 h, info))
2051 return false;
2053 /* GC or folding may have rendered this entry unused. */
2054 if (h->plt.refcount <= 0)
2056 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2057 h->plt.offset = (bfd_vma) -1;
2058 return true;
2061 /* Make sure this symbol is output as a dynamic symbol. */
2062 if (h->dynindx == -1)
2064 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2065 return false;
2068 s = bfd_get_section_by_name (dynobj, ".plt");
2069 BFD_ASSERT (s != NULL);
2071 /* If this is the first .plt entry, make room for the special
2072 first entry. */
2073 if (s->_raw_size == 0)
2074 s->_raw_size += PLT_ENTRY_SIZE;
2076 /* If this symbol is not defined in a regular file, and we are
2077 not generating a shared library, then set the symbol to this
2078 location in the .plt. This is required to make function
2079 pointers compare as equal between the normal executable and
2080 the shared library. */
2081 if (!info->shared
2082 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2084 h->root.u.def.section = s;
2085 h->root.u.def.value = s->_raw_size;
2088 /* If there's already a GOT entry, use that, not a .got.plt. A
2089 GOT field still has a reference count when we get here; it's
2090 not yet changed to an offset. */
2091 if (h->got.refcount > 0)
2093 h->got.refcount += h->plt.refcount;
2095 /* Mark the PLT offset to use the GOT entry by setting the low
2096 bit in the plt offset; it is always a multiple of
2097 pointer-size. */
2098 BFD_ASSERT ((s->_raw_size & 3) == 0);
2100 /* Change the PLT refcount to an offset. */
2101 h->plt.offset = s->_raw_size;
2103 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2104 that the got entry should be used instead. */
2105 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2106 h)->gotplt_offset == 0);
2108 /* Make room for this entry. */
2109 s->_raw_size += PLT_ENTRY_SIZE;
2111 return true;
2114 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2115 h->plt.offset = s->_raw_size;
2117 /* Make room for this entry. */
2118 s->_raw_size += PLT_ENTRY_SIZE;
2120 /* We also need to make an entry in the .got.plt section, which
2121 will be placed in the .got section by the linker script. */
2122 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2123 = elf_cris_hash_table (info)->next_gotplt_entry;
2124 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2126 s = bfd_get_section_by_name (dynobj, ".got.plt");
2127 BFD_ASSERT (s != NULL);
2128 s->_raw_size += 4;
2130 /* We also need to make an entry in the .rela.plt section. */
2132 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2133 BFD_ASSERT (s != NULL);
2134 s->_raw_size += sizeof (Elf32_External_Rela);
2136 return true;
2139 /* Reinitialize the plt offset now that it is not used as a reference
2140 count any more. */
2141 h->plt.offset = (bfd_vma) -1;
2143 /* If this is a weak symbol, and there is a real definition, the
2144 processor independent code will have arranged for us to see the
2145 real definition first, and we can just use the same value. */
2146 if (h->weakdef != NULL)
2148 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2149 || h->weakdef->root.type == bfd_link_hash_defweak);
2150 h->root.u.def.section = h->weakdef->root.u.def.section;
2151 h->root.u.def.value = h->weakdef->root.u.def.value;
2152 return true;
2155 /* This is a reference to a symbol defined by a dynamic object which
2156 is not a function. */
2158 /* If we are creating a shared library, we must presume that the
2159 only references to the symbol are via the global offset table.
2160 For such cases we need not do anything here; the relocations will
2161 be handled correctly by relocate_section. */
2162 if (info->shared)
2163 return true;
2165 /* If there are no references to this symbol that do not use the
2166 GOT, we don't need to generate a copy reloc. */
2167 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2168 return true;
2170 /* We must allocate the symbol in our .dynbss section, which will
2171 become part of the .bss section of the executable. There will be
2172 an entry for this symbol in the .dynsym section. The dynamic
2173 object will contain position independent code, so all references
2174 from the dynamic object to this symbol will go through the global
2175 offset table. The dynamic linker will use the .dynsym entry to
2176 determine the address it must put in the global offset table, so
2177 both the dynamic object and the regular object will refer to the
2178 same memory location for the variable. */
2180 s = bfd_get_section_by_name (dynobj, ".dynbss");
2181 BFD_ASSERT (s != NULL);
2183 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2184 copy the initial value out of the dynamic object and into the
2185 runtime process image. We need to remember the offset into the
2186 .rela.bss section we are going to use. */
2187 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2189 asection *srel;
2191 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2192 BFD_ASSERT (srel != NULL);
2193 srel->_raw_size += sizeof (Elf32_External_Rela);
2194 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2197 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2198 thing to copy; so do we. */
2200 /* We need to figure out the alignment required for this symbol. I
2201 have no idea how ELF linkers handle this. */
2202 power_of_two = bfd_log2 (h->size);
2203 if (power_of_two > 3)
2204 power_of_two = 3;
2206 /* Apply the required alignment. */
2207 s->_raw_size = BFD_ALIGN (s->_raw_size,
2208 (bfd_size_type) (1 << power_of_two));
2209 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2211 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2212 return false;
2215 /* Define the symbol as being at this point in the section. */
2216 h->root.u.def.section = s;
2217 h->root.u.def.value = s->_raw_size;
2219 /* Increment the section size to make room for the symbol. */
2220 s->_raw_size += h->size;
2222 return true;
2225 /* Look through the relocs for a section during the first phase. */
2227 static boolean
2228 cris_elf_check_relocs (abfd, info, sec, relocs)
2229 bfd *abfd;
2230 struct bfd_link_info *info;
2231 asection *sec;
2232 const Elf_Internal_Rela *relocs;
2234 bfd *dynobj;
2235 Elf_Internal_Shdr *symtab_hdr;
2236 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2237 bfd_signed_vma *local_got_refcounts;
2238 const Elf_Internal_Rela *rel;
2239 const Elf_Internal_Rela *rel_end;
2240 asection *sgot;
2241 asection *srelgot;
2242 asection *sreloc;
2244 if (info->relocateable)
2245 return true;
2247 dynobj = elf_hash_table (info)->dynobj;
2248 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2249 sym_hashes = elf_sym_hashes (abfd);
2250 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2251 local_got_refcounts = elf_local_got_refcounts (abfd);
2253 sgot = NULL;
2254 srelgot = NULL;
2255 sreloc = NULL;
2257 if (!elf_bad_symtab (abfd))
2258 sym_hashes_end -= symtab_hdr->sh_info;
2260 rel_end = relocs + sec->reloc_count;
2261 for (rel = relocs; rel < rel_end; rel++)
2263 struct elf_link_hash_entry *h;
2264 unsigned long r_symndx;
2265 enum elf_cris_reloc_type r_type;
2267 r_symndx = ELF32_R_SYM (rel->r_info);
2268 if (r_symndx < symtab_hdr->sh_info)
2269 h = NULL;
2270 else
2271 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2273 r_type = ELF32_R_TYPE (rel->r_info);
2275 /* Some relocs require linker-created sections; we need to hang them
2276 on the first input bfd we found that contained dynamic relocs. */
2277 switch (r_type)
2279 case R_CRIS_16_GOT:
2280 case R_CRIS_32_GOT:
2281 case R_CRIS_32_GOTREL:
2282 case R_CRIS_32_PLT_GOTREL:
2283 case R_CRIS_32_PLT_PCREL:
2284 case R_CRIS_16_GOTPLT:
2285 case R_CRIS_32_GOTPLT:
2286 if (dynobj == NULL)
2288 elf_hash_table (info)->dynobj = dynobj = abfd;
2290 /* Create the .got section, so we can assume it's always
2291 present whenever there's a dynobj. */
2292 if (!_bfd_elf_create_got_section (dynobj, info))
2293 return false;
2295 break;
2297 default:
2298 break;
2301 /* Some relocs require a global offset table (but perhaps not a
2302 specific GOT entry). */
2303 switch (r_type)
2305 case R_CRIS_16_GOT:
2306 case R_CRIS_32_GOT:
2307 case R_CRIS_32_GOTREL:
2308 case R_CRIS_32_PLT_GOTREL:
2309 if (sgot == NULL)
2310 sgot = bfd_get_section_by_name (dynobj, ".got");
2312 if (local_got_refcounts == NULL)
2314 size_t size;
2316 /* We use index local_got_refcounts[-1] to count all
2317 GOT-relative relocations that do not have explicit
2318 GOT entries. */
2319 size = (symtab_hdr->sh_info + 1) * sizeof (bfd_signed_vma);
2320 local_got_refcounts = ((bfd_signed_vma *)
2321 bfd_alloc (abfd, size));
2322 if (local_got_refcounts == NULL)
2323 return false;
2324 memset (local_got_refcounts, -1, size);
2326 local_got_refcounts++;
2327 elf_local_got_refcounts (abfd) = local_got_refcounts;
2329 break;
2331 default:
2332 break;
2335 switch (r_type)
2337 case R_CRIS_16_GOTPLT:
2338 case R_CRIS_32_GOTPLT:
2339 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2340 entry) is eliminated. We can only do this for a non-local
2341 symbol. */
2342 if (h != NULL)
2344 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2345 goto handle_gotplt_reloc;
2347 /* If h is NULL then this is a local symbol, and we must make a
2348 GOT entry for it, so handle it like a GOT reloc. */
2349 /* Fall through. */
2351 case R_CRIS_16_GOT:
2352 case R_CRIS_32_GOT:
2353 /* This symbol requires a global offset table entry. */
2355 if (srelgot == NULL
2356 && (h != NULL || info->shared))
2358 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2359 if (srelgot == NULL)
2361 srelgot = bfd_make_section (dynobj, ".rela.got");
2362 if (srelgot == NULL
2363 || !bfd_set_section_flags (dynobj, srelgot,
2364 (SEC_ALLOC
2365 | SEC_LOAD
2366 | SEC_HAS_CONTENTS
2367 | SEC_IN_MEMORY
2368 | SEC_LINKER_CREATED
2369 | SEC_READONLY))
2370 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2371 return false;
2375 if (h != NULL)
2377 if (h->got.refcount == -1)
2379 h->got.refcount = 1;
2381 /* Make sure this symbol is output as a dynamic symbol. */
2382 if (h->dynindx == -1)
2384 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2385 return false;
2388 /* Allocate space in the .got section. */
2389 sgot->_raw_size += 4;
2390 /* Allocate relocation space. */
2391 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2393 else
2394 h->got.refcount++;
2396 else
2398 /* This is a global offset table entry for a local symbol. */
2399 if (local_got_refcounts[r_symndx] == -1)
2401 local_got_refcounts[r_symndx] = 1;
2403 sgot->_raw_size += 4;
2404 if (info->shared)
2406 /* If we are generating a shared object, we need to
2407 output a R_CRIS_RELATIVE reloc so that the dynamic
2408 linker can adjust this GOT entry. */
2409 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2412 else
2413 local_got_refcounts[r_symndx]++;
2415 break;
2417 case R_CRIS_32_GOTREL:
2418 /* This reference requires a global offset table.
2419 FIXME: The actual refcount isn't used currently; the .got
2420 section can't be removed if there were any references in the
2421 input. */
2422 local_got_refcounts[-1]++;
2423 break;
2425 handle_gotplt_reloc:
2427 case R_CRIS_32_PLT_GOTREL:
2428 /* This reference requires a global offset table. */
2429 local_got_refcounts[-1]++;
2430 /* Fall through. */
2432 case R_CRIS_32_PLT_PCREL:
2433 /* This symbol requires a procedure linkage table entry. We
2434 actually build the entry in adjust_dynamic_symbol,
2435 because this might be a case of linking PIC code which is
2436 never referenced by a dynamic object, in which case we
2437 don't need to generate a procedure linkage table entry
2438 after all. */
2440 /* If this is a local symbol, we resolve it directly without
2441 creating a procedure linkage table entry. */
2442 if (h == NULL)
2443 continue;
2445 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2446 if (h->plt.refcount == -1)
2447 h->plt.refcount = 1;
2448 else
2449 h->plt.refcount++;
2450 break;
2452 case R_CRIS_8:
2453 case R_CRIS_16:
2454 case R_CRIS_32:
2455 /* Let's help debug shared library creation. Any of these
2456 relocs can be used in shared libs, but pages containing them
2457 cannot be shared. Don't warn for sections we don't care
2458 about, such as debug sections or non-constant sections. We
2459 can't help tables of (global) function pointers, for example,
2460 though they must be emitted in a data section to avoid having
2461 impure text sections. */
2462 if (info->shared
2463 && (sec->flags & SEC_ALLOC) != 0
2464 && (sec->flags & SEC_READONLY) != 0)
2466 /* FIXME: How do we make this optionally a warning only? */
2467 if (abfd->my_archive)
2468 (*_bfd_error_handler)
2469 (_("%s(%s), section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2470 bfd_get_filename (bfd_my_archive (abfd)),
2471 bfd_get_filename (abfd),
2472 sec->name,
2473 cris_elf_howto_table[r_type].name);
2474 else
2475 (*_bfd_error_handler)
2476 (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"),
2477 bfd_get_filename (abfd),
2478 sec->name,
2479 cris_elf_howto_table[r_type].name);
2481 /* Fall through. */
2483 case R_CRIS_8_PCREL:
2484 case R_CRIS_16_PCREL:
2485 case R_CRIS_32_PCREL:
2486 if (h != NULL)
2488 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2490 /* Make sure a plt entry is created for this symbol if it
2491 turns out to be a function defined by a dynamic object. */
2492 if (h->plt.refcount == -1)
2493 h->plt.refcount = 1;
2494 else
2495 h->plt.refcount++;
2498 /* If we are creating a shared library and this is not a local
2499 symbol, we need to copy the reloc into the shared library.
2500 However when linking with -Bsymbolic and this is a global
2501 symbol which is defined in an object we are including in the
2502 link (i.e., DEF_REGULAR is set), then we can resolve the
2503 reloc directly. At this point we have not seen all the input
2504 files, so it is possible that DEF_REGULAR is not set now but
2505 will be set later (it is never cleared). In case of a weak
2506 definition, DEF_REGULAR may be cleared later by a strong
2507 definition in a shared library. We account for that
2508 possibility below by storing information in the relocs_copied
2509 field of the hash table entry. A similar situation occurs
2510 when creating shared libraries and symbol visibility changes
2511 render the symbol local. */
2513 /* No need to do anything if we're not creating a shared object. */
2514 if (! info->shared)
2515 break;
2517 /* We don't need to handle relocs into sections not going into
2518 the "real" output. */
2519 if ((sec->flags & SEC_ALLOC) == 0)
2520 break;
2522 /* We can only eliminate PC-relative relocs. */
2523 if (r_type == R_CRIS_8_PCREL
2524 || r_type == R_CRIS_16_PCREL
2525 || r_type == R_CRIS_32_PCREL)
2527 /* If the symbol is local, then we can eliminate the reloc. */
2528 if (h == NULL)
2529 break;
2531 /* If this is with -Bsymbolic and the symbol isn't weak, and
2532 is defined by an ordinary object (the ones we include in
2533 this shared library) then we can also eliminate the
2534 reloc. See comment above for more eliminable cases which
2535 we can't identify at this time. */
2536 if (info->symbolic
2537 && h->root.type != bfd_link_hash_defweak
2538 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2539 break;
2542 /* We create a reloc section in dynobj and make room for this
2543 reloc. */
2544 if (sreloc == NULL)
2546 const char *name;
2548 name = (bfd_elf_string_from_elf_section
2549 (abfd,
2550 elf_elfheader (abfd)->e_shstrndx,
2551 elf_section_data (sec)->rel_hdr.sh_name));
2552 if (name == NULL)
2553 return false;
2555 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2556 && strcmp (bfd_get_section_name (abfd, sec),
2557 name + 5) == 0);
2559 sreloc = bfd_get_section_by_name (dynobj, name);
2560 if (sreloc == NULL)
2562 sreloc = bfd_make_section (dynobj, name);
2563 if (sreloc == NULL
2564 || !bfd_set_section_flags (dynobj, sreloc,
2565 (SEC_ALLOC
2566 | SEC_LOAD
2567 | SEC_HAS_CONTENTS
2568 | SEC_IN_MEMORY
2569 | SEC_LINKER_CREATED
2570 | SEC_READONLY))
2571 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2572 return false;
2576 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2578 /* If we are linking with -Bsymbolic, we count the number of PC
2579 relative relocations we have entered for this symbol, so that
2580 we can discard them again if the symbol is later defined by a
2581 regular object. We know that h is really a pointer to an
2582 elf_cris_link_hash_entry. */
2583 if ((r_type == R_CRIS_8_PCREL
2584 || r_type == R_CRIS_16_PCREL
2585 || r_type == R_CRIS_32_PCREL)
2586 && info->symbolic)
2588 struct elf_cris_link_hash_entry *eh;
2589 struct elf_cris_pcrel_relocs_copied *p;
2591 eh = (struct elf_cris_link_hash_entry *) h;
2593 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2594 if (p->section == sreloc)
2595 break;
2597 if (p == NULL)
2599 p = ((struct elf_cris_pcrel_relocs_copied *)
2600 bfd_alloc (dynobj, sizeof *p));
2601 if (p == NULL)
2602 return false;
2603 p->next = eh->pcrel_relocs_copied;
2604 eh->pcrel_relocs_copied = p;
2605 p->section = sreloc;
2606 p->count = 0;
2609 ++p->count;
2611 break;
2613 /* This relocation describes the C++ object vtable hierarchy.
2614 Reconstruct it for later use during GC. */
2615 case R_CRIS_GNU_VTINHERIT:
2616 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2617 return false;
2618 break;
2620 /* This relocation describes which C++ vtable entries are actually
2621 used. Record for later use during GC. */
2622 case R_CRIS_GNU_VTENTRY:
2623 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2624 return false;
2625 break;
2627 default:
2628 /* Other relocs do not appear here. */
2629 bfd_set_error (bfd_error_bad_value);
2630 return false;
2634 return true;
2637 /* Set the sizes of the dynamic sections. */
2639 static boolean
2640 elf_cris_size_dynamic_sections (output_bfd, info)
2641 bfd *output_bfd;
2642 struct bfd_link_info *info;
2644 bfd *dynobj;
2645 asection *s;
2646 boolean plt;
2647 boolean relocs;
2648 boolean reltext;
2650 dynobj = elf_hash_table (info)->dynobj;
2651 BFD_ASSERT (dynobj != NULL);
2653 if (elf_hash_table (info)->dynamic_sections_created)
2655 /* Set the contents of the .interp section to the interpreter. */
2656 if (!info->shared)
2658 s = bfd_get_section_by_name (dynobj, ".interp");
2659 BFD_ASSERT (s != NULL);
2660 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2661 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2664 else
2666 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2667 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2668 elf_cris_adjust_gotplt_to_got,
2669 (PTR) info);
2671 /* We may have created entries in the .rela.got section.
2672 However, if we are not creating the dynamic sections, we will
2673 not actually use these entries. Reset the size of .rela.got,
2674 which will cause it to get stripped from the output file
2675 below. */
2676 s = bfd_get_section_by_name (dynobj, ".rela.got");
2677 if (s != NULL)
2678 s->_raw_size = 0;
2681 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2682 relative relocs against symbols defined in a regular object. We
2683 allocated space for them in the check_relocs routine, but we will not
2684 fill them in in the relocate_section routine. We also discard space
2685 for relocs that have become for local symbols due to symbol
2686 visibility changes. For programs, we discard space for relocs for
2687 symbols not referenced by any dynamic object. */
2688 if (info->shared)
2689 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2690 elf_cris_discard_excess_dso_dynamics,
2691 (PTR) info);
2692 else
2693 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2694 elf_cris_discard_excess_program_dynamics,
2695 (PTR) info);
2697 /* The check_relocs and adjust_dynamic_symbol entry points have
2698 determined the sizes of the various dynamic sections. Allocate
2699 memory for them. */
2700 plt = false;
2701 relocs = false;
2702 reltext = false;
2703 for (s = dynobj->sections; s != NULL; s = s->next)
2705 const char *name;
2706 boolean strip;
2708 if ((s->flags & SEC_LINKER_CREATED) == 0)
2709 continue;
2711 /* It's OK to base decisions on the section name, because none
2712 of the dynobj section names depend upon the input files. */
2713 name = bfd_get_section_name (dynobj, s);
2715 strip = false;
2717 if (strcmp (name, ".plt") == 0)
2719 if (s->_raw_size == 0)
2721 /* Strip this section if we don't need it; see the
2722 comment below. */
2723 strip = true;
2725 else
2727 /* Remember whether there is a PLT. */
2728 plt = true;
2731 else if (strncmp (name, ".rela", 5) == 0)
2733 if (s->_raw_size == 0)
2735 /* If we don't need this section, strip it from the
2736 output file. This is mostly to handle .rela.bss and
2737 .rela.plt. We must create both sections in
2738 create_dynamic_sections, because they must be created
2739 before the linker maps input sections to output
2740 sections. The linker does that before
2741 adjust_dynamic_symbol is called, and it is that
2742 function which decides whether anything needs to go
2743 into these sections. */
2744 strip = true;
2746 else
2748 asection *target;
2750 /* Remember whether there are any reloc sections other
2751 than .rela.plt. */
2752 if (strcmp (name, ".rela.plt") != 0)
2754 const char *outname;
2756 relocs = true;
2758 /* If this relocation section applies to a read only
2759 section, then we probably need a DT_TEXTREL entry.
2760 The entries in the .rela.plt section are actually
2761 associated with .got.plt, which we created ourselves
2762 and so know is not readonly. */
2763 outname = bfd_get_section_name (output_bfd,
2764 s->output_section);
2765 target
2766 = bfd_get_section_by_name (output_bfd,
2767 outname + strlen (".rela"));
2769 /* We have to test the .text section by name, becase for
2770 some reason it does not have SEC_READONLY set at this
2771 time. That flag is actually set in ldmain.c:main
2772 specifically for ".text" at a time long after this
2773 function is called. FIXME: This might be due to a
2774 general bug. FIXME: Have testcase for this. */
2775 if (target != NULL
2776 && (target->flags & SEC_ALLOC) != 0
2777 && ((target->flags & SEC_READONLY) != 0
2778 || strcmp (outname + strlen (".rela"),
2779 ".text") == 0))
2780 reltext = true;
2783 /* We use the reloc_count field as a counter if we need
2784 to copy relocs into the output file. */
2785 s->reloc_count = 0;
2788 else if (strncmp (name, ".got", 4) != 0)
2790 /* It's not one of our sections, so don't allocate space. */
2791 continue;
2794 if (strip)
2796 _bfd_strip_section_from_output (info, s);
2797 continue;
2800 /* Allocate memory for the section contents. We use bfd_zalloc here
2801 in case unused entries are not reclaimed before the section's
2802 contents are written out. This should not happen, but this way
2803 if it does, we will not write out garbage. For reloc sections,
2804 this will make entries have the type R_CRIS_NONE. */
2805 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2806 if (s->contents == NULL && s->_raw_size != 0)
2807 return false;
2810 if (elf_hash_table (info)->dynamic_sections_created)
2812 /* Add some entries to the .dynamic section. We fill in the
2813 values later, in elf_cris_finish_dynamic_sections, but we
2814 must add the entries now so that we get the correct size for
2815 the .dynamic section. The DT_DEBUG entry is filled in by the
2816 dynamic linker and used by the debugger. */
2817 if (!info->shared)
2819 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2820 return false;
2823 if (plt)
2825 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2826 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2827 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2828 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2829 return false;
2832 if (relocs)
2834 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2835 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2836 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2837 sizeof (Elf32_External_Rela)))
2838 return false;
2841 if (reltext)
2843 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2844 return false;
2845 info->flags |= DF_TEXTREL;
2849 return true;
2852 /* This function is called via elf_cris_link_hash_traverse if we are
2853 creating a shared object. In the -Bsymbolic case, it discards the
2854 space allocated to copy PC relative relocs against symbols which
2855 are defined in regular objects. For the normal non-symbolic case,
2856 we also discard space for relocs that have become local due to
2857 symbol visibility changes. We allocated space for them in the
2858 check_relocs routine, but we won't fill them in in the
2859 relocate_section routine. */
2861 static boolean
2862 elf_cris_discard_excess_dso_dynamics (h, inf)
2863 struct elf_cris_link_hash_entry *h;
2864 PTR inf;
2866 struct elf_cris_pcrel_relocs_copied *s;
2867 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2869 /* If a symbol has been forced local or we have found a regular
2870 definition for the symbolic link case, then we won't be needing
2871 any relocs. */
2872 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2873 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2874 || info->symbolic))
2876 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2877 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2880 return true;
2883 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2884 creating a shared object. We discard space for relocs for symbols put
2885 in the .got, but which we found we do not have to resolve at run-time. */
2887 static boolean
2888 elf_cris_discard_excess_program_dynamics (h, inf)
2889 struct elf_cris_link_hash_entry *h;
2890 PTR inf;
2892 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2894 /* If we're not creating a shared library and have a symbol which is
2895 referred to by .got references, but the symbol is defined locally,
2896 (or rather, not referred to by a DSO and not defined by a DSO) then
2897 lose the reloc for the .got (don't allocate room for it). */
2898 if ((h->root.elf_link_hash_flags
2899 & (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_DEF_DYNAMIC)) == 0)
2901 if (h->root.got.refcount > 0
2902 /* The size of this section is only valid and in sync with the
2903 various reference counts if we do dynamic; don't decrement it
2904 otherwise. */
2905 && elf_hash_table (info)->dynamic_sections_created)
2907 bfd *dynobj = elf_hash_table (info)->dynobj;
2908 asection *srelgot;
2910 BFD_ASSERT (dynobj != NULL);
2912 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2914 BFD_ASSERT (srelgot != NULL);
2916 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2919 /* If the locally-defined symbol isn't used by a DSO, then we don't
2920 have to export it as a dynamic symbol. This was already done for
2921 functions; doing this for all symbols would presumably not
2922 introduce new problems. */
2923 h->root.dynindx = -1;
2926 return true;
2929 /* Reject a file depending on presence and expectation of prefixed
2930 underscores on symbols. */
2932 static boolean
2933 cris_elf_object_p (abfd)
2934 bfd *abfd;
2936 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2937 return (bfd_get_symbol_leading_char (abfd) == '_');
2938 else
2939 return (bfd_get_symbol_leading_char (abfd) == 0);
2942 /* Mark presence or absence of leading underscore. */
2944 static void
2945 cris_elf_final_write_processing (abfd, linker)
2946 bfd *abfd;
2947 boolean linker ATTRIBUTE_UNUSED;
2949 if (bfd_get_symbol_leading_char (abfd) == '_')
2950 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2951 else
2952 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2955 /* Display the flags field. */
2957 static boolean
2958 cris_elf_print_private_bfd_data (abfd, ptr)
2959 bfd *abfd;
2960 PTR ptr;
2962 FILE *file = (FILE *) ptr;
2964 BFD_ASSERT (abfd != NULL && ptr != NULL)
2966 _bfd_elf_print_private_bfd_data (abfd, ptr);
2968 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2970 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2971 fprintf (file, _(" [symbols have a _ prefix]"));
2973 fputc ('\n', file);
2974 return true;
2977 /* Don't mix files with and without a leading underscore. */
2979 static boolean
2980 cris_elf_merge_private_bfd_data (ibfd, obfd)
2981 bfd *ibfd;
2982 bfd *obfd;
2984 flagword old_flags, new_flags;
2986 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2987 return false;
2989 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2990 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2991 return true;
2993 if (! elf_flags_init (obfd))
2995 /* This happens when ld starts out with a 'blank' output file. */
2996 elf_flags_init (obfd) = true;
2998 /* Set flags according to current bfd_target. */
2999 cris_elf_final_write_processing (obfd, false);
3002 old_flags = elf_elfheader (obfd)->e_flags;
3003 new_flags = elf_elfheader (ibfd)->e_flags;
3005 /* Is this good or bad? We'll follow with other excluding flags. */
3006 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
3008 (*_bfd_error_handler)
3009 ((new_flags & EF_CRIS_UNDERSCORE)
3010 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3011 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3012 bfd_get_filename (ibfd));
3013 bfd_set_error (bfd_error_bad_value);
3014 return false;
3017 return true;
3020 #define ELF_ARCH bfd_arch_cris
3021 #define ELF_MACHINE_CODE EM_CRIS
3022 #define ELF_MAXPAGESIZE 0x2000
3024 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3025 #define TARGET_LITTLE_NAME "elf32-cris"
3026 #define elf_symbol_leading_char 0
3028 #define elf_info_to_howto_rel NULL
3029 #define elf_info_to_howto cris_info_to_howto_rela
3030 #define elf_backend_relocate_section cris_elf_relocate_section
3031 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3032 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3033 #define elf_backend_check_relocs cris_elf_check_relocs
3035 #define elf_backend_can_gc_sections 1
3037 #define elf_backend_object_p cris_elf_object_p
3038 #define elf_backend_final_write_processing \
3039 cris_elf_final_write_processing
3040 #define bfd_elf32_bfd_print_private_bfd_data \
3041 cris_elf_print_private_bfd_data
3042 #define bfd_elf32_bfd_merge_private_bfd_data \
3043 cris_elf_merge_private_bfd_data
3045 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3047 #define bfd_elf32_bfd_link_hash_table_create \
3048 elf_cris_link_hash_table_create
3049 #define elf_backend_adjust_dynamic_symbol \
3050 elf_cris_adjust_dynamic_symbol
3051 #define elf_backend_size_dynamic_sections \
3052 elf_cris_size_dynamic_sections
3053 #define elf_backend_finish_dynamic_symbol \
3054 elf_cris_finish_dynamic_symbol
3055 #define elf_backend_finish_dynamic_sections \
3056 elf_cris_finish_dynamic_sections
3057 #define elf_backend_create_dynamic_sections \
3058 _bfd_elf_create_dynamic_sections
3059 #define bfd_elf32_bfd_final_link \
3060 _bfd_elf32_gc_common_final_link
3061 #define elf_backend_hide_symbol elf_cris_hide_symbol
3063 #define elf_backend_want_got_plt 1
3064 #define elf_backend_plt_readonly 1
3065 #define elf_backend_want_plt_sym 0
3066 #define elf_backend_got_header_size 12
3067 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3069 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3070 linking and libraries (if it's a win of any significance). Until then,
3071 take the easy route. */
3072 #define elf_backend_may_use_rel_p 0
3073 #define elf_backend_may_use_rela_p 1
3075 #include "elf32-target.h"
3077 #define INCLUDED_TARGET_FILE
3079 #undef TARGET_LITTLE_SYM
3080 #undef TARGET_LITTLE_NAME
3081 #undef elf_symbol_leading_char
3083 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3084 #define TARGET_LITTLE_NAME "elf32-us-cris"
3085 #define elf_symbol_leading_char '_'
3087 #include "elf32-target.h"