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