]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-nios2.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elf32-nios2.c
CommitLineData
36591ba1 1/* 32-bit ELF support for Nios II.
82704155 2 Copyright (C) 2012-2019 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 1740 /* Track dynamic relocs copied for this symbol. */
3bf083ed 1741 struct elf_dyn_relocs *dyn_relocs;
36591ba1
SL
1742
1743#define GOT_UNKNOWN 0
1744#define GOT_NORMAL 1
1745#define GOT_TLS_GD 2
1746#define GOT_TLS_IE 4
1747 unsigned char tls_type;
1748
1749 /* We need to detect and take special action for symbols which are only
1750 referenced with %call() and not with %got(). Such symbols do not need
1751 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
1752 linking will not work if the dynamic GOT reloc exists.
1753 To check for this condition efficiently, we compare got_types_used against
1c2de463
SL
1754 CALL_USED, meaning
1755 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1756 */
1757#define GOT_USED 1
1758#define CALL_USED 2
36591ba1
SL
1759 unsigned char got_types_used;
1760};
1761
1762#define elf32_nios2_hash_entry(ent) \
1763 ((struct elf32_nios2_link_hash_entry *) (ent))
1764
1765/* Get the Nios II elf linker hash table from a link_info structure. */
1766#define elf32_nios2_hash_table(info) \
1767 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1768
1769/* Nios II ELF linker hash table. */
1770struct elf32_nios2_link_hash_table
1771 {
1772 /* The main hash table. */
1773 struct elf_link_hash_table root;
1774
78058a5e
SL
1775 /* The stub hash table. */
1776 struct bfd_hash_table bstab;
1777
1778 /* Linker stub bfd. */
1779 bfd *stub_bfd;
1780
1781 /* Linker call-backs. */
1782 asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
1783 void (*layout_sections_again) (void);
1784
1785 /* Array to keep track of which stub sections have been created, and
1786 information on stub grouping. */
1787 struct map_stub
1788 {
1789 /* These are the section to which stubs in the group will be
1790 attached. */
1791 asection *first_sec, *last_sec;
1792 /* The stub sections. There might be stubs inserted either before
1793 or after the real section.*/
1794 asection *first_stub_sec, *last_stub_sec;
1795 } *stub_group;
1796
1797 /* Assorted information used by nios2_elf32_size_stubs. */
1798 unsigned int bfd_count;
7292b3ac 1799 unsigned int top_index;
78058a5e
SL
1800 asection **input_list;
1801 Elf_Internal_Sym **all_local_syms;
1802
36591ba1 1803 /* Short-cuts to get to dynamic linker sections. */
36591ba1
SL
1804 asection *sbss;
1805
82e91538
SL
1806 /* GOT pointer symbol _gp_got. */
1807 struct elf_link_hash_entry *h_gp_got;
1808
36591ba1
SL
1809 union {
1810 bfd_signed_vma refcount;
1811 bfd_vma offset;
1812 } tls_ldm_got;
1813
1814 /* Small local sym cache. */
1815 struct sym_cache sym_cache;
1816
1817 bfd_vma res_n_size;
1818 };
1819
1820struct nios2_elf32_obj_tdata
1821{
1822 struct elf_obj_tdata root;
1823
1824 /* tls_type for each local got entry. */
1825 char *local_got_tls_type;
1826
1827 /* TRUE if TLS GD relocs have been seen for this object. */
1828 bfd_boolean has_tlsgd;
1829};
1830
1831#define elf32_nios2_tdata(abfd) \
1832 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1833
1834#define elf32_nios2_local_got_tls_type(abfd) \
1835 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1836
1837/* The name of the dynamic interpreter. This is put in the .interp
1838 section. */
1839#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1840
1841/* PLT implementation for position-dependent code. */
1842static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1843 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1844 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1845 0x7800683a /* jmp r15 */
1846};
1847
1848static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1849 0x03800034, /* movhi r14, %hiadj(res_0) */
1850 0x73800004, /* addi r14, r14, %lo(res_0) */
1851 0x7b9fc83a, /* sub r15, r15, r14 */
1852 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1853 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1854 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1855 0x6800683a /* jmp r13 */
1856};
1857
1858/* PLT implementation for position-independent code. */
1859static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1860 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1861 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1862 0x00000006 /* br .PLTresolve */
1863};
1864
1865static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1866 0x001ce03a, /* nextpc r14 */
1867 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1868 0x6b9b883a, /* add r13, r13, r14 */
1869 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1870 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1871 0x6800683a /* jmp r13 */
1872};
1873
78058a5e
SL
1874/* CALL26 stub. */
1875static const bfd_vma nios2_call26_stub_entry[] = {
1876 0x00400034, /* orhi at, r0, %hiadj(dest) */
1877 0x08400004, /* addi at, at, %lo(dest) */
1878 0x0800683a /* jmp at */
1879};
1880
1881/* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1882static void
1883nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1884{
1885 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1886
83da6e74 1887 BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
78058a5e
SL
1888
1889 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1890 sec->contents + offset);
1891}
1892
1893/* Install COUNT 32-bit values DATA starting at offset OFFSET into
1894 section SEC. */
1895static void
1896nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1897 int count)
1898{
1899 while (count--)
1900 {
1901 bfd_put_32 (sec->owner, *data, sec->contents + offset);
1902 offset += 4;
1903 ++data;
1904 }
1905}
1906
1907/* The usual way of loading a 32-bit constant into a Nios II register is to
1908 load the high 16 bits in one instruction and then add the low 16 bits with
1909 a signed add. This means that the high halfword needs to be adjusted to
1910 compensate for the sign bit of the low halfword. This function returns the
1911 adjusted high halfword for a given 32-bit constant. */
1912static
1913bfd_vma hiadj (bfd_vma symbol_value)
1914{
1915 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1916}
1917
36591ba1
SL
1918/* Implement elf_backend_grok_prstatus:
1919 Support for core dump NOTE sections. */
1920static bfd_boolean
1921nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1922{
1923 int offset;
1924 size_t size;
1925
1926 switch (note->descsz)
1927 {
1928 default:
1929 return FALSE;
1930
1931 case 212: /* Linux/Nios II */
1932 /* pr_cursig */
228e534f 1933 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
36591ba1
SL
1934
1935 /* pr_pid */
228e534f 1936 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
36591ba1
SL
1937
1938 /* pr_reg */
1939 offset = 72;
1940 size = 136;
1941
1942 break;
1943 }
1944
1945 /* Make a ".reg/999" section. */
1946 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1947 size, note->descpos + offset);
1948}
1949
1950/* Implement elf_backend_grok_psinfo. */
1951static bfd_boolean
1952nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1953{
1954 switch (note->descsz)
1955 {
1956 default:
1957 return FALSE;
1958
1959 case 124: /* Linux/Nios II elf_prpsinfo */
228e534f 1960 elf_tdata (abfd)->core->program
36591ba1 1961 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 1962 elf_tdata (abfd)->core->command
36591ba1
SL
1963 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1964 }
1965
1966 /* Note that for some reason, a spurious space is tacked
1967 onto the end of the args in some (at least one anyway)
1968 implementations, so strip it off if it exists. */
1969
1970 {
228e534f 1971 char *command = elf_tdata (abfd)->core->command;
36591ba1
SL
1972 int n = strlen (command);
1973
1974 if (0 < n && command[n - 1] == ' ')
1975 command[n - 1] = '\0';
1976 }
1977
1978 return TRUE;
1979}
1980
78058a5e
SL
1981/* Assorted hash table functions. */
1982
1983/* Initialize an entry in the stub hash table. */
1984static struct bfd_hash_entry *
1985stub_hash_newfunc (struct bfd_hash_entry *entry,
1986 struct bfd_hash_table *table,
1987 const char *string)
1988{
1989 /* Allocate the structure if it has not already been allocated by a
1990 subclass. */
1991 if (entry == NULL)
1992 {
1993 entry = bfd_hash_allocate (table,
1994 sizeof (struct elf32_nios2_stub_hash_entry));
1995 if (entry == NULL)
1996 return entry;
1997 }
1998
1999 /* Call the allocation method of the superclass. */
2000 entry = bfd_hash_newfunc (entry, table, string);
2001 if (entry != NULL)
2002 {
2003 struct elf32_nios2_stub_hash_entry *hsh;
2004
2005 /* Initialize the local fields. */
2006 hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2007 hsh->stub_sec = NULL;
2008 hsh->stub_offset = 0;
2009 hsh->target_value = 0;
2010 hsh->target_section = NULL;
2011 hsh->stub_type = nios2_stub_none;
2012 hsh->hh = NULL;
2013 hsh->id_sec = NULL;
2014 }
2015
2016 return entry;
2017}
2018
36591ba1
SL
2019/* Create an entry in a Nios II ELF linker hash table. */
2020static struct bfd_hash_entry *
2021link_hash_newfunc (struct bfd_hash_entry *entry,
2022 struct bfd_hash_table *table, const char *string)
2023{
2024 /* Allocate the structure if it has not already been allocated by a
2025 subclass. */
2026 if (entry == NULL)
2027 {
2028 entry = bfd_hash_allocate (table,
2029 sizeof (struct elf32_nios2_link_hash_entry));
2030 if (entry == NULL)
2031 return entry;
2032 }
2033
2034 /* Call the allocation method of the superclass. */
2035 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2036 if (entry)
2037 {
2038 struct elf32_nios2_link_hash_entry *eh;
2039
2040 eh = (struct elf32_nios2_link_hash_entry *) entry;
78058a5e 2041 eh->hsh_cache = NULL;
36591ba1
SL
2042 eh->dyn_relocs = NULL;
2043 eh->tls_type = GOT_UNKNOWN;
2044 eh->got_types_used = 0;
2045 }
2046
2047 return entry;
2048}
2049
78058a5e
SL
2050/* Section name for stubs is the associated section name plus this
2051 string. */
2052#define STUB_SUFFIX ".stub"
2053
2054/* Build a name for an entry in the stub hash table. */
2055static char *
2056nios2_stub_name (const asection *input_section,
2057 const asection *sym_sec,
2058 const struct elf32_nios2_link_hash_entry *hh,
2059 const Elf_Internal_Rela *rel,
2060 enum elf32_nios2_stub_type stub_type)
2061{
2062 char *stub_name;
2063 bfd_size_type len;
2064 char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2065
2066 if (hh)
2067 {
2068 len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2069 stub_name = bfd_malloc (len);
2070 if (stub_name != NULL)
2071 {
2072 sprintf (stub_name, "%08x_%c_%s+%x",
2073 input_section->id & 0xffffffff,
2074 stubpos,
2075 hh->root.root.root.string,
2076 (int) rel->r_addend & 0xffffffff);
2077 }
2078 }
2079 else
2080 {
2081 len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2082 stub_name = bfd_malloc (len);
2083 if (stub_name != NULL)
2084 {
2085 sprintf (stub_name, "%08x_%c_%x:%x+%x",
2086 input_section->id & 0xffffffff,
2087 stubpos,
2088 sym_sec->id & 0xffffffff,
2089 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2090 (int) rel->r_addend & 0xffffffff);
2091 }
2092 }
2093 return stub_name;
2094}
2095
2096/* Look up an entry in the stub hash. Stub entries are cached because
2097 creating the stub name takes a bit of time. */
2098static struct elf32_nios2_stub_hash_entry *
2099nios2_get_stub_entry (const asection *input_section,
2100 const asection *sym_sec,
2101 struct elf32_nios2_link_hash_entry *hh,
2102 const Elf_Internal_Rela *rel,
2103 struct elf32_nios2_link_hash_table *htab,
2104 enum elf32_nios2_stub_type stub_type)
2105{
2106 struct elf32_nios2_stub_hash_entry *hsh;
2107 const asection *id_sec;
2108
2109 /* If this input section is part of a group of sections sharing one
2110 stub section, then use the id of the first/last section in the group,
2111 depending on the stub section placement relative to the group.
2112 Stub names need to include a section id, as there may well be
2113 more than one stub used to reach say, printf, and we need to
2114 distinguish between them. */
2115 if (stub_type == nios2_stub_call26_before)
2116 id_sec = htab->stub_group[input_section->id].first_sec;
2117 else
2118 id_sec = htab->stub_group[input_section->id].last_sec;
2119
2120 if (hh != NULL && hh->hsh_cache != NULL
2121 && hh->hsh_cache->hh == hh
2122 && hh->hsh_cache->id_sec == id_sec
2123 && hh->hsh_cache->stub_type == stub_type)
2124 {
2125 hsh = hh->hsh_cache;
2126 }
2127 else
2128 {
2129 char *stub_name;
2130
2131 stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2132 if (stub_name == NULL)
2133 return NULL;
2134
2135 hsh = nios2_stub_hash_lookup (&htab->bstab,
2136 stub_name, FALSE, FALSE);
2137
2138 if (hh != NULL)
2139 hh->hsh_cache = hsh;
2140
2141 free (stub_name);
2142 }
2143
2144 return hsh;
2145}
2146
2147/* Add a new stub entry to the stub hash. Not all fields of the new
2148 stub entry are initialised. */
2149static struct elf32_nios2_stub_hash_entry *
2150nios2_add_stub (const char *stub_name,
2151 asection *section,
2152 struct elf32_nios2_link_hash_table *htab,
2153 enum elf32_nios2_stub_type stub_type)
2154{
2155 asection *link_sec;
2156 asection *stub_sec;
2157 asection **secptr, **linkptr;
2158 struct elf32_nios2_stub_hash_entry *hsh;
2159 bfd_boolean afterp;
2160
2161 if (stub_type == nios2_stub_call26_before)
2162 {
2163 link_sec = htab->stub_group[section->id].first_sec;
2164 secptr = &(htab->stub_group[section->id].first_stub_sec);
2165 linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2166 afterp = FALSE;
2167 }
2168 else
2169 {
2170 link_sec = htab->stub_group[section->id].last_sec;
2171 secptr = &(htab->stub_group[section->id].last_stub_sec);
2172 linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2173 afterp = TRUE;
2174 }
2175 stub_sec = *secptr;
2176 if (stub_sec == NULL)
2177 {
2178 stub_sec = *linkptr;
2179 if (stub_sec == NULL)
2180 {
2181 size_t namelen;
2182 bfd_size_type len;
2183 char *s_name;
2184
2185 namelen = strlen (link_sec->name);
2186 len = namelen + sizeof (STUB_SUFFIX);
2187 s_name = bfd_alloc (htab->stub_bfd, len);
2188 if (s_name == NULL)
2189 return NULL;
2190
2191 memcpy (s_name, link_sec->name, namelen);
2192 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2193
2194 stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2195 if (stub_sec == NULL)
2196 return NULL;
2197 *linkptr = stub_sec;
2198 }
2199 *secptr = stub_sec;
2200 }
2201
2202 /* Enter this entry into the linker stub hash table. */
2203 hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2204 TRUE, FALSE);
2205 if (hsh == NULL)
2206 {
695344c0 2207 /* xgettext:c-format */
871b3ab2 2208 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4eca0228
AM
2209 section->owner,
2210 stub_name);
78058a5e
SL
2211 return NULL;
2212 }
2213
2214 hsh->stub_sec = stub_sec;
2215 hsh->stub_offset = 0;
2216 hsh->id_sec = link_sec;
2217 return hsh;
2218}
2219
2220/* Set up various things so that we can make a list of input sections
2221 for each output section included in the link. Returns -1 on error,
2222 0 when no stubs will be needed, and 1 on success. */
2223int
2224nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2225{
2226 bfd *input_bfd;
2227 unsigned int bfd_count;
7292b3ac 2228 unsigned int top_id, top_index;
78058a5e
SL
2229 asection *section;
2230 asection **input_list, **list;
2231 bfd_size_type amt;
2232 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2233
2234 /* Count the number of input BFDs and find the top input section id. */
2235 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2236 input_bfd != NULL;
c72f2fb2 2237 input_bfd = input_bfd->link.next)
78058a5e
SL
2238 {
2239 bfd_count += 1;
2240 for (section = input_bfd->sections;
2241 section != NULL;
2242 section = section->next)
2243 {
2244 if (top_id < section->id)
2245 top_id = section->id;
2246 }
2247 }
2248
2249 htab->bfd_count = bfd_count;
2250
2251 amt = sizeof (struct map_stub) * (top_id + 1);
2252 htab->stub_group = bfd_zmalloc (amt);
2253 if (htab->stub_group == NULL)
2254 return -1;
2255
2256 /* We can't use output_bfd->section_count here to find the top output
2257 section index as some sections may have been removed, and
2258 strip_excluded_output_sections doesn't renumber the indices. */
2259 for (section = output_bfd->sections, top_index = 0;
2260 section != NULL;
2261 section = section->next)
2262 {
2263 if (top_index < section->index)
2264 top_index = section->index;
2265 }
2266
2267 htab->top_index = top_index;
2268 amt = sizeof (asection *) * (top_index + 1);
2269 input_list = bfd_malloc (amt);
2270 htab->input_list = input_list;
2271 if (input_list == NULL)
2272 return -1;
2273
2274 /* For sections we aren't interested in, mark their entries with a
2275 value we can check later. */
2276 list = input_list + top_index;
2277 do
2278 *list = bfd_abs_section_ptr;
2279 while (list-- != input_list);
2280
2281 for (section = output_bfd->sections;
2282 section != NULL;
2283 section = section->next)
2284 {
2285 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2286 * have PC relative relocs in them but no code flag set. */
2287 if (((section->flags & SEC_CODE) != 0) ||
2288 strcmp(".ctors", section->name) ||
2289 strcmp(".dtors", section->name))
2290 input_list[section->index] = NULL;
2291 }
2292
2293 return 1;
2294}
2295
2296/* The linker repeatedly calls this function for each input section,
2297 in the order that input sections are linked into output sections.
2298 Build lists of input sections to determine groupings between which
2299 we may insert linker stubs. */
2300void
2301nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2302{
2303 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2304
2305 if (isec->output_section->index <= htab->top_index)
2306 {
2307 asection **list = htab->input_list + isec->output_section->index;
2308 if (*list != bfd_abs_section_ptr)
2309 {
2310 /* Steal the last_sec pointer for our list.
2311 This happens to make the list in reverse order,
2312 which is what we want. */
2313 htab->stub_group[isec->id].last_sec = *list;
2314 *list = isec;
2315 }
2316 }
2317}
2318
2319/* Segment mask for CALL26 relocation relaxation. */
2320#define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2321
2322/* Fudge factor for approximate maximum size of all stubs that might
2323 be inserted by the linker. This does not actually limit the number
2324 of stubs that might be inserted, and only affects strategy for grouping
2325 and placement of stubs. Perhaps this should be computed based on number
2326 of relocations seen, or be specifiable on the command line. */
2327#define MAX_STUB_SECTION_SIZE 0xffff
2328
2329/* See whether we can group stub sections together. Grouping stub
2330 sections may result in fewer stubs. More importantly, we need to
2331 put all .init* and .fini* stubs at the end of the .init or
2332 .fini output sections respectively, because glibc splits the
2333 _init and _fini functions into multiple parts. Putting a stub in
2334 the middle of a function is not a good idea.
2335 Rather than computing groups of a maximum fixed size, for Nios II
2336 CALL26 relaxation it makes more sense to compute the groups based on
2337 sections that fit within a 256MB address segment. Also do not allow
2338 a group to span more than one output section, since different output
2339 sections might correspond to different memory banks on a bare-metal
2340 target, etc. */
2341static void
2342group_sections (struct elf32_nios2_link_hash_table *htab)
2343{
2344 asection **list = htab->input_list + htab->top_index;
2345 do
2346 {
2347 /* The list is in reverse order so we'll search backwards looking
2348 for the first section that begins in the same memory segment,
2349 marking sections along the way to point at the tail for this
2350 group. */
2351 asection *tail = *list;
2352 if (tail == bfd_abs_section_ptr)
2353 continue;
2354 while (tail != NULL)
2355 {
2356 bfd_vma start = tail->output_section->vma + tail->output_offset;
2357 bfd_vma end = start + tail->size;
2358 bfd_vma segment = CALL26_SEGMENT (end);
2359 asection *prev;
2360
2361 if (segment != CALL26_SEGMENT (start)
2362 || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2363 /* This section spans more than one memory segment, or is
2364 close enough to the end of the segment that adding stub
2365 sections before it might cause it to move so that it
2366 spans memory segments, or that stubs added at the end of
2367 this group might overflow into the next memory segment.
2368 Put it in a group by itself to localize the effects. */
2369 {
2370 prev = htab->stub_group[tail->id].last_sec;
2371 htab->stub_group[tail->id].last_sec = tail;
2372 htab->stub_group[tail->id].first_sec = tail;
2373 }
2374 else
2375 /* Collect more sections for this group. */
2376 {
2377 asection *curr, *first;
2378 for (curr = tail; ; curr = prev)
2379 {
2380 prev = htab->stub_group[curr->id].last_sec;
2381 if (!prev
2382 || tail->output_section != prev->output_section
2383 || (CALL26_SEGMENT (prev->output_section->vma
2384 + prev->output_offset)
2385 != segment))
2386 break;
2387 }
2388 first = curr;
2389 for (curr = tail; ; curr = prev)
2390 {
2391 prev = htab->stub_group[curr->id].last_sec;
2392 htab->stub_group[curr->id].last_sec = tail;
2393 htab->stub_group[curr->id].first_sec = first;
2394 if (curr == first)
2395 break;
2396 }
2397 }
2398
2399 /* Reset tail for the next group. */
2400 tail = prev;
2401 }
2402 }
2403 while (list-- != htab->input_list);
2404 free (htab->input_list);
2405}
2406
2407/* Determine the type of stub needed, if any, for a call. */
2408static enum elf32_nios2_stub_type
2409nios2_type_of_stub (asection *input_sec,
2410 const Elf_Internal_Rela *rel,
2411 struct elf32_nios2_link_hash_entry *hh,
2412 struct elf32_nios2_link_hash_table *htab,
2413 bfd_vma destination,
2414 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2415{
2416 bfd_vma location, segment, start, end;
2417 asection *s0, *s1, *s;
2418
2419 if (hh != NULL &&
2420 !(hh->root.root.type == bfd_link_hash_defined
2421 || hh->root.root.type == bfd_link_hash_defweak))
2422 return nios2_stub_none;
2423
2424 /* Determine where the call point is. */
2425 location = (input_sec->output_section->vma
2426 + input_sec->output_offset + rel->r_offset);
2427 segment = CALL26_SEGMENT (location);
2428
2429 /* Nios II CALL and JMPI instructions can transfer control to addresses
2430 within the same 256MB segment as the PC. */
2431 if (segment == CALL26_SEGMENT (destination))
2432 return nios2_stub_none;
2433
2434 /* Find the start and end addresses of the stub group. Also account for
2435 any already-created stub sections for this group. Note that for stubs
2436 in the end section, only the first instruction of the last stub
2437 (12 bytes long) needs to be within range. */
2438 s0 = htab->stub_group[input_sec->id].first_sec;
2439 s = htab->stub_group[s0->id].first_stub_sec;
2440 if (s != NULL && s->size > 0)
2441 start = s->output_section->vma + s->output_offset;
2442 else
2443 start = s0->output_section->vma + s0->output_offset;
2444
2445 s1 = htab->stub_group[input_sec->id].last_sec;
2446 s = htab->stub_group[s1->id].last_stub_sec;
2447 if (s != NULL && s->size > 0)
2448 end = s->output_section->vma + s->output_offset + s->size - 8;
2449 else
2450 end = s1->output_section->vma + s1->output_offset + s1->size;
2451
2452 BFD_ASSERT (start < end);
2453 BFD_ASSERT (start <= location);
2454 BFD_ASSERT (location < end);
2455
2456 /* Put stubs at the end of the group unless that is not a valid
2457 location and the beginning of the group is. It might be that
2458 neither the beginning nor end works if we have an input section
2459 so large that it spans multiple segment boundaries. In that
2460 case, punt; the end result will be a relocation overflow error no
2461 matter what we do here.
2462
2463 Note that adding stubs pushes up the addresses of all subsequent
2464 sections, so that stubs allocated on one pass through the
2465 relaxation loop may not be valid on the next pass. (E.g., we may
2466 allocate a stub at the beginning of the section on one pass and
2467 find that the call site has been bumped into the next memory
2468 segment on the next pass.) The important thing to note is that
2469 we never try to reclaim the space allocated to such unused stubs,
2470 so code size and section addresses can only increase with each
2471 iteration. Accounting for the start and end addresses of the
2472 already-created stub sections ensures that when the algorithm
2473 converges, it converges accurately, with the entire appropriate
2474 stub section accessible from the call site and not just the
2475 address at the start or end of the stub group proper. */
2476
2477 if (segment == CALL26_SEGMENT (end))
2478 return nios2_stub_call26_after;
2479 else if (segment == CALL26_SEGMENT (start))
2480 return nios2_stub_call26_before;
2481 else
2482 /* Perhaps this should be a dedicated error code. */
2483 return nios2_stub_none;
2484}
2485
2486static bfd_boolean
2487nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2488{
2489 struct elf32_nios2_stub_hash_entry *hsh
2490 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2491 asection *stub_sec = hsh->stub_sec;
2492 bfd_vma sym_value;
2493
2494 /* Make a note of the offset within the stubs for this entry. */
2495 hsh->stub_offset = stub_sec->size;
2496
2497 switch (hsh->stub_type)
2498 {
2499 case nios2_stub_call26_before:
2500 case nios2_stub_call26_after:
2501 /* A call26 stub looks like:
2502 orhi at, %hiadj(dest)
2503 addi at, at, %lo(dest)
2504 jmp at
2505 Note that call/jmpi instructions can't be used in PIC code
2506 so there is no reason for the stub to be PIC, either. */
2507 sym_value = (hsh->target_value
2508 + hsh->target_section->output_offset
2509 + hsh->target_section->output_section->vma
2510 + hsh->addend);
2511
2512 nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2513 hsh->stub_offset, 3);
2514 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2515 hiadj (sym_value));
2516 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2517 (sym_value & 0xffff));
2518 stub_sec->size += 12;
2519 break;
2520 default:
2521 BFD_FAIL ();
2522 return FALSE;
2523 }
2524
2525 return TRUE;
2526}
2527
2528/* As above, but don't actually build the stub. Just bump offset so
2529 we know stub section sizes. */
2530static bfd_boolean
2531nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2532{
2533 struct elf32_nios2_stub_hash_entry *hsh
2534 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2535
2536 switch (hsh->stub_type)
2537 {
2538 case nios2_stub_call26_before:
2539 case nios2_stub_call26_after:
2540 hsh->stub_sec->size += 12;
2541 break;
2542 default:
2543 BFD_FAIL ();
2544 return FALSE;
2545 }
2546 return TRUE;
2547}
2548
2549/* Read in all local syms for all input bfds.
2550 Returns -1 on error, 0 otherwise. */
2551
2552static int
2553get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2554 struct bfd_link_info *info)
2555{
2556 unsigned int bfd_indx;
2557 Elf_Internal_Sym *local_syms, **all_local_syms;
2558 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2559
2560 /* We want to read in symbol extension records only once. To do this
2561 we need to read in the local symbols in parallel and save them for
2562 later use; so hold pointers to the local symbols in an array. */
2563 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2564 all_local_syms = bfd_zmalloc (amt);
2565 htab->all_local_syms = all_local_syms;
2566 if (all_local_syms == NULL)
2567 return -1;
2568
2569 /* Walk over all the input BFDs, swapping in local symbols. */
2570 for (bfd_indx = 0;
2571 input_bfd != NULL;
c72f2fb2 2572 input_bfd = input_bfd->link.next, bfd_indx++)
78058a5e
SL
2573 {
2574 Elf_Internal_Shdr *symtab_hdr;
2575
2576 /* We'll need the symbol table in a second. */
2577 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2578 if (symtab_hdr->sh_info == 0)
2579 continue;
2580
2581 /* We need an array of the local symbols attached to the input bfd. */
2582 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2583 if (local_syms == NULL)
2584 {
2585 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2586 symtab_hdr->sh_info, 0,
2587 NULL, NULL, NULL);
2588 /* Cache them for elf_link_input_bfd. */
2589 symtab_hdr->contents = (unsigned char *) local_syms;
2590 }
2591 if (local_syms == NULL)
2592 return -1;
2593
2594 all_local_syms[bfd_indx] = local_syms;
2595 }
2596
2597 return 0;
2598}
2599
2600/* Determine and set the size of the stub section for a final link. */
2601bfd_boolean
2602nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2603 struct bfd_link_info *info,
2604 asection *(*add_stub_section) (const char *,
2605 asection *, bfd_boolean),
2606 void (*layout_sections_again) (void))
2607{
2608 bfd_boolean stub_changed = FALSE;
2609 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2610
2611 /* Stash our params away. */
2612 htab->stub_bfd = stub_bfd;
2613 htab->add_stub_section = add_stub_section;
2614 htab->layout_sections_again = layout_sections_again;
2615
2616 /* FIXME: We only compute the section groups once. This could cause
2617 problems if adding a large stub section causes following sections,
2618 or parts of them, to move into another segment. However, this seems
2619 to be consistent with the way other back ends handle this.... */
2620 group_sections (htab);
2621
2622 if (get_local_syms (output_bfd, info->input_bfds, info))
2623 {
2624 if (htab->all_local_syms)
2625 goto error_ret_free_local;
2626 return FALSE;
2627 }
2628
2629 while (1)
2630 {
2631 bfd *input_bfd;
2632 unsigned int bfd_indx;
2633 asection *stub_sec;
2634
2635 for (input_bfd = info->input_bfds, bfd_indx = 0;
2636 input_bfd != NULL;
c72f2fb2 2637 input_bfd = input_bfd->link.next, bfd_indx++)
78058a5e
SL
2638 {
2639 Elf_Internal_Shdr *symtab_hdr;
2640 asection *section;
2641 Elf_Internal_Sym *local_syms;
2642
2643 /* We'll need the symbol table in a second. */
2644 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2645 if (symtab_hdr->sh_info == 0)
2646 continue;
2647
2648 local_syms = htab->all_local_syms[bfd_indx];
2649
2650 /* Walk over each section attached to the input bfd. */
2651 for (section = input_bfd->sections;
2652 section != NULL;
2653 section = section->next)
2654 {
2655 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2656
2657 /* If there aren't any relocs, then there's nothing more
2658 to do. */
2659 if ((section->flags & SEC_RELOC) == 0
2660 || section->reloc_count == 0)
2661 continue;
2662
2663 /* If this section is a link-once section that will be
2664 discarded, then don't create any stubs. */
2665 if (section->output_section == NULL
2666 || section->output_section->owner != output_bfd)
2667 continue;
2668
2669 /* Get the relocs. */
2670 internal_relocs
2671 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2672 info->keep_memory);
2673 if (internal_relocs == NULL)
2674 goto error_ret_free_local;
2675
2676 /* Now examine each relocation. */
2677 irela = internal_relocs;
2678 irelaend = irela + section->reloc_count;
2679 for (; irela < irelaend; irela++)
2680 {
2681 unsigned int r_type, r_indx;
2682 enum elf32_nios2_stub_type stub_type;
2683 struct elf32_nios2_stub_hash_entry *hsh;
2684 asection *sym_sec;
2685 bfd_vma sym_value;
2686 bfd_vma destination;
2687 struct elf32_nios2_link_hash_entry *hh;
2688 char *stub_name;
2689 const asection *id_sec;
2690
2691 r_type = ELF32_R_TYPE (irela->r_info);
2692 r_indx = ELF32_R_SYM (irela->r_info);
2693
2694 if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2695 {
2696 bfd_set_error (bfd_error_bad_value);
2697 error_ret_free_internal:
2698 if (elf_section_data (section)->relocs == NULL)
2699 free (internal_relocs);
2700 goto error_ret_free_local;
2701 }
2702
2703 /* Only look for stubs on CALL and JMPI instructions. */
2704 if (r_type != (unsigned int) R_NIOS2_CALL26)
2705 continue;
2706
2707 /* Now determine the call target, its name, value,
2708 section. */
2709 sym_sec = NULL;
2710 sym_value = 0;
2711 destination = 0;
2712 hh = NULL;
2713 if (r_indx < symtab_hdr->sh_info)
2714 {
2715 /* It's a local symbol. */
2716 Elf_Internal_Sym *sym;
2717 Elf_Internal_Shdr *hdr;
2718 unsigned int shndx;
2719
2720 sym = local_syms + r_indx;
2721 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2722 sym_value = sym->st_value;
2723 shndx = sym->st_shndx;
2724 if (shndx < elf_numsections (input_bfd))
2725 {
2726 hdr = elf_elfsections (input_bfd)[shndx];
2727 sym_sec = hdr->bfd_section;
2728 destination = (sym_value + irela->r_addend
2729 + sym_sec->output_offset
2730 + sym_sec->output_section->vma);
2731 }
2732 }
2733 else
2734 {
2735 /* It's an external symbol. */
2736 int e_indx;
2737
2738 e_indx = r_indx - symtab_hdr->sh_info;
2739 hh = ((struct elf32_nios2_link_hash_entry *)
2740 elf_sym_hashes (input_bfd)[e_indx]);
2741
2742 while (hh->root.root.type == bfd_link_hash_indirect
2743 || hh->root.root.type == bfd_link_hash_warning)
2744 hh = ((struct elf32_nios2_link_hash_entry *)
2745 hh->root.root.u.i.link);
2746
2747 if (hh->root.root.type == bfd_link_hash_defined
2748 || hh->root.root.type == bfd_link_hash_defweak)
2749 {
2750 sym_sec = hh->root.root.u.def.section;
2751 sym_value = hh->root.root.u.def.value;
2752
2753 if (sym_sec->output_section != NULL)
2754 destination = (sym_value + irela->r_addend
2755 + sym_sec->output_offset
2756 + sym_sec->output_section->vma);
2757 else
2758 continue;
2759 }
2760 else if (hh->root.root.type == bfd_link_hash_undefweak)
2761 {
0e1862bb 2762 if (! bfd_link_pic (info))
78058a5e
SL
2763 continue;
2764 }
2765 else if (hh->root.root.type == bfd_link_hash_undefined)
2766 {
2767 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2768 && (ELF_ST_VISIBILITY (hh->root.other)
2769 == STV_DEFAULT)))
2770 continue;
2771 }
2772 else
2773 {
2774 bfd_set_error (bfd_error_bad_value);
2775 goto error_ret_free_internal;
2776 }
2777 }
2778
2779 /* Determine what (if any) linker stub is needed. */
2780 stub_type = nios2_type_of_stub (section, irela, hh, htab,
2781 destination, info);
2782 if (stub_type == nios2_stub_none)
2783 continue;
2784
2785 /* Support for grouping stub sections. */
2786 if (stub_type == nios2_stub_call26_before)
2787 id_sec = htab->stub_group[section->id].first_sec;
2788 else
2789 id_sec = htab->stub_group[section->id].last_sec;
2790
2791 /* Get the name of this stub. */
2792 stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2793 stub_type);
2794 if (!stub_name)
2795 goto error_ret_free_internal;
2796
2797 hsh = nios2_stub_hash_lookup (&htab->bstab,
2798 stub_name,
2799 FALSE, FALSE);
2800 if (hsh != NULL)
2801 {
2802 /* The proper stub has already been created. */
2803 free (stub_name);
2804 continue;
2805 }
2806
2807 hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2808 if (hsh == NULL)
2809 {
2810 free (stub_name);
2811 goto error_ret_free_internal;
2812 }
2813 hsh->target_value = sym_value;
2814 hsh->target_section = sym_sec;
2815 hsh->stub_type = stub_type;
2816 hsh->hh = hh;
2817 hsh->addend = irela->r_addend;
2818 stub_changed = TRUE;
2819 }
2820
2821 /* We're done with the internal relocs, free them. */
2822 if (elf_section_data (section)->relocs == NULL)
2823 free (internal_relocs);
2824 }
2825 }
2826
2827 if (!stub_changed)
2828 break;
2829
2830 /* OK, we've added some stubs. Find out the new size of the
2831 stub sections. */
2832 for (stub_sec = htab->stub_bfd->sections;
2833 stub_sec != NULL;
2834 stub_sec = stub_sec->next)
2835 stub_sec->size = 0;
2836
2837 bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2838
2839 /* Ask the linker to do its stuff. */
2840 (*htab->layout_sections_again) ();
2841 stub_changed = FALSE;
2842 }
2843
2844 free (htab->all_local_syms);
2845 return TRUE;
2846
2847 error_ret_free_local:
2848 free (htab->all_local_syms);
2849 return FALSE;
2850}
2851
2852/* Build all the stubs associated with the current output file. The
2853 stubs are kept in a hash table attached to the main linker hash
2854 table. This function is called via nios2elf_finish in the linker. */
2855bfd_boolean
2856nios2_elf32_build_stubs (struct bfd_link_info *info)
2857{
2858 asection *stub_sec;
2859 struct bfd_hash_table *table;
2860 struct elf32_nios2_link_hash_table *htab;
2861
2862 htab = elf32_nios2_hash_table (info);
2863
2864 for (stub_sec = htab->stub_bfd->sections;
2865 stub_sec != NULL;
2866 stub_sec = stub_sec->next)
1511baec
SL
2867 /* The stub_bfd may contain non-stub sections if it is also the
2868 dynobj. Any such non-stub sections are created with the
2869 SEC_LINKER_CREATED flag set, while stub sections do not
2870 have that flag. Ignore any non-stub sections here. */
2871 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
1b786873 2872 {
1511baec
SL
2873 bfd_size_type size;
2874
2875 /* Allocate memory to hold the linker stubs. */
2876 size = stub_sec->size;
2877 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2878 if (stub_sec->contents == NULL && size != 0)
2879 return FALSE;
2880 stub_sec->size = 0;
2881 }
78058a5e
SL
2882
2883 /* Build the stubs as directed by the stub hash table. */
2884 table = &htab->bstab;
2885 bfd_hash_traverse (table, nios2_build_one_stub, info);
2886
2887 return TRUE;
2888}
2889
2890
965b1d80
SL
2891#define is_nios2_elf(bfd) \
2892 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2893 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2894
2895/* Merge backend specific data from an object file to the output
2896 object file when linking. */
2897
2898static bfd_boolean
50e03d47 2899nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
965b1d80 2900{
50e03d47 2901 bfd *obfd = info->output_bfd;
965b1d80
SL
2902 flagword old_flags;
2903 flagword new_flags;
2904
2905 if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2906 return TRUE;
2907
2908 /* Check if we have the same endianness. */
50e03d47 2909 if (! _bfd_generic_verify_endian_match (ibfd, info))
965b1d80
SL
2910 return FALSE;
2911
2912 new_flags = elf_elfheader (ibfd)->e_flags;
2913 old_flags = elf_elfheader (obfd)->e_flags;
2914 if (!elf_flags_init (obfd))
2915 {
2916 /* First call, no flags set. */
2917 elf_flags_init (obfd) = TRUE;
2918 elf_elfheader (obfd)->e_flags = new_flags;
2919
2920 switch (new_flags)
2921 {
2922 default:
2923 case EF_NIOS2_ARCH_R1:
2924 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2925 break;
2926 case EF_NIOS2_ARCH_R2:
2927 if (bfd_big_endian (ibfd))
2928 {
4eca0228 2929 _bfd_error_handler
38f14ab8 2930 (_("error: %pB: big-endian R2 is not supported"), ibfd);
965b1d80
SL
2931 bfd_set_error (bfd_error_bad_value);
2932 return FALSE;
2933 }
2934 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2935 break;
2936 }
2937 }
2938
2939 /* Incompatible flags. */
2940 else if (new_flags != old_flags)
2941 {
2942 /* So far, the only incompatible flags denote incompatible
2943 architectures. */
4eca0228 2944 _bfd_error_handler
695344c0 2945 /* xgettext:c-format */
38f14ab8 2946 (_("error: %pB: conflicting CPU architectures %d/%d"),
965b1d80
SL
2947 ibfd, new_flags, old_flags);
2948 bfd_set_error (bfd_error_bad_value);
2949 return FALSE;
2950 }
2951
2952 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 2953 _bfd_elf_merge_object_attributes (ibfd, info);
965b1d80
SL
2954
2955 return TRUE;
2956}
2957
36591ba1
SL
2958/* Implement bfd_elf32_bfd_reloc_type_lookup:
2959 Given a BFD reloc type, return a howto structure. */
f3185997 2960
36591ba1 2961static reloc_howto_type *
8c163c5a 2962nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
36591ba1
SL
2963 bfd_reloc_code_real_type code)
2964{
2965 int i;
1b786873 2966
f3185997 2967 for (i = 0; i < (int) ARRAY_SIZE (nios2_reloc_map); ++i)
36591ba1 2968 if (nios2_reloc_map[i].bfd_val == code)
8c163c5a 2969 return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
36591ba1
SL
2970 return NULL;
2971}
2972
2973/* Implement bfd_elf32_bfd_reloc_name_lookup:
2974 Given a reloc name, return a howto structure. */
f3185997 2975
36591ba1 2976static reloc_howto_type *
8c163c5a 2977nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
36591ba1
SL
2978 const char *r_name)
2979{
8c163c5a
SL
2980 int i;
2981 reloc_howto_type *howto_tbl;
2982 int howto_tbl_size;
36591ba1 2983
8c163c5a
SL
2984 if (BFD_IS_R2 (abfd))
2985 {
2986 howto_tbl = elf_nios2_r2_howto_table_rel;
f3185997 2987 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
8c163c5a
SL
2988 }
2989 else
2990 {
2991 howto_tbl = elf_nios2_r1_howto_table_rel;
f3185997 2992 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
8c163c5a 2993 }
1b786873 2994
8c163c5a
SL
2995 for (i = 0; i < howto_tbl_size; i++)
2996 if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
2997 return howto_tbl + i;
f3185997 2998
1b786873 2999 return NULL;
36591ba1
SL
3000}
3001
3002/* Implement elf_info_to_howto:
3003 Given a ELF32 relocation, fill in a arelent structure. */
f3185997
NC
3004
3005static bfd_boolean
8c163c5a 3006nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
36591ba1
SL
3007 Elf_Internal_Rela *dst)
3008{
3009 unsigned int r_type;
3010
3011 r_type = ELF32_R_TYPE (dst->r_info);
f3185997
NC
3012 if ((cache_ptr->howto = lookup_howto (r_type, abfd)) == NULL)
3013 {
3014 /* xgettext:c-format */
3015 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3016 abfd, r_type);
3017 bfd_set_error (bfd_error_bad_value);
3018 return FALSE;
3019 }
3020 return TRUE;
36591ba1
SL
3021}
3022
3023/* Return the base VMA address which should be subtracted from real addresses
3024 when resolving @dtpoff relocation.
3025 This is PT_TLS segment p_vaddr. */
3026static bfd_vma
3027dtpoff_base (struct bfd_link_info *info)
3028{
3029 /* If tls_sec is NULL, we should have signalled an error already. */
3030 if (elf_hash_table (info)->tls_sec == NULL)
3031 return 0;
3032 return elf_hash_table (info)->tls_sec->vma;
3033}
3034
3035/* Return the relocation value for @tpoff relocation
3036 if STT_TLS virtual address is ADDRESS. */
3037static bfd_vma
3038tpoff (struct bfd_link_info *info, bfd_vma address)
3039{
3040 struct elf_link_hash_table *htab = elf_hash_table (info);
3041
3042 /* If tls_sec is NULL, we should have signalled an error already. */
3043 if (htab->tls_sec == NULL)
3044 return 0;
3045 return address - htab->tls_sec->vma;
3046}
3047
3048/* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3049 dangerous relocation. */
3050static bfd_boolean
3051nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3052{
3053
3054 bfd_boolean gp_found;
3055 struct bfd_hash_entry *h;
3056 struct bfd_link_hash_entry *lh;
3057
3058 /* If we've already figured out what GP will be, just return it. */
3059 *pgp = _bfd_get_gp_value (output_bfd);
3060 if (*pgp)
3061 return TRUE;
3062
3063 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
3064 lh = (struct bfd_link_hash_entry *) h;
3065lookup:
3066 if (lh)
3067 {
3068 switch (lh->type)
3069 {
3070 case bfd_link_hash_undefined:
3071 case bfd_link_hash_undefweak:
3072 case bfd_link_hash_common:
3073 gp_found = FALSE;
3074 break;
3075 case bfd_link_hash_defined:
3076 case bfd_link_hash_defweak:
3077 gp_found = TRUE;
a7be2893
SL
3078 {
3079 asection *sym_sec = lh->u.def.section;
3080 bfd_vma sym_value = lh->u.def.value;
3081
3082 if (sym_sec->output_section)
3083 sym_value = (sym_value + sym_sec->output_offset
3084 + sym_sec->output_section->vma);
3085 *pgp = sym_value;
3086 }
36591ba1
SL
3087 break;
3088 case bfd_link_hash_indirect:
3089 case bfd_link_hash_warning:
3090 lh = lh->u.i.link;
3091 /* @@FIXME ignoring warning for now */
3092 goto lookup;
3093 case bfd_link_hash_new:
3094 default:
3095 abort ();
3096 }
3097 }
3098 else
3099 gp_found = FALSE;
3100
3101 if (!gp_found)
3102 {
3103 /* Only get the error once. */
3104 *pgp = 4;
3105 _bfd_set_gp_value (output_bfd, *pgp);
3106 return FALSE;
3107 }
3108
3109 _bfd_set_gp_value (output_bfd, *pgp);
3110
3111 return TRUE;
3112}
3113
3114/* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3115 if it's not available as we don't have a link_info pointer available here
3116 to look it up in the output symbol table. We don't need to adjust the
3117 symbol value for an external symbol if we are producing relocatable
3118 output. */
3119static bfd_reloc_status_type
1b786873 3120nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
36591ba1
SL
3121 char **error_message, bfd_vma *pgp)
3122{
3123 if (bfd_is_und_section (symbol->section) && !relocatable)
3124 {
3125 *pgp = 0;
3126 return bfd_reloc_undefined;
3127 }
3128
3129 *pgp = _bfd_get_gp_value (output_bfd);
3130 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3131 {
3132 if (relocatable)
3133 {
3134 /* Make up a value. */
3135 *pgp = symbol->section->output_section->vma + 0x4000;
3136 _bfd_set_gp_value (output_bfd, *pgp);
3137 }
3138 else
3139 {
3140 *error_message
3141 = (char *) _("global pointer relative relocation when _gp not defined");
3142 return bfd_reloc_dangerous;
3143 }
3144 }
3145
3146 return bfd_reloc_ok;
3147}
3148
36591ba1
SL
3149/* Do the relocations that require special handling. */
3150static bfd_reloc_status_type
1b786873 3151nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3152 asection *input_section,
1b786873 3153 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3154 bfd_vma symbol_value, bfd_vma addend)
3155{
3156 symbol_value = symbol_value + addend;
3157 addend = 0;
3158 symbol_value = (symbol_value >> 16) & 0xffff;
3159 return _bfd_final_link_relocate (howto, abfd, input_section,
3160 data, offset, symbol_value, addend);
3161}
3162
3163static bfd_reloc_status_type
3164nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3165 asection *input_section,
1b786873 3166 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3167 bfd_vma symbol_value, bfd_vma addend)
3168{
3169 symbol_value = symbol_value + addend;
3170 addend = 0;
3171 symbol_value = symbol_value & 0xffff;
3172 return _bfd_final_link_relocate (howto, abfd, input_section,
3173 data, offset, symbol_value, addend);
3174}
3175
3176static bfd_reloc_status_type
3177nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3178 asection *input_section,
36591ba1
SL
3179 bfd_byte *data, bfd_vma offset,
3180 bfd_vma symbol_value, bfd_vma addend)
3181{
3182 symbol_value = symbol_value + addend;
3183 addend = 0;
3184 symbol_value = hiadj(symbol_value);
3185 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3186 symbol_value, addend);
3187}
3188
3189static bfd_reloc_status_type
3190nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3191 asection *input_section,
36591ba1
SL
3192 bfd_byte *data, bfd_vma offset,
3193 bfd_vma symbol_value, bfd_vma addend)
3194{
3195 symbol_value = symbol_value + addend;
3196 addend = 0;
3197 symbol_value = symbol_value & 0xffff;
3198 return _bfd_final_link_relocate (howto, abfd, input_section,
3199 data, offset, symbol_value, addend);
3200}
3201
3202static bfd_reloc_status_type
3203nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3204 asection *input_section,
36591ba1
SL
3205 bfd_byte *data, bfd_vma offset,
3206 bfd_vma symbol_value, bfd_vma addend)
3207{
3208 symbol_value = symbol_value + addend;
3209 symbol_value -= (input_section->output_section->vma
3210 + input_section->output_offset);
3211 symbol_value -= offset;
3212 addend = 0;
3213 symbol_value = hiadj(symbol_value);
3214 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3215 symbol_value, addend);
3216}
3217
3218static bfd_reloc_status_type
3219nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3220 asection *input_section,
1b786873 3221 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3222 bfd_vma symbol_value, bfd_vma addend)
3223{
1b786873 3224 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
36591ba1
SL
3225 so we need to subtract 4 before doing a final_link_relocate. */
3226 symbol_value = symbol_value + addend - 4;
3227 addend = 0;
3228 return _bfd_final_link_relocate (howto, abfd, input_section,
3229 data, offset, symbol_value, addend);
3230}
3231
3232static bfd_reloc_status_type
3233nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3234 asection *input_section,
1b786873 3235 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3236 bfd_vma symbol_value, bfd_vma addend)
3237{
3238 /* Check that the relocation is in the same page as the current address. */
1b786873 3239 if (CALL26_SEGMENT (symbol_value + addend)
78058a5e
SL
3240 != CALL26_SEGMENT (input_section->output_section->vma
3241 + input_section->output_offset
3242 + offset))
36591ba1
SL
3243 return bfd_reloc_overflow;
3244
8c163c5a
SL
3245 /* Check that the target address is correctly aligned on a 4-byte
3246 boundary. */
3247 if ((symbol_value + addend) & 0x3)
3248 return bfd_reloc_overflow;
3249
36591ba1
SL
3250 return _bfd_final_link_relocate (howto, abfd, input_section,
3251 data, offset, symbol_value, addend);
3252}
3253
3254static bfd_reloc_status_type
3255nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3256 asection *input_section,
1b786873 3257 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3258 bfd_vma symbol_value, bfd_vma addend)
3259{
3260 /* Because we need the output_bfd, the special handling is done
3261 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3262 return _bfd_final_link_relocate (howto, abfd, input_section,
3263 data, offset, symbol_value, addend);
3264}
3265
3266static bfd_reloc_status_type
3267nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3268 asection *input_section,
1b786873 3269 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3270 bfd_vma symbol_value, bfd_vma addend)
3271{
3272 bfd_vma symbol_lo16, symbol_hi16;
3273 bfd_reloc_status_type r;
3274 symbol_value = symbol_value + addend;
3275 addend = 0;
3276 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3277 symbol_lo16 = symbol_value & 0xffff;
3278
3279 r = _bfd_final_link_relocate (howto, abfd, input_section,
3280 data, offset, symbol_hi16, addend);
3281
3282 if (r == bfd_reloc_ok)
3283 return _bfd_final_link_relocate (howto, abfd, input_section,
3284 data, offset + 4, symbol_lo16, addend);
3285
3286 return r;
3287}
3288
3289static bfd_reloc_status_type
3290nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3291 asection *input_section,
1b786873 3292 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3293 bfd_vma symbol_value, bfd_vma addend)
3294{
3295 bfd_vma symbol_lo16, symbol_hi16;
3296 bfd_reloc_status_type r;
3297 symbol_value = symbol_value + addend;
3298 addend = 0;
3299 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3300 symbol_lo16 = symbol_value & 0xffff;
3301
3302 r = _bfd_final_link_relocate (howto, abfd, input_section,
3303 data, offset, symbol_hi16, addend);
3304
3305 if (r == bfd_reloc_ok)
3306 return _bfd_final_link_relocate (howto, abfd, input_section,
3307 data, offset + 4, symbol_lo16, addend);
3308
3309 return r;
3310}
3311
3312static bfd_reloc_status_type
3313nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
25153ba0 3314 asection *input_section,
1b786873 3315 bfd_byte *data, bfd_vma offset,
36591ba1
SL
3316 bfd_vma symbol_value, bfd_vma addend)
3317{
3318 bfd_vma symbol_lo16, symbol_hi16;
3319 bfd_reloc_status_type r;
3320 symbol_value = symbol_value + addend;
3321 addend = 0;
3322 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3323 symbol_lo16 = symbol_value & 0xffff;
3324
3325 r = _bfd_final_link_relocate (howto, abfd, input_section,
3326 data, offset, symbol_hi16, addend);
3327
3328 if (r == bfd_reloc_ok)
3329 return _bfd_final_link_relocate (howto, abfd, input_section,
3330 data, offset + 4, symbol_lo16, addend);
3331
3332 return r;
3333}
3334
3335/* HOWTO handlers for relocations that require special handling. */
3336
3337/* This is for relocations used only when relaxing to ensure
3338 changes in size of section don't screw up .align. */
3339static bfd_reloc_status_type
3340nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1b786873
L
3341 asymbol *symbol ATTRIBUTE_UNUSED,
3342 void *data ATTRIBUTE_UNUSED, asection *input_section,
3343 bfd *output_bfd,
36591ba1
SL
3344 char **error_message ATTRIBUTE_UNUSED)
3345{
3346 if (output_bfd != NULL)
3347 reloc_entry->address += input_section->output_offset;
3348 return bfd_reloc_ok;
3349}
3350
3351static bfd_reloc_status_type
1b786873
L
3352nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3353 void *data, asection *input_section,
3354 bfd *output_bfd,
36591ba1
SL
3355 char **error_message ATTRIBUTE_UNUSED)
3356{
3357 /* This part is from bfd_elf_generic_reloc. */
3358 if (output_bfd != NULL
3359 && (symbol->flags & BSF_SECTION_SYM) == 0
3360 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3361 {
3362 reloc_entry->address += input_section->output_offset;
3363 return bfd_reloc_ok;
3364 }
3365
3366 if (output_bfd != NULL)
3367 /* FIXME: See bfd_perform_relocation. Is this right? */
3368 return bfd_reloc_continue;
3369
3370 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3371 input_section,
3372 data, reloc_entry->address,
3373 (symbol->value
3374 + symbol->section->output_section->vma
3375 + symbol->section->output_offset),
3376 reloc_entry->addend);
3377}
3378
3379static bfd_reloc_status_type
3380nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3381 void *data, asection *input_section,
3382 bfd *output_bfd,
36591ba1
SL
3383 char **error_message ATTRIBUTE_UNUSED)
3384{
3385 /* This part is from bfd_elf_generic_reloc. */
3386 if (output_bfd != NULL
3387 && (symbol->flags & BSF_SECTION_SYM) == 0
3388 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3389 {
3390 reloc_entry->address += input_section->output_offset;
3391 return bfd_reloc_ok;
3392 }
3393
3394 if (output_bfd != NULL)
3395 /* FIXME: See bfd_perform_relocation. Is this right? */
3396 return bfd_reloc_continue;
3397
3398 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3399 input_section,
3400 data, reloc_entry->address,
3401 (symbol->value
3402 + symbol->section->output_section->vma
3403 + symbol->section->output_offset),
3404 reloc_entry->addend);
3405}
3406
3407static bfd_reloc_status_type
3408nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3409 void *data, asection *input_section,
3410 bfd *output_bfd,
36591ba1
SL
3411 char **error_message ATTRIBUTE_UNUSED)
3412{
3413 /* This part is from bfd_elf_generic_reloc. */
3414 if (output_bfd != NULL
3415 && (symbol->flags & BSF_SECTION_SYM) == 0
3416 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3417 {
3418 reloc_entry->address += input_section->output_offset;
3419 return bfd_reloc_ok;
3420 }
3421
3422 if (output_bfd != NULL)
3423 /* FIXME: See bfd_perform_relocation. Is this right? */
3424 return bfd_reloc_continue;
3425
3426 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3427 input_section,
3428 data, reloc_entry->address,
3429 (symbol->value
3430 + symbol->section->output_section->vma
3431 + symbol->section->output_offset),
3432 reloc_entry->addend);
3433}
3434
3435static bfd_reloc_status_type
3436nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3437 asymbol *symbol, void *data,
3438 asection *input_section, bfd *output_bfd,
3439 char **error_message ATTRIBUTE_UNUSED)
3440{
3441 /* This part is from bfd_elf_generic_reloc. */
3442 if (output_bfd != NULL
3443 && (symbol->flags & BSF_SECTION_SYM) == 0
3444 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3445 {
3446 reloc_entry->address += input_section->output_offset;
3447 return bfd_reloc_ok;
3448 }
3449
3450 if (output_bfd != NULL)
3451 /* FIXME: See bfd_perform_relocation. Is this right? */
3452 return bfd_reloc_continue;
3453
3454 return nios2_elf32_do_pcrel_lo16_relocate (
3455 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3456 (symbol->value + symbol->section->output_section->vma
3457 + symbol->section->output_offset),
3458 reloc_entry->addend);
3459}
3460
3461static bfd_reloc_status_type
3462nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3463 asymbol *symbol, void *data,
3464 asection *input_section, bfd *output_bfd,
3465 char **error_message ATTRIBUTE_UNUSED)
3466{
3467 /* This part is from bfd_elf_generic_reloc. */
3468 if (output_bfd != NULL
3469 && (symbol->flags & BSF_SECTION_SYM) == 0
3470 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3471 {
3472 reloc_entry->address += input_section->output_offset;
3473 return bfd_reloc_ok;
3474 }
3475
3476 if (output_bfd != NULL)
3477 /* FIXME: See bfd_perform_relocation. Is this right? */
3478 return bfd_reloc_continue;
3479
3480 return nios2_elf32_do_pcrel_hiadj16_relocate (
3481 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3482 (symbol->value + symbol->section->output_section->vma
3483 + symbol->section->output_offset),
3484 reloc_entry->addend);
3485}
3486
3487static bfd_reloc_status_type
3488nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3489 void *data, asection *input_section,
3490 bfd *output_bfd,
36591ba1
SL
3491 char **error_message ATTRIBUTE_UNUSED)
3492{
3493 /* This part is from bfd_elf_generic_reloc. */
3494 if (output_bfd != NULL
3495 && (symbol->flags & BSF_SECTION_SYM) == 0
3496 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3497 {
3498 reloc_entry->address += input_section->output_offset;
3499 return bfd_reloc_ok;
3500 }
3501
3502 if (output_bfd != NULL)
3503 /* FIXME: See bfd_perform_relocation. Is this right? */
3504 return bfd_reloc_continue;
3505
3506 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3507 input_section,
3508 data, reloc_entry->address,
3509 (symbol->value
3510 + symbol->section->output_section->vma
3511 + symbol->section->output_offset),
3512 reloc_entry->addend);
3513}
3514
3515static bfd_reloc_status_type
3516nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873
L
3517 void *data, asection *input_section,
3518 bfd *output_bfd,
36591ba1
SL
3519 char **error_message ATTRIBUTE_UNUSED)
3520{
3521 /* This part is from bfd_elf_generic_reloc. */
3522 if (output_bfd != NULL
3523 && (symbol->flags & BSF_SECTION_SYM) == 0
3524 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3525 {
3526 reloc_entry->address += input_section->output_offset;
3527 return bfd_reloc_ok;
3528 }
3529
3530 if (output_bfd != NULL)
3531 /* FIXME: See bfd_perform_relocation. Is this right? */
3532 return bfd_reloc_continue;
3533
3534 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3535 input_section,
3536 data, reloc_entry->address,
3537 (symbol->value
3538 + symbol->section->output_section->vma
3539 + symbol->section->output_offset),
3540 reloc_entry->addend);
3541}
3542
3543static bfd_reloc_status_type
3544nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3545 void *data, asection *input_section,
36591ba1
SL
3546 bfd *output_bfd, char **msg)
3547{
3548 bfd_vma relocation;
3549 bfd_vma gp;
3550 bfd_reloc_status_type r;
3551
3552
3553 /* This part is from bfd_elf_generic_reloc. */
3554 if (output_bfd != NULL
3555 && (symbol->flags & BSF_SECTION_SYM) == 0
3556 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3557 {
3558 reloc_entry->address += input_section->output_offset;
3559 return bfd_reloc_ok;
3560 }
3561
3562 if (output_bfd != NULL)
3563 /* FIXME: See bfd_perform_relocation. Is this right? */
3564 return bfd_reloc_continue;
3565
3566 relocation = (symbol->value
3567 + symbol->section->output_section->vma
3568 + symbol->section->output_offset);
3569
3570 /* This assumes we've already cached the _gp symbol. */
3571 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
3572 if (r == bfd_reloc_ok)
3573 {
3574 relocation = relocation + reloc_entry->addend - gp;
3575 reloc_entry->addend = 0;
3576 if ((signed) relocation < -32768 || (signed) relocation > 32767)
3577 {
3578 *msg = _("global pointer relative address out of range");
3579 r = bfd_reloc_outofrange;
3580 }
3581 else
3582 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3583 input_section,
3584 data, reloc_entry->address,
3585 relocation, reloc_entry->addend);
3586 }
3587
3588 return r;
3589}
3590
3591static bfd_reloc_status_type
3592nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3593 void *data, asection *input_section,
36591ba1
SL
3594 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3595{
3596 /* This part is from bfd_elf_generic_reloc. */
3597 if (output_bfd != NULL
3598 && (symbol->flags & BSF_SECTION_SYM) == 0
3599 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3600 {
3601 reloc_entry->address += input_section->output_offset;
3602 return bfd_reloc_ok;
3603 }
3604
3605 if (output_bfd != NULL)
3606 /* FIXME: See bfd_perform_relocation. Is this right? */
3607 return bfd_reloc_continue;
3608
3609 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3610 input_section,
3611 data, reloc_entry->address,
3612 (symbol->value
3613 + symbol->section->output_section->vma
3614 + symbol->section->output_offset),
3615 reloc_entry->addend);
3616}
3617
3618static bfd_reloc_status_type
3619nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3620 void *data, asection *input_section,
36591ba1
SL
3621 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3622{
3623 /* This part is from bfd_elf_generic_reloc. */
3624 if (output_bfd != NULL
3625 && (symbol->flags & BSF_SECTION_SYM) == 0
3626 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3627 {
3628 reloc_entry->address += input_section->output_offset;
3629 return bfd_reloc_ok;
3630 }
3631
3632 if (output_bfd != NULL)
3633 /* FIXME: See bfd_perform_relocation. Is this right? */
3634 return bfd_reloc_continue;
3635
3636 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3637 input_section,
3638 data, reloc_entry->address,
3639 (symbol->value
3640 + symbol->section->output_section->vma
3641 + symbol->section->output_offset),
3642 reloc_entry->addend);
3643}
3644
3645static bfd_reloc_status_type
3646nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1b786873 3647 void *data, asection *input_section,
36591ba1
SL
3648 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3649{
3650 /* This part is from bfd_elf_generic_reloc. */
3651 if (output_bfd != NULL
3652 && (symbol->flags & BSF_SECTION_SYM) == 0
3653 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3654 {
3655 reloc_entry->address += input_section->output_offset;
3656 return bfd_reloc_ok;
3657 }
3658
3659 if (output_bfd != NULL)
3660 /* FIXME: See bfd_perform_relocation. Is this right? */
3661 return bfd_reloc_continue;
3662
3663 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3664 input_section,
3665 data, reloc_entry->address,
3666 (symbol->value
3667 + symbol->section->output_section->vma
3668 + symbol->section->output_offset),
3669 reloc_entry->addend);
3670}
3671
1b786873 3672
36591ba1
SL
3673/* Implement elf_backend_relocate_section. */
3674static bfd_boolean
3675nios2_elf32_relocate_section (bfd *output_bfd,
3676 struct bfd_link_info *info,
3677 bfd *input_bfd,
3678 asection *input_section,
3679 bfd_byte *contents,
3680 Elf_Internal_Rela *relocs,
3681 Elf_Internal_Sym *local_syms,
3682 asection **local_sections)
3683{
3684 Elf_Internal_Shdr *symtab_hdr;
3685 struct elf_link_hash_entry **sym_hashes;
3686 Elf_Internal_Rela *rel;
3687 Elf_Internal_Rela *relend;
3688 struct elf32_nios2_link_hash_table *htab;
3689 asection *sgot;
3690 asection *splt;
3691 asection *sreloc = NULL;
3692 bfd_vma *local_got_offsets;
82e91538 3693 bfd_vma got_base;
36591ba1
SL
3694
3695 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3696 sym_hashes = elf_sym_hashes (input_bfd);
3697 relend = relocs + input_section->reloc_count;
3698
3699 htab = elf32_nios2_hash_table (info);
3700 sgot = htab->root.sgot;
3701 splt = htab->root.splt;
3702 local_got_offsets = elf_local_got_offsets (input_bfd);
3703
4ef97a1b 3704 if (htab->h_gp_got == NULL)
82e91538
SL
3705 got_base = 0;
3706 else
4ef97a1b 3707 got_base = htab->h_gp_got->root.u.def.value;
82e91538 3708
36591ba1
SL
3709 for (rel = relocs; rel < relend; rel++)
3710 {
3711 reloc_howto_type *howto;
3712 unsigned long r_symndx;
3713 Elf_Internal_Sym *sym;
3714 asection *sec;
3715 struct elf_link_hash_entry *h;
3716 struct elf32_nios2_link_hash_entry *eh;
3717 bfd_vma relocation;
3718 bfd_vma gp;
36591ba1
SL
3719 bfd_reloc_status_type r = bfd_reloc_ok;
3720 const char *name = NULL;
3721 int r_type;
3722 const char *format;
a31b8bd9
AM
3723 char *msgbuf = NULL;
3724 char *msg = NULL;
36591ba1
SL
3725 bfd_boolean unresolved_reloc;
3726 bfd_vma off;
3727 int use_plt;
3728
3729 r_type = ELF32_R_TYPE (rel->r_info);
3730 r_symndx = ELF32_R_SYM (rel->r_info);
3731
8c163c5a 3732 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
36591ba1
SL
3733 h = NULL;
3734 sym = NULL;
3735 sec = NULL;
3736
3737 if (r_symndx < symtab_hdr->sh_info)
3738 {
3739 sym = local_syms + r_symndx;
3740 sec = local_sections[r_symndx];
3741 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3742 }
3743 else
3744 {
62d887d4 3745 bfd_boolean warned, ignored;
36591ba1
SL
3746
3747 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3748 r_symndx, symtab_hdr, sym_hashes,
3749 h, sec, relocation,
62d887d4 3750 unresolved_reloc, warned, ignored);
36591ba1
SL
3751 }
3752
3753 if (sec && discarded_section (sec))
3754 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3755 rel, 1, relend, howto, 0, contents);
3756
3757 /* Nothing more to do unless this is a final link. */
0e1862bb 3758 if (bfd_link_relocatable (info))
36591ba1
SL
3759 continue;
3760
36591ba1
SL
3761 if (howto)
3762 {
31a53da5
L
3763 bfd_boolean resolved_to_zero;
3764
3765 resolved_to_zero = (h != NULL
3766 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
36591ba1
SL
3767 switch (howto->type)
3768 {
3769 case R_NIOS2_HI16:
3770 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3771 input_section,
3772 contents, rel->r_offset,
3773 relocation, rel->r_addend);
3774 break;
3775 case R_NIOS2_LO16:
3776 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3777 input_section,
3778 contents, rel->r_offset,
3779 relocation, rel->r_addend);
3780 break;
3781 case R_NIOS2_PCREL_LO:
3782 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3783 input_section,
3784 contents,
3785 rel->r_offset,
3786 relocation,
3787 rel->r_addend);
3788 break;
3789 case R_NIOS2_HIADJ16:
3790 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3791 input_section, contents,
3792 rel->r_offset, relocation,
3793 rel->r_addend);
3794 break;
3795 case R_NIOS2_PCREL_HA:
3796 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3797 input_section,
3798 contents,
3799 rel->r_offset,
3800 relocation,
3801 rel->r_addend);
3802 break;
3803 case R_NIOS2_PCREL16:
3804 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3805 input_section, contents,
3806 rel->r_offset, relocation,
3807 rel->r_addend);
3808 break;
3809 case R_NIOS2_GPREL:
3810 /* Turns an absolute address into a gp-relative address. */
3811 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3812 {
a7be2893
SL
3813 bfd_vma reloc_address;
3814
3815 if (sec && sec->output_section)
3816 reloc_address = (sec->output_section->vma
3817 + sec->output_offset
3818 + rel->r_offset);
3819 else
3820 reloc_address = 0;
3821
36591ba1 3822 format = _("global pointer relative relocation at address "
a31b8bd9 3823 "%#" PRIx64 " when _gp not defined\n");
34d75fb5
AM
3824 if (asprintf (&msgbuf, format,
3825 (uint64_t) reloc_address) == -1)
3826 msgbuf = NULL;
36591ba1
SL
3827 msg = msgbuf;
3828 r = bfd_reloc_dangerous;
3829 }
3830 else
3831 {
3832 bfd_vma symbol_address = rel->r_addend + relocation;
a7be2893 3833 relocation = symbol_address - gp;
36591ba1
SL
3834 rel->r_addend = 0;
3835 if (((signed) relocation < -32768
3836 || (signed) relocation > 32767)
3837 && (!h
3838 || h->root.type == bfd_link_hash_defined
3839 || h->root.type == bfd_link_hash_defweak))
3840 {
a7be2893
SL
3841 if (h)
3842 name = h->root.root.string;
a31b8bd9
AM
3843 else
3844 {
3845 name = (bfd_elf_string_from_elf_section
3846 (input_bfd, symtab_hdr->sh_link,
3847 sym->st_name));
3848 if (name == NULL || *name == '\0')
3849 name = bfd_section_name (input_bfd, sec);
3850 }
695344c0 3851 /* xgettext:c-format */
a31b8bd9
AM
3852 format = _("unable to reach %s (at %#" PRIx64 ") from "
3853 "the global pointer (at %#" PRIx64 ") "
3854 "because the offset (%" PRId64 ") is out of "
3855 "the allowed range, -32678 to 32767\n" );
34d75fb5
AM
3856 if (asprintf (&msgbuf, format, name,
3857 (uint64_t) symbol_address, (uint64_t) gp,
3858 (int64_t) relocation) == -1)
3859 msgbuf = NULL;
36591ba1
SL
3860 msg = msgbuf;
3861 r = bfd_reloc_outofrange;
3862 }
3863 else
3864 r = _bfd_final_link_relocate (howto, input_bfd,
3865 input_section, contents,
3866 rel->r_offset, relocation,
3867 rel->r_addend);
3868 }
36591ba1
SL
3869 break;
3870 case R_NIOS2_UJMP:
3871 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3872 input_section,
3873 contents, rel->r_offset,
3874 relocation, rel->r_addend);
3875 break;
3876 case R_NIOS2_CJMP:
3877 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3878 input_section,
3879 contents, rel->r_offset,
3880 relocation, rel->r_addend);
3881 break;
3882 case R_NIOS2_CALLR:
3883 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3884 input_section, contents,
3885 rel->r_offset, relocation,
3886 rel->r_addend);
3887 break;
3888 case R_NIOS2_CALL26:
78058a5e 3889 case R_NIOS2_CALL26_NOAT:
36591ba1
SL
3890 /* If we have a call to an undefined weak symbol, we just want
3891 to stuff a zero in the bits of the call instruction and
3892 bypass the normal call26 relocation handling, because it'll
3893 diagnose an overflow error if address 0 isn't in the same
3894 256MB segment as the call site. Presumably the call
3895 should be guarded by a null check anyway. */
3896 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3897 {
3898 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3899 r = _bfd_final_link_relocate (howto, input_bfd,
3900 input_section, contents,
3901 rel->r_offset, relocation,
3902 rel->r_addend);
3903 break;
3904 }
3905 /* Handle relocations which should use the PLT entry.
3906 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3907 which may point to a PLT entry, but we don't need to handle
3908 that here. If we created a PLT entry, all branches in this
3909 object should go to it. */
3910 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3911 {
3912 /* If we've created a .plt section, and assigned a PLT entry
3913 to this function, it should not be known to bind locally.
3914 If it were, we would have cleared the PLT entry. */
3915 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3916
3917 relocation = (splt->output_section->vma
3918 + splt->output_offset
3919 + h->plt.offset);
3920
3921 unresolved_reloc = FALSE;
3922 }
78058a5e
SL
3923 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3924 256MB segment. Replace the target with a reference to a
3925 trampoline instead.
3926 Note that htab->stub_group is null if relaxation has been
3927 disabled by the --no-relax linker command-line option, so
3928 we can use that to skip this processing entirely. */
3929 if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3930 {
3931 bfd_vma dest = relocation + rel->r_addend;
3932 enum elf32_nios2_stub_type stub_type;
3933
3934 eh = (struct elf32_nios2_link_hash_entry *)h;
3935 stub_type = nios2_type_of_stub (input_section, rel, eh,
3936 htab, dest, NULL);
3937
3938 if (stub_type != nios2_stub_none)
3939 {
3940 struct elf32_nios2_stub_hash_entry *hsh;
3941
3942 hsh = nios2_get_stub_entry (input_section, sec,
3943 eh, rel, htab, stub_type);
3944 if (hsh == NULL)
3945 {
3946 r = bfd_reloc_undefined;
3947 break;
3948 }
3949
3950 dest = (hsh->stub_offset
3951 + hsh->stub_sec->output_offset
3952 + hsh->stub_sec->output_section->vma);
3953 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3954 input_section,
3955 contents,
3956 rel->r_offset,
3957 dest, 0);
3958 break;
3959 }
3960 }
3961
3962 /* Normal case. */
36591ba1
SL
3963 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3964 input_section, contents,
3965 rel->r_offset, relocation,
3966 rel->r_addend);
3967 break;
3968 case R_NIOS2_ALIGN:
3969 r = bfd_reloc_ok;
3970 /* For symmetry this would be
3971 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3972 input_section, contents,
3973 rel->r_offset, relocation,
3974 rel->r_addend);
3975 but do_ignore_reloc would do no more than return
3976 bfd_reloc_ok. */
3977 break;
3978
3979 case R_NIOS2_GOT16:
3980 case R_NIOS2_CALL16:
1c2de463
SL
3981 case R_NIOS2_GOT_LO:
3982 case R_NIOS2_GOT_HA:
3983 case R_NIOS2_CALL_LO:
3984 case R_NIOS2_CALL_HA:
36591ba1
SL
3985 /* Relocation is to the entry for this symbol in the
3986 global offset table. */
3987 if (sgot == NULL)
3988 {
3989 r = bfd_reloc_notsupported;
3990 break;
3991 }
3992
3993 use_plt = 0;
3994
3995 if (h != NULL)
3996 {
3997 bfd_boolean dyn;
3998
3999 eh = (struct elf32_nios2_link_hash_entry *)h;
1c2de463 4000 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
4001 && h->plt.offset != (bfd_vma) -1);
4002
4003 off = h->got.offset;
4004 BFD_ASSERT (off != (bfd_vma) -1);
4ef97a1b 4005 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
4006 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4007 bfd_link_pic (info),
4008 h)
4009 || (bfd_link_pic (info)
36591ba1 4010 && SYMBOL_REFERENCES_LOCAL (info, h))
31a53da5
L
4011 || ((ELF_ST_VISIBILITY (h->other)
4012 || resolved_to_zero)
36591ba1
SL
4013 && h->root.type == bfd_link_hash_undefweak))
4014 {
4015 /* This is actually a static link, or it is a -Bsymbolic
4016 link and the symbol is defined locally. We must
4017 initialize this entry in the global offset table.
4018 Since the offset must always be a multiple of 4, we
4019 use the least significant bit to record whether we
4020 have initialized it already.
4021
4022 When doing a dynamic link, we create a .rela.got
4023 relocation entry to initialize the value. This is
4024 done in the finish_dynamic_symbol routine. */
4025 if ((off & 1) != 0)
4026 off &= ~1;
4027 else
4028 {
4029 bfd_put_32 (output_bfd, relocation,
4030 sgot->contents + off);
4031 h->got.offset |= 1;
4032 }
4033 }
4034 else
4035 unresolved_reloc = FALSE;
4036 }
4037 else
4038 {
4039 BFD_ASSERT (local_got_offsets != NULL
4040 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4041
4042 off = local_got_offsets[r_symndx];
4043
4044 /* The offset must always be a multiple of 4. We use the
4045 least significant bit to record whether we have already
4046 generated the necessary reloc. */
4047 if ((off & 1) != 0)
4048 off &= ~1;
4049 else
4050 {
4051 bfd_put_32 (output_bfd, relocation,
4052 sgot->contents + off);
4053
0e1862bb 4054 if (bfd_link_pic (info))
36591ba1
SL
4055 {
4056 asection *srelgot;
4057 Elf_Internal_Rela outrel;
4058 bfd_byte *loc;
4059
4060 srelgot = htab->root.srelgot;
4061 BFD_ASSERT (srelgot != NULL);
4062
4063 outrel.r_addend = relocation;
4064 outrel.r_offset = (sgot->output_section->vma
4065 + sgot->output_offset
4066 + off);
4067 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4068 loc = srelgot->contents;
4069 loc += (srelgot->reloc_count++ *
4070 sizeof (Elf32_External_Rela));
4071 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4072 }
4073
4074 local_got_offsets[r_symndx] |= 1;
4075 }
4076 }
4077
0e1862bb 4078 if (use_plt && bfd_link_pic (info))
36591ba1
SL
4079 {
4080 off = ((h->plt.offset - 24) / 12 + 3) * 4;
82e91538
SL
4081 relocation = (htab->root.sgotplt->output_offset + off
4082 - got_base);
36591ba1
SL
4083 }
4084 else
82e91538 4085 relocation = sgot->output_offset + off - got_base;
36591ba1
SL
4086
4087 /* This relocation does not use the addend. */
4088 rel->r_addend = 0;
4089
1c2de463
SL
4090 switch (howto->type)
4091 {
4092 case R_NIOS2_GOT_LO:
4093 case R_NIOS2_CALL_LO:
4094 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4095 input_section, contents,
4096 rel->r_offset, relocation,
4097 rel->r_addend);
4098 break;
4099 case R_NIOS2_GOT_HA:
4100 case R_NIOS2_CALL_HA:
4101 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4102 input_section, contents,
4103 rel->r_offset,
4104 relocation,
4105 rel->r_addend);
4106 break;
4107 default:
4108 r = _bfd_final_link_relocate (howto, input_bfd,
4109 input_section, contents,
4110 rel->r_offset, relocation,
4111 rel->r_addend);
4112 break;
4113 }
36591ba1
SL
4114 break;
4115
4116 case R_NIOS2_GOTOFF_LO:
4117 case R_NIOS2_GOTOFF_HA:
4118 case R_NIOS2_GOTOFF:
82e91538 4119 /* Relocation is relative to the global offset table pointer. */
36591ba1
SL
4120
4121 BFD_ASSERT (sgot != NULL);
4122 if (sgot == NULL)
4123 {
4124 r = bfd_reloc_notsupported;
4125 break;
4126 }
4127
d9972968
CLT
4128 /* Note that sgot->output_offset is not involved in this
4129 calculation. We always want the start of .got. */
4130 relocation -= sgot->output_section->vma;
4131
4132 /* Now we adjust the relocation to be relative to the GOT pointer
4133 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4134 relocation -= got_base;
82e91538 4135
36591ba1
SL
4136 switch (howto->type)
4137 {
4138 case R_NIOS2_GOTOFF_LO:
4139 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4140 input_section, contents,
4141 rel->r_offset, relocation,
4142 rel->r_addend);
4143 break;
4144 case R_NIOS2_GOTOFF_HA:
4145 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4146 input_section, contents,
4147 rel->r_offset,
4148 relocation,
4149 rel->r_addend);
4150 break;
4151 default:
4152 r = _bfd_final_link_relocate (howto, input_bfd,
4153 input_section, contents,
4154 rel->r_offset, relocation,
4155 rel->r_addend);
4156 break;
4157 }
4158 break;
4159
4160 case R_NIOS2_TLS_LDO16:
4161 relocation -= dtpoff_base (info) + DTP_OFFSET;
4162
4163 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4164 contents, rel->r_offset,
4165 relocation, rel->r_addend);
4166 break;
4167 case R_NIOS2_TLS_LDM16:
4168 if (htab->root.sgot == NULL)
4169 abort ();
4170
4171 off = htab->tls_ldm_got.offset;
4172
4173 if ((off & 1) != 0)
4174 off &= ~1;
4175 else
4176 {
4177 /* If we don't know the module number, create a relocation
4178 for it. */
0e1862bb 4179 if (bfd_link_pic (info))
36591ba1
SL
4180 {
4181 Elf_Internal_Rela outrel;
4182 bfd_byte *loc;
4183
4184 if (htab->root.srelgot == NULL)
4185 abort ();
4186
4187 outrel.r_addend = 0;
4188 outrel.r_offset = (htab->root.sgot->output_section->vma
4189 + htab->root.sgot->output_offset
4190 + off);
4191 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4192
4193 loc = htab->root.srelgot->contents;
4194 loc += (htab->root.srelgot->reloc_count++
4195 * sizeof (Elf32_External_Rela));
4196 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4197 }
4198 else
4199 bfd_put_32 (output_bfd, 1,
4200 htab->root.sgot->contents + off);
4201
4202 htab->tls_ldm_got.offset |= 1;
4203 }
4204
82e91538 4205 relocation = htab->root.sgot->output_offset + off - got_base;
36591ba1
SL
4206
4207 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4208 contents, rel->r_offset,
4209 relocation, rel->r_addend);
4210
4211 break;
4212 case R_NIOS2_TLS_GD16:
4213 case R_NIOS2_TLS_IE16:
4214 {
4215 int indx;
4216 char tls_type;
4217
4218 if (htab->root.sgot == NULL)
4219 abort ();
4220
4221 indx = 0;
4222 if (h != NULL)
4223 {
4224 bfd_boolean dyn;
4225 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
4226 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4227 bfd_link_pic (info),
4228 h)
4229 && (!bfd_link_pic (info)
36591ba1
SL
4230 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4231 {
4232 unresolved_reloc = FALSE;
4233 indx = h->dynindx;
4234 }
4235 off = h->got.offset;
4236 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4237 ->tls_type);
4238 }
4239 else
4240 {
4241 if (local_got_offsets == NULL)
4242 abort ();
4243 off = local_got_offsets[r_symndx];
4244 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4245 [r_symndx]);
4246 }
4247
4248 if (tls_type == GOT_UNKNOWN)
4249 abort ();
4250
4251 if ((off & 1) != 0)
4252 off &= ~1;
4253 else
4254 {
4255 bfd_boolean need_relocs = FALSE;
4256 Elf_Internal_Rela outrel;
4257 bfd_byte *loc = NULL;
4258 int cur_off = off;
4259
4260 /* The GOT entries have not been initialized yet. Do it
4261 now, and emit any relocations. If both an IE GOT and a
4262 GD GOT are necessary, we emit the GD first. */
4263
0e1862bb 4264 if ((bfd_link_pic (info) || indx != 0)
36591ba1 4265 && (h == NULL
31a53da5
L
4266 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4267 && !resolved_to_zero)
36591ba1
SL
4268 || h->root.type != bfd_link_hash_undefweak))
4269 {
4270 need_relocs = TRUE;
4271 if (htab->root.srelgot == NULL)
4272 abort ();
4273 loc = htab->root.srelgot->contents;
4274 loc += (htab->root.srelgot->reloc_count *
4275 sizeof (Elf32_External_Rela));
4276 }
4277
4278 if (tls_type & GOT_TLS_GD)
4279 {
4280 if (need_relocs)
4281 {
4282 outrel.r_addend = 0;
4283 outrel.r_offset = (htab->root.sgot->output_section->vma
4284 + htab->root.sgot->output_offset
4285 + cur_off);
4286 outrel.r_info = ELF32_R_INFO (indx,
4287 R_NIOS2_TLS_DTPMOD);
4288
4289 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4290 loc);
4291 htab->root.srelgot->reloc_count++;
4292 loc += sizeof (Elf32_External_Rela);
4293
4294 if (indx == 0)
4295 bfd_put_32 (output_bfd,
4296 (relocation - dtpoff_base (info) -
4297 DTP_OFFSET),
4298 htab->root.sgot->contents + cur_off + 4);
4299 else
4300 {
4301 outrel.r_addend = 0;
4302 outrel.r_info = ELF32_R_INFO (indx,
4303 R_NIOS2_TLS_DTPREL);
4304 outrel.r_offset += 4;
4305
4306 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4307 loc);
4308 htab->root.srelgot->reloc_count++;
4309 loc += sizeof (Elf32_External_Rela);
4310 }
4311 }
4312 else
4313 {
4314 /* If we are not emitting relocations for a
4315 general dynamic reference, then we must be in a
4316 static link or an executable link with the
4317 symbol binding locally. Mark it as belonging
4318 to module 1, the executable. */
4319 bfd_put_32 (output_bfd, 1,
4320 htab->root.sgot->contents + cur_off);
4321 bfd_put_32 (output_bfd, (relocation -
4322 dtpoff_base (info) -
4323 DTP_OFFSET),
4324 htab->root.sgot->contents + cur_off + 4);
4325 }
4326
4327 cur_off += 8;
4328 }
4329
4330 if (tls_type & GOT_TLS_IE)
4331 {
4332 if (need_relocs)
4333 {
4334 if (indx == 0)
4335 outrel.r_addend = (relocation -
4336 dtpoff_base (info));
4337 else
4338 outrel.r_addend = 0;
4339 outrel.r_offset = (htab->root.sgot->output_section->vma
4340 + htab->root.sgot->output_offset
4341 + cur_off);
4342 outrel.r_info = ELF32_R_INFO (indx,
4343 R_NIOS2_TLS_TPREL);
4344
4345 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4346 loc);
4347 htab->root.srelgot->reloc_count++;
4348 loc += sizeof (Elf32_External_Rela);
4349 }
4350 else
4351 bfd_put_32 (output_bfd, (tpoff (info, relocation)
4352 - TP_OFFSET),
4353 htab->root.sgot->contents + cur_off);
4354 cur_off += 4;
4355 }
4356
4357 if (h != NULL)
4358 h->got.offset |= 1;
4359 else
4360 local_got_offsets[r_symndx] |= 1;
4361 }
4362
4363 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4364 off += 8;
82e91538 4365 relocation = htab->root.sgot->output_offset + off - got_base;
36591ba1
SL
4366
4367 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4368 contents, rel->r_offset,
4369 relocation, rel->r_addend);
4370 }
4371
4372 break;
4373 case R_NIOS2_TLS_LE16:
3cbc1e5e 4374 if (bfd_link_dll (info))
36591ba1 4375 {
4eca0228 4376 _bfd_error_handler
695344c0 4377 /* xgettext:c-format */
2dcf00ce 4378 (_("%pB(%pA+%#" PRIx64 "): %s relocation not "
36591ba1
SL
4379 "permitted in shared object"),
4380 input_bfd, input_section,
2dcf00ce 4381 (uint64_t) rel->r_offset, howto->name);
36591ba1
SL
4382 return FALSE;
4383 }
4384 else
4385 relocation = tpoff (info, relocation) - TP_OFFSET;
4386
4387 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4388 contents, rel->r_offset,
4389 relocation, rel->r_addend);
4390 break;
4391
4392 case R_NIOS2_BFD_RELOC_32:
0e1862bb 4393 if (bfd_link_pic (info)
36591ba1
SL
4394 && (input_section->flags & SEC_ALLOC) != 0
4395 && (h == NULL
31a53da5
L
4396 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4397 && !resolved_to_zero)
36591ba1
SL
4398 || h->root.type != bfd_link_hash_undefweak))
4399 {
4400 Elf_Internal_Rela outrel;
4401 bfd_byte *loc;
4402 bfd_boolean skip, relocate;
4403
4404 /* When generating a shared object, these relocations
4405 are copied into the output file to be resolved at run
4406 time. */
4407
4408 skip = FALSE;
4409 relocate = FALSE;
4410
4411 outrel.r_offset
4412 = _bfd_elf_section_offset (output_bfd, info,
4413 input_section, rel->r_offset);
4414 if (outrel.r_offset == (bfd_vma) -1)
4415 skip = TRUE;
4416 else if (outrel.r_offset == (bfd_vma) -2)
4417 skip = TRUE, relocate = TRUE;
4418 outrel.r_offset += (input_section->output_section->vma
4419 + input_section->output_offset);
4420
4421 if (skip)
4422 memset (&outrel, 0, sizeof outrel);
4423 else if (h != NULL
4424 && h->dynindx != -1
0e1862bb 4425 && (!bfd_link_pic (info)
a496fbc8 4426 || !SYMBOLIC_BIND (info, h)
36591ba1
SL
4427 || !h->def_regular))
4428 {
4429 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4430 outrel.r_addend = rel->r_addend;
4431 }
4432 else
4433 {
4434 /* This symbol is local, or marked to become local. */
4435 outrel.r_addend = relocation + rel->r_addend;
4436 relocate = TRUE;
4437 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4438 }
4439
4440 sreloc = elf_section_data (input_section)->sreloc;
4441 if (sreloc == NULL)
4442 abort ();
4443
4444 loc = sreloc->contents;
4445 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4446 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4447
4448 /* This reloc will be computed at runtime, so there's no
4449 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4450 relocations that have been turned into
4451 R_NIOS2_RELATIVE. */
4452 if (!relocate)
4453 break;
4454 }
4455
4456 r = _bfd_final_link_relocate (howto, input_bfd,
4457 input_section, contents,
4458 rel->r_offset, relocation,
4459 rel->r_addend);
4460 break;
4461
4462 case R_NIOS2_TLS_DTPREL:
4463 relocation -= dtpoff_base (info);
4464 /* Fall through. */
4465
4466 default:
4467 r = _bfd_final_link_relocate (howto, input_bfd,
4468 input_section, contents,
4469 rel->r_offset, relocation,
4470 rel->r_addend);
4471 break;
4472 }
4473 }
4474 else
4475 r = bfd_reloc_notsupported;
4476
4477 if (r != bfd_reloc_ok)
4478 {
4479 if (h != NULL)
4480 name = h->root.root.string;
4481 else
4482 {
4483 name = bfd_elf_string_from_elf_section (input_bfd,
4484 symtab_hdr->sh_link,
4485 sym->st_name);
4486 if (name == NULL || *name == '\0')
4487 name = bfd_section_name (input_bfd, sec);
4488 }
4489
4490 switch (r)
4491 {
4492 case bfd_reloc_overflow:
1a72702b
AM
4493 (*info->callbacks->reloc_overflow) (info, NULL, name,
4494 howto->name, (bfd_vma) 0,
4495 input_bfd, input_section,
4496 rel->r_offset);
36591ba1
SL
4497 break;
4498
4499 case bfd_reloc_undefined:
1a72702b
AM
4500 (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4501 input_section,
4502 rel->r_offset, TRUE);
36591ba1
SL
4503 break;
4504
4505 case bfd_reloc_outofrange:
4506 if (msg == NULL)
4507 msg = _("relocation out of range");
4508 break;
4509
4510 case bfd_reloc_notsupported:
4511 if (msg == NULL)
4512 msg = _("unsupported relocation");
4513 break;
4514
4515 case bfd_reloc_dangerous:
4516 if (msg == NULL)
4517 msg = _("dangerous relocation");
4518 break;
4519
4520 default:
4521 if (msg == NULL)
4522 msg = _("unknown error");
4523 break;
4524 }
4525
4526 if (msg)
4527 {
1a72702b
AM
4528 (*info->callbacks->warning) (info, msg, name, input_bfd,
4529 input_section, rel->r_offset);
a31b8bd9
AM
4530 if (msgbuf)
4531 free (msgbuf);
36591ba1
SL
4532 return FALSE;
4533 }
4534 }
4535 }
4536 return TRUE;
4537}
4538
4539/* Implement elf-backend_section_flags:
4540 Convert NIOS2 specific section flags to bfd internal section flags. */
4541static bfd_boolean
4542nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
4543{
4544 if (hdr->sh_flags & SHF_NIOS2_GPREL)
4545 *flags |= SEC_SMALL_DATA;
4546
4547 return TRUE;
4548}
4549
4550/* Implement elf_backend_fake_sections:
4551 Set the correct type for an NIOS2 ELF section. We do this by the
4552 section name, which is a hack, but ought to work. */
4553static bfd_boolean
4554nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4555 Elf_Internal_Shdr *hdr, asection *sec)
4556{
4557 register const char *name = bfd_get_section_name (abfd, sec);
4558
4559 if ((sec->flags & SEC_SMALL_DATA)
4560 || strcmp (name, ".sdata") == 0
4561 || strcmp (name, ".sbss") == 0
4562 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4563 hdr->sh_flags |= SHF_NIOS2_GPREL;
4564
4565 return TRUE;
4566}
4567
4568/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4569 shortcuts to them in our hash table. */
4570static bfd_boolean
4571create_got_section (bfd *dynobj, struct bfd_link_info *info)
4572{
4573 struct elf32_nios2_link_hash_table *htab;
82e91538 4574 struct elf_link_hash_entry *h;
36591ba1
SL
4575
4576 htab = elf32_nios2_hash_table (info);
4577
4578 if (! _bfd_elf_create_got_section (dynobj, info))
4579 return FALSE;
4580
4581 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4582 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
4583 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
4584 return FALSE;
4585
82e91538
SL
4586 /* The Nios II ABI specifies that GOT-relative relocations are relative
4587 to the linker-created symbol _gp_got, rather than using
4588 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4589 points to the base of the GOT while _gp_got may include a bias. */
4590 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4591 "_gp_got");
4ef97a1b 4592 htab->h_gp_got = h;
82e91538
SL
4593 if (h == NULL)
4594 return FALSE;
4595
36591ba1
SL
4596 return TRUE;
4597}
4598
4599/* Implement elf_backend_create_dynamic_sections:
4600 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4601 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4602 hash table. */
4603static bfd_boolean
4604nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4605{
4606 struct elf32_nios2_link_hash_table *htab;
4607
4608 htab = elf32_nios2_hash_table (info);
4609 if (!htab->root.sgot && !create_got_section (dynobj, info))
4610 return FALSE;
4611
9d19e4fd
AM
4612 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4613 return FALSE;
36591ba1
SL
4614
4615 /* In order for the two loads in a shared object .PLTresolve to share the
4616 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4617 to a 16-byte boundary. This is because the addresses for these loads
4618 include the -(.plt+4) PIC correction. */
9d19e4fd 4619 return bfd_set_section_alignment (dynobj, htab->root.splt, 4);
36591ba1
SL
4620}
4621
4622/* Implement elf_backend_copy_indirect_symbol:
4623 Copy the extra info we tack onto an elf_link_hash_entry. */
4624static void
4625nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4626 struct elf_link_hash_entry *dir,
4627 struct elf_link_hash_entry *ind)
4628{
4629 struct elf32_nios2_link_hash_entry *edir, *eind;
4630
4631 edir = (struct elf32_nios2_link_hash_entry *) dir;
4632 eind = (struct elf32_nios2_link_hash_entry *) ind;
4633
4634 if (eind->dyn_relocs != NULL)
4635 {
4636 if (edir->dyn_relocs != NULL)
4637 {
3bf083ed
AM
4638 struct elf_dyn_relocs **pp;
4639 struct elf_dyn_relocs *p;
36591ba1
SL
4640
4641 /* Add reloc counts against the indirect sym to the direct sym
4642 list. Merge any entries against the same section. */
4643 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4644 {
3bf083ed 4645 struct elf_dyn_relocs *q;
36591ba1
SL
4646
4647 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4648 if (q->sec == p->sec)
4649 {
4650 q->pc_count += p->pc_count;
4651 q->count += p->count;
4652 *pp = p->next;
4653 break;
4654 }
4655 if (q == NULL)
4656 pp = &p->next;
4657 }
4658 *pp = edir->dyn_relocs;
4659 }
4660
4661 edir->dyn_relocs = eind->dyn_relocs;
4662 eind->dyn_relocs = NULL;
4663 }
4664
4665 if (ind->root.type == bfd_link_hash_indirect
4666 && dir->got.refcount <= 0)
4667 {
4668 edir->tls_type = eind->tls_type;
4669 eind->tls_type = GOT_UNKNOWN;
4670 }
4671
4672 edir->got_types_used |= eind->got_types_used;
4673
4674 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4675}
4676
965b1d80
SL
4677/* Set the right machine number for a NIOS2 ELF file. */
4678
4679static bfd_boolean
4680nios2_elf32_object_p (bfd *abfd)
4681{
4682 unsigned long mach;
4683
4684 mach = elf_elfheader (abfd)->e_flags;
4685
4686 switch (mach)
4687 {
4688 default:
4689 case EF_NIOS2_ARCH_R1:
4690 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4691 break;
4692 case EF_NIOS2_ARCH_R2:
4693 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4694 break;
4695 }
4696
4697 return TRUE;
4698}
4699
36591ba1
SL
4700/* Implement elf_backend_check_relocs:
4701 Look through the relocs for a section during the first phase. */
4702static bfd_boolean
4703nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4704 asection *sec, const Elf_Internal_Rela *relocs)
4705{
36591ba1
SL
4706 Elf_Internal_Shdr *symtab_hdr;
4707 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4708 const Elf_Internal_Rela *rel;
4709 const Elf_Internal_Rela *rel_end;
4710 struct elf32_nios2_link_hash_table *htab;
36591ba1
SL
4711 asection *sreloc = NULL;
4712 bfd_signed_vma *local_got_refcounts;
4713
0e1862bb 4714 if (bfd_link_relocatable (info))
36591ba1
SL
4715 return TRUE;
4716
36591ba1
SL
4717 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4718 sym_hashes = elf_sym_hashes (abfd);
4719 sym_hashes_end = (sym_hashes
4720 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
4721 if (!elf_bad_symtab (abfd))
4722 sym_hashes_end -= symtab_hdr->sh_info;
4723 local_got_refcounts = elf_local_got_refcounts (abfd);
4724
4725 htab = elf32_nios2_hash_table (info);
36591ba1
SL
4726
4727 rel_end = relocs + sec->reloc_count;
4728 for (rel = relocs; rel < rel_end; rel++)
4729 {
4730 unsigned int r_type;
4731 struct elf_link_hash_entry *h;
4732 unsigned long r_symndx;
4733
4734 r_symndx = ELF32_R_SYM (rel->r_info);
4735 if (r_symndx < symtab_hdr->sh_info)
4736 h = NULL;
4737 else
4738 {
4739 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4740 while (h->root.type == bfd_link_hash_indirect
4741 || h->root.type == bfd_link_hash_warning)
4742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4743 }
4744
4745 r_type = ELF32_R_TYPE (rel->r_info);
4746
4747 switch (r_type)
4748 {
4749 case R_NIOS2_GOT16:
1c2de463
SL
4750 case R_NIOS2_GOT_LO:
4751 case R_NIOS2_GOT_HA:
36591ba1 4752 case R_NIOS2_CALL16:
1c2de463
SL
4753 case R_NIOS2_CALL_LO:
4754 case R_NIOS2_CALL_HA:
36591ba1
SL
4755 case R_NIOS2_TLS_GD16:
4756 case R_NIOS2_TLS_IE16:
4757 /* This symbol requires a global offset table entry. */
4758 {
4759 int tls_type, old_tls_type;
4760
4761 switch (r_type)
4762 {
4763 default:
4764 case R_NIOS2_GOT16:
1c2de463
SL
4765 case R_NIOS2_GOT_LO:
4766 case R_NIOS2_GOT_HA:
36591ba1 4767 case R_NIOS2_CALL16:
1c2de463
SL
4768 case R_NIOS2_CALL_LO:
4769 case R_NIOS2_CALL_HA:
36591ba1
SL
4770 tls_type = GOT_NORMAL;
4771 break;
4772 case R_NIOS2_TLS_GD16:
4773 tls_type = GOT_TLS_GD;
4774 break;
4775 case R_NIOS2_TLS_IE16:
4776 tls_type = GOT_TLS_IE;
4777 break;
4778 }
4779
36591ba1
SL
4780 if (h != NULL)
4781 {
4782 struct elf32_nios2_link_hash_entry *eh
4783 = (struct elf32_nios2_link_hash_entry *)h;
4784 h->got.refcount++;
4785 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
1c2de463
SL
4786 if (r_type == R_NIOS2_CALL16
4787 || r_type == R_NIOS2_CALL_LO
4788 || r_type == R_NIOS2_CALL_HA)
36591ba1
SL
4789 {
4790 /* Make sure a plt entry is created for this symbol if
4791 it turns out to be a function defined by a dynamic
4792 object. */
4793 h->plt.refcount++;
4794 h->needs_plt = 1;
4795 h->type = STT_FUNC;
1c2de463 4796 eh->got_types_used |= CALL_USED;
36591ba1
SL
4797 }
4798 else
1c2de463 4799 eh->got_types_used |= GOT_USED;
36591ba1
SL
4800 }
4801 else
4802 {
4803 /* This is a global offset table entry for a local symbol. */
4804 if (local_got_refcounts == NULL)
4805 {
4806 bfd_size_type size;
4807
4808 size = symtab_hdr->sh_info;
4809 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4810 local_got_refcounts
4811 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4812 if (local_got_refcounts == NULL)
4813 return FALSE;
4814 elf_local_got_refcounts (abfd) = local_got_refcounts;
4815 elf32_nios2_local_got_tls_type (abfd)
4816 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4817 }
4818 local_got_refcounts[r_symndx]++;
4819 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4820 }
4821
4822 /* We will already have issued an error message if there is a
4823 TLS / non-TLS mismatch, based on the symbol type. We don't
4824 support any linker relaxations. So just combine any TLS
4825 types needed. */
4826 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4827 && tls_type != GOT_NORMAL)
4828 tls_type |= old_tls_type;
4829
4830 if (old_tls_type != tls_type)
4831 {
4832 if (h != NULL)
4833 elf32_nios2_hash_entry (h)->tls_type = tls_type;
4834 else
4835 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4836 }
4837 }
4ef97a1b 4838 make_got:
36591ba1
SL
4839 if (htab->root.sgot == NULL)
4840 {
4841 if (htab->root.dynobj == NULL)
4842 htab->root.dynobj = abfd;
4843 if (!create_got_section (htab->root.dynobj, info))
4844 return FALSE;
4845 }
4846 break;
4847
4ef97a1b
AM
4848 case R_NIOS2_TLS_LDM16:
4849 htab->tls_ldm_got.refcount++;
4850 goto make_got;
4851
36591ba1
SL
4852 /* This relocation describes the C++ object vtable hierarchy.
4853 Reconstruct it for later use during GC. */
4854 case R_NIOS2_GNU_VTINHERIT:
4855 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4856 return FALSE;
4857 break;
4858
4859 /* This relocation describes which C++ vtable entries are actually
4860 used. Record for later use during GC. */
4861 case R_NIOS2_GNU_VTENTRY:
4862 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4863 return FALSE;
4864 break;
4865
4866 case R_NIOS2_BFD_RELOC_32:
4867 case R_NIOS2_CALL26:
78058a5e 4868 case R_NIOS2_CALL26_NOAT:
36591ba1
SL
4869 case R_NIOS2_HIADJ16:
4870 case R_NIOS2_LO16:
4871
4872 if (h != NULL)
4873 {
4874 /* If this reloc is in a read-only section, we might
4875 need a copy reloc. We can't check reliably at this
4876 stage whether the section is read-only, as input
4877 sections have not yet been mapped to output sections.
4878 Tentatively set the flag for now, and correct in
4879 adjust_dynamic_symbol. */
0e1862bb 4880 if (!bfd_link_pic (info))
36591ba1
SL
4881 h->non_got_ref = 1;
4882
4883 /* Make sure a plt entry is created for this symbol if it
4884 turns out to be a function defined by a dynamic object. */
4885 h->plt.refcount++;
4886
78058a5e 4887 if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
36591ba1
SL
4888 h->needs_plt = 1;
4889 }
4890
4891 /* If we are creating a shared library, we need to copy the
4892 reloc into the shared library. */
0e1862bb 4893 if (bfd_link_pic (info)
36591ba1
SL
4894 && (sec->flags & SEC_ALLOC) != 0
4895 && (r_type == R_NIOS2_BFD_RELOC_32
4896 || (h != NULL && ! h->needs_plt
a496fbc8 4897 && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
36591ba1 4898 {
3bf083ed
AM
4899 struct elf_dyn_relocs *p;
4900 struct elf_dyn_relocs **head;
36591ba1
SL
4901
4902 /* When creating a shared object, we must copy these
4903 reloc types into the output file. We create a reloc
4904 section in dynobj and make room for this reloc. */
4905 if (sreloc == NULL)
4906 {
4ef97a1b
AM
4907 if (htab->root.dynobj == NULL)
4908 htab->root.dynobj = abfd;
4909
36591ba1 4910 sreloc = _bfd_elf_make_dynamic_reloc_section
4ef97a1b 4911 (sec, htab->root.dynobj, 2, abfd, TRUE);
36591ba1
SL
4912 if (sreloc == NULL)
4913 return FALSE;
4914 }
4915
4916 /* If this is a global symbol, we count the number of
4917 relocations we need for this symbol. */
4918 if (h != NULL)
4919 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
4920 else
4921 {
4922 /* Track dynamic relocs needed for local syms too.
4923 We really need local syms available to do this
4924 easily. Oh well. */
4925
4926 asection *s;
4927 void *vpp;
4928 Elf_Internal_Sym *isym;
4929
4930 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4931 abfd, r_symndx);
4932 if (isym == NULL)
4933 return FALSE;
4934
4935 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4936 if (s == NULL)
4937 s = sec;
4938
4939 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 4940 head = (struct elf_dyn_relocs **) vpp;
36591ba1
SL
4941 }
4942
4943 p = *head;
4944 if (p == NULL || p->sec != sec)
4945 {
4946 bfd_size_type amt = sizeof *p;
3bf083ed 4947 p = ((struct elf_dyn_relocs *)
36591ba1
SL
4948 bfd_alloc (htab->root.dynobj, amt));
4949 if (p == NULL)
4950 return FALSE;
4951 p->next = *head;
4952 *head = p;
4953 p->sec = sec;
4954 p->count = 0;
4955 p->pc_count = 0;
4956 }
4957
4958 p->count += 1;
4959
4960 }
4961 break;
4962 }
4963 }
4964
4965 return TRUE;
4966}
4967
4968
4969/* Implement elf_backend_gc_mark_hook:
4970 Return the section that should be marked against GC for a given
4971 relocation. */
4972static asection *
4973nios2_elf32_gc_mark_hook (asection *sec,
25153ba0 4974 struct bfd_link_info *info,
36591ba1
SL
4975 Elf_Internal_Rela *rel,
4976 struct elf_link_hash_entry *h,
4977 Elf_Internal_Sym *sym)
4978{
4979 if (h != NULL)
4980 switch (ELF32_R_TYPE (rel->r_info))
4981 {
4982 case R_NIOS2_GNU_VTINHERIT:
4983 case R_NIOS2_GNU_VTENTRY:
4984 return NULL;
4985 }
4986 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4987}
4988
36591ba1
SL
4989/* Implement elf_backend_finish_dynamic_symbols:
4990 Finish up dynamic symbol handling. We set the contents of various
4991 dynamic sections here. */
4992static bfd_boolean
4993nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
4994 struct bfd_link_info *info,
4995 struct elf_link_hash_entry *h,
4996 Elf_Internal_Sym *sym)
4997{
4998 struct elf32_nios2_link_hash_table *htab;
4999 struct elf32_nios2_link_hash_entry *eh
5000 = (struct elf32_nios2_link_hash_entry *)h;
5001 int use_plt;
5002
5003 htab = elf32_nios2_hash_table (info);
5004
5005 if (h->plt.offset != (bfd_vma) -1)
5006 {
5007 asection *splt;
5008 asection *sgotplt;
5009 asection *srela;
5010 bfd_vma plt_index;
5011 bfd_vma got_offset;
5012 Elf_Internal_Rela rela;
5013 bfd_byte *loc;
5014 bfd_vma got_address;
5015
5016 /* This symbol has an entry in the procedure linkage table. Set
5017 it up. */
5018 BFD_ASSERT (h->dynindx != -1);
5019 splt = htab->root.splt;
5020 sgotplt = htab->root.sgotplt;
5021 srela = htab->root.srelplt;
5022 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
5023
5024 /* Emit the PLT entry. */
0e1862bb 5025 if (bfd_link_pic (info))
36591ba1
SL
5026 {
5027 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
5028 3);
5029 plt_index = (h->plt.offset - 24) / 12;
5030 got_offset = (plt_index + 3) * 4;
5031 nios2_elf32_install_imm16 (splt, h->plt.offset,
5032 hiadj(plt_index * 4));
5033 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5034 (plt_index * 4) & 0xffff);
5035 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
5036 0xfff4 - h->plt.offset);
5037 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5038 + got_offset);
5039
5040 /* Fill in the entry in the global offset table. There are no
5041 res_n slots for a shared object PLT, instead the .got.plt entries
5042 point to the PLT entries. */
5043 bfd_put_32 (output_bfd,
5044 splt->output_section->vma + splt->output_offset
5045 + h->plt.offset, sgotplt->contents + got_offset);
5046 }
5047 else
5048 {
5049 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5050 got_offset = (plt_index + 3) * 4;
5051
5052 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5053 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5054 + got_offset);
5055 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5056 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5057 got_address & 0xffff);
5058
5059 /* Fill in the entry in the global offset table. */
5060 bfd_put_32 (output_bfd,
5061 splt->output_section->vma + splt->output_offset
5062 + plt_index * 4, sgotplt->contents + got_offset);
5063 }
5064
5065 /* Fill in the entry in the .rela.plt section. */
5066 rela.r_offset = got_address;
5067 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5068 rela.r_addend = 0;
5069 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5070 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5071
5072 if (!h->def_regular)
5073 {
5074 /* Mark the symbol as undefined, rather than as defined in
5075 the .plt section. Leave the value alone. */
5076 sym->st_shndx = SHN_UNDEF;
5077 /* If the symbol is weak, we do need to clear the value.
5078 Otherwise, the PLT entry would provide a definition for
5079 the symbol even if the symbol wasn't defined anywhere,
5080 and so the symbol would never be NULL. */
5081 if (!h->ref_regular_nonweak)
5082 sym->st_value = 0;
5083 }
5084 }
5085
1c2de463 5086 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
5087 && h->plt.offset != (bfd_vma) -1);
5088
5089 if (!use_plt && h->got.offset != (bfd_vma) -1
5090 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5091 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5092 {
5093 asection *sgot;
5094 asection *srela;
5095 Elf_Internal_Rela rela;
5096 bfd_byte *loc;
5097 bfd_vma offset;
5098
5099 /* This symbol has an entry in the global offset table. Set it
5100 up. */
5101 sgot = htab->root.sgot;
5102 srela = htab->root.srelgot;
5103 BFD_ASSERT (sgot != NULL && srela != NULL);
5104
5105 offset = (h->got.offset & ~(bfd_vma) 1);
5106 rela.r_offset = (sgot->output_section->vma
5107 + sgot->output_offset + offset);
5108
5109 /* If this is a -Bsymbolic link, and the symbol is defined
5110 locally, we just want to emit a RELATIVE reloc. Likewise if
5111 the symbol was forced to be local because of a version file.
5112 The entry in the global offset table will already have been
5113 initialized in the relocate_section function. */
5114
0e1862bb 5115 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
36591ba1
SL
5116 {
5117 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5118 rela.r_addend = bfd_get_signed_32 (output_bfd,
5119 (sgot->contents + offset));
5120 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5121 }
5122 else
5123 {
5124 bfd_put_32 (output_bfd, (bfd_vma) 0,
5125 sgot->contents + offset);
5126 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5127 rela.r_addend = 0;
5128 }
5129
5130 loc = srela->contents;
5131 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5132 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5133 }
5134
5135 if (use_plt && h->got.offset != (bfd_vma) -1)
5136 {
5137 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5138 asection *sgot = htab->root.sgot;
5139 asection *splt = htab->root.splt;
5140 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5141 + h->plt.offset),
5142 sgot->contents + offset);
5143 }
5144
5145 if (h->needs_copy)
5146 {
5147 asection *s;
5148 Elf_Internal_Rela rela;
5149 bfd_byte *loc;
5150
5151 /* This symbol needs a copy reloc. Set it up. */
5152 BFD_ASSERT (h->dynindx != -1
5153 && (h->root.type == bfd_link_hash_defined
5154 || h->root.type == bfd_link_hash_defweak));
5155
36591ba1
SL
5156 rela.r_offset = (h->root.u.def.value
5157 + h->root.u.def.section->output_section->vma
5158 + h->root.u.def.section->output_offset);
5159 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5160 rela.r_addend = 0;
afbf7e8e 5161 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
5162 s = htab->root.sreldynrelro;
5163 else
5164 s = htab->root.srelbss;
5165 BFD_ASSERT (s != NULL);
36591ba1
SL
5166 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5167 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5168 }
5169
82e91538 5170 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
36591ba1 5171 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4ef97a1b
AM
5172 || h == htab->root.hgot
5173 || h == htab->h_gp_got)
36591ba1
SL
5174 sym->st_shndx = SHN_ABS;
5175
5176 return TRUE;
5177}
5178
5179/* Implement elf_backend_finish_dynamic_sections. */
5180static bfd_boolean
5181nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5182 struct bfd_link_info *info)
5183{
36591ba1
SL
5184 asection *sgotplt;
5185 asection *sdyn;
5186 struct elf32_nios2_link_hash_table *htab;
5187
5188 htab = elf32_nios2_hash_table (info);
36591ba1 5189 sgotplt = htab->root.sgotplt;
4ef97a1b 5190 sdyn = NULL;
36591ba1 5191
4ef97a1b 5192 if (htab->root.dynamic_sections_created)
36591ba1
SL
5193 {
5194 asection *splt;
5195 Elf32_External_Dyn *dyncon, *dynconend;
5196
5197 splt = htab->root.splt;
4ef97a1b
AM
5198 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
5199 BFD_ASSERT (splt != NULL && sdyn != NULL && sgotplt != NULL);
36591ba1
SL
5200
5201 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5202 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5203 for (; dyncon < dynconend; dyncon++)
5204 {
5205 Elf_Internal_Dyn dyn;
5206 asection *s;
5207
4ef97a1b 5208 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
36591ba1
SL
5209
5210 switch (dyn.d_tag)
5211 {
5212 default:
5213 break;
5214
5215 case DT_PLTGOT:
4ade44b7
AM
5216 s = htab->root.sgotplt;
5217 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
36591ba1
SL
5218 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5219 break;
5220
5221 case DT_JMPREL:
5222 s = htab->root.srelplt;
4ade44b7 5223 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
36591ba1
SL
5224 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5225 break;
5226
5227 case DT_PLTRELSZ:
5228 s = htab->root.srelplt;
36591ba1
SL
5229 dyn.d_un.d_val = s->size;
5230 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5231 break;
5232
36591ba1 5233 case DT_NIOS2_GP:
4ade44b7
AM
5234 s = htab->root.sgotplt;
5235 dyn.d_un.d_ptr
5236 = s->output_section->vma + s->output_offset + 0x7ff0;
36591ba1
SL
5237 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5238 break;
5239 }
5240 }
5241
5242 /* Fill in the first entry in the procedure linkage table. */
5243 if (splt->size > 0)
5244 {
5245 bfd_vma got_address = (sgotplt->output_section->vma
5246 + sgotplt->output_offset);
0e1862bb 5247 if (bfd_link_pic (info))
36591ba1 5248 {
33d4099f
SL
5249 bfd_vma got_pcrel = got_address - (splt->output_section->vma
5250 + splt->output_offset);
5251 /* Both GOT and PLT must be aligned to a 16-byte boundary
5252 for the two loads to share the %hiadj part. The 4-byte
5253 offset for nextpc is accounted for in the %lo offsets
5254 on the loads. */
5255 BFD_ASSERT ((got_pcrel & 0xf) == 0);
36591ba1 5256 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
33d4099f
SL
5257 nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5258 nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5259 nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
36591ba1
SL
5260 }
5261 else
5262 {
5263 /* Divide by 4 here, not 3 because we already corrected for the
5264 res_N branches. */
5265 bfd_vma res_size = (splt->size - 28) / 4;
5266 bfd_vma res_start = (splt->output_section->vma
5267 + splt->output_offset);
5268 bfd_vma res_offset;
5269
5270 for (res_offset = 0; res_offset < res_size; res_offset += 4)
5271 bfd_put_32 (output_bfd,
5272 6 | ((res_size - (res_offset + 4)) << 6),
5273 splt->contents + res_offset);
5274
33d4099f
SL
5275 /* The GOT must be aligned to a 16-byte boundary for the
5276 two loads to share the same %hiadj part. */
5277 BFD_ASSERT ((got_address & 0xf) == 0);
5278
36591ba1
SL
5279 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5280 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5281 nios2_elf32_install_imm16 (splt, res_size + 4,
5282 res_start & 0xffff);
5283 nios2_elf32_install_imm16 (splt, res_size + 12,
5284 hiadj (got_address));
5285 nios2_elf32_install_imm16 (splt, res_size + 16,
33d4099f 5286 (got_address + 4) & 0xffff);
36591ba1 5287 nios2_elf32_install_imm16 (splt, res_size + 20,
33d4099f 5288 (got_address + 8) & 0xffff);
36591ba1
SL
5289 }
5290 }
5291 }
4ef97a1b 5292
36591ba1 5293 /* Fill in the first three entries in the global offset table. */
4ef97a1b 5294 if (sgotplt != NULL && sgotplt->size > 0)
36591ba1
SL
5295 {
5296 if (sdyn == NULL)
5297 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5298 else
5299 bfd_put_32 (output_bfd,
5300 sdyn->output_section->vma + sdyn->output_offset,
5301 sgotplt->contents);
5302 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5303 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
36591ba1 5304
4ef97a1b
AM
5305 if (sgotplt->output_section != bfd_abs_section_ptr)
5306 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5307 }
36591ba1
SL
5308
5309 return TRUE;
5310}
5311
5312/* Implement elf_backend_adjust_dynamic_symbol:
5313 Adjust a symbol defined by a dynamic object and referenced by a
5314 regular object. The current definition is in some section of the
5315 dynamic object, but we're not including those sections. We have to
5316 change the definition to something the rest of the link can
5317 understand. */
5318static bfd_boolean
5319nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5320 struct elf_link_hash_entry *h)
5321{
5322 struct elf32_nios2_link_hash_table *htab;
5323 bfd *dynobj;
5474d94f 5324 asection *s, *srel;
36591ba1
SL
5325 unsigned align2;
5326
5327 htab = elf32_nios2_hash_table (info);
4ef97a1b 5328 dynobj = htab->root.dynobj;
36591ba1
SL
5329
5330 /* Make sure we know what is going on here. */
5331 BFD_ASSERT (dynobj != NULL
5332 && (h->needs_plt
60d67dc8 5333 || h->is_weakalias
36591ba1
SL
5334 || (h->def_dynamic
5335 && h->ref_regular
5336 && !h->def_regular)));
5337
5338 /* If this is a function, put it in the procedure linkage table. We
5339 will fill in the contents of the procedure linkage table later,
5340 when we know the address of the .got section. */
5341 if (h->type == STT_FUNC || h->needs_plt)
5342 {
5343 if (h->plt.refcount <= 0
5344 || SYMBOL_CALLS_LOCAL (info, h)
5345 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5346 && h->root.type == bfd_link_hash_undefweak))
5347 {
5348 /* This case can occur if we saw a PLT reloc in an input
5349 file, but the symbol was never referred to by a dynamic
5350 object, or if all references were garbage collected. In
5351 such a case, we don't actually need to build a procedure
5352 linkage table, and we can just do a PCREL reloc instead. */
5353 h->plt.offset = (bfd_vma) -1;
5354 h->needs_plt = 0;
5355 }
5356
5357 return TRUE;
5358 }
5359
5360 /* Reinitialize the plt offset now that it is not used as a reference
5361 count any more. */
5362 h->plt.offset = (bfd_vma) -1;
5363
5364 /* If this is a weak symbol, and there is a real definition, the
5365 processor independent code will have arranged for us to see the
5366 real definition first, and we can just use the same value. */
60d67dc8 5367 if (h->is_weakalias)
36591ba1 5368 {
60d67dc8
AM
5369 struct elf_link_hash_entry *def = weakdef (h);
5370 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5371 h->root.u.def.section = def->root.u.def.section;
5372 h->root.u.def.value = def->root.u.def.value;
36591ba1
SL
5373 return TRUE;
5374 }
5375
5376 /* If there are no non-GOT references, we do not need a copy
5377 relocation. */
5378 if (!h->non_got_ref)
5379 return TRUE;
5380
5381 /* This is a reference to a symbol defined by a dynamic object which
5382 is not a function.
5383 If we are creating a shared library, we must presume that the
5384 only references to the symbol are via the global offset table.
5385 For such cases we need not do anything here; the relocations will
5386 be handled correctly by relocate_section. */
0e1862bb 5387 if (bfd_link_pic (info))
36591ba1
SL
5388 return TRUE;
5389
5390 if (h->size == 0)
5391 {
4eca0228
AM
5392 _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5393 h->root.root.string);
36591ba1
SL
5394 return TRUE;
5395 }
5396
5397 /* We must allocate the symbol in our .dynbss section, which will
5398 become part of the .bss section of the executable. There will be
5399 an entry for this symbol in the .dynsym section. The dynamic
5400 object will contain position independent code, so all references
5401 from the dynamic object to this symbol will go through the global
5402 offset table. The dynamic linker will use the .dynsym entry to
5403 determine the address it must put in the global offset table, so
5404 both the dynamic object and the regular object will refer to the
5405 same memory location for the variable. */
36591ba1
SL
5406 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5407 copy the initial value out of the dynamic object and into the
5408 runtime process image. We need to remember the offset into the
5409 .rela.bss section we are going to use. */
5474d94f 5410 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
36591ba1 5411 {
5474d94f
AM
5412 s = htab->root.sdynrelro;
5413 srel = htab->root.sreldynrelro;
5414 }
5415 else
5416 {
5417 s = htab->root.sdynbss;
9d19e4fd 5418 srel = htab->root.srelbss;
5474d94f
AM
5419 }
5420 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5421 {
36591ba1
SL
5422 srel->size += sizeof (Elf32_External_Rela);
5423 h->needs_copy = 1;
5424 }
5425
5426 align2 = bfd_log2 (h->size);
5427 if (align2 > h->root.u.def.section->alignment_power)
5428 align2 = h->root.u.def.section->alignment_power;
5429
5430 /* Align dynbss. */
5431 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
5432 if (align2 > bfd_get_section_alignment (dynobj, s)
5433 && !bfd_set_section_alignment (dynobj, s, align2))
5434 return FALSE;
5435
5436 /* Define the symbol as being at this point in the section. */
5437 h->root.u.def.section = s;
5438 h->root.u.def.value = s->size;
5439
5440 /* Increment the section size to make room for the symbol. */
5441 s->size += h->size;
5442
5443 return TRUE;
5444}
5445
5446/* Worker function for nios2_elf32_size_dynamic_sections. */
5447static bfd_boolean
5448adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5449{
5450 struct bfd_link_info *info;
5451 struct elf32_nios2_link_hash_table *htab;
5452
5453 if (h->root.type == bfd_link_hash_indirect)
5454 return TRUE;
5455
5456 if (h->root.type == bfd_link_hash_warning)
5457 /* When warning symbols are created, they **replace** the "real"
5458 entry in the hash table, thus we never get to see the real
5459 symbol in a hash traversal. So look at it now. */
5460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5461
5462 info = (struct bfd_link_info *) inf;
5463 htab = elf32_nios2_hash_table (info);
5464
5465 if (h->plt.offset != (bfd_vma)-1)
5466 h->plt.offset += htab->res_n_size;
5467 if (htab->root.splt == h->root.u.def.section)
5468 h->root.u.def.value += htab->res_n_size;
5469
5470 return TRUE;
5471}
5472
5473/* Another worker function for nios2_elf32_size_dynamic_sections.
5474 Allocate space in .plt, .got and associated reloc sections for
5475 dynamic relocs. */
5476static bfd_boolean
5477allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5478{
5479 struct bfd_link_info *info;
5480 struct elf32_nios2_link_hash_table *htab;
5481 struct elf32_nios2_link_hash_entry *eh;
3bf083ed 5482 struct elf_dyn_relocs *p;
36591ba1
SL
5483 int use_plt;
5484
5485 if (h->root.type == bfd_link_hash_indirect)
5486 return TRUE;
5487
5488 if (h->root.type == bfd_link_hash_warning)
5489 /* When warning symbols are created, they **replace** the "real"
5490 entry in the hash table, thus we never get to see the real
5491 symbol in a hash traversal. So look at it now. */
5492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5493
5494 info = (struct bfd_link_info *) inf;
5495 htab = elf32_nios2_hash_table (info);
5496
5497 if (htab->root.dynamic_sections_created
5498 && h->plt.refcount > 0)
5499 {
5500 /* Make sure this symbol is output as a dynamic symbol.
5501 Undefined weak syms won't yet be marked as dynamic. */
5502 if (h->dynindx == -1
5503 && !h->forced_local
5504 && !bfd_elf_link_record_dynamic_symbol (info, h))
5505 return FALSE;
5506
0e1862bb 5507 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
36591ba1
SL
5508 {
5509 asection *s = htab->root.splt;
5510
5511 /* Allocate room for the header. */
5512 if (s->size == 0)
5513 {
0e1862bb 5514 if (bfd_link_pic (info))
36591ba1
SL
5515 s->size = 24;
5516 else
5517 s->size = 28;
5518 }
5519
5520 h->plt.offset = s->size;
5521
5522 /* If this symbol is not defined in a regular file, and we are
5523 not generating a shared library, then set the symbol to this
5524 location in the .plt. This is required to make function
5525 pointers compare as equal between the normal executable and
5526 the shared library. */
0e1862bb 5527 if (! bfd_link_pic (info)
36591ba1
SL
5528 && !h->def_regular)
5529 {
5530 h->root.u.def.section = s;
5531 h->root.u.def.value = h->plt.offset;
5532 }
5533
5534 /* Make room for this entry. */
5535 s->size += 12;
5536
5537 /* We also need to make an entry in the .rela.plt section. */
5538 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5539
5540 /* And the .got.plt section. */
5541 htab->root.sgotplt->size += 4;
5542 }
5543 else
5544 {
5545 h->plt.offset = (bfd_vma) -1;
5546 h->needs_plt = 0;
5547 }
5548 }
5549 else
5550 {
5551 h->plt.offset = (bfd_vma) -1;
5552 h->needs_plt = 0;
5553 }
5554
5555 eh = (struct elf32_nios2_link_hash_entry *) h;
1c2de463 5556 use_plt = (eh->got_types_used == CALL_USED
36591ba1
SL
5557 && h->plt.offset != (bfd_vma) -1);
5558
5559 if (h->got.refcount > 0)
5560 {
5561 asection *s;
5562 bfd_boolean dyn;
5563 int tls_type = eh->tls_type;
5564 int indx;
5565
5566 /* Make sure this symbol is output as a dynamic symbol.
5567 Undefined weak syms won't yet be marked as dynamic. */
5568 if (h->dynindx == -1
5569 && !h->forced_local
5570 && !bfd_elf_link_record_dynamic_symbol (info, h))
5571 return FALSE;
5572
5573 s = htab->root.sgot;
5574 h->got.offset = s->size;
5575
5576 if (tls_type == GOT_UNKNOWN)
5577 abort ();
5578
5579 if (tls_type == GOT_NORMAL)
5580 /* Non-TLS symbols need one GOT slot. */
5581 s->size += 4;
5582 else
5583 {
5584 if (tls_type & GOT_TLS_GD)
5585 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5586 s->size += 8;
5587 if (tls_type & GOT_TLS_IE)
5588 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5589 s->size += 4;
5590 }
5591
5592 dyn = htab->root.dynamic_sections_created;
5593
5594 indx = 0;
0e1862bb
L
5595 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5596 && (!bfd_link_pic (info)
36591ba1
SL
5597 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5598 indx = h->dynindx;
5599
5600 if (tls_type != GOT_NORMAL
0e1862bb 5601 && (bfd_link_pic (info) || indx != 0)
36591ba1
SL
5602 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5603 || h->root.type != bfd_link_hash_undefweak))
5604 {
5605 if (tls_type & GOT_TLS_IE)
5606 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5607
5608 if (tls_type & GOT_TLS_GD)
5609 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5610
5611 if ((tls_type & GOT_TLS_GD) && indx != 0)
5612 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5613 }
5614 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5615 || h->root.type != bfd_link_hash_undefweak)
5616 && !use_plt
0e1862bb 5617 && (bfd_link_pic (info)
36591ba1
SL
5618 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5619 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5620 }
5621 else
5622 h->got.offset = (bfd_vma) -1;
5623
5624 if (eh->dyn_relocs == NULL)
5625 return TRUE;
5626
5627 /* In the shared -Bsymbolic case, discard space allocated for
5628 dynamic pc-relative relocs against symbols which turn out to be
5629 defined in regular objects. For the normal shared case, discard
5630 space for pc-relative relocs that have become local due to symbol
5631 visibility changes. */
5632
0e1862bb 5633 if (bfd_link_pic (info))
36591ba1
SL
5634 {
5635 if (h->def_regular
a496fbc8 5636 && (h->forced_local || SYMBOLIC_BIND (info, h)))
36591ba1 5637 {
3bf083ed 5638 struct elf_dyn_relocs **pp;
36591ba1
SL
5639
5640 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5641 {
5642 p->count -= p->pc_count;
5643 p->pc_count = 0;
5644 if (p->count == 0)
5645 *pp = p->next;
5646 else
5647 pp = &p->next;
5648 }
5649 }
5650
5651 /* Also discard relocs on undefined weak syms with non-default
5652 visibility. */
5653 if (eh->dyn_relocs != NULL
5654 && h->root.type == bfd_link_hash_undefweak)
5655 {
31a53da5
L
5656 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5657 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
36591ba1
SL
5658 eh->dyn_relocs = NULL;
5659
5660 /* Make sure undefined weak symbols are output as a dynamic
5661 symbol in PIEs. */
5662 else if (h->dynindx == -1
5663 && !h->forced_local
5664 && !bfd_elf_link_record_dynamic_symbol (info, h))
5665 return FALSE;
5666 }
5667 }
5668 else
5669 {
5670 /* For the non-shared case, discard space for relocs against
5671 symbols which turn out to need copy relocs or are not
5672 dynamic. */
5673
5674 if (!h->non_got_ref
5675 && ((h->def_dynamic && !h->def_regular)
5676 || (htab->root.dynamic_sections_created
5677 && (h->root.type == bfd_link_hash_undefweak
5678 || h->root.type == bfd_link_hash_undefined))))
5679 {
5680 /* Make sure this symbol is output as a dynamic symbol.
5681 Undefined weak syms won't yet be marked as dynamic. */
5682 if (h->dynindx == -1
5683 && !h->forced_local
5684 && !bfd_elf_link_record_dynamic_symbol (info, h))
5685 return FALSE;
5686
5687 /* If that succeeded, we know we'll be keeping all the
5688 relocs. */
5689 if (h->dynindx != -1)
5690 goto keep;
5691 }
5692
5693 eh->dyn_relocs = NULL;
5694
5695 keep: ;
5696 }
5697
5698 /* Finally, allocate space. */
5699 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5700 {
5701 asection *sreloc = elf_section_data (p->sec)->sreloc;
5702 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5703 }
5704
5705 return TRUE;
5706}
5707
5708/* Implement elf_backend_size_dynamic_sections:
5709 Set the sizes of the dynamic sections. */
5710static bfd_boolean
5711nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5712 struct bfd_link_info *info)
5713{
5714 bfd *dynobj;
5715 asection *s;
36591ba1
SL
5716 bfd_boolean relocs;
5717 bfd *ibfd;
5718 struct elf32_nios2_link_hash_table *htab;
5719
5720 htab = elf32_nios2_hash_table (info);
4ef97a1b 5721 dynobj = htab->root.dynobj;
36591ba1
SL
5722 BFD_ASSERT (dynobj != NULL);
5723
5724 htab->res_n_size = 0;
4ef97a1b 5725 if (htab->root.dynamic_sections_created)
36591ba1
SL
5726 {
5727 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 5728 if (bfd_link_executable (info) && !info->nointerp)
36591ba1
SL
5729 {
5730 s = bfd_get_linker_section (dynobj, ".interp");
5731 BFD_ASSERT (s != NULL);
5732 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5733 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5734 }
5735 }
5736 else
5737 {
5738 /* We may have created entries in the .rela.got section.
5739 However, if we are not creating the dynamic sections, we will
5740 not actually use these entries. Reset the size of .rela.got,
5741 which will cause it to get stripped from the output file
5742 below. */
5743 s = htab->root.srelgot;
5744 if (s != NULL)
5745 s->size = 0;
5746 }
5747
5748 /* Set up .got offsets for local syms, and space for local dynamic
5749 relocs. */
c72f2fb2 5750 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
36591ba1
SL
5751 {
5752 bfd_signed_vma *local_got;
5753 bfd_signed_vma *end_local_got;
5754 char *local_tls_type;
5755 bfd_size_type locsymcount;
5756 Elf_Internal_Shdr *symtab_hdr;
5757 asection *srel;
5758
5759 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5760 continue;
5761
5762 for (s = ibfd->sections; s != NULL; s = s->next)
5763 {
3bf083ed 5764 struct elf_dyn_relocs *p;
36591ba1
SL
5765
5766 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5767 {
5768 if (!bfd_is_abs_section (p->sec)
5769 && bfd_is_abs_section (p->sec->output_section))
5770 {
5771 /* Input section has been discarded, either because
5772 it is a copy of a linkonce section or due to
5773 linker script /DISCARD/, so we'll be discarding
5774 the relocs too. */
5775 }
5776 else if (p->count != 0)
5777 {
5778 srel = elf_section_data (p->sec)->sreloc;
5779 srel->size += p->count * sizeof (Elf32_External_Rela);
5780 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5781 info->flags |= DF_TEXTREL;
5782 }
5783 }
5784 }
5785
5786 local_got = elf_local_got_refcounts (ibfd);
5787 if (!local_got)
5788 continue;
5789
5790 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5791 locsymcount = symtab_hdr->sh_info;
5792 end_local_got = local_got + locsymcount;
5793 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5794 s = htab->root.sgot;
5795 srel = htab->root.srelgot;
5796 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5797 {
5798 if (*local_got > 0)
5799 {
5800 *local_got = s->size;
5801 if (*local_tls_type & GOT_TLS_GD)
5802 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5803 s->size += 8;
5804 if (*local_tls_type & GOT_TLS_IE)
5805 s->size += 4;
5806 if (*local_tls_type == GOT_NORMAL)
5807 s->size += 4;
5808
0e1862bb 5809 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
36591ba1
SL
5810 srel->size += sizeof (Elf32_External_Rela);
5811 }
5812 else
5813 *local_got = (bfd_vma) -1;
5814 }
5815 }
5816
5817 if (htab->tls_ldm_got.refcount > 0)
5818 {
5819 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5820 for R_NIOS2_TLS_LDM16 relocations. */
5821 htab->tls_ldm_got.offset = htab->root.sgot->size;
5822 htab->root.sgot->size += 8;
0e1862bb 5823 if (bfd_link_pic (info))
36591ba1
SL
5824 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5825 }
5826 else
5827 htab->tls_ldm_got.offset = -1;
5828
5829 /* Allocate global sym .plt and .got entries, and space for global
5830 sym dynamic relocs. */
5831 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5832
4ef97a1b 5833 if (htab->root.dynamic_sections_created)
82e91538
SL
5834 {
5835 /* If the .got section is more than 0x8000 bytes, we add
5836 0x8000 to the value of _gp_got, so that 16-bit relocations
5837 have a greater chance of working. */
5838 if (htab->root.sgot->size >= 0x8000
4ef97a1b
AM
5839 && htab->h_gp_got->root.u.def.value == 0)
5840 htab->h_gp_got->root.u.def.value = 0x8000;
82e91538
SL
5841 }
5842
36591ba1
SL
5843 /* The check_relocs and adjust_dynamic_symbol entry points have
5844 determined the sizes of the various dynamic sections. Allocate
5845 memory for them. */
36591ba1
SL
5846 relocs = FALSE;
5847 for (s = dynobj->sections; s != NULL; s = s->next)
5848 {
5849 const char *name;
5850
5851 if ((s->flags & SEC_LINKER_CREATED) == 0)
5852 continue;
5853
5854 /* It's OK to base decisions on the section name, because none
5855 of the dynobj section names depend upon the input files. */
5856 name = bfd_get_section_name (dynobj, s);
5857
4ef97a1b 5858 if (CONST_STRNEQ (name, ".rela"))
36591ba1
SL
5859 {
5860 if (s->size != 0)
5861 {
4ef97a1b
AM
5862 if (s != htab->root.srelplt)
5863 relocs = TRUE;
36591ba1
SL
5864
5865 /* We use the reloc_count field as a counter if we need
5866 to copy relocs into the output file. */
5867 s->reloc_count = 0;
5868 }
5869 }
4ef97a1b 5870 else if (s == htab->root.splt)
5474d94f 5871 {
4ef97a1b
AM
5872 /* Correct for the number of res_N branches. */
5873 if (s->size != 0 && !bfd_link_pic (info))
5874 {
5875 htab->res_n_size = (s->size - 28) / 3;
5876 s->size += htab->res_n_size;
5877 }
5474d94f 5878 }
4ef97a1b
AM
5879 else if (s != htab->sbss
5880 && s != htab->root.sgot
5881 && s != htab->root.sgotplt
5882 && s != htab->root.sdynbss
5474d94f 5883 && s != htab->root.sdynrelro)
36591ba1
SL
5884 /* It's not one of our sections, so don't allocate space. */
5885 continue;
5886
5887 if (s->size == 0)
5888 {
36591ba1
SL
5889 s->flags |= SEC_EXCLUDE;
5890 continue;
5891 }
5892
5893 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5894 continue;
5895
5896 /* Allocate memory for the section contents. */
36591ba1
SL
5897 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5898 if (s->contents == NULL)
5899 return FALSE;
5900 }
5901
5902 /* Adjust dynamic symbols that point to the plt to account for the
5903 now-known number of resN slots. */
5904 if (htab->res_n_size)
5905 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
5906
4ef97a1b 5907 if (htab->root.dynamic_sections_created)
36591ba1
SL
5908 {
5909 /* Add some entries to the .dynamic section. We fill in the
5910 values later, in elf_nios2_finish_dynamic_sections, but we
5911 must add the entries now so that we get the correct size for
5912 the .dynamic section. The DT_DEBUG entry is filled in by the
5913 dynamic linker and used by the debugger. */
5914#define add_dynamic_entry(TAG, VAL) \
5915 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5916
0e1862bb 5917 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_DEBUG, 0))
36591ba1
SL
5918 return FALSE;
5919
4ef97a1b
AM
5920 if (htab->root.sgotplt->size != 0
5921 && !add_dynamic_entry (DT_PLTGOT, 0))
36591ba1
SL
5922 return FALSE;
5923
4ef97a1b 5924 if (htab->root.splt->size != 0
36591ba1
SL
5925 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
5926 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5927 || !add_dynamic_entry (DT_JMPREL, 0)))
5928 return FALSE;
5929
5930 if (relocs
5931 && (!add_dynamic_entry (DT_RELA, 0)
5932 || !add_dynamic_entry (DT_RELASZ, 0)
5933 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
5934 return FALSE;
5935
0e1862bb 5936 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_NIOS2_GP, 0))
36591ba1
SL
5937 return FALSE;
5938
5939 if ((info->flags & DF_TEXTREL) != 0
5940 && !add_dynamic_entry (DT_TEXTREL, 0))
5941 return FALSE;
5942 }
5943#undef add_dynamic_entry
5944
5945 return TRUE;
5946}
5947
68faa637
AM
5948/* Free the derived linker hash table. */
5949static void
d495ab0d 5950nios2_elf32_link_hash_table_free (bfd *obfd)
68faa637
AM
5951{
5952 struct elf32_nios2_link_hash_table *htab
d495ab0d 5953 = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
68faa637
AM
5954
5955 bfd_hash_table_free (&htab->bstab);
d495ab0d 5956 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
5957}
5958
36591ba1
SL
5959/* Implement bfd_elf32_bfd_link_hash_table_create. */
5960static struct bfd_link_hash_table *
5961nios2_elf32_link_hash_table_create (bfd *abfd)
5962{
5963 struct elf32_nios2_link_hash_table *ret;
5964 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
5965
7bf52ea2 5966 ret = bfd_zmalloc (amt);
36591ba1
SL
5967 if (ret == NULL)
5968 return NULL;
5969
5970 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
5971 link_hash_newfunc,
5972 sizeof (struct
5973 elf32_nios2_link_hash_entry),
5974 NIOS2_ELF_DATA))
5975 {
5976 free (ret);
5977 return NULL;
5978 }
5979
78058a5e
SL
5980 /* Init the stub hash table too. */
5981 if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
5982 sizeof (struct elf32_nios2_stub_hash_entry)))
d495ab0d
AM
5983 {
5984 _bfd_elf_link_hash_table_free (abfd);
5985 return NULL;
5986 }
5987 ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
78058a5e 5988
36591ba1
SL
5989 return &ret->root.root;
5990}
5991
5992/* Implement elf_backend_reloc_type_class. */
5993static enum elf_reloc_type_class
7e612e98
AM
5994nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5995 const asection *rel_sec ATTRIBUTE_UNUSED,
5996 const Elf_Internal_Rela *rela)
36591ba1
SL
5997{
5998 switch ((int) ELF32_R_TYPE (rela->r_info))
5999 {
6000 case R_NIOS2_RELATIVE:
6001 return reloc_class_relative;
6002 case R_NIOS2_JUMP_SLOT:
6003 return reloc_class_plt;
6004 case R_NIOS2_COPY:
6005 return reloc_class_copy;
6006 default:
6007 return reloc_class_normal;
6008 }
6009}
6010
6011/* Return 1 if target is one of ours. */
6012static bfd_boolean
6013is_nios2_elf_target (const struct bfd_target *targ)
6014{
6d00b590
AM
6015 return (targ == &nios2_elf32_le_vec
6016 || targ == &nios2_elf32_be_vec);
36591ba1
SL
6017}
6018
6019/* Implement elf_backend_add_symbol_hook.
6020 This hook is called by the linker when adding symbols from an object
6021 file. We use it to put .comm items in .sbss, and not .bss. */
6022static bfd_boolean
6023nios2_elf_add_symbol_hook (bfd *abfd,
6024 struct bfd_link_info *info,
6025 Elf_Internal_Sym *sym,
6026 const char **namep ATTRIBUTE_UNUSED,
6027 flagword *flagsp ATTRIBUTE_UNUSED,
6028 asection **secp,
6029 bfd_vma *valp)
6030{
36591ba1 6031 if (sym->st_shndx == SHN_COMMON
0e1862bb 6032 && !bfd_link_relocatable (info)
36591ba1
SL
6033 && sym->st_size <= elf_gp_size (abfd)
6034 && is_nios2_elf_target (info->output_bfd->xvec))
6035 {
6036 /* Common symbols less than or equal to -G nn bytes are automatically
6037 put into .sbss. */
6038 struct elf32_nios2_link_hash_table *htab;
6039
6040 htab = elf32_nios2_hash_table (info);
6041 if (htab->sbss == NULL)
6042 {
6043 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
6044
4ef97a1b
AM
6045 if (htab->root.dynobj == NULL)
6046 htab->root.dynobj = abfd;
36591ba1 6047
4ef97a1b
AM
6048 htab->sbss = bfd_make_section_anyway_with_flags (htab->root.dynobj,
6049 ".sbss", flags);
36591ba1
SL
6050 if (htab->sbss == NULL)
6051 return FALSE;
6052 }
6053
6054 *secp = htab->sbss;
6055 *valp = sym->st_size;
6056 }
6057
6058 return TRUE;
6059}
6060
6061/* Implement elf_backend_can_make_relative_eh_frame:
6062 Decide whether to attempt to turn absptr or lsda encodings in
6063 shared libraries into pcrel within the given input section. */
6064static bfd_boolean
6065nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6066 struct bfd_link_info *info
6067 ATTRIBUTE_UNUSED,
6068 asection *eh_frame_section
6069 ATTRIBUTE_UNUSED)
6070{
6071 /* We can't use PC-relative encodings in the .eh_frame section. */
6072 return FALSE;
6073}
6074
6075/* Implement elf_backend_special_sections. */
6076const struct bfd_elf_special_section elf32_nios2_special_sections[] =
6077{
6078 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
6079 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6080 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6081 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6082 { NULL, 0, 0, 0, 0 }
6083};
6084
6085#define ELF_ARCH bfd_arch_nios2
6086#define ELF_TARGET_ID NIOS2_ELF_DATA
6087#define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6088
6089/* The Nios II MMU uses a 4K page size. */
6090
6091#define ELF_MAXPAGESIZE 0x1000
6092
6093#define bfd_elf32_bfd_link_hash_table_create \
6094 nios2_elf32_link_hash_table_create
6095
965b1d80
SL
6096#define bfd_elf32_bfd_merge_private_bfd_data \
6097 nios2_elf32_merge_private_bfd_data
6098
36591ba1
SL
6099/* Relocation table lookup macros. */
6100
6101#define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6102#define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6103
6104/* JUMP_TABLE_LINK macros. */
6105
6106/* elf_info_to_howto (using RELA relocations). */
6107
6108#define elf_info_to_howto nios2_elf32_info_to_howto
6109
6110/* elf backend functions. */
6111
6112#define elf_backend_can_gc_sections 1
6113#define elf_backend_can_refcount 1
6114#define elf_backend_plt_readonly 1
6115#define elf_backend_want_got_plt 1
5474d94f 6116#define elf_backend_want_dynrelro 1
36591ba1 6117#define elf_backend_rela_normal 1
64f52338 6118#define elf_backend_dtrel_excludes_plt 1
36591ba1
SL
6119
6120#define elf_backend_relocate_section nios2_elf32_relocate_section
6121#define elf_backend_section_flags nios2_elf32_section_flags
6122#define elf_backend_fake_sections nios2_elf32_fake_sections
6123#define elf_backend_check_relocs nios2_elf32_check_relocs
6124
6125#define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
36591ba1
SL
6126#define elf_backend_create_dynamic_sections \
6127 nios2_elf32_create_dynamic_sections
6128#define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6129#define elf_backend_finish_dynamic_sections \
6130 nios2_elf32_finish_dynamic_sections
6131#define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6132#define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6133#define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6134#define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6135#define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
965b1d80 6136#define elf_backend_object_p nios2_elf32_object_p
36591ba1
SL
6137
6138#define elf_backend_grok_prstatus nios2_grok_prstatus
6139#define elf_backend_grok_psinfo nios2_grok_psinfo
6140
6141#undef elf_backend_can_make_relative_eh_frame
6142#define elf_backend_can_make_relative_eh_frame \
6143 nios2_elf32_can_make_relative_eh_frame
6144
6145#define elf_backend_special_sections elf32_nios2_special_sections
6146
6d00b590 6147#define TARGET_LITTLE_SYM nios2_elf32_le_vec
36591ba1 6148#define TARGET_LITTLE_NAME "elf32-littlenios2"
6d00b590 6149#define TARGET_BIG_SYM nios2_elf32_be_vec
36591ba1
SL
6150#define TARGET_BIG_NAME "elf32-bignios2"
6151
6152#define elf_backend_got_header_size 12
03d5b773 6153#define elf_backend_default_execstack 0
36591ba1
SL
6154
6155#include "elf32-target.h"