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