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