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