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