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