Edit ELFv2 global entry prologue to non-PIC
[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"
33
34/* Use RELA relocations. */
35#ifndef USE_RELA
36#define USE_RELA
37#endif
38
39#ifdef USE_REL
40#undef USE_REL
41#endif
42
43/* Forward declarations. */
44static bfd_reloc_status_type nios2_elf32_ignore_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46static bfd_reloc_status_type nios2_elf32_hi16_relocate
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48static bfd_reloc_status_type nios2_elf32_lo16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58static bfd_reloc_status_type nios2_elf32_call26_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60static bfd_reloc_status_type nios2_elf32_gprel_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62static bfd_reloc_status_type nios2_elf32_ujmp_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64static bfd_reloc_status_type nios2_elf32_cjmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66static bfd_reloc_status_type nios2_elf32_callr_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68
69/* Target vector. */
70extern const bfd_target bfd_elf32_littlenios2_vec;
71extern const bfd_target bfd_elf32_bignios2_vec;
72
73/* Offset of tp and dtp pointers from start of TLS block. */
74#define TP_OFFSET 0x7000
75#define DTP_OFFSET 0x8000
76
77/* The relocation table used for SHT_REL sections. */
78static reloc_howto_type elf_nios2_howto_table_rel[] = {
79 /* No relocation. */
80 HOWTO (R_NIOS2_NONE, /* type */
81 0, /* rightshift */
82 0, /* size (0 = byte, 1 = short, 2 = long) */
83 0, /* bitsize */
84 FALSE, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_NIOS2_NONE", /* name */
89 FALSE, /* partial_inplace */
90 0, /* src_mask */
91 0, /* dst_mask */
92 FALSE), /* pcrel_offset */
93
94 /* 16-bit signed immediate relocation. */
95 HOWTO (R_NIOS2_S16, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 16, /* bitsize */
99 FALSE, /* pc_relative */
100 6, /* bitpos */
101 complain_overflow_signed, /* complain on overflow */
102 bfd_elf_generic_reloc, /* special function */
103 "R_NIOS2_S16", /* name */
104 FALSE, /* partial_inplace */
105 0x003fffc0, /* src_mask */
106 0x003fffc0, /* dest_mask */
107 FALSE), /* pcrel_offset */
108
109 /* 16-bit unsigned immediate relocation. */
110 HOWTO (R_NIOS2_U16, /* type */
111 0, /* rightshift */
112 2, /* size (0 = byte, 1 = short, 2 = long) */
113 16, /* bitsize */
114 FALSE, /* pc_relative */
115 6, /* bitpos */
116 complain_overflow_unsigned, /* complain on overflow */
117 bfd_elf_generic_reloc, /* special function */
118 "R_NIOS2_U16", /* name */
119 FALSE, /* partial_inplace */
120 0x003fffc0, /* src_mask */
121 0x003fffc0, /* dest_mask */
122 FALSE), /* pcrel_offset */
123
124 HOWTO (R_NIOS2_PCREL16, /* type */
125 0, /* rightshift */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
127 16, /* bitsize */
128 TRUE, /* pc_relative */
129 6, /* bitpos */
130 complain_overflow_signed, /* complain on overflow */
131 nios2_elf32_pcrel16_relocate, /* special function */
132 "R_NIOS2_PCREL16", /* name */
133 FALSE, /* partial_inplace */
134 0x003fffc0, /* src_mask */
135 0x003fffc0, /* dest_mask */
136 TRUE), /* pcrel_offset */
137
138 HOWTO (R_NIOS2_CALL26, /* type */
139 2, /* rightshift */
140 2, /* size (0 = byte, 1 = short, 2 = long) */
141 26, /* bitsize */
142 FALSE, /* pc_relative */
143 6, /* bitpos */
144 complain_overflow_dont, /* complain on overflow */
145 nios2_elf32_call26_relocate, /* special function */
146 "R_NIOS2_CALL26", /* name */
147 FALSE, /* partial_inplace */
148 0xffffffc0, /* src_mask */
149 0xffffffc0, /* dst_mask */
150 FALSE), /* pcrel_offset */
151
152 HOWTO (R_NIOS2_IMM5,
153 0,
154 2,
155 5,
156 FALSE,
157 6,
158 complain_overflow_bitfield,
159 bfd_elf_generic_reloc,
160 "R_NIOS2_IMM5",
161 FALSE,
162 0x000007c0,
163 0x000007c0,
164 FALSE),
165
166 HOWTO (R_NIOS2_CACHE_OPX,
167 0,
168 2,
169 5,
170 FALSE,
171 22,
172 complain_overflow_bitfield,
173 bfd_elf_generic_reloc,
174 "R_NIOS2_CACHE_OPX",
175 FALSE,
176 0x07c00000,
177 0x07c00000,
178 FALSE),
179
180 HOWTO (R_NIOS2_IMM6,
181 0,
182 2,
183 6,
184 FALSE,
185 6,
186 complain_overflow_bitfield,
187 bfd_elf_generic_reloc,
188 "R_NIOS2_IMM6",
189 FALSE,
190 0x00000fc0,
191 0x00000fc0,
192 FALSE),
193
194 HOWTO (R_NIOS2_IMM8,
195 0,
196 2,
197 8,
198 FALSE,
199 6,
200 complain_overflow_bitfield,
201 bfd_elf_generic_reloc,
202 "R_NIOS2_IMM8",
203 FALSE,
204 0x00003fc0,
205 0x00003fc0,
206 FALSE),
207
208 HOWTO (R_NIOS2_HI16,
209 0,
210 2,
211 32,
212 FALSE,
213 6,
214 complain_overflow_dont,
215 nios2_elf32_hi16_relocate,
216 "R_NIOS2_HI16",
217 FALSE,
218 0x003fffc0,
219 0x003fffc0,
220 FALSE),
221
222 HOWTO (R_NIOS2_LO16,
223 0,
224 2,
225 32,
226 FALSE,
227 6,
228 complain_overflow_dont,
229 nios2_elf32_lo16_relocate,
230 "R_NIOS2_LO16",
231 FALSE,
232 0x003fffc0,
233 0x003fffc0,
234 FALSE),
235
236 HOWTO (R_NIOS2_HIADJ16,
237 0,
238 2,
239 32,
240 FALSE,
241 6,
242 complain_overflow_dont,
243 nios2_elf32_hiadj16_relocate,
244 "R_NIOS2_HIADJ16",
245 FALSE,
246 0x003fffc0,
247 0x003fffc0,
248 FALSE),
249
250 HOWTO (R_NIOS2_BFD_RELOC_32,
251 0,
252 2, /* long */
253 32,
254 FALSE,
255 0,
256 complain_overflow_dont,
257 bfd_elf_generic_reloc,
258 "R_NIOS2_BFD_RELOC32",
259 FALSE,
260 0xffffffff,
261 0xffffffff,
262 FALSE),
263
264 HOWTO (R_NIOS2_BFD_RELOC_16,
265 0,
266 1, /* short */
267 16,
268 FALSE,
269 0,
270 complain_overflow_bitfield,
271 bfd_elf_generic_reloc,
272 "R_NIOS2_BFD_RELOC16",
273 FALSE,
274 0x0000ffff,
275 0x0000ffff,
276 FALSE),
277
278 HOWTO (R_NIOS2_BFD_RELOC_8,
279 0,
280 0, /* byte */
281 8,
282 FALSE,
283 0,
284 complain_overflow_bitfield,
285 bfd_elf_generic_reloc,
286 "R_NIOS2_BFD_RELOC8",
287 FALSE,
288 0x000000ff,
289 0x000000ff,
290 FALSE),
291
292 HOWTO (R_NIOS2_GPREL,
293 0,
294 2,
295 32,
296 FALSE,
297 6,
298 complain_overflow_dont,
299 nios2_elf32_gprel_relocate,
300 "R_NIOS2_GPREL",
301 FALSE,
302 0x003fffc0,
303 0x003fffc0,
304 FALSE),
305
306 HOWTO (R_NIOS2_GNU_VTINHERIT,
307 0,
308 2, /* short */
309 0,
310 FALSE,
311 0,
312 complain_overflow_dont,
313 NULL,
314 "R_NIOS2_GNU_VTINHERIT",
315 FALSE,
316 0,
317 0,
318 FALSE),
319
320 HOWTO (R_NIOS2_GNU_VTENTRY,
321 0,
322 2, /* byte */
323 0,
324 FALSE,
325 0,
326 complain_overflow_dont,
327 _bfd_elf_rel_vtable_reloc_fn,
328 "R_NIOS2_GNU_VTENTRY",
329 FALSE,
330 0,
331 0,
332 FALSE),
333
334 HOWTO (R_NIOS2_UJMP,
335 0,
336 2,
337 32,
338 FALSE,
339 6,
340 complain_overflow_dont,
341 nios2_elf32_ujmp_relocate,
342 "R_NIOS2_UJMP",
343 FALSE,
344 0x003fffc0,
345 0x003fffc0,
346 FALSE),
347
348 HOWTO (R_NIOS2_CJMP,
349 0,
350 2,
351 32,
352 FALSE,
353 6,
354 complain_overflow_dont,
355 nios2_elf32_cjmp_relocate,
356 "R_NIOS2_CJMP",
357 FALSE,
358 0x003fffc0,
359 0x003fffc0,
360 FALSE),
361
362 HOWTO (R_NIOS2_CALLR,
363 0,
364 2,
365 32,
366 FALSE,
367 6,
368 complain_overflow_dont,
369 nios2_elf32_callr_relocate,
370 "R_NIOS2_CALLR",
371 FALSE,
372 0x003fffc0,
373 0x003fffc0,
374 FALSE),
375
376 HOWTO (R_NIOS2_ALIGN,
377 0,
378 2,
379 0,
380 FALSE,
381 0,
382 complain_overflow_dont,
383 nios2_elf32_ignore_reloc,
384 "R_NIOS2_ALIGN",
385 FALSE,
386 0,
387 0,
388 TRUE),
389
390
391 HOWTO (R_NIOS2_GOT16,
392 0,
393 2,
394 16,
395 FALSE,
396 6,
397 complain_overflow_bitfield,
398 bfd_elf_generic_reloc,
399 "R_NIOS2_GOT16",
400 FALSE,
401 0x003fffc0,
402 0x003fffc0,
403 FALSE),
404
405 HOWTO (R_NIOS2_CALL16,
406 0,
407 2,
408 16,
409 FALSE,
410 6,
411 complain_overflow_bitfield,
412 bfd_elf_generic_reloc,
413 "R_NIOS2_CALL16",
414 FALSE,
415 0x003fffc0,
416 0x003fffc0,
417 FALSE),
418
419 HOWTO (R_NIOS2_GOTOFF_LO,
420 0,
421 2,
422 16,
423 FALSE,
424 6,
425 complain_overflow_dont,
426 bfd_elf_generic_reloc,
427 "R_NIOS2_GOTOFF_LO",
428 FALSE,
429 0x003fffc0,
430 0x003fffc0,
431 FALSE),
432
433 HOWTO (R_NIOS2_GOTOFF_HA,
434 0,
435 2,
436 16,
437 FALSE,
438 6,
439 complain_overflow_dont,
440 bfd_elf_generic_reloc,
441 "R_NIOS2_GOTOFF_HA",
442 FALSE,
443 0x003fffc0,
444 0x003fffc0,
445 FALSE),
446
447 HOWTO (R_NIOS2_PCREL_LO,
448 0,
449 2,
450 16,
451 TRUE,
452 6,
453 complain_overflow_dont,
454 nios2_elf32_pcrel_lo16_relocate,
455 "R_NIOS2_PCREL_LO",
456 FALSE,
457 0x003fffc0,
458 0x003fffc0,
459 TRUE),
460
461 HOWTO (R_NIOS2_PCREL_HA,
462 0,
463 2,
464 16,
465 FALSE, /* This is a PC-relative relocation, but we need to subtract
466 PC ourselves before the HIADJ. */
467 6,
468 complain_overflow_dont,
469 nios2_elf32_pcrel_hiadj16_relocate,
470 "R_NIOS2_PCREL_HA",
471 FALSE,
472 0x003fffc0,
473 0x003fffc0,
474 TRUE),
475
476 HOWTO (R_NIOS2_TLS_GD16,
477 0,
478 2,
479 16,
480 FALSE,
481 6,
482 complain_overflow_bitfield,
483 bfd_elf_generic_reloc,
484 "R_NIOS2_TLS_GD16",
485 FALSE,
486 0x003fffc0,
487 0x003fffc0,
488 FALSE),
489
490 HOWTO (R_NIOS2_TLS_LDM16,
491 0,
492 2,
493 16,
494 FALSE,
495 6,
496 complain_overflow_bitfield,
497 bfd_elf_generic_reloc,
498 "R_NIOS2_TLS_LDM16",
499 FALSE,
500 0x003fffc0,
501 0x003fffc0,
502 FALSE),
503
504 HOWTO (R_NIOS2_TLS_LDO16,
505 0,
506 2,
507 16,
508 FALSE,
509 6,
510 complain_overflow_bitfield,
511 bfd_elf_generic_reloc,
512 "R_NIOS2_TLS_LDO16",
513 FALSE,
514 0x003fffc0,
515 0x003fffc0,
516 FALSE),
517
518 HOWTO (R_NIOS2_TLS_IE16,
519 0,
520 2,
521 16,
522 FALSE,
523 6,
524 complain_overflow_bitfield,
525 bfd_elf_generic_reloc,
526 "R_NIOS2_TLS_IE16",
527 FALSE,
528 0x003fffc0,
529 0x003fffc0,
530 FALSE),
531
532 HOWTO (R_NIOS2_TLS_LE16,
533 0,
534 2,
535 16,
536 FALSE,
537 6,
538 complain_overflow_bitfield,
539 bfd_elf_generic_reloc,
540 "R_NIOS2_TLS_LE16",
541 FALSE,
542 0x003fffc0,
543 0x003fffc0,
544 FALSE),
545
546 HOWTO (R_NIOS2_TLS_DTPMOD,
547 0,
548 2,
549 32,
550 FALSE,
551 0,
552 complain_overflow_dont,
553 bfd_elf_generic_reloc,
554 "R_NIOS2_TLS_DTPMOD",
555 FALSE,
556 0xffffffff,
557 0xffffffff,
558 FALSE),
559
560 HOWTO (R_NIOS2_TLS_DTPREL,
561 0,
562 2,
563 32,
564 FALSE,
565 0,
566 complain_overflow_dont,
567 bfd_elf_generic_reloc,
568 "R_NIOS2_TLS_DTPREL",
569 FALSE,
570 0xffffffff,
571 0xffffffff,
572 FALSE),
573
574 HOWTO (R_NIOS2_TLS_TPREL,
575 0,
576 2,
577 32,
578 FALSE,
579 0,
580 complain_overflow_dont,
581 bfd_elf_generic_reloc,
582 "R_NIOS2_TLS_TPREL",
583 FALSE,
584 0xffffffff,
585 0xffffffff,
586 FALSE),
587
588 HOWTO (R_NIOS2_COPY,
589 0,
590 2,
591 32,
592 FALSE,
593 0,
594 complain_overflow_dont,
595 bfd_elf_generic_reloc,
596 "R_NIOS2_COPY",
597 FALSE,
598 0,
599 0,
600 FALSE),
601
602 HOWTO (R_NIOS2_GLOB_DAT,
603 0,
604 2,
605 32,
606 FALSE,
607 0,
608 complain_overflow_dont,
609 bfd_elf_generic_reloc,
610 "R_NIOS2_GLOB_DAT",
611 FALSE,
612 0xffffffff,
613 0xffffffff,
614 FALSE),
615
616 HOWTO (R_NIOS2_JUMP_SLOT,
617 0,
618 2,
619 32,
620 FALSE,
621 0,
622 complain_overflow_dont,
623 bfd_elf_generic_reloc,
624 "R_NIOS2_JUMP_SLOT",
625 FALSE,
626 0xffffffff,
627 0xffffffff,
628 FALSE),
629
630 HOWTO (R_NIOS2_RELATIVE,
631 0,
632 2,
633 32,
634 FALSE,
635 0,
636 complain_overflow_dont,
637 bfd_elf_generic_reloc,
638 "R_NIOS2_RELATIVE",
639 FALSE,
640 0xffffffff,
641 0xffffffff,
642 FALSE),
643
644 HOWTO (R_NIOS2_GOTOFF,
645 0,
646 2,
647 32,
648 FALSE,
649 0,
650 complain_overflow_dont,
651 bfd_elf_generic_reloc,
652 "R_NIOS2_GOTOFF",
653 FALSE,
654 0xffffffff,
655 0xffffffff,
656 FALSE),
657
658/* Add other relocations here. */
659};
660
661static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
662
663/* Return the howto for relocation RTYPE. */
664static reloc_howto_type *
665lookup_howto (unsigned int rtype)
666{
667 static int initialized = 0;
668 int i;
669 int howto_tbl_size = (int) (sizeof (elf_nios2_howto_table_rel)
670 / sizeof (elf_nios2_howto_table_rel[0]));
671
672 if (!initialized)
673 {
674 initialized = 1;
675 memset (elf_code_to_howto_index, 0xff,
676 sizeof (elf_code_to_howto_index));
677 for (i = 0; i < howto_tbl_size; i++)
678 elf_code_to_howto_index[elf_nios2_howto_table_rel[i].type] = i;
679 }
680
681 BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL);
682 i = elf_code_to_howto_index[rtype];
683 if (i >= howto_tbl_size)
684 return 0;
685 return elf_nios2_howto_table_rel + i;
686}
687
688/* Map for converting BFD reloc types to Nios II reloc types. */
689struct elf_reloc_map
690{
691 bfd_reloc_code_real_type bfd_val;
692 enum elf_nios2_reloc_type elf_val;
693};
694
695static const struct elf_reloc_map nios2_reloc_map[] = {
696 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
697 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
698 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
699 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
700 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
701 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
702 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
703 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
704 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
705 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
706 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
707 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
708 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
709 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
710 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
711 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
712 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
713 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
714 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
715 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
716 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
717 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
718 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
719 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
720 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
721 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
722 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
723 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
724 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
725 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
726 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
727 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
728 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
729 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
730 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
731 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
732 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
733 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
734 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
735 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF}
736};
737
738/* The Nios II linker needs to keep track of the number of relocs that it
739 decides to copy as dynamic relocs in check_relocs for each symbol.
740 This is so that it can later discard them if they are found to be
741 unnecessary. We store the information in a field extending the
742 regular ELF linker hash table. */
743
744struct elf32_nios2_dyn_relocs
745{
746 struct elf32_nios2_dyn_relocs *next;
747
748 /* The input section of the reloc. */
749 asection *sec;
750
751 /* Total number of relocs copied for the input section. */
752 bfd_size_type count;
753
754 /* Number of pc-relative relocs copied for the input section. */
755 bfd_size_type pc_count;
756};
757
758/* Nios II ELF linker hash entry. */
759
760struct elf32_nios2_link_hash_entry
761{
762 struct elf_link_hash_entry root;
763
764 /* Track dynamic relocs copied for this symbol. */
765 struct elf32_nios2_dyn_relocs *dyn_relocs;
766
767#define GOT_UNKNOWN 0
768#define GOT_NORMAL 1
769#define GOT_TLS_GD 2
770#define GOT_TLS_IE 4
771 unsigned char tls_type;
772
773 /* We need to detect and take special action for symbols which are only
774 referenced with %call() and not with %got(). Such symbols do not need
775 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
776 linking will not work if the dynamic GOT reloc exists.
777 To check for this condition efficiently, we compare got_types_used against
778 CALL16_USED, meaning
779 (got_types_used & (GOT16_USED | CALL16_USED)) == CALL16_USED. */
780#define GOT16_USED 1
781#define CALL16_USED 2
782 unsigned char got_types_used;
783};
784
785#define elf32_nios2_hash_entry(ent) \
786 ((struct elf32_nios2_link_hash_entry *) (ent))
787
788/* Get the Nios II elf linker hash table from a link_info structure. */
789#define elf32_nios2_hash_table(info) \
790 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
791
792/* Nios II ELF linker hash table. */
793struct elf32_nios2_link_hash_table
794 {
795 /* The main hash table. */
796 struct elf_link_hash_table root;
797
798 /* Short-cuts to get to dynamic linker sections. */
799 asection *sdynbss;
800 asection *srelbss;
801 asection *sbss;
802
803 union {
804 bfd_signed_vma refcount;
805 bfd_vma offset;
806 } tls_ldm_got;
807
808 /* Small local sym cache. */
809 struct sym_cache sym_cache;
810
811 bfd_vma res_n_size;
812 };
813
814struct nios2_elf32_obj_tdata
815{
816 struct elf_obj_tdata root;
817
818 /* tls_type for each local got entry. */
819 char *local_got_tls_type;
820
821 /* TRUE if TLS GD relocs have been seen for this object. */
822 bfd_boolean has_tlsgd;
823};
824
825#define elf32_nios2_tdata(abfd) \
826 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
827
828#define elf32_nios2_local_got_tls_type(abfd) \
829 (elf32_nios2_tdata (abfd)->local_got_tls_type)
830
831/* The name of the dynamic interpreter. This is put in the .interp
832 section. */
833#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
834
835/* PLT implementation for position-dependent code. */
836static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
837 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
838 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
839 0x7800683a /* jmp r15 */
840};
841
842static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
843 0x03800034, /* movhi r14, %hiadj(res_0) */
844 0x73800004, /* addi r14, r14, %lo(res_0) */
845 0x7b9fc83a, /* sub r15, r15, r14 */
846 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
847 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
848 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
849 0x6800683a /* jmp r13 */
850};
851
852/* PLT implementation for position-independent code. */
853static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
854 0x03c00034, /* movhi r15, %hiadj(index * 4) */
855 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
856 0x00000006 /* br .PLTresolve */
857};
858
859static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
860 0x001ce03a, /* nextpc r14 */
861 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
862 0x6b9b883a, /* add r13, r13, r14 */
863 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
864 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
865 0x6800683a /* jmp r13 */
866};
867
868/* Implement elf_backend_grok_prstatus:
869 Support for core dump NOTE sections. */
870static bfd_boolean
871nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
872{
873 int offset;
874 size_t size;
875
876 switch (note->descsz)
877 {
878 default:
879 return FALSE;
880
881 case 212: /* Linux/Nios II */
882 /* pr_cursig */
228e534f 883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
36591ba1
SL
884
885 /* pr_pid */
228e534f 886 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
36591ba1
SL
887
888 /* pr_reg */
889 offset = 72;
890 size = 136;
891
892 break;
893 }
894
895 /* Make a ".reg/999" section. */
896 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
897 size, note->descpos + offset);
898}
899
900/* Implement elf_backend_grok_psinfo. */
901static bfd_boolean
902nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
903{
904 switch (note->descsz)
905 {
906 default:
907 return FALSE;
908
909 case 124: /* Linux/Nios II elf_prpsinfo */
228e534f 910 elf_tdata (abfd)->core->program
36591ba1 911 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 912 elf_tdata (abfd)->core->command
36591ba1
SL
913 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
914 }
915
916 /* Note that for some reason, a spurious space is tacked
917 onto the end of the args in some (at least one anyway)
918 implementations, so strip it off if it exists. */
919
920 {
228e534f 921 char *command = elf_tdata (abfd)->core->command;
36591ba1
SL
922 int n = strlen (command);
923
924 if (0 < n && command[n - 1] == ' ')
925 command[n - 1] = '\0';
926 }
927
928 return TRUE;
929}
930
931/* Create an entry in a Nios II ELF linker hash table. */
932static struct bfd_hash_entry *
933link_hash_newfunc (struct bfd_hash_entry *entry,
934 struct bfd_hash_table *table, const char *string)
935{
936 /* Allocate the structure if it has not already been allocated by a
937 subclass. */
938 if (entry == NULL)
939 {
940 entry = bfd_hash_allocate (table,
941 sizeof (struct elf32_nios2_link_hash_entry));
942 if (entry == NULL)
943 return entry;
944 }
945
946 /* Call the allocation method of the superclass. */
947 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
948 if (entry)
949 {
950 struct elf32_nios2_link_hash_entry *eh;
951
952 eh = (struct elf32_nios2_link_hash_entry *) entry;
953 eh->dyn_relocs = NULL;
954 eh->tls_type = GOT_UNKNOWN;
955 eh->got_types_used = 0;
956 }
957
958 return entry;
959}
960
961/* Implement bfd_elf32_bfd_reloc_type_lookup:
962 Given a BFD reloc type, return a howto structure. */
963static reloc_howto_type *
964nios2_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
965 bfd_reloc_code_real_type code)
966{
967 int i;
968 for (i = 0;
969 i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
970 ++i)
971 if (nios2_reloc_map[i].bfd_val == code)
972 return &elf_nios2_howto_table_rel[(int) nios2_reloc_map[i].elf_val];
973 return NULL;
974}
975
976/* Implement bfd_elf32_bfd_reloc_name_lookup:
977 Given a reloc name, return a howto structure. */
978static reloc_howto_type *
979nios2_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
980 const char *r_name)
981{
982 unsigned int i;
983 for (i = 0;
984 i < (sizeof (elf_nios2_howto_table_rel)
985 / sizeof (elf_nios2_howto_table_rel[0]));
986 i++)
987 if (elf_nios2_howto_table_rel[i].name
988 && strcasecmp (elf_nios2_howto_table_rel[i].name, r_name) == 0)
989 return &elf_nios2_howto_table_rel[i];
990
991 return NULL;
992}
993
994/* Implement elf_info_to_howto:
995 Given a ELF32 relocation, fill in a arelent structure. */
996static void
997nios2_elf32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
998 Elf_Internal_Rela *dst)
999{
1000 unsigned int r_type;
1001
1002 r_type = ELF32_R_TYPE (dst->r_info);
1003 BFD_ASSERT (r_type < R_NIOS2_ILLEGAL);
1004 cache_ptr->howto = &elf_nios2_howto_table_rel[r_type];
1005}
1006
1007/* Return the base VMA address which should be subtracted from real addresses
1008 when resolving @dtpoff relocation.
1009 This is PT_TLS segment p_vaddr. */
1010static bfd_vma
1011dtpoff_base (struct bfd_link_info *info)
1012{
1013 /* If tls_sec is NULL, we should have signalled an error already. */
1014 if (elf_hash_table (info)->tls_sec == NULL)
1015 return 0;
1016 return elf_hash_table (info)->tls_sec->vma;
1017}
1018
1019/* Return the relocation value for @tpoff relocation
1020 if STT_TLS virtual address is ADDRESS. */
1021static bfd_vma
1022tpoff (struct bfd_link_info *info, bfd_vma address)
1023{
1024 struct elf_link_hash_table *htab = elf_hash_table (info);
1025
1026 /* If tls_sec is NULL, we should have signalled an error already. */
1027 if (htab->tls_sec == NULL)
1028 return 0;
1029 return address - htab->tls_sec->vma;
1030}
1031
1032/* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
1033 dangerous relocation. */
1034static bfd_boolean
1035nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
1036{
1037
1038 bfd_boolean gp_found;
1039 struct bfd_hash_entry *h;
1040 struct bfd_link_hash_entry *lh;
1041
1042 /* If we've already figured out what GP will be, just return it. */
1043 *pgp = _bfd_get_gp_value (output_bfd);
1044 if (*pgp)
1045 return TRUE;
1046
1047 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
1048 lh = (struct bfd_link_hash_entry *) h;
1049lookup:
1050 if (lh)
1051 {
1052 switch (lh->type)
1053 {
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 case bfd_link_hash_common:
1057 gp_found = FALSE;
1058 break;
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1061 gp_found = TRUE;
1062 *pgp = lh->u.def.value;
1063 break;
1064 case bfd_link_hash_indirect:
1065 case bfd_link_hash_warning:
1066 lh = lh->u.i.link;
1067 /* @@FIXME ignoring warning for now */
1068 goto lookup;
1069 case bfd_link_hash_new:
1070 default:
1071 abort ();
1072 }
1073 }
1074 else
1075 gp_found = FALSE;
1076
1077 if (!gp_found)
1078 {
1079 /* Only get the error once. */
1080 *pgp = 4;
1081 _bfd_set_gp_value (output_bfd, *pgp);
1082 return FALSE;
1083 }
1084
1085 _bfd_set_gp_value (output_bfd, *pgp);
1086
1087 return TRUE;
1088}
1089
1090/* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
1091 if it's not available as we don't have a link_info pointer available here
1092 to look it up in the output symbol table. We don't need to adjust the
1093 symbol value for an external symbol if we are producing relocatable
1094 output. */
1095static bfd_reloc_status_type
1096nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
1097 char **error_message, bfd_vma *pgp)
1098{
1099 if (bfd_is_und_section (symbol->section) && !relocatable)
1100 {
1101 *pgp = 0;
1102 return bfd_reloc_undefined;
1103 }
1104
1105 *pgp = _bfd_get_gp_value (output_bfd);
1106 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
1107 {
1108 if (relocatable)
1109 {
1110 /* Make up a value. */
1111 *pgp = symbol->section->output_section->vma + 0x4000;
1112 _bfd_set_gp_value (output_bfd, *pgp);
1113 }
1114 else
1115 {
1116 *error_message
1117 = (char *) _("global pointer relative relocation when _gp not defined");
1118 return bfd_reloc_dangerous;
1119 }
1120 }
1121
1122 return bfd_reloc_ok;
1123}
1124
1125/* The usual way of loading a 32-bit constant into a Nios II register is to
1126 load the high 16 bits in one instruction and then add the low 16 bits with
1127 a signed add. This means that the high halfword needs to be adjusted to
1128 compensate for the sign bit of the low halfword. This function returns the
1129 adjusted high halfword for a given 32-bit constant. */
1130static
1131bfd_vma hiadj (bfd_vma symbol_value)
1132{
1133 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1134}
1135
1136/* Do the relocations that require special handling. */
1137static bfd_reloc_status_type
1138nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
1139 asection *input_section ATTRIBUTE_UNUSED,
1140 bfd_byte *data, bfd_vma offset,
1141 bfd_vma symbol_value, bfd_vma addend)
1142{
1143 symbol_value = symbol_value + addend;
1144 addend = 0;
1145 symbol_value = (symbol_value >> 16) & 0xffff;
1146 return _bfd_final_link_relocate (howto, abfd, input_section,
1147 data, offset, symbol_value, addend);
1148}
1149
1150static bfd_reloc_status_type
1151nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
1152 asection *input_section ATTRIBUTE_UNUSED,
1153 bfd_byte *data, bfd_vma offset,
1154 bfd_vma symbol_value, bfd_vma addend)
1155{
1156 symbol_value = symbol_value + addend;
1157 addend = 0;
1158 symbol_value = symbol_value & 0xffff;
1159 return _bfd_final_link_relocate (howto, abfd, input_section,
1160 data, offset, symbol_value, addend);
1161}
1162
1163static bfd_reloc_status_type
1164nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
1165 asection *input_section ATTRIBUTE_UNUSED,
1166 bfd_byte *data, bfd_vma offset,
1167 bfd_vma symbol_value, bfd_vma addend)
1168{
1169 symbol_value = symbol_value + addend;
1170 addend = 0;
1171 symbol_value = hiadj(symbol_value);
1172 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1173 symbol_value, addend);
1174}
1175
1176static bfd_reloc_status_type
1177nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
1178 asection *input_section ATTRIBUTE_UNUSED,
1179 bfd_byte *data, bfd_vma offset,
1180 bfd_vma symbol_value, bfd_vma addend)
1181{
1182 symbol_value = symbol_value + addend;
1183 addend = 0;
1184 symbol_value = symbol_value & 0xffff;
1185 return _bfd_final_link_relocate (howto, abfd, input_section,
1186 data, offset, symbol_value, addend);
1187}
1188
1189static bfd_reloc_status_type
1190nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
1191 asection *input_section
1192 ATTRIBUTE_UNUSED,
1193 bfd_byte *data, bfd_vma offset,
1194 bfd_vma symbol_value, bfd_vma addend)
1195{
1196 symbol_value = symbol_value + addend;
1197 symbol_value -= (input_section->output_section->vma
1198 + input_section->output_offset);
1199 symbol_value -= offset;
1200 addend = 0;
1201 symbol_value = hiadj(symbol_value);
1202 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1203 symbol_value, addend);
1204}
1205
1206static bfd_reloc_status_type
1207nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
1208 asection *input_section ATTRIBUTE_UNUSED,
1209 bfd_byte *data, bfd_vma offset,
1210 bfd_vma symbol_value, bfd_vma addend)
1211{
1212 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
1213 so we need to subtract 4 before doing a final_link_relocate. */
1214 symbol_value = symbol_value + addend - 4;
1215 addend = 0;
1216 return _bfd_final_link_relocate (howto, abfd, input_section,
1217 data, offset, symbol_value, addend);
1218}
1219
1220static bfd_reloc_status_type
1221nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
1222 asection *input_section ATTRIBUTE_UNUSED,
1223 bfd_byte *data, bfd_vma offset,
1224 bfd_vma symbol_value, bfd_vma addend)
1225{
1226 /* Check that the relocation is in the same page as the current address. */
1227 if (((symbol_value + addend) & 0xf0000000)
1228 != ((input_section->output_section->vma + offset) & 0xf0000000))
1229 return bfd_reloc_overflow;
1230
1231 return _bfd_final_link_relocate (howto, abfd, input_section,
1232 data, offset, symbol_value, addend);
1233}
1234
1235static bfd_reloc_status_type
1236nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
1237 asection *input_section ATTRIBUTE_UNUSED,
1238 bfd_byte *data, bfd_vma offset,
1239 bfd_vma symbol_value, bfd_vma addend)
1240{
1241 /* Because we need the output_bfd, the special handling is done
1242 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
1243 return _bfd_final_link_relocate (howto, abfd, input_section,
1244 data, offset, symbol_value, addend);
1245}
1246
1247static bfd_reloc_status_type
1248nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
1249 asection *input_section ATTRIBUTE_UNUSED,
1250 bfd_byte *data, bfd_vma offset,
1251 bfd_vma symbol_value, bfd_vma addend)
1252{
1253 bfd_vma symbol_lo16, symbol_hi16;
1254 bfd_reloc_status_type r;
1255 symbol_value = symbol_value + addend;
1256 addend = 0;
1257 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1258 symbol_lo16 = symbol_value & 0xffff;
1259
1260 r = _bfd_final_link_relocate (howto, abfd, input_section,
1261 data, offset, symbol_hi16, addend);
1262
1263 if (r == bfd_reloc_ok)
1264 return _bfd_final_link_relocate (howto, abfd, input_section,
1265 data, offset + 4, symbol_lo16, addend);
1266
1267 return r;
1268}
1269
1270static bfd_reloc_status_type
1271nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
1272 asection *input_section ATTRIBUTE_UNUSED,
1273 bfd_byte *data, bfd_vma offset,
1274 bfd_vma symbol_value, bfd_vma addend)
1275{
1276 bfd_vma symbol_lo16, symbol_hi16;
1277 bfd_reloc_status_type r;
1278 symbol_value = symbol_value + addend;
1279 addend = 0;
1280 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1281 symbol_lo16 = symbol_value & 0xffff;
1282
1283 r = _bfd_final_link_relocate (howto, abfd, input_section,
1284 data, offset, symbol_hi16, addend);
1285
1286 if (r == bfd_reloc_ok)
1287 return _bfd_final_link_relocate (howto, abfd, input_section,
1288 data, offset + 4, symbol_lo16, addend);
1289
1290 return r;
1291}
1292
1293static bfd_reloc_status_type
1294nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
1295 asection *input_section ATTRIBUTE_UNUSED,
1296 bfd_byte *data, bfd_vma offset,
1297 bfd_vma symbol_value, bfd_vma addend)
1298{
1299 bfd_vma symbol_lo16, symbol_hi16;
1300 bfd_reloc_status_type r;
1301 symbol_value = symbol_value + addend;
1302 addend = 0;
1303 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1304 symbol_lo16 = symbol_value & 0xffff;
1305
1306 r = _bfd_final_link_relocate (howto, abfd, input_section,
1307 data, offset, symbol_hi16, addend);
1308
1309 if (r == bfd_reloc_ok)
1310 return _bfd_final_link_relocate (howto, abfd, input_section,
1311 data, offset + 4, symbol_lo16, addend);
1312
1313 return r;
1314}
1315
1316/* HOWTO handlers for relocations that require special handling. */
1317
1318/* This is for relocations used only when relaxing to ensure
1319 changes in size of section don't screw up .align. */
1320static bfd_reloc_status_type
1321nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1322 asymbol *symbol ATTRIBUTE_UNUSED,
1323 void *data ATTRIBUTE_UNUSED, asection *input_section,
1324 bfd *output_bfd,
1325 char **error_message ATTRIBUTE_UNUSED)
1326{
1327 if (output_bfd != NULL)
1328 reloc_entry->address += input_section->output_offset;
1329 return bfd_reloc_ok;
1330}
1331
1332static bfd_reloc_status_type
1333nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1334 void *data, asection *input_section,
1335 bfd *output_bfd,
1336 char **error_message ATTRIBUTE_UNUSED)
1337{
1338 /* This part is from bfd_elf_generic_reloc. */
1339 if (output_bfd != NULL
1340 && (symbol->flags & BSF_SECTION_SYM) == 0
1341 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1342 {
1343 reloc_entry->address += input_section->output_offset;
1344 return bfd_reloc_ok;
1345 }
1346
1347 if (output_bfd != NULL)
1348 /* FIXME: See bfd_perform_relocation. Is this right? */
1349 return bfd_reloc_continue;
1350
1351 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
1352 input_section,
1353 data, reloc_entry->address,
1354 (symbol->value
1355 + symbol->section->output_section->vma
1356 + symbol->section->output_offset),
1357 reloc_entry->addend);
1358}
1359
1360static bfd_reloc_status_type
1361nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1362 void *data, asection *input_section,
1363 bfd *output_bfd,
1364 char **error_message ATTRIBUTE_UNUSED)
1365{
1366 /* This part is from bfd_elf_generic_reloc. */
1367 if (output_bfd != NULL
1368 && (symbol->flags & BSF_SECTION_SYM) == 0
1369 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1370 {
1371 reloc_entry->address += input_section->output_offset;
1372 return bfd_reloc_ok;
1373 }
1374
1375 if (output_bfd != NULL)
1376 /* FIXME: See bfd_perform_relocation. Is this right? */
1377 return bfd_reloc_continue;
1378
1379 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
1380 input_section,
1381 data, reloc_entry->address,
1382 (symbol->value
1383 + symbol->section->output_section->vma
1384 + symbol->section->output_offset),
1385 reloc_entry->addend);
1386}
1387
1388static bfd_reloc_status_type
1389nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1390 void *data, asection *input_section,
1391 bfd *output_bfd,
1392 char **error_message ATTRIBUTE_UNUSED)
1393{
1394 /* This part is from bfd_elf_generic_reloc. */
1395 if (output_bfd != NULL
1396 && (symbol->flags & BSF_SECTION_SYM) == 0
1397 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1398 {
1399 reloc_entry->address += input_section->output_offset;
1400 return bfd_reloc_ok;
1401 }
1402
1403 if (output_bfd != NULL)
1404 /* FIXME: See bfd_perform_relocation. Is this right? */
1405 return bfd_reloc_continue;
1406
1407 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
1408 input_section,
1409 data, reloc_entry->address,
1410 (symbol->value
1411 + symbol->section->output_section->vma
1412 + symbol->section->output_offset),
1413 reloc_entry->addend);
1414}
1415
1416static bfd_reloc_status_type
1417nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
1418 asymbol *symbol, void *data,
1419 asection *input_section, bfd *output_bfd,
1420 char **error_message ATTRIBUTE_UNUSED)
1421{
1422 /* This part is from bfd_elf_generic_reloc. */
1423 if (output_bfd != NULL
1424 && (symbol->flags & BSF_SECTION_SYM) == 0
1425 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1426 {
1427 reloc_entry->address += input_section->output_offset;
1428 return bfd_reloc_ok;
1429 }
1430
1431 if (output_bfd != NULL)
1432 /* FIXME: See bfd_perform_relocation. Is this right? */
1433 return bfd_reloc_continue;
1434
1435 return nios2_elf32_do_pcrel_lo16_relocate (
1436 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1437 (symbol->value + symbol->section->output_section->vma
1438 + symbol->section->output_offset),
1439 reloc_entry->addend);
1440}
1441
1442static bfd_reloc_status_type
1443nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
1444 asymbol *symbol, void *data,
1445 asection *input_section, bfd *output_bfd,
1446 char **error_message ATTRIBUTE_UNUSED)
1447{
1448 /* This part is from bfd_elf_generic_reloc. */
1449 if (output_bfd != NULL
1450 && (symbol->flags & BSF_SECTION_SYM) == 0
1451 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1452 {
1453 reloc_entry->address += input_section->output_offset;
1454 return bfd_reloc_ok;
1455 }
1456
1457 if (output_bfd != NULL)
1458 /* FIXME: See bfd_perform_relocation. Is this right? */
1459 return bfd_reloc_continue;
1460
1461 return nios2_elf32_do_pcrel_hiadj16_relocate (
1462 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1463 (symbol->value + symbol->section->output_section->vma
1464 + symbol->section->output_offset),
1465 reloc_entry->addend);
1466}
1467
1468static bfd_reloc_status_type
1469nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1470 void *data, asection *input_section,
1471 bfd *output_bfd,
1472 char **error_message ATTRIBUTE_UNUSED)
1473{
1474 /* This part is from bfd_elf_generic_reloc. */
1475 if (output_bfd != NULL
1476 && (symbol->flags & BSF_SECTION_SYM) == 0
1477 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1478 {
1479 reloc_entry->address += input_section->output_offset;
1480 return bfd_reloc_ok;
1481 }
1482
1483 if (output_bfd != NULL)
1484 /* FIXME: See bfd_perform_relocation. Is this right? */
1485 return bfd_reloc_continue;
1486
1487 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
1488 input_section,
1489 data, reloc_entry->address,
1490 (symbol->value
1491 + symbol->section->output_section->vma
1492 + symbol->section->output_offset),
1493 reloc_entry->addend);
1494}
1495
1496static bfd_reloc_status_type
1497nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1498 void *data, asection *input_section,
1499 bfd *output_bfd,
1500 char **error_message ATTRIBUTE_UNUSED)
1501{
1502 /* This part is from bfd_elf_generic_reloc. */
1503 if (output_bfd != NULL
1504 && (symbol->flags & BSF_SECTION_SYM) == 0
1505 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1506 {
1507 reloc_entry->address += input_section->output_offset;
1508 return bfd_reloc_ok;
1509 }
1510
1511 if (output_bfd != NULL)
1512 /* FIXME: See bfd_perform_relocation. Is this right? */
1513 return bfd_reloc_continue;
1514
1515 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
1516 input_section,
1517 data, reloc_entry->address,
1518 (symbol->value
1519 + symbol->section->output_section->vma
1520 + symbol->section->output_offset),
1521 reloc_entry->addend);
1522}
1523
1524static bfd_reloc_status_type
1525nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1526 void *data, asection *input_section,
1527 bfd *output_bfd, char **msg)
1528{
1529 bfd_vma relocation;
1530 bfd_vma gp;
1531 bfd_reloc_status_type r;
1532
1533
1534 /* This part is from bfd_elf_generic_reloc. */
1535 if (output_bfd != NULL
1536 && (symbol->flags & BSF_SECTION_SYM) == 0
1537 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1538 {
1539 reloc_entry->address += input_section->output_offset;
1540 return bfd_reloc_ok;
1541 }
1542
1543 if (output_bfd != NULL)
1544 /* FIXME: See bfd_perform_relocation. Is this right? */
1545 return bfd_reloc_continue;
1546
1547 relocation = (symbol->value
1548 + symbol->section->output_section->vma
1549 + symbol->section->output_offset);
1550
1551 /* This assumes we've already cached the _gp symbol. */
1552 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
1553 if (r == bfd_reloc_ok)
1554 {
1555 relocation = relocation + reloc_entry->addend - gp;
1556 reloc_entry->addend = 0;
1557 if ((signed) relocation < -32768 || (signed) relocation > 32767)
1558 {
1559 *msg = _("global pointer relative address out of range");
1560 r = bfd_reloc_outofrange;
1561 }
1562 else
1563 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
1564 input_section,
1565 data, reloc_entry->address,
1566 relocation, reloc_entry->addend);
1567 }
1568
1569 return r;
1570}
1571
1572static bfd_reloc_status_type
1573nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1574 void *data, asection *input_section,
1575 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1576{
1577 /* This part is from bfd_elf_generic_reloc. */
1578 if (output_bfd != NULL
1579 && (symbol->flags & BSF_SECTION_SYM) == 0
1580 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1581 {
1582 reloc_entry->address += input_section->output_offset;
1583 return bfd_reloc_ok;
1584 }
1585
1586 if (output_bfd != NULL)
1587 /* FIXME: See bfd_perform_relocation. Is this right? */
1588 return bfd_reloc_continue;
1589
1590 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
1591 input_section,
1592 data, reloc_entry->address,
1593 (symbol->value
1594 + symbol->section->output_section->vma
1595 + symbol->section->output_offset),
1596 reloc_entry->addend);
1597}
1598
1599static bfd_reloc_status_type
1600nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1601 void *data, asection *input_section,
1602 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1603{
1604 /* This part is from bfd_elf_generic_reloc. */
1605 if (output_bfd != NULL
1606 && (symbol->flags & BSF_SECTION_SYM) == 0
1607 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1608 {
1609 reloc_entry->address += input_section->output_offset;
1610 return bfd_reloc_ok;
1611 }
1612
1613 if (output_bfd != NULL)
1614 /* FIXME: See bfd_perform_relocation. Is this right? */
1615 return bfd_reloc_continue;
1616
1617 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
1618 input_section,
1619 data, reloc_entry->address,
1620 (symbol->value
1621 + symbol->section->output_section->vma
1622 + symbol->section->output_offset),
1623 reloc_entry->addend);
1624}
1625
1626static bfd_reloc_status_type
1627nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1628 void *data, asection *input_section,
1629 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1630{
1631 /* This part is from bfd_elf_generic_reloc. */
1632 if (output_bfd != NULL
1633 && (symbol->flags & BSF_SECTION_SYM) == 0
1634 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1635 {
1636 reloc_entry->address += input_section->output_offset;
1637 return bfd_reloc_ok;
1638 }
1639
1640 if (output_bfd != NULL)
1641 /* FIXME: See bfd_perform_relocation. Is this right? */
1642 return bfd_reloc_continue;
1643
1644 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
1645 input_section,
1646 data, reloc_entry->address,
1647 (symbol->value
1648 + symbol->section->output_section->vma
1649 + symbol->section->output_offset),
1650 reloc_entry->addend);
1651}
1652
1653
1654/* Implement elf_backend_relocate_section. */
1655static bfd_boolean
1656nios2_elf32_relocate_section (bfd *output_bfd,
1657 struct bfd_link_info *info,
1658 bfd *input_bfd,
1659 asection *input_section,
1660 bfd_byte *contents,
1661 Elf_Internal_Rela *relocs,
1662 Elf_Internal_Sym *local_syms,
1663 asection **local_sections)
1664{
1665 Elf_Internal_Shdr *symtab_hdr;
1666 struct elf_link_hash_entry **sym_hashes;
1667 Elf_Internal_Rela *rel;
1668 Elf_Internal_Rela *relend;
1669 struct elf32_nios2_link_hash_table *htab;
1670 asection *sgot;
1671 asection *splt;
1672 asection *sreloc = NULL;
1673 bfd_vma *local_got_offsets;
1674
1675 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1676 sym_hashes = elf_sym_hashes (input_bfd);
1677 relend = relocs + input_section->reloc_count;
1678
1679 htab = elf32_nios2_hash_table (info);
1680 sgot = htab->root.sgot;
1681 splt = htab->root.splt;
1682 local_got_offsets = elf_local_got_offsets (input_bfd);
1683
1684 for (rel = relocs; rel < relend; rel++)
1685 {
1686 reloc_howto_type *howto;
1687 unsigned long r_symndx;
1688 Elf_Internal_Sym *sym;
1689 asection *sec;
1690 struct elf_link_hash_entry *h;
1691 struct elf32_nios2_link_hash_entry *eh;
1692 bfd_vma relocation;
1693 bfd_vma gp;
1694 bfd_vma reloc_address;
1695 bfd_reloc_status_type r = bfd_reloc_ok;
1696 const char *name = NULL;
1697 int r_type;
1698 const char *format;
1699 char msgbuf[256];
1700 const char* msg = (const char*) NULL;
1701 bfd_boolean unresolved_reloc;
1702 bfd_vma off;
1703 int use_plt;
1704
1705 r_type = ELF32_R_TYPE (rel->r_info);
1706 r_symndx = ELF32_R_SYM (rel->r_info);
1707
1708 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
1709 h = NULL;
1710 sym = NULL;
1711 sec = NULL;
1712
1713 if (r_symndx < symtab_hdr->sh_info)
1714 {
1715 sym = local_syms + r_symndx;
1716 sec = local_sections[r_symndx];
1717 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1718 }
1719 else
1720 {
1721 bfd_boolean warned;
1722
1723 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1724 r_symndx, symtab_hdr, sym_hashes,
1725 h, sec, relocation,
1726 unresolved_reloc, warned);
1727 }
1728
1729 if (sec && discarded_section (sec))
1730 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1731 rel, 1, relend, howto, 0, contents);
1732
1733 /* Nothing more to do unless this is a final link. */
1734 if (info->relocatable)
1735 continue;
1736
1737 if (sec && sec->output_section)
1738 reloc_address = (sec->output_section->vma + sec->output_offset
1739 + rel->r_offset);
1740 else
1741 reloc_address = 0;
1742
1743 if (howto)
1744 {
1745 switch (howto->type)
1746 {
1747 case R_NIOS2_HI16:
1748 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
1749 input_section,
1750 contents, rel->r_offset,
1751 relocation, rel->r_addend);
1752 break;
1753 case R_NIOS2_LO16:
1754 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
1755 input_section,
1756 contents, rel->r_offset,
1757 relocation, rel->r_addend);
1758 break;
1759 case R_NIOS2_PCREL_LO:
1760 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
1761 input_section,
1762 contents,
1763 rel->r_offset,
1764 relocation,
1765 rel->r_addend);
1766 break;
1767 case R_NIOS2_HIADJ16:
1768 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
1769 input_section, contents,
1770 rel->r_offset, relocation,
1771 rel->r_addend);
1772 break;
1773 case R_NIOS2_PCREL_HA:
1774 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
1775 input_section,
1776 contents,
1777 rel->r_offset,
1778 relocation,
1779 rel->r_addend);
1780 break;
1781 case R_NIOS2_PCREL16:
1782 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
1783 input_section, contents,
1784 rel->r_offset, relocation,
1785 rel->r_addend);
1786 break;
1787 case R_NIOS2_GPREL:
1788 /* Turns an absolute address into a gp-relative address. */
1789 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
1790 {
1791 format = _("global pointer relative relocation at address "
1792 "0x%08x when _gp not defined\n");
1793 sprintf (msgbuf, format, reloc_address);
1794 msg = msgbuf;
1795 r = bfd_reloc_dangerous;
1796 }
1797 else
1798 {
1799 bfd_vma symbol_address = rel->r_addend + relocation;
1800 relocation = relocation + rel->r_addend - gp;
1801 rel->r_addend = 0;
1802 if (((signed) relocation < -32768
1803 || (signed) relocation > 32767)
1804 && (!h
1805 || h->root.type == bfd_link_hash_defined
1806 || h->root.type == bfd_link_hash_defweak))
1807 {
1808 format = _("Unable to reach %s (at 0x%08x) from the "
1809 "global pointer (at 0x%08x) because the "
1810 "offset (%d) is out of the allowed range, "
1811 "-32678 to 32767.\n" );
1812 sprintf (msgbuf, format, name, symbol_address, gp,
1813 (signed)relocation);
1814 msg = msgbuf;
1815 r = bfd_reloc_outofrange;
1816 }
1817 else
1818 r = _bfd_final_link_relocate (howto, input_bfd,
1819 input_section, contents,
1820 rel->r_offset, relocation,
1821 rel->r_addend);
1822 }
1823
1824 break;
1825 case R_NIOS2_UJMP:
1826 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
1827 input_section,
1828 contents, rel->r_offset,
1829 relocation, rel->r_addend);
1830 break;
1831 case R_NIOS2_CJMP:
1832 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
1833 input_section,
1834 contents, rel->r_offset,
1835 relocation, rel->r_addend);
1836 break;
1837 case R_NIOS2_CALLR:
1838 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
1839 input_section, contents,
1840 rel->r_offset, relocation,
1841 rel->r_addend);
1842 break;
1843 case R_NIOS2_CALL26:
1844 /* If we have a call to an undefined weak symbol, we just want
1845 to stuff a zero in the bits of the call instruction and
1846 bypass the normal call26 relocation handling, because it'll
1847 diagnose an overflow error if address 0 isn't in the same
1848 256MB segment as the call site. Presumably the call
1849 should be guarded by a null check anyway. */
1850 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
1851 {
1852 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
1853 r = _bfd_final_link_relocate (howto, input_bfd,
1854 input_section, contents,
1855 rel->r_offset, relocation,
1856 rel->r_addend);
1857 break;
1858 }
1859 /* Handle relocations which should use the PLT entry.
1860 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
1861 which may point to a PLT entry, but we don't need to handle
1862 that here. If we created a PLT entry, all branches in this
1863 object should go to it. */
1864 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
1865 {
1866 /* If we've created a .plt section, and assigned a PLT entry
1867 to this function, it should not be known to bind locally.
1868 If it were, we would have cleared the PLT entry. */
1869 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1870
1871 relocation = (splt->output_section->vma
1872 + splt->output_offset
1873 + h->plt.offset);
1874
1875 unresolved_reloc = FALSE;
1876 }
1877 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
1878 input_section, contents,
1879 rel->r_offset, relocation,
1880 rel->r_addend);
1881 break;
1882 case R_NIOS2_ALIGN:
1883 r = bfd_reloc_ok;
1884 /* For symmetry this would be
1885 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
1886 input_section, contents,
1887 rel->r_offset, relocation,
1888 rel->r_addend);
1889 but do_ignore_reloc would do no more than return
1890 bfd_reloc_ok. */
1891 break;
1892
1893 case R_NIOS2_GOT16:
1894 case R_NIOS2_CALL16:
1895 /* Relocation is to the entry for this symbol in the
1896 global offset table. */
1897 if (sgot == NULL)
1898 {
1899 r = bfd_reloc_notsupported;
1900 break;
1901 }
1902
1903 use_plt = 0;
1904
1905 if (h != NULL)
1906 {
1907 bfd_boolean dyn;
1908
1909 eh = (struct elf32_nios2_link_hash_entry *)h;
1910 use_plt = (eh->got_types_used == CALL16_USED
1911 && h->plt.offset != (bfd_vma) -1);
1912
1913 off = h->got.offset;
1914 BFD_ASSERT (off != (bfd_vma) -1);
1915 dyn = elf_hash_table (info)->dynamic_sections_created;
1916 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1917 || (info->shared
1918 && SYMBOL_REFERENCES_LOCAL (info, h))
1919 || (ELF_ST_VISIBILITY (h->other)
1920 && h->root.type == bfd_link_hash_undefweak))
1921 {
1922 /* This is actually a static link, or it is a -Bsymbolic
1923 link and the symbol is defined locally. We must
1924 initialize this entry in the global offset table.
1925 Since the offset must always be a multiple of 4, we
1926 use the least significant bit to record whether we
1927 have initialized it already.
1928
1929 When doing a dynamic link, we create a .rela.got
1930 relocation entry to initialize the value. This is
1931 done in the finish_dynamic_symbol routine. */
1932 if ((off & 1) != 0)
1933 off &= ~1;
1934 else
1935 {
1936 bfd_put_32 (output_bfd, relocation,
1937 sgot->contents + off);
1938 h->got.offset |= 1;
1939 }
1940 }
1941 else
1942 unresolved_reloc = FALSE;
1943 }
1944 else
1945 {
1946 BFD_ASSERT (local_got_offsets != NULL
1947 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1948
1949 off = local_got_offsets[r_symndx];
1950
1951 /* The offset must always be a multiple of 4. We use the
1952 least significant bit to record whether we have already
1953 generated the necessary reloc. */
1954 if ((off & 1) != 0)
1955 off &= ~1;
1956 else
1957 {
1958 bfd_put_32 (output_bfd, relocation,
1959 sgot->contents + off);
1960
1961 if (info->shared)
1962 {
1963 asection *srelgot;
1964 Elf_Internal_Rela outrel;
1965 bfd_byte *loc;
1966
1967 srelgot = htab->root.srelgot;
1968 BFD_ASSERT (srelgot != NULL);
1969
1970 outrel.r_addend = relocation;
1971 outrel.r_offset = (sgot->output_section->vma
1972 + sgot->output_offset
1973 + off);
1974 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
1975 loc = srelgot->contents;
1976 loc += (srelgot->reloc_count++ *
1977 sizeof (Elf32_External_Rela));
1978 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1979 }
1980
1981 local_got_offsets[r_symndx] |= 1;
1982 }
1983 }
1984
1985 if (use_plt && info->shared)
1986 {
1987 off = ((h->plt.offset - 24) / 12 + 3) * 4;
1988 relocation = htab->root.sgotplt->output_offset + off;
1989 }
1990 else
1991 relocation = sgot->output_offset + off;
1992
1993 /* This relocation does not use the addend. */
1994 rel->r_addend = 0;
1995
1996 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1997 contents, rel->r_offset,
1998 relocation, rel->r_addend);
1999 break;
2000
2001 case R_NIOS2_GOTOFF_LO:
2002 case R_NIOS2_GOTOFF_HA:
2003 case R_NIOS2_GOTOFF:
2004 /* Relocation is relative to the start of the
2005 global offset table. */
2006
2007 BFD_ASSERT (sgot != NULL);
2008 if (sgot == NULL)
2009 {
2010 r = bfd_reloc_notsupported;
2011 break;
2012 }
2013
2014 /* Note that sgot->output_offset is not involved in this
2015 calculation. We always want the start of .got. If we
2016 define _GLOBAL_OFFSET_TABLE in a different way, as is
2017 permitted by the ABI, we might have to change this
2018 calculation. */
2019 relocation -= sgot->output_section->vma;
2020 switch (howto->type)
2021 {
2022 case R_NIOS2_GOTOFF_LO:
2023 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
2024 input_section, contents,
2025 rel->r_offset, relocation,
2026 rel->r_addend);
2027 break;
2028 case R_NIOS2_GOTOFF_HA:
2029 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
2030 input_section, contents,
2031 rel->r_offset,
2032 relocation,
2033 rel->r_addend);
2034 break;
2035 default:
2036 r = _bfd_final_link_relocate (howto, input_bfd,
2037 input_section, contents,
2038 rel->r_offset, relocation,
2039 rel->r_addend);
2040 break;
2041 }
2042 break;
2043
2044 case R_NIOS2_TLS_LDO16:
2045 relocation -= dtpoff_base (info) + DTP_OFFSET;
2046
2047 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2048 contents, rel->r_offset,
2049 relocation, rel->r_addend);
2050 break;
2051 case R_NIOS2_TLS_LDM16:
2052 if (htab->root.sgot == NULL)
2053 abort ();
2054
2055 off = htab->tls_ldm_got.offset;
2056
2057 if ((off & 1) != 0)
2058 off &= ~1;
2059 else
2060 {
2061 /* If we don't know the module number, create a relocation
2062 for it. */
2063 if (info->shared)
2064 {
2065 Elf_Internal_Rela outrel;
2066 bfd_byte *loc;
2067
2068 if (htab->root.srelgot == NULL)
2069 abort ();
2070
2071 outrel.r_addend = 0;
2072 outrel.r_offset = (htab->root.sgot->output_section->vma
2073 + htab->root.sgot->output_offset
2074 + off);
2075 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
2076
2077 loc = htab->root.srelgot->contents;
2078 loc += (htab->root.srelgot->reloc_count++
2079 * sizeof (Elf32_External_Rela));
2080 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2081 }
2082 else
2083 bfd_put_32 (output_bfd, 1,
2084 htab->root.sgot->contents + off);
2085
2086 htab->tls_ldm_got.offset |= 1;
2087 }
2088
2089 relocation = (htab->root.sgot->output_offset + off);
2090
2091 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2092 contents, rel->r_offset,
2093 relocation, rel->r_addend);
2094
2095 break;
2096 case R_NIOS2_TLS_GD16:
2097 case R_NIOS2_TLS_IE16:
2098 {
2099 int indx;
2100 char tls_type;
2101
2102 if (htab->root.sgot == NULL)
2103 abort ();
2104
2105 indx = 0;
2106 if (h != NULL)
2107 {
2108 bfd_boolean dyn;
2109 dyn = htab->root.dynamic_sections_created;
2110 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2111 && (!info->shared
2112 || !SYMBOL_REFERENCES_LOCAL (info, h)))
2113 {
2114 unresolved_reloc = FALSE;
2115 indx = h->dynindx;
2116 }
2117 off = h->got.offset;
2118 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
2119 ->tls_type);
2120 }
2121 else
2122 {
2123 if (local_got_offsets == NULL)
2124 abort ();
2125 off = local_got_offsets[r_symndx];
2126 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
2127 [r_symndx]);
2128 }
2129
2130 if (tls_type == GOT_UNKNOWN)
2131 abort ();
2132
2133 if ((off & 1) != 0)
2134 off &= ~1;
2135 else
2136 {
2137 bfd_boolean need_relocs = FALSE;
2138 Elf_Internal_Rela outrel;
2139 bfd_byte *loc = NULL;
2140 int cur_off = off;
2141
2142 /* The GOT entries have not been initialized yet. Do it
2143 now, and emit any relocations. If both an IE GOT and a
2144 GD GOT are necessary, we emit the GD first. */
2145
2146 if ((info->shared || indx != 0)
2147 && (h == NULL
2148 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2149 || h->root.type != bfd_link_hash_undefweak))
2150 {
2151 need_relocs = TRUE;
2152 if (htab->root.srelgot == NULL)
2153 abort ();
2154 loc = htab->root.srelgot->contents;
2155 loc += (htab->root.srelgot->reloc_count *
2156 sizeof (Elf32_External_Rela));
2157 }
2158
2159 if (tls_type & GOT_TLS_GD)
2160 {
2161 if (need_relocs)
2162 {
2163 outrel.r_addend = 0;
2164 outrel.r_offset = (htab->root.sgot->output_section->vma
2165 + htab->root.sgot->output_offset
2166 + cur_off);
2167 outrel.r_info = ELF32_R_INFO (indx,
2168 R_NIOS2_TLS_DTPMOD);
2169
2170 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2171 loc);
2172 htab->root.srelgot->reloc_count++;
2173 loc += sizeof (Elf32_External_Rela);
2174
2175 if (indx == 0)
2176 bfd_put_32 (output_bfd,
2177 (relocation - dtpoff_base (info) -
2178 DTP_OFFSET),
2179 htab->root.sgot->contents + cur_off + 4);
2180 else
2181 {
2182 outrel.r_addend = 0;
2183 outrel.r_info = ELF32_R_INFO (indx,
2184 R_NIOS2_TLS_DTPREL);
2185 outrel.r_offset += 4;
2186
2187 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2188 loc);
2189 htab->root.srelgot->reloc_count++;
2190 loc += sizeof (Elf32_External_Rela);
2191 }
2192 }
2193 else
2194 {
2195 /* If we are not emitting relocations for a
2196 general dynamic reference, then we must be in a
2197 static link or an executable link with the
2198 symbol binding locally. Mark it as belonging
2199 to module 1, the executable. */
2200 bfd_put_32 (output_bfd, 1,
2201 htab->root.sgot->contents + cur_off);
2202 bfd_put_32 (output_bfd, (relocation -
2203 dtpoff_base (info) -
2204 DTP_OFFSET),
2205 htab->root.sgot->contents + cur_off + 4);
2206 }
2207
2208 cur_off += 8;
2209 }
2210
2211 if (tls_type & GOT_TLS_IE)
2212 {
2213 if (need_relocs)
2214 {
2215 if (indx == 0)
2216 outrel.r_addend = (relocation -
2217 dtpoff_base (info));
2218 else
2219 outrel.r_addend = 0;
2220 outrel.r_offset = (htab->root.sgot->output_section->vma
2221 + htab->root.sgot->output_offset
2222 + cur_off);
2223 outrel.r_info = ELF32_R_INFO (indx,
2224 R_NIOS2_TLS_TPREL);
2225
2226 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2227 loc);
2228 htab->root.srelgot->reloc_count++;
2229 loc += sizeof (Elf32_External_Rela);
2230 }
2231 else
2232 bfd_put_32 (output_bfd, (tpoff (info, relocation)
2233 - TP_OFFSET),
2234 htab->root.sgot->contents + cur_off);
2235 cur_off += 4;
2236 }
2237
2238 if (h != NULL)
2239 h->got.offset |= 1;
2240 else
2241 local_got_offsets[r_symndx] |= 1;
2242 }
2243
2244 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
2245 off += 8;
2246 relocation = (htab->root.sgot->output_offset + off);
2247
2248 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2249 contents, rel->r_offset,
2250 relocation, rel->r_addend);
2251 }
2252
2253 break;
2254 case R_NIOS2_TLS_LE16:
2255 if (info->shared && !info->pie)
2256 {
2257 (*_bfd_error_handler)
2258 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
2259 "permitted in shared object"),
2260 input_bfd, input_section,
2261 (long) rel->r_offset, howto->name);
2262 return FALSE;
2263 }
2264 else
2265 relocation = tpoff (info, relocation) - TP_OFFSET;
2266
2267 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2268 contents, rel->r_offset,
2269 relocation, rel->r_addend);
2270 break;
2271
2272 case R_NIOS2_BFD_RELOC_32:
2273 if (info->shared
2274 && (input_section->flags & SEC_ALLOC) != 0
2275 && (h == NULL
2276 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2277 || h->root.type != bfd_link_hash_undefweak))
2278 {
2279 Elf_Internal_Rela outrel;
2280 bfd_byte *loc;
2281 bfd_boolean skip, relocate;
2282
2283 /* When generating a shared object, these relocations
2284 are copied into the output file to be resolved at run
2285 time. */
2286
2287 skip = FALSE;
2288 relocate = FALSE;
2289
2290 outrel.r_offset
2291 = _bfd_elf_section_offset (output_bfd, info,
2292 input_section, rel->r_offset);
2293 if (outrel.r_offset == (bfd_vma) -1)
2294 skip = TRUE;
2295 else if (outrel.r_offset == (bfd_vma) -2)
2296 skip = TRUE, relocate = TRUE;
2297 outrel.r_offset += (input_section->output_section->vma
2298 + input_section->output_offset);
2299
2300 if (skip)
2301 memset (&outrel, 0, sizeof outrel);
2302 else if (h != NULL
2303 && h->dynindx != -1
2304 && (!info->shared
2305 || !info->symbolic
2306 || !h->def_regular))
2307 {
2308 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2309 outrel.r_addend = rel->r_addend;
2310 }
2311 else
2312 {
2313 /* This symbol is local, or marked to become local. */
2314 outrel.r_addend = relocation + rel->r_addend;
2315 relocate = TRUE;
2316 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
2317 }
2318
2319 sreloc = elf_section_data (input_section)->sreloc;
2320 if (sreloc == NULL)
2321 abort ();
2322
2323 loc = sreloc->contents;
2324 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2325 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2326
2327 /* This reloc will be computed at runtime, so there's no
2328 need to do anything now, except for R_NIOS2_BFD_RELOC_32
2329 relocations that have been turned into
2330 R_NIOS2_RELATIVE. */
2331 if (!relocate)
2332 break;
2333 }
2334
2335 r = _bfd_final_link_relocate (howto, input_bfd,
2336 input_section, contents,
2337 rel->r_offset, relocation,
2338 rel->r_addend);
2339 break;
2340
2341 case R_NIOS2_TLS_DTPREL:
2342 relocation -= dtpoff_base (info);
2343 /* Fall through. */
2344
2345 default:
2346 r = _bfd_final_link_relocate (howto, input_bfd,
2347 input_section, contents,
2348 rel->r_offset, relocation,
2349 rel->r_addend);
2350 break;
2351 }
2352 }
2353 else
2354 r = bfd_reloc_notsupported;
2355
2356 if (r != bfd_reloc_ok)
2357 {
2358 if (h != NULL)
2359 name = h->root.root.string;
2360 else
2361 {
2362 name = bfd_elf_string_from_elf_section (input_bfd,
2363 symtab_hdr->sh_link,
2364 sym->st_name);
2365 if (name == NULL || *name == '\0')
2366 name = bfd_section_name (input_bfd, sec);
2367 }
2368
2369 switch (r)
2370 {
2371 case bfd_reloc_overflow:
2372 r = info->callbacks->reloc_overflow (info, NULL, name,
2373 howto->name, (bfd_vma) 0,
2374 input_bfd, input_section,
2375 rel->r_offset);
2376 break;
2377
2378 case bfd_reloc_undefined:
2379 r = info->callbacks->undefined_symbol (info, name, input_bfd,
2380 input_section,
2381 rel->r_offset, TRUE);
2382 break;
2383
2384 case bfd_reloc_outofrange:
2385 if (msg == NULL)
2386 msg = _("relocation out of range");
2387 break;
2388
2389 case bfd_reloc_notsupported:
2390 if (msg == NULL)
2391 msg = _("unsupported relocation");
2392 break;
2393
2394 case bfd_reloc_dangerous:
2395 if (msg == NULL)
2396 msg = _("dangerous relocation");
2397 break;
2398
2399 default:
2400 if (msg == NULL)
2401 msg = _("unknown error");
2402 break;
2403 }
2404
2405 if (msg)
2406 {
2407 r = info->callbacks->warning
2408 (info, msg, name, input_bfd, input_section, rel->r_offset);
2409 return FALSE;
2410 }
2411 }
2412 }
2413 return TRUE;
2414}
2415
2416/* Implement elf-backend_section_flags:
2417 Convert NIOS2 specific section flags to bfd internal section flags. */
2418static bfd_boolean
2419nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
2420{
2421 if (hdr->sh_flags & SHF_NIOS2_GPREL)
2422 *flags |= SEC_SMALL_DATA;
2423
2424 return TRUE;
2425}
2426
2427/* Implement elf_backend_fake_sections:
2428 Set the correct type for an NIOS2 ELF section. We do this by the
2429 section name, which is a hack, but ought to work. */
2430static bfd_boolean
2431nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2432 Elf_Internal_Shdr *hdr, asection *sec)
2433{
2434 register const char *name = bfd_get_section_name (abfd, sec);
2435
2436 if ((sec->flags & SEC_SMALL_DATA)
2437 || strcmp (name, ".sdata") == 0
2438 || strcmp (name, ".sbss") == 0
2439 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
2440 hdr->sh_flags |= SHF_NIOS2_GPREL;
2441
2442 return TRUE;
2443}
2444
2445/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2446 shortcuts to them in our hash table. */
2447static bfd_boolean
2448create_got_section (bfd *dynobj, struct bfd_link_info *info)
2449{
2450 struct elf32_nios2_link_hash_table *htab;
2451
2452 htab = elf32_nios2_hash_table (info);
2453
2454 if (! _bfd_elf_create_got_section (dynobj, info))
2455 return FALSE;
2456
2457 /* In order for the two loads in .PLTresolve to share the same %hiadj,
2458 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
2459 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
2460 return FALSE;
2461
2462 return TRUE;
2463}
2464
2465/* Implement elf_backend_create_dynamic_sections:
2466 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
2467 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
2468 hash table. */
2469static bfd_boolean
2470nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2471{
2472 struct elf32_nios2_link_hash_table *htab;
2473
2474 htab = elf32_nios2_hash_table (info);
2475 if (!htab->root.sgot && !create_got_section (dynobj, info))
2476 return FALSE;
2477
2478 _bfd_elf_create_dynamic_sections (dynobj, info);
2479
2480 /* In order for the two loads in a shared object .PLTresolve to share the
2481 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
2482 to a 16-byte boundary. This is because the addresses for these loads
2483 include the -(.plt+4) PIC correction. */
2484 if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
2485 return FALSE;
2486
2487 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
2488 if (!htab->sdynbss)
2489 return FALSE;
2490 if (!info->shared)
2491 {
2492 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
2493 if (!htab->srelbss)
2494 return FALSE;
2495 }
2496
2497 return TRUE;
2498}
2499
2500/* Implement elf_backend_copy_indirect_symbol:
2501 Copy the extra info we tack onto an elf_link_hash_entry. */
2502static void
2503nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
2504 struct elf_link_hash_entry *dir,
2505 struct elf_link_hash_entry *ind)
2506{
2507 struct elf32_nios2_link_hash_entry *edir, *eind;
2508
2509 edir = (struct elf32_nios2_link_hash_entry *) dir;
2510 eind = (struct elf32_nios2_link_hash_entry *) ind;
2511
2512 if (eind->dyn_relocs != NULL)
2513 {
2514 if (edir->dyn_relocs != NULL)
2515 {
2516 struct elf32_nios2_dyn_relocs **pp;
2517 struct elf32_nios2_dyn_relocs *p;
2518
2519 /* Add reloc counts against the indirect sym to the direct sym
2520 list. Merge any entries against the same section. */
2521 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2522 {
2523 struct elf32_nios2_dyn_relocs *q;
2524
2525 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2526 if (q->sec == p->sec)
2527 {
2528 q->pc_count += p->pc_count;
2529 q->count += p->count;
2530 *pp = p->next;
2531 break;
2532 }
2533 if (q == NULL)
2534 pp = &p->next;
2535 }
2536 *pp = edir->dyn_relocs;
2537 }
2538
2539 edir->dyn_relocs = eind->dyn_relocs;
2540 eind->dyn_relocs = NULL;
2541 }
2542
2543 if (ind->root.type == bfd_link_hash_indirect
2544 && dir->got.refcount <= 0)
2545 {
2546 edir->tls_type = eind->tls_type;
2547 eind->tls_type = GOT_UNKNOWN;
2548 }
2549
2550 edir->got_types_used |= eind->got_types_used;
2551
2552 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2553}
2554
2555/* Implement elf_backend_check_relocs:
2556 Look through the relocs for a section during the first phase. */
2557static bfd_boolean
2558nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
2559 asection *sec, const Elf_Internal_Rela *relocs)
2560{
2561 bfd *dynobj;
2562 Elf_Internal_Shdr *symtab_hdr;
2563 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2564 const Elf_Internal_Rela *rel;
2565 const Elf_Internal_Rela *rel_end;
2566 struct elf32_nios2_link_hash_table *htab;
2567 asection *sgot;
2568 asection *srelgot;
2569 asection *sreloc = NULL;
2570 bfd_signed_vma *local_got_refcounts;
2571
2572 if (info->relocatable)
2573 return TRUE;
2574
2575 dynobj = elf_hash_table (info)->dynobj;
2576 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2577 sym_hashes = elf_sym_hashes (abfd);
2578 sym_hashes_end = (sym_hashes
2579 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2580 if (!elf_bad_symtab (abfd))
2581 sym_hashes_end -= symtab_hdr->sh_info;
2582 local_got_refcounts = elf_local_got_refcounts (abfd);
2583
2584 htab = elf32_nios2_hash_table (info);
2585 sgot = htab->root.sgot;
2586 srelgot = htab->root.srelgot;
2587
2588 rel_end = relocs + sec->reloc_count;
2589 for (rel = relocs; rel < rel_end; rel++)
2590 {
2591 unsigned int r_type;
2592 struct elf_link_hash_entry *h;
2593 unsigned long r_symndx;
2594
2595 r_symndx = ELF32_R_SYM (rel->r_info);
2596 if (r_symndx < symtab_hdr->sh_info)
2597 h = NULL;
2598 else
2599 {
2600 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2601 while (h->root.type == bfd_link_hash_indirect
2602 || h->root.type == bfd_link_hash_warning)
2603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831
AM
2604
2605 /* PR15323, ref flags aren't set for references in the same
2606 object. */
2607 h->root.non_ir_ref = 1;
36591ba1
SL
2608 }
2609
2610 r_type = ELF32_R_TYPE (rel->r_info);
2611
2612 switch (r_type)
2613 {
2614 case R_NIOS2_GOT16:
2615 case R_NIOS2_CALL16:
2616 case R_NIOS2_TLS_GD16:
2617 case R_NIOS2_TLS_IE16:
2618 /* This symbol requires a global offset table entry. */
2619 {
2620 int tls_type, old_tls_type;
2621
2622 switch (r_type)
2623 {
2624 default:
2625 case R_NIOS2_GOT16:
2626 case R_NIOS2_CALL16:
2627 tls_type = GOT_NORMAL;
2628 break;
2629 case R_NIOS2_TLS_GD16:
2630 tls_type = GOT_TLS_GD;
2631 break;
2632 case R_NIOS2_TLS_IE16:
2633 tls_type = GOT_TLS_IE;
2634 break;
2635 }
2636
2637 if (dynobj == NULL)
2638 {
2639 /* Create the .got section. */
2640 elf_hash_table (info)->dynobj = dynobj = abfd;
2641 nios2_elf32_create_dynamic_sections (dynobj, info);
2642 }
2643
2644 if (sgot == NULL)
2645 {
2646 sgot = htab->root.sgot;
2647 BFD_ASSERT (sgot != NULL);
2648 }
2649
2650 if (srelgot == NULL
2651 && (h != NULL || info->shared))
2652 {
2653 srelgot = htab->root.srelgot;
2654 BFD_ASSERT (srelgot != NULL);
2655 }
2656
2657 if (h != NULL)
2658 {
2659 struct elf32_nios2_link_hash_entry *eh
2660 = (struct elf32_nios2_link_hash_entry *)h;
2661 h->got.refcount++;
2662 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
2663 if (r_type == R_NIOS2_CALL16)
2664 {
2665 /* Make sure a plt entry is created for this symbol if
2666 it turns out to be a function defined by a dynamic
2667 object. */
2668 h->plt.refcount++;
2669 h->needs_plt = 1;
2670 h->type = STT_FUNC;
2671 eh->got_types_used |= CALL16_USED;
2672 }
2673 else
2674 eh->got_types_used |= GOT16_USED;
2675 }
2676 else
2677 {
2678 /* This is a global offset table entry for a local symbol. */
2679 if (local_got_refcounts == NULL)
2680 {
2681 bfd_size_type size;
2682
2683 size = symtab_hdr->sh_info;
2684 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2685 local_got_refcounts
2686 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
2687 if (local_got_refcounts == NULL)
2688 return FALSE;
2689 elf_local_got_refcounts (abfd) = local_got_refcounts;
2690 elf32_nios2_local_got_tls_type (abfd)
2691 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2692 }
2693 local_got_refcounts[r_symndx]++;
2694 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
2695 }
2696
2697 /* We will already have issued an error message if there is a
2698 TLS / non-TLS mismatch, based on the symbol type. We don't
2699 support any linker relaxations. So just combine any TLS
2700 types needed. */
2701 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2702 && tls_type != GOT_NORMAL)
2703 tls_type |= old_tls_type;
2704
2705 if (old_tls_type != tls_type)
2706 {
2707 if (h != NULL)
2708 elf32_nios2_hash_entry (h)->tls_type = tls_type;
2709 else
2710 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
2711 }
2712 }
2713 /* Fall through */
2714 case R_NIOS2_TLS_LDM16:
2715 if (r_type == R_NIOS2_TLS_LDM16)
2716 htab->tls_ldm_got.refcount++;
2717
2718 if (htab->root.sgot == NULL)
2719 {
2720 if (htab->root.dynobj == NULL)
2721 htab->root.dynobj = abfd;
2722 if (!create_got_section (htab->root.dynobj, info))
2723 return FALSE;
2724 }
2725 break;
2726
2727 /* This relocation describes the C++ object vtable hierarchy.
2728 Reconstruct it for later use during GC. */
2729 case R_NIOS2_GNU_VTINHERIT:
2730 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2731 return FALSE;
2732 break;
2733
2734 /* This relocation describes which C++ vtable entries are actually
2735 used. Record for later use during GC. */
2736 case R_NIOS2_GNU_VTENTRY:
2737 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2738 return FALSE;
2739 break;
2740
2741 case R_NIOS2_BFD_RELOC_32:
2742 case R_NIOS2_CALL26:
2743 case R_NIOS2_HIADJ16:
2744 case R_NIOS2_LO16:
2745
2746 if (h != NULL)
2747 {
2748 /* If this reloc is in a read-only section, we might
2749 need a copy reloc. We can't check reliably at this
2750 stage whether the section is read-only, as input
2751 sections have not yet been mapped to output sections.
2752 Tentatively set the flag for now, and correct in
2753 adjust_dynamic_symbol. */
2754 if (!info->shared)
2755 h->non_got_ref = 1;
2756
2757 /* Make sure a plt entry is created for this symbol if it
2758 turns out to be a function defined by a dynamic object. */
2759 h->plt.refcount++;
2760
2761 if (r_type == R_NIOS2_CALL26)
2762 h->needs_plt = 1;
2763 }
2764
2765 /* If we are creating a shared library, we need to copy the
2766 reloc into the shared library. */
2767 if (info->shared
2768 && (sec->flags & SEC_ALLOC) != 0
2769 && (r_type == R_NIOS2_BFD_RELOC_32
2770 || (h != NULL && ! h->needs_plt
2771 && (! info->symbolic || ! h->def_regular))))
2772 {
2773 struct elf32_nios2_dyn_relocs *p;
2774 struct elf32_nios2_dyn_relocs **head;
2775
2776 /* When creating a shared object, we must copy these
2777 reloc types into the output file. We create a reloc
2778 section in dynobj and make room for this reloc. */
2779 if (sreloc == NULL)
2780 {
2781 sreloc = _bfd_elf_make_dynamic_reloc_section
2782 (sec, dynobj, 2, abfd, TRUE);
2783 if (sreloc == NULL)
2784 return FALSE;
2785 }
2786
2787 /* If this is a global symbol, we count the number of
2788 relocations we need for this symbol. */
2789 if (h != NULL)
2790 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
2791 else
2792 {
2793 /* Track dynamic relocs needed for local syms too.
2794 We really need local syms available to do this
2795 easily. Oh well. */
2796
2797 asection *s;
2798 void *vpp;
2799 Elf_Internal_Sym *isym;
2800
2801 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2802 abfd, r_symndx);
2803 if (isym == NULL)
2804 return FALSE;
2805
2806 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2807 if (s == NULL)
2808 s = sec;
2809
2810 vpp = &elf_section_data (s)->local_dynrel;
2811 head = (struct elf32_nios2_dyn_relocs **) vpp;
2812 }
2813
2814 p = *head;
2815 if (p == NULL || p->sec != sec)
2816 {
2817 bfd_size_type amt = sizeof *p;
2818 p = ((struct elf32_nios2_dyn_relocs *)
2819 bfd_alloc (htab->root.dynobj, amt));
2820 if (p == NULL)
2821 return FALSE;
2822 p->next = *head;
2823 *head = p;
2824 p->sec = sec;
2825 p->count = 0;
2826 p->pc_count = 0;
2827 }
2828
2829 p->count += 1;
2830
2831 }
2832 break;
2833 }
2834 }
2835
2836 return TRUE;
2837}
2838
2839
2840/* Implement elf_backend_gc_mark_hook:
2841 Return the section that should be marked against GC for a given
2842 relocation. */
2843static asection *
2844nios2_elf32_gc_mark_hook (asection *sec,
2845 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2846 Elf_Internal_Rela *rel,
2847 struct elf_link_hash_entry *h,
2848 Elf_Internal_Sym *sym)
2849{
2850 if (h != NULL)
2851 switch (ELF32_R_TYPE (rel->r_info))
2852 {
2853 case R_NIOS2_GNU_VTINHERIT:
2854 case R_NIOS2_GNU_VTENTRY:
2855 return NULL;
2856 }
2857 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2858}
2859
2860/* Implement elf_backend_gc_sweep_hook:
2861 Update the got entry reference counts for the section being removed. */
2862static bfd_boolean
2863nios2_elf32_gc_sweep_hook (bfd *abfd,
2864 struct bfd_link_info *info,
2865 asection *sec,
2866 const Elf_Internal_Rela *relocs)
2867{
2868 Elf_Internal_Shdr *symtab_hdr;
2869 struct elf_link_hash_entry **sym_hashes;
2870 bfd_signed_vma *local_got_refcounts;
2871 const Elf_Internal_Rela *rel, *relend;
2872 bfd *dynobj;
2873
2874 if (info->relocatable)
2875 return TRUE;
2876
2877 elf_section_data (sec)->local_dynrel = NULL;
2878
2879 dynobj = elf_hash_table (info)->dynobj;
2880 if (dynobj == NULL)
2881 return TRUE;
2882
2883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2884 sym_hashes = elf_sym_hashes (abfd);
2885 local_got_refcounts = elf_local_got_refcounts (abfd);
2886
2887 relend = relocs + sec->reloc_count;
2888 for (rel = relocs; rel < relend; rel++)
2889 {
2890 unsigned long r_symndx;
2891 struct elf_link_hash_entry *h = NULL;
2892 int r_type;
2893
2894 r_symndx = ELF32_R_SYM (rel->r_info);
2895 if (r_symndx >= symtab_hdr->sh_info)
2896 {
2897 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2898 while (h->root.type == bfd_link_hash_indirect
2899 || h->root.type == bfd_link_hash_warning)
2900 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2901 }
2902
2903 r_type = ELF32_R_TYPE (rel->r_info);
2904 switch (r_type)
2905 {
2906 case R_NIOS2_GOT16:
2907 case R_NIOS2_CALL16:
2908 if (h != NULL)
2909 {
2910 if (h->got.refcount > 0)
2911 --h->got.refcount;
2912 }
2913 else if (local_got_refcounts != NULL)
2914 {
2915 if (local_got_refcounts[r_symndx] > 0)
2916 --local_got_refcounts[r_symndx];
2917 }
2918 break;
2919
2920 case R_NIOS2_PCREL_LO:
2921 case R_NIOS2_PCREL_HA:
2922 case R_NIOS2_BFD_RELOC_32:
2923 case R_NIOS2_CALL26:
2924 if (h != NULL)
2925 {
2926 struct elf32_nios2_link_hash_entry *eh;
2927 struct elf32_nios2_dyn_relocs **pp;
2928 struct elf32_nios2_dyn_relocs *p;
2929
2930 eh = (struct elf32_nios2_link_hash_entry *) h;
2931
2932 if (h->plt.refcount > 0)
2933 --h->plt.refcount;
2934
2935 if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
2936 || r_type == R_NIOS2_BFD_RELOC_32)
2937 {
2938 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
2939 pp = &p->next)
2940 if (p->sec == sec)
2941 {
2942 p->count -= 1;
2943 if (p->count == 0)
2944 *pp = p->next;
2945 break;
2946 }
2947 }
2948 }
2949 break;
2950
2951 default:
2952 break;
2953 }
2954 }
2955
2956 return TRUE;
2957}
2958
2959/* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
2960static void
2961nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
2962{
2963 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
2964
2965 BFD_ASSERT(value <= 0xffff);
2966
2967 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
2968 sec->contents + offset);
2969}
2970
2971/* Install COUNT 32-bit values DATA starting at offset OFFSET into
2972 section SEC. */
2973static void
2974nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
2975 int count)
2976{
2977 while (count--)
2978 {
2979 bfd_put_32 (sec->owner, *data, sec->contents + offset);
2980 offset += 4;
2981 ++data;
2982 }
2983}
2984
2985/* Implement elf_backend_finish_dynamic_symbols:
2986 Finish up dynamic symbol handling. We set the contents of various
2987 dynamic sections here. */
2988static bfd_boolean
2989nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
2990 struct bfd_link_info *info,
2991 struct elf_link_hash_entry *h,
2992 Elf_Internal_Sym *sym)
2993{
2994 struct elf32_nios2_link_hash_table *htab;
2995 struct elf32_nios2_link_hash_entry *eh
2996 = (struct elf32_nios2_link_hash_entry *)h;
2997 int use_plt;
2998
2999 htab = elf32_nios2_hash_table (info);
3000
3001 if (h->plt.offset != (bfd_vma) -1)
3002 {
3003 asection *splt;
3004 asection *sgotplt;
3005 asection *srela;
3006 bfd_vma plt_index;
3007 bfd_vma got_offset;
3008 Elf_Internal_Rela rela;
3009 bfd_byte *loc;
3010 bfd_vma got_address;
3011
3012 /* This symbol has an entry in the procedure linkage table. Set
3013 it up. */
3014 BFD_ASSERT (h->dynindx != -1);
3015 splt = htab->root.splt;
3016 sgotplt = htab->root.sgotplt;
3017 srela = htab->root.srelplt;
3018 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
3019
3020 /* Emit the PLT entry. */
3021 if (info->shared)
3022 {
3023 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
3024 3);
3025 plt_index = (h->plt.offset - 24) / 12;
3026 got_offset = (plt_index + 3) * 4;
3027 nios2_elf32_install_imm16 (splt, h->plt.offset,
3028 hiadj(plt_index * 4));
3029 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3030 (plt_index * 4) & 0xffff);
3031 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
3032 0xfff4 - h->plt.offset);
3033 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3034 + got_offset);
3035
3036 /* Fill in the entry in the global offset table. There are no
3037 res_n slots for a shared object PLT, instead the .got.plt entries
3038 point to the PLT entries. */
3039 bfd_put_32 (output_bfd,
3040 splt->output_section->vma + splt->output_offset
3041 + h->plt.offset, sgotplt->contents + got_offset);
3042 }
3043 else
3044 {
3045 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
3046 got_offset = (plt_index + 3) * 4;
3047
3048 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
3049 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3050 + got_offset);
3051 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
3052 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3053 got_address & 0xffff);
3054
3055 /* Fill in the entry in the global offset table. */
3056 bfd_put_32 (output_bfd,
3057 splt->output_section->vma + splt->output_offset
3058 + plt_index * 4, sgotplt->contents + got_offset);
3059 }
3060
3061 /* Fill in the entry in the .rela.plt section. */
3062 rela.r_offset = got_address;
3063 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
3064 rela.r_addend = 0;
3065 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3066 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3067
3068 if (!h->def_regular)
3069 {
3070 /* Mark the symbol as undefined, rather than as defined in
3071 the .plt section. Leave the value alone. */
3072 sym->st_shndx = SHN_UNDEF;
3073 /* If the symbol is weak, we do need to clear the value.
3074 Otherwise, the PLT entry would provide a definition for
3075 the symbol even if the symbol wasn't defined anywhere,
3076 and so the symbol would never be NULL. */
3077 if (!h->ref_regular_nonweak)
3078 sym->st_value = 0;
3079 }
3080 }
3081
3082 use_plt = (eh->got_types_used == CALL16_USED
3083 && h->plt.offset != (bfd_vma) -1);
3084
3085 if (!use_plt && h->got.offset != (bfd_vma) -1
3086 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
3087 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
3088 {
3089 asection *sgot;
3090 asection *srela;
3091 Elf_Internal_Rela rela;
3092 bfd_byte *loc;
3093 bfd_vma offset;
3094
3095 /* This symbol has an entry in the global offset table. Set it
3096 up. */
3097 sgot = htab->root.sgot;
3098 srela = htab->root.srelgot;
3099 BFD_ASSERT (sgot != NULL && srela != NULL);
3100
3101 offset = (h->got.offset & ~(bfd_vma) 1);
3102 rela.r_offset = (sgot->output_section->vma
3103 + sgot->output_offset + offset);
3104
3105 /* If this is a -Bsymbolic link, and the symbol is defined
3106 locally, we just want to emit a RELATIVE reloc. Likewise if
3107 the symbol was forced to be local because of a version file.
3108 The entry in the global offset table will already have been
3109 initialized in the relocate_section function. */
3110
3111 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
3112 {
3113 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
3114 rela.r_addend = bfd_get_signed_32 (output_bfd,
3115 (sgot->contents + offset));
3116 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
3117 }
3118 else
3119 {
3120 bfd_put_32 (output_bfd, (bfd_vma) 0,
3121 sgot->contents + offset);
3122 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
3123 rela.r_addend = 0;
3124 }
3125
3126 loc = srela->contents;
3127 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3128 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3129 }
3130
3131 if (use_plt && h->got.offset != (bfd_vma) -1)
3132 {
3133 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
3134 asection *sgot = htab->root.sgot;
3135 asection *splt = htab->root.splt;
3136 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
3137 + h->plt.offset),
3138 sgot->contents + offset);
3139 }
3140
3141 if (h->needs_copy)
3142 {
3143 asection *s;
3144 Elf_Internal_Rela rela;
3145 bfd_byte *loc;
3146
3147 /* This symbol needs a copy reloc. Set it up. */
3148 BFD_ASSERT (h->dynindx != -1
3149 && (h->root.type == bfd_link_hash_defined
3150 || h->root.type == bfd_link_hash_defweak));
3151
3152 s = htab->srelbss;
3153 BFD_ASSERT (s != NULL);
3154
3155 rela.r_offset = (h->root.u.def.value
3156 + h->root.u.def.section->output_section->vma
3157 + h->root.u.def.section->output_offset);
3158 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
3159 rela.r_addend = 0;
3160 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3161 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3162 }
3163
3164 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3165 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3166 || h == elf_hash_table (info)->hgot)
3167 sym->st_shndx = SHN_ABS;
3168
3169 return TRUE;
3170}
3171
3172/* Implement elf_backend_finish_dynamic_sections. */
3173static bfd_boolean
3174nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
3175 struct bfd_link_info *info)
3176{
3177 bfd *dynobj;
3178 asection *sgotplt;
3179 asection *sdyn;
3180 struct elf32_nios2_link_hash_table *htab;
3181
3182 htab = elf32_nios2_hash_table (info);
3183 dynobj = elf_hash_table (info)->dynobj;
3184 sgotplt = htab->root.sgotplt;
3185 BFD_ASSERT (sgotplt != NULL);
3186 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3187
3188 if (elf_hash_table (info)->dynamic_sections_created)
3189 {
3190 asection *splt;
3191 Elf32_External_Dyn *dyncon, *dynconend;
3192
3193 splt = htab->root.splt;
3194 BFD_ASSERT (splt != NULL && sdyn != NULL);
3195
3196 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3197 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3198 for (; dyncon < dynconend; dyncon++)
3199 {
3200 Elf_Internal_Dyn dyn;
3201 asection *s;
3202
3203 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3204
3205 switch (dyn.d_tag)
3206 {
3207 default:
3208 break;
3209
3210 case DT_PLTGOT:
3211 s = htab->root.sgot;
3212 BFD_ASSERT (s != NULL);
3213 dyn.d_un.d_ptr = s->output_section->vma;
3214 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3215 break;
3216
3217 case DT_JMPREL:
3218 s = htab->root.srelplt;
3219 BFD_ASSERT (s != NULL);
3220 dyn.d_un.d_ptr = s->output_section->vma;
3221 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3222 break;
3223
3224 case DT_PLTRELSZ:
3225 s = htab->root.srelplt;
3226 BFD_ASSERT (s != NULL);
3227 dyn.d_un.d_val = s->size;
3228 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3229 break;
3230
3231 case DT_RELASZ:
3232 /* The procedure linkage table relocs (DT_JMPREL) should
3233 not be included in the overall relocs (DT_RELA).
3234 Therefore, we override the DT_RELASZ entry here to
3235 make it not include the JMPREL relocs. Since the
3236 linker script arranges for .rela.plt to follow all
3237 other relocation sections, we don't have to worry
3238 about changing the DT_RELA entry. */
3239 s = htab->root.srelplt;
3240 if (s != NULL)
3241 dyn.d_un.d_val -= s->size;
3242 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3243 break;
3244
3245 case DT_NIOS2_GP:
3246 s = htab->root.sgot;
3247 BFD_ASSERT (s != NULL);
3248 dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
3249 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3250 break;
3251 }
3252 }
3253
3254 /* Fill in the first entry in the procedure linkage table. */
3255 if (splt->size > 0)
3256 {
3257 bfd_vma got_address = (sgotplt->output_section->vma
3258 + sgotplt->output_offset);
3259 if (info->shared)
3260 {
3261 bfd_vma corrected = got_address - (splt->output_section->vma
3262 + splt->output_offset + 4);
3263 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
3264 nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
3265 nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
3266 nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
36591ba1
SL
3267 }
3268 else
3269 {
3270 /* Divide by 4 here, not 3 because we already corrected for the
3271 res_N branches. */
3272 bfd_vma res_size = (splt->size - 28) / 4;
3273 bfd_vma res_start = (splt->output_section->vma
3274 + splt->output_offset);
3275 bfd_vma res_offset;
3276
3277 for (res_offset = 0; res_offset < res_size; res_offset += 4)
3278 bfd_put_32 (output_bfd,
3279 6 | ((res_size - (res_offset + 4)) << 6),
3280 splt->contents + res_offset);
3281
3282 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
3283 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
3284 nios2_elf32_install_imm16 (splt, res_size + 4,
3285 res_start & 0xffff);
3286 nios2_elf32_install_imm16 (splt, res_size + 12,
3287 hiadj (got_address));
3288 nios2_elf32_install_imm16 (splt, res_size + 16,
3289 (got_address & 0xffff) + 4);
3290 nios2_elf32_install_imm16 (splt, res_size + 20,
3291 (got_address & 0xffff) + 8);
36591ba1
SL
3292 }
3293 }
3294 }
3295 /* Fill in the first three entries in the global offset table. */
3296 if (sgotplt->size > 0)
3297 {
3298 if (sdyn == NULL)
3299 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
3300 else
3301 bfd_put_32 (output_bfd,
3302 sdyn->output_section->vma + sdyn->output_offset,
3303 sgotplt->contents);
3304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
3305 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
3306 }
3307
3308 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
3309
3310 return TRUE;
3311}
3312
3313/* Implement elf_backend_adjust_dynamic_symbol:
3314 Adjust a symbol defined by a dynamic object and referenced by a
3315 regular object. The current definition is in some section of the
3316 dynamic object, but we're not including those sections. We have to
3317 change the definition to something the rest of the link can
3318 understand. */
3319static bfd_boolean
3320nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
3321 struct elf_link_hash_entry *h)
3322{
3323 struct elf32_nios2_link_hash_table *htab;
3324 bfd *dynobj;
3325 asection *s;
3326 unsigned align2;
3327
3328 htab = elf32_nios2_hash_table (info);
3329 dynobj = elf_hash_table (info)->dynobj;
3330
3331 /* Make sure we know what is going on here. */
3332 BFD_ASSERT (dynobj != NULL
3333 && (h->needs_plt
3334 || h->u.weakdef != NULL
3335 || (h->def_dynamic
3336 && h->ref_regular
3337 && !h->def_regular)));
3338
3339 /* If this is a function, put it in the procedure linkage table. We
3340 will fill in the contents of the procedure linkage table later,
3341 when we know the address of the .got section. */
3342 if (h->type == STT_FUNC || h->needs_plt)
3343 {
3344 if (h->plt.refcount <= 0
3345 || SYMBOL_CALLS_LOCAL (info, h)
3346 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3347 && h->root.type == bfd_link_hash_undefweak))
3348 {
3349 /* This case can occur if we saw a PLT reloc in an input
3350 file, but the symbol was never referred to by a dynamic
3351 object, or if all references were garbage collected. In
3352 such a case, we don't actually need to build a procedure
3353 linkage table, and we can just do a PCREL reloc instead. */
3354 h->plt.offset = (bfd_vma) -1;
3355 h->needs_plt = 0;
3356 }
3357
3358 return TRUE;
3359 }
3360
3361 /* Reinitialize the plt offset now that it is not used as a reference
3362 count any more. */
3363 h->plt.offset = (bfd_vma) -1;
3364
3365 /* If this is a weak symbol, and there is a real definition, the
3366 processor independent code will have arranged for us to see the
3367 real definition first, and we can just use the same value. */
3368 if (h->u.weakdef != NULL)
3369 {
3370 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3371 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3372 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3373 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3374 return TRUE;
3375 }
3376
3377 /* If there are no non-GOT references, we do not need a copy
3378 relocation. */
3379 if (!h->non_got_ref)
3380 return TRUE;
3381
3382 /* This is a reference to a symbol defined by a dynamic object which
3383 is not a function.
3384 If we are creating a shared library, we must presume that the
3385 only references to the symbol are via the global offset table.
3386 For such cases we need not do anything here; the relocations will
3387 be handled correctly by relocate_section. */
3388 if (info->shared)
3389 return TRUE;
3390
3391 if (h->size == 0)
3392 {
3393 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3394 h->root.root.string);
3395 return TRUE;
3396 }
3397
3398 /* We must allocate the symbol in our .dynbss section, which will
3399 become part of the .bss section of the executable. There will be
3400 an entry for this symbol in the .dynsym section. The dynamic
3401 object will contain position independent code, so all references
3402 from the dynamic object to this symbol will go through the global
3403 offset table. The dynamic linker will use the .dynsym entry to
3404 determine the address it must put in the global offset table, so
3405 both the dynamic object and the regular object will refer to the
3406 same memory location for the variable. */
3407 s = htab->sdynbss;
3408 BFD_ASSERT (s != NULL);
3409
3410 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
3411 copy the initial value out of the dynamic object and into the
3412 runtime process image. We need to remember the offset into the
3413 .rela.bss section we are going to use. */
3414 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3415 {
3416 asection *srel;
3417
3418 srel = htab->srelbss;
3419 BFD_ASSERT (srel != NULL);
3420 srel->size += sizeof (Elf32_External_Rela);
3421 h->needs_copy = 1;
3422 }
3423
3424 align2 = bfd_log2 (h->size);
3425 if (align2 > h->root.u.def.section->alignment_power)
3426 align2 = h->root.u.def.section->alignment_power;
3427
3428 /* Align dynbss. */
3429 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
3430 if (align2 > bfd_get_section_alignment (dynobj, s)
3431 && !bfd_set_section_alignment (dynobj, s, align2))
3432 return FALSE;
3433
3434 /* Define the symbol as being at this point in the section. */
3435 h->root.u.def.section = s;
3436 h->root.u.def.value = s->size;
3437
3438 /* Increment the section size to make room for the symbol. */
3439 s->size += h->size;
3440
3441 return TRUE;
3442}
3443
3444/* Worker function for nios2_elf32_size_dynamic_sections. */
3445static bfd_boolean
3446adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3447{
3448 struct bfd_link_info *info;
3449 struct elf32_nios2_link_hash_table *htab;
3450
3451 if (h->root.type == bfd_link_hash_indirect)
3452 return TRUE;
3453
3454 if (h->root.type == bfd_link_hash_warning)
3455 /* When warning symbols are created, they **replace** the "real"
3456 entry in the hash table, thus we never get to see the real
3457 symbol in a hash traversal. So look at it now. */
3458 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3459
3460 info = (struct bfd_link_info *) inf;
3461 htab = elf32_nios2_hash_table (info);
3462
3463 if (h->plt.offset != (bfd_vma)-1)
3464 h->plt.offset += htab->res_n_size;
3465 if (htab->root.splt == h->root.u.def.section)
3466 h->root.u.def.value += htab->res_n_size;
3467
3468 return TRUE;
3469}
3470
3471/* Another worker function for nios2_elf32_size_dynamic_sections.
3472 Allocate space in .plt, .got and associated reloc sections for
3473 dynamic relocs. */
3474static bfd_boolean
3475allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3476{
3477 struct bfd_link_info *info;
3478 struct elf32_nios2_link_hash_table *htab;
3479 struct elf32_nios2_link_hash_entry *eh;
3480 struct elf32_nios2_dyn_relocs *p;
3481 int use_plt;
3482
3483 if (h->root.type == bfd_link_hash_indirect)
3484 return TRUE;
3485
3486 if (h->root.type == bfd_link_hash_warning)
3487 /* When warning symbols are created, they **replace** the "real"
3488 entry in the hash table, thus we never get to see the real
3489 symbol in a hash traversal. So look at it now. */
3490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3491
3492 info = (struct bfd_link_info *) inf;
3493 htab = elf32_nios2_hash_table (info);
3494
3495 if (htab->root.dynamic_sections_created
3496 && h->plt.refcount > 0)
3497 {
3498 /* Make sure this symbol is output as a dynamic symbol.
3499 Undefined weak syms won't yet be marked as dynamic. */
3500 if (h->dynindx == -1
3501 && !h->forced_local
3502 && !bfd_elf_link_record_dynamic_symbol (info, h))
3503 return FALSE;
3504
3505 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3506 {
3507 asection *s = htab->root.splt;
3508
3509 /* Allocate room for the header. */
3510 if (s->size == 0)
3511 {
3512 if (info->shared)
3513 s->size = 24;
3514 else
3515 s->size = 28;
3516 }
3517
3518 h->plt.offset = s->size;
3519
3520 /* If this symbol is not defined in a regular file, and we are
3521 not generating a shared library, then set the symbol to this
3522 location in the .plt. This is required to make function
3523 pointers compare as equal between the normal executable and
3524 the shared library. */
3525 if (! info->shared
3526 && !h->def_regular)
3527 {
3528 h->root.u.def.section = s;
3529 h->root.u.def.value = h->plt.offset;
3530 }
3531
3532 /* Make room for this entry. */
3533 s->size += 12;
3534
3535 /* We also need to make an entry in the .rela.plt section. */
3536 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3537
3538 /* And the .got.plt section. */
3539 htab->root.sgotplt->size += 4;
3540 }
3541 else
3542 {
3543 h->plt.offset = (bfd_vma) -1;
3544 h->needs_plt = 0;
3545 }
3546 }
3547 else
3548 {
3549 h->plt.offset = (bfd_vma) -1;
3550 h->needs_plt = 0;
3551 }
3552
3553 eh = (struct elf32_nios2_link_hash_entry *) h;
3554 use_plt = (eh->got_types_used == CALL16_USED
3555 && h->plt.offset != (bfd_vma) -1);
3556
3557 if (h->got.refcount > 0)
3558 {
3559 asection *s;
3560 bfd_boolean dyn;
3561 int tls_type = eh->tls_type;
3562 int indx;
3563
3564 /* Make sure this symbol is output as a dynamic symbol.
3565 Undefined weak syms won't yet be marked as dynamic. */
3566 if (h->dynindx == -1
3567 && !h->forced_local
3568 && !bfd_elf_link_record_dynamic_symbol (info, h))
3569 return FALSE;
3570
3571 s = htab->root.sgot;
3572 h->got.offset = s->size;
3573
3574 if (tls_type == GOT_UNKNOWN)
3575 abort ();
3576
3577 if (tls_type == GOT_NORMAL)
3578 /* Non-TLS symbols need one GOT slot. */
3579 s->size += 4;
3580 else
3581 {
3582 if (tls_type & GOT_TLS_GD)
3583 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
3584 s->size += 8;
3585 if (tls_type & GOT_TLS_IE)
3586 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
3587 s->size += 4;
3588 }
3589
3590 dyn = htab->root.dynamic_sections_created;
3591
3592 indx = 0;
3593 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3594 && (!info->shared
3595 || !SYMBOL_REFERENCES_LOCAL (info, h)))
3596 indx = h->dynindx;
3597
3598 if (tls_type != GOT_NORMAL
3599 && (info->shared || indx != 0)
3600 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3601 || h->root.type != bfd_link_hash_undefweak))
3602 {
3603 if (tls_type & GOT_TLS_IE)
3604 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3605
3606 if (tls_type & GOT_TLS_GD)
3607 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3608
3609 if ((tls_type & GOT_TLS_GD) && indx != 0)
3610 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3611 }
3612 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3613 || h->root.type != bfd_link_hash_undefweak)
3614 && !use_plt
3615 && (info->shared
3616 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3617 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3618 }
3619 else
3620 h->got.offset = (bfd_vma) -1;
3621
3622 if (eh->dyn_relocs == NULL)
3623 return TRUE;
3624
3625 /* In the shared -Bsymbolic case, discard space allocated for
3626 dynamic pc-relative relocs against symbols which turn out to be
3627 defined in regular objects. For the normal shared case, discard
3628 space for pc-relative relocs that have become local due to symbol
3629 visibility changes. */
3630
3631 if (info->shared)
3632 {
3633 if (h->def_regular
3634 && (h->forced_local || info->symbolic))
3635 {
3636 struct elf32_nios2_dyn_relocs **pp;
3637
3638 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3639 {
3640 p->count -= p->pc_count;
3641 p->pc_count = 0;
3642 if (p->count == 0)
3643 *pp = p->next;
3644 else
3645 pp = &p->next;
3646 }
3647 }
3648
3649 /* Also discard relocs on undefined weak syms with non-default
3650 visibility. */
3651 if (eh->dyn_relocs != NULL
3652 && h->root.type == bfd_link_hash_undefweak)
3653 {
3654 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3655 eh->dyn_relocs = NULL;
3656
3657 /* Make sure undefined weak symbols are output as a dynamic
3658 symbol in PIEs. */
3659 else if (h->dynindx == -1
3660 && !h->forced_local
3661 && !bfd_elf_link_record_dynamic_symbol (info, h))
3662 return FALSE;
3663 }
3664 }
3665 else
3666 {
3667 /* For the non-shared case, discard space for relocs against
3668 symbols which turn out to need copy relocs or are not
3669 dynamic. */
3670
3671 if (!h->non_got_ref
3672 && ((h->def_dynamic && !h->def_regular)
3673 || (htab->root.dynamic_sections_created
3674 && (h->root.type == bfd_link_hash_undefweak
3675 || h->root.type == bfd_link_hash_undefined))))
3676 {
3677 /* Make sure this symbol is output as a dynamic symbol.
3678 Undefined weak syms won't yet be marked as dynamic. */
3679 if (h->dynindx == -1
3680 && !h->forced_local
3681 && !bfd_elf_link_record_dynamic_symbol (info, h))
3682 return FALSE;
3683
3684 /* If that succeeded, we know we'll be keeping all the
3685 relocs. */
3686 if (h->dynindx != -1)
3687 goto keep;
3688 }
3689
3690 eh->dyn_relocs = NULL;
3691
3692 keep: ;
3693 }
3694
3695 /* Finally, allocate space. */
3696 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3697 {
3698 asection *sreloc = elf_section_data (p->sec)->sreloc;
3699 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3700 }
3701
3702 return TRUE;
3703}
3704
3705/* Implement elf_backend_size_dynamic_sections:
3706 Set the sizes of the dynamic sections. */
3707static bfd_boolean
3708nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3709 struct bfd_link_info *info)
3710{
3711 bfd *dynobj;
3712 asection *s;
3713 bfd_boolean plt;
3714 bfd_boolean got;
3715 bfd_boolean relocs;
3716 bfd *ibfd;
3717 struct elf32_nios2_link_hash_table *htab;
3718
3719 htab = elf32_nios2_hash_table (info);
3720 dynobj = elf_hash_table (info)->dynobj;
3721 BFD_ASSERT (dynobj != NULL);
3722
3723 htab->res_n_size = 0;
3724 if (elf_hash_table (info)->dynamic_sections_created)
3725 {
3726 /* Set the contents of the .interp section to the interpreter. */
3727 if (info->executable)
3728 {
3729 s = bfd_get_linker_section (dynobj, ".interp");
3730 BFD_ASSERT (s != NULL);
3731 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3732 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3733 }
3734 }
3735 else
3736 {
3737 /* We may have created entries in the .rela.got section.
3738 However, if we are not creating the dynamic sections, we will
3739 not actually use these entries. Reset the size of .rela.got,
3740 which will cause it to get stripped from the output file
3741 below. */
3742 s = htab->root.srelgot;
3743 if (s != NULL)
3744 s->size = 0;
3745 }
3746
3747 /* Set up .got offsets for local syms, and space for local dynamic
3748 relocs. */
3749 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3750 {
3751 bfd_signed_vma *local_got;
3752 bfd_signed_vma *end_local_got;
3753 char *local_tls_type;
3754 bfd_size_type locsymcount;
3755 Elf_Internal_Shdr *symtab_hdr;
3756 asection *srel;
3757
3758 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3759 continue;
3760
3761 for (s = ibfd->sections; s != NULL; s = s->next)
3762 {
3763 struct elf32_nios2_dyn_relocs *p;
3764
3765 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
3766 {
3767 if (!bfd_is_abs_section (p->sec)
3768 && bfd_is_abs_section (p->sec->output_section))
3769 {
3770 /* Input section has been discarded, either because
3771 it is a copy of a linkonce section or due to
3772 linker script /DISCARD/, so we'll be discarding
3773 the relocs too. */
3774 }
3775 else if (p->count != 0)
3776 {
3777 srel = elf_section_data (p->sec)->sreloc;
3778 srel->size += p->count * sizeof (Elf32_External_Rela);
3779 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3780 info->flags |= DF_TEXTREL;
3781 }
3782 }
3783 }
3784
3785 local_got = elf_local_got_refcounts (ibfd);
3786 if (!local_got)
3787 continue;
3788
3789 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3790 locsymcount = symtab_hdr->sh_info;
3791 end_local_got = local_got + locsymcount;
3792 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
3793 s = htab->root.sgot;
3794 srel = htab->root.srelgot;
3795 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3796 {
3797 if (*local_got > 0)
3798 {
3799 *local_got = s->size;
3800 if (*local_tls_type & GOT_TLS_GD)
3801 /* TLS_GD relocs need an 8-byte structure in the GOT. */
3802 s->size += 8;
3803 if (*local_tls_type & GOT_TLS_IE)
3804 s->size += 4;
3805 if (*local_tls_type == GOT_NORMAL)
3806 s->size += 4;
3807
3808 if (info->shared || *local_tls_type == GOT_TLS_GD)
3809 srel->size += sizeof (Elf32_External_Rela);
3810 }
3811 else
3812 *local_got = (bfd_vma) -1;
3813 }
3814 }
3815
3816 if (htab->tls_ldm_got.refcount > 0)
3817 {
3818 /* Allocate two GOT entries and one dynamic relocation (if necessary)
3819 for R_NIOS2_TLS_LDM16 relocations. */
3820 htab->tls_ldm_got.offset = htab->root.sgot->size;
3821 htab->root.sgot->size += 8;
3822 if (info->shared)
3823 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3824 }
3825 else
3826 htab->tls_ldm_got.offset = -1;
3827
3828 /* Allocate global sym .plt and .got entries, and space for global
3829 sym dynamic relocs. */
3830 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3831
3832 /* The check_relocs and adjust_dynamic_symbol entry points have
3833 determined the sizes of the various dynamic sections. Allocate
3834 memory for them. */
3835 plt = FALSE;
3836 got = FALSE;
3837 relocs = FALSE;
3838 for (s = dynobj->sections; s != NULL; s = s->next)
3839 {
3840 const char *name;
3841
3842 if ((s->flags & SEC_LINKER_CREATED) == 0)
3843 continue;
3844
3845 /* It's OK to base decisions on the section name, because none
3846 of the dynobj section names depend upon the input files. */
3847 name = bfd_get_section_name (dynobj, s);
3848
3849 if (strcmp (name, ".plt") == 0)
3850 {
3851 /* Remember whether there is a PLT. */
3852 plt = s->size != 0;
3853
3854 /* Correct for the number of res_N branches. */
3855 if (plt && !info->shared)
3856 {
3857 htab->res_n_size = (s->size-28) / 3;
3858 s->size += htab->res_n_size;
3859 }
3860 }
3861 else if (CONST_STRNEQ (name, ".rela"))
3862 {
3863 if (s->size != 0)
3864 {
3865 relocs = TRUE;
3866
3867 /* We use the reloc_count field as a counter if we need
3868 to copy relocs into the output file. */
3869 s->reloc_count = 0;
3870 }
3871 }
3872 else if (CONST_STRNEQ (name, ".got"))
3873 got = s->size != 0;
3874 else if (strcmp (name, ".dynbss") != 0)
3875 /* It's not one of our sections, so don't allocate space. */
3876 continue;
3877
3878 if (s->size == 0)
3879 {
3880 /* If we don't need this section, strip it from the
3881 output file. This is mostly to handle .rela.bss and
3882 .rela.plt. We must create both sections in
3883 create_dynamic_sections, because they must be created
3884 before the linker maps input sections to output
3885 sections. The linker does that before
3886 adjust_dynamic_symbol is called, and it is that
3887 function which decides whether anything needs to go
3888 into these sections. */
3889 s->flags |= SEC_EXCLUDE;
3890 continue;
3891 }
3892
3893 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3894 continue;
3895
3896 /* Allocate memory for the section contents. */
3897 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3898 Unused entries should be reclaimed before the section's contents
3899 are written out, but at the moment this does not happen. Thus in
3900 order to prevent writing out garbage, we initialize the section's
3901 contents to zero. */
3902 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3903 if (s->contents == NULL)
3904 return FALSE;
3905 }
3906
3907 /* Adjust dynamic symbols that point to the plt to account for the
3908 now-known number of resN slots. */
3909 if (htab->res_n_size)
3910 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
3911
3912 if (elf_hash_table (info)->dynamic_sections_created)
3913 {
3914 /* Add some entries to the .dynamic section. We fill in the
3915 values later, in elf_nios2_finish_dynamic_sections, but we
3916 must add the entries now so that we get the correct size for
3917 the .dynamic section. The DT_DEBUG entry is filled in by the
3918 dynamic linker and used by the debugger. */
3919#define add_dynamic_entry(TAG, VAL) \
3920 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3921
3922 if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0))
3923 return FALSE;
3924
3925 if (got && !add_dynamic_entry (DT_PLTGOT, 0))
3926 return FALSE;
3927
3928 if (plt
3929 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
3930 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3931 || !add_dynamic_entry (DT_JMPREL, 0)))
3932 return FALSE;
3933
3934 if (relocs
3935 && (!add_dynamic_entry (DT_RELA, 0)
3936 || !add_dynamic_entry (DT_RELASZ, 0)
3937 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
3938 return FALSE;
3939
3940 if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0))
3941 return FALSE;
3942
3943 if ((info->flags & DF_TEXTREL) != 0
3944 && !add_dynamic_entry (DT_TEXTREL, 0))
3945 return FALSE;
3946 }
3947#undef add_dynamic_entry
3948
3949 return TRUE;
3950}
3951
3952/* Implement bfd_elf32_bfd_link_hash_table_create. */
3953static struct bfd_link_hash_table *
3954nios2_elf32_link_hash_table_create (bfd *abfd)
3955{
3956 struct elf32_nios2_link_hash_table *ret;
3957 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
3958
7bf52ea2 3959 ret = bfd_zmalloc (amt);
36591ba1
SL
3960 if (ret == NULL)
3961 return NULL;
3962
3963 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3964 link_hash_newfunc,
3965 sizeof (struct
3966 elf32_nios2_link_hash_entry),
3967 NIOS2_ELF_DATA))
3968 {
3969 free (ret);
3970 return NULL;
3971 }
3972
36591ba1
SL
3973 return &ret->root.root;
3974}
3975
3976/* Implement elf_backend_reloc_type_class. */
3977static enum elf_reloc_type_class
7e612e98
AM
3978nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3979 const asection *rel_sec ATTRIBUTE_UNUSED,
3980 const Elf_Internal_Rela *rela)
36591ba1
SL
3981{
3982 switch ((int) ELF32_R_TYPE (rela->r_info))
3983 {
3984 case R_NIOS2_RELATIVE:
3985 return reloc_class_relative;
3986 case R_NIOS2_JUMP_SLOT:
3987 return reloc_class_plt;
3988 case R_NIOS2_COPY:
3989 return reloc_class_copy;
3990 default:
3991 return reloc_class_normal;
3992 }
3993}
3994
3995/* Return 1 if target is one of ours. */
3996static bfd_boolean
3997is_nios2_elf_target (const struct bfd_target *targ)
3998{
3999 return (targ == &bfd_elf32_littlenios2_vec
4000 || targ == &bfd_elf32_bignios2_vec);
4001}
4002
4003/* Implement elf_backend_add_symbol_hook.
4004 This hook is called by the linker when adding symbols from an object
4005 file. We use it to put .comm items in .sbss, and not .bss. */
4006static bfd_boolean
4007nios2_elf_add_symbol_hook (bfd *abfd,
4008 struct bfd_link_info *info,
4009 Elf_Internal_Sym *sym,
4010 const char **namep ATTRIBUTE_UNUSED,
4011 flagword *flagsp ATTRIBUTE_UNUSED,
4012 asection **secp,
4013 bfd_vma *valp)
4014{
4015 bfd *dynobj;
4016
4017 if (sym->st_shndx == SHN_COMMON
4018 && !info->relocatable
4019 && sym->st_size <= elf_gp_size (abfd)
4020 && is_nios2_elf_target (info->output_bfd->xvec))
4021 {
4022 /* Common symbols less than or equal to -G nn bytes are automatically
4023 put into .sbss. */
4024 struct elf32_nios2_link_hash_table *htab;
4025
4026 htab = elf32_nios2_hash_table (info);
4027 if (htab->sbss == NULL)
4028 {
4029 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
4030
4031 dynobj = elf_hash_table (info)->dynobj;
4032 if (!dynobj)
4033 dynobj = abfd;
4034
4035 htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
4036 flags);
4037 if (htab->sbss == NULL)
4038 return FALSE;
4039 }
4040
4041 *secp = htab->sbss;
4042 *valp = sym->st_size;
4043 }
4044
4045 return TRUE;
4046}
4047
4048/* Implement elf_backend_can_make_relative_eh_frame:
4049 Decide whether to attempt to turn absptr or lsda encodings in
4050 shared libraries into pcrel within the given input section. */
4051static bfd_boolean
4052nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
4053 struct bfd_link_info *info
4054 ATTRIBUTE_UNUSED,
4055 asection *eh_frame_section
4056 ATTRIBUTE_UNUSED)
4057{
4058 /* We can't use PC-relative encodings in the .eh_frame section. */
4059 return FALSE;
4060}
4061
4062/* Implement elf_backend_special_sections. */
4063const struct bfd_elf_special_section elf32_nios2_special_sections[] =
4064{
4065 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
4066 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4067 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
4068 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4069 { NULL, 0, 0, 0, 0 }
4070};
4071
4072#define ELF_ARCH bfd_arch_nios2
4073#define ELF_TARGET_ID NIOS2_ELF_DATA
4074#define ELF_MACHINE_CODE EM_ALTERA_NIOS2
4075
4076/* The Nios II MMU uses a 4K page size. */
4077
4078#define ELF_MAXPAGESIZE 0x1000
4079
4080#define bfd_elf32_bfd_link_hash_table_create \
4081 nios2_elf32_link_hash_table_create
4082
4083/* Relocation table lookup macros. */
4084
4085#define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
4086#define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
4087
4088/* JUMP_TABLE_LINK macros. */
4089
4090/* elf_info_to_howto (using RELA relocations). */
4091
4092#define elf_info_to_howto nios2_elf32_info_to_howto
4093
4094/* elf backend functions. */
4095
4096#define elf_backend_can_gc_sections 1
4097#define elf_backend_can_refcount 1
4098#define elf_backend_plt_readonly 1
4099#define elf_backend_want_got_plt 1
4100#define elf_backend_rela_normal 1
4101
4102#define elf_backend_relocate_section nios2_elf32_relocate_section
4103#define elf_backend_section_flags nios2_elf32_section_flags
4104#define elf_backend_fake_sections nios2_elf32_fake_sections
4105#define elf_backend_check_relocs nios2_elf32_check_relocs
4106
4107#define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
4108#define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
4109#define elf_backend_create_dynamic_sections \
4110 nios2_elf32_create_dynamic_sections
4111#define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
4112#define elf_backend_finish_dynamic_sections \
4113 nios2_elf32_finish_dynamic_sections
4114#define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
4115#define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
4116#define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
4117#define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
4118#define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
4119
4120#define elf_backend_grok_prstatus nios2_grok_prstatus
4121#define elf_backend_grok_psinfo nios2_grok_psinfo
4122
4123#undef elf_backend_can_make_relative_eh_frame
4124#define elf_backend_can_make_relative_eh_frame \
4125 nios2_elf32_can_make_relative_eh_frame
4126
4127#define elf_backend_special_sections elf32_nios2_special_sections
4128
4129#define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
4130#define TARGET_LITTLE_NAME "elf32-littlenios2"
4131#define TARGET_BIG_SYM bfd_elf32_bignios2_vec
4132#define TARGET_BIG_NAME "elf32-bignios2"
4133
4134#define elf_backend_got_header_size 12
4135
4136#include "elf32-target.h"
This page took 0.206995 seconds and 4 git commands to generate.