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