Pass link_info to _bfd_merge_private_bfd_data
[external/binutils.git] / bfd / elf32-nios2.c
1 /* 32-bit ELF support for Nios II.
2    Copyright (C) 2012-2016 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 /* The Nios II linker needs to keep track of the number of relocs that it
1730    decides to copy as dynamic relocs in check_relocs for each symbol.
1731    This is so that it can later discard them if they are found to be
1732    unnecessary.  We store the information in a field extending the
1733    regular ELF linker hash table.  */
1734
1735 struct elf32_nios2_dyn_relocs
1736 {
1737   struct elf32_nios2_dyn_relocs *next;
1738
1739   /* The input section of the reloc.  */
1740   asection *sec;
1741
1742   /* Total number of relocs copied for the input section.  */
1743   bfd_size_type count;
1744
1745   /* Number of pc-relative relocs copied for the input section.  */
1746   bfd_size_type pc_count;
1747 };
1748
1749 /* Nios II ELF linker hash entry.  */
1750
1751 struct elf32_nios2_link_hash_entry
1752 {
1753   struct elf_link_hash_entry root;
1754
1755   /* A pointer to the most recently used stub hash entry against this
1756      symbol.  */
1757   struct elf32_nios2_stub_hash_entry *hsh_cache;
1758
1759   /* Track dynamic relocs copied for this symbol.  */
1760   struct elf32_nios2_dyn_relocs *dyn_relocs;
1761
1762 #define GOT_UNKNOWN     0
1763 #define GOT_NORMAL      1
1764 #define GOT_TLS_GD      2
1765 #define GOT_TLS_IE      4
1766   unsigned char tls_type;
1767
1768   /* We need to detect and take special action for symbols which are only
1769      referenced with %call() and not with %got().  Such symbols do not need
1770      a dynamic GOT reloc in shared objects, only a dynamic PLT reloc.  Lazy
1771      linking will not work if the dynamic GOT reloc exists.
1772      To check for this condition efficiently, we compare got_types_used against
1773      CALL_USED, meaning
1774      (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1775   */
1776 #define GOT_USED        1
1777 #define CALL_USED       2
1778   unsigned char got_types_used;
1779 };
1780
1781 #define elf32_nios2_hash_entry(ent) \
1782   ((struct elf32_nios2_link_hash_entry *) (ent))
1783
1784 /* Get the Nios II elf linker hash table from a link_info structure.  */
1785 #define elf32_nios2_hash_table(info) \
1786   ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1787
1788 /* Nios II ELF linker hash table.  */
1789 struct elf32_nios2_link_hash_table
1790   {
1791     /* The main hash table.  */
1792     struct elf_link_hash_table root;
1793
1794     /* The stub hash table.  */
1795     struct bfd_hash_table bstab;
1796
1797     /* Linker stub bfd.  */
1798     bfd *stub_bfd;
1799
1800     /* Linker call-backs.  */
1801     asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
1802     void (*layout_sections_again) (void);
1803
1804     /* Array to keep track of which stub sections have been created, and
1805        information on stub grouping.  */
1806     struct map_stub
1807     {
1808       /* These are the section to which stubs in the group will be
1809          attached.  */
1810       asection *first_sec, *last_sec;
1811       /* The stub sections.  There might be stubs inserted either before
1812          or after the real section.*/
1813       asection *first_stub_sec, *last_stub_sec;
1814     } *stub_group;
1815
1816     /* Assorted information used by nios2_elf32_size_stubs.  */
1817     unsigned int bfd_count;
1818     unsigned int top_index;
1819     asection **input_list;
1820     Elf_Internal_Sym **all_local_syms;
1821
1822     /* Short-cuts to get to dynamic linker sections.  */
1823     asection *sdynbss;
1824     asection *srelbss;
1825     asection *sbss;
1826
1827     /* GOT pointer symbol _gp_got.  */
1828     struct elf_link_hash_entry *h_gp_got;
1829
1830     union {
1831       bfd_signed_vma refcount;
1832       bfd_vma offset;
1833     } tls_ldm_got;
1834
1835     /* Small local sym cache.  */
1836     struct sym_cache sym_cache;
1837
1838     bfd_vma res_n_size;
1839   };
1840
1841 struct nios2_elf32_obj_tdata
1842 {
1843   struct elf_obj_tdata root;
1844
1845   /* tls_type for each local got entry.  */
1846   char *local_got_tls_type;
1847
1848   /* TRUE if TLS GD relocs have been seen for this object.  */
1849   bfd_boolean has_tlsgd;
1850 };
1851
1852 #define elf32_nios2_tdata(abfd) \
1853   ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1854
1855 #define elf32_nios2_local_got_tls_type(abfd) \
1856   (elf32_nios2_tdata (abfd)->local_got_tls_type)
1857
1858 /* The name of the dynamic interpreter.  This is put in the .interp
1859    section.  */
1860 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1861
1862 /* PLT implementation for position-dependent code.  */
1863 static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1864   0x03c00034,   /* movhi r15, %hiadj(plt_got_slot_address) */
1865   0x7bc00017,   /* ldw r15, %lo(plt_got_slot_address)(r15) */
1866   0x7800683a    /* jmp r15 */
1867 };
1868
1869 static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1870   0x03800034,   /* movhi r14, %hiadj(res_0) */
1871   0x73800004,   /* addi r14, r14, %lo(res_0) */
1872   0x7b9fc83a,   /* sub r15, r15, r14 */
1873   0x03400034,   /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1874   0x6b800017,   /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1875   0x6b400017,   /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1876   0x6800683a    /* jmp r13 */
1877 };
1878
1879 /* PLT implementation for position-independent code.  */
1880 static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1881   0x03c00034,   /* movhi r15, %hiadj(index * 4) */
1882   0x7bc00004,   /* addi r15, r15, %lo(index * 4) */
1883   0x00000006    /* br .PLTresolve */
1884 };
1885
1886 static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1887   0x001ce03a,   /* nextpc r14 */
1888   0x03400034,   /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1889   0x6b9b883a,   /* add r13, r13, r14 */
1890   0x6b800017,   /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1891   0x6b400017,   /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1892   0x6800683a    /* jmp r13 */
1893 };
1894
1895 /* CALL26 stub.  */
1896 static const bfd_vma nios2_call26_stub_entry[] = {
1897   0x00400034,   /* orhi at, r0, %hiadj(dest) */
1898   0x08400004,   /* addi at, at, %lo(dest) */
1899   0x0800683a    /* jmp at */
1900 };
1901
1902 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC.  */
1903 static void
1904 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1905 {
1906   bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1907
1908   BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
1909
1910   bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1911               sec->contents + offset);
1912 }
1913
1914 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1915    section SEC. */
1916 static void
1917 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1918                           int count)
1919 {
1920   while (count--)
1921     {
1922       bfd_put_32 (sec->owner, *data, sec->contents + offset);
1923       offset += 4;
1924       ++data;
1925     }
1926 }
1927
1928 /* The usual way of loading a 32-bit constant into a Nios II register is to
1929    load the high 16 bits in one instruction and then add the low 16 bits with
1930    a signed add. This means that the high halfword needs to be adjusted to
1931    compensate for the sign bit of the low halfword. This function returns the
1932    adjusted high halfword for a given 32-bit constant.  */
1933 static
1934 bfd_vma hiadj (bfd_vma symbol_value)
1935 {
1936   return ((symbol_value + 0x8000) >> 16) & 0xffff;
1937 }
1938
1939 /* Implement elf_backend_grok_prstatus:
1940    Support for core dump NOTE sections.  */
1941 static bfd_boolean
1942 nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1943 {
1944   int offset;
1945   size_t size;
1946
1947   switch (note->descsz)
1948     {
1949     default:
1950       return FALSE;
1951
1952     case 212:         /* Linux/Nios II */
1953       /* pr_cursig */
1954       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1955
1956       /* pr_pid */
1957       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
1958
1959       /* pr_reg */
1960       offset = 72;
1961       size = 136;
1962
1963       break;
1964     }
1965
1966   /* Make a ".reg/999" section.  */
1967   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1968                                           size, note->descpos + offset);
1969 }
1970
1971 /* Implement elf_backend_grok_psinfo.  */
1972 static bfd_boolean
1973 nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1974 {
1975   switch (note->descsz)
1976     {
1977     default:
1978       return FALSE;
1979
1980     case 124:         /* Linux/Nios II elf_prpsinfo */
1981       elf_tdata (abfd)->core->program
1982         = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1983       elf_tdata (abfd)->core->command
1984         = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1985     }
1986
1987   /* Note that for some reason, a spurious space is tacked
1988      onto the end of the args in some (at least one anyway)
1989      implementations, so strip it off if it exists.  */
1990
1991   {
1992     char *command = elf_tdata (abfd)->core->command;
1993     int n = strlen (command);
1994
1995     if (0 < n && command[n - 1] == ' ')
1996       command[n - 1] = '\0';
1997   }
1998
1999   return TRUE;
2000 }
2001
2002 /* Assorted hash table functions.  */
2003
2004 /* Initialize an entry in the stub hash table.  */
2005 static struct bfd_hash_entry *
2006 stub_hash_newfunc (struct bfd_hash_entry *entry,
2007                    struct bfd_hash_table *table,
2008                    const char *string)
2009 {
2010   /* Allocate the structure if it has not already been allocated by a
2011      subclass.  */
2012   if (entry == NULL)
2013     {
2014       entry = bfd_hash_allocate (table,
2015                                  sizeof (struct elf32_nios2_stub_hash_entry));
2016       if (entry == NULL)
2017         return entry;
2018     }
2019
2020   /* Call the allocation method of the superclass.  */
2021   entry = bfd_hash_newfunc (entry, table, string);
2022   if (entry != NULL)
2023     {
2024       struct elf32_nios2_stub_hash_entry *hsh;
2025
2026       /* Initialize the local fields.  */
2027       hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2028       hsh->stub_sec = NULL;
2029       hsh->stub_offset = 0;
2030       hsh->target_value = 0;
2031       hsh->target_section = NULL;
2032       hsh->stub_type = nios2_stub_none;
2033       hsh->hh = NULL;
2034       hsh->id_sec = NULL;
2035     }
2036
2037   return entry;
2038 }
2039
2040 /* Create an entry in a Nios II ELF linker hash table.  */
2041 static struct bfd_hash_entry *
2042 link_hash_newfunc (struct bfd_hash_entry *entry,
2043                    struct bfd_hash_table *table, const char *string)
2044 {
2045   /* Allocate the structure if it has not already been allocated by a
2046      subclass.  */
2047   if (entry == NULL)
2048     {
2049       entry = bfd_hash_allocate (table,
2050                                  sizeof (struct elf32_nios2_link_hash_entry));
2051       if (entry == NULL)
2052         return entry;
2053     }
2054
2055   /* Call the allocation method of the superclass.  */
2056   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2057   if (entry)
2058     {
2059       struct elf32_nios2_link_hash_entry *eh;
2060
2061       eh = (struct elf32_nios2_link_hash_entry *) entry;
2062       eh->hsh_cache = NULL;
2063       eh->dyn_relocs = NULL;
2064       eh->tls_type = GOT_UNKNOWN;
2065       eh->got_types_used = 0;
2066     }
2067
2068   return entry;
2069 }
2070
2071 /* Section name for stubs is the associated section name plus this
2072    string.  */
2073 #define STUB_SUFFIX ".stub"
2074
2075 /* Build a name for an entry in the stub hash table.  */
2076 static char *
2077 nios2_stub_name (const asection *input_section,
2078                  const asection *sym_sec,
2079                  const struct elf32_nios2_link_hash_entry *hh,
2080                  const Elf_Internal_Rela *rel,
2081                  enum elf32_nios2_stub_type stub_type)
2082 {
2083   char *stub_name;
2084   bfd_size_type len;
2085   char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2086
2087   if (hh)
2088     {
2089       len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2090       stub_name = bfd_malloc (len);
2091       if (stub_name != NULL)
2092         {
2093           sprintf (stub_name, "%08x_%c_%s+%x",
2094                    input_section->id & 0xffffffff,
2095                    stubpos,
2096                    hh->root.root.root.string,
2097                    (int) rel->r_addend & 0xffffffff);
2098         }
2099     }
2100   else
2101     {
2102       len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2103       stub_name = bfd_malloc (len);
2104       if (stub_name != NULL)
2105         {
2106           sprintf (stub_name, "%08x_%c_%x:%x+%x",
2107                    input_section->id & 0xffffffff,
2108                    stubpos,
2109                    sym_sec->id & 0xffffffff,
2110                    (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2111                    (int) rel->r_addend & 0xffffffff);
2112         }
2113     }
2114   return stub_name;
2115 }
2116
2117 /* Look up an entry in the stub hash.  Stub entries are cached because
2118    creating the stub name takes a bit of time.  */
2119 static struct elf32_nios2_stub_hash_entry *
2120 nios2_get_stub_entry (const asection *input_section,
2121                       const asection *sym_sec,
2122                       struct elf32_nios2_link_hash_entry *hh,
2123                       const Elf_Internal_Rela *rel,
2124                       struct elf32_nios2_link_hash_table *htab,
2125                       enum elf32_nios2_stub_type stub_type)
2126 {
2127   struct elf32_nios2_stub_hash_entry *hsh;
2128   const asection *id_sec;
2129
2130   /* If this input section is part of a group of sections sharing one
2131      stub section, then use the id of the first/last section in the group,
2132      depending on the stub section placement relative to the group.
2133      Stub names need to include a section id, as there may well be
2134      more than one stub used to reach say, printf, and we need to
2135      distinguish between them.  */
2136   if (stub_type == nios2_stub_call26_before)
2137     id_sec = htab->stub_group[input_section->id].first_sec;
2138   else
2139     id_sec = htab->stub_group[input_section->id].last_sec;
2140
2141   if (hh != NULL && hh->hsh_cache != NULL
2142       && hh->hsh_cache->hh == hh
2143       && hh->hsh_cache->id_sec == id_sec
2144       && hh->hsh_cache->stub_type == stub_type)
2145     {
2146       hsh = hh->hsh_cache;
2147     }
2148   else
2149     {
2150       char *stub_name;
2151
2152       stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2153       if (stub_name == NULL)
2154         return NULL;
2155
2156       hsh = nios2_stub_hash_lookup (&htab->bstab,
2157                                     stub_name, FALSE, FALSE);
2158
2159       if (hh != NULL)
2160         hh->hsh_cache = hsh;
2161
2162       free (stub_name);
2163     }
2164
2165   return hsh;
2166 }
2167
2168 /* Add a new stub entry to the stub hash.  Not all fields of the new
2169    stub entry are initialised.  */
2170 static struct elf32_nios2_stub_hash_entry *
2171 nios2_add_stub (const char *stub_name,
2172                 asection *section,
2173                 struct elf32_nios2_link_hash_table *htab,
2174                 enum elf32_nios2_stub_type stub_type)
2175 {
2176   asection *link_sec;
2177   asection *stub_sec;
2178   asection **secptr, **linkptr;
2179   struct elf32_nios2_stub_hash_entry *hsh;
2180   bfd_boolean afterp;
2181
2182   if (stub_type == nios2_stub_call26_before)
2183     {
2184       link_sec = htab->stub_group[section->id].first_sec;
2185       secptr = &(htab->stub_group[section->id].first_stub_sec);
2186       linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2187       afterp = FALSE;
2188     }
2189   else
2190     {
2191       link_sec = htab->stub_group[section->id].last_sec;
2192       secptr = &(htab->stub_group[section->id].last_stub_sec);
2193       linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2194       afterp = TRUE;
2195     }
2196   stub_sec = *secptr;
2197   if (stub_sec == NULL)
2198     {
2199       stub_sec = *linkptr;
2200       if (stub_sec == NULL)
2201         {
2202           size_t namelen;
2203           bfd_size_type len;
2204           char *s_name;
2205
2206           namelen = strlen (link_sec->name);
2207           len = namelen + sizeof (STUB_SUFFIX);
2208           s_name = bfd_alloc (htab->stub_bfd, len);
2209           if (s_name == NULL)
2210             return NULL;
2211
2212           memcpy (s_name, link_sec->name, namelen);
2213           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2214
2215           stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2216           if (stub_sec == NULL)
2217             return NULL;
2218           *linkptr = stub_sec;
2219         }
2220       *secptr = stub_sec;
2221     }
2222
2223   /* Enter this entry into the linker stub hash table.  */
2224   hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2225                                 TRUE, FALSE);
2226   if (hsh == NULL)
2227     {
2228       _bfd_error_handler (_("%B: cannot create stub entry %s"),
2229                           section->owner,
2230                           stub_name);
2231       return NULL;
2232     }
2233
2234   hsh->stub_sec = stub_sec;
2235   hsh->stub_offset = 0;
2236   hsh->id_sec = link_sec;
2237   return hsh;
2238 }
2239
2240 /* Set up various things so that we can make a list of input sections
2241    for each output section included in the link.  Returns -1 on error,
2242    0 when no stubs will be needed, and 1 on success.  */
2243 int
2244 nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2245 {
2246   bfd *input_bfd;
2247   unsigned int bfd_count;
2248   unsigned int top_id, top_index;
2249   asection *section;
2250   asection **input_list, **list;
2251   bfd_size_type amt;
2252   struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2253
2254   /* Count the number of input BFDs and find the top input section id.  */
2255   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2256        input_bfd != NULL;
2257        input_bfd = input_bfd->link.next)
2258     {
2259       bfd_count += 1;
2260       for (section = input_bfd->sections;
2261            section != NULL;
2262            section = section->next)
2263         {
2264           if (top_id < section->id)
2265             top_id = section->id;
2266         }
2267     }
2268
2269   htab->bfd_count = bfd_count;
2270
2271   amt = sizeof (struct map_stub) * (top_id + 1);
2272   htab->stub_group = bfd_zmalloc (amt);
2273   if (htab->stub_group == NULL)
2274     return -1;
2275
2276   /* We can't use output_bfd->section_count here to find the top output
2277      section index as some sections may have been removed, and
2278      strip_excluded_output_sections doesn't renumber the indices.  */
2279   for (section = output_bfd->sections, top_index = 0;
2280        section != NULL;
2281        section = section->next)
2282     {
2283       if (top_index < section->index)
2284         top_index = section->index;
2285     }
2286
2287   htab->top_index = top_index;
2288   amt = sizeof (asection *) * (top_index + 1);
2289   input_list = bfd_malloc (amt);
2290   htab->input_list = input_list;
2291   if (input_list == NULL)
2292     return -1;
2293
2294   /* For sections we aren't interested in, mark their entries with a
2295      value we can check later.  */
2296   list = input_list + top_index;
2297   do
2298     *list = bfd_abs_section_ptr;
2299   while (list-- != input_list);
2300
2301   for (section = output_bfd->sections;
2302        section != NULL;
2303        section = section->next)
2304     {
2305       /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2306        * have PC relative relocs in them but no code flag set.  */
2307       if (((section->flags & SEC_CODE) != 0) ||
2308           strcmp(".ctors", section->name) ||
2309           strcmp(".dtors", section->name))
2310         input_list[section->index] = NULL;
2311     }
2312
2313   return 1;
2314 }
2315
2316 /* The linker repeatedly calls this function for each input section,
2317    in the order that input sections are linked into output sections.
2318    Build lists of input sections to determine groupings between which
2319    we may insert linker stubs.  */
2320 void
2321 nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2322 {
2323   struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2324
2325   if (isec->output_section->index <= htab->top_index)
2326     {
2327       asection **list = htab->input_list + isec->output_section->index;
2328       if (*list != bfd_abs_section_ptr)
2329         {
2330           /* Steal the last_sec pointer for our list.
2331              This happens to make the list in reverse order,
2332              which is what we want.  */
2333           htab->stub_group[isec->id].last_sec = *list;
2334           *list = isec;
2335         }
2336     }
2337 }
2338
2339 /* Segment mask for CALL26 relocation relaxation.  */
2340 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2341
2342 /* Fudge factor for approximate maximum size of all stubs that might
2343    be inserted by the linker.  This does not actually limit the number
2344    of stubs that might be inserted, and only affects strategy for grouping
2345    and placement of stubs.  Perhaps this should be computed based on number
2346    of relocations seen, or be specifiable on the command line.  */
2347 #define MAX_STUB_SECTION_SIZE 0xffff
2348
2349 /* See whether we can group stub sections together.  Grouping stub
2350    sections may result in fewer stubs.  More importantly, we need to
2351    put all .init* and .fini* stubs at the end of the .init or
2352    .fini output sections respectively, because glibc splits the
2353    _init and _fini functions into multiple parts.  Putting a stub in
2354    the middle of a function is not a good idea.
2355    Rather than computing groups of a maximum fixed size, for Nios II
2356    CALL26 relaxation it makes more sense to compute the groups based on
2357    sections that fit within a 256MB address segment.  Also do not allow
2358    a group to span more than one output section, since different output
2359    sections might correspond to different memory banks on a bare-metal
2360    target, etc.  */
2361 static void
2362 group_sections (struct elf32_nios2_link_hash_table *htab)
2363 {
2364   asection **list = htab->input_list + htab->top_index;
2365   do
2366     {
2367       /* The list is in reverse order so we'll search backwards looking
2368          for the first section that begins in the same memory segment,
2369          marking sections along the way to point at the tail for this
2370          group.  */
2371       asection *tail = *list;
2372       if (tail == bfd_abs_section_ptr)
2373         continue;
2374       while (tail != NULL)
2375         {
2376           bfd_vma start = tail->output_section->vma + tail->output_offset;
2377           bfd_vma end = start + tail->size;
2378           bfd_vma segment = CALL26_SEGMENT (end);
2379           asection *prev;
2380
2381           if (segment != CALL26_SEGMENT (start)
2382               || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2383             /* This section spans more than one memory segment, or is
2384                close enough to the end of the segment that adding stub
2385                sections before it might cause it to move so that it
2386                spans memory segments, or that stubs added at the end of
2387                this group might overflow into the next memory segment.
2388                Put it in a group by itself to localize the effects.  */
2389             {
2390               prev = htab->stub_group[tail->id].last_sec;
2391               htab->stub_group[tail->id].last_sec = tail;
2392               htab->stub_group[tail->id].first_sec = tail;
2393             }
2394           else
2395             /* Collect more sections for this group.  */
2396             {
2397               asection *curr, *first;
2398               for (curr = tail; ; curr = prev)
2399                 {
2400                   prev = htab->stub_group[curr->id].last_sec;
2401                   if (!prev
2402                       || tail->output_section != prev->output_section
2403                       || (CALL26_SEGMENT (prev->output_section->vma
2404                                           + prev->output_offset)
2405                           != segment))
2406                     break;
2407                 }
2408               first = curr;
2409               for (curr = tail; ; curr = prev)
2410                 {
2411                   prev = htab->stub_group[curr->id].last_sec;
2412                   htab->stub_group[curr->id].last_sec = tail;
2413                   htab->stub_group[curr->id].first_sec = first;
2414                   if (curr == first)
2415                     break;
2416                 }
2417             }
2418
2419           /* Reset tail for the next group.  */
2420           tail = prev;
2421         }
2422     }
2423   while (list-- != htab->input_list);
2424   free (htab->input_list);
2425 }
2426
2427 /* Determine the type of stub needed, if any, for a call.  */
2428 static enum elf32_nios2_stub_type
2429 nios2_type_of_stub (asection *input_sec,
2430                     const Elf_Internal_Rela *rel,
2431                     struct elf32_nios2_link_hash_entry *hh,
2432                     struct elf32_nios2_link_hash_table *htab,
2433                     bfd_vma destination,
2434                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
2435 {
2436   bfd_vma location, segment, start, end;
2437   asection *s0, *s1, *s;
2438
2439   if (hh != NULL &&
2440       !(hh->root.root.type == bfd_link_hash_defined
2441         || hh->root.root.type == bfd_link_hash_defweak))
2442     return nios2_stub_none;
2443
2444   /* Determine where the call point is.  */
2445   location = (input_sec->output_section->vma
2446               + input_sec->output_offset + rel->r_offset);
2447   segment = CALL26_SEGMENT (location);
2448
2449   /* Nios II CALL and JMPI instructions can transfer control to addresses
2450      within the same 256MB segment as the PC.  */
2451   if (segment == CALL26_SEGMENT (destination))
2452     return nios2_stub_none;
2453
2454   /* Find the start and end addresses of the stub group.  Also account for
2455      any already-created stub sections for this group.  Note that for stubs
2456      in the end section, only the first instruction of the last stub
2457      (12 bytes long) needs to be within range.  */
2458   s0 = htab->stub_group[input_sec->id].first_sec;
2459   s = htab->stub_group[s0->id].first_stub_sec;
2460   if (s != NULL && s->size > 0)
2461     start = s->output_section->vma + s->output_offset;
2462   else
2463     start = s0->output_section->vma + s0->output_offset;
2464
2465   s1 = htab->stub_group[input_sec->id].last_sec;
2466   s = htab->stub_group[s1->id].last_stub_sec;
2467   if (s != NULL && s->size > 0)
2468     end = s->output_section->vma + s->output_offset + s->size - 8;
2469   else
2470     end = s1->output_section->vma + s1->output_offset + s1->size;
2471
2472   BFD_ASSERT (start < end);
2473   BFD_ASSERT (start <= location);
2474   BFD_ASSERT (location < end);
2475
2476   /* Put stubs at the end of the group unless that is not a valid
2477      location and the beginning of the group is.  It might be that
2478      neither the beginning nor end works if we have an input section
2479      so large that it spans multiple segment boundaries.  In that
2480      case, punt; the end result will be a relocation overflow error no
2481      matter what we do here.
2482
2483      Note that adding stubs pushes up the addresses of all subsequent
2484      sections, so that stubs allocated on one pass through the
2485      relaxation loop may not be valid on the next pass.  (E.g., we may
2486      allocate a stub at the beginning of the section on one pass and
2487      find that the call site has been bumped into the next memory
2488      segment on the next pass.)  The important thing to note is that
2489      we never try to reclaim the space allocated to such unused stubs,
2490      so code size and section addresses can only increase with each
2491      iteration.  Accounting for the start and end addresses of the
2492      already-created stub sections ensures that when the algorithm
2493      converges, it converges accurately, with the entire appropriate
2494      stub section accessible from the call site and not just the
2495      address at the start or end of the stub group proper.  */
2496
2497   if (segment == CALL26_SEGMENT (end))
2498     return nios2_stub_call26_after;
2499   else if (segment == CALL26_SEGMENT (start))
2500     return nios2_stub_call26_before;
2501   else
2502     /* Perhaps this should be a dedicated error code.  */
2503     return nios2_stub_none;
2504 }
2505
2506 static bfd_boolean
2507 nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2508 {
2509   struct elf32_nios2_stub_hash_entry *hsh
2510     = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2511   asection *stub_sec = hsh->stub_sec;
2512   bfd_vma sym_value;
2513
2514   /* Make a note of the offset within the stubs for this entry.  */
2515   hsh->stub_offset = stub_sec->size;
2516
2517   switch (hsh->stub_type)
2518     {
2519     case nios2_stub_call26_before:
2520     case nios2_stub_call26_after:
2521       /* A call26 stub looks like:
2522            orhi at, %hiadj(dest)
2523            addi at, at, %lo(dest)
2524            jmp at
2525          Note that call/jmpi instructions can't be used in PIC code
2526          so there is no reason for the stub to be PIC, either.  */
2527       sym_value = (hsh->target_value
2528                    + hsh->target_section->output_offset
2529                    + hsh->target_section->output_section->vma
2530                    + hsh->addend);
2531
2532       nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2533                                 hsh->stub_offset, 3);
2534       nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2535                                  hiadj (sym_value));
2536       nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2537                                  (sym_value & 0xffff));
2538       stub_sec->size += 12;
2539       break;
2540     default:
2541       BFD_FAIL ();
2542       return FALSE;
2543     }
2544
2545   return TRUE;
2546 }
2547
2548 /* As above, but don't actually build the stub.  Just bump offset so
2549    we know stub section sizes.  */
2550 static bfd_boolean
2551 nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2552 {
2553   struct elf32_nios2_stub_hash_entry *hsh
2554     = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2555
2556   switch (hsh->stub_type)
2557     {
2558     case nios2_stub_call26_before:
2559     case nios2_stub_call26_after:
2560       hsh->stub_sec->size += 12;
2561       break;
2562     default:
2563       BFD_FAIL ();
2564       return FALSE;
2565     }
2566   return TRUE;
2567 }
2568
2569 /* Read in all local syms for all input bfds.
2570    Returns -1 on error, 0 otherwise.  */
2571
2572 static int
2573 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2574                 struct bfd_link_info *info)
2575 {
2576   unsigned int bfd_indx;
2577   Elf_Internal_Sym *local_syms, **all_local_syms;
2578   struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2579
2580   /* We want to read in symbol extension records only once.  To do this
2581      we need to read in the local symbols in parallel and save them for
2582      later use; so hold pointers to the local symbols in an array.  */
2583   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2584   all_local_syms = bfd_zmalloc (amt);
2585   htab->all_local_syms = all_local_syms;
2586   if (all_local_syms == NULL)
2587     return -1;
2588
2589   /* Walk over all the input BFDs, swapping in local symbols.  */
2590   for (bfd_indx = 0;
2591        input_bfd != NULL;
2592        input_bfd = input_bfd->link.next, bfd_indx++)
2593     {
2594       Elf_Internal_Shdr *symtab_hdr;
2595
2596       /* We'll need the symbol table in a second.  */
2597       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2598       if (symtab_hdr->sh_info == 0)
2599         continue;
2600
2601       /* We need an array of the local symbols attached to the input bfd.  */
2602       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2603       if (local_syms == NULL)
2604         {
2605           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2606                                              symtab_hdr->sh_info, 0,
2607                                              NULL, NULL, NULL);
2608           /* Cache them for elf_link_input_bfd.  */
2609           symtab_hdr->contents = (unsigned char *) local_syms;
2610         }
2611       if (local_syms == NULL)
2612         return -1;
2613
2614       all_local_syms[bfd_indx] = local_syms;
2615     }
2616
2617   return 0;
2618 }
2619
2620 /* Determine and set the size of the stub section for a final link.  */
2621 bfd_boolean
2622 nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2623                         struct bfd_link_info *info,
2624                         asection *(*add_stub_section) (const char *,
2625                                                        asection *, bfd_boolean),
2626                         void (*layout_sections_again) (void))
2627 {
2628   bfd_boolean stub_changed = FALSE;
2629   struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2630
2631   /* Stash our params away.  */
2632   htab->stub_bfd = stub_bfd;
2633   htab->add_stub_section = add_stub_section;
2634   htab->layout_sections_again = layout_sections_again;
2635
2636   /* FIXME: We only compute the section groups once.  This could cause
2637      problems if adding a large stub section causes following sections,
2638      or parts of them, to move into another segment.  However, this seems
2639      to be consistent with the way other back ends handle this....  */
2640   group_sections (htab);
2641
2642   if (get_local_syms (output_bfd, info->input_bfds, info))
2643     {
2644       if (htab->all_local_syms)
2645         goto error_ret_free_local;
2646       return FALSE;
2647     }
2648
2649   while (1)
2650     {
2651       bfd *input_bfd;
2652       unsigned int bfd_indx;
2653       asection *stub_sec;
2654
2655       for (input_bfd = info->input_bfds, bfd_indx = 0;
2656            input_bfd != NULL;
2657            input_bfd = input_bfd->link.next, bfd_indx++)
2658         {
2659           Elf_Internal_Shdr *symtab_hdr;
2660           asection *section;
2661           Elf_Internal_Sym *local_syms;
2662
2663           /* We'll need the symbol table in a second.  */
2664           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2665           if (symtab_hdr->sh_info == 0)
2666             continue;
2667
2668           local_syms = htab->all_local_syms[bfd_indx];
2669
2670           /* Walk over each section attached to the input bfd.  */
2671           for (section = input_bfd->sections;
2672                section != NULL;
2673                section = section->next)
2674             {
2675               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2676
2677               /* If there aren't any relocs, then there's nothing more
2678                  to do.  */
2679               if ((section->flags & SEC_RELOC) == 0
2680                   || section->reloc_count == 0)
2681                 continue;
2682
2683               /* If this section is a link-once section that will be
2684                  discarded, then don't create any stubs.  */
2685               if (section->output_section == NULL
2686                   || section->output_section->owner != output_bfd)
2687                 continue;
2688
2689               /* Get the relocs.  */
2690               internal_relocs
2691                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2692                                              info->keep_memory);
2693               if (internal_relocs == NULL)
2694                 goto error_ret_free_local;
2695
2696               /* Now examine each relocation.  */
2697               irela = internal_relocs;
2698               irelaend = irela + section->reloc_count;
2699               for (; irela < irelaend; irela++)
2700                 {
2701                   unsigned int r_type, r_indx;
2702                   enum elf32_nios2_stub_type stub_type;
2703                   struct elf32_nios2_stub_hash_entry *hsh;
2704                   asection *sym_sec;
2705                   bfd_vma sym_value;
2706                   bfd_vma destination;
2707                   struct elf32_nios2_link_hash_entry *hh;
2708                   char *stub_name;
2709                   const asection *id_sec;
2710
2711                   r_type = ELF32_R_TYPE (irela->r_info);
2712                   r_indx = ELF32_R_SYM (irela->r_info);
2713
2714                   if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2715                     {
2716                       bfd_set_error (bfd_error_bad_value);
2717                     error_ret_free_internal:
2718                       if (elf_section_data (section)->relocs == NULL)
2719                         free (internal_relocs);
2720                       goto error_ret_free_local;
2721                     }
2722
2723                   /* Only look for stubs on CALL and JMPI instructions.  */
2724                   if (r_type != (unsigned int) R_NIOS2_CALL26)
2725                     continue;
2726
2727                   /* Now determine the call target, its name, value,
2728                      section.  */
2729                   sym_sec = NULL;
2730                   sym_value = 0;
2731                   destination = 0;
2732                   hh = NULL;
2733                   if (r_indx < symtab_hdr->sh_info)
2734                     {
2735                       /* It's a local symbol.  */
2736                       Elf_Internal_Sym *sym;
2737                       Elf_Internal_Shdr *hdr;
2738                       unsigned int shndx;
2739
2740                       sym = local_syms + r_indx;
2741                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2742                         sym_value = sym->st_value;
2743                       shndx = sym->st_shndx;
2744                       if (shndx < elf_numsections (input_bfd))
2745                         {
2746                           hdr = elf_elfsections (input_bfd)[shndx];
2747                           sym_sec = hdr->bfd_section;
2748                           destination = (sym_value + irela->r_addend
2749                                          + sym_sec->output_offset
2750                                          + sym_sec->output_section->vma);
2751                         }
2752                     }
2753                   else
2754                     {
2755                       /* It's an external symbol.  */
2756                       int e_indx;
2757
2758                       e_indx = r_indx - symtab_hdr->sh_info;
2759                       hh = ((struct elf32_nios2_link_hash_entry *)
2760                             elf_sym_hashes (input_bfd)[e_indx]);
2761
2762                       while (hh->root.root.type == bfd_link_hash_indirect
2763                              || hh->root.root.type == bfd_link_hash_warning)
2764                         hh = ((struct elf32_nios2_link_hash_entry *)
2765                               hh->root.root.u.i.link);
2766
2767                       if (hh->root.root.type == bfd_link_hash_defined
2768                           || hh->root.root.type == bfd_link_hash_defweak)
2769                         {
2770                           sym_sec = hh->root.root.u.def.section;
2771                           sym_value = hh->root.root.u.def.value;
2772
2773                           if (sym_sec->output_section != NULL)
2774                             destination = (sym_value + irela->r_addend
2775                                            + sym_sec->output_offset
2776                                            + sym_sec->output_section->vma);
2777                           else
2778                             continue;
2779                         }
2780                       else if (hh->root.root.type == bfd_link_hash_undefweak)
2781                         {
2782                           if (! bfd_link_pic (info))
2783                             continue;
2784                         }
2785                       else if (hh->root.root.type == bfd_link_hash_undefined)
2786                         {
2787                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
2788                                  && (ELF_ST_VISIBILITY (hh->root.other)
2789                                      == STV_DEFAULT)))
2790                             continue;
2791                         }
2792                       else
2793                         {
2794                           bfd_set_error (bfd_error_bad_value);
2795                           goto error_ret_free_internal;
2796                         }
2797                     }
2798
2799                   /* Determine what (if any) linker stub is needed.  */
2800                   stub_type = nios2_type_of_stub (section, irela, hh, htab,
2801                                                   destination, info);
2802                   if (stub_type == nios2_stub_none)
2803                     continue;
2804
2805                   /* Support for grouping stub sections.  */
2806                   if (stub_type == nios2_stub_call26_before)
2807                     id_sec = htab->stub_group[section->id].first_sec;
2808                   else
2809                     id_sec = htab->stub_group[section->id].last_sec;
2810
2811                   /* Get the name of this stub.  */
2812                   stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2813                                                stub_type);
2814                   if (!stub_name)
2815                     goto error_ret_free_internal;
2816
2817                   hsh = nios2_stub_hash_lookup (&htab->bstab,
2818                                                 stub_name,
2819                                                 FALSE, FALSE);
2820                   if (hsh != NULL)
2821                     {
2822                       /* The proper stub has already been created.  */
2823                       free (stub_name);
2824                       continue;
2825                     }
2826
2827                   hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2828                   if (hsh == NULL)
2829                     {
2830                       free (stub_name);
2831                       goto error_ret_free_internal;
2832                     }
2833                   hsh->target_value = sym_value;
2834                   hsh->target_section = sym_sec;
2835                   hsh->stub_type = stub_type;
2836                   hsh->hh = hh;
2837                   hsh->addend = irela->r_addend;
2838                   stub_changed = TRUE;
2839                 }
2840
2841               /* We're done with the internal relocs, free them.  */
2842               if (elf_section_data (section)->relocs == NULL)
2843                 free (internal_relocs);
2844             }
2845         }
2846
2847       if (!stub_changed)
2848         break;
2849
2850       /* OK, we've added some stubs.  Find out the new size of the
2851          stub sections.  */
2852       for (stub_sec = htab->stub_bfd->sections;
2853            stub_sec != NULL;
2854            stub_sec = stub_sec->next)
2855         stub_sec->size = 0;
2856
2857       bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2858
2859       /* Ask the linker to do its stuff.  */
2860       (*htab->layout_sections_again) ();
2861       stub_changed = FALSE;
2862     }
2863
2864   free (htab->all_local_syms);
2865   return TRUE;
2866
2867  error_ret_free_local:
2868   free (htab->all_local_syms);
2869   return FALSE;
2870 }
2871
2872 /* Build all the stubs associated with the current output file.  The
2873    stubs are kept in a hash table attached to the main linker hash
2874    table.  This function is called via nios2elf_finish in the linker.  */
2875 bfd_boolean
2876 nios2_elf32_build_stubs (struct bfd_link_info *info)
2877 {
2878   asection *stub_sec;
2879   struct bfd_hash_table *table;
2880   struct elf32_nios2_link_hash_table *htab;
2881
2882   htab = elf32_nios2_hash_table (info);
2883
2884   for (stub_sec = htab->stub_bfd->sections;
2885        stub_sec != NULL;
2886        stub_sec = stub_sec->next)
2887     /* The stub_bfd may contain non-stub sections if it is also the
2888        dynobj.  Any such non-stub sections are created with the
2889        SEC_LINKER_CREATED flag set, while stub sections do not
2890        have that flag.  Ignore any non-stub sections here.  */
2891     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2892       {
2893         bfd_size_type size;
2894
2895         /* Allocate memory to hold the linker stubs.  */
2896         size = stub_sec->size;
2897         stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2898         if (stub_sec->contents == NULL && size != 0)
2899           return FALSE;
2900         stub_sec->size = 0;
2901       }
2902
2903   /* Build the stubs as directed by the stub hash table.  */
2904   table = &htab->bstab;
2905   bfd_hash_traverse (table, nios2_build_one_stub, info);
2906
2907   return TRUE;
2908 }
2909
2910
2911 #define is_nios2_elf(bfd) \
2912   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2913    && elf_object_id (bfd) == NIOS2_ELF_DATA)
2914
2915 /* Merge backend specific data from an object file to the output
2916    object file when linking.  */
2917
2918 static bfd_boolean
2919 nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2920 {
2921   bfd *obfd = info->output_bfd;
2922   flagword old_flags;
2923   flagword new_flags;
2924
2925   if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2926     return TRUE;
2927
2928   /* Check if we have the same endianness.  */
2929   if (! _bfd_generic_verify_endian_match (ibfd, info))
2930     return FALSE;
2931
2932   new_flags = elf_elfheader (ibfd)->e_flags;
2933   old_flags = elf_elfheader (obfd)->e_flags;
2934   if (!elf_flags_init (obfd))
2935     {
2936       /* First call, no flags set.  */
2937       elf_flags_init (obfd) = TRUE;
2938       elf_elfheader (obfd)->e_flags = new_flags;
2939
2940       switch (new_flags)
2941         {
2942         default:
2943         case EF_NIOS2_ARCH_R1:
2944           bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2945           break;
2946         case EF_NIOS2_ARCH_R2:
2947           if (bfd_big_endian (ibfd))
2948             {
2949               _bfd_error_handler
2950                 (_("error: %B: Big-endian R2 is not supported."), ibfd);
2951               bfd_set_error (bfd_error_bad_value);
2952               return FALSE;
2953             }
2954           bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2955           break;
2956         }
2957     }
2958
2959   /* Incompatible flags.  */
2960   else if (new_flags != old_flags)
2961     {
2962       /* So far, the only incompatible flags denote incompatible
2963          architectures.  */
2964       _bfd_error_handler
2965         (_("error: %B: Conflicting CPU architectures %d/%d"),
2966          ibfd, new_flags, old_flags);
2967       bfd_set_error (bfd_error_bad_value);
2968       return FALSE;
2969     }
2970
2971   /* Merge Tag_compatibility attributes and any common GNU ones.  */
2972   _bfd_elf_merge_object_attributes (ibfd, info);
2973
2974   return TRUE;
2975 }
2976
2977
2978 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2979    Given a BFD reloc type, return a howto structure.  */
2980 static reloc_howto_type *
2981 nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
2982                                    bfd_reloc_code_real_type code)
2983 {
2984   int i;
2985
2986   for (i = 0;
2987        i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
2988        ++i)
2989     if (nios2_reloc_map[i].bfd_val == code)
2990       return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
2991   return NULL;
2992 }
2993
2994 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2995    Given a reloc name, return a howto structure.  */
2996 static reloc_howto_type *
2997 nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
2998                                    const char *r_name)
2999 {
3000   int i;
3001   reloc_howto_type *howto_tbl;
3002   int howto_tbl_size;
3003
3004   if (BFD_IS_R2 (abfd))
3005     {
3006       howto_tbl = elf_nios2_r2_howto_table_rel;
3007       howto_tbl_size = (int) (sizeof (elf_nios2_r2_howto_table_rel)
3008                               / sizeof (elf_nios2_r2_howto_table_rel[0]));
3009     }
3010   else
3011     {
3012       howto_tbl = elf_nios2_r1_howto_table_rel;
3013       howto_tbl_size = (int) (sizeof (elf_nios2_r1_howto_table_rel)
3014                               / sizeof (elf_nios2_r1_howto_table_rel[0]));
3015     }
3016
3017   for (i = 0; i < howto_tbl_size; i++)
3018     if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
3019       return howto_tbl + i;
3020   return NULL;
3021 }
3022
3023 /* Implement elf_info_to_howto:
3024    Given a ELF32 relocation, fill in a arelent structure.  */
3025 static void
3026 nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3027                            Elf_Internal_Rela *dst)
3028 {
3029   unsigned int r_type;
3030
3031   r_type = ELF32_R_TYPE (dst->r_info);
3032   cache_ptr->howto = lookup_howto (r_type, abfd);
3033 }
3034
3035 /* Return the base VMA address which should be subtracted from real addresses
3036    when resolving @dtpoff relocation.
3037    This is PT_TLS segment p_vaddr.  */
3038 static bfd_vma
3039 dtpoff_base (struct bfd_link_info *info)
3040 {
3041   /* If tls_sec is NULL, we should have signalled an error already.  */
3042   if (elf_hash_table (info)->tls_sec == NULL)
3043     return 0;
3044   return elf_hash_table (info)->tls_sec->vma;
3045 }
3046
3047 /* Return the relocation value for @tpoff relocation
3048    if STT_TLS virtual address is ADDRESS.  */
3049 static bfd_vma
3050 tpoff (struct bfd_link_info *info, bfd_vma address)
3051 {
3052   struct elf_link_hash_table *htab = elf_hash_table (info);
3053
3054   /* If tls_sec is NULL, we should have signalled an error already.  */
3055   if (htab->tls_sec == NULL)
3056     return 0;
3057   return address - htab->tls_sec->vma;
3058 }
3059
3060 /* Set the GP value for OUTPUT_BFD.  Returns FALSE if this is a
3061    dangerous relocation.  */
3062 static bfd_boolean
3063 nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3064 {
3065
3066   bfd_boolean gp_found;
3067   struct bfd_hash_entry *h;
3068   struct bfd_link_hash_entry *lh;
3069
3070   /* If we've already figured out what GP will be, just return it. */
3071   *pgp = _bfd_get_gp_value (output_bfd);
3072   if (*pgp)
3073     return TRUE;
3074
3075   h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
3076   lh = (struct bfd_link_hash_entry *) h;
3077 lookup:
3078   if (lh)
3079     {
3080       switch (lh->type)
3081         {
3082         case bfd_link_hash_undefined:
3083         case bfd_link_hash_undefweak:
3084         case bfd_link_hash_common:
3085           gp_found = FALSE;
3086           break;
3087         case bfd_link_hash_defined:
3088         case bfd_link_hash_defweak:
3089           gp_found = TRUE;
3090           {
3091             asection *sym_sec = lh->u.def.section;
3092             bfd_vma sym_value = lh->u.def.value;
3093
3094             if (sym_sec->output_section)
3095               sym_value = (sym_value + sym_sec->output_offset
3096                            + sym_sec->output_section->vma);
3097             *pgp = sym_value;
3098           }
3099           break;
3100         case bfd_link_hash_indirect:
3101         case bfd_link_hash_warning:
3102           lh = lh->u.i.link;
3103           /* @@FIXME  ignoring warning for now */
3104           goto lookup;
3105         case bfd_link_hash_new:
3106         default:
3107           abort ();
3108         }
3109     }
3110   else
3111     gp_found = FALSE;
3112
3113   if (!gp_found)
3114     {
3115       /* Only get the error once. */
3116       *pgp = 4;
3117       _bfd_set_gp_value (output_bfd, *pgp);
3118       return FALSE;
3119     }
3120
3121   _bfd_set_gp_value (output_bfd, *pgp);
3122
3123   return TRUE;
3124 }
3125
3126 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3127    if it's not available as we don't have a link_info pointer available here
3128    to look it up in the output symbol table.  We don't need to adjust the
3129    symbol value for an external symbol if we are producing relocatable
3130    output.  */
3131 static bfd_reloc_status_type
3132 nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
3133                     char **error_message, bfd_vma *pgp)
3134 {
3135   if (bfd_is_und_section (symbol->section) && !relocatable)
3136     {
3137       *pgp = 0;
3138       return bfd_reloc_undefined;
3139     }
3140
3141   *pgp = _bfd_get_gp_value (output_bfd);
3142   if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3143     {
3144       if (relocatable)
3145         {
3146           /* Make up a value.  */
3147           *pgp = symbol->section->output_section->vma + 0x4000;
3148           _bfd_set_gp_value (output_bfd, *pgp);
3149         }
3150       else
3151         {
3152           *error_message
3153             = (char *) _("global pointer relative relocation when _gp not defined");
3154           return bfd_reloc_dangerous;
3155         }
3156     }
3157
3158   return bfd_reloc_ok;
3159 }
3160
3161 /* Do the relocations that require special handling.  */
3162 static bfd_reloc_status_type
3163 nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
3164                               asection *input_section,
3165                               bfd_byte *data, bfd_vma offset,
3166                               bfd_vma symbol_value, bfd_vma addend)
3167 {
3168   symbol_value = symbol_value + addend;
3169   addend = 0;
3170   symbol_value = (symbol_value >> 16) & 0xffff;
3171   return _bfd_final_link_relocate (howto, abfd, input_section,
3172                                    data, offset, symbol_value, addend);
3173 }
3174
3175 static bfd_reloc_status_type
3176 nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3177                               asection *input_section,
3178                               bfd_byte *data, bfd_vma offset,
3179                               bfd_vma symbol_value, bfd_vma addend)
3180 {
3181   symbol_value = symbol_value + addend;
3182   addend = 0;
3183   symbol_value = symbol_value & 0xffff;
3184   return _bfd_final_link_relocate (howto, abfd, input_section,
3185                                    data, offset, symbol_value, addend);
3186 }
3187
3188 static bfd_reloc_status_type
3189 nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3190                                  asection *input_section,
3191                                  bfd_byte *data, bfd_vma offset,
3192                                  bfd_vma symbol_value, bfd_vma addend)
3193 {
3194   symbol_value = symbol_value + addend;
3195   addend = 0;
3196   symbol_value = hiadj(symbol_value);
3197   return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3198                                    symbol_value, addend);
3199 }
3200
3201 static bfd_reloc_status_type
3202 nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3203                                     asection *input_section,
3204                                     bfd_byte *data, bfd_vma offset,
3205                                     bfd_vma symbol_value, bfd_vma addend)
3206 {
3207   symbol_value = symbol_value + addend;
3208   addend = 0;
3209   symbol_value = symbol_value & 0xffff;
3210   return _bfd_final_link_relocate (howto, abfd, input_section,
3211                                    data, offset, symbol_value, addend);
3212 }
3213
3214 static bfd_reloc_status_type
3215 nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3216                                        asection *input_section,
3217                                        bfd_byte *data, bfd_vma offset,
3218                                        bfd_vma symbol_value, bfd_vma addend)
3219 {
3220   symbol_value = symbol_value + addend;
3221   symbol_value -= (input_section->output_section->vma
3222                    + input_section->output_offset);
3223   symbol_value -= offset;
3224   addend = 0;
3225   symbol_value = hiadj(symbol_value);
3226   return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3227                                    symbol_value, addend);
3228 }
3229
3230 static bfd_reloc_status_type
3231 nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
3232                                  asection *input_section,
3233                                  bfd_byte *data, bfd_vma offset,
3234                                  bfd_vma symbol_value, bfd_vma addend)
3235 {
3236   /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3237      so we need to subtract 4 before doing a final_link_relocate. */
3238   symbol_value = symbol_value + addend - 4;
3239   addend = 0;
3240   return _bfd_final_link_relocate (howto, abfd, input_section,
3241                                    data, offset, symbol_value, addend);
3242 }
3243
3244 static bfd_reloc_status_type
3245 nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
3246                                 asection *input_section,
3247                                 bfd_byte *data, bfd_vma offset,
3248                                 bfd_vma symbol_value, bfd_vma addend)
3249 {
3250   /* Check that the relocation is in the same page as the current address.  */
3251   if (CALL26_SEGMENT (symbol_value + addend)
3252       != CALL26_SEGMENT (input_section->output_section->vma
3253                          + input_section->output_offset
3254                          + offset))
3255     return bfd_reloc_overflow;
3256
3257   /* Check that the target address is correctly aligned on a 4-byte
3258      boundary.  */
3259   if ((symbol_value + addend) & 0x3)
3260     return bfd_reloc_overflow;
3261
3262   return _bfd_final_link_relocate (howto, abfd, input_section,
3263                                    data, offset, symbol_value, addend);
3264 }
3265
3266 static bfd_reloc_status_type
3267 nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
3268                                asection *input_section,
3269                                bfd_byte *data, bfd_vma offset,
3270                                bfd_vma symbol_value, bfd_vma addend)
3271 {
3272   /* Because we need the output_bfd, the special handling is done
3273      in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate.  */
3274   return _bfd_final_link_relocate (howto, abfd, input_section,
3275                                    data, offset, symbol_value, addend);
3276 }
3277
3278 static bfd_reloc_status_type
3279 nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
3280                               asection *input_section,
3281                               bfd_byte *data, bfd_vma offset,
3282                               bfd_vma symbol_value, bfd_vma addend)
3283 {
3284   bfd_vma symbol_lo16, symbol_hi16;
3285   bfd_reloc_status_type r;
3286   symbol_value = symbol_value + addend;
3287   addend = 0;
3288   symbol_hi16 = (symbol_value >> 16) & 0xffff;
3289   symbol_lo16 = symbol_value & 0xffff;
3290
3291   r = _bfd_final_link_relocate (howto, abfd, input_section,
3292                                 data, offset, symbol_hi16, addend);
3293
3294   if (r == bfd_reloc_ok)
3295     return _bfd_final_link_relocate (howto, abfd, input_section,
3296                                      data, offset + 4, symbol_lo16, addend);
3297
3298   return r;
3299 }
3300
3301 static bfd_reloc_status_type
3302 nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
3303                               asection *input_section,
3304                               bfd_byte *data, bfd_vma offset,
3305                               bfd_vma symbol_value, bfd_vma addend)
3306 {
3307   bfd_vma symbol_lo16, symbol_hi16;
3308   bfd_reloc_status_type r;
3309   symbol_value = symbol_value + addend;
3310   addend = 0;
3311   symbol_hi16 = (symbol_value >> 16) & 0xffff;
3312   symbol_lo16 = symbol_value & 0xffff;
3313
3314   r = _bfd_final_link_relocate (howto, abfd, input_section,
3315                                 data, offset, symbol_hi16, addend);
3316
3317   if (r == bfd_reloc_ok)
3318     return _bfd_final_link_relocate (howto, abfd, input_section,
3319                                      data, offset + 4, symbol_lo16, addend);
3320
3321   return r;
3322 }
3323
3324 static bfd_reloc_status_type
3325 nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
3326                                asection *input_section,
3327                                bfd_byte *data, bfd_vma offset,
3328                                bfd_vma symbol_value, bfd_vma addend)
3329 {
3330   bfd_vma symbol_lo16, symbol_hi16;
3331   bfd_reloc_status_type r;
3332   symbol_value = symbol_value + addend;
3333   addend = 0;
3334   symbol_hi16 = (symbol_value >> 16) & 0xffff;
3335   symbol_lo16 = symbol_value & 0xffff;
3336
3337   r = _bfd_final_link_relocate (howto, abfd, input_section,
3338                                 data, offset, symbol_hi16, addend);
3339
3340   if (r == bfd_reloc_ok)
3341     return _bfd_final_link_relocate (howto, abfd, input_section,
3342                                      data, offset + 4, symbol_lo16, addend);
3343
3344   return r;
3345 }
3346
3347 /* HOWTO handlers for relocations that require special handling.  */
3348
3349 /* This is for relocations used only when relaxing to ensure
3350    changes in size of section don't screw up .align.  */
3351 static bfd_reloc_status_type
3352 nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3353                           asymbol *symbol ATTRIBUTE_UNUSED,
3354                           void *data ATTRIBUTE_UNUSED, asection *input_section,
3355                           bfd *output_bfd,
3356                           char **error_message ATTRIBUTE_UNUSED)
3357 {
3358   if (output_bfd != NULL)
3359     reloc_entry->address += input_section->output_offset;
3360   return bfd_reloc_ok;
3361 }
3362
3363 static bfd_reloc_status_type
3364 nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3365                            void *data, asection *input_section,
3366                            bfd *output_bfd,
3367                            char **error_message ATTRIBUTE_UNUSED)
3368 {
3369   /* This part is from bfd_elf_generic_reloc.  */
3370   if (output_bfd != NULL
3371       && (symbol->flags & BSF_SECTION_SYM) == 0
3372       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3373     {
3374       reloc_entry->address += input_section->output_offset;
3375       return bfd_reloc_ok;
3376     }
3377
3378   if (output_bfd != NULL)
3379     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3380     return bfd_reloc_continue;
3381
3382   return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3383                                        input_section,
3384                                        data, reloc_entry->address,
3385                                        (symbol->value
3386                                         + symbol->section->output_section->vma
3387                                         + symbol->section->output_offset),
3388                                        reloc_entry->addend);
3389 }
3390
3391 static bfd_reloc_status_type
3392 nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3393                            void *data, asection *input_section,
3394                            bfd *output_bfd,
3395                            char **error_message ATTRIBUTE_UNUSED)
3396 {
3397   /* This part is from bfd_elf_generic_reloc.  */
3398   if (output_bfd != NULL
3399       && (symbol->flags & BSF_SECTION_SYM) == 0
3400       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3401     {
3402       reloc_entry->address += input_section->output_offset;
3403       return bfd_reloc_ok;
3404     }
3405
3406   if (output_bfd != NULL)
3407     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3408     return bfd_reloc_continue;
3409
3410   return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3411                                        input_section,
3412                                        data, reloc_entry->address,
3413                                        (symbol->value
3414                                         + symbol->section->output_section->vma
3415                                         + symbol->section->output_offset),
3416                                        reloc_entry->addend);
3417 }
3418
3419 static bfd_reloc_status_type
3420 nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3421                               void *data, asection *input_section,
3422                               bfd *output_bfd,
3423                               char **error_message ATTRIBUTE_UNUSED)
3424 {
3425   /* This part is from bfd_elf_generic_reloc.  */
3426   if (output_bfd != NULL
3427       && (symbol->flags & BSF_SECTION_SYM) == 0
3428       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3429     {
3430       reloc_entry->address += input_section->output_offset;
3431       return bfd_reloc_ok;
3432     }
3433
3434   if (output_bfd != NULL)
3435     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3436     return bfd_reloc_continue;
3437
3438   return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3439                                           input_section,
3440                                           data, reloc_entry->address,
3441                                           (symbol->value
3442                                            + symbol->section->output_section->vma
3443                                            + symbol->section->output_offset),
3444                                           reloc_entry->addend);
3445 }
3446
3447 static bfd_reloc_status_type
3448 nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3449                                  asymbol *symbol, void *data,
3450                                  asection *input_section, bfd *output_bfd,
3451                                  char **error_message ATTRIBUTE_UNUSED)
3452 {
3453   /* This part is from bfd_elf_generic_reloc.  */
3454   if (output_bfd != NULL
3455       && (symbol->flags & BSF_SECTION_SYM) == 0
3456       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3457     {
3458       reloc_entry->address += input_section->output_offset;
3459       return bfd_reloc_ok;
3460     }
3461
3462   if (output_bfd != NULL)
3463     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3464     return bfd_reloc_continue;
3465
3466   return nios2_elf32_do_pcrel_lo16_relocate (
3467     abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3468     (symbol->value + symbol->section->output_section->vma
3469      + symbol->section->output_offset),
3470     reloc_entry->addend);
3471 }
3472
3473 static bfd_reloc_status_type
3474 nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3475                                     asymbol *symbol, void *data,
3476                                     asection *input_section, bfd *output_bfd,
3477                                     char **error_message ATTRIBUTE_UNUSED)
3478 {
3479   /* This part is from bfd_elf_generic_reloc.  */
3480   if (output_bfd != NULL
3481       && (symbol->flags & BSF_SECTION_SYM) == 0
3482       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3483     {
3484       reloc_entry->address += input_section->output_offset;
3485       return bfd_reloc_ok;
3486     }
3487
3488   if (output_bfd != NULL)
3489     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3490     return bfd_reloc_continue;
3491
3492   return nios2_elf32_do_pcrel_hiadj16_relocate (
3493     abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3494     (symbol->value + symbol->section->output_section->vma
3495      + symbol->section->output_offset),
3496     reloc_entry->addend);
3497 }
3498
3499 static bfd_reloc_status_type
3500 nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3501                               void *data, asection *input_section,
3502                               bfd *output_bfd,
3503                               char **error_message ATTRIBUTE_UNUSED)
3504 {
3505   /* This part is from bfd_elf_generic_reloc.  */
3506   if (output_bfd != NULL
3507       && (symbol->flags & BSF_SECTION_SYM) == 0
3508       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3509     {
3510       reloc_entry->address += input_section->output_offset;
3511       return bfd_reloc_ok;
3512     }
3513
3514   if (output_bfd != NULL)
3515     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3516     return bfd_reloc_continue;
3517
3518   return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3519                                           input_section,
3520                                           data, reloc_entry->address,
3521                                           (symbol->value
3522                                            + symbol->section->output_section->vma
3523                                            + symbol->section->output_offset),
3524                                           reloc_entry->addend);
3525 }
3526
3527 static bfd_reloc_status_type
3528 nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3529                              void *data, asection *input_section,
3530                              bfd *output_bfd,
3531                              char **error_message ATTRIBUTE_UNUSED)
3532 {
3533   /* This part is from bfd_elf_generic_reloc.  */
3534   if (output_bfd != NULL
3535       && (symbol->flags & BSF_SECTION_SYM) == 0
3536       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3537     {
3538       reloc_entry->address += input_section->output_offset;
3539       return bfd_reloc_ok;
3540     }
3541
3542   if (output_bfd != NULL)
3543     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3544     return bfd_reloc_continue;
3545
3546   return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3547                                          input_section,
3548                                          data, reloc_entry->address,
3549                                          (symbol->value
3550                                           + symbol->section->output_section->vma
3551                                           + symbol->section->output_offset),
3552                                          reloc_entry->addend);
3553 }
3554
3555 static bfd_reloc_status_type
3556 nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3557                             void *data, asection *input_section,
3558                             bfd *output_bfd, char **msg)
3559 {
3560   bfd_vma relocation;
3561   bfd_vma gp;
3562   bfd_reloc_status_type r;
3563
3564
3565   /* This part is from bfd_elf_generic_reloc.  */
3566   if (output_bfd != NULL
3567       && (symbol->flags & BSF_SECTION_SYM) == 0
3568       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3569     {
3570       reloc_entry->address += input_section->output_offset;
3571       return bfd_reloc_ok;
3572     }
3573
3574   if (output_bfd != NULL)
3575     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3576     return bfd_reloc_continue;
3577
3578   relocation = (symbol->value
3579                 + symbol->section->output_section->vma
3580                 + symbol->section->output_offset);
3581
3582   /* This assumes we've already cached the _gp symbol.  */
3583   r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
3584   if (r == bfd_reloc_ok)
3585     {
3586       relocation = relocation + reloc_entry->addend - gp;
3587       reloc_entry->addend = 0;
3588       if ((signed) relocation < -32768 || (signed) relocation > 32767)
3589         {
3590           *msg = _("global pointer relative address out of range");
3591           r = bfd_reloc_outofrange;
3592         }
3593       else
3594         r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3595                                            input_section,
3596                                            data, reloc_entry->address,
3597                                            relocation, reloc_entry->addend);
3598     }
3599
3600   return r;
3601 }
3602
3603 static bfd_reloc_status_type
3604 nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3605                            void *data, asection *input_section,
3606                            bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3607 {
3608   /* This part is from bfd_elf_generic_reloc.  */
3609   if (output_bfd != NULL
3610       && (symbol->flags & BSF_SECTION_SYM) == 0
3611       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3612     {
3613       reloc_entry->address += input_section->output_offset;
3614       return bfd_reloc_ok;
3615     }
3616
3617   if (output_bfd != NULL)
3618     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3619     return bfd_reloc_continue;
3620
3621   return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3622                                        input_section,
3623                                        data, reloc_entry->address,
3624                                        (symbol->value
3625                                         + symbol->section->output_section->vma
3626                                         + symbol->section->output_offset),
3627                                        reloc_entry->addend);
3628 }
3629
3630 static bfd_reloc_status_type
3631 nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3632                            void *data, asection *input_section,
3633                            bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3634 {
3635   /* This part is from bfd_elf_generic_reloc.  */
3636   if (output_bfd != NULL
3637       && (symbol->flags & BSF_SECTION_SYM) == 0
3638       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3639     {
3640       reloc_entry->address += input_section->output_offset;
3641       return bfd_reloc_ok;
3642     }
3643
3644   if (output_bfd != NULL)
3645     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3646     return bfd_reloc_continue;
3647
3648   return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3649                                        input_section,
3650                                        data, reloc_entry->address,
3651                                        (symbol->value
3652                                         + symbol->section->output_section->vma
3653                                         + symbol->section->output_offset),
3654                                        reloc_entry->addend);
3655 }
3656
3657 static bfd_reloc_status_type
3658 nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3659                             void *data, asection *input_section,
3660                             bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3661 {
3662   /* This part is from bfd_elf_generic_reloc.  */
3663   if (output_bfd != NULL
3664       && (symbol->flags & BSF_SECTION_SYM) == 0
3665       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3666     {
3667       reloc_entry->address += input_section->output_offset;
3668       return bfd_reloc_ok;
3669     }
3670
3671   if (output_bfd != NULL)
3672     /* FIXME: See bfd_perform_relocation.  Is this right?  */
3673     return bfd_reloc_continue;
3674
3675   return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3676                                         input_section,
3677                                         data, reloc_entry->address,
3678                                         (symbol->value
3679                                          + symbol->section->output_section->vma
3680                                          + symbol->section->output_offset),
3681                                         reloc_entry->addend);
3682 }
3683
3684
3685 /* Implement elf_backend_relocate_section.  */
3686 static bfd_boolean
3687 nios2_elf32_relocate_section (bfd *output_bfd,
3688                               struct bfd_link_info *info,
3689                               bfd *input_bfd,
3690                               asection *input_section,
3691                               bfd_byte *contents,
3692                               Elf_Internal_Rela *relocs,
3693                               Elf_Internal_Sym *local_syms,
3694                               asection **local_sections)
3695 {
3696   Elf_Internal_Shdr *symtab_hdr;
3697   struct elf_link_hash_entry **sym_hashes;
3698   Elf_Internal_Rela *rel;
3699   Elf_Internal_Rela *relend;
3700   struct elf32_nios2_link_hash_table *htab;
3701   asection *sgot;
3702   asection *splt;
3703   asection *sreloc = NULL;
3704   bfd_vma *local_got_offsets;
3705   bfd_vma got_base;
3706
3707   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3708   sym_hashes = elf_sym_hashes (input_bfd);
3709   relend = relocs + input_section->reloc_count;
3710
3711   htab = elf32_nios2_hash_table (info);
3712   sgot = htab->root.sgot;
3713   splt = htab->root.splt;
3714   local_got_offsets = elf_local_got_offsets (input_bfd);
3715
3716   if (elf32_nios2_hash_table (info)->h_gp_got == NULL)
3717     got_base = 0;
3718   else
3719     got_base = elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value;
3720
3721   for (rel = relocs; rel < relend; rel++)
3722     {
3723       reloc_howto_type *howto;
3724       unsigned long r_symndx;
3725       Elf_Internal_Sym *sym;
3726       asection *sec;
3727       struct elf_link_hash_entry *h;
3728       struct elf32_nios2_link_hash_entry *eh;
3729       bfd_vma relocation;
3730       bfd_vma gp;
3731       bfd_reloc_status_type r = bfd_reloc_ok;
3732       const char *name = NULL;
3733       int r_type;
3734       const char *format;
3735       char msgbuf[256];
3736       const char* msg = (const char*) NULL;
3737       bfd_boolean unresolved_reloc;
3738       bfd_vma off;
3739       int use_plt;
3740
3741       r_type = ELF32_R_TYPE (rel->r_info);
3742       r_symndx = ELF32_R_SYM (rel->r_info);
3743
3744       howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
3745       h = NULL;
3746       sym = NULL;
3747       sec = NULL;
3748
3749       if (r_symndx < symtab_hdr->sh_info)
3750         {
3751           sym = local_syms + r_symndx;
3752           sec = local_sections[r_symndx];
3753           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3754         }
3755       else
3756         {
3757           bfd_boolean warned, ignored;
3758
3759           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3760                                    r_symndx, symtab_hdr, sym_hashes,
3761                                    h, sec, relocation,
3762                                    unresolved_reloc, warned, ignored);
3763         }
3764
3765       if (sec && discarded_section (sec))
3766         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3767                                          rel, 1, relend, howto, 0, contents);
3768
3769       /* Nothing more to do unless this is a final link.  */
3770       if (bfd_link_relocatable (info))
3771         continue;
3772
3773       if (howto)
3774         {
3775           switch (howto->type)
3776             {
3777             case R_NIOS2_HI16:
3778               r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3779                                                 input_section,
3780                                                 contents, rel->r_offset,
3781                                                 relocation, rel->r_addend);
3782               break;
3783             case R_NIOS2_LO16:
3784               r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3785                                                 input_section,
3786                                                 contents, rel->r_offset,
3787                                                 relocation, rel->r_addend);
3788               break;
3789             case R_NIOS2_PCREL_LO:
3790               r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3791                                                       input_section,
3792                                                       contents,
3793                                                       rel->r_offset,
3794                                                       relocation,
3795                                                       rel->r_addend);
3796               break;
3797             case R_NIOS2_HIADJ16:
3798               r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3799                                                    input_section, contents,
3800                                                    rel->r_offset, relocation,
3801                                                    rel->r_addend);
3802               break;
3803             case R_NIOS2_PCREL_HA:
3804               r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3805                                                          input_section,
3806                                                          contents,
3807                                                          rel->r_offset,
3808                                                          relocation,
3809                                                          rel->r_addend);
3810               break;
3811             case R_NIOS2_PCREL16:
3812               r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3813                                                    input_section, contents,
3814                                                    rel->r_offset, relocation,
3815                                                    rel->r_addend);
3816               break;
3817             case R_NIOS2_GPREL:
3818               /* Turns an absolute address into a gp-relative address.  */
3819               if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3820                 {
3821                   bfd_vma reloc_address;
3822
3823                   if (sec && sec->output_section)
3824                     reloc_address = (sec->output_section->vma
3825                                      + sec->output_offset
3826                                      + rel->r_offset);
3827                   else
3828                     reloc_address = 0;
3829
3830                   format = _("global pointer relative relocation at address "
3831                              "0x%08x when _gp not defined\n");
3832                   sprintf (msgbuf, format, reloc_address);
3833                   msg = msgbuf;
3834                   r = bfd_reloc_dangerous;
3835                 }
3836               else
3837                 {
3838                   bfd_vma symbol_address = rel->r_addend + relocation;
3839                   relocation = symbol_address - gp;
3840                   rel->r_addend = 0;
3841                   if (((signed) relocation < -32768
3842                        || (signed) relocation > 32767)
3843                       && (!h
3844                           || h->root.type == bfd_link_hash_defined
3845                           || h->root.type == bfd_link_hash_defweak))
3846                     {
3847                       if (h)
3848                         name = h->root.root.string;
3849                       format = _("Unable to reach %s (at 0x%08x) from the "
3850                                  "global pointer (at 0x%08x) because the "
3851                                  "offset (%d) is out of the allowed range, "
3852                                  "-32678 to 32767.\n" );
3853                       sprintf (msgbuf, format, name, symbol_address, gp,
3854                                (signed)relocation);
3855                       msg = msgbuf;
3856                       r = bfd_reloc_outofrange;
3857                     }
3858                   else
3859                     r = _bfd_final_link_relocate (howto, input_bfd,
3860                                                   input_section, contents,
3861                                                   rel->r_offset, relocation,
3862                                                   rel->r_addend);
3863                 }
3864               break;
3865             case R_NIOS2_UJMP:
3866               r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3867                                                 input_section,
3868                                                 contents, rel->r_offset,
3869                                                 relocation, rel->r_addend);
3870               break;
3871             case R_NIOS2_CJMP:
3872               r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3873                                                 input_section,
3874                                                 contents, rel->r_offset,
3875                                                 relocation, rel->r_addend);
3876               break;
3877             case R_NIOS2_CALLR:
3878               r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3879                                                  input_section, contents,
3880                                                  rel->r_offset, relocation,
3881                                                  rel->r_addend);
3882               break;
3883             case R_NIOS2_CALL26:
3884             case R_NIOS2_CALL26_NOAT:
3885               /* If we have a call to an undefined weak symbol, we just want
3886                  to stuff a zero in the bits of the call instruction and
3887                  bypass the normal call26 relocation handling, because it'll
3888                  diagnose an overflow error if address 0 isn't in the same
3889                  256MB segment as the call site.  Presumably the call
3890                  should be guarded by a null check anyway.  */
3891               if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3892                 {
3893                   BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3894                   r = _bfd_final_link_relocate (howto, input_bfd,
3895                                                 input_section, contents,
3896                                                 rel->r_offset, relocation,
3897                                                 rel->r_addend);
3898                   break;
3899                 }
3900               /* Handle relocations which should use the PLT entry.
3901                  NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3902                  which may point to a PLT entry, but we don't need to handle
3903                  that here.  If we created a PLT entry, all branches in this
3904                  object should go to it.  */
3905               if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3906                 {
3907                   /* If we've created a .plt section, and assigned a PLT entry
3908                      to this function, it should not be known to bind locally.
3909                      If it were, we would have cleared the PLT entry.  */
3910                   BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3911
3912                   relocation = (splt->output_section->vma
3913                                 + splt->output_offset
3914                                 + h->plt.offset);
3915
3916                   unresolved_reloc = FALSE;
3917                 }
3918               /* Detect R_NIOS2_CALL26 relocations that would overflow the
3919                  256MB segment.  Replace the target with a reference to a
3920                  trampoline instead.
3921                  Note that htab->stub_group is null if relaxation has been
3922                  disabled by the --no-relax linker command-line option, so
3923                  we can use that to skip this processing entirely.  */
3924               if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3925                 {
3926                   bfd_vma dest = relocation + rel->r_addend;
3927                   enum elf32_nios2_stub_type stub_type;
3928
3929                   eh = (struct elf32_nios2_link_hash_entry *)h;
3930                   stub_type = nios2_type_of_stub (input_section, rel, eh,
3931                                                   htab, dest, NULL);
3932
3933                   if (stub_type != nios2_stub_none)
3934                     {
3935                       struct elf32_nios2_stub_hash_entry *hsh;
3936
3937                       hsh = nios2_get_stub_entry (input_section, sec,
3938                                                   eh, rel, htab, stub_type);
3939                       if (hsh == NULL)
3940                         {
3941                           r = bfd_reloc_undefined;
3942                           break;
3943                         }
3944
3945                       dest = (hsh->stub_offset
3946                               + hsh->stub_sec->output_offset
3947                               + hsh->stub_sec->output_section->vma);
3948                       r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3949                                                           input_section,
3950                                                           contents,
3951                                                           rel->r_offset,
3952                                                           dest, 0);
3953                       break;
3954                     }
3955                 }
3956
3957               /* Normal case.  */
3958               r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3959                                                   input_section, contents,
3960                                                   rel->r_offset, relocation,
3961                                                   rel->r_addend);
3962               break;
3963             case R_NIOS2_ALIGN:
3964               r = bfd_reloc_ok;
3965               /* For symmetry this would be
3966                  r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3967                                                   input_section, contents,
3968                                                   rel->r_offset, relocation,
3969                                                   rel->r_addend);
3970                 but do_ignore_reloc would do no more than return
3971                 bfd_reloc_ok. */
3972               break;
3973
3974             case R_NIOS2_GOT16:
3975             case R_NIOS2_CALL16:
3976             case R_NIOS2_GOT_LO:
3977             case R_NIOS2_GOT_HA:
3978             case R_NIOS2_CALL_LO:
3979             case R_NIOS2_CALL_HA:
3980               /* Relocation is to the entry for this symbol in the
3981                  global offset table.  */
3982               if (sgot == NULL)
3983                 {
3984                   r = bfd_reloc_notsupported;
3985                   break;
3986                 }
3987
3988               use_plt = 0;
3989
3990               if (h != NULL)
3991                 {
3992                   bfd_boolean dyn;
3993
3994                   eh = (struct elf32_nios2_link_hash_entry *)h;
3995                   use_plt = (eh->got_types_used == CALL_USED
3996                              && h->plt.offset != (bfd_vma) -1);
3997
3998                   off = h->got.offset;
3999                   BFD_ASSERT (off != (bfd_vma) -1);
4000                   dyn = elf_hash_table (info)->dynamic_sections_created;
4001                   if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4002                                                          bfd_link_pic (info),
4003                                                          h)
4004                       || (bfd_link_pic (info)
4005                           && SYMBOL_REFERENCES_LOCAL (info, h))
4006                       || (ELF_ST_VISIBILITY (h->other)
4007                           && h->root.type == bfd_link_hash_undefweak))
4008                     {
4009                       /* This is actually a static link, or it is a -Bsymbolic
4010                          link and the symbol is defined locally.  We must
4011                          initialize this entry in the global offset table.
4012                          Since the offset must always be a multiple of 4, we
4013                          use the least significant bit to record whether we
4014                          have initialized it already.
4015
4016                          When doing a dynamic link, we create a .rela.got
4017                          relocation entry to initialize the value.  This is
4018                          done in the finish_dynamic_symbol routine.  */
4019                       if ((off & 1) != 0)
4020                         off &= ~1;
4021                       else
4022                         {
4023                           bfd_put_32 (output_bfd, relocation,
4024                                       sgot->contents + off);
4025                           h->got.offset |= 1;
4026                         }
4027                     }
4028                   else
4029                     unresolved_reloc = FALSE;
4030                 }
4031               else
4032                 {
4033                   BFD_ASSERT (local_got_offsets != NULL
4034                               && local_got_offsets[r_symndx] != (bfd_vma) -1);
4035
4036                   off = local_got_offsets[r_symndx];
4037
4038                   /* The offset must always be a multiple of 4.  We use the
4039                      least significant bit to record whether we have already
4040                      generated the necessary reloc.  */
4041                   if ((off & 1) != 0)
4042                     off &= ~1;
4043                   else
4044                     {
4045                       bfd_put_32 (output_bfd, relocation,
4046                                   sgot->contents + off);
4047
4048                       if (bfd_link_pic (info))
4049                         {
4050                           asection *srelgot;
4051                           Elf_Internal_Rela outrel;
4052                           bfd_byte *loc;
4053
4054                           srelgot = htab->root.srelgot;
4055                           BFD_ASSERT (srelgot != NULL);
4056
4057                           outrel.r_addend = relocation;
4058                           outrel.r_offset = (sgot->output_section->vma
4059                                              + sgot->output_offset
4060                                              + off);
4061                           outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4062                           loc = srelgot->contents;
4063                           loc += (srelgot->reloc_count++ *
4064                                   sizeof (Elf32_External_Rela));
4065                           bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4066                         }
4067
4068                       local_got_offsets[r_symndx] |= 1;
4069                     }
4070                 }
4071
4072               if (use_plt && bfd_link_pic (info))
4073                 {
4074                   off = ((h->plt.offset - 24) / 12 + 3) * 4;
4075                   relocation = (htab->root.sgotplt->output_offset + off
4076                                 - got_base);
4077                 }
4078               else
4079                 relocation = sgot->output_offset + off - got_base;
4080
4081               /* This relocation does not use the addend.  */
4082               rel->r_addend = 0;
4083
4084               switch (howto->type)
4085                 {
4086                 case R_NIOS2_GOT_LO:
4087                 case R_NIOS2_CALL_LO:
4088                   r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4089                                                     input_section, contents,
4090                                                     rel->r_offset, relocation,
4091                                                     rel->r_addend);
4092                   break;
4093                 case R_NIOS2_GOT_HA:
4094                 case R_NIOS2_CALL_HA:
4095                   r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4096                                                        input_section, contents,
4097                                                        rel->r_offset,
4098                                                        relocation,
4099                                                        rel->r_addend);
4100                   break;
4101                 default:
4102                   r = _bfd_final_link_relocate (howto, input_bfd,
4103                                                 input_section, contents,
4104                                                 rel->r_offset, relocation,
4105                                                 rel->r_addend);
4106                   break;
4107                 }
4108               break;
4109
4110             case R_NIOS2_GOTOFF_LO:
4111             case R_NIOS2_GOTOFF_HA:
4112             case R_NIOS2_GOTOFF:
4113               /* Relocation is relative to the global offset table pointer.  */
4114
4115               BFD_ASSERT (sgot != NULL);
4116               if (sgot == NULL)
4117                 {
4118                   r = bfd_reloc_notsupported;
4119                   break;
4120                 }
4121
4122               /* Note that sgot->output_offset is not involved in this
4123                  calculation.  We always want the start of .got.  */
4124               relocation -= sgot->output_section->vma;
4125
4126               /* Now we adjust the relocation to be relative to the GOT pointer
4127                  (the _gp_got symbol), which possibly contains the 0x8000 bias.  */
4128               relocation -= got_base;
4129
4130               switch (howto->type)
4131                 {
4132                 case R_NIOS2_GOTOFF_LO:
4133                   r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4134                                                     input_section, contents,
4135                                                     rel->r_offset, relocation,
4136                                                     rel->r_addend);
4137                   break;
4138                 case R_NIOS2_GOTOFF_HA:
4139                   r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4140                                                        input_section, contents,
4141                                                        rel->r_offset,
4142                                                        relocation,
4143                                                        rel->r_addend);
4144                   break;
4145                 default:
4146                   r = _bfd_final_link_relocate (howto, input_bfd,
4147                                                 input_section, contents,
4148                                                 rel->r_offset, relocation,
4149                                                 rel->r_addend);
4150                   break;
4151                 }
4152               break;
4153
4154             case R_NIOS2_TLS_LDO16:
4155               relocation -= dtpoff_base (info) + DTP_OFFSET;
4156
4157               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4158                                             contents, rel->r_offset,
4159                                             relocation, rel->r_addend);
4160               break;
4161             case R_NIOS2_TLS_LDM16:
4162               if (htab->root.sgot == NULL)
4163                 abort ();
4164
4165               off = htab->tls_ldm_got.offset;
4166
4167               if ((off & 1) != 0)
4168                 off &= ~1;
4169               else
4170                 {
4171                   /* If we don't know the module number, create a relocation
4172                      for it.  */
4173                   if (bfd_link_pic (info))
4174                     {
4175                       Elf_Internal_Rela outrel;
4176                       bfd_byte *loc;
4177
4178                       if (htab->root.srelgot == NULL)
4179                         abort ();
4180
4181                       outrel.r_addend = 0;
4182                       outrel.r_offset = (htab->root.sgot->output_section->vma
4183                                          + htab->root.sgot->output_offset
4184                                          + off);
4185                       outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4186
4187                       loc = htab->root.srelgot->contents;
4188                       loc += (htab->root.srelgot->reloc_count++
4189                               * sizeof (Elf32_External_Rela));
4190                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4191                     }
4192                   else
4193                     bfd_put_32 (output_bfd, 1,
4194                                 htab->root.sgot->contents + off);
4195
4196                   htab->tls_ldm_got.offset |= 1;
4197                 }
4198
4199               relocation = htab->root.sgot->output_offset + off - got_base;
4200
4201               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4202                                             contents, rel->r_offset,
4203                                             relocation, rel->r_addend);
4204
4205               break;
4206             case R_NIOS2_TLS_GD16:
4207             case R_NIOS2_TLS_IE16:
4208               {
4209                 int indx;
4210                 char tls_type;
4211
4212                 if (htab->root.sgot == NULL)
4213                   abort ();
4214
4215                 indx = 0;
4216                 if (h != NULL)
4217                   {
4218                     bfd_boolean dyn;
4219                     dyn = htab->root.dynamic_sections_created;
4220                     if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4221                                                          bfd_link_pic (info),
4222                                                          h)
4223                         && (!bfd_link_pic (info)
4224                             || !SYMBOL_REFERENCES_LOCAL (info, h)))
4225                       {
4226                         unresolved_reloc = FALSE;
4227                         indx = h->dynindx;
4228                       }
4229                     off = h->got.offset;
4230                     tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4231                                 ->tls_type);
4232                   }
4233                 else
4234                   {
4235                     if (local_got_offsets == NULL)
4236                       abort ();
4237                     off = local_got_offsets[r_symndx];
4238                     tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4239                                 [r_symndx]);
4240                   }
4241
4242                 if (tls_type == GOT_UNKNOWN)
4243                   abort ();
4244
4245                 if ((off & 1) != 0)
4246                   off &= ~1;
4247                 else
4248                   {
4249                     bfd_boolean need_relocs = FALSE;
4250                     Elf_Internal_Rela outrel;
4251                     bfd_byte *loc = NULL;
4252                     int cur_off = off;
4253
4254                     /* The GOT entries have not been initialized yet.  Do it
4255                        now, and emit any relocations.  If both an IE GOT and a
4256                        GD GOT are necessary, we emit the GD first.  */
4257
4258                     if ((bfd_link_pic (info) || indx != 0)
4259                         && (h == NULL
4260                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4261                             || h->root.type != bfd_link_hash_undefweak))
4262                       {
4263                         need_relocs = TRUE;
4264                         if (htab->root.srelgot == NULL)
4265                           abort ();
4266                         loc = htab->root.srelgot->contents;
4267                         loc += (htab->root.srelgot->reloc_count *
4268                                 sizeof (Elf32_External_Rela));
4269                       }
4270
4271                     if (tls_type & GOT_TLS_GD)
4272                       {
4273                         if (need_relocs)
4274                           {
4275                             outrel.r_addend = 0;
4276                             outrel.r_offset = (htab->root.sgot->output_section->vma
4277                                                + htab->root.sgot->output_offset
4278                                                + cur_off);
4279                             outrel.r_info = ELF32_R_INFO (indx,
4280                                                           R_NIOS2_TLS_DTPMOD);
4281
4282                             bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4283                                                        loc);
4284                             htab->root.srelgot->reloc_count++;
4285                             loc += sizeof (Elf32_External_Rela);
4286
4287                             if (indx == 0)
4288                               bfd_put_32 (output_bfd,
4289                                           (relocation - dtpoff_base (info) -
4290                                            DTP_OFFSET),
4291                                           htab->root.sgot->contents + cur_off + 4);
4292                             else
4293                               {
4294                                 outrel.r_addend = 0;
4295                                 outrel.r_info = ELF32_R_INFO (indx,
4296                                   R_NIOS2_TLS_DTPREL);
4297                                 outrel.r_offset += 4;
4298
4299                                 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4300                                                            loc);
4301                                 htab->root.srelgot->reloc_count++;
4302                                 loc += sizeof (Elf32_External_Rela);
4303                               }
4304                           }
4305                         else
4306                           {
4307                             /* If we are not emitting relocations for a
4308                                general dynamic reference, then we must be in a
4309                                static link or an executable link with the
4310                                symbol binding locally.  Mark it as belonging
4311                                to module 1, the executable.  */
4312                             bfd_put_32 (output_bfd, 1,
4313                                         htab->root.sgot->contents + cur_off);
4314                             bfd_put_32 (output_bfd, (relocation -
4315                                                      dtpoff_base (info) -
4316                                                      DTP_OFFSET),
4317                                         htab->root.sgot->contents + cur_off + 4);
4318                           }
4319
4320                         cur_off += 8;
4321                       }
4322
4323                     if (tls_type & GOT_TLS_IE)
4324                       {
4325                         if (need_relocs)
4326                           {
4327                             if (indx == 0)
4328                               outrel.r_addend = (relocation -
4329                                                  dtpoff_base (info));
4330                             else
4331                               outrel.r_addend = 0;
4332                             outrel.r_offset = (htab->root.sgot->output_section->vma
4333                                                + htab->root.sgot->output_offset
4334                                                + cur_off);
4335                             outrel.r_info = ELF32_R_INFO (indx,
4336                                                           R_NIOS2_TLS_TPREL);
4337
4338                             bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4339                                                        loc);
4340                             htab->root.srelgot->reloc_count++;
4341                             loc += sizeof (Elf32_External_Rela);
4342                           }
4343                         else
4344                           bfd_put_32 (output_bfd, (tpoff (info, relocation)
4345                                                    - TP_OFFSET),
4346                                       htab->root.sgot->contents + cur_off);
4347                         cur_off += 4;
4348                       }
4349
4350                     if (h != NULL)
4351                       h->got.offset |= 1;
4352                     else
4353                       local_got_offsets[r_symndx] |= 1;
4354                   }
4355
4356                 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4357                   off += 8;
4358                 relocation = htab->root.sgot->output_offset + off - got_base;
4359
4360                 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4361                                               contents, rel->r_offset,
4362                                               relocation, rel->r_addend);
4363               }
4364
4365               break;
4366             case R_NIOS2_TLS_LE16:
4367               if (bfd_link_dll (info))
4368                 {
4369                   _bfd_error_handler
4370                     (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
4371                        "permitted in shared object"),
4372                      input_bfd, input_section,
4373                      (long) rel->r_offset, howto->name);
4374                   return FALSE;
4375                 }
4376               else
4377                 relocation = tpoff (info, relocation) - TP_OFFSET;
4378
4379               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4380                                             contents, rel->r_offset,
4381                                             relocation, rel->r_addend);
4382               break;
4383
4384             case R_NIOS2_BFD_RELOC_32:
4385               if (bfd_link_pic (info)
4386                   && (input_section->flags & SEC_ALLOC) != 0
4387                   && (h == NULL
4388                       || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4389                       || h->root.type != bfd_link_hash_undefweak))
4390                 {
4391                   Elf_Internal_Rela outrel;
4392                   bfd_byte *loc;
4393                   bfd_boolean skip, relocate;
4394
4395                   /* When generating a shared object, these relocations
4396                      are copied into the output file to be resolved at run
4397                      time.  */
4398
4399                   skip = FALSE;
4400                   relocate = FALSE;
4401
4402                   outrel.r_offset
4403                     = _bfd_elf_section_offset (output_bfd, info,
4404                                                input_section, rel->r_offset);
4405                   if (outrel.r_offset == (bfd_vma) -1)
4406                     skip = TRUE;
4407                   else if (outrel.r_offset == (bfd_vma) -2)
4408                     skip = TRUE, relocate = TRUE;
4409                   outrel.r_offset += (input_section->output_section->vma
4410                                       + input_section->output_offset);
4411
4412                   if (skip)
4413                     memset (&outrel, 0, sizeof outrel);
4414                   else if (h != NULL
4415                            && h->dynindx != -1
4416                            && (!bfd_link_pic (info)
4417                                || !SYMBOLIC_BIND (info, h)
4418                                || !h->def_regular))
4419                     {
4420                       outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4421                       outrel.r_addend = rel->r_addend;
4422                     }
4423                   else
4424                     {
4425                       /* This symbol is local, or marked to become local.  */
4426                       outrel.r_addend = relocation + rel->r_addend;
4427                       relocate = TRUE;
4428                       outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4429                     }
4430
4431                   sreloc = elf_section_data (input_section)->sreloc;
4432                   if (sreloc == NULL)
4433                     abort ();
4434
4435                   loc = sreloc->contents;
4436                   loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4437                   bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4438
4439                   /* This reloc will be computed at runtime, so there's no
4440                      need to do anything now, except for R_NIOS2_BFD_RELOC_32
4441                      relocations that have been turned into
4442                      R_NIOS2_RELATIVE.  */
4443                   if (!relocate)
4444                     break;
4445                 }
4446
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             case R_NIOS2_TLS_DTPREL:
4454               relocation -= dtpoff_base (info);
4455               /* Fall through.  */
4456
4457             default:
4458               r = _bfd_final_link_relocate (howto, input_bfd,
4459                                             input_section, contents,
4460                                             rel->r_offset, relocation,
4461                                             rel->r_addend);
4462               break;
4463             }
4464         }
4465       else
4466         r = bfd_reloc_notsupported;
4467
4468       if (r != bfd_reloc_ok)
4469         {
4470           if (h != NULL)
4471             name = h->root.root.string;
4472           else
4473             {
4474               name = bfd_elf_string_from_elf_section (input_bfd,
4475                                                       symtab_hdr->sh_link,
4476                                                       sym->st_name);
4477               if (name == NULL || *name == '\0')
4478                 name = bfd_section_name (input_bfd, sec);
4479             }
4480
4481           switch (r)
4482             {
4483             case bfd_reloc_overflow:
4484               (*info->callbacks->reloc_overflow) (info, NULL, name,
4485                                                   howto->name, (bfd_vma) 0,
4486                                                   input_bfd, input_section,
4487                                                   rel->r_offset);
4488               break;
4489
4490             case bfd_reloc_undefined:
4491               (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4492                                                     input_section,
4493                                                     rel->r_offset, TRUE);
4494               break;
4495
4496             case bfd_reloc_outofrange:
4497               if (msg == NULL)
4498                 msg = _("relocation out of range");
4499               break;
4500
4501             case bfd_reloc_notsupported:
4502               if (msg == NULL)
4503                 msg = _("unsupported relocation");
4504               break;
4505
4506             case bfd_reloc_dangerous:
4507               if (msg == NULL)
4508                 msg = _("dangerous relocation");
4509               break;
4510
4511             default:
4512               if (msg == NULL)
4513                 msg = _("unknown error");
4514               break;
4515             }
4516
4517           if (msg)
4518             {
4519               (*info->callbacks->warning) (info, msg, name, input_bfd,
4520                                            input_section, rel->r_offset);
4521               return FALSE;
4522             }
4523         }
4524     }
4525   return TRUE;
4526 }
4527
4528 /* Implement elf-backend_section_flags:
4529    Convert NIOS2 specific section flags to bfd internal section flags.  */
4530 static bfd_boolean
4531 nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
4532 {
4533   if (hdr->sh_flags & SHF_NIOS2_GPREL)
4534     *flags |= SEC_SMALL_DATA;
4535
4536   return TRUE;
4537 }
4538
4539 /* Implement elf_backend_fake_sections:
4540    Set the correct type for an NIOS2 ELF section.  We do this by the
4541    section name, which is a hack, but ought to work.  */
4542 static bfd_boolean
4543 nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4544                            Elf_Internal_Shdr *hdr, asection *sec)
4545 {
4546   register const char *name = bfd_get_section_name (abfd, sec);
4547
4548   if ((sec->flags & SEC_SMALL_DATA)
4549       || strcmp (name, ".sdata") == 0
4550       || strcmp (name, ".sbss") == 0
4551       || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4552     hdr->sh_flags |= SHF_NIOS2_GPREL;
4553
4554   return TRUE;
4555 }
4556
4557 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4558    shortcuts to them in our hash table.  */
4559 static bfd_boolean
4560 create_got_section (bfd *dynobj, struct bfd_link_info *info)
4561 {
4562   struct elf32_nios2_link_hash_table *htab;
4563   struct elf_link_hash_entry *h;
4564
4565   htab = elf32_nios2_hash_table (info);
4566
4567   if (! _bfd_elf_create_got_section (dynobj, info))
4568     return FALSE;
4569
4570   /* In order for the two loads in .PLTresolve to share the same %hiadj,
4571      _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary.  */
4572   if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
4573     return FALSE;
4574
4575   /* The Nios II ABI specifies that GOT-relative relocations are relative
4576      to the linker-created symbol _gp_got, rather than using
4577      _GLOBAL_OFFSET_TABLE_ directly.  In particular, the latter always
4578      points to the base of the GOT while _gp_got may include a bias.  */
4579   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4580                                    "_gp_got");
4581   elf32_nios2_hash_table (info)->h_gp_got = h;
4582   if (h == NULL)
4583     return FALSE;
4584
4585   return TRUE;
4586 }
4587
4588 /* Implement elf_backend_create_dynamic_sections:
4589    Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4590    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4591    hash table.  */
4592 static bfd_boolean
4593 nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4594 {
4595   struct elf32_nios2_link_hash_table *htab;
4596
4597   htab = elf32_nios2_hash_table (info);
4598   if (!htab->root.sgot && !create_got_section (dynobj, info))
4599     return FALSE;
4600
4601   _bfd_elf_create_dynamic_sections (dynobj, info);
4602
4603   /* In order for the two loads in a shared object .PLTresolve to share the
4604      same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4605      to a 16-byte boundary.  This is because the addresses for these loads
4606      include the -(.plt+4) PIC correction.  */
4607   if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
4608     return FALSE;
4609
4610   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
4611   if (!htab->sdynbss)
4612     return FALSE;
4613   if (!bfd_link_pic (info))
4614     {
4615       htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
4616       if (!htab->srelbss)
4617         return FALSE;
4618     }
4619
4620   return TRUE;
4621 }
4622
4623 /* Implement elf_backend_copy_indirect_symbol:
4624    Copy the extra info we tack onto an elf_link_hash_entry.  */
4625 static void
4626 nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4627                                   struct elf_link_hash_entry *dir,
4628                                   struct elf_link_hash_entry *ind)
4629 {
4630   struct elf32_nios2_link_hash_entry *edir, *eind;
4631
4632   edir = (struct elf32_nios2_link_hash_entry *) dir;
4633   eind = (struct elf32_nios2_link_hash_entry *) ind;
4634
4635   if (eind->dyn_relocs != NULL)
4636     {
4637       if (edir->dyn_relocs != NULL)
4638         {
4639           struct elf32_nios2_dyn_relocs **pp;
4640           struct elf32_nios2_dyn_relocs *p;
4641
4642           /* Add reloc counts against the indirect sym to the direct sym
4643              list.  Merge any entries against the same section.  */
4644           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4645             {
4646               struct elf32_nios2_dyn_relocs *q;
4647
4648               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4649                 if (q->sec == p->sec)
4650                   {
4651                     q->pc_count += p->pc_count;
4652                     q->count += p->count;
4653                     *pp = p->next;
4654                     break;
4655                   }
4656               if (q == NULL)
4657                 pp = &p->next;
4658             }
4659           *pp = edir->dyn_relocs;
4660         }
4661
4662       edir->dyn_relocs = eind->dyn_relocs;
4663       eind->dyn_relocs = NULL;
4664     }
4665
4666   if (ind->root.type == bfd_link_hash_indirect
4667       && dir->got.refcount <= 0)
4668     {
4669       edir->tls_type = eind->tls_type;
4670       eind->tls_type = GOT_UNKNOWN;
4671     }
4672
4673   edir->got_types_used |= eind->got_types_used;
4674
4675   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4676 }
4677
4678 /* Set the right machine number for a NIOS2 ELF file.  */
4679
4680 static bfd_boolean
4681 nios2_elf32_object_p (bfd *abfd)
4682 {
4683   unsigned long mach;
4684
4685   mach = elf_elfheader (abfd)->e_flags;
4686
4687   switch (mach)
4688     {
4689     default:
4690     case EF_NIOS2_ARCH_R1:
4691       bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4692       break;
4693     case EF_NIOS2_ARCH_R2:
4694       bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4695       break;
4696     }
4697
4698   return TRUE;
4699 }
4700
4701 /* Implement elf_backend_check_relocs:
4702    Look through the relocs for a section during the first phase.  */
4703 static bfd_boolean
4704 nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4705                           asection *sec, const Elf_Internal_Rela *relocs)
4706 {
4707   bfd *dynobj;
4708   Elf_Internal_Shdr *symtab_hdr;
4709   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4710   const Elf_Internal_Rela *rel;
4711   const Elf_Internal_Rela *rel_end;
4712   struct elf32_nios2_link_hash_table *htab;
4713   asection *sgot;
4714   asection *srelgot;
4715   asection *sreloc = NULL;
4716   bfd_signed_vma *local_got_refcounts;
4717
4718   if (bfd_link_relocatable (info))
4719     return TRUE;
4720
4721   dynobj = elf_hash_table (info)->dynobj;
4722   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4723   sym_hashes = elf_sym_hashes (abfd);
4724   sym_hashes_end = (sym_hashes
4725                     + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
4726   if (!elf_bad_symtab (abfd))
4727     sym_hashes_end -= symtab_hdr->sh_info;
4728   local_got_refcounts = elf_local_got_refcounts (abfd);
4729
4730   htab = elf32_nios2_hash_table (info);
4731   sgot = htab->root.sgot;
4732   srelgot = htab->root.srelgot;
4733
4734   rel_end = relocs + sec->reloc_count;
4735   for (rel = relocs; rel < rel_end; rel++)
4736     {
4737       unsigned int r_type;
4738       struct elf_link_hash_entry *h;
4739       unsigned long r_symndx;
4740
4741       r_symndx = ELF32_R_SYM (rel->r_info);
4742       if (r_symndx < symtab_hdr->sh_info)
4743         h = NULL;
4744       else
4745         {
4746           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4747           while (h->root.type == bfd_link_hash_indirect
4748                  || h->root.type == bfd_link_hash_warning)
4749             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4750
4751           /* PR15323, ref flags aren't set for references in the same
4752              object.  */
4753           h->root.non_ir_ref = 1;
4754         }
4755
4756       r_type = ELF32_R_TYPE (rel->r_info);
4757
4758       switch (r_type)
4759         {
4760         case R_NIOS2_GOT16:
4761         case R_NIOS2_GOT_LO:
4762         case R_NIOS2_GOT_HA:
4763         case R_NIOS2_CALL16:
4764         case R_NIOS2_CALL_LO:
4765         case R_NIOS2_CALL_HA:
4766         case R_NIOS2_TLS_GD16:
4767         case R_NIOS2_TLS_IE16:
4768           /* This symbol requires a global offset table entry.  */
4769           {
4770             int tls_type, old_tls_type;
4771
4772             switch (r_type)
4773               {
4774               default:
4775               case R_NIOS2_GOT16:
4776               case R_NIOS2_GOT_LO:
4777               case R_NIOS2_GOT_HA:
4778               case R_NIOS2_CALL16:
4779               case R_NIOS2_CALL_LO:
4780               case R_NIOS2_CALL_HA:
4781                 tls_type = GOT_NORMAL;
4782                 break;
4783               case R_NIOS2_TLS_GD16:
4784                 tls_type = GOT_TLS_GD;
4785                 break;
4786               case R_NIOS2_TLS_IE16:
4787                 tls_type = GOT_TLS_IE;
4788                 break;
4789               }
4790
4791             if (dynobj == NULL)
4792               {
4793                 /* Create the .got section.  */
4794                 elf_hash_table (info)->dynobj = dynobj = abfd;
4795                 nios2_elf32_create_dynamic_sections (dynobj, info);
4796               }
4797
4798             if (sgot == NULL)
4799               {
4800                 sgot = htab->root.sgot;
4801                 BFD_ASSERT (sgot != NULL);
4802               }
4803
4804             if (srelgot == NULL
4805                 && (h != NULL || bfd_link_pic (info)))
4806               {
4807                 srelgot = htab->root.srelgot;
4808                 BFD_ASSERT (srelgot != NULL);
4809               }
4810
4811             if (h != NULL)
4812               {
4813                 struct elf32_nios2_link_hash_entry *eh
4814                   = (struct elf32_nios2_link_hash_entry *)h;
4815                 h->got.refcount++;
4816                 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
4817                 if (r_type == R_NIOS2_CALL16
4818                     || r_type == R_NIOS2_CALL_LO
4819                     || r_type == R_NIOS2_CALL_HA)
4820                   {
4821                     /* Make sure a plt entry is created for this symbol if
4822                        it turns out to be a function defined by a dynamic
4823                        object.  */
4824                     h->plt.refcount++;
4825                     h->needs_plt = 1;
4826                     h->type = STT_FUNC;
4827                     eh->got_types_used |= CALL_USED;
4828                   }
4829                 else
4830                   eh->got_types_used |= GOT_USED;
4831               }
4832             else
4833               {
4834                 /* This is a global offset table entry for a local symbol.  */
4835                 if (local_got_refcounts == NULL)
4836                   {
4837                     bfd_size_type size;
4838
4839                     size = symtab_hdr->sh_info;
4840                     size *= (sizeof (bfd_signed_vma) + sizeof (char));
4841                     local_got_refcounts
4842                       = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4843                     if (local_got_refcounts == NULL)
4844                       return FALSE;
4845                     elf_local_got_refcounts (abfd) = local_got_refcounts;
4846                     elf32_nios2_local_got_tls_type (abfd)
4847                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4848                   }
4849                 local_got_refcounts[r_symndx]++;
4850                 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4851               }
4852
4853             /* We will already have issued an error message if there is a
4854                TLS / non-TLS mismatch, based on the symbol type.  We don't
4855                support any linker relaxations.  So just combine any TLS
4856                types needed.  */
4857             if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4858                 && tls_type != GOT_NORMAL)
4859               tls_type |= old_tls_type;
4860
4861             if (old_tls_type != tls_type)
4862               {
4863                 if (h != NULL)
4864                   elf32_nios2_hash_entry (h)->tls_type = tls_type;
4865                 else
4866                   elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4867               }
4868           }
4869           /* Fall through */
4870         case R_NIOS2_TLS_LDM16:
4871           if (r_type == R_NIOS2_TLS_LDM16)
4872             htab->tls_ldm_got.refcount++;
4873
4874           if (htab->root.sgot == NULL)
4875             {
4876               if (htab->root.dynobj == NULL)
4877                 htab->root.dynobj = abfd;
4878               if (!create_got_section (htab->root.dynobj, info))
4879                 return FALSE;
4880             }
4881           break;
4882
4883           /* This relocation describes the C++ object vtable hierarchy.
4884              Reconstruct it for later use during GC.  */
4885         case R_NIOS2_GNU_VTINHERIT:
4886           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4887             return FALSE;
4888           break;
4889
4890           /* This relocation describes which C++ vtable entries are actually
4891              used.  Record for later use during GC.  */
4892         case R_NIOS2_GNU_VTENTRY:
4893           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4894             return FALSE;
4895           break;
4896
4897         case R_NIOS2_BFD_RELOC_32:
4898         case R_NIOS2_CALL26:
4899         case R_NIOS2_CALL26_NOAT:
4900         case R_NIOS2_HIADJ16:
4901         case R_NIOS2_LO16:
4902
4903           if (h != NULL)
4904             {
4905               /* If this reloc is in a read-only section, we might
4906                    need a copy reloc.  We can't check reliably at this
4907                    stage whether the section is read-only, as input
4908                    sections have not yet been mapped to output sections.
4909                    Tentatively set the flag for now, and correct in
4910                    adjust_dynamic_symbol.  */
4911               if (!bfd_link_pic (info))
4912                 h->non_got_ref = 1;
4913
4914               /* Make sure a plt entry is created for this symbol if it
4915                  turns out to be a function defined by a dynamic object.  */
4916               h->plt.refcount++;
4917
4918               if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
4919                 h->needs_plt = 1;
4920             }
4921
4922           /* If we are creating a shared library, we need to copy the
4923              reloc into the shared library.  */
4924           if (bfd_link_pic (info)
4925               && (sec->flags & SEC_ALLOC) != 0
4926               && (r_type == R_NIOS2_BFD_RELOC_32
4927                   || (h != NULL && ! h->needs_plt
4928                       && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
4929             {
4930               struct elf32_nios2_dyn_relocs *p;
4931               struct elf32_nios2_dyn_relocs **head;
4932
4933               /* When creating a shared object, we must copy these
4934                  reloc types into the output file.  We create a reloc
4935                  section in dynobj and make room for this reloc.  */
4936               if (sreloc == NULL)
4937                 {
4938                   sreloc = _bfd_elf_make_dynamic_reloc_section
4939                     (sec, dynobj, 2, abfd, TRUE);
4940                   if (sreloc == NULL)
4941                     return FALSE;
4942                 }
4943
4944               /* If this is a global symbol, we count the number of
4945                  relocations we need for this symbol.  */
4946               if (h != NULL)
4947                 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
4948               else
4949                 {
4950                   /* Track dynamic relocs needed for local syms too.
4951                      We really need local syms available to do this
4952                      easily.  Oh well.  */
4953
4954                   asection *s;
4955                   void *vpp;
4956                   Elf_Internal_Sym *isym;
4957
4958                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4959                                                 abfd, r_symndx);
4960                   if (isym == NULL)
4961                     return FALSE;
4962
4963                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4964                   if (s == NULL)
4965                     s = sec;
4966
4967                   vpp = &elf_section_data (s)->local_dynrel;
4968                   head = (struct elf32_nios2_dyn_relocs **) vpp;
4969                 }
4970
4971               p = *head;
4972               if (p == NULL || p->sec != sec)
4973                 {
4974                   bfd_size_type amt = sizeof *p;
4975                   p = ((struct elf32_nios2_dyn_relocs *)
4976                        bfd_alloc (htab->root.dynobj, amt));
4977                   if (p == NULL)
4978                     return FALSE;
4979                   p->next = *head;
4980                   *head = p;
4981                   p->sec = sec;
4982                   p->count = 0;
4983                   p->pc_count = 0;
4984                 }
4985
4986               p->count += 1;
4987
4988             }
4989           break;
4990         }
4991     }
4992
4993   return TRUE;
4994 }
4995
4996
4997 /* Implement elf_backend_gc_mark_hook:
4998    Return the section that should be marked against GC for a given
4999    relocation.  */
5000 static asection *
5001 nios2_elf32_gc_mark_hook (asection *sec,
5002                           struct bfd_link_info *info,
5003                           Elf_Internal_Rela *rel,
5004                           struct elf_link_hash_entry *h,
5005                           Elf_Internal_Sym *sym)
5006 {
5007   if (h != NULL)
5008     switch (ELF32_R_TYPE (rel->r_info))
5009       {
5010       case R_NIOS2_GNU_VTINHERIT:
5011       case R_NIOS2_GNU_VTENTRY:
5012         return NULL;
5013       }
5014   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5015 }
5016
5017 /* Implement elf_backend_gc_sweep_hook:
5018    Update the got entry reference counts for the section being removed.  */
5019 static bfd_boolean
5020 nios2_elf32_gc_sweep_hook (bfd *abfd,
5021                            struct bfd_link_info *info,
5022                            asection *sec,
5023                            const Elf_Internal_Rela *relocs)
5024 {
5025   Elf_Internal_Shdr *symtab_hdr;
5026   struct elf_link_hash_entry **sym_hashes;
5027   bfd_signed_vma *local_got_refcounts;
5028   const Elf_Internal_Rela *rel, *relend;
5029   bfd *dynobj;
5030
5031   if (bfd_link_relocatable (info))
5032     return TRUE;
5033
5034   elf_section_data (sec)->local_dynrel = NULL;
5035
5036   dynobj = elf_hash_table (info)->dynobj;
5037   if (dynobj == NULL)
5038     return TRUE;
5039
5040   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5041   sym_hashes = elf_sym_hashes (abfd);
5042   local_got_refcounts = elf_local_got_refcounts (abfd);
5043
5044   relend = relocs + sec->reloc_count;
5045   for (rel = relocs; rel < relend; rel++)
5046     {
5047       unsigned long r_symndx;
5048       struct elf_link_hash_entry *h = NULL;
5049       int r_type;
5050
5051       r_symndx = ELF32_R_SYM (rel->r_info);
5052       if (r_symndx >= symtab_hdr->sh_info)
5053         {
5054           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5055           while (h->root.type == bfd_link_hash_indirect
5056                  || h->root.type == bfd_link_hash_warning)
5057             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5058         }
5059
5060       r_type = ELF32_R_TYPE (rel->r_info);
5061       switch (r_type)
5062         {
5063         case R_NIOS2_GOT16:
5064         case R_NIOS2_GOT_LO:
5065         case R_NIOS2_GOT_HA:
5066         case R_NIOS2_CALL16:
5067         case R_NIOS2_CALL_LO:
5068         case R_NIOS2_CALL_HA:
5069           if (h != NULL)
5070             {
5071               if (h->got.refcount > 0)
5072                 --h->got.refcount;
5073             }
5074           else if (local_got_refcounts != NULL)
5075             {
5076               if (local_got_refcounts[r_symndx] > 0)
5077                 --local_got_refcounts[r_symndx];
5078             }
5079           break;
5080
5081         case R_NIOS2_PCREL_LO:
5082         case R_NIOS2_PCREL_HA:
5083         case R_NIOS2_BFD_RELOC_32:
5084         case R_NIOS2_CALL26:
5085         case R_NIOS2_CALL26_NOAT:
5086           if (h != NULL)
5087             {
5088               struct elf32_nios2_link_hash_entry *eh;
5089               struct elf32_nios2_dyn_relocs **pp;
5090               struct elf32_nios2_dyn_relocs *p;
5091
5092               eh = (struct elf32_nios2_link_hash_entry *) h;
5093
5094               if (h->plt.refcount > 0)
5095                 --h->plt.refcount;
5096
5097               if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
5098                   || r_type == R_NIOS2_BFD_RELOC_32)
5099                 {
5100                   for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
5101                        pp = &p->next)
5102                     if (p->sec == sec)
5103                       {
5104                         p->count -= 1;
5105                         if (p->count == 0)
5106                           *pp = p->next;
5107                         break;
5108                       }
5109                 }
5110             }
5111           break;
5112
5113         default:
5114           break;
5115         }
5116     }
5117
5118   return TRUE;
5119 }
5120
5121 /* Implement elf_backend_finish_dynamic_symbols:
5122    Finish up dynamic symbol handling.  We set the contents of various
5123    dynamic sections here.  */
5124 static bfd_boolean
5125 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
5126                                    struct bfd_link_info *info,
5127                                    struct elf_link_hash_entry *h,
5128                                    Elf_Internal_Sym *sym)
5129 {
5130   struct elf32_nios2_link_hash_table *htab;
5131   struct elf32_nios2_link_hash_entry *eh
5132     = (struct elf32_nios2_link_hash_entry *)h;
5133   int use_plt;
5134
5135   htab = elf32_nios2_hash_table (info);
5136
5137   if (h->plt.offset != (bfd_vma) -1)
5138     {
5139       asection *splt;
5140       asection *sgotplt;
5141       asection *srela;
5142       bfd_vma plt_index;
5143       bfd_vma got_offset;
5144       Elf_Internal_Rela rela;
5145       bfd_byte *loc;
5146       bfd_vma got_address;
5147
5148       /* This symbol has an entry in the procedure linkage table.  Set
5149          it up.  */
5150       BFD_ASSERT (h->dynindx != -1);
5151       splt = htab->root.splt;
5152       sgotplt = htab->root.sgotplt;
5153       srela = htab->root.srelplt;
5154       BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
5155
5156       /* Emit the PLT entry.  */
5157       if (bfd_link_pic (info))
5158         {
5159           nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
5160                                     3);
5161           plt_index = (h->plt.offset - 24) / 12;
5162           got_offset = (plt_index + 3) * 4;
5163           nios2_elf32_install_imm16 (splt, h->plt.offset,
5164                                      hiadj(plt_index * 4));
5165           nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5166                                      (plt_index * 4) & 0xffff);
5167           nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
5168                                      0xfff4 - h->plt.offset);
5169           got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5170                          + got_offset);
5171
5172           /* Fill in the entry in the global offset table.  There are no
5173              res_n slots for a shared object PLT, instead the .got.plt entries
5174              point to the PLT entries.  */
5175           bfd_put_32 (output_bfd,
5176                       splt->output_section->vma + splt->output_offset
5177                       + h->plt.offset, sgotplt->contents + got_offset);
5178         }
5179       else
5180         {
5181           plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5182           got_offset = (plt_index + 3) * 4;
5183
5184           nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5185           got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5186                          + got_offset);
5187           nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5188           nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5189                                      got_address & 0xffff);
5190
5191           /* Fill in the entry in the global offset table.  */
5192           bfd_put_32 (output_bfd,
5193                       splt->output_section->vma + splt->output_offset
5194                       + plt_index * 4, sgotplt->contents + got_offset);
5195         }
5196
5197       /* Fill in the entry in the .rela.plt section.  */
5198       rela.r_offset = got_address;
5199       rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5200       rela.r_addend = 0;
5201       loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5202       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5203
5204       if (!h->def_regular)
5205         {
5206           /* Mark the symbol as undefined, rather than as defined in
5207              the .plt section.  Leave the value alone.  */
5208           sym->st_shndx = SHN_UNDEF;
5209           /* If the symbol is weak, we do need to clear the value.
5210              Otherwise, the PLT entry would provide a definition for
5211              the symbol even if the symbol wasn't defined anywhere,
5212              and so the symbol would never be NULL.  */
5213           if (!h->ref_regular_nonweak)
5214             sym->st_value = 0;
5215         }
5216     }
5217
5218   use_plt = (eh->got_types_used == CALL_USED
5219              && h->plt.offset != (bfd_vma) -1);
5220
5221   if (!use_plt && h->got.offset != (bfd_vma) -1
5222       && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5223       && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5224     {
5225       asection *sgot;
5226       asection *srela;
5227       Elf_Internal_Rela rela;
5228       bfd_byte *loc;
5229       bfd_vma offset;
5230
5231       /* This symbol has an entry in the global offset table.  Set it
5232          up.  */
5233       sgot = htab->root.sgot;
5234       srela = htab->root.srelgot;
5235       BFD_ASSERT (sgot != NULL && srela != NULL);
5236
5237       offset = (h->got.offset & ~(bfd_vma) 1);
5238       rela.r_offset = (sgot->output_section->vma
5239                        + sgot->output_offset + offset);
5240
5241       /* If this is a -Bsymbolic link, and the symbol is defined
5242          locally, we just want to emit a RELATIVE reloc.  Likewise if
5243          the symbol was forced to be local because of a version file.
5244          The entry in the global offset table will already have been
5245          initialized in the relocate_section function.  */
5246
5247       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
5248         {
5249           rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5250           rela.r_addend = bfd_get_signed_32 (output_bfd,
5251                                              (sgot->contents + offset));
5252           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5253         }
5254       else
5255         {
5256           bfd_put_32 (output_bfd, (bfd_vma) 0,
5257                       sgot->contents + offset);
5258           rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5259           rela.r_addend = 0;
5260         }
5261
5262       loc = srela->contents;
5263       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5264       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5265     }
5266
5267   if (use_plt && h->got.offset != (bfd_vma) -1)
5268     {
5269       bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5270       asection *sgot = htab->root.sgot;
5271       asection *splt = htab->root.splt;
5272       bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5273                                + h->plt.offset),
5274                   sgot->contents + offset);
5275     }
5276
5277   if (h->needs_copy)
5278     {
5279       asection *s;
5280       Elf_Internal_Rela rela;
5281       bfd_byte *loc;
5282
5283       /* This symbol needs a copy reloc.  Set it up.  */
5284       BFD_ASSERT (h->dynindx != -1
5285                   && (h->root.type == bfd_link_hash_defined
5286                       || h->root.type == bfd_link_hash_defweak));
5287
5288       s = htab->srelbss;
5289       BFD_ASSERT (s != NULL);
5290
5291       rela.r_offset = (h->root.u.def.value
5292                        + h->root.u.def.section->output_section->vma
5293                        + h->root.u.def.section->output_offset);
5294       rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5295       rela.r_addend = 0;
5296       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5297       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5298     }
5299
5300   /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute.  */
5301   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5302       || h == elf_hash_table (info)->hgot
5303       || h == elf32_nios2_hash_table (info)->h_gp_got)
5304     sym->st_shndx = SHN_ABS;
5305
5306   return TRUE;
5307 }
5308
5309 /* Implement elf_backend_finish_dynamic_sections.  */
5310 static bfd_boolean
5311 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5312                                      struct bfd_link_info *info)
5313 {
5314   bfd *dynobj;
5315   asection *sgotplt;
5316   asection *sdyn;
5317   struct elf32_nios2_link_hash_table *htab;
5318
5319   htab = elf32_nios2_hash_table (info);
5320   dynobj = elf_hash_table (info)->dynobj;
5321   sgotplt = htab->root.sgotplt;
5322   BFD_ASSERT (sgotplt != NULL);
5323   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5324
5325   if (elf_hash_table (info)->dynamic_sections_created)
5326     {
5327       asection *splt;
5328       Elf32_External_Dyn *dyncon, *dynconend;
5329
5330       splt = htab->root.splt;
5331       BFD_ASSERT (splt != NULL && sdyn != NULL);
5332
5333       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5334       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5335       for (; dyncon < dynconend; dyncon++)
5336         {
5337           Elf_Internal_Dyn dyn;
5338           asection *s;
5339
5340           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5341
5342           switch (dyn.d_tag)
5343             {
5344             default:
5345               break;
5346
5347             case DT_PLTGOT:
5348               s = htab->root.sgotplt;
5349               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5350               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5351               break;
5352
5353             case DT_JMPREL:
5354               s = htab->root.srelplt;
5355               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5356               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5357               break;
5358
5359             case DT_PLTRELSZ:
5360               s = htab->root.srelplt;
5361               dyn.d_un.d_val = s->size;
5362               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5363               break;
5364
5365             case DT_RELASZ:
5366               /* The procedure linkage table relocs (DT_JMPREL) should
5367                  not be included in the overall relocs (DT_RELA).
5368                  Therefore, we override the DT_RELASZ entry here to
5369                  make it not include the JMPREL relocs.  Since the
5370                  linker script arranges for .rela.plt to follow all
5371                  other relocation sections, we don't have to worry
5372                  about changing the DT_RELA entry.  */
5373               s = htab->root.srelplt;
5374               if (s != NULL)
5375                 dyn.d_un.d_val -= s->size;
5376               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5377               break;
5378
5379             case DT_NIOS2_GP:
5380               s = htab->root.sgotplt;
5381               dyn.d_un.d_ptr
5382                 = s->output_section->vma + s->output_offset + 0x7ff0;
5383               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5384               break;
5385             }
5386         }
5387
5388       /* Fill in the first entry in the procedure linkage table.  */
5389       if (splt->size > 0)
5390         {
5391           bfd_vma got_address = (sgotplt->output_section->vma
5392                                  + sgotplt->output_offset);
5393           if (bfd_link_pic (info))
5394             {
5395               bfd_vma got_pcrel = got_address - (splt->output_section->vma
5396                                                  + splt->output_offset);
5397               /* Both GOT and PLT must be aligned to a 16-byte boundary
5398                  for the two loads to share the %hiadj part.  The 4-byte
5399                  offset for nextpc is accounted for in the %lo offsets
5400                  on the loads.  */
5401               BFD_ASSERT ((got_pcrel & 0xf) == 0);
5402               nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
5403               nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5404               nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5405               nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
5406             }
5407           else
5408             {
5409               /* Divide by 4 here, not 3 because we already corrected for the
5410                  res_N branches.  */
5411               bfd_vma res_size = (splt->size - 28) / 4;
5412               bfd_vma res_start = (splt->output_section->vma
5413                                    + splt->output_offset);
5414               bfd_vma res_offset;
5415
5416               for (res_offset = 0; res_offset < res_size; res_offset += 4)
5417                 bfd_put_32 (output_bfd,
5418                             6 | ((res_size - (res_offset + 4)) << 6),
5419                             splt->contents + res_offset);
5420
5421               /* The GOT must be aligned to a 16-byte boundary for the
5422                  two loads to share the same %hiadj part.  */
5423               BFD_ASSERT ((got_address & 0xf) == 0);
5424
5425               nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5426               nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5427               nios2_elf32_install_imm16 (splt, res_size + 4,
5428                                          res_start & 0xffff);
5429               nios2_elf32_install_imm16 (splt, res_size + 12,
5430                                          hiadj (got_address));
5431               nios2_elf32_install_imm16 (splt, res_size + 16,
5432                                          (got_address + 4) & 0xffff);
5433               nios2_elf32_install_imm16 (splt, res_size + 20,
5434                                          (got_address + 8) & 0xffff);
5435             }
5436         }
5437     }
5438   /* Fill in the first three entries in the global offset table.  */
5439   if (sgotplt->size > 0)
5440     {
5441       if (sdyn == NULL)
5442         bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5443       else
5444         bfd_put_32 (output_bfd,
5445                     sdyn->output_section->vma + sdyn->output_offset,
5446                     sgotplt->contents);
5447       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5448       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
5449     }
5450
5451   elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5452
5453   return TRUE;
5454 }
5455
5456 /* Implement elf_backend_adjust_dynamic_symbol:
5457    Adjust a symbol defined by a dynamic object and referenced by a
5458    regular object.  The current definition is in some section of the
5459    dynamic object, but we're not including those sections.  We have to
5460    change the definition to something the rest of the link can
5461    understand.  */
5462 static bfd_boolean
5463 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5464                                    struct elf_link_hash_entry *h)
5465 {
5466   struct elf32_nios2_link_hash_table *htab;
5467   bfd *dynobj;
5468   asection *s;
5469   unsigned align2;
5470
5471   htab = elf32_nios2_hash_table (info);
5472   dynobj = elf_hash_table (info)->dynobj;
5473
5474   /* Make sure we know what is going on here.  */
5475   BFD_ASSERT (dynobj != NULL
5476               && (h->needs_plt
5477                   || h->u.weakdef != NULL
5478                   || (h->def_dynamic
5479                       && h->ref_regular
5480                       && !h->def_regular)));
5481
5482   /* If this is a function, put it in the procedure linkage table.  We
5483      will fill in the contents of the procedure linkage table later,
5484      when we know the address of the .got section.  */
5485   if (h->type == STT_FUNC || h->needs_plt)
5486     {
5487       if (h->plt.refcount <= 0
5488           || SYMBOL_CALLS_LOCAL (info, h)
5489           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5490               && h->root.type == bfd_link_hash_undefweak))
5491         {
5492           /* This case can occur if we saw a PLT reloc in an input
5493              file, but the symbol was never referred to by a dynamic
5494              object, or if all references were garbage collected.  In
5495              such a case, we don't actually need to build a procedure
5496              linkage table, and we can just do a PCREL reloc instead.  */
5497           h->plt.offset = (bfd_vma) -1;
5498           h->needs_plt = 0;
5499         }
5500
5501       return TRUE;
5502     }
5503
5504   /* Reinitialize the plt offset now that it is not used as a reference
5505      count any more.  */
5506   h->plt.offset = (bfd_vma) -1;
5507
5508   /* If this is a weak symbol, and there is a real definition, the
5509      processor independent code will have arranged for us to see the
5510      real definition first, and we can just use the same value.  */
5511   if (h->u.weakdef != NULL)
5512     {
5513       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5514                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
5515       h->root.u.def.section = h->u.weakdef->root.u.def.section;
5516       h->root.u.def.value = h->u.weakdef->root.u.def.value;
5517       return TRUE;
5518     }
5519
5520   /* If there are no non-GOT references, we do not need a copy
5521      relocation.  */
5522   if (!h->non_got_ref)
5523     return TRUE;
5524
5525   /* This is a reference to a symbol defined by a dynamic object which
5526      is not a function.
5527      If we are creating a shared library, we must presume that the
5528      only references to the symbol are via the global offset table.
5529      For such cases we need not do anything here; the relocations will
5530      be handled correctly by relocate_section.  */
5531   if (bfd_link_pic (info))
5532     return TRUE;
5533
5534   if (h->size == 0)
5535     {
5536       _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5537                           h->root.root.string);
5538       return TRUE;
5539     }
5540
5541   /* We must allocate the symbol in our .dynbss section, which will
5542      become part of the .bss section of the executable.  There will be
5543      an entry for this symbol in the .dynsym section.  The dynamic
5544      object will contain position independent code, so all references
5545      from the dynamic object to this symbol will go through the global
5546      offset table.  The dynamic linker will use the .dynsym entry to
5547      determine the address it must put in the global offset table, so
5548      both the dynamic object and the regular object will refer to the
5549      same memory location for the variable.  */
5550   s = htab->sdynbss;
5551   BFD_ASSERT (s != NULL);
5552
5553   /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5554      copy the initial value out of the dynamic object and into the
5555      runtime process image.  We need to remember the offset into the
5556      .rela.bss section we are going to use.  */
5557   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5558     {
5559       asection *srel;
5560
5561       srel = htab->srelbss;
5562       BFD_ASSERT (srel != NULL);
5563       srel->size += sizeof (Elf32_External_Rela);
5564       h->needs_copy = 1;
5565     }
5566
5567   align2 = bfd_log2 (h->size);
5568   if (align2 > h->root.u.def.section->alignment_power)
5569     align2 = h->root.u.def.section->alignment_power;
5570
5571   /* Align dynbss.  */
5572   s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
5573   if (align2 > bfd_get_section_alignment (dynobj, s)
5574       && !bfd_set_section_alignment (dynobj, s, align2))
5575     return FALSE;
5576
5577   /* Define the symbol as being at this point in the section.  */
5578   h->root.u.def.section = s;
5579   h->root.u.def.value = s->size;
5580
5581   /* Increment the section size to make room for the symbol.  */
5582   s->size += h->size;
5583
5584   return TRUE;
5585 }
5586
5587 /* Worker function for nios2_elf32_size_dynamic_sections.  */
5588 static bfd_boolean
5589 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5590 {
5591   struct bfd_link_info *info;
5592   struct elf32_nios2_link_hash_table *htab;
5593
5594   if (h->root.type == bfd_link_hash_indirect)
5595     return TRUE;
5596
5597   if (h->root.type == bfd_link_hash_warning)
5598     /* When warning symbols are created, they **replace** the "real"
5599        entry in the hash table, thus we never get to see the real
5600        symbol in a hash traversal.  So look at it now.  */
5601     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5602
5603   info = (struct bfd_link_info *) inf;
5604   htab = elf32_nios2_hash_table (info);
5605
5606   if (h->plt.offset != (bfd_vma)-1)
5607     h->plt.offset += htab->res_n_size;
5608   if (htab->root.splt == h->root.u.def.section)
5609     h->root.u.def.value += htab->res_n_size;
5610
5611   return TRUE;
5612 }
5613
5614 /* Another worker function for nios2_elf32_size_dynamic_sections.
5615    Allocate space in .plt, .got and associated reloc sections for
5616    dynamic relocs.  */
5617 static bfd_boolean
5618 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5619 {
5620   struct bfd_link_info *info;
5621   struct elf32_nios2_link_hash_table *htab;
5622   struct elf32_nios2_link_hash_entry *eh;
5623   struct elf32_nios2_dyn_relocs *p;
5624   int use_plt;
5625
5626   if (h->root.type == bfd_link_hash_indirect)
5627     return TRUE;
5628
5629   if (h->root.type == bfd_link_hash_warning)
5630     /* When warning symbols are created, they **replace** the "real"
5631        entry in the hash table, thus we never get to see the real
5632        symbol in a hash traversal.  So look at it now.  */
5633     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5634
5635   info = (struct bfd_link_info *) inf;
5636   htab = elf32_nios2_hash_table (info);
5637
5638   if (htab->root.dynamic_sections_created
5639       && h->plt.refcount > 0)
5640     {
5641       /* Make sure this symbol is output as a dynamic symbol.
5642          Undefined weak syms won't yet be marked as dynamic.  */
5643       if (h->dynindx == -1
5644           && !h->forced_local
5645           && !bfd_elf_link_record_dynamic_symbol (info, h))
5646         return FALSE;
5647
5648       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
5649         {
5650           asection *s = htab->root.splt;
5651
5652           /* Allocate room for the header.  */
5653           if (s->size == 0)
5654             {
5655               if (bfd_link_pic (info))
5656                 s->size = 24;
5657               else
5658                 s->size = 28;
5659             }
5660
5661           h->plt.offset = s->size;
5662
5663           /* If this symbol is not defined in a regular file, and we are
5664              not generating a shared library, then set the symbol to this
5665              location in the .plt.  This is required to make function
5666              pointers compare as equal between the normal executable and
5667              the shared library.  */
5668           if (! bfd_link_pic (info)
5669               && !h->def_regular)
5670             {
5671               h->root.u.def.section = s;
5672               h->root.u.def.value = h->plt.offset;
5673             }
5674
5675           /* Make room for this entry.  */
5676           s->size += 12;
5677
5678           /* We also need to make an entry in the .rela.plt section.  */
5679           htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5680
5681           /* And the .got.plt section.  */
5682           htab->root.sgotplt->size += 4;
5683         }
5684       else
5685         {
5686           h->plt.offset = (bfd_vma) -1;
5687           h->needs_plt = 0;
5688         }
5689     }
5690   else
5691     {
5692       h->plt.offset = (bfd_vma) -1;
5693       h->needs_plt = 0;
5694     }
5695
5696   eh = (struct elf32_nios2_link_hash_entry *) h;
5697   use_plt = (eh->got_types_used == CALL_USED
5698              && h->plt.offset != (bfd_vma) -1);
5699
5700   if (h->got.refcount > 0)
5701     {
5702       asection *s;
5703       bfd_boolean dyn;
5704       int tls_type = eh->tls_type;
5705       int indx;
5706
5707       /* Make sure this symbol is output as a dynamic symbol.
5708          Undefined weak syms won't yet be marked as dynamic.  */
5709       if (h->dynindx == -1
5710           && !h->forced_local
5711           && !bfd_elf_link_record_dynamic_symbol (info, h))
5712         return FALSE;
5713
5714       s = htab->root.sgot;
5715       h->got.offset = s->size;
5716
5717       if (tls_type == GOT_UNKNOWN)
5718         abort ();
5719
5720       if (tls_type == GOT_NORMAL)
5721         /* Non-TLS symbols need one GOT slot.  */
5722         s->size += 4;
5723       else
5724         {
5725           if (tls_type & GOT_TLS_GD)
5726             /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots.  */
5727             s->size += 8;
5728           if (tls_type & GOT_TLS_IE)
5729             /* R_NIOS2_TLS_IE16 needs one GOT slot.  */
5730             s->size += 4;
5731         }
5732
5733       dyn = htab->root.dynamic_sections_created;
5734
5735       indx = 0;
5736       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5737           && (!bfd_link_pic (info)
5738               || !SYMBOL_REFERENCES_LOCAL (info, h)))
5739         indx = h->dynindx;
5740
5741       if (tls_type != GOT_NORMAL
5742           && (bfd_link_pic (info) || indx != 0)
5743           && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5744               || h->root.type != bfd_link_hash_undefweak))
5745         {
5746           if (tls_type & GOT_TLS_IE)
5747             htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5748
5749           if (tls_type & GOT_TLS_GD)
5750             htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5751
5752           if ((tls_type & GOT_TLS_GD) && indx != 0)
5753             htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5754         }
5755       else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5756                 || h->root.type != bfd_link_hash_undefweak)
5757                && !use_plt
5758                && (bfd_link_pic (info)
5759                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5760         htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5761     }
5762   else
5763     h->got.offset = (bfd_vma) -1;
5764
5765   if (eh->dyn_relocs == NULL)
5766     return TRUE;
5767
5768   /* In the shared -Bsymbolic case, discard space allocated for
5769      dynamic pc-relative relocs against symbols which turn out to be
5770      defined in regular objects.  For the normal shared case, discard
5771      space for pc-relative relocs that have become local due to symbol
5772      visibility changes.  */
5773
5774   if (bfd_link_pic (info))
5775     {
5776       if (h->def_regular
5777           && (h->forced_local || SYMBOLIC_BIND (info, h)))
5778         {
5779           struct elf32_nios2_dyn_relocs **pp;
5780
5781           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5782             {
5783               p->count -= p->pc_count;
5784               p->pc_count = 0;
5785               if (p->count == 0)
5786                 *pp = p->next;
5787               else
5788                 pp = &p->next;
5789             }
5790         }
5791
5792       /* Also discard relocs on undefined weak syms with non-default
5793          visibility.  */
5794       if (eh->dyn_relocs != NULL
5795           && h->root.type == bfd_link_hash_undefweak)
5796         {
5797           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5798             eh->dyn_relocs = NULL;
5799
5800           /* Make sure undefined weak symbols are output as a dynamic
5801              symbol in PIEs.  */
5802           else if (h->dynindx == -1
5803                    && !h->forced_local
5804                    && !bfd_elf_link_record_dynamic_symbol (info, h))
5805             return FALSE;
5806         }
5807     }
5808   else
5809     {
5810       /* For the non-shared case, discard space for relocs against
5811          symbols which turn out to need copy relocs or are not
5812          dynamic.  */
5813
5814       if (!h->non_got_ref
5815           && ((h->def_dynamic && !h->def_regular)
5816               || (htab->root.dynamic_sections_created
5817                   && (h->root.type == bfd_link_hash_undefweak
5818                       || h->root.type == bfd_link_hash_undefined))))
5819         {
5820           /* Make sure this symbol is output as a dynamic symbol.
5821              Undefined weak syms won't yet be marked as dynamic.  */
5822           if (h->dynindx == -1
5823               && !h->forced_local
5824               && !bfd_elf_link_record_dynamic_symbol (info, h))
5825             return FALSE;
5826
5827           /* If that succeeded, we know we'll be keeping all the
5828              relocs.  */
5829           if (h->dynindx != -1)
5830             goto keep;
5831         }
5832
5833       eh->dyn_relocs = NULL;
5834
5835     keep: ;
5836     }
5837
5838   /* Finally, allocate space.  */
5839   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5840     {
5841       asection *sreloc = elf_section_data (p->sec)->sreloc;
5842       sreloc->size += p->count * sizeof (Elf32_External_Rela);
5843     }
5844
5845   return TRUE;
5846 }
5847
5848 /* Implement elf_backend_size_dynamic_sections:
5849    Set the sizes of the dynamic sections.  */
5850 static bfd_boolean
5851 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5852                                    struct bfd_link_info *info)
5853 {
5854   bfd *dynobj;
5855   asection *s;
5856   bfd_boolean plt;
5857   bfd_boolean got;
5858   bfd_boolean relocs;
5859   bfd *ibfd;
5860   struct elf32_nios2_link_hash_table *htab;
5861
5862   htab = elf32_nios2_hash_table (info);
5863   dynobj = elf_hash_table (info)->dynobj;
5864   BFD_ASSERT (dynobj != NULL);
5865
5866   htab->res_n_size = 0;
5867   if (elf_hash_table (info)->dynamic_sections_created)
5868     {
5869       /* Set the contents of the .interp section to the interpreter.  */
5870       if (bfd_link_executable (info) && !info->nointerp)
5871         {
5872           s = bfd_get_linker_section (dynobj, ".interp");
5873           BFD_ASSERT (s != NULL);
5874           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5875           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5876         }
5877     }
5878   else
5879     {
5880       /* We may have created entries in the .rela.got section.
5881          However, if we are not creating the dynamic sections, we will
5882          not actually use these entries.  Reset the size of .rela.got,
5883          which will cause it to get stripped from the output file
5884          below.  */
5885       s = htab->root.srelgot;
5886       if (s != NULL)
5887         s->size = 0;
5888     }
5889
5890   /* Set up .got offsets for local syms, and space for local dynamic
5891      relocs.  */
5892   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5893     {
5894       bfd_signed_vma *local_got;
5895       bfd_signed_vma *end_local_got;
5896       char *local_tls_type;
5897       bfd_size_type locsymcount;
5898       Elf_Internal_Shdr *symtab_hdr;
5899       asection *srel;
5900
5901       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5902         continue;
5903
5904       for (s = ibfd->sections; s != NULL; s = s->next)
5905         {
5906           struct elf32_nios2_dyn_relocs *p;
5907
5908           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5909             {
5910               if (!bfd_is_abs_section (p->sec)
5911                   && bfd_is_abs_section (p->sec->output_section))
5912                 {
5913                   /* Input section has been discarded, either because
5914                      it is a copy of a linkonce section or due to
5915                      linker script /DISCARD/, so we'll be discarding
5916                      the relocs too.  */
5917                 }
5918               else if (p->count != 0)
5919                 {
5920                   srel = elf_section_data (p->sec)->sreloc;
5921                   srel->size += p->count * sizeof (Elf32_External_Rela);
5922                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5923                     info->flags |= DF_TEXTREL;
5924                 }
5925             }
5926         }
5927
5928       local_got = elf_local_got_refcounts (ibfd);
5929       if (!local_got)
5930         continue;
5931
5932       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5933       locsymcount = symtab_hdr->sh_info;
5934       end_local_got = local_got + locsymcount;
5935       local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5936       s = htab->root.sgot;
5937       srel = htab->root.srelgot;
5938       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5939         {
5940           if (*local_got > 0)
5941             {
5942               *local_got = s->size;
5943               if (*local_tls_type & GOT_TLS_GD)
5944                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
5945                 s->size += 8;
5946               if (*local_tls_type & GOT_TLS_IE)
5947                 s->size += 4;
5948               if (*local_tls_type == GOT_NORMAL)
5949                 s->size += 4;
5950
5951               if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
5952                 srel->size += sizeof (Elf32_External_Rela);
5953             }
5954           else
5955             *local_got = (bfd_vma) -1;
5956         }
5957     }
5958
5959   if (htab->tls_ldm_got.refcount > 0)
5960     {
5961       /* Allocate two GOT entries and one dynamic relocation (if necessary)
5962          for R_NIOS2_TLS_LDM16 relocations.  */
5963       htab->tls_ldm_got.offset = htab->root.sgot->size;
5964       htab->root.sgot->size += 8;
5965       if (bfd_link_pic (info))
5966         htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5967     }
5968   else
5969     htab->tls_ldm_got.offset = -1;
5970
5971   /* Allocate global sym .plt and .got entries, and space for global
5972      sym dynamic relocs.  */
5973   elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5974
5975   if (elf_hash_table (info)->dynamic_sections_created)
5976     {
5977       /* If the .got section is more than 0x8000 bytes, we add
5978          0x8000 to the value of _gp_got, so that 16-bit relocations
5979          have a greater chance of working. */
5980       if (htab->root.sgot->size >= 0x8000
5981           && elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value == 0)
5982         elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value = 0x8000;
5983     }
5984
5985   /* The check_relocs and adjust_dynamic_symbol entry points have
5986      determined the sizes of the various dynamic sections.  Allocate
5987      memory for them.  */
5988   plt = FALSE;
5989   got = FALSE;
5990   relocs = FALSE;
5991   for (s = dynobj->sections; s != NULL; s = s->next)
5992     {
5993       const char *name;
5994
5995       if ((s->flags & SEC_LINKER_CREATED) == 0)
5996         continue;
5997
5998       /* It's OK to base decisions on the section name, because none
5999          of the dynobj section names depend upon the input files.  */
6000       name = bfd_get_section_name (dynobj, s);
6001
6002       if (strcmp (name, ".plt") == 0)
6003         {
6004           /* Remember whether there is a PLT.  */
6005           plt = s->size != 0;
6006
6007           /* Correct for the number of res_N branches.  */
6008           if (plt && !bfd_link_pic (info))
6009             {
6010               htab->res_n_size = (s->size-28) / 3;
6011               s->size += htab->res_n_size;
6012             }
6013         }
6014       else if (CONST_STRNEQ (name, ".rela"))
6015         {
6016           if (s->size != 0)
6017             {
6018               relocs = TRUE;
6019
6020               /* We use the reloc_count field as a counter if we need
6021                  to copy relocs into the output file.  */
6022               s->reloc_count = 0;
6023             }
6024         }
6025       else if (CONST_STRNEQ (name, ".got"))
6026         got = s->size != 0;
6027       else if (strcmp (name, ".dynbss") != 0)
6028         /* It's not one of our sections, so don't allocate space.  */
6029         continue;
6030
6031       if (s->size == 0)
6032         {
6033           /* If we don't need this section, strip it from the
6034              output file.  This is mostly to handle .rela.bss and
6035              .rela.plt.  We must create both sections in
6036              create_dynamic_sections, because they must be created
6037              before the linker maps input sections to output
6038              sections.  The linker does that before
6039              adjust_dynamic_symbol is called, and it is that
6040              function which decides whether anything needs to go
6041              into these sections.  */
6042           s->flags |= SEC_EXCLUDE;
6043           continue;
6044         }
6045
6046       if ((s->flags & SEC_HAS_CONTENTS) == 0)
6047         continue;
6048
6049       /* Allocate memory for the section contents.  */
6050       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
6051          Unused entries should be reclaimed before the section's contents
6052          are written out, but at the moment this does not happen.  Thus in
6053          order to prevent writing out garbage, we initialize the section's
6054          contents to zero.  */
6055       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
6056       if (s->contents == NULL)
6057         return FALSE;
6058     }
6059
6060   /* Adjust dynamic symbols that point to the plt to account for the
6061      now-known number of resN slots.  */
6062   if (htab->res_n_size)
6063     elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
6064
6065   if (elf_hash_table (info)->dynamic_sections_created)
6066     {
6067       /* Add some entries to the .dynamic section.  We fill in the
6068          values later, in elf_nios2_finish_dynamic_sections, but we
6069          must add the entries now so that we get the correct size for
6070          the .dynamic section.  The DT_DEBUG entry is filled in by the
6071          dynamic linker and used by the debugger.  */
6072 #define add_dynamic_entry(TAG, VAL) \
6073   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6074
6075       if (!bfd_link_pic (info) && !add_dynamic_entry (DT_DEBUG, 0))
6076         return FALSE;
6077
6078       if (got && !add_dynamic_entry (DT_PLTGOT, 0))
6079         return FALSE;
6080
6081       if (plt
6082           && (!add_dynamic_entry (DT_PLTRELSZ, 0)
6083               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
6084               || !add_dynamic_entry (DT_JMPREL, 0)))
6085         return FALSE;
6086
6087       if (relocs
6088           && (!add_dynamic_entry (DT_RELA, 0)
6089               || !add_dynamic_entry (DT_RELASZ, 0)
6090               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
6091         return FALSE;
6092
6093       if (!bfd_link_pic (info) && !add_dynamic_entry (DT_NIOS2_GP, 0))
6094         return FALSE;
6095
6096       if ((info->flags & DF_TEXTREL) != 0
6097           && !add_dynamic_entry (DT_TEXTREL, 0))
6098         return FALSE;
6099     }
6100 #undef add_dynamic_entry
6101
6102   return TRUE;
6103 }
6104
6105 /* Free the derived linker hash table.  */
6106 static void
6107 nios2_elf32_link_hash_table_free (bfd *obfd)
6108 {
6109   struct elf32_nios2_link_hash_table *htab
6110     = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
6111
6112   bfd_hash_table_free (&htab->bstab);
6113   _bfd_elf_link_hash_table_free (obfd);
6114 }
6115
6116 /* Implement bfd_elf32_bfd_link_hash_table_create.  */
6117 static struct bfd_link_hash_table *
6118 nios2_elf32_link_hash_table_create (bfd *abfd)
6119 {
6120   struct elf32_nios2_link_hash_table *ret;
6121   bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
6122
6123   ret = bfd_zmalloc (amt);
6124   if (ret == NULL)
6125     return NULL;
6126
6127   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
6128                                       link_hash_newfunc,
6129                                       sizeof (struct
6130                                               elf32_nios2_link_hash_entry),
6131                                       NIOS2_ELF_DATA))
6132     {
6133       free (ret);
6134       return NULL;
6135     }
6136
6137   /* Init the stub hash table too.  */
6138   if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
6139                             sizeof (struct elf32_nios2_stub_hash_entry)))
6140     {
6141       _bfd_elf_link_hash_table_free (abfd);
6142       return NULL;
6143     }
6144   ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
6145
6146   return &ret->root.root;
6147 }
6148
6149 /* Implement elf_backend_reloc_type_class.  */
6150 static enum elf_reloc_type_class
6151 nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6152                               const asection *rel_sec ATTRIBUTE_UNUSED,
6153                               const Elf_Internal_Rela *rela)
6154 {
6155   switch ((int) ELF32_R_TYPE (rela->r_info))
6156     {
6157     case R_NIOS2_RELATIVE:
6158       return reloc_class_relative;
6159     case R_NIOS2_JUMP_SLOT:
6160       return reloc_class_plt;
6161     case R_NIOS2_COPY:
6162       return reloc_class_copy;
6163     default:
6164       return reloc_class_normal;
6165     }
6166 }
6167
6168 /* Return 1 if target is one of ours.  */
6169 static bfd_boolean
6170 is_nios2_elf_target (const struct bfd_target *targ)
6171 {
6172   return (targ == &nios2_elf32_le_vec
6173           || targ == &nios2_elf32_be_vec);
6174 }
6175
6176 /* Implement elf_backend_add_symbol_hook.
6177    This hook is called by the linker when adding symbols from an object
6178    file.  We use it to put .comm items in .sbss, and not .bss.  */
6179 static bfd_boolean
6180 nios2_elf_add_symbol_hook (bfd *abfd,
6181                            struct bfd_link_info *info,
6182                            Elf_Internal_Sym *sym,
6183                            const char **namep ATTRIBUTE_UNUSED,
6184                            flagword *flagsp ATTRIBUTE_UNUSED,
6185                            asection **secp,
6186                            bfd_vma *valp)
6187 {
6188   bfd *dynobj;
6189
6190   if (sym->st_shndx == SHN_COMMON
6191       && !bfd_link_relocatable (info)
6192       && sym->st_size <= elf_gp_size (abfd)
6193       && is_nios2_elf_target (info->output_bfd->xvec))
6194     {
6195       /* Common symbols less than or equal to -G nn bytes are automatically
6196          put into .sbss.  */
6197       struct elf32_nios2_link_hash_table *htab;
6198
6199       htab = elf32_nios2_hash_table (info);
6200       if (htab->sbss == NULL)
6201         {
6202           flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
6203
6204           dynobj = elf_hash_table (info)->dynobj;
6205           if (!dynobj)
6206             dynobj = abfd;
6207
6208           htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
6209                                                            flags);
6210           if (htab->sbss == NULL)
6211             return FALSE;
6212         }
6213
6214       *secp = htab->sbss;
6215       *valp = sym->st_size;
6216     }
6217
6218   return TRUE;
6219 }
6220
6221 /* Implement elf_backend_can_make_relative_eh_frame:
6222    Decide whether to attempt to turn absptr or lsda encodings in
6223    shared libraries into pcrel within the given input section.  */
6224 static bfd_boolean
6225 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6226                                         struct bfd_link_info *info
6227                                         ATTRIBUTE_UNUSED,
6228                                         asection *eh_frame_section
6229                                         ATTRIBUTE_UNUSED)
6230 {
6231   /* We can't use PC-relative encodings in the .eh_frame section.  */
6232   return FALSE;
6233 }
6234
6235 /* Implement elf_backend_special_sections.  */
6236 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
6237 {
6238   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,
6239     SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6240   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6241     SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6242   { NULL,                     0,  0, 0,              0 }
6243 };
6244
6245 #define ELF_ARCH                        bfd_arch_nios2
6246 #define ELF_TARGET_ID                   NIOS2_ELF_DATA
6247 #define ELF_MACHINE_CODE                EM_ALTERA_NIOS2
6248
6249 /* The Nios II MMU uses a 4K page size.  */
6250
6251 #define ELF_MAXPAGESIZE                 0x1000
6252
6253 #define bfd_elf32_bfd_link_hash_table_create \
6254                                           nios2_elf32_link_hash_table_create
6255
6256 #define bfd_elf32_bfd_merge_private_bfd_data \
6257                                           nios2_elf32_merge_private_bfd_data
6258
6259 /* Relocation table lookup macros.  */
6260
6261 #define bfd_elf32_bfd_reloc_type_lookup   nios2_elf32_bfd_reloc_type_lookup
6262 #define bfd_elf32_bfd_reloc_name_lookup   nios2_elf32_bfd_reloc_name_lookup
6263
6264 /* JUMP_TABLE_LINK macros.  */
6265
6266 /* elf_info_to_howto (using RELA relocations).  */
6267
6268 #define elf_info_to_howto                 nios2_elf32_info_to_howto
6269
6270 /* elf backend functions.  */
6271
6272 #define elf_backend_can_gc_sections     1
6273 #define elf_backend_can_refcount        1
6274 #define elf_backend_plt_readonly        1
6275 #define elf_backend_want_got_plt        1
6276 #define elf_backend_rela_normal         1
6277
6278 #define elf_backend_relocate_section      nios2_elf32_relocate_section
6279 #define elf_backend_section_flags         nios2_elf32_section_flags
6280 #define elf_backend_fake_sections         nios2_elf32_fake_sections
6281 #define elf_backend_check_relocs          nios2_elf32_check_relocs
6282
6283 #define elf_backend_gc_mark_hook          nios2_elf32_gc_mark_hook
6284 #define elf_backend_gc_sweep_hook         nios2_elf32_gc_sweep_hook
6285 #define elf_backend_create_dynamic_sections \
6286                                           nios2_elf32_create_dynamic_sections
6287 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6288 #define elf_backend_finish_dynamic_sections \
6289                                           nios2_elf32_finish_dynamic_sections
6290 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6291 #define elf_backend_reloc_type_class      nios2_elf32_reloc_type_class
6292 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6293 #define elf_backend_add_symbol_hook       nios2_elf_add_symbol_hook
6294 #define elf_backend_copy_indirect_symbol  nios2_elf32_copy_indirect_symbol
6295 #define elf_backend_object_p              nios2_elf32_object_p
6296
6297 #define elf_backend_grok_prstatus         nios2_grok_prstatus
6298 #define elf_backend_grok_psinfo           nios2_grok_psinfo
6299
6300 #undef elf_backend_can_make_relative_eh_frame
6301 #define elf_backend_can_make_relative_eh_frame \
6302                                           nios2_elf32_can_make_relative_eh_frame
6303
6304 #define elf_backend_special_sections      elf32_nios2_special_sections
6305
6306 #define TARGET_LITTLE_SYM               nios2_elf32_le_vec
6307 #define TARGET_LITTLE_NAME              "elf32-littlenios2"
6308 #define TARGET_BIG_SYM                  nios2_elf32_be_vec
6309 #define TARGET_BIG_NAME                 "elf32-bignios2"
6310
6311 #define elf_backend_got_header_size     12
6312 #define elf_backend_default_execstack   0
6313
6314 #include "elf32-target.h"