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