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