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