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