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