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