ELF: Copy dyn_relocs in _bfd_elf_link_hash_copy_indirect
[deliverable/binutils-gdb.git] / bfd / elf32-nios2.c
CommitLineData
36591ba1 1/* 32-bit ELF support for Nios II.
b3adc24a 2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
36591ba1
SL
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"
78058a5e 33#include "elf32-nios2.h"
f3185997 34#include "libiberty.h"
36591ba1
SL
35
36/* Use RELA relocations. */
37#ifndef USE_RELA
38#define USE_RELA
39#endif
40
41#ifdef USE_REL
42#undef USE_REL
43#endif
44
45/* Forward declarations. */
46static bfd_reloc_status_type nios2_elf32_ignore_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48static bfd_reloc_status_type nios2_elf32_hi16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50static bfd_reloc_status_type nios2_elf32_lo16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60static bfd_reloc_status_type nios2_elf32_call26_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62static bfd_reloc_status_type nios2_elf32_gprel_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64static bfd_reloc_status_type nios2_elf32_ujmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66static bfd_reloc_status_type nios2_elf32_cjmp_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68static bfd_reloc_status_type nios2_elf32_callr_relocate
69 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
70
71/* Target vector. */
6d00b590
AM
72extern const bfd_target nios2_elf32_le_vec;
73extern const bfd_target nios2_elf32_be_vec;
36591ba1
SL
74
75/* Offset of tp and dtp pointers from start of TLS block. */
76#define TP_OFFSET 0x7000
77#define DTP_OFFSET 0x8000
78
8c163c5a
SL
79/* The relocation tables used for SHT_REL sections. There are separate
80 tables for R1 and R2 encodings. */
81static reloc_howto_type elf_nios2_r1_howto_table_rel[] = {
36591ba1
SL
82 /* No relocation. */
83 HOWTO (R_NIOS2_NONE, /* type */
84 0, /* rightshift */
6346d5ca 85 3, /* size (0 = byte, 1 = short, 2 = long) */
36591ba1
SL
86 0, /* bitsize */
87 FALSE, /* pc_relative */
88 0, /* bitpos */
89 complain_overflow_dont, /* complain_on_overflow */
90 bfd_elf_generic_reloc, /* special_function */
91 "R_NIOS2_NONE", /* name */
92 FALSE, /* partial_inplace */
93 0, /* src_mask */
94 0, /* dst_mask */
95 FALSE), /* pcrel_offset */
96
97 /* 16-bit signed immediate relocation. */
98 HOWTO (R_NIOS2_S16, /* type */
99 0, /* rightshift */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
101 16, /* bitsize */
102 FALSE, /* pc_relative */
103 6, /* bitpos */
104 complain_overflow_signed, /* complain on overflow */
105 bfd_elf_generic_reloc, /* special function */
106 "R_NIOS2_S16", /* name */
107 FALSE, /* partial_inplace */
108 0x003fffc0, /* src_mask */
109 0x003fffc0, /* dest_mask */
110 FALSE), /* pcrel_offset */
111
112 /* 16-bit unsigned immediate relocation. */
113 HOWTO (R_NIOS2_U16, /* type */
114 0, /* rightshift */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
116 16, /* bitsize */
117 FALSE, /* pc_relative */
118 6, /* bitpos */
119 complain_overflow_unsigned, /* complain on overflow */
120 bfd_elf_generic_reloc, /* special function */
121 "R_NIOS2_U16", /* name */
122 FALSE, /* partial_inplace */
123 0x003fffc0, /* src_mask */
124 0x003fffc0, /* dest_mask */
125 FALSE), /* pcrel_offset */
126
127 HOWTO (R_NIOS2_PCREL16, /* type */
128 0, /* rightshift */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
130 16, /* bitsize */
131 TRUE, /* pc_relative */
132 6, /* bitpos */
133 complain_overflow_signed, /* complain on overflow */
134 nios2_elf32_pcrel16_relocate, /* special function */
135 "R_NIOS2_PCREL16", /* name */
136 FALSE, /* partial_inplace */
137 0x003fffc0, /* src_mask */
138 0x003fffc0, /* dest_mask */
139 TRUE), /* pcrel_offset */
140
141 HOWTO (R_NIOS2_CALL26, /* type */
142 2, /* rightshift */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
144 26, /* bitsize */
145 FALSE, /* pc_relative */
146 6, /* bitpos */
147 complain_overflow_dont, /* complain on overflow */
148 nios2_elf32_call26_relocate, /* special function */
149 "R_NIOS2_CALL26", /* name */
150 FALSE, /* partial_inplace */
151 0xffffffc0, /* src_mask */
152 0xffffffc0, /* dst_mask */
153 FALSE), /* pcrel_offset */
154
155 HOWTO (R_NIOS2_IMM5,
156 0,
157 2,
158 5,
159 FALSE,
160 6,
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
163 "R_NIOS2_IMM5",
164 FALSE,
165 0x000007c0,
166 0x000007c0,
167 FALSE),
168
169 HOWTO (R_NIOS2_CACHE_OPX,
170 0,
171 2,
172 5,
173 FALSE,
174 22,
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
177 "R_NIOS2_CACHE_OPX",
178 FALSE,
179 0x07c00000,
180 0x07c00000,
181 FALSE),
182
183 HOWTO (R_NIOS2_IMM6,
184 0,
185 2,
186 6,
187 FALSE,
188 6,
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
191 "R_NIOS2_IMM6",
192 FALSE,
193 0x00000fc0,
194 0x00000fc0,
195 FALSE),
196
197 HOWTO (R_NIOS2_IMM8,
198 0,
199 2,
200 8,
201 FALSE,
202 6,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
205 "R_NIOS2_IMM8",
206 FALSE,
207 0x00003fc0,
208 0x00003fc0,
209 FALSE),
210
211 HOWTO (R_NIOS2_HI16,
212 0,
213 2,
214 32,
215 FALSE,
216 6,
217 complain_overflow_dont,
218 nios2_elf32_hi16_relocate,
219 "R_NIOS2_HI16",
220 FALSE,
221 0x003fffc0,
222 0x003fffc0,
223 FALSE),
224
225 HOWTO (R_NIOS2_LO16,
226 0,
227 2,
228 32,
229 FALSE,
230 6,
231 complain_overflow_dont,
232 nios2_elf32_lo16_relocate,
233 "R_NIOS2_LO16",
234 FALSE,
235 0x003fffc0,
236 0x003fffc0,
237 FALSE),
238
239 HOWTO (R_NIOS2_HIADJ16,
240 0,
241 2,
242 32,
243 FALSE,
244 6,
245 complain_overflow_dont,
246 nios2_elf32_hiadj16_relocate,
247 "R_NIOS2_HIADJ16",
248 FALSE,
249 0x003fffc0,
250 0x003fffc0,
251 FALSE),
252
253 HOWTO (R_NIOS2_BFD_RELOC_32,
254 0,
255 2, /* long */
256 32,
257 FALSE,
258 0,
259 complain_overflow_dont,
260 bfd_elf_generic_reloc,
261 "R_NIOS2_BFD_RELOC32",
262 FALSE,
263 0xffffffff,
264 0xffffffff,
265 FALSE),
266
267 HOWTO (R_NIOS2_BFD_RELOC_16,
268 0,
269 1, /* short */
270 16,
271 FALSE,
272 0,
273 complain_overflow_bitfield,
274 bfd_elf_generic_reloc,
275 "R_NIOS2_BFD_RELOC16",
276 FALSE,
277 0x0000ffff,
278 0x0000ffff,
279 FALSE),
280
281 HOWTO (R_NIOS2_BFD_RELOC_8,
282 0,
283 0, /* byte */
284 8,
285 FALSE,
286 0,
287 complain_overflow_bitfield,
288 bfd_elf_generic_reloc,
289 "R_NIOS2_BFD_RELOC8",
290 FALSE,
291 0x000000ff,
292 0x000000ff,
293 FALSE),
294
295 HOWTO (R_NIOS2_GPREL,
296 0,
297 2,
298 32,
299 FALSE,
300 6,
301 complain_overflow_dont,
302 nios2_elf32_gprel_relocate,
303 "R_NIOS2_GPREL",
304 FALSE,
305 0x003fffc0,
306 0x003fffc0,
307 FALSE),
308
309 HOWTO (R_NIOS2_GNU_VTINHERIT,
310 0,
311 2, /* short */
312 0,
313 FALSE,
314 0,
315 complain_overflow_dont,
316 NULL,
317 "R_NIOS2_GNU_VTINHERIT",
318 FALSE,
319 0,
320 0,
321 FALSE),
322
323 HOWTO (R_NIOS2_GNU_VTENTRY,
324 0,
325 2, /* byte */
326 0,
327 FALSE,
328 0,
329 complain_overflow_dont,
330 _bfd_elf_rel_vtable_reloc_fn,
331 "R_NIOS2_GNU_VTENTRY",
332 FALSE,
333 0,
334 0,
335 FALSE),
336
337 HOWTO (R_NIOS2_UJMP,
338 0,
339 2,
340 32,
341 FALSE,
342 6,
343 complain_overflow_dont,
344 nios2_elf32_ujmp_relocate,
345 "R_NIOS2_UJMP",
346 FALSE,
347 0x003fffc0,
348 0x003fffc0,
349 FALSE),
350
351 HOWTO (R_NIOS2_CJMP,
352 0,
353 2,
354 32,
355 FALSE,
356 6,
357 complain_overflow_dont,
358 nios2_elf32_cjmp_relocate,
359 "R_NIOS2_CJMP",
360 FALSE,
361 0x003fffc0,
362 0x003fffc0,
363 FALSE),
364
365 HOWTO (R_NIOS2_CALLR,
366 0,
367 2,
368 32,
369 FALSE,
370 6,
371 complain_overflow_dont,
372 nios2_elf32_callr_relocate,
373 "R_NIOS2_CALLR",
374 FALSE,
375 0x003fffc0,
376 0x003fffc0,
377 FALSE),
378
379 HOWTO (R_NIOS2_ALIGN,
380 0,
381 2,
382 0,
383 FALSE,
384 0,
385 complain_overflow_dont,
386 nios2_elf32_ignore_reloc,
387 "R_NIOS2_ALIGN",
388 FALSE,
389 0,
390 0,
391 TRUE),
392
393
394 HOWTO (R_NIOS2_GOT16,
395 0,
396 2,
397 16,
398 FALSE,
399 6,
400 complain_overflow_bitfield,
401 bfd_elf_generic_reloc,
402 "R_NIOS2_GOT16",
403 FALSE,
404 0x003fffc0,
405 0x003fffc0,
406 FALSE),
407
408 HOWTO (R_NIOS2_CALL16,
409 0,
410 2,
411 16,
412 FALSE,
413 6,
414 complain_overflow_bitfield,
415 bfd_elf_generic_reloc,
416 "R_NIOS2_CALL16",
417 FALSE,
418 0x003fffc0,
419 0x003fffc0,
420 FALSE),
421
422 HOWTO (R_NIOS2_GOTOFF_LO,
423 0,
424 2,
425 16,
426 FALSE,
427 6,
428 complain_overflow_dont,
429 bfd_elf_generic_reloc,
430 "R_NIOS2_GOTOFF_LO",
431 FALSE,
432 0x003fffc0,
433 0x003fffc0,
434 FALSE),
435
436 HOWTO (R_NIOS2_GOTOFF_HA,
437 0,
438 2,
439 16,
440 FALSE,
441 6,
442 complain_overflow_dont,
443 bfd_elf_generic_reloc,
444 "R_NIOS2_GOTOFF_HA",
445 FALSE,
446 0x003fffc0,
447 0x003fffc0,
448 FALSE),
449
450 HOWTO (R_NIOS2_PCREL_LO,
451 0,
452 2,
453 16,
454 TRUE,
455 6,
456 complain_overflow_dont,
457 nios2_elf32_pcrel_lo16_relocate,
458 "R_NIOS2_PCREL_LO",
459 FALSE,
460 0x003fffc0,
461 0x003fffc0,
462 TRUE),
463
464 HOWTO (R_NIOS2_PCREL_HA,
465 0,
466 2,
467 16,
468 FALSE, /* This is a PC-relative relocation, but we need to subtract
469 PC ourselves before the HIADJ. */
470 6,
471 complain_overflow_dont,
472 nios2_elf32_pcrel_hiadj16_relocate,
473 "R_NIOS2_PCREL_HA",
474 FALSE,
475 0x003fffc0,
476 0x003fffc0,
477 TRUE),
478
479 HOWTO (R_NIOS2_TLS_GD16,
480 0,
481 2,
482 16,
483 FALSE,
484 6,
485 complain_overflow_bitfield,
486 bfd_elf_generic_reloc,
487 "R_NIOS2_TLS_GD16",
488 FALSE,
489 0x003fffc0,
490 0x003fffc0,
491 FALSE),
492
493 HOWTO (R_NIOS2_TLS_LDM16,
494 0,
495 2,
496 16,
497 FALSE,
498 6,
499 complain_overflow_bitfield,
500 bfd_elf_generic_reloc,
501 "R_NIOS2_TLS_LDM16",
502 FALSE,
503 0x003fffc0,
504 0x003fffc0,
505 FALSE),
506
507 HOWTO (R_NIOS2_TLS_LDO16,
508 0,
509 2,
510 16,
511 FALSE,
512 6,
513 complain_overflow_bitfield,
514 bfd_elf_generic_reloc,
515 "R_NIOS2_TLS_LDO16",
516 FALSE,
517 0x003fffc0,
518 0x003fffc0,
519 FALSE),
520
521 HOWTO (R_NIOS2_TLS_IE16,
522 0,
523 2,
524 16,
525 FALSE,
526 6,
527 complain_overflow_bitfield,
528 bfd_elf_generic_reloc,
529 "R_NIOS2_TLS_IE16",
530 FALSE,
531 0x003fffc0,
532 0x003fffc0,
533 FALSE),
534
535 HOWTO (R_NIOS2_TLS_LE16,
536 0,
537 2,
538 16,
539 FALSE,
540 6,
541 complain_overflow_bitfield,
542 bfd_elf_generic_reloc,
543 "R_NIOS2_TLS_LE16",
544 FALSE,
545 0x003fffc0,
546 0x003fffc0,
547 FALSE),
548
549 HOWTO (R_NIOS2_TLS_DTPMOD,
550 0,
551 2,
552 32,
553 FALSE,
554 0,
555 complain_overflow_dont,
556 bfd_elf_generic_reloc,
557 "R_NIOS2_TLS_DTPMOD",
558 FALSE,
559 0xffffffff,
560 0xffffffff,
561 FALSE),
562
563 HOWTO (R_NIOS2_TLS_DTPREL,
564 0,
565 2,
566 32,
567 FALSE,
568 0,
569 complain_overflow_dont,
570 bfd_elf_generic_reloc,
571 "R_NIOS2_TLS_DTPREL",
572 FALSE,
573 0xffffffff,
574 0xffffffff,
575 FALSE),
576
577 HOWTO (R_NIOS2_TLS_TPREL,
578 0,
579 2,
580 32,
581 FALSE,
582 0,
583 complain_overflow_dont,
584 bfd_elf_generic_reloc,
585 "R_NIOS2_TLS_TPREL",
586 FALSE,
587 0xffffffff,
588 0xffffffff,
589 FALSE),
590
591 HOWTO (R_NIOS2_COPY,
592 0,
593 2,
594 32,
595 FALSE,
596 0,
597 complain_overflow_dont,
598 bfd_elf_generic_reloc,
599 "R_NIOS2_COPY",
600 FALSE,
601 0,
602 0,
603 FALSE),
604
605 HOWTO (R_NIOS2_GLOB_DAT,
606 0,
607 2,
608 32,
609 FALSE,
610 0,
611 complain_overflow_dont,
612 bfd_elf_generic_reloc,
613 "R_NIOS2_GLOB_DAT",
614 FALSE,
615 0xffffffff,
616 0xffffffff,
617 FALSE),
618
619 HOWTO (R_NIOS2_JUMP_SLOT,
620 0,
621 2,
622 32,
623 FALSE,
624 0,
625 complain_overflow_dont,
626 bfd_elf_generic_reloc,
627 "R_NIOS2_JUMP_SLOT",
628 FALSE,
629 0xffffffff,
630 0xffffffff,
631 FALSE),
632
633 HOWTO (R_NIOS2_RELATIVE,
634 0,
635 2,
636 32,
637 FALSE,
638 0,
639 complain_overflow_dont,
640 bfd_elf_generic_reloc,
641 "R_NIOS2_RELATIVE",
642 FALSE,
643 0xffffffff,
644 0xffffffff,
645 FALSE),
646
647 HOWTO (R_NIOS2_GOTOFF,
648 0,
649 2,
650 32,
651 FALSE,
652 0,
653 complain_overflow_dont,
654 bfd_elf_generic_reloc,
655 "R_NIOS2_GOTOFF",
656 FALSE,
657 0xffffffff,
658 0xffffffff,
659 FALSE),
660
78058a5e
SL
661 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
662 2, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 26, /* bitsize */
665 FALSE, /* pc_relative */
666 6, /* bitpos */
667 complain_overflow_dont, /* complain on overflow */
668 nios2_elf32_call26_relocate, /* special function */
669 "R_NIOS2_CALL26_NOAT", /* name */
670 FALSE, /* partial_inplace */
671 0xffffffc0, /* src_mask */
672 0xffffffc0, /* dst_mask */
673 FALSE), /* pcrel_offset */
674
1c2de463
SL
675 HOWTO (R_NIOS2_GOT_LO,
676 0,
677 2,
678 16,
679 FALSE,
680 6,
681 complain_overflow_dont,
682 bfd_elf_generic_reloc,
683 "R_NIOS2_GOT_LO",
684 FALSE,
685 0x003fffc0,
686 0x003fffc0,
687 FALSE),
688
689 HOWTO (R_NIOS2_GOT_HA,
690 0,
691 2,
692 16,
693 FALSE,
694 6,
695 complain_overflow_dont,
696 bfd_elf_generic_reloc,
697 "R_NIOS2_GOT_HA",
698 FALSE,
699 0x003fffc0,
700 0x003fffc0,
701 FALSE),
702
703 HOWTO (R_NIOS2_CALL_LO,
704 0,
705 2,
706 16,
707 FALSE,
708 6,
709 complain_overflow_dont,
710 bfd_elf_generic_reloc,
711 "R_NIOS2_CALL_LO",
712 FALSE,
713 0x003fffc0,
714 0x003fffc0,
715 FALSE),
716
717 HOWTO (R_NIOS2_CALL_HA,
718 0,
719 2,
720 16,
721 FALSE,
722 6,
723 complain_overflow_dont,
724 bfd_elf_generic_reloc,
725 "R_NIOS2_CALL_HA",
726 FALSE,
727 0x003fffc0,
728 0x003fffc0,
729 FALSE),
730
36591ba1
SL
731/* Add other relocations here. */
732};
733
8c163c5a
SL
734static reloc_howto_type elf_nios2_r2_howto_table_rel[] = {
735 /* No relocation. */
736 HOWTO (R_NIOS2_NONE, /* type */
737 0, /* rightshift */
738 0, /* size (0 = byte, 1 = short, 2 = long) */
739 0, /* bitsize */
740 FALSE, /* pc_relative */
741 0, /* bitpos */
742 complain_overflow_dont, /* complain_on_overflow */
743 bfd_elf_generic_reloc, /* special_function */
744 "R_NIOS2_NONE", /* name */
745 FALSE, /* partial_inplace */
746 0, /* src_mask */
747 0, /* dst_mask */
748 FALSE), /* pcrel_offset */
749
750 /* 16-bit signed immediate relocation. */
751 HOWTO (R_NIOS2_S16, /* type */
752 0, /* rightshift */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
754 16, /* bitsize */
755 FALSE, /* pc_relative */
756 16, /* bitpos */
757 complain_overflow_signed, /* complain on overflow */
758 bfd_elf_generic_reloc, /* special function */
759 "R_NIOS2_S16", /* name */
760 FALSE, /* partial_inplace */
761 0xffff0000, /* src_mask */
762 0xffff0000, /* dest_mask */
763 FALSE), /* pcrel_offset */
764
765 /* 16-bit unsigned immediate relocation. */
766 HOWTO (R_NIOS2_U16, /* type */
767 0, /* rightshift */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
769 16, /* bitsize */
770 FALSE, /* pc_relative */
771 16, /* bitpos */
772 complain_overflow_unsigned, /* complain on overflow */
773 bfd_elf_generic_reloc, /* special function */
774 "R_NIOS2_U16", /* name */
775 FALSE, /* partial_inplace */
776 0xffff0000, /* src_mask */
777 0xffff0000, /* dest_mask */
778 FALSE), /* pcrel_offset */
779
780 HOWTO (R_NIOS2_PCREL16, /* type */
781 0, /* rightshift */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
783 16, /* bitsize */
784 TRUE, /* pc_relative */
785 16, /* bitpos */
786 complain_overflow_signed, /* complain on overflow */
787 nios2_elf32_pcrel16_relocate, /* special function */
788 "R_NIOS2_PCREL16", /* name */
789 FALSE, /* partial_inplace */
790 0xffff0000, /* src_mask */
791 0xffff0000, /* dest_mask */
792 TRUE), /* pcrel_offset */
793
794 HOWTO (R_NIOS2_CALL26, /* type */
795 2, /* rightshift */
796 2, /* size (0 = byte, 1 = short, 2 = long) */
797 26, /* bitsize */
798 FALSE, /* pc_relative */
799 6, /* bitpos */
800 complain_overflow_dont, /* complain on overflow */
801 nios2_elf32_call26_relocate, /* special function */
802 "R_NIOS2_CALL26", /* name */
803 FALSE, /* partial_inplace */
804 0xffffffc0, /* src_mask */
805 0xffffffc0, /* dst_mask */
806 FALSE), /* pcrel_offset */
807
808 HOWTO (R_NIOS2_IMM5,
809 0,
810 2,
811 5,
812 FALSE,
813 21,
814 complain_overflow_bitfield,
815 bfd_elf_generic_reloc,
816 "R_NIOS2_IMM5",
817 FALSE,
818 0x03e00000,
819 0x03e00000,
820 FALSE),
821
822 HOWTO (R_NIOS2_CACHE_OPX,
823 0,
824 2,
825 5,
826 FALSE,
827 11,
828 complain_overflow_bitfield,
829 bfd_elf_generic_reloc,
830 "R_NIOS2_CACHE_OPX",
831 FALSE,
832 0x0000f800,
833 0x0000f800,
834 FALSE),
835
836 HOWTO (R_NIOS2_IMM6,
837 0,
838 2,
839 6,
840 FALSE,
841 26,
842 complain_overflow_bitfield,
843 bfd_elf_generic_reloc,
844 "R_NIOS2_IMM6",
845 FALSE,
846 0xfc000000,
847 0xfc000000,
848 FALSE),
849
850 HOWTO (R_NIOS2_IMM8,
851 0,
852 2,
853 8,
854 FALSE,
855 24,
856 complain_overflow_bitfield,
857 bfd_elf_generic_reloc,
858 "R_NIOS2_IMM8",
859 FALSE,
860 0xff000000,
861 0xff000000,
862 FALSE),
863
864 HOWTO (R_NIOS2_HI16,
865 0,
866 2,
867 32,
868 FALSE,
869 16,
870 complain_overflow_dont,
871 nios2_elf32_hi16_relocate,
872 "R_NIOS2_HI16",
873 FALSE,
874 0xffff0000,
875 0xffff0000,
876 FALSE),
877
878 HOWTO (R_NIOS2_LO16,
879 0,
880 2,
881 32,
882 FALSE,
883 16,
884 complain_overflow_dont,
885 nios2_elf32_lo16_relocate,
886 "R_NIOS2_LO16",
887 FALSE,
888 0xffff0000,
889 0xffff0000,
890 FALSE),
891
892 HOWTO (R_NIOS2_HIADJ16,
893 0,
894 2,
895 32,
896 FALSE,
897 16,
898 complain_overflow_dont,
899 nios2_elf32_hiadj16_relocate,
900 "R_NIOS2_HIADJ16",
901 FALSE,
902 0xffff0000,
903 0xffff0000,
904 FALSE),
905
906 HOWTO (R_NIOS2_BFD_RELOC_32,
907 0,
908 2, /* long */
909 32,
910 FALSE,
911 0,
912 complain_overflow_dont,
913 bfd_elf_generic_reloc,
914 "R_NIOS2_BFD_RELOC32",
915 FALSE,
916 0xffffffff,
917 0xffffffff,
918 FALSE),
919
920 HOWTO (R_NIOS2_BFD_RELOC_16,
921 0,
922 1, /* short */
923 16,
924 FALSE,
925 0,
926 complain_overflow_bitfield,
927 bfd_elf_generic_reloc,
928 "R_NIOS2_BFD_RELOC16",
929 FALSE,
930 0x0000ffff,
931 0x0000ffff,
932 FALSE),
933
934 HOWTO (R_NIOS2_BFD_RELOC_8,
935 0,
936 0, /* byte */
937 8,
938 FALSE,
939 0,
940 complain_overflow_bitfield,
941 bfd_elf_generic_reloc,
942 "R_NIOS2_BFD_RELOC8",
943 FALSE,
944 0x000000ff,
945 0x000000ff,
946 FALSE),
947
948 HOWTO (R_NIOS2_GPREL,
949 0,
950 2,
951 32,
952 FALSE,
953 16,
954 complain_overflow_dont,
955 nios2_elf32_gprel_relocate,
956 "R_NIOS2_GPREL",
957 FALSE,
958 0xffff0000,
959 0xffff0000,
960 FALSE),
961
962 HOWTO (R_NIOS2_GNU_VTINHERIT,
963 0,
964 2, /* short */
965 0,
966 FALSE,
967 0,
968 complain_overflow_dont,
969 NULL,
970 "R_NIOS2_GNU_VTINHERIT",
971 FALSE,
972 0,
973 0,
974 FALSE),
975
976 HOWTO (R_NIOS2_GNU_VTENTRY,
977 0,
978 2, /* byte */
979 0,
980 FALSE,
981 0,
982 complain_overflow_dont,
983 _bfd_elf_rel_vtable_reloc_fn,
984 "R_NIOS2_GNU_VTENTRY",
985 FALSE,
986 0,
987 0,
988 FALSE),
989
990 HOWTO (R_NIOS2_UJMP,
991 0,
992 2,
993 32,
994 FALSE,
995 16,
996 complain_overflow_dont,
997 nios2_elf32_ujmp_relocate,
998 "R_NIOS2_UJMP",
999 FALSE,
1000 0xffff0000,
1001 0xffff0000,
1002 FALSE),
1003
1004 HOWTO (R_NIOS2_CJMP,
1005 0,
1006 2,
1007 32,
1008 FALSE,
1009 16,
1010 complain_overflow_dont,
1011 nios2_elf32_cjmp_relocate,
1012 "R_NIOS2_CJMP",
1013 FALSE,
1014 0xffff0000,
1015 0xffff0000,
1016 FALSE),
1017
1018 HOWTO (R_NIOS2_CALLR,
1019 0,
1020 2,
1021 32,
1022 FALSE,
1023 16,
1024 complain_overflow_dont,
1025 nios2_elf32_callr_relocate,
1026 "R_NIOS2_CALLR",
1027 FALSE,
1028 0xffff0000,
1029 0xffff0000,
1030 FALSE),
1031
1032 HOWTO (R_NIOS2_ALIGN,
1033 0,
1034 2,
1035 0,
1036 FALSE,
1037 0,
1038 complain_overflow_dont,
1039 nios2_elf32_ignore_reloc,
1040 "R_NIOS2_ALIGN",
1041 FALSE,
1042 0,
1043 0,
1044 TRUE),
1045
1046 HOWTO (R_NIOS2_GOT16,
1047 0,
1048 2,
1049 16,
1050 FALSE,
1051 16,
1052 complain_overflow_bitfield,
1053 bfd_elf_generic_reloc,
1054 "R_NIOS2_GOT16",
1055 FALSE,
1056 0xffff0000,
1057 0xffff0000,
1058 FALSE),
1059
1060 HOWTO (R_NIOS2_CALL16,
1061 0,
1062 2,
1063 16,
1064 FALSE,
1065 16,
1066 complain_overflow_bitfield,
1067 bfd_elf_generic_reloc,
1068 "R_NIOS2_CALL16",
1069 FALSE,
1070 0xffff0000,
1071 0xffff0000,
1072 FALSE),
1073
1074 HOWTO (R_NIOS2_GOTOFF_LO,
1075 0,
1076 2,
1077 16,
1078 FALSE,
1079 16,
1080 complain_overflow_dont,
1081 bfd_elf_generic_reloc,
1082 "R_NIOS2_GOTOFF_LO",
1083 FALSE,
1084 0xffff0000,
1085 0xffff0000,
1086 FALSE),
1087
1088 HOWTO (R_NIOS2_GOTOFF_HA,
1089 0,
1090 2,
1091 16,
1092 FALSE,
1093 16,
1094 complain_overflow_dont,
1095 bfd_elf_generic_reloc,
1096 "R_NIOS2_GOTOFF_HA",
1097 FALSE,
1098 0xffff0000,
1099 0xffff0000,
1100 FALSE),
1101
1102 HOWTO (R_NIOS2_PCREL_LO,
1103 0,
1104 2,
1105 16,
1106 TRUE,
1107 16,
1108 complain_overflow_dont,
1109 nios2_elf32_pcrel_lo16_relocate,
1110 "R_NIOS2_PCREL_LO",
1111 FALSE,
1112 0xffff0000,
1113 0xffff0000,
1114 TRUE),
1115
1116 HOWTO (R_NIOS2_PCREL_HA,
1117 0,
1118 2,
1119 16,
1120 FALSE, /* This is a PC-relative relocation, but we need to subtract
1121 PC ourselves before the HIADJ. */
1122 16,
1123 complain_overflow_dont,
1124 nios2_elf32_pcrel_hiadj16_relocate,
1125 "R_NIOS2_PCREL_HA",
1126 FALSE,
1127 0xffff0000,
1128 0xffff0000,
1129 TRUE),
1130
1131 HOWTO (R_NIOS2_TLS_GD16,
1132 0,
1133 2,
1134 16,
1135 FALSE,
1136 16,
1137 complain_overflow_bitfield,
1138 bfd_elf_generic_reloc,
1139 "R_NIOS2_TLS_GD16",
1140 FALSE,
1141 0xffff0000,
1142 0xffff0000,
1143 FALSE),
1144
1145 HOWTO (R_NIOS2_TLS_LDM16,
1146 0,
1147 2,
1148 16,
1149 FALSE,
1150 16,
1151 complain_overflow_bitfield,
1152 bfd_elf_generic_reloc,
1153 "R_NIOS2_TLS_LDM16",
1154 FALSE,
1155 0xffff0000,
1156 0xffff0000,
1157 FALSE),
1158
1159 HOWTO (R_NIOS2_TLS_LDO16,
1160 0,
1161 2,
1162 16,
1163 FALSE,
1164 16,
1165 complain_overflow_bitfield,
1166 bfd_elf_generic_reloc,
1167 "R_NIOS2_TLS_LDO16",
1168 FALSE,
1169 0xffff0000,
1170 0xffff0000,
1171 FALSE),
1172
1173 HOWTO (R_NIOS2_TLS_IE16,
1174 0,
1175 2,
1176 16,
1177 FALSE,
1178 16,
1179 complain_overflow_bitfield,
1180 bfd_elf_generic_reloc,
1181 "R_NIOS2_TLS_IE16",
1182 FALSE,
1183 0xffff0000,
1184 0xffff0000,
1185 FALSE),
1186
1187 HOWTO (R_NIOS2_TLS_LE16,
1188 0,
1189 2,
1190 16,
1191 FALSE,
1192 16,
1193 complain_overflow_bitfield,
1194 bfd_elf_generic_reloc,
1195 "R_NIOS2_TLS_LE16",
1196 FALSE,
1197 0xffff0000,
1198 0xffff0000,
1199 FALSE),
1200
1201 HOWTO (R_NIOS2_TLS_DTPMOD,
1202 0,
1203 2,
1204 32,
1205 FALSE,
1206 0,
1207 complain_overflow_dont,
1208 bfd_elf_generic_reloc,
1209 "R_NIOS2_TLS_DTPMOD",
1210 FALSE,
1211 0xffffffff,
1212 0xffffffff,
1213 FALSE),
1214
1215 HOWTO (R_NIOS2_TLS_DTPREL,
1216 0,
1217 2,
1218 32,
1219 FALSE,
1220 0,
1221 complain_overflow_dont,
1222 bfd_elf_generic_reloc,
1223 "R_NIOS2_TLS_DTPREL",
1224 FALSE,
1225 0xffffffff,
1226 0xffffffff,
1227 FALSE),
1228
1229 HOWTO (R_NIOS2_TLS_TPREL,
1230 0,
1231 2,
1232 32,
1233 FALSE,
1234 0,
1235 complain_overflow_dont,
1236 bfd_elf_generic_reloc,
1237 "R_NIOS2_TLS_TPREL",
1238 FALSE,
1239 0xffffffff,
1240 0xffffffff,
1241 FALSE),
1242
1243 HOWTO (R_NIOS2_COPY,
1244 0,
1245 2,
1246 32,
1247 FALSE,
1248 0,
1249 complain_overflow_dont,
1250 bfd_elf_generic_reloc,
1251 "R_NIOS2_COPY",
1252 FALSE,
1253 0,
1254 0,
1255 FALSE),
1256
1257 HOWTO (R_NIOS2_GLOB_DAT,
1258 0,
1259 2,
1260 32,
1261 FALSE,
1262 0,
1263 complain_overflow_dont,
1264 bfd_elf_generic_reloc,
1265 "R_NIOS2_GLOB_DAT",
1266 FALSE,
1267 0xffffffff,
1268 0xffffffff,
1269 FALSE),
1270
1271 HOWTO (R_NIOS2_JUMP_SLOT,
1272 0,
1273 2,
1274 32,
1275 FALSE,
1276 0,
1277 complain_overflow_dont,
1278 bfd_elf_generic_reloc,
1279 "R_NIOS2_JUMP_SLOT",
1280 FALSE,
1281 0xffffffff,
1282 0xffffffff,
1283 FALSE),
1284
1285 HOWTO (R_NIOS2_RELATIVE,
1286 0,
1287 2,
1288 32,
1289 FALSE,
1290 0,
1291 complain_overflow_dont,
1292 bfd_elf_generic_reloc,
1293 "R_NIOS2_RELATIVE",
1294 FALSE,
1295 0xffffffff,
1296 0xffffffff,
1297 FALSE),
1298
1299 HOWTO (R_NIOS2_GOTOFF,
1300 0,
1301 2,
1302 32,
1303 FALSE,
1304 0,
1305 complain_overflow_dont,
1306 bfd_elf_generic_reloc,
1307 "R_NIOS2_GOTOFF",
1308 FALSE,
1309 0xffffffff,
1310 0xffffffff,
1311 FALSE),
1312
1313 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
1314 2, /* rightshift */
1315 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 26, /* bitsize */
1317 FALSE, /* pc_relative */
1318 6, /* bitpos */
1319 complain_overflow_dont, /* complain on overflow */
1320 nios2_elf32_call26_relocate, /* special function */
1321 "R_NIOS2_CALL26_NOAT", /* name */
1322 FALSE, /* partial_inplace */
1323 0xffffffc0, /* src_mask */
1324 0xffffffc0, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1326
1327 HOWTO (R_NIOS2_GOT_LO,
1328 0,
1329 2,
1330 16,
1331 FALSE,
1332 16,
1333 complain_overflow_dont,
1334 bfd_elf_generic_reloc,
1335 "R_NIOS2_GOT_LO",
1336 FALSE,
1337 0xffff0000,
1338 0xffff0000,
1339 FALSE),
1340
1341 HOWTO (R_NIOS2_GOT_HA,
1342 0,
1343 2,
1344 16,
1345 FALSE,
1346 16,
1347 complain_overflow_dont,
1348 bfd_elf_generic_reloc,
1349 "R_NIOS2_GOT_HA",
1350 FALSE,
1351 0xffff0000,
1352 0xffff0000,
1353 FALSE),
1354
1355 HOWTO (R_NIOS2_CALL_LO,
1356 0,
1357 2,
1358 16,
1359 FALSE,
1360 16,
1361 complain_overflow_dont,
1362 bfd_elf_generic_reloc,
1363 "R_NIOS2_CALL_LO",
1364 FALSE,
1365 0xffff0000,
1366 0xffff0000,
1367 FALSE),
1368
1369 HOWTO (R_NIOS2_CALL_HA,
1370 0,
1371 2,
1372 16,
1373 FALSE,
1374 16,
1375 complain_overflow_dont,
1376 bfd_elf_generic_reloc,
1377 "R_NIOS2_CALL_HA",
1378 FALSE,
1379 0xffff0000,
1380 0xffff0000,
1381 FALSE),
1382
1383 HOWTO (R_NIOS2_R2_S12,
1384 0,
1385 2,
1386 12,
1387 FALSE,
1388 16,
1389 complain_overflow_signed,
1390 bfd_elf_generic_reloc,
1391 "R_NIOS2_R2_S12",
1392 FALSE,
1393 0x0fff0000,
1394 0x0fff0000,
1395 FALSE),
1396
1397 HOWTO (R_NIOS2_R2_I10_1_PCREL,
1398 1,
1399 1,
1400 10,
1401 TRUE,
1402 6,
1403 complain_overflow_signed,
07d6d2b8 1404 bfd_elf_generic_reloc, /* FIXME? */
8c163c5a
SL
1405 "R_NIOS2_R2_I10_1_PCREL",
1406 FALSE,
1407 0xffc0,
1408 0xffc0,
1409 TRUE),
1410
1411 HOWTO (R_NIOS2_R2_T1I7_1_PCREL,
1412 1,
1413 1,
1414 7,
1415 TRUE,
1416 9,
1417 complain_overflow_signed,
1418 bfd_elf_generic_reloc, /* FIXME? */
1419 "R_NIOS2_R2_T1I7_1_PCREL",
1420 FALSE,
1421 0xfe00,
1422 0xfe00,
1423 TRUE),
1b786873 1424
8c163c5a
SL
1425 HOWTO (R_NIOS2_R2_T1I7_2,
1426 2,
1427 1,
1428 7,
1429 FALSE,
1430 9,
1431 complain_overflow_unsigned,
1432 bfd_elf_generic_reloc,
1433 "R_NIOS2_R2_T1I7_2",
1434 FALSE,
1435 0xfe00,
1436 0xfe00,
1437 FALSE),
1b786873 1438
8c163c5a
SL
1439 HOWTO (R_NIOS2_R2_T2I4,
1440 0,
1441 1,
1442 4,
1443 FALSE,
1444 12,
1445 complain_overflow_unsigned,
1446 bfd_elf_generic_reloc,
1447 "R_NIOS2_R2_T2I4",
1448 FALSE,
1449 0xf000,
1450 0xf000,
1451 FALSE),
1452
1453 HOWTO (R_NIOS2_R2_T2I4_1,
1454 1,
1455 1,
1456 4,
1457 FALSE,
1458 12,
1459 complain_overflow_unsigned,
1460 bfd_elf_generic_reloc,
1461 "R_NIOS2_R2_T2I4_1",
1462 FALSE,
1463 0xf000,
1464 0xf000,
1465 FALSE),
1466
1467 HOWTO (R_NIOS2_R2_T2I4_2,
1468 2,
1469 1,
1470 4,
1471 FALSE,
1472 12,
1473 complain_overflow_unsigned,
1474 bfd_elf_generic_reloc,
1475 "R_NIOS2_R2_T2I4_2",
1476 FALSE,
1477 0xf000,
1478 0xf000,
1479 FALSE),
1480
1481 HOWTO (R_NIOS2_R2_X1I7_2,
1482 2,
1483 1,
1484 7,
1485 FALSE,
1486 6,
1487 complain_overflow_unsigned,
1488 bfd_elf_generic_reloc,
1489 "R_NIOS2_R2_X1I7_2",
1490 FALSE,
1491 0x1fc0,
1492 0x1fc0,
1493 FALSE),
1b786873 1494
8c163c5a
SL
1495 HOWTO (R_NIOS2_R2_X2L5,
1496 0,
1497 1,
1498 5,
1499 FALSE,
1500 6,
1501 complain_overflow_unsigned,
1502 bfd_elf_generic_reloc,
1503 "R_NIOS2_R2_X2L5",
1504 FALSE,
1505 0x07c0,
1506 0x07c0,
1507 FALSE),
1b786873 1508
8c163c5a
SL
1509 HOWTO (R_NIOS2_R2_F1I5_2,
1510 2,
1511 1,
1512 5,
1513 FALSE,
1514 6,
1515 complain_overflow_unsigned,
1516 bfd_elf_generic_reloc,
1517 "R_NIOS2_R2_F1L5_2",
1518 FALSE,
1519 0x07c0,
1520 0x07c0,
1521 FALSE),
1522
1523 HOWTO (R_NIOS2_R2_L5I4X1,
1524 2,
1525 1,
1526 4,
1527 FALSE,
1528 6,
1529 complain_overflow_unsigned,
1530 bfd_elf_generic_reloc,
1531 "R_NIOS2_R2_L5I4X1",
1532 FALSE,
1533 0x03c0,
1534 0x03c0,
1535 FALSE),
1536
1537 HOWTO (R_NIOS2_R2_T1X1I6,
1538 0,
1539 1,
1540 6,
1541 FALSE,
1542 9,
1543 complain_overflow_unsigned,
1544 bfd_elf_generic_reloc,
1545 "R_NIOS2_R2_T1X1I6",
1546 FALSE,
1547 0x7e00,
1548 0x7e00,
1549 FALSE),
1b786873 1550
8c163c5a
SL
1551 HOWTO (R_NIOS2_R2_T1X1I6_2,
1552 2,
1553 2,
1554 6,
1555 FALSE,
1556 9,
1557 complain_overflow_unsigned,
1558 bfd_elf_generic_reloc,
1559 "R_NIOS2_R2_T1I1X6_2",
1560 FALSE,
1561 0x7e00,
1562 0x7e00,
1563 FALSE),
1b786873 1564
8c163c5a
SL
1565/* Add other relocations here. */
1566};
1567
36591ba1
SL
1568static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
1569
8c163c5a
SL
1570
1571/* Return true if producing output for a R2 BFD. */
1572#define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
1573
36591ba1
SL
1574/* Return the howto for relocation RTYPE. */
1575static reloc_howto_type *
8c163c5a 1576lookup_howto (unsigned int rtype, bfd *abfd)
36591ba1
SL
1577{
1578 static int initialized = 0;
1579 int i;
8c163c5a
SL
1580 /* R2 relocations are a superset of R1, so use that for the lookup
1581 table. */
f3185997
NC
1582 int r1_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
1583 int r2_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
36591ba1
SL
1584
1585 if (!initialized)
1586 {
1587 initialized = 1;
1588 memset (elf_code_to_howto_index, 0xff,
1589 sizeof (elf_code_to_howto_index));
8c163c5a
SL
1590 for (i = 0; i < r2_howto_tbl_size; i++)
1591 {
1592 elf_code_to_howto_index[elf_nios2_r2_howto_table_rel[i].type] = i;
1593 if (i < r1_howto_tbl_size)
1594 BFD_ASSERT (elf_nios2_r2_howto_table_rel[i].type
1595 == elf_nios2_r1_howto_table_rel[i].type);
1596 }
36591ba1
SL
1597 }
1598
f3185997
NC
1599 if (rtype > R_NIOS2_ILLEGAL)
1600 return NULL;
36591ba1 1601 i = elf_code_to_howto_index[rtype];
8c163c5a
SL
1602 if (BFD_IS_R2 (abfd))
1603 {
1604 if (i >= r2_howto_tbl_size)
f3185997 1605 return NULL;
8c163c5a
SL
1606 return elf_nios2_r2_howto_table_rel + i;
1607 }
1608 else
1609 {
1610 if (i >= r1_howto_tbl_size)
f3185997 1611 return NULL;
8c163c5a
SL
1612 return elf_nios2_r1_howto_table_rel + i;
1613 }
36591ba1
SL
1614}
1615
1616/* Map for converting BFD reloc types to Nios II reloc types. */
1617struct elf_reloc_map
1618{
1619 bfd_reloc_code_real_type bfd_val;
1620 enum elf_nios2_reloc_type elf_val;
1621};
1622
f3185997
NC
1623static const struct elf_reloc_map nios2_reloc_map[] =
1624{
0ba38529 1625 {BFD_RELOC_NONE, R_NIOS2_NONE},
36591ba1
SL
1626 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
1627 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
1628 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
1629 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
1630 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
1631 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
1632 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
1633 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
1634 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
1635 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
1636 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
1637 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
1638 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
1639 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
1640 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
1641 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
1642 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
1643 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
1644 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
1645 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
1646 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
1647 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
1648 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
1649 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
1650 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
1651 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
1652 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
1653 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
1654 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
1655 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
1656 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
1657 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
1658 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
1659 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
1660 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
1661 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
1662 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
1663 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
1664 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
78058a5e
SL
1665 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF},
1666 {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT},
1c2de463
SL
1667 {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO},
1668 {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA},
1669 {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO},
1670 {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA},
8c163c5a
SL
1671 {BFD_RELOC_NIOS2_R2_S12, R_NIOS2_R2_S12},
1672 {BFD_RELOC_NIOS2_R2_I10_1_PCREL, R_NIOS2_R2_I10_1_PCREL},
1673 {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, R_NIOS2_R2_T1I7_1_PCREL},
1674 {BFD_RELOC_NIOS2_R2_T1I7_2, R_NIOS2_R2_T1I7_2},
1675 {BFD_RELOC_NIOS2_R2_T2I4, R_NIOS2_R2_T2I4},
1676 {BFD_RELOC_NIOS2_R2_T2I4_1, R_NIOS2_R2_T2I4_1},
1677 {BFD_RELOC_NIOS2_R2_T2I4_2, R_NIOS2_R2_T2I4_2},
1678 {BFD_RELOC_NIOS2_R2_X1I7_2, R_NIOS2_R2_X1I7_2},
1679 {BFD_RELOC_NIOS2_R2_X2L5, R_NIOS2_R2_X2L5},
1680 {BFD_RELOC_NIOS2_R2_F1I5_2, R_NIOS2_R2_F1I5_2},
1681 {BFD_RELOC_NIOS2_R2_L5I4X1, R_NIOS2_R2_L5I4X1},
1682 {BFD_RELOC_NIOS2_R2_T1X1I6, R_NIOS2_R2_T1X1I6},
1683 {BFD_RELOC_NIOS2_R2_T1X1I6_2, R_NIOS2_R2_T1X1I6_2},
78058a5e
SL
1684};
1685
1686enum elf32_nios2_stub_type
1687{
1688 nios2_stub_call26_before,
1689 nios2_stub_call26_after,
1690 nios2_stub_none
1691};
1692
1693struct elf32_nios2_stub_hash_entry
1694{
1695 /* Base hash table entry structure. */
1696 struct bfd_hash_entry bh_root;
1697
1698 /* The stub section. */
1699 asection *stub_sec;
1700
1701 /* Offset within stub_sec of the beginning of this stub. */
1702 bfd_vma stub_offset;
1703
1704 /* Given the symbol's value and its section we can determine its final
1705 value when building the stubs (so the stub knows where to jump. */
1706 bfd_vma target_value;
1707 asection *target_section;
1708
1709 enum elf32_nios2_stub_type stub_type;
1710
1711 /* The symbol table entry, if any, that this was derived from. */
1712 struct elf32_nios2_link_hash_entry *hh;
1713
1714 /* And the reloc addend that this was derived from. */
1715 bfd_vma addend;
1716
1717 /* Where this stub is being called from, or, in the case of combined
1718 stub sections, the first input section in the group. */
1719 asection *id_sec;
36591ba1
SL
1720};
1721
78058a5e
SL
1722#define nios2_stub_hash_entry(ent) \
1723 ((struct elf32_nios2_stub_hash_entry *)(ent))
1724
1725#define nios2_stub_hash_lookup(table, string, create, copy) \
1726 ((struct elf32_nios2_stub_hash_entry *) \
1727 bfd_hash_lookup ((table), (string), (create), (copy)))
1728
1729
36591ba1
SL
1730/* Nios II ELF linker hash entry. */
1731
1732struct elf32_nios2_link_hash_entry
1733{
1734 struct elf_link_hash_entry root;
1735
78058a5e
SL
1736 /* A pointer to the most recently used stub hash entry against this
1737 symbol. */
1738 struct elf32_nios2_stub_hash_entry *hsh_cache;
1739
36591ba1
SL
1740#define GOT_UNKNOWN 0
1741#define GOT_NORMAL 1
1742#define GOT_TLS_GD 2
1743#define GOT_TLS_IE 4
1744 unsigned char tls_type;
1745
1746 /* We need to detect and take special action for symbols which are only
1747 referenced with %call() and not with %got(). Such symbols do not need
1748 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
1749 linking will not work if the dynamic GOT reloc exists.
1750 To check for this condition efficiently, we compare got_types_used against
1c2de463
SL
1751 CALL_USED, meaning
1752 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1753 */
1754#define GOT_USED 1
1755#define CALL_USED 2
36591ba1
SL
1756 unsigned char got_types_used;
1757};
1758
1759#define elf32_nios2_hash_entry(ent) \
1760 ((struct elf32_nios2_link_hash_entry *) (ent))
1761
1762/* Get the Nios II elf linker hash table from a link_info structure. */
1763#define elf32_nios2_hash_table(info) \
1764 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1765
1766/* Nios II ELF linker hash table. */
1767struct elf32_nios2_link_hash_table
1768 {
1769 /* The main hash table. */
1770 struct elf_link_hash_table root;
1771
78058a5e
SL
1772 /* The stub hash table. */
1773 struct bfd_hash_table bstab;
1774
1775 /* Linker stub bfd. */
1776 bfd *stub_bfd;
1777
1778 /* Linker call-backs. */
1779 asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
1780 void (*layout_sections_again) (void);
1781
1782 /* Array to keep track of which stub sections have been created, and
1783 information on stub grouping. */
1784 struct map_stub
1785 {
1786 /* These are the section to which stubs in the group will be
1787 attached. */
1788 asection *first_sec, *last_sec;
1789 /* The stub sections. There might be stubs inserted either before
1790 or after the real section.*/
1791 asection *first_stub_sec, *last_stub_sec;
1792 } *stub_group;
1793
1794 /* Assorted information used by nios2_elf32_size_stubs. */
1795 unsigned int bfd_count;
7292b3ac 1796 unsigned int top_index;
78058a5e
SL
1797 asection **input_list;
1798 Elf_Internal_Sym **all_local_syms;
1799
36591ba1 1800 /* Short-cuts to get to dynamic linker sections. */
36591ba1
SL
1801 asection *sbss;
1802
82e91538
SL
1803 /* GOT pointer symbol _gp_got. */
1804 struct elf_link_hash_entry *h_gp_got;
1805
36591ba1
SL
1806 union {
1807 bfd_signed_vma refcount;
1808 bfd_vma offset;
1809 } tls_ldm_got;
1810
1811 /* Small local sym cache. */
1812 struct sym_cache sym_cache;
1813
1814 bfd_vma res_n_size;
1815 };
1816
1817struct nios2_elf32_obj_tdata
1818{
1819 struct elf_obj_tdata root;
1820
1821 /* tls_type for each local got entry. */
1822 char *local_got_tls_type;
1823
1824 /* TRUE if TLS GD relocs have been seen for this object. */
1825 bfd_boolean has_tlsgd;
1826};
1827
1828#define elf32_nios2_tdata(abfd) \
1829 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1830
1831#define elf32_nios2_local_got_tls_type(abfd) \
1832 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1833
1834/* The name of the dynamic interpreter. This is put in the .interp
1835 section. */
1836#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1837
1838/* PLT implementation for position-dependent code. */
1839static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1840 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1841 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1842 0x7800683a /* jmp r15 */
1843};
1844
1845static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1846 0x03800034, /* movhi r14, %hiadj(res_0) */
1847 0x73800004, /* addi r14, r14, %lo(res_0) */
1848 0x7b9fc83a, /* sub r15, r15, r14 */
1849 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1850 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1851 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1852 0x6800683a /* jmp r13 */
1853};
1854
1855/* PLT implementation for position-independent code. */
1856static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1857 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1858 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1859 0x00000006 /* br .PLTresolve */
1860};
1861
1862static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1863 0x001ce03a, /* nextpc r14 */
1864 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1865 0x6b9b883a, /* add r13, r13, r14 */
1866 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1867 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1868 0x6800683a /* jmp r13 */
1869};
1870
78058a5e
SL
1871/* CALL26 stub. */
1872static const bfd_vma nios2_call26_stub_entry[] = {
1873 0x00400034, /* orhi at, r0, %hiadj(dest) */
1874 0x08400004, /* addi at, at, %lo(dest) */
1875 0x0800683a /* jmp at */
1876};
1877
1878/* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1879static void
1880nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1881{
1882 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1883
83da6e74 1884 BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
78058a5e
SL
1885
1886 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1887 sec->contents + offset);
1888}
1889
1890/* Install COUNT 32-bit values DATA starting at offset OFFSET into
1891 section SEC. */
1892static void
1893nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1894 int count)
1895{
1896 while (count--)
1897 {
1898 bfd_put_32 (sec->owner, *data, sec->contents + offset);
1899 offset += 4;
1900 ++data;
1901 }
1902}
1903
1904/* The usual way of loading a 32-bit constant into a Nios II register is to
1905 load the high 16 bits in one instruction and then add the low 16 bits with
1906 a signed add. This means that the high halfword needs to be adjusted to
1907 compensate for the sign bit of the low halfword. This function returns the
1908 adjusted high halfword for a given 32-bit constant. */
1909static
1910bfd_vma hiadj (bfd_vma symbol_value)
1911{
1912 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1913}
1914
36591ba1
SL
1915/* Implement elf_backend_grok_prstatus:
1916 Support for core dump NOTE sections. */
1917static bfd_boolean
1918nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1919{
1920 int offset;
1921 size_t size;
1922
1923 switch (note->descsz)
1924 {
1925 default:
1926 return FALSE;
1927
1928 case 212: /* Linux/Nios II */
1929 /* pr_cursig */
228e534f 1930 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
36591ba1
SL
1931
1932 /* pr_pid */
228e534f 1933 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
36591ba1
SL
1934
1935 /* pr_reg */
1936 offset = 72;
1937 size = 136;
1938
1939 break;
1940 }
1941
1942 /* Make a ".reg/999" section. */
1943 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1944 size, note->descpos + offset);
1945}
1946
1947/* Implement elf_backend_grok_psinfo. */
1948static bfd_boolean
1949nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1950{
1951 switch (note->descsz)
1952 {
1953 default:
1954 return FALSE;
1955
1956 case 124: /* Linux/Nios II elf_prpsinfo */
228e534f 1957 elf_tdata (abfd)->core->program
36591ba1 1958 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 1959 elf_tdata (abfd)->core->command
36591ba1
SL
1960 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1961 }
1962
1963 /* Note that for some reason, a spurious space is tacked
1964 onto the end of the args in some (at least one anyway)
1965 implementations, so strip it off if it exists. */
1966
1967 {
228e534f 1968 char *command = elf_tdata (abfd)->core->command;
36591ba1
SL
1969 int n = strlen (command);
1970
1971 if (0 < n && command[n - 1] == ' ')
1972 command[n - 1] = '\0';
1973 }
1974
1975 return TRUE;
1976}
1977
78058a5e
SL
1978/* Assorted hash table functions. */
1979
1980/* Initialize an entry in the stub hash table. */
1981static struct bfd_hash_entry *
1982stub_hash_newfunc (struct bfd_hash_entry *entry,
1983 struct bfd_hash_table *table,
1984 const char *string)
1985{
1986 /* Allocate the structure if it has not already been allocated by a
1987 subclass. */
1988 if (entry == NULL)
1989 {
1990 entry = bfd_hash_allocate (table,
1991 sizeof (struct elf32_nios2_stub_hash_entry));
1992 if (entry == NULL)
1993 return entry;
1994 }
1995
1996 /* Call the allocation method of the superclass. */
1997 entry = bfd_hash_newfunc (entry, table, string);
1998 if (entry != NULL)
1999 {
2000 struct elf32_nios2_stub_hash_entry *hsh;
2001
2002 /* Initialize the local fields. */
2003 hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2004 hsh->stub_sec = NULL;
2005 hsh->stub_offset = 0;
2006 hsh->target_value = 0;
2007 hsh->target_section = NULL;
2008 hsh->stub_type = nios2_stub_none;
2009 hsh->hh = NULL;
2010 hsh->id_sec = NULL;
2011 }
2012
2013 return entry;
2014}
2015
36591ba1
SL
2016/* Create an entry in a Nios II ELF linker hash table. */
2017static struct bfd_hash_entry *
2018link_hash_newfunc (struct bfd_hash_entry *entry,
2019 struct bfd_hash_table *table, const char *string)
2020{
2021 /* Allocate the structure if it has not already been allocated by a
2022 subclass. */
2023 if (entry == NULL)
2024 {
2025 entry = bfd_hash_allocate (table,
2026 sizeof (struct elf32_nios2_link_hash_entry));
2027 if (entry == NULL)
2028 return entry;
2029 }
2030
2031 /* Call the allocation method of the superclass. */
2032 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2033 if (entry)
2034 {
2035 struct elf32_nios2_link_hash_entry *eh;
2036
2037 eh = (struct elf32_nios2_link_hash_entry *) entry;
78058a5e 2038 eh->hsh_cache = NULL;
36591ba1
SL
2039 eh->tls_type = GOT_UNKNOWN;
2040 eh->got_types_used = 0;
2041 }
2042
2043 return entry;
2044}
2045
78058a5e
SL
2046/* Section name for stubs is the associated section name plus this
2047 string. */
2048#define STUB_SUFFIX ".stub"
2049
2050/* Build a name for an entry in the stub hash table. */
2051static char *
2052nios2_stub_name (const asection *input_section,
2053 const asection *sym_sec,
2054 const struct elf32_nios2_link_hash_entry *hh,
2055 const Elf_Internal_Rela *rel,
2056 enum elf32_nios2_stub_type stub_type)
2057{
2058 char *stub_name;
2059 bfd_size_type len;
2060 char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2061
2062 if (hh)
2063 {
2064 len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2065 stub_name = bfd_malloc (len);
2066 if (stub_name != NULL)
2067 {
2068 sprintf (stub_name, "%08x_%c_%s+%x",
2069 input_section->id & 0xffffffff,
2070 stubpos,
2071 hh->root.root.root.string,
2072 (int) rel->r_addend & 0xffffffff);
2073 }
2074 }
2075 else
2076 {
2077 len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2078 stub_name = bfd_malloc (len);
2079 if (stub_name != NULL)
2080 {
2081 sprintf (stub_name, "%08x_%c_%x:%x+%x",
2082 input_section->id & 0xffffffff,
2083 stubpos,
2084 sym_sec->id & 0xffffffff,
2085 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2086 (int) rel->r_addend & 0xffffffff);
2087 }
2088 }
2089 return stub_name;
2090}
2091
2092/* Look up an entry in the stub hash. Stub entries are cached because
2093 creating the stub name takes a bit of time. */
2094static struct elf32_nios2_stub_hash_entry *
2095nios2_get_stub_entry (const asection *input_section,
2096 const asection *sym_sec,
2097 struct elf32_nios2_link_hash_entry *hh,
2098 const Elf_Internal_Rela *rel,
2099 struct elf32_nios2_link_hash_table *htab,
2100 enum elf32_nios2_stub_type stub_type)
2101{
2102 struct elf32_nios2_stub_hash_entry *hsh;
2103 const asection *id_sec;
2104
2105 /* If this input section is part of a group of sections sharing one
2106 stub section, then use the id of the first/last section in the group,
2107 depending on the stub section placement relative to the group.
2108 Stub names need to include a section id, as there may well be
2109 more than one stub used to reach say, printf, and we need to
2110 distinguish between them. */
2111 if (stub_type == nios2_stub_call26_before)
2112 id_sec = htab->stub_group[input_section->id].first_sec;
2113 else
2114 id_sec = htab->stub_group[input_section->id].last_sec;
2115
2116 if (hh != NULL && hh->hsh_cache != NULL
2117 && hh->hsh_cache->hh == hh
2118 && hh->hsh_cache->id_sec == id_sec
2119 && hh->hsh_cache->stub_type == stub_type)
2120 {
2121 hsh = hh->hsh_cache;
2122 }
2123 else
2124 {
2125 char *stub_name;
2126
2127 stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2128 if (stub_name == NULL)
2129 return NULL;
2130
2131 hsh = nios2_stub_hash_lookup (&htab->bstab,
2132 stub_name, FALSE, FALSE);
2133
2134 if (hh != NULL)
2135 hh->hsh_cache = hsh;
2136
2137 free (stub_name);
2138 }
2139
2140 return hsh;
2141}
2142
2143/* Add a new stub entry to the stub hash. Not all fields of the new
2144 stub entry are initialised. */
2145static struct elf32_nios2_stub_hash_entry *
2146nios2_add_stub (const char *stub_name,
2147 asection *section,
2148 struct elf32_nios2_link_hash_table *htab,
2149 enum elf32_nios2_stub_type stub_type)
2150{
2151 asection *link_sec;
2152 asection *stub_sec;
2153 asection **secptr, **linkptr;
2154 struct elf32_nios2_stub_hash_entry *hsh;
2155 bfd_boolean afterp;
2156
2157 if (stub_type == nios2_stub_call26_before)
2158 {
2159 link_sec = htab->stub_group[section->id].first_sec;
2160 secptr = &(htab->stub_group[section->id].first_stub_sec);
2161 linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2162 afterp = FALSE;
2163 }
2164 else
2165 {
2166 link_sec = htab->stub_group[section->id].last_sec;
2167 secptr = &(htab->stub_group[section->id].last_stub_sec);
2168 linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2169 afterp = TRUE;
2170 }
2171 stub_sec = *secptr;
2172 if (stub_sec == NULL)
2173 {
2174 stub_sec = *linkptr;
2175 if (stub_sec == NULL)
2176 {
2177 size_t namelen;
2178 bfd_size_type len;
2179 char *s_name;
2180
2181 namelen = strlen (link_sec->name);
2182 len = namelen + sizeof (STUB_SUFFIX);
2183 s_name = bfd_alloc (htab->stub_bfd, len);
2184 if (s_name == NULL)
2185 return NULL;
2186
2187 memcpy (s_name, link_sec->name, namelen);
2188 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2189
2190 stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2191 if (stub_sec == NULL)
2192 return NULL;
2193 *linkptr = stub_sec;
2194 }
2195 *secptr = stub_sec;
2196 }
2197
2198 /* Enter this entry into the linker stub hash table. */
2199 hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2200 TRUE, FALSE);
2201 if (hsh == NULL)
2202 {
695344c0 2203 /* xgettext:c-format */
871b3ab2 2204 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4eca0228
AM
2205 section->owner,
2206 stub_name);
78058a5e
SL
2207 return NULL;
2208 }
2209
2210 hsh->stub_sec = stub_sec;
2211 hsh->stub_offset = 0;
2212 hsh->id_sec = link_sec;
2213 return hsh;
2214}
2215
2216/* Set up various things so that we can make a list of input sections
2217 for each output section included in the link. Returns -1 on error,
2218 0 when no stubs will be needed, and 1 on success. */
2219int
2220nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2221{
2222 bfd *input_bfd;
2223 unsigned int bfd_count;
7292b3ac 2224 unsigned int top_id, top_index;
78058a5e
SL
2225 asection *section;
2226 asection **input_list, **list;
986f0783 2227 size_t amt;
78058a5e
SL
2228 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2229
2230 /* Count the number of input BFDs and find the top input section id. */
2231 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2232 input_bfd != NULL;
c72f2fb2 2233 input_bfd = input_bfd->link.next)
78058a5e
SL
2234 {
2235 bfd_count += 1;
2236 for (section = input_bfd->sections;
2237 section != NULL;
2238 section = section->next)
2239 {
2240 if (top_id < section->id)
2241 top_id = section->id;
2242 }
2243 }
2244
2245 htab->bfd_count = bfd_count;
2246
2247 amt = sizeof (struct map_stub) * (top_id + 1);
2248 htab->stub_group = bfd_zmalloc (amt);
2249 if (htab->stub_group == NULL)
2250 return -1;
2251
2252 /* We can't use output_bfd->section_count here to find the top output
2253 section index as some sections may have been removed, and
2254 strip_excluded_output_sections doesn't renumber the indices. */
2255 for (section = output_bfd->sections, top_index = 0;
2256 section != NULL;
2257 section = section->next)
2258 {
2259 if (top_index < section->index)
2260 top_index = section->index;
2261 }
2262
2263 htab->top_index = top_index;
2264 amt = sizeof (asection *) * (top_index + 1);
2265 input_list = bfd_malloc (amt);
2266 htab->input_list = input_list;
2267 if (input_list == NULL)
2268 return -1;
2269
2270 /* For sections we aren't interested in, mark their entries with a
2271 value we can check later. */
2272 list = input_list + top_index;
2273 do
2274 *list = bfd_abs_section_ptr;
2275 while (list-- != input_list);
2276
2277 for (section = output_bfd->sections;
2278 section != NULL;
2279 section = section->next)
2280 {
2281 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2282 * have PC relative relocs in them but no code flag set. */
2283 if (((section->flags & SEC_CODE) != 0) ||
2284 strcmp(".ctors", section->name) ||
2285 strcmp(".dtors", section->name))
2286 input_list[section->index] = NULL;
2287 }
2288
2289 return 1;
2290}
2291
2292/* The linker repeatedly calls this function for each input section,
2293 in the order that input sections are linked into output sections.
2294 Build lists of input sections to determine groupings between which
2295 we may insert linker stubs. */
2296void
2297nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2298{
2299 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2300
2301 if (isec->output_section->index <= htab->top_index)
2302 {
2303 asection **list = htab->input_list + isec->output_section->index;
2304 if (*list != bfd_abs_section_ptr)
2305 {
2306 /* Steal the last_sec pointer for our list.
2307 This happens to make the list in reverse order,
2308 which is what we want. */
2309 htab->stub_group[isec->id].last_sec = *list;
2310 *list = isec;
2311 }
2312 }
2313}
2314
2315/* Segment mask for CALL26 relocation relaxation. */
2316#define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2317
2318/* Fudge factor for approximate maximum size of all stubs that might
2319 be inserted by the linker. This does not actually limit the number
2320 of stubs that might be inserted, and only affects strategy for grouping
2321 and placement of stubs. Perhaps this should be computed based on number
2322 of relocations seen, or be specifiable on the command line. */
2323#define MAX_STUB_SECTION_SIZE 0xffff
2324
2325/* See whether we can group stub sections together. Grouping stub
2326 sections may result in fewer stubs. More importantly, we need to
2327 put all .init* and .fini* stubs at the end of the .init or
2328 .fini output sections respectively, because glibc splits the
2329 _init and _fini functions into multiple parts. Putting a stub in
2330 the middle of a function is not a good idea.
2331 Rather than computing groups of a maximum fixed size, for Nios II
2332 CALL26 relaxation it makes more sense to compute the groups based on
2333 sections that fit within a 256MB address segment. Also do not allow
2334 a group to span more than one output section, since different output
2335 sections might correspond to different memory banks on a bare-metal
2336 target, etc. */
2337static void
2338group_sections (struct elf32_nios2_link_hash_table *htab)
2339{
2340 asection **list = htab->input_list + htab->top_index;
2341 do
2342 {
2343 /* The list is in reverse order so we'll search backwards looking
2344 for the first section that begins in the same memory segment,
2345 marking sections along the way to point at the tail for this
2346 group. */
2347 asection *tail = *list;
2348 if (tail == bfd_abs_section_ptr)
2349 continue;
2350 while (tail != NULL)
2351 {
2352 bfd_vma start = tail->output_section->vma + tail->output_offset;
2353 bfd_vma end = start + tail->size;
2354 bfd_vma segment = CALL26_SEGMENT (end);
2355 asection *prev;
2356
2357 if (segment != CALL26_SEGMENT (start)
2358 || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2359 /* This section spans more than one memory segment, or is
2360 close enough to the end of the segment that adding stub
2361 sections before it might cause it to move so that it
2362 spans memory segments, or that stubs added at the end of
2363 this group might overflow into the next memory segment.
2364 Put it in a group by itself to localize the effects. */
2365 {
2366 prev = htab->stub_group[tail->id].last_sec;
2367 htab->stub_group[tail->id].last_sec = tail;
2368 htab->stub_group[tail->id].first_sec = tail;
2369 }
2370 else
2371 /* Collect more sections for this group. */
2372 {
2373 asection *curr, *first;
2374 for (curr = tail; ; curr = prev)
2375 {
2376 prev = htab->stub_group[curr->id].last_sec;
2377 if (!prev
2378 || tail->output_section != prev->output_section
2379 || (CALL26_SEGMENT (prev->output_section->vma
2380 + prev->output_offset)
2381 != segment))
2382 break;
2383 }
2384 first = curr;
2385 for (curr = tail; ; curr = prev)
2386 {
2387 prev = htab->stub_group[curr->id].last_sec;
2388 htab->stub_group[curr->id].last_sec = tail;
2389 htab->stub_group[curr->id].first_sec = first;
2390 if (curr == first)
2391 break;
2392 }
2393 }
2394
2395 /* Reset tail for the next group. */
2396 tail = prev;
2397 }
2398 }
2399 while (list-- != htab->input_list);
2400 free (htab->input_list);
2401}
2402
2403/* Determine the type of stub needed, if any, for a call. */
2404static enum elf32_nios2_stub_type
2405nios2_type_of_stub (asection *input_sec,
2406 const Elf_Internal_Rela *rel,
2407 struct elf32_nios2_link_hash_entry *hh,
2408 struct elf32_nios2_link_hash_table *htab,
2409 bfd_vma destination,
2410 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2411{
2412 bfd_vma location, segment, start, end;
2413 asection *s0, *s1, *s;
2414
2415 if (hh != NULL &&
2416 !(hh->root.root.type == bfd_link_hash_defined
2417 || hh->root.root.type == bfd_link_hash_defweak))
2418 return nios2_stub_none;
2419
2420 /* Determine where the call point is. */
2421 location = (input_sec->output_section->vma
2422 + input_sec->output_offset + rel->r_offset);
2423 segment = CALL26_SEGMENT (location);
2424
2425 /* Nios II CALL and JMPI instructions can transfer control to addresses
2426 within the same 256MB segment as the PC. */
2427 if (segment == CALL26_SEGMENT (destination))
2428 return nios2_stub_none;
2429
2430 /* Find the start and end addresses of the stub group. Also account for
2431 any already-created stub sections for this group. Note that for stubs
2432 in the end section, only the first instruction of the last stub
2433 (12 bytes long) needs to be within range. */
2434 s0 = htab->stub_group[input_sec->id].first_sec;
2435 s = htab->stub_group[s0->id].first_stub_sec;
2436 if (s != NULL && s->size > 0)
2437 start = s->output_section->vma + s->output_offset;
2438 else
2439 start = s0->output_section->vma + s0->output_offset;
2440
2441 s1 = htab->stub_group[input_sec->id].last_sec;
2442 s = htab->stub_group[s1->id].last_stub_sec;
2443 if (s != NULL && s->size > 0)
2444 end = s->output_section->vma + s->output_offset + s->size - 8;
2445 else
2446 end = s1->output_section->vma + s1->output_offset + s1->size;
2447
2448 BFD_ASSERT (start < end);
2449 BFD_ASSERT (start <= location);
2450 BFD_ASSERT (location < end);
2451
2452 /* Put stubs at the end of the group unless that is not a valid
2453 location and the beginning of the group is. It might be that
2454 neither the beginning nor end works if we have an input section
2455 so large that it spans multiple segment boundaries. In that
2456 case, punt; the end result will be a relocation overflow error no
2457 matter what we do here.
2458
2459 Note that adding stubs pushes up the addresses of all subsequent
2460 sections, so that stubs allocated on one pass through the
2461 relaxation loop may not be valid on the next pass. (E.g., we may
2462 allocate a stub at the beginning of the section on one pass and
2463 find that the call site has been bumped into the next memory
2464 segment on the next pass.) The important thing to note is that
2465 we never try to reclaim the space allocated to such unused stubs,
2466 so code size and section addresses can only increase with each
2467 iteration. Accounting for the start and end addresses of the
2468 already-created stub sections ensures that when the algorithm
2469 converges, it converges accurately, with the entire appropriate
2470 stub section accessible from the call site and not just the
2471 address at the start or end of the stub group proper. */
2472
2473 if (segment == CALL26_SEGMENT (end))
2474 return nios2_stub_call26_after;
2475 else if (segment == CALL26_SEGMENT (start))
2476 return nios2_stub_call26_before;
2477 else
2478 /* Perhaps this should be a dedicated error code. */
2479 return nios2_stub_none;
2480}
2481
2482static bfd_boolean
2483nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2484{
2485 struct elf32_nios2_stub_hash_entry *hsh
2486 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2487 asection *stub_sec = hsh->stub_sec;
2488 bfd_vma sym_value;
abf874aa 2489 struct bfd_link_info *info;
78058a5e 2490
abf874aa
CL
2491 info = (struct bfd_link_info *) in_arg;
2492
2493 /* Fail if the target section could not be assigned to an output
2494 section. The user should fix his linker script. */
2495 if (hsh->target_section->output_section == NULL
2496 && info->non_contiguous_regions)
53215f21
CL
2497 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
2498 "Retry without --enable-non-contiguous-regions.\n"),
2499 hsh->target_section);
2500
78058a5e
SL
2501 /* Make a note of the offset within the stubs for this entry. */
2502 hsh->stub_offset = stub_sec->size;
2503
2504 switch (hsh->stub_type)
2505 {
2506 case nios2_stub_call26_before:
2507 case nios2_stub_call26_after:
2508 /* A call26 stub looks like:
2509 orhi at, %hiadj(dest)
2510 addi at, at, %lo(dest)
2511 jmp at
2512 Note that call/jmpi instructions can't be used in PIC code
2513 so there is no reason for the stub to be PIC, either. */
2514 sym_value = (hsh->target_value
2515 + hsh->target_section->output_offset
2516 + hsh->target_section->output_section->vma
2517 + hsh->addend);
2518
2519 nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2520 hsh->stub_offset, 3);
2521 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2522 hiadj (sym_value));
2523 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2524 (sym_value & 0xffff));
2525 stub_sec->size += 12;
2526 break;
2527 default:
2528 BFD_FAIL ();
2529 return FALSE;
2530 }
2531
2532 return TRUE;
2533}
2534
2535/* As above, but don't actually build the stub. Just bump offset so
2536 we know stub section sizes. */
2537static bfd_boolean
2538nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2539{
2540 struct elf32_nios2_stub_hash_entry *hsh
2541 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2542
2543 switch (hsh->stub_type)
2544 {
2545 case nios2_stub_call26_before:
2546 case nios2_stub_call26_after:
2547 hsh->stub_sec->size += 12;
2548 break;
2549 default:
2550 BFD_FAIL ();
2551 return FALSE;
2552 }
2553 return TRUE;
2554}
2555
2556/* Read in all local syms for all input bfds.
2557 Returns -1 on error, 0 otherwise. */
2558
2559static int
2560get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2561 struct bfd_link_info *info)
2562{
2563 unsigned int bfd_indx;
2564 Elf_Internal_Sym *local_syms, **all_local_syms;
2565 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2566
2567 /* We want to read in symbol extension records only once. To do this
2568 we need to read in the local symbols in parallel and save them for
2569 later use; so hold pointers to the local symbols in an array. */
986f0783 2570 size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
78058a5e
SL
2571 all_local_syms = bfd_zmalloc (amt);
2572 htab->all_local_syms = all_local_syms;
2573 if (all_local_syms == NULL)
2574 return -1;
2575
2576 /* Walk over all the input BFDs, swapping in local symbols. */
2577 for (bfd_indx = 0;
2578 input_bfd != NULL;
c72f2fb2 2579 input_bfd = input_bfd->link.next, bfd_indx++)
78058a5e
SL
2580 {
2581 Elf_Internal_Shdr *symtab_hdr;
2582
2583 /* We'll need the symbol table in a second. */
2584 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2585 if (symtab_hdr->sh_info == 0)
2586 continue;
2587
2588 /* We need an array of the local symbols attached to the input bfd. */
2589 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2590 if (local_syms == NULL)
2591 {
2592 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2593 symtab_hdr->sh_info, 0,
2594 NULL, NULL, NULL);
2595 /* Cache them for elf_link_input_bfd. */
2596 symtab_hdr->contents = (unsigned char *) local_syms;
2597 }
2598 if (local_syms == NULL)
2599 return -1;
2600
2601 all_local_syms[bfd_indx] = local_syms;
2602 }
2603
2604 return 0;
2605}
2606
2607/* Determine and set the size of the stub section for a final link. */
2608bfd_boolean
2609nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2610 struct bfd_link_info *info,
2611 asection *(*add_stub_section) (const char *,
2612 asection *, bfd_boolean),
2613 void (*layout_sections_again) (void))
2614{
2615 bfd_boolean stub_changed = FALSE;
2616 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2617
2618 /* Stash our params away. */
2619 htab->stub_bfd = stub_bfd;
2620 htab->add_stub_section = add_stub_section;
2621 htab->layout_sections_again = layout_sections_again;
2622
2623 /* FIXME: We only compute the section groups once. This could cause
2624 problems if adding a large stub section causes following sections,
2625 or parts of them, to move into another segment. However, this seems
2626 to be consistent with the way other back ends handle this.... */
2627 group_sections (htab);
2628
2629 if (get_local_syms (output_bfd, info->input_bfds, info))
2630 {
2631 if (htab->all_local_syms)
2632 goto error_ret_free_local;
2633 return FALSE;
2634 }
2635
2636 while (1)
2637 {
2638 bfd *input_bfd;
2639 unsigned int bfd_indx;
2640 asection *stub_sec;
2641
2642 for (input_bfd = info->input_bfds, bfd_indx = 0;
2643 input_bfd != NULL;
c72f2fb2 2644 input_bfd = input_bfd->link.next, bfd_indx++)
78058a5e
SL
2645 {
2646 Elf_Internal_Shdr *symtab_hdr;
2647 asection *section;
2648 Elf_Internal_Sym *local_syms;
2649
2650 /* We'll need the symbol table in a second. */
2651 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2652 if (symtab_hdr->sh_info == 0)
2653 continue;
2654
2655 local_syms = htab->all_local_syms[bfd_indx];
2656
2657 /* Walk over each section attached to the input bfd. */
2658 for (section = input_bfd->sections;
2659 section != NULL;
2660 section = section->next)
2661 {
2662 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2663
2664 /* If there aren't any relocs, then there's nothing more
2665 to do. */
2666 if ((section->flags & SEC_RELOC) == 0
2667 || section->reloc_count == 0)
2668 continue;
2669
2670 /* If this section is a link-once section that will be
2671 discarded, then don't create any stubs. */
2672 if (section->output_section == NULL
2673 || section->output_section->owner != output_bfd)
2674 continue;
2675
2676 /* Get the relocs. */
2677 internal_relocs
2678 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2679 info->keep_memory);
2680 if (internal_relocs == NULL)
2681 goto error_ret_free_local;
2682
2683 /* Now examine each relocation. */
2684 irela = internal_relocs;
2685 irelaend = irela + section->reloc_count;
2686 for (; irela < irelaend; irela++)
2687 {
2688 unsigned int r_type, r_indx;
2689 enum elf32_nios2_stub_type stub_type;
2690 struct elf32_nios2_stub_hash_entry *hsh;
2691 asection *sym_sec;
2692 bfd_vma sym_value;
2693 bfd_vma destination;
2694 struct elf32_nios2_link_hash_entry *hh;
2695 char *stub_name;
2696 const asection *id_sec;
2697
2698 r_type = ELF32_R_TYPE (irela->r_info);
2699 r_indx = ELF32_R_SYM (irela->r_info);
2700
2701 if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2702 {
2703 bfd_set_error (bfd_error_bad_value);
2704 error_ret_free_internal:
2705 if (elf_section_data (section)->relocs == NULL)
2706 free (internal_relocs);
2707 goto error_ret_free_local;
2708 }
2709
2710 /* Only look for stubs on CALL and JMPI instructions. */
2711 if (r_type != (unsigned int) R_NIOS2_CALL26)
2712 continue;
2713
2714 /* Now determine the call target, its name, value,
2715 section. */
2716 sym_sec = NULL;
2717 sym_value = 0;
2718 destination = 0;
2719 hh = NULL;
2720 if (r_indx < symtab_hdr->sh_info)
2721 {
2722 /* It's a local symbol. */
2723 Elf_Internal_Sym *sym;
2724 Elf_Internal_Shdr *hdr;
2725 unsigned int shndx;
2726
2727 sym = local_syms + r_indx;
2728 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2729 sym_value = sym->st_value;
2730 shndx = sym->st_shndx;
2731 if (shndx < elf_numsections (input_bfd))
2732 {
2733 hdr = elf_elfsections (input_bfd)[shndx];
2734 sym_sec = hdr->bfd_section;
2735 destination = (sym_value + irela->r_addend
2736 + sym_sec->output_offset
2737 + sym_sec->output_section->vma);
2738 }
2739 }
2740 else
2741 {
2742 /* It's an external symbol. */
2743 int e_indx;
2744
2745 e_indx = r_indx - symtab_hdr->sh_info;
2746 hh = ((struct elf32_nios2_link_hash_entry *)
2747 elf_sym_hashes (input_bfd)[e_indx]);
2748
2749 while (hh->root.root.type == bfd_link_hash_indirect
2750 || hh->root.root.type == bfd_link_hash_warning)
2751 hh = ((struct elf32_nios2_link_hash_entry *)
2752 hh->root.root.u.i.link);
2753
2754 if (hh->root.root.type == bfd_link_hash_defined
2755 || hh->root.root.type == bfd_link_hash_defweak)
2756 {
2757 sym_sec = hh->root.root.u.def.section;
2758 sym_value = hh->root.root.u.def.value;
2759
2760 if (sym_sec->output_section != NULL)
2761 destination = (sym_value + irela->r_addend
2762 + sym_sec->output_offset
2763 + sym_sec->output_section->vma);
2764 else
2765 continue;
2766 }
2767 else if (hh->root.root.type == bfd_link_hash_undefweak)
2768 {
0e1862bb 2769 if (! bfd_link_pic (info))
78058a5e
SL
2770 continue;
2771 }
2772 else if (hh->root.root.type == bfd_link_hash_undefined)
2773 {
2774 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2775 && (ELF_ST_VISIBILITY (hh->root.other)
2776 == STV_DEFAULT)))
2777 continue;
2778 }
2779 else
2780 {
2781 bfd_set_error (bfd_error_bad_value);
2782 goto error_ret_free_internal;
2783 }
2784 }
2785
2786 /* Determine what (if any) linker stub is needed. */
2787 stub_type = nios2_type_of_stub (section, irela, hh, htab,
2788 destination, info);
2789 if (stub_type == nios2_stub_none)
2790 continue;
2791
2792 /* Support for grouping stub sections. */
2793 if (stub_type == nios2_stub_call26_before)
2794 id_sec = htab->stub_group[section->id].first_sec;
2795 else
2796 id_sec = htab->stub_group[section->id].last_sec;
2797
2798 /* Get the name of this stub. */
2799 stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2800 stub_type);
2801 if (!stub_name)
2802 goto error_ret_free_internal;
2803
2804 hsh = nios2_stub_hash_lookup (&htab->bstab,
2805 stub_name,
2806 FALSE, FALSE);
2807 if (hsh != NULL)
2808 {
2809 /* The proper stub has already been created. */
2810 free (stub_name);
2811 continue;
2812 }
2813
2814 hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2815 if (hsh == NULL)
2816 {
2817 free (stub_name);
2818 goto error_ret_free_internal;
2819 }
2820 hsh->target_value = sym_value;
2821 hsh->target_section = sym_sec;
2822 hsh->stub_type = stub_type;
2823 hsh->hh = hh;
2824 hsh->addend = irela->r_addend;
2825 stub_changed = TRUE;
2826 }
2827
2828 /* We're done with the internal relocs, free them. */
2829 if (elf_section_data (section)->relocs == NULL)
2830 free (internal_relocs);
2831 }
2832 }
2833
2834 if (!stub_changed)
2835 break;
2836
2837 /* OK, we've added some stubs. Find out the new size of the
2838 stub sections. */
2839 for (stub_sec = htab->stub_bfd->sections;
2840 stub_sec != NULL;
2841 stub_sec = stub_sec->next)
2842 stub_sec->size = 0;
2843
2844 bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2845
2846 /* Ask the linker to do its stuff. */
2847 (*htab->layout_sections_again) ();
2848 stub_changed = FALSE;
2849 }
2850
2851 free (htab->all_local_syms);
2852 return TRUE;
2853
2854 error_ret_free_local:
2855 free (htab->all_local_syms);
2856 return FALSE;
2857}
2858
2859/* Build all the stubs associated with the current output file. The
2860 stubs are kept in a hash table attached to the main linker hash
2861 table. This function is called via nios2elf_finish in the linker. */
2862bfd_boolean
2863nios2_elf32_build_stubs (struct bfd_link_info *info)
2864{
2865 asection *stub_sec;
2866 struct bfd_hash_table *table;
2867 struct elf32_nios2_link_hash_table *htab;
2868
2869 htab = elf32_nios2_hash_table (info);
2870
2871 for (stub_sec = htab->stub_bfd->sections;
2872 stub_sec != NULL;
2873 stub_sec = stub_sec->next)
1511baec
SL
2874 /* The stub_bfd may contain non-stub sections if it is also the
2875 dynobj. Any such non-stub sections are created with the
2876 SEC_LINKER_CREATED flag set, while stub sections do not
2877 have that flag. Ignore any non-stub sections here. */
2878 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
1b786873 2879 {
1511baec
SL
2880 bfd_size_type size;
2881
2882 /* Allocate memory to hold the linker stubs. */
2883 size = stub_sec->size;
2884 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2885 if (stub_sec->contents == NULL && size != 0)
2886 return FALSE;
2887 stub_sec->size = 0;
2888 }
78058a5e
SL
2889
2890 /* Build the stubs as directed by the stub hash table. */
2891 table = &htab->bstab;
2892 bfd_hash_traverse (table, nios2_build_one_stub, info);
2893
2894 return TRUE;
2895}
2896
2897
965b1d80
SL
2898#define is_nios2_elf(bfd) \
2899 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2900 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2901
2902/* Merge backend specific data from an object file to the output
2903 object file when linking. */
2904
2905static bfd_boolean
50e03d47 2906nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
965b1d80 2907{
50e03d47 2908 bfd *obfd = info->output_bfd;
965b1d80
SL
2909 flagword old_flags;
2910 flagword new_flags;
2911
2912 if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2913 return TRUE;
2914
2915 /* Check if we have the same endianness. */
50e03d47 2916 if (! _bfd_generic_verify_endian_match (ibfd, info))
965b1d80
SL
2917 return FALSE;
2918
2919 new_flags = elf_elfheader (ibfd)->e_flags;
2920 old_flags = elf_elfheader (obfd)->e_flags;
2921 if (!elf_flags_init (obfd))
2922 {
2923 /* First call, no flags set. */
2924 elf_flags_init (obfd) = TRUE;
2925 elf_elfheader (obfd)->e_flags = new_flags;
2926
2927 switch (new_flags)
2928 {
2929 default:
2930 case EF_NIOS2_ARCH_R1:
2931 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2932 break;
2933 case EF_NIOS2_ARCH_R2:
2934 if (bfd_big_endian (ibfd))
2935 {
4eca0228 2936 _bfd_error_handler
38f14ab8 2937 (_("error: %pB: big-endian R2 is not supported"), ibfd);
965b1d80
SL
2938 bfd_set_error (bfd_error_bad_value);
2939 return FALSE;
2940 }
2941 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2942 break;
2943 }
2944 }
2945
2946 /* Incompatible flags. */
2947 else if (new_flags != old_flags)
2948 {
2949 /* So far, the only incompatible flags denote incompatible
2950 architectures. */
4eca0228 2951 _bfd_error_handler
695344c0 2952 /* xgettext:c-format */
38f14ab8 2953 (_("error: %pB: conflicting CPU architectures %d/%d"),
965b1d80
SL
2954 ibfd, new_flags, old_flags);
2955 bfd_set_error (bfd_error_bad_value);
2956 return FALSE;
2957 }
2958
2959 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 2960 _bfd_elf_merge_object_attributes (ibfd, info);
965b1d80
SL
2961
2962 return TRUE;
2963}
2964
36591ba1
SL
2965/* Implement bfd_elf32_bfd_reloc_type_lookup:
2966 Given a BFD reloc type, return a howto structure. */
f3185997 2967
36591ba1 2968static reloc_howto_type *
8c163c5a 2969nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
36591ba1
SL
2970 bfd_reloc_code_real_type code)
2971{
2972 int i;
1b786873 2973
f3185997 2974 for (i = 0; i < (int) ARRAY_SIZE (nios2_reloc_map); ++i)
36591ba1 2975 if (nios2_reloc_map[i].bfd_val == code)
8c163c5a 2976 return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
36591ba1
SL
2977 return NULL;
2978}
2979
2980/* Implement bfd_elf32_bfd_reloc_name_lookup:
2981 Given a reloc name, return a howto structure. */
f3185997 2982
36591ba1 2983static reloc_howto_type *
8c163c5a 2984nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
36591ba1
SL
2985 const char *r_name)
2986{
8c163c5a
SL
2987 int i;
2988 reloc_howto_type *howto_tbl;
2989 int howto_tbl_size;
36591ba1 2990
8c163c5a
SL
2991 if (BFD_IS_R2 (abfd))
2992 {
2993 howto_tbl = elf_nios2_r2_howto_table_rel;
f3185997 2994 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
8c163c5a
SL
2995 }
2996 else
2997 {
2998 howto_tbl = elf_nios2_r1_howto_table_rel;
f3185997 2999 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
8c163c5a 3000 }
1b786873 3001
8c163c5a
SL
3002 for (i = 0; i < howto_tbl_size; i++)
3003 if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
3004 return howto_tbl + i;
f3185997 3005
1b786873 3006 return NULL;
36591ba1
SL
3007}
3008
3009/* Implement elf_info_to_howto:
3010 Given a ELF32 relocation, fill in a arelent structure. */
f3185997
NC
3011
3012static bfd_boolean
8c163c5a 3013nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
36591ba1
SL
3014 Elf_Internal_Rela *dst)
3015{
3016 unsigned int r_type;
3017
3018 r_type = ELF32_R_TYPE (dst->r_info);
f3185997
NC
3019 if ((cache_ptr->howto = lookup_howto (r_type, abfd)) == NULL)
3020 {
3021 /* xgettext:c-format */
3022 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3023 abfd, r_type);
3024 bfd_set_error (bfd_error_bad_value);
3025 return FALSE;
3026 }
3027 return TRUE;
36591ba1
SL
3028}
3029
3030/* Return the base VMA address which should be subtracted from real addresses
3031 when resolving @dtpoff relocation.
3032 This is PT_TLS segment p_vaddr. */
3033static bfd_vma
3034dtpoff_base (struct bfd_link_info *info)
3035{
3036 /* If tls_sec is NULL, we should have signalled an error already. */
3037 if (elf_hash_table (info)->tls_sec == NULL)
3038 return 0;
3039 return elf_hash_table (info)->tls_sec->vma;
3040}
3041
3042/* Return the relocation value for @tpoff relocation
3043 if STT_TLS virtual address is ADDRESS. */
3044static bfd_vma
3045tpoff (struct bfd_link_info *info, bfd_vma address)
3046{
3047 struct elf_link_hash_table *htab = elf_hash_table (info);
3048
3049 /* If tls_sec is NULL, we should have signalled an error already. */
3050 if (htab->tls_sec == NULL)
3051 return 0;
3052 return address - htab->tls_sec->vma;
3053}
3054
3055/* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3056 dangerous relocation. */
3057static bfd_boolean
3058nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3059{
3060
3061 bfd_boolean gp_found;
3062 struct bfd_hash_entry *h;
3063 struct bfd_link_hash_entry *lh;
3064
3065 /* If we've already figured out what GP will be, just return it. */
3066 *pgp = _bfd_get_gp_value (output_bfd);
3067 if (*pgp)
3068 return TRUE;
3069
3070 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
3071 lh = (struct bfd_link_hash_entry *) h;
dc1e8a47 3072 lookup:
36591ba1
SL
3073 if (lh)
3074 {
3075 switch (lh->type)
3076 {
3077 case bfd_link_hash_undefined:
3078 case bfd_link_hash_undefweak:
3079 case bfd_link_hash_common:
3080 gp_found = FALSE;
3081 break;
3082 case bfd_link_hash_defined:
3083 case bfd_link_hash_defweak:
3084 gp_found = TRUE;
a7be2893
SL
3085 {
3086 asection *sym_sec = lh->u.def.section;
3087 bfd_vma sym_value = lh->u.def.value;
3088
3089 if (sym_sec->output_section)
3090 sym_value = (sym_value + sym_sec->output_offset
3091 + sym_sec->output_section->vma);
3092 *pgp = sym_value;
3093 }
36591ba1
SL
3094 break;
3095 case bfd_link_hash_indirect:
3096 case bfd_link_hash_warning:
3097 lh = lh->u.i.link;
3098 /* @@FIXME ignoring warning for now */
3099 goto lookup;
3100 case bfd_link_hash_new:
3101 default:
3102 abort ();
3103 }
3104 }
3105 else
3106 gp_found = FALSE;
3107
3108 if (!gp_found)
3109 {
3110 /* Only get the error once. */
3111 *pgp = 4;
3112 _bfd_set_gp_value (output_bfd, *pgp);
3113 return FALSE;
3114 }
3115
3116 _bfd_set_gp_value (output_bfd, *pgp);
3117
3118 return TRUE;
3119}
3120
3121/* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3122 if it's not available as we don't have a link_info pointer available here
3123 to look it up in the output symbol table. We don't need to adjust the
3124 symbol value for an external symbol if we are producing relocatable
3125 output. */
3126static bfd_reloc_status_type
1b786873 3127nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
36591ba1
SL
3128 char **error_message, bfd_vma *pgp)
3129{
3130 if (bfd_is_und_section (symbol->section) && !relocatable)
3131 {
3132 *pgp = 0;
3133 return bfd_reloc_undefined;
3134 }
3135
3136 *pgp = _bfd_get_gp_value (output_bfd);
3137 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3138 {
3139 if (relocatable)
3140 {
3141 /* Make up a value. */
3142 *pgp = symbol->section->output_section->vma + 0x4000;
3143 _bfd_set_gp_value (output_bfd, *pgp);
3144 }
3145 else
3146 {
3147 *error_message
3148 = (char *) _("global pointer relative relocation when _gp not defined");
3149 return bfd_reloc_dangerous;
3150 }
3151 }
3152
3153 return bfd_reloc_ok;
3154}
3155
36591ba1
SL
3156/* Do the relocations that require special handling. */
3157static bfd_reloc_status_type
1b786873 3158nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3159 asection *input_section,
1b786873 3160 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3161 bfd_vma symbol_value, bfd_vma addend)
3162{
3163 symbol_value = symbol_value + addend;
3164 addend = 0;
3165 symbol_value = (symbol_value >> 16) & 0xffff;
3166 return _bfd_final_link_relocate (howto, abfd, input_section,
3167 data, offset, symbol_value, addend);
3168}
3169
3170static bfd_reloc_status_type
3171nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3172 asection *input_section,
1b786873 3173 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3174 bfd_vma symbol_value, bfd_vma addend)
3175{
3176 symbol_value = symbol_value + addend;
3177 addend = 0;
3178 symbol_value = symbol_value & 0xffff;
3179 return _bfd_final_link_relocate (howto, abfd, input_section,
3180 data, offset, symbol_value, addend);
3181}
3182
3183static bfd_reloc_status_type
3184nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3185 asection *input_section,
36591ba1
SL
3186 bfd_byte *data, bfd_vma offset,
3187 bfd_vma symbol_value, bfd_vma addend)
3188{
3189 symbol_value = symbol_value + addend;
3190 addend = 0;
3191 symbol_value = hiadj(symbol_value);
3192 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3193 symbol_value, addend);
3194}
3195
3196static bfd_reloc_status_type
3197nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3198 asection *input_section,
36591ba1
SL
3199 bfd_byte *data, bfd_vma offset,
3200 bfd_vma symbol_value, bfd_vma addend)
3201{
3202 symbol_value = symbol_value + addend;
3203 addend = 0;
3204 symbol_value = symbol_value & 0xffff;
3205 return _bfd_final_link_relocate (howto, abfd, input_section,
3206 data, offset, symbol_value, addend);
3207}
3208
3209static bfd_reloc_status_type
3210nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3211 asection *input_section,
36591ba1
SL
3212 bfd_byte *data, bfd_vma offset,
3213 bfd_vma symbol_value, bfd_vma addend)
3214{
3215 symbol_value = symbol_value + addend;
3216 symbol_value -= (input_section->output_section->vma
3217 + input_section->output_offset);
3218 symbol_value -= offset;
3219 addend = 0;
3220 symbol_value = hiadj(symbol_value);
3221 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3222 symbol_value, addend);
3223}
3224
3225static bfd_reloc_status_type
3226nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3227 asection *input_section,
1b786873 3228 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3229 bfd_vma symbol_value, bfd_vma addend)
3230{
1b786873 3231 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
36591ba1
SL
3232 so we need to subtract 4 before doing a final_link_relocate. */
3233 symbol_value = symbol_value + addend - 4;
3234 addend = 0;
3235 return _bfd_final_link_relocate (howto, abfd, input_section,
3236 data, offset, symbol_value, addend);
3237}
3238
3239static bfd_reloc_status_type
3240nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3241 asection *input_section,
1b786873 3242 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3243 bfd_vma symbol_value, bfd_vma addend)
3244{
3245 /* Check that the relocation is in the same page as the current address. */
1b786873 3246 if (CALL26_SEGMENT (symbol_value + addend)
78058a5e
SL
3247 != CALL26_SEGMENT (input_section->output_section->vma
3248 + input_section->output_offset
3249 + offset))
36591ba1
SL
3250 return bfd_reloc_overflow;
3251
8c163c5a
SL
3252 /* Check that the target address is correctly aligned on a 4-byte
3253 boundary. */
3254 if ((symbol_value + addend) & 0x3)
3255 return bfd_reloc_overflow;
3256
36591ba1
SL
3257 return _bfd_final_link_relocate (howto, abfd, input_section,
3258 data, offset, symbol_value, addend);
3259}
3260
3261static bfd_reloc_status_type
3262nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3263 asection *input_section,
1b786873 3264 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3265 bfd_vma symbol_value, bfd_vma addend)
3266{
3267 /* Because we need the output_bfd, the special handling is done
3268 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3269 return _bfd_final_link_relocate (howto, abfd, input_section,
3270 data, offset, symbol_value, addend);
3271}
3272
3273static bfd_reloc_status_type
3274nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3275 asection *input_section,
1b786873 3276 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3277 bfd_vma symbol_value, bfd_vma addend)
3278{
3279 bfd_vma symbol_lo16, symbol_hi16;
3280 bfd_reloc_status_type r;
3281 symbol_value = symbol_value + addend;
3282 addend = 0;
3283 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3284 symbol_lo16 = symbol_value & 0xffff;
3285
3286 r = _bfd_final_link_relocate (howto, abfd, input_section,
3287 data, offset, symbol_hi16, addend);
3288
3289 if (r == bfd_reloc_ok)
3290 return _bfd_final_link_relocate (howto, abfd, input_section,
3291 data, offset + 4, symbol_lo16, addend);
3292
3293 return r;
3294}
3295
3296static bfd_reloc_status_type
3297nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3298 asection *input_section,
1b786873 3299 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3300 bfd_vma symbol_value, bfd_vma addend)
3301{
3302 bfd_vma symbol_lo16, symbol_hi16;
3303 bfd_reloc_status_type r;
3304 symbol_value = symbol_value + addend;
3305 addend = 0;
3306 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3307 symbol_lo16 = symbol_value & 0xffff;
3308
3309 r = _bfd_final_link_relocate (howto, abfd, input_section,
3310 data, offset, symbol_hi16, addend);
3311
3312 if (r == bfd_reloc_ok)
3313 return _bfd_final_link_relocate (howto, abfd, input_section,
3314 data, offset + 4, symbol_lo16, addend);
3315
3316 return r;
3317}
3318
3319static bfd_reloc_status_type
3320nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3321 asection *input_section,
1b786873 3322 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3323 bfd_vma symbol_value, bfd_vma addend)
3324{
3325 bfd_vma symbol_lo16, symbol_hi16;
3326 bfd_reloc_status_type r;
3327 symbol_value = symbol_value + addend;
3328 addend = 0;
3329 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3330 symbol_lo16 = symbol_value & 0xffff;
3331
3332 r = _bfd_final_link_relocate (howto, abfd, input_section,
3333 data, offset, symbol_hi16, addend);
3334
3335 if (r == bfd_reloc_ok)
3336 return _bfd_final_link_relocate (howto, abfd, input_section,
3337 data, offset + 4, symbol_lo16, addend);
3338
3339 return r;
3340}
3341
3342/* HOWTO handlers for relocations that require special handling. */
3343
3344/* This is for relocations used only when relaxing to ensure
3345 changes in size of section don't screw up .align. */
3346static bfd_reloc_status_type
3347nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1b786873
L
3348 asymbol *symbol ATTRIBUTE_UNUSED,
3349 void *data ATTRIBUTE_UNUSED, asection *input_section,
3350 bfd *output_bfd,
36591ba1
SL
3351 char **error_message ATTRIBUTE_UNUSED)
3352{
3353 if (output_bfd != NULL)
3354 reloc_entry->address += input_section->output_offset;
3355 return bfd_reloc_ok;
3356}
3357
3358static bfd_reloc_status_type
1b786873
L
3359nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3360 void *data, asection *input_section,
3361 bfd *output_bfd,
36591ba1
SL
3362 char **error_message ATTRIBUTE_UNUSED)
3363{
3364 /* This part is from bfd_elf_generic_reloc. */
3365 if (output_bfd != NULL
3366 && (symbol->flags & BSF_SECTION_SYM) == 0
3367 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3368 {
3369 reloc_entry->address += input_section->output_offset;
3370 return bfd_reloc_ok;
3371 }
3372
3373 if (output_bfd != NULL)
3374 /* FIXME: See bfd_perform_relocation. Is this right? */
3375 return bfd_reloc_continue;
3376
3377 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3378 input_section,
3379 data, reloc_entry->address,
3380 (symbol->value
3381 + symbol->section->output_section->vma
3382 + symbol->section->output_offset),
3383 reloc_entry->addend);
3384}
3385
3386static bfd_reloc_status_type
3387nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3388 void *data, asection *input_section,
3389 bfd *output_bfd,
36591ba1
SL
3390 char **error_message ATTRIBUTE_UNUSED)
3391{
3392 /* This part is from bfd_elf_generic_reloc. */
3393 if (output_bfd != NULL
3394 && (symbol->flags & BSF_SECTION_SYM) == 0
3395 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3396 {
3397 reloc_entry->address += input_section->output_offset;
3398 return bfd_reloc_ok;
3399 }
3400
3401 if (output_bfd != NULL)
3402 /* FIXME: See bfd_perform_relocation. Is this right? */
3403 return bfd_reloc_continue;
3404
3405 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3406 input_section,
3407 data, reloc_entry->address,
3408 (symbol->value
3409 + symbol->section->output_section->vma
3410 + symbol->section->output_offset),
3411 reloc_entry->addend);
3412}
3413
3414static bfd_reloc_status_type
3415nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3416 void *data, asection *input_section,
3417 bfd *output_bfd,
36591ba1
SL
3418 char **error_message ATTRIBUTE_UNUSED)
3419{
3420 /* This part is from bfd_elf_generic_reloc. */
3421 if (output_bfd != NULL
3422 && (symbol->flags & BSF_SECTION_SYM) == 0
3423 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3424 {
3425 reloc_entry->address += input_section->output_offset;
3426 return bfd_reloc_ok;
3427 }
3428
3429 if (output_bfd != NULL)
3430 /* FIXME: See bfd_perform_relocation. Is this right? */
3431 return bfd_reloc_continue;
3432
3433 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3434 input_section,
3435 data, reloc_entry->address,
3436 (symbol->value
3437 + symbol->section->output_section->vma
3438 + symbol->section->output_offset),
3439 reloc_entry->addend);
3440}
3441
3442static bfd_reloc_status_type
3443nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3444 asymbol *symbol, void *data,
3445 asection *input_section, bfd *output_bfd,
3446 char **error_message ATTRIBUTE_UNUSED)
3447{
3448 /* This part is from bfd_elf_generic_reloc. */
3449 if (output_bfd != NULL
3450 && (symbol->flags & BSF_SECTION_SYM) == 0
3451 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3452 {
3453 reloc_entry->address += input_section->output_offset;
3454 return bfd_reloc_ok;
3455 }
3456
3457 if (output_bfd != NULL)
3458 /* FIXME: See bfd_perform_relocation. Is this right? */
3459 return bfd_reloc_continue;
3460
3461 return nios2_elf32_do_pcrel_lo16_relocate (
3462 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3463 (symbol->value + symbol->section->output_section->vma
3464 + symbol->section->output_offset),
3465 reloc_entry->addend);
3466}
3467
3468static bfd_reloc_status_type
3469nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3470 asymbol *symbol, void *data,
3471 asection *input_section, bfd *output_bfd,
3472 char **error_message ATTRIBUTE_UNUSED)
3473{
3474 /* This part is from bfd_elf_generic_reloc. */
3475 if (output_bfd != NULL
3476 && (symbol->flags & BSF_SECTION_SYM) == 0
3477 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3478 {
3479 reloc_entry->address += input_section->output_offset;
3480 return bfd_reloc_ok;
3481 }
3482
3483 if (output_bfd != NULL)
3484 /* FIXME: See bfd_perform_relocation. Is this right? */
3485 return bfd_reloc_continue;
3486
3487 return nios2_elf32_do_pcrel_hiadj16_relocate (
3488 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3489 (symbol->value + symbol->section->output_section->vma
3490 + symbol->section->output_offset),
3491 reloc_entry->addend);
3492}
3493
3494static bfd_reloc_status_type
3495nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3496 void *data, asection *input_section,
3497 bfd *output_bfd,
36591ba1
SL
3498 char **error_message ATTRIBUTE_UNUSED)
3499{
3500 /* This part is from bfd_elf_generic_reloc. */
3501 if (output_bfd != NULL
3502 && (symbol->flags & BSF_SECTION_SYM) == 0
3503 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3504 {
3505 reloc_entry->address += input_section->output_offset;
3506 return bfd_reloc_ok;
3507 }
3508
3509 if (output_bfd != NULL)
3510 /* FIXME: See bfd_perform_relocation. Is this right? */
3511 return bfd_reloc_continue;
3512
3513 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3514 input_section,
3515 data, reloc_entry->address,
3516 (symbol->value
3517 + symbol->section->output_section->vma
3518 + symbol->section->output_offset),
3519 reloc_entry->addend);
3520}
3521
3522static bfd_reloc_status_type
3523nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3524 void *data, asection *input_section,
3525 bfd *output_bfd,
36591ba1
SL
3526 char **error_message ATTRIBUTE_UNUSED)
3527{
3528 /* This part is from bfd_elf_generic_reloc. */
3529 if (output_bfd != NULL
3530 && (symbol->flags & BSF_SECTION_SYM) == 0
3531 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3532 {
3533 reloc_entry->address += input_section->output_offset;
3534 return bfd_reloc_ok;
3535 }
3536
3537 if (output_bfd != NULL)
3538 /* FIXME: See bfd_perform_relocation. Is this right? */
3539 return bfd_reloc_continue;
3540
3541 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3542 input_section,
3543 data, reloc_entry->address,
3544 (symbol->value
3545 + symbol->section->output_section->vma
3546 + symbol->section->output_offset),
3547 reloc_entry->addend);
3548}
3549
3550static bfd_reloc_status_type
3551nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3552 void *data, asection *input_section,
36591ba1
SL
3553 bfd *output_bfd, char **msg)
3554{
3555 bfd_vma relocation;
3556 bfd_vma gp;
3557 bfd_reloc_status_type r;
3558
3559
3560 /* This part is from bfd_elf_generic_reloc. */
3561 if (output_bfd != NULL
3562 && (symbol->flags & BSF_SECTION_SYM) == 0
3563 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3564 {
3565 reloc_entry->address += input_section->output_offset;
3566 return bfd_reloc_ok;
3567 }
3568
3569 if (output_bfd != NULL)
3570 /* FIXME: See bfd_perform_relocation. Is this right? */
3571 return bfd_reloc_continue;
3572
3573 relocation = (symbol->value
3574 + symbol->section->output_section->vma
3575 + symbol->section->output_offset);
3576
3577 /* This assumes we've already cached the _gp symbol. */
3578 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
3579 if (r == bfd_reloc_ok)
3580 {
3581 relocation = relocation + reloc_entry->addend - gp;
3582 reloc_entry->addend = 0;
3583 if ((signed) relocation < -32768 || (signed) relocation > 32767)
3584 {
3585 *msg = _("global pointer relative address out of range");
3586 r = bfd_reloc_outofrange;
3587 }
3588 else
3589 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3590 input_section,
3591 data, reloc_entry->address,
3592 relocation, reloc_entry->addend);
3593 }
3594
3595 return r;
3596}
3597
3598static bfd_reloc_status_type
3599nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3600 void *data, asection *input_section,
36591ba1
SL
3601 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3602{
3603 /* This part is from bfd_elf_generic_reloc. */
3604 if (output_bfd != NULL
3605 && (symbol->flags & BSF_SECTION_SYM) == 0
3606 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3607 {
3608 reloc_entry->address += input_section->output_offset;
3609 return bfd_reloc_ok;
3610 }
3611
3612 if (output_bfd != NULL)
3613 /* FIXME: See bfd_perform_relocation. Is this right? */
3614 return bfd_reloc_continue;
3615
3616 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3617 input_section,
3618 data, reloc_entry->address,
3619 (symbol->value
3620 + symbol->section->output_section->vma
3621 + symbol->section->output_offset),
3622 reloc_entry->addend);
3623}
3624
3625static bfd_reloc_status_type
3626nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3627 void *data, asection *input_section,
36591ba1
SL
3628 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3629{
3630 /* This part is from bfd_elf_generic_reloc. */
3631 if (output_bfd != NULL
3632 && (symbol->flags & BSF_SECTION_SYM) == 0
3633 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3634 {
3635 reloc_entry->address += input_section->output_offset;
3636 return bfd_reloc_ok;
3637 }
3638
3639 if (output_bfd != NULL)
3640 /* FIXME: See bfd_perform_relocation. Is this right? */
3641 return bfd_reloc_continue;
3642
3643 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3644 input_section,
3645 data, reloc_entry->address,
3646 (symbol->value
3647 + symbol->section->output_section->vma
3648 + symbol->section->output_offset),
3649 reloc_entry->addend);
3650}
3651
3652static bfd_reloc_status_type
3653nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3654 void *data, asection *input_section,
36591ba1
SL
3655 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3656{
3657 /* This part is from bfd_elf_generic_reloc. */
3658 if (output_bfd != NULL
3659 && (symbol->flags & BSF_SECTION_SYM) == 0
3660 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3661 {
3662 reloc_entry->address += input_section->output_offset;
3663 return bfd_reloc_ok;
3664 }
3665
3666 if (output_bfd != NULL)
3667 /* FIXME: See bfd_perform_relocation. Is this right? */
3668 return bfd_reloc_continue;
3669
3670 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3671 input_section,
3672 data, reloc_entry->address,
3673 (symbol->value
3674 + symbol->section->output_section->vma
3675 + symbol->section->output_offset),
3676 reloc_entry->addend);
3677}
3678
1b786873 3679
36591ba1
SL
3680/* Implement elf_backend_relocate_section. */
3681static bfd_boolean
3682nios2_elf32_relocate_section (bfd *output_bfd,
3683 struct bfd_link_info *info,
3684 bfd *input_bfd,
3685 asection *input_section,
3686 bfd_byte *contents,
3687 Elf_Internal_Rela *relocs,
3688 Elf_Internal_Sym *local_syms,
3689 asection **local_sections)
3690{
3691 Elf_Internal_Shdr *symtab_hdr;
3692 struct elf_link_hash_entry **sym_hashes;
3693 Elf_Internal_Rela *rel;
3694 Elf_Internal_Rela *relend;
3695 struct elf32_nios2_link_hash_table *htab;
3696 asection *sgot;
3697 asection *splt;
3698 asection *sreloc = NULL;
3699 bfd_vma *local_got_offsets;
82e91538 3700 bfd_vma got_base;
36591ba1
SL
3701
3702 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3703 sym_hashes = elf_sym_hashes (input_bfd);
3704 relend = relocs + input_section->reloc_count;
3705
3706 htab = elf32_nios2_hash_table (info);
3707 sgot = htab->root.sgot;
3708 splt = htab->root.splt;
3709 local_got_offsets = elf_local_got_offsets (input_bfd);
3710
4ef97a1b 3711 if (htab->h_gp_got == NULL)
82e91538
SL
3712 got_base = 0;
3713 else
4ef97a1b 3714 got_base = htab->h_gp_got->root.u.def.value;
82e91538 3715
36591ba1
SL
3716 for (rel = relocs; rel < relend; rel++)
3717 {
3718 reloc_howto_type *howto;
3719 unsigned long r_symndx;
3720 Elf_Internal_Sym *sym;
3721 asection *sec;
3722 struct elf_link_hash_entry *h;
3723 struct elf32_nios2_link_hash_entry *eh;
3724 bfd_vma relocation;
3725 bfd_vma gp;
36591ba1
SL
3726 bfd_reloc_status_type r = bfd_reloc_ok;
3727 const char *name = NULL;
3728 int r_type;
3729 const char *format;
a31b8bd9
AM
3730 char *msgbuf = NULL;
3731 char *msg = NULL;
36591ba1
SL
3732 bfd_boolean unresolved_reloc;
3733 bfd_vma off;
3734 int use_plt;
3735
3736 r_type = ELF32_R_TYPE (rel->r_info);
3737 r_symndx = ELF32_R_SYM (rel->r_info);
3738
8c163c5a 3739 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
36591ba1
SL
3740 h = NULL;
3741 sym = NULL;
3742 sec = NULL;
3743
3744 if (r_symndx < symtab_hdr->sh_info)
3745 {
3746 sym = local_syms + r_symndx;
3747 sec = local_sections[r_symndx];
3748 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3749 }
3750 else
3751 {
62d887d4 3752 bfd_boolean warned, ignored;
36591ba1
SL
3753
3754 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3755 r_symndx, symtab_hdr, sym_hashes,
3756 h, sec, relocation,
62d887d4 3757 unresolved_reloc, warned, ignored);
36591ba1
SL
3758 }
3759
3760 if (sec && discarded_section (sec))
3761 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3762 rel, 1, relend, howto, 0, contents);
3763
3764 /* Nothing more to do unless this is a final link. */
0e1862bb 3765 if (bfd_link_relocatable (info))
36591ba1
SL
3766 continue;
3767
36591ba1
SL
3768 if (howto)
3769 {
31a53da5
L
3770 bfd_boolean resolved_to_zero;
3771
3772 resolved_to_zero = (h != NULL
3773 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
36591ba1
SL
3774 switch (howto->type)
3775 {
3776 case R_NIOS2_HI16:
3777 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3778 input_section,
3779 contents, rel->r_offset,
3780 relocation, rel->r_addend);
3781 break;
3782 case R_NIOS2_LO16:
3783 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3784 input_section,
3785 contents, rel->r_offset,
3786 relocation, rel->r_addend);
3787 break;
3788 case R_NIOS2_PCREL_LO:
3789 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3790 input_section,
3791 contents,
3792 rel->r_offset,
3793 relocation,
3794 rel->r_addend);
3795 break;
3796 case R_NIOS2_HIADJ16:
3797 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3798 input_section, contents,
3799 rel->r_offset, relocation,
3800 rel->r_addend);
3801 break;
3802 case R_NIOS2_PCREL_HA:
3803 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3804 input_section,
3805 contents,
3806 rel->r_offset,
3807 relocation,
3808 rel->r_addend);
3809 break;
3810 case R_NIOS2_PCREL16:
3811 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3812 input_section, contents,
3813 rel->r_offset, relocation,
3814 rel->r_addend);
3815 break;
3816 case R_NIOS2_GPREL:
3817 /* Turns an absolute address into a gp-relative address. */
3818 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3819 {
a7be2893
SL
3820 bfd_vma reloc_address;
3821
3822 if (sec && sec->output_section)
3823 reloc_address = (sec->output_section->vma
3824 + sec->output_offset
3825 + rel->r_offset);
3826 else
3827 reloc_address = 0;
3828
36591ba1 3829 format = _("global pointer relative relocation at address "
a31b8bd9 3830 "%#" PRIx64 " when _gp not defined\n");
34d75fb5
AM
3831 if (asprintf (&msgbuf, format,
3832 (uint64_t) reloc_address) == -1)
3833 msgbuf = NULL;
36591ba1
SL
3834 msg = msgbuf;
3835 r = bfd_reloc_dangerous;
3836 }
3837 else
3838 {
3839 bfd_vma symbol_address = rel->r_addend + relocation;
a7be2893 3840 relocation = symbol_address - gp;
36591ba1
SL
3841 rel->r_addend = 0;
3842 if (((signed) relocation < -32768
3843 || (signed) relocation > 32767)
3844 && (!h
3845 || h->root.type == bfd_link_hash_defined
3846 || h->root.type == bfd_link_hash_defweak))
3847 {
a7be2893
SL
3848 if (h)
3849 name = h->root.root.string;
a31b8bd9
AM
3850 else
3851 {
3852 name = (bfd_elf_string_from_elf_section
3853 (input_bfd, symtab_hdr->sh_link,
3854 sym->st_name));
3855 if (name == NULL || *name == '\0')
fd361982 3856 name = bfd_section_name (sec);
a31b8bd9 3857 }
695344c0 3858 /* xgettext:c-format */
a31b8bd9
AM
3859 format = _("unable to reach %s (at %#" PRIx64 ") from "
3860 "the global pointer (at %#" PRIx64 ") "
3861 "because the offset (%" PRId64 ") is out of "
3862 "the allowed range, -32678 to 32767\n" );
34d75fb5
AM
3863 if (asprintf (&msgbuf, format, name,
3864 (uint64_t) symbol_address, (uint64_t) gp,
3865 (int64_t) relocation) == -1)
3866 msgbuf = NULL;
36591ba1
SL
3867 msg = msgbuf;
3868 r = bfd_reloc_outofrange;
3869 }
3870 else
3871 r = _bfd_final_link_relocate (howto, input_bfd,
3872 input_section, contents,
3873 rel->r_offset, relocation,
3874 rel->r_addend);
3875 }
36591ba1
SL
3876 break;
3877 case R_NIOS2_UJMP:
3878 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3879 input_section,
3880 contents, rel->r_offset,
3881 relocation, rel->r_addend);
3882 break;
3883 case R_NIOS2_CJMP:
3884 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3885 input_section,
3886 contents, rel->r_offset,
3887 relocation, rel->r_addend);
3888 break;
3889 case R_NIOS2_CALLR:
3890 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3891 input_section, contents,
3892 rel->r_offset, relocation,
3893 rel->r_addend);
3894 break;
3895 case R_NIOS2_CALL26:
78058a5e 3896 case R_NIOS2_CALL26_NOAT:
36591ba1
SL
3897 /* If we have a call to an undefined weak symbol, we just want
3898 to stuff a zero in the bits of the call instruction and
3899 bypass the normal call26 relocation handling, because it'll
3900 diagnose an overflow error if address 0 isn't in the same
3901 256MB segment as the call site. Presumably the call
3902 should be guarded by a null check anyway. */
3903 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3904 {
3905 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3906 r = _bfd_final_link_relocate (howto, input_bfd,
3907 input_section, contents,
3908 rel->r_offset, relocation,
3909 rel->r_addend);
3910 break;
3911 }
3912 /* Handle relocations which should use the PLT entry.
3913 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3914 which may point to a PLT entry, but we don't need to handle
3915 that here. If we created a PLT entry, all branches in this
3916 object should go to it. */
3917 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3918 {
3919 /* If we've created a .plt section, and assigned a PLT entry
3920 to this function, it should not be known to bind locally.
3921 If it were, we would have cleared the PLT entry. */
3922 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3923
3924 relocation = (splt->output_section->vma
3925 + splt->output_offset
3926 + h->plt.offset);
3927
3928 unresolved_reloc = FALSE;
3929 }
78058a5e
SL
3930 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3931 256MB segment. Replace the target with a reference to a
3932 trampoline instead.
3933 Note that htab->stub_group is null if relaxation has been
3934 disabled by the --no-relax linker command-line option, so
3935 we can use that to skip this processing entirely. */
3936 if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3937 {
3938 bfd_vma dest = relocation + rel->r_addend;
3939 enum elf32_nios2_stub_type stub_type;
3940
3941 eh = (struct elf32_nios2_link_hash_entry *)h;
3942 stub_type = nios2_type_of_stub (input_section, rel, eh,
3943 htab, dest, NULL);
3944
3945 if (stub_type != nios2_stub_none)
3946 {
3947 struct elf32_nios2_stub_hash_entry *hsh;
3948
3949 hsh = nios2_get_stub_entry (input_section, sec,
3950 eh, rel, htab, stub_type);
3951 if (hsh == NULL)
3952 {
3953 r = bfd_reloc_undefined;
3954 break;
3955 }
3956
3957 dest = (hsh->stub_offset
3958 + hsh->stub_sec->output_offset
3959 + hsh->stub_sec->output_section->vma);
3960 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3961 input_section,
3962 contents,
3963 rel->r_offset,
3964 dest, 0);
3965 break;
3966 }
3967 }
3968
3969 /* Normal case. */
36591ba1
SL
3970 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3971 input_section, contents,
3972 rel->r_offset, relocation,
3973 rel->r_addend);
3974 break;
3975 case R_NIOS2_ALIGN:
3976 r = bfd_reloc_ok;
3977 /* For symmetry this would be
3978 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3979 input_section, contents,
3980 rel->r_offset, relocation,
3981 rel->r_addend);
3982 but do_ignore_reloc would do no more than return
3983 bfd_reloc_ok. */
3984 break;
3985
3986 case R_NIOS2_GOT16:
3987 case R_NIOS2_CALL16:
1c2de463
SL
3988 case R_NIOS2_GOT_LO:
3989 case R_NIOS2_GOT_HA:
3990 case R_NIOS2_CALL_LO:
3991 case R_NIOS2_CALL_HA:
36591ba1
SL
3992 /* Relocation is to the entry for this symbol in the
3993 global offset table. */
3994 if (sgot == NULL)
3995 {
3996 r = bfd_reloc_notsupported;
3997 break;
3998 }
3999
4000 use_plt = 0;
4001
4002 if (h != NULL)
4003 {
4004 bfd_boolean dyn;
4005
4006 eh = (struct elf32_nios2_link_hash_entry *)h;
1c2de463 4007 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
4008 && h->plt.offset != (bfd_vma) -1);
4009
4010 off = h->got.offset;
4011 BFD_ASSERT (off != (bfd_vma) -1);
4ef97a1b 4012 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
4013 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4014 bfd_link_pic (info),
4015 h)
4016 || (bfd_link_pic (info)
36591ba1 4017 && SYMBOL_REFERENCES_LOCAL (info, h))
31a53da5
L
4018 || ((ELF_ST_VISIBILITY (h->other)
4019 || resolved_to_zero)
36591ba1
SL
4020 && h->root.type == bfd_link_hash_undefweak))
4021 {
4022 /* This is actually a static link, or it is a -Bsymbolic
4023 link and the symbol is defined locally. We must
4024 initialize this entry in the global offset table.
4025 Since the offset must always be a multiple of 4, we
4026 use the least significant bit to record whether we
4027 have initialized it already.
4028
4029 When doing a dynamic link, we create a .rela.got
4030 relocation entry to initialize the value. This is
4031 done in the finish_dynamic_symbol routine. */
4032 if ((off & 1) != 0)
4033 off &= ~1;
4034 else
4035 {
4036 bfd_put_32 (output_bfd, relocation,
4037 sgot->contents + off);
4038 h->got.offset |= 1;
4039 }
4040 }
4041 else
4042 unresolved_reloc = FALSE;
4043 }
4044 else
4045 {
4046 BFD_ASSERT (local_got_offsets != NULL
4047 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4048
4049 off = local_got_offsets[r_symndx];
4050
4051 /* The offset must always be a multiple of 4. We use the
4052 least significant bit to record whether we have already
4053 generated the necessary reloc. */
4054 if ((off & 1) != 0)
4055 off &= ~1;
4056 else
4057 {
4058 bfd_put_32 (output_bfd, relocation,
4059 sgot->contents + off);
4060
0e1862bb 4061 if (bfd_link_pic (info))
36591ba1
SL
4062 {
4063 asection *srelgot;
4064 Elf_Internal_Rela outrel;
4065 bfd_byte *loc;
4066
4067 srelgot = htab->root.srelgot;
4068 BFD_ASSERT (srelgot != NULL);
4069
4070 outrel.r_addend = relocation;
4071 outrel.r_offset = (sgot->output_section->vma
4072 + sgot->output_offset
4073 + off);
4074 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4075 loc = srelgot->contents;
4076 loc += (srelgot->reloc_count++ *
4077 sizeof (Elf32_External_Rela));
4078 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4079 }
4080
4081 local_got_offsets[r_symndx] |= 1;
4082 }
4083 }
4084
0e1862bb 4085 if (use_plt && bfd_link_pic (info))
36591ba1
SL
4086 {
4087 off = ((h->plt.offset - 24) / 12 + 3) * 4;
82e91538
SL
4088 relocation = (htab->root.sgotplt->output_offset + off
4089 - got_base);
36591ba1
SL
4090 }
4091 else
82e91538 4092 relocation = sgot->output_offset + off - got_base;
36591ba1
SL
4093
4094 /* This relocation does not use the addend. */
4095 rel->r_addend = 0;
4096
1c2de463
SL
4097 switch (howto->type)
4098 {
4099 case R_NIOS2_GOT_LO:
4100 case R_NIOS2_CALL_LO:
4101 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4102 input_section, contents,
4103 rel->r_offset, relocation,
4104 rel->r_addend);
4105 break;
4106 case R_NIOS2_GOT_HA:
4107 case R_NIOS2_CALL_HA:
4108 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4109 input_section, contents,
4110 rel->r_offset,
4111 relocation,
4112 rel->r_addend);
4113 break;
4114 default:
4115 r = _bfd_final_link_relocate (howto, input_bfd,
4116 input_section, contents,
4117 rel->r_offset, relocation,
4118 rel->r_addend);
4119 break;
4120 }
36591ba1
SL
4121 break;
4122
4123 case R_NIOS2_GOTOFF_LO:
4124 case R_NIOS2_GOTOFF_HA:
4125 case R_NIOS2_GOTOFF:
82e91538 4126 /* Relocation is relative to the global offset table pointer. */
36591ba1
SL
4127
4128 BFD_ASSERT (sgot != NULL);
4129 if (sgot == NULL)
4130 {
4131 r = bfd_reloc_notsupported;
4132 break;
4133 }
4134
d9972968
CLT
4135 /* Note that sgot->output_offset is not involved in this
4136 calculation. We always want the start of .got. */
4137 relocation -= sgot->output_section->vma;
4138
4139 /* Now we adjust the relocation to be relative to the GOT pointer
4140 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4141 relocation -= got_base;
82e91538 4142
36591ba1
SL
4143 switch (howto->type)
4144 {
4145 case R_NIOS2_GOTOFF_LO:
4146 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4147 input_section, contents,
4148 rel->r_offset, relocation,
4149 rel->r_addend);
4150 break;
4151 case R_NIOS2_GOTOFF_HA:
4152 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4153 input_section, contents,
4154 rel->r_offset,
4155 relocation,
4156 rel->r_addend);
4157 break;
4158 default:
4159 r = _bfd_final_link_relocate (howto, input_bfd,
4160 input_section, contents,
4161 rel->r_offset, relocation,
4162 rel->r_addend);
4163 break;
4164 }
4165 break;
4166
4167 case R_NIOS2_TLS_LDO16:
4168 relocation -= dtpoff_base (info) + DTP_OFFSET;
4169
4170 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4171 contents, rel->r_offset,
4172 relocation, rel->r_addend);
4173 break;
4174 case R_NIOS2_TLS_LDM16:
4175 if (htab->root.sgot == NULL)
4176 abort ();
4177
4178 off = htab->tls_ldm_got.offset;
4179
4180 if ((off & 1) != 0)
4181 off &= ~1;
4182 else
4183 {
4184 /* If we don't know the module number, create a relocation
4185 for it. */
0e1862bb 4186 if (bfd_link_pic (info))
36591ba1
SL
4187 {
4188 Elf_Internal_Rela outrel;
4189 bfd_byte *loc;
4190
4191 if (htab->root.srelgot == NULL)
4192 abort ();
4193
4194 outrel.r_addend = 0;
4195 outrel.r_offset = (htab->root.sgot->output_section->vma
4196 + htab->root.sgot->output_offset
4197 + off);
4198 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4199
4200 loc = htab->root.srelgot->contents;
4201 loc += (htab->root.srelgot->reloc_count++
4202 * sizeof (Elf32_External_Rela));
4203 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4204 }
4205 else
4206 bfd_put_32 (output_bfd, 1,
4207 htab->root.sgot->contents + off);
4208
4209 htab->tls_ldm_got.offset |= 1;
4210 }
4211
82e91538 4212 relocation = htab->root.sgot->output_offset + off - got_base;
36591ba1
SL
4213
4214 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4215 contents, rel->r_offset,
4216 relocation, rel->r_addend);
4217
4218 break;
4219 case R_NIOS2_TLS_GD16:
4220 case R_NIOS2_TLS_IE16:
4221 {
4222 int indx;
4223 char tls_type;
4224
4225 if (htab->root.sgot == NULL)
4226 abort ();
4227
4228 indx = 0;
4229 if (h != NULL)
4230 {
4231 bfd_boolean dyn;
4232 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
4233 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4234 bfd_link_pic (info),
4235 h)
4236 && (!bfd_link_pic (info)
36591ba1
SL
4237 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4238 {
4239 unresolved_reloc = FALSE;
4240 indx = h->dynindx;
4241 }
4242 off = h->got.offset;
4243 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4244 ->tls_type);
4245 }
4246 else
4247 {
4248 if (local_got_offsets == NULL)
4249 abort ();
4250 off = local_got_offsets[r_symndx];
4251 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4252 [r_symndx]);
4253 }
4254
4255 if (tls_type == GOT_UNKNOWN)
4256 abort ();
4257
4258 if ((off & 1) != 0)
4259 off &= ~1;
4260 else
4261 {
4262 bfd_boolean need_relocs = FALSE;
4263 Elf_Internal_Rela outrel;
4264 bfd_byte *loc = NULL;
4265 int cur_off = off;
4266
4267 /* The GOT entries have not been initialized yet. Do it
4268 now, and emit any relocations. If both an IE GOT and a
4269 GD GOT are necessary, we emit the GD first. */
4270
0e1862bb 4271 if ((bfd_link_pic (info) || indx != 0)
36591ba1 4272 && (h == NULL
31a53da5
L
4273 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4274 && !resolved_to_zero)
36591ba1
SL
4275 || h->root.type != bfd_link_hash_undefweak))
4276 {
4277 need_relocs = TRUE;
4278 if (htab->root.srelgot == NULL)
4279 abort ();
4280 loc = htab->root.srelgot->contents;
4281 loc += (htab->root.srelgot->reloc_count *
4282 sizeof (Elf32_External_Rela));
4283 }
4284
4285 if (tls_type & GOT_TLS_GD)
4286 {
4287 if (need_relocs)
4288 {
4289 outrel.r_addend = 0;
4290 outrel.r_offset = (htab->root.sgot->output_section->vma
4291 + htab->root.sgot->output_offset
4292 + cur_off);
4293 outrel.r_info = ELF32_R_INFO (indx,
4294 R_NIOS2_TLS_DTPMOD);
4295
4296 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4297 loc);
4298 htab->root.srelgot->reloc_count++;
4299 loc += sizeof (Elf32_External_Rela);
4300
4301 if (indx == 0)
4302 bfd_put_32 (output_bfd,
4303 (relocation - dtpoff_base (info) -
4304 DTP_OFFSET),
4305 htab->root.sgot->contents + cur_off + 4);
4306 else
4307 {
4308 outrel.r_addend = 0;
4309 outrel.r_info = ELF32_R_INFO (indx,
4310 R_NIOS2_TLS_DTPREL);
4311 outrel.r_offset += 4;
4312
4313 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4314 loc);
4315 htab->root.srelgot->reloc_count++;
4316 loc += sizeof (Elf32_External_Rela);
4317 }
4318 }
4319 else
4320 {
4321 /* If we are not emitting relocations for a
4322 general dynamic reference, then we must be in a
4323 static link or an executable link with the
4324 symbol binding locally. Mark it as belonging
4325 to module 1, the executable. */
4326 bfd_put_32 (output_bfd, 1,
4327 htab->root.sgot->contents + cur_off);
4328 bfd_put_32 (output_bfd, (relocation -
4329 dtpoff_base (info) -
4330 DTP_OFFSET),
4331 htab->root.sgot->contents + cur_off + 4);
4332 }
4333
4334 cur_off += 8;
4335 }
4336
4337 if (tls_type & GOT_TLS_IE)
4338 {
4339 if (need_relocs)
4340 {
4341 if (indx == 0)
4342 outrel.r_addend = (relocation -
4343 dtpoff_base (info));
4344 else
4345 outrel.r_addend = 0;
4346 outrel.r_offset = (htab->root.sgot->output_section->vma
4347 + htab->root.sgot->output_offset
4348 + cur_off);
4349 outrel.r_info = ELF32_R_INFO (indx,
4350 R_NIOS2_TLS_TPREL);
4351
4352 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4353 loc);
4354 htab->root.srelgot->reloc_count++;
4355 loc += sizeof (Elf32_External_Rela);
4356 }
4357 else
4358 bfd_put_32 (output_bfd, (tpoff (info, relocation)
4359 - TP_OFFSET),
4360 htab->root.sgot->contents + cur_off);
4361 cur_off += 4;
4362 }
4363
4364 if (h != NULL)
4365 h->got.offset |= 1;
4366 else
4367 local_got_offsets[r_symndx] |= 1;
4368 }
4369
4370 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4371 off += 8;
82e91538 4372 relocation = htab->root.sgot->output_offset + off - got_base;
36591ba1
SL
4373
4374 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4375 contents, rel->r_offset,
4376 relocation, rel->r_addend);
4377 }
4378
4379 break;
4380 case R_NIOS2_TLS_LE16:
3cbc1e5e 4381 if (bfd_link_dll (info))
36591ba1 4382 {
4eca0228 4383 _bfd_error_handler
695344c0 4384 /* xgettext:c-format */
2dcf00ce 4385 (_("%pB(%pA+%#" PRIx64 "): %s relocation not "
36591ba1
SL
4386 "permitted in shared object"),
4387 input_bfd, input_section,
2dcf00ce 4388 (uint64_t) rel->r_offset, howto->name);
36591ba1
SL
4389 return FALSE;
4390 }
4391 else
4392 relocation = tpoff (info, relocation) - TP_OFFSET;
4393
4394 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4395 contents, rel->r_offset,
4396 relocation, rel->r_addend);
4397 break;
4398
4399 case R_NIOS2_BFD_RELOC_32:
0e1862bb 4400 if (bfd_link_pic (info)
36591ba1
SL
4401 && (input_section->flags & SEC_ALLOC) != 0
4402 && (h == NULL
31a53da5
L
4403 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4404 && !resolved_to_zero)
36591ba1
SL
4405 || h->root.type != bfd_link_hash_undefweak))
4406 {
4407 Elf_Internal_Rela outrel;
4408 bfd_byte *loc;
4409 bfd_boolean skip, relocate;
4410
4411 /* When generating a shared object, these relocations
4412 are copied into the output file to be resolved at run
4413 time. */
4414
4415 skip = FALSE;
4416 relocate = FALSE;
4417
4418 outrel.r_offset
4419 = _bfd_elf_section_offset (output_bfd, info,
4420 input_section, rel->r_offset);
4421 if (outrel.r_offset == (bfd_vma) -1)
4422 skip = TRUE;
4423 else if (outrel.r_offset == (bfd_vma) -2)
4424 skip = TRUE, relocate = TRUE;
4425 outrel.r_offset += (input_section->output_section->vma
4426 + input_section->output_offset);
4427
4428 if (skip)
4429 memset (&outrel, 0, sizeof outrel);
4430 else if (h != NULL
4431 && h->dynindx != -1
0e1862bb 4432 && (!bfd_link_pic (info)
a496fbc8 4433 || !SYMBOLIC_BIND (info, h)
36591ba1
SL
4434 || !h->def_regular))
4435 {
4436 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4437 outrel.r_addend = rel->r_addend;
4438 }
4439 else
4440 {
4441 /* This symbol is local, or marked to become local. */
4442 outrel.r_addend = relocation + rel->r_addend;
4443 relocate = TRUE;
4444 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4445 }
4446
4447 sreloc = elf_section_data (input_section)->sreloc;
4448 if (sreloc == NULL)
4449 abort ();
4450
4451 loc = sreloc->contents;
4452 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4453 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4454
4455 /* This reloc will be computed at runtime, so there's no
4456 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4457 relocations that have been turned into
4458 R_NIOS2_RELATIVE. */
4459 if (!relocate)
4460 break;
4461 }
4462
4463 r = _bfd_final_link_relocate (howto, input_bfd,
4464 input_section, contents,
4465 rel->r_offset, relocation,
4466 rel->r_addend);
4467 break;
4468
4469 case R_NIOS2_TLS_DTPREL:
4470 relocation -= dtpoff_base (info);
4471 /* Fall through. */
4472
4473 default:
4474 r = _bfd_final_link_relocate (howto, input_bfd,
4475 input_section, contents,
4476 rel->r_offset, relocation,
4477 rel->r_addend);
4478 break;
4479 }
4480 }
4481 else
4482 r = bfd_reloc_notsupported;
4483
4484 if (r != bfd_reloc_ok)
4485 {
4486 if (h != NULL)
4487 name = h->root.root.string;
4488 else
4489 {
4490 name = bfd_elf_string_from_elf_section (input_bfd,
4491 symtab_hdr->sh_link,
4492 sym->st_name);
4493 if (name == NULL || *name == '\0')
fd361982 4494 name = bfd_section_name (sec);
36591ba1
SL
4495 }
4496
4497 switch (r)
4498 {
4499 case bfd_reloc_overflow:
1a72702b
AM
4500 (*info->callbacks->reloc_overflow) (info, NULL, name,
4501 howto->name, (bfd_vma) 0,
4502 input_bfd, input_section,
4503 rel->r_offset);
36591ba1
SL
4504 break;
4505
4506 case bfd_reloc_undefined:
1a72702b
AM
4507 (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4508 input_section,
4509 rel->r_offset, TRUE);
36591ba1
SL
4510 break;
4511
4512 case bfd_reloc_outofrange:
4513 if (msg == NULL)
4514 msg = _("relocation out of range");
4515 break;
4516
4517 case bfd_reloc_notsupported:
4518 if (msg == NULL)
4519 msg = _("unsupported relocation");
4520 break;
4521
4522 case bfd_reloc_dangerous:
4523 if (msg == NULL)
4524 msg = _("dangerous relocation");
4525 break;
4526
4527 default:
4528 if (msg == NULL)
4529 msg = _("unknown error");
4530 break;
4531 }
4532
4533 if (msg)
4534 {
1a72702b
AM
4535 (*info->callbacks->warning) (info, msg, name, input_bfd,
4536 input_section, rel->r_offset);
c9594989 4537 free (msgbuf);
36591ba1
SL
4538 return FALSE;
4539 }
4540 }
4541 }
4542 return TRUE;
4543}
4544
4545/* Implement elf-backend_section_flags:
4546 Convert NIOS2 specific section flags to bfd internal section flags. */
4547static bfd_boolean
8c803a2d 4548nios2_elf32_section_flags (const Elf_Internal_Shdr *hdr)
36591ba1
SL
4549{
4550 if (hdr->sh_flags & SHF_NIOS2_GPREL)
8c803a2d 4551 hdr->bfd_section->flags |= SEC_SMALL_DATA;
36591ba1
SL
4552
4553 return TRUE;
4554}
4555
4556/* Implement elf_backend_fake_sections:
4557 Set the correct type for an NIOS2 ELF section. We do this by the
4558 section name, which is a hack, but ought to work. */
4559static bfd_boolean
4560nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4561 Elf_Internal_Shdr *hdr, asection *sec)
4562{
fd361982 4563 const char *name = bfd_section_name (sec);
36591ba1
SL
4564
4565 if ((sec->flags & SEC_SMALL_DATA)
4566 || strcmp (name, ".sdata") == 0
4567 || strcmp (name, ".sbss") == 0
4568 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4569 hdr->sh_flags |= SHF_NIOS2_GPREL;
4570
4571 return TRUE;
4572}
4573
4574/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4575 shortcuts to them in our hash table. */
4576static bfd_boolean
4577create_got_section (bfd *dynobj, struct bfd_link_info *info)
4578{
4579 struct elf32_nios2_link_hash_table *htab;
82e91538 4580 struct elf_link_hash_entry *h;
36591ba1
SL
4581
4582 htab = elf32_nios2_hash_table (info);
4583
4584 if (! _bfd_elf_create_got_section (dynobj, info))
4585 return FALSE;
4586
4587 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4588 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
fd361982 4589 if (!bfd_set_section_alignment (htab->root.sgotplt, 4))
36591ba1
SL
4590 return FALSE;
4591
82e91538
SL
4592 /* The Nios II ABI specifies that GOT-relative relocations are relative
4593 to the linker-created symbol _gp_got, rather than using
4594 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4595 points to the base of the GOT while _gp_got may include a bias. */
4596 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4597 "_gp_got");
4ef97a1b 4598 htab->h_gp_got = h;
82e91538
SL
4599 if (h == NULL)
4600 return FALSE;
4601
36591ba1
SL
4602 return TRUE;
4603}
4604
4605/* Implement elf_backend_create_dynamic_sections:
4606 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4607 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4608 hash table. */
4609static bfd_boolean
4610nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4611{
4612 struct elf32_nios2_link_hash_table *htab;
4613
4614 htab = elf32_nios2_hash_table (info);
4615 if (!htab->root.sgot && !create_got_section (dynobj, info))
4616 return FALSE;
4617
9d19e4fd
AM
4618 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4619 return FALSE;
36591ba1
SL
4620
4621 /* In order for the two loads in a shared object .PLTresolve to share the
4622 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4623 to a 16-byte boundary. This is because the addresses for these loads
4624 include the -(.plt+4) PIC correction. */
fd361982 4625 return bfd_set_section_alignment (htab->root.splt, 4);
36591ba1
SL
4626}
4627
4628/* Implement elf_backend_copy_indirect_symbol:
4629 Copy the extra info we tack onto an elf_link_hash_entry. */
4630static void
4631nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4632 struct elf_link_hash_entry *dir,
4633 struct elf_link_hash_entry *ind)
4634{
4635 struct elf32_nios2_link_hash_entry *edir, *eind;
4636
4637 edir = (struct elf32_nios2_link_hash_entry *) dir;
4638 eind = (struct elf32_nios2_link_hash_entry *) ind;
4639
36591ba1
SL
4640 if (ind->root.type == bfd_link_hash_indirect
4641 && dir->got.refcount <= 0)
4642 {
4643 edir->tls_type = eind->tls_type;
4644 eind->tls_type = GOT_UNKNOWN;
4645 }
4646
4647 edir->got_types_used |= eind->got_types_used;
4648
4649 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4650}
4651
965b1d80
SL
4652/* Set the right machine number for a NIOS2 ELF file. */
4653
4654static bfd_boolean
4655nios2_elf32_object_p (bfd *abfd)
4656{
4657 unsigned long mach;
4658
4659 mach = elf_elfheader (abfd)->e_flags;
4660
4661 switch (mach)
4662 {
4663 default:
4664 case EF_NIOS2_ARCH_R1:
4665 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4666 break;
4667 case EF_NIOS2_ARCH_R2:
4668 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4669 break;
4670 }
4671
4672 return TRUE;
4673}
4674
36591ba1
SL
4675/* Implement elf_backend_check_relocs:
4676 Look through the relocs for a section during the first phase. */
4677static bfd_boolean
4678nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4679 asection *sec, const Elf_Internal_Rela *relocs)
4680{
36591ba1
SL
4681 Elf_Internal_Shdr *symtab_hdr;
4682 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4683 const Elf_Internal_Rela *rel;
4684 const Elf_Internal_Rela *rel_end;
4685 struct elf32_nios2_link_hash_table *htab;
36591ba1
SL
4686 asection *sreloc = NULL;
4687 bfd_signed_vma *local_got_refcounts;
4688
0e1862bb 4689 if (bfd_link_relocatable (info))
36591ba1
SL
4690 return TRUE;
4691
36591ba1
SL
4692 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4693 sym_hashes = elf_sym_hashes (abfd);
4694 sym_hashes_end = (sym_hashes
4695 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
4696 if (!elf_bad_symtab (abfd))
4697 sym_hashes_end -= symtab_hdr->sh_info;
4698 local_got_refcounts = elf_local_got_refcounts (abfd);
4699
4700 htab = elf32_nios2_hash_table (info);
36591ba1
SL
4701
4702 rel_end = relocs + sec->reloc_count;
4703 for (rel = relocs; rel < rel_end; rel++)
4704 {
4705 unsigned int r_type;
4706 struct elf_link_hash_entry *h;
4707 unsigned long r_symndx;
4708
4709 r_symndx = ELF32_R_SYM (rel->r_info);
4710 if (r_symndx < symtab_hdr->sh_info)
4711 h = NULL;
4712 else
4713 {
4714 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4715 while (h->root.type == bfd_link_hash_indirect
4716 || h->root.type == bfd_link_hash_warning)
4717 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4718 }
4719
4720 r_type = ELF32_R_TYPE (rel->r_info);
4721
4722 switch (r_type)
4723 {
4724 case R_NIOS2_GOT16:
1c2de463
SL
4725 case R_NIOS2_GOT_LO:
4726 case R_NIOS2_GOT_HA:
36591ba1 4727 case R_NIOS2_CALL16:
1c2de463
SL
4728 case R_NIOS2_CALL_LO:
4729 case R_NIOS2_CALL_HA:
36591ba1
SL
4730 case R_NIOS2_TLS_GD16:
4731 case R_NIOS2_TLS_IE16:
4732 /* This symbol requires a global offset table entry. */
4733 {
4734 int tls_type, old_tls_type;
4735
4736 switch (r_type)
4737 {
4738 default:
4739 case R_NIOS2_GOT16:
1c2de463
SL
4740 case R_NIOS2_GOT_LO:
4741 case R_NIOS2_GOT_HA:
36591ba1 4742 case R_NIOS2_CALL16:
1c2de463
SL
4743 case R_NIOS2_CALL_LO:
4744 case R_NIOS2_CALL_HA:
36591ba1
SL
4745 tls_type = GOT_NORMAL;
4746 break;
4747 case R_NIOS2_TLS_GD16:
4748 tls_type = GOT_TLS_GD;
4749 break;
4750 case R_NIOS2_TLS_IE16:
4751 tls_type = GOT_TLS_IE;
4752 break;
4753 }
4754
36591ba1
SL
4755 if (h != NULL)
4756 {
4757 struct elf32_nios2_link_hash_entry *eh
4758 = (struct elf32_nios2_link_hash_entry *)h;
4759 h->got.refcount++;
4760 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
1c2de463
SL
4761 if (r_type == R_NIOS2_CALL16
4762 || r_type == R_NIOS2_CALL_LO
4763 || r_type == R_NIOS2_CALL_HA)
36591ba1
SL
4764 {
4765 /* Make sure a plt entry is created for this symbol if
4766 it turns out to be a function defined by a dynamic
4767 object. */
4768 h->plt.refcount++;
4769 h->needs_plt = 1;
4770 h->type = STT_FUNC;
1c2de463 4771 eh->got_types_used |= CALL_USED;
36591ba1
SL
4772 }
4773 else
1c2de463 4774 eh->got_types_used |= GOT_USED;
36591ba1
SL
4775 }
4776 else
4777 {
4778 /* This is a global offset table entry for a local symbol. */
4779 if (local_got_refcounts == NULL)
4780 {
4781 bfd_size_type size;
4782
4783 size = symtab_hdr->sh_info;
4784 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4785 local_got_refcounts
4786 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4787 if (local_got_refcounts == NULL)
4788 return FALSE;
4789 elf_local_got_refcounts (abfd) = local_got_refcounts;
4790 elf32_nios2_local_got_tls_type (abfd)
4791 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4792 }
4793 local_got_refcounts[r_symndx]++;
4794 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4795 }
4796
4797 /* We will already have issued an error message if there is a
4798 TLS / non-TLS mismatch, based on the symbol type. We don't
4799 support any linker relaxations. So just combine any TLS
4800 types needed. */
4801 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4802 && tls_type != GOT_NORMAL)
4803 tls_type |= old_tls_type;
4804
4805 if (old_tls_type != tls_type)
4806 {
4807 if (h != NULL)
4808 elf32_nios2_hash_entry (h)->tls_type = tls_type;
4809 else
4810 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4811 }
4812 }
4ef97a1b 4813 make_got:
36591ba1
SL
4814 if (htab->root.sgot == NULL)
4815 {
4816 if (htab->root.dynobj == NULL)
4817 htab->root.dynobj = abfd;
4818 if (!create_got_section (htab->root.dynobj, info))
4819 return FALSE;
4820 }
4821 break;
4822
4ef97a1b
AM
4823 case R_NIOS2_TLS_LDM16:
4824 htab->tls_ldm_got.refcount++;
4825 goto make_got;
4826
36591ba1
SL
4827 /* This relocation describes the C++ object vtable hierarchy.
4828 Reconstruct it for later use during GC. */
4829 case R_NIOS2_GNU_VTINHERIT:
4830 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4831 return FALSE;
4832 break;
4833
4834 /* This relocation describes which C++ vtable entries are actually
4835 used. Record for later use during GC. */
4836 case R_NIOS2_GNU_VTENTRY:
4837 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4838 return FALSE;
4839 break;
4840
4841 case R_NIOS2_BFD_RELOC_32:
4842 case R_NIOS2_CALL26:
78058a5e 4843 case R_NIOS2_CALL26_NOAT:
36591ba1
SL
4844 case R_NIOS2_HIADJ16:
4845 case R_NIOS2_LO16:
4846
4847 if (h != NULL)
4848 {
4849 /* If this reloc is in a read-only section, we might
4850 need a copy reloc. We can't check reliably at this
4851 stage whether the section is read-only, as input
4852 sections have not yet been mapped to output sections.
4853 Tentatively set the flag for now, and correct in
4854 adjust_dynamic_symbol. */
0e1862bb 4855 if (!bfd_link_pic (info))
36591ba1
SL
4856 h->non_got_ref = 1;
4857
4858 /* Make sure a plt entry is created for this symbol if it
4859 turns out to be a function defined by a dynamic object. */
4860 h->plt.refcount++;
4861
78058a5e 4862 if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
36591ba1
SL
4863 h->needs_plt = 1;
4864 }
4865
4866 /* If we are creating a shared library, we need to copy the
4867 reloc into the shared library. */
0e1862bb 4868 if (bfd_link_pic (info)
36591ba1
SL
4869 && (sec->flags & SEC_ALLOC) != 0
4870 && (r_type == R_NIOS2_BFD_RELOC_32
4871 || (h != NULL && ! h->needs_plt
a496fbc8 4872 && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
36591ba1 4873 {
3bf083ed
AM
4874 struct elf_dyn_relocs *p;
4875 struct elf_dyn_relocs **head;
36591ba1
SL
4876
4877 /* When creating a shared object, we must copy these
4878 reloc types into the output file. We create a reloc
4879 section in dynobj and make room for this reloc. */
4880 if (sreloc == NULL)
4881 {
4ef97a1b
AM
4882 if (htab->root.dynobj == NULL)
4883 htab->root.dynobj = abfd;
4884
36591ba1 4885 sreloc = _bfd_elf_make_dynamic_reloc_section
4ef97a1b 4886 (sec, htab->root.dynobj, 2, abfd, TRUE);
36591ba1
SL
4887 if (sreloc == NULL)
4888 return FALSE;
4889 }
4890
4891 /* If this is a global symbol, we count the number of
4892 relocations we need for this symbol. */
4893 if (h != NULL)
190eb1dd 4894 head = &h->dyn_relocs;
36591ba1
SL
4895 else
4896 {
4897 /* Track dynamic relocs needed for local syms too.
4898 We really need local syms available to do this
4899 easily. Oh well. */
4900
4901 asection *s;
4902 void *vpp;
4903 Elf_Internal_Sym *isym;
4904
4905 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4906 abfd, r_symndx);
4907 if (isym == NULL)
4908 return FALSE;
4909
4910 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4911 if (s == NULL)
4912 s = sec;
4913
4914 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 4915 head = (struct elf_dyn_relocs **) vpp;
36591ba1
SL
4916 }
4917
4918 p = *head;
4919 if (p == NULL || p->sec != sec)
4920 {
986f0783 4921 size_t amt = sizeof *p;
3bf083ed 4922 p = ((struct elf_dyn_relocs *)
36591ba1
SL
4923 bfd_alloc (htab->root.dynobj, amt));
4924 if (p == NULL)
4925 return FALSE;
4926 p->next = *head;
4927 *head = p;
4928 p->sec = sec;
4929 p->count = 0;
4930 p->pc_count = 0;
4931 }
4932
4933 p->count += 1;
4934
4935 }
4936 break;
4937 }
4938 }
4939
4940 return TRUE;
4941}
4942
4943
4944/* Implement elf_backend_gc_mark_hook:
4945 Return the section that should be marked against GC for a given
4946 relocation. */
4947static asection *
4948nios2_elf32_gc_mark_hook (asection *sec,
25153ba0 4949 struct bfd_link_info *info,
36591ba1
SL
4950 Elf_Internal_Rela *rel,
4951 struct elf_link_hash_entry *h,
4952 Elf_Internal_Sym *sym)
4953{
4954 if (h != NULL)
4955 switch (ELF32_R_TYPE (rel->r_info))
4956 {
4957 case R_NIOS2_GNU_VTINHERIT:
4958 case R_NIOS2_GNU_VTENTRY:
4959 return NULL;
4960 }
4961 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4962}
4963
36591ba1
SL
4964/* Implement elf_backend_finish_dynamic_symbols:
4965 Finish up dynamic symbol handling. We set the contents of various
4966 dynamic sections here. */
4967static bfd_boolean
4968nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
4969 struct bfd_link_info *info,
4970 struct elf_link_hash_entry *h,
4971 Elf_Internal_Sym *sym)
4972{
4973 struct elf32_nios2_link_hash_table *htab;
4974 struct elf32_nios2_link_hash_entry *eh
4975 = (struct elf32_nios2_link_hash_entry *)h;
4976 int use_plt;
4977
4978 htab = elf32_nios2_hash_table (info);
4979
4980 if (h->plt.offset != (bfd_vma) -1)
4981 {
4982 asection *splt;
4983 asection *sgotplt;
4984 asection *srela;
4985 bfd_vma plt_index;
4986 bfd_vma got_offset;
4987 Elf_Internal_Rela rela;
4988 bfd_byte *loc;
4989 bfd_vma got_address;
4990
4991 /* This symbol has an entry in the procedure linkage table. Set
4992 it up. */
4993 BFD_ASSERT (h->dynindx != -1);
4994 splt = htab->root.splt;
4995 sgotplt = htab->root.sgotplt;
4996 srela = htab->root.srelplt;
4997 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
4998
4999 /* Emit the PLT entry. */
0e1862bb 5000 if (bfd_link_pic (info))
36591ba1
SL
5001 {
5002 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
5003 3);
5004 plt_index = (h->plt.offset - 24) / 12;
5005 got_offset = (plt_index + 3) * 4;
5006 nios2_elf32_install_imm16 (splt, h->plt.offset,
5007 hiadj(plt_index * 4));
5008 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5009 (plt_index * 4) & 0xffff);
5010 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
5011 0xfff4 - h->plt.offset);
5012 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5013 + got_offset);
5014
5015 /* Fill in the entry in the global offset table. There are no
5016 res_n slots for a shared object PLT, instead the .got.plt entries
5017 point to the PLT entries. */
5018 bfd_put_32 (output_bfd,
5019 splt->output_section->vma + splt->output_offset
5020 + h->plt.offset, sgotplt->contents + got_offset);
5021 }
5022 else
5023 {
5024 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5025 got_offset = (plt_index + 3) * 4;
5026
5027 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5028 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5029 + got_offset);
5030 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5031 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5032 got_address & 0xffff);
5033
5034 /* Fill in the entry in the global offset table. */
5035 bfd_put_32 (output_bfd,
5036 splt->output_section->vma + splt->output_offset
5037 + plt_index * 4, sgotplt->contents + got_offset);
5038 }
5039
5040 /* Fill in the entry in the .rela.plt section. */
5041 rela.r_offset = got_address;
5042 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5043 rela.r_addend = 0;
5044 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5045 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5046
5047 if (!h->def_regular)
5048 {
5049 /* Mark the symbol as undefined, rather than as defined in
5050 the .plt section. Leave the value alone. */
5051 sym->st_shndx = SHN_UNDEF;
5052 /* If the symbol is weak, we do need to clear the value.
5053 Otherwise, the PLT entry would provide a definition for
5054 the symbol even if the symbol wasn't defined anywhere,
5055 and so the symbol would never be NULL. */
5056 if (!h->ref_regular_nonweak)
5057 sym->st_value = 0;
5058 }
5059 }
5060
1c2de463 5061 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
5062 && h->plt.offset != (bfd_vma) -1);
5063
5064 if (!use_plt && h->got.offset != (bfd_vma) -1
5065 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5066 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5067 {
5068 asection *sgot;
5069 asection *srela;
5070 Elf_Internal_Rela rela;
5071 bfd_byte *loc;
5072 bfd_vma offset;
5073
5074 /* This symbol has an entry in the global offset table. Set it
5075 up. */
5076 sgot = htab->root.sgot;
5077 srela = htab->root.srelgot;
5078 BFD_ASSERT (sgot != NULL && srela != NULL);
5079
5080 offset = (h->got.offset & ~(bfd_vma) 1);
5081 rela.r_offset = (sgot->output_section->vma
5082 + sgot->output_offset + offset);
5083
5084 /* If this is a -Bsymbolic link, and the symbol is defined
5085 locally, we just want to emit a RELATIVE reloc. Likewise if
5086 the symbol was forced to be local because of a version file.
5087 The entry in the global offset table will already have been
5088 initialized in the relocate_section function. */
5089
0e1862bb 5090 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
36591ba1
SL
5091 {
5092 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5093 rela.r_addend = bfd_get_signed_32 (output_bfd,
5094 (sgot->contents + offset));
5095 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5096 }
5097 else
5098 {
5099 bfd_put_32 (output_bfd, (bfd_vma) 0,
5100 sgot->contents + offset);
5101 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5102 rela.r_addend = 0;
5103 }
5104
5105 loc = srela->contents;
5106 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5107 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5108 }
5109
5110 if (use_plt && h->got.offset != (bfd_vma) -1)
5111 {
5112 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5113 asection *sgot = htab->root.sgot;
5114 asection *splt = htab->root.splt;
5115 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5116 + h->plt.offset),
5117 sgot->contents + offset);
5118 }
5119
5120 if (h->needs_copy)
5121 {
5122 asection *s;
5123 Elf_Internal_Rela rela;
5124 bfd_byte *loc;
5125
5126 /* This symbol needs a copy reloc. Set it up. */
5127 BFD_ASSERT (h->dynindx != -1
5128 && (h->root.type == bfd_link_hash_defined
5129 || h->root.type == bfd_link_hash_defweak));
5130
36591ba1
SL
5131 rela.r_offset = (h->root.u.def.value
5132 + h->root.u.def.section->output_section->vma
5133 + h->root.u.def.section->output_offset);
5134 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5135 rela.r_addend = 0;
afbf7e8e 5136 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
5137 s = htab->root.sreldynrelro;
5138 else
5139 s = htab->root.srelbss;
5140 BFD_ASSERT (s != NULL);
36591ba1
SL
5141 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5142 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5143 }
5144
82e91538 5145 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
36591ba1 5146 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4ef97a1b
AM
5147 || h == htab->root.hgot
5148 || h == htab->h_gp_got)
36591ba1
SL
5149 sym->st_shndx = SHN_ABS;
5150
5151 return TRUE;
5152}
5153
5154/* Implement elf_backend_finish_dynamic_sections. */
5155static bfd_boolean
5156nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5157 struct bfd_link_info *info)
5158{
36591ba1
SL
5159 asection *sgotplt;
5160 asection *sdyn;
5161 struct elf32_nios2_link_hash_table *htab;
5162
5163 htab = elf32_nios2_hash_table (info);
36591ba1 5164 sgotplt = htab->root.sgotplt;
4ef97a1b 5165 sdyn = NULL;
36591ba1 5166
4ef97a1b 5167 if (htab->root.dynamic_sections_created)
36591ba1
SL
5168 {
5169 asection *splt;
5170 Elf32_External_Dyn *dyncon, *dynconend;
5171
5172 splt = htab->root.splt;
4ef97a1b
AM
5173 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
5174 BFD_ASSERT (splt != NULL && sdyn != NULL && sgotplt != NULL);
36591ba1
SL
5175
5176 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5177 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5178 for (; dyncon < dynconend; dyncon++)
5179 {
5180 Elf_Internal_Dyn dyn;
5181 asection *s;
5182
4ef97a1b 5183 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
36591ba1
SL
5184
5185 switch (dyn.d_tag)
5186 {
5187 default:
5188 break;
5189
5190 case DT_PLTGOT:
4ade44b7
AM
5191 s = htab->root.sgotplt;
5192 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
36591ba1
SL
5193 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5194 break;
5195
5196 case DT_JMPREL:
5197 s = htab->root.srelplt;
4ade44b7 5198 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
36591ba1
SL
5199 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5200 break;
5201
5202 case DT_PLTRELSZ:
5203 s = htab->root.srelplt;
36591ba1
SL
5204 dyn.d_un.d_val = s->size;
5205 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5206 break;
5207
36591ba1 5208 case DT_NIOS2_GP:
4ade44b7
AM
5209 s = htab->root.sgotplt;
5210 dyn.d_un.d_ptr
5211 = s->output_section->vma + s->output_offset + 0x7ff0;
36591ba1
SL
5212 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5213 break;
5214 }
5215 }
5216
5217 /* Fill in the first entry in the procedure linkage table. */
5218 if (splt->size > 0)
5219 {
5220 bfd_vma got_address = (sgotplt->output_section->vma
5221 + sgotplt->output_offset);
0e1862bb 5222 if (bfd_link_pic (info))
36591ba1 5223 {
33d4099f
SL
5224 bfd_vma got_pcrel = got_address - (splt->output_section->vma
5225 + splt->output_offset);
5226 /* Both GOT and PLT must be aligned to a 16-byte boundary
5227 for the two loads to share the %hiadj part. The 4-byte
5228 offset for nextpc is accounted for in the %lo offsets
5229 on the loads. */
5230 BFD_ASSERT ((got_pcrel & 0xf) == 0);
36591ba1 5231 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
33d4099f
SL
5232 nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5233 nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5234 nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
36591ba1
SL
5235 }
5236 else
5237 {
5238 /* Divide by 4 here, not 3 because we already corrected for the
5239 res_N branches. */
5240 bfd_vma res_size = (splt->size - 28) / 4;
5241 bfd_vma res_start = (splt->output_section->vma
5242 + splt->output_offset);
5243 bfd_vma res_offset;
5244
5245 for (res_offset = 0; res_offset < res_size; res_offset += 4)
5246 bfd_put_32 (output_bfd,
5247 6 | ((res_size - (res_offset + 4)) << 6),
5248 splt->contents + res_offset);
5249
33d4099f
SL
5250 /* The GOT must be aligned to a 16-byte boundary for the
5251 two loads to share the same %hiadj part. */
5252 BFD_ASSERT ((got_address & 0xf) == 0);
5253
36591ba1
SL
5254 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5255 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5256 nios2_elf32_install_imm16 (splt, res_size + 4,
5257 res_start & 0xffff);
5258 nios2_elf32_install_imm16 (splt, res_size + 12,
5259 hiadj (got_address));
5260 nios2_elf32_install_imm16 (splt, res_size + 16,
33d4099f 5261 (got_address + 4) & 0xffff);
36591ba1 5262 nios2_elf32_install_imm16 (splt, res_size + 20,
33d4099f 5263 (got_address + 8) & 0xffff);
36591ba1
SL
5264 }
5265 }
5266 }
4ef97a1b 5267
36591ba1 5268 /* Fill in the first three entries in the global offset table. */
4ef97a1b 5269 if (sgotplt != NULL && sgotplt->size > 0)
36591ba1
SL
5270 {
5271 if (sdyn == NULL)
5272 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5273 else
5274 bfd_put_32 (output_bfd,
5275 sdyn->output_section->vma + sdyn->output_offset,
5276 sgotplt->contents);
5277 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5278 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
36591ba1 5279
4ef97a1b
AM
5280 if (sgotplt->output_section != bfd_abs_section_ptr)
5281 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5282 }
36591ba1
SL
5283
5284 return TRUE;
5285}
5286
5287/* Implement elf_backend_adjust_dynamic_symbol:
5288 Adjust a symbol defined by a dynamic object and referenced by a
5289 regular object. The current definition is in some section of the
5290 dynamic object, but we're not including those sections. We have to
5291 change the definition to something the rest of the link can
5292 understand. */
5293static bfd_boolean
5294nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5295 struct elf_link_hash_entry *h)
5296{
5297 struct elf32_nios2_link_hash_table *htab;
5298 bfd *dynobj;
5474d94f 5299 asection *s, *srel;
36591ba1
SL
5300 unsigned align2;
5301
5302 htab = elf32_nios2_hash_table (info);
4ef97a1b 5303 dynobj = htab->root.dynobj;
36591ba1
SL
5304
5305 /* Make sure we know what is going on here. */
5306 BFD_ASSERT (dynobj != NULL
5307 && (h->needs_plt
60d67dc8 5308 || h->is_weakalias
36591ba1
SL
5309 || (h->def_dynamic
5310 && h->ref_regular
5311 && !h->def_regular)));
5312
5313 /* If this is a function, put it in the procedure linkage table. We
5314 will fill in the contents of the procedure linkage table later,
5315 when we know the address of the .got section. */
5316 if (h->type == STT_FUNC || h->needs_plt)
5317 {
5318 if (h->plt.refcount <= 0
5319 || SYMBOL_CALLS_LOCAL (info, h)
5320 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5321 && h->root.type == bfd_link_hash_undefweak))
5322 {
5323 /* This case can occur if we saw a PLT reloc in an input
5324 file, but the symbol was never referred to by a dynamic
5325 object, or if all references were garbage collected. In
5326 such a case, we don't actually need to build a procedure
5327 linkage table, and we can just do a PCREL reloc instead. */
5328 h->plt.offset = (bfd_vma) -1;
5329 h->needs_plt = 0;
5330 }
5331
5332 return TRUE;
5333 }
5334
5335 /* Reinitialize the plt offset now that it is not used as a reference
5336 count any more. */
5337 h->plt.offset = (bfd_vma) -1;
5338
5339 /* If this is a weak symbol, and there is a real definition, the
5340 processor independent code will have arranged for us to see the
5341 real definition first, and we can just use the same value. */
60d67dc8 5342 if (h->is_weakalias)
36591ba1 5343 {
60d67dc8
AM
5344 struct elf_link_hash_entry *def = weakdef (h);
5345 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5346 h->root.u.def.section = def->root.u.def.section;
5347 h->root.u.def.value = def->root.u.def.value;
36591ba1
SL
5348 return TRUE;
5349 }
5350
5351 /* If there are no non-GOT references, we do not need a copy
5352 relocation. */
5353 if (!h->non_got_ref)
5354 return TRUE;
5355
5356 /* This is a reference to a symbol defined by a dynamic object which
5357 is not a function.
5358 If we are creating a shared library, we must presume that the
5359 only references to the symbol are via the global offset table.
5360 For such cases we need not do anything here; the relocations will
5361 be handled correctly by relocate_section. */
0e1862bb 5362 if (bfd_link_pic (info))
36591ba1
SL
5363 return TRUE;
5364
5365 if (h->size == 0)
5366 {
4eca0228
AM
5367 _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5368 h->root.root.string);
36591ba1
SL
5369 return TRUE;
5370 }
5371
5372 /* We must allocate the symbol in our .dynbss section, which will
5373 become part of the .bss section of the executable. There will be
5374 an entry for this symbol in the .dynsym section. The dynamic
5375 object will contain position independent code, so all references
5376 from the dynamic object to this symbol will go through the global
5377 offset table. The dynamic linker will use the .dynsym entry to
5378 determine the address it must put in the global offset table, so
5379 both the dynamic object and the regular object will refer to the
5380 same memory location for the variable. */
36591ba1
SL
5381 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5382 copy the initial value out of the dynamic object and into the
5383 runtime process image. We need to remember the offset into the
5384 .rela.bss section we are going to use. */
5474d94f 5385 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
36591ba1 5386 {
5474d94f
AM
5387 s = htab->root.sdynrelro;
5388 srel = htab->root.sreldynrelro;
5389 }
5390 else
5391 {
5392 s = htab->root.sdynbss;
9d19e4fd 5393 srel = htab->root.srelbss;
5474d94f
AM
5394 }
5395 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5396 {
36591ba1
SL
5397 srel->size += sizeof (Elf32_External_Rela);
5398 h->needs_copy = 1;
5399 }
5400
5401 align2 = bfd_log2 (h->size);
5402 if (align2 > h->root.u.def.section->alignment_power)
5403 align2 = h->root.u.def.section->alignment_power;
5404
5405 /* Align dynbss. */
5406 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
fd361982
AM
5407 if (align2 > bfd_section_alignment (s)
5408 && !bfd_set_section_alignment (s, align2))
36591ba1
SL
5409 return FALSE;
5410
5411 /* Define the symbol as being at this point in the section. */
5412 h->root.u.def.section = s;
5413 h->root.u.def.value = s->size;
5414
5415 /* Increment the section size to make room for the symbol. */
5416 s->size += h->size;
5417
5418 return TRUE;
5419}
5420
5421/* Worker function for nios2_elf32_size_dynamic_sections. */
5422static bfd_boolean
5423adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5424{
5425 struct bfd_link_info *info;
5426 struct elf32_nios2_link_hash_table *htab;
5427
5428 if (h->root.type == bfd_link_hash_indirect)
5429 return TRUE;
5430
5431 if (h->root.type == bfd_link_hash_warning)
5432 /* When warning symbols are created, they **replace** the "real"
5433 entry in the hash table, thus we never get to see the real
5434 symbol in a hash traversal. So look at it now. */
5435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5436
5437 info = (struct bfd_link_info *) inf;
5438 htab = elf32_nios2_hash_table (info);
5439
5440 if (h->plt.offset != (bfd_vma)-1)
5441 h->plt.offset += htab->res_n_size;
5442 if (htab->root.splt == h->root.u.def.section)
5443 h->root.u.def.value += htab->res_n_size;
5444
5445 return TRUE;
5446}
5447
5448/* Another worker function for nios2_elf32_size_dynamic_sections.
5449 Allocate space in .plt, .got and associated reloc sections for
5450 dynamic relocs. */
5451static bfd_boolean
5452allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5453{
5454 struct bfd_link_info *info;
5455 struct elf32_nios2_link_hash_table *htab;
5456 struct elf32_nios2_link_hash_entry *eh;
3bf083ed 5457 struct elf_dyn_relocs *p;
36591ba1
SL
5458 int use_plt;
5459
5460 if (h->root.type == bfd_link_hash_indirect)
5461 return TRUE;
5462
5463 if (h->root.type == bfd_link_hash_warning)
5464 /* When warning symbols are created, they **replace** the "real"
5465 entry in the hash table, thus we never get to see the real
5466 symbol in a hash traversal. So look at it now. */
5467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5468
5469 info = (struct bfd_link_info *) inf;
5470 htab = elf32_nios2_hash_table (info);
5471
5472 if (htab->root.dynamic_sections_created
5473 && h->plt.refcount > 0)
5474 {
5475 /* Make sure this symbol is output as a dynamic symbol.
5476 Undefined weak syms won't yet be marked as dynamic. */
5477 if (h->dynindx == -1
5478 && !h->forced_local
5479 && !bfd_elf_link_record_dynamic_symbol (info, h))
5480 return FALSE;
5481
0e1862bb 5482 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
36591ba1
SL
5483 {
5484 asection *s = htab->root.splt;
5485
5486 /* Allocate room for the header. */
5487 if (s->size == 0)
5488 {
0e1862bb 5489 if (bfd_link_pic (info))
36591ba1
SL
5490 s->size = 24;
5491 else
5492 s->size = 28;
5493 }
5494
5495 h->plt.offset = s->size;
5496
5497 /* If this symbol is not defined in a regular file, and we are
5498 not generating a shared library, then set the symbol to this
5499 location in the .plt. This is required to make function
5500 pointers compare as equal between the normal executable and
5501 the shared library. */
0e1862bb 5502 if (! bfd_link_pic (info)
36591ba1
SL
5503 && !h->def_regular)
5504 {
5505 h->root.u.def.section = s;
5506 h->root.u.def.value = h->plt.offset;
5507 }
5508
5509 /* Make room for this entry. */
5510 s->size += 12;
5511
5512 /* We also need to make an entry in the .rela.plt section. */
5513 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5514
5515 /* And the .got.plt section. */
5516 htab->root.sgotplt->size += 4;
5517 }
5518 else
5519 {
5520 h->plt.offset = (bfd_vma) -1;
5521 h->needs_plt = 0;
5522 }
5523 }
5524 else
5525 {
5526 h->plt.offset = (bfd_vma) -1;
5527 h->needs_plt = 0;
5528 }
5529
5530 eh = (struct elf32_nios2_link_hash_entry *) h;
1c2de463 5531 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
5532 && h->plt.offset != (bfd_vma) -1);
5533
5534 if (h->got.refcount > 0)
5535 {
5536 asection *s;
5537 bfd_boolean dyn;
5538 int tls_type = eh->tls_type;
5539 int indx;
5540
5541 /* Make sure this symbol is output as a dynamic symbol.
5542 Undefined weak syms won't yet be marked as dynamic. */
5543 if (h->dynindx == -1
5544 && !h->forced_local
5545 && !bfd_elf_link_record_dynamic_symbol (info, h))
5546 return FALSE;
5547
5548 s = htab->root.sgot;
5549 h->got.offset = s->size;
5550
5551 if (tls_type == GOT_UNKNOWN)
5552 abort ();
5553
5554 if (tls_type == GOT_NORMAL)
5555 /* Non-TLS symbols need one GOT slot. */
5556 s->size += 4;
5557 else
5558 {
5559 if (tls_type & GOT_TLS_GD)
5560 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5561 s->size += 8;
5562 if (tls_type & GOT_TLS_IE)
5563 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5564 s->size += 4;
5565 }
5566
5567 dyn = htab->root.dynamic_sections_created;
5568
5569 indx = 0;
0e1862bb
L
5570 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5571 && (!bfd_link_pic (info)
36591ba1
SL
5572 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5573 indx = h->dynindx;
5574
5575 if (tls_type != GOT_NORMAL
0e1862bb 5576 && (bfd_link_pic (info) || indx != 0)
36591ba1
SL
5577 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5578 || h->root.type != bfd_link_hash_undefweak))
5579 {
5580 if (tls_type & GOT_TLS_IE)
5581 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5582
5583 if (tls_type & GOT_TLS_GD)
5584 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5585
5586 if ((tls_type & GOT_TLS_GD) && indx != 0)
5587 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5588 }
5589 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5590 || h->root.type != bfd_link_hash_undefweak)
5591 && !use_plt
0e1862bb 5592 && (bfd_link_pic (info)
36591ba1
SL
5593 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5594 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5595 }
5596 else
5597 h->got.offset = (bfd_vma) -1;
5598
190eb1dd 5599 if (h->dyn_relocs == NULL)
36591ba1
SL
5600 return TRUE;
5601
5602 /* In the shared -Bsymbolic case, discard space allocated for
5603 dynamic pc-relative relocs against symbols which turn out to be
5604 defined in regular objects. For the normal shared case, discard
5605 space for pc-relative relocs that have become local due to symbol
5606 visibility changes. */
5607
0e1862bb 5608 if (bfd_link_pic (info))
36591ba1
SL
5609 {
5610 if (h->def_regular
a496fbc8 5611 && (h->forced_local || SYMBOLIC_BIND (info, h)))
36591ba1 5612 {
3bf083ed 5613 struct elf_dyn_relocs **pp;
36591ba1 5614
190eb1dd 5615 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
36591ba1
SL
5616 {
5617 p->count -= p->pc_count;
5618 p->pc_count = 0;
5619 if (p->count == 0)
5620 *pp = p->next;
5621 else
5622 pp = &p->next;
5623 }
5624 }
5625
5626 /* Also discard relocs on undefined weak syms with non-default
5627 visibility. */
190eb1dd 5628 if (h->dyn_relocs != NULL
36591ba1
SL
5629 && h->root.type == bfd_link_hash_undefweak)
5630 {
31a53da5
L
5631 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5632 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 5633 h->dyn_relocs = NULL;
36591ba1
SL
5634
5635 /* Make sure undefined weak symbols are output as a dynamic
5636 symbol in PIEs. */
5637 else if (h->dynindx == -1
5638 && !h->forced_local
5639 && !bfd_elf_link_record_dynamic_symbol (info, h))
5640 return FALSE;
5641 }
5642 }
5643 else
5644 {
5645 /* For the non-shared case, discard space for relocs against
5646 symbols which turn out to need copy relocs or are not
5647 dynamic. */
5648
5649 if (!h->non_got_ref
5650 && ((h->def_dynamic && !h->def_regular)
5651 || (htab->root.dynamic_sections_created
5652 && (h->root.type == bfd_link_hash_undefweak
5653 || h->root.type == bfd_link_hash_undefined))))
5654 {
5655 /* Make sure this symbol is output as a dynamic symbol.
5656 Undefined weak syms won't yet be marked as dynamic. */
5657 if (h->dynindx == -1
5658 && !h->forced_local
5659 && !bfd_elf_link_record_dynamic_symbol (info, h))
5660 return FALSE;
5661
5662 /* If that succeeded, we know we'll be keeping all the
5663 relocs. */
5664 if (h->dynindx != -1)
5665 goto keep;
5666 }
5667
190eb1dd 5668 h->dyn_relocs = NULL;
36591ba1
SL
5669
5670 keep: ;
5671 }
5672
5673 /* Finally, allocate space. */
190eb1dd 5674 for (p = h->dyn_relocs; p != NULL; p = p->next)
36591ba1
SL
5675 {
5676 asection *sreloc = elf_section_data (p->sec)->sreloc;
5677 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5678 }
5679
5680 return TRUE;
5681}
5682
5683/* Implement elf_backend_size_dynamic_sections:
5684 Set the sizes of the dynamic sections. */
5685static bfd_boolean
5686nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5687 struct bfd_link_info *info)
5688{
5689 bfd *dynobj;
5690 asection *s;
36591ba1
SL
5691 bfd_boolean relocs;
5692 bfd *ibfd;
5693 struct elf32_nios2_link_hash_table *htab;
5694
5695 htab = elf32_nios2_hash_table (info);
4ef97a1b 5696 dynobj = htab->root.dynobj;
36591ba1
SL
5697 BFD_ASSERT (dynobj != NULL);
5698
5699 htab->res_n_size = 0;
4ef97a1b 5700 if (htab->root.dynamic_sections_created)
36591ba1
SL
5701 {
5702 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 5703 if (bfd_link_executable (info) && !info->nointerp)
36591ba1
SL
5704 {
5705 s = bfd_get_linker_section (dynobj, ".interp");
5706 BFD_ASSERT (s != NULL);
5707 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5708 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5709 }
5710 }
5711 else
5712 {
5713 /* We may have created entries in the .rela.got section.
5714 However, if we are not creating the dynamic sections, we will
5715 not actually use these entries. Reset the size of .rela.got,
5716 which will cause it to get stripped from the output file
5717 below. */
5718 s = htab->root.srelgot;
5719 if (s != NULL)
5720 s->size = 0;
5721 }
5722
5723 /* Set up .got offsets for local syms, and space for local dynamic
5724 relocs. */
c72f2fb2 5725 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
36591ba1
SL
5726 {
5727 bfd_signed_vma *local_got;
5728 bfd_signed_vma *end_local_got;
5729 char *local_tls_type;
5730 bfd_size_type locsymcount;
5731 Elf_Internal_Shdr *symtab_hdr;
5732 asection *srel;
5733
5734 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5735 continue;
5736
5737 for (s = ibfd->sections; s != NULL; s = s->next)
5738 {
3bf083ed 5739 struct elf_dyn_relocs *p;
36591ba1
SL
5740
5741 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5742 {
5743 if (!bfd_is_abs_section (p->sec)
5744 && bfd_is_abs_section (p->sec->output_section))
5745 {
5746 /* Input section has been discarded, either because
5747 it is a copy of a linkonce section or due to
5748 linker script /DISCARD/, so we'll be discarding
5749 the relocs too. */
5750 }
5751 else if (p->count != 0)
5752 {
5753 srel = elf_section_data (p->sec)->sreloc;
5754 srel->size += p->count * sizeof (Elf32_External_Rela);
5755 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5756 info->flags |= DF_TEXTREL;
5757 }
5758 }
5759 }
5760
5761 local_got = elf_local_got_refcounts (ibfd);
5762 if (!local_got)
5763 continue;
5764
5765 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5766 locsymcount = symtab_hdr->sh_info;
5767 end_local_got = local_got + locsymcount;
5768 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5769 s = htab->root.sgot;
5770 srel = htab->root.srelgot;
5771 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5772 {
5773 if (*local_got > 0)
5774 {
5775 *local_got = s->size;
5776 if (*local_tls_type & GOT_TLS_GD)
5777 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5778 s->size += 8;
5779 if (*local_tls_type & GOT_TLS_IE)
5780 s->size += 4;
5781 if (*local_tls_type == GOT_NORMAL)
5782 s->size += 4;
5783
0e1862bb 5784 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
36591ba1
SL
5785 srel->size += sizeof (Elf32_External_Rela);
5786 }
5787 else
5788 *local_got = (bfd_vma) -1;
5789 }
5790 }
5791
5792 if (htab->tls_ldm_got.refcount > 0)
5793 {
5794 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5795 for R_NIOS2_TLS_LDM16 relocations. */
5796 htab->tls_ldm_got.offset = htab->root.sgot->size;
5797 htab->root.sgot->size += 8;
0e1862bb 5798 if (bfd_link_pic (info))
36591ba1
SL
5799 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5800 }
5801 else
5802 htab->tls_ldm_got.offset = -1;
5803
5804 /* Allocate global sym .plt and .got entries, and space for global
5805 sym dynamic relocs. */
5806 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5807
4ef97a1b 5808 if (htab->root.dynamic_sections_created)
82e91538
SL
5809 {
5810 /* If the .got section is more than 0x8000 bytes, we add
5811 0x8000 to the value of _gp_got, so that 16-bit relocations
5812 have a greater chance of working. */
5813 if (htab->root.sgot->size >= 0x8000
4ef97a1b
AM
5814 && htab->h_gp_got->root.u.def.value == 0)
5815 htab->h_gp_got->root.u.def.value = 0x8000;
82e91538
SL
5816 }
5817
36591ba1
SL
5818 /* The check_relocs and adjust_dynamic_symbol entry points have
5819 determined the sizes of the various dynamic sections. Allocate
5820 memory for them. */
36591ba1
SL
5821 relocs = FALSE;
5822 for (s = dynobj->sections; s != NULL; s = s->next)
5823 {
5824 const char *name;
5825
5826 if ((s->flags & SEC_LINKER_CREATED) == 0)
5827 continue;
5828
5829 /* It's OK to base decisions on the section name, because none
5830 of the dynobj section names depend upon the input files. */
fd361982 5831 name = bfd_section_name (s);
36591ba1 5832
4ef97a1b 5833 if (CONST_STRNEQ (name, ".rela"))
36591ba1
SL
5834 {
5835 if (s->size != 0)
5836 {
4ef97a1b
AM
5837 if (s != htab->root.srelplt)
5838 relocs = TRUE;
36591ba1
SL
5839
5840 /* We use the reloc_count field as a counter if we need
5841 to copy relocs into the output file. */
5842 s->reloc_count = 0;
5843 }
5844 }
4ef97a1b 5845 else if (s == htab->root.splt)
5474d94f 5846 {
4ef97a1b
AM
5847 /* Correct for the number of res_N branches. */
5848 if (s->size != 0 && !bfd_link_pic (info))
5849 {
5850 htab->res_n_size = (s->size - 28) / 3;
5851 s->size += htab->res_n_size;
5852 }
5474d94f 5853 }
4ef97a1b
AM
5854 else if (s != htab->sbss
5855 && s != htab->root.sgot
5856 && s != htab->root.sgotplt
5857 && s != htab->root.sdynbss
5474d94f 5858 && s != htab->root.sdynrelro)
36591ba1
SL
5859 /* It's not one of our sections, so don't allocate space. */
5860 continue;
5861
5862 if (s->size == 0)
5863 {
36591ba1
SL
5864 s->flags |= SEC_EXCLUDE;
5865 continue;
5866 }
5867
5868 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5869 continue;
5870
5871 /* Allocate memory for the section contents. */
36591ba1
SL
5872 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5873 if (s->contents == NULL)
5874 return FALSE;
5875 }
5876
5877 /* Adjust dynamic symbols that point to the plt to account for the
5878 now-known number of resN slots. */
5879 if (htab->res_n_size)
5880 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
5881
4ef97a1b 5882 if (htab->root.dynamic_sections_created)
36591ba1
SL
5883 {
5884 /* Add some entries to the .dynamic section. We fill in the
5885 values later, in elf_nios2_finish_dynamic_sections, but we
5886 must add the entries now so that we get the correct size for
5887 the .dynamic section. The DT_DEBUG entry is filled in by the
5888 dynamic linker and used by the debugger. */
5889#define add_dynamic_entry(TAG, VAL) \
5890 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5891
0e1862bb 5892 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_DEBUG, 0))
36591ba1
SL
5893 return FALSE;
5894
4ef97a1b
AM
5895 if (htab->root.sgotplt->size != 0
5896 && !add_dynamic_entry (DT_PLTGOT, 0))
36591ba1
SL
5897 return FALSE;
5898
4ef97a1b 5899 if (htab->root.splt->size != 0
36591ba1
SL
5900 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
5901 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5902 || !add_dynamic_entry (DT_JMPREL, 0)))
5903 return FALSE;
5904
5905 if (relocs
5906 && (!add_dynamic_entry (DT_RELA, 0)
5907 || !add_dynamic_entry (DT_RELASZ, 0)
5908 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
5909 return FALSE;
5910
0e1862bb 5911 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_NIOS2_GP, 0))
36591ba1
SL
5912 return FALSE;
5913
5914 if ((info->flags & DF_TEXTREL) != 0
5915 && !add_dynamic_entry (DT_TEXTREL, 0))
5916 return FALSE;
5917 }
5918#undef add_dynamic_entry
5919
5920 return TRUE;
5921}
5922
68faa637
AM
5923/* Free the derived linker hash table. */
5924static void
d495ab0d 5925nios2_elf32_link_hash_table_free (bfd *obfd)
68faa637
AM
5926{
5927 struct elf32_nios2_link_hash_table *htab
d495ab0d 5928 = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
68faa637
AM
5929
5930 bfd_hash_table_free (&htab->bstab);
d495ab0d 5931 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
5932}
5933
36591ba1
SL
5934/* Implement bfd_elf32_bfd_link_hash_table_create. */
5935static struct bfd_link_hash_table *
5936nios2_elf32_link_hash_table_create (bfd *abfd)
5937{
5938 struct elf32_nios2_link_hash_table *ret;
986f0783 5939 size_t amt = sizeof (struct elf32_nios2_link_hash_table);
36591ba1 5940
7bf52ea2 5941 ret = bfd_zmalloc (amt);
36591ba1
SL
5942 if (ret == NULL)
5943 return NULL;
5944
5945 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
5946 link_hash_newfunc,
5947 sizeof (struct
5948 elf32_nios2_link_hash_entry),
5949 NIOS2_ELF_DATA))
5950 {
5951 free (ret);
5952 return NULL;
5953 }
5954
78058a5e
SL
5955 /* Init the stub hash table too. */
5956 if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
5957 sizeof (struct elf32_nios2_stub_hash_entry)))
d495ab0d
AM
5958 {
5959 _bfd_elf_link_hash_table_free (abfd);
5960 return NULL;
5961 }
5962 ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
78058a5e 5963
36591ba1
SL
5964 return &ret->root.root;
5965}
5966
5967/* Implement elf_backend_reloc_type_class. */
5968static enum elf_reloc_type_class
7e612e98
AM
5969nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5970 const asection *rel_sec ATTRIBUTE_UNUSED,
5971 const Elf_Internal_Rela *rela)
36591ba1
SL
5972{
5973 switch ((int) ELF32_R_TYPE (rela->r_info))
5974 {
5975 case R_NIOS2_RELATIVE:
5976 return reloc_class_relative;
5977 case R_NIOS2_JUMP_SLOT:
5978 return reloc_class_plt;
5979 case R_NIOS2_COPY:
5980 return reloc_class_copy;
5981 default:
5982 return reloc_class_normal;
5983 }
5984}
5985
5986/* Return 1 if target is one of ours. */
5987static bfd_boolean
5988is_nios2_elf_target (const struct bfd_target *targ)
5989{
6d00b590
AM
5990 return (targ == &nios2_elf32_le_vec
5991 || targ == &nios2_elf32_be_vec);
36591ba1
SL
5992}
5993
5994/* Implement elf_backend_add_symbol_hook.
5995 This hook is called by the linker when adding symbols from an object
5996 file. We use it to put .comm items in .sbss, and not .bss. */
5997static bfd_boolean
5998nios2_elf_add_symbol_hook (bfd *abfd,
5999 struct bfd_link_info *info,
6000 Elf_Internal_Sym *sym,
6001 const char **namep ATTRIBUTE_UNUSED,
6002 flagword *flagsp ATTRIBUTE_UNUSED,
6003 asection **secp,
6004 bfd_vma *valp)
6005{
36591ba1 6006 if (sym->st_shndx == SHN_COMMON
0e1862bb 6007 && !bfd_link_relocatable (info)
36591ba1
SL
6008 && sym->st_size <= elf_gp_size (abfd)
6009 && is_nios2_elf_target (info->output_bfd->xvec))
6010 {
6011 /* Common symbols less than or equal to -G nn bytes are automatically
6012 put into .sbss. */
6013 struct elf32_nios2_link_hash_table *htab;
6014
6015 htab = elf32_nios2_hash_table (info);
6016 if (htab->sbss == NULL)
6017 {
6018 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
6019
4ef97a1b
AM
6020 if (htab->root.dynobj == NULL)
6021 htab->root.dynobj = abfd;
36591ba1 6022
4ef97a1b
AM
6023 htab->sbss = bfd_make_section_anyway_with_flags (htab->root.dynobj,
6024 ".sbss", flags);
36591ba1
SL
6025 if (htab->sbss == NULL)
6026 return FALSE;
6027 }
6028
6029 *secp = htab->sbss;
6030 *valp = sym->st_size;
6031 }
6032
6033 return TRUE;
6034}
6035
6036/* Implement elf_backend_can_make_relative_eh_frame:
6037 Decide whether to attempt to turn absptr or lsda encodings in
6038 shared libraries into pcrel within the given input section. */
6039static bfd_boolean
6040nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6041 struct bfd_link_info *info
6042 ATTRIBUTE_UNUSED,
6043 asection *eh_frame_section
6044 ATTRIBUTE_UNUSED)
6045{
6046 /* We can't use PC-relative encodings in the .eh_frame section. */
6047 return FALSE;
6048}
6049
6050/* Implement elf_backend_special_sections. */
6051const struct bfd_elf_special_section elf32_nios2_special_sections[] =
6052{
6053 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
6054 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6055 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6056 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6057 { NULL, 0, 0, 0, 0 }
6058};
6059
6060#define ELF_ARCH bfd_arch_nios2
6061#define ELF_TARGET_ID NIOS2_ELF_DATA
6062#define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6063
6064/* The Nios II MMU uses a 4K page size. */
6065
6066#define ELF_MAXPAGESIZE 0x1000
6067
6068#define bfd_elf32_bfd_link_hash_table_create \
6069 nios2_elf32_link_hash_table_create
6070
965b1d80
SL
6071#define bfd_elf32_bfd_merge_private_bfd_data \
6072 nios2_elf32_merge_private_bfd_data
6073
36591ba1
SL
6074/* Relocation table lookup macros. */
6075
6076#define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6077#define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6078
6079/* JUMP_TABLE_LINK macros. */
6080
6081/* elf_info_to_howto (using RELA relocations). */
6082
6083#define elf_info_to_howto nios2_elf32_info_to_howto
6084
6085/* elf backend functions. */
6086
6087#define elf_backend_can_gc_sections 1
6088#define elf_backend_can_refcount 1
6089#define elf_backend_plt_readonly 1
6090#define elf_backend_want_got_plt 1
5474d94f 6091#define elf_backend_want_dynrelro 1
36591ba1 6092#define elf_backend_rela_normal 1
64f52338 6093#define elf_backend_dtrel_excludes_plt 1
36591ba1
SL
6094
6095#define elf_backend_relocate_section nios2_elf32_relocate_section
6096#define elf_backend_section_flags nios2_elf32_section_flags
6097#define elf_backend_fake_sections nios2_elf32_fake_sections
6098#define elf_backend_check_relocs nios2_elf32_check_relocs
6099
6100#define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
36591ba1
SL
6101#define elf_backend_create_dynamic_sections \
6102 nios2_elf32_create_dynamic_sections
6103#define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6104#define elf_backend_finish_dynamic_sections \
6105 nios2_elf32_finish_dynamic_sections
6106#define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6107#define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6108#define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6109#define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6110#define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
965b1d80 6111#define elf_backend_object_p nios2_elf32_object_p
36591ba1
SL
6112
6113#define elf_backend_grok_prstatus nios2_grok_prstatus
6114#define elf_backend_grok_psinfo nios2_grok_psinfo
6115
6116#undef elf_backend_can_make_relative_eh_frame
6117#define elf_backend_can_make_relative_eh_frame \
6118 nios2_elf32_can_make_relative_eh_frame
6119
6120#define elf_backend_special_sections elf32_nios2_special_sections
6121
6d00b590 6122#define TARGET_LITTLE_SYM nios2_elf32_le_vec
36591ba1 6123#define TARGET_LITTLE_NAME "elf32-littlenios2"
6d00b590 6124#define TARGET_BIG_SYM nios2_elf32_be_vec
36591ba1
SL
6125#define TARGET_BIG_NAME "elf32-bignios2"
6126
6127#define elf_backend_got_header_size 12
03d5b773 6128#define elf_backend_default_execstack 0
36591ba1
SL
6129
6130#include "elf32-target.h"
This page took 0.830524 seconds and 4 git commands to generate.