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