elf32-cris.c revision 1.1 1 /* CRIS-specific support for 32-bit ELF.
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Axis Communications AB.
5 Written by Hans-Peter Nilsson, based on elf32-fr30.c
6 PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/cris.h"
30
31 /* Forward declarations. */
32 static reloc_howto_type * cris_reloc_type_lookup
33 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
34
35 static void cris_info_to_howto_rela
36 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
37
38 static bfd_reloc_status_type cris_elf_pcrel_reloc
39 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
40
41 static bfd_boolean cris_elf_grok_prstatus
42 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
43
44 static bfd_boolean cris_elf_grok_psinfo
45 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
46
47 static bfd_boolean cris_elf_relocate_section
48 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
49 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
50
51 static bfd_reloc_status_type cris_final_link_relocate
52 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
53 Elf_Internal_Rela *, bfd_vma));
54
55 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
56
57 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
58
59 static bfd_boolean cris_elf_set_mach_from_flags
60 PARAMS ((bfd *, unsigned long int));
61
62 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
63
64 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
65 static bfd_boolean cris_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
66
67 struct elf_cris_link_hash_entry;
68 static bfd_boolean elf_cris_discard_excess_dso_dynamics
69 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
70 static bfd_boolean elf_cris_discard_excess_program_dynamics
71 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
72 static bfd_boolean elf_cris_adjust_gotplt_to_got
73 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
74 static bfd_boolean elf_cris_try_fold_plt_to_got
75 PARAMS ((struct elf_cris_link_hash_entry *, PTR));
76 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
77 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
78 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
79 PARAMS ((bfd *));
80 static bfd_boolean elf_cris_adjust_dynamic_symbol
81 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
82 static bfd_boolean cris_elf_check_relocs
83 PARAMS ((bfd *, struct bfd_link_info *, asection *,
84 const Elf_Internal_Rela *));
85
86 static bfd_boolean elf_cris_size_dynamic_sections
87 PARAMS ((bfd *, struct bfd_link_info *));
88 static bfd_boolean elf_cris_finish_dynamic_symbol
89 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
90 Elf_Internal_Sym *));
91 static bfd_boolean elf_cris_finish_dynamic_sections
92 PARAMS ((bfd *, struct bfd_link_info *));
93 static void elf_cris_hide_symbol
94 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
95 static enum elf_reloc_type_class elf_cris_reloc_type_class
96 PARAMS ((const Elf_Internal_Rela *));
97
98 static reloc_howto_type cris_elf_howto_table [] =
99 {
100 /* This reloc does nothing. */
101 HOWTO (R_CRIS_NONE, /* type */
102 0, /* rightshift */
103 2, /* size (0 = byte, 1 = short, 2 = long) */
104 32, /* bitsize */
105 FALSE, /* pc_relative */
106 0, /* bitpos */
107 complain_overflow_bitfield, /* complain_on_overflow */
108 bfd_elf_generic_reloc, /* special_function */
109 "R_CRIS_NONE", /* name */
110 FALSE, /* partial_inplace */
111 0, /* src_mask */
112 0, /* dst_mask */
113 FALSE), /* pcrel_offset */
114
115 /* An 8 bit absolute relocation. */
116 HOWTO (R_CRIS_8, /* type */
117 0, /* rightshift */
118 0, /* size (0 = byte, 1 = short, 2 = long) */
119 8, /* bitsize */
120 FALSE, /* pc_relative */
121 0, /* bitpos */
122 complain_overflow_bitfield, /* complain_on_overflow */
123 bfd_elf_generic_reloc, /* special_function */
124 "R_CRIS_8", /* name */
125 FALSE, /* partial_inplace */
126 0x0000, /* src_mask */
127 0x00ff, /* dst_mask */
128 FALSE), /* pcrel_offset */
129
130 /* A 16 bit absolute relocation. */
131 HOWTO (R_CRIS_16, /* type */
132 0, /* rightshift */
133 1, /* size (0 = byte, 1 = short, 2 = long) */
134 16, /* bitsize */
135 FALSE, /* pc_relative */
136 0, /* bitpos */
137 complain_overflow_bitfield, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_CRIS_16", /* name */
140 FALSE, /* partial_inplace */
141 0x00000000, /* src_mask */
142 0x0000ffff, /* dst_mask */
143 FALSE), /* pcrel_offset */
144
145 /* A 32 bit absolute relocation. */
146 HOWTO (R_CRIS_32, /* type */
147 0, /* rightshift */
148 2, /* size (0 = byte, 1 = short, 2 = long) */
149 32, /* bitsize */
150 FALSE, /* pc_relative */
151 0, /* bitpos */
152 /* We don't want overflow complaints for 64-bit vma builds
153 for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
154 32-bit ELF) where sym=0xc0001234.
155 Don't do this for the PIC relocs, as we don't expect to
156 see them with large offsets. */
157 complain_overflow_dont, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_CRIS_32", /* name */
160 FALSE, /* partial_inplace */
161 0x00000000, /* src_mask */
162 0xffffffff, /* dst_mask */
163 FALSE), /* pcrel_offset */
164
165 /* An 8 bit PC-relative relocation. */
166 HOWTO (R_CRIS_8_PCREL, /* type */
167 0, /* rightshift */
168 0, /* size (0 = byte, 1 = short, 2 = long) */
169 8, /* bitsize */
170 TRUE, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 cris_elf_pcrel_reloc, /* special_function */
174 "R_CRIS_8_PCREL", /* name */
175 FALSE, /* partial_inplace */
176 0x0000, /* src_mask */
177 0x00ff, /* dst_mask */
178 TRUE), /* pcrel_offset */
179
180 /* A 16 bit PC-relative relocation. */
181 HOWTO (R_CRIS_16_PCREL, /* type */
182 0, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 16, /* bitsize */
185 TRUE, /* pc_relative */
186 0, /* bitpos */
187 complain_overflow_bitfield, /* complain_on_overflow */
188 cris_elf_pcrel_reloc, /* special_function */
189 "R_CRIS_16_PCREL", /* name */
190 FALSE, /* partial_inplace */
191 0x00000000, /* src_mask */
192 0x0000ffff, /* dst_mask */
193 TRUE), /* pcrel_offset */
194
195 /* A 32 bit PC-relative relocation. */
196 HOWTO (R_CRIS_32_PCREL, /* type */
197 0, /* rightshift */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
199 32, /* bitsize */
200 TRUE, /* pc_relative */
201 0, /* bitpos */
202 complain_overflow_bitfield, /* complain_on_overflow */
203 cris_elf_pcrel_reloc, /* special_function */
204 "R_CRIS_32_PCREL", /* name */
205 FALSE, /* partial_inplace */
206 0x00000000, /* src_mask */
207 0xffffffff, /* dst_mask */
208 TRUE), /* pcrel_offset */
209
210 /* GNU extension to record C++ vtable hierarchy. */
211 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
212 0, /* rightshift */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
214 0, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 NULL, /* special_function */
219 "R_CRIS_GNU_VTINHERIT", /* name */
220 FALSE, /* partial_inplace */
221 0, /* src_mask */
222 0, /* dst_mask */
223 FALSE), /* pcrel_offset */
224
225 /* GNU extension to record C++ vtable member usage. */
226 HOWTO (R_CRIS_GNU_VTENTRY, /* type */
227 0, /* rightshift */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
229 0, /* bitsize */
230 FALSE, /* pc_relative */
231 0, /* bitpos */
232 complain_overflow_dont, /* complain_on_overflow */
233 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
234 "R_CRIS_GNU_VTENTRY", /* name */
235 FALSE, /* partial_inplace */
236 0, /* src_mask */
237 0, /* dst_mask */
238 FALSE), /* pcrel_offset */
239
240 /* This is used only by the dynamic linker. The symbol should exist
241 both in the object being run and in some shared library. The
242 dynamic linker copies the data addressed by the symbol from the
243 shared library into the object, because the object being
244 run has to have the data at some particular address. */
245 HOWTO (R_CRIS_COPY, /* type */
246 0, /* rightshift */
247 2, /* size (0 = byte, 1 = short, 2 = long) */
248 32, /* bitsize */
249 FALSE, /* pc_relative */
250 0, /* bitpos */
251 complain_overflow_bitfield, /* complain_on_overflow */
252 bfd_elf_generic_reloc, /* special_function */
253 "R_CRIS_COPY", /* name */
254 FALSE, /* partial_inplace */
255 0, /* src_mask */
256 0, /* dst_mask */
257 FALSE), /* pcrel_offset */
258
259 /* Like R_CRIS_32, but used when setting global offset table entries. */
260 HOWTO (R_CRIS_GLOB_DAT, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 32, /* bitsize */
264 FALSE, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_CRIS_GLOB_DAT", /* name */
269 FALSE, /* partial_inplace */
270 0, /* src_mask */
271 0xffffffff, /* dst_mask */
272 FALSE), /* pcrel_offset */
273
274 /* Marks a procedure linkage table entry for a symbol. */
275 HOWTO (R_CRIS_JUMP_SLOT, /* type */
276 0, /* rightshift */
277 2, /* size (0 = byte, 1 = short, 2 = long) */
278 32, /* bitsize */
279 FALSE, /* pc_relative */
280 0, /* bitpos */
281 complain_overflow_bitfield, /* complain_on_overflow */
282 bfd_elf_generic_reloc, /* special_function */
283 "R_CRIS_JUMP_SLOT", /* name */
284 FALSE, /* partial_inplace */
285 0, /* src_mask */
286 0, /* dst_mask */
287 FALSE), /* pcrel_offset */
288
289 /* Used only by the dynamic linker. When the object is run, this
290 longword is set to the load address of the object, plus the
291 addend. */
292 HOWTO (R_CRIS_RELATIVE, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 32, /* bitsize */
296 FALSE, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_bitfield, /* complain_on_overflow */
299 bfd_elf_generic_reloc, /* special_function */
300 "R_CRIS_RELATIVE", /* name */
301 FALSE, /* partial_inplace */
302 0, /* src_mask */
303 0xffffffff, /* dst_mask */
304 FALSE), /* pcrel_offset */
305
306 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
307 HOWTO (R_CRIS_16_GOT, /* type */
308 0, /* rightshift */
309 1, /* size (0 = byte, 1 = short, 2 = long) */
310 16, /* bitsize */
311 FALSE, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_bitfield, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_CRIS_16_GOT", /* name */
316 FALSE, /* partial_inplace */
317 0, /* src_mask */
318 0xffff, /* dst_mask */
319 FALSE), /* pcrel_offset */
320
321 HOWTO (R_CRIS_32_GOT, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 32, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_CRIS_32_GOT", /* name */
330 FALSE, /* partial_inplace */
331 0, /* src_mask */
332 0xffffffff, /* dst_mask */
333 FALSE), /* pcrel_offset */
334
335 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
336 the GOT table for the symbol. */
337 HOWTO (R_CRIS_16_GOTPLT, /* type */
338 0, /* rightshift */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
341 FALSE, /* pc_relative */
342 0, /* bitpos */
343 complain_overflow_bitfield, /* complain_on_overflow */
344 bfd_elf_generic_reloc, /* special_function */
345 "R_CRIS_16_GOTPLT", /* name */
346 FALSE, /* partial_inplace */
347 0, /* src_mask */
348 0xffff, /* dst_mask */
349 FALSE), /* pcrel_offset */
350
351 HOWTO (R_CRIS_32_GOTPLT, /* type */
352 0, /* rightshift */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
354 32, /* bitsize */
355 FALSE, /* pc_relative */
356 0, /* bitpos */
357 complain_overflow_bitfield, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_CRIS_32_GOTPLT", /* name */
360 FALSE, /* partial_inplace */
361 0, /* src_mask */
362 0xffffffff, /* dst_mask */
363 FALSE), /* pcrel_offset */
364
365 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
366 be necessary. */
367 HOWTO (R_CRIS_32_GOTREL, /* type */
368 0, /* rightshift */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
370 32, /* bitsize */
371 FALSE, /* pc_relative */
372 0, /* bitpos */
373 complain_overflow_bitfield, /* complain_on_overflow */
374 bfd_elf_generic_reloc, /* special_function */
375 "R_CRIS_32_GOTREL", /* name */
376 FALSE, /* partial_inplace */
377 0, /* src_mask */
378 0xffffffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
380
381 /* A 32-bit offset from GOT to entry for this symbol in PLT and request
382 to create PLT entry for symbol. */
383 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
384 0, /* rightshift */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
386 32, /* bitsize */
387 FALSE, /* pc_relative */
388 0, /* bitpos */
389 complain_overflow_bitfield, /* complain_on_overflow */
390 bfd_elf_generic_reloc, /* special_function */
391 "R_CRIS_32_PLT_GOTREL", /* name */
392 FALSE, /* partial_inplace */
393 0, /* src_mask */
394 0xffffffff, /* dst_mask */
395 FALSE), /* pcrel_offset */
396
397 /* A 32-bit offset from PC (location after the relocation) + addend to
398 entry for this symbol in PLT and request to create PLT entry for
399 symbol. */
400 HOWTO (R_CRIS_32_PLT_PCREL, /* type */
401 0, /* rightshift */
402 2, /* size (0 = byte, 1 = short, 2 = long) */
403 32, /* bitsize */
404 TRUE, /* pc_relative */
405 0, /* bitpos */
406 complain_overflow_bitfield, /* complain_on_overflow */
407 cris_elf_pcrel_reloc, /* special_function */
408 "R_CRIS_32_PLT_PCREL", /* name */
409 FALSE, /* partial_inplace */
410 0, /* src_mask */
411 0xffffffff, /* dst_mask */
412 TRUE) /* pcrel_offset */
413 };
414
415 /* Map BFD reloc types to CRIS ELF reloc types. */
417
418 struct cris_reloc_map
419 {
420 bfd_reloc_code_real_type bfd_reloc_val;
421 unsigned int cris_reloc_val;
422 };
423
424 static const struct cris_reloc_map cris_reloc_map [] =
425 {
426 { BFD_RELOC_NONE, R_CRIS_NONE },
427 { BFD_RELOC_8, R_CRIS_8 },
428 { BFD_RELOC_16, R_CRIS_16 },
429 { BFD_RELOC_32, R_CRIS_32 },
430 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
431 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
432 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
433 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
434 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
435 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
436 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
437 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
438 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
439 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
440 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
441 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
442 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
443 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
444 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
445 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
446 };
447
448 static reloc_howto_type *
449 cris_reloc_type_lookup (abfd, code)
450 bfd * abfd ATTRIBUTE_UNUSED;
451 bfd_reloc_code_real_type code;
452 {
453 unsigned int i;
454
455 for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
456 if (cris_reloc_map [i].bfd_reloc_val == code)
457 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
458
459 return NULL;
460 }
461
462 static reloc_howto_type *
463 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
464 {
465 unsigned int i;
466
467 for (i = 0;
468 i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
469 i++)
470 if (cris_elf_howto_table[i].name != NULL
471 && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
472 return &cris_elf_howto_table[i];
473
474 return NULL;
475 }
476
477 /* Set the howto pointer for an CRIS ELF reloc. */
478
479 static void
480 cris_info_to_howto_rela (abfd, cache_ptr, dst)
481 bfd * abfd ATTRIBUTE_UNUSED;
482 arelent * cache_ptr;
483 Elf_Internal_Rela * dst;
484 {
485 unsigned int r_type;
486
487 r_type = ELF32_R_TYPE (dst->r_info);
488 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
489 cache_ptr->howto = & cris_elf_howto_table [r_type];
490 }
491
492 bfd_reloc_status_type
493 cris_elf_pcrel_reloc (abfd, reloc_entry, symbol, data, input_section,
494 output_bfd, error_message)
495 bfd *abfd ATTRIBUTE_UNUSED;
496 arelent *reloc_entry;
497 asymbol *symbol;
498 PTR data ATTRIBUTE_UNUSED;
499 asection *input_section;
500 bfd *output_bfd;
501 char **error_message ATTRIBUTE_UNUSED;
502 {
503 /* By default (using only bfd_elf_generic_reloc when linking to
504 non-ELF formats) PC-relative relocs are relative to the beginning
505 of the reloc. CRIS PC-relative relocs are relative to the position
506 *after* the reloc because that's what pre-CRISv32 PC points to
507 after reading an insn field with that reloc. (For CRISv32, PC is
508 actually relative to the start of the insn, but we keep the old
509 definition.) Still, we use as much generic machinery as we can.
510
511 Only adjust when doing a final link. */
512 if (output_bfd == (bfd *) NULL)
513 reloc_entry->addend -= 1 << reloc_entry->howto->size;
514
515 return
516 bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
517 input_section, output_bfd, error_message);
518 }
519
520 /* Support for core dump NOTE sections.
522 The slightly unintuitive code layout is an attempt to keep at least
523 some similarities with other ports, hoping to simplify general
524 changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart. */
525
526 static bfd_boolean
527 cris_elf_grok_prstatus (abfd, note)
528 bfd *abfd;
529 Elf_Internal_Note *note;
530 {
531 int offset;
532 size_t size;
533
534 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
535 switch (note->descsz)
536 {
537 default:
538 return FALSE;
539
540 case 202: /* Linux/CRISv32 */
541 /* pr_cursig */
542 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
543
544 /* pr_pid */
545 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
546
547 /* pr_reg */
548 offset = 70;
549 size = 128;
550
551 break;
552 }
553 else
554 switch (note->descsz)
555 {
556 default:
557 return FALSE;
558
559 case 214: /* Linux/CRIS */
560 /* pr_cursig */
561 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
562
563 /* pr_pid */
564 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
565
566 /* pr_reg */
567 offset = 70;
568 size = 140;
569
570 break;
571 }
572
573 /* Make a ".reg/999" section. */
574 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
575 size, note->descpos + offset);
576 }
577
578 static bfd_boolean
579 cris_elf_grok_psinfo (abfd, note)
580 bfd *abfd;
581 Elf_Internal_Note *note;
582 {
583 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
584 switch (note->descsz)
585 {
586 default:
587 return FALSE;
588
589 case 124: /* Linux/CRISv32 elf_prpsinfo */
590 elf_tdata (abfd)->core_program
591 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
592 elf_tdata (abfd)->core_command
593 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
594 }
595 else
596 switch (note->descsz)
597 {
598 default:
599 return FALSE;
600
601 case 124: /* Linux/CRIS elf_prpsinfo */
602 elf_tdata (abfd)->core_program
603 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
604 elf_tdata (abfd)->core_command
605 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
606 }
607
608 /* Note that for some reason, a spurious space is tacked
609 onto the end of the args in some (at least one anyway)
610 implementations, so strip it off if it exists. */
611
612 {
613 char *command = elf_tdata (abfd)->core_command;
614 int n = strlen (command);
615
616 if (0 < n && command[n - 1] == ' ')
617 command[n - 1] = '\0';
618 }
619
620 return TRUE;
621 }
622
623 /* The name of the dynamic interpreter. This is put in the .interp
625 section. */
626
627 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
628
629 /* The size in bytes of an entry in the procedure linkage table. */
630
631 #define PLT_ENTRY_SIZE 20
632 #define PLT_ENTRY_SIZE_V32 26
633
634 /* The first entry in an absolute procedure linkage table looks like this. */
635
636 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
637 {
638 0xfc, 0xe1,
639 0x7e, 0x7e, /* push mof. */
640 0x7f, 0x0d, /* (dip [pc+]) */
641 0, 0, 0, 0, /* Replaced with address of .got + 4. */
642 0x30, 0x7a, /* move [...],mof */
643 0x7f, 0x0d, /* (dip [pc+]) */
644 0, 0, 0, 0, /* Replaced with address of .got + 8. */
645 0x30, 0x09 /* jump [...] */
646 };
647
648 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
649 {
650 0x84, 0xe2, /* subq 4,$sp */
651 0x6f, 0xfe, /* move.d 0,$acr */
652 0, 0, 0, 0, /* Replaced by address of .got + 4. */
653 0x7e, 0x7a, /* move $mof,[$sp] */
654 0x3f, 0x7a, /* move [$acr],$mof */
655 0x04, 0xf2, /* addq 4,acr */
656 0x6f, 0xfa, /* move.d [$acr],$acr */
657 0xbf, 0x09, /* jump $acr */
658 0xb0, 0x05, /* nop */
659 0, 0 /* Pad out to 26 bytes. */
660 };
661
662 /* Subsequent entries in an absolute procedure linkage table look like
663 this. */
664
665 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
666 {
667 0x7f, 0x0d, /* (dip [pc+]) */
668 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
669 0x30, 0x09, /* jump [...] */
670 0x3f, 0x7e, /* move [pc+],mof */
671 0, 0, 0, 0, /* Replaced with offset into relocation table. */
672 0x2f, 0xfe, /* add.d [pc+],pc */
673 0xec, 0xff,
674 0xff, 0xff /* Replaced with offset to start of .plt. */
675 };
676
677 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
678 {
679 0x6f, 0xfe, /* move.d 0,$acr */
680 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
681 0x6f, 0xfa, /* move.d [$acr],$acr */
682 0xbf, 0x09, /* jump $acr */
683 0xb0, 0x05, /* nop */
684 0x3f, 0x7e, /* move 0,mof */
685 0, 0, 0, 0, /* Replaced with offset into relocation table. */
686 0xbf, 0x0e, /* ba start_of_plt0_entry */
687 0, 0, 0, 0, /* Replaced with offset to plt0 entry. */
688 0xb0, 0x05 /* nop */
689 };
690
691 /* The first entry in a PIC procedure linkage table looks like this. */
692
693 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
694 {
695 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
696 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
697 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
698 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
699 };
700
701 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
702 {
703 0x84, 0xe2, /* subq 4,$sp */
704 0x04, 0x01, /* addoq 4,$r0,$acr */
705 0x7e, 0x7a, /* move $mof,[$sp] */
706 0x3f, 0x7a, /* move [$acr],$mof */
707 0x04, 0xf2, /* addq 4,$acr */
708 0x6f, 0xfa, /* move.d [$acr],$acr */
709 0xbf, 0x09, /* jump $acr */
710 0xb0, 0x05, /* nop */
711 0, 0, /* Pad out to 26 bytes. */
712 0, 0, 0, 0,
713 0, 0, 0, 0
714 };
715
716 /* Subsequent entries in a PIC procedure linkage table look like this. */
717
718 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
719 {
720 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
721 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
722 0x30, 0x09, /* jump [...] */
723 0x3f, 0x7e, /* move [pc+],mof */
724 0, 0, 0, 0, /* Replaced with offset into relocation table. */
725 0x2f, 0xfe, /* add.d [pc+],pc */
726 0xec, 0xff, /* Replaced with offset to start of .plt. */
727 0xff, 0xff
728 };
729
730 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
731 {
732 0x6f, 0x0d, /* addo.d 0,$r0,$acr */
733 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
734 0x6f, 0xfa, /* move.d [$acr],$acr */
735 0xbf, 0x09, /* jump $acr */
736 0xb0, 0x05, /* nop */
737 0x3f, 0x7e, /* move relocoffs,$mof */
738 0, 0, 0, 0, /* Replaced with offset into relocation table. */
739 0xbf, 0x0e, /* ba start_of_plt */
740 0, 0, 0, 0, /* Replaced with offset to start of .plt. */
741 0xb0, 0x05 /* nop */
742 };
743
744 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
746 (and most other PIC/shlib stuff). Check that we don't drift away
747 without reason.
748
749 The CRIS linker, like the m68k and i386 linkers (and probably the rest
750 too) needs to keep track of the number of relocs that it decides to
751 copy in check_relocs for each symbol. This is so that it can discard
752 PC relative relocs if it doesn't need them when linking with
753 -Bsymbolic. We store the information in a field extending the regular
754 ELF linker hash table. */
755
756 /* This structure keeps track of the number of PC relative relocs we have
757 copied for a given symbol. */
758
759 struct elf_cris_pcrel_relocs_copied
760 {
761 /* Next section. */
762 struct elf_cris_pcrel_relocs_copied *next;
763 /* A section in dynobj. */
764 asection *section;
765 /* Number of relocs copied in this section. */
766 bfd_size_type count;
767 };
768
769 /* CRIS ELF linker hash entry. */
770
771 struct elf_cris_link_hash_entry
772 {
773 struct elf_link_hash_entry root;
774
775 /* Number of PC relative relocs copied for this symbol. */
776 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
777
778 /* The GOTPLT references are CRIS-specific; the goal is to avoid having
779 both a general GOT and a PLT-specific GOT entry for the same symbol,
780 when it is referenced both as a function and as a function pointer.
781
782 Number of GOTPLT references for a function. */
783 bfd_signed_vma gotplt_refcount;
784
785 /* Actual GOTPLT index for this symbol, if applicable, or zero if not
786 (zero is never used as an index). FIXME: We should be able to fold
787 this with gotplt_refcount in a union, like the got and plt unions in
788 elf_link_hash_entry. */
789 bfd_size_type gotplt_offset;
790 };
791
792 /* CRIS ELF linker hash table. */
793
794 struct elf_cris_link_hash_table
795 {
796 struct elf_link_hash_table root;
797
798 /* We can't use the PLT offset and calculate to get the GOTPLT offset,
799 since we try and avoid creating GOTPLT:s when there's already a GOT.
800 Instead, we keep and update the next available index here. */
801 bfd_size_type next_gotplt_entry;
802 };
803
804 /* Traverse a CRIS ELF linker hash table. */
805
806 #define elf_cris_link_hash_traverse(table, func, info) \
807 (elf_link_hash_traverse \
808 (&(table)->root, \
809 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
810 (info)))
811
812 /* Get the CRIS ELF linker hash table from a link_info structure. */
813
814 #define elf_cris_hash_table(p) \
815 ((struct elf_cris_link_hash_table *) (p)->hash)
816
817 /* Create an entry in a CRIS ELF linker hash table. */
818
819 static struct bfd_hash_entry *
820 elf_cris_link_hash_newfunc (entry, table, string)
821 struct bfd_hash_entry *entry;
822 struct bfd_hash_table *table;
823 const char *string;
824 {
825 struct elf_cris_link_hash_entry *ret =
826 (struct elf_cris_link_hash_entry *) entry;
827
828 /* Allocate the structure if it has not already been allocated by a
829 subclass. */
830 if (ret == (struct elf_cris_link_hash_entry *) NULL)
831 ret = ((struct elf_cris_link_hash_entry *)
832 bfd_hash_allocate (table,
833 sizeof (struct elf_cris_link_hash_entry)));
834 if (ret == (struct elf_cris_link_hash_entry *) NULL)
835 return (struct bfd_hash_entry *) ret;
836
837 /* Call the allocation method of the superclass. */
838 ret = ((struct elf_cris_link_hash_entry *)
839 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
840 table, string));
841 if (ret != (struct elf_cris_link_hash_entry *) NULL)
842 {
843 ret->pcrel_relocs_copied = NULL;
844 ret->gotplt_refcount = 0;
845 ret->gotplt_offset = 0;
846 }
847
848 return (struct bfd_hash_entry *) ret;
849 }
850
851 /* Create a CRIS ELF linker hash table. */
852
853 static struct bfd_link_hash_table *
854 elf_cris_link_hash_table_create (abfd)
855 bfd *abfd;
856 {
857 struct elf_cris_link_hash_table *ret;
858 bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
859
860 ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt));
861 if (ret == (struct elf_cris_link_hash_table *) NULL)
862 return NULL;
863
864 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
865 elf_cris_link_hash_newfunc,
866 sizeof (struct elf_cris_link_hash_entry)))
867 {
868 free (ret);
869 return NULL;
870 }
871
872 /* Initialize to skip over the first three entries in the gotplt; they
873 are used for run-time symbol evaluation. */
874 ret->next_gotplt_entry = 12;
875
876 return &ret->root.root;
877 }
878
879 /* Perform a single relocation. By default we use the standard BFD
881 routines, with a few tweaks. */
882
883 static bfd_reloc_status_type
884 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
885 relocation)
886 reloc_howto_type * howto;
887 bfd * input_bfd;
888 asection * input_section;
889 bfd_byte * contents;
890 Elf_Internal_Rela * rel;
891 bfd_vma relocation;
892 {
893 bfd_reloc_status_type r;
894
895 /* PC-relative relocations are relative to the position *after*
896 the reloc. Note that for R_CRIS_8_PCREL the adjustment is
897 not a single byte, since PC must be 16-bit-aligned. */
898 switch (ELF32_R_TYPE (rel->r_info))
899 {
900 /* Check that the 16-bit GOT relocs are positive. */
901 case R_CRIS_16_GOTPLT:
902 case R_CRIS_16_GOT:
903 if ((bfd_signed_vma) relocation < 0)
904 return bfd_reloc_overflow;
905 break;
906
907 case R_CRIS_32_PLT_PCREL:
908 case R_CRIS_32_PCREL:
909 relocation -= 2;
910 /* Fall through. */
911 case R_CRIS_8_PCREL:
912 case R_CRIS_16_PCREL:
913 relocation -= 2;
914 break;
915
916 default:
917 break;
918 }
919
920 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
921 contents, rel->r_offset,
922 relocation, rel->r_addend);
923 return r;
924 }
925
926 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
928 copied, for further comments. */
929
930 static bfd_boolean
931 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
932 contents, relocs, local_syms, local_sections)
933 bfd *output_bfd ATTRIBUTE_UNUSED;
934 struct bfd_link_info *info;
935 bfd *input_bfd;
936 asection *input_section;
937 bfd_byte *contents;
938 Elf_Internal_Rela *relocs;
939 Elf_Internal_Sym *local_syms;
940 asection **local_sections;
941 {
942 bfd *dynobj;
943 Elf_Internal_Shdr *symtab_hdr;
944 struct elf_link_hash_entry **sym_hashes;
945 bfd_vma *local_got_offsets;
946 asection *sgot;
947 asection *splt;
948 asection *sreloc;
949 Elf_Internal_Rela *rel;
950 Elf_Internal_Rela *relend;
951
952 dynobj = elf_hash_table (info)->dynobj;
953 local_got_offsets = elf_local_got_offsets (input_bfd);
954 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
955 sym_hashes = elf_sym_hashes (input_bfd);
956 relend = relocs + input_section->reloc_count;
957
958 sgot = NULL;
959 splt = NULL;
960 sreloc = NULL;
961
962 if (dynobj != NULL)
963 {
964 splt = bfd_get_section_by_name (dynobj, ".plt");
965 sgot = bfd_get_section_by_name (dynobj, ".got");
966 }
967
968 for (rel = relocs; rel < relend; rel ++)
969 {
970 reloc_howto_type *howto;
971 unsigned long r_symndx;
972 Elf_Internal_Sym *sym;
973 asection *sec;
974 struct elf_link_hash_entry *h;
975 bfd_vma relocation;
976 bfd_reloc_status_type r;
977 const char *symname = NULL;
978 int r_type;
979
980 r_type = ELF32_R_TYPE (rel->r_info);
981
982 if ( r_type == R_CRIS_GNU_VTINHERIT
983 || r_type == R_CRIS_GNU_VTENTRY)
984 continue;
985
986 r_symndx = ELF32_R_SYM (rel->r_info);
987 howto = cris_elf_howto_table + r_type;
988 h = NULL;
989 sym = NULL;
990 sec = NULL;
991
992 if (r_symndx < symtab_hdr->sh_info)
993 {
994 sym = local_syms + r_symndx;
995 sec = local_sections [r_symndx];
996 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
997
998 symname = (bfd_elf_string_from_elf_section
999 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1000 if (symname == NULL)
1001 symname = bfd_section_name (input_bfd, sec);
1002 }
1003 else
1004 {
1005 bfd_boolean warned;
1006 bfd_boolean unresolved_reloc;
1007
1008 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1009 r_symndx, symtab_hdr, sym_hashes,
1010 h, sec, relocation,
1011 unresolved_reloc, warned);
1012
1013 if (unresolved_reloc
1014 /* Perhaps we should detect the cases that
1015 sec->output_section is expected to be NULL like i386 and
1016 m68k, but apparently (and according to elfxx-ia64.c) all
1017 valid cases are where the symbol is defined in a shared
1018 object which we link dynamically against. This includes
1019 PLT relocs for which we've created a PLT entry and other
1020 relocs for which we're prepared to create dynamic
1021 relocations.
1022
1023 For now, new situations cause us to just err when
1024 sec->output_offset is NULL but the object with the symbol
1025 is *not* dynamically linked against. Thus this will
1026 automatically remind us so we can see if there are other
1027 valid cases we need to revisit. */
1028 && (sec->owner->flags & DYNAMIC) != 0)
1029 relocation = 0;
1030
1031 else if (h->root.type == bfd_link_hash_defined
1032 || h->root.type == bfd_link_hash_defweak)
1033 {
1034 /* Here follow the cases where the relocation value must
1035 be zero (or when further handling is simplified when
1036 zero). I can't claim to understand the various
1037 conditions and they weren't described in the files
1038 where I copied them from (elf32-m68k.c and
1039 elf32-i386.c), but let's mention examples of where
1040 they happen. FIXME: Perhaps define and use a
1041 dynamic_symbol_p function like ia64.
1042
1043 - When creating a shared library, we can have an
1044 ordinary relocation for a symbol defined in a shared
1045 library (perhaps the one we create). We then make
1046 the relocation value zero, as the value seen now will
1047 be added into the relocation addend in this shared
1048 library, but must be handled only at dynamic-link
1049 time. FIXME: Not sure this example covers the
1050 h->elf_link_hash_flags test, though it's there in
1051 other targets. */
1052 if (info->shared
1053 && ((! info->symbolic && h->dynindx != -1)
1054 || !h->def_regular)
1055 && (input_section->flags & SEC_ALLOC) != 0
1056 && (r_type == R_CRIS_8
1057 || r_type == R_CRIS_16
1058 || r_type == R_CRIS_32
1059 || r_type == R_CRIS_8_PCREL
1060 || r_type == R_CRIS_16_PCREL
1061 || r_type == R_CRIS_32_PCREL))
1062 relocation = 0;
1063 else if (!info->relocatable && unresolved_reloc)
1064 {
1065 _bfd_error_handler
1066 (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1067 input_bfd,
1068 input_section,
1069 cris_elf_howto_table[r_type].name,
1070 symname);
1071 bfd_set_error (bfd_error_bad_value);
1072 return FALSE;
1073 }
1074 }
1075 }
1076
1077 if (sec != NULL && elf_discarded_section (sec))
1078 {
1079 /* For relocs against symbols from removed linkonce sections,
1080 or sections discarded by a linker script, we just want the
1081 section contents zeroed. Avoid any special processing. */
1082 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1083 rel->r_info = 0;
1084 rel->r_addend = 0;
1085 continue;
1086 }
1087
1088 if (info->relocatable)
1089 continue;
1090
1091 switch (r_type)
1092 {
1093 case R_CRIS_16_GOTPLT:
1094 case R_CRIS_32_GOTPLT:
1095 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1096 but we require a PLT, and the PLT handling will take care of
1097 filling in the PLT-specific GOT entry. For the GOT offset,
1098 calculate it as we do when filling it in for the .got.plt
1099 section. If we don't have a PLT, punt to GOT handling. */
1100 if (h != NULL
1101 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1102 {
1103 asection *sgotplt
1104 = bfd_get_section_by_name (dynobj, ".got.plt");
1105 bfd_vma got_offset;
1106
1107 BFD_ASSERT (h->dynindx != -1);
1108 BFD_ASSERT (sgotplt != NULL);
1109
1110 got_offset
1111 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1112
1113 relocation = got_offset;
1114 break;
1115 }
1116
1117 /* We didn't make a PLT entry for this symbol. Maybe everything is
1118 folded into the GOT. Other than folding, this happens when
1119 statically linking PIC code, or when using -Bsymbolic. Check
1120 that we instead have a GOT entry as done for us by
1121 elf_cris_adjust_dynamic_symbol, and drop through into the
1122 ordinary GOT cases. This must not happen for the
1123 executable, because any reference it does to a function
1124 that is satisfied by a DSO must generate a PLT. We assume
1125 these call-specific relocs don't address non-functions. */
1126 if (h != NULL
1127 && (h->got.offset == (bfd_vma) -1
1128 || (!info->shared
1129 && !(h->def_regular
1130 || (!h->def_dynamic
1131 && h->root.type == bfd_link_hash_undefweak)))))
1132 {
1133 (*_bfd_error_handler)
1134 ((h->got.offset == (bfd_vma) -1)
1135 ? _("%B, section %A: No PLT nor GOT for relocation %s"
1136 " against symbol `%s'")
1137 : _("%B, section %A: No PLT for relocation %s"
1138 " against symbol `%s'"),
1139 input_bfd,
1140 input_section,
1141 cris_elf_howto_table[r_type].name,
1142 (symname != NULL && symname[0] != '\0'
1143 ? symname : _("[whose name is lost]")));
1144
1145 /* FIXME: Perhaps blaming input is not the right thing to
1146 do; this is probably an internal error. But it is true
1147 that we didn't like that particular input. */
1148 bfd_set_error (bfd_error_bad_value);
1149 return FALSE;
1150 }
1151 /* Fall through. */
1152
1153 /* The size of the actual relocation is not used here; we only
1154 fill in the GOT table here. */
1155 case R_CRIS_16_GOT:
1156 case R_CRIS_32_GOT:
1157 {
1158 bfd_vma off;
1159
1160 /* Note that despite using RELA relocations, the .got contents
1161 is always filled in with the link-relative relocation
1162 value; the addend. */
1163
1164 if (h != NULL)
1165 {
1166 off = h->got.offset;
1167 BFD_ASSERT (off != (bfd_vma) -1);
1168
1169 if (!elf_hash_table (info)->dynamic_sections_created
1170 || (! info->shared
1171 && (h->def_regular
1172 || h->type == STT_FUNC
1173 || h->needs_plt))
1174 || (info->shared
1175 && (info->symbolic || h->dynindx == -1)
1176 && h->def_regular))
1177 {
1178 /* This wasn't checked above for ! info->shared, but
1179 must hold there if we get here; the symbol must
1180 be defined in the regular program or be undefweak
1181 or be a function or otherwise need a PLT. */
1182 BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1183 || info->shared
1184 || h->def_regular
1185 || h->type == STT_FUNC
1186 || h->needs_plt
1187 || h->root.type == bfd_link_hash_undefweak);
1188
1189 /* This is actually a static link, or it is a
1190 -Bsymbolic link and the symbol is defined locally,
1191 or is undefweak, or the symbol was forced to be
1192 local because of a version file, or we're not
1193 creating a dynamic object. We must initialize this
1194 entry in the global offset table. Since the offset
1195 must always be a multiple of 4, we use the least
1196 significant bit to record whether we have
1197 initialized it already.
1198
1199 If this GOT entry should be runtime-initialized, we
1200 will create a .rela.got relocation entry to
1201 initialize the value. This is done in the
1202 finish_dynamic_symbol routine. */
1203 if ((off & 1) != 0)
1204 off &= ~1;
1205 else
1206 {
1207 bfd_put_32 (output_bfd, relocation,
1208 sgot->contents + off);
1209 h->got.offset |= 1;
1210 }
1211 }
1212 }
1213 else
1214 {
1215 BFD_ASSERT (local_got_offsets != NULL
1216 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1217
1218 off = local_got_offsets[r_symndx];
1219
1220 /* The offset must always be a multiple of 4. We use
1221 the least significant bit to record whether we have
1222 already generated the necessary reloc. */
1223 if ((off & 1) != 0)
1224 off &= ~1;
1225 else
1226 {
1227 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1228
1229 if (info->shared)
1230 {
1231 asection *s;
1232 Elf_Internal_Rela outrel;
1233 bfd_byte *loc;
1234
1235 s = bfd_get_section_by_name (dynobj, ".rela.got");
1236 BFD_ASSERT (s != NULL);
1237
1238 outrel.r_offset = (sgot->output_section->vma
1239 + sgot->output_offset
1240 + off);
1241 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1242 outrel.r_addend = relocation;
1243 loc = s->contents;
1244 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1245 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1246 }
1247
1248 local_got_offsets[r_symndx] |= 1;
1249 }
1250 }
1251
1252 relocation = sgot->output_offset + off;
1253 if (rel->r_addend != 0)
1254 {
1255 /* We can't do anything for a relocation which is against
1256 a symbol *plus offset*. GOT holds relocations for
1257 symbols. Make this an error; the compiler isn't
1258 allowed to pass us these kinds of things. */
1259 if (h == NULL)
1260 (*_bfd_error_handler)
1261 (_("%B, section %A: relocation %s with non-zero addend %d"
1262 " against local symbol"),
1263 input_bfd,
1264 input_section,
1265 cris_elf_howto_table[r_type].name,
1266 rel->r_addend);
1267 else
1268 (*_bfd_error_handler)
1269 (_("%B, section %A: relocation %s with non-zero addend %d"
1270 " against symbol `%s'"),
1271 input_bfd,
1272 input_section,
1273 cris_elf_howto_table[r_type].name,
1274 rel->r_addend,
1275 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1276
1277 bfd_set_error (bfd_error_bad_value);
1278 return FALSE;
1279 }
1280 }
1281 break;
1282
1283 case R_CRIS_32_GOTREL:
1284 /* This relocation must only be performed against local symbols.
1285 It's also ok when we link a program and the symbol is either
1286 defined in an ordinary (non-DSO) object or is undefined weak. */
1287 if (h != NULL
1288 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1289 && !(!info->shared
1290 && (h->def_regular
1291 || (!h->def_dynamic
1292 && h->root.type == bfd_link_hash_undefweak))))
1293 {
1294 (*_bfd_error_handler)
1295 (_("%B, section %A: relocation %s is"
1296 " not allowed for global symbol: `%s'"),
1297 input_bfd,
1298 input_section,
1299 cris_elf_howto_table[r_type].name,
1300 symname);
1301 bfd_set_error (bfd_error_bad_value);
1302 return FALSE;
1303 }
1304
1305 /* This can happen if we get a link error with the input ELF
1306 variant mismatching the output variant. Emit an error so
1307 it's noticed if it happens elsewhere. */
1308 if (sgot == NULL)
1309 {
1310 (*_bfd_error_handler)
1311 (_("%B, section %A: relocation %s with no GOT created"),
1312 input_bfd,
1313 input_section,
1314 cris_elf_howto_table[r_type].name);
1315 bfd_set_error (bfd_error_bad_value);
1316 return FALSE;
1317 }
1318
1319 /* This relocation is like a PC-relative one, except the
1320 reference point is the location of GOT. Note that
1321 sgot->output_offset is not involved in this calculation. We
1322 always want the start of entire .got section, not the
1323 position after the reserved header. */
1324 relocation -= sgot->output_section->vma;
1325 break;
1326
1327 case R_CRIS_32_PLT_PCREL:
1328 /* Relocation is to the entry for this symbol in the
1329 procedure linkage table. */
1330
1331 /* Resolve a PLT_PCREL reloc against a local symbol directly,
1332 without using the procedure linkage table. */
1333 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1334 break;
1335
1336 if (h->plt.offset == (bfd_vma) -1
1337 || splt == NULL)
1338 {
1339 /* We didn't make a PLT entry for this symbol. This
1340 happens when statically linking PIC code, or when
1341 using -Bsymbolic. */
1342 break;
1343 }
1344
1345 relocation = (splt->output_section->vma
1346 + splt->output_offset
1347 + h->plt.offset);
1348 break;
1349
1350 case R_CRIS_32_PLT_GOTREL:
1351 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1352 start of the .got section. See also comment at
1353 R_CRIS_32_GOT. */
1354 relocation -= sgot->output_section->vma;
1355
1356 /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1357 without using the procedure linkage table. */
1358 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1359 break;
1360
1361 if (h->plt.offset == (bfd_vma) -1
1362 || splt == NULL)
1363 {
1364 /* We didn't make a PLT entry for this symbol. This
1365 happens when statically linking PIC code, or when
1366 using -Bsymbolic. */
1367 break;
1368 }
1369
1370 relocation = (splt->output_section->vma
1371 + splt->output_offset
1372 + h->plt.offset
1373 - sgot->output_section->vma);
1374 break;
1375
1376 case R_CRIS_8_PCREL:
1377 case R_CRIS_16_PCREL:
1378 case R_CRIS_32_PCREL:
1379 /* If the symbol was local, we need no shlib-specific handling. */
1380 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1381 break;
1382
1383 /* Fall through. */
1384 case R_CRIS_8:
1385 case R_CRIS_16:
1386 case R_CRIS_32:
1387 if (info->shared
1388 && r_symndx != 0
1389 && (input_section->flags & SEC_ALLOC) != 0
1390 && ((r_type != R_CRIS_8_PCREL
1391 && r_type != R_CRIS_16_PCREL
1392 && r_type != R_CRIS_32_PCREL)
1393 || (!info->symbolic
1394 || !h->def_regular)))
1395 {
1396 Elf_Internal_Rela outrel;
1397 bfd_byte *loc;
1398 bfd_boolean skip, relocate;
1399
1400 /* When generating a shared object, these relocations
1401 are copied into the output file to be resolved at run
1402 time. */
1403
1404 if (sreloc == NULL)
1405 {
1406 const char *name;
1407
1408 name = (bfd_elf_string_from_elf_section
1409 (input_bfd,
1410 elf_elfheader (input_bfd)->e_shstrndx,
1411 elf_section_data (input_section)->rel_hdr.sh_name));
1412 if (name == NULL)
1413 return FALSE;
1414
1415 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
1416 && strcmp (bfd_get_section_name (input_bfd,
1417 input_section),
1418 name + 5) == 0);
1419
1420 sreloc = bfd_get_section_by_name (dynobj, name);
1421
1422 /* That section should have been created in
1423 cris_elf_check_relocs, but that function will not be
1424 called for objects which fail in
1425 cris_elf_merge_private_bfd_data. */
1426 if (sreloc == NULL)
1427 {
1428 (*_bfd_error_handler)
1429 (_("%B: Internal inconsistency; no relocation section %s"),
1430 input_bfd,
1431 name);
1432
1433 bfd_set_error (bfd_error_bad_value);
1434 return FALSE;
1435 }
1436 }
1437
1438 skip = FALSE;
1439 relocate = FALSE;
1440
1441 outrel.r_offset =
1442 _bfd_elf_section_offset (output_bfd, info, input_section,
1443 rel->r_offset);
1444 if (outrel.r_offset == (bfd_vma) -1)
1445 skip = TRUE;
1446 else if (outrel.r_offset == (bfd_vma) -2)
1447 skip = TRUE, relocate = TRUE;
1448 outrel.r_offset += (input_section->output_section->vma
1449 + input_section->output_offset);
1450
1451 if (skip)
1452 memset (&outrel, 0, sizeof outrel);
1453 /* h->dynindx may be -1 if the symbol was marked to
1454 become local. */
1455 else if (h != NULL
1456 && ((! info->symbolic && h->dynindx != -1)
1457 || !h->def_regular))
1458 {
1459 BFD_ASSERT (h->dynindx != -1);
1460 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1461 outrel.r_addend = relocation + rel->r_addend;
1462 }
1463 else
1464 {
1465 outrel.r_addend = relocation + rel->r_addend;
1466
1467 if (r_type == R_CRIS_32)
1468 {
1469 relocate = TRUE;
1470 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1471 }
1472 else
1473 {
1474 long indx;
1475
1476 if (bfd_is_abs_section (sec))
1477 indx = 0;
1478 else if (sec == NULL || sec->owner == NULL)
1479 {
1480 bfd_set_error (bfd_error_bad_value);
1481 return FALSE;
1482 }
1483 else
1484 {
1485 asection *osec;
1486
1487 /* We are turning this relocation into one
1488 against a section symbol. It would be
1489 proper to subtract the symbol's value,
1490 osec->vma, from the emitted reloc addend,
1491 but ld.so expects buggy relocs. */
1492 osec = sec->output_section;
1493 indx = elf_section_data (osec)->dynindx;
1494 if (indx == 0)
1495 {
1496 struct elf_cris_link_hash_table *htab;
1497 htab = elf_cris_hash_table (info);
1498 osec = htab->root.text_index_section;
1499 indx = elf_section_data (osec)->dynindx;
1500 }
1501 BFD_ASSERT (indx != 0);
1502 }
1503
1504 outrel.r_info = ELF32_R_INFO (indx, r_type);
1505 }
1506 }
1507
1508 loc = sreloc->contents;
1509 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1510 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1511
1512 /* This reloc will be computed at runtime, so there's no
1513 need to do anything now, except for R_CRIS_32 relocations
1514 that have been turned into R_CRIS_RELATIVE. */
1515 if (!relocate)
1516 continue;
1517 }
1518
1519 break;
1520 }
1521
1522 r = cris_final_link_relocate (howto, input_bfd, input_section,
1523 contents, rel, relocation);
1524
1525 if (r != bfd_reloc_ok)
1526 {
1527 const char * msg = (const char *) NULL;
1528
1529 switch (r)
1530 {
1531 case bfd_reloc_overflow:
1532 r = info->callbacks->reloc_overflow
1533 (info, (h ? &h->root : NULL), symname, howto->name,
1534 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1535 break;
1536
1537 case bfd_reloc_undefined:
1538 r = info->callbacks->undefined_symbol
1539 (info, symname, input_bfd, input_section, rel->r_offset,
1540 TRUE);
1541 break;
1542
1543 case bfd_reloc_outofrange:
1544 msg = _("internal error: out of range error");
1545 break;
1546
1547 case bfd_reloc_notsupported:
1548 msg = _("internal error: unsupported relocation error");
1549 break;
1550
1551 case bfd_reloc_dangerous:
1552 msg = _("internal error: dangerous relocation");
1553 break;
1554
1555 default:
1556 msg = _("internal error: unknown error");
1557 break;
1558 }
1559
1560 if (msg)
1561 r = info->callbacks->warning
1562 (info, msg, symname, input_bfd, input_section, rel->r_offset);
1563
1564 if (! r)
1565 return FALSE;
1566 }
1567 }
1568
1569 return TRUE;
1570 }
1571
1572 /* Finish up dynamic symbol handling. We set the contents of various
1574 dynamic sections here. */
1575
1576 static bfd_boolean
1577 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1578 bfd *output_bfd;
1579 struct bfd_link_info *info;
1580 struct elf_link_hash_entry *h;
1581 Elf_Internal_Sym *sym;
1582 {
1583 bfd *dynobj;
1584
1585 /* Where in the plt entry to put values. */
1586 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1587
1588 /* What offset to add to the distance to the first PLT entry for the
1589 value at plt_off3. */
1590 int plt_off3_value_bias = 4;
1591
1592 /* Where in the PLT entry the call-dynlink-stub is (happens to be same
1593 for PIC and non-PIC for v32 and pre-v32). */
1594 int plt_stub_offset = 8;
1595 int plt_entry_size = PLT_ENTRY_SIZE;
1596 const bfd_byte *plt_entry = elf_cris_plt_entry;
1597 const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
1598
1599 /* Adjust the various PLT entry offsets. */
1600 if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1601 {
1602 plt_off2 = 14;
1603 plt_off3 = 20;
1604 plt_off3_value_bias = -2;
1605 plt_stub_offset = 12;
1606 plt_entry_size = PLT_ENTRY_SIZE_V32;
1607 plt_entry = elf_cris_plt_entry_v32;
1608 plt_pic_entry = elf_cris_pic_plt_entry_v32;
1609 }
1610
1611 dynobj = elf_hash_table (info)->dynobj;
1612
1613 if (h->plt.offset != (bfd_vma) -1)
1614 {
1615 asection *splt;
1616 asection *sgotplt;
1617 asection *sgot;
1618 asection *srela;
1619 bfd_vma got_base;
1620
1621 bfd_vma gotplt_offset
1622 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1623 Elf_Internal_Rela rela;
1624 bfd_byte *loc;
1625 bfd_boolean has_gotplt = gotplt_offset != 0;
1626
1627 /* Get the index in the procedure linkage table which
1628 corresponds to this symbol. This is the index of this symbol
1629 in all the symbols for which we are making plt entries. The
1630 first entry in the procedure linkage table is reserved. */
1631 /* We have to count backwards here, and the result is only valid as
1632 an index into .got.plt and its relocations. FIXME: Constants... */
1633 bfd_vma gotplt_index = gotplt_offset/4 - 3;
1634
1635 /* Get the offset into the .got table of the entry that corresponds
1636 to this function. Note that we embed knowledge that "incoming"
1637 .got goes after .got.plt in the output without padding (pointer
1638 aligned). However, that knowledge is present in several other
1639 places too. */
1640 bfd_vma got_offset
1641 = (has_gotplt
1642 ? gotplt_offset
1643 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1644
1645 /* This symbol has an entry in the procedure linkage table. Set it
1646 up. */
1647
1648 BFD_ASSERT (h->dynindx != -1);
1649
1650 splt = bfd_get_section_by_name (dynobj, ".plt");
1651 sgot = bfd_get_section_by_name (dynobj, ".got");
1652 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1653 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1654 BFD_ASSERT (splt != NULL && sgotplt != NULL
1655 && (! has_gotplt || srela != NULL));
1656
1657 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1658
1659 /* Fill in the entry in the procedure linkage table. */
1660 if (! info->shared)
1661 {
1662 memcpy (splt->contents + h->plt.offset, plt_entry,
1663 plt_entry_size);
1664
1665 /* We need to enter the absolute address of the GOT entry here. */
1666 bfd_put_32 (output_bfd, got_base + got_offset,
1667 splt->contents + h->plt.offset + plt_off1);
1668 }
1669 else
1670 {
1671 memcpy (splt->contents + h->plt.offset, plt_pic_entry,
1672 plt_entry_size);
1673 bfd_put_32 (output_bfd, got_offset,
1674 splt->contents + h->plt.offset + plt_off1);
1675 }
1676
1677 /* Fill in the plt entry and make a relocation, if this is a "real"
1678 PLT entry. */
1679 if (has_gotplt)
1680 {
1681 /* Fill in the offset to the reloc table. */
1682 bfd_put_32 (output_bfd,
1683 gotplt_index * sizeof (Elf32_External_Rela),
1684 splt->contents + h->plt.offset + plt_off2);
1685
1686 /* Fill in the offset to the first PLT entry, where to "jump". */
1687 bfd_put_32 (output_bfd,
1688 - (h->plt.offset + plt_off3 + plt_off3_value_bias),
1689 splt->contents + h->plt.offset + plt_off3);
1690
1691 /* Fill in the entry in the global offset table with the address of
1692 the relocating stub. */
1693 bfd_put_32 (output_bfd,
1694 (splt->output_section->vma
1695 + splt->output_offset
1696 + h->plt.offset
1697 + plt_stub_offset),
1698 sgotplt->contents + got_offset);
1699
1700 /* Fill in the entry in the .rela.plt section. */
1701 rela.r_offset = (sgotplt->output_section->vma
1702 + sgotplt->output_offset
1703 + got_offset);
1704 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1705 rela.r_addend = 0;
1706 loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
1707 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1708 }
1709
1710 if (!h->def_regular)
1711 {
1712 /* Mark the symbol as undefined, rather than as defined in
1713 the .plt section. Leave the value alone. */
1714 sym->st_shndx = SHN_UNDEF;
1715
1716 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
1717 know whether resetting the value is significant; if it really
1718 is, rather than a quirk or bug in the sparc port, then I
1719 believe we'd see this elsewhere. */
1720 /* If the symbol is weak, we do need to clear the value.
1721 Otherwise, the PLT entry would provide a definition for
1722 the symbol even if the symbol wasn't defined anywhere,
1723 and so the symbol would never be NULL. */
1724 if (!h->ref_regular_nonweak)
1725 sym->st_value = 0;
1726 }
1727 }
1728
1729 /* For an ordinary program, we emit .got relocs only for symbols that
1730 are in the dynamic-symbols table and are either defined by the
1731 program or are undefined weak symbols, or are function symbols
1732 where we do not output a PLT: the PLT reloc was output above and all
1733 references to the function symbol are redirected to the PLT. */
1734 if (h->got.offset != (bfd_vma) -1
1735 && (info->shared
1736 || (h->dynindx != -1
1737 && h->plt.offset == (bfd_vma) -1
1738 && !h->def_regular
1739 && h->root.type != bfd_link_hash_undefweak)))
1740 {
1741 asection *sgot;
1742 asection *srela;
1743 Elf_Internal_Rela rela;
1744 bfd_byte *loc;
1745 bfd_byte *where;
1746
1747 /* This symbol has an entry in the global offset table. Set it up. */
1748
1749 sgot = bfd_get_section_by_name (dynobj, ".got");
1750 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1751 BFD_ASSERT (sgot != NULL && srela != NULL);
1752
1753 rela.r_offset = (sgot->output_section->vma
1754 + sgot->output_offset
1755 + (h->got.offset &~ (bfd_vma) 1));
1756
1757 /* If this is a static link, or it is a -Bsymbolic link and the
1758 symbol is defined locally or was forced to be local because
1759 of a version file, we just want to emit a RELATIVE reloc.
1760 The entry in the global offset table will already have been
1761 initialized in the relocate_section function. */
1762 where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1763 if (! elf_hash_table (info)->dynamic_sections_created
1764 || (info->shared
1765 && (info->symbolic || h->dynindx == -1)
1766 && h->def_regular))
1767 {
1768 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1769 rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1770 }
1771 else
1772 {
1773 bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1774 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1775 rela.r_addend = 0;
1776 }
1777
1778 loc = srela->contents;
1779 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1780 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1781 }
1782
1783 if (h->needs_copy)
1784 {
1785 asection *s;
1786 Elf_Internal_Rela rela;
1787 bfd_byte *loc;
1788
1789 /* This symbol needs a copy reloc. Set it up. */
1790
1791 BFD_ASSERT (h->dynindx != -1
1792 && (h->root.type == bfd_link_hash_defined
1793 || h->root.type == bfd_link_hash_defweak));
1794
1795 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1796 ".rela.bss");
1797 BFD_ASSERT (s != NULL);
1798
1799 rela.r_offset = (h->root.u.def.value
1800 + h->root.u.def.section->output_section->vma
1801 + h->root.u.def.section->output_offset);
1802 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1803 rela.r_addend = 0;
1804 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1805 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1806 }
1807
1808 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1809 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1810 || h == elf_hash_table (info)->hgot)
1811 sym->st_shndx = SHN_ABS;
1812
1813 return TRUE;
1814 }
1815
1816 /* Finish up the dynamic sections. */
1818
1819 static bfd_boolean
1820 elf_cris_finish_dynamic_sections (output_bfd, info)
1821 bfd *output_bfd;
1822 struct bfd_link_info *info;
1823 {
1824 bfd *dynobj;
1825 asection *sgot;
1826 asection *sdyn;
1827
1828 dynobj = elf_hash_table (info)->dynobj;
1829
1830 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1831 BFD_ASSERT (sgot != NULL);
1832 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1833
1834 if (elf_hash_table (info)->dynamic_sections_created)
1835 {
1836 asection *splt;
1837 Elf32_External_Dyn *dyncon, *dynconend;
1838
1839 splt = bfd_get_section_by_name (dynobj, ".plt");
1840 BFD_ASSERT (splt != NULL && sdyn != NULL);
1841
1842 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1843 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1844 for (; dyncon < dynconend; dyncon++)
1845 {
1846 Elf_Internal_Dyn dyn;
1847 asection *s;
1848
1849 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1850
1851 switch (dyn.d_tag)
1852 {
1853 default:
1854 break;
1855
1856 case DT_PLTGOT:
1857 s = bfd_get_section_by_name (output_bfd, ".got");
1858 BFD_ASSERT (s != NULL);
1859 dyn.d_un.d_ptr = s->vma;
1860 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1861 break;
1862
1863 case DT_JMPREL:
1864 /* Yes, we *can* have a .plt and no .plt.rela, for instance
1865 if all symbols are found in the .got (not .got.plt). */
1866 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1867 dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1868 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1869 break;
1870
1871 case DT_PLTRELSZ:
1872 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1873 if (s == NULL)
1874 dyn.d_un.d_val = 0;
1875 else
1876 dyn.d_un.d_val = s->size;
1877 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1878 break;
1879
1880 case DT_RELASZ:
1881 /* The procedure linkage table relocs (DT_JMPREL) should
1882 not be included in the overall relocs (DT_RELA).
1883 Therefore, we override the DT_RELASZ entry here to
1884 make it not include the JMPREL relocs. Since the
1885 linker script arranges for .rela.plt to follow all
1886 other relocation sections, we don't have to worry
1887 about changing the DT_RELA entry. */
1888 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1889 if (s != NULL)
1890 dyn.d_un.d_val -= s->size;
1891 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1892 break;
1893 }
1894 }
1895
1896 /* Fill in the first entry in the procedure linkage table. */
1897 if (splt->size > 0)
1898 {
1899 if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1900 {
1901 if (info->shared)
1902 memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
1903 PLT_ENTRY_SIZE_V32);
1904 else
1905 {
1906 memcpy (splt->contents, elf_cris_plt0_entry_v32,
1907 PLT_ENTRY_SIZE_V32);
1908 bfd_put_32 (output_bfd,
1909 sgot->output_section->vma
1910 + sgot->output_offset + 4,
1911 splt->contents + 4);
1912
1913 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1914 = PLT_ENTRY_SIZE_V32;
1915 }
1916 }
1917 else
1918 {
1919 if (info->shared)
1920 memcpy (splt->contents, elf_cris_pic_plt0_entry,
1921 PLT_ENTRY_SIZE);
1922 else
1923 {
1924 memcpy (splt->contents, elf_cris_plt0_entry,
1925 PLT_ENTRY_SIZE);
1926 bfd_put_32 (output_bfd,
1927 sgot->output_section->vma
1928 + sgot->output_offset + 4,
1929 splt->contents + 6);
1930 bfd_put_32 (output_bfd,
1931 sgot->output_section->vma
1932 + sgot->output_offset + 8,
1933 splt->contents + 14);
1934
1935 elf_section_data (splt->output_section)->this_hdr.sh_entsize
1936 = PLT_ENTRY_SIZE;
1937 }
1938 }
1939 }
1940 }
1941
1942 /* Fill in the first three entries in the global offset table. */
1943 if (sgot->size > 0)
1944 {
1945 if (sdyn == NULL)
1946 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1947 else
1948 bfd_put_32 (output_bfd,
1949 sdyn->output_section->vma + sdyn->output_offset,
1950 sgot->contents);
1951 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1952 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1953 }
1954
1955 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1956
1957 return TRUE;
1958 }
1959
1960 /* Return the section that should be marked against GC for a given
1962 relocation. */
1963
1964 static asection *
1965 cris_elf_gc_mark_hook (asection *sec,
1966 struct bfd_link_info *info,
1967 Elf_Internal_Rela *rel,
1968 struct elf_link_hash_entry *h,
1969 Elf_Internal_Sym *sym)
1970 {
1971 if (h != NULL)
1972 switch (ELF32_R_TYPE (rel->r_info))
1973 {
1974 case R_CRIS_GNU_VTINHERIT:
1975 case R_CRIS_GNU_VTENTRY:
1976 return NULL;
1977 }
1978
1979 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1980 }
1981
1982 /* Update the got entry reference counts for the section being removed. */
1983
1984 static bfd_boolean
1985 cris_elf_gc_sweep_hook (bfd *abfd,
1986 struct bfd_link_info *info,
1987 asection *sec,
1988 const Elf_Internal_Rela *relocs)
1989 {
1990 Elf_Internal_Shdr *symtab_hdr;
1991 struct elf_link_hash_entry **sym_hashes;
1992 bfd_signed_vma *local_got_refcounts;
1993 const Elf_Internal_Rela *rel, *relend;
1994 bfd *dynobj;
1995 asection *sgot;
1996 asection *srelgot;
1997
1998 if (info->relocatable)
1999 return TRUE;
2000
2001 dynobj = elf_hash_table (info)->dynobj;
2002 if (dynobj == NULL)
2003 return TRUE;
2004
2005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2006 sym_hashes = elf_sym_hashes (abfd);
2007 local_got_refcounts = elf_local_got_refcounts (abfd);
2008
2009 sgot = bfd_get_section_by_name (dynobj, ".got");
2010 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2011
2012 relend = relocs + sec->reloc_count;
2013 for (rel = relocs; rel < relend; rel++)
2014 {
2015 unsigned long r_symndx;
2016 struct elf_link_hash_entry *h = NULL;
2017
2018 r_symndx = ELF32_R_SYM (rel->r_info);
2019 if (r_symndx >= symtab_hdr->sh_info)
2020 {
2021 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2022 while (h->root.type == bfd_link_hash_indirect
2023 || h->root.type == bfd_link_hash_warning)
2024 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2025 }
2026
2027 switch (ELF32_R_TYPE (rel->r_info))
2028 {
2029 case R_CRIS_16_GOT:
2030 case R_CRIS_32_GOT:
2031 if (h != NULL)
2032 {
2033 if (h->got.refcount > 0)
2034 {
2035 --h->got.refcount;
2036 if (h->got.refcount == 0)
2037 {
2038 /* We don't need the .got entry any more. */
2039 sgot->size -= 4;
2040 srelgot->size -= sizeof (Elf32_External_Rela);
2041 }
2042 }
2043 break;
2044 }
2045
2046 local_got_reloc:
2047 if (local_got_refcounts != NULL)
2048 {
2049 if (local_got_refcounts[r_symndx] > 0)
2050 {
2051 --local_got_refcounts[r_symndx];
2052 if (local_got_refcounts[r_symndx] == 0)
2053 {
2054 /* We don't need the .got entry any more. */
2055 sgot->size -= 4;
2056 if (info->shared)
2057 srelgot->size -= sizeof (Elf32_External_Rela);
2058 }
2059 }
2060 }
2061 break;
2062
2063 case R_CRIS_16_GOTPLT:
2064 case R_CRIS_32_GOTPLT:
2065 /* For local symbols, treat these like GOT relocs. */
2066 if (h == NULL)
2067 goto local_got_reloc;
2068 /* Fall through. */
2069
2070 case R_CRIS_32_PLT_GOTREL:
2071 /* FIXME: We don't garbage-collect away the .got section. */
2072 if (local_got_refcounts != NULL)
2073 local_got_refcounts[-1]--;
2074 /* Fall through. */
2075
2076 case R_CRIS_8_PCREL:
2077 case R_CRIS_16_PCREL:
2078 case R_CRIS_32_PCREL:
2079 case R_CRIS_32_PLT_PCREL:
2080 if (h != NULL)
2081 {
2082 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2083 && h->plt.refcount > 0)
2084 --h->plt.refcount;
2085 }
2086 break;
2087
2088 default:
2089 break;
2090 }
2091 }
2092
2093 return TRUE;
2094 }
2095
2096 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2097 entry but we found we will not create any. Called when we find we will
2098 not have any PLT for this symbol, by for example
2099 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2100 or elf_cris_size_dynamic_sections if no dynamic sections will be
2101 created (we're only linking static objects). */
2102
2103 static bfd_boolean
2104 elf_cris_adjust_gotplt_to_got (h, p)
2105 struct elf_cris_link_hash_entry *h;
2106 PTR p;
2107 {
2108 struct bfd_link_info *info = (struct bfd_link_info *) p;
2109
2110 if (h->root.root.type == bfd_link_hash_warning)
2111 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2112
2113 /* If nobody wanted a GOTPLT with this symbol, we're done. */
2114 if (h->gotplt_refcount <= 0)
2115 return TRUE;
2116
2117 if (h->root.got.refcount > 0)
2118 {
2119 /* There's a GOT entry for this symbol. Just adjust the refcount.
2120 Probably not necessary at this stage, but keeping it accurate
2121 helps avoiding surprises later. */
2122 h->root.got.refcount += h->gotplt_refcount;
2123 h->gotplt_refcount = 0;
2124 }
2125 else
2126 {
2127 /* No GOT entry for this symbol. We need to create one. */
2128 bfd *dynobj = elf_hash_table (info)->dynobj;
2129 asection *sgot;
2130 asection *srelgot;
2131
2132 BFD_ASSERT (dynobj != NULL);
2133 sgot = bfd_get_section_by_name (dynobj, ".got");
2134 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2135
2136 /* Put an accurate refcount there. */
2137 h->root.got.refcount = h->gotplt_refcount;
2138
2139 h->gotplt_refcount = 0;
2140
2141 /* We always have a .got and a .rela.got section if there were
2142 GOTPLT relocs in input. */
2143 BFD_ASSERT (sgot != NULL && srelgot != NULL);
2144
2145 /* Allocate space in the .got section. */
2146 sgot->size += 4;
2147
2148 /* Allocate relocation space. */
2149 srelgot->size += sizeof (Elf32_External_Rela);
2150 }
2151
2152 return TRUE;
2153 }
2154
2155 /* Try to fold PLT entries with GOT entries. There are two cases when we
2156 want to do this:
2157
2158 - When all PLT references are GOTPLT references, and there are GOT
2159 references, and this is not the executable. We don't have to
2160 generate a PLT at all.
2161
2162 - When there are both (ordinary) PLT references and GOT references,
2163 and this isn't the executable.
2164 We want to make the PLT reference use the ordinary GOT entry rather
2165 than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2166 since the GOT entry will have to be resolved at startup anyway.
2167
2168 Though the latter case is handled when room for the PLT is allocated,
2169 not here.
2170
2171 By folding into the GOT, we may need a round-trip to a PLT in the
2172 executable for calls, a loss in performance. Still, losing a
2173 reloc is a win in size and at least in start-up time.
2174
2175 Note that this function is called before symbols are forced local by
2176 version scripts. The differing cases are handled by
2177 elf_cris_hide_symbol. */
2178
2179 static bfd_boolean
2180 elf_cris_try_fold_plt_to_got (h, p)
2181 struct elf_cris_link_hash_entry *h;
2182 PTR p;
2183 {
2184 struct bfd_link_info *info = (struct bfd_link_info *) p;
2185
2186 /* If there are no GOT references for this symbol, we can't fold any
2187 other reference so there's nothing to do. Likewise if there are no
2188 PLT references; GOTPLT references included. */
2189 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2190 return TRUE;
2191
2192 /* GOTPLT relocs are supposed to be included into the PLT refcount. */
2193 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2194
2195 if (h->gotplt_refcount == h->root.plt.refcount)
2196 {
2197 /* The only PLT references are GOTPLT references, and there are GOT
2198 references. Convert PLT to GOT references. */
2199 if (! elf_cris_adjust_gotplt_to_got (h, info))
2200 return FALSE;
2201
2202 /* Clear the PLT references, so no PLT will be created. */
2203 h->root.plt.offset = (bfd_vma) -1;
2204 }
2205
2206 return TRUE;
2207 }
2208
2209 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2210 to use a GOT entry (and create one) rather than requiring a GOTPLT
2211 entry. */
2212
2213 static void
2214 elf_cris_hide_symbol (info, h, force_local)
2215 struct bfd_link_info *info;
2216 struct elf_link_hash_entry *h;
2217 bfd_boolean force_local;
2218 {
2219 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2220
2221 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2222 }
2223
2224 /* Adjust a symbol defined by a dynamic object and referenced by a
2225 regular object. The current definition is in some section of the
2226 dynamic object, but we're not including those sections. We have to
2227 change the definition to something the rest of the link can
2228 understand. */
2229
2230 static bfd_boolean
2231 elf_cris_adjust_dynamic_symbol (info, h)
2232 struct bfd_link_info *info;
2233 struct elf_link_hash_entry *h;
2234 {
2235 bfd *dynobj;
2236 asection *s;
2237 bfd_size_type plt_entry_size;
2238
2239 dynobj = elf_hash_table (info)->dynobj;
2240
2241 /* Make sure we know what is going on here. */
2242 BFD_ASSERT (dynobj != NULL
2243 && (h->needs_plt
2244 || h->u.weakdef != NULL
2245 || (h->def_dynamic
2246 && h->ref_regular
2247 && !h->def_regular)));
2248
2249 plt_entry_size
2250 = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2251 ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2252
2253 /* If this is a function, put it in the procedure linkage table. We
2254 will fill in the contents of the procedure linkage table later,
2255 when we know the address of the .got section. */
2256 if (h->type == STT_FUNC
2257 || h->needs_plt)
2258 {
2259 /* If we link a program (not a DSO), we'll get rid of unnecessary
2260 PLT entries; we point to the actual symbols -- even for pic
2261 relocs, because a program built with -fpic should have the same
2262 result as one built without -fpic, specifically considering weak
2263 symbols.
2264 FIXME: m68k and i386 differ here, for unclear reasons. */
2265 if (! info->shared
2266 && !h->def_dynamic)
2267 {
2268 /* This case can occur if we saw a PLT reloc in an input file,
2269 but the symbol was not defined by a dynamic object. In such
2270 a case, we don't actually need to build a procedure linkage
2271 table, and we can just do an absolute or PC reloc instead, or
2272 change a .got.plt index to a .got index for GOTPLT relocs. */
2273 BFD_ASSERT (h->needs_plt);
2274 h->needs_plt = 0;
2275 h->plt.offset = (bfd_vma) -1;
2276 return
2277 elf_cris_adjust_gotplt_to_got ((struct
2278 elf_cris_link_hash_entry *) h,
2279 info);
2280 }
2281
2282 /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2283 where a pointer-equivalent symbol was unimportant (i.e. more
2284 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2285 of the PLT. We can't for the executable, because the GOT
2286 entries will point to the PLT there (and be constant). */
2287 if (info->shared
2288 && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2289 h, info))
2290 return FALSE;
2291
2292 /* GC or folding may have rendered this entry unused. */
2293 if (h->plt.refcount <= 0)
2294 {
2295 h->needs_plt = 0;
2296 h->plt.offset = (bfd_vma) -1;
2297 return TRUE;
2298 }
2299
2300 /* Make sure this symbol is output as a dynamic symbol. */
2301 if (h->dynindx == -1)
2302 {
2303 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2304 return FALSE;
2305 }
2306
2307 s = bfd_get_section_by_name (dynobj, ".plt");
2308 BFD_ASSERT (s != NULL);
2309
2310 /* If this is the first .plt entry, make room for the special
2311 first entry. */
2312 if (s->size == 0)
2313 s->size += plt_entry_size;
2314
2315 /* If this symbol is not defined in a regular file, and we are
2316 not generating a shared library, then set the symbol to this
2317 location in the .plt. */
2318 if (!info->shared
2319 && !h->def_regular)
2320 {
2321 h->root.u.def.section = s;
2322 h->root.u.def.value = s->size;
2323 }
2324
2325 /* If there's already a GOT entry, use that, not a .got.plt. A
2326 GOT field still has a reference count when we get here; it's
2327 not yet changed to an offset. We can't do this for an
2328 executable, because then the reloc associated with the PLT
2329 would get a non-PLT reloc pointing to the PLT. FIXME: Move
2330 this to elf_cris_try_fold_plt_to_got. */
2331 if (info->shared && h->got.refcount > 0)
2332 {
2333 h->got.refcount += h->plt.refcount;
2334
2335 /* Mark the PLT offset to use the GOT entry by setting the low
2336 bit in the plt offset; it is always a multiple of
2337 plt_entry_size (which is at least a multiple of 2). */
2338 BFD_ASSERT ((s->size % plt_entry_size) == 0);
2339
2340 /* Change the PLT refcount to an offset. */
2341 h->plt.offset = s->size;
2342
2343 /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2344 that the got entry should be used instead. */
2345 BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2346 h)->gotplt_offset == 0);
2347
2348 /* Make room for this entry. */
2349 s->size += plt_entry_size;
2350
2351 return TRUE;
2352 }
2353
2354 /* No GOT reference for this symbol; prepare for an ordinary PLT. */
2355 h->plt.offset = s->size;
2356
2357 /* Make room for this entry. */
2358 s->size += plt_entry_size;
2359
2360 /* We also need to make an entry in the .got.plt section, which
2361 will be placed in the .got section by the linker script. */
2362 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2363 = elf_cris_hash_table (info)->next_gotplt_entry;
2364 elf_cris_hash_table (info)->next_gotplt_entry += 4;
2365
2366 s = bfd_get_section_by_name (dynobj, ".got.plt");
2367 BFD_ASSERT (s != NULL);
2368 s->size += 4;
2369
2370 /* We also need to make an entry in the .rela.plt section. */
2371
2372 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2373 BFD_ASSERT (s != NULL);
2374 s->size += sizeof (Elf32_External_Rela);
2375
2376 return TRUE;
2377 }
2378
2379 /* Reinitialize the plt offset now that it is not used as a reference
2380 count any more. */
2381 h->plt.offset = (bfd_vma) -1;
2382
2383 /* If this is a weak symbol, and there is a real definition, the
2384 processor independent code will have arranged for us to see the
2385 real definition first, and we can just use the same value. */
2386 if (h->u.weakdef != NULL)
2387 {
2388 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2389 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2390 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2391 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2392 return TRUE;
2393 }
2394
2395 /* This is a reference to a symbol defined by a dynamic object which
2396 is not a function. */
2397
2398 /* If we are creating a shared library, we must presume that the
2399 only references to the symbol are via the global offset table.
2400 For such cases we need not do anything here; the relocations will
2401 be handled correctly by relocate_section. */
2402 if (info->shared)
2403 return TRUE;
2404
2405 /* If there are no references to this symbol that do not use the
2406 GOT, we don't need to generate a copy reloc. */
2407 if (!h->non_got_ref)
2408 return TRUE;
2409
2410 if (h->size == 0)
2411 {
2412 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2413 h->root.root.string);
2414 return TRUE;
2415 }
2416
2417 /* We must allocate the symbol in our .dynbss section, which will
2418 become part of the .bss section of the executable. There will be
2419 an entry for this symbol in the .dynsym section. The dynamic
2420 object will contain position independent code, so all references
2421 from the dynamic object to this symbol will go through the global
2422 offset table. The dynamic linker will use the .dynsym entry to
2423 determine the address it must put in the global offset table, so
2424 both the dynamic object and the regular object will refer to the
2425 same memory location for the variable. */
2426
2427 s = bfd_get_section_by_name (dynobj, ".dynbss");
2428 BFD_ASSERT (s != NULL);
2429
2430 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2431 copy the initial value out of the dynamic object and into the
2432 runtime process image. We need to remember the offset into the
2433 .rela.bss section we are going to use. */
2434 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2435 {
2436 asection *srel;
2437
2438 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2439 BFD_ASSERT (srel != NULL);
2440 srel->size += sizeof (Elf32_External_Rela);
2441 h->needs_copy = 1;
2442 }
2443
2444 return _bfd_elf_adjust_dynamic_copy (h, s);
2445 }
2446
2447 /* Look through the relocs for a section during the first phase. */
2448
2449 static bfd_boolean
2450 cris_elf_check_relocs (abfd, info, sec, relocs)
2451 bfd *abfd;
2452 struct bfd_link_info *info;
2453 asection *sec;
2454 const Elf_Internal_Rela *relocs;
2455 {
2456 bfd *dynobj;
2457 Elf_Internal_Shdr *symtab_hdr;
2458 struct elf_link_hash_entry **sym_hashes;
2459 bfd_signed_vma *local_got_refcounts;
2460 const Elf_Internal_Rela *rel;
2461 const Elf_Internal_Rela *rel_end;
2462 asection *sgot;
2463 asection *srelgot;
2464 asection *sreloc;
2465
2466 if (info->relocatable)
2467 return TRUE;
2468
2469 dynobj = elf_hash_table (info)->dynobj;
2470 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2471 sym_hashes = elf_sym_hashes (abfd);
2472 local_got_refcounts = elf_local_got_refcounts (abfd);
2473
2474 sgot = NULL;
2475 srelgot = NULL;
2476 sreloc = NULL;
2477
2478 rel_end = relocs + sec->reloc_count;
2479 for (rel = relocs; rel < rel_end; rel++)
2480 {
2481 struct elf_link_hash_entry *h;
2482 unsigned long r_symndx;
2483 enum elf_cris_reloc_type r_type;
2484
2485 r_symndx = ELF32_R_SYM (rel->r_info);
2486 if (r_symndx < symtab_hdr->sh_info)
2487 h = NULL;
2488 else
2489 {
2490 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2491 while (h->root.type == bfd_link_hash_indirect
2492 || h->root.type == bfd_link_hash_warning)
2493 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2494 }
2495
2496 r_type = ELF32_R_TYPE (rel->r_info);
2497
2498 /* Some relocs require linker-created sections; we need to hang them
2499 on the first input bfd we found that contained dynamic relocs. */
2500 switch (r_type)
2501 {
2502 case R_CRIS_16_GOT:
2503 case R_CRIS_32_GOT:
2504 case R_CRIS_32_GOTREL:
2505 case R_CRIS_32_PLT_GOTREL:
2506 case R_CRIS_32_PLT_PCREL:
2507 case R_CRIS_16_GOTPLT:
2508 case R_CRIS_32_GOTPLT:
2509 if (dynobj == NULL)
2510 {
2511 elf_hash_table (info)->dynobj = dynobj = abfd;
2512
2513 /* We could handle this if we can get a handle on the
2514 output bfd in elf_cris_adjust_dynamic_symbol. Failing
2515 that, we must insist on dynobj being a specific mach. */
2516 if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
2517 {
2518 (*_bfd_error_handler)
2519 (_("%B, section %A:\n v10/v32 compatible object %s"
2520 " must not contain a PIC relocation"),
2521 abfd, sec);
2522 return FALSE;
2523 }
2524
2525 /* Create the .got section, so we can assume it's always
2526 present whenever there's a dynobj. */
2527 if (!_bfd_elf_create_got_section (dynobj, info))
2528 return FALSE;
2529 }
2530 break;
2531
2532 default:
2533 break;
2534 }
2535
2536 /* Some relocs require a global offset table (but perhaps not a
2537 specific GOT entry). */
2538 switch (r_type)
2539 {
2540 /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
2541 entry only for local symbols. Unfortunately, we don't know
2542 until later on if there's a version script that forces the
2543 symbol local. We must have the .rela.got section in place
2544 before we know if the symbol looks global now, so we need
2545 to treat the reloc just like for R_CRIS_16_GOT and
2546 R_CRIS_32_GOT. */
2547 case R_CRIS_16_GOTPLT:
2548 case R_CRIS_32_GOTPLT:
2549 case R_CRIS_16_GOT:
2550 case R_CRIS_32_GOT:
2551 if (srelgot == NULL
2552 && (h != NULL || info->shared))
2553 {
2554 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2555 if (srelgot == NULL)
2556 {
2557 srelgot = bfd_make_section_with_flags (dynobj,
2558 ".rela.got",
2559 (SEC_ALLOC
2560 | SEC_LOAD
2561 | SEC_HAS_CONTENTS
2562 | SEC_IN_MEMORY
2563 | SEC_LINKER_CREATED
2564 | SEC_READONLY));
2565 if (srelgot == NULL
2566 || !bfd_set_section_alignment (dynobj, srelgot, 2))
2567 return FALSE;
2568 }
2569 }
2570 /* Fall through. */
2571
2572 case R_CRIS_32_GOTREL:
2573 case R_CRIS_32_PLT_GOTREL:
2574 if (sgot == NULL)
2575 sgot = bfd_get_section_by_name (dynobj, ".got");
2576
2577 if (local_got_refcounts == NULL)
2578 {
2579 bfd_size_type amt;
2580
2581 /* We use index local_got_refcounts[-1] to count all
2582 GOT-relative relocations that do not have explicit
2583 GOT entries. */
2584 amt = symtab_hdr->sh_info + 1;
2585 amt *= sizeof (bfd_signed_vma);
2586 local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2587 if (local_got_refcounts == NULL)
2588 return FALSE;
2589
2590 local_got_refcounts++;
2591 elf_local_got_refcounts (abfd) = local_got_refcounts;
2592 }
2593 break;
2594
2595 default:
2596 break;
2597 }
2598
2599 switch (r_type)
2600 {
2601 case R_CRIS_16_GOTPLT:
2602 case R_CRIS_32_GOTPLT:
2603 /* Mark that we need a GOT entry if the PLT entry (and its GOT
2604 entry) is eliminated. We can only do this for a non-local
2605 symbol. */
2606 if (h != NULL)
2607 {
2608 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2609 goto handle_gotplt_reloc;
2610 }
2611 /* If h is NULL then this is a local symbol, and we must make a
2612 GOT entry for it, so handle it like a GOT reloc. */
2613 /* Fall through. */
2614
2615 case R_CRIS_16_GOT:
2616 case R_CRIS_32_GOT:
2617 /* This symbol requires a global offset table entry. */
2618 if (h != NULL)
2619 {
2620 if (h->got.refcount == 0)
2621 {
2622 /* Make sure this symbol is output as a dynamic symbol. */
2623 if (h->dynindx == -1)
2624 {
2625 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2626 return FALSE;
2627 }
2628
2629 /* Allocate space in the .got section. */
2630 sgot->size += 4;
2631 /* Allocate relocation space. */
2632 srelgot->size += sizeof (Elf32_External_Rela);
2633 }
2634 h->got.refcount++;
2635 }
2636 else
2637 {
2638 /* This is a global offset table entry for a local symbol. */
2639 if (local_got_refcounts[r_symndx] == 0)
2640 {
2641 sgot->size += 4;
2642 if (info->shared)
2643 {
2644 /* If we are generating a shared object, we need to
2645 output a R_CRIS_RELATIVE reloc so that the dynamic
2646 linker can adjust this GOT entry. */
2647 srelgot->size += sizeof (Elf32_External_Rela);
2648 }
2649 }
2650 local_got_refcounts[r_symndx]++;
2651 }
2652 break;
2653
2654 case R_CRIS_32_GOTREL:
2655 /* This reference requires a global offset table.
2656 FIXME: The actual refcount isn't used currently; the .got
2657 section can't be removed if there were any references in the
2658 input. */
2659 local_got_refcounts[-1]++;
2660 break;
2661
2662 handle_gotplt_reloc:
2663
2664 case R_CRIS_32_PLT_GOTREL:
2665 /* This reference requires a global offset table. */
2666 local_got_refcounts[-1]++;
2667 /* Fall through. */
2668
2669 case R_CRIS_32_PLT_PCREL:
2670 /* This symbol requires a procedure linkage table entry. We
2671 actually build the entry in adjust_dynamic_symbol,
2672 because this might be a case of linking PIC code which is
2673 never referenced by a dynamic object, in which case we
2674 don't need to generate a procedure linkage table entry
2675 after all. */
2676
2677 /* Beware: if we'd check for visibility of the symbol here
2678 (and not marking the need for a PLT when non-visible), we'd
2679 get into trouble with keeping handling consistent with
2680 regards to relocs found before definition and GOTPLT
2681 handling. Eliminable PLT entries will be dealt with later
2682 anyway. */
2683 if (h == NULL)
2684 continue;
2685
2686 h->needs_plt = 1;
2687 h->plt.refcount++;
2688 break;
2689
2690 case R_CRIS_8:
2691 case R_CRIS_16:
2692 case R_CRIS_32:
2693 /* Let's help debug shared library creation. Any of these
2694 relocs can be used in shared libs, but pages containing them
2695 cannot be shared. Don't warn for sections we don't care
2696 about, such as debug sections or non-constant sections. We
2697 can't help tables of (global) function pointers, for example,
2698 though they must be emitted in a data section to avoid having
2699 impure text sections. */
2700 if (info->shared
2701 && (sec->flags & SEC_ALLOC) != 0
2702 && (sec->flags & SEC_READONLY) != 0)
2703 {
2704 /* FIXME: How do we make this optionally a warning only? */
2705 (*_bfd_error_handler)
2706 (_("%B, section %A:\n relocation %s should not"
2707 " be used in a shared object; recompile with -fPIC"),
2708 abfd,
2709 sec,
2710 cris_elf_howto_table[r_type].name);
2711 }
2712 /* Fall through. */
2713
2714 case R_CRIS_8_PCREL:
2715 case R_CRIS_16_PCREL:
2716 case R_CRIS_32_PCREL:
2717 if (h != NULL)
2718 {
2719 h->non_got_ref = 1;
2720
2721 /* Make sure a plt entry is created for this symbol if it
2722 turns out to be a function defined by a dynamic object. */
2723 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2724 h->plt.refcount++;
2725 }
2726
2727 /* If we are creating a shared library and this is not a local
2728 symbol, we need to copy the reloc into the shared library.
2729 However when linking with -Bsymbolic and this is a global
2730 symbol which is defined in an object we are including in the
2731 link (i.e., DEF_REGULAR is set), then we can resolve the
2732 reloc directly. At this point we have not seen all the input
2733 files, so it is possible that DEF_REGULAR is not set now but
2734 will be set later (it is never cleared). In case of a weak
2735 definition, DEF_REGULAR may be cleared later by a strong
2736 definition in a shared library. We account for that
2737 possibility below by storing information in the relocs_copied
2738 field of the hash table entry. A similar situation occurs
2739 when creating shared libraries and symbol visibility changes
2740 render the symbol local. */
2741
2742 /* No need to do anything if we're not creating a shared object. */
2743 if (! info->shared)
2744 break;
2745
2746 /* We don't need to handle relocs into sections not going into
2747 the "real" output. */
2748 if ((sec->flags & SEC_ALLOC) == 0)
2749 break;
2750
2751 /* We can only eliminate PC-relative relocs. */
2752 if (r_type == R_CRIS_8_PCREL
2753 || r_type == R_CRIS_16_PCREL
2754 || r_type == R_CRIS_32_PCREL)
2755 {
2756 /* If the symbol is local, then we can eliminate the reloc. */
2757 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2758 break;
2759
2760 /* If this is with -Bsymbolic and the symbol isn't weak, and
2761 is defined by an ordinary object (the ones we include in
2762 this shared library) then we can also eliminate the
2763 reloc. See comment above for more eliminable cases which
2764 we can't identify at this time. */
2765 if (info->symbolic
2766 && h->root.type != bfd_link_hash_defweak
2767 && h->def_regular)
2768 break;
2769
2770 if ((sec->flags & SEC_READONLY) != 0)
2771 {
2772 /* FIXME: How do we make this optionally a warning only? */
2773 (*_bfd_error_handler)
2774 (_("%B, section %A:\n relocation %s should not be used"
2775 " in a shared object; recompile with -fPIC"),
2776 abfd,
2777 sec,
2778 cris_elf_howto_table[r_type].name);
2779 }
2780 }
2781
2782 /* We create a reloc section in dynobj and make room for this
2783 reloc. */
2784 if (sreloc == NULL)
2785 {
2786 const char *name;
2787
2788 name = (bfd_elf_string_from_elf_section
2789 (abfd,
2790 elf_elfheader (abfd)->e_shstrndx,
2791 elf_section_data (sec)->rel_hdr.sh_name));
2792 if (name == NULL)
2793 return FALSE;
2794
2795 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
2796 && strcmp (bfd_get_section_name (abfd, sec),
2797 name + 5) == 0);
2798
2799 sreloc = bfd_get_section_by_name (dynobj, name);
2800 if (sreloc == NULL)
2801 {
2802 sreloc = bfd_make_section_with_flags (dynobj, name,
2803 (SEC_ALLOC
2804 | SEC_LOAD
2805 | SEC_HAS_CONTENTS
2806 | SEC_IN_MEMORY
2807 | SEC_LINKER_CREATED
2808 | SEC_READONLY));
2809 if (sreloc == NULL
2810 || !bfd_set_section_alignment (dynobj, sreloc, 2))
2811 return FALSE;
2812 }
2813 if (sec->flags & SEC_READONLY)
2814 info->flags |= DF_TEXTREL;
2815 }
2816
2817 sreloc->size += sizeof (Elf32_External_Rela);
2818
2819 /* If we are linking with -Bsymbolic, we count the number of PC
2820 relative relocations we have entered for this symbol, so that
2821 we can discard them again if the symbol is later defined by a
2822 regular object. We know that h is really a pointer to an
2823 elf_cris_link_hash_entry. */
2824 if ((r_type == R_CRIS_8_PCREL
2825 || r_type == R_CRIS_16_PCREL
2826 || r_type == R_CRIS_32_PCREL)
2827 && info->symbolic)
2828 {
2829 struct elf_cris_link_hash_entry *eh;
2830 struct elf_cris_pcrel_relocs_copied *p;
2831
2832 eh = (struct elf_cris_link_hash_entry *) h;
2833
2834 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2835 if (p->section == sreloc)
2836 break;
2837
2838 if (p == NULL)
2839 {
2840 p = ((struct elf_cris_pcrel_relocs_copied *)
2841 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2842 if (p == NULL)
2843 return FALSE;
2844 p->next = eh->pcrel_relocs_copied;
2845 eh->pcrel_relocs_copied = p;
2846 p->section = sreloc;
2847 p->count = 0;
2848 }
2849
2850 ++p->count;
2851 }
2852 break;
2853
2854 /* This relocation describes the C++ object vtable hierarchy.
2855 Reconstruct it for later use during GC. */
2856 case R_CRIS_GNU_VTINHERIT:
2857 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2858 return FALSE;
2859 break;
2860
2861 /* This relocation describes which C++ vtable entries are actually
2862 used. Record for later use during GC. */
2863 case R_CRIS_GNU_VTENTRY:
2864 BFD_ASSERT (h != NULL);
2865 if (h != NULL
2866 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2867 return FALSE;
2868 break;
2869
2870 default:
2871 /* Other relocs do not appear here. */
2872 bfd_set_error (bfd_error_bad_value);
2873 return FALSE;
2874 }
2875 }
2876
2877 return TRUE;
2878 }
2879
2880 /* Set the sizes of the dynamic sections. */
2881
2882 static bfd_boolean
2883 elf_cris_size_dynamic_sections (output_bfd, info)
2884 bfd *output_bfd ATTRIBUTE_UNUSED;
2885 struct bfd_link_info *info;
2886 {
2887 bfd *dynobj;
2888 asection *s;
2889 bfd_boolean plt;
2890 bfd_boolean relocs;
2891
2892 dynobj = elf_hash_table (info)->dynobj;
2893 BFD_ASSERT (dynobj != NULL);
2894
2895 if (elf_hash_table (info)->dynamic_sections_created)
2896 {
2897 /* Set the contents of the .interp section to the interpreter. */
2898 if (info->executable)
2899 {
2900 s = bfd_get_section_by_name (dynobj, ".interp");
2901 BFD_ASSERT (s != NULL);
2902 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2903 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2904 }
2905 }
2906 else
2907 {
2908 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
2909 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2910 elf_cris_adjust_gotplt_to_got,
2911 (PTR) info);
2912
2913 /* We may have created entries in the .rela.got section.
2914 However, if we are not creating the dynamic sections, we will
2915 not actually use these entries. Reset the size of .rela.got,
2916 which will cause it to get stripped from the output file
2917 below. */
2918 s = bfd_get_section_by_name (dynobj, ".rela.got");
2919 if (s != NULL)
2920 s->size = 0;
2921 }
2922
2923 /* If this is a -Bsymbolic shared link, then we need to discard all PC
2924 relative relocs against symbols defined in a regular object. We
2925 allocated space for them in the check_relocs routine, but we will not
2926 fill them in in the relocate_section routine. We also discard space
2927 for relocs that have become for local symbols due to symbol
2928 visibility changes. For programs, we discard space for relocs for
2929 symbols not referenced by any dynamic object. */
2930 if (info->shared)
2931 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2932 elf_cris_discard_excess_dso_dynamics,
2933 (PTR) info);
2934 else
2935 elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2936 elf_cris_discard_excess_program_dynamics,
2937 (PTR) info);
2938
2939 /* The check_relocs and adjust_dynamic_symbol entry points have
2940 determined the sizes of the various dynamic sections. Allocate
2941 memory for them. */
2942 plt = FALSE;
2943 relocs = FALSE;
2944 for (s = dynobj->sections; s != NULL; s = s->next)
2945 {
2946 const char *name;
2947
2948 if ((s->flags & SEC_LINKER_CREATED) == 0)
2949 continue;
2950
2951 /* It's OK to base decisions on the section name, because none
2952 of the dynobj section names depend upon the input files. */
2953 name = bfd_get_section_name (dynobj, s);
2954
2955 if (strcmp (name, ".plt") == 0)
2956 {
2957 /* Remember whether there is a PLT. */
2958 plt = s->size != 0;
2959 }
2960 else if (CONST_STRNEQ (name, ".rela"))
2961 {
2962 if (s->size != 0)
2963 {
2964 /* Remember whether there are any reloc sections other
2965 than .rela.plt. */
2966 if (strcmp (name, ".rela.plt") != 0)
2967 relocs = TRUE;
2968
2969 /* We use the reloc_count field as a counter if we need
2970 to copy relocs into the output file. */
2971 s->reloc_count = 0;
2972 }
2973 }
2974 else if (! CONST_STRNEQ (name, ".got")
2975 && strcmp (name, ".dynbss") != 0)
2976 {
2977 /* It's not one of our sections, so don't allocate space. */
2978 continue;
2979 }
2980
2981 if (s->size == 0)
2982 {
2983 /* If we don't need this section, strip it from the
2984 output file. This is mostly to handle .rela.bss and
2985 .rela.plt. We must create both sections in
2986 create_dynamic_sections, because they must be created
2987 before the linker maps input sections to output
2988 sections. The linker does that before
2989 adjust_dynamic_symbol is called, and it is that
2990 function which decides whether anything needs to go
2991 into these sections. */
2992 s->flags |= SEC_EXCLUDE;
2993 continue;
2994 }
2995
2996 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2997 continue;
2998
2999 /* Allocate memory for the section contents. We use bfd_zalloc here
3000 in case unused entries are not reclaimed before the section's
3001 contents are written out. This should not happen, but this way
3002 if it does, we will not write out garbage. For reloc sections,
3003 this will make entries have the type R_CRIS_NONE. */
3004 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3005 if (s->contents == NULL)
3006 return FALSE;
3007 }
3008
3009 if (elf_hash_table (info)->dynamic_sections_created)
3010 {
3011 /* Add some entries to the .dynamic section. We fill in the
3012 values later, in elf_cris_finish_dynamic_sections, but we
3013 must add the entries now so that we get the correct size for
3014 the .dynamic section. The DT_DEBUG entry is filled in by the
3015 dynamic linker and used by the debugger. */
3016 #define add_dynamic_entry(TAG, VAL) \
3017 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3018
3019 if (!info->shared)
3020 {
3021 if (!add_dynamic_entry (DT_DEBUG, 0))
3022 return FALSE;
3023 }
3024
3025 if (plt)
3026 {
3027 if (!add_dynamic_entry (DT_PLTGOT, 0)
3028 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3029 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3030 || !add_dynamic_entry (DT_JMPREL, 0))
3031 return FALSE;
3032 }
3033
3034 if (relocs)
3035 {
3036 if (!add_dynamic_entry (DT_RELA, 0)
3037 || !add_dynamic_entry (DT_RELASZ, 0)
3038 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3039 return FALSE;
3040 }
3041
3042 if ((info->flags & DF_TEXTREL) != 0)
3043 {
3044 if (!add_dynamic_entry (DT_TEXTREL, 0))
3045 return FALSE;
3046 info->flags |= DF_TEXTREL;
3047 }
3048 }
3049 #undef add_dynamic_entry
3050
3051 return TRUE;
3052 }
3053
3054 /* This function is called via elf_cris_link_hash_traverse if we are
3055 creating a shared object. In the -Bsymbolic case, it discards the
3056 space allocated to copy PC relative relocs against symbols which
3057 are defined in regular objects. For the normal non-symbolic case,
3058 we also discard space for relocs that have become local due to
3059 symbol visibility changes. We allocated space for them in the
3060 check_relocs routine, but we won't fill them in in the
3061 relocate_section routine. */
3062
3063 static bfd_boolean
3064 elf_cris_discard_excess_dso_dynamics (h, inf)
3065 struct elf_cris_link_hash_entry *h;
3066 PTR inf;
3067 {
3068 struct elf_cris_pcrel_relocs_copied *s;
3069 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3070
3071 if (h->root.root.type == bfd_link_hash_warning)
3072 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3073
3074 /* If a symbol has been forced local or we have found a regular
3075 definition for the symbolic link case, then we won't be needing
3076 any relocs. */
3077 if (h->root.def_regular
3078 && (h->root.forced_local
3079 || info->symbolic))
3080 {
3081 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3082 s->section->size -= s->count * sizeof (Elf32_External_Rela);
3083 }
3084
3085 return TRUE;
3086 }
3087
3088 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3089 creating a shared object. We discard space for relocs for symbols put
3090 in the .got, but which we found we do not have to resolve at run-time. */
3091
3092 static bfd_boolean
3093 elf_cris_discard_excess_program_dynamics (h, inf)
3094 struct elf_cris_link_hash_entry *h;
3095 PTR inf;
3096 {
3097 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3098
3099 if (h->root.root.type == bfd_link_hash_warning)
3100 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3101
3102 /* If we're not creating a shared library and have a symbol which is
3103 referred to by .got references, but the symbol is defined locally,
3104 (or rather, not defined by a DSO) then lose the reloc for the .got
3105 (don't allocate room for it). Likewise for relocs for something
3106 for which we create a PLT. */
3107 if (!h->root.def_dynamic
3108 || h->root.plt.refcount > 0)
3109 {
3110 if (h->root.got.refcount > 0
3111 /* The size of this section is only valid and in sync with the
3112 various reference counts if we do dynamic; don't decrement it
3113 otherwise. */
3114 && elf_hash_table (info)->dynamic_sections_created)
3115 {
3116 bfd *dynobj = elf_hash_table (info)->dynobj;
3117 asection *srelgot;
3118
3119 BFD_ASSERT (dynobj != NULL);
3120
3121 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3122
3123 BFD_ASSERT (srelgot != NULL);
3124
3125 srelgot->size -= sizeof (Elf32_External_Rela);
3126 }
3127
3128 /* If the locally-defined symbol isn't used by a DSO, then we don't
3129 have to export it as a dynamic symbol. This was already done for
3130 functions; doing this for all symbols would presumably not
3131 introduce new problems. Of course we don't do this if we're
3132 exporting all dynamic symbols. */
3133 if (! info->export_dynamic
3134 && h->root.dynindx != -1
3135 && !h->root.def_dynamic
3136 && !h->root.ref_dynamic)
3137 {
3138 h->root.dynindx = -1;
3139 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3140 h->root.dynstr_index);
3141 }
3142 }
3143
3144 return TRUE;
3145 }
3146
3147 /* Reject a file depending on presence and expectation of prefixed
3148 underscores on symbols. */
3149
3150 static bfd_boolean
3151 cris_elf_object_p (abfd)
3152 bfd *abfd;
3153 {
3154 if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
3155 return FALSE;
3156
3157 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
3158 return (bfd_get_symbol_leading_char (abfd) == '_');
3159 else
3160 return (bfd_get_symbol_leading_char (abfd) == 0);
3161 }
3162
3163 /* Mark presence or absence of leading underscore. Set machine type
3164 flags from mach type. */
3165
3166 static void
3167 cris_elf_final_write_processing (abfd, linker)
3168 bfd *abfd;
3169 bfd_boolean linker ATTRIBUTE_UNUSED;
3170 {
3171 unsigned long e_flags = elf_elfheader (abfd)->e_flags;
3172
3173 e_flags &= ~EF_CRIS_UNDERSCORE;
3174 if (bfd_get_symbol_leading_char (abfd) == '_')
3175 e_flags |= EF_CRIS_UNDERSCORE;
3176
3177 switch (bfd_get_mach (abfd))
3178 {
3179 case bfd_mach_cris_v0_v10:
3180 e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
3181 break;
3182
3183 case bfd_mach_cris_v10_v32:
3184 e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
3185 break;
3186
3187 case bfd_mach_cris_v32:
3188 e_flags |= EF_CRIS_VARIANT_V32;
3189 break;
3190
3191 default:
3192 _bfd_abort (__FILE__, __LINE__,
3193 _("Unexpected machine number"));
3194 }
3195
3196 elf_elfheader (abfd)->e_flags = e_flags;
3197 }
3198
3199 /* Set the mach type from e_flags value. */
3200
3201 static bfd_boolean
3202 cris_elf_set_mach_from_flags (abfd, flags)
3203 bfd *abfd;
3204 unsigned long flags;
3205 {
3206 switch (flags & EF_CRIS_VARIANT_MASK)
3207 {
3208 case EF_CRIS_VARIANT_ANY_V0_V10:
3209 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
3210 break;
3211
3212 case EF_CRIS_VARIANT_V32:
3213 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
3214 break;
3215
3216 case EF_CRIS_VARIANT_COMMON_V10_V32:
3217 bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
3218 break;
3219
3220 default:
3221 /* Since we don't recognize them, we obviously can't support them
3222 with this code; we'd have to require that all future handling
3223 would be optional. */
3224 bfd_set_error (bfd_error_wrong_format);
3225 return FALSE;
3226 }
3227
3228 return TRUE;
3229 }
3230
3231 /* Display the flags field. */
3232
3233 static bfd_boolean
3234 cris_elf_print_private_bfd_data (abfd, ptr)
3235 bfd *abfd;
3236 PTR ptr;
3237 {
3238 FILE *file = (FILE *) ptr;
3239
3240 BFD_ASSERT (abfd != NULL && ptr != NULL);
3241
3242 _bfd_elf_print_private_bfd_data (abfd, ptr);
3243
3244 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3245
3246 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
3247 fprintf (file, _(" [symbols have a _ prefix]"));
3248 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3249 == EF_CRIS_VARIANT_COMMON_V10_V32)
3250 fprintf (file, _(" [v10 and v32]"));
3251 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3252 == EF_CRIS_VARIANT_V32)
3253 fprintf (file, _(" [v32]"));
3254
3255 fputc ('\n', file);
3256 return TRUE;
3257 }
3258
3259 /* Don't mix files with and without a leading underscore. */
3260
3261 static bfd_boolean
3262 cris_elf_merge_private_bfd_data (ibfd, obfd)
3263 bfd *ibfd;
3264 bfd *obfd;
3265 {
3266 int imach, omach;
3267
3268 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3269 return FALSE;
3270
3271 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3272 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3273 return TRUE;
3274
3275 imach = bfd_get_mach (ibfd);
3276
3277 if (! elf_flags_init (obfd))
3278 {
3279 /* This happens when ld starts out with a 'blank' output file. */
3280 elf_flags_init (obfd) = TRUE;
3281
3282 /* We ignore the linker-set mach, and instead set it according to
3283 the first input file. This would also happen if we could
3284 somehow filter out the OUTPUT_ARCH () setting from elf.sc.
3285 This allows us to keep the same linker config across
3286 cris(v0..v10) and crisv32. The drawback is that we can't force
3287 the output type, which might be a sane thing to do for a
3288 v10+v32 compatibility object. */
3289 if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3290 return FALSE;
3291 }
3292
3293 if (bfd_get_symbol_leading_char (ibfd)
3294 != bfd_get_symbol_leading_char (obfd))
3295 {
3296 (*_bfd_error_handler)
3297 (bfd_get_symbol_leading_char (ibfd) == '_'
3298 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3299 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3300 ibfd);
3301 bfd_set_error (bfd_error_bad_value);
3302 return FALSE;
3303 }
3304
3305 omach = bfd_get_mach (obfd);
3306
3307 if (imach != omach)
3308 {
3309 /* We can get an incompatible combination only if either is
3310 bfd_mach_cris_v32, and the other one isn't compatible. */
3311 if ((imach == bfd_mach_cris_v32
3312 && omach != bfd_mach_cris_v10_v32)
3313 || (omach == bfd_mach_cris_v32
3314 && imach != bfd_mach_cris_v10_v32))
3315 {
3316 (*_bfd_error_handler)
3317 ((imach == bfd_mach_cris_v32)
3318 ? _("%B contains CRIS v32 code, incompatible"
3319 " with previous objects")
3320 : _("%B contains non-CRIS-v32 code, incompatible"
3321 " with previous objects"),
3322 ibfd);
3323 bfd_set_error (bfd_error_bad_value);
3324 return FALSE;
3325 }
3326
3327 /* We don't have to check the case where the input is compatible
3328 with v10 and v32, because the output is already known to be set
3329 to the other (compatible) mach. */
3330 if (omach == bfd_mach_cris_v10_v32
3331 && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3332 return FALSE;
3333 }
3334
3335 return TRUE;
3336 }
3337
3338 /* Do side-effects of e_flags copying to obfd. */
3339
3340 static bfd_boolean
3341 cris_elf_copy_private_bfd_data (ibfd, obfd)
3342 bfd *ibfd;
3343 bfd *obfd;
3344 {
3345 /* Call the base function. */
3346 if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
3347 return FALSE;
3348
3349 /* If output is big-endian for some obscure reason, stop here. */
3350 if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
3351 return FALSE;
3352
3353 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3354 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3355 return TRUE;
3356
3357 /* Do what we really came here for. */
3358 return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
3359 }
3360
3361 static enum elf_reloc_type_class
3362 elf_cris_reloc_type_class (rela)
3363 const Elf_Internal_Rela *rela;
3364 {
3365 switch ((int) ELF32_R_TYPE (rela->r_info))
3366 {
3367 case R_CRIS_RELATIVE:
3368 return reloc_class_relative;
3369 case R_CRIS_JUMP_SLOT:
3370 return reloc_class_plt;
3371 case R_CRIS_COPY:
3372 return reloc_class_copy;
3373 default:
3374 return reloc_class_normal;
3375 }
3376 }
3377
3378 #define ELF_ARCH bfd_arch_cris
3380 #define ELF_MACHINE_CODE EM_CRIS
3381 #define ELF_MAXPAGESIZE 0x2000
3382
3383 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
3384 #define TARGET_LITTLE_NAME "elf32-cris"
3385 #define elf_symbol_leading_char 0
3386
3387 #define elf_info_to_howto_rel NULL
3388 #define elf_info_to_howto cris_info_to_howto_rela
3389 #define elf_backend_relocate_section cris_elf_relocate_section
3390 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
3391 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
3392 #define elf_backend_check_relocs cris_elf_check_relocs
3393 #define elf_backend_grok_prstatus cris_elf_grok_prstatus
3394 #define elf_backend_grok_psinfo cris_elf_grok_psinfo
3395
3396 #define elf_backend_can_gc_sections 1
3397 #define elf_backend_can_refcount 1
3398
3399 #define elf_backend_object_p cris_elf_object_p
3400 #define elf_backend_final_write_processing \
3401 cris_elf_final_write_processing
3402 #define bfd_elf32_bfd_print_private_bfd_data \
3403 cris_elf_print_private_bfd_data
3404 #define bfd_elf32_bfd_merge_private_bfd_data \
3405 cris_elf_merge_private_bfd_data
3406 #define bfd_elf32_bfd_copy_private_bfd_data \
3407 cris_elf_copy_private_bfd_data
3408
3409 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
3410 #define bfd_elf32_bfd_reloc_name_lookup cris_reloc_name_lookup
3411
3412 #define bfd_elf32_bfd_link_hash_table_create \
3413 elf_cris_link_hash_table_create
3414 #define elf_backend_adjust_dynamic_symbol \
3415 elf_cris_adjust_dynamic_symbol
3416 #define elf_backend_size_dynamic_sections \
3417 elf_cris_size_dynamic_sections
3418 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3419 #define elf_backend_finish_dynamic_symbol \
3420 elf_cris_finish_dynamic_symbol
3421 #define elf_backend_finish_dynamic_sections \
3422 elf_cris_finish_dynamic_sections
3423 #define elf_backend_create_dynamic_sections \
3424 _bfd_elf_create_dynamic_sections
3425 #define bfd_elf32_bfd_final_link \
3426 bfd_elf_gc_common_final_link
3427 #define elf_backend_hide_symbol elf_cris_hide_symbol
3428 #define elf_backend_reloc_type_class elf_cris_reloc_type_class
3429
3430 #define elf_backend_want_got_plt 1
3431 #define elf_backend_plt_readonly 1
3432 #define elf_backend_want_plt_sym 0
3433 #define elf_backend_got_header_size 12
3434
3435 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3436 linking and libraries (if it's a win of any significance). Until then,
3437 take the easy route. */
3438 #define elf_backend_may_use_rel_p 0
3439 #define elf_backend_may_use_rela_p 1
3440 #define elf_backend_rela_normal 1
3441
3442 #include "elf32-target.h"
3443
3444 #undef TARGET_LITTLE_SYM
3445 #undef TARGET_LITTLE_NAME
3446 #undef elf_symbol_leading_char
3447
3448 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3449 #define TARGET_LITTLE_NAME "elf32-us-cris"
3450 #define elf_symbol_leading_char '_'
3451 #undef elf32_bed
3452 #define elf32_bed elf32_us_cris_bed
3453
3454 #include "elf32-target.h"
3455