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