daily update
[deliverable/binutils-gdb.git] / bfd / elf32-nios2.c
CommitLineData
36591ba1
SL
1/* 32-bit ELF support for Nios II.
2 Copyright (C) 2012, 2013 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23/* This file handles Altera Nios II ELF targets. */
24
25#include "sysdep.h"
26#include "bfd.h"
27#include "libbfd.h"
28#include "bfdlink.h"
29#include "genlink.h"
30#include "elf-bfd.h"
31#include "elf/nios2.h"
32#include "opcode/nios2.h"
33
34/* Use RELA relocations. */
35#ifndef USE_RELA
36#define USE_RELA
37#endif
38
39#ifdef USE_REL
40#undef USE_REL
41#endif
42
43/* Forward declarations. */
44static bfd_reloc_status_type nios2_elf32_ignore_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46static bfd_reloc_status_type nios2_elf32_hi16_relocate
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48static bfd_reloc_status_type nios2_elf32_lo16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58static bfd_reloc_status_type nios2_elf32_call26_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60static bfd_reloc_status_type nios2_elf32_gprel_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62static bfd_reloc_status_type nios2_elf32_ujmp_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64static bfd_reloc_status_type nios2_elf32_cjmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66static bfd_reloc_status_type nios2_elf32_callr_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68
69/* Target vector. */
70extern const bfd_target bfd_elf32_littlenios2_vec;
71extern const bfd_target bfd_elf32_bignios2_vec;
72
73/* Offset of tp and dtp pointers from start of TLS block. */
74#define TP_OFFSET 0x7000
75#define DTP_OFFSET 0x8000
76
77/* The relocation table used for SHT_REL sections. */
78static reloc_howto_type elf_nios2_howto_table_rel[] = {
79 /* No relocation. */
80 HOWTO (R_NIOS2_NONE, /* type */
81 0, /* rightshift */
82 0, /* size (0 = byte, 1 = short, 2 = long) */
83 0, /* bitsize */
84 FALSE, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_NIOS2_NONE", /* name */
89 FALSE, /* partial_inplace */
90 0, /* src_mask */
91 0, /* dst_mask */
92 FALSE), /* pcrel_offset */
93
94 /* 16-bit signed immediate relocation. */
95 HOWTO (R_NIOS2_S16, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 16, /* bitsize */
99 FALSE, /* pc_relative */
100 6, /* bitpos */
101 complain_overflow_signed, /* complain on overflow */
102 bfd_elf_generic_reloc, /* special function */
103 "R_NIOS2_S16", /* name */
104 FALSE, /* partial_inplace */
105 0x003fffc0, /* src_mask */
106 0x003fffc0, /* dest_mask */
107 FALSE), /* pcrel_offset */
108
109 /* 16-bit unsigned immediate relocation. */
110 HOWTO (R_NIOS2_U16, /* type */
111 0, /* rightshift */
112 2, /* size (0 = byte, 1 = short, 2 = long) */
113 16, /* bitsize */
114 FALSE, /* pc_relative */
115 6, /* bitpos */
116 complain_overflow_unsigned, /* complain on overflow */
117 bfd_elf_generic_reloc, /* special function */
118 "R_NIOS2_U16", /* name */
119 FALSE, /* partial_inplace */
120 0x003fffc0, /* src_mask */
121 0x003fffc0, /* dest_mask */
122 FALSE), /* pcrel_offset */
123
124 HOWTO (R_NIOS2_PCREL16, /* type */
125 0, /* rightshift */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
127 16, /* bitsize */
128 TRUE, /* pc_relative */
129 6, /* bitpos */
130 complain_overflow_signed, /* complain on overflow */
131 nios2_elf32_pcrel16_relocate, /* special function */
132 "R_NIOS2_PCREL16", /* name */
133 FALSE, /* partial_inplace */
134 0x003fffc0, /* src_mask */
135 0x003fffc0, /* dest_mask */
136 TRUE), /* pcrel_offset */
137
138 HOWTO (R_NIOS2_CALL26, /* type */
139 2, /* rightshift */
140 2, /* size (0 = byte, 1 = short, 2 = long) */
141 26, /* bitsize */
142 FALSE, /* pc_relative */
143 6, /* bitpos */
144 complain_overflow_dont, /* complain on overflow */
145 nios2_elf32_call26_relocate, /* special function */
146 "R_NIOS2_CALL26", /* name */
147 FALSE, /* partial_inplace */
148 0xffffffc0, /* src_mask */
149 0xffffffc0, /* dst_mask */
150 FALSE), /* pcrel_offset */
151
152 HOWTO (R_NIOS2_IMM5,
153 0,
154 2,
155 5,
156 FALSE,
157 6,
158 complain_overflow_bitfield,
159 bfd_elf_generic_reloc,
160 "R_NIOS2_IMM5",
161 FALSE,
162 0x000007c0,
163 0x000007c0,
164 FALSE),
165
166 HOWTO (R_NIOS2_CACHE_OPX,
167 0,
168 2,
169 5,
170 FALSE,
171 22,
172 complain_overflow_bitfield,
173 bfd_elf_generic_reloc,
174 "R_NIOS2_CACHE_OPX",
175 FALSE,
176 0x07c00000,
177 0x07c00000,
178 FALSE),
179
180 HOWTO (R_NIOS2_IMM6,
181 0,
182 2,
183 6,
184 FALSE,
185 6,
186 complain_overflow_bitfield,
187 bfd_elf_generic_reloc,
188 "R_NIOS2_IMM6",
189 FALSE,
190 0x00000fc0,
191 0x00000fc0,
192 FALSE),
193
194 HOWTO (R_NIOS2_IMM8,
195 0,
196 2,
197 8,
198 FALSE,
199 6,
200 complain_overflow_bitfield,
201 bfd_elf_generic_reloc,
202 "R_NIOS2_IMM8",
203 FALSE,
204 0x00003fc0,
205 0x00003fc0,
206 FALSE),
207
208 HOWTO (R_NIOS2_HI16,
209 0,
210 2,
211 32,
212 FALSE,
213 6,
214 complain_overflow_dont,
215 nios2_elf32_hi16_relocate,
216 "R_NIOS2_HI16",
217 FALSE,
218 0x003fffc0,
219 0x003fffc0,
220 FALSE),
221
222 HOWTO (R_NIOS2_LO16,
223 0,
224 2,
225 32,
226 FALSE,
227 6,
228 complain_overflow_dont,
229 nios2_elf32_lo16_relocate,
230 "R_NIOS2_LO16",
231 FALSE,
232 0x003fffc0,
233 0x003fffc0,
234 FALSE),
235
236 HOWTO (R_NIOS2_HIADJ16,
237 0,
238 2,
239 32,
240 FALSE,
241 6,
242 complain_overflow_dont,
243 nios2_elf32_hiadj16_relocate,
244 "R_NIOS2_HIADJ16",
245 FALSE,
246 0x003fffc0,
247 0x003fffc0,
248 FALSE),
249
250 HOWTO (R_NIOS2_BFD_RELOC_32,
251 0,
252 2, /* long */
253 32,
254 FALSE,
255 0,
256 complain_overflow_dont,
257 bfd_elf_generic_reloc,
258 "R_NIOS2_BFD_RELOC32",
259 FALSE,
260 0xffffffff,
261 0xffffffff,
262 FALSE),
263
264 HOWTO (R_NIOS2_BFD_RELOC_16,
265 0,
266 1, /* short */
267 16,
268 FALSE,
269 0,
270 complain_overflow_bitfield,
271 bfd_elf_generic_reloc,
272 "R_NIOS2_BFD_RELOC16",
273 FALSE,
274 0x0000ffff,
275 0x0000ffff,
276 FALSE),
277
278 HOWTO (R_NIOS2_BFD_RELOC_8,
279 0,
280 0, /* byte */
281 8,
282 FALSE,
283 0,
284 complain_overflow_bitfield,
285 bfd_elf_generic_reloc,
286 "R_NIOS2_BFD_RELOC8",
287 FALSE,
288 0x000000ff,
289 0x000000ff,
290 FALSE),
291
292 HOWTO (R_NIOS2_GPREL,
293 0,
294 2,
295 32,
296 FALSE,
297 6,
298 complain_overflow_dont,
299 nios2_elf32_gprel_relocate,
300 "R_NIOS2_GPREL",
301 FALSE,
302 0x003fffc0,
303 0x003fffc0,
304 FALSE),
305
306 HOWTO (R_NIOS2_GNU_VTINHERIT,
307 0,
308 2, /* short */
309 0,
310 FALSE,
311 0,
312 complain_overflow_dont,
313 NULL,
314 "R_NIOS2_GNU_VTINHERIT",
315 FALSE,
316 0,
317 0,
318 FALSE),
319
320 HOWTO (R_NIOS2_GNU_VTENTRY,
321 0,
322 2, /* byte */
323 0,
324 FALSE,
325 0,
326 complain_overflow_dont,
327 _bfd_elf_rel_vtable_reloc_fn,
328 "R_NIOS2_GNU_VTENTRY",
329 FALSE,
330 0,
331 0,
332 FALSE),
333
334 HOWTO (R_NIOS2_UJMP,
335 0,
336 2,
337 32,
338 FALSE,
339 6,
340 complain_overflow_dont,
341 nios2_elf32_ujmp_relocate,
342 "R_NIOS2_UJMP",
343 FALSE,
344 0x003fffc0,
345 0x003fffc0,
346 FALSE),
347
348 HOWTO (R_NIOS2_CJMP,
349 0,
350 2,
351 32,
352 FALSE,
353 6,
354 complain_overflow_dont,
355 nios2_elf32_cjmp_relocate,
356 "R_NIOS2_CJMP",
357 FALSE,
358 0x003fffc0,
359 0x003fffc0,
360 FALSE),
361
362 HOWTO (R_NIOS2_CALLR,
363 0,
364 2,
365 32,
366 FALSE,
367 6,
368 complain_overflow_dont,
369 nios2_elf32_callr_relocate,
370 "R_NIOS2_CALLR",
371 FALSE,
372 0x003fffc0,
373 0x003fffc0,
374 FALSE),
375
376 HOWTO (R_NIOS2_ALIGN,
377 0,
378 2,
379 0,
380 FALSE,
381 0,
382 complain_overflow_dont,
383 nios2_elf32_ignore_reloc,
384 "R_NIOS2_ALIGN",
385 FALSE,
386 0,
387 0,
388 TRUE),
389
390
391 HOWTO (R_NIOS2_GOT16,
392 0,
393 2,
394 16,
395 FALSE,
396 6,
397 complain_overflow_bitfield,
398 bfd_elf_generic_reloc,
399 "R_NIOS2_GOT16",
400 FALSE,
401 0x003fffc0,
402 0x003fffc0,
403 FALSE),
404
405 HOWTO (R_NIOS2_CALL16,
406 0,
407 2,
408 16,
409 FALSE,
410 6,
411 complain_overflow_bitfield,
412 bfd_elf_generic_reloc,
413 "R_NIOS2_CALL16",
414 FALSE,
415 0x003fffc0,
416 0x003fffc0,
417 FALSE),
418
419 HOWTO (R_NIOS2_GOTOFF_LO,
420 0,
421 2,
422 16,
423 FALSE,
424 6,
425 complain_overflow_dont,
426 bfd_elf_generic_reloc,
427 "R_NIOS2_GOTOFF_LO",
428 FALSE,
429 0x003fffc0,
430 0x003fffc0,
431 FALSE),
432
433 HOWTO (R_NIOS2_GOTOFF_HA,
434 0,
435 2,
436 16,
437 FALSE,
438 6,
439 complain_overflow_dont,
440 bfd_elf_generic_reloc,
441 "R_NIOS2_GOTOFF_HA",
442 FALSE,
443 0x003fffc0,
444 0x003fffc0,
445 FALSE),
446
447 HOWTO (R_NIOS2_PCREL_LO,
448 0,
449 2,
450 16,
451 TRUE,
452 6,
453 complain_overflow_dont,
454 nios2_elf32_pcrel_lo16_relocate,
455 "R_NIOS2_PCREL_LO",
456 FALSE,
457 0x003fffc0,
458 0x003fffc0,
459 TRUE),
460
461 HOWTO (R_NIOS2_PCREL_HA,
462 0,
463 2,
464 16,
465 FALSE, /* This is a PC-relative relocation, but we need to subtract
466 PC ourselves before the HIADJ. */
467 6,
468 complain_overflow_dont,
469 nios2_elf32_pcrel_hiadj16_relocate,
470 "R_NIOS2_PCREL_HA",
471 FALSE,
472 0x003fffc0,
473 0x003fffc0,
474 TRUE),
475
476 HOWTO (R_NIOS2_TLS_GD16,
477 0,
478 2,
479 16,
480 FALSE,
481 6,
482 complain_overflow_bitfield,
483 bfd_elf_generic_reloc,
484 "R_NIOS2_TLS_GD16",
485 FALSE,
486 0x003fffc0,
487 0x003fffc0,
488 FALSE),
489
490 HOWTO (R_NIOS2_TLS_LDM16,
491 0,
492 2,
493 16,
494 FALSE,
495 6,
496 complain_overflow_bitfield,
497 bfd_elf_generic_reloc,
498 "R_NIOS2_TLS_LDM16",
499 FALSE,
500 0x003fffc0,
501 0x003fffc0,
502 FALSE),
503
504 HOWTO (R_NIOS2_TLS_LDO16,
505 0,
506 2,
507 16,
508 FALSE,
509 6,
510 complain_overflow_bitfield,
511 bfd_elf_generic_reloc,
512 "R_NIOS2_TLS_LDO16",
513 FALSE,
514 0x003fffc0,
515 0x003fffc0,
516 FALSE),
517
518 HOWTO (R_NIOS2_TLS_IE16,
519 0,
520 2,
521 16,
522 FALSE,
523 6,
524 complain_overflow_bitfield,
525 bfd_elf_generic_reloc,
526 "R_NIOS2_TLS_IE16",
527 FALSE,
528 0x003fffc0,
529 0x003fffc0,
530 FALSE),
531
532 HOWTO (R_NIOS2_TLS_LE16,
533 0,
534 2,
535 16,
536 FALSE,
537 6,
538 complain_overflow_bitfield,
539 bfd_elf_generic_reloc,
540 "R_NIOS2_TLS_LE16",
541 FALSE,
542 0x003fffc0,
543 0x003fffc0,
544 FALSE),
545
546 HOWTO (R_NIOS2_TLS_DTPMOD,
547 0,
548 2,
549 32,
550 FALSE,
551 0,
552 complain_overflow_dont,
553 bfd_elf_generic_reloc,
554 "R_NIOS2_TLS_DTPMOD",
555 FALSE,
556 0xffffffff,
557 0xffffffff,
558 FALSE),
559
560 HOWTO (R_NIOS2_TLS_DTPREL,
561 0,
562 2,
563 32,
564 FALSE,
565 0,
566 complain_overflow_dont,
567 bfd_elf_generic_reloc,
568 "R_NIOS2_TLS_DTPREL",
569 FALSE,
570 0xffffffff,
571 0xffffffff,
572 FALSE),
573
574 HOWTO (R_NIOS2_TLS_TPREL,
575 0,
576 2,
577 32,
578 FALSE,
579 0,
580 complain_overflow_dont,
581 bfd_elf_generic_reloc,
582 "R_NIOS2_TLS_TPREL",
583 FALSE,
584 0xffffffff,
585 0xffffffff,
586 FALSE),
587
588 HOWTO (R_NIOS2_COPY,
589 0,
590 2,
591 32,
592 FALSE,
593 0,
594 complain_overflow_dont,
595 bfd_elf_generic_reloc,
596 "R_NIOS2_COPY",
597 FALSE,
598 0,
599 0,
600 FALSE),
601
602 HOWTO (R_NIOS2_GLOB_DAT,
603 0,
604 2,
605 32,
606 FALSE,
607 0,
608 complain_overflow_dont,
609 bfd_elf_generic_reloc,
610 "R_NIOS2_GLOB_DAT",
611 FALSE,
612 0xffffffff,
613 0xffffffff,
614 FALSE),
615
616 HOWTO (R_NIOS2_JUMP_SLOT,
617 0,
618 2,
619 32,
620 FALSE,
621 0,
622 complain_overflow_dont,
623 bfd_elf_generic_reloc,
624 "R_NIOS2_JUMP_SLOT",
625 FALSE,
626 0xffffffff,
627 0xffffffff,
628 FALSE),
629
630 HOWTO (R_NIOS2_RELATIVE,
631 0,
632 2,
633 32,
634 FALSE,
635 0,
636 complain_overflow_dont,
637 bfd_elf_generic_reloc,
638 "R_NIOS2_RELATIVE",
639 FALSE,
640 0xffffffff,
641 0xffffffff,
642 FALSE),
643
644 HOWTO (R_NIOS2_GOTOFF,
645 0,
646 2,
647 32,
648 FALSE,
649 0,
650 complain_overflow_dont,
651 bfd_elf_generic_reloc,
652 "R_NIOS2_GOTOFF",
653 FALSE,
654 0xffffffff,
655 0xffffffff,
656 FALSE),
657
658/* Add other relocations here. */
659};
660
661static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
662
663/* Return the howto for relocation RTYPE. */
664static reloc_howto_type *
665lookup_howto (unsigned int rtype)
666{
667 static int initialized = 0;
668 int i;
669 int howto_tbl_size = (int) (sizeof (elf_nios2_howto_table_rel)
670 / sizeof (elf_nios2_howto_table_rel[0]));
671
672 if (!initialized)
673 {
674 initialized = 1;
675 memset (elf_code_to_howto_index, 0xff,
676 sizeof (elf_code_to_howto_index));
677 for (i = 0; i < howto_tbl_size; i++)
678 elf_code_to_howto_index[elf_nios2_howto_table_rel[i].type] = i;
679 }
680
681 BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL);
682 i = elf_code_to_howto_index[rtype];
683 if (i >= howto_tbl_size)
684 return 0;
685 return elf_nios2_howto_table_rel + i;
686}
687
688/* Map for converting BFD reloc types to Nios II reloc types. */
689struct elf_reloc_map
690{
691 bfd_reloc_code_real_type bfd_val;
692 enum elf_nios2_reloc_type elf_val;
693};
694
695static const struct elf_reloc_map nios2_reloc_map[] = {
696 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
697 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
698 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
699 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
700 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
701 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
702 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
703 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
704 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
705 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
706 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
707 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
708 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
709 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
710 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
711 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
712 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
713 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
714 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
715 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
716 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
717 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
718 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
719 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
720 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
721 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
722 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
723 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
724 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
725 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
726 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
727 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
728 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
729 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
730 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
731 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
732 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
733 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
734 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
735 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF}
736};
737
738/* The Nios II linker needs to keep track of the number of relocs that it
739 decides to copy as dynamic relocs in check_relocs for each symbol.
740 This is so that it can later discard them if they are found to be
741 unnecessary. We store the information in a field extending the
742 regular ELF linker hash table. */
743
744struct elf32_nios2_dyn_relocs
745{
746 struct elf32_nios2_dyn_relocs *next;
747
748 /* The input section of the reloc. */
749 asection *sec;
750
751 /* Total number of relocs copied for the input section. */
752 bfd_size_type count;
753
754 /* Number of pc-relative relocs copied for the input section. */
755 bfd_size_type pc_count;
756};
757
758/* Nios II ELF linker hash entry. */
759
760struct elf32_nios2_link_hash_entry
761{
762 struct elf_link_hash_entry root;
763
764 /* Track dynamic relocs copied for this symbol. */
765 struct elf32_nios2_dyn_relocs *dyn_relocs;
766
767#define GOT_UNKNOWN 0
768#define GOT_NORMAL 1
769#define GOT_TLS_GD 2
770#define GOT_TLS_IE 4
771 unsigned char tls_type;
772
773 /* We need to detect and take special action for symbols which are only
774 referenced with %call() and not with %got(). Such symbols do not need
775 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
776 linking will not work if the dynamic GOT reloc exists.
777 To check for this condition efficiently, we compare got_types_used against
778 CALL16_USED, meaning
779 (got_types_used & (GOT16_USED | CALL16_USED)) == CALL16_USED. */
780#define GOT16_USED 1
781#define CALL16_USED 2
782 unsigned char got_types_used;
783};
784
785#define elf32_nios2_hash_entry(ent) \
786 ((struct elf32_nios2_link_hash_entry *) (ent))
787
788/* Get the Nios II elf linker hash table from a link_info structure. */
789#define elf32_nios2_hash_table(info) \
790 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
791
792/* Nios II ELF linker hash table. */
793struct elf32_nios2_link_hash_table
794 {
795 /* The main hash table. */
796 struct elf_link_hash_table root;
797
798 /* Short-cuts to get to dynamic linker sections. */
799 asection *sdynbss;
800 asection *srelbss;
801 asection *sbss;
802
803 union {
804 bfd_signed_vma refcount;
805 bfd_vma offset;
806 } tls_ldm_got;
807
808 /* Small local sym cache. */
809 struct sym_cache sym_cache;
810
811 bfd_vma res_n_size;
812 };
813
814struct nios2_elf32_obj_tdata
815{
816 struct elf_obj_tdata root;
817
818 /* tls_type for each local got entry. */
819 char *local_got_tls_type;
820
821 /* TRUE if TLS GD relocs have been seen for this object. */
822 bfd_boolean has_tlsgd;
823};
824
825#define elf32_nios2_tdata(abfd) \
826 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
827
828#define elf32_nios2_local_got_tls_type(abfd) \
829 (elf32_nios2_tdata (abfd)->local_got_tls_type)
830
831/* The name of the dynamic interpreter. This is put in the .interp
832 section. */
833#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
834
835/* PLT implementation for position-dependent code. */
836static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
837 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
838 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
839 0x7800683a /* jmp r15 */
840};
841
842static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
843 0x03800034, /* movhi r14, %hiadj(res_0) */
844 0x73800004, /* addi r14, r14, %lo(res_0) */
845 0x7b9fc83a, /* sub r15, r15, r14 */
846 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
847 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
848 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
849 0x6800683a /* jmp r13 */
850};
851
852/* PLT implementation for position-independent code. */
853static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
854 0x03c00034, /* movhi r15, %hiadj(index * 4) */
855 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
856 0x00000006 /* br .PLTresolve */
857};
858
859static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
860 0x001ce03a, /* nextpc r14 */
861 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
862 0x6b9b883a, /* add r13, r13, r14 */
863 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
864 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
865 0x6800683a /* jmp r13 */
866};
867
868/* Implement elf_backend_grok_prstatus:
869 Support for core dump NOTE sections. */
870static bfd_boolean
871nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
872{
873 int offset;
874 size_t size;
875
876 switch (note->descsz)
877 {
878 default:
879 return FALSE;
880
881 case 212: /* Linux/Nios II */
882 /* pr_cursig */
228e534f 883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
36591ba1
SL
884
885 /* pr_pid */
228e534f 886 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
36591ba1
SL
887
888 /* pr_reg */
889 offset = 72;
890 size = 136;
891
892 break;
893 }
894
895 /* Make a ".reg/999" section. */
896 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
897 size, note->descpos + offset);
898}
899
900/* Implement elf_backend_grok_psinfo. */
901static bfd_boolean
902nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
903{
904 switch (note->descsz)
905 {
906 default:
907 return FALSE;
908
909 case 124: /* Linux/Nios II elf_prpsinfo */
228e534f 910 elf_tdata (abfd)->core->program
36591ba1 911 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 912 elf_tdata (abfd)->core->command
36591ba1
SL
913 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
914 }
915
916 /* Note that for some reason, a spurious space is tacked
917 onto the end of the args in some (at least one anyway)
918 implementations, so strip it off if it exists. */
919
920 {
228e534f 921 char *command = elf_tdata (abfd)->core->command;
36591ba1
SL
922 int n = strlen (command);
923
924 if (0 < n && command[n - 1] == ' ')
925 command[n - 1] = '\0';
926 }
927
928 return TRUE;
929}
930
931/* Create an entry in a Nios II ELF linker hash table. */
932static struct bfd_hash_entry *
933link_hash_newfunc (struct bfd_hash_entry *entry,
934 struct bfd_hash_table *table, const char *string)
935{
936 /* Allocate the structure if it has not already been allocated by a
937 subclass. */
938 if (entry == NULL)
939 {
940 entry = bfd_hash_allocate (table,
941 sizeof (struct elf32_nios2_link_hash_entry));
942 if (entry == NULL)
943 return entry;
944 }
945
946 /* Call the allocation method of the superclass. */
947 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
948 if (entry)
949 {
950 struct elf32_nios2_link_hash_entry *eh;
951
952 eh = (struct elf32_nios2_link_hash_entry *) entry;
953 eh->dyn_relocs = NULL;
954 eh->tls_type = GOT_UNKNOWN;
955 eh->got_types_used = 0;
956 }
957
958 return entry;
959}
960
961/* Implement bfd_elf32_bfd_reloc_type_lookup:
962 Given a BFD reloc type, return a howto structure. */
963static reloc_howto_type *
964nios2_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
965 bfd_reloc_code_real_type code)
966{
967 int i;
968 for (i = 0;
969 i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
970 ++i)
971 if (nios2_reloc_map[i].bfd_val == code)
972 return &elf_nios2_howto_table_rel[(int) nios2_reloc_map[i].elf_val];
973 return NULL;
974}
975
976/* Implement bfd_elf32_bfd_reloc_name_lookup:
977 Given a reloc name, return a howto structure. */
978static reloc_howto_type *
979nios2_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
980 const char *r_name)
981{
982 unsigned int i;
983 for (i = 0;
984 i < (sizeof (elf_nios2_howto_table_rel)
985 / sizeof (elf_nios2_howto_table_rel[0]));
986 i++)
987 if (elf_nios2_howto_table_rel[i].name
988 && strcasecmp (elf_nios2_howto_table_rel[i].name, r_name) == 0)
989 return &elf_nios2_howto_table_rel[i];
990
991 return NULL;
992}
993
994/* Implement elf_info_to_howto:
995 Given a ELF32 relocation, fill in a arelent structure. */
996static void
997nios2_elf32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
998 Elf_Internal_Rela *dst)
999{
1000 unsigned int r_type;
1001
1002 r_type = ELF32_R_TYPE (dst->r_info);
1003 BFD_ASSERT (r_type < R_NIOS2_ILLEGAL);
1004 cache_ptr->howto = &elf_nios2_howto_table_rel[r_type];
1005}
1006
1007/* Return the base VMA address which should be subtracted from real addresses
1008 when resolving @dtpoff relocation.
1009 This is PT_TLS segment p_vaddr. */
1010static bfd_vma
1011dtpoff_base (struct bfd_link_info *info)
1012{
1013 /* If tls_sec is NULL, we should have signalled an error already. */
1014 if (elf_hash_table (info)->tls_sec == NULL)
1015 return 0;
1016 return elf_hash_table (info)->tls_sec->vma;
1017}
1018
1019/* Return the relocation value for @tpoff relocation
1020 if STT_TLS virtual address is ADDRESS. */
1021static bfd_vma
1022tpoff (struct bfd_link_info *info, bfd_vma address)
1023{
1024 struct elf_link_hash_table *htab = elf_hash_table (info);
1025
1026 /* If tls_sec is NULL, we should have signalled an error already. */
1027 if (htab->tls_sec == NULL)
1028 return 0;
1029 return address - htab->tls_sec->vma;
1030}
1031
1032/* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
1033 dangerous relocation. */
1034static bfd_boolean
1035nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
1036{
1037
1038 bfd_boolean gp_found;
1039 struct bfd_hash_entry *h;
1040 struct bfd_link_hash_entry *lh;
1041
1042 /* If we've already figured out what GP will be, just return it. */
1043 *pgp = _bfd_get_gp_value (output_bfd);
1044 if (*pgp)
1045 return TRUE;
1046
1047 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
1048 lh = (struct bfd_link_hash_entry *) h;
1049lookup:
1050 if (lh)
1051 {
1052 switch (lh->type)
1053 {
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 case bfd_link_hash_common:
1057 gp_found = FALSE;
1058 break;
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1061 gp_found = TRUE;
1062 *pgp = lh->u.def.value;
1063 break;
1064 case bfd_link_hash_indirect:
1065 case bfd_link_hash_warning:
1066 lh = lh->u.i.link;
1067 /* @@FIXME ignoring warning for now */
1068 goto lookup;
1069 case bfd_link_hash_new:
1070 default:
1071 abort ();
1072 }
1073 }
1074 else
1075 gp_found = FALSE;
1076
1077 if (!gp_found)
1078 {
1079 /* Only get the error once. */
1080 *pgp = 4;
1081 _bfd_set_gp_value (output_bfd, *pgp);
1082 return FALSE;
1083 }
1084
1085 _bfd_set_gp_value (output_bfd, *pgp);
1086
1087 return TRUE;
1088}
1089
1090/* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
1091 if it's not available as we don't have a link_info pointer available here
1092 to look it up in the output symbol table. We don't need to adjust the
1093 symbol value for an external symbol if we are producing relocatable
1094 output. */
1095static bfd_reloc_status_type
1096nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
1097 char **error_message, bfd_vma *pgp)
1098{
1099 if (bfd_is_und_section (symbol->section) && !relocatable)
1100 {
1101 *pgp = 0;
1102 return bfd_reloc_undefined;
1103 }
1104
1105 *pgp = _bfd_get_gp_value (output_bfd);
1106 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
1107 {
1108 if (relocatable)
1109 {
1110 /* Make up a value. */
1111 *pgp = symbol->section->output_section->vma + 0x4000;
1112 _bfd_set_gp_value (output_bfd, *pgp);
1113 }
1114 else
1115 {
1116 *error_message
1117 = (char *) _("global pointer relative relocation when _gp not defined");
1118 return bfd_reloc_dangerous;
1119 }
1120 }
1121
1122 return bfd_reloc_ok;
1123}
1124
1125/* The usual way of loading a 32-bit constant into a Nios II register is to
1126 load the high 16 bits in one instruction and then add the low 16 bits with
1127 a signed add. This means that the high halfword needs to be adjusted to
1128 compensate for the sign bit of the low halfword. This function returns the
1129 adjusted high halfword for a given 32-bit constant. */
1130static
1131bfd_vma hiadj (bfd_vma symbol_value)
1132{
1133 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1134}
1135
1136/* Do the relocations that require special handling. */
1137static bfd_reloc_status_type
1138nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1139 asection *input_section,
36591ba1
SL
1140 bfd_byte *data, bfd_vma offset,
1141 bfd_vma symbol_value, bfd_vma addend)
1142{
1143 symbol_value = symbol_value + addend;
1144 addend = 0;
1145 symbol_value = (symbol_value >> 16) & 0xffff;
1146 return _bfd_final_link_relocate (howto, abfd, input_section,
1147 data, offset, symbol_value, addend);
1148}
1149
1150static bfd_reloc_status_type
1151nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1152 asection *input_section,
36591ba1
SL
1153 bfd_byte *data, bfd_vma offset,
1154 bfd_vma symbol_value, bfd_vma addend)
1155{
1156 symbol_value = symbol_value + addend;
1157 addend = 0;
1158 symbol_value = symbol_value & 0xffff;
1159 return _bfd_final_link_relocate (howto, abfd, input_section,
1160 data, offset, symbol_value, addend);
1161}
1162
1163static bfd_reloc_status_type
1164nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1165 asection *input_section,
36591ba1
SL
1166 bfd_byte *data, bfd_vma offset,
1167 bfd_vma symbol_value, bfd_vma addend)
1168{
1169 symbol_value = symbol_value + addend;
1170 addend = 0;
1171 symbol_value = hiadj(symbol_value);
1172 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1173 symbol_value, addend);
1174}
1175
1176static bfd_reloc_status_type
1177nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1178 asection *input_section,
36591ba1
SL
1179 bfd_byte *data, bfd_vma offset,
1180 bfd_vma symbol_value, bfd_vma addend)
1181{
1182 symbol_value = symbol_value + addend;
1183 addend = 0;
1184 symbol_value = symbol_value & 0xffff;
1185 return _bfd_final_link_relocate (howto, abfd, input_section,
1186 data, offset, symbol_value, addend);
1187}
1188
1189static bfd_reloc_status_type
1190nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1191 asection *input_section,
36591ba1
SL
1192 bfd_byte *data, bfd_vma offset,
1193 bfd_vma symbol_value, bfd_vma addend)
1194{
1195 symbol_value = symbol_value + addend;
1196 symbol_value -= (input_section->output_section->vma
1197 + input_section->output_offset);
1198 symbol_value -= offset;
1199 addend = 0;
1200 symbol_value = hiadj(symbol_value);
1201 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1202 symbol_value, addend);
1203}
1204
1205static bfd_reloc_status_type
1206nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1207 asection *input_section,
36591ba1
SL
1208 bfd_byte *data, bfd_vma offset,
1209 bfd_vma symbol_value, bfd_vma addend)
1210{
1211 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
1212 so we need to subtract 4 before doing a final_link_relocate. */
1213 symbol_value = symbol_value + addend - 4;
1214 addend = 0;
1215 return _bfd_final_link_relocate (howto, abfd, input_section,
1216 data, offset, symbol_value, addend);
1217}
1218
1219static bfd_reloc_status_type
1220nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1221 asection *input_section,
36591ba1
SL
1222 bfd_byte *data, bfd_vma offset,
1223 bfd_vma symbol_value, bfd_vma addend)
1224{
1225 /* Check that the relocation is in the same page as the current address. */
1226 if (((symbol_value + addend) & 0xf0000000)
1227 != ((input_section->output_section->vma + offset) & 0xf0000000))
1228 return bfd_reloc_overflow;
1229
1230 return _bfd_final_link_relocate (howto, abfd, input_section,
1231 data, offset, symbol_value, addend);
1232}
1233
1234static bfd_reloc_status_type
1235nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1236 asection *input_section,
36591ba1
SL
1237 bfd_byte *data, bfd_vma offset,
1238 bfd_vma symbol_value, bfd_vma addend)
1239{
1240 /* Because we need the output_bfd, the special handling is done
1241 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
1242 return _bfd_final_link_relocate (howto, abfd, input_section,
1243 data, offset, symbol_value, addend);
1244}
1245
1246static bfd_reloc_status_type
1247nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1248 asection *input_section,
36591ba1
SL
1249 bfd_byte *data, bfd_vma offset,
1250 bfd_vma symbol_value, bfd_vma addend)
1251{
1252 bfd_vma symbol_lo16, symbol_hi16;
1253 bfd_reloc_status_type r;
1254 symbol_value = symbol_value + addend;
1255 addend = 0;
1256 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1257 symbol_lo16 = symbol_value & 0xffff;
1258
1259 r = _bfd_final_link_relocate (howto, abfd, input_section,
1260 data, offset, symbol_hi16, addend);
1261
1262 if (r == bfd_reloc_ok)
1263 return _bfd_final_link_relocate (howto, abfd, input_section,
1264 data, offset + 4, symbol_lo16, addend);
1265
1266 return r;
1267}
1268
1269static bfd_reloc_status_type
1270nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1271 asection *input_section,
36591ba1
SL
1272 bfd_byte *data, bfd_vma offset,
1273 bfd_vma symbol_value, bfd_vma addend)
1274{
1275 bfd_vma symbol_lo16, symbol_hi16;
1276 bfd_reloc_status_type r;
1277 symbol_value = symbol_value + addend;
1278 addend = 0;
1279 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1280 symbol_lo16 = symbol_value & 0xffff;
1281
1282 r = _bfd_final_link_relocate (howto, abfd, input_section,
1283 data, offset, symbol_hi16, addend);
1284
1285 if (r == bfd_reloc_ok)
1286 return _bfd_final_link_relocate (howto, abfd, input_section,
1287 data, offset + 4, symbol_lo16, addend);
1288
1289 return r;
1290}
1291
1292static bfd_reloc_status_type
1293nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 1294 asection *input_section,
36591ba1
SL
1295 bfd_byte *data, bfd_vma offset,
1296 bfd_vma symbol_value, bfd_vma addend)
1297{
1298 bfd_vma symbol_lo16, symbol_hi16;
1299 bfd_reloc_status_type r;
1300 symbol_value = symbol_value + addend;
1301 addend = 0;
1302 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1303 symbol_lo16 = symbol_value & 0xffff;
1304
1305 r = _bfd_final_link_relocate (howto, abfd, input_section,
1306 data, offset, symbol_hi16, addend);
1307
1308 if (r == bfd_reloc_ok)
1309 return _bfd_final_link_relocate (howto, abfd, input_section,
1310 data, offset + 4, symbol_lo16, addend);
1311
1312 return r;
1313}
1314
1315/* HOWTO handlers for relocations that require special handling. */
1316
1317/* This is for relocations used only when relaxing to ensure
1318 changes in size of section don't screw up .align. */
1319static bfd_reloc_status_type
1320nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1321 asymbol *symbol ATTRIBUTE_UNUSED,
1322 void *data ATTRIBUTE_UNUSED, asection *input_section,
1323 bfd *output_bfd,
1324 char **error_message ATTRIBUTE_UNUSED)
1325{
1326 if (output_bfd != NULL)
1327 reloc_entry->address += input_section->output_offset;
1328 return bfd_reloc_ok;
1329}
1330
1331static bfd_reloc_status_type
1332nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1333 void *data, asection *input_section,
1334 bfd *output_bfd,
1335 char **error_message ATTRIBUTE_UNUSED)
1336{
1337 /* This part is from bfd_elf_generic_reloc. */
1338 if (output_bfd != NULL
1339 && (symbol->flags & BSF_SECTION_SYM) == 0
1340 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1341 {
1342 reloc_entry->address += input_section->output_offset;
1343 return bfd_reloc_ok;
1344 }
1345
1346 if (output_bfd != NULL)
1347 /* FIXME: See bfd_perform_relocation. Is this right? */
1348 return bfd_reloc_continue;
1349
1350 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
1351 input_section,
1352 data, reloc_entry->address,
1353 (symbol->value
1354 + symbol->section->output_section->vma
1355 + symbol->section->output_offset),
1356 reloc_entry->addend);
1357}
1358
1359static bfd_reloc_status_type
1360nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1361 void *data, asection *input_section,
1362 bfd *output_bfd,
1363 char **error_message ATTRIBUTE_UNUSED)
1364{
1365 /* This part is from bfd_elf_generic_reloc. */
1366 if (output_bfd != NULL
1367 && (symbol->flags & BSF_SECTION_SYM) == 0
1368 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1369 {
1370 reloc_entry->address += input_section->output_offset;
1371 return bfd_reloc_ok;
1372 }
1373
1374 if (output_bfd != NULL)
1375 /* FIXME: See bfd_perform_relocation. Is this right? */
1376 return bfd_reloc_continue;
1377
1378 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
1379 input_section,
1380 data, reloc_entry->address,
1381 (symbol->value
1382 + symbol->section->output_section->vma
1383 + symbol->section->output_offset),
1384 reloc_entry->addend);
1385}
1386
1387static bfd_reloc_status_type
1388nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1389 void *data, asection *input_section,
1390 bfd *output_bfd,
1391 char **error_message ATTRIBUTE_UNUSED)
1392{
1393 /* This part is from bfd_elf_generic_reloc. */
1394 if (output_bfd != NULL
1395 && (symbol->flags & BSF_SECTION_SYM) == 0
1396 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1397 {
1398 reloc_entry->address += input_section->output_offset;
1399 return bfd_reloc_ok;
1400 }
1401
1402 if (output_bfd != NULL)
1403 /* FIXME: See bfd_perform_relocation. Is this right? */
1404 return bfd_reloc_continue;
1405
1406 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
1407 input_section,
1408 data, reloc_entry->address,
1409 (symbol->value
1410 + symbol->section->output_section->vma
1411 + symbol->section->output_offset),
1412 reloc_entry->addend);
1413}
1414
1415static bfd_reloc_status_type
1416nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
1417 asymbol *symbol, void *data,
1418 asection *input_section, bfd *output_bfd,
1419 char **error_message ATTRIBUTE_UNUSED)
1420{
1421 /* This part is from bfd_elf_generic_reloc. */
1422 if (output_bfd != NULL
1423 && (symbol->flags & BSF_SECTION_SYM) == 0
1424 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1425 {
1426 reloc_entry->address += input_section->output_offset;
1427 return bfd_reloc_ok;
1428 }
1429
1430 if (output_bfd != NULL)
1431 /* FIXME: See bfd_perform_relocation. Is this right? */
1432 return bfd_reloc_continue;
1433
1434 return nios2_elf32_do_pcrel_lo16_relocate (
1435 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1436 (symbol->value + symbol->section->output_section->vma
1437 + symbol->section->output_offset),
1438 reloc_entry->addend);
1439}
1440
1441static bfd_reloc_status_type
1442nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
1443 asymbol *symbol, void *data,
1444 asection *input_section, bfd *output_bfd,
1445 char **error_message ATTRIBUTE_UNUSED)
1446{
1447 /* This part is from bfd_elf_generic_reloc. */
1448 if (output_bfd != NULL
1449 && (symbol->flags & BSF_SECTION_SYM) == 0
1450 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1451 {
1452 reloc_entry->address += input_section->output_offset;
1453 return bfd_reloc_ok;
1454 }
1455
1456 if (output_bfd != NULL)
1457 /* FIXME: See bfd_perform_relocation. Is this right? */
1458 return bfd_reloc_continue;
1459
1460 return nios2_elf32_do_pcrel_hiadj16_relocate (
1461 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1462 (symbol->value + symbol->section->output_section->vma
1463 + symbol->section->output_offset),
1464 reloc_entry->addend);
1465}
1466
1467static bfd_reloc_status_type
1468nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1469 void *data, asection *input_section,
1470 bfd *output_bfd,
1471 char **error_message ATTRIBUTE_UNUSED)
1472{
1473 /* This part is from bfd_elf_generic_reloc. */
1474 if (output_bfd != NULL
1475 && (symbol->flags & BSF_SECTION_SYM) == 0
1476 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1477 {
1478 reloc_entry->address += input_section->output_offset;
1479 return bfd_reloc_ok;
1480 }
1481
1482 if (output_bfd != NULL)
1483 /* FIXME: See bfd_perform_relocation. Is this right? */
1484 return bfd_reloc_continue;
1485
1486 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
1487 input_section,
1488 data, reloc_entry->address,
1489 (symbol->value
1490 + symbol->section->output_section->vma
1491 + symbol->section->output_offset),
1492 reloc_entry->addend);
1493}
1494
1495static bfd_reloc_status_type
1496nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1497 void *data, asection *input_section,
1498 bfd *output_bfd,
1499 char **error_message ATTRIBUTE_UNUSED)
1500{
1501 /* This part is from bfd_elf_generic_reloc. */
1502 if (output_bfd != NULL
1503 && (symbol->flags & BSF_SECTION_SYM) == 0
1504 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1505 {
1506 reloc_entry->address += input_section->output_offset;
1507 return bfd_reloc_ok;
1508 }
1509
1510 if (output_bfd != NULL)
1511 /* FIXME: See bfd_perform_relocation. Is this right? */
1512 return bfd_reloc_continue;
1513
1514 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
1515 input_section,
1516 data, reloc_entry->address,
1517 (symbol->value
1518 + symbol->section->output_section->vma
1519 + symbol->section->output_offset),
1520 reloc_entry->addend);
1521}
1522
1523static bfd_reloc_status_type
1524nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1525 void *data, asection *input_section,
1526 bfd *output_bfd, char **msg)
1527{
1528 bfd_vma relocation;
1529 bfd_vma gp;
1530 bfd_reloc_status_type r;
1531
1532
1533 /* This part is from bfd_elf_generic_reloc. */
1534 if (output_bfd != NULL
1535 && (symbol->flags & BSF_SECTION_SYM) == 0
1536 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1537 {
1538 reloc_entry->address += input_section->output_offset;
1539 return bfd_reloc_ok;
1540 }
1541
1542 if (output_bfd != NULL)
1543 /* FIXME: See bfd_perform_relocation. Is this right? */
1544 return bfd_reloc_continue;
1545
1546 relocation = (symbol->value
1547 + symbol->section->output_section->vma
1548 + symbol->section->output_offset);
1549
1550 /* This assumes we've already cached the _gp symbol. */
1551 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
1552 if (r == bfd_reloc_ok)
1553 {
1554 relocation = relocation + reloc_entry->addend - gp;
1555 reloc_entry->addend = 0;
1556 if ((signed) relocation < -32768 || (signed) relocation > 32767)
1557 {
1558 *msg = _("global pointer relative address out of range");
1559 r = bfd_reloc_outofrange;
1560 }
1561 else
1562 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
1563 input_section,
1564 data, reloc_entry->address,
1565 relocation, reloc_entry->addend);
1566 }
1567
1568 return r;
1569}
1570
1571static bfd_reloc_status_type
1572nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1575{
1576 /* This part is from bfd_elf_generic_reloc. */
1577 if (output_bfd != NULL
1578 && (symbol->flags & BSF_SECTION_SYM) == 0
1579 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1580 {
1581 reloc_entry->address += input_section->output_offset;
1582 return bfd_reloc_ok;
1583 }
1584
1585 if (output_bfd != NULL)
1586 /* FIXME: See bfd_perform_relocation. Is this right? */
1587 return bfd_reloc_continue;
1588
1589 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
1590 input_section,
1591 data, reloc_entry->address,
1592 (symbol->value
1593 + symbol->section->output_section->vma
1594 + symbol->section->output_offset),
1595 reloc_entry->addend);
1596}
1597
1598static bfd_reloc_status_type
1599nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1600 void *data, asection *input_section,
1601 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1602{
1603 /* This part is from bfd_elf_generic_reloc. */
1604 if (output_bfd != NULL
1605 && (symbol->flags & BSF_SECTION_SYM) == 0
1606 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1607 {
1608 reloc_entry->address += input_section->output_offset;
1609 return bfd_reloc_ok;
1610 }
1611
1612 if (output_bfd != NULL)
1613 /* FIXME: See bfd_perform_relocation. Is this right? */
1614 return bfd_reloc_continue;
1615
1616 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
1617 input_section,
1618 data, reloc_entry->address,
1619 (symbol->value
1620 + symbol->section->output_section->vma
1621 + symbol->section->output_offset),
1622 reloc_entry->addend);
1623}
1624
1625static bfd_reloc_status_type
1626nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1627 void *data, asection *input_section,
1628 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1629{
1630 /* This part is from bfd_elf_generic_reloc. */
1631 if (output_bfd != NULL
1632 && (symbol->flags & BSF_SECTION_SYM) == 0
1633 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1634 {
1635 reloc_entry->address += input_section->output_offset;
1636 return bfd_reloc_ok;
1637 }
1638
1639 if (output_bfd != NULL)
1640 /* FIXME: See bfd_perform_relocation. Is this right? */
1641 return bfd_reloc_continue;
1642
1643 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
1644 input_section,
1645 data, reloc_entry->address,
1646 (symbol->value
1647 + symbol->section->output_section->vma
1648 + symbol->section->output_offset),
1649 reloc_entry->addend);
1650}
1651
1652
1653/* Implement elf_backend_relocate_section. */
1654static bfd_boolean
1655nios2_elf32_relocate_section (bfd *output_bfd,
1656 struct bfd_link_info *info,
1657 bfd *input_bfd,
1658 asection *input_section,
1659 bfd_byte *contents,
1660 Elf_Internal_Rela *relocs,
1661 Elf_Internal_Sym *local_syms,
1662 asection **local_sections)
1663{
1664 Elf_Internal_Shdr *symtab_hdr;
1665 struct elf_link_hash_entry **sym_hashes;
1666 Elf_Internal_Rela *rel;
1667 Elf_Internal_Rela *relend;
1668 struct elf32_nios2_link_hash_table *htab;
1669 asection *sgot;
1670 asection *splt;
1671 asection *sreloc = NULL;
1672 bfd_vma *local_got_offsets;
1673
1674 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1675 sym_hashes = elf_sym_hashes (input_bfd);
1676 relend = relocs + input_section->reloc_count;
1677
1678 htab = elf32_nios2_hash_table (info);
1679 sgot = htab->root.sgot;
1680 splt = htab->root.splt;
1681 local_got_offsets = elf_local_got_offsets (input_bfd);
1682
1683 for (rel = relocs; rel < relend; rel++)
1684 {
1685 reloc_howto_type *howto;
1686 unsigned long r_symndx;
1687 Elf_Internal_Sym *sym;
1688 asection *sec;
1689 struct elf_link_hash_entry *h;
1690 struct elf32_nios2_link_hash_entry *eh;
1691 bfd_vma relocation;
1692 bfd_vma gp;
1693 bfd_vma reloc_address;
1694 bfd_reloc_status_type r = bfd_reloc_ok;
1695 const char *name = NULL;
1696 int r_type;
1697 const char *format;
1698 char msgbuf[256];
1699 const char* msg = (const char*) NULL;
1700 bfd_boolean unresolved_reloc;
1701 bfd_vma off;
1702 int use_plt;
1703
1704 r_type = ELF32_R_TYPE (rel->r_info);
1705 r_symndx = ELF32_R_SYM (rel->r_info);
1706
1707 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
1708 h = NULL;
1709 sym = NULL;
1710 sec = NULL;
1711
1712 if (r_symndx < symtab_hdr->sh_info)
1713 {
1714 sym = local_syms + r_symndx;
1715 sec = local_sections[r_symndx];
1716 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1717 }
1718 else
1719 {
62d887d4 1720 bfd_boolean warned, ignored;
36591ba1
SL
1721
1722 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1723 r_symndx, symtab_hdr, sym_hashes,
1724 h, sec, relocation,
62d887d4 1725 unresolved_reloc, warned, ignored);
36591ba1
SL
1726 }
1727
1728 if (sec && discarded_section (sec))
1729 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1730 rel, 1, relend, howto, 0, contents);
1731
1732 /* Nothing more to do unless this is a final link. */
1733 if (info->relocatable)
1734 continue;
1735
1736 if (sec && sec->output_section)
1737 reloc_address = (sec->output_section->vma + sec->output_offset
1738 + rel->r_offset);
1739 else
1740 reloc_address = 0;
1741
1742 if (howto)
1743 {
1744 switch (howto->type)
1745 {
1746 case R_NIOS2_HI16:
1747 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
1748 input_section,
1749 contents, rel->r_offset,
1750 relocation, rel->r_addend);
1751 break;
1752 case R_NIOS2_LO16:
1753 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
1754 input_section,
1755 contents, rel->r_offset,
1756 relocation, rel->r_addend);
1757 break;
1758 case R_NIOS2_PCREL_LO:
1759 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
1760 input_section,
1761 contents,
1762 rel->r_offset,
1763 relocation,
1764 rel->r_addend);
1765 break;
1766 case R_NIOS2_HIADJ16:
1767 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
1768 input_section, contents,
1769 rel->r_offset, relocation,
1770 rel->r_addend);
1771 break;
1772 case R_NIOS2_PCREL_HA:
1773 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
1774 input_section,
1775 contents,
1776 rel->r_offset,
1777 relocation,
1778 rel->r_addend);
1779 break;
1780 case R_NIOS2_PCREL16:
1781 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
1782 input_section, contents,
1783 rel->r_offset, relocation,
1784 rel->r_addend);
1785 break;
1786 case R_NIOS2_GPREL:
1787 /* Turns an absolute address into a gp-relative address. */
1788 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
1789 {
1790 format = _("global pointer relative relocation at address "
1791 "0x%08x when _gp not defined\n");
1792 sprintf (msgbuf, format, reloc_address);
1793 msg = msgbuf;
1794 r = bfd_reloc_dangerous;
1795 }
1796 else
1797 {
1798 bfd_vma symbol_address = rel->r_addend + relocation;
1799 relocation = relocation + rel->r_addend - gp;
1800 rel->r_addend = 0;
1801 if (((signed) relocation < -32768
1802 || (signed) relocation > 32767)
1803 && (!h
1804 || h->root.type == bfd_link_hash_defined
1805 || h->root.type == bfd_link_hash_defweak))
1806 {
1807 format = _("Unable to reach %s (at 0x%08x) from the "
1808 "global pointer (at 0x%08x) because the "
1809 "offset (%d) is out of the allowed range, "
1810 "-32678 to 32767.\n" );
1811 sprintf (msgbuf, format, name, symbol_address, gp,
1812 (signed)relocation);
1813 msg = msgbuf;
1814 r = bfd_reloc_outofrange;
1815 }
1816 else
1817 r = _bfd_final_link_relocate (howto, input_bfd,
1818 input_section, contents,
1819 rel->r_offset, relocation,
1820 rel->r_addend);
1821 }
1822
1823 break;
1824 case R_NIOS2_UJMP:
1825 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
1826 input_section,
1827 contents, rel->r_offset,
1828 relocation, rel->r_addend);
1829 break;
1830 case R_NIOS2_CJMP:
1831 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
1832 input_section,
1833 contents, rel->r_offset,
1834 relocation, rel->r_addend);
1835 break;
1836 case R_NIOS2_CALLR:
1837 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
1838 input_section, contents,
1839 rel->r_offset, relocation,
1840 rel->r_addend);
1841 break;
1842 case R_NIOS2_CALL26:
1843 /* If we have a call to an undefined weak symbol, we just want
1844 to stuff a zero in the bits of the call instruction and
1845 bypass the normal call26 relocation handling, because it'll
1846 diagnose an overflow error if address 0 isn't in the same
1847 256MB segment as the call site. Presumably the call
1848 should be guarded by a null check anyway. */
1849 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
1850 {
1851 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
1852 r = _bfd_final_link_relocate (howto, input_bfd,
1853 input_section, contents,
1854 rel->r_offset, relocation,
1855 rel->r_addend);
1856 break;
1857 }
1858 /* Handle relocations which should use the PLT entry.
1859 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
1860 which may point to a PLT entry, but we don't need to handle
1861 that here. If we created a PLT entry, all branches in this
1862 object should go to it. */
1863 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
1864 {
1865 /* If we've created a .plt section, and assigned a PLT entry
1866 to this function, it should not be known to bind locally.
1867 If it were, we would have cleared the PLT entry. */
1868 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1869
1870 relocation = (splt->output_section->vma
1871 + splt->output_offset
1872 + h->plt.offset);
1873
1874 unresolved_reloc = FALSE;
1875 }
1876 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
1877 input_section, contents,
1878 rel->r_offset, relocation,
1879 rel->r_addend);
1880 break;
1881 case R_NIOS2_ALIGN:
1882 r = bfd_reloc_ok;
1883 /* For symmetry this would be
1884 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
1885 input_section, contents,
1886 rel->r_offset, relocation,
1887 rel->r_addend);
1888 but do_ignore_reloc would do no more than return
1889 bfd_reloc_ok. */
1890 break;
1891
1892 case R_NIOS2_GOT16:
1893 case R_NIOS2_CALL16:
1894 /* Relocation is to the entry for this symbol in the
1895 global offset table. */
1896 if (sgot == NULL)
1897 {
1898 r = bfd_reloc_notsupported;
1899 break;
1900 }
1901
1902 use_plt = 0;
1903
1904 if (h != NULL)
1905 {
1906 bfd_boolean dyn;
1907
1908 eh = (struct elf32_nios2_link_hash_entry *)h;
1909 use_plt = (eh->got_types_used == CALL16_USED
1910 && h->plt.offset != (bfd_vma) -1);
1911
1912 off = h->got.offset;
1913 BFD_ASSERT (off != (bfd_vma) -1);
1914 dyn = elf_hash_table (info)->dynamic_sections_created;
1915 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1916 || (info->shared
1917 && SYMBOL_REFERENCES_LOCAL (info, h))
1918 || (ELF_ST_VISIBILITY (h->other)
1919 && h->root.type == bfd_link_hash_undefweak))
1920 {
1921 /* This is actually a static link, or it is a -Bsymbolic
1922 link and the symbol is defined locally. We must
1923 initialize this entry in the global offset table.
1924 Since the offset must always be a multiple of 4, we
1925 use the least significant bit to record whether we
1926 have initialized it already.
1927
1928 When doing a dynamic link, we create a .rela.got
1929 relocation entry to initialize the value. This is
1930 done in the finish_dynamic_symbol routine. */
1931 if ((off & 1) != 0)
1932 off &= ~1;
1933 else
1934 {
1935 bfd_put_32 (output_bfd, relocation,
1936 sgot->contents + off);
1937 h->got.offset |= 1;
1938 }
1939 }
1940 else
1941 unresolved_reloc = FALSE;
1942 }
1943 else
1944 {
1945 BFD_ASSERT (local_got_offsets != NULL
1946 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1947
1948 off = local_got_offsets[r_symndx];
1949
1950 /* The offset must always be a multiple of 4. We use the
1951 least significant bit to record whether we have already
1952 generated the necessary reloc. */
1953 if ((off & 1) != 0)
1954 off &= ~1;
1955 else
1956 {
1957 bfd_put_32 (output_bfd, relocation,
1958 sgot->contents + off);
1959
1960 if (info->shared)
1961 {
1962 asection *srelgot;
1963 Elf_Internal_Rela outrel;
1964 bfd_byte *loc;
1965
1966 srelgot = htab->root.srelgot;
1967 BFD_ASSERT (srelgot != NULL);
1968
1969 outrel.r_addend = relocation;
1970 outrel.r_offset = (sgot->output_section->vma
1971 + sgot->output_offset
1972 + off);
1973 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
1974 loc = srelgot->contents;
1975 loc += (srelgot->reloc_count++ *
1976 sizeof (Elf32_External_Rela));
1977 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1978 }
1979
1980 local_got_offsets[r_symndx] |= 1;
1981 }
1982 }
1983
1984 if (use_plt && info->shared)
1985 {
1986 off = ((h->plt.offset - 24) / 12 + 3) * 4;
1987 relocation = htab->root.sgotplt->output_offset + off;
1988 }
1989 else
1990 relocation = sgot->output_offset + off;
1991
1992 /* This relocation does not use the addend. */
1993 rel->r_addend = 0;
1994
1995 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1996 contents, rel->r_offset,
1997 relocation, rel->r_addend);
1998 break;
1999
2000 case R_NIOS2_GOTOFF_LO:
2001 case R_NIOS2_GOTOFF_HA:
2002 case R_NIOS2_GOTOFF:
2003 /* Relocation is relative to the start of the
2004 global offset table. */
2005
2006 BFD_ASSERT (sgot != NULL);
2007 if (sgot == NULL)
2008 {
2009 r = bfd_reloc_notsupported;
2010 break;
2011 }
2012
2013 /* Note that sgot->output_offset is not involved in this
2014 calculation. We always want the start of .got. If we
2015 define _GLOBAL_OFFSET_TABLE in a different way, as is
2016 permitted by the ABI, we might have to change this
2017 calculation. */
2018 relocation -= sgot->output_section->vma;
2019 switch (howto->type)
2020 {
2021 case R_NIOS2_GOTOFF_LO:
2022 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
2023 input_section, contents,
2024 rel->r_offset, relocation,
2025 rel->r_addend);
2026 break;
2027 case R_NIOS2_GOTOFF_HA:
2028 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
2029 input_section, contents,
2030 rel->r_offset,
2031 relocation,
2032 rel->r_addend);
2033 break;
2034 default:
2035 r = _bfd_final_link_relocate (howto, input_bfd,
2036 input_section, contents,
2037 rel->r_offset, relocation,
2038 rel->r_addend);
2039 break;
2040 }
2041 break;
2042
2043 case R_NIOS2_TLS_LDO16:
2044 relocation -= dtpoff_base (info) + DTP_OFFSET;
2045
2046 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2047 contents, rel->r_offset,
2048 relocation, rel->r_addend);
2049 break;
2050 case R_NIOS2_TLS_LDM16:
2051 if (htab->root.sgot == NULL)
2052 abort ();
2053
2054 off = htab->tls_ldm_got.offset;
2055
2056 if ((off & 1) != 0)
2057 off &= ~1;
2058 else
2059 {
2060 /* If we don't know the module number, create a relocation
2061 for it. */
2062 if (info->shared)
2063 {
2064 Elf_Internal_Rela outrel;
2065 bfd_byte *loc;
2066
2067 if (htab->root.srelgot == NULL)
2068 abort ();
2069
2070 outrel.r_addend = 0;
2071 outrel.r_offset = (htab->root.sgot->output_section->vma
2072 + htab->root.sgot->output_offset
2073 + off);
2074 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
2075
2076 loc = htab->root.srelgot->contents;
2077 loc += (htab->root.srelgot->reloc_count++
2078 * sizeof (Elf32_External_Rela));
2079 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2080 }
2081 else
2082 bfd_put_32 (output_bfd, 1,
2083 htab->root.sgot->contents + off);
2084
2085 htab->tls_ldm_got.offset |= 1;
2086 }
2087
2088 relocation = (htab->root.sgot->output_offset + off);
2089
2090 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2091 contents, rel->r_offset,
2092 relocation, rel->r_addend);
2093
2094 break;
2095 case R_NIOS2_TLS_GD16:
2096 case R_NIOS2_TLS_IE16:
2097 {
2098 int indx;
2099 char tls_type;
2100
2101 if (htab->root.sgot == NULL)
2102 abort ();
2103
2104 indx = 0;
2105 if (h != NULL)
2106 {
2107 bfd_boolean dyn;
2108 dyn = htab->root.dynamic_sections_created;
2109 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2110 && (!info->shared
2111 || !SYMBOL_REFERENCES_LOCAL (info, h)))
2112 {
2113 unresolved_reloc = FALSE;
2114 indx = h->dynindx;
2115 }
2116 off = h->got.offset;
2117 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
2118 ->tls_type);
2119 }
2120 else
2121 {
2122 if (local_got_offsets == NULL)
2123 abort ();
2124 off = local_got_offsets[r_symndx];
2125 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
2126 [r_symndx]);
2127 }
2128
2129 if (tls_type == GOT_UNKNOWN)
2130 abort ();
2131
2132 if ((off & 1) != 0)
2133 off &= ~1;
2134 else
2135 {
2136 bfd_boolean need_relocs = FALSE;
2137 Elf_Internal_Rela outrel;
2138 bfd_byte *loc = NULL;
2139 int cur_off = off;
2140
2141 /* The GOT entries have not been initialized yet. Do it
2142 now, and emit any relocations. If both an IE GOT and a
2143 GD GOT are necessary, we emit the GD first. */
2144
2145 if ((info->shared || indx != 0)
2146 && (h == NULL
2147 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2148 || h->root.type != bfd_link_hash_undefweak))
2149 {
2150 need_relocs = TRUE;
2151 if (htab->root.srelgot == NULL)
2152 abort ();
2153 loc = htab->root.srelgot->contents;
2154 loc += (htab->root.srelgot->reloc_count *
2155 sizeof (Elf32_External_Rela));
2156 }
2157
2158 if (tls_type & GOT_TLS_GD)
2159 {
2160 if (need_relocs)
2161 {
2162 outrel.r_addend = 0;
2163 outrel.r_offset = (htab->root.sgot->output_section->vma
2164 + htab->root.sgot->output_offset
2165 + cur_off);
2166 outrel.r_info = ELF32_R_INFO (indx,
2167 R_NIOS2_TLS_DTPMOD);
2168
2169 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2170 loc);
2171 htab->root.srelgot->reloc_count++;
2172 loc += sizeof (Elf32_External_Rela);
2173
2174 if (indx == 0)
2175 bfd_put_32 (output_bfd,
2176 (relocation - dtpoff_base (info) -
2177 DTP_OFFSET),
2178 htab->root.sgot->contents + cur_off + 4);
2179 else
2180 {
2181 outrel.r_addend = 0;
2182 outrel.r_info = ELF32_R_INFO (indx,
2183 R_NIOS2_TLS_DTPREL);
2184 outrel.r_offset += 4;
2185
2186 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2187 loc);
2188 htab->root.srelgot->reloc_count++;
2189 loc += sizeof (Elf32_External_Rela);
2190 }
2191 }
2192 else
2193 {
2194 /* If we are not emitting relocations for a
2195 general dynamic reference, then we must be in a
2196 static link or an executable link with the
2197 symbol binding locally. Mark it as belonging
2198 to module 1, the executable. */
2199 bfd_put_32 (output_bfd, 1,
2200 htab->root.sgot->contents + cur_off);
2201 bfd_put_32 (output_bfd, (relocation -
2202 dtpoff_base (info) -
2203 DTP_OFFSET),
2204 htab->root.sgot->contents + cur_off + 4);
2205 }
2206
2207 cur_off += 8;
2208 }
2209
2210 if (tls_type & GOT_TLS_IE)
2211 {
2212 if (need_relocs)
2213 {
2214 if (indx == 0)
2215 outrel.r_addend = (relocation -
2216 dtpoff_base (info));
2217 else
2218 outrel.r_addend = 0;
2219 outrel.r_offset = (htab->root.sgot->output_section->vma
2220 + htab->root.sgot->output_offset
2221 + cur_off);
2222 outrel.r_info = ELF32_R_INFO (indx,
2223 R_NIOS2_TLS_TPREL);
2224
2225 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2226 loc);
2227 htab->root.srelgot->reloc_count++;
2228 loc += sizeof (Elf32_External_Rela);
2229 }
2230 else
2231 bfd_put_32 (output_bfd, (tpoff (info, relocation)
2232 - TP_OFFSET),
2233 htab->root.sgot->contents + cur_off);
2234 cur_off += 4;
2235 }
2236
2237 if (h != NULL)
2238 h->got.offset |= 1;
2239 else
2240 local_got_offsets[r_symndx] |= 1;
2241 }
2242
2243 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
2244 off += 8;
2245 relocation = (htab->root.sgot->output_offset + off);
2246
2247 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2248 contents, rel->r_offset,
2249 relocation, rel->r_addend);
2250 }
2251
2252 break;
2253 case R_NIOS2_TLS_LE16:
2254 if (info->shared && !info->pie)
2255 {
2256 (*_bfd_error_handler)
2257 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
2258 "permitted in shared object"),
2259 input_bfd, input_section,
2260 (long) rel->r_offset, howto->name);
2261 return FALSE;
2262 }
2263 else
2264 relocation = tpoff (info, relocation) - TP_OFFSET;
2265
2266 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2267 contents, rel->r_offset,
2268 relocation, rel->r_addend);
2269 break;
2270
2271 case R_NIOS2_BFD_RELOC_32:
2272 if (info->shared
2273 && (input_section->flags & SEC_ALLOC) != 0
2274 && (h == NULL
2275 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2276 || h->root.type != bfd_link_hash_undefweak))
2277 {
2278 Elf_Internal_Rela outrel;
2279 bfd_byte *loc;
2280 bfd_boolean skip, relocate;
2281
2282 /* When generating a shared object, these relocations
2283 are copied into the output file to be resolved at run
2284 time. */
2285
2286 skip = FALSE;
2287 relocate = FALSE;
2288
2289 outrel.r_offset
2290 = _bfd_elf_section_offset (output_bfd, info,
2291 input_section, rel->r_offset);
2292 if (outrel.r_offset == (bfd_vma) -1)
2293 skip = TRUE;
2294 else if (outrel.r_offset == (bfd_vma) -2)
2295 skip = TRUE, relocate = TRUE;
2296 outrel.r_offset += (input_section->output_section->vma
2297 + input_section->output_offset);
2298
2299 if (skip)
2300 memset (&outrel, 0, sizeof outrel);
2301 else if (h != NULL
2302 && h->dynindx != -1
2303 && (!info->shared
2304 || !info->symbolic
2305 || !h->def_regular))
2306 {
2307 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2308 outrel.r_addend = rel->r_addend;
2309 }
2310 else
2311 {
2312 /* This symbol is local, or marked to become local. */
2313 outrel.r_addend = relocation + rel->r_addend;
2314 relocate = TRUE;
2315 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
2316 }
2317
2318 sreloc = elf_section_data (input_section)->sreloc;
2319 if (sreloc == NULL)
2320 abort ();
2321
2322 loc = sreloc->contents;
2323 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2324 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2325
2326 /* This reloc will be computed at runtime, so there's no
2327 need to do anything now, except for R_NIOS2_BFD_RELOC_32
2328 relocations that have been turned into
2329 R_NIOS2_RELATIVE. */
2330 if (!relocate)
2331 break;
2332 }
2333
2334 r = _bfd_final_link_relocate (howto, input_bfd,
2335 input_section, contents,
2336 rel->r_offset, relocation,
2337 rel->r_addend);
2338 break;
2339
2340 case R_NIOS2_TLS_DTPREL:
2341 relocation -= dtpoff_base (info);
2342 /* Fall through. */
2343
2344 default:
2345 r = _bfd_final_link_relocate (howto, input_bfd,
2346 input_section, contents,
2347 rel->r_offset, relocation,
2348 rel->r_addend);
2349 break;
2350 }
2351 }
2352 else
2353 r = bfd_reloc_notsupported;
2354
2355 if (r != bfd_reloc_ok)
2356 {
2357 if (h != NULL)
2358 name = h->root.root.string;
2359 else
2360 {
2361 name = bfd_elf_string_from_elf_section (input_bfd,
2362 symtab_hdr->sh_link,
2363 sym->st_name);
2364 if (name == NULL || *name == '\0')
2365 name = bfd_section_name (input_bfd, sec);
2366 }
2367
2368 switch (r)
2369 {
2370 case bfd_reloc_overflow:
2371 r = info->callbacks->reloc_overflow (info, NULL, name,
2372 howto->name, (bfd_vma) 0,
2373 input_bfd, input_section,
2374 rel->r_offset);
2375 break;
2376
2377 case bfd_reloc_undefined:
2378 r = info->callbacks->undefined_symbol (info, name, input_bfd,
2379 input_section,
2380 rel->r_offset, TRUE);
2381 break;
2382
2383 case bfd_reloc_outofrange:
2384 if (msg == NULL)
2385 msg = _("relocation out of range");
2386 break;
2387
2388 case bfd_reloc_notsupported:
2389 if (msg == NULL)
2390 msg = _("unsupported relocation");
2391 break;
2392
2393 case bfd_reloc_dangerous:
2394 if (msg == NULL)
2395 msg = _("dangerous relocation");
2396 break;
2397
2398 default:
2399 if (msg == NULL)
2400 msg = _("unknown error");
2401 break;
2402 }
2403
2404 if (msg)
2405 {
2406 r = info->callbacks->warning
2407 (info, msg, name, input_bfd, input_section, rel->r_offset);
2408 return FALSE;
2409 }
2410 }
2411 }
2412 return TRUE;
2413}
2414
2415/* Implement elf-backend_section_flags:
2416 Convert NIOS2 specific section flags to bfd internal section flags. */
2417static bfd_boolean
2418nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
2419{
2420 if (hdr->sh_flags & SHF_NIOS2_GPREL)
2421 *flags |= SEC_SMALL_DATA;
2422
2423 return TRUE;
2424}
2425
2426/* Implement elf_backend_fake_sections:
2427 Set the correct type for an NIOS2 ELF section. We do this by the
2428 section name, which is a hack, but ought to work. */
2429static bfd_boolean
2430nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2431 Elf_Internal_Shdr *hdr, asection *sec)
2432{
2433 register const char *name = bfd_get_section_name (abfd, sec);
2434
2435 if ((sec->flags & SEC_SMALL_DATA)
2436 || strcmp (name, ".sdata") == 0
2437 || strcmp (name, ".sbss") == 0
2438 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
2439 hdr->sh_flags |= SHF_NIOS2_GPREL;
2440
2441 return TRUE;
2442}
2443
2444/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2445 shortcuts to them in our hash table. */
2446static bfd_boolean
2447create_got_section (bfd *dynobj, struct bfd_link_info *info)
2448{
2449 struct elf32_nios2_link_hash_table *htab;
2450
2451 htab = elf32_nios2_hash_table (info);
2452
2453 if (! _bfd_elf_create_got_section (dynobj, info))
2454 return FALSE;
2455
2456 /* In order for the two loads in .PLTresolve to share the same %hiadj,
2457 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
2458 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
2459 return FALSE;
2460
2461 return TRUE;
2462}
2463
2464/* Implement elf_backend_create_dynamic_sections:
2465 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
2466 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
2467 hash table. */
2468static bfd_boolean
2469nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2470{
2471 struct elf32_nios2_link_hash_table *htab;
2472
2473 htab = elf32_nios2_hash_table (info);
2474 if (!htab->root.sgot && !create_got_section (dynobj, info))
2475 return FALSE;
2476
2477 _bfd_elf_create_dynamic_sections (dynobj, info);
2478
2479 /* In order for the two loads in a shared object .PLTresolve to share the
2480 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
2481 to a 16-byte boundary. This is because the addresses for these loads
2482 include the -(.plt+4) PIC correction. */
2483 if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
2484 return FALSE;
2485
2486 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
2487 if (!htab->sdynbss)
2488 return FALSE;
2489 if (!info->shared)
2490 {
2491 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
2492 if (!htab->srelbss)
2493 return FALSE;
2494 }
2495
2496 return TRUE;
2497}
2498
2499/* Implement elf_backend_copy_indirect_symbol:
2500 Copy the extra info we tack onto an elf_link_hash_entry. */
2501static void
2502nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
2503 struct elf_link_hash_entry *dir,
2504 struct elf_link_hash_entry *ind)
2505{
2506 struct elf32_nios2_link_hash_entry *edir, *eind;
2507
2508 edir = (struct elf32_nios2_link_hash_entry *) dir;
2509 eind = (struct elf32_nios2_link_hash_entry *) ind;
2510
2511 if (eind->dyn_relocs != NULL)
2512 {
2513 if (edir->dyn_relocs != NULL)
2514 {
2515 struct elf32_nios2_dyn_relocs **pp;
2516 struct elf32_nios2_dyn_relocs *p;
2517
2518 /* Add reloc counts against the indirect sym to the direct sym
2519 list. Merge any entries against the same section. */
2520 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2521 {
2522 struct elf32_nios2_dyn_relocs *q;
2523
2524 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2525 if (q->sec == p->sec)
2526 {
2527 q->pc_count += p->pc_count;
2528 q->count += p->count;
2529 *pp = p->next;
2530 break;
2531 }
2532 if (q == NULL)
2533 pp = &p->next;
2534 }
2535 *pp = edir->dyn_relocs;
2536 }
2537
2538 edir->dyn_relocs = eind->dyn_relocs;
2539 eind->dyn_relocs = NULL;
2540 }
2541
2542 if (ind->root.type == bfd_link_hash_indirect
2543 && dir->got.refcount <= 0)
2544 {
2545 edir->tls_type = eind->tls_type;
2546 eind->tls_type = GOT_UNKNOWN;
2547 }
2548
2549 edir->got_types_used |= eind->got_types_used;
2550
2551 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2552}
2553
2554/* Implement elf_backend_check_relocs:
2555 Look through the relocs for a section during the first phase. */
2556static bfd_boolean
2557nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
2558 asection *sec, const Elf_Internal_Rela *relocs)
2559{
2560 bfd *dynobj;
2561 Elf_Internal_Shdr *symtab_hdr;
2562 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2563 const Elf_Internal_Rela *rel;
2564 const Elf_Internal_Rela *rel_end;
2565 struct elf32_nios2_link_hash_table *htab;
2566 asection *sgot;
2567 asection *srelgot;
2568 asection *sreloc = NULL;
2569 bfd_signed_vma *local_got_refcounts;
2570
2571 if (info->relocatable)
2572 return TRUE;
2573
2574 dynobj = elf_hash_table (info)->dynobj;
2575 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2576 sym_hashes = elf_sym_hashes (abfd);
2577 sym_hashes_end = (sym_hashes
2578 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2579 if (!elf_bad_symtab (abfd))
2580 sym_hashes_end -= symtab_hdr->sh_info;
2581 local_got_refcounts = elf_local_got_refcounts (abfd);
2582
2583 htab = elf32_nios2_hash_table (info);
2584 sgot = htab->root.sgot;
2585 srelgot = htab->root.srelgot;
2586
2587 rel_end = relocs + sec->reloc_count;
2588 for (rel = relocs; rel < rel_end; rel++)
2589 {
2590 unsigned int r_type;
2591 struct elf_link_hash_entry *h;
2592 unsigned long r_symndx;
2593
2594 r_symndx = ELF32_R_SYM (rel->r_info);
2595 if (r_symndx < symtab_hdr->sh_info)
2596 h = NULL;
2597 else
2598 {
2599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2600 while (h->root.type == bfd_link_hash_indirect
2601 || h->root.type == bfd_link_hash_warning)
2602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831
AM
2603
2604 /* PR15323, ref flags aren't set for references in the same
2605 object. */
2606 h->root.non_ir_ref = 1;
36591ba1
SL
2607 }
2608
2609 r_type = ELF32_R_TYPE (rel->r_info);
2610
2611 switch (r_type)
2612 {
2613 case R_NIOS2_GOT16:
2614 case R_NIOS2_CALL16:
2615 case R_NIOS2_TLS_GD16:
2616 case R_NIOS2_TLS_IE16:
2617 /* This symbol requires a global offset table entry. */
2618 {
2619 int tls_type, old_tls_type;
2620
2621 switch (r_type)
2622 {
2623 default:
2624 case R_NIOS2_GOT16:
2625 case R_NIOS2_CALL16:
2626 tls_type = GOT_NORMAL;
2627 break;
2628 case R_NIOS2_TLS_GD16:
2629 tls_type = GOT_TLS_GD;
2630 break;
2631 case R_NIOS2_TLS_IE16:
2632 tls_type = GOT_TLS_IE;
2633 break;
2634 }
2635
2636 if (dynobj == NULL)
2637 {
2638 /* Create the .got section. */
2639 elf_hash_table (info)->dynobj = dynobj = abfd;
2640 nios2_elf32_create_dynamic_sections (dynobj, info);
2641 }
2642
2643 if (sgot == NULL)
2644 {
2645 sgot = htab->root.sgot;
2646 BFD_ASSERT (sgot != NULL);
2647 }
2648
2649 if (srelgot == NULL
2650 && (h != NULL || info->shared))
2651 {
2652 srelgot = htab->root.srelgot;
2653 BFD_ASSERT (srelgot != NULL);
2654 }
2655
2656 if (h != NULL)
2657 {
2658 struct elf32_nios2_link_hash_entry *eh
2659 = (struct elf32_nios2_link_hash_entry *)h;
2660 h->got.refcount++;
2661 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
2662 if (r_type == R_NIOS2_CALL16)
2663 {
2664 /* Make sure a plt entry is created for this symbol if
2665 it turns out to be a function defined by a dynamic
2666 object. */
2667 h->plt.refcount++;
2668 h->needs_plt = 1;
2669 h->type = STT_FUNC;
2670 eh->got_types_used |= CALL16_USED;
2671 }
2672 else
2673 eh->got_types_used |= GOT16_USED;
2674 }
2675 else
2676 {
2677 /* This is a global offset table entry for a local symbol. */
2678 if (local_got_refcounts == NULL)
2679 {
2680 bfd_size_type size;
2681
2682 size = symtab_hdr->sh_info;
2683 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2684 local_got_refcounts
2685 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
2686 if (local_got_refcounts == NULL)
2687 return FALSE;
2688 elf_local_got_refcounts (abfd) = local_got_refcounts;
2689 elf32_nios2_local_got_tls_type (abfd)
2690 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2691 }
2692 local_got_refcounts[r_symndx]++;
2693 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
2694 }
2695
2696 /* We will already have issued an error message if there is a
2697 TLS / non-TLS mismatch, based on the symbol type. We don't
2698 support any linker relaxations. So just combine any TLS
2699 types needed. */
2700 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2701 && tls_type != GOT_NORMAL)
2702 tls_type |= old_tls_type;
2703
2704 if (old_tls_type != tls_type)
2705 {
2706 if (h != NULL)
2707 elf32_nios2_hash_entry (h)->tls_type = tls_type;
2708 else
2709 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
2710 }
2711 }
2712 /* Fall through */
2713 case R_NIOS2_TLS_LDM16:
2714 if (r_type == R_NIOS2_TLS_LDM16)
2715 htab->tls_ldm_got.refcount++;
2716
2717 if (htab->root.sgot == NULL)
2718 {
2719 if (htab->root.dynobj == NULL)
2720 htab->root.dynobj = abfd;
2721 if (!create_got_section (htab->root.dynobj, info))
2722 return FALSE;
2723 }
2724 break;
2725
2726 /* This relocation describes the C++ object vtable hierarchy.
2727 Reconstruct it for later use during GC. */
2728 case R_NIOS2_GNU_VTINHERIT:
2729 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2730 return FALSE;
2731 break;
2732
2733 /* This relocation describes which C++ vtable entries are actually
2734 used. Record for later use during GC. */
2735 case R_NIOS2_GNU_VTENTRY:
2736 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2737 return FALSE;
2738 break;
2739
2740 case R_NIOS2_BFD_RELOC_32:
2741 case R_NIOS2_CALL26:
2742 case R_NIOS2_HIADJ16:
2743 case R_NIOS2_LO16:
2744
2745 if (h != NULL)
2746 {
2747 /* If this reloc is in a read-only section, we might
2748 need a copy reloc. We can't check reliably at this
2749 stage whether the section is read-only, as input
2750 sections have not yet been mapped to output sections.
2751 Tentatively set the flag for now, and correct in
2752 adjust_dynamic_symbol. */
2753 if (!info->shared)
2754 h->non_got_ref = 1;
2755
2756 /* Make sure a plt entry is created for this symbol if it
2757 turns out to be a function defined by a dynamic object. */
2758 h->plt.refcount++;
2759
2760 if (r_type == R_NIOS2_CALL26)
2761 h->needs_plt = 1;
2762 }
2763
2764 /* If we are creating a shared library, we need to copy the
2765 reloc into the shared library. */
2766 if (info->shared
2767 && (sec->flags & SEC_ALLOC) != 0
2768 && (r_type == R_NIOS2_BFD_RELOC_32
2769 || (h != NULL && ! h->needs_plt
2770 && (! info->symbolic || ! h->def_regular))))
2771 {
2772 struct elf32_nios2_dyn_relocs *p;
2773 struct elf32_nios2_dyn_relocs **head;
2774
2775 /* When creating a shared object, we must copy these
2776 reloc types into the output file. We create a reloc
2777 section in dynobj and make room for this reloc. */
2778 if (sreloc == NULL)
2779 {
2780 sreloc = _bfd_elf_make_dynamic_reloc_section
2781 (sec, dynobj, 2, abfd, TRUE);
2782 if (sreloc == NULL)
2783 return FALSE;
2784 }
2785
2786 /* If this is a global symbol, we count the number of
2787 relocations we need for this symbol. */
2788 if (h != NULL)
2789 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
2790 else
2791 {
2792 /* Track dynamic relocs needed for local syms too.
2793 We really need local syms available to do this
2794 easily. Oh well. */
2795
2796 asection *s;
2797 void *vpp;
2798 Elf_Internal_Sym *isym;
2799
2800 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2801 abfd, r_symndx);
2802 if (isym == NULL)
2803 return FALSE;
2804
2805 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2806 if (s == NULL)
2807 s = sec;
2808
2809 vpp = &elf_section_data (s)->local_dynrel;
2810 head = (struct elf32_nios2_dyn_relocs **) vpp;
2811 }
2812
2813 p = *head;
2814 if (p == NULL || p->sec != sec)
2815 {
2816 bfd_size_type amt = sizeof *p;
2817 p = ((struct elf32_nios2_dyn_relocs *)
2818 bfd_alloc (htab->root.dynobj, amt));
2819 if (p == NULL)
2820 return FALSE;
2821 p->next = *head;
2822 *head = p;
2823 p->sec = sec;
2824 p->count = 0;
2825 p->pc_count = 0;
2826 }
2827
2828 p->count += 1;
2829
2830 }
2831 break;
2832 }
2833 }
2834
2835 return TRUE;
2836}
2837
2838
2839/* Implement elf_backend_gc_mark_hook:
2840 Return the section that should be marked against GC for a given
2841 relocation. */
2842static asection *
2843nios2_elf32_gc_mark_hook (asection *sec,
25153ba0 2844 struct bfd_link_info *info,
36591ba1
SL
2845 Elf_Internal_Rela *rel,
2846 struct elf_link_hash_entry *h,
2847 Elf_Internal_Sym *sym)
2848{
2849 if (h != NULL)
2850 switch (ELF32_R_TYPE (rel->r_info))
2851 {
2852 case R_NIOS2_GNU_VTINHERIT:
2853 case R_NIOS2_GNU_VTENTRY:
2854 return NULL;
2855 }
2856 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2857}
2858
2859/* Implement elf_backend_gc_sweep_hook:
2860 Update the got entry reference counts for the section being removed. */
2861static bfd_boolean
2862nios2_elf32_gc_sweep_hook (bfd *abfd,
2863 struct bfd_link_info *info,
2864 asection *sec,
2865 const Elf_Internal_Rela *relocs)
2866{
2867 Elf_Internal_Shdr *symtab_hdr;
2868 struct elf_link_hash_entry **sym_hashes;
2869 bfd_signed_vma *local_got_refcounts;
2870 const Elf_Internal_Rela *rel, *relend;
2871 bfd *dynobj;
2872
2873 if (info->relocatable)
2874 return TRUE;
2875
2876 elf_section_data (sec)->local_dynrel = NULL;
2877
2878 dynobj = elf_hash_table (info)->dynobj;
2879 if (dynobj == NULL)
2880 return TRUE;
2881
2882 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2883 sym_hashes = elf_sym_hashes (abfd);
2884 local_got_refcounts = elf_local_got_refcounts (abfd);
2885
2886 relend = relocs + sec->reloc_count;
2887 for (rel = relocs; rel < relend; rel++)
2888 {
2889 unsigned long r_symndx;
2890 struct elf_link_hash_entry *h = NULL;
2891 int r_type;
2892
2893 r_symndx = ELF32_R_SYM (rel->r_info);
2894 if (r_symndx >= symtab_hdr->sh_info)
2895 {
2896 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2897 while (h->root.type == bfd_link_hash_indirect
2898 || h->root.type == bfd_link_hash_warning)
2899 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2900 }
2901
2902 r_type = ELF32_R_TYPE (rel->r_info);
2903 switch (r_type)
2904 {
2905 case R_NIOS2_GOT16:
2906 case R_NIOS2_CALL16:
2907 if (h != NULL)
2908 {
2909 if (h->got.refcount > 0)
2910 --h->got.refcount;
2911 }
2912 else if (local_got_refcounts != NULL)
2913 {
2914 if (local_got_refcounts[r_symndx] > 0)
2915 --local_got_refcounts[r_symndx];
2916 }
2917 break;
2918
2919 case R_NIOS2_PCREL_LO:
2920 case R_NIOS2_PCREL_HA:
2921 case R_NIOS2_BFD_RELOC_32:
2922 case R_NIOS2_CALL26:
2923 if (h != NULL)
2924 {
2925 struct elf32_nios2_link_hash_entry *eh;
2926 struct elf32_nios2_dyn_relocs **pp;
2927 struct elf32_nios2_dyn_relocs *p;
2928
2929 eh = (struct elf32_nios2_link_hash_entry *) h;
2930
2931 if (h->plt.refcount > 0)
2932 --h->plt.refcount;
2933
2934 if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
2935 || r_type == R_NIOS2_BFD_RELOC_32)
2936 {
2937 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
2938 pp = &p->next)
2939 if (p->sec == sec)
2940 {
2941 p->count -= 1;
2942 if (p->count == 0)
2943 *pp = p->next;
2944 break;
2945 }
2946 }
2947 }
2948 break;
2949
2950 default:
2951 break;
2952 }
2953 }
2954
2955 return TRUE;
2956}
2957
2958/* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
2959static void
2960nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
2961{
2962 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
2963
2964 BFD_ASSERT(value <= 0xffff);
2965
2966 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
2967 sec->contents + offset);
2968}
2969
2970/* Install COUNT 32-bit values DATA starting at offset OFFSET into
2971 section SEC. */
2972static void
2973nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
2974 int count)
2975{
2976 while (count--)
2977 {
2978 bfd_put_32 (sec->owner, *data, sec->contents + offset);
2979 offset += 4;
2980 ++data;
2981 }
2982}
2983
2984/* Implement elf_backend_finish_dynamic_symbols:
2985 Finish up dynamic symbol handling. We set the contents of various
2986 dynamic sections here. */
2987static bfd_boolean
2988nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
2989 struct bfd_link_info *info,
2990 struct elf_link_hash_entry *h,
2991 Elf_Internal_Sym *sym)
2992{
2993 struct elf32_nios2_link_hash_table *htab;
2994 struct elf32_nios2_link_hash_entry *eh
2995 = (struct elf32_nios2_link_hash_entry *)h;
2996 int use_plt;
2997
2998 htab = elf32_nios2_hash_table (info);
2999
3000 if (h->plt.offset != (bfd_vma) -1)
3001 {
3002 asection *splt;
3003 asection *sgotplt;
3004 asection *srela;
3005 bfd_vma plt_index;
3006 bfd_vma got_offset;
3007 Elf_Internal_Rela rela;
3008 bfd_byte *loc;
3009 bfd_vma got_address;
3010
3011 /* This symbol has an entry in the procedure linkage table. Set
3012 it up. */
3013 BFD_ASSERT (h->dynindx != -1);
3014 splt = htab->root.splt;
3015 sgotplt = htab->root.sgotplt;
3016 srela = htab->root.srelplt;
3017 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
3018
3019 /* Emit the PLT entry. */
3020 if (info->shared)
3021 {
3022 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
3023 3);
3024 plt_index = (h->plt.offset - 24) / 12;
3025 got_offset = (plt_index + 3) * 4;
3026 nios2_elf32_install_imm16 (splt, h->plt.offset,
3027 hiadj(plt_index * 4));
3028 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3029 (plt_index * 4) & 0xffff);
3030 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
3031 0xfff4 - h->plt.offset);
3032 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3033 + got_offset);
3034
3035 /* Fill in the entry in the global offset table. There are no
3036 res_n slots for a shared object PLT, instead the .got.plt entries
3037 point to the PLT entries. */
3038 bfd_put_32 (output_bfd,
3039 splt->output_section->vma + splt->output_offset
3040 + h->plt.offset, sgotplt->contents + got_offset);
3041 }
3042 else
3043 {
3044 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
3045 got_offset = (plt_index + 3) * 4;
3046
3047 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
3048 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3049 + got_offset);
3050 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
3051 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3052 got_address & 0xffff);
3053
3054 /* Fill in the entry in the global offset table. */
3055 bfd_put_32 (output_bfd,
3056 splt->output_section->vma + splt->output_offset
3057 + plt_index * 4, sgotplt->contents + got_offset);
3058 }
3059
3060 /* Fill in the entry in the .rela.plt section. */
3061 rela.r_offset = got_address;
3062 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
3063 rela.r_addend = 0;
3064 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3065 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3066
3067 if (!h->def_regular)
3068 {
3069 /* Mark the symbol as undefined, rather than as defined in
3070 the .plt section. Leave the value alone. */
3071 sym->st_shndx = SHN_UNDEF;
3072 /* If the symbol is weak, we do need to clear the value.
3073 Otherwise, the PLT entry would provide a definition for
3074 the symbol even if the symbol wasn't defined anywhere,
3075 and so the symbol would never be NULL. */
3076 if (!h->ref_regular_nonweak)
3077 sym->st_value = 0;
3078 }
3079 }
3080
3081 use_plt = (eh->got_types_used == CALL16_USED
3082 && h->plt.offset != (bfd_vma) -1);
3083
3084 if (!use_plt && h->got.offset != (bfd_vma) -1
3085 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
3086 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
3087 {
3088 asection *sgot;
3089 asection *srela;
3090 Elf_Internal_Rela rela;
3091 bfd_byte *loc;
3092 bfd_vma offset;
3093
3094 /* This symbol has an entry in the global offset table. Set it
3095 up. */
3096 sgot = htab->root.sgot;
3097 srela = htab->root.srelgot;
3098 BFD_ASSERT (sgot != NULL && srela != NULL);
3099
3100 offset = (h->got.offset & ~(bfd_vma) 1);
3101 rela.r_offset = (sgot->output_section->vma
3102 + sgot->output_offset + offset);
3103
3104 /* If this is a -Bsymbolic link, and the symbol is defined
3105 locally, we just want to emit a RELATIVE reloc. Likewise if
3106 the symbol was forced to be local because of a version file.
3107 The entry in the global offset table will already have been
3108 initialized in the relocate_section function. */
3109
3110 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
3111 {
3112 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
3113 rela.r_addend = bfd_get_signed_32 (output_bfd,
3114 (sgot->contents + offset));
3115 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
3116 }
3117 else
3118 {
3119 bfd_put_32 (output_bfd, (bfd_vma) 0,
3120 sgot->contents + offset);
3121 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
3122 rela.r_addend = 0;
3123 }
3124
3125 loc = srela->contents;
3126 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3127 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3128 }
3129
3130 if (use_plt && h->got.offset != (bfd_vma) -1)
3131 {
3132 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
3133 asection *sgot = htab->root.sgot;
3134 asection *splt = htab->root.splt;
3135 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
3136 + h->plt.offset),
3137 sgot->contents + offset);
3138 }
3139
3140 if (h->needs_copy)
3141 {
3142 asection *s;
3143 Elf_Internal_Rela rela;
3144 bfd_byte *loc;
3145
3146 /* This symbol needs a copy reloc. Set it up. */
3147 BFD_ASSERT (h->dynindx != -1
3148 && (h->root.type == bfd_link_hash_defined
3149 || h->root.type == bfd_link_hash_defweak));
3150
3151 s = htab->srelbss;
3152 BFD_ASSERT (s != NULL);
3153
3154 rela.r_offset = (h->root.u.def.value
3155 + h->root.u.def.section->output_section->vma
3156 + h->root.u.def.section->output_offset);
3157 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
3158 rela.r_addend = 0;
3159 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3160 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3161 }
3162
3163 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3164 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3165 || h == elf_hash_table (info)->hgot)
3166 sym->st_shndx = SHN_ABS;
3167
3168 return TRUE;
3169}
3170
3171/* Implement elf_backend_finish_dynamic_sections. */
3172static bfd_boolean
3173nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
3174 struct bfd_link_info *info)
3175{
3176 bfd *dynobj;
3177 asection *sgotplt;
3178 asection *sdyn;
3179 struct elf32_nios2_link_hash_table *htab;
3180
3181 htab = elf32_nios2_hash_table (info);
3182 dynobj = elf_hash_table (info)->dynobj;
3183 sgotplt = htab->root.sgotplt;
3184 BFD_ASSERT (sgotplt != NULL);
3185 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3186
3187 if (elf_hash_table (info)->dynamic_sections_created)
3188 {
3189 asection *splt;
3190 Elf32_External_Dyn *dyncon, *dynconend;
3191
3192 splt = htab->root.splt;
3193 BFD_ASSERT (splt != NULL && sdyn != NULL);
3194
3195 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3196 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3197 for (; dyncon < dynconend; dyncon++)
3198 {
3199 Elf_Internal_Dyn dyn;
3200 asection *s;
3201
3202 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3203
3204 switch (dyn.d_tag)
3205 {
3206 default:
3207 break;
3208
3209 case DT_PLTGOT:
3210 s = htab->root.sgot;
3211 BFD_ASSERT (s != NULL);
3212 dyn.d_un.d_ptr = s->output_section->vma;
3213 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3214 break;
3215
3216 case DT_JMPREL:
3217 s = htab->root.srelplt;
3218 BFD_ASSERT (s != NULL);
3219 dyn.d_un.d_ptr = s->output_section->vma;
3220 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3221 break;
3222
3223 case DT_PLTRELSZ:
3224 s = htab->root.srelplt;
3225 BFD_ASSERT (s != NULL);
3226 dyn.d_un.d_val = s->size;
3227 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3228 break;
3229
3230 case DT_RELASZ:
3231 /* The procedure linkage table relocs (DT_JMPREL) should
3232 not be included in the overall relocs (DT_RELA).
3233 Therefore, we override the DT_RELASZ entry here to
3234 make it not include the JMPREL relocs. Since the
3235 linker script arranges for .rela.plt to follow all
3236 other relocation sections, we don't have to worry
3237 about changing the DT_RELA entry. */
3238 s = htab->root.srelplt;
3239 if (s != NULL)
3240 dyn.d_un.d_val -= s->size;
3241 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3242 break;
3243
3244 case DT_NIOS2_GP:
3245 s = htab->root.sgot;
3246 BFD_ASSERT (s != NULL);
3247 dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
3248 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3249 break;
3250 }
3251 }
3252
3253 /* Fill in the first entry in the procedure linkage table. */
3254 if (splt->size > 0)
3255 {
3256 bfd_vma got_address = (sgotplt->output_section->vma
3257 + sgotplt->output_offset);
3258 if (info->shared)
3259 {
3260 bfd_vma corrected = got_address - (splt->output_section->vma
3261 + splt->output_offset + 4);
3262 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
3263 nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
3264 nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
3265 nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
36591ba1
SL
3266 }
3267 else
3268 {
3269 /* Divide by 4 here, not 3 because we already corrected for the
3270 res_N branches. */
3271 bfd_vma res_size = (splt->size - 28) / 4;
3272 bfd_vma res_start = (splt->output_section->vma
3273 + splt->output_offset);
3274 bfd_vma res_offset;
3275
3276 for (res_offset = 0; res_offset < res_size; res_offset += 4)
3277 bfd_put_32 (output_bfd,
3278 6 | ((res_size - (res_offset + 4)) << 6),
3279 splt->contents + res_offset);
3280
3281 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
3282 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
3283 nios2_elf32_install_imm16 (splt, res_size + 4,
3284 res_start & 0xffff);
3285 nios2_elf32_install_imm16 (splt, res_size + 12,
3286 hiadj (got_address));
3287 nios2_elf32_install_imm16 (splt, res_size + 16,
3288 (got_address & 0xffff) + 4);
3289 nios2_elf32_install_imm16 (splt, res_size + 20,
3290 (got_address & 0xffff) + 8);
36591ba1
SL
3291 }
3292 }
3293 }
3294 /* Fill in the first three entries in the global offset table. */
3295 if (sgotplt->size > 0)
3296 {
3297 if (sdyn == NULL)
3298 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
3299 else
3300 bfd_put_32 (output_bfd,
3301 sdyn->output_section->vma + sdyn->output_offset,
3302 sgotplt->contents);
3303 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
3304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
3305 }
3306
3307 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
3308
3309 return TRUE;
3310}
3311
3312/* Implement elf_backend_adjust_dynamic_symbol:
3313 Adjust a symbol defined by a dynamic object and referenced by a
3314 regular object. The current definition is in some section of the
3315 dynamic object, but we're not including those sections. We have to
3316 change the definition to something the rest of the link can
3317 understand. */
3318static bfd_boolean
3319nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
3320 struct elf_link_hash_entry *h)
3321{
3322 struct elf32_nios2_link_hash_table *htab;
3323 bfd *dynobj;
3324 asection *s;
3325 unsigned align2;
3326
3327 htab = elf32_nios2_hash_table (info);
3328 dynobj = elf_hash_table (info)->dynobj;
3329
3330 /* Make sure we know what is going on here. */
3331 BFD_ASSERT (dynobj != NULL
3332 && (h->needs_plt
3333 || h->u.weakdef != NULL
3334 || (h->def_dynamic
3335 && h->ref_regular
3336 && !h->def_regular)));
3337
3338 /* If this is a function, put it in the procedure linkage table. We
3339 will fill in the contents of the procedure linkage table later,
3340 when we know the address of the .got section. */
3341 if (h->type == STT_FUNC || h->needs_plt)
3342 {
3343 if (h->plt.refcount <= 0
3344 || SYMBOL_CALLS_LOCAL (info, h)
3345 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3346 && h->root.type == bfd_link_hash_undefweak))
3347 {
3348 /* This case can occur if we saw a PLT reloc in an input
3349 file, but the symbol was never referred to by a dynamic
3350 object, or if all references were garbage collected. In
3351 such a case, we don't actually need to build a procedure
3352 linkage table, and we can just do a PCREL reloc instead. */
3353 h->plt.offset = (bfd_vma) -1;
3354 h->needs_plt = 0;
3355 }
3356
3357 return TRUE;
3358 }
3359
3360 /* Reinitialize the plt offset now that it is not used as a reference
3361 count any more. */
3362 h->plt.offset = (bfd_vma) -1;
3363
3364 /* If this is a weak symbol, and there is a real definition, the
3365 processor independent code will have arranged for us to see the
3366 real definition first, and we can just use the same value. */
3367 if (h->u.weakdef != NULL)
3368 {
3369 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3370 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3371 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3372 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3373 return TRUE;
3374 }
3375
3376 /* If there are no non-GOT references, we do not need a copy
3377 relocation. */
3378 if (!h->non_got_ref)
3379 return TRUE;
3380
3381 /* This is a reference to a symbol defined by a dynamic object which
3382 is not a function.
3383 If we are creating a shared library, we must presume that the
3384 only references to the symbol are via the global offset table.
3385 For such cases we need not do anything here; the relocations will
3386 be handled correctly by relocate_section. */
3387 if (info->shared)
3388 return TRUE;
3389
3390 if (h->size == 0)
3391 {
3392 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3393 h->root.root.string);
3394 return TRUE;
3395 }
3396
3397 /* We must allocate the symbol in our .dynbss section, which will
3398 become part of the .bss section of the executable. There will be
3399 an entry for this symbol in the .dynsym section. The dynamic
3400 object will contain position independent code, so all references
3401 from the dynamic object to this symbol will go through the global
3402 offset table. The dynamic linker will use the .dynsym entry to
3403 determine the address it must put in the global offset table, so
3404 both the dynamic object and the regular object will refer to the
3405 same memory location for the variable. */
3406 s = htab->sdynbss;
3407 BFD_ASSERT (s != NULL);
3408
3409 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
3410 copy the initial value out of the dynamic object and into the
3411 runtime process image. We need to remember the offset into the
3412 .rela.bss section we are going to use. */
3413 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3414 {
3415 asection *srel;
3416
3417 srel = htab->srelbss;
3418 BFD_ASSERT (srel != NULL);
3419 srel->size += sizeof (Elf32_External_Rela);
3420 h->needs_copy = 1;
3421 }
3422
3423 align2 = bfd_log2 (h->size);
3424 if (align2 > h->root.u.def.section->alignment_power)
3425 align2 = h->root.u.def.section->alignment_power;
3426
3427 /* Align dynbss. */
3428 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
3429 if (align2 > bfd_get_section_alignment (dynobj, s)
3430 && !bfd_set_section_alignment (dynobj, s, align2))
3431 return FALSE;
3432
3433 /* Define the symbol as being at this point in the section. */
3434 h->root.u.def.section = s;
3435 h->root.u.def.value = s->size;
3436
3437 /* Increment the section size to make room for the symbol. */
3438 s->size += h->size;
3439
3440 return TRUE;
3441}
3442
3443/* Worker function for nios2_elf32_size_dynamic_sections. */
3444static bfd_boolean
3445adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3446{
3447 struct bfd_link_info *info;
3448 struct elf32_nios2_link_hash_table *htab;
3449
3450 if (h->root.type == bfd_link_hash_indirect)
3451 return TRUE;
3452
3453 if (h->root.type == bfd_link_hash_warning)
3454 /* When warning symbols are created, they **replace** the "real"
3455 entry in the hash table, thus we never get to see the real
3456 symbol in a hash traversal. So look at it now. */
3457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3458
3459 info = (struct bfd_link_info *) inf;
3460 htab = elf32_nios2_hash_table (info);
3461
3462 if (h->plt.offset != (bfd_vma)-1)
3463 h->plt.offset += htab->res_n_size;
3464 if (htab->root.splt == h->root.u.def.section)
3465 h->root.u.def.value += htab->res_n_size;
3466
3467 return TRUE;
3468}
3469
3470/* Another worker function for nios2_elf32_size_dynamic_sections.
3471 Allocate space in .plt, .got and associated reloc sections for
3472 dynamic relocs. */
3473static bfd_boolean
3474allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3475{
3476 struct bfd_link_info *info;
3477 struct elf32_nios2_link_hash_table *htab;
3478 struct elf32_nios2_link_hash_entry *eh;
3479 struct elf32_nios2_dyn_relocs *p;
3480 int use_plt;
3481
3482 if (h->root.type == bfd_link_hash_indirect)
3483 return TRUE;
3484
3485 if (h->root.type == bfd_link_hash_warning)
3486 /* When warning symbols are created, they **replace** the "real"
3487 entry in the hash table, thus we never get to see the real
3488 symbol in a hash traversal. So look at it now. */
3489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3490
3491 info = (struct bfd_link_info *) inf;
3492 htab = elf32_nios2_hash_table (info);
3493
3494 if (htab->root.dynamic_sections_created
3495 && h->plt.refcount > 0)
3496 {
3497 /* Make sure this symbol is output as a dynamic symbol.
3498 Undefined weak syms won't yet be marked as dynamic. */
3499 if (h->dynindx == -1
3500 && !h->forced_local
3501 && !bfd_elf_link_record_dynamic_symbol (info, h))
3502 return FALSE;
3503
3504 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3505 {
3506 asection *s = htab->root.splt;
3507
3508 /* Allocate room for the header. */
3509 if (s->size == 0)
3510 {
3511 if (info->shared)
3512 s->size = 24;
3513 else
3514 s->size = 28;
3515 }
3516
3517 h->plt.offset = s->size;
3518
3519 /* If this symbol is not defined in a regular file, and we are
3520 not generating a shared library, then set the symbol to this
3521 location in the .plt. This is required to make function
3522 pointers compare as equal between the normal executable and
3523 the shared library. */
3524 if (! info->shared
3525 && !h->def_regular)
3526 {
3527 h->root.u.def.section = s;
3528 h->root.u.def.value = h->plt.offset;
3529 }
3530
3531 /* Make room for this entry. */
3532 s->size += 12;
3533
3534 /* We also need to make an entry in the .rela.plt section. */
3535 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3536
3537 /* And the .got.plt section. */
3538 htab->root.sgotplt->size += 4;
3539 }
3540 else
3541 {
3542 h->plt.offset = (bfd_vma) -1;
3543 h->needs_plt = 0;
3544 }
3545 }
3546 else
3547 {
3548 h->plt.offset = (bfd_vma) -1;
3549 h->needs_plt = 0;
3550 }
3551
3552 eh = (struct elf32_nios2_link_hash_entry *) h;
3553 use_plt = (eh->got_types_used == CALL16_USED
3554 && h->plt.offset != (bfd_vma) -1);
3555
3556 if (h->got.refcount > 0)
3557 {
3558 asection *s;
3559 bfd_boolean dyn;
3560 int tls_type = eh->tls_type;
3561 int indx;
3562
3563 /* Make sure this symbol is output as a dynamic symbol.
3564 Undefined weak syms won't yet be marked as dynamic. */
3565 if (h->dynindx == -1
3566 && !h->forced_local
3567 && !bfd_elf_link_record_dynamic_symbol (info, h))
3568 return FALSE;
3569
3570 s = htab->root.sgot;
3571 h->got.offset = s->size;
3572
3573 if (tls_type == GOT_UNKNOWN)
3574 abort ();
3575
3576 if (tls_type == GOT_NORMAL)
3577 /* Non-TLS symbols need one GOT slot. */
3578 s->size += 4;
3579 else
3580 {
3581 if (tls_type & GOT_TLS_GD)
3582 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
3583 s->size += 8;
3584 if (tls_type & GOT_TLS_IE)
3585 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
3586 s->size += 4;
3587 }
3588
3589 dyn = htab->root.dynamic_sections_created;
3590
3591 indx = 0;
3592 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3593 && (!info->shared
3594 || !SYMBOL_REFERENCES_LOCAL (info, h)))
3595 indx = h->dynindx;
3596
3597 if (tls_type != GOT_NORMAL
3598 && (info->shared || indx != 0)
3599 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3600 || h->root.type != bfd_link_hash_undefweak))
3601 {
3602 if (tls_type & GOT_TLS_IE)
3603 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3604
3605 if (tls_type & GOT_TLS_GD)
3606 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3607
3608 if ((tls_type & GOT_TLS_GD) && indx != 0)
3609 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3610 }
3611 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3612 || h->root.type != bfd_link_hash_undefweak)
3613 && !use_plt
3614 && (info->shared
3615 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3616 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3617 }
3618 else
3619 h->got.offset = (bfd_vma) -1;
3620
3621 if (eh->dyn_relocs == NULL)
3622 return TRUE;
3623
3624 /* In the shared -Bsymbolic case, discard space allocated for
3625 dynamic pc-relative relocs against symbols which turn out to be
3626 defined in regular objects. For the normal shared case, discard
3627 space for pc-relative relocs that have become local due to symbol
3628 visibility changes. */
3629
3630 if (info->shared)
3631 {
3632 if (h->def_regular
3633 && (h->forced_local || info->symbolic))
3634 {
3635 struct elf32_nios2_dyn_relocs **pp;
3636
3637 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3638 {
3639 p->count -= p->pc_count;
3640 p->pc_count = 0;
3641 if (p->count == 0)
3642 *pp = p->next;
3643 else
3644 pp = &p->next;
3645 }
3646 }
3647
3648 /* Also discard relocs on undefined weak syms with non-default
3649 visibility. */
3650 if (eh->dyn_relocs != NULL
3651 && h->root.type == bfd_link_hash_undefweak)
3652 {
3653 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3654 eh->dyn_relocs = NULL;
3655
3656 /* Make sure undefined weak symbols are output as a dynamic
3657 symbol in PIEs. */
3658 else if (h->dynindx == -1
3659 && !h->forced_local
3660 && !bfd_elf_link_record_dynamic_symbol (info, h))
3661 return FALSE;
3662 }
3663 }
3664 else
3665 {
3666 /* For the non-shared case, discard space for relocs against
3667 symbols which turn out to need copy relocs or are not
3668 dynamic. */
3669
3670 if (!h->non_got_ref
3671 && ((h->def_dynamic && !h->def_regular)
3672 || (htab->root.dynamic_sections_created
3673 && (h->root.type == bfd_link_hash_undefweak
3674 || h->root.type == bfd_link_hash_undefined))))
3675 {
3676 /* Make sure this symbol is output as a dynamic symbol.
3677 Undefined weak syms won't yet be marked as dynamic. */
3678 if (h->dynindx == -1
3679 && !h->forced_local
3680 && !bfd_elf_link_record_dynamic_symbol (info, h))
3681 return FALSE;
3682
3683 /* If that succeeded, we know we'll be keeping all the
3684 relocs. */
3685 if (h->dynindx != -1)
3686 goto keep;
3687 }
3688
3689 eh->dyn_relocs = NULL;
3690
3691 keep: ;
3692 }
3693
3694 /* Finally, allocate space. */
3695 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3696 {
3697 asection *sreloc = elf_section_data (p->sec)->sreloc;
3698 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3699 }
3700
3701 return TRUE;
3702}
3703
3704/* Implement elf_backend_size_dynamic_sections:
3705 Set the sizes of the dynamic sections. */
3706static bfd_boolean
3707nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3708 struct bfd_link_info *info)
3709{
3710 bfd *dynobj;
3711 asection *s;
3712 bfd_boolean plt;
3713 bfd_boolean got;
3714 bfd_boolean relocs;
3715 bfd *ibfd;
3716 struct elf32_nios2_link_hash_table *htab;
3717
3718 htab = elf32_nios2_hash_table (info);
3719 dynobj = elf_hash_table (info)->dynobj;
3720 BFD_ASSERT (dynobj != NULL);
3721
3722 htab->res_n_size = 0;
3723 if (elf_hash_table (info)->dynamic_sections_created)
3724 {
3725 /* Set the contents of the .interp section to the interpreter. */
3726 if (info->executable)
3727 {
3728 s = bfd_get_linker_section (dynobj, ".interp");
3729 BFD_ASSERT (s != NULL);
3730 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3731 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3732 }
3733 }
3734 else
3735 {
3736 /* We may have created entries in the .rela.got section.
3737 However, if we are not creating the dynamic sections, we will
3738 not actually use these entries. Reset the size of .rela.got,
3739 which will cause it to get stripped from the output file
3740 below. */
3741 s = htab->root.srelgot;
3742 if (s != NULL)
3743 s->size = 0;
3744 }
3745
3746 /* Set up .got offsets for local syms, and space for local dynamic
3747 relocs. */
3748 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3749 {
3750 bfd_signed_vma *local_got;
3751 bfd_signed_vma *end_local_got;
3752 char *local_tls_type;
3753 bfd_size_type locsymcount;
3754 Elf_Internal_Shdr *symtab_hdr;
3755 asection *srel;
3756
3757 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3758 continue;
3759
3760 for (s = ibfd->sections; s != NULL; s = s->next)
3761 {
3762 struct elf32_nios2_dyn_relocs *p;
3763
3764 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
3765 {
3766 if (!bfd_is_abs_section (p->sec)
3767 && bfd_is_abs_section (p->sec->output_section))
3768 {
3769 /* Input section has been discarded, either because
3770 it is a copy of a linkonce section or due to
3771 linker script /DISCARD/, so we'll be discarding
3772 the relocs too. */
3773 }
3774 else if (p->count != 0)
3775 {
3776 srel = elf_section_data (p->sec)->sreloc;
3777 srel->size += p->count * sizeof (Elf32_External_Rela);
3778 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3779 info->flags |= DF_TEXTREL;
3780 }
3781 }
3782 }
3783
3784 local_got = elf_local_got_refcounts (ibfd);
3785 if (!local_got)
3786 continue;
3787
3788 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3789 locsymcount = symtab_hdr->sh_info;
3790 end_local_got = local_got + locsymcount;
3791 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
3792 s = htab->root.sgot;
3793 srel = htab->root.srelgot;
3794 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3795 {
3796 if (*local_got > 0)
3797 {
3798 *local_got = s->size;
3799 if (*local_tls_type & GOT_TLS_GD)
3800 /* TLS_GD relocs need an 8-byte structure in the GOT. */
3801 s->size += 8;
3802 if (*local_tls_type & GOT_TLS_IE)
3803 s->size += 4;
3804 if (*local_tls_type == GOT_NORMAL)
3805 s->size += 4;
3806
3807 if (info->shared || *local_tls_type == GOT_TLS_GD)
3808 srel->size += sizeof (Elf32_External_Rela);
3809 }
3810 else
3811 *local_got = (bfd_vma) -1;
3812 }
3813 }
3814
3815 if (htab->tls_ldm_got.refcount > 0)
3816 {
3817 /* Allocate two GOT entries and one dynamic relocation (if necessary)
3818 for R_NIOS2_TLS_LDM16 relocations. */
3819 htab->tls_ldm_got.offset = htab->root.sgot->size;
3820 htab->root.sgot->size += 8;
3821 if (info->shared)
3822 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3823 }
3824 else
3825 htab->tls_ldm_got.offset = -1;
3826
3827 /* Allocate global sym .plt and .got entries, and space for global
3828 sym dynamic relocs. */
3829 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3830
3831 /* The check_relocs and adjust_dynamic_symbol entry points have
3832 determined the sizes of the various dynamic sections. Allocate
3833 memory for them. */
3834 plt = FALSE;
3835 got = FALSE;
3836 relocs = FALSE;
3837 for (s = dynobj->sections; s != NULL; s = s->next)
3838 {
3839 const char *name;
3840
3841 if ((s->flags & SEC_LINKER_CREATED) == 0)
3842 continue;
3843
3844 /* It's OK to base decisions on the section name, because none
3845 of the dynobj section names depend upon the input files. */
3846 name = bfd_get_section_name (dynobj, s);
3847
3848 if (strcmp (name, ".plt") == 0)
3849 {
3850 /* Remember whether there is a PLT. */
3851 plt = s->size != 0;
3852
3853 /* Correct for the number of res_N branches. */
3854 if (plt && !info->shared)
3855 {
3856 htab->res_n_size = (s->size-28) / 3;
3857 s->size += htab->res_n_size;
3858 }
3859 }
3860 else if (CONST_STRNEQ (name, ".rela"))
3861 {
3862 if (s->size != 0)
3863 {
3864 relocs = TRUE;
3865
3866 /* We use the reloc_count field as a counter if we need
3867 to copy relocs into the output file. */
3868 s->reloc_count = 0;
3869 }
3870 }
3871 else if (CONST_STRNEQ (name, ".got"))
3872 got = s->size != 0;
3873 else if (strcmp (name, ".dynbss") != 0)
3874 /* It's not one of our sections, so don't allocate space. */
3875 continue;
3876
3877 if (s->size == 0)
3878 {
3879 /* If we don't need this section, strip it from the
3880 output file. This is mostly to handle .rela.bss and
3881 .rela.plt. We must create both sections in
3882 create_dynamic_sections, because they must be created
3883 before the linker maps input sections to output
3884 sections. The linker does that before
3885 adjust_dynamic_symbol is called, and it is that
3886 function which decides whether anything needs to go
3887 into these sections. */
3888 s->flags |= SEC_EXCLUDE;
3889 continue;
3890 }
3891
3892 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3893 continue;
3894
3895 /* Allocate memory for the section contents. */
3896 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3897 Unused entries should be reclaimed before the section's contents
3898 are written out, but at the moment this does not happen. Thus in
3899 order to prevent writing out garbage, we initialize the section's
3900 contents to zero. */
3901 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3902 if (s->contents == NULL)
3903 return FALSE;
3904 }
3905
3906 /* Adjust dynamic symbols that point to the plt to account for the
3907 now-known number of resN slots. */
3908 if (htab->res_n_size)
3909 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
3910
3911 if (elf_hash_table (info)->dynamic_sections_created)
3912 {
3913 /* Add some entries to the .dynamic section. We fill in the
3914 values later, in elf_nios2_finish_dynamic_sections, but we
3915 must add the entries now so that we get the correct size for
3916 the .dynamic section. The DT_DEBUG entry is filled in by the
3917 dynamic linker and used by the debugger. */
3918#define add_dynamic_entry(TAG, VAL) \
3919 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3920
3921 if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0))
3922 return FALSE;
3923
3924 if (got && !add_dynamic_entry (DT_PLTGOT, 0))
3925 return FALSE;
3926
3927 if (plt
3928 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
3929 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3930 || !add_dynamic_entry (DT_JMPREL, 0)))
3931 return FALSE;
3932
3933 if (relocs
3934 && (!add_dynamic_entry (DT_RELA, 0)
3935 || !add_dynamic_entry (DT_RELASZ, 0)
3936 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
3937 return FALSE;
3938
3939 if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0))
3940 return FALSE;
3941
3942 if ((info->flags & DF_TEXTREL) != 0
3943 && !add_dynamic_entry (DT_TEXTREL, 0))
3944 return FALSE;
3945 }
3946#undef add_dynamic_entry
3947
3948 return TRUE;
3949}
3950
3951/* Implement bfd_elf32_bfd_link_hash_table_create. */
3952static struct bfd_link_hash_table *
3953nios2_elf32_link_hash_table_create (bfd *abfd)
3954{
3955 struct elf32_nios2_link_hash_table *ret;
3956 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
3957
7bf52ea2 3958 ret = bfd_zmalloc (amt);
36591ba1
SL
3959 if (ret == NULL)
3960 return NULL;
3961
3962 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3963 link_hash_newfunc,
3964 sizeof (struct
3965 elf32_nios2_link_hash_entry),
3966 NIOS2_ELF_DATA))
3967 {
3968 free (ret);
3969 return NULL;
3970 }
3971
36591ba1
SL
3972 return &ret->root.root;
3973}
3974
3975/* Implement elf_backend_reloc_type_class. */
3976static enum elf_reloc_type_class
7e612e98
AM
3977nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3978 const asection *rel_sec ATTRIBUTE_UNUSED,
3979 const Elf_Internal_Rela *rela)
36591ba1
SL
3980{
3981 switch ((int) ELF32_R_TYPE (rela->r_info))
3982 {
3983 case R_NIOS2_RELATIVE:
3984 return reloc_class_relative;
3985 case R_NIOS2_JUMP_SLOT:
3986 return reloc_class_plt;
3987 case R_NIOS2_COPY:
3988 return reloc_class_copy;
3989 default:
3990 return reloc_class_normal;
3991 }
3992}
3993
3994/* Return 1 if target is one of ours. */
3995static bfd_boolean
3996is_nios2_elf_target (const struct bfd_target *targ)
3997{
3998 return (targ == &bfd_elf32_littlenios2_vec
3999 || targ == &bfd_elf32_bignios2_vec);
4000}
4001
4002/* Implement elf_backend_add_symbol_hook.
4003 This hook is called by the linker when adding symbols from an object
4004 file. We use it to put .comm items in .sbss, and not .bss. */
4005static bfd_boolean
4006nios2_elf_add_symbol_hook (bfd *abfd,
4007 struct bfd_link_info *info,
4008 Elf_Internal_Sym *sym,
4009 const char **namep ATTRIBUTE_UNUSED,
4010 flagword *flagsp ATTRIBUTE_UNUSED,
4011 asection **secp,
4012 bfd_vma *valp)
4013{
4014 bfd *dynobj;
4015
4016 if (sym->st_shndx == SHN_COMMON
4017 && !info->relocatable
4018 && sym->st_size <= elf_gp_size (abfd)
4019 && is_nios2_elf_target (info->output_bfd->xvec))
4020 {
4021 /* Common symbols less than or equal to -G nn bytes are automatically
4022 put into .sbss. */
4023 struct elf32_nios2_link_hash_table *htab;
4024
4025 htab = elf32_nios2_hash_table (info);
4026 if (htab->sbss == NULL)
4027 {
4028 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
4029
4030 dynobj = elf_hash_table (info)->dynobj;
4031 if (!dynobj)
4032 dynobj = abfd;
4033
4034 htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
4035 flags);
4036 if (htab->sbss == NULL)
4037 return FALSE;
4038 }
4039
4040 *secp = htab->sbss;
4041 *valp = sym->st_size;
4042 }
4043
4044 return TRUE;
4045}
4046
4047/* Implement elf_backend_can_make_relative_eh_frame:
4048 Decide whether to attempt to turn absptr or lsda encodings in
4049 shared libraries into pcrel within the given input section. */
4050static bfd_boolean
4051nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
4052 struct bfd_link_info *info
4053 ATTRIBUTE_UNUSED,
4054 asection *eh_frame_section
4055 ATTRIBUTE_UNUSED)
4056{
4057 /* We can't use PC-relative encodings in the .eh_frame section. */
4058 return FALSE;
4059}
4060
4061/* Implement elf_backend_special_sections. */
4062const struct bfd_elf_special_section elf32_nios2_special_sections[] =
4063{
4064 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
4065 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4066 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
4067 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4068 { NULL, 0, 0, 0, 0 }
4069};
4070
4071#define ELF_ARCH bfd_arch_nios2
4072#define ELF_TARGET_ID NIOS2_ELF_DATA
4073#define ELF_MACHINE_CODE EM_ALTERA_NIOS2
4074
4075/* The Nios II MMU uses a 4K page size. */
4076
4077#define ELF_MAXPAGESIZE 0x1000
4078
4079#define bfd_elf32_bfd_link_hash_table_create \
4080 nios2_elf32_link_hash_table_create
4081
4082/* Relocation table lookup macros. */
4083
4084#define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
4085#define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
4086
4087/* JUMP_TABLE_LINK macros. */
4088
4089/* elf_info_to_howto (using RELA relocations). */
4090
4091#define elf_info_to_howto nios2_elf32_info_to_howto
4092
4093/* elf backend functions. */
4094
4095#define elf_backend_can_gc_sections 1
4096#define elf_backend_can_refcount 1
4097#define elf_backend_plt_readonly 1
4098#define elf_backend_want_got_plt 1
4099#define elf_backend_rela_normal 1
4100
4101#define elf_backend_relocate_section nios2_elf32_relocate_section
4102#define elf_backend_section_flags nios2_elf32_section_flags
4103#define elf_backend_fake_sections nios2_elf32_fake_sections
4104#define elf_backend_check_relocs nios2_elf32_check_relocs
4105
4106#define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
4107#define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
4108#define elf_backend_create_dynamic_sections \
4109 nios2_elf32_create_dynamic_sections
4110#define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
4111#define elf_backend_finish_dynamic_sections \
4112 nios2_elf32_finish_dynamic_sections
4113#define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
4114#define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
4115#define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
4116#define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
4117#define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
4118
4119#define elf_backend_grok_prstatus nios2_grok_prstatus
4120#define elf_backend_grok_psinfo nios2_grok_psinfo
4121
4122#undef elf_backend_can_make_relative_eh_frame
4123#define elf_backend_can_make_relative_eh_frame \
4124 nios2_elf32_can_make_relative_eh_frame
4125
4126#define elf_backend_special_sections elf32_nios2_special_sections
4127
4128#define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
4129#define TARGET_LITTLE_NAME "elf32-littlenios2"
4130#define TARGET_BIG_SYM bfd_elf32_bignios2_vec
4131#define TARGET_BIG_NAME "elf32-bignios2"
4132
4133#define elf_backend_got_header_size 12
4134
4135#include "elf32-target.h"
This page took 0.288677 seconds and 4 git commands to generate.