2013-02-06 Sandra Loosemore <sandra@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / elf32-nios2.c
1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012, 2013 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 /* This file handles Altera Nios II ELF targets. */
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "bfdlink.h"
29 #include "genlink.h"
30 #include "elf-bfd.h"
31 #include "elf/nios2.h"
32 #include "opcode/nios2.h"
33
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. */
44 static bfd_reloc_status_type nios2_elf32_ignore_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type nios2_elf32_hi16_relocate
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type nios2_elf32_lo16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type nios2_elf32_call26_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type nios2_elf32_gprel_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_reloc_status_type nios2_elf32_callr_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68
69 /* Target vector. */
70 extern const bfd_target bfd_elf32_littlenios2_vec;
71 extern 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. */
78 static 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
661 static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
662
663 /* Return the howto for relocation RTYPE. */
664 static reloc_howto_type *
665 lookup_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. */
689 struct elf_reloc_map
690 {
691 bfd_reloc_code_real_type bfd_val;
692 enum elf_nios2_reloc_type elf_val;
693 };
694
695 static 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
744 struct 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
760 struct 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. */
793 struct 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
814 struct 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. */
836 static 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
842 static 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. */
853 static 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
859 static 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. */
870 static bfd_boolean
871 nios2_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 */
883 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
884
885 /* pr_pid */
886 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
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. */
901 static bfd_boolean
902 nios2_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 */
910 elf_tdata (abfd)->core_program
911 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
912 elf_tdata (abfd)->core_command
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 {
921 char *command = elf_tdata (abfd)->core_command;
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. */
932 static struct bfd_hash_entry *
933 link_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. */
963 static reloc_howto_type *
964 nios2_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. */
978 static reloc_howto_type *
979 nios2_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. */
996 static void
997 nios2_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. */
1010 static bfd_vma
1011 dtpoff_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. */
1021 static bfd_vma
1022 tpoff (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. */
1034 static bfd_boolean
1035 nios2_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;
1049 lookup:
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. */
1095 static bfd_reloc_status_type
1096 nios2_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. */
1130 static
1131 bfd_vma hiadj (bfd_vma symbol_value)
1132 {
1133 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1134 }
1135
1136 /* Do the relocations that require special handling. */
1137 static bfd_reloc_status_type
1138 nios2_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
1150 static bfd_reloc_status_type
1151 nios2_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
1163 static bfd_reloc_status_type
1164 nios2_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
1176 static bfd_reloc_status_type
1177 nios2_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
1189 static bfd_reloc_status_type
1190 nios2_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
1206 static bfd_reloc_status_type
1207 nios2_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
1220 static bfd_reloc_status_type
1221 nios2_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
1235 static bfd_reloc_status_type
1236 nios2_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
1247 static bfd_reloc_status_type
1248 nios2_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
1270 static bfd_reloc_status_type
1271 nios2_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
1293 static bfd_reloc_status_type
1294 nios2_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. */
1320 static bfd_reloc_status_type
1321 nios2_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
1332 static bfd_reloc_status_type
1333 nios2_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
1360 static bfd_reloc_status_type
1361 nios2_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
1388 static bfd_reloc_status_type
1389 nios2_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
1416 static bfd_reloc_status_type
1417 nios2_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
1442 static bfd_reloc_status_type
1443 nios2_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
1468 static bfd_reloc_status_type
1469 nios2_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
1496 static bfd_reloc_status_type
1497 nios2_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
1524 static bfd_reloc_status_type
1525 nios2_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
1572 static bfd_reloc_status_type
1573 nios2_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
1599 static bfd_reloc_status_type
1600 nios2_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
1626 static bfd_reloc_status_type
1627 nios2_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. */
1655 static bfd_boolean
1656 nios2_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. */
2418 static bfd_boolean
2419 nios2_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. */
2430 static bfd_boolean
2431 nios2_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. */
2447 static bfd_boolean
2448 create_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. */
2469 static bfd_boolean
2470 nios2_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. */
2502 static void
2503 nios2_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. */
2557 static bfd_boolean
2558 nios2_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;
2604 }
2605
2606 r_type = ELF32_R_TYPE (rel->r_info);
2607
2608 switch (r_type)
2609 {
2610 case R_NIOS2_GOT16:
2611 case R_NIOS2_CALL16:
2612 case R_NIOS2_TLS_GD16:
2613 case R_NIOS2_TLS_IE16:
2614 /* This symbol requires a global offset table entry. */
2615 {
2616 int tls_type, old_tls_type;
2617
2618 switch (r_type)
2619 {
2620 default:
2621 case R_NIOS2_GOT16:
2622 case R_NIOS2_CALL16:
2623 tls_type = GOT_NORMAL;
2624 break;
2625 case R_NIOS2_TLS_GD16:
2626 tls_type = GOT_TLS_GD;
2627 break;
2628 case R_NIOS2_TLS_IE16:
2629 tls_type = GOT_TLS_IE;
2630 break;
2631 }
2632
2633 if (dynobj == NULL)
2634 {
2635 /* Create the .got section. */
2636 elf_hash_table (info)->dynobj = dynobj = abfd;
2637 nios2_elf32_create_dynamic_sections (dynobj, info);
2638 }
2639
2640 if (sgot == NULL)
2641 {
2642 sgot = htab->root.sgot;
2643 BFD_ASSERT (sgot != NULL);
2644 }
2645
2646 if (srelgot == NULL
2647 && (h != NULL || info->shared))
2648 {
2649 srelgot = htab->root.srelgot;
2650 BFD_ASSERT (srelgot != NULL);
2651 }
2652
2653 if (h != NULL)
2654 {
2655 struct elf32_nios2_link_hash_entry *eh
2656 = (struct elf32_nios2_link_hash_entry *)h;
2657 h->got.refcount++;
2658 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
2659 if (r_type == R_NIOS2_CALL16)
2660 {
2661 /* Make sure a plt entry is created for this symbol if
2662 it turns out to be a function defined by a dynamic
2663 object. */
2664 h->plt.refcount++;
2665 h->needs_plt = 1;
2666 h->type = STT_FUNC;
2667 eh->got_types_used |= CALL16_USED;
2668 }
2669 else
2670 eh->got_types_used |= GOT16_USED;
2671 }
2672 else
2673 {
2674 /* This is a global offset table entry for a local symbol. */
2675 if (local_got_refcounts == NULL)
2676 {
2677 bfd_size_type size;
2678
2679 size = symtab_hdr->sh_info;
2680 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2681 local_got_refcounts
2682 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
2683 if (local_got_refcounts == NULL)
2684 return FALSE;
2685 elf_local_got_refcounts (abfd) = local_got_refcounts;
2686 elf32_nios2_local_got_tls_type (abfd)
2687 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2688 }
2689 local_got_refcounts[r_symndx]++;
2690 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
2691 }
2692
2693 /* We will already have issued an error message if there is a
2694 TLS / non-TLS mismatch, based on the symbol type. We don't
2695 support any linker relaxations. So just combine any TLS
2696 types needed. */
2697 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2698 && tls_type != GOT_NORMAL)
2699 tls_type |= old_tls_type;
2700
2701 if (old_tls_type != tls_type)
2702 {
2703 if (h != NULL)
2704 elf32_nios2_hash_entry (h)->tls_type = tls_type;
2705 else
2706 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
2707 }
2708 }
2709 /* Fall through */
2710 case R_NIOS2_TLS_LDM16:
2711 if (r_type == R_NIOS2_TLS_LDM16)
2712 htab->tls_ldm_got.refcount++;
2713
2714 if (htab->root.sgot == NULL)
2715 {
2716 if (htab->root.dynobj == NULL)
2717 htab->root.dynobj = abfd;
2718 if (!create_got_section (htab->root.dynobj, info))
2719 return FALSE;
2720 }
2721 break;
2722
2723 /* This relocation describes the C++ object vtable hierarchy.
2724 Reconstruct it for later use during GC. */
2725 case R_NIOS2_GNU_VTINHERIT:
2726 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2727 return FALSE;
2728 break;
2729
2730 /* This relocation describes which C++ vtable entries are actually
2731 used. Record for later use during GC. */
2732 case R_NIOS2_GNU_VTENTRY:
2733 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2734 return FALSE;
2735 break;
2736
2737 case R_NIOS2_BFD_RELOC_32:
2738 case R_NIOS2_CALL26:
2739 case R_NIOS2_HIADJ16:
2740 case R_NIOS2_LO16:
2741
2742 if (h != NULL)
2743 {
2744 /* If this reloc is in a read-only section, we might
2745 need a copy reloc. We can't check reliably at this
2746 stage whether the section is read-only, as input
2747 sections have not yet been mapped to output sections.
2748 Tentatively set the flag for now, and correct in
2749 adjust_dynamic_symbol. */
2750 if (!info->shared)
2751 h->non_got_ref = 1;
2752
2753 /* Make sure a plt entry is created for this symbol if it
2754 turns out to be a function defined by a dynamic object. */
2755 h->plt.refcount++;
2756
2757 if (r_type == R_NIOS2_CALL26)
2758 h->needs_plt = 1;
2759 }
2760
2761 /* If we are creating a shared library, we need to copy the
2762 reloc into the shared library. */
2763 if (info->shared
2764 && (sec->flags & SEC_ALLOC) != 0
2765 && (r_type == R_NIOS2_BFD_RELOC_32
2766 || (h != NULL && ! h->needs_plt
2767 && (! info->symbolic || ! h->def_regular))))
2768 {
2769 struct elf32_nios2_dyn_relocs *p;
2770 struct elf32_nios2_dyn_relocs **head;
2771
2772 /* When creating a shared object, we must copy these
2773 reloc types into the output file. We create a reloc
2774 section in dynobj and make room for this reloc. */
2775 if (sreloc == NULL)
2776 {
2777 sreloc = _bfd_elf_make_dynamic_reloc_section
2778 (sec, dynobj, 2, abfd, TRUE);
2779 if (sreloc == NULL)
2780 return FALSE;
2781 }
2782
2783 /* If this is a global symbol, we count the number of
2784 relocations we need for this symbol. */
2785 if (h != NULL)
2786 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
2787 else
2788 {
2789 /* Track dynamic relocs needed for local syms too.
2790 We really need local syms available to do this
2791 easily. Oh well. */
2792
2793 asection *s;
2794 void *vpp;
2795 Elf_Internal_Sym *isym;
2796
2797 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2798 abfd, r_symndx);
2799 if (isym == NULL)
2800 return FALSE;
2801
2802 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2803 if (s == NULL)
2804 s = sec;
2805
2806 vpp = &elf_section_data (s)->local_dynrel;
2807 head = (struct elf32_nios2_dyn_relocs **) vpp;
2808 }
2809
2810 p = *head;
2811 if (p == NULL || p->sec != sec)
2812 {
2813 bfd_size_type amt = sizeof *p;
2814 p = ((struct elf32_nios2_dyn_relocs *)
2815 bfd_alloc (htab->root.dynobj, amt));
2816 if (p == NULL)
2817 return FALSE;
2818 p->next = *head;
2819 *head = p;
2820 p->sec = sec;
2821 p->count = 0;
2822 p->pc_count = 0;
2823 }
2824
2825 p->count += 1;
2826
2827 }
2828 break;
2829 }
2830 }
2831
2832 return TRUE;
2833 }
2834
2835
2836 /* Implement elf_backend_gc_mark_hook:
2837 Return the section that should be marked against GC for a given
2838 relocation. */
2839 static asection *
2840 nios2_elf32_gc_mark_hook (asection *sec,
2841 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2842 Elf_Internal_Rela *rel,
2843 struct elf_link_hash_entry *h,
2844 Elf_Internal_Sym *sym)
2845 {
2846 if (h != NULL)
2847 switch (ELF32_R_TYPE (rel->r_info))
2848 {
2849 case R_NIOS2_GNU_VTINHERIT:
2850 case R_NIOS2_GNU_VTENTRY:
2851 return NULL;
2852 }
2853 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2854 }
2855
2856 /* Implement elf_backend_gc_sweep_hook:
2857 Update the got entry reference counts for the section being removed. */
2858 static bfd_boolean
2859 nios2_elf32_gc_sweep_hook (bfd *abfd,
2860 struct bfd_link_info *info,
2861 asection *sec,
2862 const Elf_Internal_Rela *relocs)
2863 {
2864 Elf_Internal_Shdr *symtab_hdr;
2865 struct elf_link_hash_entry **sym_hashes;
2866 bfd_signed_vma *local_got_refcounts;
2867 const Elf_Internal_Rela *rel, *relend;
2868 bfd *dynobj;
2869
2870 if (info->relocatable)
2871 return TRUE;
2872
2873 elf_section_data (sec)->local_dynrel = NULL;
2874
2875 dynobj = elf_hash_table (info)->dynobj;
2876 if (dynobj == NULL)
2877 return TRUE;
2878
2879 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2880 sym_hashes = elf_sym_hashes (abfd);
2881 local_got_refcounts = elf_local_got_refcounts (abfd);
2882
2883 relend = relocs + sec->reloc_count;
2884 for (rel = relocs; rel < relend; rel++)
2885 {
2886 unsigned long r_symndx;
2887 struct elf_link_hash_entry *h = NULL;
2888 int r_type;
2889
2890 r_symndx = ELF32_R_SYM (rel->r_info);
2891 if (r_symndx >= symtab_hdr->sh_info)
2892 {
2893 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2894 while (h->root.type == bfd_link_hash_indirect
2895 || h->root.type == bfd_link_hash_warning)
2896 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2897 }
2898
2899 r_type = ELF32_R_TYPE (rel->r_info);
2900 switch (r_type)
2901 {
2902 case R_NIOS2_GOT16:
2903 case R_NIOS2_CALL16:
2904 if (h != NULL)
2905 {
2906 if (h->got.refcount > 0)
2907 --h->got.refcount;
2908 }
2909 else if (local_got_refcounts != NULL)
2910 {
2911 if (local_got_refcounts[r_symndx] > 0)
2912 --local_got_refcounts[r_symndx];
2913 }
2914 break;
2915
2916 case R_NIOS2_PCREL_LO:
2917 case R_NIOS2_PCREL_HA:
2918 case R_NIOS2_BFD_RELOC_32:
2919 case R_NIOS2_CALL26:
2920 if (h != NULL)
2921 {
2922 struct elf32_nios2_link_hash_entry *eh;
2923 struct elf32_nios2_dyn_relocs **pp;
2924 struct elf32_nios2_dyn_relocs *p;
2925
2926 eh = (struct elf32_nios2_link_hash_entry *) h;
2927
2928 if (h->plt.refcount > 0)
2929 --h->plt.refcount;
2930
2931 if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
2932 || r_type == R_NIOS2_BFD_RELOC_32)
2933 {
2934 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
2935 pp = &p->next)
2936 if (p->sec == sec)
2937 {
2938 p->count -= 1;
2939 if (p->count == 0)
2940 *pp = p->next;
2941 break;
2942 }
2943 }
2944 }
2945 break;
2946
2947 default:
2948 break;
2949 }
2950 }
2951
2952 return TRUE;
2953 }
2954
2955 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
2956 static void
2957 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
2958 {
2959 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
2960
2961 BFD_ASSERT(value <= 0xffff);
2962
2963 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
2964 sec->contents + offset);
2965 }
2966
2967 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
2968 section SEC. */
2969 static void
2970 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
2971 int count)
2972 {
2973 while (count--)
2974 {
2975 bfd_put_32 (sec->owner, *data, sec->contents + offset);
2976 offset += 4;
2977 ++data;
2978 }
2979 }
2980
2981 /* Implement elf_backend_finish_dynamic_symbols:
2982 Finish up dynamic symbol handling. We set the contents of various
2983 dynamic sections here. */
2984 static bfd_boolean
2985 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
2986 struct bfd_link_info *info,
2987 struct elf_link_hash_entry *h,
2988 Elf_Internal_Sym *sym)
2989 {
2990 struct elf32_nios2_link_hash_table *htab;
2991 struct elf32_nios2_link_hash_entry *eh
2992 = (struct elf32_nios2_link_hash_entry *)h;
2993 int use_plt;
2994
2995 htab = elf32_nios2_hash_table (info);
2996
2997 if (h->plt.offset != (bfd_vma) -1)
2998 {
2999 asection *splt;
3000 asection *sgotplt;
3001 asection *srela;
3002 bfd_vma plt_index;
3003 bfd_vma got_offset;
3004 Elf_Internal_Rela rela;
3005 bfd_byte *loc;
3006 bfd_vma got_address;
3007
3008 /* This symbol has an entry in the procedure linkage table. Set
3009 it up. */
3010 BFD_ASSERT (h->dynindx != -1);
3011 splt = htab->root.splt;
3012 sgotplt = htab->root.sgotplt;
3013 srela = htab->root.srelplt;
3014 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
3015
3016 /* Emit the PLT entry. */
3017 if (info->shared)
3018 {
3019 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
3020 3);
3021 plt_index = (h->plt.offset - 24) / 12;
3022 got_offset = (plt_index + 3) * 4;
3023 nios2_elf32_install_imm16 (splt, h->plt.offset,
3024 hiadj(plt_index * 4));
3025 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3026 (plt_index * 4) & 0xffff);
3027 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
3028 0xfff4 - h->plt.offset);
3029 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3030 + got_offset);
3031
3032 /* Fill in the entry in the global offset table. There are no
3033 res_n slots for a shared object PLT, instead the .got.plt entries
3034 point to the PLT entries. */
3035 bfd_put_32 (output_bfd,
3036 splt->output_section->vma + splt->output_offset
3037 + h->plt.offset, sgotplt->contents + got_offset);
3038 }
3039 else
3040 {
3041 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
3042 got_offset = (plt_index + 3) * 4;
3043
3044 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
3045 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3046 + got_offset);
3047 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
3048 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3049 got_address & 0xffff);
3050
3051 /* Fill in the entry in the global offset table. */
3052 bfd_put_32 (output_bfd,
3053 splt->output_section->vma + splt->output_offset
3054 + plt_index * 4, sgotplt->contents + got_offset);
3055 }
3056
3057 /* Fill in the entry in the .rela.plt section. */
3058 rela.r_offset = got_address;
3059 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
3060 rela.r_addend = 0;
3061 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3062 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3063
3064 if (!h->def_regular)
3065 {
3066 /* Mark the symbol as undefined, rather than as defined in
3067 the .plt section. Leave the value alone. */
3068 sym->st_shndx = SHN_UNDEF;
3069 /* If the symbol is weak, we do need to clear the value.
3070 Otherwise, the PLT entry would provide a definition for
3071 the symbol even if the symbol wasn't defined anywhere,
3072 and so the symbol would never be NULL. */
3073 if (!h->ref_regular_nonweak)
3074 sym->st_value = 0;
3075 }
3076 }
3077
3078 use_plt = (eh->got_types_used == CALL16_USED
3079 && h->plt.offset != (bfd_vma) -1);
3080
3081 if (!use_plt && h->got.offset != (bfd_vma) -1
3082 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
3083 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
3084 {
3085 asection *sgot;
3086 asection *srela;
3087 Elf_Internal_Rela rela;
3088 bfd_byte *loc;
3089 bfd_vma offset;
3090
3091 /* This symbol has an entry in the global offset table. Set it
3092 up. */
3093 sgot = htab->root.sgot;
3094 srela = htab->root.srelgot;
3095 BFD_ASSERT (sgot != NULL && srela != NULL);
3096
3097 offset = (h->got.offset & ~(bfd_vma) 1);
3098 rela.r_offset = (sgot->output_section->vma
3099 + sgot->output_offset + offset);
3100
3101 /* If this is a -Bsymbolic link, and the symbol is defined
3102 locally, we just want to emit a RELATIVE reloc. Likewise if
3103 the symbol was forced to be local because of a version file.
3104 The entry in the global offset table will already have been
3105 initialized in the relocate_section function. */
3106
3107 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
3108 {
3109 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
3110 rela.r_addend = bfd_get_signed_32 (output_bfd,
3111 (sgot->contents + offset));
3112 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
3113 }
3114 else
3115 {
3116 bfd_put_32 (output_bfd, (bfd_vma) 0,
3117 sgot->contents + offset);
3118 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
3119 rela.r_addend = 0;
3120 }
3121
3122 loc = srela->contents;
3123 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3124 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3125 }
3126
3127 if (use_plt && h->got.offset != (bfd_vma) -1)
3128 {
3129 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
3130 asection *sgot = htab->root.sgot;
3131 asection *splt = htab->root.splt;
3132 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
3133 + h->plt.offset),
3134 sgot->contents + offset);
3135 }
3136
3137 if (h->needs_copy)
3138 {
3139 asection *s;
3140 Elf_Internal_Rela rela;
3141 bfd_byte *loc;
3142
3143 /* This symbol needs a copy reloc. Set it up. */
3144 BFD_ASSERT (h->dynindx != -1
3145 && (h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak));
3147
3148 s = htab->srelbss;
3149 BFD_ASSERT (s != NULL);
3150
3151 rela.r_offset = (h->root.u.def.value
3152 + h->root.u.def.section->output_section->vma
3153 + h->root.u.def.section->output_offset);
3154 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
3155 rela.r_addend = 0;
3156 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3157 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3158 }
3159
3160 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3161 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3162 || h == elf_hash_table (info)->hgot)
3163 sym->st_shndx = SHN_ABS;
3164
3165 return TRUE;
3166 }
3167
3168 /* Implement elf_backend_finish_dynamic_sections. */
3169 static bfd_boolean
3170 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
3171 struct bfd_link_info *info)
3172 {
3173 bfd *dynobj;
3174 asection *sgotplt;
3175 asection *sdyn;
3176 struct elf32_nios2_link_hash_table *htab;
3177
3178 htab = elf32_nios2_hash_table (info);
3179 dynobj = elf_hash_table (info)->dynobj;
3180 sgotplt = htab->root.sgotplt;
3181 BFD_ASSERT (sgotplt != NULL);
3182 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3183
3184 if (elf_hash_table (info)->dynamic_sections_created)
3185 {
3186 asection *splt;
3187 Elf32_External_Dyn *dyncon, *dynconend;
3188
3189 splt = htab->root.splt;
3190 BFD_ASSERT (splt != NULL && sdyn != NULL);
3191
3192 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3193 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3194 for (; dyncon < dynconend; dyncon++)
3195 {
3196 Elf_Internal_Dyn dyn;
3197 asection *s;
3198
3199 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3200
3201 switch (dyn.d_tag)
3202 {
3203 default:
3204 break;
3205
3206 case DT_PLTGOT:
3207 s = htab->root.sgot;
3208 BFD_ASSERT (s != NULL);
3209 dyn.d_un.d_ptr = s->output_section->vma;
3210 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3211 break;
3212
3213 case DT_JMPREL:
3214 s = htab->root.srelplt;
3215 BFD_ASSERT (s != NULL);
3216 dyn.d_un.d_ptr = s->output_section->vma;
3217 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3218 break;
3219
3220 case DT_PLTRELSZ:
3221 s = htab->root.srelplt;
3222 BFD_ASSERT (s != NULL);
3223 dyn.d_un.d_val = s->size;
3224 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3225 break;
3226
3227 case DT_RELASZ:
3228 /* The procedure linkage table relocs (DT_JMPREL) should
3229 not be included in the overall relocs (DT_RELA).
3230 Therefore, we override the DT_RELASZ entry here to
3231 make it not include the JMPREL relocs. Since the
3232 linker script arranges for .rela.plt to follow all
3233 other relocation sections, we don't have to worry
3234 about changing the DT_RELA entry. */
3235 s = htab->root.srelplt;
3236 if (s != NULL)
3237 dyn.d_un.d_val -= s->size;
3238 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3239 break;
3240
3241 case DT_NIOS2_GP:
3242 s = htab->root.sgot;
3243 BFD_ASSERT (s != NULL);
3244 dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
3245 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3246 break;
3247 }
3248 }
3249
3250 /* Fill in the first entry in the procedure linkage table. */
3251 if (splt->size > 0)
3252 {
3253 bfd_vma got_address = (sgotplt->output_section->vma
3254 + sgotplt->output_offset);
3255 if (info->shared)
3256 {
3257 bfd_vma corrected = got_address - (splt->output_section->vma
3258 + splt->output_offset + 4);
3259 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
3260 nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
3261 nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
3262 nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
3263
3264 elf_section_data (splt->output_section)->this_hdr.sh_entsize
3265 = 24;
3266 }
3267 else
3268 {
3269 /* Divide by 4 here, not 3 because we already corrected for the
3270 res_N branches. */
3271 bfd_vma res_size = (splt->size - 28) / 4;
3272 bfd_vma res_start = (splt->output_section->vma
3273 + splt->output_offset);
3274 bfd_vma res_offset;
3275
3276 for (res_offset = 0; res_offset < res_size; res_offset += 4)
3277 bfd_put_32 (output_bfd,
3278 6 | ((res_size - (res_offset + 4)) << 6),
3279 splt->contents + res_offset);
3280
3281 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
3282 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
3283 nios2_elf32_install_imm16 (splt, res_size + 4,
3284 res_start & 0xffff);
3285 nios2_elf32_install_imm16 (splt, res_size + 12,
3286 hiadj (got_address));
3287 nios2_elf32_install_imm16 (splt, res_size + 16,
3288 (got_address & 0xffff) + 4);
3289 nios2_elf32_install_imm16 (splt, res_size + 20,
3290 (got_address & 0xffff) + 8);
3291
3292 elf_section_data (splt->output_section)->this_hdr.sh_entsize
3293 = 28 + res_size;
3294 }
3295 }
3296 }
3297 /* Fill in the first three entries in the global offset table. */
3298 if (sgotplt->size > 0)
3299 {
3300 if (sdyn == NULL)
3301 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
3302 else
3303 bfd_put_32 (output_bfd,
3304 sdyn->output_section->vma + sdyn->output_offset,
3305 sgotplt->contents);
3306 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
3307 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
3308 }
3309
3310 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
3311
3312 return TRUE;
3313 }
3314
3315 /* Implement elf_backend_adjust_dynamic_symbol:
3316 Adjust a symbol defined by a dynamic object and referenced by a
3317 regular object. The current definition is in some section of the
3318 dynamic object, but we're not including those sections. We have to
3319 change the definition to something the rest of the link can
3320 understand. */
3321 static bfd_boolean
3322 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
3323 struct elf_link_hash_entry *h)
3324 {
3325 struct elf32_nios2_link_hash_table *htab;
3326 bfd *dynobj;
3327 asection *s;
3328 unsigned align2;
3329
3330 htab = elf32_nios2_hash_table (info);
3331 dynobj = elf_hash_table (info)->dynobj;
3332
3333 /* Make sure we know what is going on here. */
3334 BFD_ASSERT (dynobj != NULL
3335 && (h->needs_plt
3336 || h->u.weakdef != NULL
3337 || (h->def_dynamic
3338 && h->ref_regular
3339 && !h->def_regular)));
3340
3341 /* If this is a function, put it in the procedure linkage table. We
3342 will fill in the contents of the procedure linkage table later,
3343 when we know the address of the .got section. */
3344 if (h->type == STT_FUNC || h->needs_plt)
3345 {
3346 if (h->plt.refcount <= 0
3347 || SYMBOL_CALLS_LOCAL (info, h)
3348 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3349 && h->root.type == bfd_link_hash_undefweak))
3350 {
3351 /* This case can occur if we saw a PLT reloc in an input
3352 file, but the symbol was never referred to by a dynamic
3353 object, or if all references were garbage collected. In
3354 such a case, we don't actually need to build a procedure
3355 linkage table, and we can just do a PCREL reloc instead. */
3356 h->plt.offset = (bfd_vma) -1;
3357 h->needs_plt = 0;
3358 }
3359
3360 return TRUE;
3361 }
3362
3363 /* Reinitialize the plt offset now that it is not used as a reference
3364 count any more. */
3365 h->plt.offset = (bfd_vma) -1;
3366
3367 /* If this is a weak symbol, and there is a real definition, the
3368 processor independent code will have arranged for us to see the
3369 real definition first, and we can just use the same value. */
3370 if (h->u.weakdef != NULL)
3371 {
3372 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3373 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3374 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3375 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3376 return TRUE;
3377 }
3378
3379 /* If there are no non-GOT references, we do not need a copy
3380 relocation. */
3381 if (!h->non_got_ref)
3382 return TRUE;
3383
3384 /* This is a reference to a symbol defined by a dynamic object which
3385 is not a function.
3386 If we are creating a shared library, we must presume that the
3387 only references to the symbol are via the global offset table.
3388 For such cases we need not do anything here; the relocations will
3389 be handled correctly by relocate_section. */
3390 if (info->shared)
3391 return TRUE;
3392
3393 if (h->size == 0)
3394 {
3395 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3396 h->root.root.string);
3397 return TRUE;
3398 }
3399
3400 /* We must allocate the symbol in our .dynbss section, which will
3401 become part of the .bss section of the executable. There will be
3402 an entry for this symbol in the .dynsym section. The dynamic
3403 object will contain position independent code, so all references
3404 from the dynamic object to this symbol will go through the global
3405 offset table. The dynamic linker will use the .dynsym entry to
3406 determine the address it must put in the global offset table, so
3407 both the dynamic object and the regular object will refer to the
3408 same memory location for the variable. */
3409 s = htab->sdynbss;
3410 BFD_ASSERT (s != NULL);
3411
3412 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
3413 copy the initial value out of the dynamic object and into the
3414 runtime process image. We need to remember the offset into the
3415 .rela.bss section we are going to use. */
3416 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3417 {
3418 asection *srel;
3419
3420 srel = htab->srelbss;
3421 BFD_ASSERT (srel != NULL);
3422 srel->size += sizeof (Elf32_External_Rela);
3423 h->needs_copy = 1;
3424 }
3425
3426 align2 = bfd_log2 (h->size);
3427 if (align2 > h->root.u.def.section->alignment_power)
3428 align2 = h->root.u.def.section->alignment_power;
3429
3430 /* Align dynbss. */
3431 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
3432 if (align2 > bfd_get_section_alignment (dynobj, s)
3433 && !bfd_set_section_alignment (dynobj, s, align2))
3434 return FALSE;
3435
3436 /* Define the symbol as being at this point in the section. */
3437 h->root.u.def.section = s;
3438 h->root.u.def.value = s->size;
3439
3440 /* Increment the section size to make room for the symbol. */
3441 s->size += h->size;
3442
3443 return TRUE;
3444 }
3445
3446 /* Worker function for nios2_elf32_size_dynamic_sections. */
3447 static bfd_boolean
3448 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3449 {
3450 struct bfd_link_info *info;
3451 struct elf32_nios2_link_hash_table *htab;
3452
3453 if (h->root.type == bfd_link_hash_indirect)
3454 return TRUE;
3455
3456 if (h->root.type == bfd_link_hash_warning)
3457 /* When warning symbols are created, they **replace** the "real"
3458 entry in the hash table, thus we never get to see the real
3459 symbol in a hash traversal. So look at it now. */
3460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3461
3462 info = (struct bfd_link_info *) inf;
3463 htab = elf32_nios2_hash_table (info);
3464
3465 if (h->plt.offset != (bfd_vma)-1)
3466 h->plt.offset += htab->res_n_size;
3467 if (htab->root.splt == h->root.u.def.section)
3468 h->root.u.def.value += htab->res_n_size;
3469
3470 return TRUE;
3471 }
3472
3473 /* Another worker function for nios2_elf32_size_dynamic_sections.
3474 Allocate space in .plt, .got and associated reloc sections for
3475 dynamic relocs. */
3476 static bfd_boolean
3477 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3478 {
3479 struct bfd_link_info *info;
3480 struct elf32_nios2_link_hash_table *htab;
3481 struct elf32_nios2_link_hash_entry *eh;
3482 struct elf32_nios2_dyn_relocs *p;
3483 int use_plt;
3484
3485 if (h->root.type == bfd_link_hash_indirect)
3486 return TRUE;
3487
3488 if (h->root.type == bfd_link_hash_warning)
3489 /* When warning symbols are created, they **replace** the "real"
3490 entry in the hash table, thus we never get to see the real
3491 symbol in a hash traversal. So look at it now. */
3492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3493
3494 info = (struct bfd_link_info *) inf;
3495 htab = elf32_nios2_hash_table (info);
3496
3497 if (htab->root.dynamic_sections_created
3498 && h->plt.refcount > 0)
3499 {
3500 /* Make sure this symbol is output as a dynamic symbol.
3501 Undefined weak syms won't yet be marked as dynamic. */
3502 if (h->dynindx == -1
3503 && !h->forced_local
3504 && !bfd_elf_link_record_dynamic_symbol (info, h))
3505 return FALSE;
3506
3507 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3508 {
3509 asection *s = htab->root.splt;
3510
3511 /* Allocate room for the header. */
3512 if (s->size == 0)
3513 {
3514 if (info->shared)
3515 s->size = 24;
3516 else
3517 s->size = 28;
3518 }
3519
3520 h->plt.offset = s->size;
3521
3522 /* If this symbol is not defined in a regular file, and we are
3523 not generating a shared library, then set the symbol to this
3524 location in the .plt. This is required to make function
3525 pointers compare as equal between the normal executable and
3526 the shared library. */
3527 if (! info->shared
3528 && !h->def_regular)
3529 {
3530 h->root.u.def.section = s;
3531 h->root.u.def.value = h->plt.offset;
3532 }
3533
3534 /* Make room for this entry. */
3535 s->size += 12;
3536
3537 /* We also need to make an entry in the .rela.plt section. */
3538 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3539
3540 /* And the .got.plt section. */
3541 htab->root.sgotplt->size += 4;
3542 }
3543 else
3544 {
3545 h->plt.offset = (bfd_vma) -1;
3546 h->needs_plt = 0;
3547 }
3548 }
3549 else
3550 {
3551 h->plt.offset = (bfd_vma) -1;
3552 h->needs_plt = 0;
3553 }
3554
3555 eh = (struct elf32_nios2_link_hash_entry *) h;
3556 use_plt = (eh->got_types_used == CALL16_USED
3557 && h->plt.offset != (bfd_vma) -1);
3558
3559 if (h->got.refcount > 0)
3560 {
3561 asection *s;
3562 bfd_boolean dyn;
3563 int tls_type = eh->tls_type;
3564 int indx;
3565
3566 /* Make sure this symbol is output as a dynamic symbol.
3567 Undefined weak syms won't yet be marked as dynamic. */
3568 if (h->dynindx == -1
3569 && !h->forced_local
3570 && !bfd_elf_link_record_dynamic_symbol (info, h))
3571 return FALSE;
3572
3573 s = htab->root.sgot;
3574 h->got.offset = s->size;
3575
3576 if (tls_type == GOT_UNKNOWN)
3577 abort ();
3578
3579 if (tls_type == GOT_NORMAL)
3580 /* Non-TLS symbols need one GOT slot. */
3581 s->size += 4;
3582 else
3583 {
3584 if (tls_type & GOT_TLS_GD)
3585 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
3586 s->size += 8;
3587 if (tls_type & GOT_TLS_IE)
3588 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
3589 s->size += 4;
3590 }
3591
3592 dyn = htab->root.dynamic_sections_created;
3593
3594 indx = 0;
3595 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3596 && (!info->shared
3597 || !SYMBOL_REFERENCES_LOCAL (info, h)))
3598 indx = h->dynindx;
3599
3600 if (tls_type != GOT_NORMAL
3601 && (info->shared || indx != 0)
3602 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3603 || h->root.type != bfd_link_hash_undefweak))
3604 {
3605 if (tls_type & GOT_TLS_IE)
3606 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3607
3608 if (tls_type & GOT_TLS_GD)
3609 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3610
3611 if ((tls_type & GOT_TLS_GD) && indx != 0)
3612 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3613 }
3614 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3615 || h->root.type != bfd_link_hash_undefweak)
3616 && !use_plt
3617 && (info->shared
3618 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3619 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3620 }
3621 else
3622 h->got.offset = (bfd_vma) -1;
3623
3624 if (eh->dyn_relocs == NULL)
3625 return TRUE;
3626
3627 /* In the shared -Bsymbolic case, discard space allocated for
3628 dynamic pc-relative relocs against symbols which turn out to be
3629 defined in regular objects. For the normal shared case, discard
3630 space for pc-relative relocs that have become local due to symbol
3631 visibility changes. */
3632
3633 if (info->shared)
3634 {
3635 if (h->def_regular
3636 && (h->forced_local || info->symbolic))
3637 {
3638 struct elf32_nios2_dyn_relocs **pp;
3639
3640 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3641 {
3642 p->count -= p->pc_count;
3643 p->pc_count = 0;
3644 if (p->count == 0)
3645 *pp = p->next;
3646 else
3647 pp = &p->next;
3648 }
3649 }
3650
3651 /* Also discard relocs on undefined weak syms with non-default
3652 visibility. */
3653 if (eh->dyn_relocs != NULL
3654 && h->root.type == bfd_link_hash_undefweak)
3655 {
3656 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3657 eh->dyn_relocs = NULL;
3658
3659 /* Make sure undefined weak symbols are output as a dynamic
3660 symbol in PIEs. */
3661 else if (h->dynindx == -1
3662 && !h->forced_local
3663 && !bfd_elf_link_record_dynamic_symbol (info, h))
3664 return FALSE;
3665 }
3666 }
3667 else
3668 {
3669 /* For the non-shared case, discard space for relocs against
3670 symbols which turn out to need copy relocs or are not
3671 dynamic. */
3672
3673 if (!h->non_got_ref
3674 && ((h->def_dynamic && !h->def_regular)
3675 || (htab->root.dynamic_sections_created
3676 && (h->root.type == bfd_link_hash_undefweak
3677 || h->root.type == bfd_link_hash_undefined))))
3678 {
3679 /* Make sure this symbol is output as a dynamic symbol.
3680 Undefined weak syms won't yet be marked as dynamic. */
3681 if (h->dynindx == -1
3682 && !h->forced_local
3683 && !bfd_elf_link_record_dynamic_symbol (info, h))
3684 return FALSE;
3685
3686 /* If that succeeded, we know we'll be keeping all the
3687 relocs. */
3688 if (h->dynindx != -1)
3689 goto keep;
3690 }
3691
3692 eh->dyn_relocs = NULL;
3693
3694 keep: ;
3695 }
3696
3697 /* Finally, allocate space. */
3698 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3699 {
3700 asection *sreloc = elf_section_data (p->sec)->sreloc;
3701 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3702 }
3703
3704 return TRUE;
3705 }
3706
3707 /* Implement elf_backend_size_dynamic_sections:
3708 Set the sizes of the dynamic sections. */
3709 static bfd_boolean
3710 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3711 struct bfd_link_info *info)
3712 {
3713 bfd *dynobj;
3714 asection *s;
3715 bfd_boolean plt;
3716 bfd_boolean got;
3717 bfd_boolean relocs;
3718 bfd *ibfd;
3719 struct elf32_nios2_link_hash_table *htab;
3720
3721 htab = elf32_nios2_hash_table (info);
3722 dynobj = elf_hash_table (info)->dynobj;
3723 BFD_ASSERT (dynobj != NULL);
3724
3725 htab->res_n_size = 0;
3726 if (elf_hash_table (info)->dynamic_sections_created)
3727 {
3728 /* Set the contents of the .interp section to the interpreter. */
3729 if (info->executable)
3730 {
3731 s = bfd_get_linker_section (dynobj, ".interp");
3732 BFD_ASSERT (s != NULL);
3733 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3734 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3735 }
3736 }
3737 else
3738 {
3739 /* We may have created entries in the .rela.got section.
3740 However, if we are not creating the dynamic sections, we will
3741 not actually use these entries. Reset the size of .rela.got,
3742 which will cause it to get stripped from the output file
3743 below. */
3744 s = htab->root.srelgot;
3745 if (s != NULL)
3746 s->size = 0;
3747 }
3748
3749 /* Set up .got offsets for local syms, and space for local dynamic
3750 relocs. */
3751 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3752 {
3753 bfd_signed_vma *local_got;
3754 bfd_signed_vma *end_local_got;
3755 char *local_tls_type;
3756 bfd_size_type locsymcount;
3757 Elf_Internal_Shdr *symtab_hdr;
3758 asection *srel;
3759
3760 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3761 continue;
3762
3763 for (s = ibfd->sections; s != NULL; s = s->next)
3764 {
3765 struct elf32_nios2_dyn_relocs *p;
3766
3767 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
3768 {
3769 if (!bfd_is_abs_section (p->sec)
3770 && bfd_is_abs_section (p->sec->output_section))
3771 {
3772 /* Input section has been discarded, either because
3773 it is a copy of a linkonce section or due to
3774 linker script /DISCARD/, so we'll be discarding
3775 the relocs too. */
3776 }
3777 else if (p->count != 0)
3778 {
3779 srel = elf_section_data (p->sec)->sreloc;
3780 srel->size += p->count * sizeof (Elf32_External_Rela);
3781 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3782 info->flags |= DF_TEXTREL;
3783 }
3784 }
3785 }
3786
3787 local_got = elf_local_got_refcounts (ibfd);
3788 if (!local_got)
3789 continue;
3790
3791 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3792 locsymcount = symtab_hdr->sh_info;
3793 end_local_got = local_got + locsymcount;
3794 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
3795 s = htab->root.sgot;
3796 srel = htab->root.srelgot;
3797 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3798 {
3799 if (*local_got > 0)
3800 {
3801 *local_got = s->size;
3802 if (*local_tls_type & GOT_TLS_GD)
3803 /* TLS_GD relocs need an 8-byte structure in the GOT. */
3804 s->size += 8;
3805 if (*local_tls_type & GOT_TLS_IE)
3806 s->size += 4;
3807 if (*local_tls_type == GOT_NORMAL)
3808 s->size += 4;
3809
3810 if (info->shared || *local_tls_type == GOT_TLS_GD)
3811 srel->size += sizeof (Elf32_External_Rela);
3812 }
3813 else
3814 *local_got = (bfd_vma) -1;
3815 }
3816 }
3817
3818 if (htab->tls_ldm_got.refcount > 0)
3819 {
3820 /* Allocate two GOT entries and one dynamic relocation (if necessary)
3821 for R_NIOS2_TLS_LDM16 relocations. */
3822 htab->tls_ldm_got.offset = htab->root.sgot->size;
3823 htab->root.sgot->size += 8;
3824 if (info->shared)
3825 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3826 }
3827 else
3828 htab->tls_ldm_got.offset = -1;
3829
3830 /* Allocate global sym .plt and .got entries, and space for global
3831 sym dynamic relocs. */
3832 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3833
3834 /* The check_relocs and adjust_dynamic_symbol entry points have
3835 determined the sizes of the various dynamic sections. Allocate
3836 memory for them. */
3837 plt = FALSE;
3838 got = FALSE;
3839 relocs = FALSE;
3840 for (s = dynobj->sections; s != NULL; s = s->next)
3841 {
3842 const char *name;
3843
3844 if ((s->flags & SEC_LINKER_CREATED) == 0)
3845 continue;
3846
3847 /* It's OK to base decisions on the section name, because none
3848 of the dynobj section names depend upon the input files. */
3849 name = bfd_get_section_name (dynobj, s);
3850
3851 if (strcmp (name, ".plt") == 0)
3852 {
3853 /* Remember whether there is a PLT. */
3854 plt = s->size != 0;
3855
3856 /* Correct for the number of res_N branches. */
3857 if (plt && !info->shared)
3858 {
3859 htab->res_n_size = (s->size-28) / 3;
3860 s->size += htab->res_n_size;
3861 }
3862 }
3863 else if (CONST_STRNEQ (name, ".rela"))
3864 {
3865 if (s->size != 0)
3866 {
3867 relocs = TRUE;
3868
3869 /* We use the reloc_count field as a counter if we need
3870 to copy relocs into the output file. */
3871 s->reloc_count = 0;
3872 }
3873 }
3874 else if (CONST_STRNEQ (name, ".got"))
3875 got = s->size != 0;
3876 else if (strcmp (name, ".dynbss") != 0)
3877 /* It's not one of our sections, so don't allocate space. */
3878 continue;
3879
3880 if (s->size == 0)
3881 {
3882 /* If we don't need this section, strip it from the
3883 output file. This is mostly to handle .rela.bss and
3884 .rela.plt. We must create both sections in
3885 create_dynamic_sections, because they must be created
3886 before the linker maps input sections to output
3887 sections. The linker does that before
3888 adjust_dynamic_symbol is called, and it is that
3889 function which decides whether anything needs to go
3890 into these sections. */
3891 s->flags |= SEC_EXCLUDE;
3892 continue;
3893 }
3894
3895 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3896 continue;
3897
3898 /* Allocate memory for the section contents. */
3899 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3900 Unused entries should be reclaimed before the section's contents
3901 are written out, but at the moment this does not happen. Thus in
3902 order to prevent writing out garbage, we initialize the section's
3903 contents to zero. */
3904 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3905 if (s->contents == NULL)
3906 return FALSE;
3907 }
3908
3909 /* Adjust dynamic symbols that point to the plt to account for the
3910 now-known number of resN slots. */
3911 if (htab->res_n_size)
3912 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
3913
3914 if (elf_hash_table (info)->dynamic_sections_created)
3915 {
3916 /* Add some entries to the .dynamic section. We fill in the
3917 values later, in elf_nios2_finish_dynamic_sections, but we
3918 must add the entries now so that we get the correct size for
3919 the .dynamic section. The DT_DEBUG entry is filled in by the
3920 dynamic linker and used by the debugger. */
3921 #define add_dynamic_entry(TAG, VAL) \
3922 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3923
3924 if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0))
3925 return FALSE;
3926
3927 if (got && !add_dynamic_entry (DT_PLTGOT, 0))
3928 return FALSE;
3929
3930 if (plt
3931 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
3932 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3933 || !add_dynamic_entry (DT_JMPREL, 0)))
3934 return FALSE;
3935
3936 if (relocs
3937 && (!add_dynamic_entry (DT_RELA, 0)
3938 || !add_dynamic_entry (DT_RELASZ, 0)
3939 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
3940 return FALSE;
3941
3942 if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0))
3943 return FALSE;
3944
3945 if ((info->flags & DF_TEXTREL) != 0
3946 && !add_dynamic_entry (DT_TEXTREL, 0))
3947 return FALSE;
3948 }
3949 #undef add_dynamic_entry
3950
3951 return TRUE;
3952 }
3953
3954 /* Implement bfd_elf32_bfd_link_hash_table_create. */
3955 static struct bfd_link_hash_table *
3956 nios2_elf32_link_hash_table_create (bfd *abfd)
3957 {
3958 struct elf32_nios2_link_hash_table *ret;
3959 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
3960
3961 ret = bfd_malloc (amt);
3962 if (ret == NULL)
3963 return NULL;
3964
3965 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3966 link_hash_newfunc,
3967 sizeof (struct
3968 elf32_nios2_link_hash_entry),
3969 NIOS2_ELF_DATA))
3970 {
3971 free (ret);
3972 return NULL;
3973 }
3974
3975 ret->sdynbss = NULL;
3976 ret->srelbss = NULL;
3977 ret->sbss = NULL;
3978 ret->tls_ldm_got.refcount = 0;
3979 ret->sym_cache.abfd = NULL;
3980 return &ret->root.root;
3981 }
3982
3983 /* Implement elf_backend_reloc_type_class. */
3984 static enum elf_reloc_type_class
3985 nios2_elf32_reloc_type_class (const Elf_Internal_Rela *rela)
3986 {
3987 switch ((int) ELF32_R_TYPE (rela->r_info))
3988 {
3989 case R_NIOS2_RELATIVE:
3990 return reloc_class_relative;
3991 case R_NIOS2_JUMP_SLOT:
3992 return reloc_class_plt;
3993 case R_NIOS2_COPY:
3994 return reloc_class_copy;
3995 default:
3996 return reloc_class_normal;
3997 }
3998 }
3999
4000 /* Return 1 if target is one of ours. */
4001 static bfd_boolean
4002 is_nios2_elf_target (const struct bfd_target *targ)
4003 {
4004 return (targ == &bfd_elf32_littlenios2_vec
4005 || targ == &bfd_elf32_bignios2_vec);
4006 }
4007
4008 /* Implement elf_backend_add_symbol_hook.
4009 This hook is called by the linker when adding symbols from an object
4010 file. We use it to put .comm items in .sbss, and not .bss. */
4011 static bfd_boolean
4012 nios2_elf_add_symbol_hook (bfd *abfd,
4013 struct bfd_link_info *info,
4014 Elf_Internal_Sym *sym,
4015 const char **namep ATTRIBUTE_UNUSED,
4016 flagword *flagsp ATTRIBUTE_UNUSED,
4017 asection **secp,
4018 bfd_vma *valp)
4019 {
4020 bfd *dynobj;
4021
4022 if (sym->st_shndx == SHN_COMMON
4023 && !info->relocatable
4024 && sym->st_size <= elf_gp_size (abfd)
4025 && is_nios2_elf_target (info->output_bfd->xvec))
4026 {
4027 /* Common symbols less than or equal to -G nn bytes are automatically
4028 put into .sbss. */
4029 struct elf32_nios2_link_hash_table *htab;
4030
4031 htab = elf32_nios2_hash_table (info);
4032 if (htab->sbss == NULL)
4033 {
4034 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
4035
4036 dynobj = elf_hash_table (info)->dynobj;
4037 if (!dynobj)
4038 dynobj = abfd;
4039
4040 htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
4041 flags);
4042 if (htab->sbss == NULL)
4043 return FALSE;
4044 }
4045
4046 *secp = htab->sbss;
4047 *valp = sym->st_size;
4048 }
4049
4050 return TRUE;
4051 }
4052
4053 /* Implement elf_backend_can_make_relative_eh_frame:
4054 Decide whether to attempt to turn absptr or lsda encodings in
4055 shared libraries into pcrel within the given input section. */
4056 static bfd_boolean
4057 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
4058 struct bfd_link_info *info
4059 ATTRIBUTE_UNUSED,
4060 asection *eh_frame_section
4061 ATTRIBUTE_UNUSED)
4062 {
4063 /* We can't use PC-relative encodings in the .eh_frame section. */
4064 return FALSE;
4065 }
4066
4067 /* Implement elf_backend_special_sections. */
4068 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
4069 {
4070 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
4071 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4072 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
4073 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4074 { NULL, 0, 0, 0, 0 }
4075 };
4076
4077 #define ELF_ARCH bfd_arch_nios2
4078 #define ELF_TARGET_ID NIOS2_ELF_DATA
4079 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
4080
4081 /* The Nios II MMU uses a 4K page size. */
4082
4083 #define ELF_MAXPAGESIZE 0x1000
4084
4085 #define bfd_elf32_bfd_link_hash_table_create \
4086 nios2_elf32_link_hash_table_create
4087
4088 /* Relocation table lookup macros. */
4089
4090 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
4091 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
4092
4093 /* JUMP_TABLE_LINK macros. */
4094
4095 /* elf_info_to_howto (using RELA relocations). */
4096
4097 #define elf_info_to_howto nios2_elf32_info_to_howto
4098
4099 /* elf backend functions. */
4100
4101 #define elf_backend_can_gc_sections 1
4102 #define elf_backend_can_refcount 1
4103 #define elf_backend_plt_readonly 1
4104 #define elf_backend_want_got_plt 1
4105 #define elf_backend_rela_normal 1
4106
4107 #define elf_backend_relocate_section nios2_elf32_relocate_section
4108 #define elf_backend_section_flags nios2_elf32_section_flags
4109 #define elf_backend_fake_sections nios2_elf32_fake_sections
4110 #define elf_backend_check_relocs nios2_elf32_check_relocs
4111
4112 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
4113 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
4114 #define elf_backend_create_dynamic_sections \
4115 nios2_elf32_create_dynamic_sections
4116 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
4117 #define elf_backend_finish_dynamic_sections \
4118 nios2_elf32_finish_dynamic_sections
4119 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
4120 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
4121 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
4122 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
4123 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
4124
4125 #define elf_backend_grok_prstatus nios2_grok_prstatus
4126 #define elf_backend_grok_psinfo nios2_grok_psinfo
4127
4128 #undef elf_backend_can_make_relative_eh_frame
4129 #define elf_backend_can_make_relative_eh_frame \
4130 nios2_elf32_can_make_relative_eh_frame
4131
4132 #define elf_backend_special_sections elf32_nios2_special_sections
4133
4134 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
4135 #define TARGET_LITTLE_NAME "elf32-littlenios2"
4136 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
4137 #define TARGET_BIG_NAME "elf32-bignios2"
4138
4139 #define elf_backend_got_header_size 12
4140
4141 #include "elf32-target.h"
This page took 0.111501 seconds and 5 git commands to generate.