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