dyn_relocs tidy
[external/binutils.git] / bfd / elf32-or1k.c
1 /* Or1k-specific support for 32-bit ELF.
2    Copyright (C) 2001-2017 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          3,                     /* size (0 = byte, 1 = short, 2 = long) */
64          0,                     /* 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          TRUE),                 /* 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          TRUE),                 /* 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          TRUE),                 /* 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 #define TLS_UNKNOWN    0
560 #define TLS_NONE       1
561 #define TLS_GD         2
562 #define TLS_LD         3
563 #define TLS_IE         4
564 #define TLS_LE         5
565
566 /* ELF linker hash entry.  */
567 struct elf_or1k_link_hash_entry
568 {
569   struct elf_link_hash_entry root;
570
571   /* Track dynamic relocs copied for this symbol.  */
572   struct elf_dyn_relocs *dyn_relocs;
573
574   /* Track type of TLS access.  */
575   unsigned char tls_type;
576 };
577
578 /* ELF object data.  */
579 struct elf_or1k_obj_tdata
580 {
581   struct elf_obj_tdata root;
582
583   /* tls_type for each local got entry.  */
584   unsigned char *local_tls_type;
585 };
586
587 #define elf_or1k_tdata(abfd) \
588   ((struct elf_or1k_obj_tdata *) (abfd)->tdata.any)
589
590 #define elf_or1k_local_tls_type(abfd) \
591   (elf_or1k_tdata (abfd)->local_tls_type)
592
593 /* ELF linker hash table.  */
594 struct elf_or1k_link_hash_table
595 {
596   struct elf_link_hash_table root;
597
598   /* Small local sym to section mapping cache.  */
599   struct sym_cache sym_sec;
600 };
601
602 /* Get the ELF linker hash table from a link_info structure.  */
603 #define or1k_elf_hash_table(p) \
604   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
605    == OR1K_ELF_DATA ? ((struct elf_or1k_link_hash_table *) ((p)->hash)) : NULL)
606
607 static bfd_boolean
608 elf_or1k_mkobject (bfd *abfd)
609 {
610   return bfd_elf_allocate_object (abfd, sizeof (struct elf_or1k_obj_tdata),
611                                   OR1K_ELF_DATA);
612 }
613
614 /* Create an entry in an or1k ELF linker hash table.  */
615
616 static struct bfd_hash_entry *
617 or1k_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
618                             struct bfd_hash_table *table,
619                             const char *string)
620 {
621   struct elf_or1k_link_hash_entry *ret =
622     (struct elf_or1k_link_hash_entry *) entry;
623
624   /* Allocate the structure if it has not already been allocated by a
625      subclass.  */
626   if (ret == NULL)
627     ret = bfd_hash_allocate (table,
628                              sizeof (struct elf_or1k_link_hash_entry));
629   if (ret == NULL)
630     return NULL;
631
632   /* Call the allocation method of the superclass.  */
633   ret = ((struct elf_or1k_link_hash_entry *)
634          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
635                                      table, string));
636   if (ret != NULL)
637     {
638       struct elf_or1k_link_hash_entry *eh;
639
640       eh = (struct elf_or1k_link_hash_entry *) ret;
641       eh->dyn_relocs = NULL;
642       eh->tls_type = TLS_UNKNOWN;
643     }
644
645   return (struct bfd_hash_entry *) ret;
646 }
647
648 /* Create an or1k ELF linker hash table.  */
649
650 static struct bfd_link_hash_table *
651 or1k_elf_link_hash_table_create (bfd *abfd)
652 {
653   struct elf_or1k_link_hash_table *ret;
654   bfd_size_type amt = sizeof (struct elf_or1k_link_hash_table);
655
656   ret = bfd_zmalloc (amt);
657   if (ret == NULL)
658     return NULL;
659
660   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
661                                       or1k_elf_link_hash_newfunc,
662                                       sizeof (struct elf_or1k_link_hash_entry),
663                                       OR1K_ELF_DATA))
664     {
665       free (ret);
666       return NULL;
667     }
668
669   return &ret->root.root;
670 }
671
672 static reloc_howto_type *
673 or1k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
674                         bfd_reloc_code_real_type code)
675 {
676   unsigned int i;
677
678   for (i = ARRAY_SIZE (or1k_reloc_map); i--;)
679     if (or1k_reloc_map[i].bfd_reloc_val == code)
680       return & or1k_elf_howto_table[or1k_reloc_map[i].or1k_reloc_val];
681
682   return NULL;
683 }
684
685 static reloc_howto_type *
686 or1k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
687                         const char *r_name)
688 {
689   unsigned int i;
690
691   for (i = 0;
692        i < (sizeof (or1k_elf_howto_table)
693             / sizeof (or1k_elf_howto_table[0]));
694        i++)
695     if (or1k_elf_howto_table[i].name != NULL
696         && strcasecmp (or1k_elf_howto_table[i].name, r_name) == 0)
697       return &or1k_elf_howto_table[i];
698
699   return NULL;
700 }
701
702 /* Set the howto pointer for an Or1k ELF reloc.  */
703
704 static void
705 or1k_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
706                          arelent * cache_ptr,
707                          Elf_Internal_Rela * dst)
708 {
709   unsigned int r_type;
710
711   r_type = ELF32_R_TYPE (dst->r_info);
712   if (r_type >= (unsigned int) R_OR1K_max)
713     {
714       /* xgettext:c-format */
715       _bfd_error_handler (_("%B: invalid OR1K reloc number: %d"), abfd, r_type);
716       r_type = 0;
717     }
718   cache_ptr->howto = & or1k_elf_howto_table[r_type];
719 }
720
721
722 /* Return the relocation value for @tpoff relocations..  */
723 static bfd_vma
724 tpoff (struct bfd_link_info *info, bfd_vma address)
725 {
726   /* If tls_sec is NULL, we should have signalled an error already.  */
727   if (elf_hash_table (info)->tls_sec == NULL)
728     return 0;
729
730   /* The thread pointer on or1k stores the address after the TCB where
731      the data is, just compute the difference. No need to compensate
732      for the size of TCB.  */
733   return (address - elf_hash_table (info)->tls_sec->vma);
734 }
735
736 /* Relocate an Or1k ELF section.
737
738    The RELOCATE_SECTION function is called by the new ELF backend linker
739    to handle the relocations for a section.
740
741    The relocs are always passed as Rela structures; if the section
742    actually uses Rel structures, the r_addend field will always be
743    zero.
744
745    This function is responsible for adjusting the section contents as
746    necessary, and (if using Rela relocs and generating a relocatable
747    output file) adjusting the reloc addend as necessary.
748
749    This function does not have to worry about setting the reloc
750    address or the reloc symbol index.
751
752    LOCAL_SYMS is a pointer to the swapped in local symbols.
753
754    LOCAL_SECTIONS is an array giving the section in the input file
755    corresponding to the st_shndx field of each local symbol.
756
757    The global hash table entry for the global symbols can be found
758    via elf_sym_hashes (input_bfd).
759
760    When generating relocatable output, this function must handle
761    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
762    going to be the section symbol corresponding to the output
763    section, which means that the addend must be adjusted
764    accordingly.  */
765
766 static bfd_boolean
767 or1k_elf_relocate_section (bfd *output_bfd,
768                            struct bfd_link_info *info,
769                            bfd *input_bfd,
770                            asection *input_section,
771                            bfd_byte *contents,
772                            Elf_Internal_Rela *relocs,
773                            Elf_Internal_Sym *local_syms,
774                            asection **local_sections)
775 {
776   Elf_Internal_Shdr *symtab_hdr;
777   struct elf_link_hash_entry **sym_hashes;
778   Elf_Internal_Rela *rel;
779   Elf_Internal_Rela *relend;
780   struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
781   bfd *dynobj;
782   asection *sreloc;
783   bfd_vma *local_got_offsets;
784   asection *sgot;
785
786   if (htab == NULL)
787     return FALSE;
788
789   dynobj = htab->root.dynobj;
790   local_got_offsets = elf_local_got_offsets (input_bfd);
791
792   sreloc = elf_section_data (input_section)->sreloc;
793
794   sgot = htab->root.sgot;
795
796   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
797   sym_hashes = elf_sym_hashes (input_bfd);
798   relend = relocs + input_section->reloc_count;
799
800   for (rel = relocs; rel < relend; rel++)
801     {
802       reloc_howto_type *howto;
803       unsigned long r_symndx;
804       Elf_Internal_Sym *sym;
805       asection *sec;
806       struct elf_link_hash_entry *h;
807       bfd_vma relocation;
808       bfd_reloc_status_type r;
809       const char *name = NULL;
810       int r_type;
811
812       r_type = ELF32_R_TYPE (rel->r_info);
813       r_symndx = ELF32_R_SYM (rel->r_info);
814
815       if (r_type == R_OR1K_GNU_VTINHERIT
816           || r_type == R_OR1K_GNU_VTENTRY)
817         continue;
818
819       if (r_type < 0 || r_type >= (int) R_OR1K_max)
820         {
821           bfd_set_error (bfd_error_bad_value);
822           return FALSE;
823         }
824
825       howto = or1k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
826       h = NULL;
827       sym = NULL;
828       sec = NULL;
829
830       if (r_symndx < symtab_hdr->sh_info)
831         {
832           sym = local_syms + r_symndx;
833           sec = local_sections[r_symndx];
834           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
835
836           name = bfd_elf_string_from_elf_section
837             (input_bfd, symtab_hdr->sh_link, sym->st_name);
838           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
839         }
840       else
841         {
842           bfd_boolean unresolved_reloc, warned, ignored;
843
844           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
845                                    r_symndx, symtab_hdr, sym_hashes,
846                                    h, sec, relocation,
847                                    unresolved_reloc, warned, ignored);
848         }
849
850       if (sec != NULL && discarded_section (sec))
851         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
852                                          rel, 1, relend, howto, 0, contents);
853
854       if (bfd_link_relocatable (info))
855         continue;
856
857       switch (howto->type)
858         {
859         case R_OR1K_PLT26:
860           {
861             if (htab->root.splt != NULL && h != NULL
862                 && h->plt.offset != (bfd_vma) -1)
863               {
864                 relocation = (htab->root.splt->output_section->vma
865                               + htab->root.splt->output_offset
866                               + h->plt.offset);
867               }
868             break;
869           }
870
871         case R_OR1K_GOT16:
872           /* Relocation is to the entry for this symbol in the global
873              offset table.  */
874           BFD_ASSERT (sgot != NULL);
875           if (h != NULL)
876             {
877               bfd_boolean dyn;
878               bfd_vma off;
879
880               off = h->got.offset;
881               BFD_ASSERT (off != (bfd_vma) -1);
882
883               dyn = htab->root.dynamic_sections_created;
884               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
885                                                      bfd_link_pic (info),
886                                                      h)
887                   || (bfd_link_pic (info)
888                       && SYMBOL_REFERENCES_LOCAL (info, h)))
889                 {
890                   /* This is actually a static link, or it is a
891                      -Bsymbolic link and the symbol is defined
892                      locally, or the symbol was forced to be local
893                      because of a version file.  We must initialize
894                      this entry in the global offset table.  Since the
895                      offset must always be a multiple of 4, we use the
896                      least significant bit to record whether we have
897                      initialized it already.
898
899                      When doing a dynamic link, we create a .rela.got
900                      relocation entry to initialize the value.  This
901                      is done in the finish_dynamic_symbol routine.  */
902                   if ((off & 1) != 0)
903                     off &= ~1;
904                   else
905                     {
906                       /* Write entry in GOT.  */
907                       bfd_put_32 (output_bfd, relocation,
908                                   sgot->contents + off);
909                       /* Mark GOT entry as having been written.  */
910                       h->got.offset |= 1;
911                     }
912                 }
913
914               relocation = sgot->output_offset + off;
915             }
916           else
917             {
918               bfd_vma off;
919               bfd_byte *loc;
920
921               BFD_ASSERT (local_got_offsets != NULL
922                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
923
924               /* Get offset into GOT table.  */
925               off = local_got_offsets[r_symndx];
926
927               /* The offset must always be a multiple of 4.  We use
928                  the least significant bit to record whether we have
929                  already processed this entry.  */
930               if ((off & 1) != 0)
931                 off &= ~1;
932               else
933                 {
934                   /* Write entry in GOT.  */
935                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
936                   if (bfd_link_pic (info))
937                     {
938                       asection *srelgot;
939                       Elf_Internal_Rela outrel;
940
941                       /* We need to generate a R_OR1K_RELATIVE reloc
942                          for the dynamic linker.  */
943                       srelgot = htab->root.srelgot;
944                       BFD_ASSERT (srelgot != NULL);
945
946                       outrel.r_offset = (sgot->output_section->vma
947                                          + sgot->output_offset
948                                          + off);
949                       outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
950                       outrel.r_addend = relocation;
951                       loc = srelgot->contents;
952                       loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
953                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
954                       ++srelgot->reloc_count;
955                     }
956
957                   local_got_offsets[r_symndx] |= 1;
958                 }
959               relocation = sgot->output_offset + off;
960             }
961
962           /* Addend should be zero.  */
963           if (rel->r_addend != 0)
964             _bfd_error_handler
965               (_("internal error: addend should be zero for R_OR1K_GOT16"));
966
967           break;
968
969         case R_OR1K_GOTOFF_LO16:
970         case R_OR1K_GOTOFF_HI16:
971           /* Relocation is offset from GOT.  */
972           BFD_ASSERT (sgot != NULL);
973           relocation
974             -= (htab->root.hgot->root.u.def.value
975                 + htab->root.hgot->root.u.def.section->output_offset
976                 + htab->root.hgot->root.u.def.section->output_section->vma);
977           break;
978
979         case R_OR1K_INSN_REL_26:
980         case R_OR1K_HI_16_IN_INSN:
981         case R_OR1K_LO_16_IN_INSN:
982         case R_OR1K_32:
983           /* R_OR1K_16? */
984           {
985             /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
986                from removed linkonce sections, or sections discarded by
987                a linker script.  */
988             if (r_symndx == STN_UNDEF
989                 || (input_section->flags & SEC_ALLOC) == 0)
990               break;
991
992             if ((bfd_link_pic (info)
993                  && (h == NULL
994                      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
995                      || h->root.type != bfd_link_hash_undefweak)
996                  && (howto->type != R_OR1K_INSN_REL_26
997                      || !SYMBOL_CALLS_LOCAL (info, h)))
998                 || (!bfd_link_pic (info)
999                     && h != NULL
1000                     && h->dynindx != -1
1001                     && !h->non_got_ref
1002                     && ((h->def_dynamic
1003                          && !h->def_regular)
1004                         || h->root.type == bfd_link_hash_undefweak
1005                         || h->root.type == bfd_link_hash_undefined)))
1006               {
1007                 Elf_Internal_Rela outrel;
1008                 bfd_byte *loc;
1009                 bfd_boolean skip;
1010
1011                 /* When generating a shared object, these relocations
1012                    are copied into the output file to be resolved at run
1013                    time.  */
1014
1015                 BFD_ASSERT (sreloc != NULL);
1016
1017                 skip = FALSE;
1018
1019                 outrel.r_offset =
1020                   _bfd_elf_section_offset (output_bfd, info, input_section,
1021                                            rel->r_offset);
1022                 if (outrel.r_offset == (bfd_vma) -1)
1023                   skip = TRUE;
1024                 else if (outrel.r_offset == (bfd_vma) -2)
1025                   skip = TRUE;
1026                 outrel.r_offset += (input_section->output_section->vma
1027                                     + input_section->output_offset);
1028
1029                 if (skip)
1030                   memset (&outrel, 0, sizeof outrel);
1031                 /* h->dynindx may be -1 if the symbol was marked to
1032                    become local.  */
1033                 else if (h != NULL
1034                          && ((! info->symbolic && h->dynindx != -1)
1035                              || !h->def_regular))
1036                   {
1037                     BFD_ASSERT (h->dynindx != -1);
1038                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1039                     outrel.r_addend = rel->r_addend;
1040                   }
1041                 else
1042                   {
1043                     if (r_type == R_OR1K_32)
1044                       {
1045                         outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1046                         outrel.r_addend = relocation + rel->r_addend;
1047                       }
1048                     else
1049                       {
1050                         BFD_FAIL ();
1051                         _bfd_error_handler
1052                           (_("%B: probably compiled without -fPIC?"),
1053                            input_bfd);
1054                         bfd_set_error (bfd_error_bad_value);
1055                         return FALSE;
1056                       }
1057                   }
1058
1059                 loc = sreloc->contents;
1060                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1061                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1062                 break;
1063               }
1064             break;
1065           }
1066
1067         case R_OR1K_TLS_LDM_HI16:
1068         case R_OR1K_TLS_LDM_LO16:
1069         case R_OR1K_TLS_LDO_HI16:
1070         case R_OR1K_TLS_LDO_LO16:
1071           /* TODO: implement support for local dynamic.  */
1072           BFD_FAIL ();
1073           _bfd_error_handler
1074             (_("%B: support for local dynamic not implemented"),
1075              input_bfd);
1076           bfd_set_error (bfd_error_bad_value);
1077           return FALSE;
1078
1079
1080         case R_OR1K_TLS_GD_HI16:
1081         case R_OR1K_TLS_GD_LO16:
1082         case R_OR1K_TLS_IE_HI16:
1083         case R_OR1K_TLS_IE_LO16:
1084           {
1085             bfd_vma gotoff;
1086             Elf_Internal_Rela rela;
1087             bfd_byte *loc;
1088             int dynamic;
1089
1090             sreloc = bfd_get_section_by_name (dynobj, ".rela.got");
1091
1092             /* Mark as TLS related GOT entry by setting
1093                bit 2 as well as bit 1.  */
1094             if (h != NULL)
1095               {
1096                 gotoff = h->got.offset;
1097                 h->got.offset |= 3;
1098               }
1099             else
1100               {
1101                 gotoff = local_got_offsets[r_symndx];
1102                 local_got_offsets[r_symndx] |= 3;
1103               }
1104
1105             /* Only process the relocation once.  */
1106             if (gotoff & 1)
1107               {
1108                 relocation = sgot->output_offset + (gotoff  & ~3);
1109                 break;
1110               }
1111
1112             BFD_ASSERT (elf_hash_table (info)->hgot == NULL
1113                         || elf_hash_table (info)->hgot->root.u.def.value == 0);
1114
1115             /* Dynamic entries will require relocations. if we do not need
1116                them we will just use the default R_OR1K_NONE and
1117                not set anything.  */
1118             dynamic = bfd_link_pic (info)
1119               || (sec && (sec->flags & SEC_ALLOC) != 0
1120                   && h != NULL
1121                   && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
1122
1123             /* Shared GD.  */
1124             if (dynamic && (howto->type == R_OR1K_TLS_GD_HI16
1125                             || howto->type == R_OR1K_TLS_GD_LO16))
1126               {
1127                 int i;
1128
1129                 /* Add DTPMOD and DTPOFF GOT and rela entries.  */
1130                 for (i = 0; i < 2; ++i)
1131                   {
1132                     rela.r_offset = sgot->output_section->vma +
1133                       sgot->output_offset + gotoff + i*4;
1134                     if (h != NULL && h->dynindx != -1)
1135                       {
1136                         rela.r_info = ELF32_R_INFO (h->dynindx,
1137                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1138                         rela.r_addend = 0;
1139                       }
1140                     else
1141                       {
1142                         rela.r_info = ELF32_R_INFO (0,
1143                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1144                         rela.r_addend = tpoff (info, relocation);
1145                       }
1146
1147                     loc = sreloc->contents;
1148                     loc += sreloc->reloc_count++ *
1149                       sizeof (Elf32_External_Rela);
1150
1151                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1152                     bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
1153                   }
1154               }
1155             /* Static GD.  */
1156             else if (howto->type == R_OR1K_TLS_GD_HI16
1157                      || howto->type == R_OR1K_TLS_GD_LO16)
1158               {
1159                 bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
1160                 bfd_put_32 (output_bfd, tpoff (info, relocation),
1161                     sgot->contents + gotoff + 4);
1162               }
1163             /* Shared IE.  */
1164             else if (dynamic)
1165               {
1166                 /* Add TPOFF GOT and rela entries.  */
1167                 rela.r_offset = sgot->output_section->vma +
1168                   sgot->output_offset + gotoff;
1169                 if (h != NULL && h->dynindx != -1)
1170                   {
1171                     rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_TLS_TPOFF);
1172                     rela.r_addend = 0;
1173                   }
1174                 else
1175                   {
1176                     rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
1177                     rela.r_addend = tpoff (info, relocation);
1178                   }
1179
1180                 loc = sreloc->contents;
1181                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1182
1183                 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1184                 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
1185               }
1186             /* Static IE.  */
1187             else
1188               {
1189                 bfd_put_32 (output_bfd, tpoff (info, relocation),
1190                             sgot->contents + gotoff);
1191               }
1192             relocation = sgot->output_offset + gotoff;
1193             break;
1194           }
1195         case R_OR1K_TLS_LE_HI16:
1196         case R_OR1K_TLS_LE_LO16:
1197
1198           /* Relocation is offset from TP.  */
1199           relocation = tpoff (info, relocation);
1200           break;
1201
1202         case R_OR1K_TLS_DTPMOD:
1203         case R_OR1K_TLS_DTPOFF:
1204         case R_OR1K_TLS_TPOFF:
1205           /* These are resolved dynamically on load and shouldn't
1206              be used as linker input.  */
1207           BFD_FAIL ();
1208           _bfd_error_handler
1209             (_("%B: will not resolve runtime TLS relocation"),
1210              input_bfd);
1211           bfd_set_error (bfd_error_bad_value);
1212           return FALSE;
1213
1214         default:
1215           break;
1216         }
1217       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
1218                                     rel->r_offset, relocation, rel->r_addend);
1219
1220       if (r != bfd_reloc_ok)
1221         {
1222           const char *msg = NULL;
1223
1224           switch (r)
1225             {
1226             case bfd_reloc_overflow:
1227               (*info->callbacks->reloc_overflow)
1228                 (info, (h ? &h->root : NULL), name, howto->name,
1229                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1230               break;
1231
1232             case bfd_reloc_undefined:
1233               (*info->callbacks->undefined_symbol)
1234                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1235               break;
1236
1237             case bfd_reloc_outofrange:
1238               msg = _("internal error: out of range error");
1239               break;
1240
1241             case bfd_reloc_notsupported:
1242               msg = _("internal error: unsupported relocation error");
1243               break;
1244
1245             case bfd_reloc_dangerous:
1246               msg = _("internal error: dangerous relocation");
1247               break;
1248
1249             default:
1250               msg = _("internal error: unknown error");
1251               break;
1252             }
1253
1254           if (msg)
1255             (*info->callbacks->warning) (info, msg, name, input_bfd,
1256                                          input_section, rel->r_offset);
1257         }
1258     }
1259
1260   return TRUE;
1261 }
1262
1263 /* Return the section that should be marked against GC for a given
1264    relocation.  */
1265
1266 static asection *
1267 or1k_elf_gc_mark_hook (asection *sec,
1268                        struct bfd_link_info *info,
1269                        Elf_Internal_Rela *rel,
1270                        struct elf_link_hash_entry *h,
1271                        Elf_Internal_Sym *sym)
1272 {
1273   if (h != NULL)
1274     switch (ELF32_R_TYPE (rel->r_info))
1275       {
1276       case R_OR1K_GNU_VTINHERIT:
1277       case R_OR1K_GNU_VTENTRY:
1278         return NULL;
1279       }
1280
1281   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1282 }
1283
1284 /* Look through the relocs for a section during the first phase.  */
1285
1286 static bfd_boolean
1287 or1k_elf_check_relocs (bfd *abfd,
1288                        struct bfd_link_info *info,
1289                        asection *sec,
1290                        const Elf_Internal_Rela *relocs)
1291 {
1292   Elf_Internal_Shdr *symtab_hdr;
1293   struct elf_link_hash_entry **sym_hashes;
1294   const Elf_Internal_Rela *rel;
1295
1296   const Elf_Internal_Rela *rel_end;
1297   struct elf_or1k_link_hash_table *htab;
1298   bfd *dynobj;
1299   asection *sreloc = NULL;
1300
1301   if (bfd_link_relocatable (info))
1302     return TRUE;
1303
1304   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1305   sym_hashes = elf_sym_hashes (abfd);
1306
1307   htab = or1k_elf_hash_table (info);
1308   if (htab == NULL)
1309     return FALSE;
1310
1311   dynobj = htab->root.dynobj;
1312
1313   rel_end = relocs + sec->reloc_count;
1314   for (rel = relocs; rel < rel_end; rel++)
1315     {
1316       struct elf_link_hash_entry *h;
1317       unsigned long r_symndx;
1318       unsigned char tls_type;
1319
1320       r_symndx = ELF32_R_SYM (rel->r_info);
1321       if (r_symndx < symtab_hdr->sh_info)
1322         h = NULL;
1323       else
1324         {
1325           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1326           while (h->root.type == bfd_link_hash_indirect
1327                  || h->root.type == bfd_link_hash_warning)
1328             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1329         }
1330
1331       switch (ELF32_R_TYPE (rel->r_info))
1332         {
1333         case R_OR1K_TLS_GD_HI16:
1334         case R_OR1K_TLS_GD_LO16:
1335           tls_type = TLS_GD;
1336           break;
1337         case R_OR1K_TLS_LDM_HI16:
1338         case R_OR1K_TLS_LDM_LO16:
1339         case R_OR1K_TLS_LDO_HI16:
1340         case R_OR1K_TLS_LDO_LO16:
1341           tls_type = TLS_LD;
1342           break;
1343         case R_OR1K_TLS_IE_HI16:
1344         case R_OR1K_TLS_IE_LO16:
1345           tls_type = TLS_IE;
1346           break;
1347         case R_OR1K_TLS_LE_HI16:
1348         case R_OR1K_TLS_LE_LO16:
1349           tls_type = TLS_LE;
1350           break;
1351         default:
1352           tls_type = TLS_NONE;
1353         }
1354
1355       /* Record TLS type.  */
1356       if (h != NULL)
1357           ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
1358       else
1359         {
1360           unsigned char *local_tls_type;
1361
1362           /* This is a TLS type record for a local symbol.  */
1363           local_tls_type = (unsigned char *) elf_or1k_local_tls_type (abfd);
1364           if (local_tls_type == NULL)
1365             {
1366               bfd_size_type size;
1367
1368               size = symtab_hdr->sh_info;
1369               local_tls_type = bfd_zalloc (abfd, size);
1370               if (local_tls_type == NULL)
1371                 return FALSE;
1372               elf_or1k_local_tls_type (abfd) = local_tls_type;
1373             }
1374           local_tls_type[r_symndx] = tls_type;
1375         }
1376
1377       switch (ELF32_R_TYPE (rel->r_info))
1378         {
1379           /* This relocation describes the C++ object vtable hierarchy.
1380              Reconstruct it for later use during GC.  */
1381         case R_OR1K_GNU_VTINHERIT:
1382           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1383             return FALSE;
1384           break;
1385
1386           /* This relocation describes which C++ vtable entries are actually
1387              used.  Record for later use during GC.  */
1388         case R_OR1K_GNU_VTENTRY:
1389           BFD_ASSERT (h != NULL);
1390           if (h != NULL
1391               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1392             return FALSE;
1393           break;
1394
1395           /* This relocation requires .plt entry.  */
1396         case R_OR1K_PLT26:
1397           if (h != NULL)
1398             {
1399               h->needs_plt = 1;
1400               h->plt.refcount += 1;
1401             }
1402           break;
1403
1404         case R_OR1K_GOT16:
1405         case R_OR1K_GOTOFF_HI16:
1406         case R_OR1K_GOTOFF_LO16:
1407         case R_OR1K_TLS_GD_HI16:
1408         case R_OR1K_TLS_GD_LO16:
1409         case R_OR1K_TLS_IE_HI16:
1410         case R_OR1K_TLS_IE_LO16:
1411           if (htab->root.sgot == NULL)
1412             {
1413               if (dynobj == NULL)
1414                 htab->root.dynobj = dynobj = abfd;
1415               if (!_bfd_elf_create_got_section (dynobj, info))
1416                 return FALSE;
1417             }
1418
1419           if (ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_HI16 &&
1420               ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_LO16)
1421             {
1422               if (h != NULL)
1423                 h->got.refcount += 1;
1424               else
1425                 {
1426                   bfd_signed_vma *local_got_refcounts;
1427
1428                   /* This is a global offset table entry for a local symbol.  */
1429                   local_got_refcounts = elf_local_got_refcounts (abfd);
1430                   if (local_got_refcounts == NULL)
1431                     {
1432                       bfd_size_type size;
1433
1434                       size = symtab_hdr->sh_info;
1435                       size *= sizeof (bfd_signed_vma);
1436                       local_got_refcounts = bfd_zalloc (abfd, size);
1437                       if (local_got_refcounts == NULL)
1438                         return FALSE;
1439                       elf_local_got_refcounts (abfd) = local_got_refcounts;
1440                     }
1441                   local_got_refcounts[r_symndx] += 1;
1442                 }
1443             }
1444           break;
1445
1446         case R_OR1K_INSN_REL_26:
1447         case R_OR1K_HI_16_IN_INSN:
1448         case R_OR1K_LO_16_IN_INSN:
1449         case R_OR1K_32:
1450           /* R_OR1K_16? */
1451           {
1452             if (h != NULL && !bfd_link_pic (info))
1453               {
1454                 /* We may need a copy reloc.  */
1455                 h->non_got_ref = 1;
1456
1457                 /* We may also need a .plt entry.  */
1458                 h->plt.refcount += 1;
1459                 if (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26)
1460                   h->pointer_equality_needed = 1;
1461               }
1462
1463             /* If we are creating a shared library, and this is a reloc
1464                against a global symbol, or a non PC relative reloc
1465                against a local symbol, then we need to copy the reloc
1466                into the shared library.  However, if we are linking with
1467                -Bsymbolic, we do not need to copy a reloc against a
1468                global symbol which is defined in an object we are
1469                including in the link (i.e., DEF_REGULAR is set).  At
1470                this point we have not seen all the input files, so it is
1471                possible that DEF_REGULAR is not set now but will be set
1472                later (it is never cleared).  In case of a weak definition,
1473                DEF_REGULAR may be cleared later by a strong definition in
1474                a shared library.  We account for that possibility below by
1475                storing information in the relocs_copied field of the hash
1476                table entry.  A similar situation occurs when creating
1477                shared libraries and symbol visibility changes render the
1478                symbol local.
1479
1480                If on the other hand, we are creating an executable, we
1481                may need to keep relocations for symbols satisfied by a
1482                dynamic library if we manage to avoid copy relocs for the
1483                symbol.  */
1484
1485             if ((bfd_link_pic (info)
1486                  && (sec->flags & SEC_ALLOC) != 0
1487                  && (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26
1488                      || (h != NULL
1489                          && (!SYMBOLIC_BIND (info, h)
1490                              || h->root.type == bfd_link_hash_defweak
1491                              || !h->def_regular))))
1492                 || (!bfd_link_pic (info)
1493                     && (sec->flags & SEC_ALLOC) != 0
1494                     && h != NULL
1495                     && (h->root.type == bfd_link_hash_defweak
1496                         || !h->def_regular)))
1497               {
1498                 struct elf_dyn_relocs *p;
1499                 struct elf_dyn_relocs **head;
1500
1501                 /* When creating a shared object, we must copy these
1502                    relocs into the output file.  We create a reloc
1503                    section in dynobj and make room for the reloc.  */
1504                 if (sreloc == NULL)
1505                   {
1506                     const char *name;
1507                     unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1508                     unsigned int shnam = _bfd_elf_single_rel_hdr (sec)->sh_name;
1509
1510                     name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1511                     if (name == NULL)
1512                       return FALSE;
1513
1514                     if (strncmp (name, ".rela", 5) != 0
1515                         || strcmp (bfd_get_section_name (abfd, sec),
1516                                    name + 5) != 0)
1517                       {
1518                         _bfd_error_handler
1519                           /* xgettext:c-format */
1520                           (_("%B: bad relocation section name `%s\'"),
1521                            abfd, name);
1522                       }
1523
1524                     if (htab->root.dynobj == NULL)
1525                       htab->root.dynobj = abfd;
1526                     dynobj = htab->root.dynobj;
1527
1528                     sreloc = bfd_get_section_by_name (dynobj, name);
1529                     if (sreloc == NULL)
1530                       {
1531                         sreloc = _bfd_elf_make_dynamic_reloc_section
1532                           (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
1533
1534                         if (sreloc == NULL)
1535                           return FALSE;
1536                       }
1537                     elf_section_data (sec)->sreloc = sreloc;
1538                   }
1539
1540                 /* If this is a global symbol, we count the number of
1541                    relocations we need for this symbol.  */
1542                 if (h != NULL)
1543                   head = &((struct elf_or1k_link_hash_entry *) h)->dyn_relocs;
1544                 else
1545                   {
1546                     /* Track dynamic relocs needed for local syms too.
1547                        We really need local syms available to do this
1548                        easily.  Oh well.  */
1549
1550                     asection *s;
1551                     Elf_Internal_Sym *isym;
1552                     void *vpp;
1553
1554                     isym = bfd_sym_from_r_symndx (&htab->sym_sec,
1555                                                   abfd, r_symndx);
1556                     if (isym == NULL)
1557                       return FALSE;
1558
1559                     s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1560                     if (s == NULL)
1561                       return FALSE;
1562
1563                     vpp = &elf_section_data (s)->local_dynrel;
1564                     head = (struct elf_dyn_relocs **) vpp;
1565                   }
1566
1567                 p = *head;
1568                 if (p == NULL || p->sec != sec)
1569                   {
1570                     bfd_size_type amt = sizeof *p;
1571                     p = ((struct elf_dyn_relocs *)
1572                          bfd_alloc (htab->root.dynobj, amt));
1573                     if (p == NULL)
1574                       return FALSE;
1575                     p->next = *head;
1576                     *head = p;
1577                     p->sec = sec;
1578                     p->count = 0;
1579                     p->pc_count = 0;
1580                   }
1581
1582                 p->count += 1;
1583                 if (ELF32_R_TYPE (rel->r_info) == R_OR1K_INSN_REL_26)
1584                   p->pc_count += 1;
1585               }
1586           }
1587           break;
1588         }
1589     }
1590
1591   return TRUE;
1592 }
1593
1594 /* Finish up the dynamic sections.  */
1595
1596 static bfd_boolean
1597 or1k_elf_finish_dynamic_sections (bfd *output_bfd,
1598                                   struct bfd_link_info *info)
1599 {
1600   bfd *dynobj;
1601   asection *sdyn, *sgot;
1602   struct elf_or1k_link_hash_table *htab;
1603
1604   htab = or1k_elf_hash_table (info);
1605   if (htab == NULL)
1606     return FALSE;
1607
1608   dynobj = htab->root.dynobj;
1609
1610   sgot = htab->root.sgotplt;
1611   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1612
1613   if (htab->root.dynamic_sections_created)
1614     {
1615       asection *splt;
1616       Elf32_External_Dyn *dyncon, *dynconend;
1617
1618       BFD_ASSERT (sgot != NULL && sdyn != NULL);
1619
1620       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1621       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1622
1623       for (; dyncon < dynconend; dyncon++)
1624         {
1625           Elf_Internal_Dyn dyn;
1626           asection *s;
1627
1628           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1629
1630           switch (dyn.d_tag)
1631             {
1632             default:
1633               continue;
1634
1635             case DT_PLTGOT:
1636               s = htab->root.sgotplt;
1637               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1638               break;
1639
1640             case DT_JMPREL:
1641               s = htab->root.srelplt;
1642               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1643               break;
1644
1645             case DT_PLTRELSZ:
1646               s = htab->root.srelplt;
1647               dyn.d_un.d_val = s->size;
1648               break;
1649             }
1650           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1651         }
1652
1653
1654       /* Fill in the first entry in the procedure linkage table.  */
1655       splt = htab->root.splt;
1656       if (splt && splt->size > 0)
1657         {
1658           if (bfd_link_pic (info))
1659             {
1660               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0,
1661                           splt->contents);
1662               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1,
1663                           splt->contents + 4);
1664               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2,
1665                           splt->contents + 8);
1666               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3,
1667                           splt->contents + 12);
1668               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4,
1669                           splt->contents + 16);
1670             }
1671           else
1672             {
1673               unsigned long addr;
1674               /* addr = .got + 4 */
1675               addr = sgot->output_section->vma + sgot->output_offset + 4;
1676               bfd_put_32 (output_bfd,
1677                           PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
1678                           splt->contents);
1679               bfd_put_32 (output_bfd,
1680                           PLT0_ENTRY_WORD1 | (addr & 0xffff),
1681                           splt->contents + 4);
1682               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
1683               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
1684               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
1685             }
1686
1687           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
1688         }
1689     }
1690
1691   /* Set the first entry in the global offset table to the address of
1692      the dynamic section.  */
1693   if (sgot && sgot->size > 0)
1694     {
1695       if (sdyn == NULL)
1696         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1697       else
1698         bfd_put_32 (output_bfd,
1699                     sdyn->output_section->vma + sdyn->output_offset,
1700                     sgot->contents);
1701       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1702     }
1703
1704   if (htab->root.sgot && htab->root.sgot->size > 0)
1705     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize = 4;
1706
1707   return TRUE;
1708 }
1709
1710 /* Finish up dynamic symbol handling.  We set the contents of various
1711    dynamic sections here.  */
1712
1713 static bfd_boolean
1714 or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
1715                                 struct bfd_link_info *info,
1716                                 struct elf_link_hash_entry *h,
1717                                 Elf_Internal_Sym *sym)
1718 {
1719   struct elf_or1k_link_hash_table *htab;
1720   bfd_byte *loc;
1721
1722   htab = or1k_elf_hash_table (info);
1723   if (htab == NULL)
1724     return FALSE;
1725
1726   if (h->plt.offset != (bfd_vma) -1)
1727     {
1728       asection *splt;
1729       asection *sgot;
1730       asection *srela;
1731
1732       bfd_vma plt_index;
1733       bfd_vma got_offset;
1734       bfd_vma got_addr;
1735       Elf_Internal_Rela rela;
1736
1737       /* This symbol has an entry in the procedure linkage table.  Set
1738          it up.  */
1739       BFD_ASSERT (h->dynindx != -1);
1740
1741       splt = htab->root.splt;
1742       sgot = htab->root.sgotplt;
1743       srela = htab->root.srelplt;
1744       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1745
1746       /* Get the index in the procedure linkage table which
1747          corresponds to this symbol.  This is the index of this symbol
1748          in all the symbols for which we are making plt entries.  The
1749          first entry in the procedure linkage table is reserved.  */
1750       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1751
1752       /* Get the offset into the .got table of the entry that
1753         corresponds to this function.  Each .got entry is 4 bytes.
1754         The first three are reserved.  */
1755       got_offset = (plt_index + 3) * 4;
1756       got_addr = got_offset;
1757
1758       /* Fill in the entry in the procedure linkage table.  */
1759       if (! bfd_link_pic (info))
1760         {
1761           got_addr += htab->root.sgotplt->output_section->vma
1762             + htab->root.sgotplt->output_offset;
1763           bfd_put_32 (output_bfd, PLT_ENTRY_WORD0 | ((got_addr >> 16) & 0xffff),
1764                       splt->contents + h->plt.offset);
1765           bfd_put_32 (output_bfd, PLT_ENTRY_WORD1 | (got_addr & 0xffff),
1766                       splt->contents + h->plt.offset + 4);
1767           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
1768                       splt->contents + h->plt.offset + 8);
1769           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
1770                       splt->contents + h->plt.offset + 12);
1771           bfd_put_32 (output_bfd, PLT_ENTRY_WORD4
1772                       | plt_index * sizeof (Elf32_External_Rela),
1773                       splt->contents + h->plt.offset + 16);
1774         }
1775       else
1776         {
1777           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD0 | (got_addr & 0xffff),
1778                       splt->contents + h->plt.offset);
1779           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD1
1780                       | plt_index * sizeof (Elf32_External_Rela),
1781                       splt->contents + h->plt.offset + 4);
1782           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
1783                       splt->contents + h->plt.offset + 8);
1784           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
1785                       splt->contents + h->plt.offset + 12);
1786           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
1787                       splt->contents + h->plt.offset + 16);
1788         }
1789
1790       /* Fill in the entry in the global offset table.  */
1791       bfd_put_32 (output_bfd,
1792                   (splt->output_section->vma
1793                    + splt->output_offset), /* Same offset.  */
1794                   sgot->contents + got_offset);
1795
1796       /* Fill in the entry in the .rela.plt section.  */
1797       rela.r_offset = (sgot->output_section->vma
1798                        + sgot->output_offset
1799                        + got_offset);
1800       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_JMP_SLOT);
1801       rela.r_addend = 0;
1802       loc = srela->contents;
1803       loc += plt_index * sizeof (Elf32_External_Rela);
1804       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1805
1806       if (!h->def_regular)
1807         {
1808           /* Mark the symbol as undefined, rather than as defined in
1809              the .plt section.  Leave the value alone.  */
1810           sym->st_shndx = SHN_UNDEF;
1811         }
1812
1813     }
1814
1815   if (h->got.offset != (bfd_vma) -1
1816       && (h->got.offset & 2) == 0) /* Homemade TLS check.  */
1817     {
1818       asection *sgot;
1819       asection *srela;
1820       Elf_Internal_Rela rela;
1821
1822       /* This symbol has an entry in the global offset table.  Set it
1823          up.  */
1824       sgot = htab->root.sgot;
1825       srela = htab->root.srelgot;
1826       BFD_ASSERT (sgot != NULL && srela != NULL);
1827
1828       rela.r_offset = (sgot->output_section->vma
1829                        + sgot->output_offset
1830                        + (h->got.offset &~ 1));
1831
1832       /* If this is a -Bsymbolic link, and the symbol is defined
1833          locally, we just want to emit a RELATIVE reloc.  Likewise if
1834          the symbol was forced to be local because of a version file.
1835          The entry in the global offset table will already have been
1836          initialized in the relocate_section function.  */
1837       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
1838         {
1839           rela.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1840           rela.r_addend = (h->root.u.def.value
1841                            + h->root.u.def.section->output_section->vma
1842                            + h->root.u.def.section->output_offset);
1843         }
1844       else
1845         {
1846           BFD_ASSERT ((h->got.offset & 1) == 0);
1847           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1848           rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_GLOB_DAT);
1849           rela.r_addend = 0;
1850         }
1851
1852       loc = srela->contents;
1853       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
1854       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1855       ++srela->reloc_count;
1856     }
1857
1858   if (h->needs_copy)
1859     {
1860       asection *s;
1861       Elf_Internal_Rela rela;
1862
1863       /* This symbols needs a copy reloc.  Set it up.  */
1864       BFD_ASSERT (h->dynindx != -1
1865                   && (h->root.type == bfd_link_hash_defined
1866                       || h->root.type == bfd_link_hash_defweak));
1867
1868       rela.r_offset = (h->root.u.def.value
1869                        + h->root.u.def.section->output_section->vma
1870                        + h->root.u.def.section->output_offset);
1871       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_COPY);
1872       rela.r_addend = 0;
1873       if (h->root.u.def.section == htab->root.sdynrelro)
1874         s = htab->root.sreldynrelro;
1875       else
1876         s = htab->root.srelbss;
1877       loc = s->contents + s->reloc_count * sizeof (Elf32_External_Rela);
1878       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1879       ++s->reloc_count;
1880     }
1881
1882   /* Mark some specially defined symbols as absolute.  */
1883   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1884       || h == htab->root.hgot)
1885     sym->st_shndx = SHN_ABS;
1886
1887   return TRUE;
1888 }
1889
1890 static enum elf_reloc_type_class
1891 or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
1892                            const asection *rel_sec ATTRIBUTE_UNUSED,
1893                            const Elf_Internal_Rela *rela)
1894 {
1895   switch ((int) ELF32_R_TYPE (rela->r_info))
1896     {
1897     case R_OR1K_RELATIVE:  return reloc_class_relative;
1898     case R_OR1K_JMP_SLOT:  return reloc_class_plt;
1899     case R_OR1K_COPY:      return reloc_class_copy;
1900     default:               return reloc_class_normal;
1901     }
1902 }
1903
1904 /* Find dynamic relocs for H that apply to read-only sections.  */
1905
1906 static asection *
1907 readonly_dynrelocs (struct elf_link_hash_entry *h)
1908 {
1909   struct elf_dyn_relocs *p;
1910   struct elf_or1k_link_hash_entry *eh = (struct elf_or1k_link_hash_entry *) h;
1911
1912   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1913     {
1914       asection *s = p->sec->output_section;
1915
1916       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1917         return p->sec;
1918     }
1919   return NULL;
1920 }
1921
1922 /* Adjust a symbol defined by a dynamic object and referenced by a
1923    regular object.  The current definition is in some section of the
1924    dynamic object, but we're not including those sections.  We have to
1925    change the definition to something the rest of the link can
1926    understand.  */
1927
1928 static bfd_boolean
1929 or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1930                                 struct elf_link_hash_entry *h)
1931 {
1932   struct elf_or1k_link_hash_table *htab;
1933   bfd *dynobj;
1934   asection *s, *srel;
1935
1936   dynobj = elf_hash_table (info)->dynobj;
1937
1938   /* Make sure we know what is going on here.  */
1939   BFD_ASSERT (dynobj != NULL
1940               && (h->needs_plt
1941                   || h->is_weakalias
1942                   || (h->def_dynamic
1943                       && h->ref_regular
1944                       && !h->def_regular)));
1945
1946   /* If this is a function, put it in the procedure linkage table.  We
1947      will fill in the contents of the procedure linkage table later,
1948      when we know the address of the .got section.  */
1949   if (h->type == STT_FUNC
1950       || h->needs_plt)
1951     {
1952       if (! bfd_link_pic (info)
1953           && !h->def_dynamic
1954           && !h->ref_dynamic
1955           && h->root.type != bfd_link_hash_undefweak
1956           && h->root.type != bfd_link_hash_undefined)
1957         {
1958           /* This case can occur if we saw a PLT reloc in an input
1959              file, but the symbol was never referred to by a dynamic
1960              object.  In such a case, we don't actually need to build
1961              a procedure linkage table, and we can just do a PCREL
1962              reloc instead.  */
1963           h->plt.offset = (bfd_vma) -1;
1964           h->needs_plt = 0;
1965         }
1966
1967       return TRUE;
1968     }
1969   else
1970     h->plt.offset = (bfd_vma) -1;
1971
1972   /* If this is a weak symbol, and there is a real definition, the
1973      processor independent code will have arranged for us to see the
1974      real definition first, and we can just use the same value.  */
1975   if (h->is_weakalias)
1976     {
1977       struct elf_link_hash_entry *def = weakdef (h);
1978       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1979       h->root.u.def.section = def->root.u.def.section;
1980       h->root.u.def.value = def->root.u.def.value;
1981       return TRUE;
1982     }
1983
1984   /* This is a reference to a symbol defined by a dynamic object which
1985      is not a function.  */
1986
1987   /* If we are creating a shared library, we must presume that the
1988      only references to the symbol are via the global offset table.
1989      For such cases we need not do anything here; the relocations will
1990      be handled correctly by relocate_section.  */
1991   if (bfd_link_pic (info))
1992     return TRUE;
1993
1994   /* If there are no references to this symbol that do not use the
1995      GOT, we don't need to generate a copy reloc.  */
1996   if (!h->non_got_ref)
1997     return TRUE;
1998
1999   /* If -z nocopyreloc was given, we won't generate them either.  */
2000   if (info->nocopyreloc)
2001     {
2002       h->non_got_ref = 0;
2003       return TRUE;
2004     }
2005
2006   /* If we don't find any dynamic relocs in read-only sections, then
2007      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2008   if (!readonly_dynrelocs (h))
2009     {
2010       h->non_got_ref = 0;
2011       return TRUE;
2012     }
2013
2014   /* We must allocate the symbol in our .dynbss section, which will
2015      become part of the .bss section of the executable.  There will be
2016      an entry for this symbol in the .dynsym section.  The dynamic
2017      object will contain position independent code, so all references
2018      from the dynamic object to this symbol will go through the global
2019      offset table.  The dynamic linker will use the .dynsym entry to
2020      determine the address it must put in the global offset table, so
2021      both the dynamic object and the regular object will refer to the
2022      same memory location for the variable.  */
2023
2024   htab = or1k_elf_hash_table (info);
2025   if (htab == NULL)
2026     return FALSE;
2027
2028   /* We must generate a R_OR1K_COPY reloc to tell the dynamic linker
2029      to copy the initial value out of the dynamic object and into the
2030      runtime process image.  We need to remember the offset into the
2031      .rela.bss section we are going to use.  */
2032   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2033     {
2034       s = htab->root.sdynrelro;
2035       srel = htab->root.sreldynrelro;
2036     }
2037   else
2038     {
2039       s = htab->root.sdynbss;
2040       srel = htab->root.srelbss;
2041     }
2042   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2043     {
2044       srel->size += sizeof (Elf32_External_Rela);
2045       h->needs_copy = 1;
2046     }
2047
2048   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2049 }
2050
2051 /* Allocate space in .plt, .got and associated reloc sections for
2052    dynamic relocs.  */
2053
2054 static bfd_boolean
2055 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2056 {
2057   struct bfd_link_info *info;
2058   struct elf_or1k_link_hash_table *htab;
2059   struct elf_or1k_link_hash_entry *eh;
2060   struct elf_dyn_relocs *p;
2061
2062   if (h->root.type == bfd_link_hash_indirect)
2063     return TRUE;
2064
2065   info = (struct bfd_link_info *) inf;
2066   htab = or1k_elf_hash_table (info);
2067   if (htab == NULL)
2068     return FALSE;
2069
2070   eh = (struct elf_or1k_link_hash_entry *) h;
2071
2072   if (htab->root.dynamic_sections_created
2073       && h->plt.refcount > 0)
2074     {
2075       /* Make sure this symbol is output as a dynamic symbol.
2076          Undefined weak syms won't yet be marked as dynamic.  */
2077       if (h->dynindx == -1
2078           && !h->forced_local)
2079         {
2080           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2081             return FALSE;
2082         }
2083
2084       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
2085         {
2086           asection *s = htab->root.splt;
2087
2088           /* If this is the first .plt entry, make room for the special
2089              first entry.  */
2090           if (s->size == 0)
2091             s->size = PLT_ENTRY_SIZE;
2092
2093           h->plt.offset = s->size;
2094
2095           /* If this symbol is not defined in a regular file, and we are
2096              not generating a shared library, then set the symbol to this
2097              location in the .plt.  This is required to make function
2098              pointers compare as equal between the normal executable and
2099              the shared library.  */
2100           if (! bfd_link_pic (info)
2101               && !h->def_regular)
2102             {
2103               h->root.u.def.section = s;
2104               h->root.u.def.value = h->plt.offset;
2105             }
2106
2107           /* Make room for this entry.  */
2108           s->size += PLT_ENTRY_SIZE;
2109
2110           /* We also need to make an entry in the .got.plt section, which
2111              will be placed in the .got section by the linker script.  */
2112           htab->root.sgotplt->size += 4;
2113
2114           /* We also need to make an entry in the .rel.plt section.  */
2115           htab->root.srelplt->size += sizeof (Elf32_External_Rela);
2116         }
2117       else
2118         {
2119           h->plt.offset = (bfd_vma) -1;
2120           h->needs_plt = 0;
2121         }
2122     }
2123   else
2124     {
2125       h->plt.offset = (bfd_vma) -1;
2126       h->needs_plt = 0;
2127     }
2128
2129   if (h->got.refcount > 0)
2130     {
2131       asection *s;
2132       bfd_boolean dyn;
2133       unsigned char tls_type;
2134
2135       /* Make sure this symbol is output as a dynamic symbol.
2136          Undefined weak syms won't yet be marked as dynamic.  */
2137       if (h->dynindx == -1
2138           && !h->forced_local)
2139         {
2140           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2141             return FALSE;
2142         }
2143
2144       s = htab->root.sgot;
2145
2146       h->got.offset = s->size;
2147
2148       tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
2149
2150       /* TLS GD requires two GOT and two relocs.  */
2151       if (tls_type == TLS_GD)
2152         s->size += 8;
2153       else
2154         s->size += 4;
2155       dyn = htab->root.dynamic_sections_created;
2156       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
2157         {
2158           if (tls_type == TLS_GD)
2159             htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2160           else
2161             htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2162         }
2163     }
2164   else
2165     h->got.offset = (bfd_vma) -1;
2166
2167   if (eh->dyn_relocs == NULL)
2168     return TRUE;
2169
2170   /* In the shared -Bsymbolic case, discard space allocated for
2171      dynamic pc-relative relocs against symbols which turn out to be
2172      defined in regular objects.  For the normal shared case, discard
2173      space for pc-relative relocs that have become local due to symbol
2174      visibility changes.  */
2175
2176   if (bfd_link_pic (info))
2177     {
2178       if (SYMBOL_CALLS_LOCAL (info, h))
2179         {
2180           struct elf_dyn_relocs **pp;
2181
2182           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
2183             {
2184               p->count -= p->pc_count;
2185               p->pc_count = 0;
2186               if (p->count == 0)
2187                 *pp = p->next;
2188               else
2189                 pp = &p->next;
2190             }
2191         }
2192
2193       /* Also discard relocs on undefined weak syms with non-default
2194          visibility.  */
2195       if (eh->dyn_relocs != NULL
2196           && h->root.type == bfd_link_hash_undefweak)
2197         {
2198           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2199             eh->dyn_relocs = NULL;
2200
2201           /* Make sure undefined weak symbols are output as a dynamic
2202              symbol in PIEs.  */
2203           else if (h->dynindx == -1
2204                    && !h->forced_local)
2205             {
2206               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2207                 return FALSE;
2208             }
2209         }
2210     }
2211   else
2212     {
2213       /* For the non-shared case, discard space for relocs against
2214          symbols which turn out to need copy relocs or are not
2215          dynamic.  */
2216
2217       if (!h->non_got_ref
2218           && ((h->def_dynamic
2219                && !h->def_regular)
2220               || (htab->root.dynamic_sections_created
2221                   && (h->root.type == bfd_link_hash_undefweak
2222                       || h->root.type == bfd_link_hash_undefined))))
2223         {
2224           /* Make sure this symbol is output as a dynamic symbol.
2225              Undefined weak syms won't yet be marked as dynamic.  */
2226           if (h->dynindx == -1
2227               && !h->forced_local)
2228             {
2229               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2230                 return FALSE;
2231             }
2232
2233           /* If that succeeded, we know we'll be keeping all the
2234              relocs.  */
2235           if (h->dynindx != -1)
2236             goto keep;
2237         }
2238
2239       eh->dyn_relocs = NULL;
2240
2241     keep: ;
2242     }
2243
2244   /* Finally, allocate space.  */
2245   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2246     {
2247       asection *sreloc = elf_section_data (p->sec)->sreloc;
2248       sreloc->size += p->count * sizeof (Elf32_External_Rela);
2249     }
2250
2251   return TRUE;
2252 }
2253
2254 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2255    read-only sections.  */
2256
2257 static bfd_boolean
2258 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2259 {
2260   asection *sec;
2261
2262   if (h->root.type == bfd_link_hash_indirect)
2263     return TRUE;
2264
2265   sec = readonly_dynrelocs (h);
2266   if (sec != NULL)
2267     {
2268       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2269
2270       info->flags |= DF_TEXTREL;
2271       info->callbacks->minfo
2272         (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
2273          sec->owner, h->root.root.string, sec);
2274
2275       /* Not an error, just cut short the traversal.  */
2276       return FALSE;
2277     }
2278   return TRUE;
2279 }
2280
2281 /* Set the sizes of the dynamic sections.  */
2282
2283 static bfd_boolean
2284 or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2285                                 struct bfd_link_info *info)
2286 {
2287   struct elf_or1k_link_hash_table *htab;
2288   bfd *dynobj;
2289   asection *s;
2290   bfd_boolean relocs;
2291   bfd *ibfd;
2292
2293   htab = or1k_elf_hash_table (info);
2294   if (htab == NULL)
2295     return FALSE;
2296
2297   dynobj = htab->root.dynobj;
2298   BFD_ASSERT (dynobj != NULL);
2299
2300   if (htab->root.dynamic_sections_created)
2301     {
2302       /* Set the contents of the .interp section to the interpreter.  */
2303       if (bfd_link_executable (info) && !info->nointerp)
2304         {
2305           s = bfd_get_section_by_name (dynobj, ".interp");
2306           BFD_ASSERT (s != NULL);
2307           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2308           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2309         }
2310     }
2311
2312   /* Set up .got offsets for local syms, and space for local dynamic
2313      relocs.  */
2314   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2315     {
2316       bfd_signed_vma *local_got;
2317       bfd_signed_vma *end_local_got;
2318       bfd_size_type locsymcount;
2319       Elf_Internal_Shdr *symtab_hdr;
2320       unsigned char *local_tls_type;
2321       asection *srel;
2322
2323       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2324         continue;
2325
2326       for (s = ibfd->sections; s != NULL; s = s->next)
2327         {
2328           struct elf_dyn_relocs *p;
2329
2330           for (p = ((struct elf_dyn_relocs *)
2331                     elf_section_data (s)->local_dynrel);
2332                p != NULL;
2333                p = p->next)
2334             {
2335               if (! bfd_is_abs_section (p->sec)
2336                   && bfd_is_abs_section (p->sec->output_section))
2337                 {
2338                   /* Input section has been discarded, either because
2339                      it is a copy of a linkonce section or due to
2340                      linker script /DISCARD/, so we'll be discarding
2341                      the relocs too.  */
2342                 }
2343               else if (p->count != 0)
2344                 {
2345                   srel = elf_section_data (p->sec)->sreloc;
2346                   srel->size += p->count * sizeof (Elf32_External_Rela);
2347                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2348                     info->flags |= DF_TEXTREL;
2349                 }
2350             }
2351         }
2352
2353       local_got = elf_local_got_refcounts (ibfd);
2354       if (!local_got)
2355         continue;
2356
2357       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2358       locsymcount = symtab_hdr->sh_info;
2359       end_local_got = local_got + locsymcount;
2360       s = htab->root.sgot;
2361       srel = htab->root.srelgot;
2362       local_tls_type = (unsigned char *) elf_or1k_local_tls_type (ibfd);
2363       for (; local_got < end_local_got; ++local_got)
2364         {
2365           if (*local_got > 0)
2366             {
2367               *local_got = s->size;
2368
2369               /* TLS GD requires two GOT and two relocs.  */
2370               if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2371                 s->size += 8;
2372               else
2373                 s->size += 4;
2374               if (bfd_link_pic (info))
2375                 {
2376                   if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2377                     srel->size += 2 * sizeof (Elf32_External_Rela);
2378                   else
2379                     srel->size += sizeof (Elf32_External_Rela);
2380                 }
2381             }
2382           else
2383
2384             *local_got = (bfd_vma) -1;
2385
2386           if (local_tls_type)
2387             ++local_tls_type;
2388         }
2389     }
2390
2391   /* Allocate global sym .plt and .got entries, and space for global
2392      sym dynamic relocs.  */
2393   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
2394
2395   /* We now have determined the sizes of the various dynamic sections.
2396      Allocate memory for them.  */
2397   relocs = FALSE;
2398   for (s = dynobj->sections; s != NULL; s = s->next)
2399     {
2400       if ((s->flags & SEC_LINKER_CREATED) == 0)
2401         continue;
2402
2403       if (s == htab->root.splt
2404           || s == htab->root.sgot
2405           || s == htab->root.sgotplt
2406           || s == htab->root.sdynbss
2407           || s == htab->root.sdynrelro)
2408         {
2409           /* Strip this section if we don't need it; see the
2410              comment below.  */
2411         }
2412       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2413         {
2414           if (s->size != 0 && s != htab->root.srelplt)
2415             relocs = TRUE;
2416
2417           /* We use the reloc_count field as a counter if we need
2418              to copy relocs into the output file.  */
2419           s->reloc_count = 0;
2420         }
2421       else
2422         /* It's not one of our sections, so don't allocate space.  */
2423         continue;
2424
2425       if (s->size == 0)
2426         {
2427           /* If we don't need this section, strip it from the
2428              output file.  This is mostly to handle .rela.bss and
2429              .rela.plt.  We must create both sections in
2430              create_dynamic_sections, because they must be created
2431              before the linker maps input sections to output
2432              sections.  The linker does that before
2433              adjust_dynamic_symbol is called, and it is that
2434              function which decides whether anything needs to go
2435              into these sections.  */
2436           s->flags |= SEC_EXCLUDE;
2437           continue;
2438         }
2439
2440       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2441         continue;
2442
2443       /* Allocate memory for the section contents.  We use bfd_zalloc
2444          here in case unused entries are not reclaimed before the
2445          section's contents are written out.  This should not happen,
2446          but this way if it does, we get a R_OR1K_NONE reloc instead
2447          of garbage.  */
2448       s->contents = bfd_zalloc (dynobj, s->size);
2449
2450       if (s->contents == NULL)
2451         return FALSE;
2452     }
2453
2454   if (htab->root.dynamic_sections_created)
2455     {
2456       /* Add some entries to the .dynamic section.  We fill in the
2457          values later, in or1k_elf_finish_dynamic_sections, but we
2458          must add the entries now so that we get the correct size for
2459          the .dynamic section.  The DT_DEBUG entry is filled in by the
2460          dynamic linker and used by the debugger.  */
2461 #define add_dynamic_entry(TAG, VAL) \
2462   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2463
2464      if (bfd_link_executable (info))
2465        {
2466          if (! add_dynamic_entry (DT_DEBUG, 0))
2467            return FALSE;
2468        }
2469
2470      if (htab->root.splt->size != 0)
2471        {
2472          if (! add_dynamic_entry (DT_PLTGOT, 0)
2473              || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2474              || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2475              || ! add_dynamic_entry (DT_JMPREL, 0))
2476            return FALSE;
2477         }
2478
2479      if (relocs)
2480        {
2481          if (! add_dynamic_entry (DT_RELA, 0)
2482              || ! add_dynamic_entry (DT_RELASZ, 0)
2483              || ! add_dynamic_entry (DT_RELAENT,
2484                                      sizeof (Elf32_External_Rela)))
2485            return FALSE;
2486
2487          /* If any dynamic relocs apply to a read-only section,
2488             then we need a DT_TEXTREL entry.  */
2489          if ((info->flags & DF_TEXTREL) == 0)
2490            elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
2491
2492          if ((info->flags & DF_TEXTREL) != 0)
2493            {
2494              if (! add_dynamic_entry (DT_TEXTREL, 0))
2495                return FALSE;
2496            }
2497        }
2498     }
2499
2500 #undef add_dynamic_entry
2501   return TRUE;
2502 }
2503
2504 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2505
2506 static void
2507 or1k_elf_copy_indirect_symbol (struct bfd_link_info *info,
2508                                struct elf_link_hash_entry *dir,
2509                                struct elf_link_hash_entry *ind)
2510 {
2511   struct elf_or1k_link_hash_entry * edir;
2512   struct elf_or1k_link_hash_entry * eind;
2513
2514   edir = (struct elf_or1k_link_hash_entry *) dir;
2515   eind = (struct elf_or1k_link_hash_entry *) ind;
2516
2517   if (eind->dyn_relocs != NULL)
2518     {
2519       if (edir->dyn_relocs != NULL)
2520         {
2521           struct elf_dyn_relocs **pp;
2522           struct elf_dyn_relocs *p;
2523
2524           /* Add reloc counts against the indirect sym to the direct sym
2525              list.  Merge any entries against the same section.  */
2526           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2527             {
2528               struct elf_dyn_relocs *q;
2529
2530               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2531                 if (q->sec == p->sec)
2532                   {
2533                     q->pc_count += p->pc_count;
2534                     q->count += p->count;
2535                     *pp = p->next;
2536                     break;
2537                   }
2538               if (q == NULL)
2539                 pp = &p->next;
2540             }
2541           *pp = edir->dyn_relocs;
2542         }
2543
2544       edir->dyn_relocs = eind->dyn_relocs;
2545       eind->dyn_relocs = NULL;
2546     }
2547
2548   if (ind->root.type == bfd_link_hash_indirect)
2549     {
2550       if (dir->got.refcount <= 0)
2551         {
2552           edir->tls_type = eind->tls_type;
2553           eind->tls_type = TLS_UNKNOWN;
2554         }
2555     }
2556
2557   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2558 }
2559
2560 /* Set the right machine number.  */
2561
2562 static bfd_boolean
2563 or1k_elf_object_p (bfd *abfd)
2564 {
2565   unsigned long mach = bfd_mach_or1k;
2566
2567   if (elf_elfheader (abfd)->e_flags & EF_OR1K_NODELAY)
2568     mach = bfd_mach_or1knd;
2569
2570   return bfd_default_set_arch_mach (abfd, bfd_arch_or1k, mach);
2571 }
2572
2573 /* Store the machine number in the flags field.  */
2574
2575 static void
2576 or1k_elf_final_write_processing (bfd *abfd,
2577                                  bfd_boolean linker ATTRIBUTE_UNUSED)
2578 {
2579   switch (bfd_get_mach (abfd))
2580     {
2581     default:
2582     case bfd_mach_or1k:
2583       break;
2584     case bfd_mach_or1knd:
2585       elf_elfheader (abfd)->e_flags |= EF_OR1K_NODELAY;
2586       break;
2587     }
2588 }
2589
2590 static bfd_boolean
2591 or1k_elf_set_private_flags (bfd *abfd, flagword flags)
2592 {
2593   BFD_ASSERT (!elf_flags_init (abfd)
2594               || elf_elfheader (abfd)->e_flags == flags);
2595
2596   elf_elfheader (abfd)->e_flags = flags;
2597   elf_flags_init (abfd) = TRUE;
2598   return TRUE;
2599 }
2600
2601 /* Make sure all input files are consistent with respect to
2602    EF_OR1K_NODELAY flag setting.  */
2603
2604 static bfd_boolean
2605 elf32_or1k_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2606 {
2607   bfd *obfd = info->output_bfd;
2608   flagword out_flags;
2609   flagword in_flags;
2610
2611   in_flags  = elf_elfheader (ibfd)->e_flags;
2612   out_flags = elf_elfheader (obfd)->e_flags;
2613
2614   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2615       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2616     return TRUE;
2617
2618   if (!elf_flags_init (obfd))
2619     {
2620       elf_flags_init (obfd) = TRUE;
2621       elf_elfheader (obfd)->e_flags = in_flags;
2622
2623       return TRUE;
2624     }
2625
2626   if (in_flags == out_flags)
2627     return TRUE;
2628
2629   if ((in_flags & EF_OR1K_NODELAY) != (out_flags & EF_OR1K_NODELAY))
2630     {
2631       _bfd_error_handler
2632         (_("%B: EF_OR1K_NODELAY flag mismatch with previous modules"), ibfd);
2633
2634       bfd_set_error (bfd_error_bad_value);
2635       return FALSE;
2636     }
2637
2638   return TRUE;
2639
2640 }
2641
2642 #define ELF_ARCH                        bfd_arch_or1k
2643 #define ELF_MACHINE_CODE                EM_OR1K
2644 #define ELF_TARGET_ID                   OR1K_ELF_DATA
2645 #define ELF_MAXPAGESIZE                 0x2000
2646
2647 #define TARGET_BIG_SYM                  or1k_elf32_vec
2648 #define TARGET_BIG_NAME                 "elf32-or1k"
2649
2650 #define elf_info_to_howto_rel           NULL
2651 #define elf_info_to_howto               or1k_info_to_howto_rela
2652 #define elf_backend_relocate_section    or1k_elf_relocate_section
2653 #define elf_backend_gc_mark_hook        or1k_elf_gc_mark_hook
2654 #define elf_backend_check_relocs        or1k_elf_check_relocs
2655 #define elf_backend_reloc_type_class    or1k_elf_reloc_type_class
2656 #define elf_backend_can_gc_sections     1
2657 #define elf_backend_rela_normal         1
2658
2659 #define bfd_elf32_mkobject                   elf_or1k_mkobject
2660
2661 #define bfd_elf32_bfd_merge_private_bfd_data elf32_or1k_merge_private_bfd_data
2662 #define bfd_elf32_bfd_set_private_flags or1k_elf_set_private_flags
2663 #define bfd_elf32_bfd_reloc_type_lookup or1k_reloc_type_lookup
2664 #define bfd_elf32_bfd_reloc_name_lookup or1k_reloc_name_lookup
2665
2666 #define elf_backend_object_p                or1k_elf_object_p
2667 #define elf_backend_final_write_processing  or1k_elf_final_write_processing
2668 #define elf_backend_can_refcount                1
2669
2670 #define elf_backend_plt_readonly                1
2671 #define elf_backend_want_got_plt                1
2672 #define elf_backend_want_plt_sym                0
2673 #define elf_backend_got_header_size             12
2674 #define elf_backend_dtrel_excludes_plt          1
2675 #define elf_backend_want_dynrelro               1
2676
2677 #define bfd_elf32_bfd_link_hash_table_create    or1k_elf_link_hash_table_create
2678 #define elf_backend_copy_indirect_symbol        or1k_elf_copy_indirect_symbol
2679 #define elf_backend_create_dynamic_sections     _bfd_elf_create_dynamic_sections
2680 #define elf_backend_finish_dynamic_sections     or1k_elf_finish_dynamic_sections
2681 #define elf_backend_size_dynamic_sections       or1k_elf_size_dynamic_sections
2682 #define elf_backend_adjust_dynamic_symbol       or1k_elf_adjust_dynamic_symbol
2683 #define elf_backend_finish_dynamic_symbol       or1k_elf_finish_dynamic_symbol
2684
2685 #include "elf32-target.h"