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