ELF: Consolidate readonly_dynrelocs
[deliverable/binutils-gdb.git] / bfd / elf32-csky.c
CommitLineData
b8891f8d 1/* 32-bit ELF support for C-SKY.
b3adc24a 2 Copyright (C) 1998-2020 Free Software Foundation, Inc.
b8891f8d
AJ
3 Contributed by C-SKY Microsystems and Mentor Graphics.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22#include "sysdep.h"
23#include "bfd.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "elf/csky.h"
28#include "opcode/csky.h"
29#include <assert.h>
30#include "libiberty.h"
6e67e6b0 31#include "elf32-csky.h"
b8891f8d
AJ
32
33/* Data structures used for merging different arch variants.
34 V1 (510/610) and V2 (8xx) processors are incompatible, but
35 we can merge wthin each family. */
36
37enum merge_class
38{
39 CSKY_V1,
40 CSKY_V2
41};
42
43typedef struct csky_arch_for_merge
44{
45 const char *name;
46 const unsigned long arch_eflag;
47 /* The files can merge only if they are in same class. */
48 enum merge_class class;
49 /* When input files have different levels,
50 the target sets arch_eflag to the largest level file's arch_eflag. */
51 unsigned int class_level;
52 /* Control whether to print warning when merging with different arch. */
53 unsigned int do_warning;
54} csky_arch_for_merge;
55
56static struct csky_arch_for_merge csky_archs[] =
57{
58 /* 510 and 610 merge to 610 without warning. */
59 { "510", CSKY_ARCH_510, CSKY_V1, 0, 0},
60 { "610", CSKY_ARCH_610, CSKY_V1, 1, 0},
61 /* 801, 802, 803, 807, 810 merge to largest one. */
62 { "801", CSKY_ARCH_801, CSKY_V2, 0, 1},
63 { "802", CSKY_ARCH_802, CSKY_V2, 1, 1},
64 { "803", CSKY_ARCH_803, CSKY_V2, 2, 1},
65 { "807", CSKY_ARCH_807, CSKY_V2, 3, 1},
66 { "810", CSKY_ARCH_810, CSKY_V2, 4, 1},
67 { NULL, 0, 0, 0, 0}
68};
69
70/* Return the ARCH bits out of ABFD. */
71#define bfd_csky_arch(abfd) \
72 (elf_elfheader (abfd)->e_flags & CSKY_ARCH_MASK)
73
74/* Return the ABI bits out of ABFD. */
75#define bfd_csky_abi(abfd) \
76 (elf_elfheader (abfd)->e_flags & CSKY_ABI_MASK)
77
78
79/* The index of a howto-item is implicitly equal to
80 the corresponding Relocation Type Encoding. */
81static reloc_howto_type csky_elf_howto_table[] =
82{
83 /* 0 */
84 HOWTO (R_CKCORE_NONE, /* type */
85 0, /* rightshift */
86 0, /* size */
87 0, /* bitsize */
88 FALSE, /* pc_relative */
89 0, /* bitpos */
90 complain_overflow_dont, /* complain_on_overflow */
91 NULL, /* special_function */
92 "R_CKCORE_NONE", /* name */
93 FALSE, /* partial_inplace */
94 0, /* src_mask */
95 0, /* dst_mask */
96 FALSE), /* pcrel_offset */
97
98 /* 1. */
99 HOWTO (R_CKCORE_ADDR32, /* type */
100 0, /* rightshift */
101 2, /* size */
102 32, /* bitsize */
103 FALSE, /* pc_relative */
104 0, /* bitpos */
105 complain_overflow_dont, /* complain_on_overflow */
106 bfd_elf_generic_reloc, /* special_function */
107 "R_CKCORE_ADDR32", /* name */
108 FALSE, /* partial_inplace */
109 0, /* src_mask */
110 0xffffffff, /* dst_mask */
111 FALSE), /* pcrel_offset */
112
113 /* 2: Only for csky v1. */
114 HOWTO (R_CKCORE_PCREL_IMM8BY4, /* type */
115 2, /* rightshift */
116 1, /* size */
117 8, /* bitsize */
118 TRUE, /* pc_relative */
119 0, /* bitpos */
120 complain_overflow_bitfield, /* complain_on_overflow */
121 NULL, /* special_function */
122 "R_CKCORE_PCREL_IMM8BY4", /* name */
123 FALSE, /* partial_inplace */
124 0xff, /* src_mask */
125 0xff, /* dst_mask */
126 TRUE), /* pcrel_offset */
127
128 /* 3: Only for csky v1. */
129 HOWTO (R_CKCORE_PCREL_IMM11BY2, /* type */
130 1, /* rightshift */
131 1, /* size */
132 11, /* bitsize */
133 TRUE, /* pc_relative */
134 0, /* bitpos */
135 complain_overflow_signed, /* complain_on_overflow */
136 bfd_elf_generic_reloc, /* special_function */
137 "R_CKCORE_PCREL_IMM11BY2", /* name */
138 FALSE, /* partial_inplace */
139 0x7ff, /* src_mask */
140 0x7ff, /* dst_mask */
141 TRUE), /* pcrel_offset */
142
143 /* 4: DELETED. */
144 HOWTO (R_CKCORE_PCREL_IMM4BY2,0,0,0,0,0,0,0,"R_CKCORE_PCREL_IMM4BY2",0,0,0,0),
145
146 /* 5. */
147 HOWTO (R_CKCORE_PCREL32, /* type */
148 0, /* rightshift */
149 2, /* size */
150 32, /* bitsize */
151 TRUE, /* pc_relative */
152 0, /* bitpos */
153 complain_overflow_dont, /* complain_on_overflow */
fe75f42e 154 bfd_elf_generic_reloc, /* special_function */
b8891f8d
AJ
155 "R_CKCORE_PCREL32", /* name */
156 FALSE, /* partial_inplace */
157 0x0, /* src_mask */
158 0xffffffff, /* dst_mask */
159 TRUE), /* pcrel_offset */
160
161 /* 6: Only for csky v1. */
162 HOWTO (R_CKCORE_PCREL_JSR_IMM11BY2, /* type */
163 1, /* rightshift */
164 1, /* size */
165 11, /* bitsize */
166 TRUE, /* pc_relative */
167 0, /* bitpos */
168 complain_overflow_signed, /* complain_on_overflow */
169 bfd_elf_generic_reloc, /* special_function */
170 "R_CKCORE_PCREL_JSR_IMM11BY2", /* name */
171 FALSE, /* partial_inplace */
172 0x7ff, /* src_mask */
173 0x7ff, /* dst_mask */
174 TRUE), /* pcrel_offset */
175
176 /* 7: GNU extension to record C++ vtable member usage. */
177 HOWTO (R_CKCORE_GNU_VTENTRY, /* type */
178 0, /* rightshift */
179 2, /* size */
180 0, /* bitsize */
181 FALSE, /* pc_relative */
182 0, /* bitpos */
183 complain_overflow_dont, /* complain_on_overflow */
184 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
185 "R_CKCORE_GNU_VTENTRY", /* name */
186 FALSE, /* partial_inplace */
187 0x0, /* src_mask */
188 0x0, /* dst_mask */
189 FALSE), /* pcrel_offset */
190
191 /* 8: GNU extension to record C++ vtable hierarchy. */
192 HOWTO (R_CKCORE_GNU_VTINHERIT, /* type */
193 0, /* rightshift */
194 2, /* size */
195 0, /* bitsize */
196 FALSE, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_dont, /* complain_on_overflow */
199 NULL, /* special_function */
200 "R_CKCORE_GNU_VTINHERIT", /* name */
201 FALSE, /* partial_inplace */
202 0x0, /* src_mask */
203 0x0, /* dst_mask */
204 FALSE), /* pcrel_offset */
205
206 /* 9. */
207 HOWTO (R_CKCORE_RELATIVE, /* type */
208 0, /* rightshift */
209 2, /* size */
210 32, /* bitsize */
211 FALSE, /* pc_relative */
212 0, /* bitpos */
213 complain_overflow_signed, /* complain_on_overflow */
214 bfd_elf_generic_reloc, /* special_function */
215 "R_CKCORE_RELATIVE", /* name */
216 TRUE, /* partial_inplace */
217 0x0, /* src_mask */
218 0xffffffff, /* dst_mask */
219 FALSE), /* pcrel_offset */
220
221 /* 10: None. */
222 /* FIXME: It is a bug that copy relocations are not implemented. */
223 HOWTO (R_CKCORE_COPY, /* type */
224 0, /* rightshift */
225 2, /* size */
226 32, /* bitsize */
227 FALSE, /* pc_relative */
228 0, /* bitpos */
229 complain_overflow_bitfield, /* complain_on_overflow */
230 bfd_elf_generic_reloc, /* special_function */
231 "R_CKCORE_COPY", /* name */
232 TRUE, /* partial_inplace */
233 0xffffffff, /* src_mask */
234 0xffffffff, /* dst_mask */
235 FALSE), /* pcrel_offset */
236
237 /* 11: None. */
238 HOWTO (R_CKCORE_GLOB_DAT,0,0,0,0,0,0,0,"R_CKCORE_GLOB_DAT",0,0,0,0),
239
240 /* 12: None. */
241 HOWTO (R_CKCORE_JUMP_SLOT,0,0,0,0,0,0,0,"R_CKCORE_JUMP_SLOT",0,0,0,0),
242
243 /* 13. */
244 HOWTO (R_CKCORE_GOTOFF, /* type */
245 0, /* rightshift */
246 2, /* size */
247 32, /* bitsize */
248 FALSE, /* pc_relative */
249 0, /* bitpos */
250 complain_overflow_dont, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_CKCORE_GOTOFF", /* name */
253 TRUE, /* partial_inplace */
254 0x0, /* src_mask */
255 0xffffffffl, /* dst_mask */
256 FALSE), /* pcrel_offset */
257
258 /* 14. */
259 HOWTO (R_CKCORE_GOTPC, /* type */
260 0, /* rightshift */
261 2, /* size */
262 32, /* bitsize */
263 TRUE, /* pc_relative */
264 0, /* bitpos */
265 complain_overflow_dont, /* complain_on_overflow */
266 bfd_elf_generic_reloc, /* special_function */
267 "R_CKCORE_GOTPC", /* name */
268 TRUE, /* partial_inplace */
269 0x0, /* src_mask */
270 0xffffffff, /* dst_mask */
271 FALSE), /* pcrel_offset */
272
273 /* 15. */
274 HOWTO (R_CKCORE_GOT32, /* type */
275 0, /* rightshift */
276 2, /* size */
277 32, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_dont, /* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_CKCORE_GOT32", /* name */
283 TRUE, /* partial_inplace */
284 0x0, /* src_mask */
285 0xffffffff, /* dst_mask */
286 TRUE), /* pcrel_offset */
287
288 /* 16. */
289 HOWTO (R_CKCORE_PLT32, /* type */
290 0, /* rightshift */
291 2, /* size */
292 32, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_dont, /* complain_on_overflow */
296 bfd_elf_generic_reloc, /* special_function */
297 "R_CKCORE_PLT32", /* name */
298 TRUE, /* partial_inplace */
299 0x0, /* src_mask */
300 0xffffffff, /* dst_mask */
301 TRUE), /* pcrel_offset */
302
303 /* 17: None. */
304 HOWTO (R_CKCORE_ADDRGOT,0,0,0,0,0,0,0,"R_CKCORE_ADDRGOT",0,0,0,0),
305
306 /* 18: None. */
307 HOWTO (R_CKCORE_ADDRPLT,0,0,0,0,0,0,0,"R_CKCORE_ADDRPLT",0,0,0,0),
308
309 /* 19: Only for csky v2. */
310 HOWTO (R_CKCORE_PCREL_IMM26BY2, /* type */
311 1, /* rightshift */
312 2, /* size */
313 26, /* bitsize */
314 TRUE, /* pc_relative */
315 0, /* bitpos */
316 complain_overflow_signed, /* complain_on_overflow */
317 bfd_elf_generic_reloc, /* special_function */
318 "R_CKCORE_PCREL_IMM26BY2", /* name */
319 FALSE, /* partial_inplace */
320 0x0, /* src_mask */
321 0x3ffffff, /* dst_mask */
322 TRUE), /* pcrel_offset */
323
324 /* 20: Only for csky v2. */
325 HOWTO (R_CKCORE_PCREL_IMM16BY2, /* type */
326 1, /* rightshift */
327 2, /* size */
328 16, /* bitsize */
329 TRUE, /* pc_relative */
330 0, /* bitpos */
331 complain_overflow_signed, /* complain_on_overflow */
332 NULL, /* special_function */
333 "R_CKCORE_PCREL_IMM16BY2", /* name */
334 FALSE, /* partial_inplace */
335 0x0, /* src_mask */
336 0xffff, /* dst_mask */
337 TRUE), /* pcrel_offset */
338
339 /* 21: Only for csky v2. */
340 HOWTO (R_CKCORE_PCREL_IMM16BY4, /* type */
341 2, /* rightshift */
342 2, /* size */
343 16, /* bitsize */
344 TRUE, /* pc_relative */
345 0, /* bitpos */
346 complain_overflow_bitfield, /* complain_on_overflow */
347 NULL, /* special_function */
348 "R_CKCORE_PCREL_IMM16BY4", /* name */
349 FALSE, /* partial_inplace */
350 0xffff0000, /* src_mask */
351 0xffff, /* dst_mask */
352 TRUE), /* pcrel_offset */
353
354 /* 22: Only for csky v2. */
355 HOWTO (R_CKCORE_PCREL_IMM10BY2, /* type */
356 1, /* rightshift */
357 1, /* size */
358 10, /* bitsize */
359 TRUE, /* pc_relative */
360 0, /* bitpos */
361 complain_overflow_signed, /* complain_on_overflow */
362 bfd_elf_generic_reloc, /* special_function */
363 "R_CKCORE_PCREL_IMM10BY2", /* name */
364 FALSE, /* partial_inplace */
365 0x0, /* src_mask */
366 0x3ff, /* dst_mask */
367 TRUE), /* pcrel_offset */
368
369 /* 23: Only for csky v2. */
370 HOWTO (R_CKCORE_PCREL_IMM10BY4, /* type */
371 2, /* rightshift */
372 2, /* size */
373 10, /* bitsize */
374 TRUE, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_bitfield, /* complain_on_overflow */
377 NULL, /* special_function */
378 "R_CKCORE_PCREL_IMM10BY4", /* name */
379 FALSE, /* partial_inplace */
380 0x0, /* src_mask */
381 0x3ff, /* dst_mask */
382 TRUE), /* pcrel_offset */
383
384 /* 24: Only for csky v2. */
385 HOWTO (R_CKCORE_ADDR_HI16, /* type */
386 16, /* rightshift */
387 2, /* size */
388 16, /* bitsize */
389 FALSE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_dont, /* complain_on_overflow */
392 bfd_elf_generic_reloc, /* special_function */
393 "R_CKCORE_ADDR_HI16", /* name */
394 FALSE, /* partial_inplace */
395 0x0, /* src_mask */
396 0xffff, /* dst_mask */
397 FALSE), /* pcrel_offset */
398
399 /* 25. */
400 HOWTO (R_CKCORE_ADDR_LO16, /* type */
401 0, /* rightshift */
402 2, /* size */
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_CKCORE_ADDR_LO16", /* name */
409 FALSE, /* partial_inplace */
410 0x0, /* src_mask */
411 0xffff, /* dst_mask */
412 FALSE), /* pcrel_offset */
413
414 /* 26. */
415 HOWTO (R_CKCORE_GOTPC_HI16, /* type */
416 16, /* rightshift */
417 2, /* size */
418 16, /* bitsize */
419 TRUE, /* pc_relative */
420 0, /* bitpos */
421 complain_overflow_dont, /* complain_on_overflow */
422 bfd_elf_generic_reloc, /* special_function */
423 "R_CKCORE_GOTPC_HI16", /* name */
424 FALSE, /* partial_inplace */
425 0x0, /* src_mask */
426 0xffff, /* dst_mask */
427 FALSE), /* pcrel_offset */
428
429 /* 27. */
430 HOWTO (R_CKCORE_GOTPC_LO16, /* type */
431 0, /* rightshift */
432 2, /* size */
433 16, /* bitsize */
434 TRUE, /* pc_relative */
435 0, /* bitpos */
436 complain_overflow_dont, /* complain_on_overflow */
437 bfd_elf_generic_reloc, /* special_function */
438 "R_CKCORE_GOTPC_LO16", /* name */
439 FALSE, /* partial_inplace */
440 0x0, /* src_mask */
441 0xffff, /* dst_mask */
442 FALSE), /* pcrel_offset */
443
444 /* 28. */
445 HOWTO (R_CKCORE_GOTOFF_HI16, /* type */
446 16, /* rightshift */
447 2, /* size */
448 16, /* bitsize */
449 FALSE, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_dont, /* complain_on_overflow */
452 bfd_elf_generic_reloc, /* special_function */
453 "R_CKCORE_GOTOFF_HI16", /* name */
454 FALSE, /* partial_inplace */
455 0x0, /* src_mask */
456 0xffff, /* dst_mask */
457 FALSE), /* pcrel_offset */
458
459 /* 29. */
460 HOWTO (R_CKCORE_GOTOFF_LO16, /* type */
461 0, /* rightshift */
462 2, /* size */
463 16, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
467 bfd_elf_generic_reloc, /* special_function */
468 "R_CKCORE_GOTOFF_LO16", /* name */
469 FALSE, /* partial_inplace */
470 0x0, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
473
474 /* 30. */
475 HOWTO (R_CKCORE_GOT12, /* type */
476 2, /* rightshift */
477 2, /* size */
478 12, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_bitfield, /* complain_on_overflow */
482 bfd_elf_generic_reloc, /* special_function */
483 "R_CKCORE_GOT12", /* name */
484 TRUE, /* partial_inplace */
485 0x0, /* src_mask */
486 0xfff, /* dst_mask */
487 FALSE), /* pcrel_offset */
488
489 /* 31. */
490 HOWTO (R_CKCORE_GOT_HI16, /* type */
491 16, /* rightshift */
492 2, /* size */
493 16, /* bitsize */
494 FALSE, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_dont, /* complain_on_overflow */
497 bfd_elf_generic_reloc, /* special_function */
498 "R_CKCORE_GOT_HI16", /* name */
499 TRUE, /* partial_inplace */
500 0x0, /* src_mask */
501 0xffff, /* dst_mask */
502 FALSE), /* pcrel_offset */
503
504 /* 32. */
505 HOWTO (R_CKCORE_GOT_LO16, /* type */
506 0, /* rightshift */
507 2, /* size */
508 16, /* bitsize */
509 FALSE, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_dont, /* complain_on_overflow */
512 bfd_elf_generic_reloc, /* special_function */
513 "R_CKCORE_GOT_LO16", /* name */
514 TRUE, /* partial_inplace */
515 0x0, /* src_mask */
516 0xffff, /* dst_mask */
517 FALSE), /* pcrel_offset */
518
519 /* 33. */
520 HOWTO (R_CKCORE_PLT12, /* type */
521 2, /* rightshift */
522 2, /* size */
523 12, /* bitsize */
524 FALSE, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_bitfield, /* complain_on_overflow */
527 bfd_elf_generic_reloc, /* special_function */
528 "R_CKCORE_PLT12", /* name */
529 TRUE, /* partial_inplace */
530 0x0, /* src_mask */
531 0xfff, /* dst_mask */
532 FALSE), /* pcrel_offset */
533
534 /* 34. */
535 HOWTO (R_CKCORE_PLT_HI16, /* type */
536 16, /* rightshift */
537 2, /* size */
538 16, /* bitsize */
539 FALSE, /* pc_relative */
540 0, /* bitpos */
541 complain_overflow_dont, /* complain_on_overflow */
542 bfd_elf_generic_reloc, /* special_function */
543 "R_CKCORE_PLT_HI16", /* name */
544 TRUE, /* partial_inplace */
545 0x0, /* src_mask */
546 0xffff, /* dst_mask */
547 FALSE), /* pcrel_offset */
548
549 /* 35. */
550 HOWTO (R_CKCORE_PLT_LO16, /* type */
551 0, /* rightshift */
552 2, /* size */
553 16, /* bitsize */
554 FALSE, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_dont, /* complain_on_overflow */
557 bfd_elf_generic_reloc, /* special_function */
558 "R_CKCORE_PLT_LO16", /* name */
559 TRUE, /* partial_inplace */
560 0x0, /* src_mask */
561 0xffff, /* dst_mask */
562 FALSE), /* pcrel_offset */
563
564 /* 36: None. */
565 HOWTO (R_CKCORE_ADDRGOT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
566
567 /* 37: None. */
568 HOWTO (R_CKCORE_ADDRGOT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
569
570 /* 38: None. */
571 HOWTO (R_CKCORE_ADDRPLT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
572
573 /* 39: None. */
574 HOWTO (R_CKCORE_ADDRPLT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
575
576 /* 40. */
577 HOWTO (R_CKCORE_PCREL_JSR_IMM26BY2, /* type */
578 1, /* rightshift */
579 2, /* size */
580 26, /* bitsize */
581 TRUE, /* pc_relative */
582 0, /* bitpos */
583 complain_overflow_signed, /* complain_on_overflow */
584 bfd_elf_generic_reloc, /* special_function */
585 "R_CKCORE_PCREL_JSR_IMM26BY2", /* name */
586 FALSE, /* partial_inplace */
587 0x0, /* src_mask */
588 0x3ffffff, /* dst_mask */
589 TRUE), /* pcrel_offset */
590
591 /* 41. */
592 HOWTO (R_CKCORE_TOFFSET_LO16, /* type */
593 0, /* rightshift */
594 2, /* size */
595 16, /* bitsize */
596 FALSE, /* pc_relative */
597 0, /* bitpos */
598 complain_overflow_unsigned, /* complain_on_overflow */
599 NULL, /* special_function */
600 "R_CKCORE_TOFFSET_LO16", /* name */
601 FALSE, /* partial_inplace */
602 0x0, /* src_mask */
603 0xffff, /* dst_mask */
604 FALSE), /* pcrel_offset */
605
606 /* 42. */
607 HOWTO (R_CKCORE_DOFFSET_LO16, /* type */
608 0, /* rightshift */
609 2, /* size */
610 16, /* bitsize */
611 FALSE, /* pc_relative */
612 0, /* bitpos */
613 complain_overflow_unsigned, /* complain_on_overflow */
614 NULL, /* special_function */
615 "R_CKCORE_DOFFSET_LO16", /* name */
616 FALSE, /* partial_inplace */
617 0x0, /* src_mask */
618 0xffff, /* dst_mask */
619 FALSE), /* pcrel_offset */
620
621 /* 43. */
622 HOWTO (R_CKCORE_PCREL_IMM18BY2, /* type */
623 1, /* rightshift */
624 2, /* size */
625 18, /* bitsize */
626 TRUE, /* pc_relative */
627 0, /* bitpos */
628 complain_overflow_signed, /* complain_on_overflow */
629 NULL, /* special_function */
630 "R_CKCORE_PCREL_IMM18BY2", /* name */
631 FALSE, /* partial_inplace */
632 0x0, /* src_mask */
633 0x3ffff, /* dst_mask */
634 TRUE), /* pcrel_offset */
635
636 /* 44. */
637 HOWTO (R_CKCORE_DOFFSET_IMM18, /* type */
638 0, /* rightshift */
639 2, /* size */
640 18, /* bitsize */
641 FALSE, /* pc_relative */
642 0, /* bitpos */
643 complain_overflow_unsigned, /* complain_on_overflow */
644 NULL, /* special_function */
645 "R_CKCORE_DOFFSET_IMM18", /* name */
646 FALSE, /* partial_inplace */
647 0x0, /* src_mask */
648 0x3ffff, /* dst_mask */
649 FALSE), /* pcrel_offset */
650
651 /* 45. */
652 HOWTO (R_CKCORE_DOFFSET_IMM18BY2, /* type */
653 1, /* rightshift */
654 2, /* size */
655 18, /* bitsize */
656 FALSE, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_unsigned, /* complain_on_overflow */
659 NULL, /* special_function */
660 "R_CKCORE_DOFFSET_IMM18BY2", /* name */
661 FALSE, /* partial_inplace */
662 0x0, /* src_mask */
663 0x3ffff, /* dst_mask */
664 FALSE), /* pcrel_offset */
665
666 /* 46. */
667 HOWTO (R_CKCORE_DOFFSET_IMM18BY4, /* type */
668 2, /* rightshift */
669 2, /* size */
670 18, /* bitsize */
671 FALSE, /* pc_relative */
672 0, /* bitpos */
673 complain_overflow_unsigned, /* complain_on_overflow */
674 NULL, /* special_function */
675 "R_CKCORE_DOFFSET_IMM18BY4", /* name */
676 FALSE, /* partial_inplace */
677 0x0, /* src_mask */
678 0x3ffff, /* dst_mask */
679 FALSE), /* pcrel_offset */
680
681 /* 47. */
682 HOWTO (R_CKCORE_GOTOFF_IMM18, /* type */
683 0, /* rightshift */
684 2, /* size */
685 18, /* bitsize */
686 FALSE, /* pc_relative */
687 0, /* bitpos */
688 complain_overflow_bitfield, /* complain_on_overflow */
689 bfd_elf_generic_reloc, /* special_function */
690 "R_CKCORE_GOTOFF_IMM18", /* name */
691 TRUE, /* partial_inplace */
692 0xfffc, /* src_mask */
693 0x3ffff, /* dst_mask */
694 FALSE), /* pcrel_offset */
695
696 /* 48. */
697 HOWTO (R_CKCORE_GOT_IMM18BY4, /* type */
698 2, /* rightshift */
699 2, /* size */
700 18, /* bitsize */
701 FALSE, /* pc_relative */
702 0, /* bitpos */
703 complain_overflow_bitfield, /* complain_on_overflow */
704 bfd_elf_generic_reloc, /* special_function */
705 "R_CKCORE_GOT_IMM18BY4", /* name */
706 TRUE, /* partial_inplace */
707 0xfffc, /* src_mask */
708 0x3ffff, /* dst_mask */
709 FALSE), /* pcrel_offset */
710
711 /* 49. */
712 HOWTO (R_CKCORE_PLT_IMM18BY4, /* type */
713 2, /* rightshift */
714 2, /* size */
715 18, /* bitsize */
716 FALSE, /* pc_relative */
717 0, /* bitpos */
718 complain_overflow_bitfield, /* complain_on_overflow */
719 bfd_elf_generic_reloc, /* special_function */
720 "R_CKCORE_PLT_IMM18BY4", /* name */
721 TRUE, /* partial_inplace */
722 0xfffc, /* src_mask */
723 0x3ffff, /* dst_mask */
724 TRUE), /* pcrel_offset */
725
726 /* 50: for lrw16. */
727 HOWTO (R_CKCORE_PCREL_IMM7BY4, /* type */
728 2, /* rightshift */
729 1, /* size */
730 7, /* bitsize */
731 TRUE, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_bitfield, /* complain_on_overflow */
734 bfd_elf_generic_reloc, /* special_function */
735 "R_CKCORE_PCREL_IMM7BY4", /* name */
736 FALSE, /* partial_inplace */
737 0xec1f, /* src_mask */
738 0x31f, /* dst_mask */
739 TRUE), /* pcrel_offset */
740
741 /* 51: for static nptl. */
742 HOWTO (R_CKCORE_TLS_LE32, /* type */
743 0, /* rightshift */
744 2, /* size */
745 32, /* bitsize */
746 FALSE, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_dont, /* complain_on_overflow */
749 bfd_elf_generic_reloc, /* special_function */
750 "R_CKCORE_TLS_LE32", /* name */
751 FALSE, /* partial_inplace */
752 0x0, /* src_mask */
753 0xffffffff, /* dst_mask */
754 TRUE), /* pcrel_offset */
755
756 /* 52: for static nptl. */
757 HOWTO (R_CKCORE_TLS_IE32, /* type */
758 0, /* rightshift */
759 2, /* size */
760 32, /* bitsize */
761 FALSE, /* pc_relative */
762 0, /* bitpos */
763 complain_overflow_dont, /* complain_on_overflow */
764 bfd_elf_generic_reloc, /* special_function */
765 "R_CKCORE_TLS_IE32", /* name */
766 FALSE, /* partial_inplace */
767 0x0, /* src_mask */
768 0xffffffff, /* dst_mask */
769 TRUE), /* pcrel_offset */
770
771 /* 53: for pic nptl. */
772 HOWTO (R_CKCORE_TLS_GD32, /* type */
773 0, /* rightshift */
774 2, /* size */
775 32, /* bitsize */
776 FALSE, /* pc_relative */
777 0, /* bitpos */
778 complain_overflow_dont, /* complain_on_overflow */
779 bfd_elf_generic_reloc, /* special_function */
780 "R_CKCORE_TLS_GD32", /* name */
781 FALSE, /* partial_inplace */
782 0x0, /* src_mask */
783 0xffffffff, /* dst_mask */
784 TRUE), /* pcrel_offset */
785
786 /* 54: for pic nptl. */
787 HOWTO (R_CKCORE_TLS_LDM32, /* type */
788 0, /* rightshift */
789 2, /* size */
790 32, /* bitsize */
791 FALSE, /* pc_relative */
792 0, /* bitpos */
793 complain_overflow_dont, /* complain_on_overflow */
794 bfd_elf_generic_reloc, /* special_function */
795 "R_CKCORE_TLS_LDM32", /* name */
796 FALSE, /* partial_inplace */
797 0x0, /* src_mask */
798 0xffffffff, /* dst_mask */
799 TRUE), /* pcrel_offset */
800
801 /* 55: for pic nptl. */
802 HOWTO (R_CKCORE_TLS_LDO32, /* type */
803 0, /* rightshift */
804 2, /* size */
805 32, /* bitsize */
806 FALSE, /* pc_relative */
807 0, /* bitpos */
808 complain_overflow_dont, /* complain_on_overflow */
809 bfd_elf_generic_reloc, /* special_function */
810 "R_CKCORE_TLS_LDO32", /* name */
811 FALSE, /* partial_inplace */
812 0x0, /* src_mask */
813 0xffffffff, /* dst_mask */
814 TRUE), /* pcrel_offset */
815
816 /* 56: for linker. */
817 HOWTO (R_CKCORE_TLS_DTPMOD32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPMOD32",0,0,0,0),
818
819 /* 57: for linker. */
820 HOWTO (R_CKCORE_TLS_DTPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPOFF32",0,0,0,0),
821
822 /* 58: for linker. */
823 HOWTO (R_CKCORE_TLS_TPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_TPOFF32",0,0,0,0),
824
825 /* 59: for ck807f. */
826 HOWTO (R_CKCORE_PCREL_FLRW_IMM8BY4, /* type */
827 2, /* rightshift */
828 2, /* size */
829 8, /* bitsize */
830 TRUE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_bitfield, /* complain_on_overflow */
833 NULL, /* special_function */
834 "R_CKCORE_PCREL_FLRW_IMM8BY4", /* name */
835 FALSE, /* partial_inplace */
836 0xfe1fff0f, /* src_mask */
837 0x1e000f0, /* dst_mask */
838 TRUE), /* pcrel_offset */
839
840 /* 60: for 810 not to generate jsri. */
841 HOWTO (R_CKCORE_NOJSRI, /* type */
842 0, /* rightshift */
843 2, /* size */
844 32, /* bitsize */
845 FALSE, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_dont, /* complain_on_overflow */
848 bfd_elf_generic_reloc, /* special_function */
849 "R_CKCORE_NOJSRI", /* name */
850 FALSE, /* partial_inplace */
851 0xffff, /* src_mask */
852 0xffff, /* dst_mask */
853 FALSE), /* pcrel_offset */
854
855 /* 61: for callgraph. */
856 HOWTO (R_CKCORE_CALLGRAPH, /* type */
857 0, /* rightshift */
858 0, /* size */
859 0, /* bitsize */
860 FALSE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_dont, /* complain_on_overflow */
863 NULL, /* special_function */
864 "R_CKCORE_CALLGRAPH", /* name */
865 FALSE, /* partial_inplace */
866 0x0, /* src_mask */
867 0x0, /* dst_mask */
868 TRUE), /* pcrel_offset */
869
870 /* 62: IRELATIVE*/
871 HOWTO (R_CKCORE_IRELATIVE,0,0,0,0,0,0,0,"R_CKCORE_IRELATIVE",0,0,0,0),
872
873 /* 63: for bloop instruction */
874 HOWTO (R_CKCORE_PCREL_BLOOP_IMM4BY4, /* type */
875 1, /* rightshift */
876 2, /* size */
877 4, /* bitsize */
878 1, /* pc_relative */
879 0, /* bitpos */
880 complain_overflow_signed, /* complain_on_overflow */
881 bfd_elf_generic_reloc, /* special_function */
882 "R_CKCORE_PCREL_BLOOP_IMM4BY4", /* name */
883 FALSE, /* partial_inplace */
884 0x0, /* src_mask */
885 0xf, /* dst_mask */
886 TRUE), /* pcrel_offset */
887 /* 64: for bloop instruction */
888 HOWTO (R_CKCORE_PCREL_BLOOP_IMM12BY4, /* type */
889 1, /* rightshift */
890 2, /* size */
891 12, /* bitsize */
892 1, /* pc_relative */
893 0, /* bitpos */
894 complain_overflow_signed, /* complain_on_overflow */
895 bfd_elf_generic_reloc, /* special_function */
896 "R_CKCORE_PCREL_BLOOP_IMM12BY4", /* name */
897 FALSE, /* partial_inplace */
898 0x0, /* src_mask */
899 0xfff, /* dst_mask */
900 TRUE), /* pcrel_offset */
901
902
903};
904
905
906/* Whether GOT overflow checking is needed. */
907static int check_got_overflow = 0;
908
909/* Whether the target 32 bits is forced so that the high
910 16 bits is at the low address. */
911static int need_reverse_bits;
912
913/* Used for relaxation. See csky_relocate_contents. */
914static bfd_vma read_content_substitute;
915
916/* NOTICE!
917 The way the following two look-up functions work demands
918 that BFD_RELOC_CKCORE_xxx are defined contiguously. */
919
920static reloc_howto_type *
921csky_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
922 bfd_reloc_code_real_type code)
923{
924 int csky_code = code - BFD_RELOC_CKCORE_NONE;
925
926 if (csky_code < 0 || csky_code >= R_CKCORE_MAX)
927 {
928 switch (code)
929 {
930 case BFD_RELOC_NONE:
931 csky_code = R_CKCORE_NONE;
932 break;
933 case BFD_RELOC_32:
934 csky_code = R_CKCORE_ADDR32;
935 break;
936 case BFD_RELOC_32_PCREL:
937 csky_code = R_CKCORE_PCREL32;
938 break;
939 case BFD_RELOC_VTABLE_INHERIT:
940 csky_code = R_CKCORE_GNU_VTINHERIT;
941 break;
942 case BFD_RELOC_VTABLE_ENTRY:
943 csky_code = R_CKCORE_GNU_VTENTRY;
944 break;
945 case BFD_RELOC_RVA:
946 csky_code = R_CKCORE_RELATIVE;
947 break;
948 default:
949 return (reloc_howto_type *)NULL;
950 }
951 }
952 /* Note: when adding csky bfd reloc types in bfd-in2.h
953 and csky elf reloc types in elf/csky.h,
954 the order of the two reloc type tables should be consistent. */
955 return &csky_elf_howto_table[csky_code];
956}
957
958static reloc_howto_type *
959csky_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
960 const char *r_name)
961{
962 unsigned int i;
963 for (i = 0; i < R_CKCORE_MAX; i++)
964 if (strcasecmp (csky_elf_howto_table[i].name, r_name) == 0)
965 return &csky_elf_howto_table[i];
966 return NULL;
967}
968
969static reloc_howto_type *
970elf32_csky_howto_from_type (unsigned int r_type)
971{
972 if (r_type < R_CKCORE_MAX)
973 return &csky_elf_howto_table[r_type];
974 else
975 return NULL;
976}
977
978static bfd_boolean
979csky_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
980 arelent *cache_ptr,
981 Elf_Internal_Rela *dst)
982{
983 unsigned int r_type;
984
985 r_type = ELF32_R_TYPE (dst->r_info);
986 cache_ptr->howto = elf32_csky_howto_from_type (r_type);
987 if (cache_ptr->howto == NULL)
988 {
989 /* xgettext:c-format */
990 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
991 abfd, r_type);
992 bfd_set_error (bfd_error_bad_value);
993 return FALSE;
994 }
995 return TRUE;
996}
997
998/* The Global Offset Table max size. */
999#define GOT_MAX_SIZE 0xFFFF8
1000
1001/* The name of the dynamic interpreter. This is put in the .interp
1002 section. */
1003#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
1004
1005/* The size in bytes of an entry in the procedure linkage table. */
1006#define PLT_ENTRY_SIZE 12
1007#define PLT_ENTRY_SIZE_P 16
1008
1009/* The first entry in a procedure linkage table looks like
1010 this. It is set up so that any shared library function that is
1011 called before the relocation has been set up calls the dynamic
1012 linker first. */
1013static const bfd_vma csky_elf_plt_entry_v2[PLT_ENTRY_SIZE / 4] =
1014{
1015 0xd99c2002, /* ldw r12, (gb, 8) */
1016 0xea0d0000, /* movi r13,offset */
1017 0xe8cc0000 /* jmp r12 */
1018};
1019
1020static const bfd_vma csky_elf_plt_entry_v1[PLT_ENTRY_SIZE / 2 ] =
1021{
1022 0x25f0, /* subi r0, 32 */
1023 0x9200, /* stw r2, (r0, 0) */
1024 0x9310, /* stw r3, (r0, 4) */
1025 0x822e, /* ldw r2, (gb, 8) */
1026 0x7301, /* lrw r3, #offset */
1027 0x00c2, /* jmp r2 */
1028};
1029
1030/* Branch stub support. */
1031
1032enum stub_insn_type
1033{
1034 INSN16,
1035 INSN32,
1036 DATA_TYPE
1037};
1038
1039bfd_boolean use_branch_stub = TRUE;
1040typedef struct
1041{
1042 bfd_vma data;
1043 enum stub_insn_type type;
1044 unsigned int r_type;
1045 int reloc_addend;
1046} insn_sequence;
1047
1048static const insn_sequence elf32_csky_stub_long_branch[] =
1049{
1050 {0xea8d0002, INSN32, R_CKCORE_NONE, 0x0}, /* lrw t1,[pc+8] */
1051 {0x7834, INSN16, R_CKCORE_NONE, 0x0}, /* jmp t1 */
1052 {0x6c03, INSN16, R_CKCORE_NONE, 0x0}, /* nop */
1053 {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
1054};
1055
1056static const insn_sequence elf32_csky_stub_long_branch_jmpi[] =
1057{
1058 {0xeac00001, INSN32, R_CKCORE_NONE, 0x0}, /* jmpi [pc+4] */
1059 {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
1060};
1061
1062/* The bsr instruction offset limit. */
1063#define BSR_MAX_FWD_BRANCH_OFFSET (((1 << 25) - 1) << 1)
1064#define BSR_MAX_BWD_BRANCH_OFFSET (-(1 << 26))
1065
1066#define STUB_SUFFIX ".stub"
1067#define STUB_ENTRY_NAME "__%s_veneer"
1068
1069/* One entry per long/short branch stub defined above. */
1070#define DEF_STUBS \
1071 DEF_STUB(long_branch) \
1072 DEF_STUB(long_branch_jmpi)
1073
1074#define DEF_STUB(x) csky_stub_##x,
1075enum elf32_csky_stub_type
1076{
1077 csky_stub_none,
1078 DEF_STUBS
1079};
1080#undef DEF_STUB
1081
1082typedef struct
1083{
1084 const insn_sequence* template_sequence;
1085 int template_size;
1086} stub_def;
1087
1088#define DEF_STUB(x) {elf32_csky_stub_##x, ARRAY_SIZE(elf32_csky_stub_##x)},
1089static const stub_def stub_definitions[] = {
1090 {NULL, 0},
1091 DEF_STUBS
1092};
1093
1094/* The size of the thread control block. */
1095#define TCB_SIZE 8
1096
1097struct csky_elf_obj_tdata
1098{
1099 struct elf_obj_tdata root;
1100
1101 /* tls_type for each local got entry. */
1102 char *local_got_tls_type;
1103};
1104
1105#define csky_elf_local_got_tls_type(bfd) \
1106 (csky_elf_tdata (bfd)->local_got_tls_type)
1107
1108#define csky_elf_tdata(bfd) \
1109 ((struct csky_elf_obj_tdata *) (bfd)->tdata.any)
1110
1111struct elf32_csky_stub_hash_entry
1112{
1113 /* Base hash table entry structure. */
1114 struct bfd_hash_entry root;
1115
1116 /* The stub section. */
1117 asection *stub_sec;
1118
1119 /* Offset within stub_sec of the beginning of this stub. */
1120 bfd_vma stub_offset;
1121
1122 /* Given the symbol's value and its section we can determine its final
1123 value when building the stubs (so the stub knows where to jump). */
1124 bfd_vma target_value;
1125 asection *target_section;
1126
1127 /* Offset to apply to relocation referencing target_value. */
1128 bfd_vma target_addend;
1129
1130 /* The stub type. */
1131 enum elf32_csky_stub_type stub_type;
1132 /* Its encoding size in bytes. */
1133 int stub_size;
1134 /* Its template. */
1135 const insn_sequence *stub_template;
1136 /* The size of the template (number of entries). */
1137 int stub_template_size;
1138
1139 /* The symbol table entry, if any, that this was derived from. */
1140 struct csky_elf_link_hash_entry *h;
1141
1142 /* Destination symbol type. */
1143 unsigned char st_type;
1144
1145 /* Where this stub is being called from, or, in the case of combined
1146 stub sections, the first input section in the group. */
1147 asection *id_sec;
1148
1149 /* The name for the local symbol at the start of this stub. The
1150 stub name in the hash table has to be unique; this does not, so
1151 it can be friendlier. */
1152 char *output_name;
1153};
1154
1155#define csky_stub_hash_lookup(table, string, create, copy) \
1156 ((struct elf32_csky_stub_hash_entry *) \
1157 bfd_hash_lookup ((table), (string), (create), (copy)))
1158
1159/* C-SKY ELF linker hash entry. */
1160struct csky_elf_link_hash_entry
1161{
1162 struct elf_link_hash_entry elf;
1163 int plt_refcount;
1164 /* For sub jsri2bsr relocs count. */
1165 int jsri2bsr_refcount;
b8891f8d
AJ
1166
1167#define GOT_UNKNOWN 0
1168#define GOT_NORMAL 1
1169#define GOT_TLS_GD 2
1170#define GOT_TLS_IE 4
1171
1172 unsigned char tls_type;
1173
1174 /* A pointer to the most recently used stub hash entry against this
1175 symbol. */
1176 struct elf32_csky_stub_hash_entry *stub_cache;
1177};
1178
1179/* Traverse an C-SKY ELF linker hash table. */
1180#define csky_elf_link_hash_traverse(table, func, info) \
1181 (elf_link_hash_traverse \
1182 (&(table)->root, \
1183 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1184 (info)))
1185
1186/* Get the C-SKY ELF linker hash table from a link_info structure. */
1187#define csky_elf_hash_table(info) \
1188 ((elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
1189 == CSKY_ELF_DATA) \
1190 ? ((struct csky_elf_link_hash_table *) ((info)->hash)) \
1191 : NULL)
1192
1193#define csky_elf_hash_entry(ent) ((struct csky_elf_link_hash_entry*)(ent))
1194
1195/* Array to keep track of which stub sections have been created, and
1196 information on stub grouping. */
1197struct map_stub
1198{
1199 /* This is the section to which stubs in the group will be
1200 attached. */
1201 asection *link_sec;
1202 /* The stub section. */
1203 asection *stub_sec;
1204};
1205
1206/* C-SKY ELF linker hash table. */
1207struct csky_elf_link_hash_table
1208{
1209 struct elf_link_hash_table elf;
1210
1211 /* Small local sym cache. */
1212 struct sym_cache sym_cache;
1213
1214 /* Data for R_CKCORE_TLS_LDM32 relocations. */
1215 union
1216 {
1217 bfd_signed_vma refcount;
1218 bfd_vma offset;
1219 } tls_ldm_got;
1220
1221 /* The stub hash table. */
1222 struct bfd_hash_table stub_hash_table;
1223
1224 /* Linker stub bfd. */
1225 bfd *stub_bfd;
1226
1227 /* Linker call-backs. */
1228 asection * (*add_stub_section) (const char *, asection *);
1229 void (*layout_sections_again) (void);
1230
1231 /* Array to keep track of which stub sections have been created, and
1232 * information on stub grouping. */
1233 struct map_stub *stub_group;
1234
1235 /* Number of elements in stub_group. */
1236 unsigned int top_id;
1237
1238 /* Assorted information used by elf32_csky_size_stubs. */
1239 unsigned int bfd_count;
1240 unsigned int top_index;
1241 asection **input_list;
1242};
1243
1244/* We can't change vectors in the bfd target which will apply to
1245 data sections, however we only do this to the text sections. */
1246
1247static bfd_vma
1248csky_get_insn_32 (bfd *input_bfd,
1249 bfd_byte *location)
1250{
1251 if (bfd_big_endian (input_bfd))
1252 return bfd_get_32 (input_bfd, location);
1253 else
1254 return (bfd_get_16 (input_bfd, location) << 16
1255 | bfd_get_16 (input_bfd, location + 2));
1256}
1257
1258static void
1259csky_put_insn_32 (bfd *input_bfd,
1260 bfd_vma x,
1261 bfd_byte *location)
1262{
1263 if (bfd_big_endian (input_bfd))
1264 bfd_put_32 (input_bfd, x, location);
1265 else
1266 {
1267 bfd_put_16 (input_bfd, x >> 16, location);
1268 bfd_put_16 (input_bfd, x & 0xffff, location + 2);
1269 }
1270}
1271
1272/* Find or create a stub section. Returns a pointer to the stub section, and
1273 the section to which the stub section will be attached (in *LINK_SEC_P).
1274 LINK_SEC_P may be NULL. */
1275
1276static asection *
1277elf32_csky_create_or_find_stub_sec (asection **link_sec_p, asection *section,
1278 struct csky_elf_link_hash_table *htab)
1279{
1280 asection *link_sec;
1281 asection *stub_sec;
1282
1283 link_sec = htab->stub_group[section->id].link_sec;
1284 stub_sec = htab->stub_group[section->id].stub_sec;
1285 if (stub_sec == NULL)
1286 {
1287 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1288 if (stub_sec == NULL)
1289 {
1290 size_t namelen;
1291 bfd_size_type len;
1292 char *s_name;
1293
1294 namelen = strlen (link_sec->name);
1295 len = namelen + sizeof (STUB_SUFFIX);
1296 s_name = bfd_alloc (htab->stub_bfd, len);
1297 if (s_name == NULL)
1298 return NULL;
1299
1300 memcpy (s_name, link_sec->name, namelen);
1301 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1302 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1303 if (stub_sec == NULL)
1304 return NULL;
1305 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1306 }
1307 htab->stub_group[section->id].stub_sec = stub_sec;
1308 }
1309
1310 if (link_sec_p)
1311 *link_sec_p = link_sec;
1312
1313 return stub_sec;
1314}
1315
1316/* Build a name for an entry in the stub hash table. */
1317
1318static char *
1319elf32_csky_stub_name (const asection *input_section,
1320 const asection *sym_sec,
1321 const struct csky_elf_link_hash_entry *hash,
1322 const Elf_Internal_Rela *rel)
1323{
1324 char *stub_name;
1325 bfd_size_type len;
1326
1327 if (hash)
1328 {
1329 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
1330 stub_name = bfd_malloc (len);
1331 if (stub_name != NULL)
1332 sprintf (stub_name, "%08x_%s+%x",
1333 input_section->id & 0xffffffff,
1334 hash->elf.root.root.string,
1335 (int) rel->r_addend & 0xffffffff);
1336 }
1337 else
1338 {
1339 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1340 stub_name = bfd_malloc (len);
1341 if (stub_name != NULL)
1342 sprintf (stub_name, "%08x_%x:%x+%x",
1343 input_section->id & 0xffffffff,
1344 sym_sec->id & 0xffffffff,
1345 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1346 (int) rel->r_addend & 0xffffffff);
1347 }
1348
1349 return stub_name;
1350}
1351
1352/* Determine the type of stub needed, if any, for a call. */
1353
1354static enum elf32_csky_stub_type
1355csky_type_of_stub (struct bfd_link_info *info,
1356 asection *input_sec,
1357 const Elf_Internal_Rela *rel,
1358 unsigned char st_type,
1359 struct csky_elf_link_hash_entry *hash,
1360 bfd_vma destination,
1361 asection *sym_sec ATTRIBUTE_UNUSED,
1362 bfd *input_bfd ATTRIBUTE_UNUSED,
1363 const char *name ATTRIBUTE_UNUSED)
1364{
1365 bfd_vma location;
1366 bfd_signed_vma branch_offset;
1367 unsigned int r_type;
1368 enum elf32_csky_stub_type stub_type = csky_stub_none;
1369 struct elf_link_hash_entry * h = &hash->elf;
1370
1371 /* We don't know the actual type of destination in case it is of
1372 type STT_SECTION: give up. */
1373 if (st_type == STT_SECTION)
1374 return stub_type;
1375
1376 location = (input_sec->output_offset
1377 + input_sec->output_section->vma
1378 + rel->r_offset);
1379
1380 branch_offset = (bfd_signed_vma)(destination - location);
1381 r_type = ELF32_R_TYPE (rel->r_info);
1382 if (r_type == R_CKCORE_PCREL_IMM26BY2
1383 && ((h != NULL
1384 && ((h->def_dynamic && !h->def_regular)
1385 || (bfd_link_pic (info)
1386 && h->root.type == bfd_link_hash_defweak)))
1387 || branch_offset > BSR_MAX_FWD_BRANCH_OFFSET
1388 || branch_offset < BSR_MAX_BWD_BRANCH_OFFSET))
1389 {
1390 if (bfd_csky_arch (info->output_bfd) == CSKY_ARCH_810
1391 || bfd_csky_arch (info->output_bfd) == CSKY_ARCH_807)
1392 stub_type = csky_stub_long_branch_jmpi;
1393 else
1394 stub_type = csky_stub_long_branch;
1395 }
1396
1397 return stub_type;
1398}
1399
1400/* Create an entry in an C-SKY ELF linker hash table. */
1401
1402static struct bfd_hash_entry *
1403csky_elf_link_hash_newfunc (struct bfd_hash_entry * entry,
1404 struct bfd_hash_table * table,
1405 const char * string)
1406{
1407 struct csky_elf_link_hash_entry * ret =
1408 (struct csky_elf_link_hash_entry *) entry;
1409
1410 /* Allocate the structure if it has not already been allocated by a
1411 subclass. */
1412 if (ret == NULL)
1413 {
1414 ret = (struct csky_elf_link_hash_entry *)
1415 bfd_hash_allocate (table,
1416 sizeof (struct csky_elf_link_hash_entry));
1417 if (ret == NULL)
1418 return (struct bfd_hash_entry *) ret;
1419 }
1420
1421 /* Call the allocation method of the superclass. */
1422 ret = ((struct csky_elf_link_hash_entry *)
1423 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret,
1424 table, string));
1425 if (ret != NULL)
1426 {
1427 struct csky_elf_link_hash_entry *eh;
1428
1429 eh = (struct csky_elf_link_hash_entry *) ret;
b8891f8d
AJ
1430 eh->plt_refcount = 0;
1431 eh->jsri2bsr_refcount = 0;
1432 eh->tls_type = GOT_NORMAL;
1433 ret->stub_cache = NULL;
1434 }
1435
1436 return (struct bfd_hash_entry *) ret;
1437}
1438
1439/* Initialize an entry in the stub hash table. */
1440
1441static struct bfd_hash_entry *
1442stub_hash_newfunc (struct bfd_hash_entry *entry,
1443 struct bfd_hash_table *table,
1444 const char *string)
1445{
1446 /* Allocate the structure if it has not already been allocated by a
1447 subclass. */
1448 if (entry == NULL)
1449 {
1450 entry = ((struct bfd_hash_entry *)
1451 bfd_hash_allocate (table,
1452 sizeof (struct elf32_csky_stub_hash_entry)));
1453 if (entry == NULL)
1454 return entry;
1455 }
1456
1457 /* Call the allocation method of the superclass. */
1458 entry = bfd_hash_newfunc (entry, table, string);
1459 if (entry != NULL)
1460 {
1461 struct elf32_csky_stub_hash_entry *eh;
1462
1463 /* Initialize the local fields. */
1464 eh = (struct elf32_csky_stub_hash_entry *) entry;
1465 eh->stub_sec = NULL;
1466 eh->stub_offset = 0;
1467 eh->target_value = 0;
1468 eh->target_section = NULL;
1469 eh->target_addend = 0;
1470 eh->stub_type = csky_stub_none;
1471 eh->stub_size = 0;
1472 eh->stub_template = NULL;
1473 eh->stub_template_size = -1;
1474 eh->h = NULL;
1475 eh->id_sec = NULL;
1476 eh->output_name = NULL;
1477 }
1478
1479 return entry;
1480}
1481
1482/* Free the derived linker hash table. */
1483
1484static void
1485csky_elf_link_hash_table_free (bfd *obfd)
1486{
1487 struct csky_elf_link_hash_table *ret
1488 = (struct csky_elf_link_hash_table *) obfd->link.hash;
1489
1490 bfd_hash_table_free (&ret->stub_hash_table);
1491 _bfd_elf_link_hash_table_free (obfd);
1492}
1493
1494/* Create an CSKY elf linker hash table. */
1495
1496static struct bfd_link_hash_table *
1497csky_elf_link_hash_table_create (bfd *abfd)
1498{
1499 struct csky_elf_link_hash_table *ret;
986f0783 1500 size_t amt = sizeof (struct csky_elf_link_hash_table);
b8891f8d
AJ
1501
1502 ret = (struct csky_elf_link_hash_table*) bfd_zmalloc (amt);
1503 if (ret == NULL)
1504 return NULL;
1505
1506 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1507 csky_elf_link_hash_newfunc,
1508 sizeof (struct csky_elf_link_hash_entry),
1509 CSKY_ELF_DATA))
1510 {
1511 free (ret);
1512 return NULL;
1513 }
1514
1515 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
1516 sizeof (struct elf32_csky_stub_hash_entry)))
1517 {
1518 free (ret);
1519 return NULL;
1520 }
1521 ret->elf.root.hash_table_free = csky_elf_link_hash_table_free;
1522 return &ret->elf.root;
1523}
1524
1525static bfd_boolean
1526csky_elf_mkobject (bfd *abfd)
1527{
1528 return bfd_elf_allocate_object (abfd, sizeof (struct csky_elf_obj_tdata),
1529 CSKY_ELF_DATA);
1530}
1531
1532/* Adjust a symbol defined by a dynamic object and referenced by a
1533 regular object. The current definition is in some section of the
1534 dynamic object, but we're not including those sections. We have to
1535 change the definition to something the rest of the link can
1536 understand. */
1537
1538static bfd_boolean
1539csky_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1540 struct elf_link_hash_entry *h)
1541{
1542 struct csky_elf_link_hash_entry *eh;
1543 struct csky_elf_link_hash_table *htab;
1544 asection *srel;
1545 asection *s;
1546 eh = (struct csky_elf_link_hash_entry *)h;
1547 if (eh == NULL)
1548 return FALSE;
1549
1550 htab = csky_elf_hash_table (info);
1551 if (htab == NULL)
1552 return FALSE;
1553
1554 /* Clear jsri2bsr_refcount, if creating shared library files. */
1555 if (bfd_link_pic (info) && eh->jsri2bsr_refcount > 0)
1556 eh->jsri2bsr_refcount = 0;
1557
1558 /* If there is a function, put it in the procedure linkage table. We
1559 will fill in the contents of the procedure linkage table later. */
1560 if (h->needs_plt)
1561 {
1562 /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
1563 symbol binds locally. */
1564 if (h->plt.refcount <= 0
1565 || (h->type != STT_GNU_IFUNC
1566 && (SYMBOL_CALLS_LOCAL (info, h)
1567 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1568 && h->root.type == bfd_link_hash_undefweak))))
1569
1570 {
1571 /* This case can occur if we saw a PLT32 reloc in an input
1572 file, but the symbol was never referred to by a dynamic
1573 object, or if all references were garbage collected. In
1574 such a case, we don't actually need to build a procedure
1575 linkage table, and we can just do a PC32 reloc instead. */
1576 h->plt.offset = (bfd_vma) -1;
1577 h->needs_plt = 0;
1578 if (h->got.refcount == 0)
1579 h->got.refcount += 1;
1580 }
1581 else if (h->got.refcount != 0)
1582 {
1583 h->got.refcount -= eh->plt_refcount;
1584 eh->plt_refcount = 0;
1585 }
1586 return TRUE;
1587 }
1588 else
1589 /* It's possible that we incorrectly decided a .plt reloc was
1590 needed for an R_CKCORE_PC32 or similar reloc to a non-function
1591 sym in check_relocs. We can't decide accurately between function
1592 and non-function syms in check_relocs; objects loaded later in
1593 the link may change h->type. So fix it now. */
1594 h->plt.offset = (bfd_vma) -1;
1595
1596 /* If this is a weak symbol, and there is a real definition, the
1597 processor independent code will have arranged for us to see the
1598 real definition first, and we can just use the same value. */
1599 if (h->is_weakalias)
1600 {
1601 struct elf_link_hash_entry *def = weakdef (h);
1602 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1603 h->root.u.def.section = def->root.u.def.section;
1604 h->root.u.def.value = def->root.u.def.value;
1605 return TRUE;
1606 }
1607
1608 /* If there are no non-GOT references, we do not need a copy
1609 relocation. */
1610 if (!h->non_got_ref)
1611 return TRUE;
1612
1613 /* This is a reference to a symbol defined by a dynamic object which
1614 is not a function. */
1615
1616 /* If we are creating a shared library, we must presume that the
1617 only references to the symbol are via the global offset table.
1618 For such cases we need not do anything here; the relocations will
1619 be handled correctly by relocate_section. */
1620 if (bfd_link_pic (info) || htab->elf.is_relocatable_executable)
1621 return TRUE;
1622
1623 /* We must allocate the symbol in our .dynbss section, which will
1624 become part of the .bss section of the executable. There will be
1625 an entry for this symbol in the .dynsym section. The dynamic
1626 object will contain position independent code, so all references
1627 from the dynamic object to this symbol will go through the global
1628 offset table. The dynamic linker will use the .dynsym entry to
1629 determine the address it must put in the global offset table, so
1630 both the dynamic object and the regular object will refer to the
1631 same memory location for the variable. */
1632 /* We must generate a R_CKCORE_COPY reloc to tell the dynamic linker to
1633 copy the initial value out of the dynamic object and into the
1634 runtime process image. We need to remember the offset into the
1635 .rela.bss section we are going to use. */
1636 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1637 {
1638 s = htab->elf.sdynrelro;
1639 srel = htab->elf.sreldynrelro;
1640 }
1641 else
1642 {
1643 s = htab->elf.sdynbss;
1644 srel = htab->elf.srelbss;
1645 }
1646 if (info->nocopyreloc == 0
1647 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1648 && h->size != 0
1649 && srel != NULL
1650 && s != NULL)
1651 {
1652 srel->size += sizeof (Elf32_External_Rela);
1653 h->needs_copy = 1;
1654 return _bfd_elf_adjust_dynamic_copy (info, h, s);
1655 }
1656
1657 h->non_got_ref = 0;
1658 return TRUE;
1659}
1660
1661/* Allocate space in .plt, .got and associated reloc sections for
1662 dynamic relocs. */
1663
1664static bfd_boolean
1665csky_allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1666{
1667 struct bfd_link_info *info;
1668 struct csky_elf_link_hash_table *htab;
1669 struct csky_elf_link_hash_entry *eh;
1670 struct elf_dyn_relocs *p;
1671
1672 /* For indirect case, such as _ZdlPv to _ZdlPv@@GLIBCXX_3.4. */
1673 if (h->root.type == bfd_link_hash_indirect)
1674 return TRUE;
1675
1676 if (h->root.type == bfd_link_hash_warning)
1677 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1678
1679
1680 info = (struct bfd_link_info *) inf;
1681 htab = csky_elf_hash_table (info);
1682 if (htab == NULL)
1683 return FALSE;
1684 /*TODO: how to deal with weak symbol relocs. */
1685 if ((htab->elf.dynamic_sections_created || h->type == STT_GNU_IFUNC)
1686 && h->plt.refcount > 0)
1687 {
1688 /* Make sure this symbol is output as a dynamic symbol.
1689 Undefined weak syms won't yet be marked as dynamic. */
1690 if (h->dynindx == -1 && !h->forced_local
1691 && h->root.type == bfd_link_hash_undefweak
1692 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1693 return FALSE;
1694 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1695 {
1696 asection *splt = htab->elf.splt;
1697
1698 /* If this is the first .plt entry, make room for the special
1699 first entry. */
1700 if (splt->size == 0)
1701 {
1702 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1703 splt->size += PLT_ENTRY_SIZE_P;
1704 else
1705 splt->size += PLT_ENTRY_SIZE;
1706 }
1707 h->plt.offset = splt->size;
1708
1709 /* If this symbol is not defined in a regular file, and we are
1710 not generating a shared library, then set the symbol to this
1711 location in the .plt. This is required to make function
1712 pointers compare as equal between the normal executable and
1713 the shared library. */
1714 if (!bfd_link_pic (info) && !h->def_regular)
1715 {
1716 h->root.u.def.section = splt;
1717 h->root.u.def.value = h->plt.offset;
1718 }
1719
1720 /* Make room for this entry. */
1721 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1722 splt->size += PLT_ENTRY_SIZE_P;
1723 else
1724 splt->size += PLT_ENTRY_SIZE;
1725 /* We also need to make an entry in the .rela.plt section. */
1726 htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
1727
1728 /* We also need to make an entry in the .got.plt section, which
1729 will be placed in the .got section by the linker script. */
1730 htab->elf.sgotplt->size += 4;
1731 }
1732 else
1733 {
1734 h->plt.offset = (bfd_vma) -1;
1735 h->needs_plt = 0;
1736 }
1737 }
1738 else
1739 {
1740 h->plt.offset = (bfd_vma) -1;
1741 h->needs_plt = 0;
1742 }
1743
1744 if (h->got.refcount > 0)
1745 {
1746 asection *sgot;
1747 bfd_boolean dyn;
1748 int indx;
1749
1750 int tls_type = csky_elf_hash_entry (h)->tls_type;
1751 /* Make sure this symbol is output as a dynamic symbol.
1752 Undefined weak syms won't yet be marked as dynamic. */
1753 if (h->dynindx == -1 && !h->forced_local
1754 && h->root.type == bfd_link_hash_undefweak
1755 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1756 return FALSE;
1757
1758 sgot = htab->elf.sgot;
1759 h->got.offset = sgot->size;
1760 BFD_ASSERT (tls_type != GOT_UNKNOWN);
1761 if (tls_type == GOT_NORMAL)
1762 /* Non-TLS symbols need one GOT slot. */
1763 sgot->size += 4;
1764 else
1765 {
1766 if (tls_type & GOT_TLS_GD)
1767 /* R_CKCORE_TLS_GD32 needs 2 consecutive GOT slots. */
1768 sgot->size += 8;
1769 if (tls_type & GOT_TLS_IE)
1770 /* R_CKCORE_TLS_IE32 needs one GOT slot. */
1771 sgot->size += 4;
1772 }
1773 dyn = htab->elf.dynamic_sections_created;
1774 indx = 0;
1775 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1776 && (! bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
1777 indx = h->dynindx;
1778
1779 if (tls_type != GOT_NORMAL
1780 && (bfd_link_pic (info) || indx != 0)
1781 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1782 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1783 || h->root.type != bfd_link_hash_undefweak))
1784 {
1785 if (tls_type & GOT_TLS_IE)
1786 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1787 if (tls_type & GOT_TLS_GD)
1788 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1789 if ((tls_type & GOT_TLS_GD) && indx != 0)
1790 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1791 }
1792 else if (((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1793 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1794 || h->root.type != bfd_link_hash_undefweak)
1795 && (bfd_link_pic (info)
1796 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
1797 || h->plt.offset == (bfd_vma) -1))
1798 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1799 }
1800 else
1801 h->got.offset = (bfd_vma) -1;
1802
1803 eh = (struct csky_elf_link_hash_entry *) h;
190eb1dd 1804 if (h->dyn_relocs == NULL)
b8891f8d
AJ
1805 return TRUE;
1806
1807 /* In the shared -Bsymbolic case, discard space allocated for
1808 dynamic pc-relative relocs against symbols which turn out to be
1809 defined in regular objects. For the normal shared case, discard
1810 space for pc-relative relocs that have become local due to symbol
1811 visibility changes. */
1812
1813 if (bfd_link_pic (info))
1814 {
1815 if (SYMBOL_CALLS_LOCAL (info, h))
1816 {
1817 struct elf_dyn_relocs **pp;
1818
190eb1dd 1819 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
b8891f8d
AJ
1820 {
1821 p->count -= p->pc_count;
1822 p->pc_count = 0;
1823 if (p->count == 0)
1824 *pp = p->next;
1825 else
1826 pp = &p->next;
1827 }
1828 }
1829
1830 if (eh->jsri2bsr_refcount
1831 && h->root.type == bfd_link_hash_defined
190eb1dd
L
1832 && h->dyn_relocs != NULL)
1833 h->dyn_relocs->count -= eh->jsri2bsr_refcount;
b8891f8d
AJ
1834
1835 /* Also discard relocs on undefined weak syms with non-default
1836 visibility. */
190eb1dd 1837 if (h->dyn_relocs != NULL
b8891f8d
AJ
1838 && h->root.type == bfd_link_hash_undefweak)
1839 {
1840 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1841 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 1842 h->dyn_relocs = NULL;
b8891f8d
AJ
1843
1844 /* Make sure undefined weak symbols are output as a dynamic
1845 symbol in PIEs. */
1846 else if (h->dynindx == -1
1847 && !h->forced_local
1848 && !bfd_elf_link_record_dynamic_symbol (info, h))
1849 return FALSE;
1850 }
1851
1852 }
1853 else
1854 {
1855 /* For the non-shared case, discard space for relocs against
1856 symbols which turn out to need copy relocs or are not
1857 dynamic. */
1858
1859 if (!h->non_got_ref
1860 && ((h->def_dynamic && !h->def_regular)
1861 || (htab->elf.dynamic_sections_created
1862 && (h->root.type == bfd_link_hash_undefweak
1863 || h->root.type == bfd_link_hash_indirect
1864 || h->root.type == bfd_link_hash_undefined))))
1865 {
1866 /* Make sure this symbol is output as a dynamic symbol.
1867 Undefined weak syms won't yet be marked as dynamic. */
1868 if (h->dynindx == -1 && !h->forced_local
1869 && h->root.type == bfd_link_hash_undefweak)
1870 {
1871 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1872 return FALSE;
1873 }
1874
1875 /* If that succeeded, we know we'll be keeping all the
1876 relocs. */
1877 if (h->dynindx != -1)
1878 goto keep;
1879 }
1880
190eb1dd 1881 h->dyn_relocs = NULL;
b8891f8d
AJ
1882
1883 keep: ;
1884 }
1885
1886 /* Finally, allocate space. */
190eb1dd 1887 for (p = h->dyn_relocs; p != NULL; p = p->next)
b8891f8d
AJ
1888 {
1889 asection *srelgot = htab->elf.srelgot;
1890 srelgot->size += p->count * sizeof (Elf32_External_Rela);
1891 }
1892
1893 return TRUE;
1894}
1895
b8891f8d
AJ
1896/* Set DF_TEXTREL if we find any dynamic relocs that apply to
1897 read-only sections. */
1898
1899static bfd_boolean
1900maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
1901{
1902 asection *sec;
1903
1904 if (h->root.type == bfd_link_hash_indirect)
1905 return TRUE;
1906
5dbc8b37 1907 sec = _bfd_elf_readonly_dynrelocs (h);
b8891f8d
AJ
1908 if (sec != NULL)
1909 {
1910 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
1911
1912 info->flags |= DF_TEXTREL;
1913 info->callbacks->minfo
1914 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1915 sec->owner, h->root.root.string, sec);
1916
1917 /* Not an error, just cut short the traversal. */
1918 return FALSE;
1919 }
1920 return TRUE;
1921}
1922
1923/* Set the sizes of the dynamic sections. */
1924
1925static bfd_boolean
1926csky_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1927 struct bfd_link_info *info)
1928{
1929 struct csky_elf_link_hash_table *htab;
1930 bfd *dynobj;
1931 asection *s;
1932 bfd_boolean relocs;
1933 bfd *ibfd;
1934
1935 htab = csky_elf_hash_table (info);
1936 if (htab == NULL)
1937 return FALSE;
1938 dynobj = htab->elf.dynobj;
1939 if (dynobj == NULL)
1940 return FALSE;
1941
1942 if (htab->elf.dynamic_sections_created)
1943 {
1944 /* Set the contents of the .interp section to the interpreter. */
1945 if (!bfd_link_pic (info) && !info->nointerp)
1946 {
1947 s = bfd_get_section_by_name (dynobj, ".interp");
1948 BFD_ASSERT (s != NULL);
1949 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1950 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1951 }
1952 }
1953
1954 /* Set up .got offsets for local syms, and space for local dynamic
1955 relocs. */
1956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1957 {
1958 bfd_signed_vma *local_got_refcounts;
1959 bfd_signed_vma *end_local_got;
1960 bfd_size_type locsymcount;
1961 Elf_Internal_Shdr *symtab_hdr;
1962 asection *srelgot, *sgot;
1963 char *local_tls_type;
1964
1965 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1966 continue;
1967
1968 sgot = htab->elf.sgot;
1969 srelgot = htab->elf.srelgot;
1970
1971 for (s = ibfd->sections; s != NULL; s = s->next)
1972 {
1973 struct elf_dyn_relocs *p;
1974
1975 for (p = *((struct elf_dyn_relocs **)
1976 &elf_section_data (s)->local_dynrel);
1977 p != NULL;
1978 p = p->next)
1979 {
1980 if (!bfd_is_abs_section (p->sec)
1981 && bfd_is_abs_section (p->sec->output_section))
1982 /* Input section has been discarded, either because
1983 it is a copy of a linkonce section or due to
1984 linker script /DISCARD/, so we'll be discarding
1985 the relocs too. */
1986 ;
1987 else if (p->count != 0)
1988 {
1989 srelgot->size += p->count * sizeof (Elf32_External_Rela);
1990 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1991 info->flags |= DF_TEXTREL;
1992 }
1993 }
1994 }
1995
1996 local_got_refcounts = elf_local_got_refcounts (ibfd);
1997 if (!local_got_refcounts)
1998 continue;
1999
2000 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2001 locsymcount = symtab_hdr->sh_info;
2002 end_local_got = local_got_refcounts + locsymcount;
2003 local_tls_type = csky_elf_local_got_tls_type (ibfd);
2004
2005 for (; local_got_refcounts < end_local_got;
2006 ++local_got_refcounts, ++local_tls_type)
2007 {
2008 if (*local_got_refcounts > 0)
2009 {
2010 /* GOT_TLS_GD and GOT_TLS_IE type for TLS, GOT_NORMAL type
2011 for GOT. If output file is shared library, we should output
2012 GOT_TLS_GD type relocation in .rel.got. */
2013 *local_got_refcounts = sgot->size;
2014 if (*local_tls_type & GOT_TLS_GD)
2015 /* TLS_GD relocs need an 8-byte structure in the GOT. */
2016 sgot->size += 8;
2017 if (*local_tls_type & GOT_TLS_IE)
2018 sgot->size += 4;
2019 if (*local_tls_type == GOT_NORMAL)
2020 sgot->size += 4;
2021 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
2022 srelgot->size += sizeof (Elf32_External_Rela);
2023 }
2024 else
2025 *local_got_refcounts = (bfd_vma) -1;
2026 }
2027 }
2028
2029 if (htab->tls_ldm_got.refcount > 0)
2030 {
2031 /* Allocate two GOT entries and one dynamic relocation (if necessary)
2032 for R_CSKY_TLS_LDM32 relocations. */
2033 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2034 htab->elf.sgot->size += 8;
2035 if (bfd_link_pic (info))
2036 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
2037 }
2038 else
2039 htab->tls_ldm_got.offset = -1;
2040
2041 /* Allocate global sym .plt and .got entries, and space for global
2042 sym dynamic relocs. */
2043 elf_link_hash_traverse (&htab->elf, csky_allocate_dynrelocs, (PTR) info);
2044
2045 /* Check for GOT overflow. */
2046 if (check_got_overflow == 1
2047 && htab->elf.sgot->size + htab->elf.sgotplt->size > GOT_MAX_SIZE)
2048 {
2049 _bfd_error_handler (_("GOT table size out of range")); /* */
2050 return FALSE;
2051 }
2052
2053 /* We now have determined the sizes of the various dynamic sections.
2054 Allocate memory for them. */
2055 relocs = FALSE;
2056 for (s = dynobj->sections; s != NULL; s = s->next)
2057 {
2058 bfd_boolean strip_section = TRUE;
2059
2060 if ((s->flags & SEC_LINKER_CREATED) == 0)
2061 continue;
2062
2063 if (s == htab->elf.splt
2064 || s == htab->elf.sgot
2065 || s == htab->elf.sgotplt
2066 || s == htab->elf.sdynrelro
2067 || s == htab->elf.sreldynrelro)
2068 {
2069 /* Strip this section if we don't need it;
2070 see the comment below. */
2071 /* We'd like to strip these sections if they aren't needed, but if
2072 we've exported dynamic symbols from them we must leave them.
2073 It's too late to tell BFD to get rid of the symbols. */
2074
2075 if (htab->elf.hplt != NULL)
2076 strip_section = FALSE;
2077 }
fd361982 2078 else if (CONST_STRNEQ (bfd_section_name (s), ".rel") )
b8891f8d
AJ
2079 {
2080 if (s->size != 0 )
2081 relocs = TRUE;
2082
2083 /* We use the reloc_count field as a counter if we need
2084 to copy relocs into the output file. */
2085 s->reloc_count = 0;
2086 }
2087 else
2088 /* It's not one of our sections, so don't allocate space. */
2089 continue;
2090
2091 /* Strip this section if we don't need it; see the
2092 comment below. */
2093 if (s->size == 0)
2094 {
2095 /* If we don't need this section, strip it from the
2096 output file. This is mostly to handle .rel.bss and
2097 .rel.plt. We must create both sections in
2098 create_dynamic_sections, because they must be created
2099 before the linker maps input sections to output
2100 sections. The linker does that before
2101 adjust_dynamic_symbol is called, and it is that
2102 function which decides whether anything needs to go
2103 into these sections. */
2104 if (strip_section)
2105 s->flags |= SEC_EXCLUDE;
2106 continue;
2107 }
2108
2109 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2110 continue;
2111
2112 /* Allocate memory for the section contents. We use bfd_zalloc
2113 here in case unused entries are not reclaimed before the
2114 section's contents are written out. This should not happen,
2115 but this way if it does, we get a R_CKCORE_NONE reloc instead
2116 of garbage. */
2117 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2118 if (s->contents == NULL)
2119 return FALSE;
2120 }
2121
2122 if (htab->elf.dynamic_sections_created)
2123 {
2124 /* Add some entries to the .dynamic section. We fill in the
2125 values later, in csky_elf_finish_dynamic_sections, but we
2126 must add the entries now so that we get the correct size for
2127 the .dynamic section. The DT_DEBUG entry is filled in by the
2128 dynamic linker and used by the debugger. */
2129#define add_dynamic_entry(TAG, VAL) \
2130 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2131
2132 if (bfd_link_executable (info) && !add_dynamic_entry (DT_DEBUG, 0))
2133 return FALSE;
2134
2135 if (htab->elf.sgot->size != 0 || htab->elf.splt->size)
2136 {
2137 if (!add_dynamic_entry (DT_PLTGOT, 0)
2138 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2139 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2140 || !add_dynamic_entry (DT_JMPREL, 0))
2141 return FALSE;
2142 }
2143
2144 if (relocs)
2145 {
2146 if (!add_dynamic_entry (DT_RELA, 0)
2147 || !add_dynamic_entry (DT_RELASZ, 0)
2148 || !add_dynamic_entry (DT_RELAENT,
2149 sizeof (Elf32_External_Rela)))
2150 return FALSE;
2151
2152 /* If any dynamic relocs apply to a read-only section,
2153 then we need a DT_TEXTREL entry. */
2154 if ((info->flags & DF_TEXTREL) == 0)
2155 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
2156
2157 if ((info->flags & DF_TEXTREL) != 0
2158 && !add_dynamic_entry (DT_TEXTREL, 0))
2159 return FALSE;
2160 }
2161 }
2162#undef add_dynamic_entry
2163
2164 return TRUE;
2165}
2166
2167/* Finish up dynamic symbol handling. We set the contents of various
2168 dynamic sections here. */
2169
2170static bfd_boolean
2171csky_elf_finish_dynamic_symbol (bfd *output_bfd,
2172 struct bfd_link_info *info,
2173 struct elf_link_hash_entry *h,
2174 Elf_Internal_Sym *sym)
2175{
2176 struct csky_elf_link_hash_table *htab;
2177
2178 htab = csky_elf_hash_table (info);
2179 if (htab == NULL)
2180 return FALSE;
2181
2182 /* Sanity check to make sure no unexpected symbol reaches here.
2183 This matches the test in csky_elf_relocate_section handling
2184 of GOT/PLT entries. */
2185 BFD_ASSERT (! (h->dynindx == -1
2186 && !h->forced_local
2187 && h->root.type != bfd_link_hash_undefweak
2188 && bfd_link_pic (info)));
2189
2190 if (h->plt.offset != (bfd_vma) -1)
2191 {
2192 bfd_vma plt_index;
2193 bfd_vma got_offset;
2194 Elf_Internal_Rela rel;
2195 bfd_byte *loc;
2196 asection *plt, *relplt, *gotplt;
2197
2198 plt = htab->elf.splt;
2199 relplt = htab->elf.srelplt;
2200 gotplt = htab->elf.sgotplt;
2201
2202 /* This symbol has an entry in the procedure linkage table. Set
2203 it up. */
2204 BFD_ASSERT (h->dynindx != -1
2205 || ((h->forced_local || bfd_link_executable (info))
2206 && h->def_regular));
2207 BFD_ASSERT (plt != NULL && gotplt != NULL && relplt != NULL);
2208 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2209 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2210 else
2211 plt_index = h->plt.offset / PLT_ENTRY_SIZE_P - 1;
2212 got_offset = (plt_index + 3) * 4;
2213
2214 /* Fill in the entry in the procedure linkage table. */
2215 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2216 {
2217 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[0],
2218 plt->contents + h->plt.offset);
2219 csky_put_insn_32 (output_bfd,
2220 (csky_elf_plt_entry_v2[1] | plt_index),
2221 plt->contents + h->plt.offset + 4);
2222 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[2],
2223 plt->contents + h->plt.offset + 8);
2224 }
2225 else
2226 {
2227 int i;
2228 for (i = 0; i < 6; i++)
2229 bfd_put_16 (output_bfd, csky_elf_plt_entry_v1[i],
2230 plt->contents + h->plt.offset + i * 2);
2231 bfd_put_32 (output_bfd, plt_index,
2232 plt->contents + h->plt.offset + i * 2);
2233 }
2234
2235 /* Fill in the entry in the .rel.plt section. */
2236 rel.r_offset = (htab->elf.sgotplt->output_section->vma
2237 + htab->elf.sgotplt->output_offset
2238 + got_offset);
2239 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_JUMP_SLOT);
2240 rel.r_addend = (plt->output_section->vma
2241 + plt->output_offset
2242 + h->plt.offset);
2243 loc = (htab->elf.srelplt->contents
2244 + plt_index * sizeof (Elf32_External_Rela));
2245
2246 if (loc != NULL)
2247 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2248 if (! h->def_regular)
2249 {
2250 /* Mark the symbol as undefined, rather than as defined in
2251 the .plt section. Leave the value alone. */
2252 sym->st_shndx = SHN_UNDEF;
2253 /* If the symbol is weak, we do need to clear the value.
2254 Otherwise, the PLT entry would provide a definition for
2255 the symbol even if the symbol wasn't defined anywhere,
2256 and so the symbol would never be NULL. Leave the value if
2257 there were any relocations where pointer equality matters
2258 (this is a clue for the dynamic linker, to make function
2259 pointer comparisons work between an application and shared
2260 library). */
2261 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
2262 sym->st_value = 0;
2263 }
2264 }
2265
2266 /* Fill in the entry in the .got section. */
2267 if (h->got.offset != (bfd_vma) -1
2268 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_GD) == 0)
2269 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_IE) == 0))
2270 {
2271 Elf_Internal_Rela rel;
2272 bfd_byte *loc;
2273
2274 /* This symbol has an entry in the global offset table.
2275 Set it up. */
2276 BFD_ASSERT (htab->elf.sgot != NULL && htab->elf.srelgot != NULL);
2277
2278 rel.r_offset = (htab->elf.sgot->output_section->vma
2279 + htab->elf.sgot->output_offset
2280 + (h->got.offset & ~(bfd_vma) 1));
2281
2282 /* If this is a static link, or it is a -Bsymbolic link and the
2283 symbol is defined locally or was forced to be local because
2284 of a version file, we just want to emit a RELATIVE reloc.
2285 The entry in the global offset table will already have been
2286 initialized in the relocate_section function. */
2287 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
2288 {
2289 BFD_ASSERT ((h->got.offset & 1) != 0);
2290 rel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
2291 rel.r_addend = (h->root.u.def.value
2292 + h->root.u.def.section->output_offset
2293 + h->root.u.def.section->output_section->vma);
2294 }
2295 else
2296 {
2297 BFD_ASSERT ((h->got.offset & 1) == 0);
2298 bfd_put_32 (output_bfd, (bfd_vma) 0,
2299 htab->elf.sgot->contents + h->got.offset);
2300 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_GLOB_DAT);
2301 rel.r_addend = 0;
2302 }
2303
2304 loc = htab->elf.srelgot->contents;
2305 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2306
2307 if (loc != NULL)
2308 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2309 }
2310
2311 if (h->needs_copy)
2312 {
2313 asection *s;
2314 Elf_Internal_Rela rela;
2315 bfd_byte *loc;
2316
2317 /* This symbol needs a copy reloc. Set it up. */
2318 BFD_ASSERT (h->dynindx != -1
2319 && (h->root.type == bfd_link_hash_defined
2320 || h->root.type == bfd_link_hash_defweak));
2321
2322 rela.r_offset = (h->root.u.def.value
2323 + h->root.u.def.section->output_section->vma
2324 + h->root.u.def.section->output_offset);
2325 rela.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_COPY);
2326 rela.r_addend = 0;
2327 if (h->root.u.def.section == htab->elf.sdynrelro)
2328 s = htab->elf.sreldynrelro;
2329 else
2330 s = htab->elf.srelbss;
2331 BFD_ASSERT (s != NULL);
2332 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2333 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2334 }
2335
2336 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2337 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2338 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2339 sym->st_shndx = SHN_ABS;
2340
2341 return TRUE;
2342}
2343
2344/* Finish up the dynamic sections. */
2345
2346static bfd_boolean
2347csky_elf_finish_dynamic_sections (bfd *output_bfd,
2348 struct bfd_link_info *info)
2349{
2350 struct csky_elf_link_hash_table *htab;
2351 bfd *dynobj;
2352 asection *sdyn;
2353 asection *got_sec;
2354
2355 htab = csky_elf_hash_table (info);
2356 if (htab == NULL)
2357 return FALSE;
2358
2359 dynobj = htab->elf.dynobj;
2360 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2361
2362 if (htab->elf.dynamic_sections_created)
2363 {
2364 Elf32_External_Dyn *dyncon, *dynconend;
2365
2366 BFD_ASSERT (sdyn != NULL && htab->elf.sgot != NULL);
2367
2368 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2369 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2370 for (; dyncon < dynconend; dyncon++)
2371 {
2372 Elf_Internal_Dyn dyn;
2373 bfd_boolean size = FALSE;
2374 const char *name = NULL;
2375
2376 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2377 switch (dyn.d_tag)
2378 {
2379 default:
2380 continue;
2381 case DT_RELA:
2382 name = ".rela.dyn";
2383 size = FALSE;
2384 break;
2385 case DT_RELASZ:
2386 name = ".rela.dyn";
2387 size = TRUE;
2388 break;
2389 case DT_PLTRELSZ:
2390 name = ".rela.plt";
2391 size = TRUE;
2392 break;
2393 case DT_PLTGOT:
2394 dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
2395 break;
2396 case DT_JMPREL:
2397 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma
2398 + htab->elf.srelplt->output_offset;
2399 break;
2400 }
2401
2402 if (name != NULL)
2403 {
2404 asection *s = bfd_get_section_by_name (output_bfd, name);
2405
2406 if (s == NULL)
2407 dyn.d_un.d_val = 0;
2408 else if (!size)
2409 dyn.d_un.d_ptr = s->vma;
2410 else
2411 dyn.d_un.d_val = s->size;
2412 }
2413 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2414 }
2415 }
2416
2417 /* Fill in the first three entries in the global offset table. */
2418 if (htab->elf.sgotplt)
2419 got_sec = htab->elf.sgotplt;
2420 else
2421 got_sec = htab->elf.sgot;
2422 if (got_sec != NULL)
2423 {
2424 if (got_sec->size > 0)
2425 {
2426 bfd_put_32 (output_bfd,
2427 (sdyn == NULL ? (bfd_vma) 0
2428 : sdyn->output_section->vma + sdyn->output_offset),
2429 got_sec->contents);
2430 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 4);
2431 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 8);
2432 }
2433 elf_section_data (got_sec->output_section)->this_hdr.sh_entsize = 4;
2434 }
2435 return TRUE;
2436}
2437
2438/* Copy the extra info we tack onto an elf_link_hash_entry. */
2439
2440static void
2441csky_elf_copy_indirect_symbol (struct bfd_link_info *info,
2442 struct elf_link_hash_entry *dir,
2443 struct elf_link_hash_entry *ind)
2444{
2445 struct csky_elf_link_hash_entry *edir, *eind;
2446
2447 edir = (struct csky_elf_link_hash_entry *) dir;
2448 eind = (struct csky_elf_link_hash_entry *) ind;
2449
190eb1dd 2450 if (ind->dyn_relocs != NULL)
b8891f8d 2451 {
190eb1dd 2452 if (dir->dyn_relocs != NULL)
b8891f8d
AJ
2453 {
2454 struct elf_dyn_relocs **pp;
2455 struct elf_dyn_relocs *p;
2456
2457 /* Add reloc counts against the indirect sym to the direct sym
2458 list. Merge any entries against the same section. */
190eb1dd 2459 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
b8891f8d
AJ
2460 {
2461 struct elf_dyn_relocs *q;
2462
190eb1dd 2463 for (q = dir->dyn_relocs; q != NULL; q = q->next)
b8891f8d
AJ
2464 if (q->sec == p->sec)
2465 {
2466 q->pc_count += p->pc_count;
2467 q->count += p->count;
2468 *pp = p->next;
2469 break;
2470 }
2471 if (q == NULL)
2472 pp = &p->next;
2473 }
190eb1dd 2474 *pp = dir->dyn_relocs;
b8891f8d 2475 }
190eb1dd
L
2476 dir->dyn_relocs = ind->dyn_relocs;
2477 ind->dyn_relocs = NULL;
b8891f8d
AJ
2478 }
2479 if (ind->root.type == bfd_link_hash_indirect
2480 && dir->got.refcount <= 0)
2481 {
2482 edir->tls_type = eind->tls_type;
2483 eind->tls_type = GOT_UNKNOWN;
2484 }
2485 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2486}
2487
2488/* Used to decide how to sort relocs in an optimal manner for the
2489 dynamic linker, before writing them out. */
2490
2491static enum elf_reloc_type_class
2492csky_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2493 const asection *rel_sec ATTRIBUTE_UNUSED,
2494 const Elf_Internal_Rela *rela)
2495{
2496 switch ((int) ELF32_R_TYPE (rela->r_info))
2497 {
2498 case R_CKCORE_RELATIVE:
2499 return reloc_class_relative;
2500 case R_CKCORE_JUMP_SLOT:
2501 return reloc_class_plt;
2502 case R_CKCORE_COPY:
2503 return reloc_class_copy;
2504 case R_CKCORE_IRELATIVE:
2505 return reloc_class_ifunc;
2506 default:
2507 return reloc_class_normal;
2508 }
2509}
2510
2511/* Return the section that should be marked against GC for a given
2512 relocation. */
2513
2514static asection *
2515csky_elf_gc_mark_hook (asection *sec,
2516 struct bfd_link_info *info,
2517 Elf_Internal_Rela *rel,
2518 struct elf_link_hash_entry *h,
2519 Elf_Internal_Sym *sym)
2520{
2521 if (h != NULL)
2522 {
2523 switch (ELF32_R_TYPE (rel->r_info))
2524 {
2525 case R_CKCORE_GNU_VTINHERIT:
2526 case R_CKCORE_GNU_VTENTRY:
2527 return NULL;
2528 }
2529 }
2530
2531 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2532}
2533
2534/* Look through the relocs for a section during the first phase.
2535 Since we don't do .gots or .plts, we just need to consider the
2536 virtual table relocs for gc. */
2537
2538static bfd_boolean
2539csky_elf_check_relocs (bfd * abfd,
2540 struct bfd_link_info * info,
2541 asection * sec,
2542 const Elf_Internal_Rela * relocs)
2543{
2544 Elf_Internal_Shdr * symtab_hdr;
2545 struct elf_link_hash_entry ** sym_hashes;
2546 const Elf_Internal_Rela * rel;
2547 const Elf_Internal_Rela * rel_end;
2548 struct csky_elf_link_hash_table *htab;
2549 asection *sreloc;
2550
2551 /* if output type is relocatable, return. */
2552 if (bfd_link_relocatable (info))
2553 return TRUE;
2554
2555 htab = csky_elf_hash_table (info);
2556 if (htab == NULL)
2557 return FALSE;
2558
2559 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2560 sym_hashes = elf_sym_hashes (abfd);
2561
2562 rel_end = relocs + sec->reloc_count;
2563 sreloc = NULL;
2564 for (rel = relocs; rel < rel_end; rel++)
2565 {
2566 struct elf_link_hash_entry *h;
2567 unsigned long r_symndx;
2568 Elf_Internal_Sym *isym;
2569 int r_type;
2570
2571 r_symndx = ELF32_R_SYM (rel->r_info);
2572 r_type = ELF32_R_TYPE (rel->r_info);
2573 if (r_symndx < symtab_hdr->sh_info)
2574 {
2575 /* A local symbol. */
2576 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2577 abfd, r_symndx);
2578 if (isym == NULL)
2579 return FALSE;
2580 h = NULL;
2581 }
2582 else
2583 {
2584 isym = NULL;
2585 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2586 while (h->root.type == bfd_link_hash_indirect
2587 || h->root.type == bfd_link_hash_warning)
2588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2589 }
2590
2591 switch (r_type)
2592 {
2593 case R_CKCORE_PCREL_IMM26BY2:
2594 case R_CKCORE_PCREL_IMM11BY2:
2595 case R_CKCORE_PCREL_JSR_IMM11BY2:
2596 case R_CKCORE_PCREL_JSR_IMM26BY2:
2597 /* If the symbol is '*UND*', means this reloc is used for
2598 * callgraph, don't need to leave to shared object. */
2599 if (r_symndx == 0)
2600 break;
2601 /* Else fall through. */
2602 case R_CKCORE_ADDR32:
2603 case R_CKCORE_ADDR_HI16:
2604 case R_CKCORE_ADDR_LO16:
2605 if (h != NULL
2606 && bfd_link_executable (info)
2607 && r_type == R_CKCORE_ADDR32
2608 && h->type == STT_OBJECT
2609 && (sec->flags & SEC_ALLOC) != 0
2610 && (sec->flags & SEC_READONLY))
2611 /* If this reloc is in a read-only section, we might
2612 need a copy reloc. We can't check reliably at this
2613 stage whether the section is read-only, as input
2614 sections have not yet been mapped to output sections.
2615 Tentatively set the flag for now, and correct in
2616 adjust_dynamic_symbol. */
2617 h->non_got_ref = 1;
2618
2619 /* If we are creating a shared library or relocatable executable,
2620 and this is a reloc against a global symbol, then we need to
2621 copy the reloc into the shared library. However, if we are
2622 linking with -Bsymbolic, we do not need to copy a reloc
2623 against a global symbol which is defined in an object we are
2624 including in the link (i.e., DEF_REGULAR is set). At
2625 this point we have not seen all the input files, so it is
2626 possible that DEF_REGULAR is not set now but will be set
2627 later (it is never cleared). We account for that possibility
2628 below by storing information in the relocs_copied field of
2629 the hash table entry. */
2630 if ((bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
2631 || (!bfd_link_pic (info)
2632 && (sec->flags & SEC_ALLOC) != 0
2633 && h != NULL
2634 && (h->root.type == bfd_link_hash_defweak
2635 || !h->def_regular)))
2636 {
2637 struct elf_dyn_relocs *p;
2638 struct elf_dyn_relocs **head;
2639 /* We must copy these reloc types into the output file.
2640 Create a reloc section in dynobj and make room for
2641 this reloc. */
2642 if (sreloc == NULL)
2643 {
2644 if (htab->elf.dynobj == NULL)
2645 htab->elf.dynobj = abfd;
2646
2647 sreloc = _bfd_elf_make_dynamic_reloc_section
2648 (sec, htab->elf.dynobj, 2, abfd, TRUE);
2649
2650 if (sreloc == NULL)
2651 return FALSE;
2652 }
2653
2654 if (h == NULL && !use_branch_stub
2655 && ((ELF32_R_TYPE (rel->r_info)
2656 == R_CKCORE_PCREL_IMM26BY2)
2657 || (ELF32_R_TYPE (rel->r_info)
2658 == R_CKCORE_PCREL_IMM11BY2)))
2659 break;
2660
2661 /* If this is a global symbol, we count the number of
2662 relocations we need for this symbol. */
2663 if (h != NULL)
2664 {
2665 struct csky_elf_link_hash_entry *eh;
2666 eh = (struct csky_elf_link_hash_entry *)h;
2667 if ((ELF32_R_TYPE (rel->r_info)
2668 == R_CKCORE_PCREL_JSR_IMM26BY2)
2669 || (ELF32_R_TYPE (rel->r_info)
2670 == R_CKCORE_PCREL_JSR_IMM11BY2))
2671 eh->jsri2bsr_refcount += 1;
190eb1dd 2672 head = &h->dyn_relocs;
b8891f8d
AJ
2673 }
2674 else
2675 {
2676 /* Track dynamic relocs needed for local syms too.
2677 We really need local syms available to do this
2678 easily. Oh well. */
2679 void **vpp;
2680 asection *s;
2681 Elf_Internal_Sym *loc_isym;
2682
2683 loc_isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2684 abfd, r_symndx);
2685 if (loc_isym == NULL)
2686 return FALSE;
2687 s = bfd_section_from_elf_index (abfd, loc_isym->st_shndx);
2688 if (s == NULL)
2689 s = sec;
2690 vpp = &elf_section_data (s)->local_dynrel;
2691 head = (struct elf_dyn_relocs **)vpp;
2692 }
2693
2694 p = *head;
2695 if (p == NULL || p->sec != sec)
2696 {
986f0783 2697 size_t amt = sizeof *p;
b8891f8d
AJ
2698 p = ((struct elf_dyn_relocs *)
2699 bfd_alloc (htab->elf.dynobj, amt));
2700 if (p == NULL)
2701 return FALSE;
2702 p->next = *head;
2703 *head = p;
2704 p->sec = sec;
2705 p->count = 0;
2706 p->pc_count = 0;
2707 }
2708
2709 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2
2710 || ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM11BY2)
2711 p->pc_count += 1;
2712 p->count += 1;
2713 }
2714 break;
2715
2716 case R_CKCORE_PLT_IMM18BY4:
2717 case R_CKCORE_PLT32:
2718 /* This symbol requires a procedure linkage table entry. We
2719 actually build the entry in adjust_dynamic_symbol,
2720 because this might be a case of linking PIC code which is
2721 never referenced by a dynamic object, in which case we
2722 don't need to generate a procedure linkage table entry
2723 after all. */
2724
2725 /* If this is a local symbol, we resolve it directly without
2726 creating a procedure linkage table entry. */
2727 if (h == NULL)
2728 continue;
2729 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PLT_IMM18BY4)
2730 check_got_overflow = 1;
2731
2732 h->needs_plt = 1;
2733 h->plt.refcount += 1;
2734 h->got.refcount += 1;
2735 ((struct csky_elf_link_hash_entry *)h)->plt_refcount += 1;
2736 break;
2737
2738 case R_CKCORE_GOT12:
2739 case R_CKCORE_PLT12:
2740 case R_CKCORE_GOT32:
2741 case R_CKCORE_GOT_HI16:
2742 case R_CKCORE_GOT_LO16:
2743 case R_CKCORE_PLT_HI16:
2744 case R_CKCORE_PLT_LO16:
2745 case R_CKCORE_GOT_IMM18BY4:
2746 case R_CKCORE_TLS_IE32:
2747 case R_CKCORE_TLS_GD32:
2748 {
2749 int tls_type, old_tls_type;
2750
2751 if (h != NULL
2752 && bfd_link_executable (info)
2753 && r_type == R_CKCORE_GOT_IMM18BY4
2754 && (sec->flags & SEC_ALLOC) != 0
2755 && (sec->flags & SEC_READONLY))
2756 /* If this reloc is in a read-only section, we might
2757 need a copy reloc. We can't check reliably at this
2758 stage whether the section is read-only, as input
2759 sections have not yet been mapped to output sections.
2760 Tentatively set the flag for now, and correct in
2761 adjust_dynamic_symbol. */
2762 h->non_got_ref = 1;
2763
2764 switch (ELF32_R_TYPE (rel->r_info))
2765 {
2766 case R_CKCORE_TLS_IE32:
2767 tls_type = GOT_TLS_IE;
2768 break;
2769 case R_CKCORE_TLS_GD32:
2770 tls_type = GOT_TLS_GD;
2771 break;
2772 default:
2773 tls_type = GOT_NORMAL;
2774 break;
2775 }
2776 if (h != NULL)
2777 {
2778 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_GOT_IMM18BY4)
2779 check_got_overflow = 1;
2780 h->got.refcount += 1;
2781 old_tls_type = csky_elf_hash_entry (h)->tls_type;
2782 }
2783 else
2784 {
2785 bfd_signed_vma *local_got_refcounts;
2786
2787 /* This is a global offset table entry for a local symbol. */
2788 /* we can write a new function named
2789 elf32_csky_allocate_local_sym_info() to replace
2790 following code. */
2791 local_got_refcounts = elf_local_got_refcounts (abfd);
2792 if (local_got_refcounts == NULL)
2793 {
2794 bfd_size_type size;
2795
2796 size = symtab_hdr->sh_info;
2797 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2798 local_got_refcounts = ((bfd_signed_vma *)
2799 bfd_zalloc (abfd, size));
2800 if (local_got_refcounts == NULL)
2801 return FALSE;
2802 elf_local_got_refcounts (abfd) = local_got_refcounts;
2803 csky_elf_local_got_tls_type (abfd)
2804 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2805 }
2806 local_got_refcounts[r_symndx] += 1;
2807 old_tls_type = csky_elf_local_got_tls_type (abfd)[r_symndx];
2808 }
2809
2810 /* We will already have issued an error message if there is a
2811 TLS / non-TLS mismatch, based on the symbol type. We don't
2812 support any linker relaxations. So just combine any TLS
2813 types needed. */
2814 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2815 && tls_type != GOT_NORMAL)
2816 tls_type |= old_tls_type;
2817
2818 if (old_tls_type != tls_type)
2819 {
2820 if (h != NULL)
2821 csky_elf_hash_entry (h)->tls_type = tls_type;
2822 else
2823 csky_elf_local_got_tls_type (abfd)[r_symndx] = tls_type;
2824 }
2825 }
2826 /* Fall through. */
2827
2828 case R_CKCORE_TLS_LDM32:
2829 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_TLS_LDM32)
2830 htab->tls_ldm_got.refcount++;
2831 /* Fall through. */
2832
2833 case R_CKCORE_GOTOFF:
2834 case R_CKCORE_GOTPC:
2835 case R_CKCORE_GOTOFF_HI16:
2836 case R_CKCORE_GOTOFF_LO16:
2837 case R_CKCORE_GOTPC_HI16:
2838 case R_CKCORE_GOTPC_LO16:
2839 case R_CKCORE_GOTOFF_IMM18:
2840 if (htab->elf.sgot == NULL)
2841 {
2842 if (htab->elf.dynobj == NULL)
2843 htab->elf.dynobj = abfd;
2844 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2845 return FALSE;
2846 }
2847 break;
2848
2849 /* This relocation describes the C++ object vtable hierarchy.
2850 Reconstruct it for later use during GC. */
2851 case R_CKCORE_GNU_VTINHERIT:
2852 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2853 return FALSE;
2854 break;
2855
2856 /* This relocation describes which C++ vtable entries are actually
2857 used. Record for later use during GC. */
2858 case R_CKCORE_GNU_VTENTRY:
a0ea3a14 2859 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b8891f8d
AJ
2860 return FALSE;
2861 break;
2862 }
2863 }
2864
2865 return TRUE;
2866}
2867
2868static const struct bfd_elf_special_section csky_elf_special_sections[]=
2869{
2870 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2871 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2872 { NULL, 0, 0, 0, 0 }
2873};
2874
2875/* Function to keep CSKY specific flags in the ELF header. */
2876
2877static bfd_boolean
2878csky_elf_set_private_flags (bfd * abfd, flagword flags)
2879{
2880 BFD_ASSERT (! elf_flags_init (abfd)
2881 || elf_elfheader (abfd)->e_flags == flags);
2882
2883 elf_elfheader (abfd)->e_flags = flags;
2884 elf_flags_init (abfd) = TRUE;
2885 return TRUE;
2886}
2887
2888static csky_arch_for_merge *
2889csky_find_arch_with_eflag (const unsigned long arch_eflag)
2890{
2891 csky_arch_for_merge *csky_arch = NULL;
2892
2893 for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
2894 if (csky_arch->arch_eflag == arch_eflag)
2895 break;
2896 if (csky_arch == NULL)
2897 {
2898 _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
2899 arch_eflag);
2900 bfd_set_error (bfd_error_wrong_format);
2901 }
2902 return csky_arch;
2903}
2904
2905/* Merge backend specific data from an object file to the output
2906 object file when linking. */
2907
2908static bfd_boolean
2909csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2910{
2911 bfd *obfd = info->output_bfd;
2912 flagword old_flags;
2913 flagword new_flags;
2914 csky_arch_for_merge *old_arch = NULL;
2915 csky_arch_for_merge *new_arch = NULL;
2916
2917 /* Check if we have the same endianness. */
2918 if (! _bfd_generic_verify_endian_match (ibfd, info))
2919 return FALSE;
2920
2921 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2922 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2923 return TRUE;
2924
2925 new_flags = elf_elfheader (ibfd)->e_flags;
2926 old_flags = elf_elfheader (obfd)->e_flags;
2927
2928 if (! elf_flags_init (obfd))
2929 {
2930 /* First call, no flags set. */
2931 elf_flags_init (obfd) = TRUE;
2932 elf_elfheader (obfd)->e_flags = new_flags;
2933 }
2934 else if (new_flags == old_flags)
2935 /* Do nothing. */
2936 ;
2937 else if (new_flags == 0 || old_flags == 0)
2938 /* When one flag is 0, assign the other one's flag. */
2939 elf_elfheader (obfd)->e_flags = new_flags | old_flags;
2940 else
2941 {
2942 flagword newest_flag = 0;
2943
2944 if ((new_flags & CSKY_ARCH_MASK) != 0
2945 && (old_flags & CSKY_ARCH_MASK) != 0)
2946 {
2947 new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
2948 old_arch = csky_find_arch_with_eflag (old_flags & CSKY_ARCH_MASK);
2949 /* Collect flags like e, f, g. */
2950 newest_flag = (old_flags & (~CSKY_ARCH_MASK))
2951 | (new_flags & (~CSKY_ARCH_MASK));
2952 if (new_arch != NULL && old_arch != NULL)
2953 {
2954 if (new_arch->class != old_arch->class)
2955 {
2956 _bfd_error_handler
2957 /* xgettext:c-format */
2958 (_("%pB: machine flag conflict with target"), ibfd);
2959 bfd_set_error (bfd_error_wrong_format);
2960 return FALSE;
2961 }
2962 else if (new_arch->class_level != old_arch->class_level)
2963 {
2964 csky_arch_for_merge *newest_arch
2965 = (new_arch->class_level > old_arch->class_level
2966 ? new_arch : old_arch);
2967 if (new_arch->do_warning || old_arch->do_warning)
2968 {
2969 _bfd_error_handler
2970 /* xgettext:c-format */
2971 (_("warning: file %pB's arch flag ck%s conflicts with "
2972 "target ck%s, using ck%s"),
2973 ibfd, new_arch->name, old_arch->name,
2974 newest_arch->name);
2975 bfd_set_error (bfd_error_wrong_format);
2976 }
2977
2978 newest_flag |= newest_arch->arch_eflag;
2979 }
2980 else
2981 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2982 | (old_flags
2983 & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2984 }
2985 else
2986 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2987 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2988 }
2989 else
2990 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2991 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2992
2993 elf_elfheader (obfd)->e_flags = newest_flag;
2994 }
2995 return TRUE;
2996}
2997
2998/* Ignore the discarded relocs in special sections in link time. */
2999
3000static bfd_boolean
3001csky_elf_ignore_discarded_relocs (asection *sec)
3002{
3003 if (strcmp (sec->name, ".csky_stack_size") == 0)
3004 return TRUE;
3005 return FALSE;
3006}
3007
3008/* .csky_stack_size are not referenced directly. This pass marks all of
3009 them as required. */
3010
3011static bfd_boolean
3012elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
3013 elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
3014{
3015 bfd *sub;
3016
3017 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
3018
3019 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3020 {
3021 asection *o;
3022
3023 for (o = sub->sections; o != NULL; o = o->next)
3024 if (strcmp (o->name, ".csky_stack_size") == 0)
3025 o->gc_mark = 1;
3026 }
3027
3028 return TRUE;
3029}
3030
3031/* The linker repeatedly calls this function for each input section,
3032 in the order that input sections are linked into output sections.
3033 Build lists of input sections to determine groupings between which
3034 we may insert linker stubs. */
3035
3036void
3037elf32_csky_next_input_section (struct bfd_link_info *info,
3038 asection *isec)
3039{
3040 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3041 if (htab == NULL)
3042 return;
3043 if (isec->output_section->index <= htab->top_index)
3044 {
3045 asection **list = htab->input_list + isec->output_section->index;
3046
3047 if (*list != bfd_abs_section_ptr)
3048 {
3049 /* Steal the link_sec pointer for our list. */
3050#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3051 /* This happens to make the list in reverse order,
3052 which we reverse later in group_sections. */
3053 PREV_SEC (isec) = *list;
3054 *list = isec;
3055 }
3056 }
3057}
3058
3059/* See whether we can group stub sections together. Grouping stub
3060 sections may result in fewer stubs. More importantly, we need to
3061 put all .init* and .fini* stubs at the end of the .init or
3062 .fini output sections respectively, because glibc splits the
3063 _init and _fini functions into multiple parts. Putting a stub in
3064 the middle of a function is not a good idea. */
3065
3066static void
3067group_sections (struct csky_elf_link_hash_table *htab,
3068 bfd_size_type stub_group_size,
3069 bfd_boolean stubs_always_after_branch)
3070{
3071 asection **list = htab->input_list;
3072
3073 do
3074 {
3075 asection *tail = *list;
3076 asection *head;
3077
3078 if (tail == bfd_abs_section_ptr)
3079 continue;
3080
3081 /* Reverse the list: we must avoid placing stubs at the
4b24dd1a
AM
3082 beginning of the section because the beginning of the text
3083 section may be required for an interrupt vector in bare metal
3084 code. */
b8891f8d
AJ
3085#define NEXT_SEC PREV_SEC
3086 head = NULL;
3087 while (tail != NULL)
3088 {
3089 /* Pop from tail. */
3090 asection *item = tail;
3091 tail = PREV_SEC (item);
3092
3093 /* Push on head. */
3094 NEXT_SEC (item) = head;
3095 head = item;
3096 }
3097
3098 while (head != NULL)
3099 {
3100 asection *curr;
3101 asection *next;
3102 bfd_vma stub_group_start = head->output_offset;
3103 bfd_vma end_of_next;
3104
3105 curr = head;
3106 while (NEXT_SEC (curr) != NULL)
3107 {
3108 next = NEXT_SEC (curr);
3109 end_of_next = next->output_offset + next->size;
3110 if (end_of_next - stub_group_start >= stub_group_size)
3111 /* End of NEXT is too far from start, so stop. */
3112 break;
3113 curr = next;
3114 }
3115
3116 /* OK, the size from the start to the start of CURR is less
3117 * than stub_group_size and thus can be handled by one stub
3118 * section. (Or the head section is itself larger than
3119 * stub_group_size, in which case we may be toast.)
3120 * We should really be keeping track of the total size of
3121 * stubs added here, as stubs contribute to the final output
3122 * section size. */
3123 do
3124 {
3125 next = NEXT_SEC (head);
3126 /* Set up this stub group. */
3127 htab->stub_group[head->id].link_sec = curr;
3128 }
3129 while (head != curr && (head = next) != NULL);
3130
3131 /* But wait, there's more! Input sections up to stub_group_size
3132 * bytes after the stub section can be handled by it too. */
3133 if (!stubs_always_after_branch)
3134 {
3135 stub_group_start = curr->output_offset + curr->size;
3136
3137 while (next != NULL)
3138 {
3139 end_of_next = next->output_offset + next->size;
3140 if (end_of_next - stub_group_start >= stub_group_size)
3141 /* End of NEXT is too far from stubs, so stop. */
3142 break;
3143 /* Add NEXT to the stub group. */
3144 head = next;
3145 next = NEXT_SEC (head);
3146 htab->stub_group[head->id].link_sec = curr;
3147 }
3148 }
3149 head = next;
3150 }
3151 }
3152 while (list++ != htab->input_list + htab->top_index);
3153
3154 free (htab->input_list);
3155#undef PREV_SEC
3156#undef NEXT_SEC
3157}
3158
3159/* If the symbol referenced by bsr is defined in shared object file,
3160 or it is a weak symbol and we aim to create shared object file,
3161 we must create a stub for this bsr. */
3162
3163static bfd_boolean
3164sym_must_create_stub (struct elf_link_hash_entry *h,
3165 struct bfd_link_info *info)
3166{
3167 if (h != NULL
3168 && ((h->def_dynamic && !h->def_regular)
3169 || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
3170 return TRUE;
3171 else
3172 return FALSE;
3173}
3174
3175/* Calculate the template, template size and instruction size for a stub.
3176 Return value is the instruction size. */
3177
3178static unsigned int
3179find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
3180 const insn_sequence **stub_template,
3181 int *stub_template_size)
3182{
3183 const insn_sequence *template_sequence = NULL;
3184 int template_size = 0;
3185 int i;
3186 unsigned int size;
3187
3188 template_sequence = stub_definitions[stub_type].template_sequence;
3189 template_size = stub_definitions[stub_type].template_size;
3190
3191 size = 0;
3192 for (i = 0; i < template_size; i++)
3193 {
3194 switch (template_sequence[i].type)
3195 {
3196 case INSN16:
3197 size += 2;
3198 break;
3199
3200 case INSN32:
3201 case DATA_TYPE:
3202 size += 4;
3203 break;
3204
3205 default:
3206 BFD_FAIL ();
3207 return FALSE;
3208 }
3209 }
3210
3211 if (stub_template)
3212 *stub_template = template_sequence;
3213 if (stub_template_size)
3214 *stub_template_size = template_size;
3215
3216 return size;
3217}
3218
3219/* As above, but don't actually build the stub. Just bump offset so
3220 we know stub section sizes. */
3221
3222static bfd_boolean
3223csky_size_one_stub (struct bfd_hash_entry *gen_entry,
3224 void * in_arg ATTRIBUTE_UNUSED)
3225{
3226 struct elf32_csky_stub_hash_entry *stub_entry;
3227 const insn_sequence *template_sequence = NULL;
3228 int template_size = 0;
3229 int size = 0;
3230
3231 /* Massage our args to the form they really have. */
3232 stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
3233
3234 BFD_ASSERT (stub_entry->stub_type > csky_stub_none
3235 && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
3236 size = find_stub_size_and_template (stub_entry->stub_type,
3237 &template_sequence, &template_size);
3238 stub_entry->stub_size = size;
3239 stub_entry->stub_template = template_sequence;
3240 stub_entry->stub_template_size = template_size;
3241
3242 size = (size + 7) & ~7;
3243 stub_entry->stub_sec->size += size;
3244 return TRUE;
3245}
3246
3247/* Add a new stub entry to the stub hash. Not all fields of the new
3248 stub entry are initialised. */
3249
3250static struct elf32_csky_stub_hash_entry *
3251elf32_csky_add_stub (const char *stub_name,
3252 asection *section,
3253 struct csky_elf_link_hash_table *htab)
3254{
3255 asection *link_sec;
3256 asection *stub_sec;
3257 struct elf32_csky_stub_hash_entry *stub_entry;
3258
3259 stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
3260 if (stub_sec == NULL)
3261 return NULL;
3262
3263 /* Enter this entry into the linker stub hash table. */
3264 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3265 TRUE, FALSE);
3266 if (stub_entry == NULL)
3267 {
3268 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3269 section->owner, stub_name);
3270 return NULL;
3271 }
3272
3273 stub_entry->stub_sec = stub_sec;
3274 stub_entry->stub_offset = 0;
3275 stub_entry->id_sec = link_sec;
3276
3277 return stub_entry;
3278}
3279
3280/* Determine and set the size of the stub section for a final link.
3281 The basic idea here is to examine all the relocations looking for
3282 PC-relative calls to a target that is unreachable with a "bsr"
3283 instruction. */
3284
3285bfd_boolean
3286elf32_csky_size_stubs (bfd *output_bfd,
3287 bfd *stub_bfd,
3288 struct bfd_link_info *info,
3289 bfd_signed_vma group_size,
3290 asection *(*add_stub_section) (const char*, asection*),
3291 void (*layout_sections_again) (void))
3292{
3293 bfd_size_type stub_group_size;
3294 bfd_boolean stubs_always_after_branch;
3295 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3296
3297 if (htab == NULL)
3298 return FALSE;
3299
3300 /* Propagate mach to stub bfd, because it may not have been
3301 finalized when we created stub_bfd. */
3302 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3303 bfd_get_mach (output_bfd));
3304
3305 /* Stash our params away. */
3306 htab->stub_bfd = stub_bfd;
3307 htab->add_stub_section = add_stub_section;
3308 htab->layout_sections_again = layout_sections_again;
3309 stubs_always_after_branch = group_size < 0;
3310
3311 if (group_size < 0)
3312 stub_group_size = -group_size;
3313 else
3314 stub_group_size = group_size;
3315
3316 if (stub_group_size == 1)
3317 /* The 'bsr' range in abiv2 is +-64MB has to be used as the
3318 default maximum size.
3319 This value is 128K less than that, which allows for 131072
3320 byte stubs. If we exceed that, then we will fail to link.
3321 The user will have to relink with an explicit group size
3322 option. */
3323 stub_group_size = 66977792;
3324
3325 group_sections (htab, stub_group_size, stubs_always_after_branch);
3326
3327 while (1)
3328 {
3329 bfd *input_bfd;
3330 unsigned int bfd_indx;
3331 asection *stub_sec;
3332 bfd_boolean stub_changed = FALSE;
3333
3334 for (input_bfd = info->input_bfds, bfd_indx = 0;
3335 input_bfd != NULL;
3336 input_bfd = input_bfd->link.next, bfd_indx++)
3337 {
3338 Elf_Internal_Shdr *symtab_hdr;
3339 asection *section;
3340 Elf_Internal_Sym *local_syms = NULL;
3341
3342 /* We'll need the symbol table in a second. */
3343 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3344 if (symtab_hdr->sh_info == 0)
3345 continue;
3346
3347 /* Walk over each section attached to the input bfd. */
3348 for (section = input_bfd->sections;
3349 section != NULL;
3350 section = section->next)
3351 {
3352 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3353
3354 /* If there aren't any relocs, then there's nothing more
3355 * to do. */
3356 if ((section->flags & SEC_RELOC) == 0
3357 || section->reloc_count == 0
3358 || (section->flags & SEC_CODE) == 0)
3359 continue;
3360
3361 /* If this section is a link-once section that will be
3362 discarded, then don't create any stubs. */
3363 if (section->output_section == NULL
3364 || section->output_section->owner != output_bfd)
3365 continue;
3366
3367 /* Get the relocs. */
3368 internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
3369 section,
3370 NULL, NULL,
3371 info->keep_memory);
3372
3373 if (internal_relocs == NULL)
3374 goto error_ret_free_local;
3375
3376 /* Now examine each relocation. */
3377 irela = internal_relocs;
3378 irelaend = irela + section->reloc_count;
3379 for (; irela < irelaend; irela++)
3380 {
3381 unsigned int r_type, r_indx;
3382 enum elf32_csky_stub_type stub_type;
3383 struct elf32_csky_stub_hash_entry *stub_entry;
3384 asection *sym_sec;
3385 bfd_vma sym_value;
3386 bfd_vma destination;
3387 struct csky_elf_link_hash_entry *hash;
3388 const char *sym_name;
3389 char *stub_name;
3390 const asection *id_sec;
3391 unsigned char st_type;
3392
3393 r_type = ELF32_R_TYPE (irela->r_info);
3394 r_indx = ELF32_R_SYM (irela->r_info);
3395 if (r_type >= (unsigned int) R_CKCORE_MAX)
3396 {
3397 bfd_set_error (bfd_error_bad_value);
dc1e8a47 3398 error_ret_free_internal:
b8891f8d
AJ
3399 if (elf_section_data (section)->relocs == NULL)
3400 free (internal_relocs);
3401 goto error_ret_free_local;
3402 }
3403
3404 /* Only look for stubs on branch instructions. */
3405 if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
3406 continue;
3407 /* Now determine the call target, its name, value,
3408 section. */
3409 sym_sec = NULL;
3410 sym_value = 0;
3411 destination = 0;
3412 hash = NULL;
3413 sym_name = NULL;
3414 if (r_indx < symtab_hdr->sh_info)
3415 {
3416 /* It's a local symbol. */
3417 Elf_Internal_Sym *sym;
3418 Elf_Internal_Shdr *hdr;
3419 if (local_syms == NULL)
3420 local_syms =
3421 (Elf_Internal_Sym *) symtab_hdr->contents;
3422 if (local_syms == NULL)
3423 {
3424 local_syms =
3425 bfd_elf_get_elf_syms (input_bfd,
3426 symtab_hdr,
3427 symtab_hdr->sh_info,
3428 0, NULL, NULL, NULL);
3429 if (local_syms == NULL)
3430 goto error_ret_free_internal;
3431 }
3432 sym = local_syms + r_indx;
3433 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3434 sym_sec = hdr->bfd_section;
3435 if (!sym_sec)
3436 /* This is an undefined symbol. It can never
3437 be resolved. */
3438 continue;
3439 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3440 sym_value = sym->st_value;
3441 destination = (sym_value + irela->r_addend
3442 + sym_sec->output_offset
3443 + sym_sec->output_section->vma);
3444 st_type = ELF_ST_TYPE (sym->st_info);
3445 sym_name =
3446 bfd_elf_string_from_elf_section (input_bfd,
3447 symtab_hdr->sh_link,
3448 sym->st_name);
3449 }
3450 else
3451 {
3452 /* It's an external symbol. */
3453 int e_indx;
3454 e_indx = r_indx - symtab_hdr->sh_info;
3455 hash = ((struct csky_elf_link_hash_entry *)
3456 elf_sym_hashes (input_bfd)[e_indx]);
3457
3458 while (hash->elf.root.type == bfd_link_hash_indirect
3459 || hash->elf.root.type == bfd_link_hash_warning)
3460 hash = ((struct csky_elf_link_hash_entry *)
3461 hash->elf.root.u.i.link);
3462 if (hash->elf.root.type == bfd_link_hash_defined
3463 || hash->elf.root.type == bfd_link_hash_defweak)
3464 {
3465 sym_sec = hash->elf.root.u.def.section;
3466 sym_value = hash->elf.root.u.def.value;
3467
3468 struct csky_elf_link_hash_table *globals =
3469 csky_elf_hash_table (info);
3470 /* FIXME For a destination in a shared library. */
3471 if (globals->elf.splt != NULL && hash != NULL
3472 && hash->elf.plt.offset != (bfd_vma) -1)
3473 continue;
3474 else if (sym_sec->output_section != NULL)
3475 destination = (sym_value + irela->r_addend
3476 + sym_sec->output_offset
3477 + sym_sec->output_section->vma);
3478 }
3479 else if (hash->elf.root.type == bfd_link_hash_undefined
3480 || (hash->elf.root.type
3481 == bfd_link_hash_undefweak))
3482 /* FIXME For a destination in a shared library. */
3483 continue;
3484 else
3485 {
3486 bfd_set_error (bfd_error_bad_value);
3487 goto error_ret_free_internal;
3488 }
3489 st_type = ELF_ST_TYPE (hash->elf.type);
3490 sym_name = hash->elf.root.root.string;
3491 }
3492 do
3493 {
3494 /* Determine what (if any) linker stub is needed. */
3495 stub_type = csky_type_of_stub (info, section, irela,
3496 st_type, hash,
3497 destination, sym_sec,
3498 input_bfd, sym_name);
3499 if (stub_type == csky_stub_none)
3500 break;
3501
3502 /* Support for grouping stub sections. */
3503 id_sec = htab->stub_group[section->id].link_sec;
3504
3505 /* Get the name of this stub. */
3506 stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
3507 irela);
3508 if (!stub_name)
3509 goto error_ret_free_internal;
3510 /* We've either created a stub for this reloc already,
3511 or we are about to. */
3512 stub_entry
3513 = csky_stub_hash_lookup (&htab->stub_hash_table,
3514 stub_name,
3515 FALSE, FALSE);
3516 if (stub_entry != NULL)
3517 {
3518 /* The proper stub has already been created. */
3519 free (stub_name);
3520 stub_entry->target_value = sym_value;
3521 break;
3522 }
3523 stub_entry = elf32_csky_add_stub (stub_name, section,
3524 htab);
3525 if (stub_entry == NULL)
3526 {
3527 free (stub_name);
3528 goto error_ret_free_internal;
3529 }
3530 stub_entry->target_value = sym_value;
3531 stub_entry->target_section = sym_sec;
3532 stub_entry->stub_type = stub_type;
3533 stub_entry->h = hash;
3534 stub_entry->st_type = st_type;
3535
3536 if (sym_name == NULL)
3537 sym_name = "unnamed";
3538 stub_entry->output_name =
3539 bfd_alloc (htab->stub_bfd,
3540 (sizeof (STUB_ENTRY_NAME)
3541 + strlen (sym_name)));
3542 if (stub_entry->output_name == NULL)
3543 {
3544 free (stub_name);
3545 goto error_ret_free_internal;
3546 }
3547 sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3548 sym_name);
3549 stub_changed = TRUE;
3550 }
3551 while (0);
3552 }
3553 /* We're done with the internal relocs, free them. */
3554 if (elf_section_data (section)->relocs == NULL)
3555 free (internal_relocs);
3556 }
3557 }
3558 if (!stub_changed)
3559 break;
3560 /* OK, we've added some stubs. Find out the new size of the
3561 stub sections. */
3562 for (stub_sec = htab->stub_bfd->sections;
3563 stub_sec != NULL;
3564 stub_sec = stub_sec->next)
3565 {
3566 /* Ignore non-stub sections. */
3567 if (!strstr (stub_sec->name, STUB_SUFFIX))
3568 continue;
3569 stub_sec->size = 0;
3570 }
3571 bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
3572 /* Ask the linker to do its stuff. */
3573 (*htab->layout_sections_again) ();
3574 }
3575
3576 return TRUE;
dc1e8a47 3577 error_ret_free_local:
b8891f8d
AJ
3578 return FALSE;
3579}
3580
3581static bfd_boolean
3582csky_build_one_stub (struct bfd_hash_entry *gen_entry,
3583 void * in_arg)
3584{
3585#define MAXRELOCS 2
3586 struct elf32_csky_stub_hash_entry *stub_entry;
3587 struct bfd_link_info *info;
3588 asection *stub_sec;
3589 bfd *stub_bfd;
3590 bfd_byte *loc;
3591 bfd_vma sym_value;
3592 int template_size;
3593 int size;
3594 const insn_sequence *template_sequence;
3595 int i;
3596 struct csky_elf_link_hash_table * globals;
3597 int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3598 int stub_reloc_offset[MAXRELOCS] = {0, 0};
3599 int nrelocs = 0;
3600 struct elf_link_hash_entry *h = NULL;
3601
3602 /* Massage our args to the form they really have. */
3603 stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
3604 info = (struct bfd_link_info *) in_arg;
3605
abf874aa
CL
3606 /* Fail if the target section could not be assigned to an output
3607 section. The user should fix his linker script. */
3608 if (stub_entry->target_section->output_section == NULL
3609 && info->non_contiguous_regions)
53215f21
CL
3610 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
3611 "Retry without --enable-non-contiguous-regions.\n"),
3612 stub_entry->target_section);
abf874aa 3613
b8891f8d
AJ
3614 globals = csky_elf_hash_table (info);
3615 if (globals == NULL)
3616 return FALSE;
3617 stub_sec = stub_entry->stub_sec;
3618
3619 /* Make a note of the offset within the stubs for this entry. */
3620 stub_entry->stub_offset = stub_sec->size;
3621 loc = stub_sec->contents + stub_entry->stub_offset;
3622
3623 stub_bfd = stub_sec->owner;
3624
3625 /* This is the address of the stub destination. */
3626 h = &stub_entry->h->elf;
3627 if (sym_must_create_stub (h, info)
3628 && !(bfd_link_pic (info)
3629 && h->root.type == bfd_link_hash_defweak
3630 && h->def_regular
3631 && !h->def_dynamic))
3632 sym_value = 0;
3633 else
3634 sym_value = (stub_entry->target_value
3635 + stub_entry->target_section->output_offset
3636 + stub_entry->target_section->output_section->vma);
3637
3638 template_sequence = stub_entry->stub_template;
3639 template_size = stub_entry->stub_template_size;
3640
3641 size = 0;
3642 for (i = 0; i < template_size; i++)
3643 switch (template_sequence[i].type)
3644 {
3645 case INSN16:
3646 bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
3647 loc + size);
3648 size += 2;
3649 break;
3650 case INSN32:
3651 csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3652 loc + size);
3653 size += 4;
3654 break;
3655 case DATA_TYPE:
3656 bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3657 loc + size);
3658 stub_reloc_idx[nrelocs] = i;
3659 stub_reloc_offset[nrelocs++] = size;
3660 size += 4;
3661 break;
3662 default:
3663 BFD_FAIL ();
3664 return FALSE;
3665 }
3666 stub_sec->size += size;
3667
3668 /* Stub size has already been computed in csky_size_one_stub. Check
3669 consistency. */
3670 BFD_ASSERT (size == stub_entry->stub_size);
3671
3672 /* Assume there is at least one and at most MAXRELOCS entries to relocate
3673 in each stub. */
3674 BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3675
3676 for (i = 0; i < nrelocs; i++)
3677 {
3678 if (sym_must_create_stub (h, info))
3679 {
3680 Elf_Internal_Rela outrel;
3681 asection * sreloc = globals->elf.srelgot;
3682
3683 outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3684 outrel.r_info =
3685 ELF32_R_INFO (h->dynindx,
3686 template_sequence[stub_reloc_idx[i]].r_type);
3687 outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
3688
3689 loc = sreloc->contents;
3690 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3691
3692 if (loc != NULL)
3693 bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3694 }
3695 _bfd_final_link_relocate (elf32_csky_howto_from_type
3696 (template_sequence[stub_reloc_idx[i]].r_type),
3697 stub_bfd, stub_sec, stub_sec->contents,
3698 stub_entry->stub_offset + stub_reloc_offset[i],
3699 sym_value + stub_entry->target_addend,
3700 template_sequence[stub_reloc_idx[i]].reloc_addend);
3701 }
3702
3703 return TRUE;
3704#undef MAXRELOCS
3705}
3706
3707/* Build all the stubs associated with the current output file. The
3708 stubs are kept in a hash table attached to the main linker hash
3709 table. We also set up the .plt entries for statically linked PIC
3710 functions here. This function is called via arm_elf_finish in the
3711 linker. */
3712
3713bfd_boolean
3714elf32_csky_build_stubs (struct bfd_link_info *info)
3715{
3716 asection *stub_sec;
3717 struct bfd_hash_table *table;
3718 struct csky_elf_link_hash_table *htab;
3719
3720 htab = csky_elf_hash_table (info);
3721
3722 if (htab == NULL)
3723 return FALSE;
3724
3725 for (stub_sec = htab->stub_bfd->sections;
3726 stub_sec != NULL;
3727 stub_sec = stub_sec->next)
3728 {
3729 bfd_size_type size;
3730
3731 /* Ignore non-stub sections. */
3732 if (!strstr (stub_sec->name, STUB_SUFFIX))
3733 continue;
3734
3735 /* Allocate memory to hold the linker stubs. */
3736 size = stub_sec->size;
3737 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3738 if (stub_sec->contents == NULL && size != 0)
3739 return FALSE;
3740 stub_sec->size = 0;
3741 }
3742
3743 /* Build the stubs as directed by the stub hash table. */
3744 table = &htab->stub_hash_table;
3745 bfd_hash_traverse (table, csky_build_one_stub, info);
3746
3747 return TRUE;
3748}
3749
3750/* Set up various things so that we can make a list of input sections
3751 for each output section included in the link. Returns -1 on error,
3752 0 when no stubs will be needed, and 1 on success. */
3753
3754int
3755elf32_csky_setup_section_lists (bfd *output_bfd,
3756 struct bfd_link_info *info)
3757{
3758 bfd *input_bfd;
3759 unsigned int bfd_count;
3760 unsigned int top_id, top_index;
3761 asection *section;
3762 asection **input_list, **list;
986f0783 3763 size_t amt;
b8891f8d
AJ
3764 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3765
3766 if (!htab)
3767 return 0;
3768 if (! is_elf_hash_table (htab))
3769 return 0;
3770
3771 /* Count the number of input BFDs and find the top input section id. */
3772 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3773 input_bfd != NULL;
3774 input_bfd = input_bfd->link.next)
3775 {
3776 bfd_count += 1;
3777 for (section = input_bfd->sections;
3778 section != NULL;
3779 section = section->next)
3780 if (top_id < section->id)
3781 top_id = section->id;
3782 }
3783 htab->bfd_count = bfd_count;
3784 amt = sizeof (struct map_stub) * (top_id + 1);
3785 htab->stub_group = bfd_zmalloc (amt);
3786 if (htab->stub_group == NULL)
3787 return -1;
3788
3789 /* We can't use output_bfd->section_count here to find the top output
3790 section index as some sections may have been removed, and
3791 _bfd_strip_section_from_output doesn't renumber the indices. */
3792 for (section = output_bfd->sections, top_index = 0;
3793 section != NULL;
3794 section = section->next)
3795 if (top_index < section->index)
3796 top_index = section->index;
3797 htab->top_index = top_index;
3798 amt = sizeof (asection *) * (top_index + 1);
3799 input_list = bfd_malloc (amt);
3800 htab->input_list = input_list;
3801 if (input_list == NULL)
3802 return -1;
3803 /* For sections we aren't interested in, mark their entries with a
3804 value we can check later. */
3805 list = input_list + top_index;
3806 do
3807 *list = bfd_abs_section_ptr;
3808 while (list-- != input_list);
3809 for (section = output_bfd->sections;
3810 section != NULL;
3811 section = section->next)
3812 if ((section->flags & SEC_CODE) != 0)
3813 input_list[section->index] = NULL;
3814
3815 return 1;
3816}
3817
3818static bfd_reloc_status_type
3819csky_relocate_contents (reloc_howto_type *howto,
3820 bfd *input_bfd,
3821 long relocation,
3822 bfd_byte *location)
3823{
3824 int size;
3825 bfd_vma x = 0;
3826 bfd_reloc_status_type flag;
3827 unsigned int rightshift = howto->rightshift;
3828 unsigned int bitpos = howto->bitpos;
3829
3830 /* If the size is negative, negate RELOCATION. This isn't very
3831 general. */
3832 if (howto->size < 0)
3833 relocation = -relocation;
3834
3835 /* FIXME: these macros should be defined at file head or head file head. */
3836#define CSKY_INSN_ADDI_TO_SUBI 0x04000000
3837#define CSKY_INSN_MOV_RTB 0xc41d4820 // mov32 rx, r29, 0
3838#define CSKY_INSN_MOV_RDB 0xc41c4820 // mov32 rx, r28, 0
3839#define CSKY_INSN_GET_ADDI_RZ(x) (((x) & 0x03e00000) >> 21)
3840#define CSKY_INSN_SET_MOV_RZ(x) ((x) & 0x0000001f)
3841#define CSKY_INSN_JSRI_TO_LRW 0xea9a0000
3842#define CSKY_INSN_JSR_R26 0xe8fa0000
3843
3844 /* Get the value we are going to relocate. */
3845 size = bfd_get_reloc_size (howto);
3846 switch (size)
3847 {
3848 default:
3849 case 0:
3850 abort ();
3851 case 1:
3852 x = bfd_get_8 (input_bfd, location);
3853 break;
3854 case 2:
3855 x = bfd_get_16 (input_bfd, location);
3856 break;
3857 case 4:
3858 if (need_reverse_bits)
3859 {
3860 x = csky_get_insn_32 (input_bfd, location);
3861
3862 if (R_CKCORE_DOFFSET_LO16 == howto->type)
3863 {
3864 if ((signed) relocation < 0)
3865 {
3866 x |= CSKY_INSN_ADDI_TO_SUBI;
3867 relocation = -relocation;
3868 }
3869 else if (0 == relocation)
3870 x = (CSKY_INSN_MOV_RDB |
3871 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3872 }
3873 else if (R_CKCORE_TOFFSET_LO16 == howto->type)
3874 {
3875 if ((signed) relocation < 0)
3876 {
3877 x |= CSKY_INSN_ADDI_TO_SUBI;
3878 relocation = -relocation;
3879 }
3880 else if (0 == relocation)
3881 x = (CSKY_INSN_MOV_RTB |
3882 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3883 }
3884 }
3885 else
3886 x = bfd_get_32 (input_bfd, location);
3887 break;
3888 }
3889 /* Check for overflow. FIXME: We may drop bits during the addition
3890 which we don't check for. We must either check at every single
3891 operation, which would be tedious, or we must do the computations
3892 in a type larger than bfd_vma, which would be inefficient. */
3893 flag = bfd_reloc_ok;
3894 if (howto->complain_on_overflow != complain_overflow_dont)
3895 {
3896 int addrmask;
3897 int fieldmask;
3898 int signmask;
3899 int ss;
3900 int a;
3901 int b;
3902 int sum;
3903 /* Get the values to be added together. For signed and unsigned
3904 relocations, we assume that all values should be truncated to
3905 the size of an address. For bitfields, all the bits matter.
3906 See also bfd_check_overflow. */
3907#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
3908 fieldmask = N_ONES (howto->bitsize);
3909 signmask = ~fieldmask;
3910 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3911 a = (relocation & addrmask) >> rightshift;
3912 if (read_content_substitute)
3913 x = read_content_substitute;
3914 b = (x & howto->src_mask & addrmask) >> bitpos;
3915
3916 switch (howto->complain_on_overflow)
3917 {
3918 case complain_overflow_signed:
3919 /* If any sign bits are set, all sign bits must be set.
3920 That is, A must be a valid negative address after
3921 shifting. */
3922 signmask = ~(fieldmask >> 1);
3923 /* Fall through. */
3924
3925 case complain_overflow_bitfield:
3926 /* Much like the signed check, but for a field one bit
3927 wider. We allow a bitfield to represent numbers in the
3928 range -2**n to 2**n-1, where n is the number of bits in the
3929 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
3930 can't overflow, which is exactly what we want. */
3931 ss = a & signmask;
3932 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3933 flag = bfd_reloc_overflow;
3934 /* We only need this next bit of code if the sign bit of B
3935 is below the sign bit of A. This would only happen if
3936 SRC_MASK had fewer bits than BITSIZE. Note that if
3937 SRC_MASK has more bits than BITSIZE, we can get into
3938 trouble; we would need to verify that B is in range, as
3939 we do for A above. */
3940 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3941 ss >>= bitpos;
3942
3943 /* Set all the bits above the sign bit. */
3944 b = (b ^ ss) - ss;
3945
3946 /* Now we can do the addition. */
3947 sum = a + b;
3948
3949 /* See if the result has the correct sign. Bits above the
3950 sign bit are junk now; ignore them. If the sum is
3951 positive, make sure we did not have all negative inputs;
3952 if the sum is negative, make sure we did not have all
3953 positive inputs. The test below looks only at the sign
3954 bits, and it really just
3955 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3956
3957 We mask with addrmask here to explicitly allow an address
3958 wrap-around. The Linux kernel relies on it, and it is
3959 the only way to write assembler code which can run when
3960 loaded at a location 0x80000000 away from the location at
3961 which it is linked. */
3962
3963 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3964 flag = bfd_reloc_overflow;
3965 break;
3966 case complain_overflow_unsigned:
3967 /* Checking for an unsigned overflow is relatively easy:
3968 trim the addresses and add, and trim the result as well.
3969 Overflow is normally indicated when the result does not
3970 fit in the field. However, we also need to consider the
3971 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3972 input is 0x80000000, and bfd_vma is only 32 bits; then we
3973 will get sum == 0, but there is an overflow, since the
3974 inputs did not fit in the field. Instead of doing a
3975 separate test, we can check for this by or-ing in the
3976 operands when testing for the sum overflowing its final
3977 field. */
3978 sum = (a + b) & addrmask;
3979 if ((a | b | sum) & signmask)
3980 flag = bfd_reloc_overflow;
3981 break;
3982 default:
3983 abort ();
3984 }
3985
3986 }
3987 /* Put RELOCATION in the right bits. */
3988 relocation >>= (bfd_vma) rightshift;
3989
3990 if ((howto->type == R_CKCORE_DOFFSET_LO16
3991 || howto->type == R_CKCORE_TOFFSET_LO16)
3992 && relocation == 0)
3993 /* Do nothing lsli32 rx, rz, 0. */
3994 ;
3995 else
3996 {
3997 /* Fir V1, all this relocation must be x -1. */
3998 if (howto->type == R_CKCORE_PCREL_IMM11BY2
3999 || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
4000 || howto->type == R_CKCORE_DOFFSET_LO16
4001 || howto->type == R_CKCORE_TOFFSET_LO16)
4002 relocation -= 1;
4003 else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
4004 relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
4005 else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
4006 relocation
4007 = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
4008 else if (howto->type == R_CKCORE_NOJSRI)
4009 {
4010 x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
4011 relocation = 0;
4012 csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
4013 }
4014
4015 relocation <<= (bfd_vma) bitpos;
4016 /* Add RELOCATION to the right bits of X. */
4017 x = ((x & ~howto->dst_mask)
4018 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4019 }
4020 /* Put the relocated value back in the object file. */
4021 switch (size)
4022 {
4023 default:
4024 abort ();
4025 case 1:
4026 bfd_put_8 (input_bfd, x, location);
4027 break;
4028 case 2:
4029 bfd_put_16 (input_bfd, x, location);
4030 break;
4031 case 4:
4032 if (need_reverse_bits)
4033 csky_put_insn_32 (input_bfd, x, location);
4034 else
4035 bfd_put_32 (input_bfd, x, location);
4036 break;
4037 }
4038 return flag;
4039}
4040
4041/* Look up an entry in the stub hash. Stub entries are cached because
4042 creating the stub name takes a bit of time. */
4043
4044static struct elf32_csky_stub_hash_entry *
4045elf32_csky_get_stub_entry (const asection *input_section,
4046 const asection *sym_sec,
4047 struct elf_link_hash_entry *hash,
4048 const Elf_Internal_Rela *rel,
4049 struct csky_elf_link_hash_table *htab)
4050{
4051 struct elf32_csky_stub_hash_entry *stub_entry;
4052 struct csky_elf_link_hash_entry *h
4053 = (struct csky_elf_link_hash_entry *) hash;
4054 const asection *id_sec;
4055
4056 if ((input_section->flags & SEC_CODE) == 0)
4057 return NULL;
4058
4059 /* If this input section is part of a group of sections sharing one
4060 stub section, then use the id of the first section in the group.
4061 Stub names need to include a section id, as there may well be
4062 more than one stub used to reach say, printf, and we need to
4063 distinguish between them. */
4064 id_sec = htab->stub_group[input_section->id].link_sec;
4065 if (h != NULL && h->stub_cache != NULL
4066 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
4067 stub_entry = h->stub_cache;
4068 else
4069 {
4070 char *stub_name;
4071 stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
4072 if (stub_name == NULL)
4073 return NULL;
4074 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
4075 stub_name, FALSE, FALSE);
4076 if (h != NULL)
4077 h->stub_cache = stub_entry;
4078 free (stub_name);
4079 }
4080
4081 return stub_entry;
4082}
4083
4084static bfd_reloc_status_type
4085csky_final_link_relocate (reloc_howto_type *howto,
4086 bfd *input_bfd,
4087 asection *input_section,
4088 bfd_byte *contents,
4089 bfd_vma address,
4090 bfd_vma value,
4091 bfd_vma addend)
4092{
4093 bfd_vma relocation;
4094
4095 /* Sanity check the address. */
4096 if (address > bfd_get_section_limit (input_bfd, input_section))
4097 return bfd_reloc_outofrange;
4098
4099 /* This function assumes that we are dealing with a basic relocation
4100 against a symbol. We want to compute the value of the symbol to
4101 relocate to. This is just VALUE, the value of the symbol,
4102 plus ADDEND, any addend associated with the reloc. */
4103 relocation = value + addend;
4104
4105 /* If the relocation is PC relative, we want to set RELOCATION to
4106 the distance between the symbol (currently in RELOCATION) and the
4107 location we are relocating. Some targets (e.g., i386-aout)
4108 arrange for the contents of the section to be the negative of the
4109 offset of the location within the section; for such targets
4110 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4111 simply leave the contents of the section as zero; for such
4112 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4113 need to subtract out the offset of the location within the
4114 section (which is just ADDRESS). */
4115 if (howto->pc_relative)
4116 {
4117 relocation -= (input_section->output_section->vma
4118 + input_section->output_offset);
4119 if (howto->pcrel_offset)
4120 relocation -= address;
4121 }
4122
4123 return csky_relocate_contents (howto, input_bfd, relocation,
4124 contents + address);
4125
4126}
4127
4128/* Return the base VMA address which should be subtracted from real addresses
4129 when resolving @dtpoff relocation.
4130 This is PT_TLS segment p_vaddr. */
4131
4132static bfd_vma
4133dtpoff_base (struct bfd_link_info *info)
4134{
4135 /* If tls_sec is NULL, we should have signalled an error already. */
4136 if (elf_hash_table (info)->tls_sec == NULL)
4137 return 0;
4138 return elf_hash_table (info)->tls_sec->vma;
4139}
4140
4141/* Return the relocation value for @tpoff relocation
4142 if STT_TLS virtual address is ADDRESS. */
4143
4144static bfd_vma
4145tpoff (struct bfd_link_info *info, bfd_vma address)
4146{
4147 struct elf_link_hash_table *htab = elf_hash_table (info);
4148 bfd_vma base;
4149
4150 /* If tls_sec is NULL, we should have signalled an error already. */
4151 if (htab->tls_sec == NULL)
4152 return 0;
4153 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
4154 return address - htab->tls_sec->vma + base;
4155}
4156
4157/* Relocate a csky section. */
4158
4159static bfd_boolean
4160csky_elf_relocate_section (bfd * output_bfd,
4161 struct bfd_link_info * info,
4162 bfd * input_bfd,
4163 asection * input_section,
4164 bfd_byte * contents,
4165 Elf_Internal_Rela * relocs,
4166 Elf_Internal_Sym * local_syms,
4167 asection ** local_sections)
4168{
4169 Elf_Internal_Shdr *symtab_hdr;
4170 struct elf_link_hash_entry **sym_hashes;
4171 Elf_Internal_Rela *rel;
4172 Elf_Internal_Rela *relend;
4173 const char *name;
4174 bfd_boolean ret = TRUE;
4175 struct csky_elf_link_hash_table * htab;
4176 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
4177
4178 htab = csky_elf_hash_table (info);
4179 if (htab == NULL)
4180 return FALSE;
4181
4182 symtab_hdr = & elf_symtab_hdr (input_bfd);
4183 sym_hashes = elf_sym_hashes (input_bfd);
4184
4185 rel = relocs;
4186 relend = relocs + input_section->reloc_count;
4187 for (; rel < relend; rel++)
4188 {
4189 enum elf_csky_reloc_type r_type
4190 = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
4191 unsigned long r_symndx;
4192 reloc_howto_type * howto;
4193 Elf_Internal_Sym * sym;
4194 asection * sec;
4195 bfd_vma relocation;
4196 bfd_vma off;
4197 struct elf_link_hash_entry * h;
4198 bfd_vma addend = (bfd_vma)rel->r_addend;
4199 bfd_reloc_status_type r = bfd_reloc_ok;
4200 bfd_boolean unresolved_reloc = FALSE;
4201 int do_final_relocate = TRUE;
4202 bfd_boolean relative_reloc = FALSE;
4203 bfd_signed_vma disp;
4204
4205 /* Ignore these relocation types:
4206 R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY. */
4207 if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
4208 continue;
4209
4210 if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
4211 {
4212 /* The r_type is error, not support it. */
4213 /* xgettext:c-format */
4214 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
4215 input_bfd, r_type);
4216 bfd_set_error (bfd_error_bad_value);
4217 ret = FALSE;
4218 continue;
4219 }
4220
4221 howto = &csky_elf_howto_table[(int) r_type];
4222
4223 r_symndx = ELF32_R_SYM(rel->r_info);
4224 h = NULL;
4225 sym = NULL;
4226 sec = NULL;
4227 unresolved_reloc = FALSE;
4228
4229 if (r_symndx < symtab_hdr->sh_info)
4230 {
4231 /* Get symbol table entry. */
4232 sym = local_syms + r_symndx;
4233 sec = local_sections[r_symndx];
4234 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4235 addend = (bfd_vma)rel->r_addend;
4236 }
4237 else
4238 {
4239 bfd_boolean warned, ignored;
4240
4241 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4242 r_symndx, symtab_hdr, sym_hashes,
4243 h, sec, relocation,
4244 unresolved_reloc, warned, ignored);
4245 }
4246
4247 if (sec != NULL && discarded_section (sec))
4248 {
4249 /* For relocs against symbols from removed linkonce sections,
4250 or sections discarded by a linker script, we just want the
4251 section contents zeroed. Avoid any special processing.
4252 And if the symbol is referenced in '.csky_stack_size' section,
4253 set the address to SEC_DISCARDED(0xffffffff). */
4254#if 0
4255 /* The .csky_stack_size section is just for callgraph. */
4256 if (strcmp (input_section->name, ".csky_stack_size") == 0)
4257 {
4258/* FIXME: it should define in head file. */
4259#define SEC_DISCARDED 0xffffffff
4260 bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
4261 rel->r_info = 0;
4262 rel->r_addend = 0;
4263 continue;
4264 }
4265 else
4266#endif
4267 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4268 rel, 1, relend, howto, 0,
4269 contents);
4270 }
4271
4272 if (bfd_link_relocatable (info))
4273 continue;
4274
4275 read_content_substitute = 0;
4276
4277 /* Final link. */
4278 disp = (relocation
4279 + (bfd_signed_vma) addend
4280 - input_section->output_section->vma
4281 - input_section->output_offset
4282 - rel->r_offset);
4283/* It is for ck8xx. */
4284#define CSKY_INSN_BSR32 0xe0000000
4285/* It is for ck5xx/ck6xx. */
4286#define CSKY_INSN_BSR16 0xf800
4287#define within_range(x, L) (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
4288 switch (howto->type)
4289 {
4290 case R_CKCORE_PCREL_IMM18BY2:
4291 /* When h is NULL, means the instruction written as
4292 grs rx, imm32
4293 if the highest bit is set, prevent the high 32bits
4294 turn to 0xffffffff when signed extern in 64bit
4295 host machine. */
4296 if (h == NULL && (addend & 0x80000000))
4297 addend &= 0xffffffff;
4298 break;
fe75f42e
LX
4299
4300 case R_CKCORE_PCREL32:
4301 break;
4302
b8891f8d
AJ
4303 case R_CKCORE_GOT12:
4304 case R_CKCORE_PLT12:
4305 case R_CKCORE_GOT_HI16:
4306 case R_CKCORE_GOT_LO16:
4307 case R_CKCORE_PLT_HI16:
4308 case R_CKCORE_PLT_LO16:
4309 case R_CKCORE_GOT32:
4310 case R_CKCORE_GOT_IMM18BY4:
4311 /* Relocation is to the entry for this symbol in the global
4312 offset table. */
4313 BFD_ASSERT (htab->elf.sgot != NULL);
4314 if (h != NULL)
4315 {
4316 /* Global symbol is defined by other modules. */
4317 bfd_boolean dyn;
4318 off = h->got.offset;
4319 dyn = htab->elf.dynamic_sections_created;
4320 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4321 bfd_link_pic (info), h)
4322 || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
4323 || (ELF_ST_VISIBILITY(h->other)
4324 && h->root.type == bfd_link_hash_undefweak))
4325 {
4326 /* This is actually a static link, or it is a
4327 -Bsymbolic link and the symbol is defined
4328 locally, or the symbol was forced to be local
4329 because of a version file. We must initialize
4330 this entry in the global offset table. Since the
4331 offset must always be a multiple of 4, we use the
4332 least significant bit to record whether we have
4333 initialized it already.
4334 When doing a dynamic link, we create a .rela.dyn
4335 relocation entry to initialize the value. This
4336 is done in the finish_dynamic_symbol routine. FIXME */
4337 if (off & 1)
4338 off &= ~1;
4339 else
4340 {
4341 bfd_put_32 (output_bfd, relocation,
4342 htab->elf.sgot->contents + off);
4343 h->got.offset |= 1;
4344
4345/* TRUE if relative relocation should be generated. GOT reference to
4346 global symbol in PIC will lead to dynamic symbol. It becomes a
4347 problem when "time" or "times" is defined as a variable in an
4348 executable, clashing with functions of the same name in libc. If a
4349 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
4350 generate relative relocation. */
4351#define GENERATE_RELATIVE_RELOC_P(INFO, H) \
4352 ((H)->dynindx == -1 \
4353 && !(H)->forced_local \
4354 && (H)->root.type != bfd_link_hash_undefweak \
4355 && bfd_link_pic (INFO))
4356
4357 if (GENERATE_RELATIVE_RELOC_P (info, h))
4358 /* If this symbol isn't dynamic
4359 in PIC, generate R_CKCORE_RELATIVE here. */
4360 relative_reloc = TRUE;
4361 }
4362 }
4363 else
4364 unresolved_reloc = FALSE;
4365 } /* End if h != NULL. */
4366 else
4367 {
4368 BFD_ASSERT (local_got_offsets != NULL);
4369 off = local_got_offsets[r_symndx];
4370
4371 /* The offset must always be a multiple of 4. We use
4372 the least significant bit to record whether we have
4373 already generated the necessary reloc. */
4374 if (off & 1)
4375 off &= ~1;
4376 else
4377 {
4378 bfd_put_32 (output_bfd, relocation,
4379 htab->elf.sgot->contents + off);
4380 local_got_offsets[r_symndx] |= 1;
4381 if (bfd_link_pic (info))
4382 relative_reloc = TRUE;
4383 }
4384 }
4385 if (relative_reloc)
4386 {
4387 asection *srelgot;
4388 Elf_Internal_Rela outrel;
4389 bfd_byte *loc;
4390
4391 srelgot = htab->elf.srelgot;
4392 BFD_ASSERT (srelgot != NULL);
4393
4394 outrel.r_offset
4395 = (htab->elf.sgot->output_section->vma
4396 + htab->elf.sgot->output_offset + off);
4397 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4398 outrel.r_addend = relocation;
4399 loc = srelgot->contents;
4400 loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
4401 if (loc != NULL)
4402 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4403 }
4404 relocation = htab->elf.sgot->output_offset + off;
4405 break;
4406
4407 case R_CKCORE_GOTOFF_IMM18:
4408 case R_CKCORE_GOTOFF:
4409 case R_CKCORE_GOTOFF_HI16:
4410 case R_CKCORE_GOTOFF_LO16:
4411 /* Relocation is relative to the start of the global offset
4412 table. */
4413 /* Note that sgot->output_offset is not involved in this
4414 calculation. We always want the start of .got. If we
4415 defined _GLOBAL_OFFSET_TABLE in a different way, as is
4416 permitted by the ABI, we might have to change this
4417 calculation. */
4418 relocation -= htab->elf.sgot->output_section->vma;
4419 break;
4420
4421 case R_CKCORE_GOTPC:
4422 case R_CKCORE_GOTPC_HI16:
4423 case R_CKCORE_GOTPC_LO16:
4424 /* Use global offset table as symbol value. */
4425 relocation = htab->elf.sgot->output_section->vma;
4426 addend = -addend;
4427 unresolved_reloc = FALSE;
4428 break;
4429
4430 case R_CKCORE_DOFFSET_IMM18:
4431 case R_CKCORE_DOFFSET_IMM18BY2:
4432 case R_CKCORE_DOFFSET_IMM18BY4:
4433 {
4434 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4435 relocation -= sdata->output_section->vma;
4436 }
4437 break;
4438
4439 case R_CKCORE_DOFFSET_LO16:
4440 {
4441 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4442 relocation -= sdata->output_section->vma;
4443 }
4444 break;
4445
4446 case R_CKCORE_TOFFSET_LO16:
4447 {
4448 asection *stext = bfd_get_section_by_name (output_bfd, ".text");
4449 if (stext)
4450 relocation -= stext->output_section->vma;
4451 }
4452 break;
4453
4454 case R_CKCORE_PLT_IMM18BY4:
4455 case R_CKCORE_PLT32:
4456 /* Relocation is to the entry for this symbol in the
4457 procedure linkage table. */
4458
4459 /* Resolve a PLT32 reloc against a local symbol directly,
4460 without using the procedure linkage table. */
4461 if (h == NULL)
4462 break;
4463
4464 if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
4465 {
4466 /* We didn't make a PLT entry for this symbol. This
4467 happens when statically linking PIC code, or when
4468 using -Bsymbolic. */
4469 if (h->got.offset != (bfd_vma) -1)
4470 {
4471 bfd_boolean dyn;
4472
4473 off = h->got.offset;
4474 dyn = htab->elf.dynamic_sections_created;
4475 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4476 bfd_link_pic (info), h)
4477 || (bfd_link_pic (info)
4478 && SYMBOL_REFERENCES_LOCAL (info, h))
4479 || (ELF_ST_VISIBILITY (h->other)
4480 && h->root.type == bfd_link_hash_undefweak))
4481 {
4482 /* This is actually a static link, or it is a
4483 -Bsymbolic link and the symbol is defined
4484 locally, or the symbol was forced to be local
4485 because of a version file. We must initialize
4486 this entry in the global offset table. Since the
4487 offset must always be a multiple of 4, we use the
4488 least significant bit to record whether we have
4489 initialized it already.
4490
4491 When doing a dynamic link, we create a .rela.dyn
4492 relocation entry to initialize the value. This
4493 is done in the finish_dynamic_symbol routine.
4494 FIXME! */
4495 if (off & 1)
4496 off &= ~1;
4497 else
4498 {
4499 h->got.offset |= 1;
4500 if (GENERATE_RELATIVE_RELOC_P (info, h))
4b24dd1a 4501 relative_reloc = TRUE;
b8891f8d
AJ
4502 }
4503 }
4504 bfd_put_32 (output_bfd, relocation,
4505 htab->elf.sgot->contents + off);
4506
4507 if (relative_reloc)
4508 {
4509 asection *srelgot;
4510 Elf_Internal_Rela outrel;
4511 bfd_byte *loc;
4512
4513 srelgot = htab->elf.srelgot;
4514 BFD_ASSERT (srelgot != NULL);
4515
4516 outrel.r_offset
4517 = (htab->elf.sgot->output_section->vma
4518 + htab->elf.sgot->output_offset + off);
4519 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4520 outrel.r_addend = relocation;
4521 loc = srelgot->contents;
4522 loc += (srelgot->reloc_count++
4523 * sizeof (Elf32_External_Rela));
4524 if (loc != NULL)
4525 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4526 }
4527 relocation = off + htab->elf.sgot->output_offset;
4528 }
4529 break;
4530 }
4531 /* The relocation is the got offset. */
4532 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
4533 relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
4534 else
4535 relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
4536 unresolved_reloc = FALSE;
4537 break;
4538
4539 case R_CKCORE_PCREL_IMM26BY2:
4540 case R_CKCORE_PCREL_JSR_IMM26BY2:
4541 case R_CKCORE_PCREL_JSR_IMM11BY2:
4542 case R_CKCORE_PCREL_IMM11BY2:
4543 case R_CKCORE_CALLGRAPH:
4544 /* Emit callgraph information first. */
4545 /* TODO: deal with callgraph. */
4546 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
4547 break;
4548 /* Some reloc need further handling. */
4549 /* h == NULL means the symbol is a local symbol,
4550 r_symndx == 0 means the symbol is 'ABS' and
4551 the relocation is already handled in assemble,
4552 here just use for callgraph. */
4553 /* TODO: deal with callgraph. */
4554 if (h == NULL && r_symndx == 0)
4555 {
4556 do_final_relocate = FALSE;
4557 break;
4558 }
4559
4560 /* Ignore weak references to undefined symbols. */
4561 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
4562 {
4563 do_final_relocate = FALSE;
4564 break;
4565 }
4566
4567 /* Using branch stub. */
4568 if (use_branch_stub == TRUE
4569 && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
4570 {
4571 struct elf32_csky_stub_hash_entry *stub_entry = NULL;
4572 if (sym_must_create_stub (h, info))
4573 stub_entry = elf32_csky_get_stub_entry (input_section,
4574 input_section,
4575 h, rel, htab);
4576 else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
4577 || disp < BSR_MAX_BWD_BRANCH_OFFSET)
4578 stub_entry = elf32_csky_get_stub_entry (input_section,
4579 input_section,
4580 h, rel, htab);
4581 if (stub_entry != NULL)
4582 relocation
4583 = (stub_entry->stub_offset
4584 + stub_entry->stub_sec->output_offset
4585 + stub_entry->stub_sec->output_section->vma);
4586 break;
4587 }
4588
4589 else if (h == NULL
4590 || (h->root.type == bfd_link_hash_defined
4591 && h->dynindx == -1)
4592 || ((h->def_regular && !h->def_dynamic)
4593 && (h->root.type != bfd_link_hash_defweak
4594 || ! bfd_link_pic (info))))
4595 {
4596 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
4597 {
4598 if (within_range (disp, 26))
4599 {
4600 /* In range for BSR32. */
4601 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
4602 read_content_substitute = CSKY_INSN_BSR32;
4603 }
4604 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
4605 /* if bsr32 cannot reach, generate
4606 "lrw r25, label; jsr r25" instead of
4607 jsri label. */
4608 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4609 } /* if ELF32_R_TYPE (rel->r_info)... */
4610 else if (ELF32_R_TYPE (rel->r_info)
4611 == R_CKCORE_PCREL_JSR_IMM11BY2)
4612 {
4613 if (within_range (disp, 11))
4614 {
4615 /* In range for BSR16. */
4616 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
4617 read_content_substitute = CSKY_INSN_BSR16;
4618 }
4619 }
4620 break;
4621 } /* else if h == NULL... */
4622
4623 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
4624 && (ELF32_R_TYPE (rel->r_info)
4625 == R_CKCORE_PCREL_JSR_IMM26BY2))
4626 {
4627 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4628 break;
4629 }
4630 /* Other situation, h->def_dynamic == 1,
4631 undefined_symbol when output file is shared object, etc. */
4632 /* Else fall through. */
4633
4634 case R_CKCORE_ADDR_HI16:
4635 case R_CKCORE_ADDR_LO16:
4636 if (bfd_link_pic (info)
4637 || (!bfd_link_pic (info)
4638 && h != NULL
4639 && h->dynindx != -1
4640 && !h->non_got_ref
4641 && ((h->def_dynamic && !h->def_regular)
4642 || (htab->elf.dynamic_sections_created
4643 && (h->root.type == bfd_link_hash_undefweak
4644 || h->root.type == bfd_link_hash_undefined
4645 || h->root.type == bfd_link_hash_indirect)))))
4646 {
4647 Elf_Internal_Rela outrel;
4648 bfd_boolean skip, relocate;
4649 bfd_byte *loc;
4650
4651 /* When generating a shared object, these relocations
4652 are copied into the output file to be resolved at
4653 run time. */
4654 skip = FALSE;
4655 relocate = FALSE;
4656
4657 outrel.r_offset =
4658 _bfd_elf_section_offset (output_bfd, info, input_section,
4659 rel->r_offset);
4660 if (outrel.r_offset == (bfd_vma) -1)
4661 skip = TRUE;
4662 else if (outrel.r_offset == (bfd_vma) -2)
4663 {
4664 skip = TRUE;
4665 relocate = TRUE;
4666 }
4667 outrel.r_offset += (input_section->output_section->vma
4668 + input_section->output_offset);
4669 if (skip)
4670 memset (&outrel, 0, sizeof (outrel));
4671 else if (h != NULL
4672 && h->dynindx != -1
4673 && (!bfd_link_pic (info)
4674 || (!SYMBOLIC_BIND (info, h)
4675 && h->root.type == bfd_link_hash_defweak)
4676 || !h->def_regular))
4677 {
4678 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4679 outrel.r_addend = rel->r_addend;
4680 }
4681 else
4682 {
4683 /* This symbol is local, or marked to become local. */
4684 relocate = TRUE;
4685 outrel.r_info = ELF32_R_INFO (0, r_type);
4686 outrel.r_addend = relocation + rel->r_addend;
4687 }
4688 loc = htab->elf.srelgot->contents;
4689 loc += (htab->elf.srelgot->reloc_count++
4690 * sizeof (Elf32_External_Rela));
4691
4692 if (loc != NULL)
4693 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4694
4695 /* If this reloc is against an external symbol, we do not
4696 want to diddle with the addend. Otherwise, we need to
4697 include the symbol value so that it becomes an addend
4698 for the dynamic reloc. */
4699 if (!relocate)
4700 continue;
4701 } /* if bfd_link_pic (info) ... */
4702 break;
4703
4704 case R_CKCORE_ADDR32:
4705 /* r_symndx will be zero only for relocs against symbols
4706 from removed linkonce sections, or sections discarded
4707 by a linker script.
4708 This relocation don't nedd to handle, the value will
4709 be set to SEC_DISCARDED(0xffffffff). */
4710 if (r_symndx == 0
4711 && strcmp (sec->name, ".csky_stack_size") == 0)
4712 {
4713 do_final_relocate = FALSE;
4714 break;
4715 }
4716 if (r_symndx >= symtab_hdr->sh_info
4717 && h->non_got_ref
4718 && bfd_link_executable (info))
4719 break;
4720
4721 if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
4722 break;
4723
4724 if (bfd_link_pic (info)
4725 || (h != NULL
4726 && h->dynindx != -1
4727 && ((h->def_dynamic && !h->def_regular)
4728 || (htab->elf.dynamic_sections_created
4729 && (h->root.type == bfd_link_hash_undefweak
4730 || h->root.type == bfd_link_hash_undefined
4731 || h->root.type == bfd_link_hash_indirect)))))
4732 {
4733 Elf_Internal_Rela outrel;
4734 bfd_boolean skip, relocate;
4735 bfd_byte *loc;
4736
4737 /* When generating a shared object, these relocations
4738 are copied into the output file to be resolved at
4739 run time. */
4740 skip = FALSE;
4741 relocate = FALSE;
4742
4743 outrel.r_offset =
4744 _bfd_elf_section_offset (output_bfd, info, input_section,
4745 rel->r_offset);
4746
4747 if (outrel.r_offset == (bfd_vma) -1)
4748 skip = TRUE;
4749 else if (outrel.r_offset == (bfd_vma) -2)
4750 {
4751 skip = TRUE;
4752 relocate = TRUE;
4753 }
4754
4755 outrel.r_offset += (input_section->output_section->vma
4756 + input_section->output_offset);
4757
4758 if (skip)
4759 memset (&outrel, 0, sizeof (outrel));
4760 else if (h != NULL
4761 && h->dynindx != -1
4762 && (!bfd_link_pic (info)
4763 || (!SYMBOLIC_BIND (info, h)
4764 && h->root.type == bfd_link_hash_defweak)
4765 || !h->def_regular))
4766 {
4767 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4768 outrel.r_addend = rel->r_addend;
4769 }
4770 else
4771 {
4772 /* This symbol is local, or marked to become local. */
4773 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4774 outrel.r_addend = relocation + rel->r_addend;
4775 }
4776
4777 loc = htab->elf.srelgot->contents;
4778 loc += (htab->elf.srelgot->reloc_count++
4779 * sizeof (Elf32_External_Rela));
4780
4781 if (loc != NULL)
4782 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4783
4784 /* If this reloc is against an external symbol, we do
4785 want to diddle with the addend. Otherwise, we need to
4786 include the symbol value so that it becomes an addend
4787 for the dynamic reloc. */
4788 if (! relocate)
4789 continue;
4790 }
4791 break;
4792
4793 case R_CKCORE_TLS_LDO32:
4794 relocation = relocation - dtpoff_base (info);
4795 break;
4796
4797 case R_CKCORE_TLS_LDM32:
4798 BFD_ASSERT (htab->elf.sgot != NULL);
4799 off = htab->tls_ldm_got.offset;
4800 if (off & 1)
4801 off &= ~1;
4802 else
4803 {
4804 /* If we don't know the module number,
4805 create a relocation for it. */
4806 if (!bfd_link_executable (info))
4807 {
4808 Elf_Internal_Rela outrel;
4809 bfd_byte *loc;
4810
4811 BFD_ASSERT (htab->elf.srelgot != NULL);
4812 outrel.r_addend = 0;
4813 outrel.r_offset
4814 = (htab->elf.sgot->output_section->vma
4815 + htab->elf.sgot->output_offset + off);
4816 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
4817 bfd_put_32 (output_bfd, outrel.r_addend,
4818 htab->elf.sgot->contents + off);
4819
4820 loc = htab->elf.srelgot->contents;
4821 loc += (htab->elf.srelgot->reloc_count++
4822 * sizeof (Elf32_External_Rela));
4823 if (loc)
4824 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4825 }
4826 else
4827 bfd_put_32 (output_bfd, 1,
4828 htab->elf.sgot->contents + off);
4829 htab->tls_ldm_got.offset |= 1;
4830 }
4831 relocation
4832 = (htab->elf.sgot->output_section->vma
4833 + htab->elf.sgot->output_offset + off
4834 - (input_section->output_section->vma
4835 + input_section->output_offset + rel->r_offset));
4836 break;
4837 case R_CKCORE_TLS_LE32:
4838 if (bfd_link_dll (info))
4839 {
4840 _bfd_error_handler
4841 /* xgettext:c-format */
4842 (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
4843 "in shared object"),
4844 input_bfd, input_section, (uint64_t)rel->r_offset,
4845 howto->name);
4846 return FALSE;
4847 }
4848 else
4849 relocation = tpoff (info, relocation);
4850 break;
4851 case R_CKCORE_TLS_GD32:
4852 case R_CKCORE_TLS_IE32:
4853 {
4854 int indx;
4855 char tls_type;
4856
4857 BFD_ASSERT (htab->elf.sgot != NULL);
4858
4859 indx = 0;
4860 if (h != NULL)
4861 {
4862 bfd_boolean dyn;
4863 dyn = htab->elf.dynamic_sections_created;
4864 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4865 bfd_link_pic (info), h)
4866 && (!bfd_link_pic (info)
4867 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4868 {
4869 unresolved_reloc = FALSE;
4870 indx = h->dynindx;
4871 }
4872 off = h->got.offset;
4873 tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
4874 }
4875 else
4876 {
4877 BFD_ASSERT (local_got_offsets != NULL);
4878 off = local_got_offsets[r_symndx];
4879 tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
4880 }
4881
4882 BFD_ASSERT (tls_type != GOT_UNKNOWN);
4883
4884 if (off & 1)
4885 off &= ~1;
4886 else
4887 {
4888 bfd_boolean need_relocs = FALSE;
4889 Elf_Internal_Rela outrel;
4890 bfd_byte *loc = NULL;
4891 int cur_off = off;
4892 /* The GOT entries have not been initialized yet. Do it
4893 now, and emit any relocations. If both an IE GOT and a
4894 GD GOT are necessary, we emit the GD first. */
4895 if ((!bfd_link_executable (info) || indx != 0)
4896 && (h == NULL
4897 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4898 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
4899 || h->root.type != bfd_link_hash_undefined))
4900 {
4901 need_relocs = TRUE;
4902 BFD_ASSERT (htab->elf.srelgot != NULL);
4903
4904 loc = htab->elf.srelgot->contents;
4905 loc += (htab->elf.srelgot->reloc_count
4906 * sizeof (Elf32_External_Rela));
4907 }
4908 if (tls_type & GOT_TLS_GD)
4909 {
4910 if (need_relocs)
4911 {
4912 outrel.r_addend = 0;
4913 outrel.r_offset
4914 = (htab->elf.sgot->output_section->vma
4915 + htab->elf.sgot->output_offset
4916 + cur_off);
4917 outrel.r_info
4918 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
4919 bfd_put_32 (output_bfd, outrel.r_addend,
4920 htab->elf.sgot->contents + cur_off);
4921 if (loc)
4922 bfd_elf32_swap_reloca_out (output_bfd,
4923 &outrel, loc);
4924 loc += sizeof (Elf32_External_Rela);
4925 htab->elf.srelgot->reloc_count++;
4926 if (indx == 0)
4927 bfd_put_32 (output_bfd,
4928 relocation - dtpoff_base (info),
4929 (htab->elf.sgot->contents
4930 + cur_off + 4));
4931 else
4932 {
4933 outrel.r_addend = 0;
4934 outrel.r_info
4935 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
4936 outrel.r_offset += 4;
4937 bfd_put_32 (output_bfd, outrel.r_addend,
4938 (htab->elf.sgot->contents
4939 + cur_off + 4));
4940 outrel.r_info =
4941 ELF32_R_INFO (indx,
4942 R_CKCORE_TLS_DTPOFF32);
4943 if (loc)
4944 bfd_elf32_swap_reloca_out (output_bfd,
4945 &outrel,
4946 loc);
4947 htab->elf.srelgot->reloc_count++;
4948 loc += sizeof (Elf32_External_Rela);
4949 }
4950
4951 }
4952 else
4953 {
4954 /* If are not emitting relocations for a
4955 general dynamic reference, then we must be in a
4956 static link or an executable link with the
4957 symbol binding locally. Mark it as belonging
4958 to module 1, the executable. */
4959 bfd_put_32 (output_bfd, 1,
4960 htab->elf.sgot->contents + cur_off);
4961 bfd_put_32 (output_bfd,
4962 relocation - dtpoff_base (info),
4963 htab->elf.sgot->contents
4964 + cur_off + 4);
4965 }
4966 cur_off += 8;
4967 }
4968 if (tls_type & GOT_TLS_IE)
4969 {
4970 if (need_relocs)
4971 {
4972 if (indx == 0)
4973 outrel.r_addend = relocation - dtpoff_base (info);
4974 else
4975 outrel.r_addend = 0;
4976 outrel.r_offset
4977 = (htab->elf.sgot->output_section->vma
4978 + htab->elf.sgot->output_offset + cur_off);
4979 outrel.r_info
4980 = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
4981
4982 bfd_put_32 (output_bfd, outrel.r_addend,
4983 htab->elf.sgot->contents + cur_off);
4984 if (loc)
4985 bfd_elf32_swap_reloca_out (output_bfd,
4986 &outrel, loc);
4987 htab->elf.srelgot->reloc_count++;
4988 loc += sizeof (Elf32_External_Rela);
4989 }
4990 else
4991 bfd_put_32 (output_bfd, tpoff (info, relocation),
4992 htab->elf.sgot->contents + cur_off);
4993 }
4994 if (h != NULL)
4995 h->got.offset |= 1;
4996 else
4997 local_got_offsets[r_symndx] |= 1;
4998 }
4999 if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
5000 off += 8;
5001 relocation
5002 = (htab->elf.sgot->output_section->vma
5003 + htab->elf.sgot->output_offset + off
5004 - (input_section->output_section->vma
5005 + input_section->output_offset
5006 + rel->r_offset));
5007 break;
5008 }
5009 default:
5010 /* No substitution when final linking. */
5011 read_content_substitute = 0;
5012 break;
5013 } /* End switch (howto->type). */
5014
5015 /* Make sure 32-bit data in the text section will not be affected by
5016 our special endianness.
5017 However, this currently affects noting, since the ADDR32 howto type
5018 does no change with the data read. But we may need this mechanism in
5019 the future. */
5020
5021 if (howto->size == 2
5022 && (howto->type == R_CKCORE_ADDR32
fe75f42e 5023 || howto->type == R_CKCORE_PCREL32
b8891f8d
AJ
5024 || howto->type == R_CKCORE_GOT32
5025 || howto->type == R_CKCORE_GOTOFF
5026 || howto->type == R_CKCORE_GOTPC
5027 || howto->type == R_CKCORE_PLT32
5028 || howto->type == R_CKCORE_TLS_LE32
5029 || howto->type == R_CKCORE_TLS_IE32
5030 || howto->type == R_CKCORE_TLS_LDM32
5031 || howto->type == R_CKCORE_TLS_GD32
5032 || howto->type == R_CKCORE_TLS_LDO32
5033 || howto->type == R_CKCORE_RELATIVE))
5034 need_reverse_bits = 0;
5035 else
5036 need_reverse_bits = 1;
5037 /* Do the final link. */
5038 if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
5039 && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
5040 && howto->type != R_CKCORE_CALLGRAPH
5041 && do_final_relocate)
5042 r = csky_final_link_relocate (howto, input_bfd, input_section,
5043 contents, rel->r_offset,
5044 relocation, addend);
5045
5046 if (r != bfd_reloc_ok)
5047 {
5048 ret = FALSE;
5049 switch (r)
5050 {
5051 default:
5052 break;
5053 case bfd_reloc_overflow:
5054 if (h != NULL)
5055 name = NULL;
5056 else
5057 {
5058 name = bfd_elf_string_from_elf_section (input_bfd,
5059 symtab_hdr->sh_link,
5060 sym->st_name);
5061 if (name == NULL)
5062 break;
5063 if (*name == '\0')
fd361982 5064 name = bfd_section_name (sec);
b8891f8d
AJ
5065 }
5066 (*info->callbacks->reloc_overflow)
5067 (info,
5068 (h ? &h->root : NULL),
5069 name, howto->name, (bfd_vma) 0,
5070 input_bfd, input_section, rel->r_offset);
5071 break;
5072 }
5073 }
5074 } /* End for (;rel < relend; rel++). */
5075 return ret;
5076}
5077
5078static bfd_boolean
5079csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5080{
5081 int offset;
5082 size_t size;
5083
5084 switch (note->descsz)
5085 {
5086 default:
5087 return FALSE;
5088 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5089 case 148:
5090 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5091 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5092 offset = 72;
5093 size = 72;
5094 break;
5095 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5096 case 220:
5097 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5098 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5099 offset = 72;
5100 size = 34 * 4;
5101 break;
5102 }
5103 /* Make a ".reg/999" section. */
5104 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5105 size, note->descpos + offset);
5106}
5107
5108static bfd_boolean
5109csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5110{
5111 switch (note->descsz)
5112 {
5113 default:
5114 return FALSE;
5115
5116 /* Sizeof (struct elf_prpsinfo) on linux csky. */
5117 case 124:
5118 elf_tdata (abfd)->core->program
5119 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5120 elf_tdata (abfd)->core->command
5121 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5122 }
5123
5124 /* Note that for some reason, a spurious space is tacked
5125 onto the end of the args in some (at least one anyway)
5126 implementations, so strip it off if it exists. */
5127 {
5128 char *command = elf_tdata (abfd)->core->command;
5129 int n = strlen (command);
5130
5131 if (0 < n && command[n - 1] == ' ')
5132 command[n - 1] = '\0';
5133 }
5134
5135 return TRUE;
5136}
5137
5138/* End of external entry points for sizing and building linker stubs. */
5139
5140/* CPU-related basic API. */
5141#define TARGET_BIG_SYM csky_elf32_be_vec
5142#define TARGET_BIG_NAME "elf32-csky-big"
5143#define TARGET_LITTLE_SYM csky_elf32_le_vec
5144#define TARGET_LITTLE_NAME "elf32-csky-little"
5145#define ELF_ARCH bfd_arch_csky
5146#define ELF_MACHINE_CODE EM_CSKY
5147#define ELF_MACHINE_ALT1 EM_CSKY_OLD
5148#define ELF_MAXPAGESIZE 0x1000
5149#define elf_info_to_howto csky_elf_info_to_howto
5150#define elf_info_to_howto_rel NULL
5151#define elf_backend_special_sections csky_elf_special_sections
5152#define bfd_elf32_bfd_link_hash_table_create csky_elf_link_hash_table_create
5153
5154/* Target related API. */
5155#define bfd_elf32_mkobject csky_elf_mkobject
5156#define bfd_elf32_bfd_merge_private_bfd_data csky_elf_merge_private_bfd_data
5157#define bfd_elf32_bfd_set_private_flags csky_elf_set_private_flags
5158#define elf_backend_copy_indirect_symbol csky_elf_copy_indirect_symbol
5159
5160/* GC section related API. */
5161#define elf_backend_can_gc_sections 1
5162#define elf_backend_gc_mark_hook csky_elf_gc_mark_hook
5163#define elf_backend_gc_mark_extra_sections elf32_csky_gc_mark_extra_sections
5164
5165/* Relocation related API. */
5166#define elf_backend_reloc_type_class csky_elf_reloc_type_class
5167#define bfd_elf32_bfd_reloc_type_lookup csky_elf_reloc_type_lookup
5168#define bfd_elf32_bfd_reloc_name_lookup csky_elf_reloc_name_lookup
5169#define elf_backend_ignore_discarded_relocs csky_elf_ignore_discarded_relocs
5170#define elf_backend_relocate_section csky_elf_relocate_section
5171#define elf_backend_check_relocs csky_elf_check_relocs
5172
5173/* Dynamic relocate related API. */
5174#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5175#define elf_backend_adjust_dynamic_symbol csky_elf_adjust_dynamic_symbol
5176#define elf_backend_size_dynamic_sections csky_elf_size_dynamic_sections
5177#define elf_backend_finish_dynamic_symbol csky_elf_finish_dynamic_symbol
5178#define elf_backend_finish_dynamic_sections csky_elf_finish_dynamic_sections
5179#define elf_backend_rela_normal 1
5180#define elf_backend_can_refcount 1
5181#define elf_backend_plt_readonly 1
5182#define elf_backend_want_got_sym 1
5183#define elf_backend_want_dynrelro 1
5184#define elf_backend_got_header_size 12
5185#define elf_backend_want_got_plt 1
5186
5187/* C-SKY coredump support. */
5188#define elf_backend_grok_prstatus csky_elf_grok_prstatus
5189#define elf_backend_grok_psinfo csky_elf_grok_psinfo
5190
5191#include "elf32-target.h"
This page took 0.310574 seconds and 4 git commands to generate.