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