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