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