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