Remove support for the (deprecated) openrisc and or32 configurations and replace
[external/binutils.git] / bfd / elf32-or1k.c
1 /* Or1k-specific support for 32-bit ELF.
2    Copyright 2001-2014 Free Software Foundation, Inc.
3    Contributed for OR32 by Johan Rydberg, jrydberg@opencores.org
4
5    PIC parts added by Stefan Kristiansson, stefan.kristiansson@saunalahti.fi,
6    largely based on elf32-m32r.c and elf32-microblaze.c.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/or1k.h"
28 #include "libiberty.h"
29
30 #define PLT_ENTRY_SIZE 20
31
32 #define PLT0_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(.got+4) */
33 #define PLT0_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(.got+4) */
34 #define PLT0_ENTRY_WORD2 0x85ec0004 /* l.lwz r15, 4(r12) <- *(.got+8)*/
35 #define PLT0_ENTRY_WORD3 0x44007800 /* l.jr r15 */
36 #define PLT0_ENTRY_WORD4 0x858c0000 /* l.lwz r12, 0(r12) */
37
38 #define PLT0_PIC_ENTRY_WORD0 0x85900004 /* l.lwz r12, 4(r16) */
39 #define PLT0_PIC_ENTRY_WORD1 0x85f00008 /* l.lwz r15, 8(r16) */
40 #define PLT0_PIC_ENTRY_WORD2 0x44007800 /* l.jr r15 */
41 #define PLT0_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
42 #define PLT0_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
43
44 #define PLT_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(got idx addr) */
45 #define PLT_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(got idx addr) */
46 #define PLT_ENTRY_WORD2 0x858c0000 /* l.lwz r12, 0(r12) */
47 #define PLT_ENTRY_WORD3 0x44006000 /* l.jr r12 */
48 #define PLT_ENTRY_WORD4 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
49
50 #define PLT_PIC_ENTRY_WORD0 0x85900000 /* l.lwz r12, 0(r16) <- index in got */
51 #define PLT_PIC_ENTRY_WORD1 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
52 #define PLT_PIC_ENTRY_WORD2 0x44006000 /* l.jr r12 */
53 #define PLT_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
54 #define PLT_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
55
56 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
57
58 static reloc_howto_type or1k_elf_howto_table[] =
59 {
60   /* This reloc does nothing.  */
61   HOWTO (R_OR1K_NONE,           /* type */
62          0,                     /* rightshift */
63          2,                     /* size (0 = byte, 1 = short, 2 = long) */
64          32,                    /* bitsize */
65          FALSE,                 /* pc_relative */
66          0,                     /* bitpos */
67          complain_overflow_dont, /* complain_on_overflow */
68          bfd_elf_generic_reloc, /* special_function */
69          "R_OR1K_NONE",         /* name */
70          FALSE,                 /* partial_inplace */
71          0,                     /* src_mask */
72          0,                     /* dst_mask */
73          FALSE),                /* pcrel_offset */
74
75   HOWTO (R_OR1K_32,
76          0,                     /* rightshift */
77          2,                     /* size (0 = byte, 1 = short, 2 = long) */
78          32,                    /* bitsize */
79          FALSE,                 /* pc_relative */
80          0,                     /* bitpos */
81          complain_overflow_unsigned, /* complain_on_overflow */
82          bfd_elf_generic_reloc, /* special_function */
83          "R_OR1K_32",           /* name */
84          FALSE,                 /* partial_inplace */
85          0,                     /* src_mask */
86          0xffffffff,            /* dst_mask */
87          FALSE),                /* pcrel_offset */
88
89   HOWTO (R_OR1K_16,
90          0,                     /* rightshift */
91          1,                     /* size (0 = byte, 1 = short, 2 = long) */
92          16,                    /* bitsize */
93          FALSE,                 /* pc_relative */
94          0,                     /* bitpos */
95          complain_overflow_unsigned, /* complain_on_overflow */
96          bfd_elf_generic_reloc, /* special_function */
97          "R_OR1K_16",           /* name */
98          FALSE,                 /* partial_inplace */
99          0,                     /* src_mask */
100          0xffff,                /* dst_mask */
101          FALSE),                /* pcrel_offset */
102
103   HOWTO (R_OR1K_8,
104          0,                     /* rightshift */
105          0,                     /* size (0 = byte, 1 = short, 2 = long) */
106          8,                     /* bitsize */
107          FALSE,                 /* pc_relative */
108          0,                     /* bitpos */
109          complain_overflow_unsigned, /* complain_on_overflow */
110          bfd_elf_generic_reloc, /* special_function */
111          "R_OR1K_8",            /* name */
112          FALSE,                 /* partial_inplace */
113          0,                     /* src_mask */
114          0xff,                  /* dst_mask */
115          FALSE),                /* pcrel_offset */
116
117   HOWTO (R_OR1K_LO_16_IN_INSN, /* type */
118          0,                     /* rightshift */
119          2,                     /* size (0 = byte, 1 = short, 2 = long) */
120          16,                    /* bitsize */
121          FALSE,                 /* pc_relative */
122          0,                     /* bitpos */
123          complain_overflow_dont, /* complain_on_overflow */
124          bfd_elf_generic_reloc, /* special_function */
125          "R_OR1K_LO_16_IN_INSN", /* name */
126          FALSE,                 /* partial_inplace */
127          0,                     /* src_mask */
128          0x0000ffff,            /* dst_mask */
129          FALSE),                /* pcrel_offset */
130
131   HOWTO (R_OR1K_HI_16_IN_INSN, /* type */
132          16,                    /* rightshift */
133          2,                     /* size (0 = byte, 1 = short, 2 = long) */
134          16,                    /* bitsize */
135          FALSE,                 /* pc_relative */
136          0,                     /* bitpos */
137          complain_overflow_dont, /* complain_on_overflow */
138          bfd_elf_generic_reloc, /* special_function */
139          "R_OR1K_HI_16_IN_INSN", /* name */
140          FALSE,                 /* partial_inplace */
141          0,                     /* src_mask */
142          0x0000ffff,            /* dst_mask */
143          FALSE),                /* pcrel_offset */
144
145   /* A PC relative 26 bit relocation, right shifted by 2.  */
146   HOWTO (R_OR1K_INSN_REL_26, /* type */
147          2,                     /* rightshift */
148          2,                     /* size (0 = byte, 1 = short, 2 = long) */
149          26,                    /* bitsize */
150          TRUE,                  /* pc_relative */
151          0,                     /* bitpos */
152          complain_overflow_signed, /* complain_on_overflow */
153          bfd_elf_generic_reloc, /* special_function */
154          "R_OR1K_INSN_REL_26", /* name */
155          FALSE,                 /* partial_inplace */
156          0,                     /* src_mask */
157          0x03ffffff,            /* dst_mask */
158          TRUE),                 /* pcrel_offset */
159
160   /* GNU extension to record C++ vtable hierarchy.  */
161   HOWTO (R_OR1K_GNU_VTINHERIT, /* type */
162          0,                     /* rightshift */
163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
164          0,                     /* bitsize */
165          FALSE,                 /* pc_relative */
166          0,                     /* bitpos */
167          complain_overflow_dont, /* complain_on_overflow */
168          NULL,                  /* special_function */
169          "R_OR1K_GNU_VTINHERIT", /* name */
170          FALSE,                 /* partial_inplace */
171          0,                     /* src_mask */
172          0,                     /* dst_mask */
173          FALSE),                /* pcrel_offset */
174
175   /* GNU extension to record C++ vtable member usage.  */
176   HOWTO (R_OR1K_GNU_VTENTRY, /* type */
177          0,                     /* rightshift */
178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
179          0,                     /* bitsize */
180          FALSE,                 /* pc_relative */
181          0,                     /* bitpos */
182          complain_overflow_dont, /* complain_on_overflow */
183          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
184          "R_OR1K_GNU_VTENTRY", /* name */
185          FALSE,                 /* partial_inplace */
186          0,                     /* src_mask */
187          0,                     /* dst_mask */
188          FALSE),                /* pcrel_offset */
189
190   HOWTO (R_OR1K_32_PCREL,
191          0,                     /* rightshift */
192          2,                     /* size (0 = byte, 1 = short, 2 = long) */
193          32,                    /* bitsize */
194          TRUE,                  /* pc_relative */
195          0,                     /* bitpos */
196          complain_overflow_signed, /* complain_on_overflow */
197          bfd_elf_generic_reloc, /* special_function */
198          "R_OR1K_32_PCREL",     /* name */
199          FALSE,                 /* partial_inplace */
200          0,                     /* src_mask */
201          0xffffffff,            /* dst_mask */
202          FALSE),                /* pcrel_offset */
203
204   HOWTO (R_OR1K_16_PCREL,
205          0,                     /* rightshift */
206          1,                     /* size (0 = byte, 1 = short, 2 = long) */
207          16,                    /* bitsize */
208          TRUE,                  /* pc_relative */
209          0,                     /* bitpos */
210          complain_overflow_signed, /* complain_on_overflow */
211          bfd_elf_generic_reloc, /* special_function */
212          "R_OR1K_16_PCREL",     /* name */
213          FALSE,                 /* partial_inplace */
214          0,                     /* src_mask */
215          0xffff,                /* dst_mask */
216          FALSE),                /* pcrel_offset */
217
218   HOWTO (R_OR1K_8_PCREL,
219          0,                     /* rightshift */
220          0,                     /* size (0 = byte, 1 = short, 2 = long) */
221          8,                     /* bitsize */
222          TRUE,                  /* pc_relative */
223          0,                     /* bitpos */
224          complain_overflow_signed, /* complain_on_overflow */
225          bfd_elf_generic_reloc, /* special_function */
226          "R_OR1K_8_PCREL",      /* name */
227          FALSE,                 /* partial_inplace */
228          0,                     /* src_mask */
229          0xff,                  /* dst_mask */
230          FALSE),                /* pcrel_offset */
231
232    HOWTO (R_OR1K_GOTPC_HI16,    /* Type.  */
233          16,                    /* Rightshift.  */
234          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
235          16,                    /* Bitsize.  */
236          TRUE,                  /* PC_relative.  */
237          0,                     /* Bitpos.  */
238          complain_overflow_dont, /* Complain on overflow.  */
239          bfd_elf_generic_reloc, /* Special Function.  */
240          "R_OR1K_GOTPC_HI16",   /* Name.  */
241          FALSE,         /* Partial Inplace.  */
242          0,                     /* Source Mask.  */
243          0xffff,                /* Dest Mask.  */
244          TRUE),                 /* PC relative offset?  */
245
246    HOWTO (R_OR1K_GOTPC_LO16,    /* Type.  */
247          0,                     /* Rightshift.  */
248          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
249          16,                    /* Bitsize.  */
250          TRUE,                  /* PC_relative.  */
251          0,                     /* Bitpos.  */
252          complain_overflow_dont, /* Complain on overflow.  */
253          bfd_elf_generic_reloc, /* Special Function.  */
254          "R_OR1K_GOTPC_LO16",   /* Name.  */
255          FALSE,         /* Partial Inplace.  */
256          0,                     /* Source Mask.  */
257          0xffff,                /* Dest Mask.  */
258          TRUE),                 /* PC relative offset?  */
259
260   HOWTO (R_OR1K_GOT16,          /* type */
261          0,                     /* rightshift */
262          2,                     /* size (0 = byte, 1 = short, 2 = long) */
263          16,                    /* bitsize */
264          FALSE,                 /* pc_relative */
265          0,                     /* bitpos */
266          complain_overflow_signed, /* complain_on_overflow */
267          bfd_elf_generic_reloc, /* special_function */
268          "R_OR1K_GOT16",        /* name */
269          FALSE,                 /* partial_inplace */
270          0,                     /* src_mask */
271          0xffff,                /* dst_mask */
272          FALSE),                /* pcrel_offset */
273
274   /* A 26 bit PLT relocation.  Shifted by 2.  */
275   HOWTO (R_OR1K_PLT26,  /* Type.  */
276          2,                     /* Rightshift.  */
277          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
278          26,                    /* Bitsize.  */
279          TRUE,                  /* PC_relative.  */
280          0,                     /* Bitpos.  */
281          complain_overflow_dont, /* Complain on overflow.  */
282          bfd_elf_generic_reloc,/* Special Function.  */
283          "R_OR1K_PLT26",        /* Name.  */
284          FALSE,         /* Partial Inplace.  */
285          0,                     /* Source Mask.  */
286          0x03ffffff,            /* Dest Mask.  */
287          TRUE),                 /* PC relative offset?  */
288
289   HOWTO (R_OR1K_GOTOFF_HI16,    /* type */
290          16,                    /* rightshift */
291          2,                     /* size (0 = byte, 1 = short, 2 = long) */
292          16,                    /* bitsize */
293          FALSE,                 /* pc_relative */
294          0,                     /* bitpos */
295          complain_overflow_dont, /* complain_on_overflow */
296          bfd_elf_generic_reloc, /* special_function */
297          "R_OR1K_GOTOFF_HI16",  /* name */
298          FALSE,                 /* partial_inplace */
299          0x0,                   /* src_mask */
300          0xffff,                /* dst_mask */
301          FALSE),                /* pcrel_offset */
302
303   HOWTO (R_OR1K_GOTOFF_LO16,    /* type */
304          0,                     /* rightshift */
305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
306          16,                    /* bitsize */
307          FALSE,                 /* pc_relative */
308          0,                     /* bitpos */
309          complain_overflow_dont, /* complain_on_overflow */
310          bfd_elf_generic_reloc, /* special_function */
311          "R_OR1K_GOTOFF_LO16",  /* name */
312          FALSE,                 /* partial_inplace */
313          0x0,                   /* src_mask */
314          0xffff,                /* dst_mask */
315          FALSE),                /* pcrel_offset */
316
317   HOWTO (R_OR1K_COPY,           /* type */
318          0,                     /* rightshift */
319          2,                     /* size (0 = byte, 1 = short, 2 = long) */
320          32,                    /* bitsize */
321          FALSE,                 /* pc_relative */
322          0,                     /* bitpos */
323          complain_overflow_bitfield, /* complain_on_overflow */
324          bfd_elf_generic_reloc, /* special_function */
325          "R_OR1K_COPY",         /* name */
326          FALSE,                 /* partial_inplace */
327          0xffffffff,            /* src_mask */
328          0xffffffff,            /* dst_mask */
329          FALSE),                /* pcrel_offset */
330
331   HOWTO (R_OR1K_GLOB_DAT,       /* type */
332          0,                     /* rightshift */
333          2,                     /* size (0 = byte, 1 = short, 2 = long) */
334          32,                    /* bitsize */
335          FALSE,                 /* pc_relative */
336          0,                     /* bitpos */
337          complain_overflow_bitfield, /* complain_on_overflow */
338          bfd_elf_generic_reloc, /* special_function */
339          "R_OR1K_GLOB_DAT",     /* name */
340          FALSE,                 /* partial_inplace */
341          0xffffffff,            /* src_mask */
342          0xffffffff,            /* dst_mask */
343          FALSE),                /* pcrel_offset */
344
345   HOWTO (R_OR1K_JMP_SLOT,       /* type */
346          0,                     /* rightshift */
347          2,                     /* size (0 = byte, 1 = short, 2 = long) */
348          32,                    /* bitsize */
349          FALSE,                 /* pc_relative */
350          0,                     /* bitpos */
351          complain_overflow_bitfield, /* complain_on_overflow */
352          bfd_elf_generic_reloc, /* special_function */
353          "R_OR1K_JMP_SLOT",     /* name */
354          FALSE,                 /* partial_inplace */
355          0xffffffff,            /* src_mask */
356          0xffffffff,            /* dst_mask */
357          FALSE),                /* pcrel_offset */
358
359   HOWTO (R_OR1K_RELATIVE,       /* type */
360          0,                     /* rightshift */
361          2,                     /* size (0 = byte, 1 = short, 2 = long) */
362          32,                    /* bitsize */
363          FALSE,                 /* pc_relative */
364          0,                     /* bitpos */
365          complain_overflow_bitfield, /* complain_on_overflow */
366          bfd_elf_generic_reloc, /* special_function */
367          "R_OR1K_RELATIVE",     /* name */
368          FALSE,                 /* partial_inplace */
369          0xffffffff,            /* src_mask */
370          0xffffffff,            /* dst_mask */
371          FALSE),                /* pcrel_offset */
372
373   HOWTO (R_OR1K_TLS_GD_HI16,    /* type */
374          16,                    /* rightshift */
375          2,                     /* size (0 = byte, 1 = short, 2 = long) */
376          16,                    /* bitsize */
377          FALSE,                 /* pc_relative */
378          0,                     /* bitpos */
379          complain_overflow_dont, /* complain_on_overflow */
380          bfd_elf_generic_reloc, /* special_function */
381          "R_OR1K_TLS_GD_HI16",  /* name */
382          FALSE,                 /* partial_inplace */
383          0x0,                   /* src_mask */
384          0xffff,                /* dst_mask */
385          FALSE),                /* pcrel_offset */
386
387   HOWTO (R_OR1K_TLS_GD_LO16,    /* type */
388          0,                     /* rightshift */
389          2,                     /* size (0 = byte, 1 = short, 2 = long) */
390          16,                    /* bitsize */
391          FALSE,                 /* pc_relative */
392          0,                     /* bitpos */
393          complain_overflow_dont, /* complain_on_overflow */
394          bfd_elf_generic_reloc, /* special_function */
395          "R_OR1K_TLS_GD_LO16",  /* name */
396          FALSE,                 /* partial_inplace */
397          0x0,                   /* src_mask */
398          0xffff,                /* dst_mask */
399          FALSE),                /* pcrel_offset */
400
401   HOWTO (R_OR1K_TLS_LDM_HI16,   /* type */
402          16,                    /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          FALSE,                 /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_dont, /* complain_on_overflow */
408          bfd_elf_generic_reloc, /* special_function */
409          "R_OR1K_TLS_LDM_HI16", /* name */
410          FALSE,                 /* partial_inplace */
411          0x0,                   /* src_mask */
412          0xffff,                /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   HOWTO (R_OR1K_TLS_LDM_LO16,   /* type */
416          0,                     /* rightshift */
417          2,                     /* size (0 = byte, 1 = short, 2 = long) */
418          16,                    /* bitsize */
419          FALSE,                 /* pc_relative */
420          0,                     /* bitpos */
421          complain_overflow_dont, /* complain_on_overflow */
422          bfd_elf_generic_reloc, /* special_function */
423          "R_OR1K_TLS_LDM_LO16", /* name */
424          FALSE,                 /* partial_inplace */
425          0x0,                   /* src_mask */
426          0xffff,                /* dst_mask */
427          FALSE),                /* pcrel_offset */
428
429   HOWTO (R_OR1K_TLS_LDO_HI16,   /* type */
430          16,                    /* rightshift */
431          2,                     /* size (0 = byte, 1 = short, 2 = long) */
432          16,                    /* bitsize */
433          FALSE,                 /* pc_relative */
434          0,                     /* bitpos */
435          complain_overflow_dont, /* complain_on_overflow */
436          bfd_elf_generic_reloc, /* special_function */
437          "R_OR1K_TLS_LDO_HI16", /* name */
438          FALSE,                 /* partial_inplace */
439          0x0,                   /* src_mask */
440          0xffff,                /* dst_mask */
441          FALSE),                /* pcrel_offset */
442
443   HOWTO (R_OR1K_TLS_LDO_LO16,   /* type */
444          0,                     /* rightshift */
445          2,                     /* size (0 = byte, 1 = short, 2 = long) */
446          16,                    /* bitsize */
447          FALSE,                 /* pc_relative */
448          0,                     /* bitpos */
449          complain_overflow_dont, /* complain_on_overflow */
450          bfd_elf_generic_reloc, /* special_function */
451          "R_OR1K_TLS_LDO_LO16", /* name */
452          FALSE,                 /* partial_inplace */
453          0x0,                   /* src_mask */
454          0xffff,                /* dst_mask */
455          FALSE),                /* pcrel_offset */
456
457   HOWTO (R_OR1K_TLS_IE_HI16,    /* type */
458          16,                    /* rightshift */
459          2,                     /* size (0 = byte, 1 = short, 2 = long) */
460          16,                    /* bitsize */
461          FALSE,                 /* pc_relative */
462          0,                     /* bitpos */
463          complain_overflow_dont, /* complain_on_overflow */
464          bfd_elf_generic_reloc, /* special_function */
465          "R_OR1K_TLS_IE_HI16",  /* name */
466          FALSE,                 /* partial_inplace */
467          0x0,                   /* src_mask */
468          0xffff,                /* dst_mask */
469          FALSE),                /* pcrel_offset */
470
471   HOWTO (R_OR1K_TLS_IE_LO16,    /* type */
472          0,                     /* rightshift */
473          2,                     /* size (0 = byte, 1 = short, 2 = long) */
474          16,                    /* bitsize */
475          FALSE,                 /* pc_relative */
476          0,                     /* bitpos */
477          complain_overflow_dont, /* complain_on_overflow */
478          bfd_elf_generic_reloc, /* special_function */
479          "R_OR1K_TLS_IE_LO16",  /* name */
480          FALSE,                 /* partial_inplace */
481          0x0,                   /* src_mask */
482          0xffff,                /* dst_mask */
483          FALSE),                /* pcrel_offset */
484
485   HOWTO (R_OR1K_TLS_LE_HI16,    /* type */
486          16,                    /* rightshift */
487          2,                     /* size (0 = byte, 1 = short, 2 = long) */
488          16,                    /* bitsize */
489          FALSE,                 /* pc_relative */
490          0,                     /* bitpos */
491          complain_overflow_dont, /* complain_on_overflow */
492          bfd_elf_generic_reloc, /* special_function */
493          "R_OR1K_TLS_LE_HI16",  /* name */
494          FALSE,                 /* partial_inplace */
495          0x0,                   /* src_mask */
496          0xffff,                /* dst_mask */
497          FALSE),                /* pcrel_offset */
498
499   HOWTO (R_OR1K_TLS_LE_LO16,    /* type */
500          0,                     /* rightshift */
501          2,                     /* size (0 = byte, 1 = short, 2 = long) */
502          16,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_dont, /* complain_on_overflow */
506          bfd_elf_generic_reloc, /* special_function */
507          "R_OR1K_TLS_LE_LO16",  /* name */
508          FALSE,                 /* partial_inplace */
509          0x0,                   /* src_mask */
510          0xffff,                /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513 };
514
515 /* Map BFD reloc types to Or1k ELF reloc types.  */
516
517 struct or1k_reloc_map
518 {
519   bfd_reloc_code_real_type bfd_reloc_val;
520   unsigned int or1k_reloc_val;
521 };
522
523 static const struct or1k_reloc_map or1k_reloc_map[] =
524 {
525   { BFD_RELOC_NONE,             R_OR1K_NONE },
526   { BFD_RELOC_32,               R_OR1K_32 },
527   { BFD_RELOC_16,               R_OR1K_16 },
528   { BFD_RELOC_8,                R_OR1K_8 },
529   { BFD_RELOC_LO16,             R_OR1K_LO_16_IN_INSN },
530   { BFD_RELOC_HI16,             R_OR1K_HI_16_IN_INSN },
531   { BFD_RELOC_OR1K_REL_26,      R_OR1K_INSN_REL_26 },
532   { BFD_RELOC_VTABLE_ENTRY,     R_OR1K_GNU_VTENTRY },
533   { BFD_RELOC_VTABLE_INHERIT,   R_OR1K_GNU_VTINHERIT },
534   { BFD_RELOC_32_PCREL,         R_OR1K_32_PCREL },
535   { BFD_RELOC_16_PCREL,         R_OR1K_16_PCREL },
536   { BFD_RELOC_8_PCREL,          R_OR1K_8_PCREL },
537   { BFD_RELOC_OR1K_GOTPC_HI16,  R_OR1K_GOTPC_HI16 },
538   { BFD_RELOC_OR1K_GOTPC_LO16,  R_OR1K_GOTPC_LO16 },
539   { BFD_RELOC_OR1K_GOT16,       R_OR1K_GOT16 },
540   { BFD_RELOC_OR1K_PLT26,       R_OR1K_PLT26 },
541   { BFD_RELOC_OR1K_GOTOFF_HI16, R_OR1K_GOTOFF_HI16 },
542   { BFD_RELOC_OR1K_GOTOFF_LO16, R_OR1K_GOTOFF_LO16 },
543   { BFD_RELOC_OR1K_GLOB_DAT,    R_OR1K_GLOB_DAT },
544   { BFD_RELOC_OR1K_COPY,        R_OR1K_COPY },
545   { BFD_RELOC_OR1K_JMP_SLOT,    R_OR1K_JMP_SLOT },
546   { BFD_RELOC_OR1K_RELATIVE,    R_OR1K_RELATIVE },
547   { BFD_RELOC_OR1K_TLS_GD_HI16, R_OR1K_TLS_GD_HI16 },
548   { BFD_RELOC_OR1K_TLS_GD_LO16, R_OR1K_TLS_GD_LO16 },
549   { BFD_RELOC_OR1K_TLS_LDM_HI16,        R_OR1K_TLS_LDM_HI16 },
550   { BFD_RELOC_OR1K_TLS_LDM_LO16,        R_OR1K_TLS_LDM_LO16 },
551   { BFD_RELOC_OR1K_TLS_LDO_HI16,        R_OR1K_TLS_LDO_HI16 },
552   { BFD_RELOC_OR1K_TLS_LDO_LO16,        R_OR1K_TLS_LDO_LO16 },
553   { BFD_RELOC_OR1K_TLS_IE_HI16, R_OR1K_TLS_IE_HI16 },
554   { BFD_RELOC_OR1K_TLS_IE_LO16, R_OR1K_TLS_IE_LO16 },
555   { BFD_RELOC_OR1K_TLS_LE_HI16, R_OR1K_TLS_LE_HI16 },
556   { BFD_RELOC_OR1K_TLS_LE_LO16, R_OR1K_TLS_LE_LO16 },
557 };
558
559 /* The linker needs to keep track of the number of relocs that it
560    decides to copy as dynamic relocs in check_relocs for each symbol.
561    This is so that it can later discard them if they are found to be
562    unnecessary.  We store the information in a field extending the
563    regular ELF linker hash table.  */
564
565 struct elf_or1k_dyn_relocs
566 {
567   struct elf_or1k_dyn_relocs *next;
568
569   /* The input section of the reloc.  */
570   asection *sec;
571
572   /* Total number of relocs copied for the input section.  */
573   bfd_size_type count;
574
575   /* Number of pc-relative relocs copied for the input section.  */
576   bfd_size_type pc_count;
577 };
578
579 #define TLS_UNKNOWN    0
580 #define TLS_NONE       1
581 #define TLS_GD         2
582 #define TLS_LD         3
583 #define TLS_IE         4
584 #define TLS_LE         5
585
586 /* ELF linker hash entry.  */
587 struct elf_or1k_link_hash_entry
588 {
589   struct elf_link_hash_entry root;
590
591   /* Track dynamic relocs copied for this symbol.  */
592   struct elf_or1k_dyn_relocs *dyn_relocs;
593
594   /* Track type of TLS access.  */
595   unsigned char tls_type;
596 };
597
598 /* ELF object data.  */
599 struct elf_or1k_obj_tdata
600 {
601   struct elf_obj_tdata root;
602
603   /* tls_type for each local got entry.  */
604   unsigned char *local_tls_type;
605 };
606
607 #define elf_or1k_tdata(abfd) \
608   ((struct elf_or1k_obj_tdata *) (abfd)->tdata.any)
609
610 #define elf_or1k_local_tls_type(abfd) \
611   (elf_or1k_tdata (abfd)->local_tls_type)
612
613 /* ELF linker hash table.  */
614 struct elf_or1k_link_hash_table
615 {
616   struct elf_link_hash_table root;
617
618   /* Short-cuts to get to dynamic linker sections.  */
619   asection *sgot;
620   asection *sgotplt;
621   asection *srelgot;
622   asection *splt;
623   asection *srelplt;
624   asection *sdynbss;
625   asection *srelbss;
626
627   /* Small local sym to section mapping cache.  */
628   struct sym_cache sym_sec;
629 };
630
631 /* Get the ELF linker hash table from a link_info structure.  */
632 #define or1k_elf_hash_table(p) \
633   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
634    == OR1K_ELF_DATA ? ((struct elf_or1k_link_hash_table *) ((p)->hash)) : NULL)
635
636 static bfd_boolean
637 elf_or1k_mkobject (bfd *abfd)
638 {
639   return bfd_elf_allocate_object (abfd, sizeof (struct elf_or1k_obj_tdata),
640                                   OR1K_ELF_DATA);
641 }
642
643 /* Create an entry in an or1k ELF linker hash table.  */
644
645 static struct bfd_hash_entry *
646 or1k_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
647                             struct bfd_hash_table *table,
648                             const char *string)
649 {
650   struct elf_or1k_link_hash_entry *ret =
651     (struct elf_or1k_link_hash_entry *) entry;
652
653   /* Allocate the structure if it has not already been allocated by a
654      subclass.  */
655   if (ret == NULL)
656     ret = bfd_hash_allocate (table,
657                              sizeof (struct elf_or1k_link_hash_entry));
658   if (ret == NULL)
659     return NULL;
660
661   /* Call the allocation method of the superclass.  */
662   ret = ((struct elf_or1k_link_hash_entry *)
663          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
664                                      table, string));
665   if (ret != NULL)
666     {
667       struct elf_or1k_link_hash_entry *eh;
668
669       eh = (struct elf_or1k_link_hash_entry *) ret;
670       eh->dyn_relocs = NULL;
671       eh->tls_type = TLS_UNKNOWN;
672     }
673
674   return (struct bfd_hash_entry *) ret;
675 }
676
677 /* Create an or1k ELF linker hash table.  */
678
679 static struct bfd_link_hash_table *
680 or1k_elf_link_hash_table_create (bfd *abfd)
681 {
682   struct elf_or1k_link_hash_table *ret;
683   bfd_size_type amt = sizeof (struct elf_or1k_link_hash_table);
684
685   ret = bfd_zmalloc (amt);
686   if (ret == NULL)
687     return NULL;
688
689   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
690                                       or1k_elf_link_hash_newfunc,
691                                       sizeof (struct elf_or1k_link_hash_entry),
692                                       OR1K_ELF_DATA))
693     {
694       free (ret);
695       return NULL;
696     }
697
698   return &ret->root.root;
699 }
700
701 static reloc_howto_type *
702 or1k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
703                         bfd_reloc_code_real_type code)
704 {
705   unsigned int i;
706
707   for (i = ARRAY_SIZE (or1k_reloc_map); --i;)
708     if (or1k_reloc_map[i].bfd_reloc_val == code)
709       return & or1k_elf_howto_table[or1k_reloc_map[i].or1k_reloc_val];
710
711   return NULL;
712 }
713
714 static reloc_howto_type *
715 or1k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
716                         const char *r_name)
717 {
718   unsigned int i;
719
720   for (i = 0;
721        i < (sizeof (or1k_elf_howto_table)
722             / sizeof (or1k_elf_howto_table[0]));
723        i++)
724     if (or1k_elf_howto_table[i].name != NULL
725         && strcasecmp (or1k_elf_howto_table[i].name, r_name) == 0)
726       return &or1k_elf_howto_table[i];
727
728   return NULL;
729 }
730
731 /* Set the howto pointer for an Or1k ELF reloc.  */
732
733 static void
734 or1k_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
735                          arelent * cache_ptr,
736                          Elf_Internal_Rela * dst)
737 {
738   unsigned int r_type;
739
740   r_type = ELF32_R_TYPE (dst->r_info);
741   BFD_ASSERT (r_type < (unsigned int) R_OR1K_max);
742   cache_ptr->howto = & or1k_elf_howto_table[r_type];
743 }
744
745
746 /* Return the relocation value for @tpoff relocations..  */
747 static bfd_vma
748 tpoff (struct bfd_link_info *info, bfd_vma address)
749 {
750   /* If tls_sec is NULL, we should have signalled an error already.  */
751   if (elf_hash_table (info)->tls_sec == NULL)
752     return 0;
753
754   /* The thread pointer on or1k stores the address after the TCB where
755      the data is, just compute the difference. No need to compensate
756      for the size of TCB.  */
757   return (address - elf_hash_table (info)->tls_sec->vma);
758 }
759
760 /* Relocate an Or1k ELF section.
761
762    The RELOCATE_SECTION function is called by the new ELF backend linker
763    to handle the relocations for a section.
764
765    The relocs are always passed as Rela structures; if the section
766    actually uses Rel structures, the r_addend field will always be
767    zero.
768
769    This function is responsible for adjusting the section contents as
770    necessary, and (if using Rela relocs and generating a relocatable
771    output file) adjusting the reloc addend as necessary.
772
773    This function does not have to worry about setting the reloc
774    address or the reloc symbol index.
775
776    LOCAL_SYMS is a pointer to the swapped in local symbols.
777
778    LOCAL_SECTIONS is an array giving the section in the input file
779    corresponding to the st_shndx field of each local symbol.
780
781    The global hash table entry for the global symbols can be found
782    via elf_sym_hashes (input_bfd).
783
784    When generating relocatable output, this function must handle
785    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
786    going to be the section symbol corresponding to the output
787    section, which means that the addend must be adjusted
788    accordingly.  */
789
790 static bfd_boolean
791 or1k_elf_relocate_section (bfd *output_bfd,
792                            struct bfd_link_info *info,
793                            bfd *input_bfd,
794                            asection *input_section,
795                            bfd_byte *contents,
796                            Elf_Internal_Rela *relocs,
797                            Elf_Internal_Sym *local_syms,
798                            asection **local_sections)
799 {
800   Elf_Internal_Shdr *symtab_hdr;
801   struct elf_link_hash_entry **sym_hashes;
802   Elf_Internal_Rela *rel;
803   Elf_Internal_Rela *relend;
804   struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
805   bfd *dynobj;
806   asection *sreloc;
807   bfd_vma *local_got_offsets;
808   asection *sgot;
809
810   if (htab == NULL)
811     return FALSE;
812
813   dynobj = htab->root.dynobj;
814   local_got_offsets = elf_local_got_offsets (input_bfd);
815
816   sreloc = elf_section_data (input_section)->sreloc;
817
818   sgot = htab->sgot;
819
820   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
821   sym_hashes = elf_sym_hashes (input_bfd);
822   relend = relocs + input_section->reloc_count;
823
824   for (rel = relocs; rel < relend; rel++)
825     {
826       reloc_howto_type *howto;
827       unsigned long r_symndx;
828       Elf_Internal_Sym *sym;
829       asection *sec;
830       struct elf_link_hash_entry *h;
831       bfd_vma relocation;
832       bfd_reloc_status_type r;
833       const char *name = NULL;
834       int r_type;
835
836       r_type = ELF32_R_TYPE (rel->r_info);
837       r_symndx = ELF32_R_SYM (rel->r_info);
838
839       if (r_type == R_OR1K_GNU_VTINHERIT
840           || r_type == R_OR1K_GNU_VTENTRY)
841         continue;
842
843       if (r_type < 0 || r_type >= (int) R_OR1K_max)
844         {
845           bfd_set_error (bfd_error_bad_value);
846           return FALSE;
847         }
848
849       howto = or1k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
850       h = NULL;
851       sym = NULL;
852       sec = NULL;
853
854       if (r_symndx < symtab_hdr->sh_info)
855         {
856           sym = local_syms + r_symndx;
857           sec = local_sections[r_symndx];
858           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
859
860           name = bfd_elf_string_from_elf_section
861             (input_bfd, symtab_hdr->sh_link, sym->st_name);
862           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
863         }
864       else
865         {
866           bfd_boolean unresolved_reloc, warned, ignored;
867
868           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
869                                    r_symndx, symtab_hdr, sym_hashes,
870                                    h, sec, relocation,
871                                    unresolved_reloc, warned, ignored);
872         }
873
874       if (sec != NULL && discarded_section (sec))
875         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
876                                          rel, 1, relend, howto, 0, contents);
877
878       if (info->relocatable)
879         continue;
880
881       switch (howto->type)
882         {
883         case R_OR1K_PLT26:
884           {
885             if (htab->splt != NULL && h != NULL
886                 && h->plt.offset != (bfd_vma) -1)
887               {
888                 relocation = (htab->splt->output_section->vma
889                               + htab->splt->output_offset
890                               + h->plt.offset);
891               }
892             break;
893           }
894
895         case R_OR1K_GOT16:
896           /* Relocation is to the entry for this symbol in the global
897              offset table.  */
898           BFD_ASSERT (sgot != NULL);
899           if (h != NULL)
900             {
901               bfd_boolean dyn;
902               bfd_vma off;
903
904               off = h->got.offset;
905               BFD_ASSERT (off != (bfd_vma) -1);
906
907               dyn = htab->root.dynamic_sections_created;
908               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
909                   || (info->shared
910                       && (info->symbolic
911                           || h->dynindx == -1
912                           || h->forced_local)
913                       && h->def_regular))
914                 {
915                   /* This is actually a static link, or it is a
916                      -Bsymbolic link and the symbol is defined
917                      locally, or the symbol was forced to be local
918                      because of a version file.  We must initialize
919                      this entry in the global offset table.  Since the
920                      offset must always be a multiple of 4, we use the
921                      least significant bit to record whether we have
922                      initialized it already.
923
924                      When doing a dynamic link, we create a .rela.got
925                      relocation entry to initialize the value.  This
926                      is done in the finish_dynamic_symbol routine.  */
927                   if ((off & 1) != 0)
928                     off &= ~1;
929                   else
930                     {
931                       /* Write entry in GOT.  */
932                       bfd_put_32 (output_bfd, relocation,
933                                   sgot->contents + off);
934                       /* Mark GOT entry as having been written.  */
935                       h->got.offset |= 1;
936                     }
937                 }
938
939               relocation = sgot->output_offset + off;
940             }
941           else
942             {
943               bfd_vma off;
944               bfd_byte *loc;
945
946               BFD_ASSERT (local_got_offsets != NULL
947                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
948
949               /* Get offset into GOT table.  */
950               off = local_got_offsets[r_symndx];
951
952               /* The offset must always be a multiple of 4.  We use
953                  the least significant bit to record whether we have
954                  already processed this entry.  */
955               if ((off & 1) != 0)
956                 off &= ~1;
957               else
958                 {
959                   /* Write entry in GOT.  */
960                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
961                   if (info->shared)
962                     {
963                       asection *srelgot;
964                       Elf_Internal_Rela outrel;
965
966                       /* We need to generate a R_OR1K_RELATIVE reloc
967                          for the dynamic linker.  */
968                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
969                       BFD_ASSERT (srelgot != NULL);
970
971                       outrel.r_offset = (sgot->output_section->vma
972                                          + sgot->output_offset
973                                          + off);
974                       outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
975                       outrel.r_addend = relocation;
976                       loc = srelgot->contents;
977                       loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
978                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
979                       ++srelgot->reloc_count;
980                     }
981
982                   local_got_offsets[r_symndx] |= 1;
983                 }
984               relocation = sgot->output_offset + off;
985             }
986
987           /* Addend should be zero.  */
988           if (rel->r_addend != 0)
989             (*_bfd_error_handler)
990               (_("internal error: addend should be zero for R_OR1K_GOT16"));
991
992           break;
993
994         case R_OR1K_GOTOFF_LO16:
995         case R_OR1K_GOTOFF_HI16:
996           /* Relocation is offset from GOT.  */
997           BFD_ASSERT (sgot != NULL);
998           relocation -= sgot->output_section->vma;
999           break;
1000
1001         case R_OR1K_INSN_REL_26:
1002         case R_OR1K_HI_16_IN_INSN:
1003         case R_OR1K_LO_16_IN_INSN:
1004         case R_OR1K_32:
1005           /* R_OR1K_16? */
1006           {
1007             /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
1008                from removed linkonce sections, or sections discarded by
1009                a linker script.  */
1010             if (r_symndx == STN_UNDEF
1011                 || (input_section->flags & SEC_ALLOC) == 0)
1012               break;
1013
1014             if ((info->shared
1015                  && (h == NULL
1016                      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1017                      || h->root.type != bfd_link_hash_undefweak)
1018                  && (!howto->pc_relative
1019                      || (h != NULL
1020                          && h->dynindx != -1
1021                          && (!info->symbolic
1022                              || !h->def_regular))))
1023                 || (!info->shared
1024                     && h != NULL
1025                     && h->dynindx != -1
1026                     && !h->non_got_ref
1027                     && ((h->def_dynamic
1028                          && !h->def_regular)
1029                         || h->root.type == bfd_link_hash_undefweak
1030                         || h->root.type == bfd_link_hash_undefined)))
1031               {
1032                 Elf_Internal_Rela outrel;
1033                 bfd_byte *loc;
1034                 bfd_boolean skip;
1035
1036                 /* When generating a shared object, these relocations
1037                    are copied into the output file to be resolved at run
1038                    time.  */
1039
1040                 BFD_ASSERT (sreloc != NULL);
1041
1042                 skip = FALSE;
1043
1044                 outrel.r_offset =
1045                   _bfd_elf_section_offset (output_bfd, info, input_section,
1046                                            rel->r_offset);
1047                 if (outrel.r_offset == (bfd_vma) -1)
1048                   skip = TRUE;
1049                 else if (outrel.r_offset == (bfd_vma) -2)
1050                   skip = TRUE;
1051                 outrel.r_offset += (input_section->output_section->vma
1052                                     + input_section->output_offset);
1053
1054                 if (skip)
1055                   memset (&outrel, 0, sizeof outrel);
1056                 /* h->dynindx may be -1 if the symbol was marked to
1057                    become local.  */
1058                 else if (h != NULL
1059                          && ((! info->symbolic && h->dynindx != -1)
1060                              || !h->def_regular))
1061                   {
1062                     BFD_ASSERT (h->dynindx != -1);
1063                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1064                     outrel.r_addend = rel->r_addend;
1065                   }
1066                 else
1067                   {
1068                     if (r_type == R_OR1K_32)
1069                       {
1070                         outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1071                         outrel.r_addend = relocation + rel->r_addend;
1072                       }
1073                     else
1074                       {
1075                         BFD_FAIL ();
1076                         (*_bfd_error_handler)
1077                           (_("%B: probably compiled without -fPIC?"),
1078                            input_bfd);
1079                         bfd_set_error (bfd_error_bad_value);
1080                         return FALSE;
1081                       }
1082                   }
1083
1084                 loc = sreloc->contents;
1085                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1086                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1087                 break;
1088               }
1089             break;
1090           }
1091
1092         case R_OR1K_TLS_LDM_HI16:
1093         case R_OR1K_TLS_LDM_LO16:
1094         case R_OR1K_TLS_LDO_HI16:
1095         case R_OR1K_TLS_LDO_LO16:
1096           /* TODO: implement support for local dynamic.  */
1097           BFD_FAIL ();
1098           (*_bfd_error_handler)
1099             (_("%B: support for local dynamic not implemented"),
1100              input_bfd);
1101           bfd_set_error (bfd_error_bad_value);
1102           return FALSE;
1103
1104
1105         case R_OR1K_TLS_GD_HI16:
1106         case R_OR1K_TLS_GD_LO16:
1107         case R_OR1K_TLS_IE_HI16:
1108         case R_OR1K_TLS_IE_LO16:
1109           {
1110             bfd_vma gotoff;
1111             Elf_Internal_Rela rela;
1112             bfd_byte *loc;
1113             int dynamic;
1114
1115             /* Mark as TLS related GOT entry by setting
1116                bit 2 as well as bit 1.  */
1117             if (h != NULL)
1118               {
1119                 gotoff = h->got.offset;
1120                 h->got.offset |= 3;
1121               }
1122             else
1123               {
1124                 gotoff = local_got_offsets[r_symndx];
1125                 local_got_offsets[r_symndx] |= 3;
1126               }
1127
1128             /* Only process the relocation once.  */
1129             if (gotoff & 1)
1130               {
1131                 relocation = sgot->output_offset + (gotoff  & ~3);
1132                 break;
1133               }
1134
1135             BFD_ASSERT (elf_hash_table (info)->hgot == NULL
1136                         || elf_hash_table (info)->hgot->root.u.def.value == 0);
1137
1138             /* Dynamic entries will require relocations. if we do not need
1139                them we will just use the default R_OR1K_NONE and
1140                not set anything.  */
1141             dynamic = info->shared
1142               || (sec && (sec->flags & SEC_ALLOC) != 0
1143                   && h != NULL
1144                   && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
1145
1146             /* Shared GD.  */
1147             if (dynamic && (howto->type == R_OR1K_TLS_GD_HI16
1148                             || howto->type == R_OR1K_TLS_GD_LO16))
1149               {
1150                 int i;
1151
1152                 /* Add DTPMOD and DTPOFF GOT and rela entries.  */
1153                 for (i = 0; i < 2; ++i)
1154                   {
1155                     rela.r_offset = sgot->output_section->vma +
1156                       sgot->output_offset + gotoff + i*4;
1157                     if (h != NULL && h->dynindx != -1)
1158                       {
1159                         rela.r_info = ELF32_R_INFO (h->dynindx,
1160                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1161                         rela.r_addend = 0;
1162                       }
1163                     else
1164                       {
1165                         rela.r_info = ELF32_R_INFO (0,
1166                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1167                         rela.r_addend = tpoff (info, relocation);
1168                       }
1169
1170                     loc = sreloc->contents;
1171                     loc += sreloc->reloc_count++ *
1172                       sizeof (Elf32_External_Rela);
1173
1174                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1175                     bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
1176                   }
1177               }
1178             /* Static GD.  */
1179             else if (howto->type == R_OR1K_TLS_GD_HI16
1180                      || howto->type == R_OR1K_TLS_GD_LO16)
1181               {
1182                 bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
1183                 bfd_put_32 (output_bfd, tpoff (info, relocation),
1184                     sgot->contents + gotoff + 4);
1185               }
1186             /* Shared IE.  */
1187             else if (dynamic)
1188               {
1189                 /* Add TPOFF GOT and rela entries.  */
1190                 rela.r_offset = sgot->output_section->vma +
1191                   sgot->output_offset + gotoff;
1192                 if (h != NULL && h->dynindx != -1)
1193                   {
1194                     rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_TLS_TPOFF);
1195                     rela.r_addend = 0;
1196                   }
1197                 else
1198                   {
1199                     rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
1200                     rela.r_addend = tpoff (info, relocation);
1201                   }
1202
1203                 loc = sreloc->contents;
1204                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1205
1206                 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1207                 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
1208               }
1209             /* Static IE.  */
1210             else
1211               {
1212                 bfd_put_32 (output_bfd, tpoff (info, relocation),
1213                             sgot->contents + gotoff);
1214               }
1215             relocation = sgot->output_offset + gotoff;
1216             break;
1217           }
1218         case R_OR1K_TLS_LE_HI16:
1219         case R_OR1K_TLS_LE_LO16:
1220
1221           /* Relocation is offset from TP.  */
1222           relocation = tpoff (info, relocation);
1223           break;
1224
1225         case R_OR1K_TLS_DTPMOD:
1226         case R_OR1K_TLS_DTPOFF:
1227         case R_OR1K_TLS_TPOFF:
1228           /* These are resolved dynamically on load and shouldn't
1229              be used as linker input.  */
1230           BFD_FAIL ();
1231           (*_bfd_error_handler)
1232             (_("%B: will not resolve runtime TLS relocation"),
1233              input_bfd);
1234           bfd_set_error (bfd_error_bad_value);
1235           return FALSE;
1236
1237         default:
1238           break;
1239         }
1240       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
1241                                     rel->r_offset, relocation, rel->r_addend);
1242
1243       if (r != bfd_reloc_ok)
1244         {
1245           const char *msg = NULL;
1246
1247           switch (r)
1248             {
1249             case bfd_reloc_overflow:
1250               r = info->callbacks->reloc_overflow
1251                 (info, (h ? &h->root : NULL), name, howto->name,
1252                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1253               break;
1254
1255             case bfd_reloc_undefined:
1256               r = info->callbacks->undefined_symbol
1257                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1258               break;
1259
1260             case bfd_reloc_outofrange:
1261               msg = _("internal error: out of range error");
1262               break;
1263
1264             case bfd_reloc_notsupported:
1265               msg = _("internal error: unsupported relocation error");
1266               break;
1267
1268             case bfd_reloc_dangerous:
1269               msg = _("internal error: dangerous relocation");
1270               break;
1271
1272             default:
1273               msg = _("internal error: unknown error");
1274               break;
1275             }
1276
1277           if (msg)
1278             r = info->callbacks->warning
1279               (info, msg, name, input_bfd, input_section, rel->r_offset);
1280
1281           if (!r)
1282             return FALSE;
1283         }
1284     }
1285
1286   return TRUE;
1287 }
1288
1289 /* Return the section that should be marked against GC for a given
1290    relocation.  */
1291
1292 static asection *
1293 or1k_elf_gc_mark_hook (asection *sec,
1294                        struct bfd_link_info *info,
1295                        Elf_Internal_Rela *rel,
1296                        struct elf_link_hash_entry *h,
1297                        Elf_Internal_Sym *sym)
1298 {
1299   if (h != NULL)
1300     switch (ELF32_R_TYPE (rel->r_info))
1301       {
1302       case R_OR1K_GNU_VTINHERIT:
1303       case R_OR1K_GNU_VTENTRY:
1304         return NULL;
1305       }
1306
1307   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1308 }
1309
1310 static bfd_boolean
1311 or1k_elf_gc_sweep_hook (bfd *abfd,
1312                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
1313                         asection *sec,
1314                         const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
1315 {
1316   /* Update the got entry reference counts for the section being removed.  */
1317   Elf_Internal_Shdr *symtab_hdr;
1318   struct elf_link_hash_entry **sym_hashes;
1319   bfd_signed_vma *local_got_refcounts;
1320   const Elf_Internal_Rela *rel, *relend;
1321
1322   elf_section_data (sec)->local_dynrel = NULL;
1323
1324   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1325   sym_hashes = elf_sym_hashes (abfd);
1326   local_got_refcounts = elf_local_got_refcounts (abfd);
1327
1328   relend = relocs + sec->reloc_count;
1329   for (rel = relocs; rel < relend; rel++)
1330     {
1331       unsigned long r_symndx;
1332       struct elf_link_hash_entry *h = NULL;
1333
1334       r_symndx = ELF32_R_SYM (rel->r_info);
1335       if (r_symndx >= symtab_hdr->sh_info)
1336         {
1337           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1338           while (h->root.type == bfd_link_hash_indirect
1339                  || h->root.type == bfd_link_hash_warning)
1340             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1341         }
1342
1343       switch (ELF32_R_TYPE (rel->r_info))
1344         {
1345         case R_OR1K_GOT16:
1346           if (h != NULL)
1347             {
1348               if (h->got.refcount > 0)
1349                 h->got.refcount--;
1350             }
1351           else
1352             {
1353               if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
1354                 local_got_refcounts[r_symndx]--;
1355             }
1356           break;
1357
1358         default:
1359           break;
1360         }
1361     }
1362   return TRUE;
1363 }
1364
1365 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1366    shortcuts to them in our hash table.  */
1367
1368 static bfd_boolean
1369 create_got_section (bfd *dynobj, struct bfd_link_info *info)
1370 {
1371   struct elf_or1k_link_hash_table *htab;
1372   asection *s;
1373
1374   /* This function may be called more than once.  */
1375   s = bfd_get_section_by_name (dynobj, ".got");
1376   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
1377     return TRUE;
1378
1379   htab = or1k_elf_hash_table (info);
1380   if (htab == NULL)
1381     return FALSE;
1382
1383   if (! _bfd_elf_create_got_section (dynobj, info))
1384     return FALSE;
1385
1386   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1387   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1388   htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1389
1390   if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
1391     abort ();
1392
1393   if (! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC
1394                                | SEC_LOAD
1395                                | SEC_HAS_CONTENTS
1396                                | SEC_IN_MEMORY
1397                                | SEC_LINKER_CREATED
1398                                | SEC_READONLY)
1399       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1400     return FALSE;
1401
1402   return TRUE;
1403 }
1404
1405 /* Look through the relocs for a section during the first phase.  */
1406
1407 static bfd_boolean
1408 or1k_elf_check_relocs (bfd *abfd,
1409                        struct bfd_link_info *info,
1410                        asection *sec,
1411                        const Elf_Internal_Rela *relocs)
1412 {
1413   Elf_Internal_Shdr *symtab_hdr;
1414   struct elf_link_hash_entry **sym_hashes;
1415   const Elf_Internal_Rela *rel;
1416
1417   const Elf_Internal_Rela *rel_end;
1418   struct elf_or1k_link_hash_table *htab;
1419   bfd *dynobj;
1420   asection *sreloc = NULL;
1421
1422   if (info->relocatable)
1423     return TRUE;
1424
1425   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1426   sym_hashes = elf_sym_hashes (abfd);
1427
1428   htab = or1k_elf_hash_table (info);
1429   if (htab == NULL)
1430     return FALSE;
1431
1432   dynobj = htab->root.dynobj;
1433
1434   rel_end = relocs + sec->reloc_count;
1435   for (rel = relocs; rel < rel_end; rel++)
1436     {
1437       struct elf_link_hash_entry *h;
1438       unsigned long r_symndx;
1439       unsigned char tls_type;
1440
1441       r_symndx = ELF32_R_SYM (rel->r_info);
1442       if (r_symndx < symtab_hdr->sh_info)
1443         h = NULL;
1444       else
1445         {
1446           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1447           while (h->root.type == bfd_link_hash_indirect
1448                  || h->root.type == bfd_link_hash_warning)
1449             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1450
1451           /* PR15323, ref flags aren't set for references in the same
1452              object.  */
1453           h->root.non_ir_ref = 1;
1454         }
1455
1456       switch (ELF32_R_TYPE (rel->r_info))
1457         {
1458         case R_OR1K_TLS_GD_HI16:
1459         case R_OR1K_TLS_GD_LO16:
1460           tls_type = TLS_GD;
1461           break;
1462         case R_OR1K_TLS_LDM_HI16:
1463         case R_OR1K_TLS_LDM_LO16:
1464         case R_OR1K_TLS_LDO_HI16:
1465         case R_OR1K_TLS_LDO_LO16:
1466           tls_type = TLS_LD;
1467           break;
1468         case R_OR1K_TLS_IE_HI16:
1469         case R_OR1K_TLS_IE_LO16:
1470           tls_type = TLS_IE;
1471           break;
1472         case R_OR1K_TLS_LE_HI16:
1473         case R_OR1K_TLS_LE_LO16:
1474           tls_type = TLS_LE;
1475           break;
1476         default:
1477           tls_type = TLS_NONE;
1478         }
1479
1480       /* Record TLS type.  */
1481       if (h != NULL)
1482           ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
1483       else
1484         {
1485           unsigned char *local_tls_type;
1486
1487           /* This is a TLS type record for a local symbol.  */
1488           local_tls_type = (unsigned char *) elf_or1k_local_tls_type (abfd);
1489           if (local_tls_type == NULL)
1490             {
1491               bfd_size_type size;
1492
1493               size = symtab_hdr->sh_info;
1494               local_tls_type = bfd_zalloc (abfd, size);
1495               if (local_tls_type == NULL)
1496                 return FALSE;
1497               elf_or1k_local_tls_type (abfd) = local_tls_type;
1498             }
1499           local_tls_type[r_symndx] = tls_type;
1500         }
1501
1502       switch (ELF32_R_TYPE (rel->r_info))
1503         {
1504           /* This relocation describes the C++ object vtable hierarchy.
1505              Reconstruct it for later use during GC.  */
1506         case R_OR1K_GNU_VTINHERIT:
1507           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1508             return FALSE;
1509           break;
1510
1511           /* This relocation describes which C++ vtable entries are actually
1512              used.  Record for later use during GC.  */
1513         case R_OR1K_GNU_VTENTRY:
1514           BFD_ASSERT (h != NULL);
1515           if (h != NULL
1516               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1517             return FALSE;
1518           break;
1519
1520           /* This relocation requires .plt entry.  */
1521         case R_OR1K_PLT26:
1522           if (h != NULL)
1523             {
1524               h->needs_plt = 1;
1525               h->plt.refcount += 1;
1526             }
1527           break;
1528
1529         case R_OR1K_GOT16:
1530         case R_OR1K_GOTOFF_HI16:
1531         case R_OR1K_GOTOFF_LO16:
1532         case R_OR1K_TLS_GD_HI16:
1533         case R_OR1K_TLS_GD_LO16:
1534         case R_OR1K_TLS_IE_HI16:
1535         case R_OR1K_TLS_IE_LO16:
1536           if (htab->sgot == NULL)
1537             {
1538               if (dynobj == NULL)
1539                 htab->root.dynobj = dynobj = abfd;
1540               if (! create_got_section (dynobj, info))
1541                 return FALSE;
1542             }
1543
1544           /* TLS specific.  */
1545           if (ELF32_R_TYPE (rel->r_info) >= R_OR1K_TLS_GD_HI16 &&
1546               ELF32_R_TYPE (rel->r_info) <= R_OR1K_TLS_IE_LO16)
1547             {
1548               /* Set which rela section to use.  */
1549               elf_section_data (sec)->sreloc =
1550                 bfd_get_section_by_name (dynobj, ".rela.got");;
1551             }
1552
1553           if (ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_HI16 &&
1554               ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_LO16)
1555             {
1556               if (h != NULL)
1557                 h->got.refcount += 1;
1558               else
1559                 {
1560                   bfd_signed_vma *local_got_refcounts;
1561
1562                   /* This is a global offset table entry for a local symbol.  */
1563                   local_got_refcounts = elf_local_got_refcounts (abfd);
1564                   if (local_got_refcounts == NULL)
1565                     {
1566                       bfd_size_type size;
1567
1568                       size = symtab_hdr->sh_info;
1569                       size *= sizeof (bfd_signed_vma);
1570                       local_got_refcounts = bfd_zalloc (abfd, size);
1571                       if (local_got_refcounts == NULL)
1572                         return FALSE;
1573                       elf_local_got_refcounts (abfd) = local_got_refcounts;
1574                     }
1575                   local_got_refcounts[r_symndx] += 1;
1576                 }
1577             }
1578           break;
1579
1580         case R_OR1K_INSN_REL_26:
1581         case R_OR1K_HI_16_IN_INSN:
1582         case R_OR1K_LO_16_IN_INSN:
1583         case R_OR1K_32:
1584           /* R_OR1K_16? */
1585           {
1586             if (h != NULL && !info->shared)
1587               {
1588                 /* We may need a copy reloc.  */
1589                 h->non_got_ref = 1;
1590
1591                 /* We may also need a .plt entry.  */
1592                 h->plt.refcount += 1;
1593                 if (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26)
1594                   h->pointer_equality_needed = 1;
1595               }
1596
1597             /* If we are creating a shared library, and this is a reloc
1598                against a global symbol, or a non PC relative reloc
1599                against a local symbol, then we need to copy the reloc
1600                into the shared library.  However, if we are linking with
1601                -Bsymbolic, we do not need to copy a reloc against a
1602                global symbol which is defined in an object we are
1603                including in the link (i.e., DEF_REGULAR is set).  At
1604                this point we have not seen all the input files, so it is
1605                possible that DEF_REGULAR is not set now but will be set
1606                later (it is never cleared).  In case of a weak definition,
1607                DEF_REGULAR may be cleared later by a strong definition in
1608                a shared library.  We account for that possibility below by
1609                storing information in the relocs_copied field of the hash
1610                table entry.  A similar situation occurs when creating
1611                shared libraries and symbol visibility changes render the
1612                symbol local.
1613
1614                If on the other hand, we are creating an executable, we
1615                may need to keep relocations for symbols satisfied by a
1616                dynamic library if we manage to avoid copy relocs for the
1617                symbol.  */
1618
1619             if ((info->shared
1620                  && (sec->flags & SEC_ALLOC) != 0
1621                  && (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26
1622                      || (h != NULL
1623                          && (! info->symbolic
1624                              || h->root.type == bfd_link_hash_defweak
1625                              || !h->def_regular))))
1626                 || (!info->shared
1627                     && (sec->flags & SEC_ALLOC) != 0
1628                     && h != NULL
1629                     && (h->root.type == bfd_link_hash_defweak
1630                         || !h->def_regular)))
1631               {
1632                 struct elf_or1k_dyn_relocs *p;
1633                 struct elf_or1k_dyn_relocs **head;
1634
1635                 /* When creating a shared object, we must copy these
1636                    relocs into the output file.  We create a reloc
1637                    section in dynobj and make room for the reloc.  */
1638                 if (sreloc == NULL)
1639                   {
1640                     const char *name;
1641                     unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1642                     unsigned int shnam = _bfd_elf_single_rel_hdr (sec)->sh_name;
1643
1644                     name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1645                     if (name == NULL)
1646                       return FALSE;
1647
1648                     if (strncmp (name, ".rela", 5) != 0
1649                         || strcmp (bfd_get_section_name (abfd, sec),
1650                                    name + 5) != 0)
1651                       {
1652                         (*_bfd_error_handler)
1653                           (_("%B: bad relocation section name `%s\'"),
1654                            abfd, name);
1655                       }
1656
1657                     if (htab->root.dynobj == NULL)
1658                       htab->root.dynobj = abfd;
1659                     dynobj = htab->root.dynobj;
1660
1661                     sreloc = bfd_get_section_by_name (dynobj, name);
1662                     if (sreloc == NULL)
1663                       {
1664                         sreloc = _bfd_elf_make_dynamic_reloc_section
1665                           (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
1666
1667                         if (sreloc == NULL)
1668                           return FALSE;
1669                       }
1670                     elf_section_data (sec)->sreloc = sreloc;
1671                   }
1672
1673                 /* If this is a global symbol, we count the number of
1674                    relocations we need for this symbol.  */
1675                 if (h != NULL)
1676                   head = &((struct elf_or1k_link_hash_entry *) h)->dyn_relocs;
1677                 else
1678                   {
1679                     /* Track dynamic relocs needed for local syms too.
1680                        We really need local syms available to do this
1681                        easily.  Oh well.  */
1682
1683                     asection *s;
1684                     Elf_Internal_Sym *isym;
1685                     void *vpp;
1686
1687                     isym = bfd_sym_from_r_symndx (&htab->sym_sec,
1688                                                   abfd, r_symndx);
1689                     if (isym == NULL)
1690                       return FALSE;
1691
1692                     s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1693                     if (s == NULL)
1694                       return FALSE;
1695
1696                     vpp = &elf_section_data (s)->local_dynrel;
1697                     head = (struct elf_or1k_dyn_relocs **) vpp;
1698                   }
1699
1700                 p = *head;
1701                 if (p == NULL || p->sec != sec)
1702                   {
1703                     bfd_size_type amt = sizeof *p;
1704                     p = ((struct elf_or1k_dyn_relocs *)
1705                          bfd_alloc (htab->root.dynobj, amt));
1706                     if (p == NULL)
1707                       return FALSE;
1708                     p->next = *head;
1709                     *head = p;
1710                     p->sec = sec;
1711                     p->count = 0;
1712                     p->pc_count = 0;
1713                   }
1714
1715                 p->count += 1;
1716                 if (ELF32_R_TYPE (rel->r_info) == R_OR1K_INSN_REL_26)
1717                   p->pc_count += 1;
1718               }
1719           }
1720           break;
1721         }
1722     }
1723
1724   return TRUE;
1725 }
1726
1727 /* Finish up the dynamic sections.  */
1728
1729 static bfd_boolean
1730 or1k_elf_finish_dynamic_sections (bfd *output_bfd,
1731                                   struct bfd_link_info *info)
1732 {
1733   bfd *dynobj;
1734   asection *sdyn, *sgot;
1735   struct elf_or1k_link_hash_table *htab;
1736
1737   htab = or1k_elf_hash_table (info);
1738   if (htab == NULL)
1739     return FALSE;
1740
1741   dynobj = htab->root.dynobj;
1742
1743   sgot = htab->sgotplt;
1744   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1745
1746   if (htab->root.dynamic_sections_created)
1747     {
1748       asection *splt;
1749       Elf32_External_Dyn *dyncon, *dynconend;
1750
1751       BFD_ASSERT (sgot != NULL && sdyn != NULL);
1752
1753       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1754       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1755
1756       for (; dyncon < dynconend; dyncon++)
1757         {
1758           Elf_Internal_Dyn dyn;
1759           asection *s;
1760
1761           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1762
1763           switch (dyn.d_tag)
1764             {
1765             default:
1766               continue;
1767
1768             case DT_PLTGOT:
1769               s = htab->sgot->output_section;
1770               BFD_ASSERT (s != NULL);
1771               dyn.d_un.d_ptr = s->vma;
1772               break;
1773
1774             case DT_JMPREL:
1775               s = htab->srelplt->output_section;
1776               BFD_ASSERT (s != NULL);
1777               dyn.d_un.d_ptr = s->vma;
1778               break;
1779
1780             case DT_PLTRELSZ:
1781               s = htab->srelplt->output_section;
1782               BFD_ASSERT (s != NULL);
1783               dyn.d_un.d_val = s->size;
1784               break;
1785
1786             case DT_RELASZ:
1787               /* My reading of the SVR4 ABI indicates that the
1788                  procedure linkage table relocs (DT_JMPREL) should be
1789                  included in the overall relocs (DT_RELA).  This is
1790                  what Solaris does.  However, UnixWare can not handle
1791                  that case.  Therefore, we override the DT_RELASZ entry
1792                  here to make it not include the JMPREL relocs.  Since
1793                  the linker script arranges for .rela.plt to follow all
1794                  other relocation sections, we don't have to worry
1795                  about changing the DT_RELA entry.  */
1796               if (htab->srelplt != NULL)
1797                 {
1798                   /* FIXME: this calculation sometimes produces
1799                      wrong result, the problem is that the dyn.d_un.d_val
1800                      is not always correct, needs investigation why
1801                      that happens. In the meantime, reading the
1802                      ".rela.dyn" section by name seems to yield
1803                      correct result.
1804
1805                   s = htab->srelplt->output_section;
1806                   dyn.d_un.d_val -= s->size;
1807                   */
1808
1809                   s = bfd_get_section_by_name (output_bfd, ".rela.dyn");
1810                   dyn.d_un.d_val = s ? s->size : 0;
1811                 }
1812               break;
1813             }
1814           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1815         }
1816
1817
1818       /* Fill in the first entry in the procedure linkage table.  */
1819       splt = htab->splt;
1820       if (splt && splt->size > 0)
1821         {
1822           if (info->shared)
1823             {
1824               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0,
1825                           splt->contents);
1826               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1,
1827                           splt->contents + 4);
1828               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2,
1829                           splt->contents + 8);
1830               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3,
1831                           splt->contents + 12);
1832               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4,
1833                           splt->contents + 16);
1834             }
1835           else
1836             {
1837               unsigned long addr;
1838               /* addr = .got + 4 */
1839               addr = sgot->output_section->vma + sgot->output_offset + 4;
1840               bfd_put_32 (output_bfd,
1841                           PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
1842                           splt->contents);
1843               bfd_put_32 (output_bfd,
1844                           PLT0_ENTRY_WORD1 | (addr & 0xffff),
1845                           splt->contents + 4);
1846               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
1847               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
1848               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
1849             }
1850
1851           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
1852         }
1853     }
1854
1855   /* Set the first entry in the global offset table to the address of
1856      the dynamic section.  */
1857   if (sgot && sgot->size > 0)
1858     {
1859       if (sdyn == NULL)
1860         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1861       else
1862         bfd_put_32 (output_bfd,
1863                     sdyn->output_section->vma + sdyn->output_offset,
1864                     sgot->contents);
1865       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1866     }
1867
1868   if (htab->sgot && htab->sgot->size > 0)
1869     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
1870
1871   return TRUE;
1872 }
1873
1874 /* Finish up dynamic symbol handling.  We set the contents of various
1875    dynamic sections here.  */
1876
1877 static bfd_boolean
1878 or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
1879                                 struct bfd_link_info *info,
1880                                 struct elf_link_hash_entry *h,
1881                                 Elf_Internal_Sym *sym)
1882 {
1883   struct elf_or1k_link_hash_table *htab;
1884   bfd_byte *loc;
1885
1886   htab = or1k_elf_hash_table (info);
1887   if (htab == NULL)
1888     return FALSE;
1889
1890   if (h->plt.offset != (bfd_vma) -1)
1891     {
1892       asection *splt;
1893       asection *sgot;
1894       asection *srela;
1895
1896       bfd_vma plt_index;
1897       bfd_vma got_offset;
1898       bfd_vma got_addr;
1899       Elf_Internal_Rela rela;
1900
1901       /* This symbol has an entry in the procedure linkage table.  Set
1902          it up.  */
1903       BFD_ASSERT (h->dynindx != -1);
1904
1905       splt = htab->splt;
1906       sgot = htab->sgotplt;
1907       srela = htab->srelplt;
1908       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1909
1910       /* Get the index in the procedure linkage table which
1911          corresponds to this symbol.  This is the index of this symbol
1912          in all the symbols for which we are making plt entries.  The
1913          first entry in the procedure linkage table is reserved.  */
1914       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1915
1916       /* Get the offset into the .got table of the entry that
1917         corresponds to this function.  Each .got entry is 4 bytes.
1918         The first three are reserved.  */
1919       got_offset = (plt_index + 3) * 4;
1920       got_addr = got_offset;
1921
1922       /* Fill in the entry in the procedure linkage table.  */
1923       if (! info->shared)
1924         {
1925           got_addr += htab->sgotplt->output_section->vma
1926             + htab->sgotplt->output_offset;
1927           bfd_put_32 (output_bfd, PLT_ENTRY_WORD0 | ((got_addr >> 16) & 0xffff),
1928                       splt->contents + h->plt.offset);
1929           bfd_put_32 (output_bfd, PLT_ENTRY_WORD1 | (got_addr & 0xffff),
1930                       splt->contents + h->plt.offset + 4);
1931           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
1932                       splt->contents + h->plt.offset + 8);
1933           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
1934                       splt->contents + h->plt.offset + 12);
1935           bfd_put_32 (output_bfd, PLT_ENTRY_WORD4
1936                       | plt_index * sizeof (Elf32_External_Rela),
1937                       splt->contents + h->plt.offset + 16);
1938         }
1939       else
1940         {
1941           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD0 | (got_addr & 0xffff),
1942                       splt->contents + h->plt.offset);
1943           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD1
1944                       | plt_index * sizeof (Elf32_External_Rela),
1945                       splt->contents + h->plt.offset + 4);
1946           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
1947                       splt->contents + h->plt.offset + 8);
1948           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
1949                       splt->contents + h->plt.offset + 12);
1950           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
1951                       splt->contents + h->plt.offset + 16);
1952         }
1953
1954       /* Fill in the entry in the global offset table.  */
1955       bfd_put_32 (output_bfd,
1956                   (splt->output_section->vma
1957                    + splt->output_offset), /* Same offset.  */
1958                   sgot->contents + got_offset);
1959
1960       /* Fill in the entry in the .rela.plt section.  */
1961       rela.r_offset = (sgot->output_section->vma
1962                        + sgot->output_offset
1963                        + got_offset);
1964       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_JMP_SLOT);
1965       rela.r_addend = 0;
1966       loc = srela->contents;
1967       loc += plt_index * sizeof (Elf32_External_Rela);
1968       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1969
1970       if (!h->def_regular)
1971         {
1972           /* Mark the symbol as undefined, rather than as defined in
1973              the .plt section.  Leave the value alone.  */
1974           sym->st_shndx = SHN_UNDEF;
1975         }
1976
1977     }
1978
1979   if (h->got.offset != (bfd_vma) -1
1980       && (h->got.offset & 2) == 0) /* Homemade TLS check.  */
1981     {
1982       asection *sgot;
1983       asection *srela;
1984       Elf_Internal_Rela rela;
1985
1986       /* This symbol has an entry in the global offset table.  Set it
1987          up.  */
1988       sgot = htab->sgot;
1989       srela = htab->srelgot;
1990       BFD_ASSERT (sgot != NULL && srela != NULL);
1991
1992       rela.r_offset = (sgot->output_section->vma
1993                        + sgot->output_offset
1994                        + (h->got.offset &~ 1));
1995
1996       /* If this is a -Bsymbolic link, and the symbol is defined
1997          locally, we just want to emit a RELATIVE reloc.  Likewise if
1998          the symbol was forced to be local because of a version file.
1999          The entry in the global offset table will already have been
2000          initialized in the relocate_section function.  */
2001       if (info->shared
2002           && (info->symbolic
2003               || h->dynindx == -1
2004               || h->forced_local)
2005           && h->def_regular)
2006         {
2007           rela.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
2008           rela.r_addend = (h->root.u.def.value
2009                            + h->root.u.def.section->output_section->vma
2010                            + h->root.u.def.section->output_offset);
2011         }
2012       else
2013         {
2014           BFD_ASSERT ((h->got.offset & 1) == 0);
2015           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2016           rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_GLOB_DAT);
2017           rela.r_addend = 0;
2018         }
2019
2020       loc = srela->contents;
2021       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
2022       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2023       ++srela->reloc_count;
2024     }
2025
2026   if (h->needs_copy)
2027     {
2028       asection *s;
2029       Elf_Internal_Rela rela;
2030
2031       /* This symbols needs a copy reloc.  Set it up.  */
2032       BFD_ASSERT (h->dynindx != -1
2033                   && (h->root.type == bfd_link_hash_defined
2034                       || h->root.type == bfd_link_hash_defweak));
2035
2036       s = bfd_get_section_by_name (h->root.u.def.section->owner,
2037                                    ".rela.bss");
2038       BFD_ASSERT (s != NULL);
2039
2040       rela.r_offset = (h->root.u.def.value
2041                        + h->root.u.def.section->output_section->vma
2042                        + h->root.u.def.section->output_offset);
2043       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_COPY);
2044       rela.r_addend = 0;
2045       loc = s->contents;
2046       loc += s->reloc_count * sizeof (Elf32_External_Rela);
2047       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2048       ++s->reloc_count;
2049     }
2050
2051   /* Mark some specially defined symbols as absolute.  */
2052   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2053       || h == htab->root.hgot)
2054     sym->st_shndx = SHN_ABS;
2055
2056   return TRUE;
2057 }
2058
2059 static enum elf_reloc_type_class
2060 or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2061                            const asection *rel_sec ATTRIBUTE_UNUSED,
2062                            const Elf_Internal_Rela *rela)
2063 {
2064   switch ((int) ELF32_R_TYPE (rela->r_info))
2065     {
2066     case R_OR1K_RELATIVE:  return reloc_class_relative;
2067     case R_OR1K_JMP_SLOT:  return reloc_class_plt;
2068     case R_OR1K_COPY:      return reloc_class_copy;
2069     default:               return reloc_class_normal;
2070     }
2071 }
2072
2073 /* Adjust a symbol defined by a dynamic object and referenced by a
2074    regular object.  The current definition is in some section of the
2075    dynamic object, but we're not including those sections.  We have to
2076    change the definition to something the rest of the link can
2077    understand.  */
2078
2079 static bfd_boolean
2080 or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2081                                 struct elf_link_hash_entry *h)
2082 {
2083   struct elf_or1k_link_hash_table *htab;
2084   struct elf_or1k_link_hash_entry *eh;
2085   struct elf_or1k_dyn_relocs *p;
2086   bfd *dynobj;
2087   asection *s;
2088
2089   dynobj = elf_hash_table (info)->dynobj;
2090
2091   /* Make sure we know what is going on here.  */
2092   BFD_ASSERT (dynobj != NULL
2093               && (h->needs_plt
2094                   || h->u.weakdef != NULL
2095                   || (h->def_dynamic
2096                       && h->ref_regular
2097                       && !h->def_regular)));
2098
2099   /* If this is a function, put it in the procedure linkage table.  We
2100      will fill in the contents of the procedure linkage table later,
2101      when we know the address of the .got section.  */
2102   if (h->type == STT_FUNC
2103       || h->needs_plt)
2104     {
2105       if (! info->shared
2106           && !h->def_dynamic
2107           && !h->ref_dynamic
2108           && h->root.type != bfd_link_hash_undefweak
2109           && h->root.type != bfd_link_hash_undefined)
2110         {
2111           /* This case can occur if we saw a PLT reloc in an input
2112              file, but the symbol was never referred to by a dynamic
2113              object.  In such a case, we don't actually need to build
2114              a procedure linkage table, and we can just do a PCREL
2115              reloc instead.  */
2116           h->plt.offset = (bfd_vma) -1;
2117           h->needs_plt = 0;
2118         }
2119
2120       return TRUE;
2121     }
2122   else
2123     h->plt.offset = (bfd_vma) -1;
2124
2125   /* If this is a weak symbol, and there is a real definition, the
2126      processor independent code will have arranged for us to see the
2127      real definition first, and we can just use the same value.  */
2128   if (h->u.weakdef != NULL)
2129     {
2130       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2131                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2132       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2133       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2134       return TRUE;
2135     }
2136
2137   /* This is a reference to a symbol defined by a dynamic object which
2138      is not a function.  */
2139
2140   /* If we are creating a shared library, we must presume that the
2141      only references to the symbol are via the global offset table.
2142      For such cases we need not do anything here; the relocations will
2143      be handled correctly by relocate_section.  */
2144   if (info->shared)
2145     return TRUE;
2146
2147   /* If there are no references to this symbol that do not use the
2148      GOT, we don't need to generate a copy reloc.  */
2149   if (!h->non_got_ref)
2150     return TRUE;
2151
2152   /* If -z nocopyreloc was given, we won't generate them either.  */
2153   if (info->nocopyreloc)
2154     {
2155       h->non_got_ref = 0;
2156       return TRUE;
2157     }
2158
2159   eh = (struct elf_or1k_link_hash_entry *) h;
2160   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2161     {
2162       s = p->sec->output_section;
2163       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2164         break;
2165     }
2166
2167   /* If we didn't find any dynamic relocs in sections which needs the
2168      copy reloc, then we'll be keeping the dynamic relocs and avoiding
2169      the copy reloc.  */
2170   if (p == NULL)
2171     {
2172       h->non_got_ref = 0;
2173       return TRUE;
2174     }
2175
2176   /* We must allocate the symbol in our .dynbss section, which will
2177      become part of the .bss section of the executable.  There will be
2178      an entry for this symbol in the .dynsym section.  The dynamic
2179      object will contain position independent code, so all references
2180      from the dynamic object to this symbol will go through the global
2181      offset table.  The dynamic linker will use the .dynsym entry to
2182      determine the address it must put in the global offset table, so
2183      both the dynamic object and the regular object will refer to the
2184      same memory location for the variable.  */
2185
2186   htab = or1k_elf_hash_table (info);
2187   if (htab == NULL)
2188     return FALSE;
2189
2190   s = htab->sdynbss;
2191   BFD_ASSERT (s != NULL);
2192
2193   /* We must generate a R_OR1K_COPY reloc to tell the dynamic linker
2194      to copy the initial value out of the dynamic object and into the
2195      runtime process image.  We need to remember the offset into the
2196      .rela.bss section we are going to use.  */
2197   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2198     {
2199       asection *srel;
2200
2201       srel = htab->srelbss;
2202       BFD_ASSERT (srel != NULL);
2203       srel->size += sizeof (Elf32_External_Rela);
2204       h->needs_copy = 1;
2205     }
2206
2207   return _bfd_elf_adjust_dynamic_copy (h, s);
2208 }
2209
2210 /* Allocate space in .plt, .got and associated reloc sections for
2211    dynamic relocs.  */
2212
2213 static bfd_boolean
2214 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2215 {
2216   struct bfd_link_info *info;
2217   struct elf_or1k_link_hash_table *htab;
2218   struct elf_or1k_link_hash_entry *eh;
2219   struct elf_or1k_dyn_relocs *p;
2220
2221   if (h->root.type == bfd_link_hash_indirect)
2222     return TRUE;
2223
2224   info = (struct bfd_link_info *) inf;
2225   htab = or1k_elf_hash_table (info);
2226   if (htab == NULL)
2227     return FALSE;
2228
2229   eh = (struct elf_or1k_link_hash_entry *) h;
2230
2231   if (htab->root.dynamic_sections_created
2232       && h->plt.refcount > 0)
2233     {
2234       /* Make sure this symbol is output as a dynamic symbol.
2235          Undefined weak syms won't yet be marked as dynamic.  */
2236       if (h->dynindx == -1
2237           && !h->forced_local)
2238         {
2239           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2240             return FALSE;
2241         }
2242
2243       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2244         {
2245           asection *s = htab->splt;
2246
2247           /* If this is the first .plt entry, make room for the special
2248              first entry.  */
2249           if (s->size == 0)
2250             s->size = PLT_ENTRY_SIZE;
2251
2252           h->plt.offset = s->size;
2253
2254           /* If this symbol is not defined in a regular file, and we are
2255              not generating a shared library, then set the symbol to this
2256              location in the .plt.  This is required to make function
2257              pointers compare as equal between the normal executable and
2258              the shared library.  */
2259           if (! info->shared
2260               && !h->def_regular)
2261             {
2262               h->root.u.def.section = s;
2263               h->root.u.def.value = h->plt.offset;
2264             }
2265
2266           /* Make room for this entry.  */
2267           s->size += PLT_ENTRY_SIZE;
2268
2269           /* We also need to make an entry in the .got.plt section, which
2270              will be placed in the .got section by the linker script.  */
2271           htab->sgotplt->size += 4;
2272
2273           /* We also need to make an entry in the .rel.plt section.  */
2274           htab->srelplt->size += sizeof (Elf32_External_Rela);
2275         }
2276       else
2277         {
2278           h->plt.offset = (bfd_vma) -1;
2279           h->needs_plt = 0;
2280         }
2281     }
2282   else
2283     {
2284       h->plt.offset = (bfd_vma) -1;
2285       h->needs_plt = 0;
2286     }
2287
2288   if (h->got.refcount > 0)
2289     {
2290       asection *s;
2291       bfd_boolean dyn;
2292       unsigned char tls_type;
2293
2294       /* Make sure this symbol is output as a dynamic symbol.
2295          Undefined weak syms won't yet be marked as dynamic.  */
2296       if (h->dynindx == -1
2297           && !h->forced_local)
2298         {
2299           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2300             return FALSE;
2301         }
2302
2303       s = htab->sgot;
2304
2305       h->got.offset = s->size;
2306
2307       tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
2308
2309       /* TLS GD requires two GOT and two relocs.  */
2310       if (tls_type == TLS_GD)
2311         s->size += 8;
2312       else
2313         s->size += 4;
2314       dyn = htab->root.dynamic_sections_created;
2315       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2316         {
2317           if (tls_type == TLS_GD)
2318             htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
2319           else
2320             htab->srelgot->size += sizeof (Elf32_External_Rela);
2321         }
2322     }
2323   else
2324     h->got.offset = (bfd_vma) -1;
2325
2326   if (eh->dyn_relocs == NULL)
2327     return TRUE;
2328
2329   /* In the shared -Bsymbolic case, discard space allocated for
2330      dynamic pc-relative relocs against symbols which turn out to be
2331      defined in regular objects.  For the normal shared case, discard
2332      space for pc-relative relocs that have become local due to symbol
2333      visibility changes.  */
2334
2335   if (info->shared)
2336     {
2337       if (h->def_regular
2338           && (h->forced_local
2339               || info->symbolic))
2340         {
2341           struct elf_or1k_dyn_relocs **pp;
2342
2343           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
2344             {
2345               p->count -= p->pc_count;
2346               p->pc_count = 0;
2347               if (p->count == 0)
2348                 *pp = p->next;
2349               else
2350                 pp = &p->next;
2351             }
2352         }
2353
2354       /* Also discard relocs on undefined weak syms with non-default
2355          visibility.  */
2356       if (eh->dyn_relocs != NULL
2357           && h->root.type == bfd_link_hash_undefweak)
2358         {
2359           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2360             eh->dyn_relocs = NULL;
2361
2362           /* Make sure undefined weak symbols are output as a dynamic
2363              symbol in PIEs.  */
2364           else if (h->dynindx == -1
2365                    && !h->forced_local)
2366             {
2367               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2368                 return FALSE;
2369             }
2370         }
2371     }
2372   else
2373     {
2374       /* For the non-shared case, discard space for relocs against
2375          symbols which turn out to need copy relocs or are not
2376          dynamic.  */
2377
2378       if (!h->non_got_ref
2379           && ((h->def_dynamic
2380                && !h->def_regular)
2381               || (htab->root.dynamic_sections_created
2382                   && (h->root.type == bfd_link_hash_undefweak
2383                       || h->root.type == bfd_link_hash_undefined))))
2384         {
2385           /* Make sure this symbol is output as a dynamic symbol.
2386              Undefined weak syms won't yet be marked as dynamic.  */
2387           if (h->dynindx == -1
2388               && !h->forced_local)
2389             {
2390               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2391                 return FALSE;
2392             }
2393
2394           /* If that succeeded, we know we'll be keeping all the
2395              relocs.  */
2396           if (h->dynindx != -1)
2397             goto keep;
2398         }
2399
2400       eh->dyn_relocs = NULL;
2401
2402     keep: ;
2403     }
2404
2405   /* Finally, allocate space.  */
2406   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2407     {
2408       asection *sreloc = elf_section_data (p->sec)->sreloc;
2409       sreloc->size += p->count * sizeof (Elf32_External_Rela);
2410     }
2411
2412   return TRUE;
2413 }
2414
2415 /* Find any dynamic relocs that apply to read-only sections.  */
2416
2417 static bfd_boolean
2418 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2419 {
2420   struct elf_or1k_link_hash_entry *eh;
2421   struct elf_or1k_dyn_relocs *p;
2422
2423   eh = (struct elf_or1k_link_hash_entry *) h;
2424   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2425     {
2426       asection *s = p->sec->output_section;
2427
2428       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2429         {
2430           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2431
2432           info->flags |= DF_TEXTREL;
2433
2434           /* Not an error, just cut short the traversal.  */
2435           return FALSE;
2436         }
2437     }
2438   return TRUE;
2439 }
2440
2441 /* Set the sizes of the dynamic sections.  */
2442
2443 static bfd_boolean
2444 or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2445                                 struct bfd_link_info *info)
2446 {
2447   struct elf_or1k_link_hash_table *htab;
2448   bfd *dynobj;
2449   asection *s;
2450   bfd_boolean relocs;
2451   bfd *ibfd;
2452
2453   htab = or1k_elf_hash_table (info);
2454   if (htab == NULL)
2455     return FALSE;
2456
2457   dynobj = htab->root.dynobj;
2458   BFD_ASSERT (dynobj != NULL);
2459
2460   if (htab->root.dynamic_sections_created)
2461     {
2462       /* Set the contents of the .interp section to the interpreter.  */
2463       if (info->executable)
2464         {
2465           s = bfd_get_section_by_name (dynobj, ".interp");
2466           BFD_ASSERT (s != NULL);
2467           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2468           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2469         }
2470     }
2471
2472   /* Set up .got offsets for local syms, and space for local dynamic
2473      relocs.  */
2474   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2475     {
2476       bfd_signed_vma *local_got;
2477       bfd_signed_vma *end_local_got;
2478       bfd_size_type locsymcount;
2479       Elf_Internal_Shdr *symtab_hdr;
2480       unsigned char *local_tls_type;
2481       asection *srel;
2482
2483       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2484         continue;
2485
2486       for (s = ibfd->sections; s != NULL; s = s->next)
2487         {
2488           struct elf_or1k_dyn_relocs *p;
2489
2490           for (p = ((struct elf_or1k_dyn_relocs *)
2491                     elf_section_data (s)->local_dynrel);
2492                p != NULL;
2493                p = p->next)
2494             {
2495               if (! bfd_is_abs_section (p->sec)
2496                   && bfd_is_abs_section (p->sec->output_section))
2497                 {
2498                   /* Input section has been discarded, either because
2499                      it is a copy of a linkonce section or due to
2500                      linker script /DISCARD/, so we'll be discarding
2501                      the relocs too.  */
2502                 }
2503               else if (p->count != 0)
2504                 {
2505                   srel = elf_section_data (p->sec)->sreloc;
2506                   srel->size += p->count * sizeof (Elf32_External_Rela);
2507                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2508                     info->flags |= DF_TEXTREL;
2509                 }
2510             }
2511         }
2512
2513       local_got = elf_local_got_refcounts (ibfd);
2514       if (!local_got)
2515         continue;
2516
2517       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2518       locsymcount = symtab_hdr->sh_info;
2519       end_local_got = local_got + locsymcount;
2520       s = htab->sgot;
2521       srel = htab->srelgot;
2522       local_tls_type = (unsigned char *) elf_or1k_local_tls_type (ibfd);
2523       for (; local_got < end_local_got; ++local_got)
2524         {
2525           if (*local_got > 0)
2526             {
2527               *local_got = s->size;
2528
2529               /* TLS GD requires two GOT and two relocs.  */
2530               if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2531                 s->size += 8;
2532               else
2533                 s->size += 4;
2534               if (info->shared)
2535                 {
2536                   if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2537                     srel->size += 2 * sizeof (Elf32_External_Rela);
2538                   else
2539                     srel->size += sizeof (Elf32_External_Rela);
2540                 }
2541             }
2542           else
2543
2544             *local_got = (bfd_vma) -1;
2545
2546           if (local_tls_type)
2547             ++local_tls_type;
2548         }
2549     }
2550
2551   /* Allocate global sym .plt and .got entries, and space for global
2552      sym dynamic relocs.  */
2553   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
2554
2555   /* We now have determined the sizes of the various dynamic sections.
2556      Allocate memory for them.  */
2557   relocs = FALSE;
2558   for (s = dynobj->sections; s != NULL; s = s->next)
2559     {
2560       if ((s->flags & SEC_LINKER_CREATED) == 0)
2561         continue;
2562
2563       if (s == htab->splt
2564           || s == htab->sgot
2565           || s == htab->sgotplt
2566           || s == htab->sdynbss)
2567         {
2568           /* Strip this section if we don't need it; see the
2569              comment below.  */
2570         }
2571       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2572         {
2573           if (s->size != 0 && s != htab->srelplt)
2574             relocs = TRUE;
2575
2576           /* We use the reloc_count field as a counter if we need
2577              to copy relocs into the output file.  */
2578           s->reloc_count = 0;
2579         }
2580       else
2581         /* It's not one of our sections, so don't allocate space.  */
2582         continue;
2583
2584       if (s->size == 0)
2585         {
2586           /* If we don't need this section, strip it from the
2587              output file.  This is mostly to handle .rela.bss and
2588              .rela.plt.  We must create both sections in
2589              create_dynamic_sections, because they must be created
2590              before the linker maps input sections to output
2591              sections.  The linker does that before
2592              adjust_dynamic_symbol is called, and it is that
2593              function which decides whether anything needs to go
2594              into these sections.  */
2595           s->flags |= SEC_EXCLUDE;
2596           continue;
2597         }
2598
2599       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2600         continue;
2601
2602       /* Allocate memory for the section contents.  We use bfd_zalloc
2603          here in case unused entries are not reclaimed before the
2604          section's contents are written out.  This should not happen,
2605          but this way if it does, we get a R_OR1K_NONE reloc instead
2606          of garbage.  */
2607       s->contents = bfd_zalloc (dynobj, s->size);
2608
2609       if (s->contents == NULL)
2610         return FALSE;
2611     }
2612
2613   if (htab->root.dynamic_sections_created)
2614     {
2615       /* Add some entries to the .dynamic section.  We fill in the
2616          values later, in or1k_elf_finish_dynamic_sections, but we
2617          must add the entries now so that we get the correct size for
2618          the .dynamic section.  The DT_DEBUG entry is filled in by the
2619          dynamic linker and used by the debugger.  */
2620 #define add_dynamic_entry(TAG, VAL) \
2621   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2622
2623      if (info->executable)
2624        {
2625          if (! add_dynamic_entry (DT_DEBUG, 0))
2626            return FALSE;
2627        }
2628
2629      if (htab->splt->size != 0)
2630        {
2631          if (! add_dynamic_entry (DT_PLTGOT, 0)
2632              || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2633              || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2634              || ! add_dynamic_entry (DT_JMPREL, 0))
2635            return FALSE;
2636         }
2637
2638      if (relocs)
2639        {
2640          if (! add_dynamic_entry (DT_RELA, 0)
2641              || ! add_dynamic_entry (DT_RELASZ, 0)
2642              || ! add_dynamic_entry (DT_RELAENT,
2643                                      sizeof (Elf32_External_Rela)))
2644            return FALSE;
2645
2646          /* If any dynamic relocs apply to a read-only section,
2647             then we need a DT_TEXTREL entry.  */
2648          if ((info->flags & DF_TEXTREL) == 0)
2649            elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2650                                    info);
2651
2652          if ((info->flags & DF_TEXTREL) != 0)
2653            {
2654              if (! add_dynamic_entry (DT_TEXTREL, 0))
2655                return FALSE;
2656            }
2657        }
2658     }
2659
2660 #undef add_dynamic_entry
2661   return TRUE;
2662 }
2663
2664 /* Create dynamic sections when linking against a dynamic object.  */
2665
2666 static bfd_boolean
2667 or1k_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2668 {
2669   struct elf_or1k_link_hash_table *htab;
2670
2671   htab = or1k_elf_hash_table (info);
2672   if (htab == NULL)
2673     return FALSE;
2674
2675   if (!htab->sgot && !create_got_section (dynobj, info))
2676     return FALSE;
2677
2678   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2679     return FALSE;
2680
2681   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2682   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2683   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2684   if (!info->shared)
2685     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2686
2687   if (!htab->splt || !htab->srelplt || !htab->sdynbss
2688       || (!info->shared && !htab->srelbss))
2689     abort ();
2690
2691   return TRUE;
2692 }
2693
2694 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2695
2696 static void
2697 or1k_elf_copy_indirect_symbol (struct bfd_link_info *info,
2698                                struct elf_link_hash_entry *dir,
2699                                struct elf_link_hash_entry *ind)
2700 {
2701   struct elf_or1k_link_hash_entry * edir;
2702   struct elf_or1k_link_hash_entry * eind;
2703
2704   edir = (struct elf_or1k_link_hash_entry *) dir;
2705   eind = (struct elf_or1k_link_hash_entry *) ind;
2706
2707   if (eind->dyn_relocs != NULL)
2708     {
2709       if (edir->dyn_relocs != NULL)
2710         {
2711           struct elf_or1k_dyn_relocs **pp;
2712           struct elf_or1k_dyn_relocs *p;
2713
2714           /* Add reloc counts against the indirect sym to the direct sym
2715              list.  Merge any entries against the same section.  */
2716           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2717             {
2718               struct elf_or1k_dyn_relocs *q;
2719
2720               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2721                 if (q->sec == p->sec)
2722                   {
2723                     q->pc_count += p->pc_count;
2724                     q->count += p->count;
2725                     *pp = p->next;
2726                     break;
2727                   }
2728               if (q == NULL)
2729                 pp = &p->next;
2730             }
2731           *pp = edir->dyn_relocs;
2732         }
2733
2734       edir->dyn_relocs = eind->dyn_relocs;
2735       eind->dyn_relocs = NULL;
2736     }
2737
2738   if (ind->root.type == bfd_link_hash_indirect)
2739     {
2740       if (dir->got.refcount <= 0)
2741         {
2742           edir->tls_type = eind->tls_type;
2743           eind->tls_type = TLS_UNKNOWN;
2744         }
2745     }
2746
2747   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2748 }
2749
2750 /* Set the right machine number.  */
2751
2752 static bfd_boolean
2753 or1k_elf_object_p (bfd *abfd)
2754 {
2755   unsigned long mach = bfd_mach_or1k;
2756
2757   if (elf_elfheader (abfd)->e_flags & EF_OR1K_NODELAY)
2758     mach = bfd_mach_or1knd;
2759
2760   return bfd_default_set_arch_mach (abfd, bfd_arch_or1k, mach);
2761 }
2762
2763 /* Store the machine number in the flags field.  */
2764
2765 static void
2766 or1k_elf_final_write_processing (bfd *abfd,
2767                                  bfd_boolean linker ATTRIBUTE_UNUSED)
2768 {
2769   switch (bfd_get_mach (abfd))
2770     {
2771     default:
2772     case bfd_mach_or1k:
2773       break;
2774     case bfd_mach_or1knd:
2775       elf_elfheader (abfd)->e_flags |= EF_OR1K_NODELAY;
2776       break;
2777     }
2778 }
2779
2780 static bfd_boolean
2781 or1k_elf_set_private_flags (bfd *abfd, flagword flags)
2782 {
2783   BFD_ASSERT (!elf_flags_init (abfd)
2784               || elf_elfheader (abfd)->e_flags == flags);
2785
2786   elf_elfheader (abfd)->e_flags = flags;
2787   elf_flags_init (abfd) = TRUE;
2788   return TRUE;
2789 }
2790
2791 /* Make sure all input files are consistent with respect to
2792    EF_OR1K_NODELAY flag setting.  */
2793
2794 static bfd_boolean
2795 elf32_or1k_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2796 {
2797   flagword out_flags;
2798   flagword in_flags;
2799
2800   in_flags  = elf_elfheader (ibfd)->e_flags;
2801   out_flags = elf_elfheader (obfd)->e_flags;
2802
2803   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2804       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2805     return TRUE;
2806
2807   if (!elf_flags_init (obfd))
2808     {
2809       elf_flags_init (obfd) = TRUE;
2810       elf_elfheader (obfd)->e_flags = in_flags;
2811
2812       return TRUE;
2813     }
2814
2815   if (in_flags == out_flags)
2816     return TRUE;
2817
2818   if ((in_flags & EF_OR1K_NODELAY) != (out_flags & EF_OR1K_NODELAY))
2819     {
2820       (*_bfd_error_handler)
2821         (_("%B: EF_OR1K_NODELAY flag mismatch with previous modules"), ibfd);
2822
2823       bfd_set_error (bfd_error_bad_value);
2824       return FALSE;
2825     }
2826
2827   return TRUE;
2828
2829 }
2830
2831 #define ELF_ARCH                        bfd_arch_or1k
2832 #define ELF_MACHINE_CODE                EM_OR1K
2833 #define ELF_TARGET_ID                   OR1K_ELF_DATA
2834 #define ELF_MAXPAGESIZE                 0x2000
2835
2836 #define TARGET_BIG_SYM                  bfd_elf32_or1k_vec
2837 #define TARGET_BIG_NAME                 "elf32-or1k"
2838
2839 #define elf_info_to_howto_rel           NULL
2840 #define elf_info_to_howto               or1k_info_to_howto_rela
2841 #define elf_backend_relocate_section    or1k_elf_relocate_section
2842 #define elf_backend_gc_mark_hook        or1k_elf_gc_mark_hook
2843 #define elf_backend_gc_sweep_hook       or1k_elf_gc_sweep_hook
2844 #define elf_backend_check_relocs        or1k_elf_check_relocs
2845 #define elf_backend_reloc_type_class    or1k_elf_reloc_type_class
2846 #define elf_backend_can_gc_sections     1
2847 #define elf_backend_rela_normal         1
2848
2849 #define bfd_elf32_mkobject                   elf_or1k_mkobject
2850
2851 #define bfd_elf32_bfd_merge_private_bfd_data elf32_or1k_merge_private_bfd_data
2852 #define bfd_elf32_bfd_set_private_flags or1k_elf_set_private_flags
2853 #define bfd_elf32_bfd_reloc_type_lookup or1k_reloc_type_lookup
2854 #define bfd_elf32_bfd_reloc_name_lookup or1k_reloc_name_lookup
2855
2856 #define elf_backend_object_p                or1k_elf_object_p
2857 #define elf_backend_final_write_processing  or1k_elf_final_write_processing
2858 #define elf_backend_can_refcount                1
2859
2860 #define elf_backend_plt_readonly                1
2861 #define elf_backend_want_got_plt                1
2862 #define elf_backend_want_plt_sym                0
2863 #define elf_backend_got_header_size             12
2864 #define bfd_elf32_bfd_link_hash_table_create    or1k_elf_link_hash_table_create
2865 #define elf_backend_copy_indirect_symbol        or1k_elf_copy_indirect_symbol
2866 #define elf_backend_create_dynamic_sections     or1k_elf_create_dynamic_sections
2867 #define elf_backend_finish_dynamic_sections     or1k_elf_finish_dynamic_sections
2868 #define elf_backend_size_dynamic_sections       or1k_elf_size_dynamic_sections
2869 #define elf_backend_adjust_dynamic_symbol       or1k_elf_adjust_dynamic_symbol
2870 #define elf_backend_finish_dynamic_symbol       or1k_elf_finish_dynamic_symbol
2871
2872 #include "elf32-target.h"