Coverage Report

Created: 2026-11-21 09:00

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