]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-csky.c
Two fixes in dwarf-mode.el
[thirdparty/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
b8891f8d
AJ
1211 /* Data for R_CKCORE_TLS_LDM32 relocations. */
1212 union
1213 {
1214 bfd_signed_vma refcount;
1215 bfd_vma offset;
1216 } tls_ldm_got;
1217
1218 /* The stub hash table. */
1219 struct bfd_hash_table stub_hash_table;
1220
1221 /* Linker stub bfd. */
1222 bfd *stub_bfd;
1223
1224 /* Linker call-backs. */
1225 asection * (*add_stub_section) (const char *, asection *);
1226 void (*layout_sections_again) (void);
1227
1228 /* Array to keep track of which stub sections have been created, and
1229 * information on stub grouping. */
1230 struct map_stub *stub_group;
1231
1232 /* Number of elements in stub_group. */
1233 unsigned int top_id;
1234
1235 /* Assorted information used by elf32_csky_size_stubs. */
1236 unsigned int bfd_count;
1237 unsigned int top_index;
1238 asection **input_list;
1239};
1240
1241/* We can't change vectors in the bfd target which will apply to
1242 data sections, however we only do this to the text sections. */
1243
1244static bfd_vma
1245csky_get_insn_32 (bfd *input_bfd,
1246 bfd_byte *location)
1247{
1248 if (bfd_big_endian (input_bfd))
1249 return bfd_get_32 (input_bfd, location);
1250 else
1251 return (bfd_get_16 (input_bfd, location) << 16
1252 | bfd_get_16 (input_bfd, location + 2));
1253}
1254
1255static void
1256csky_put_insn_32 (bfd *input_bfd,
1257 bfd_vma x,
1258 bfd_byte *location)
1259{
1260 if (bfd_big_endian (input_bfd))
1261 bfd_put_32 (input_bfd, x, location);
1262 else
1263 {
1264 bfd_put_16 (input_bfd, x >> 16, location);
1265 bfd_put_16 (input_bfd, x & 0xffff, location + 2);
1266 }
1267}
1268
1269/* Find or create a stub section. Returns a pointer to the stub section, and
1270 the section to which the stub section will be attached (in *LINK_SEC_P).
1271 LINK_SEC_P may be NULL. */
1272
1273static asection *
1274elf32_csky_create_or_find_stub_sec (asection **link_sec_p, asection *section,
1275 struct csky_elf_link_hash_table *htab)
1276{
1277 asection *link_sec;
1278 asection *stub_sec;
1279
1280 link_sec = htab->stub_group[section->id].link_sec;
1281 stub_sec = htab->stub_group[section->id].stub_sec;
1282 if (stub_sec == NULL)
1283 {
1284 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1285 if (stub_sec == NULL)
1286 {
1287 size_t namelen;
1288 bfd_size_type len;
1289 char *s_name;
1290
1291 namelen = strlen (link_sec->name);
1292 len = namelen + sizeof (STUB_SUFFIX);
1293 s_name = bfd_alloc (htab->stub_bfd, len);
1294 if (s_name == NULL)
1295 return NULL;
1296
1297 memcpy (s_name, link_sec->name, namelen);
1298 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1299 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1300 if (stub_sec == NULL)
1301 return NULL;
1302 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1303 }
1304 htab->stub_group[section->id].stub_sec = stub_sec;
1305 }
1306
1307 if (link_sec_p)
1308 *link_sec_p = link_sec;
1309
1310 return stub_sec;
1311}
1312
1313/* Build a name for an entry in the stub hash table. */
1314
1315static char *
1316elf32_csky_stub_name (const asection *input_section,
1317 const asection *sym_sec,
1318 const struct csky_elf_link_hash_entry *hash,
1319 const Elf_Internal_Rela *rel)
1320{
1321 char *stub_name;
1322 bfd_size_type len;
1323
1324 if (hash)
1325 {
1326 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
1327 stub_name = bfd_malloc (len);
1328 if (stub_name != NULL)
1329 sprintf (stub_name, "%08x_%s+%x",
1330 input_section->id & 0xffffffff,
1331 hash->elf.root.root.string,
1332 (int) rel->r_addend & 0xffffffff);
1333 }
1334 else
1335 {
1336 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1337 stub_name = bfd_malloc (len);
1338 if (stub_name != NULL)
1339 sprintf (stub_name, "%08x_%x:%x+%x",
1340 input_section->id & 0xffffffff,
1341 sym_sec->id & 0xffffffff,
1342 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1343 (int) rel->r_addend & 0xffffffff);
1344 }
1345
1346 return stub_name;
1347}
1348
1349/* Determine the type of stub needed, if any, for a call. */
1350
1351static enum elf32_csky_stub_type
1352csky_type_of_stub (struct bfd_link_info *info,
1353 asection *input_sec,
1354 const Elf_Internal_Rela *rel,
1355 unsigned char st_type,
1356 struct csky_elf_link_hash_entry *hash,
1357 bfd_vma destination,
1358 asection *sym_sec ATTRIBUTE_UNUSED,
1359 bfd *input_bfd ATTRIBUTE_UNUSED,
1360 const char *name ATTRIBUTE_UNUSED)
1361{
1362 bfd_vma location;
1363 bfd_signed_vma branch_offset;
1364 unsigned int r_type;
1365 enum elf32_csky_stub_type stub_type = csky_stub_none;
1366 struct elf_link_hash_entry * h = &hash->elf;
1367
1368 /* We don't know the actual type of destination in case it is of
1369 type STT_SECTION: give up. */
1370 if (st_type == STT_SECTION)
1371 return stub_type;
1372
1373 location = (input_sec->output_offset
1374 + input_sec->output_section->vma
1375 + rel->r_offset);
1376
1377 branch_offset = (bfd_signed_vma)(destination - location);
1378 r_type = ELF32_R_TYPE (rel->r_info);
1379 if (r_type == R_CKCORE_PCREL_IMM26BY2
1380 && ((h != NULL
1381 && ((h->def_dynamic && !h->def_regular)
1382 || (bfd_link_pic (info)
1383 && h->root.type == bfd_link_hash_defweak)))
1384 || branch_offset > BSR_MAX_FWD_BRANCH_OFFSET
1385 || branch_offset < BSR_MAX_BWD_BRANCH_OFFSET))
1386 {
1387 if (bfd_csky_arch (info->output_bfd) == CSKY_ARCH_810
1388 || bfd_csky_arch (info->output_bfd) == CSKY_ARCH_807)
1389 stub_type = csky_stub_long_branch_jmpi;
1390 else
1391 stub_type = csky_stub_long_branch;
1392 }
1393
1394 return stub_type;
1395}
1396
1397/* Create an entry in an C-SKY ELF linker hash table. */
1398
1399static struct bfd_hash_entry *
1400csky_elf_link_hash_newfunc (struct bfd_hash_entry * entry,
1401 struct bfd_hash_table * table,
1402 const char * string)
1403{
1404 struct csky_elf_link_hash_entry * ret =
1405 (struct csky_elf_link_hash_entry *) entry;
1406
1407 /* Allocate the structure if it has not already been allocated by a
1408 subclass. */
1409 if (ret == NULL)
1410 {
1411 ret = (struct csky_elf_link_hash_entry *)
1412 bfd_hash_allocate (table,
1413 sizeof (struct csky_elf_link_hash_entry));
1414 if (ret == NULL)
1415 return (struct bfd_hash_entry *) ret;
1416 }
1417
1418 /* Call the allocation method of the superclass. */
1419 ret = ((struct csky_elf_link_hash_entry *)
1420 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret,
1421 table, string));
1422 if (ret != NULL)
1423 {
1424 struct csky_elf_link_hash_entry *eh;
1425
1426 eh = (struct csky_elf_link_hash_entry *) ret;
b8891f8d
AJ
1427 eh->plt_refcount = 0;
1428 eh->jsri2bsr_refcount = 0;
1429 eh->tls_type = GOT_NORMAL;
1430 ret->stub_cache = NULL;
1431 }
1432
1433 return (struct bfd_hash_entry *) ret;
1434}
1435
1436/* Initialize an entry in the stub hash table. */
1437
1438static struct bfd_hash_entry *
1439stub_hash_newfunc (struct bfd_hash_entry *entry,
1440 struct bfd_hash_table *table,
1441 const char *string)
1442{
1443 /* Allocate the structure if it has not already been allocated by a
1444 subclass. */
1445 if (entry == NULL)
1446 {
1447 entry = ((struct bfd_hash_entry *)
1448 bfd_hash_allocate (table,
1449 sizeof (struct elf32_csky_stub_hash_entry)));
1450 if (entry == NULL)
1451 return entry;
1452 }
1453
1454 /* Call the allocation method of the superclass. */
1455 entry = bfd_hash_newfunc (entry, table, string);
1456 if (entry != NULL)
1457 {
1458 struct elf32_csky_stub_hash_entry *eh;
1459
1460 /* Initialize the local fields. */
1461 eh = (struct elf32_csky_stub_hash_entry *) entry;
1462 eh->stub_sec = NULL;
1463 eh->stub_offset = 0;
1464 eh->target_value = 0;
1465 eh->target_section = NULL;
1466 eh->target_addend = 0;
1467 eh->stub_type = csky_stub_none;
1468 eh->stub_size = 0;
1469 eh->stub_template = NULL;
1470 eh->stub_template_size = -1;
1471 eh->h = NULL;
1472 eh->id_sec = NULL;
1473 eh->output_name = NULL;
1474 }
1475
1476 return entry;
1477}
1478
1479/* Free the derived linker hash table. */
1480
1481static void
1482csky_elf_link_hash_table_free (bfd *obfd)
1483{
1484 struct csky_elf_link_hash_table *ret
1485 = (struct csky_elf_link_hash_table *) obfd->link.hash;
1486
1487 bfd_hash_table_free (&ret->stub_hash_table);
1488 _bfd_elf_link_hash_table_free (obfd);
1489}
1490
1491/* Create an CSKY elf linker hash table. */
1492
1493static struct bfd_link_hash_table *
1494csky_elf_link_hash_table_create (bfd *abfd)
1495{
1496 struct csky_elf_link_hash_table *ret;
986f0783 1497 size_t amt = sizeof (struct csky_elf_link_hash_table);
b8891f8d
AJ
1498
1499 ret = (struct csky_elf_link_hash_table*) bfd_zmalloc (amt);
1500 if (ret == NULL)
1501 return NULL;
1502
1503 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1504 csky_elf_link_hash_newfunc,
1505 sizeof (struct csky_elf_link_hash_entry),
1506 CSKY_ELF_DATA))
1507 {
1508 free (ret);
1509 return NULL;
1510 }
1511
1512 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
1513 sizeof (struct elf32_csky_stub_hash_entry)))
1514 {
1515 free (ret);
1516 return NULL;
1517 }
1518 ret->elf.root.hash_table_free = csky_elf_link_hash_table_free;
1519 return &ret->elf.root;
1520}
1521
1522static bfd_boolean
1523csky_elf_mkobject (bfd *abfd)
1524{
1525 return bfd_elf_allocate_object (abfd, sizeof (struct csky_elf_obj_tdata),
1526 CSKY_ELF_DATA);
1527}
1528
1529/* Adjust a symbol defined by a dynamic object and referenced by a
1530 regular object. The current definition is in some section of the
1531 dynamic object, but we're not including those sections. We have to
1532 change the definition to something the rest of the link can
1533 understand. */
1534
1535static bfd_boolean
1536csky_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1537 struct elf_link_hash_entry *h)
1538{
1539 struct csky_elf_link_hash_entry *eh;
1540 struct csky_elf_link_hash_table *htab;
1541 asection *srel;
1542 asection *s;
1543 eh = (struct csky_elf_link_hash_entry *)h;
1544 if (eh == NULL)
1545 return FALSE;
1546
1547 htab = csky_elf_hash_table (info);
1548 if (htab == NULL)
1549 return FALSE;
1550
1551 /* Clear jsri2bsr_refcount, if creating shared library files. */
1552 if (bfd_link_pic (info) && eh->jsri2bsr_refcount > 0)
1553 eh->jsri2bsr_refcount = 0;
1554
1555 /* If there is a function, put it in the procedure linkage table. We
1556 will fill in the contents of the procedure linkage table later. */
1557 if (h->needs_plt)
1558 {
1559 /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
1560 symbol binds locally. */
1561 if (h->plt.refcount <= 0
1562 || (h->type != STT_GNU_IFUNC
1563 && (SYMBOL_CALLS_LOCAL (info, h)
1564 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1565 && h->root.type == bfd_link_hash_undefweak))))
1566
1567 {
1568 /* This case can occur if we saw a PLT32 reloc in an input
1569 file, but the symbol was never referred to by a dynamic
1570 object, or if all references were garbage collected. In
1571 such a case, we don't actually need to build a procedure
1572 linkage table, and we can just do a PC32 reloc instead. */
1573 h->plt.offset = (bfd_vma) -1;
1574 h->needs_plt = 0;
1575 if (h->got.refcount == 0)
1576 h->got.refcount += 1;
1577 }
1578 else if (h->got.refcount != 0)
1579 {
1580 h->got.refcount -= eh->plt_refcount;
1581 eh->plt_refcount = 0;
1582 }
1583 return TRUE;
1584 }
1585 else
1586 /* It's possible that we incorrectly decided a .plt reloc was
1587 needed for an R_CKCORE_PC32 or similar reloc to a non-function
1588 sym in check_relocs. We can't decide accurately between function
1589 and non-function syms in check_relocs; objects loaded later in
1590 the link may change h->type. So fix it now. */
1591 h->plt.offset = (bfd_vma) -1;
1592
1593 /* If this is a weak symbol, and there is a real definition, the
1594 processor independent code will have arranged for us to see the
1595 real definition first, and we can just use the same value. */
1596 if (h->is_weakalias)
1597 {
1598 struct elf_link_hash_entry *def = weakdef (h);
1599 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1600 h->root.u.def.section = def->root.u.def.section;
1601 h->root.u.def.value = def->root.u.def.value;
1602 return TRUE;
1603 }
1604
1605 /* If there are no non-GOT references, we do not need a copy
1606 relocation. */
1607 if (!h->non_got_ref)
1608 return TRUE;
1609
1610 /* This is a reference to a symbol defined by a dynamic object which
1611 is not a function. */
1612
1613 /* If we are creating a shared library, we must presume that the
1614 only references to the symbol are via the global offset table.
1615 For such cases we need not do anything here; the relocations will
1616 be handled correctly by relocate_section. */
1617 if (bfd_link_pic (info) || htab->elf.is_relocatable_executable)
1618 return TRUE;
1619
1620 /* We must allocate the symbol in our .dynbss section, which will
1621 become part of the .bss section of the executable. There will be
1622 an entry for this symbol in the .dynsym section. The dynamic
1623 object will contain position independent code, so all references
1624 from the dynamic object to this symbol will go through the global
1625 offset table. The dynamic linker will use the .dynsym entry to
1626 determine the address it must put in the global offset table, so
1627 both the dynamic object and the regular object will refer to the
1628 same memory location for the variable. */
1629 /* We must generate a R_CKCORE_COPY reloc to tell the dynamic linker to
1630 copy the initial value out of the dynamic object and into the
1631 runtime process image. We need to remember the offset into the
1632 .rela.bss section we are going to use. */
1633 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1634 {
1635 s = htab->elf.sdynrelro;
1636 srel = htab->elf.sreldynrelro;
1637 }
1638 else
1639 {
1640 s = htab->elf.sdynbss;
1641 srel = htab->elf.srelbss;
1642 }
1643 if (info->nocopyreloc == 0
1644 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1645 && h->size != 0
1646 && srel != NULL
1647 && s != NULL)
1648 {
1649 srel->size += sizeof (Elf32_External_Rela);
1650 h->needs_copy = 1;
1651 return _bfd_elf_adjust_dynamic_copy (info, h, s);
1652 }
1653
1654 h->non_got_ref = 0;
1655 return TRUE;
1656}
1657
1658/* Allocate space in .plt, .got and associated reloc sections for
1659 dynamic relocs. */
1660
1661static bfd_boolean
1662csky_allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1663{
1664 struct bfd_link_info *info;
1665 struct csky_elf_link_hash_table *htab;
1666 struct csky_elf_link_hash_entry *eh;
1667 struct elf_dyn_relocs *p;
1668
1669 /* For indirect case, such as _ZdlPv to _ZdlPv@@GLIBCXX_3.4. */
1670 if (h->root.type == bfd_link_hash_indirect)
1671 return TRUE;
1672
1673 if (h->root.type == bfd_link_hash_warning)
1674 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1675
1676
1677 info = (struct bfd_link_info *) inf;
1678 htab = csky_elf_hash_table (info);
1679 if (htab == NULL)
1680 return FALSE;
1681 /*TODO: how to deal with weak symbol relocs. */
1682 if ((htab->elf.dynamic_sections_created || h->type == STT_GNU_IFUNC)
1683 && h->plt.refcount > 0)
1684 {
1685 /* Make sure this symbol is output as a dynamic symbol.
1686 Undefined weak syms won't yet be marked as dynamic. */
1687 if (h->dynindx == -1 && !h->forced_local
1688 && h->root.type == bfd_link_hash_undefweak
1689 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1690 return FALSE;
1691 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1692 {
1693 asection *splt = htab->elf.splt;
1694
1695 /* If this is the first .plt entry, make room for the special
1696 first entry. */
1697 if (splt->size == 0)
1698 {
1699 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1700 splt->size += PLT_ENTRY_SIZE_P;
1701 else
1702 splt->size += PLT_ENTRY_SIZE;
1703 }
1704 h->plt.offset = splt->size;
1705
1706 /* If this symbol is not defined in a regular file, and we are
1707 not generating a shared library, then set the symbol to this
1708 location in the .plt. This is required to make function
1709 pointers compare as equal between the normal executable and
1710 the shared library. */
1711 if (!bfd_link_pic (info) && !h->def_regular)
1712 {
1713 h->root.u.def.section = splt;
1714 h->root.u.def.value = h->plt.offset;
1715 }
1716
1717 /* Make room for this entry. */
1718 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1719 splt->size += PLT_ENTRY_SIZE_P;
1720 else
1721 splt->size += PLT_ENTRY_SIZE;
1722 /* We also need to make an entry in the .rela.plt section. */
1723 htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
1724
1725 /* We also need to make an entry in the .got.plt section, which
1726 will be placed in the .got section by the linker script. */
1727 htab->elf.sgotplt->size += 4;
1728 }
1729 else
1730 {
1731 h->plt.offset = (bfd_vma) -1;
1732 h->needs_plt = 0;
1733 }
1734 }
1735 else
1736 {
1737 h->plt.offset = (bfd_vma) -1;
1738 h->needs_plt = 0;
1739 }
1740
1741 if (h->got.refcount > 0)
1742 {
1743 asection *sgot;
1744 bfd_boolean dyn;
1745 int indx;
1746
1747 int tls_type = csky_elf_hash_entry (h)->tls_type;
1748 /* Make sure this symbol is output as a dynamic symbol.
1749 Undefined weak syms won't yet be marked as dynamic. */
1750 if (h->dynindx == -1 && !h->forced_local
1751 && h->root.type == bfd_link_hash_undefweak
1752 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1753 return FALSE;
1754
1755 sgot = htab->elf.sgot;
1756 h->got.offset = sgot->size;
1757 BFD_ASSERT (tls_type != GOT_UNKNOWN);
1758 if (tls_type == GOT_NORMAL)
1759 /* Non-TLS symbols need one GOT slot. */
1760 sgot->size += 4;
1761 else
1762 {
1763 if (tls_type & GOT_TLS_GD)
1764 /* R_CKCORE_TLS_GD32 needs 2 consecutive GOT slots. */
1765 sgot->size += 8;
1766 if (tls_type & GOT_TLS_IE)
1767 /* R_CKCORE_TLS_IE32 needs one GOT slot. */
1768 sgot->size += 4;
1769 }
1770 dyn = htab->elf.dynamic_sections_created;
1771 indx = 0;
1772 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1773 && (! bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
1774 indx = h->dynindx;
1775
1776 if (tls_type != GOT_NORMAL
1777 && (bfd_link_pic (info) || indx != 0)
1778 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1779 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1780 || h->root.type != bfd_link_hash_undefweak))
1781 {
1782 if (tls_type & GOT_TLS_IE)
1783 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1784 if (tls_type & GOT_TLS_GD)
1785 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1786 if ((tls_type & GOT_TLS_GD) && indx != 0)
1787 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1788 }
1789 else if (((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1790 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1791 || h->root.type != bfd_link_hash_undefweak)
1792 && (bfd_link_pic (info)
1793 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
1794 || h->plt.offset == (bfd_vma) -1))
1795 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1796 }
1797 else
1798 h->got.offset = (bfd_vma) -1;
1799
1800 eh = (struct csky_elf_link_hash_entry *) h;
190eb1dd 1801 if (h->dyn_relocs == NULL)
b8891f8d
AJ
1802 return TRUE;
1803
1804 /* In the shared -Bsymbolic case, discard space allocated for
1805 dynamic pc-relative relocs against symbols which turn out to be
1806 defined in regular objects. For the normal shared case, discard
1807 space for pc-relative relocs that have become local due to symbol
1808 visibility changes. */
1809
1810 if (bfd_link_pic (info))
1811 {
1812 if (SYMBOL_CALLS_LOCAL (info, h))
1813 {
1814 struct elf_dyn_relocs **pp;
1815
190eb1dd 1816 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
b8891f8d
AJ
1817 {
1818 p->count -= p->pc_count;
1819 p->pc_count = 0;
1820 if (p->count == 0)
1821 *pp = p->next;
1822 else
1823 pp = &p->next;
1824 }
1825 }
1826
1827 if (eh->jsri2bsr_refcount
1828 && h->root.type == bfd_link_hash_defined
190eb1dd
L
1829 && h->dyn_relocs != NULL)
1830 h->dyn_relocs->count -= eh->jsri2bsr_refcount;
b8891f8d
AJ
1831
1832 /* Also discard relocs on undefined weak syms with non-default
1833 visibility. */
190eb1dd 1834 if (h->dyn_relocs != NULL
b8891f8d
AJ
1835 && h->root.type == bfd_link_hash_undefweak)
1836 {
1837 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1838 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 1839 h->dyn_relocs = NULL;
b8891f8d
AJ
1840
1841 /* Make sure undefined weak symbols are output as a dynamic
1842 symbol in PIEs. */
1843 else if (h->dynindx == -1
1844 && !h->forced_local
1845 && !bfd_elf_link_record_dynamic_symbol (info, h))
1846 return FALSE;
1847 }
1848
1849 }
1850 else
1851 {
1852 /* For the non-shared case, discard space for relocs against
1853 symbols which turn out to need copy relocs or are not
1854 dynamic. */
1855
1856 if (!h->non_got_ref
1857 && ((h->def_dynamic && !h->def_regular)
1858 || (htab->elf.dynamic_sections_created
1859 && (h->root.type == bfd_link_hash_undefweak
1860 || h->root.type == bfd_link_hash_indirect
1861 || h->root.type == bfd_link_hash_undefined))))
1862 {
1863 /* Make sure this symbol is output as a dynamic symbol.
1864 Undefined weak syms won't yet be marked as dynamic. */
1865 if (h->dynindx == -1 && !h->forced_local
1866 && h->root.type == bfd_link_hash_undefweak)
1867 {
1868 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1869 return FALSE;
1870 }
1871
1872 /* If that succeeded, we know we'll be keeping all the
1873 relocs. */
1874 if (h->dynindx != -1)
1875 goto keep;
1876 }
1877
190eb1dd 1878 h->dyn_relocs = NULL;
b8891f8d
AJ
1879
1880 keep: ;
1881 }
1882
1883 /* Finally, allocate space. */
190eb1dd 1884 for (p = h->dyn_relocs; p != NULL; p = p->next)
b8891f8d
AJ
1885 {
1886 asection *srelgot = htab->elf.srelgot;
1887 srelgot->size += p->count * sizeof (Elf32_External_Rela);
1888 }
1889
1890 return TRUE;
1891}
1892
b8891f8d
AJ
1893/* Set the sizes of the dynamic sections. */
1894
1895static bfd_boolean
1896csky_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1897 struct bfd_link_info *info)
1898{
1899 struct csky_elf_link_hash_table *htab;
1900 bfd *dynobj;
1901 asection *s;
1902 bfd_boolean relocs;
1903 bfd *ibfd;
1904
1905 htab = csky_elf_hash_table (info);
1906 if (htab == NULL)
1907 return FALSE;
1908 dynobj = htab->elf.dynobj;
1909 if (dynobj == NULL)
1910 return FALSE;
1911
1912 if (htab->elf.dynamic_sections_created)
1913 {
1914 /* Set the contents of the .interp section to the interpreter. */
1915 if (!bfd_link_pic (info) && !info->nointerp)
1916 {
1917 s = bfd_get_section_by_name (dynobj, ".interp");
1918 BFD_ASSERT (s != NULL);
1919 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1920 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1921 }
1922 }
1923
1924 /* Set up .got offsets for local syms, and space for local dynamic
1925 relocs. */
1926 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1927 {
1928 bfd_signed_vma *local_got_refcounts;
1929 bfd_signed_vma *end_local_got;
1930 bfd_size_type locsymcount;
1931 Elf_Internal_Shdr *symtab_hdr;
1932 asection *srelgot, *sgot;
1933 char *local_tls_type;
1934
1935 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1936 continue;
1937
1938 sgot = htab->elf.sgot;
1939 srelgot = htab->elf.srelgot;
1940
1941 for (s = ibfd->sections; s != NULL; s = s->next)
1942 {
1943 struct elf_dyn_relocs *p;
1944
1945 for (p = *((struct elf_dyn_relocs **)
1946 &elf_section_data (s)->local_dynrel);
1947 p != NULL;
1948 p = p->next)
1949 {
1950 if (!bfd_is_abs_section (p->sec)
1951 && bfd_is_abs_section (p->sec->output_section))
1952 /* Input section has been discarded, either because
1953 it is a copy of a linkonce section or due to
1954 linker script /DISCARD/, so we'll be discarding
1955 the relocs too. */
1956 ;
1957 else if (p->count != 0)
1958 {
1959 srelgot->size += p->count * sizeof (Elf32_External_Rela);
1960 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1961 info->flags |= DF_TEXTREL;
1962 }
1963 }
1964 }
1965
1966 local_got_refcounts = elf_local_got_refcounts (ibfd);
1967 if (!local_got_refcounts)
1968 continue;
1969
1970 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1971 locsymcount = symtab_hdr->sh_info;
1972 end_local_got = local_got_refcounts + locsymcount;
1973 local_tls_type = csky_elf_local_got_tls_type (ibfd);
1974
1975 for (; local_got_refcounts < end_local_got;
1976 ++local_got_refcounts, ++local_tls_type)
1977 {
1978 if (*local_got_refcounts > 0)
1979 {
1980 /* GOT_TLS_GD and GOT_TLS_IE type for TLS, GOT_NORMAL type
1981 for GOT. If output file is shared library, we should output
1982 GOT_TLS_GD type relocation in .rel.got. */
1983 *local_got_refcounts = sgot->size;
1984 if (*local_tls_type & GOT_TLS_GD)
1985 /* TLS_GD relocs need an 8-byte structure in the GOT. */
1986 sgot->size += 8;
1987 if (*local_tls_type & GOT_TLS_IE)
1988 sgot->size += 4;
1989 if (*local_tls_type == GOT_NORMAL)
1990 sgot->size += 4;
1991 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
1992 srelgot->size += sizeof (Elf32_External_Rela);
1993 }
1994 else
1995 *local_got_refcounts = (bfd_vma) -1;
1996 }
1997 }
1998
1999 if (htab->tls_ldm_got.refcount > 0)
2000 {
2001 /* Allocate two GOT entries and one dynamic relocation (if necessary)
2002 for R_CSKY_TLS_LDM32 relocations. */
2003 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2004 htab->elf.sgot->size += 8;
2005 if (bfd_link_pic (info))
2006 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
2007 }
2008 else
2009 htab->tls_ldm_got.offset = -1;
2010
2011 /* Allocate global sym .plt and .got entries, and space for global
2012 sym dynamic relocs. */
2013 elf_link_hash_traverse (&htab->elf, csky_allocate_dynrelocs, (PTR) info);
2014
2015 /* Check for GOT overflow. */
2016 if (check_got_overflow == 1
2017 && htab->elf.sgot->size + htab->elf.sgotplt->size > GOT_MAX_SIZE)
2018 {
2019 _bfd_error_handler (_("GOT table size out of range")); /* */
2020 return FALSE;
2021 }
2022
2023 /* We now have determined the sizes of the various dynamic sections.
2024 Allocate memory for them. */
2025 relocs = FALSE;
2026 for (s = dynobj->sections; s != NULL; s = s->next)
2027 {
2028 bfd_boolean strip_section = TRUE;
2029
2030 if ((s->flags & SEC_LINKER_CREATED) == 0)
2031 continue;
2032
2033 if (s == htab->elf.splt
2034 || s == htab->elf.sgot
2035 || s == htab->elf.sgotplt
2036 || s == htab->elf.sdynrelro
2037 || s == htab->elf.sreldynrelro)
2038 {
2039 /* Strip this section if we don't need it;
2040 see the comment below. */
2041 /* We'd like to strip these sections if they aren't needed, but if
2042 we've exported dynamic symbols from them we must leave them.
2043 It's too late to tell BFD to get rid of the symbols. */
2044
2045 if (htab->elf.hplt != NULL)
2046 strip_section = FALSE;
2047 }
fd361982 2048 else if (CONST_STRNEQ (bfd_section_name (s), ".rel") )
b8891f8d
AJ
2049 {
2050 if (s->size != 0 )
2051 relocs = TRUE;
2052
2053 /* We use the reloc_count field as a counter if we need
2054 to copy relocs into the output file. */
2055 s->reloc_count = 0;
2056 }
2057 else
2058 /* It's not one of our sections, so don't allocate space. */
2059 continue;
2060
2061 /* Strip this section if we don't need it; see the
2062 comment below. */
2063 if (s->size == 0)
2064 {
2065 /* If we don't need this section, strip it from the
2066 output file. This is mostly to handle .rel.bss and
2067 .rel.plt. We must create both sections in
2068 create_dynamic_sections, because they must be created
2069 before the linker maps input sections to output
2070 sections. The linker does that before
2071 adjust_dynamic_symbol is called, and it is that
2072 function which decides whether anything needs to go
2073 into these sections. */
2074 if (strip_section)
2075 s->flags |= SEC_EXCLUDE;
2076 continue;
2077 }
2078
2079 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2080 continue;
2081
2082 /* Allocate memory for the section contents. We use bfd_zalloc
2083 here in case unused entries are not reclaimed before the
2084 section's contents are written out. This should not happen,
2085 but this way if it does, we get a R_CKCORE_NONE reloc instead
2086 of garbage. */
2087 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2088 if (s->contents == NULL)
2089 return FALSE;
2090 }
2091
2092 if (htab->elf.dynamic_sections_created)
2c8e3708
L
2093 htab->elf.dt_pltgot_required = htab->elf.sgot->size != 0;
2094 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
b8891f8d
AJ
2095}
2096
2097/* Finish up dynamic symbol handling. We set the contents of various
2098 dynamic sections here. */
2099
2100static bfd_boolean
2101csky_elf_finish_dynamic_symbol (bfd *output_bfd,
2102 struct bfd_link_info *info,
2103 struct elf_link_hash_entry *h,
2104 Elf_Internal_Sym *sym)
2105{
2106 struct csky_elf_link_hash_table *htab;
2107
2108 htab = csky_elf_hash_table (info);
2109 if (htab == NULL)
2110 return FALSE;
2111
2112 /* Sanity check to make sure no unexpected symbol reaches here.
2113 This matches the test in csky_elf_relocate_section handling
2114 of GOT/PLT entries. */
2115 BFD_ASSERT (! (h->dynindx == -1
2116 && !h->forced_local
2117 && h->root.type != bfd_link_hash_undefweak
2118 && bfd_link_pic (info)));
2119
2120 if (h->plt.offset != (bfd_vma) -1)
2121 {
2122 bfd_vma plt_index;
2123 bfd_vma got_offset;
2124 Elf_Internal_Rela rel;
2125 bfd_byte *loc;
2126 asection *plt, *relplt, *gotplt;
2127
2128 plt = htab->elf.splt;
2129 relplt = htab->elf.srelplt;
2130 gotplt = htab->elf.sgotplt;
2131
2132 /* This symbol has an entry in the procedure linkage table. Set
2133 it up. */
2134 BFD_ASSERT (h->dynindx != -1
2135 || ((h->forced_local || bfd_link_executable (info))
2136 && h->def_regular));
2137 BFD_ASSERT (plt != NULL && gotplt != NULL && relplt != NULL);
2138 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2139 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2140 else
2141 plt_index = h->plt.offset / PLT_ENTRY_SIZE_P - 1;
2142 got_offset = (plt_index + 3) * 4;
2143
2144 /* Fill in the entry in the procedure linkage table. */
2145 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2146 {
2147 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[0],
2148 plt->contents + h->plt.offset);
2149 csky_put_insn_32 (output_bfd,
2150 (csky_elf_plt_entry_v2[1] | plt_index),
2151 plt->contents + h->plt.offset + 4);
2152 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[2],
2153 plt->contents + h->plt.offset + 8);
2154 }
2155 else
2156 {
2157 int i;
2158 for (i = 0; i < 6; i++)
2159 bfd_put_16 (output_bfd, csky_elf_plt_entry_v1[i],
2160 plt->contents + h->plt.offset + i * 2);
2161 bfd_put_32 (output_bfd, plt_index,
2162 plt->contents + h->plt.offset + i * 2);
2163 }
2164
2165 /* Fill in the entry in the .rel.plt section. */
2166 rel.r_offset = (htab->elf.sgotplt->output_section->vma
2167 + htab->elf.sgotplt->output_offset
2168 + got_offset);
2169 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_JUMP_SLOT);
2170 rel.r_addend = (plt->output_section->vma
2171 + plt->output_offset
2172 + h->plt.offset);
2173 loc = (htab->elf.srelplt->contents
2174 + plt_index * sizeof (Elf32_External_Rela));
2175
2176 if (loc != NULL)
2177 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2178 if (! h->def_regular)
2179 {
2180 /* Mark the symbol as undefined, rather than as defined in
2181 the .plt section. Leave the value alone. */
2182 sym->st_shndx = SHN_UNDEF;
2183 /* If the symbol is weak, we do need to clear the value.
2184 Otherwise, the PLT entry would provide a definition for
2185 the symbol even if the symbol wasn't defined anywhere,
2186 and so the symbol would never be NULL. Leave the value if
2187 there were any relocations where pointer equality matters
2188 (this is a clue for the dynamic linker, to make function
2189 pointer comparisons work between an application and shared
2190 library). */
2191 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
2192 sym->st_value = 0;
2193 }
2194 }
2195
2196 /* Fill in the entry in the .got section. */
2197 if (h->got.offset != (bfd_vma) -1
2198 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_GD) == 0)
2199 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_IE) == 0))
2200 {
2201 Elf_Internal_Rela rel;
2202 bfd_byte *loc;
2203
2204 /* This symbol has an entry in the global offset table.
2205 Set it up. */
2206 BFD_ASSERT (htab->elf.sgot != NULL && htab->elf.srelgot != NULL);
2207
2208 rel.r_offset = (htab->elf.sgot->output_section->vma
2209 + htab->elf.sgot->output_offset
2210 + (h->got.offset & ~(bfd_vma) 1));
2211
2212 /* If this is a static link, or it is a -Bsymbolic link and the
2213 symbol is defined locally or was forced to be local because
2214 of a version file, we just want to emit a RELATIVE reloc.
2215 The entry in the global offset table will already have been
2216 initialized in the relocate_section function. */
2217 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
2218 {
2219 BFD_ASSERT ((h->got.offset & 1) != 0);
2220 rel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
2221 rel.r_addend = (h->root.u.def.value
2222 + h->root.u.def.section->output_offset
2223 + h->root.u.def.section->output_section->vma);
2224 }
2225 else
2226 {
2227 BFD_ASSERT ((h->got.offset & 1) == 0);
2228 bfd_put_32 (output_bfd, (bfd_vma) 0,
2229 htab->elf.sgot->contents + h->got.offset);
2230 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_GLOB_DAT);
2231 rel.r_addend = 0;
2232 }
2233
2234 loc = htab->elf.srelgot->contents;
2235 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2236
2237 if (loc != NULL)
2238 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2239 }
2240
2241 if (h->needs_copy)
2242 {
2243 asection *s;
2244 Elf_Internal_Rela rela;
2245 bfd_byte *loc;
2246
2247 /* This symbol needs a copy reloc. Set it up. */
2248 BFD_ASSERT (h->dynindx != -1
2249 && (h->root.type == bfd_link_hash_defined
2250 || h->root.type == bfd_link_hash_defweak));
2251
2252 rela.r_offset = (h->root.u.def.value
2253 + h->root.u.def.section->output_section->vma
2254 + h->root.u.def.section->output_offset);
2255 rela.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_COPY);
2256 rela.r_addend = 0;
2257 if (h->root.u.def.section == htab->elf.sdynrelro)
2258 s = htab->elf.sreldynrelro;
2259 else
2260 s = htab->elf.srelbss;
2261 BFD_ASSERT (s != NULL);
2262 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2263 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2264 }
2265
2266 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2267 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2268 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2269 sym->st_shndx = SHN_ABS;
2270
2271 return TRUE;
2272}
2273
2274/* Finish up the dynamic sections. */
2275
2276static bfd_boolean
2277csky_elf_finish_dynamic_sections (bfd *output_bfd,
2278 struct bfd_link_info *info)
2279{
2280 struct csky_elf_link_hash_table *htab;
2281 bfd *dynobj;
2282 asection *sdyn;
2283 asection *got_sec;
2284
2285 htab = csky_elf_hash_table (info);
2286 if (htab == NULL)
2287 return FALSE;
2288
2289 dynobj = htab->elf.dynobj;
2290 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2291
2292 if (htab->elf.dynamic_sections_created)
2293 {
2294 Elf32_External_Dyn *dyncon, *dynconend;
2295
2296 BFD_ASSERT (sdyn != NULL && htab->elf.sgot != NULL);
2297
2298 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2299 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2300 for (; dyncon < dynconend; dyncon++)
2301 {
2302 Elf_Internal_Dyn dyn;
2303 bfd_boolean size = FALSE;
2304 const char *name = NULL;
2305
2306 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2307 switch (dyn.d_tag)
2308 {
2309 default:
2310 continue;
2311 case DT_RELA:
2312 name = ".rela.dyn";
2313 size = FALSE;
2314 break;
2315 case DT_RELASZ:
2316 name = ".rela.dyn";
2317 size = TRUE;
2318 break;
2319 case DT_PLTRELSZ:
2320 name = ".rela.plt";
2321 size = TRUE;
2322 break;
2323 case DT_PLTGOT:
2324 dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
2325 break;
2326 case DT_JMPREL:
2327 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma
2328 + htab->elf.srelplt->output_offset;
2329 break;
2330 }
2331
2332 if (name != NULL)
2333 {
2334 asection *s = bfd_get_section_by_name (output_bfd, name);
2335
2336 if (s == NULL)
2337 dyn.d_un.d_val = 0;
2338 else if (!size)
2339 dyn.d_un.d_ptr = s->vma;
2340 else
2341 dyn.d_un.d_val = s->size;
2342 }
2343 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2344 }
2345 }
2346
2347 /* Fill in the first three entries in the global offset table. */
2348 if (htab->elf.sgotplt)
2349 got_sec = htab->elf.sgotplt;
2350 else
2351 got_sec = htab->elf.sgot;
2352 if (got_sec != NULL)
2353 {
2354 if (got_sec->size > 0)
2355 {
2356 bfd_put_32 (output_bfd,
2357 (sdyn == NULL ? (bfd_vma) 0
2358 : sdyn->output_section->vma + sdyn->output_offset),
2359 got_sec->contents);
2360 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 4);
2361 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 8);
2362 }
2363 elf_section_data (got_sec->output_section)->this_hdr.sh_entsize = 4;
2364 }
2365 return TRUE;
2366}
2367
2368/* Copy the extra info we tack onto an elf_link_hash_entry. */
2369
2370static void
2371csky_elf_copy_indirect_symbol (struct bfd_link_info *info,
2372 struct elf_link_hash_entry *dir,
2373 struct elf_link_hash_entry *ind)
2374{
2375 struct csky_elf_link_hash_entry *edir, *eind;
2376
2377 edir = (struct csky_elf_link_hash_entry *) dir;
2378 eind = (struct csky_elf_link_hash_entry *) ind;
2379
b8891f8d
AJ
2380 if (ind->root.type == bfd_link_hash_indirect
2381 && dir->got.refcount <= 0)
2382 {
2383 edir->tls_type = eind->tls_type;
2384 eind->tls_type = GOT_UNKNOWN;
2385 }
2386 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2387}
2388
2389/* Used to decide how to sort relocs in an optimal manner for the
2390 dynamic linker, before writing them out. */
2391
2392static enum elf_reloc_type_class
2393csky_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2394 const asection *rel_sec ATTRIBUTE_UNUSED,
2395 const Elf_Internal_Rela *rela)
2396{
2397 switch ((int) ELF32_R_TYPE (rela->r_info))
2398 {
2399 case R_CKCORE_RELATIVE:
2400 return reloc_class_relative;
2401 case R_CKCORE_JUMP_SLOT:
2402 return reloc_class_plt;
2403 case R_CKCORE_COPY:
2404 return reloc_class_copy;
2405 case R_CKCORE_IRELATIVE:
2406 return reloc_class_ifunc;
2407 default:
2408 return reloc_class_normal;
2409 }
2410}
2411
2412/* Return the section that should be marked against GC for a given
2413 relocation. */
2414
2415static asection *
2416csky_elf_gc_mark_hook (asection *sec,
2417 struct bfd_link_info *info,
2418 Elf_Internal_Rela *rel,
2419 struct elf_link_hash_entry *h,
2420 Elf_Internal_Sym *sym)
2421{
2422 if (h != NULL)
2423 {
2424 switch (ELF32_R_TYPE (rel->r_info))
2425 {
2426 case R_CKCORE_GNU_VTINHERIT:
2427 case R_CKCORE_GNU_VTENTRY:
2428 return NULL;
2429 }
2430 }
2431
2432 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2433}
2434
2435/* Look through the relocs for a section during the first phase.
2436 Since we don't do .gots or .plts, we just need to consider the
2437 virtual table relocs for gc. */
2438
2439static bfd_boolean
2440csky_elf_check_relocs (bfd * abfd,
2441 struct bfd_link_info * info,
2442 asection * sec,
2443 const Elf_Internal_Rela * relocs)
2444{
2445 Elf_Internal_Shdr * symtab_hdr;
2446 struct elf_link_hash_entry ** sym_hashes;
2447 const Elf_Internal_Rela * rel;
2448 const Elf_Internal_Rela * rel_end;
2449 struct csky_elf_link_hash_table *htab;
2450 asection *sreloc;
2451
2452 /* if output type is relocatable, return. */
2453 if (bfd_link_relocatable (info))
2454 return TRUE;
2455
2456 htab = csky_elf_hash_table (info);
2457 if (htab == NULL)
2458 return FALSE;
2459
2460 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2461 sym_hashes = elf_sym_hashes (abfd);
2462
2463 rel_end = relocs + sec->reloc_count;
2464 sreloc = NULL;
2465 for (rel = relocs; rel < rel_end; rel++)
2466 {
2467 struct elf_link_hash_entry *h;
2468 unsigned long r_symndx;
2469 Elf_Internal_Sym *isym;
2470 int r_type;
2471
2472 r_symndx = ELF32_R_SYM (rel->r_info);
2473 r_type = ELF32_R_TYPE (rel->r_info);
2474 if (r_symndx < symtab_hdr->sh_info)
2475 {
2476 /* A local symbol. */
f1dfbfdb 2477 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
b8891f8d
AJ
2478 abfd, r_symndx);
2479 if (isym == NULL)
2480 return FALSE;
2481 h = NULL;
2482 }
2483 else
2484 {
2485 isym = NULL;
2486 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2487 while (h->root.type == bfd_link_hash_indirect
2488 || h->root.type == bfd_link_hash_warning)
2489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2490 }
2491
2492 switch (r_type)
2493 {
2494 case R_CKCORE_PCREL_IMM26BY2:
2495 case R_CKCORE_PCREL_IMM11BY2:
2496 case R_CKCORE_PCREL_JSR_IMM11BY2:
2497 case R_CKCORE_PCREL_JSR_IMM26BY2:
2498 /* If the symbol is '*UND*', means this reloc is used for
2499 * callgraph, don't need to leave to shared object. */
2500 if (r_symndx == 0)
2501 break;
2502 /* Else fall through. */
2503 case R_CKCORE_ADDR32:
2504 case R_CKCORE_ADDR_HI16:
2505 case R_CKCORE_ADDR_LO16:
2506 if (h != NULL
2507 && bfd_link_executable (info)
2508 && r_type == R_CKCORE_ADDR32
2509 && h->type == STT_OBJECT
2510 && (sec->flags & SEC_ALLOC) != 0
2511 && (sec->flags & SEC_READONLY))
2512 /* If this reloc is in a read-only section, we might
2513 need a copy reloc. We can't check reliably at this
2514 stage whether the section is read-only, as input
2515 sections have not yet been mapped to output sections.
2516 Tentatively set the flag for now, and correct in
2517 adjust_dynamic_symbol. */
2518 h->non_got_ref = 1;
2519
2520 /* If we are creating a shared library or relocatable executable,
2521 and this is a reloc against a global symbol, then we need to
2522 copy the reloc into the shared library. However, if we are
2523 linking with -Bsymbolic, we do not need to copy a reloc
2524 against a global symbol which is defined in an object we are
2525 including in the link (i.e., DEF_REGULAR is set). At
2526 this point we have not seen all the input files, so it is
2527 possible that DEF_REGULAR is not set now but will be set
2528 later (it is never cleared). We account for that possibility
2529 below by storing information in the relocs_copied field of
2530 the hash table entry. */
2531 if ((bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
2532 || (!bfd_link_pic (info)
2533 && (sec->flags & SEC_ALLOC) != 0
2534 && h != NULL
2535 && (h->root.type == bfd_link_hash_defweak
2536 || !h->def_regular)))
2537 {
2538 struct elf_dyn_relocs *p;
2539 struct elf_dyn_relocs **head;
2540 /* We must copy these reloc types into the output file.
2541 Create a reloc section in dynobj and make room for
2542 this reloc. */
2543 if (sreloc == NULL)
2544 {
2545 if (htab->elf.dynobj == NULL)
2546 htab->elf.dynobj = abfd;
2547
2548 sreloc = _bfd_elf_make_dynamic_reloc_section
2549 (sec, htab->elf.dynobj, 2, abfd, TRUE);
2550
2551 if (sreloc == NULL)
2552 return FALSE;
2553 }
2554
2555 if (h == NULL && !use_branch_stub
2556 && ((ELF32_R_TYPE (rel->r_info)
2557 == R_CKCORE_PCREL_IMM26BY2)
2558 || (ELF32_R_TYPE (rel->r_info)
2559 == R_CKCORE_PCREL_IMM11BY2)))
2560 break;
2561
2562 /* If this is a global symbol, we count the number of
2563 relocations we need for this symbol. */
2564 if (h != NULL)
2565 {
2566 struct csky_elf_link_hash_entry *eh;
2567 eh = (struct csky_elf_link_hash_entry *)h;
2568 if ((ELF32_R_TYPE (rel->r_info)
2569 == R_CKCORE_PCREL_JSR_IMM26BY2)
2570 || (ELF32_R_TYPE (rel->r_info)
2571 == R_CKCORE_PCREL_JSR_IMM11BY2))
2572 eh->jsri2bsr_refcount += 1;
190eb1dd 2573 head = &h->dyn_relocs;
b8891f8d
AJ
2574 }
2575 else
2576 {
2577 /* Track dynamic relocs needed for local syms too.
2578 We really need local syms available to do this
2579 easily. Oh well. */
2580 void **vpp;
2581 asection *s;
2582 Elf_Internal_Sym *loc_isym;
2583
f1dfbfdb 2584 loc_isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
b8891f8d
AJ
2585 abfd, r_symndx);
2586 if (loc_isym == NULL)
2587 return FALSE;
2588 s = bfd_section_from_elf_index (abfd, loc_isym->st_shndx);
2589 if (s == NULL)
2590 s = sec;
2591 vpp = &elf_section_data (s)->local_dynrel;
2592 head = (struct elf_dyn_relocs **)vpp;
2593 }
2594
2595 p = *head;
2596 if (p == NULL || p->sec != sec)
2597 {
986f0783 2598 size_t amt = sizeof *p;
b8891f8d
AJ
2599 p = ((struct elf_dyn_relocs *)
2600 bfd_alloc (htab->elf.dynobj, amt));
2601 if (p == NULL)
2602 return FALSE;
2603 p->next = *head;
2604 *head = p;
2605 p->sec = sec;
2606 p->count = 0;
2607 p->pc_count = 0;
2608 }
2609
2610 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2
2611 || ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM11BY2)
2612 p->pc_count += 1;
2613 p->count += 1;
2614 }
2615 break;
2616
2617 case R_CKCORE_PLT_IMM18BY4:
2618 case R_CKCORE_PLT32:
2619 /* This symbol requires a procedure linkage table entry. We
2620 actually build the entry in adjust_dynamic_symbol,
2621 because this might be a case of linking PIC code which is
2622 never referenced by a dynamic object, in which case we
2623 don't need to generate a procedure linkage table entry
2624 after all. */
2625
2626 /* If this is a local symbol, we resolve it directly without
2627 creating a procedure linkage table entry. */
2628 if (h == NULL)
2629 continue;
2630 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PLT_IMM18BY4)
2631 check_got_overflow = 1;
2632
2633 h->needs_plt = 1;
2634 h->plt.refcount += 1;
2635 h->got.refcount += 1;
2636 ((struct csky_elf_link_hash_entry *)h)->plt_refcount += 1;
2637 break;
2638
2639 case R_CKCORE_GOT12:
2640 case R_CKCORE_PLT12:
2641 case R_CKCORE_GOT32:
2642 case R_CKCORE_GOT_HI16:
2643 case R_CKCORE_GOT_LO16:
2644 case R_CKCORE_PLT_HI16:
2645 case R_CKCORE_PLT_LO16:
2646 case R_CKCORE_GOT_IMM18BY4:
2647 case R_CKCORE_TLS_IE32:
2648 case R_CKCORE_TLS_GD32:
2649 {
2650 int tls_type, old_tls_type;
2651
2652 if (h != NULL
2653 && bfd_link_executable (info)
2654 && r_type == R_CKCORE_GOT_IMM18BY4
2655 && (sec->flags & SEC_ALLOC) != 0
2656 && (sec->flags & SEC_READONLY))
2657 /* If this reloc is in a read-only section, we might
2658 need a copy reloc. We can't check reliably at this
2659 stage whether the section is read-only, as input
2660 sections have not yet been mapped to output sections.
2661 Tentatively set the flag for now, and correct in
2662 adjust_dynamic_symbol. */
2663 h->non_got_ref = 1;
2664
2665 switch (ELF32_R_TYPE (rel->r_info))
2666 {
2667 case R_CKCORE_TLS_IE32:
2668 tls_type = GOT_TLS_IE;
2669 break;
2670 case R_CKCORE_TLS_GD32:
2671 tls_type = GOT_TLS_GD;
2672 break;
2673 default:
2674 tls_type = GOT_NORMAL;
2675 break;
2676 }
2677 if (h != NULL)
2678 {
2679 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_GOT_IMM18BY4)
2680 check_got_overflow = 1;
2681 h->got.refcount += 1;
2682 old_tls_type = csky_elf_hash_entry (h)->tls_type;
2683 }
2684 else
2685 {
2686 bfd_signed_vma *local_got_refcounts;
2687
2688 /* This is a global offset table entry for a local symbol. */
2689 /* we can write a new function named
2690 elf32_csky_allocate_local_sym_info() to replace
2691 following code. */
2692 local_got_refcounts = elf_local_got_refcounts (abfd);
2693 if (local_got_refcounts == NULL)
2694 {
2695 bfd_size_type size;
2696
2697 size = symtab_hdr->sh_info;
2698 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2699 local_got_refcounts = ((bfd_signed_vma *)
2700 bfd_zalloc (abfd, size));
2701 if (local_got_refcounts == NULL)
2702 return FALSE;
2703 elf_local_got_refcounts (abfd) = local_got_refcounts;
2704 csky_elf_local_got_tls_type (abfd)
2705 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2706 }
2707 local_got_refcounts[r_symndx] += 1;
2708 old_tls_type = csky_elf_local_got_tls_type (abfd)[r_symndx];
2709 }
2710
2711 /* We will already have issued an error message if there is a
2712 TLS / non-TLS mismatch, based on the symbol type. We don't
2713 support any linker relaxations. So just combine any TLS
2714 types needed. */
2715 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2716 && tls_type != GOT_NORMAL)
2717 tls_type |= old_tls_type;
2718
2719 if (old_tls_type != tls_type)
2720 {
2721 if (h != NULL)
2722 csky_elf_hash_entry (h)->tls_type = tls_type;
2723 else
2724 csky_elf_local_got_tls_type (abfd)[r_symndx] = tls_type;
2725 }
2726 }
2727 /* Fall through. */
2728
2729 case R_CKCORE_TLS_LDM32:
2730 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_TLS_LDM32)
2731 htab->tls_ldm_got.refcount++;
2732 /* Fall through. */
2733
2734 case R_CKCORE_GOTOFF:
2735 case R_CKCORE_GOTPC:
2736 case R_CKCORE_GOTOFF_HI16:
2737 case R_CKCORE_GOTOFF_LO16:
2738 case R_CKCORE_GOTPC_HI16:
2739 case R_CKCORE_GOTPC_LO16:
2740 case R_CKCORE_GOTOFF_IMM18:
2741 if (htab->elf.sgot == NULL)
2742 {
2743 if (htab->elf.dynobj == NULL)
2744 htab->elf.dynobj = abfd;
2745 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2746 return FALSE;
2747 }
2748 break;
2749
2750 /* This relocation describes the C++ object vtable hierarchy.
2751 Reconstruct it for later use during GC. */
2752 case R_CKCORE_GNU_VTINHERIT:
2753 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2754 return FALSE;
2755 break;
2756
2757 /* This relocation describes which C++ vtable entries are actually
2758 used. Record for later use during GC. */
2759 case R_CKCORE_GNU_VTENTRY:
a0ea3a14 2760 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b8891f8d
AJ
2761 return FALSE;
2762 break;
2763 }
2764 }
2765
2766 return TRUE;
2767}
2768
2769static const struct bfd_elf_special_section csky_elf_special_sections[]=
2770{
2771 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2772 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2773 { NULL, 0, 0, 0, 0 }
2774};
2775
2776/* Function to keep CSKY specific flags in the ELF header. */
2777
2778static bfd_boolean
2779csky_elf_set_private_flags (bfd * abfd, flagword flags)
2780{
2781 BFD_ASSERT (! elf_flags_init (abfd)
2782 || elf_elfheader (abfd)->e_flags == flags);
2783
2784 elf_elfheader (abfd)->e_flags = flags;
2785 elf_flags_init (abfd) = TRUE;
2786 return TRUE;
2787}
2788
2789static csky_arch_for_merge *
2790csky_find_arch_with_eflag (const unsigned long arch_eflag)
2791{
2792 csky_arch_for_merge *csky_arch = NULL;
2793
2794 for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
2795 if (csky_arch->arch_eflag == arch_eflag)
2796 break;
2797 if (csky_arch == NULL)
2798 {
2799 _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
2800 arch_eflag);
2801 bfd_set_error (bfd_error_wrong_format);
2802 }
2803 return csky_arch;
2804}
2805
2806/* Merge backend specific data from an object file to the output
2807 object file when linking. */
2808
2809static bfd_boolean
2810csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2811{
2812 bfd *obfd = info->output_bfd;
2813 flagword old_flags;
2814 flagword new_flags;
2815 csky_arch_for_merge *old_arch = NULL;
2816 csky_arch_for_merge *new_arch = NULL;
2817
2818 /* Check if we have the same endianness. */
2819 if (! _bfd_generic_verify_endian_match (ibfd, info))
2820 return FALSE;
2821
2822 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2823 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2824 return TRUE;
2825
2826 new_flags = elf_elfheader (ibfd)->e_flags;
2827 old_flags = elf_elfheader (obfd)->e_flags;
2828
2829 if (! elf_flags_init (obfd))
2830 {
2831 /* First call, no flags set. */
2832 elf_flags_init (obfd) = TRUE;
2833 elf_elfheader (obfd)->e_flags = new_flags;
2834 }
2835 else if (new_flags == old_flags)
2836 /* Do nothing. */
2837 ;
2838 else if (new_flags == 0 || old_flags == 0)
2839 /* When one flag is 0, assign the other one's flag. */
2840 elf_elfheader (obfd)->e_flags = new_flags | old_flags;
2841 else
2842 {
2843 flagword newest_flag = 0;
2844
2845 if ((new_flags & CSKY_ARCH_MASK) != 0
2846 && (old_flags & CSKY_ARCH_MASK) != 0)
2847 {
2848 new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
2849 old_arch = csky_find_arch_with_eflag (old_flags & CSKY_ARCH_MASK);
2850 /* Collect flags like e, f, g. */
2851 newest_flag = (old_flags & (~CSKY_ARCH_MASK))
2852 | (new_flags & (~CSKY_ARCH_MASK));
2853 if (new_arch != NULL && old_arch != NULL)
2854 {
2855 if (new_arch->class != old_arch->class)
2856 {
2857 _bfd_error_handler
2858 /* xgettext:c-format */
2859 (_("%pB: machine flag conflict with target"), ibfd);
2860 bfd_set_error (bfd_error_wrong_format);
2861 return FALSE;
2862 }
2863 else if (new_arch->class_level != old_arch->class_level)
2864 {
2865 csky_arch_for_merge *newest_arch
2866 = (new_arch->class_level > old_arch->class_level
2867 ? new_arch : old_arch);
2868 if (new_arch->do_warning || old_arch->do_warning)
2869 {
2870 _bfd_error_handler
2871 /* xgettext:c-format */
2872 (_("warning: file %pB's arch flag ck%s conflicts with "
2873 "target ck%s, using ck%s"),
2874 ibfd, new_arch->name, old_arch->name,
2875 newest_arch->name);
2876 bfd_set_error (bfd_error_wrong_format);
2877 }
2878
2879 newest_flag |= newest_arch->arch_eflag;
2880 }
2881 else
2882 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2883 | (old_flags
2884 & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2885 }
2886 else
2887 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2888 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2889 }
2890 else
2891 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2892 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
2893
2894 elf_elfheader (obfd)->e_flags = newest_flag;
2895 }
2896 return TRUE;
2897}
2898
2899/* Ignore the discarded relocs in special sections in link time. */
2900
2901static bfd_boolean
2902csky_elf_ignore_discarded_relocs (asection *sec)
2903{
2904 if (strcmp (sec->name, ".csky_stack_size") == 0)
2905 return TRUE;
2906 return FALSE;
2907}
2908
2909/* .csky_stack_size are not referenced directly. This pass marks all of
2910 them as required. */
2911
2912static bfd_boolean
2913elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
2914 elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
2915{
2916 bfd *sub;
2917
2918 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
2919
2920 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2921 {
2922 asection *o;
2923
2924 for (o = sub->sections; o != NULL; o = o->next)
2925 if (strcmp (o->name, ".csky_stack_size") == 0)
2926 o->gc_mark = 1;
2927 }
2928
2929 return TRUE;
2930}
2931
2932/* The linker repeatedly calls this function for each input section,
2933 in the order that input sections are linked into output sections.
2934 Build lists of input sections to determine groupings between which
2935 we may insert linker stubs. */
2936
2937void
2938elf32_csky_next_input_section (struct bfd_link_info *info,
2939 asection *isec)
2940{
2941 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
2942 if (htab == NULL)
2943 return;
2944 if (isec->output_section->index <= htab->top_index)
2945 {
2946 asection **list = htab->input_list + isec->output_section->index;
2947
2948 if (*list != bfd_abs_section_ptr)
2949 {
2950 /* Steal the link_sec pointer for our list. */
2951#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2952 /* This happens to make the list in reverse order,
2953 which we reverse later in group_sections. */
2954 PREV_SEC (isec) = *list;
2955 *list = isec;
2956 }
2957 }
2958}
2959
2960/* See whether we can group stub sections together. Grouping stub
2961 sections may result in fewer stubs. More importantly, we need to
2962 put all .init* and .fini* stubs at the end of the .init or
2963 .fini output sections respectively, because glibc splits the
2964 _init and _fini functions into multiple parts. Putting a stub in
2965 the middle of a function is not a good idea. */
2966
2967static void
2968group_sections (struct csky_elf_link_hash_table *htab,
2969 bfd_size_type stub_group_size,
2970 bfd_boolean stubs_always_after_branch)
2971{
2972 asection **list = htab->input_list;
2973
2974 do
2975 {
2976 asection *tail = *list;
2977 asection *head;
2978
2979 if (tail == bfd_abs_section_ptr)
2980 continue;
2981
2982 /* Reverse the list: we must avoid placing stubs at the
4b24dd1a
AM
2983 beginning of the section because the beginning of the text
2984 section may be required for an interrupt vector in bare metal
2985 code. */
b8891f8d
AJ
2986#define NEXT_SEC PREV_SEC
2987 head = NULL;
2988 while (tail != NULL)
2989 {
2990 /* Pop from tail. */
2991 asection *item = tail;
2992 tail = PREV_SEC (item);
2993
2994 /* Push on head. */
2995 NEXT_SEC (item) = head;
2996 head = item;
2997 }
2998
2999 while (head != NULL)
3000 {
3001 asection *curr;
3002 asection *next;
3003 bfd_vma stub_group_start = head->output_offset;
3004 bfd_vma end_of_next;
3005
3006 curr = head;
3007 while (NEXT_SEC (curr) != NULL)
3008 {
3009 next = NEXT_SEC (curr);
3010 end_of_next = next->output_offset + next->size;
3011 if (end_of_next - stub_group_start >= stub_group_size)
3012 /* End of NEXT is too far from start, so stop. */
3013 break;
3014 curr = next;
3015 }
3016
3017 /* OK, the size from the start to the start of CURR is less
3018 * than stub_group_size and thus can be handled by one stub
3019 * section. (Or the head section is itself larger than
3020 * stub_group_size, in which case we may be toast.)
3021 * We should really be keeping track of the total size of
3022 * stubs added here, as stubs contribute to the final output
3023 * section size. */
3024 do
3025 {
3026 next = NEXT_SEC (head);
3027 /* Set up this stub group. */
3028 htab->stub_group[head->id].link_sec = curr;
3029 }
3030 while (head != curr && (head = next) != NULL);
3031
3032 /* But wait, there's more! Input sections up to stub_group_size
3033 * bytes after the stub section can be handled by it too. */
3034 if (!stubs_always_after_branch)
3035 {
3036 stub_group_start = curr->output_offset + curr->size;
3037
3038 while (next != NULL)
3039 {
3040 end_of_next = next->output_offset + next->size;
3041 if (end_of_next - stub_group_start >= stub_group_size)
3042 /* End of NEXT is too far from stubs, so stop. */
3043 break;
3044 /* Add NEXT to the stub group. */
3045 head = next;
3046 next = NEXT_SEC (head);
3047 htab->stub_group[head->id].link_sec = curr;
3048 }
3049 }
3050 head = next;
3051 }
3052 }
3053 while (list++ != htab->input_list + htab->top_index);
3054
3055 free (htab->input_list);
3056#undef PREV_SEC
3057#undef NEXT_SEC
3058}
3059
3060/* If the symbol referenced by bsr is defined in shared object file,
3061 or it is a weak symbol and we aim to create shared object file,
3062 we must create a stub for this bsr. */
3063
3064static bfd_boolean
3065sym_must_create_stub (struct elf_link_hash_entry *h,
3066 struct bfd_link_info *info)
3067{
3068 if (h != NULL
3069 && ((h->def_dynamic && !h->def_regular)
3070 || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
3071 return TRUE;
3072 else
3073 return FALSE;
3074}
3075
3076/* Calculate the template, template size and instruction size for a stub.
3077 Return value is the instruction size. */
3078
3079static unsigned int
3080find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
3081 const insn_sequence **stub_template,
3082 int *stub_template_size)
3083{
3084 const insn_sequence *template_sequence = NULL;
3085 int template_size = 0;
3086 int i;
3087 unsigned int size;
3088
3089 template_sequence = stub_definitions[stub_type].template_sequence;
3090 template_size = stub_definitions[stub_type].template_size;
3091
3092 size = 0;
3093 for (i = 0; i < template_size; i++)
3094 {
3095 switch (template_sequence[i].type)
3096 {
3097 case INSN16:
3098 size += 2;
3099 break;
3100
3101 case INSN32:
3102 case DATA_TYPE:
3103 size += 4;
3104 break;
3105
3106 default:
3107 BFD_FAIL ();
3108 return FALSE;
3109 }
3110 }
3111
3112 if (stub_template)
3113 *stub_template = template_sequence;
3114 if (stub_template_size)
3115 *stub_template_size = template_size;
3116
3117 return size;
3118}
3119
3120/* As above, but don't actually build the stub. Just bump offset so
3121 we know stub section sizes. */
3122
3123static bfd_boolean
3124csky_size_one_stub (struct bfd_hash_entry *gen_entry,
3125 void * in_arg ATTRIBUTE_UNUSED)
3126{
3127 struct elf32_csky_stub_hash_entry *stub_entry;
3128 const insn_sequence *template_sequence = NULL;
3129 int template_size = 0;
3130 int size = 0;
3131
3132 /* Massage our args to the form they really have. */
3133 stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
3134
3135 BFD_ASSERT (stub_entry->stub_type > csky_stub_none
3136 && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
3137 size = find_stub_size_and_template (stub_entry->stub_type,
3138 &template_sequence, &template_size);
3139 stub_entry->stub_size = size;
3140 stub_entry->stub_template = template_sequence;
3141 stub_entry->stub_template_size = template_size;
3142
3143 size = (size + 7) & ~7;
3144 stub_entry->stub_sec->size += size;
3145 return TRUE;
3146}
3147
3148/* Add a new stub entry to the stub hash. Not all fields of the new
3149 stub entry are initialised. */
3150
3151static struct elf32_csky_stub_hash_entry *
3152elf32_csky_add_stub (const char *stub_name,
3153 asection *section,
3154 struct csky_elf_link_hash_table *htab)
3155{
3156 asection *link_sec;
3157 asection *stub_sec;
3158 struct elf32_csky_stub_hash_entry *stub_entry;
3159
3160 stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
3161 if (stub_sec == NULL)
3162 return NULL;
3163
3164 /* Enter this entry into the linker stub hash table. */
3165 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3166 TRUE, FALSE);
3167 if (stub_entry == NULL)
3168 {
3169 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3170 section->owner, stub_name);
3171 return NULL;
3172 }
3173
3174 stub_entry->stub_sec = stub_sec;
3175 stub_entry->stub_offset = 0;
3176 stub_entry->id_sec = link_sec;
3177
3178 return stub_entry;
3179}
3180
3181/* Determine and set the size of the stub section for a final link.
3182 The basic idea here is to examine all the relocations looking for
3183 PC-relative calls to a target that is unreachable with a "bsr"
3184 instruction. */
3185
3186bfd_boolean
3187elf32_csky_size_stubs (bfd *output_bfd,
3188 bfd *stub_bfd,
3189 struct bfd_link_info *info,
3190 bfd_signed_vma group_size,
3191 asection *(*add_stub_section) (const char*, asection*),
3192 void (*layout_sections_again) (void))
3193{
3194 bfd_size_type stub_group_size;
3195 bfd_boolean stubs_always_after_branch;
3196 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3197
3198 if (htab == NULL)
3199 return FALSE;
3200
3201 /* Propagate mach to stub bfd, because it may not have been
3202 finalized when we created stub_bfd. */
3203 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3204 bfd_get_mach (output_bfd));
3205
3206 /* Stash our params away. */
3207 htab->stub_bfd = stub_bfd;
3208 htab->add_stub_section = add_stub_section;
3209 htab->layout_sections_again = layout_sections_again;
3210 stubs_always_after_branch = group_size < 0;
3211
3212 if (group_size < 0)
3213 stub_group_size = -group_size;
3214 else
3215 stub_group_size = group_size;
3216
3217 if (stub_group_size == 1)
3218 /* The 'bsr' range in abiv2 is +-64MB has to be used as the
3219 default maximum size.
3220 This value is 128K less than that, which allows for 131072
3221 byte stubs. If we exceed that, then we will fail to link.
3222 The user will have to relink with an explicit group size
3223 option. */
3224 stub_group_size = 66977792;
3225
3226 group_sections (htab, stub_group_size, stubs_always_after_branch);
3227
3228 while (1)
3229 {
3230 bfd *input_bfd;
3231 unsigned int bfd_indx;
3232 asection *stub_sec;
3233 bfd_boolean stub_changed = FALSE;
3234
3235 for (input_bfd = info->input_bfds, bfd_indx = 0;
3236 input_bfd != NULL;
3237 input_bfd = input_bfd->link.next, bfd_indx++)
3238 {
3239 Elf_Internal_Shdr *symtab_hdr;
3240 asection *section;
3241 Elf_Internal_Sym *local_syms = NULL;
3242
3243 /* We'll need the symbol table in a second. */
3244 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3245 if (symtab_hdr->sh_info == 0)
3246 continue;
3247
3248 /* Walk over each section attached to the input bfd. */
3249 for (section = input_bfd->sections;
3250 section != NULL;
3251 section = section->next)
3252 {
3253 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3254
3255 /* If there aren't any relocs, then there's nothing more
3256 * to do. */
3257 if ((section->flags & SEC_RELOC) == 0
3258 || section->reloc_count == 0
3259 || (section->flags & SEC_CODE) == 0)
3260 continue;
3261
3262 /* If this section is a link-once section that will be
3263 discarded, then don't create any stubs. */
3264 if (section->output_section == NULL
3265 || section->output_section->owner != output_bfd)
3266 continue;
3267
3268 /* Get the relocs. */
3269 internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
3270 section,
3271 NULL, NULL,
3272 info->keep_memory);
3273
3274 if (internal_relocs == NULL)
3275 goto error_ret_free_local;
3276
3277 /* Now examine each relocation. */
3278 irela = internal_relocs;
3279 irelaend = irela + section->reloc_count;
3280 for (; irela < irelaend; irela++)
3281 {
3282 unsigned int r_type, r_indx;
3283 enum elf32_csky_stub_type stub_type;
3284 struct elf32_csky_stub_hash_entry *stub_entry;
3285 asection *sym_sec;
3286 bfd_vma sym_value;
3287 bfd_vma destination;
3288 struct csky_elf_link_hash_entry *hash;
3289 const char *sym_name;
3290 char *stub_name;
3291 const asection *id_sec;
3292 unsigned char st_type;
3293
3294 r_type = ELF32_R_TYPE (irela->r_info);
3295 r_indx = ELF32_R_SYM (irela->r_info);
3296 if (r_type >= (unsigned int) R_CKCORE_MAX)
3297 {
3298 bfd_set_error (bfd_error_bad_value);
dc1e8a47 3299 error_ret_free_internal:
b8891f8d
AJ
3300 if (elf_section_data (section)->relocs == NULL)
3301 free (internal_relocs);
3302 goto error_ret_free_local;
3303 }
3304
3305 /* Only look for stubs on branch instructions. */
3306 if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
3307 continue;
3308 /* Now determine the call target, its name, value,
3309 section. */
3310 sym_sec = NULL;
3311 sym_value = 0;
3312 destination = 0;
3313 hash = NULL;
3314 sym_name = NULL;
3315 if (r_indx < symtab_hdr->sh_info)
3316 {
3317 /* It's a local symbol. */
3318 Elf_Internal_Sym *sym;
3319 Elf_Internal_Shdr *hdr;
3320 if (local_syms == NULL)
3321 local_syms =
3322 (Elf_Internal_Sym *) symtab_hdr->contents;
3323 if (local_syms == NULL)
3324 {
3325 local_syms =
3326 bfd_elf_get_elf_syms (input_bfd,
3327 symtab_hdr,
3328 symtab_hdr->sh_info,
3329 0, NULL, NULL, NULL);
3330 if (local_syms == NULL)
3331 goto error_ret_free_internal;
3332 }
3333 sym = local_syms + r_indx;
3334 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3335 sym_sec = hdr->bfd_section;
3336 if (!sym_sec)
3337 /* This is an undefined symbol. It can never
3338 be resolved. */
3339 continue;
3340 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3341 sym_value = sym->st_value;
3342 destination = (sym_value + irela->r_addend
3343 + sym_sec->output_offset
3344 + sym_sec->output_section->vma);
3345 st_type = ELF_ST_TYPE (sym->st_info);
3346 sym_name =
3347 bfd_elf_string_from_elf_section (input_bfd,
3348 symtab_hdr->sh_link,
3349 sym->st_name);
3350 }
3351 else
3352 {
3353 /* It's an external symbol. */
3354 int e_indx;
3355 e_indx = r_indx - symtab_hdr->sh_info;
3356 hash = ((struct csky_elf_link_hash_entry *)
3357 elf_sym_hashes (input_bfd)[e_indx]);
3358
3359 while (hash->elf.root.type == bfd_link_hash_indirect
3360 || hash->elf.root.type == bfd_link_hash_warning)
3361 hash = ((struct csky_elf_link_hash_entry *)
3362 hash->elf.root.u.i.link);
3363 if (hash->elf.root.type == bfd_link_hash_defined
3364 || hash->elf.root.type == bfd_link_hash_defweak)
3365 {
3366 sym_sec = hash->elf.root.u.def.section;
3367 sym_value = hash->elf.root.u.def.value;
3368
3369 struct csky_elf_link_hash_table *globals =
3370 csky_elf_hash_table (info);
3371 /* FIXME For a destination in a shared library. */
3372 if (globals->elf.splt != NULL && hash != NULL
3373 && hash->elf.plt.offset != (bfd_vma) -1)
3374 continue;
3375 else if (sym_sec->output_section != NULL)
3376 destination = (sym_value + irela->r_addend
3377 + sym_sec->output_offset
3378 + sym_sec->output_section->vma);
3379 }
3380 else if (hash->elf.root.type == bfd_link_hash_undefined
3381 || (hash->elf.root.type
3382 == bfd_link_hash_undefweak))
3383 /* FIXME For a destination in a shared library. */
3384 continue;
3385 else
3386 {
3387 bfd_set_error (bfd_error_bad_value);
3388 goto error_ret_free_internal;
3389 }
3390 st_type = ELF_ST_TYPE (hash->elf.type);
3391 sym_name = hash->elf.root.root.string;
3392 }
3393 do
3394 {
3395 /* Determine what (if any) linker stub is needed. */
3396 stub_type = csky_type_of_stub (info, section, irela,
3397 st_type, hash,
3398 destination, sym_sec,
3399 input_bfd, sym_name);
3400 if (stub_type == csky_stub_none)
3401 break;
3402
3403 /* Support for grouping stub sections. */
3404 id_sec = htab->stub_group[section->id].link_sec;
3405
3406 /* Get the name of this stub. */
3407 stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
3408 irela);
3409 if (!stub_name)
3410 goto error_ret_free_internal;
3411 /* We've either created a stub for this reloc already,
3412 or we are about to. */
3413 stub_entry
3414 = csky_stub_hash_lookup (&htab->stub_hash_table,
3415 stub_name,
3416 FALSE, FALSE);
3417 if (stub_entry != NULL)
3418 {
3419 /* The proper stub has already been created. */
3420 free (stub_name);
3421 stub_entry->target_value = sym_value;
3422 break;
3423 }
3424 stub_entry = elf32_csky_add_stub (stub_name, section,
3425 htab);
3426 if (stub_entry == NULL)
3427 {
3428 free (stub_name);
3429 goto error_ret_free_internal;
3430 }
3431 stub_entry->target_value = sym_value;
3432 stub_entry->target_section = sym_sec;
3433 stub_entry->stub_type = stub_type;
3434 stub_entry->h = hash;
3435 stub_entry->st_type = st_type;
3436
3437 if (sym_name == NULL)
3438 sym_name = "unnamed";
3439 stub_entry->output_name =
3440 bfd_alloc (htab->stub_bfd,
3441 (sizeof (STUB_ENTRY_NAME)
3442 + strlen (sym_name)));
3443 if (stub_entry->output_name == NULL)
3444 {
3445 free (stub_name);
3446 goto error_ret_free_internal;
3447 }
3448 sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3449 sym_name);
3450 stub_changed = TRUE;
3451 }
3452 while (0);
3453 }
3454 /* We're done with the internal relocs, free them. */
3455 if (elf_section_data (section)->relocs == NULL)
3456 free (internal_relocs);
3457 }
3458 }
3459 if (!stub_changed)
3460 break;
3461 /* OK, we've added some stubs. Find out the new size of the
3462 stub sections. */
3463 for (stub_sec = htab->stub_bfd->sections;
3464 stub_sec != NULL;
3465 stub_sec = stub_sec->next)
3466 {
3467 /* Ignore non-stub sections. */
3468 if (!strstr (stub_sec->name, STUB_SUFFIX))
3469 continue;
3470 stub_sec->size = 0;
3471 }
3472 bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
3473 /* Ask the linker to do its stuff. */
3474 (*htab->layout_sections_again) ();
3475 }
3476
3477 return TRUE;
dc1e8a47 3478 error_ret_free_local:
b8891f8d
AJ
3479 return FALSE;
3480}
3481
3482static bfd_boolean
3483csky_build_one_stub (struct bfd_hash_entry *gen_entry,
3484 void * in_arg)
3485{
3486#define MAXRELOCS 2
3487 struct elf32_csky_stub_hash_entry *stub_entry;
3488 struct bfd_link_info *info;
3489 asection *stub_sec;
3490 bfd *stub_bfd;
3491 bfd_byte *loc;
3492 bfd_vma sym_value;
3493 int template_size;
3494 int size;
3495 const insn_sequence *template_sequence;
3496 int i;
3497 struct csky_elf_link_hash_table * globals;
3498 int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3499 int stub_reloc_offset[MAXRELOCS] = {0, 0};
3500 int nrelocs = 0;
3501 struct elf_link_hash_entry *h = NULL;
3502
3503 /* Massage our args to the form they really have. */
3504 stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
3505 info = (struct bfd_link_info *) in_arg;
3506
abf874aa
CL
3507 /* Fail if the target section could not be assigned to an output
3508 section. The user should fix his linker script. */
3509 if (stub_entry->target_section->output_section == NULL
3510 && info->non_contiguous_regions)
53215f21
CL
3511 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
3512 "Retry without --enable-non-contiguous-regions.\n"),
3513 stub_entry->target_section);
abf874aa 3514
b8891f8d
AJ
3515 globals = csky_elf_hash_table (info);
3516 if (globals == NULL)
3517 return FALSE;
3518 stub_sec = stub_entry->stub_sec;
3519
3520 /* Make a note of the offset within the stubs for this entry. */
3521 stub_entry->stub_offset = stub_sec->size;
3522 loc = stub_sec->contents + stub_entry->stub_offset;
3523
3524 stub_bfd = stub_sec->owner;
3525
3526 /* This is the address of the stub destination. */
3527 h = &stub_entry->h->elf;
3528 if (sym_must_create_stub (h, info)
3529 && !(bfd_link_pic (info)
3530 && h->root.type == bfd_link_hash_defweak
3531 && h->def_regular
3532 && !h->def_dynamic))
3533 sym_value = 0;
3534 else
3535 sym_value = (stub_entry->target_value
3536 + stub_entry->target_section->output_offset
3537 + stub_entry->target_section->output_section->vma);
3538
3539 template_sequence = stub_entry->stub_template;
3540 template_size = stub_entry->stub_template_size;
3541
3542 size = 0;
3543 for (i = 0; i < template_size; i++)
3544 switch (template_sequence[i].type)
3545 {
3546 case INSN16:
3547 bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
3548 loc + size);
3549 size += 2;
3550 break;
3551 case INSN32:
3552 csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3553 loc + size);
3554 size += 4;
3555 break;
3556 case DATA_TYPE:
3557 bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3558 loc + size);
3559 stub_reloc_idx[nrelocs] = i;
3560 stub_reloc_offset[nrelocs++] = size;
3561 size += 4;
3562 break;
3563 default:
3564 BFD_FAIL ();
3565 return FALSE;
3566 }
3567 stub_sec->size += size;
3568
3569 /* Stub size has already been computed in csky_size_one_stub. Check
3570 consistency. */
3571 BFD_ASSERT (size == stub_entry->stub_size);
3572
3573 /* Assume there is at least one and at most MAXRELOCS entries to relocate
3574 in each stub. */
3575 BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3576
3577 for (i = 0; i < nrelocs; i++)
3578 {
3579 if (sym_must_create_stub (h, info))
3580 {
3581 Elf_Internal_Rela outrel;
3582 asection * sreloc = globals->elf.srelgot;
3583
3584 outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3585 outrel.r_info =
3586 ELF32_R_INFO (h->dynindx,
3587 template_sequence[stub_reloc_idx[i]].r_type);
3588 outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
3589
3590 loc = sreloc->contents;
3591 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3592
3593 if (loc != NULL)
3594 bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3595 }
3596 _bfd_final_link_relocate (elf32_csky_howto_from_type
3597 (template_sequence[stub_reloc_idx[i]].r_type),
3598 stub_bfd, stub_sec, stub_sec->contents,
3599 stub_entry->stub_offset + stub_reloc_offset[i],
3600 sym_value + stub_entry->target_addend,
3601 template_sequence[stub_reloc_idx[i]].reloc_addend);
3602 }
3603
3604 return TRUE;
3605#undef MAXRELOCS
3606}
3607
3608/* Build all the stubs associated with the current output file. The
3609 stubs are kept in a hash table attached to the main linker hash
3610 table. We also set up the .plt entries for statically linked PIC
3611 functions here. This function is called via arm_elf_finish in the
3612 linker. */
3613
3614bfd_boolean
3615elf32_csky_build_stubs (struct bfd_link_info *info)
3616{
3617 asection *stub_sec;
3618 struct bfd_hash_table *table;
3619 struct csky_elf_link_hash_table *htab;
3620
3621 htab = csky_elf_hash_table (info);
3622
3623 if (htab == NULL)
3624 return FALSE;
3625
3626 for (stub_sec = htab->stub_bfd->sections;
3627 stub_sec != NULL;
3628 stub_sec = stub_sec->next)
3629 {
3630 bfd_size_type size;
3631
3632 /* Ignore non-stub sections. */
3633 if (!strstr (stub_sec->name, STUB_SUFFIX))
3634 continue;
3635
3636 /* Allocate memory to hold the linker stubs. */
3637 size = stub_sec->size;
3638 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3639 if (stub_sec->contents == NULL && size != 0)
3640 return FALSE;
3641 stub_sec->size = 0;
3642 }
3643
3644 /* Build the stubs as directed by the stub hash table. */
3645 table = &htab->stub_hash_table;
3646 bfd_hash_traverse (table, csky_build_one_stub, info);
3647
3648 return TRUE;
3649}
3650
3651/* Set up various things so that we can make a list of input sections
3652 for each output section included in the link. Returns -1 on error,
3653 0 when no stubs will be needed, and 1 on success. */
3654
3655int
3656elf32_csky_setup_section_lists (bfd *output_bfd,
3657 struct bfd_link_info *info)
3658{
3659 bfd *input_bfd;
3660 unsigned int bfd_count;
3661 unsigned int top_id, top_index;
3662 asection *section;
3663 asection **input_list, **list;
986f0783 3664 size_t amt;
b8891f8d
AJ
3665 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3666
3667 if (!htab)
3668 return 0;
3669 if (! is_elf_hash_table (htab))
3670 return 0;
3671
3672 /* Count the number of input BFDs and find the top input section id. */
3673 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3674 input_bfd != NULL;
3675 input_bfd = input_bfd->link.next)
3676 {
3677 bfd_count += 1;
3678 for (section = input_bfd->sections;
3679 section != NULL;
3680 section = section->next)
3681 if (top_id < section->id)
3682 top_id = section->id;
3683 }
3684 htab->bfd_count = bfd_count;
3685 amt = sizeof (struct map_stub) * (top_id + 1);
3686 htab->stub_group = bfd_zmalloc (amt);
3687 if (htab->stub_group == NULL)
3688 return -1;
3689
3690 /* We can't use output_bfd->section_count here to find the top output
3691 section index as some sections may have been removed, and
3692 _bfd_strip_section_from_output doesn't renumber the indices. */
3693 for (section = output_bfd->sections, top_index = 0;
3694 section != NULL;
3695 section = section->next)
3696 if (top_index < section->index)
3697 top_index = section->index;
3698 htab->top_index = top_index;
3699 amt = sizeof (asection *) * (top_index + 1);
3700 input_list = bfd_malloc (amt);
3701 htab->input_list = input_list;
3702 if (input_list == NULL)
3703 return -1;
3704 /* For sections we aren't interested in, mark their entries with a
3705 value we can check later. */
3706 list = input_list + top_index;
3707 do
3708 *list = bfd_abs_section_ptr;
3709 while (list-- != input_list);
3710 for (section = output_bfd->sections;
3711 section != NULL;
3712 section = section->next)
3713 if ((section->flags & SEC_CODE) != 0)
3714 input_list[section->index] = NULL;
3715
3716 return 1;
3717}
3718
3719static bfd_reloc_status_type
3720csky_relocate_contents (reloc_howto_type *howto,
3721 bfd *input_bfd,
3722 long relocation,
3723 bfd_byte *location)
3724{
3725 int size;
3726 bfd_vma x = 0;
3727 bfd_reloc_status_type flag;
3728 unsigned int rightshift = howto->rightshift;
3729 unsigned int bitpos = howto->bitpos;
3730
3731 /* If the size is negative, negate RELOCATION. This isn't very
3732 general. */
3733 if (howto->size < 0)
3734 relocation = -relocation;
3735
3736 /* FIXME: these macros should be defined at file head or head file head. */
3737#define CSKY_INSN_ADDI_TO_SUBI 0x04000000
279edac5
AM
3738#define CSKY_INSN_MOV_RTB 0xc41d4820 /* mov32 rx, r29, 0 */
3739#define CSKY_INSN_MOV_RDB 0xc41c4820 /* mov32 rx, r28, 0 */
b8891f8d
AJ
3740#define CSKY_INSN_GET_ADDI_RZ(x) (((x) & 0x03e00000) >> 21)
3741#define CSKY_INSN_SET_MOV_RZ(x) ((x) & 0x0000001f)
3742#define CSKY_INSN_JSRI_TO_LRW 0xea9a0000
3743#define CSKY_INSN_JSR_R26 0xe8fa0000
3744
3745 /* Get the value we are going to relocate. */
3746 size = bfd_get_reloc_size (howto);
3747 switch (size)
3748 {
3749 default:
3750 case 0:
3751 abort ();
3752 case 1:
3753 x = bfd_get_8 (input_bfd, location);
3754 break;
3755 case 2:
3756 x = bfd_get_16 (input_bfd, location);
3757 break;
3758 case 4:
3759 if (need_reverse_bits)
3760 {
3761 x = csky_get_insn_32 (input_bfd, location);
3762
3763 if (R_CKCORE_DOFFSET_LO16 == howto->type)
3764 {
3765 if ((signed) relocation < 0)
3766 {
3767 x |= CSKY_INSN_ADDI_TO_SUBI;
3768 relocation = -relocation;
3769 }
3770 else if (0 == relocation)
3771 x = (CSKY_INSN_MOV_RDB |
3772 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3773 }
3774 else if (R_CKCORE_TOFFSET_LO16 == howto->type)
3775 {
3776 if ((signed) relocation < 0)
3777 {
3778 x |= CSKY_INSN_ADDI_TO_SUBI;
3779 relocation = -relocation;
3780 }
3781 else if (0 == relocation)
3782 x = (CSKY_INSN_MOV_RTB |
3783 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3784 }
3785 }
3786 else
3787 x = bfd_get_32 (input_bfd, location);
3788 break;
3789 }
3790 /* Check for overflow. FIXME: We may drop bits during the addition
3791 which we don't check for. We must either check at every single
3792 operation, which would be tedious, or we must do the computations
3793 in a type larger than bfd_vma, which would be inefficient. */
3794 flag = bfd_reloc_ok;
3795 if (howto->complain_on_overflow != complain_overflow_dont)
3796 {
3797 int addrmask;
3798 int fieldmask;
3799 int signmask;
3800 int ss;
3801 int a;
3802 int b;
3803 int sum;
3804 /* Get the values to be added together. For signed and unsigned
3805 relocations, we assume that all values should be truncated to
3806 the size of an address. For bitfields, all the bits matter.
3807 See also bfd_check_overflow. */
3808#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
3809 fieldmask = N_ONES (howto->bitsize);
3810 signmask = ~fieldmask;
3811 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3812 a = (relocation & addrmask) >> rightshift;
3813 if (read_content_substitute)
3814 x = read_content_substitute;
3815 b = (x & howto->src_mask & addrmask) >> bitpos;
3816
3817 switch (howto->complain_on_overflow)
3818 {
3819 case complain_overflow_signed:
3820 /* If any sign bits are set, all sign bits must be set.
3821 That is, A must be a valid negative address after
3822 shifting. */
3823 signmask = ~(fieldmask >> 1);
3824 /* Fall through. */
3825
3826 case complain_overflow_bitfield:
3827 /* Much like the signed check, but for a field one bit
3828 wider. We allow a bitfield to represent numbers in the
3829 range -2**n to 2**n-1, where n is the number of bits in the
3830 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
3831 can't overflow, which is exactly what we want. */
3832 ss = a & signmask;
3833 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3834 flag = bfd_reloc_overflow;
3835 /* We only need this next bit of code if the sign bit of B
3836 is below the sign bit of A. This would only happen if
3837 SRC_MASK had fewer bits than BITSIZE. Note that if
3838 SRC_MASK has more bits than BITSIZE, we can get into
3839 trouble; we would need to verify that B is in range, as
3840 we do for A above. */
3841 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3842 ss >>= bitpos;
3843
3844 /* Set all the bits above the sign bit. */
3845 b = (b ^ ss) - ss;
3846
3847 /* Now we can do the addition. */
3848 sum = a + b;
3849
3850 /* See if the result has the correct sign. Bits above the
3851 sign bit are junk now; ignore them. If the sum is
3852 positive, make sure we did not have all negative inputs;
3853 if the sum is negative, make sure we did not have all
3854 positive inputs. The test below looks only at the sign
3855 bits, and it really just
3856 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3857
3858 We mask with addrmask here to explicitly allow an address
3859 wrap-around. The Linux kernel relies on it, and it is
3860 the only way to write assembler code which can run when
3861 loaded at a location 0x80000000 away from the location at
3862 which it is linked. */
3863
3864 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3865 flag = bfd_reloc_overflow;
3866 break;
3867 case complain_overflow_unsigned:
3868 /* Checking for an unsigned overflow is relatively easy:
3869 trim the addresses and add, and trim the result as well.
3870 Overflow is normally indicated when the result does not
3871 fit in the field. However, we also need to consider the
3872 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3873 input is 0x80000000, and bfd_vma is only 32 bits; then we
3874 will get sum == 0, but there is an overflow, since the
3875 inputs did not fit in the field. Instead of doing a
3876 separate test, we can check for this by or-ing in the
3877 operands when testing for the sum overflowing its final
3878 field. */
3879 sum = (a + b) & addrmask;
3880 if ((a | b | sum) & signmask)
3881 flag = bfd_reloc_overflow;
3882 break;
3883 default:
3884 abort ();
3885 }
3886
3887 }
3888 /* Put RELOCATION in the right bits. */
3889 relocation >>= (bfd_vma) rightshift;
3890
3891 if ((howto->type == R_CKCORE_DOFFSET_LO16
3892 || howto->type == R_CKCORE_TOFFSET_LO16)
3893 && relocation == 0)
3894 /* Do nothing lsli32 rx, rz, 0. */
3895 ;
3896 else
3897 {
3898 /* Fir V1, all this relocation must be x -1. */
3899 if (howto->type == R_CKCORE_PCREL_IMM11BY2
3900 || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
3901 || howto->type == R_CKCORE_DOFFSET_LO16
3902 || howto->type == R_CKCORE_TOFFSET_LO16)
3903 relocation -= 1;
3904 else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
3905 relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
3906 else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
3907 relocation
3908 = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
3909 else if (howto->type == R_CKCORE_NOJSRI)
3910 {
3911 x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
3912 relocation = 0;
3913 csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
3914 }
3915
3916 relocation <<= (bfd_vma) bitpos;
3917 /* Add RELOCATION to the right bits of X. */
3918 x = ((x & ~howto->dst_mask)
3919 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
3920 }
3921 /* Put the relocated value back in the object file. */
3922 switch (size)
3923 {
3924 default:
3925 abort ();
3926 case 1:
3927 bfd_put_8 (input_bfd, x, location);
3928 break;
3929 case 2:
3930 bfd_put_16 (input_bfd, x, location);
3931 break;
3932 case 4:
3933 if (need_reverse_bits)
3934 csky_put_insn_32 (input_bfd, x, location);
3935 else
3936 bfd_put_32 (input_bfd, x, location);
3937 break;
3938 }
3939 return flag;
3940}
3941
3942/* Look up an entry in the stub hash. Stub entries are cached because
3943 creating the stub name takes a bit of time. */
3944
3945static struct elf32_csky_stub_hash_entry *
3946elf32_csky_get_stub_entry (const asection *input_section,
3947 const asection *sym_sec,
3948 struct elf_link_hash_entry *hash,
3949 const Elf_Internal_Rela *rel,
3950 struct csky_elf_link_hash_table *htab)
3951{
3952 struct elf32_csky_stub_hash_entry *stub_entry;
3953 struct csky_elf_link_hash_entry *h
3954 = (struct csky_elf_link_hash_entry *) hash;
3955 const asection *id_sec;
3956
3957 if ((input_section->flags & SEC_CODE) == 0)
3958 return NULL;
3959
3960 /* If this input section is part of a group of sections sharing one
3961 stub section, then use the id of the first section in the group.
3962 Stub names need to include a section id, as there may well be
3963 more than one stub used to reach say, printf, and we need to
3964 distinguish between them. */
3965 id_sec = htab->stub_group[input_section->id].link_sec;
3966 if (h != NULL && h->stub_cache != NULL
3967 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
3968 stub_entry = h->stub_cache;
3969 else
3970 {
3971 char *stub_name;
3972 stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
3973 if (stub_name == NULL)
3974 return NULL;
3975 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
3976 stub_name, FALSE, FALSE);
3977 if (h != NULL)
3978 h->stub_cache = stub_entry;
3979 free (stub_name);
3980 }
3981
3982 return stub_entry;
3983}
3984
3985static bfd_reloc_status_type
3986csky_final_link_relocate (reloc_howto_type *howto,
3987 bfd *input_bfd,
3988 asection *input_section,
3989 bfd_byte *contents,
3990 bfd_vma address,
3991 bfd_vma value,
3992 bfd_vma addend)
3993{
3994 bfd_vma relocation;
3995
3996 /* Sanity check the address. */
3997 if (address > bfd_get_section_limit (input_bfd, input_section))
3998 return bfd_reloc_outofrange;
3999
4000 /* This function assumes that we are dealing with a basic relocation
4001 against a symbol. We want to compute the value of the symbol to
4002 relocate to. This is just VALUE, the value of the symbol,
4003 plus ADDEND, any addend associated with the reloc. */
4004 relocation = value + addend;
4005
4006 /* If the relocation is PC relative, we want to set RELOCATION to
4007 the distance between the symbol (currently in RELOCATION) and the
4008 location we are relocating. Some targets (e.g., i386-aout)
4009 arrange for the contents of the section to be the negative of the
4010 offset of the location within the section; for such targets
4011 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4012 simply leave the contents of the section as zero; for such
4013 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4014 need to subtract out the offset of the location within the
4015 section (which is just ADDRESS). */
4016 if (howto->pc_relative)
4017 {
4018 relocation -= (input_section->output_section->vma
4019 + input_section->output_offset);
4020 if (howto->pcrel_offset)
4021 relocation -= address;
4022 }
4023
4024 return csky_relocate_contents (howto, input_bfd, relocation,
4025 contents + address);
4026
4027}
4028
4029/* Return the base VMA address which should be subtracted from real addresses
4030 when resolving @dtpoff relocation.
4031 This is PT_TLS segment p_vaddr. */
4032
4033static bfd_vma
4034dtpoff_base (struct bfd_link_info *info)
4035{
4036 /* If tls_sec is NULL, we should have signalled an error already. */
4037 if (elf_hash_table (info)->tls_sec == NULL)
4038 return 0;
4039 return elf_hash_table (info)->tls_sec->vma;
4040}
4041
4042/* Return the relocation value for @tpoff relocation
4043 if STT_TLS virtual address is ADDRESS. */
4044
4045static bfd_vma
4046tpoff (struct bfd_link_info *info, bfd_vma address)
4047{
4048 struct elf_link_hash_table *htab = elf_hash_table (info);
4049 bfd_vma base;
4050
4051 /* If tls_sec is NULL, we should have signalled an error already. */
4052 if (htab->tls_sec == NULL)
4053 return 0;
4054 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
4055 return address - htab->tls_sec->vma + base;
4056}
4057
4058/* Relocate a csky section. */
4059
4060static bfd_boolean
4061csky_elf_relocate_section (bfd * output_bfd,
4062 struct bfd_link_info * info,
4063 bfd * input_bfd,
4064 asection * input_section,
4065 bfd_byte * contents,
4066 Elf_Internal_Rela * relocs,
4067 Elf_Internal_Sym * local_syms,
4068 asection ** local_sections)
4069{
4070 Elf_Internal_Shdr *symtab_hdr;
4071 struct elf_link_hash_entry **sym_hashes;
4072 Elf_Internal_Rela *rel;
4073 Elf_Internal_Rela *relend;
4074 const char *name;
4075 bfd_boolean ret = TRUE;
4076 struct csky_elf_link_hash_table * htab;
4077 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
4078
4079 htab = csky_elf_hash_table (info);
4080 if (htab == NULL)
4081 return FALSE;
4082
4083 symtab_hdr = & elf_symtab_hdr (input_bfd);
4084 sym_hashes = elf_sym_hashes (input_bfd);
4085
4086 rel = relocs;
4087 relend = relocs + input_section->reloc_count;
4088 for (; rel < relend; rel++)
4089 {
4090 enum elf_csky_reloc_type r_type
4091 = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
4092 unsigned long r_symndx;
4093 reloc_howto_type * howto;
4094 Elf_Internal_Sym * sym;
4095 asection * sec;
4096 bfd_vma relocation;
4097 bfd_vma off;
4098 struct elf_link_hash_entry * h;
4099 bfd_vma addend = (bfd_vma)rel->r_addend;
4100 bfd_reloc_status_type r = bfd_reloc_ok;
4101 bfd_boolean unresolved_reloc = FALSE;
4102 int do_final_relocate = TRUE;
4103 bfd_boolean relative_reloc = FALSE;
4104 bfd_signed_vma disp;
4105
4106 /* Ignore these relocation types:
4107 R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY. */
4108 if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
4109 continue;
4110
4111 if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
4112 {
4113 /* The r_type is error, not support it. */
4114 /* xgettext:c-format */
4115 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
4116 input_bfd, r_type);
4117 bfd_set_error (bfd_error_bad_value);
4118 ret = FALSE;
4119 continue;
4120 }
4121
4122 howto = &csky_elf_howto_table[(int) r_type];
4123
4124 r_symndx = ELF32_R_SYM(rel->r_info);
4125 h = NULL;
4126 sym = NULL;
4127 sec = NULL;
4128 unresolved_reloc = FALSE;
4129
4130 if (r_symndx < symtab_hdr->sh_info)
4131 {
4132 /* Get symbol table entry. */
4133 sym = local_syms + r_symndx;
4134 sec = local_sections[r_symndx];
4135 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4136 addend = (bfd_vma)rel->r_addend;
4137 }
4138 else
4139 {
4140 bfd_boolean warned, ignored;
4141
4142 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4143 r_symndx, symtab_hdr, sym_hashes,
4144 h, sec, relocation,
4145 unresolved_reloc, warned, ignored);
4146 }
4147
4148 if (sec != NULL && discarded_section (sec))
4149 {
4150 /* For relocs against symbols from removed linkonce sections,
4151 or sections discarded by a linker script, we just want the
4152 section contents zeroed. Avoid any special processing.
4153 And if the symbol is referenced in '.csky_stack_size' section,
4154 set the address to SEC_DISCARDED(0xffffffff). */
4155#if 0
4156 /* The .csky_stack_size section is just for callgraph. */
4157 if (strcmp (input_section->name, ".csky_stack_size") == 0)
4158 {
4159/* FIXME: it should define in head file. */
4160#define SEC_DISCARDED 0xffffffff
4161 bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
4162 rel->r_info = 0;
4163 rel->r_addend = 0;
4164 continue;
4165 }
4166 else
4167#endif
4168 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4169 rel, 1, relend, howto, 0,
4170 contents);
4171 }
4172
4173 if (bfd_link_relocatable (info))
4174 continue;
4175
4176 read_content_substitute = 0;
4177
4178 /* Final link. */
4179 disp = (relocation
4180 + (bfd_signed_vma) addend
4181 - input_section->output_section->vma
4182 - input_section->output_offset
4183 - rel->r_offset);
4184/* It is for ck8xx. */
4185#define CSKY_INSN_BSR32 0xe0000000
4186/* It is for ck5xx/ck6xx. */
4187#define CSKY_INSN_BSR16 0xf800
4188#define within_range(x, L) (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
4189 switch (howto->type)
4190 {
4191 case R_CKCORE_PCREL_IMM18BY2:
4192 /* When h is NULL, means the instruction written as
4193 grs rx, imm32
4194 if the highest bit is set, prevent the high 32bits
4195 turn to 0xffffffff when signed extern in 64bit
4196 host machine. */
4197 if (h == NULL && (addend & 0x80000000))
4198 addend &= 0xffffffff;
4199 break;
fe75f42e
LX
4200
4201 case R_CKCORE_PCREL32:
4202 break;
4203
b8891f8d
AJ
4204 case R_CKCORE_GOT12:
4205 case R_CKCORE_PLT12:
4206 case R_CKCORE_GOT_HI16:
4207 case R_CKCORE_GOT_LO16:
4208 case R_CKCORE_PLT_HI16:
4209 case R_CKCORE_PLT_LO16:
4210 case R_CKCORE_GOT32:
4211 case R_CKCORE_GOT_IMM18BY4:
4212 /* Relocation is to the entry for this symbol in the global
4213 offset table. */
4214 BFD_ASSERT (htab->elf.sgot != NULL);
4215 if (h != NULL)
4216 {
4217 /* Global symbol is defined by other modules. */
4218 bfd_boolean dyn;
4219 off = h->got.offset;
4220 dyn = htab->elf.dynamic_sections_created;
4221 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4222 bfd_link_pic (info), h)
4223 || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
4224 || (ELF_ST_VISIBILITY(h->other)
4225 && h->root.type == bfd_link_hash_undefweak))
4226 {
4227 /* This is actually a static link, or it is a
4228 -Bsymbolic link and the symbol is defined
4229 locally, or the symbol was forced to be local
4230 because of a version file. We must initialize
4231 this entry in the global offset table. Since the
4232 offset must always be a multiple of 4, we use the
4233 least significant bit to record whether we have
4234 initialized it already.
4235 When doing a dynamic link, we create a .rela.dyn
4236 relocation entry to initialize the value. This
4237 is done in the finish_dynamic_symbol routine. FIXME */
4238 if (off & 1)
4239 off &= ~1;
4240 else
4241 {
4242 bfd_put_32 (output_bfd, relocation,
4243 htab->elf.sgot->contents + off);
4244 h->got.offset |= 1;
4245
4246/* TRUE if relative relocation should be generated. GOT reference to
4247 global symbol in PIC will lead to dynamic symbol. It becomes a
4248 problem when "time" or "times" is defined as a variable in an
4249 executable, clashing with functions of the same name in libc. If a
4250 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
4251 generate relative relocation. */
4252#define GENERATE_RELATIVE_RELOC_P(INFO, H) \
4253 ((H)->dynindx == -1 \
4254 && !(H)->forced_local \
4255 && (H)->root.type != bfd_link_hash_undefweak \
4256 && bfd_link_pic (INFO))
4257
4258 if (GENERATE_RELATIVE_RELOC_P (info, h))
4259 /* If this symbol isn't dynamic
4260 in PIC, generate R_CKCORE_RELATIVE here. */
4261 relative_reloc = TRUE;
4262 }
4263 }
4264 else
4265 unresolved_reloc = FALSE;
4266 } /* End if h != NULL. */
4267 else
4268 {
4269 BFD_ASSERT (local_got_offsets != NULL);
4270 off = local_got_offsets[r_symndx];
4271
4272 /* The offset must always be a multiple of 4. We use
4273 the least significant bit to record whether we have
4274 already generated the necessary reloc. */
4275 if (off & 1)
4276 off &= ~1;
4277 else
4278 {
4279 bfd_put_32 (output_bfd, relocation,
4280 htab->elf.sgot->contents + off);
4281 local_got_offsets[r_symndx] |= 1;
4282 if (bfd_link_pic (info))
4283 relative_reloc = TRUE;
4284 }
4285 }
4286 if (relative_reloc)
4287 {
4288 asection *srelgot;
4289 Elf_Internal_Rela outrel;
4290 bfd_byte *loc;
4291
4292 srelgot = htab->elf.srelgot;
4293 BFD_ASSERT (srelgot != NULL);
4294
4295 outrel.r_offset
4296 = (htab->elf.sgot->output_section->vma
4297 + htab->elf.sgot->output_offset + off);
4298 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4299 outrel.r_addend = relocation;
4300 loc = srelgot->contents;
4301 loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
4302 if (loc != NULL)
4303 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4304 }
4305 relocation = htab->elf.sgot->output_offset + off;
4306 break;
4307
4308 case R_CKCORE_GOTOFF_IMM18:
4309 case R_CKCORE_GOTOFF:
4310 case R_CKCORE_GOTOFF_HI16:
4311 case R_CKCORE_GOTOFF_LO16:
4312 /* Relocation is relative to the start of the global offset
4313 table. */
4314 /* Note that sgot->output_offset is not involved in this
4315 calculation. We always want the start of .got. If we
4316 defined _GLOBAL_OFFSET_TABLE in a different way, as is
4317 permitted by the ABI, we might have to change this
4318 calculation. */
4319 relocation -= htab->elf.sgot->output_section->vma;
4320 break;
4321
4322 case R_CKCORE_GOTPC:
4323 case R_CKCORE_GOTPC_HI16:
4324 case R_CKCORE_GOTPC_LO16:
4325 /* Use global offset table as symbol value. */
4326 relocation = htab->elf.sgot->output_section->vma;
4327 addend = -addend;
4328 unresolved_reloc = FALSE;
4329 break;
4330
4331 case R_CKCORE_DOFFSET_IMM18:
4332 case R_CKCORE_DOFFSET_IMM18BY2:
4333 case R_CKCORE_DOFFSET_IMM18BY4:
4334 {
4335 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4336 relocation -= sdata->output_section->vma;
4337 }
4338 break;
4339
4340 case R_CKCORE_DOFFSET_LO16:
4341 {
4342 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4343 relocation -= sdata->output_section->vma;
4344 }
4345 break;
4346
4347 case R_CKCORE_TOFFSET_LO16:
4348 {
4349 asection *stext = bfd_get_section_by_name (output_bfd, ".text");
4350 if (stext)
4351 relocation -= stext->output_section->vma;
4352 }
4353 break;
4354
4355 case R_CKCORE_PLT_IMM18BY4:
4356 case R_CKCORE_PLT32:
4357 /* Relocation is to the entry for this symbol in the
4358 procedure linkage table. */
4359
4360 /* Resolve a PLT32 reloc against a local symbol directly,
4361 without using the procedure linkage table. */
4362 if (h == NULL)
4363 break;
4364
4365 if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
4366 {
4367 /* We didn't make a PLT entry for this symbol. This
4368 happens when statically linking PIC code, or when
4369 using -Bsymbolic. */
4370 if (h->got.offset != (bfd_vma) -1)
4371 {
4372 bfd_boolean dyn;
4373
4374 off = h->got.offset;
4375 dyn = htab->elf.dynamic_sections_created;
4376 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4377 bfd_link_pic (info), h)
4378 || (bfd_link_pic (info)
4379 && SYMBOL_REFERENCES_LOCAL (info, h))
4380 || (ELF_ST_VISIBILITY (h->other)
4381 && h->root.type == bfd_link_hash_undefweak))
4382 {
4383 /* This is actually a static link, or it is a
4384 -Bsymbolic link and the symbol is defined
4385 locally, or the symbol was forced to be local
4386 because of a version file. We must initialize
4387 this entry in the global offset table. Since the
4388 offset must always be a multiple of 4, we use the
4389 least significant bit to record whether we have
4390 initialized it already.
4391
4392 When doing a dynamic link, we create a .rela.dyn
4393 relocation entry to initialize the value. This
4394 is done in the finish_dynamic_symbol routine.
4395 FIXME! */
4396 if (off & 1)
4397 off &= ~1;
4398 else
4399 {
4400 h->got.offset |= 1;
4401 if (GENERATE_RELATIVE_RELOC_P (info, h))
4b24dd1a 4402 relative_reloc = TRUE;
b8891f8d
AJ
4403 }
4404 }
4405 bfd_put_32 (output_bfd, relocation,
4406 htab->elf.sgot->contents + off);
4407
4408 if (relative_reloc)
4409 {
4410 asection *srelgot;
4411 Elf_Internal_Rela outrel;
4412 bfd_byte *loc;
4413
4414 srelgot = htab->elf.srelgot;
4415 BFD_ASSERT (srelgot != NULL);
4416
4417 outrel.r_offset
4418 = (htab->elf.sgot->output_section->vma
4419 + htab->elf.sgot->output_offset + off);
4420 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4421 outrel.r_addend = relocation;
4422 loc = srelgot->contents;
4423 loc += (srelgot->reloc_count++
4424 * sizeof (Elf32_External_Rela));
4425 if (loc != NULL)
4426 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4427 }
4428 relocation = off + htab->elf.sgot->output_offset;
4429 }
4430 break;
4431 }
4432 /* The relocation is the got offset. */
4433 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
4434 relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
4435 else
4436 relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
4437 unresolved_reloc = FALSE;
4438 break;
4439
4440 case R_CKCORE_PCREL_IMM26BY2:
4441 case R_CKCORE_PCREL_JSR_IMM26BY2:
4442 case R_CKCORE_PCREL_JSR_IMM11BY2:
4443 case R_CKCORE_PCREL_IMM11BY2:
4444 case R_CKCORE_CALLGRAPH:
4445 /* Emit callgraph information first. */
4446 /* TODO: deal with callgraph. */
4447 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
4448 break;
4449 /* Some reloc need further handling. */
4450 /* h == NULL means the symbol is a local symbol,
4451 r_symndx == 0 means the symbol is 'ABS' and
4452 the relocation is already handled in assemble,
4453 here just use for callgraph. */
4454 /* TODO: deal with callgraph. */
4455 if (h == NULL && r_symndx == 0)
4456 {
4457 do_final_relocate = FALSE;
4458 break;
4459 }
4460
4461 /* Ignore weak references to undefined symbols. */
4462 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
4463 {
4464 do_final_relocate = FALSE;
4465 break;
4466 }
4467
4468 /* Using branch stub. */
4469 if (use_branch_stub == TRUE
4470 && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
4471 {
4472 struct elf32_csky_stub_hash_entry *stub_entry = NULL;
4473 if (sym_must_create_stub (h, info))
4474 stub_entry = elf32_csky_get_stub_entry (input_section,
4475 input_section,
4476 h, rel, htab);
4477 else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
4478 || disp < BSR_MAX_BWD_BRANCH_OFFSET)
4479 stub_entry = elf32_csky_get_stub_entry (input_section,
4480 input_section,
4481 h, rel, htab);
4482 if (stub_entry != NULL)
4483 relocation
4484 = (stub_entry->stub_offset
4485 + stub_entry->stub_sec->output_offset
4486 + stub_entry->stub_sec->output_section->vma);
4487 break;
4488 }
4489
4490 else if (h == NULL
4491 || (h->root.type == bfd_link_hash_defined
4492 && h->dynindx == -1)
4493 || ((h->def_regular && !h->def_dynamic)
4494 && (h->root.type != bfd_link_hash_defweak
4495 || ! bfd_link_pic (info))))
4496 {
4497 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
4498 {
4499 if (within_range (disp, 26))
4500 {
4501 /* In range for BSR32. */
4502 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
4503 read_content_substitute = CSKY_INSN_BSR32;
4504 }
4505 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
4506 /* if bsr32 cannot reach, generate
4507 "lrw r25, label; jsr r25" instead of
4508 jsri label. */
4509 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4510 } /* if ELF32_R_TYPE (rel->r_info)... */
4511 else if (ELF32_R_TYPE (rel->r_info)
4512 == R_CKCORE_PCREL_JSR_IMM11BY2)
4513 {
4514 if (within_range (disp, 11))
4515 {
4516 /* In range for BSR16. */
4517 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
4518 read_content_substitute = CSKY_INSN_BSR16;
4519 }
4520 }
4521 break;
4522 } /* else if h == NULL... */
4523
4524 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
4525 && (ELF32_R_TYPE (rel->r_info)
4526 == R_CKCORE_PCREL_JSR_IMM26BY2))
4527 {
4528 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4529 break;
4530 }
4531 /* Other situation, h->def_dynamic == 1,
4532 undefined_symbol when output file is shared object, etc. */
4533 /* Else fall through. */
4534
4535 case R_CKCORE_ADDR_HI16:
4536 case R_CKCORE_ADDR_LO16:
4537 if (bfd_link_pic (info)
4538 || (!bfd_link_pic (info)
4539 && h != NULL
4540 && h->dynindx != -1
4541 && !h->non_got_ref
4542 && ((h->def_dynamic && !h->def_regular)
4543 || (htab->elf.dynamic_sections_created
4544 && (h->root.type == bfd_link_hash_undefweak
4545 || h->root.type == bfd_link_hash_undefined
4546 || h->root.type == bfd_link_hash_indirect)))))
4547 {
4548 Elf_Internal_Rela outrel;
4549 bfd_boolean skip, relocate;
4550 bfd_byte *loc;
4551
4552 /* When generating a shared object, these relocations
4553 are copied into the output file to be resolved at
4554 run time. */
4555 skip = FALSE;
4556 relocate = FALSE;
4557
4558 outrel.r_offset =
4559 _bfd_elf_section_offset (output_bfd, info, input_section,
4560 rel->r_offset);
4561 if (outrel.r_offset == (bfd_vma) -1)
4562 skip = TRUE;
4563 else if (outrel.r_offset == (bfd_vma) -2)
4564 {
4565 skip = TRUE;
4566 relocate = TRUE;
4567 }
4568 outrel.r_offset += (input_section->output_section->vma
4569 + input_section->output_offset);
4570 if (skip)
4571 memset (&outrel, 0, sizeof (outrel));
4572 else if (h != NULL
4573 && h->dynindx != -1
4574 && (!bfd_link_pic (info)
4575 || (!SYMBOLIC_BIND (info, h)
4576 && h->root.type == bfd_link_hash_defweak)
4577 || !h->def_regular))
4578 {
4579 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4580 outrel.r_addend = rel->r_addend;
4581 }
4582 else
4583 {
4584 /* This symbol is local, or marked to become local. */
4585 relocate = TRUE;
4586 outrel.r_info = ELF32_R_INFO (0, r_type);
4587 outrel.r_addend = relocation + rel->r_addend;
4588 }
4589 loc = htab->elf.srelgot->contents;
4590 loc += (htab->elf.srelgot->reloc_count++
4591 * sizeof (Elf32_External_Rela));
4592
4593 if (loc != NULL)
4594 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4595
4596 /* If this reloc is against an external symbol, we do not
4597 want to diddle with the addend. Otherwise, we need to
4598 include the symbol value so that it becomes an addend
4599 for the dynamic reloc. */
4600 if (!relocate)
4601 continue;
4602 } /* if bfd_link_pic (info) ... */
4603 break;
4604
4605 case R_CKCORE_ADDR32:
4606 /* r_symndx will be zero only for relocs against symbols
4607 from removed linkonce sections, or sections discarded
4608 by a linker script.
4609 This relocation don't nedd to handle, the value will
4610 be set to SEC_DISCARDED(0xffffffff). */
4611 if (r_symndx == 0
4612 && strcmp (sec->name, ".csky_stack_size") == 0)
4613 {
4614 do_final_relocate = FALSE;
4615 break;
4616 }
4617 if (r_symndx >= symtab_hdr->sh_info
4618 && h->non_got_ref
4619 && bfd_link_executable (info))
4620 break;
4621
4622 if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
4623 break;
4624
4625 if (bfd_link_pic (info)
4626 || (h != NULL
4627 && h->dynindx != -1
4628 && ((h->def_dynamic && !h->def_regular)
4629 || (htab->elf.dynamic_sections_created
4630 && (h->root.type == bfd_link_hash_undefweak
4631 || h->root.type == bfd_link_hash_undefined
4632 || h->root.type == bfd_link_hash_indirect)))))
4633 {
4634 Elf_Internal_Rela outrel;
4635 bfd_boolean skip, relocate;
4636 bfd_byte *loc;
4637
4638 /* When generating a shared object, these relocations
4639 are copied into the output file to be resolved at
4640 run time. */
4641 skip = FALSE;
4642 relocate = FALSE;
4643
4644 outrel.r_offset =
4645 _bfd_elf_section_offset (output_bfd, info, input_section,
4646 rel->r_offset);
4647
4648 if (outrel.r_offset == (bfd_vma) -1)
4649 skip = TRUE;
4650 else if (outrel.r_offset == (bfd_vma) -2)
4651 {
4652 skip = TRUE;
4653 relocate = TRUE;
4654 }
4655
4656 outrel.r_offset += (input_section->output_section->vma
4657 + input_section->output_offset);
4658
4659 if (skip)
4660 memset (&outrel, 0, sizeof (outrel));
4661 else if (h != NULL
4662 && h->dynindx != -1
4663 && (!bfd_link_pic (info)
4664 || (!SYMBOLIC_BIND (info, h)
4665 && h->root.type == bfd_link_hash_defweak)
4666 || !h->def_regular))
4667 {
4668 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4669 outrel.r_addend = rel->r_addend;
4670 }
4671 else
4672 {
4673 /* This symbol is local, or marked to become local. */
4674 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4675 outrel.r_addend = relocation + rel->r_addend;
4676 }
4677
4678 loc = htab->elf.srelgot->contents;
4679 loc += (htab->elf.srelgot->reloc_count++
4680 * sizeof (Elf32_External_Rela));
4681
4682 if (loc != NULL)
4683 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4684
4685 /* If this reloc is against an external symbol, we do
4686 want to diddle with the addend. Otherwise, we need to
4687 include the symbol value so that it becomes an addend
4688 for the dynamic reloc. */
4689 if (! relocate)
4690 continue;
4691 }
4692 break;
4693
4694 case R_CKCORE_TLS_LDO32:
4695 relocation = relocation - dtpoff_base (info);
4696 break;
4697
4698 case R_CKCORE_TLS_LDM32:
4699 BFD_ASSERT (htab->elf.sgot != NULL);
4700 off = htab->tls_ldm_got.offset;
4701 if (off & 1)
4702 off &= ~1;
4703 else
4704 {
4705 /* If we don't know the module number,
4706 create a relocation for it. */
4707 if (!bfd_link_executable (info))
4708 {
4709 Elf_Internal_Rela outrel;
4710 bfd_byte *loc;
4711
4712 BFD_ASSERT (htab->elf.srelgot != NULL);
4713 outrel.r_addend = 0;
4714 outrel.r_offset
4715 = (htab->elf.sgot->output_section->vma
4716 + htab->elf.sgot->output_offset + off);
4717 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
4718 bfd_put_32 (output_bfd, outrel.r_addend,
4719 htab->elf.sgot->contents + off);
4720
4721 loc = htab->elf.srelgot->contents;
4722 loc += (htab->elf.srelgot->reloc_count++
4723 * sizeof (Elf32_External_Rela));
4724 if (loc)
4725 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4726 }
4727 else
4728 bfd_put_32 (output_bfd, 1,
4729 htab->elf.sgot->contents + off);
4730 htab->tls_ldm_got.offset |= 1;
4731 }
4732 relocation
4733 = (htab->elf.sgot->output_section->vma
4734 + htab->elf.sgot->output_offset + off
4735 - (input_section->output_section->vma
4736 + input_section->output_offset + rel->r_offset));
4737 break;
4738 case R_CKCORE_TLS_LE32:
4739 if (bfd_link_dll (info))
4740 {
4741 _bfd_error_handler
4742 /* xgettext:c-format */
4743 (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
4744 "in shared object"),
4745 input_bfd, input_section, (uint64_t)rel->r_offset,
4746 howto->name);
4747 return FALSE;
4748 }
4749 else
4750 relocation = tpoff (info, relocation);
4751 break;
4752 case R_CKCORE_TLS_GD32:
4753 case R_CKCORE_TLS_IE32:
4754 {
4755 int indx;
4756 char tls_type;
4757
4758 BFD_ASSERT (htab->elf.sgot != NULL);
4759
4760 indx = 0;
4761 if (h != NULL)
4762 {
4763 bfd_boolean dyn;
4764 dyn = htab->elf.dynamic_sections_created;
4765 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4766 bfd_link_pic (info), h)
4767 && (!bfd_link_pic (info)
4768 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4769 {
4770 unresolved_reloc = FALSE;
4771 indx = h->dynindx;
4772 }
4773 off = h->got.offset;
4774 tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
4775 }
4776 else
4777 {
4778 BFD_ASSERT (local_got_offsets != NULL);
4779 off = local_got_offsets[r_symndx];
4780 tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
4781 }
4782
4783 BFD_ASSERT (tls_type != GOT_UNKNOWN);
4784
4785 if (off & 1)
4786 off &= ~1;
4787 else
4788 {
4789 bfd_boolean need_relocs = FALSE;
4790 Elf_Internal_Rela outrel;
4791 bfd_byte *loc = NULL;
4792 int cur_off = off;
4793 /* The GOT entries have not been initialized yet. Do it
4794 now, and emit any relocations. If both an IE GOT and a
4795 GD GOT are necessary, we emit the GD first. */
4796 if ((!bfd_link_executable (info) || indx != 0)
4797 && (h == NULL
4798 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4799 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
4800 || h->root.type != bfd_link_hash_undefined))
4801 {
4802 need_relocs = TRUE;
4803 BFD_ASSERT (htab->elf.srelgot != NULL);
4804
4805 loc = htab->elf.srelgot->contents;
4806 loc += (htab->elf.srelgot->reloc_count
4807 * sizeof (Elf32_External_Rela));
4808 }
4809 if (tls_type & GOT_TLS_GD)
4810 {
4811 if (need_relocs)
4812 {
4813 outrel.r_addend = 0;
4814 outrel.r_offset
4815 = (htab->elf.sgot->output_section->vma
4816 + htab->elf.sgot->output_offset
4817 + cur_off);
4818 outrel.r_info
4819 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
4820 bfd_put_32 (output_bfd, outrel.r_addend,
4821 htab->elf.sgot->contents + cur_off);
4822 if (loc)
4823 bfd_elf32_swap_reloca_out (output_bfd,
4824 &outrel, loc);
4825 loc += sizeof (Elf32_External_Rela);
4826 htab->elf.srelgot->reloc_count++;
4827 if (indx == 0)
4828 bfd_put_32 (output_bfd,
4829 relocation - dtpoff_base (info),
4830 (htab->elf.sgot->contents
4831 + cur_off + 4));
4832 else
4833 {
4834 outrel.r_addend = 0;
4835 outrel.r_info
4836 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
4837 outrel.r_offset += 4;
4838 bfd_put_32 (output_bfd, outrel.r_addend,
4839 (htab->elf.sgot->contents
4840 + cur_off + 4));
4841 outrel.r_info =
4842 ELF32_R_INFO (indx,
4843 R_CKCORE_TLS_DTPOFF32);
4844 if (loc)
4845 bfd_elf32_swap_reloca_out (output_bfd,
4846 &outrel,
4847 loc);
4848 htab->elf.srelgot->reloc_count++;
4849 loc += sizeof (Elf32_External_Rela);
4850 }
4851
4852 }
4853 else
4854 {
4855 /* If are not emitting relocations for a
4856 general dynamic reference, then we must be in a
4857 static link or an executable link with the
4858 symbol binding locally. Mark it as belonging
4859 to module 1, the executable. */
4860 bfd_put_32 (output_bfd, 1,
4861 htab->elf.sgot->contents + cur_off);
4862 bfd_put_32 (output_bfd,
4863 relocation - dtpoff_base (info),
4864 htab->elf.sgot->contents
4865 + cur_off + 4);
4866 }
4867 cur_off += 8;
4868 }
4869 if (tls_type & GOT_TLS_IE)
4870 {
4871 if (need_relocs)
4872 {
4873 if (indx == 0)
4874 outrel.r_addend = relocation - dtpoff_base (info);
4875 else
4876 outrel.r_addend = 0;
4877 outrel.r_offset
4878 = (htab->elf.sgot->output_section->vma
4879 + htab->elf.sgot->output_offset + cur_off);
4880 outrel.r_info
4881 = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
4882
4883 bfd_put_32 (output_bfd, outrel.r_addend,
4884 htab->elf.sgot->contents + cur_off);
4885 if (loc)
4886 bfd_elf32_swap_reloca_out (output_bfd,
4887 &outrel, loc);
4888 htab->elf.srelgot->reloc_count++;
4889 loc += sizeof (Elf32_External_Rela);
4890 }
4891 else
4892 bfd_put_32 (output_bfd, tpoff (info, relocation),
4893 htab->elf.sgot->contents + cur_off);
4894 }
4895 if (h != NULL)
4896 h->got.offset |= 1;
4897 else
4898 local_got_offsets[r_symndx] |= 1;
4899 }
4900 if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
4901 off += 8;
4902 relocation
4903 = (htab->elf.sgot->output_section->vma
4904 + htab->elf.sgot->output_offset + off
4905 - (input_section->output_section->vma
4906 + input_section->output_offset
4907 + rel->r_offset));
4908 break;
4909 }
4910 default:
4911 /* No substitution when final linking. */
4912 read_content_substitute = 0;
4913 break;
4914 } /* End switch (howto->type). */
4915
4916 /* Make sure 32-bit data in the text section will not be affected by
4917 our special endianness.
4918 However, this currently affects noting, since the ADDR32 howto type
4919 does no change with the data read. But we may need this mechanism in
4920 the future. */
4921
4922 if (howto->size == 2
4923 && (howto->type == R_CKCORE_ADDR32
fe75f42e 4924 || howto->type == R_CKCORE_PCREL32
b8891f8d
AJ
4925 || howto->type == R_CKCORE_GOT32
4926 || howto->type == R_CKCORE_GOTOFF
4927 || howto->type == R_CKCORE_GOTPC
4928 || howto->type == R_CKCORE_PLT32
4929 || howto->type == R_CKCORE_TLS_LE32
4930 || howto->type == R_CKCORE_TLS_IE32
4931 || howto->type == R_CKCORE_TLS_LDM32
4932 || howto->type == R_CKCORE_TLS_GD32
4933 || howto->type == R_CKCORE_TLS_LDO32
4934 || howto->type == R_CKCORE_RELATIVE))
4935 need_reverse_bits = 0;
4936 else
4937 need_reverse_bits = 1;
4938 /* Do the final link. */
4939 if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
4940 && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
4941 && howto->type != R_CKCORE_CALLGRAPH
4942 && do_final_relocate)
4943 r = csky_final_link_relocate (howto, input_bfd, input_section,
4944 contents, rel->r_offset,
4945 relocation, addend);
4946
4947 if (r != bfd_reloc_ok)
4948 {
4949 ret = FALSE;
4950 switch (r)
4951 {
4952 default:
4953 break;
4954 case bfd_reloc_overflow:
4955 if (h != NULL)
4956 name = NULL;
4957 else
4958 {
4959 name = bfd_elf_string_from_elf_section (input_bfd,
4960 symtab_hdr->sh_link,
4961 sym->st_name);
4962 if (name == NULL)
4963 break;
4964 if (*name == '\0')
fd361982 4965 name = bfd_section_name (sec);
b8891f8d
AJ
4966 }
4967 (*info->callbacks->reloc_overflow)
4968 (info,
4969 (h ? &h->root : NULL),
4970 name, howto->name, (bfd_vma) 0,
4971 input_bfd, input_section, rel->r_offset);
4972 break;
4973 }
4974 }
4975 } /* End for (;rel < relend; rel++). */
4976 return ret;
4977}
4978
4979static bfd_boolean
4980csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4981{
4982 int offset;
4983 size_t size;
4984
4985 switch (note->descsz)
4986 {
4987 default:
4988 return FALSE;
4989 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
4990 case 148:
4991 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4992 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
4993 offset = 72;
4994 size = 72;
4995 break;
4996 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
4997 case 220:
4998 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4999 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5000 offset = 72;
5001 size = 34 * 4;
5002 break;
5003 }
5004 /* Make a ".reg/999" section. */
5005 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5006 size, note->descpos + offset);
5007}
5008
5009static bfd_boolean
5010csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5011{
5012 switch (note->descsz)
5013 {
5014 default:
5015 return FALSE;
5016
5017 /* Sizeof (struct elf_prpsinfo) on linux csky. */
5018 case 124:
5019 elf_tdata (abfd)->core->program
5020 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5021 elf_tdata (abfd)->core->command
5022 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5023 }
5024
5025 /* Note that for some reason, a spurious space is tacked
5026 onto the end of the args in some (at least one anyway)
5027 implementations, so strip it off if it exists. */
5028 {
5029 char *command = elf_tdata (abfd)->core->command;
5030 int n = strlen (command);
5031
5032 if (0 < n && command[n - 1] == ' ')
5033 command[n - 1] = '\0';
5034 }
5035
5036 return TRUE;
5037}
5038
5039/* End of external entry points for sizing and building linker stubs. */
5040
5041/* CPU-related basic API. */
5042#define TARGET_BIG_SYM csky_elf32_be_vec
5043#define TARGET_BIG_NAME "elf32-csky-big"
5044#define TARGET_LITTLE_SYM csky_elf32_le_vec
5045#define TARGET_LITTLE_NAME "elf32-csky-little"
5046#define ELF_ARCH bfd_arch_csky
5047#define ELF_MACHINE_CODE EM_CSKY
5048#define ELF_MACHINE_ALT1 EM_CSKY_OLD
5049#define ELF_MAXPAGESIZE 0x1000
5050#define elf_info_to_howto csky_elf_info_to_howto
5051#define elf_info_to_howto_rel NULL
5052#define elf_backend_special_sections csky_elf_special_sections
5053#define bfd_elf32_bfd_link_hash_table_create csky_elf_link_hash_table_create
5054
5055/* Target related API. */
5056#define bfd_elf32_mkobject csky_elf_mkobject
5057#define bfd_elf32_bfd_merge_private_bfd_data csky_elf_merge_private_bfd_data
5058#define bfd_elf32_bfd_set_private_flags csky_elf_set_private_flags
5059#define elf_backend_copy_indirect_symbol csky_elf_copy_indirect_symbol
5060
5061/* GC section related API. */
5062#define elf_backend_can_gc_sections 1
5063#define elf_backend_gc_mark_hook csky_elf_gc_mark_hook
5064#define elf_backend_gc_mark_extra_sections elf32_csky_gc_mark_extra_sections
5065
5066/* Relocation related API. */
5067#define elf_backend_reloc_type_class csky_elf_reloc_type_class
5068#define bfd_elf32_bfd_reloc_type_lookup csky_elf_reloc_type_lookup
5069#define bfd_elf32_bfd_reloc_name_lookup csky_elf_reloc_name_lookup
5070#define elf_backend_ignore_discarded_relocs csky_elf_ignore_discarded_relocs
5071#define elf_backend_relocate_section csky_elf_relocate_section
5072#define elf_backend_check_relocs csky_elf_check_relocs
5073
5074/* Dynamic relocate related API. */
5075#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5076#define elf_backend_adjust_dynamic_symbol csky_elf_adjust_dynamic_symbol
5077#define elf_backend_size_dynamic_sections csky_elf_size_dynamic_sections
5078#define elf_backend_finish_dynamic_symbol csky_elf_finish_dynamic_symbol
5079#define elf_backend_finish_dynamic_sections csky_elf_finish_dynamic_sections
5080#define elf_backend_rela_normal 1
5081#define elf_backend_can_refcount 1
5082#define elf_backend_plt_readonly 1
5083#define elf_backend_want_got_sym 1
5084#define elf_backend_want_dynrelro 1
5085#define elf_backend_got_header_size 12
5086#define elf_backend_want_got_plt 1
5087
5088/* C-SKY coredump support. */
5089#define elf_backend_grok_prstatus csky_elf_grok_prstatus
5090#define elf_backend_grok_psinfo csky_elf_grok_psinfo
5091
5092#include "elf32-target.h"