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