Coverage Report

Created: 2023-08-28 06:23

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