Coverage Report

Created: 2023-08-28 06:26

/src/binutils-gdb/bfd/elf32-nios2.c
Line
Count
Source (jump to first uncovered line)
1
/* 32-bit ELF support for Nios II.
2
   Copyright (C) 2012-2023 Free Software Foundation, Inc.
3
   Contributed by Nigel Gray (ngray@altera.com).
4
   Contributed by Mentor Graphics, Inc.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
/* This file handles Altera Nios II ELF targets.  */
24
25
#include "sysdep.h"
26
#include "bfd.h"
27
#include "libbfd.h"
28
#include "bfdlink.h"
29
#include "genlink.h"
30
#include "elf-bfd.h"
31
#include "elf/nios2.h"
32
#include "opcode/nios2.h"
33
#include "elf32-nios2.h"
34
#include "libiberty.h"
35
36
/* Use RELA relocations.  */
37
#ifndef USE_RELA
38
#define USE_RELA
39
#endif
40
41
#ifdef USE_REL
42
#undef USE_REL
43
#endif
44
45
/* Forward declarations.  */
46
static bfd_reloc_status_type nios2_elf32_ignore_reloc
47
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48
static bfd_reloc_status_type nios2_elf32_hi16_relocate
49
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50
static bfd_reloc_status_type nios2_elf32_lo16_relocate
51
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52
static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
53
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54
static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
55
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56
static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
57
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58
static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
59
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60
static bfd_reloc_status_type nios2_elf32_call26_relocate
61
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62
static bfd_reloc_status_type nios2_elf32_gprel_relocate
63
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64
static bfd_reloc_status_type nios2_elf32_ujmp_relocate
65
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66
static bfd_reloc_status_type nios2_elf32_cjmp_relocate
67
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68
static bfd_reloc_status_type nios2_elf32_callr_relocate
69
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
70
71
/* Target vector.  */
72
extern const bfd_target nios2_elf32_le_vec;
73
extern const bfd_target nios2_elf32_be_vec;
74
75
/* Offset of tp and dtp pointers from start of TLS block.  */
76
0
#define TP_OFFSET 0x7000
77
0
#define DTP_OFFSET  0x8000
78
79
/* The relocation tables used for SHT_REL sections.  There are separate
80
   tables for R1 and R2 encodings.  */
81
static reloc_howto_type elf_nios2_r1_howto_table_rel[] = {
82
  /* No relocation.  */
83
  HOWTO (R_NIOS2_NONE,    /* type */
84
   0,     /* rightshift */
85
   0,     /* size */
86
   0,     /* bitsize */
87
   false,     /* pc_relative */
88
   0,     /* bitpos */
89
   complain_overflow_dont,  /* complain_on_overflow */
90
   bfd_elf_generic_reloc, /* special_function */
91
   "R_NIOS2_NONE",  /* name */
92
   false,     /* partial_inplace */
93
   0,     /* src_mask */
94
   0,     /* dst_mask */
95
   false),    /* pcrel_offset */
96
97
  /* 16-bit signed immediate relocation.  */
98
  HOWTO (R_NIOS2_S16,   /* type */
99
   0,     /* rightshift */
100
   4,     /* size */
101
   16,      /* bitsize */
102
   false,     /* pc_relative */
103
   6,     /* bitpos */
104
   complain_overflow_signed,  /* complain on overflow */
105
   bfd_elf_generic_reloc, /* special function */
106
   "R_NIOS2_S16",   /* name */
107
   false,     /* partial_inplace */
108
   0x003fffc0,    /* src_mask */
109
   0x003fffc0,    /* dest_mask */
110
   false),    /* pcrel_offset */
111
112
  /* 16-bit unsigned immediate relocation.  */
113
  HOWTO (R_NIOS2_U16,   /* type */
114
   0,     /* rightshift */
115
   4,     /* size */
116
   16,      /* bitsize */
117
   false,     /* pc_relative */
118
   6,     /* bitpos */
119
   complain_overflow_unsigned,  /* complain on overflow */
120
   bfd_elf_generic_reloc, /* special function */
121
   "R_NIOS2_U16",   /* name */
122
   false,     /* partial_inplace */
123
   0x003fffc0,    /* src_mask */
124
   0x003fffc0,    /* dest_mask */
125
   false),    /* pcrel_offset */
126
127
  HOWTO (R_NIOS2_PCREL16, /* type */
128
   0,     /* rightshift */
129
   4,     /* size */
130
   16,      /* bitsize */
131
   true,      /* pc_relative */
132
   6,     /* bitpos */
133
   complain_overflow_signed,  /* complain on overflow */
134
   nios2_elf32_pcrel16_relocate,  /* special function */
135
   "R_NIOS2_PCREL16", /* name */
136
   false,     /* partial_inplace */
137
   0x003fffc0,    /* src_mask */
138
   0x003fffc0,    /* dest_mask */
139
   true),     /* pcrel_offset */
140
141
  HOWTO (R_NIOS2_CALL26,  /* type */
142
   2,     /* rightshift */
143
   4,     /* size */
144
   26,      /* bitsize */
145
   false,     /* pc_relative */
146
   6,     /* bitpos */
147
   complain_overflow_dont,  /* complain on overflow */
148
   nios2_elf32_call26_relocate, /* special function */
149
   "R_NIOS2_CALL26",  /* name */
150
   false,     /* partial_inplace */
151
   0xffffffc0,    /* src_mask */
152
   0xffffffc0,    /* dst_mask */
153
   false),    /* pcrel_offset */
154
155
  HOWTO (R_NIOS2_IMM5,
156
   0,
157
   4,
158
   5,
159
   false,
160
   6,
161
   complain_overflow_bitfield,
162
   bfd_elf_generic_reloc,
163
   "R_NIOS2_IMM5",
164
   false,
165
   0x000007c0,
166
   0x000007c0,
167
   false),
168
169
  HOWTO (R_NIOS2_CACHE_OPX,
170
   0,
171
   4,
172
   5,
173
   false,
174
   22,
175
   complain_overflow_bitfield,
176
   bfd_elf_generic_reloc,
177
   "R_NIOS2_CACHE_OPX",
178
   false,
179
   0x07c00000,
180
   0x07c00000,
181
   false),
182
183
  HOWTO (R_NIOS2_IMM6,
184
   0,
185
   4,
186
   6,
187
   false,
188
   6,
189
   complain_overflow_bitfield,
190
   bfd_elf_generic_reloc,
191
   "R_NIOS2_IMM6",
192
   false,
193
   0x00000fc0,
194
   0x00000fc0,
195
   false),
196
197
  HOWTO (R_NIOS2_IMM8,
198
   0,
199
   4,
200
   8,
201
   false,
202
   6,
203
   complain_overflow_bitfield,
204
   bfd_elf_generic_reloc,
205
   "R_NIOS2_IMM8",
206
   false,
207
   0x00003fc0,
208
   0x00003fc0,
209
   false),
210
211
  HOWTO (R_NIOS2_HI16,
212
   0,
213
   4,
214
   32,
215
   false,
216
   6,
217
   complain_overflow_dont,
218
   nios2_elf32_hi16_relocate,
219
   "R_NIOS2_HI16",
220
   false,
221
   0x003fffc0,
222
   0x003fffc0,
223
   false),
224
225
  HOWTO (R_NIOS2_LO16,
226
   0,
227
   4,
228
   32,
229
   false,
230
   6,
231
   complain_overflow_dont,
232
   nios2_elf32_lo16_relocate,
233
   "R_NIOS2_LO16",
234
   false,
235
   0x003fffc0,
236
   0x003fffc0,
237
   false),
238
239
  HOWTO (R_NIOS2_HIADJ16,
240
   0,
241
   4,
242
   32,
243
   false,
244
   6,
245
   complain_overflow_dont,
246
   nios2_elf32_hiadj16_relocate,
247
   "R_NIOS2_HIADJ16",
248
   false,
249
   0x003fffc0,
250
   0x003fffc0,
251
   false),
252
253
  HOWTO (R_NIOS2_BFD_RELOC_32,
254
   0,
255
   4,     /* long */
256
   32,
257
   false,
258
   0,
259
   complain_overflow_dont,
260
   bfd_elf_generic_reloc,
261
   "R_NIOS2_BFD_RELOC32",
262
   false,
263
   0xffffffff,
264
   0xffffffff,
265
   false),
266
267
  HOWTO (R_NIOS2_BFD_RELOC_16,
268
   0,
269
   2,     /* short */
270
   16,
271
   false,
272
   0,
273
   complain_overflow_bitfield,
274
   bfd_elf_generic_reloc,
275
   "R_NIOS2_BFD_RELOC16",
276
   false,
277
   0x0000ffff,
278
   0x0000ffff,
279
   false),
280
281
  HOWTO (R_NIOS2_BFD_RELOC_8,
282
   0,
283
   1,     /* byte */
284
   8,
285
   false,
286
   0,
287
   complain_overflow_bitfield,
288
   bfd_elf_generic_reloc,
289
   "R_NIOS2_BFD_RELOC8",
290
   false,
291
   0x000000ff,
292
   0x000000ff,
293
   false),
294
295
  HOWTO (R_NIOS2_GPREL,
296
   0,
297
   4,
298
   32,
299
   false,
300
   6,
301
   complain_overflow_dont,
302
   nios2_elf32_gprel_relocate,
303
   "R_NIOS2_GPREL",
304
   false,
305
   0x003fffc0,
306
   0x003fffc0,
307
   false),
308
309
  HOWTO (R_NIOS2_GNU_VTINHERIT,
310
   0,
311
   4,
312
   0,
313
   false,
314
   0,
315
   complain_overflow_dont,
316
   NULL,
317
   "R_NIOS2_GNU_VTINHERIT",
318
   false,
319
   0,
320
   0,
321
   false),
322
323
  HOWTO (R_NIOS2_GNU_VTENTRY,
324
   0,
325
   4,
326
   0,
327
   false,
328
   0,
329
   complain_overflow_dont,
330
   _bfd_elf_rel_vtable_reloc_fn,
331
   "R_NIOS2_GNU_VTENTRY",
332
   false,
333
   0,
334
   0,
335
   false),
336
337
  HOWTO (R_NIOS2_UJMP,
338
   0,
339
   4,
340
   32,
341
   false,
342
   6,
343
   complain_overflow_dont,
344
   nios2_elf32_ujmp_relocate,
345
   "R_NIOS2_UJMP",
346
   false,
347
   0x003fffc0,
348
   0x003fffc0,
349
   false),
350
351
  HOWTO (R_NIOS2_CJMP,
352
   0,
353
   4,
354
   32,
355
   false,
356
   6,
357
   complain_overflow_dont,
358
   nios2_elf32_cjmp_relocate,
359
   "R_NIOS2_CJMP",
360
   false,
361
   0x003fffc0,
362
   0x003fffc0,
363
   false),
364
365
  HOWTO (R_NIOS2_CALLR,
366
   0,
367
   4,
368
   32,
369
   false,
370
   6,
371
   complain_overflow_dont,
372
   nios2_elf32_callr_relocate,
373
   "R_NIOS2_CALLR",
374
   false,
375
   0x003fffc0,
376
   0x003fffc0,
377
   false),
378
379
  HOWTO (R_NIOS2_ALIGN,
380
   0,
381
   4,
382
   0,
383
   false,
384
   0,
385
   complain_overflow_dont,
386
   nios2_elf32_ignore_reloc,
387
   "R_NIOS2_ALIGN",
388
   false,
389
   0,
390
   0,
391
   true),
392
393
394
  HOWTO (R_NIOS2_GOT16,
395
   0,
396
   4,
397
   16,
398
   false,
399
   6,
400
   complain_overflow_bitfield,
401
   bfd_elf_generic_reloc,
402
   "R_NIOS2_GOT16",
403
   false,
404
   0x003fffc0,
405
   0x003fffc0,
406
   false),
407
408
  HOWTO (R_NIOS2_CALL16,
409
   0,
410
   4,
411
   16,
412
   false,
413
   6,
414
   complain_overflow_bitfield,
415
   bfd_elf_generic_reloc,
416
   "R_NIOS2_CALL16",
417
   false,
418
   0x003fffc0,
419
   0x003fffc0,
420
   false),
421
422
  HOWTO (R_NIOS2_GOTOFF_LO,
423
   0,
424
   4,
425
   16,
426
   false,
427
   6,
428
   complain_overflow_dont,
429
   bfd_elf_generic_reloc,
430
   "R_NIOS2_GOTOFF_LO",
431
   false,
432
   0x003fffc0,
433
   0x003fffc0,
434
   false),
435
436
  HOWTO (R_NIOS2_GOTOFF_HA,
437
   0,
438
   4,
439
   16,
440
   false,
441
   6,
442
   complain_overflow_dont,
443
   bfd_elf_generic_reloc,
444
   "R_NIOS2_GOTOFF_HA",
445
   false,
446
   0x003fffc0,
447
   0x003fffc0,
448
   false),
449
450
  HOWTO (R_NIOS2_PCREL_LO,
451
   0,
452
   4,
453
   16,
454
   true,
455
   6,
456
   complain_overflow_dont,
457
   nios2_elf32_pcrel_lo16_relocate,
458
   "R_NIOS2_PCREL_LO",
459
   false,
460
   0x003fffc0,
461
   0x003fffc0,
462
   true),
463
464
  HOWTO (R_NIOS2_PCREL_HA,
465
   0,
466
   4,
467
   16,
468
   false, /* This is a PC-relative relocation, but we need to subtract
469
       PC ourselves before the HIADJ.  */
470
   6,
471
   complain_overflow_dont,
472
   nios2_elf32_pcrel_hiadj16_relocate,
473
   "R_NIOS2_PCREL_HA",
474
   false,
475
   0x003fffc0,
476
   0x003fffc0,
477
   true),
478
479
  HOWTO (R_NIOS2_TLS_GD16,
480
   0,
481
   4,
482
   16,
483
   false,
484
   6,
485
   complain_overflow_bitfield,
486
   bfd_elf_generic_reloc,
487
   "R_NIOS2_TLS_GD16",
488
   false,
489
   0x003fffc0,
490
   0x003fffc0,
491
   false),
492
493
  HOWTO (R_NIOS2_TLS_LDM16,
494
   0,
495
   4,
496
   16,
497
   false,
498
   6,
499
   complain_overflow_bitfield,
500
   bfd_elf_generic_reloc,
501
   "R_NIOS2_TLS_LDM16",
502
   false,
503
   0x003fffc0,
504
   0x003fffc0,
505
   false),
506
507
  HOWTO (R_NIOS2_TLS_LDO16,
508
   0,
509
   4,
510
   16,
511
   false,
512
   6,
513
   complain_overflow_bitfield,
514
   bfd_elf_generic_reloc,
515
   "R_NIOS2_TLS_LDO16",
516
   false,
517
   0x003fffc0,
518
   0x003fffc0,
519
   false),
520
521
  HOWTO (R_NIOS2_TLS_IE16,
522
   0,
523
   4,
524
   16,
525
   false,
526
   6,
527
   complain_overflow_bitfield,
528
   bfd_elf_generic_reloc,
529
   "R_NIOS2_TLS_IE16",
530
   false,
531
   0x003fffc0,
532
   0x003fffc0,
533
   false),
534
535
  HOWTO (R_NIOS2_TLS_LE16,
536
   0,
537
   4,
538
   16,
539
   false,
540
   6,
541
   complain_overflow_bitfield,
542
   bfd_elf_generic_reloc,
543
   "R_NIOS2_TLS_LE16",
544
   false,
545
   0x003fffc0,
546
   0x003fffc0,
547
   false),
548
549
  HOWTO (R_NIOS2_TLS_DTPMOD,
550
   0,
551
   4,
552
   32,
553
   false,
554
   0,
555
   complain_overflow_dont,
556
   bfd_elf_generic_reloc,
557
   "R_NIOS2_TLS_DTPMOD",
558
   false,
559
   0xffffffff,
560
   0xffffffff,
561
   false),
562
563
  HOWTO (R_NIOS2_TLS_DTPREL,
564
   0,
565
   4,
566
   32,
567
   false,
568
   0,
569
   complain_overflow_dont,
570
   bfd_elf_generic_reloc,
571
   "R_NIOS2_TLS_DTPREL",
572
   false,
573
   0xffffffff,
574
   0xffffffff,
575
   false),
576
577
  HOWTO (R_NIOS2_TLS_TPREL,
578
   0,
579
   4,
580
   32,
581
   false,
582
   0,
583
   complain_overflow_dont,
584
   bfd_elf_generic_reloc,
585
   "R_NIOS2_TLS_TPREL",
586
   false,
587
   0xffffffff,
588
   0xffffffff,
589
   false),
590
591
  HOWTO (R_NIOS2_COPY,
592
   0,
593
   4,
594
   32,
595
   false,
596
   0,
597
   complain_overflow_dont,
598
   bfd_elf_generic_reloc,
599
   "R_NIOS2_COPY",
600
   false,
601
   0,
602
   0,
603
   false),
604
605
  HOWTO (R_NIOS2_GLOB_DAT,
606
   0,
607
   4,
608
   32,
609
   false,
610
   0,
611
   complain_overflow_dont,
612
   bfd_elf_generic_reloc,
613
   "R_NIOS2_GLOB_DAT",
614
   false,
615
   0xffffffff,
616
   0xffffffff,
617
   false),
618
619
  HOWTO (R_NIOS2_JUMP_SLOT,
620
   0,
621
   4,
622
   32,
623
   false,
624
   0,
625
   complain_overflow_dont,
626
   bfd_elf_generic_reloc,
627
   "R_NIOS2_JUMP_SLOT",
628
   false,
629
   0xffffffff,
630
   0xffffffff,
631
   false),
632
633
  HOWTO (R_NIOS2_RELATIVE,
634
   0,
635
   4,
636
   32,
637
   false,
638
   0,
639
   complain_overflow_dont,
640
   bfd_elf_generic_reloc,
641
   "R_NIOS2_RELATIVE",
642
   false,
643
   0xffffffff,
644
   0xffffffff,
645
   false),
646
647
  HOWTO (R_NIOS2_GOTOFF,
648
   0,
649
   4,
650
   32,
651
   false,
652
   0,
653
   complain_overflow_dont,
654
   bfd_elf_generic_reloc,
655
   "R_NIOS2_GOTOFF",
656
   false,
657
   0xffffffff,
658
   0xffffffff,
659
   false),
660
661
  HOWTO (R_NIOS2_CALL26_NOAT, /* type */
662
   2,     /* rightshift */
663
   4,     /* size */
664
   26,      /* bitsize */
665
   false,     /* pc_relative */
666
   6,     /* bitpos */
667
   complain_overflow_dont,  /* complain on overflow */
668
   nios2_elf32_call26_relocate, /* special function */
669
   "R_NIOS2_CALL26_NOAT", /* name */
670
   false,     /* partial_inplace */
671
   0xffffffc0,    /* src_mask */
672
   0xffffffc0,    /* dst_mask */
673
   false),    /* pcrel_offset */
674
675
  HOWTO (R_NIOS2_GOT_LO,
676
   0,
677
   4,
678
   16,
679
   false,
680
   6,
681
   complain_overflow_dont,
682
   bfd_elf_generic_reloc,
683
   "R_NIOS2_GOT_LO",
684
   false,
685
   0x003fffc0,
686
   0x003fffc0,
687
   false),
688
689
  HOWTO (R_NIOS2_GOT_HA,
690
   0,
691
   4,
692
   16,
693
   false,
694
   6,
695
   complain_overflow_dont,
696
   bfd_elf_generic_reloc,
697
   "R_NIOS2_GOT_HA",
698
   false,
699
   0x003fffc0,
700
   0x003fffc0,
701
   false),
702
703
  HOWTO (R_NIOS2_CALL_LO,
704
   0,
705
   4,
706
   16,
707
   false,
708
   6,
709
   complain_overflow_dont,
710
   bfd_elf_generic_reloc,
711
   "R_NIOS2_CALL_LO",
712
   false,
713
   0x003fffc0,
714
   0x003fffc0,
715
   false),
716
717
  HOWTO (R_NIOS2_CALL_HA,
718
   0,
719
   4,
720
   16,
721
   false,
722
   6,
723
   complain_overflow_dont,
724
   bfd_elf_generic_reloc,
725
   "R_NIOS2_CALL_HA",
726
   false,
727
   0x003fffc0,
728
   0x003fffc0,
729
   false),
730
731
/* Add other relocations here.  */
732
};
733
734
static reloc_howto_type elf_nios2_r2_howto_table_rel[] = {
735
  /* No relocation.  */
736
  HOWTO (R_NIOS2_NONE,    /* type */
737
   0,     /* rightshift */
738
   0,     /* size */
739
   0,     /* bitsize */
740
   false,     /* pc_relative */
741
   0,     /* bitpos */
742
   complain_overflow_dont,  /* complain_on_overflow */
743
   bfd_elf_generic_reloc, /* special_function */
744
   "R_NIOS2_NONE",  /* name */
745
   false,     /* partial_inplace */
746
   0,     /* src_mask */
747
   0,     /* dst_mask */
748
   false),    /* pcrel_offset */
749
750
  /* 16-bit signed immediate relocation.  */
751
  HOWTO (R_NIOS2_S16,   /* type */
752
   0,     /* rightshift */
753
   4,     /* size */
754
   16,      /* bitsize */
755
   false,     /* pc_relative */
756
   16,      /* bitpos */
757
   complain_overflow_signed,  /* complain on overflow */
758
   bfd_elf_generic_reloc, /* special function */
759
   "R_NIOS2_S16",   /* name */
760
   false,     /* partial_inplace */
761
   0xffff0000,    /* src_mask */
762
   0xffff0000,    /* dest_mask */
763
   false),    /* pcrel_offset */
764
765
  /* 16-bit unsigned immediate relocation.  */
766
  HOWTO (R_NIOS2_U16,   /* type */
767
   0,     /* rightshift */
768
   4,     /* size */
769
   16,      /* bitsize */
770
   false,     /* pc_relative */
771
   16,      /* bitpos */
772
   complain_overflow_unsigned,  /* complain on overflow */
773
   bfd_elf_generic_reloc, /* special function */
774
   "R_NIOS2_U16",   /* name */
775
   false,     /* partial_inplace */
776
   0xffff0000,    /* src_mask */
777
   0xffff0000,    /* dest_mask */
778
   false),    /* pcrel_offset */
779
780
  HOWTO (R_NIOS2_PCREL16, /* type */
781
   0,     /* rightshift */
782
   4,     /* size */
783
   16,      /* bitsize */
784
   true,      /* pc_relative */
785
   16,      /* bitpos */
786
   complain_overflow_signed,  /* complain on overflow */
787
   nios2_elf32_pcrel16_relocate,  /* special function */
788
   "R_NIOS2_PCREL16", /* name */
789
   false,     /* partial_inplace */
790
   0xffff0000,    /* src_mask */
791
   0xffff0000,    /* dest_mask */
792
   true),     /* pcrel_offset */
793
794
  HOWTO (R_NIOS2_CALL26,  /* type */
795
   2,     /* rightshift */
796
   4,     /* size */
797
   26,      /* bitsize */
798
   false,     /* pc_relative */
799
   6,     /* bitpos */
800
   complain_overflow_dont,  /* complain on overflow */
801
   nios2_elf32_call26_relocate, /* special function */
802
   "R_NIOS2_CALL26",  /* name */
803
   false,     /* partial_inplace */
804
   0xffffffc0,    /* src_mask */
805
   0xffffffc0,    /* dst_mask */
806
   false),    /* pcrel_offset */
807
808
  HOWTO (R_NIOS2_IMM5,
809
   0,
810
   4,
811
   5,
812
   false,
813
   21,
814
   complain_overflow_bitfield,
815
   bfd_elf_generic_reloc,
816
   "R_NIOS2_IMM5",
817
   false,
818
   0x03e00000,
819
   0x03e00000,
820
   false),
821
822
  HOWTO (R_NIOS2_CACHE_OPX,
823
   0,
824
   4,
825
   5,
826
   false,
827
   11,
828
   complain_overflow_bitfield,
829
   bfd_elf_generic_reloc,
830
   "R_NIOS2_CACHE_OPX",
831
   false,
832
   0x0000f800,
833
   0x0000f800,
834
   false),
835
836
  HOWTO (R_NIOS2_IMM6,
837
   0,
838
   4,
839
   6,
840
   false,
841
   26,
842
   complain_overflow_bitfield,
843
   bfd_elf_generic_reloc,
844
   "R_NIOS2_IMM6",
845
   false,
846
   0xfc000000,
847
   0xfc000000,
848
   false),
849
850
  HOWTO (R_NIOS2_IMM8,
851
   0,
852
   4,
853
   8,
854
   false,
855
   24,
856
   complain_overflow_bitfield,
857
   bfd_elf_generic_reloc,
858
   "R_NIOS2_IMM8",
859
   false,
860
   0xff000000,
861
   0xff000000,
862
   false),
863
864
  HOWTO (R_NIOS2_HI16,
865
   0,
866
   4,
867
   32,
868
   false,
869
   16,
870
   complain_overflow_dont,
871
   nios2_elf32_hi16_relocate,
872
   "R_NIOS2_HI16",
873
   false,
874
   0xffff0000,
875
   0xffff0000,
876
   false),
877
878
  HOWTO (R_NIOS2_LO16,
879
   0,
880
   4,
881
   32,
882
   false,
883
   16,
884
   complain_overflow_dont,
885
   nios2_elf32_lo16_relocate,
886
   "R_NIOS2_LO16",
887
   false,
888
   0xffff0000,
889
   0xffff0000,
890
   false),
891
892
  HOWTO (R_NIOS2_HIADJ16,
893
   0,
894
   4,
895
   32,
896
   false,
897
   16,
898
   complain_overflow_dont,
899
   nios2_elf32_hiadj16_relocate,
900
   "R_NIOS2_HIADJ16",
901
   false,
902
   0xffff0000,
903
   0xffff0000,
904
   false),
905
906
  HOWTO (R_NIOS2_BFD_RELOC_32,
907
   0,
908
   4,     /* long */
909
   32,
910
   false,
911
   0,
912
   complain_overflow_dont,
913
   bfd_elf_generic_reloc,
914
   "R_NIOS2_BFD_RELOC32",
915
   false,
916
   0xffffffff,
917
   0xffffffff,
918
   false),
919
920
  HOWTO (R_NIOS2_BFD_RELOC_16,
921
   0,
922
   2,     /* short */
923
   16,
924
   false,
925
   0,
926
   complain_overflow_bitfield,
927
   bfd_elf_generic_reloc,
928
   "R_NIOS2_BFD_RELOC16",
929
   false,
930
   0x0000ffff,
931
   0x0000ffff,
932
   false),
933
934
  HOWTO (R_NIOS2_BFD_RELOC_8,
935
   0,
936
   1,     /* byte */
937
   8,
938
   false,
939
   0,
940
   complain_overflow_bitfield,
941
   bfd_elf_generic_reloc,
942
   "R_NIOS2_BFD_RELOC8",
943
   false,
944
   0x000000ff,
945
   0x000000ff,
946
   false),
947
948
  HOWTO (R_NIOS2_GPREL,
949
   0,
950
   4,
951
   32,
952
   false,
953
   16,
954
   complain_overflow_dont,
955
   nios2_elf32_gprel_relocate,
956
   "R_NIOS2_GPREL",
957
   false,
958
   0xffff0000,
959
   0xffff0000,
960
   false),
961
962
  HOWTO (R_NIOS2_GNU_VTINHERIT,
963
   0,
964
   4,
965
   0,
966
   false,
967
   0,
968
   complain_overflow_dont,
969
   NULL,
970
   "R_NIOS2_GNU_VTINHERIT",
971
   false,
972
   0,
973
   0,
974
   false),
975
976
  HOWTO (R_NIOS2_GNU_VTENTRY,
977
   0,
978
   4,
979
   0,
980
   false,
981
   0,
982
   complain_overflow_dont,
983
   _bfd_elf_rel_vtable_reloc_fn,
984
   "R_NIOS2_GNU_VTENTRY",
985
   false,
986
   0,
987
   0,
988
   false),
989
990
  HOWTO (R_NIOS2_UJMP,
991
   0,
992
   4,
993
   32,
994
   false,
995
   16,
996
   complain_overflow_dont,
997
   nios2_elf32_ujmp_relocate,
998
   "R_NIOS2_UJMP",
999
   false,
1000
   0xffff0000,
1001
   0xffff0000,
1002
   false),
1003
1004
  HOWTO (R_NIOS2_CJMP,
1005
   0,
1006
   4,
1007
   32,
1008
   false,
1009
   16,
1010
   complain_overflow_dont,
1011
   nios2_elf32_cjmp_relocate,
1012
   "R_NIOS2_CJMP",
1013
   false,
1014
   0xffff0000,
1015
   0xffff0000,
1016
   false),
1017
1018
  HOWTO (R_NIOS2_CALLR,
1019
   0,
1020
   4,
1021
   32,
1022
   false,
1023
   16,
1024
   complain_overflow_dont,
1025
   nios2_elf32_callr_relocate,
1026
   "R_NIOS2_CALLR",
1027
   false,
1028
   0xffff0000,
1029
   0xffff0000,
1030
   false),
1031
1032
  HOWTO (R_NIOS2_ALIGN,
1033
   0,
1034
   4,
1035
   0,
1036
   false,
1037
   0,
1038
   complain_overflow_dont,
1039
   nios2_elf32_ignore_reloc,
1040
   "R_NIOS2_ALIGN",
1041
   false,
1042
   0,
1043
   0,
1044
   true),
1045
1046
  HOWTO (R_NIOS2_GOT16,
1047
   0,
1048
   4,
1049
   16,
1050
   false,
1051
   16,
1052
   complain_overflow_bitfield,
1053
   bfd_elf_generic_reloc,
1054
   "R_NIOS2_GOT16",
1055
   false,
1056
   0xffff0000,
1057
   0xffff0000,
1058
   false),
1059
1060
  HOWTO (R_NIOS2_CALL16,
1061
   0,
1062
   4,
1063
   16,
1064
   false,
1065
   16,
1066
   complain_overflow_bitfield,
1067
   bfd_elf_generic_reloc,
1068
   "R_NIOS2_CALL16",
1069
   false,
1070
   0xffff0000,
1071
   0xffff0000,
1072
   false),
1073
1074
  HOWTO (R_NIOS2_GOTOFF_LO,
1075
   0,
1076
   4,
1077
   16,
1078
   false,
1079
   16,
1080
   complain_overflow_dont,
1081
   bfd_elf_generic_reloc,
1082
   "R_NIOS2_GOTOFF_LO",
1083
   false,
1084
   0xffff0000,
1085
   0xffff0000,
1086
   false),
1087
1088
  HOWTO (R_NIOS2_GOTOFF_HA,
1089
   0,
1090
   4,
1091
   16,
1092
   false,
1093
   16,
1094
   complain_overflow_dont,
1095
   bfd_elf_generic_reloc,
1096
   "R_NIOS2_GOTOFF_HA",
1097
   false,
1098
   0xffff0000,
1099
   0xffff0000,
1100
   false),
1101
1102
  HOWTO (R_NIOS2_PCREL_LO,
1103
   0,
1104
   4,
1105
   16,
1106
   true,
1107
   16,
1108
   complain_overflow_dont,
1109
   nios2_elf32_pcrel_lo16_relocate,
1110
   "R_NIOS2_PCREL_LO",
1111
   false,
1112
   0xffff0000,
1113
   0xffff0000,
1114
   true),
1115
1116
  HOWTO (R_NIOS2_PCREL_HA,
1117
   0,
1118
   4,
1119
   16,
1120
   false, /* This is a PC-relative relocation, but we need to subtract
1121
       PC ourselves before the HIADJ.  */
1122
   16,
1123
   complain_overflow_dont,
1124
   nios2_elf32_pcrel_hiadj16_relocate,
1125
   "R_NIOS2_PCREL_HA",
1126
   false,
1127
   0xffff0000,
1128
   0xffff0000,
1129
   true),
1130
1131
  HOWTO (R_NIOS2_TLS_GD16,
1132
   0,
1133
   4,
1134
   16,
1135
   false,
1136
   16,
1137
   complain_overflow_bitfield,
1138
   bfd_elf_generic_reloc,
1139
   "R_NIOS2_TLS_GD16",
1140
   false,
1141
   0xffff0000,
1142
   0xffff0000,
1143
   false),
1144
1145
  HOWTO (R_NIOS2_TLS_LDM16,
1146
   0,
1147
   4,
1148
   16,
1149
   false,
1150
   16,
1151
   complain_overflow_bitfield,
1152
   bfd_elf_generic_reloc,
1153
   "R_NIOS2_TLS_LDM16",
1154
   false,
1155
   0xffff0000,
1156
   0xffff0000,
1157
   false),
1158
1159
  HOWTO (R_NIOS2_TLS_LDO16,
1160
   0,
1161
   4,
1162
   16,
1163
   false,
1164
   16,
1165
   complain_overflow_bitfield,
1166
   bfd_elf_generic_reloc,
1167
   "R_NIOS2_TLS_LDO16",
1168
   false,
1169
   0xffff0000,
1170
   0xffff0000,
1171
   false),
1172
1173
  HOWTO (R_NIOS2_TLS_IE16,
1174
   0,
1175
   4,
1176
   16,
1177
   false,
1178
   16,
1179
   complain_overflow_bitfield,
1180
   bfd_elf_generic_reloc,
1181
   "R_NIOS2_TLS_IE16",
1182
   false,
1183
   0xffff0000,
1184
   0xffff0000,
1185
   false),
1186
1187
  HOWTO (R_NIOS2_TLS_LE16,
1188
   0,
1189
   4,
1190
   16,
1191
   false,
1192
   16,
1193
   complain_overflow_bitfield,
1194
   bfd_elf_generic_reloc,
1195
   "R_NIOS2_TLS_LE16",
1196
   false,
1197
   0xffff0000,
1198
   0xffff0000,
1199
   false),
1200
1201
  HOWTO (R_NIOS2_TLS_DTPMOD,
1202
   0,
1203
   4,
1204
   32,
1205
   false,
1206
   0,
1207
   complain_overflow_dont,
1208
   bfd_elf_generic_reloc,
1209
   "R_NIOS2_TLS_DTPMOD",
1210
   false,
1211
   0xffffffff,
1212
   0xffffffff,
1213
   false),
1214
1215
  HOWTO (R_NIOS2_TLS_DTPREL,
1216
   0,
1217
   4,
1218
   32,
1219
   false,
1220
   0,
1221
   complain_overflow_dont,
1222
   bfd_elf_generic_reloc,
1223
   "R_NIOS2_TLS_DTPREL",
1224
   false,
1225
   0xffffffff,
1226
   0xffffffff,
1227
   false),
1228
1229
  HOWTO (R_NIOS2_TLS_TPREL,
1230
   0,
1231
   4,
1232
   32,
1233
   false,
1234
   0,
1235
   complain_overflow_dont,
1236
   bfd_elf_generic_reloc,
1237
   "R_NIOS2_TLS_TPREL",
1238
   false,
1239
   0xffffffff,
1240
   0xffffffff,
1241
   false),
1242
1243
  HOWTO (R_NIOS2_COPY,
1244
   0,
1245
   4,
1246
   32,
1247
   false,
1248
   0,
1249
   complain_overflow_dont,
1250
   bfd_elf_generic_reloc,
1251
   "R_NIOS2_COPY",
1252
   false,
1253
   0,
1254
   0,
1255
   false),
1256
1257
  HOWTO (R_NIOS2_GLOB_DAT,
1258
   0,
1259
   4,
1260
   32,
1261
   false,
1262
   0,
1263
   complain_overflow_dont,
1264
   bfd_elf_generic_reloc,
1265
   "R_NIOS2_GLOB_DAT",
1266
   false,
1267
   0xffffffff,
1268
   0xffffffff,
1269
   false),
1270
1271
  HOWTO (R_NIOS2_JUMP_SLOT,
1272
   0,
1273
   4,
1274
   32,
1275
   false,
1276
   0,
1277
   complain_overflow_dont,
1278
   bfd_elf_generic_reloc,
1279
   "R_NIOS2_JUMP_SLOT",
1280
   false,
1281
   0xffffffff,
1282
   0xffffffff,
1283
   false),
1284
1285
  HOWTO (R_NIOS2_RELATIVE,
1286
   0,
1287
   4,
1288
   32,
1289
   false,
1290
   0,
1291
   complain_overflow_dont,
1292
   bfd_elf_generic_reloc,
1293
   "R_NIOS2_RELATIVE",
1294
   false,
1295
   0xffffffff,
1296
   0xffffffff,
1297
   false),
1298
1299
  HOWTO (R_NIOS2_GOTOFF,
1300
   0,
1301
   4,
1302
   32,
1303
   false,
1304
   0,
1305
   complain_overflow_dont,
1306
   bfd_elf_generic_reloc,
1307
   "R_NIOS2_GOTOFF",
1308
   false,
1309
   0xffffffff,
1310
   0xffffffff,
1311
   false),
1312
1313
  HOWTO (R_NIOS2_CALL26_NOAT, /* type */
1314
   2,     /* rightshift */
1315
   4,     /* size */
1316
   26,      /* bitsize */
1317
   false,     /* pc_relative */
1318
   6,     /* bitpos */
1319
   complain_overflow_dont,  /* complain on overflow */
1320
   nios2_elf32_call26_relocate, /* special function */
1321
   "R_NIOS2_CALL26_NOAT", /* name */
1322
   false,     /* partial_inplace */
1323
   0xffffffc0,    /* src_mask */
1324
   0xffffffc0,    /* dst_mask */
1325
   false),    /* pcrel_offset */
1326
1327
  HOWTO (R_NIOS2_GOT_LO,
1328
   0,
1329
   4,
1330
   16,
1331
   false,
1332
   16,
1333
   complain_overflow_dont,
1334
   bfd_elf_generic_reloc,
1335
   "R_NIOS2_GOT_LO",
1336
   false,
1337
   0xffff0000,
1338
   0xffff0000,
1339
   false),
1340
1341
  HOWTO (R_NIOS2_GOT_HA,
1342
   0,
1343
   4,
1344
   16,
1345
   false,
1346
   16,
1347
   complain_overflow_dont,
1348
   bfd_elf_generic_reloc,
1349
   "R_NIOS2_GOT_HA",
1350
   false,
1351
   0xffff0000,
1352
   0xffff0000,
1353
   false),
1354
1355
  HOWTO (R_NIOS2_CALL_LO,
1356
   0,
1357
   4,
1358
   16,
1359
   false,
1360
   16,
1361
   complain_overflow_dont,
1362
   bfd_elf_generic_reloc,
1363
   "R_NIOS2_CALL_LO",
1364
   false,
1365
   0xffff0000,
1366
   0xffff0000,
1367
   false),
1368
1369
  HOWTO (R_NIOS2_CALL_HA,
1370
   0,
1371
   4,
1372
   16,
1373
   false,
1374
   16,
1375
   complain_overflow_dont,
1376
   bfd_elf_generic_reloc,
1377
   "R_NIOS2_CALL_HA",
1378
   false,
1379
   0xffff0000,
1380
   0xffff0000,
1381
   false),
1382
1383
  HOWTO (R_NIOS2_R2_S12,
1384
   0,
1385
   4,
1386
   12,
1387
   false,
1388
   16,
1389
   complain_overflow_signed,
1390
   bfd_elf_generic_reloc,
1391
   "R_NIOS2_R2_S12",
1392
   false,
1393
   0x0fff0000,
1394
   0x0fff0000,
1395
   false),
1396
1397
  HOWTO (R_NIOS2_R2_I10_1_PCREL,
1398
   1,
1399
   2,
1400
   10,
1401
   true,
1402
   6,
1403
   complain_overflow_signed,
1404
   bfd_elf_generic_reloc,   /* FIXME? */
1405
   "R_NIOS2_R2_I10_1_PCREL",
1406
   false,
1407
   0xffc0,
1408
   0xffc0,
1409
   true),
1410
1411
  HOWTO (R_NIOS2_R2_T1I7_1_PCREL,
1412
   1,
1413
   2,
1414
   7,
1415
   true,
1416
   9,
1417
   complain_overflow_signed,
1418
   bfd_elf_generic_reloc,   /* FIXME? */
1419
   "R_NIOS2_R2_T1I7_1_PCREL",
1420
   false,
1421
   0xfe00,
1422
   0xfe00,
1423
   true),
1424
1425
  HOWTO (R_NIOS2_R2_T1I7_2,
1426
   2,
1427
   2,
1428
   7,
1429
   false,
1430
   9,
1431
   complain_overflow_unsigned,
1432
   bfd_elf_generic_reloc,
1433
   "R_NIOS2_R2_T1I7_2",
1434
   false,
1435
   0xfe00,
1436
   0xfe00,
1437
   false),
1438
1439
  HOWTO (R_NIOS2_R2_T2I4,
1440
   0,
1441
   2,
1442
   4,
1443
   false,
1444
   12,
1445
   complain_overflow_unsigned,
1446
   bfd_elf_generic_reloc,
1447
   "R_NIOS2_R2_T2I4",
1448
   false,
1449
   0xf000,
1450
   0xf000,
1451
   false),
1452
1453
  HOWTO (R_NIOS2_R2_T2I4_1,
1454
   1,
1455
   2,
1456
   4,
1457
   false,
1458
   12,
1459
   complain_overflow_unsigned,
1460
   bfd_elf_generic_reloc,
1461
   "R_NIOS2_R2_T2I4_1",
1462
   false,
1463
   0xf000,
1464
   0xf000,
1465
   false),
1466
1467
  HOWTO (R_NIOS2_R2_T2I4_2,
1468
   2,
1469
   2,
1470
   4,
1471
   false,
1472
   12,
1473
   complain_overflow_unsigned,
1474
   bfd_elf_generic_reloc,
1475
   "R_NIOS2_R2_T2I4_2",
1476
   false,
1477
   0xf000,
1478
   0xf000,
1479
   false),
1480
1481
  HOWTO (R_NIOS2_R2_X1I7_2,
1482
   2,
1483
   2,
1484
   7,
1485
   false,
1486
   6,
1487
   complain_overflow_unsigned,
1488
   bfd_elf_generic_reloc,
1489
   "R_NIOS2_R2_X1I7_2",
1490
   false,
1491
   0x1fc0,
1492
   0x1fc0,
1493
   false),
1494
1495
  HOWTO (R_NIOS2_R2_X2L5,
1496
   0,
1497
   2,
1498
   5,
1499
   false,
1500
   6,
1501
   complain_overflow_unsigned,
1502
   bfd_elf_generic_reloc,
1503
   "R_NIOS2_R2_X2L5",
1504
   false,
1505
   0x07c0,
1506
   0x07c0,
1507
   false),
1508
1509
  HOWTO (R_NIOS2_R2_F1I5_2,
1510
   2,
1511
   2,
1512
   5,
1513
   false,
1514
   6,
1515
   complain_overflow_unsigned,
1516
   bfd_elf_generic_reloc,
1517
   "R_NIOS2_R2_F1L5_2",
1518
   false,
1519
   0x07c0,
1520
   0x07c0,
1521
   false),
1522
1523
  HOWTO (R_NIOS2_R2_L5I4X1,
1524
   2,
1525
   2,
1526
   4,
1527
   false,
1528
   6,
1529
   complain_overflow_unsigned,
1530
   bfd_elf_generic_reloc,
1531
   "R_NIOS2_R2_L5I4X1",
1532
   false,
1533
   0x03c0,
1534
   0x03c0,
1535
   false),
1536
1537
  HOWTO (R_NIOS2_R2_T1X1I6,
1538
   0,
1539
   2,
1540
   6,
1541
   false,
1542
   9,
1543
   complain_overflow_unsigned,
1544
   bfd_elf_generic_reloc,
1545
   "R_NIOS2_R2_T1X1I6",
1546
   false,
1547
   0x7e00,
1548
   0x7e00,
1549
   false),
1550
1551
  HOWTO (R_NIOS2_R2_T1X1I6_2,
1552
   2,
1553
   4,
1554
   6,
1555
   false,
1556
   9,
1557
   complain_overflow_unsigned,
1558
   bfd_elf_generic_reloc,
1559
   "R_NIOS2_R2_T1I1X6_2",
1560
   false,
1561
   0x7e00,
1562
   0x7e00,
1563
   false),
1564
1565
/* Add other relocations here.  */
1566
};
1567
1568
static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
1569
1570
1571
/* Return true if producing output for a R2 BFD.  */
1572
0
#define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
1573
1574
/* Return the howto for relocation RTYPE.  */
1575
static reloc_howto_type *
1576
lookup_howto (unsigned int rtype, bfd *abfd)
1577
0
{
1578
0
  static int initialized = 0;
1579
0
  int i;
1580
  /* R2 relocations are a superset of R1, so use that for the lookup
1581
     table.  */
1582
0
  int r1_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
1583
0
  int r2_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
1584
1585
0
  if (!initialized)
1586
0
    {
1587
0
      initialized = 1;
1588
0
      memset (elf_code_to_howto_index, 0xff,
1589
0
        sizeof (elf_code_to_howto_index));
1590
0
      for (i = 0; i < r2_howto_tbl_size; i++)
1591
0
  {
1592
0
    elf_code_to_howto_index[elf_nios2_r2_howto_table_rel[i].type] = i;
1593
0
    if (i < r1_howto_tbl_size)
1594
0
      BFD_ASSERT (elf_nios2_r2_howto_table_rel[i].type
1595
0
      == elf_nios2_r1_howto_table_rel[i].type);
1596
0
  }
1597
0
    }
1598
1599
0
  if (rtype > R_NIOS2_ILLEGAL)
1600
0
    return NULL;
1601
0
  i = elf_code_to_howto_index[rtype];
1602
0
  if (BFD_IS_R2 (abfd))
1603
0
    {
1604
0
      if (i >= r2_howto_tbl_size)
1605
0
  return NULL;
1606
0
      return elf_nios2_r2_howto_table_rel + i;
1607
0
    }
1608
0
  else
1609
0
    {
1610
0
      if (i >= r1_howto_tbl_size)
1611
0
  return NULL;
1612
0
      return elf_nios2_r1_howto_table_rel + i;
1613
0
    }
1614
0
}
1615
1616
/* Map for converting BFD reloc types to Nios II reloc types.  */
1617
struct elf_reloc_map
1618
{
1619
  bfd_reloc_code_real_type bfd_val;
1620
  enum elf_nios2_reloc_type elf_val;
1621
};
1622
1623
static const struct elf_reloc_map nios2_reloc_map[] =
1624
{
1625
  {BFD_RELOC_NONE, R_NIOS2_NONE},
1626
  {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
1627
  {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
1628
  {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
1629
  {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
1630
  {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
1631
  {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
1632
  {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
1633
  {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
1634
  {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
1635
  {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
1636
  {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
1637
  {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
1638
  {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
1639
  {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
1640
  {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
1641
  {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
1642
  {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
1643
  {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
1644
  {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
1645
  {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
1646
  {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
1647
  {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
1648
  {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
1649
  {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
1650
  {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
1651
  {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
1652
  {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
1653
  {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
1654
  {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
1655
  {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
1656
  {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
1657
  {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
1658
  {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
1659
  {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
1660
  {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
1661
  {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
1662
  {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
1663
  {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
1664
  {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
1665
  {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF},
1666
  {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT},
1667
  {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO},
1668
  {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA},
1669
  {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO},
1670
  {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA},
1671
  {BFD_RELOC_NIOS2_R2_S12, R_NIOS2_R2_S12},
1672
  {BFD_RELOC_NIOS2_R2_I10_1_PCREL, R_NIOS2_R2_I10_1_PCREL},
1673
  {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, R_NIOS2_R2_T1I7_1_PCREL},
1674
  {BFD_RELOC_NIOS2_R2_T1I7_2, R_NIOS2_R2_T1I7_2},
1675
  {BFD_RELOC_NIOS2_R2_T2I4, R_NIOS2_R2_T2I4},
1676
  {BFD_RELOC_NIOS2_R2_T2I4_1, R_NIOS2_R2_T2I4_1},
1677
  {BFD_RELOC_NIOS2_R2_T2I4_2, R_NIOS2_R2_T2I4_2},
1678
  {BFD_RELOC_NIOS2_R2_X1I7_2, R_NIOS2_R2_X1I7_2},
1679
  {BFD_RELOC_NIOS2_R2_X2L5, R_NIOS2_R2_X2L5},
1680
  {BFD_RELOC_NIOS2_R2_F1I5_2, R_NIOS2_R2_F1I5_2},
1681
  {BFD_RELOC_NIOS2_R2_L5I4X1, R_NIOS2_R2_L5I4X1},
1682
  {BFD_RELOC_NIOS2_R2_T1X1I6, R_NIOS2_R2_T1X1I6},
1683
  {BFD_RELOC_NIOS2_R2_T1X1I6_2, R_NIOS2_R2_T1X1I6_2},
1684
};
1685
1686
enum elf32_nios2_stub_type
1687
{
1688
  nios2_stub_call26_before,
1689
  nios2_stub_call26_after,
1690
  nios2_stub_none
1691
};
1692
1693
struct elf32_nios2_stub_hash_entry
1694
{
1695
  /* Base hash table entry structure.  */
1696
  struct bfd_hash_entry bh_root;
1697
1698
  /* The stub section.  */
1699
  asection *stub_sec;
1700
1701
  /* Offset within stub_sec of the beginning of this stub.  */
1702
  bfd_vma stub_offset;
1703
1704
  /* Given the symbol's value and its section we can determine its final
1705
     value when building the stubs (so the stub knows where to jump.  */
1706
  bfd_vma target_value;
1707
  asection *target_section;
1708
1709
  enum elf32_nios2_stub_type stub_type;
1710
1711
  /* The symbol table entry, if any, that this was derived from.  */
1712
  struct elf32_nios2_link_hash_entry *hh;
1713
1714
  /* And the reloc addend that this was derived from.  */
1715
  bfd_vma addend;
1716
1717
  /* Where this stub is being called from, or, in the case of combined
1718
     stub sections, the first input section in the group.  */
1719
  asection *id_sec;
1720
};
1721
1722
#define nios2_stub_hash_entry(ent) \
1723
  ((struct elf32_nios2_stub_hash_entry *)(ent))
1724
1725
#define nios2_stub_hash_lookup(table, string, create, copy) \
1726
0
  ((struct elf32_nios2_stub_hash_entry *) \
1727
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
1728
1729
1730
/* Nios II ELF linker hash entry.  */
1731
1732
struct elf32_nios2_link_hash_entry
1733
{
1734
  struct elf_link_hash_entry root;
1735
1736
  /* A pointer to the most recently used stub hash entry against this
1737
     symbol.  */
1738
  struct elf32_nios2_stub_hash_entry *hsh_cache;
1739
1740
0
#define GOT_UNKNOWN 0
1741
0
#define GOT_NORMAL  1
1742
0
#define GOT_TLS_GD  2
1743
0
#define GOT_TLS_IE  4
1744
  unsigned char tls_type;
1745
1746
  /* We need to detect and take special action for symbols which are only
1747
     referenced with %call() and not with %got().  Such symbols do not need
1748
     a dynamic GOT reloc in shared objects, only a dynamic PLT reloc.  Lazy
1749
     linking will not work if the dynamic GOT reloc exists.
1750
     To check for this condition efficiently, we compare got_types_used against
1751
     CALL_USED, meaning
1752
     (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1753
  */
1754
0
#define GOT_USED  1
1755
0
#define CALL_USED 2
1756
  unsigned char got_types_used;
1757
};
1758
1759
#define elf32_nios2_hash_entry(ent) \
1760
0
  ((struct elf32_nios2_link_hash_entry *) (ent))
1761
1762
/* Get the Nios II elf linker hash table from a link_info structure.  */
1763
#define elf32_nios2_hash_table(info) \
1764
0
  ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1765
1766
/* Nios II ELF linker hash table.  */
1767
struct elf32_nios2_link_hash_table
1768
  {
1769
    /* The main hash table.  */
1770
    struct elf_link_hash_table root;
1771
1772
    /* The stub hash table.  */
1773
    struct bfd_hash_table bstab;
1774
1775
    /* Linker stub bfd.  */
1776
    bfd *stub_bfd;
1777
1778
    /* Linker call-backs.  */
1779
    asection * (*add_stub_section) (const char *, asection *, bool);
1780
    void (*layout_sections_again) (void);
1781
1782
    /* Array to keep track of which stub sections have been created, and
1783
       information on stub grouping.  */
1784
    struct map_stub
1785
    {
1786
      /* These are the section to which stubs in the group will be
1787
   attached.  */
1788
      asection *first_sec, *last_sec;
1789
      /* The stub sections.  There might be stubs inserted either before
1790
   or after the real section.*/
1791
      asection *first_stub_sec, *last_stub_sec;
1792
    } *stub_group;
1793
1794
    /* Assorted information used by nios2_elf32_size_stubs.  */
1795
    unsigned int bfd_count;
1796
    unsigned int top_index;
1797
    asection **input_list;
1798
    Elf_Internal_Sym **all_local_syms;
1799
1800
    /* Short-cuts to get to dynamic linker sections.  */
1801
    asection *sbss;
1802
1803
    /* GOT pointer symbol _gp_got.  */
1804
    struct elf_link_hash_entry *h_gp_got;
1805
1806
    union {
1807
      bfd_signed_vma refcount;
1808
      bfd_vma offset;
1809
    } tls_ldm_got;
1810
1811
    bfd_vma res_n_size;
1812
  };
1813
1814
struct nios2_elf32_obj_tdata
1815
{
1816
  struct elf_obj_tdata root;
1817
1818
  /* tls_type for each local got entry.  */
1819
  char *local_got_tls_type;
1820
1821
  /* TRUE if TLS GD relocs have been seen for this object.  */
1822
  bool has_tlsgd;
1823
};
1824
1825
#define elf32_nios2_tdata(abfd) \
1826
0
  ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1827
1828
#define elf32_nios2_local_got_tls_type(abfd) \
1829
0
  (elf32_nios2_tdata (abfd)->local_got_tls_type)
1830
1831
/* The name of the dynamic interpreter.  This is put in the .interp
1832
   section.  */
1833
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1834
1835
/* PLT implementation for position-dependent code.  */
1836
static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1837
  0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1838
  0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1839
  0x7800683a  /* jmp r15 */
1840
};
1841
1842
static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1843
  0x03800034, /* movhi r14, %hiadj(res_0) */
1844
  0x73800004, /* addi r14, r14, %lo(res_0) */
1845
  0x7b9fc83a, /* sub r15, r15, r14 */
1846
  0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1847
  0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1848
  0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1849
  0x6800683a  /* jmp r13 */
1850
};
1851
1852
/* PLT implementation for position-independent code.  */
1853
static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1854
  0x03c00034, /* movhi r15, %hiadj(index * 4) */
1855
  0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1856
  0x00000006  /* br .PLTresolve */
1857
};
1858
1859
static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1860
  0x001ce03a, /* nextpc r14 */
1861
  0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1862
  0x6b9b883a, /* add r13, r13, r14 */
1863
  0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1864
  0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1865
  0x6800683a  /* jmp r13 */
1866
};
1867
1868
/* CALL26 stub.  */
1869
static const bfd_vma nios2_call26_stub_entry[] = {
1870
  0x00400034, /* orhi at, r0, %hiadj(dest) */
1871
  0x08400004, /* addi at, at, %lo(dest) */
1872
  0x0800683a  /* jmp at */
1873
};
1874
1875
/* Install 16-bit immediate value VALUE at offset OFFSET into section SEC.  */
1876
static void
1877
nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1878
0
{
1879
0
  bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1880
1881
0
  BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
1882
1883
0
  bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1884
0
        sec->contents + offset);
1885
0
}
1886
1887
/* Install COUNT 32-bit values DATA starting at offset OFFSET into
1888
   section SEC. */
1889
static void
1890
nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1891
        int count)
1892
0
{
1893
0
  while (count--)
1894
0
    {
1895
0
      bfd_put_32 (sec->owner, *data, sec->contents + offset);
1896
0
      offset += 4;
1897
0
      ++data;
1898
0
    }
1899
0
}
1900
1901
/* The usual way of loading a 32-bit constant into a Nios II register is to
1902
   load the high 16 bits in one instruction and then add the low 16 bits with
1903
   a signed add. This means that the high halfword needs to be adjusted to
1904
   compensate for the sign bit of the low halfword. This function returns the
1905
   adjusted high halfword for a given 32-bit constant.  */
1906
static
1907
bfd_vma hiadj (bfd_vma symbol_value)
1908
0
{
1909
0
  return ((symbol_value + 0x8000) >> 16) & 0xffff;
1910
0
}
1911
1912
/* Implement elf_backend_grok_prstatus:
1913
   Support for core dump NOTE sections.  */
1914
static bool
1915
nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1916
0
{
1917
0
  int offset;
1918
0
  size_t size;
1919
1920
0
  switch (note->descsz)
1921
0
    {
1922
0
    default:
1923
0
      return false;
1924
1925
0
    case 212:       /* Linux/Nios II */
1926
      /* pr_cursig */
1927
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1928
1929
      /* pr_pid */
1930
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
1931
1932
      /* pr_reg */
1933
0
      offset = 72;
1934
0
      size = 136;
1935
1936
0
      break;
1937
0
    }
1938
1939
  /* Make a ".reg/999" section.  */
1940
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1941
0
            size, note->descpos + offset);
1942
0
}
1943
1944
/* Implement elf_backend_grok_psinfo.  */
1945
static bool
1946
nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1947
0
{
1948
0
  switch (note->descsz)
1949
0
    {
1950
0
    default:
1951
0
      return false;
1952
1953
0
    case 124:       /* Linux/Nios II elf_prpsinfo */
1954
0
      elf_tdata (abfd)->core->program
1955
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1956
0
      elf_tdata (abfd)->core->command
1957
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1958
0
    }
1959
1960
  /* Note that for some reason, a spurious space is tacked
1961
     onto the end of the args in some (at least one anyway)
1962
     implementations, so strip it off if it exists.  */
1963
1964
0
  {
1965
0
    char *command = elf_tdata (abfd)->core->command;
1966
0
    int n = strlen (command);
1967
1968
0
    if (0 < n && command[n - 1] == ' ')
1969
0
      command[n - 1] = '\0';
1970
0
  }
1971
1972
0
  return true;
1973
0
}
1974
1975
/* Assorted hash table functions.  */
1976
1977
/* Initialize an entry in the stub hash table.  */
1978
static struct bfd_hash_entry *
1979
stub_hash_newfunc (struct bfd_hash_entry *entry,
1980
       struct bfd_hash_table *table,
1981
       const char *string)
1982
0
{
1983
  /* Allocate the structure if it has not already been allocated by a
1984
     subclass.  */
1985
0
  if (entry == NULL)
1986
0
    {
1987
0
      entry = bfd_hash_allocate (table,
1988
0
         sizeof (struct elf32_nios2_stub_hash_entry));
1989
0
      if (entry == NULL)
1990
0
  return entry;
1991
0
    }
1992
1993
  /* Call the allocation method of the superclass.  */
1994
0
  entry = bfd_hash_newfunc (entry, table, string);
1995
0
  if (entry != NULL)
1996
0
    {
1997
0
      struct elf32_nios2_stub_hash_entry *hsh;
1998
1999
      /* Initialize the local fields.  */
2000
0
      hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2001
0
      hsh->stub_sec = NULL;
2002
0
      hsh->stub_offset = 0;
2003
0
      hsh->target_value = 0;
2004
0
      hsh->target_section = NULL;
2005
0
      hsh->stub_type = nios2_stub_none;
2006
0
      hsh->hh = NULL;
2007
0
      hsh->id_sec = NULL;
2008
0
    }
2009
2010
0
  return entry;
2011
0
}
2012
2013
/* Create an entry in a Nios II ELF linker hash table.  */
2014
static struct bfd_hash_entry *
2015
link_hash_newfunc (struct bfd_hash_entry *entry,
2016
       struct bfd_hash_table *table, const char *string)
2017
0
{
2018
  /* Allocate the structure if it has not already been allocated by a
2019
     subclass.  */
2020
0
  if (entry == NULL)
2021
0
    {
2022
0
      entry = bfd_hash_allocate (table,
2023
0
         sizeof (struct elf32_nios2_link_hash_entry));
2024
0
      if (entry == NULL)
2025
0
  return entry;
2026
0
    }
2027
2028
  /* Call the allocation method of the superclass.  */
2029
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2030
0
  if (entry)
2031
0
    {
2032
0
      struct elf32_nios2_link_hash_entry *eh;
2033
2034
0
      eh = (struct elf32_nios2_link_hash_entry *) entry;
2035
0
      eh->hsh_cache = NULL;
2036
0
      eh->tls_type = GOT_UNKNOWN;
2037
0
      eh->got_types_used = 0;
2038
0
    }
2039
2040
0
  return entry;
2041
0
}
2042
2043
/* Section name for stubs is the associated section name plus this
2044
   string.  */
2045
0
#define STUB_SUFFIX ".stub"
2046
2047
/* Build a name for an entry in the stub hash table.  */
2048
static char *
2049
nios2_stub_name (const asection *input_section,
2050
     const asection *sym_sec,
2051
     const struct elf32_nios2_link_hash_entry *hh,
2052
     const Elf_Internal_Rela *rel,
2053
     enum elf32_nios2_stub_type stub_type)
2054
0
{
2055
0
  char *stub_name;
2056
0
  bfd_size_type len;
2057
0
  char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2058
2059
0
  if (hh)
2060
0
    {
2061
0
      len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2062
0
      stub_name = bfd_malloc (len);
2063
0
      if (stub_name != NULL)
2064
0
  {
2065
0
    sprintf (stub_name, "%08x_%c_%s+%x",
2066
0
       input_section->id & 0xffffffff,
2067
0
       stubpos,
2068
0
       hh->root.root.root.string,
2069
0
       (int) rel->r_addend & 0xffffffff);
2070
0
  }
2071
0
    }
2072
0
  else
2073
0
    {
2074
0
      len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2075
0
      stub_name = bfd_malloc (len);
2076
0
      if (stub_name != NULL)
2077
0
  {
2078
0
    sprintf (stub_name, "%08x_%c_%x:%x+%x",
2079
0
       input_section->id & 0xffffffff,
2080
0
       stubpos,
2081
0
       sym_sec->id & 0xffffffff,
2082
0
       (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2083
0
       (int) rel->r_addend & 0xffffffff);
2084
0
  }
2085
0
    }
2086
0
  return stub_name;
2087
0
}
2088
2089
/* Look up an entry in the stub hash.  Stub entries are cached because
2090
   creating the stub name takes a bit of time.  */
2091
static struct elf32_nios2_stub_hash_entry *
2092
nios2_get_stub_entry (const asection *input_section,
2093
          const asection *sym_sec,
2094
          struct elf32_nios2_link_hash_entry *hh,
2095
          const Elf_Internal_Rela *rel,
2096
          struct elf32_nios2_link_hash_table *htab,
2097
          enum elf32_nios2_stub_type stub_type)
2098
0
{
2099
0
  struct elf32_nios2_stub_hash_entry *hsh;
2100
0
  const asection *id_sec;
2101
2102
  /* If this input section is part of a group of sections sharing one
2103
     stub section, then use the id of the first/last section in the group,
2104
     depending on the stub section placement relative to the group.
2105
     Stub names need to include a section id, as there may well be
2106
     more than one stub used to reach say, printf, and we need to
2107
     distinguish between them.  */
2108
0
  if (stub_type == nios2_stub_call26_before)
2109
0
    id_sec = htab->stub_group[input_section->id].first_sec;
2110
0
  else
2111
0
    id_sec = htab->stub_group[input_section->id].last_sec;
2112
2113
0
  if (hh != NULL && hh->hsh_cache != NULL
2114
0
      && hh->hsh_cache->hh == hh
2115
0
      && hh->hsh_cache->id_sec == id_sec
2116
0
      && hh->hsh_cache->stub_type == stub_type)
2117
0
    {
2118
0
      hsh = hh->hsh_cache;
2119
0
    }
2120
0
  else
2121
0
    {
2122
0
      char *stub_name;
2123
2124
0
      stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2125
0
      if (stub_name == NULL)
2126
0
  return NULL;
2127
2128
0
      hsh = nios2_stub_hash_lookup (&htab->bstab,
2129
0
            stub_name, false, false);
2130
2131
0
      if (hh != NULL)
2132
0
  hh->hsh_cache = hsh;
2133
2134
0
      free (stub_name);
2135
0
    }
2136
2137
0
  return hsh;
2138
0
}
2139
2140
/* Add a new stub entry to the stub hash.  Not all fields of the new
2141
   stub entry are initialised.  */
2142
static struct elf32_nios2_stub_hash_entry *
2143
nios2_add_stub (const char *stub_name,
2144
    asection *section,
2145
    struct elf32_nios2_link_hash_table *htab,
2146
    enum elf32_nios2_stub_type stub_type)
2147
0
{
2148
0
  asection *link_sec;
2149
0
  asection *stub_sec;
2150
0
  asection **secptr, **linkptr;
2151
0
  struct elf32_nios2_stub_hash_entry *hsh;
2152
0
  bool afterp;
2153
2154
0
  if (stub_type == nios2_stub_call26_before)
2155
0
    {
2156
0
      link_sec = htab->stub_group[section->id].first_sec;
2157
0
      secptr = &(htab->stub_group[section->id].first_stub_sec);
2158
0
      linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2159
0
      afterp = false;
2160
0
    }
2161
0
  else
2162
0
    {
2163
0
      link_sec = htab->stub_group[section->id].last_sec;
2164
0
      secptr = &(htab->stub_group[section->id].last_stub_sec);
2165
0
      linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2166
0
      afterp = true;
2167
0
    }
2168
0
  stub_sec = *secptr;
2169
0
  if (stub_sec == NULL)
2170
0
    {
2171
0
      stub_sec = *linkptr;
2172
0
      if (stub_sec == NULL)
2173
0
  {
2174
0
    size_t namelen;
2175
0
    bfd_size_type len;
2176
0
    char *s_name;
2177
2178
0
    namelen = strlen (link_sec->name);
2179
0
    len = namelen + sizeof (STUB_SUFFIX);
2180
0
    s_name = bfd_alloc (htab->stub_bfd, len);
2181
0
    if (s_name == NULL)
2182
0
      return NULL;
2183
2184
0
    memcpy (s_name, link_sec->name, namelen);
2185
0
    memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2186
2187
0
    stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2188
0
    if (stub_sec == NULL)
2189
0
      return NULL;
2190
0
    *linkptr = stub_sec;
2191
0
  }
2192
0
      *secptr = stub_sec;
2193
0
    }
2194
2195
  /* Enter this entry into the linker stub hash table.  */
2196
0
  hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2197
0
        true, false);
2198
0
  if (hsh == NULL)
2199
0
    {
2200
      /* xgettext:c-format */
2201
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
2202
0
        section->owner,
2203
0
        stub_name);
2204
0
      return NULL;
2205
0
    }
2206
2207
0
  hsh->stub_sec = stub_sec;
2208
0
  hsh->stub_offset = 0;
2209
0
  hsh->id_sec = link_sec;
2210
0
  return hsh;
2211
0
}
2212
2213
/* Set up various things so that we can make a list of input sections
2214
   for each output section included in the link.  Returns -1 on error,
2215
   0 when no stubs will be needed, and 1 on success.  */
2216
int
2217
nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2218
0
{
2219
0
  bfd *input_bfd;
2220
0
  unsigned int bfd_count;
2221
0
  unsigned int top_id, top_index;
2222
0
  asection *section;
2223
0
  asection **input_list, **list;
2224
0
  size_t amt;
2225
0
  struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2226
2227
  /* Count the number of input BFDs and find the top input section id.  */
2228
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2229
0
       input_bfd != NULL;
2230
0
       input_bfd = input_bfd->link.next)
2231
0
    {
2232
0
      bfd_count += 1;
2233
0
      for (section = input_bfd->sections;
2234
0
     section != NULL;
2235
0
     section = section->next)
2236
0
  {
2237
0
    if (top_id < section->id)
2238
0
      top_id = section->id;
2239
0
  }
2240
0
    }
2241
2242
0
  htab->bfd_count = bfd_count;
2243
2244
0
  amt = sizeof (struct map_stub) * (top_id + 1);
2245
0
  htab->stub_group = bfd_zmalloc (amt);
2246
0
  if (htab->stub_group == NULL)
2247
0
    return -1;
2248
2249
  /* We can't use output_bfd->section_count here to find the top output
2250
     section index as some sections may have been removed, and
2251
     strip_excluded_output_sections doesn't renumber the indices.  */
2252
0
  for (section = output_bfd->sections, top_index = 0;
2253
0
       section != NULL;
2254
0
       section = section->next)
2255
0
    {
2256
0
      if (top_index < section->index)
2257
0
  top_index = section->index;
2258
0
    }
2259
2260
0
  htab->top_index = top_index;
2261
0
  amt = sizeof (asection *) * (top_index + 1);
2262
0
  input_list = bfd_malloc (amt);
2263
0
  htab->input_list = input_list;
2264
0
  if (input_list == NULL)
2265
0
    return -1;
2266
2267
  /* For sections we aren't interested in, mark their entries with a
2268
     value we can check later.  */
2269
0
  list = input_list + top_index;
2270
0
  do
2271
0
    *list = bfd_abs_section_ptr;
2272
0
  while (list-- != input_list);
2273
2274
0
  for (section = output_bfd->sections;
2275
0
       section != NULL;
2276
0
       section = section->next)
2277
0
    {
2278
      /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2279
       * have PC relative relocs in them but no code flag set.  */
2280
0
      if (((section->flags & SEC_CODE) != 0) ||
2281
0
    strcmp(".ctors", section->name) ||
2282
0
    strcmp(".dtors", section->name))
2283
0
  input_list[section->index] = NULL;
2284
0
    }
2285
2286
0
  return 1;
2287
0
}
2288
2289
/* The linker repeatedly calls this function for each input section,
2290
   in the order that input sections are linked into output sections.
2291
   Build lists of input sections to determine groupings between which
2292
   we may insert linker stubs.  */
2293
void
2294
nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2295
0
{
2296
0
  struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2297
2298
0
  if (isec->output_section->index <= htab->top_index)
2299
0
    {
2300
0
      asection **list = htab->input_list + isec->output_section->index;
2301
0
      if (*list != bfd_abs_section_ptr)
2302
0
  {
2303
    /* Steal the last_sec pointer for our list.
2304
       This happens to make the list in reverse order,
2305
       which is what we want.  */
2306
0
    htab->stub_group[isec->id].last_sec = *list;
2307
0
    *list = isec;
2308
0
  }
2309
0
    }
2310
0
}
2311
2312
/* Segment mask for CALL26 relocation relaxation.  */
2313
0
#define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2314
2315
/* Fudge factor for approximate maximum size of all stubs that might
2316
   be inserted by the linker.  This does not actually limit the number
2317
   of stubs that might be inserted, and only affects strategy for grouping
2318
   and placement of stubs.  Perhaps this should be computed based on number
2319
   of relocations seen, or be specifiable on the command line.  */
2320
#define MAX_STUB_SECTION_SIZE 0xffff
2321
2322
/* See whether we can group stub sections together.  Grouping stub
2323
   sections may result in fewer stubs.  More importantly, we need to
2324
   put all .init* and .fini* stubs at the end of the .init or
2325
   .fini output sections respectively, because glibc splits the
2326
   _init and _fini functions into multiple parts.  Putting a stub in
2327
   the middle of a function is not a good idea.
2328
   Rather than computing groups of a maximum fixed size, for Nios II
2329
   CALL26 relaxation it makes more sense to compute the groups based on
2330
   sections that fit within a 256MB address segment.  Also do not allow
2331
   a group to span more than one output section, since different output
2332
   sections might correspond to different memory banks on a bare-metal
2333
   target, etc.  */
2334
static void
2335
group_sections (struct elf32_nios2_link_hash_table *htab)
2336
0
{
2337
0
  asection **list = htab->input_list + htab->top_index;
2338
0
  do
2339
0
    {
2340
      /* The list is in reverse order so we'll search backwards looking
2341
   for the first section that begins in the same memory segment,
2342
   marking sections along the way to point at the tail for this
2343
   group.  */
2344
0
      asection *tail = *list;
2345
0
      if (tail == bfd_abs_section_ptr)
2346
0
  continue;
2347
0
      while (tail != NULL)
2348
0
  {
2349
0
    bfd_vma start = tail->output_section->vma + tail->output_offset;
2350
0
    bfd_vma end = start + tail->size;
2351
0
    bfd_vma segment = CALL26_SEGMENT (end);
2352
0
    asection *prev;
2353
2354
0
    if (segment != CALL26_SEGMENT (start)
2355
0
        || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2356
      /* This section spans more than one memory segment, or is
2357
         close enough to the end of the segment that adding stub
2358
         sections before it might cause it to move so that it
2359
         spans memory segments, or that stubs added at the end of
2360
         this group might overflow into the next memory segment.
2361
         Put it in a group by itself to localize the effects.  */
2362
0
      {
2363
0
        prev = htab->stub_group[tail->id].last_sec;
2364
0
        htab->stub_group[tail->id].last_sec = tail;
2365
0
        htab->stub_group[tail->id].first_sec = tail;
2366
0
      }
2367
0
    else
2368
      /* Collect more sections for this group.  */
2369
0
      {
2370
0
        asection *curr, *first;
2371
0
        for (curr = tail; ; curr = prev)
2372
0
    {
2373
0
      prev = htab->stub_group[curr->id].last_sec;
2374
0
      if (!prev
2375
0
          || tail->output_section != prev->output_section
2376
0
          || (CALL26_SEGMENT (prev->output_section->vma
2377
0
            + prev->output_offset)
2378
0
        != segment))
2379
0
        break;
2380
0
    }
2381
0
        first = curr;
2382
0
        for (curr = tail; ; curr = prev)
2383
0
    {
2384
0
      prev = htab->stub_group[curr->id].last_sec;
2385
0
      htab->stub_group[curr->id].last_sec = tail;
2386
0
      htab->stub_group[curr->id].first_sec = first;
2387
0
      if (curr == first)
2388
0
        break;
2389
0
    }
2390
0
      }
2391
2392
    /* Reset tail for the next group.  */
2393
0
    tail = prev;
2394
0
  }
2395
0
    }
2396
0
  while (list-- != htab->input_list);
2397
0
  free (htab->input_list);
2398
0
}
2399
2400
/* Determine the type of stub needed, if any, for a call.  */
2401
static enum elf32_nios2_stub_type
2402
nios2_type_of_stub (asection *input_sec,
2403
        const Elf_Internal_Rela *rel,
2404
        struct elf32_nios2_link_hash_entry *hh,
2405
        struct elf32_nios2_link_hash_table *htab,
2406
        bfd_vma destination,
2407
        struct bfd_link_info *info ATTRIBUTE_UNUSED)
2408
0
{
2409
0
  bfd_vma location, segment, start, end;
2410
0
  asection *s0, *s1, *s;
2411
2412
0
  if (hh != NULL &&
2413
0
      !(hh->root.root.type == bfd_link_hash_defined
2414
0
  || hh->root.root.type == bfd_link_hash_defweak))
2415
0
    return nios2_stub_none;
2416
2417
  /* Determine where the call point is.  */
2418
0
  location = (input_sec->output_section->vma
2419
0
        + input_sec->output_offset + rel->r_offset);
2420
0
  segment = CALL26_SEGMENT (location);
2421
2422
  /* Nios II CALL and JMPI instructions can transfer control to addresses
2423
     within the same 256MB segment as the PC.  */
2424
0
  if (segment == CALL26_SEGMENT (destination))
2425
0
    return nios2_stub_none;
2426
2427
  /* Find the start and end addresses of the stub group.  Also account for
2428
     any already-created stub sections for this group.  Note that for stubs
2429
     in the end section, only the first instruction of the last stub
2430
     (12 bytes long) needs to be within range.  */
2431
0
  s0 = htab->stub_group[input_sec->id].first_sec;
2432
0
  s = htab->stub_group[s0->id].first_stub_sec;
2433
0
  if (s != NULL && s->size > 0)
2434
0
    start = s->output_section->vma + s->output_offset;
2435
0
  else
2436
0
    start = s0->output_section->vma + s0->output_offset;
2437
2438
0
  s1 = htab->stub_group[input_sec->id].last_sec;
2439
0
  s = htab->stub_group[s1->id].last_stub_sec;
2440
0
  if (s != NULL && s->size > 0)
2441
0
    end = s->output_section->vma + s->output_offset + s->size - 8;
2442
0
  else
2443
0
    end = s1->output_section->vma + s1->output_offset + s1->size;
2444
2445
0
  BFD_ASSERT (start < end);
2446
0
  BFD_ASSERT (start <= location);
2447
0
  BFD_ASSERT (location < end);
2448
2449
  /* Put stubs at the end of the group unless that is not a valid
2450
     location and the beginning of the group is.  It might be that
2451
     neither the beginning nor end works if we have an input section
2452
     so large that it spans multiple segment boundaries.  In that
2453
     case, punt; the end result will be a relocation overflow error no
2454
     matter what we do here.
2455
2456
     Note that adding stubs pushes up the addresses of all subsequent
2457
     sections, so that stubs allocated on one pass through the
2458
     relaxation loop may not be valid on the next pass.  (E.g., we may
2459
     allocate a stub at the beginning of the section on one pass and
2460
     find that the call site has been bumped into the next memory
2461
     segment on the next pass.)  The important thing to note is that
2462
     we never try to reclaim the space allocated to such unused stubs,
2463
     so code size and section addresses can only increase with each
2464
     iteration.  Accounting for the start and end addresses of the
2465
     already-created stub sections ensures that when the algorithm
2466
     converges, it converges accurately, with the entire appropriate
2467
     stub section accessible from the call site and not just the
2468
     address at the start or end of the stub group proper.  */
2469
2470
0
  if (segment == CALL26_SEGMENT (end))
2471
0
    return nios2_stub_call26_after;
2472
0
  else if (segment == CALL26_SEGMENT (start))
2473
0
    return nios2_stub_call26_before;
2474
0
  else
2475
    /* Perhaps this should be a dedicated error code.  */
2476
0
    return nios2_stub_none;
2477
0
}
2478
2479
static bool
2480
nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2481
0
{
2482
0
  struct elf32_nios2_stub_hash_entry *hsh
2483
0
    = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2484
0
  asection *stub_sec = hsh->stub_sec;
2485
0
  bfd_vma sym_value;
2486
0
  struct bfd_link_info *info;
2487
2488
0
  info = (struct bfd_link_info *) in_arg;
2489
2490
  /* Fail if the target section could not be assigned to an output
2491
     section.  The user should fix his linker script.  */
2492
0
  if (hsh->target_section->output_section == NULL
2493
0
      && info->non_contiguous_regions)
2494
0
    info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
2495
0
            "Retry without --enable-non-contiguous-regions.\n"),
2496
0
          hsh->target_section);
2497
2498
  /* Make a note of the offset within the stubs for this entry.  */
2499
0
  hsh->stub_offset = stub_sec->size;
2500
2501
0
  switch (hsh->stub_type)
2502
0
    {
2503
0
    case nios2_stub_call26_before:
2504
0
    case nios2_stub_call26_after:
2505
      /* A call26 stub looks like:
2506
     orhi at, %hiadj(dest)
2507
     addi at, at, %lo(dest)
2508
     jmp at
2509
   Note that call/jmpi instructions can't be used in PIC code
2510
   so there is no reason for the stub to be PIC, either.  */
2511
0
      sym_value = (hsh->target_value
2512
0
       + hsh->target_section->output_offset
2513
0
       + hsh->target_section->output_section->vma
2514
0
       + hsh->addend);
2515
2516
0
      nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2517
0
        hsh->stub_offset, 3);
2518
0
      nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2519
0
         hiadj (sym_value));
2520
0
      nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2521
0
         (sym_value & 0xffff));
2522
0
      stub_sec->size += 12;
2523
0
      break;
2524
0
    default:
2525
0
      BFD_FAIL ();
2526
0
      return false;
2527
0
    }
2528
2529
0
  return true;
2530
0
}
2531
2532
/* As above, but don't actually build the stub.  Just bump offset so
2533
   we know stub section sizes.  */
2534
static bool
2535
nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2536
0
{
2537
0
  struct elf32_nios2_stub_hash_entry *hsh
2538
0
    = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2539
2540
0
  switch (hsh->stub_type)
2541
0
    {
2542
0
    case nios2_stub_call26_before:
2543
0
    case nios2_stub_call26_after:
2544
0
      hsh->stub_sec->size += 12;
2545
0
      break;
2546
0
    default:
2547
0
      BFD_FAIL ();
2548
0
      return false;
2549
0
    }
2550
0
  return true;
2551
0
}
2552
2553
/* Read in all local syms for all input bfds.
2554
   Returns -1 on error, 0 otherwise.  */
2555
2556
static int
2557
get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2558
    struct bfd_link_info *info)
2559
0
{
2560
0
  unsigned int bfd_indx;
2561
0
  Elf_Internal_Sym *local_syms, **all_local_syms;
2562
0
  struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2563
2564
  /* We want to read in symbol extension records only once.  To do this
2565
     we need to read in the local symbols in parallel and save them for
2566
     later use; so hold pointers to the local symbols in an array.  */
2567
0
  size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2568
0
  all_local_syms = bfd_zmalloc (amt);
2569
0
  htab->all_local_syms = all_local_syms;
2570
0
  if (all_local_syms == NULL)
2571
0
    return -1;
2572
2573
  /* Walk over all the input BFDs, swapping in local symbols.  */
2574
0
  for (bfd_indx = 0;
2575
0
       input_bfd != NULL;
2576
0
       input_bfd = input_bfd->link.next, bfd_indx++)
2577
0
    {
2578
0
      Elf_Internal_Shdr *symtab_hdr;
2579
2580
      /* We'll need the symbol table in a second.  */
2581
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2582
0
      if (symtab_hdr->sh_info == 0)
2583
0
  continue;
2584
2585
      /* We need an array of the local symbols attached to the input bfd.  */
2586
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2587
0
      if (local_syms == NULL)
2588
0
  {
2589
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2590
0
               symtab_hdr->sh_info, 0,
2591
0
               NULL, NULL, NULL);
2592
    /* Cache them for elf_link_input_bfd.  */
2593
0
    symtab_hdr->contents = (unsigned char *) local_syms;
2594
0
  }
2595
0
      if (local_syms == NULL)
2596
0
  return -1;
2597
2598
0
      all_local_syms[bfd_indx] = local_syms;
2599
0
    }
2600
2601
0
  return 0;
2602
0
}
2603
2604
/* Determine and set the size of the stub section for a final link.  */
2605
bool
2606
nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2607
      struct bfd_link_info *info,
2608
      asection *(*add_stub_section) (const char *,
2609
                   asection *, bool),
2610
      void (*layout_sections_again) (void))
2611
0
{
2612
0
  bool stub_changed = false;
2613
0
  struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2614
2615
  /* Stash our params away.  */
2616
0
  htab->stub_bfd = stub_bfd;
2617
0
  htab->add_stub_section = add_stub_section;
2618
0
  htab->layout_sections_again = layout_sections_again;
2619
2620
  /* FIXME: We only compute the section groups once.  This could cause
2621
     problems if adding a large stub section causes following sections,
2622
     or parts of them, to move into another segment.  However, this seems
2623
     to be consistent with the way other back ends handle this....  */
2624
0
  group_sections (htab);
2625
2626
0
  if (get_local_syms (output_bfd, info->input_bfds, info))
2627
0
    {
2628
0
      if (htab->all_local_syms)
2629
0
  goto error_ret_free_local;
2630
0
      return false;
2631
0
    }
2632
2633
0
  while (1)
2634
0
    {
2635
0
      bfd *input_bfd;
2636
0
      unsigned int bfd_indx;
2637
0
      asection *stub_sec;
2638
2639
0
      for (input_bfd = info->input_bfds, bfd_indx = 0;
2640
0
     input_bfd != NULL;
2641
0
     input_bfd = input_bfd->link.next, bfd_indx++)
2642
0
  {
2643
0
    Elf_Internal_Shdr *symtab_hdr;
2644
0
    asection *section;
2645
0
    Elf_Internal_Sym *local_syms;
2646
2647
    /* We'll need the symbol table in a second.  */
2648
0
    symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2649
0
    if (symtab_hdr->sh_info == 0)
2650
0
      continue;
2651
2652
0
    local_syms = htab->all_local_syms[bfd_indx];
2653
2654
    /* Walk over each section attached to the input bfd.  */
2655
0
    for (section = input_bfd->sections;
2656
0
         section != NULL;
2657
0
         section = section->next)
2658
0
      {
2659
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2660
2661
        /* If there aren't any relocs, then there's nothing more
2662
     to do.  */
2663
0
        if ((section->flags & SEC_RELOC) == 0
2664
0
      || section->reloc_count == 0)
2665
0
    continue;
2666
2667
        /* If this section is a link-once section that will be
2668
     discarded, then don't create any stubs.  */
2669
0
        if (section->output_section == NULL
2670
0
      || section->output_section->owner != output_bfd)
2671
0
    continue;
2672
2673
        /* Get the relocs.  */
2674
0
        internal_relocs
2675
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2676
0
               info->keep_memory);
2677
0
        if (internal_relocs == NULL)
2678
0
    goto error_ret_free_local;
2679
2680
        /* Now examine each relocation.  */
2681
0
        irela = internal_relocs;
2682
0
        irelaend = irela + section->reloc_count;
2683
0
        for (; irela < irelaend; irela++)
2684
0
    {
2685
0
      unsigned int r_type, r_indx;
2686
0
      enum elf32_nios2_stub_type stub_type;
2687
0
      struct elf32_nios2_stub_hash_entry *hsh;
2688
0
      asection *sym_sec;
2689
0
      bfd_vma sym_value;
2690
0
      bfd_vma destination;
2691
0
      struct elf32_nios2_link_hash_entry *hh;
2692
0
      char *stub_name;
2693
0
      const asection *id_sec;
2694
2695
0
      r_type = ELF32_R_TYPE (irela->r_info);
2696
0
      r_indx = ELF32_R_SYM (irela->r_info);
2697
2698
0
      if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2699
0
        {
2700
0
          bfd_set_error (bfd_error_bad_value);
2701
0
        error_ret_free_internal:
2702
0
          if (elf_section_data (section)->relocs == NULL)
2703
0
      free (internal_relocs);
2704
0
          goto error_ret_free_local;
2705
0
        }
2706
2707
      /* Only look for stubs on CALL and JMPI instructions.  */
2708
0
      if (r_type != (unsigned int) R_NIOS2_CALL26)
2709
0
        continue;
2710
2711
      /* Now determine the call target, its name, value,
2712
         section.  */
2713
0
      sym_sec = NULL;
2714
0
      sym_value = 0;
2715
0
      destination = 0;
2716
0
      hh = NULL;
2717
0
      if (r_indx < symtab_hdr->sh_info)
2718
0
        {
2719
          /* It's a local symbol.  */
2720
0
          Elf_Internal_Sym *sym;
2721
0
          Elf_Internal_Shdr *hdr;
2722
0
          unsigned int shndx;
2723
2724
0
          sym = local_syms + r_indx;
2725
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2726
0
      sym_value = sym->st_value;
2727
0
          shndx = sym->st_shndx;
2728
0
          if (shndx < elf_numsections (input_bfd))
2729
0
      {
2730
0
        hdr = elf_elfsections (input_bfd)[shndx];
2731
0
        sym_sec = hdr->bfd_section;
2732
0
        destination = (sym_value + irela->r_addend
2733
0
           + sym_sec->output_offset
2734
0
           + sym_sec->output_section->vma);
2735
0
      }
2736
0
        }
2737
0
      else
2738
0
        {
2739
          /* It's an external symbol.  */
2740
0
          int e_indx;
2741
2742
0
          e_indx = r_indx - symtab_hdr->sh_info;
2743
0
          hh = ((struct elf32_nios2_link_hash_entry *)
2744
0
          elf_sym_hashes (input_bfd)[e_indx]);
2745
2746
0
          while (hh->root.root.type == bfd_link_hash_indirect
2747
0
           || hh->root.root.type == bfd_link_hash_warning)
2748
0
      hh = ((struct elf32_nios2_link_hash_entry *)
2749
0
            hh->root.root.u.i.link);
2750
2751
0
          if (hh->root.root.type == bfd_link_hash_defined
2752
0
        || hh->root.root.type == bfd_link_hash_defweak)
2753
0
      {
2754
0
        sym_sec = hh->root.root.u.def.section;
2755
0
        sym_value = hh->root.root.u.def.value;
2756
2757
0
        if (sym_sec->output_section != NULL)
2758
0
          destination = (sym_value + irela->r_addend
2759
0
             + sym_sec->output_offset
2760
0
             + sym_sec->output_section->vma);
2761
0
        else
2762
0
          continue;
2763
0
      }
2764
0
          else if (hh->root.root.type == bfd_link_hash_undefweak)
2765
0
      {
2766
0
        if (! bfd_link_pic (info))
2767
0
          continue;
2768
0
      }
2769
0
          else if (hh->root.root.type == bfd_link_hash_undefined)
2770
0
      {
2771
0
        if (! (info->unresolved_syms_in_objects == RM_IGNORE
2772
0
         && (ELF_ST_VISIBILITY (hh->root.other)
2773
0
             == STV_DEFAULT)))
2774
0
          continue;
2775
0
      }
2776
0
          else
2777
0
      {
2778
0
        bfd_set_error (bfd_error_bad_value);
2779
0
        goto error_ret_free_internal;
2780
0
      }
2781
0
        }
2782
2783
      /* Determine what (if any) linker stub is needed.  */
2784
0
      stub_type = nios2_type_of_stub (section, irela, hh, htab,
2785
0
              destination, info);
2786
0
      if (stub_type == nios2_stub_none)
2787
0
        continue;
2788
2789
      /* Support for grouping stub sections.  */
2790
0
      if (stub_type == nios2_stub_call26_before)
2791
0
        id_sec = htab->stub_group[section->id].first_sec;
2792
0
      else
2793
0
        id_sec = htab->stub_group[section->id].last_sec;
2794
2795
      /* Get the name of this stub.  */
2796
0
      stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2797
0
                 stub_type);
2798
0
      if (!stub_name)
2799
0
        goto error_ret_free_internal;
2800
2801
0
      hsh = nios2_stub_hash_lookup (&htab->bstab,
2802
0
            stub_name,
2803
0
            false, false);
2804
0
      if (hsh != NULL)
2805
0
        {
2806
          /* The proper stub has already been created.  */
2807
0
          free (stub_name);
2808
0
          continue;
2809
0
        }
2810
2811
0
      hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2812
0
      if (hsh == NULL)
2813
0
        {
2814
0
          free (stub_name);
2815
0
          goto error_ret_free_internal;
2816
0
        }
2817
0
      hsh->target_value = sym_value;
2818
0
      hsh->target_section = sym_sec;
2819
0
      hsh->stub_type = stub_type;
2820
0
      hsh->hh = hh;
2821
0
      hsh->addend = irela->r_addend;
2822
0
      stub_changed = true;
2823
0
    }
2824
2825
        /* We're done with the internal relocs, free them.  */
2826
0
        if (elf_section_data (section)->relocs == NULL)
2827
0
    free (internal_relocs);
2828
0
      }
2829
0
  }
2830
2831
0
      if (!stub_changed)
2832
0
  break;
2833
2834
      /* OK, we've added some stubs.  Find out the new size of the
2835
   stub sections.  */
2836
0
      for (stub_sec = htab->stub_bfd->sections;
2837
0
     stub_sec != NULL;
2838
0
     stub_sec = stub_sec->next)
2839
0
  stub_sec->size = 0;
2840
2841
0
      bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2842
2843
      /* Ask the linker to do its stuff.  */
2844
0
      (*htab->layout_sections_again) ();
2845
0
      stub_changed = false;
2846
0
    }
2847
2848
0
  free (htab->all_local_syms);
2849
0
  return true;
2850
2851
0
 error_ret_free_local:
2852
0
  free (htab->all_local_syms);
2853
0
  return false;
2854
0
}
2855
2856
/* Build all the stubs associated with the current output file.  The
2857
   stubs are kept in a hash table attached to the main linker hash
2858
   table.  This function is called via nios2elf_finish in the linker.  */
2859
bool
2860
nios2_elf32_build_stubs (struct bfd_link_info *info)
2861
0
{
2862
0
  asection *stub_sec;
2863
0
  struct bfd_hash_table *table;
2864
0
  struct elf32_nios2_link_hash_table *htab;
2865
2866
0
  htab = elf32_nios2_hash_table (info);
2867
2868
0
  for (stub_sec = htab->stub_bfd->sections;
2869
0
       stub_sec != NULL;
2870
0
       stub_sec = stub_sec->next)
2871
    /* The stub_bfd may contain non-stub sections if it is also the
2872
       dynobj.  Any such non-stub sections are created with the
2873
       SEC_LINKER_CREATED flag set, while stub sections do not
2874
       have that flag.  Ignore any non-stub sections here.  */
2875
0
    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2876
0
      {
2877
0
  bfd_size_type size;
2878
2879
  /* Allocate memory to hold the linker stubs.  */
2880
0
  size = stub_sec->size;
2881
0
  stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2882
0
  if (stub_sec->contents == NULL && size != 0)
2883
0
    return false;
2884
0
  stub_sec->size = 0;
2885
0
      }
2886
2887
  /* Build the stubs as directed by the stub hash table.  */
2888
0
  table = &htab->bstab;
2889
0
  bfd_hash_traverse (table, nios2_build_one_stub, info);
2890
2891
0
  return true;
2892
0
}
2893
2894
2895
#define is_nios2_elf(bfd) \
2896
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2897
0
   && elf_object_id (bfd) == NIOS2_ELF_DATA)
2898
2899
/* Merge backend specific data from an object file to the output
2900
   object file when linking.  */
2901
2902
static bool
2903
nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2904
0
{
2905
0
  bfd *obfd = info->output_bfd;
2906
0
  flagword old_flags;
2907
0
  flagword new_flags;
2908
2909
0
  if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2910
0
    return true;
2911
2912
  /* Check if we have the same endianness.  */
2913
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
2914
0
    return false;
2915
2916
0
  new_flags = elf_elfheader (ibfd)->e_flags;
2917
0
  old_flags = elf_elfheader (obfd)->e_flags;
2918
0
  if (!elf_flags_init (obfd))
2919
0
    {
2920
      /* First call, no flags set.  */
2921
0
      elf_flags_init (obfd) = true;
2922
0
      elf_elfheader (obfd)->e_flags = new_flags;
2923
2924
0
      switch (new_flags)
2925
0
  {
2926
0
  default:
2927
0
  case EF_NIOS2_ARCH_R1:
2928
0
    bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2929
0
    break;
2930
0
  case EF_NIOS2_ARCH_R2:
2931
0
    if (bfd_big_endian (ibfd))
2932
0
      {
2933
0
        _bfd_error_handler
2934
0
    (_("error: %pB: big-endian R2 is not supported"), ibfd);
2935
0
        bfd_set_error (bfd_error_bad_value);
2936
0
        return false;
2937
0
      }
2938
0
    bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2939
0
    break;
2940
0
  }
2941
0
    }
2942
2943
  /* Incompatible flags.  */
2944
0
  else if (new_flags != old_flags)
2945
0
    {
2946
      /* So far, the only incompatible flags denote incompatible
2947
   architectures.  */
2948
0
      _bfd_error_handler
2949
  /* xgettext:c-format */
2950
0
  (_("error: %pB: conflicting CPU architectures %d/%d"),
2951
0
   ibfd, new_flags, old_flags);
2952
0
      bfd_set_error (bfd_error_bad_value);
2953
0
      return false;
2954
0
    }
2955
2956
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
2957
0
  _bfd_elf_merge_object_attributes (ibfd, info);
2958
2959
0
  return true;
2960
0
}
2961
2962
/* Implement bfd_elf32_bfd_reloc_type_lookup:
2963
   Given a BFD reloc type, return a howto structure.  */
2964
2965
static reloc_howto_type *
2966
nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
2967
           bfd_reloc_code_real_type code)
2968
0
{
2969
0
  int i;
2970
2971
0
  for (i = 0; i < (int) ARRAY_SIZE (nios2_reloc_map); ++i)
2972
0
    if (nios2_reloc_map[i].bfd_val == code)
2973
0
      return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
2974
0
  return NULL;
2975
0
}
2976
2977
/* Implement bfd_elf32_bfd_reloc_name_lookup:
2978
   Given a reloc name, return a howto structure.  */
2979
2980
static reloc_howto_type *
2981
nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
2982
           const char *r_name)
2983
0
{
2984
0
  int i;
2985
0
  reloc_howto_type *howto_tbl;
2986
0
  int howto_tbl_size;
2987
2988
0
  if (BFD_IS_R2 (abfd))
2989
0
    {
2990
0
      howto_tbl = elf_nios2_r2_howto_table_rel;
2991
0
      howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
2992
0
    }
2993
0
  else
2994
0
    {
2995
0
      howto_tbl = elf_nios2_r1_howto_table_rel;
2996
0
      howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
2997
0
    }
2998
2999
0
  for (i = 0; i < howto_tbl_size; i++)
3000
0
    if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
3001
0
      return howto_tbl + i;
3002
3003
0
  return NULL;
3004
0
}
3005
3006
/* Implement elf_info_to_howto:
3007
   Given a ELF32 relocation, fill in a arelent structure.  */
3008
3009
static bool
3010
nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3011
         Elf_Internal_Rela *dst)
3012
0
{
3013
0
  unsigned int r_type;
3014
3015
0
  r_type = ELF32_R_TYPE (dst->r_info);
3016
0
  if ((cache_ptr->howto = lookup_howto (r_type, abfd)) == NULL)
3017
0
    {
3018
      /* xgettext:c-format */
3019
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3020
0
        abfd, r_type);
3021
0
      bfd_set_error (bfd_error_bad_value);
3022
0
      return false;
3023
0
    }
3024
0
  return true;
3025
0
}
3026
3027
/* Return the base VMA address which should be subtracted from real addresses
3028
   when resolving @dtpoff relocation.
3029
   This is PT_TLS segment p_vaddr.  */
3030
static bfd_vma
3031
dtpoff_base (struct bfd_link_info *info)
3032
0
{
3033
  /* If tls_sec is NULL, we should have signalled an error already.  */
3034
0
  if (elf_hash_table (info)->tls_sec == NULL)
3035
0
    return 0;
3036
0
  return elf_hash_table (info)->tls_sec->vma;
3037
0
}
3038
3039
/* Return the relocation value for @tpoff relocation
3040
   if STT_TLS virtual address is ADDRESS.  */
3041
static bfd_vma
3042
tpoff (struct bfd_link_info *info, bfd_vma address)
3043
0
{
3044
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
3045
3046
  /* If tls_sec is NULL, we should have signalled an error already.  */
3047
0
  if (htab->tls_sec == NULL)
3048
0
    return 0;
3049
0
  return address - htab->tls_sec->vma;
3050
0
}
3051
3052
/* Set the GP value for OUTPUT_BFD.  Returns FALSE if this is a
3053
   dangerous relocation.  */
3054
static bool
3055
nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3056
0
{
3057
3058
0
  bool gp_found;
3059
0
  struct bfd_hash_entry *h;
3060
0
  struct bfd_link_hash_entry *lh;
3061
3062
  /* If we've already figured out what GP will be, just return it. */
3063
0
  *pgp = _bfd_get_gp_value (output_bfd);
3064
0
  if (*pgp)
3065
0
    return true;
3066
3067
0
  h = bfd_hash_lookup (&info->hash->table, "_gp", false, false);
3068
0
  lh = (struct bfd_link_hash_entry *) h;
3069
0
 lookup:
3070
0
  if (lh)
3071
0
    {
3072
0
      switch (lh->type)
3073
0
  {
3074
0
  case bfd_link_hash_undefined:
3075
0
  case bfd_link_hash_undefweak:
3076
0
  case bfd_link_hash_common:
3077
0
    gp_found = false;
3078
0
    break;
3079
0
  case bfd_link_hash_defined:
3080
0
  case bfd_link_hash_defweak:
3081
0
    gp_found = true;
3082
0
    {
3083
0
      asection *sym_sec = lh->u.def.section;
3084
0
      bfd_vma sym_value = lh->u.def.value;
3085
3086
0
      if (sym_sec->output_section)
3087
0
        sym_value = (sym_value + sym_sec->output_offset
3088
0
         + sym_sec->output_section->vma);
3089
0
      *pgp = sym_value;
3090
0
    }
3091
0
    break;
3092
0
  case bfd_link_hash_indirect:
3093
0
  case bfd_link_hash_warning:
3094
0
    lh = lh->u.i.link;
3095
    /* @@FIXME  ignoring warning for now */
3096
0
    goto lookup;
3097
0
  case bfd_link_hash_new:
3098
0
  default:
3099
0
    abort ();
3100
0
  }
3101
0
    }
3102
0
  else
3103
0
    gp_found = false;
3104
3105
0
  if (!gp_found)
3106
0
    {
3107
      /* Only get the error once. */
3108
0
      *pgp = 4;
3109
0
      _bfd_set_gp_value (output_bfd, *pgp);
3110
0
      return false;
3111
0
    }
3112
3113
0
  _bfd_set_gp_value (output_bfd, *pgp);
3114
3115
0
  return true;
3116
0
}
3117
3118
/* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3119
   if it's not available as we don't have a link_info pointer available here
3120
   to look it up in the output symbol table.  We don't need to adjust the
3121
   symbol value for an external symbol if we are producing relocatable
3122
   output.  */
3123
static bfd_reloc_status_type
3124
nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bool relocatable,
3125
        char **error_message, bfd_vma *pgp)
3126
0
{
3127
0
  if (bfd_is_und_section (symbol->section) && !relocatable)
3128
0
    {
3129
0
      *pgp = 0;
3130
0
      return bfd_reloc_undefined;
3131
0
    }
3132
3133
0
  *pgp = _bfd_get_gp_value (output_bfd);
3134
0
  if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3135
0
    {
3136
0
      if (relocatable)
3137
0
  {
3138
    /* Make up a value.  */
3139
0
    *pgp = symbol->section->output_section->vma + 0x4000;
3140
0
    _bfd_set_gp_value (output_bfd, *pgp);
3141
0
  }
3142
0
      else
3143
0
  {
3144
0
    *error_message
3145
0
      = (char *) _("global pointer relative relocation when _gp not defined");
3146
0
    return bfd_reloc_dangerous;
3147
0
  }
3148
0
    }
3149
3150
0
  return bfd_reloc_ok;
3151
0
}
3152
3153
/* Do the relocations that require special handling.  */
3154
static bfd_reloc_status_type
3155
nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
3156
            asection *input_section,
3157
            bfd_byte *data, bfd_vma offset,
3158
            bfd_vma symbol_value, bfd_vma addend)
3159
0
{
3160
0
  symbol_value = symbol_value + addend;
3161
0
  addend = 0;
3162
0
  symbol_value = (symbol_value >> 16) & 0xffff;
3163
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3164
0
           data, offset, symbol_value, addend);
3165
0
}
3166
3167
static bfd_reloc_status_type
3168
nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3169
            asection *input_section,
3170
            bfd_byte *data, bfd_vma offset,
3171
            bfd_vma symbol_value, bfd_vma addend)
3172
0
{
3173
0
  symbol_value = symbol_value + addend;
3174
0
  addend = 0;
3175
0
  symbol_value = symbol_value & 0xffff;
3176
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3177
0
           data, offset, symbol_value, addend);
3178
0
}
3179
3180
static bfd_reloc_status_type
3181
nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3182
         asection *input_section,
3183
         bfd_byte *data, bfd_vma offset,
3184
         bfd_vma symbol_value, bfd_vma addend)
3185
0
{
3186
0
  symbol_value = symbol_value + addend;
3187
0
  addend = 0;
3188
0
  symbol_value = hiadj(symbol_value);
3189
0
  return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3190
0
           symbol_value, addend);
3191
0
}
3192
3193
static bfd_reloc_status_type
3194
nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3195
            asection *input_section,
3196
            bfd_byte *data, bfd_vma offset,
3197
            bfd_vma symbol_value, bfd_vma addend)
3198
0
{
3199
0
  symbol_value = symbol_value + addend;
3200
0
  addend = 0;
3201
0
  symbol_value = symbol_value & 0xffff;
3202
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3203
0
           data, offset, symbol_value, addend);
3204
0
}
3205
3206
static bfd_reloc_status_type
3207
nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3208
               asection *input_section,
3209
               bfd_byte *data, bfd_vma offset,
3210
               bfd_vma symbol_value, bfd_vma addend)
3211
0
{
3212
0
  symbol_value = symbol_value + addend;
3213
0
  symbol_value -= (input_section->output_section->vma
3214
0
       + input_section->output_offset);
3215
0
  symbol_value -= offset;
3216
0
  addend = 0;
3217
0
  symbol_value = hiadj(symbol_value);
3218
0
  return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3219
0
           symbol_value, addend);
3220
0
}
3221
3222
static bfd_reloc_status_type
3223
nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
3224
         asection *input_section,
3225
         bfd_byte *data, bfd_vma offset,
3226
         bfd_vma symbol_value, bfd_vma addend)
3227
0
{
3228
  /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3229
     so we need to subtract 4 before doing a final_link_relocate. */
3230
0
  symbol_value = symbol_value + addend - 4;
3231
0
  addend = 0;
3232
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3233
0
           data, offset, symbol_value, addend);
3234
0
}
3235
3236
static bfd_reloc_status_type
3237
nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
3238
        asection *input_section,
3239
        bfd_byte *data, bfd_vma offset,
3240
        bfd_vma symbol_value, bfd_vma addend)
3241
0
{
3242
  /* Check that the relocation is in the same page as the current address.  */
3243
0
  if (CALL26_SEGMENT (symbol_value + addend)
3244
0
      != CALL26_SEGMENT (input_section->output_section->vma
3245
0
       + input_section->output_offset
3246
0
       + offset))
3247
0
    return bfd_reloc_overflow;
3248
3249
  /* Check that the target address is correctly aligned on a 4-byte
3250
     boundary.  */
3251
0
  if ((symbol_value + addend) & 0x3)
3252
0
    return bfd_reloc_overflow;
3253
3254
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3255
0
           data, offset, symbol_value, addend);
3256
0
}
3257
3258
static bfd_reloc_status_type
3259
nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
3260
             asection *input_section,
3261
             bfd_byte *data, bfd_vma offset,
3262
             bfd_vma symbol_value, bfd_vma addend)
3263
0
{
3264
  /* Because we need the output_bfd, the special handling is done
3265
     in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate.  */
3266
0
  return _bfd_final_link_relocate (howto, abfd, input_section,
3267
0
           data, offset, symbol_value, addend);
3268
0
}
3269
3270
static bfd_reloc_status_type
3271
nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
3272
            asection *input_section,
3273
            bfd_byte *data, bfd_vma offset,
3274
            bfd_vma symbol_value, bfd_vma addend)
3275
0
{
3276
0
  bfd_vma symbol_lo16, symbol_hi16;
3277
0
  bfd_reloc_status_type r;
3278
0
  symbol_value = symbol_value + addend;
3279
0
  addend = 0;
3280
0
  symbol_hi16 = (symbol_value >> 16) & 0xffff;
3281
0
  symbol_lo16 = symbol_value & 0xffff;
3282
3283
0
  r = _bfd_final_link_relocate (howto, abfd, input_section,
3284
0
        data, offset, symbol_hi16, addend);
3285
3286
0
  if (r == bfd_reloc_ok)
3287
0
    return _bfd_final_link_relocate (howto, abfd, input_section,
3288
0
             data, offset + 4, symbol_lo16, addend);
3289
3290
0
  return r;
3291
0
}
3292
3293
static bfd_reloc_status_type
3294
nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
3295
            asection *input_section,
3296
            bfd_byte *data, bfd_vma offset,
3297
            bfd_vma symbol_value, bfd_vma addend)
3298
0
{
3299
0
  bfd_vma symbol_lo16, symbol_hi16;
3300
0
  bfd_reloc_status_type r;
3301
0
  symbol_value = symbol_value + addend;
3302
0
  addend = 0;
3303
0
  symbol_hi16 = (symbol_value >> 16) & 0xffff;
3304
0
  symbol_lo16 = symbol_value & 0xffff;
3305
3306
0
  r = _bfd_final_link_relocate (howto, abfd, input_section,
3307
0
        data, offset, symbol_hi16, addend);
3308
3309
0
  if (r == bfd_reloc_ok)
3310
0
    return _bfd_final_link_relocate (howto, abfd, input_section,
3311
0
             data, offset + 4, symbol_lo16, addend);
3312
3313
0
  return r;
3314
0
}
3315
3316
static bfd_reloc_status_type
3317
nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
3318
             asection *input_section,
3319
             bfd_byte *data, bfd_vma offset,
3320
             bfd_vma symbol_value, bfd_vma addend)
3321
0
{
3322
0
  bfd_vma symbol_lo16, symbol_hi16;
3323
0
  bfd_reloc_status_type r;
3324
0
  symbol_value = symbol_value + addend;
3325
0
  addend = 0;
3326
0
  symbol_hi16 = (symbol_value >> 16) & 0xffff;
3327
0
  symbol_lo16 = symbol_value & 0xffff;
3328
3329
0
  r = _bfd_final_link_relocate (howto, abfd, input_section,
3330
0
        data, offset, symbol_hi16, addend);
3331
3332
0
  if (r == bfd_reloc_ok)
3333
0
    return _bfd_final_link_relocate (howto, abfd, input_section,
3334
0
             data, offset + 4, symbol_lo16, addend);
3335
3336
0
  return r;
3337
0
}
3338
3339
/* HOWTO handlers for relocations that require special handling.  */
3340
3341
/* This is for relocations used only when relaxing to ensure
3342
   changes in size of section don't screw up .align.  */
3343
static bfd_reloc_status_type
3344
nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3345
        asymbol *symbol ATTRIBUTE_UNUSED,
3346
        void *data ATTRIBUTE_UNUSED, asection *input_section,
3347
        bfd *output_bfd,
3348
        char **error_message ATTRIBUTE_UNUSED)
3349
0
{
3350
0
  if (output_bfd != NULL)
3351
0
    reloc_entry->address += input_section->output_offset;
3352
0
  return bfd_reloc_ok;
3353
0
}
3354
3355
static bfd_reloc_status_type
3356
nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3357
         void *data, asection *input_section,
3358
         bfd *output_bfd,
3359
         char **error_message ATTRIBUTE_UNUSED)
3360
0
{
3361
  /* This part is from bfd_elf_generic_reloc.  */
3362
0
  if (output_bfd != NULL
3363
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3364
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3365
0
    {
3366
0
      reloc_entry->address += input_section->output_offset;
3367
0
      return bfd_reloc_ok;
3368
0
    }
3369
3370
0
  if (output_bfd != NULL)
3371
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3372
0
    return bfd_reloc_continue;
3373
3374
0
  return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3375
0
               input_section,
3376
0
               data, reloc_entry->address,
3377
0
               (symbol->value
3378
0
          + symbol->section->output_section->vma
3379
0
          + symbol->section->output_offset),
3380
0
               reloc_entry->addend);
3381
0
}
3382
3383
static bfd_reloc_status_type
3384
nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3385
         void *data, asection *input_section,
3386
         bfd *output_bfd,
3387
         char **error_message ATTRIBUTE_UNUSED)
3388
0
{
3389
  /* This part is from bfd_elf_generic_reloc.  */
3390
0
  if (output_bfd != NULL
3391
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3392
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3393
0
    {
3394
0
      reloc_entry->address += input_section->output_offset;
3395
0
      return bfd_reloc_ok;
3396
0
    }
3397
3398
0
  if (output_bfd != NULL)
3399
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3400
0
    return bfd_reloc_continue;
3401
3402
0
  return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3403
0
               input_section,
3404
0
               data, reloc_entry->address,
3405
0
               (symbol->value
3406
0
          + symbol->section->output_section->vma
3407
0
          + symbol->section->output_offset),
3408
0
               reloc_entry->addend);
3409
0
}
3410
3411
static bfd_reloc_status_type
3412
nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3413
            void *data, asection *input_section,
3414
            bfd *output_bfd,
3415
            char **error_message ATTRIBUTE_UNUSED)
3416
0
{
3417
  /* This part is from bfd_elf_generic_reloc.  */
3418
0
  if (output_bfd != NULL
3419
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3420
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3421
0
    {
3422
0
      reloc_entry->address += input_section->output_offset;
3423
0
      return bfd_reloc_ok;
3424
0
    }
3425
3426
0
  if (output_bfd != NULL)
3427
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3428
0
    return bfd_reloc_continue;
3429
3430
0
  return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3431
0
            input_section,
3432
0
            data, reloc_entry->address,
3433
0
            (symbol->value
3434
0
             + symbol->section->output_section->vma
3435
0
             + symbol->section->output_offset),
3436
0
            reloc_entry->addend);
3437
0
}
3438
3439
static bfd_reloc_status_type
3440
nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3441
         asymbol *symbol, void *data,
3442
         asection *input_section, bfd *output_bfd,
3443
         char **error_message ATTRIBUTE_UNUSED)
3444
0
{
3445
  /* This part is from bfd_elf_generic_reloc.  */
3446
0
  if (output_bfd != NULL
3447
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3448
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3449
0
    {
3450
0
      reloc_entry->address += input_section->output_offset;
3451
0
      return bfd_reloc_ok;
3452
0
    }
3453
3454
0
  if (output_bfd != NULL)
3455
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3456
0
    return bfd_reloc_continue;
3457
3458
0
  return nios2_elf32_do_pcrel_lo16_relocate (
3459
0
    abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3460
0
    (symbol->value + symbol->section->output_section->vma
3461
0
     + symbol->section->output_offset),
3462
0
    reloc_entry->addend);
3463
0
}
3464
3465
static bfd_reloc_status_type
3466
nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3467
            asymbol *symbol, void *data,
3468
            asection *input_section, bfd *output_bfd,
3469
            char **error_message ATTRIBUTE_UNUSED)
3470
0
{
3471
  /* This part is from bfd_elf_generic_reloc.  */
3472
0
  if (output_bfd != NULL
3473
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3474
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3475
0
    {
3476
0
      reloc_entry->address += input_section->output_offset;
3477
0
      return bfd_reloc_ok;
3478
0
    }
3479
3480
0
  if (output_bfd != NULL)
3481
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3482
0
    return bfd_reloc_continue;
3483
3484
0
  return nios2_elf32_do_pcrel_hiadj16_relocate (
3485
0
    abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3486
0
    (symbol->value + symbol->section->output_section->vma
3487
0
     + symbol->section->output_offset),
3488
0
    reloc_entry->addend);
3489
0
}
3490
3491
static bfd_reloc_status_type
3492
nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3493
            void *data, asection *input_section,
3494
            bfd *output_bfd,
3495
            char **error_message ATTRIBUTE_UNUSED)
3496
0
{
3497
  /* This part is from bfd_elf_generic_reloc.  */
3498
0
  if (output_bfd != NULL
3499
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3500
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3501
0
    {
3502
0
      reloc_entry->address += input_section->output_offset;
3503
0
      return bfd_reloc_ok;
3504
0
    }
3505
3506
0
  if (output_bfd != NULL)
3507
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3508
0
    return bfd_reloc_continue;
3509
3510
0
  return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3511
0
            input_section,
3512
0
            data, reloc_entry->address,
3513
0
            (symbol->value
3514
0
             + symbol->section->output_section->vma
3515
0
             + symbol->section->output_offset),
3516
0
            reloc_entry->addend);
3517
0
}
3518
3519
static bfd_reloc_status_type
3520
nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3521
           void *data, asection *input_section,
3522
           bfd *output_bfd,
3523
           char **error_message ATTRIBUTE_UNUSED)
3524
0
{
3525
  /* This part is from bfd_elf_generic_reloc.  */
3526
0
  if (output_bfd != NULL
3527
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3528
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3529
0
    {
3530
0
      reloc_entry->address += input_section->output_offset;
3531
0
      return bfd_reloc_ok;
3532
0
    }
3533
3534
0
  if (output_bfd != NULL)
3535
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3536
0
    return bfd_reloc_continue;
3537
3538
0
  return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3539
0
           input_section,
3540
0
           data, reloc_entry->address,
3541
0
           (symbol->value
3542
0
            + symbol->section->output_section->vma
3543
0
            + symbol->section->output_offset),
3544
0
           reloc_entry->addend);
3545
0
}
3546
3547
static bfd_reloc_status_type
3548
nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3549
          void *data, asection *input_section,
3550
          bfd *output_bfd, char **msg)
3551
0
{
3552
0
  bfd_vma relocation;
3553
0
  bfd_vma gp;
3554
0
  bfd_reloc_status_type r;
3555
3556
3557
  /* This part is from bfd_elf_generic_reloc.  */
3558
0
  if (output_bfd != NULL
3559
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3560
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3561
0
    {
3562
0
      reloc_entry->address += input_section->output_offset;
3563
0
      return bfd_reloc_ok;
3564
0
    }
3565
3566
0
  if (output_bfd != NULL)
3567
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3568
0
    return bfd_reloc_continue;
3569
3570
0
  relocation = (symbol->value
3571
0
    + symbol->section->output_section->vma
3572
0
    + symbol->section->output_offset);
3573
3574
  /* This assumes we've already cached the _gp symbol.  */
3575
0
  r = nios2_elf_final_gp (abfd, symbol, false, msg, &gp);
3576
0
  if (r == bfd_reloc_ok)
3577
0
    {
3578
0
      relocation = relocation + reloc_entry->addend - gp;
3579
0
      reloc_entry->addend = 0;
3580
0
      if ((signed) relocation < -32768 || (signed) relocation > 32767)
3581
0
  {
3582
0
    *msg = _("global pointer relative address out of range");
3583
0
    r = bfd_reloc_outofrange;
3584
0
  }
3585
0
      else
3586
0
  r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3587
0
             input_section,
3588
0
             data, reloc_entry->address,
3589
0
             relocation, reloc_entry->addend);
3590
0
    }
3591
3592
0
  return r;
3593
0
}
3594
3595
static bfd_reloc_status_type
3596
nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3597
         void *data, asection *input_section,
3598
         bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3599
0
{
3600
  /* This part is from bfd_elf_generic_reloc.  */
3601
0
  if (output_bfd != NULL
3602
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3603
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3604
0
    {
3605
0
      reloc_entry->address += input_section->output_offset;
3606
0
      return bfd_reloc_ok;
3607
0
    }
3608
3609
0
  if (output_bfd != NULL)
3610
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3611
0
    return bfd_reloc_continue;
3612
3613
0
  return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3614
0
               input_section,
3615
0
               data, reloc_entry->address,
3616
0
               (symbol->value
3617
0
          + symbol->section->output_section->vma
3618
0
          + symbol->section->output_offset),
3619
0
               reloc_entry->addend);
3620
0
}
3621
3622
static bfd_reloc_status_type
3623
nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3624
         void *data, asection *input_section,
3625
         bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3626
0
{
3627
  /* This part is from bfd_elf_generic_reloc.  */
3628
0
  if (output_bfd != NULL
3629
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3630
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3631
0
    {
3632
0
      reloc_entry->address += input_section->output_offset;
3633
0
      return bfd_reloc_ok;
3634
0
    }
3635
3636
0
  if (output_bfd != NULL)
3637
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3638
0
    return bfd_reloc_continue;
3639
3640
0
  return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3641
0
               input_section,
3642
0
               data, reloc_entry->address,
3643
0
               (symbol->value
3644
0
          + symbol->section->output_section->vma
3645
0
          + symbol->section->output_offset),
3646
0
               reloc_entry->addend);
3647
0
}
3648
3649
static bfd_reloc_status_type
3650
nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3651
          void *data, asection *input_section,
3652
          bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3653
0
{
3654
  /* This part is from bfd_elf_generic_reloc.  */
3655
0
  if (output_bfd != NULL
3656
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3657
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3658
0
    {
3659
0
      reloc_entry->address += input_section->output_offset;
3660
0
      return bfd_reloc_ok;
3661
0
    }
3662
3663
0
  if (output_bfd != NULL)
3664
    /* FIXME: See bfd_perform_relocation.  Is this right?  */
3665
0
    return bfd_reloc_continue;
3666
3667
0
  return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3668
0
          input_section,
3669
0
          data, reloc_entry->address,
3670
0
          (symbol->value
3671
0
           + symbol->section->output_section->vma
3672
0
           + symbol->section->output_offset),
3673
0
          reloc_entry->addend);
3674
0
}
3675
3676
3677
/* Implement elf_backend_relocate_section.  */
3678
static int
3679
nios2_elf32_relocate_section (bfd *output_bfd,
3680
            struct bfd_link_info *info,
3681
            bfd *input_bfd,
3682
            asection *input_section,
3683
            bfd_byte *contents,
3684
            Elf_Internal_Rela *relocs,
3685
            Elf_Internal_Sym *local_syms,
3686
            asection **local_sections)
3687
0
{
3688
0
  Elf_Internal_Shdr *symtab_hdr;
3689
0
  struct elf_link_hash_entry **sym_hashes;
3690
0
  Elf_Internal_Rela *rel;
3691
0
  Elf_Internal_Rela *relend;
3692
0
  struct elf32_nios2_link_hash_table *htab;
3693
0
  asection *sgot;
3694
0
  asection *splt;
3695
0
  asection *sreloc = NULL;
3696
0
  bfd_vma *local_got_offsets;
3697
0
  bfd_vma got_base;
3698
3699
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3700
0
  sym_hashes = elf_sym_hashes (input_bfd);
3701
0
  relend = relocs + input_section->reloc_count;
3702
3703
0
  htab = elf32_nios2_hash_table (info);
3704
0
  sgot = htab->root.sgot;
3705
0
  splt = htab->root.splt;
3706
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
3707
3708
0
  if (htab->h_gp_got == NULL)
3709
0
    got_base = 0;
3710
0
  else
3711
0
    got_base = htab->h_gp_got->root.u.def.value;
3712
3713
0
  for (rel = relocs; rel < relend; rel++)
3714
0
    {
3715
0
      reloc_howto_type *howto;
3716
0
      unsigned long r_symndx;
3717
0
      Elf_Internal_Sym *sym;
3718
0
      asection *sec;
3719
0
      struct elf_link_hash_entry *h;
3720
0
      struct elf32_nios2_link_hash_entry *eh;
3721
0
      bfd_vma relocation;
3722
0
      bfd_vma gp;
3723
0
      bfd_reloc_status_type r = bfd_reloc_ok;
3724
0
      const char *name = NULL;
3725
0
      int r_type;
3726
0
      const char *format;
3727
0
      char *msg = NULL;
3728
0
      bool unresolved_reloc;
3729
0
      bfd_vma off;
3730
0
      int use_plt;
3731
3732
0
      r_type = ELF32_R_TYPE (rel->r_info);
3733
0
      r_symndx = ELF32_R_SYM (rel->r_info);
3734
3735
0
      howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
3736
0
      h = NULL;
3737
0
      sym = NULL;
3738
0
      sec = NULL;
3739
3740
0
      if (r_symndx < symtab_hdr->sh_info)
3741
0
  {
3742
0
    sym = local_syms + r_symndx;
3743
0
    sec = local_sections[r_symndx];
3744
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3745
0
  }
3746
0
      else
3747
0
  {
3748
0
    bool warned, ignored;
3749
3750
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3751
0
           r_symndx, symtab_hdr, sym_hashes,
3752
0
           h, sec, relocation,
3753
0
           unresolved_reloc, warned, ignored);
3754
0
  }
3755
3756
0
      if (sec && discarded_section (sec))
3757
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3758
0
           rel, 1, relend, howto, 0, contents);
3759
3760
      /* Nothing more to do unless this is a final link.  */
3761
0
      if (bfd_link_relocatable (info))
3762
0
  continue;
3763
3764
0
      if (howto)
3765
0
  {
3766
0
    bool resolved_to_zero;
3767
3768
0
    resolved_to_zero = (h != NULL
3769
0
            && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
3770
0
    switch (howto->type)
3771
0
      {
3772
0
      case R_NIOS2_HI16:
3773
0
        r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3774
0
            input_section,
3775
0
            contents, rel->r_offset,
3776
0
            relocation, rel->r_addend);
3777
0
        break;
3778
0
      case R_NIOS2_LO16:
3779
0
        r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3780
0
            input_section,
3781
0
            contents, rel->r_offset,
3782
0
            relocation, rel->r_addend);
3783
0
        break;
3784
0
      case R_NIOS2_PCREL_LO:
3785
0
        r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3786
0
                  input_section,
3787
0
                  contents,
3788
0
                  rel->r_offset,
3789
0
                  relocation,
3790
0
                  rel->r_addend);
3791
0
        break;
3792
0
      case R_NIOS2_HIADJ16:
3793
0
        r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3794
0
               input_section, contents,
3795
0
               rel->r_offset, relocation,
3796
0
               rel->r_addend);
3797
0
        break;
3798
0
      case R_NIOS2_PCREL_HA:
3799
0
        r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3800
0
               input_section,
3801
0
               contents,
3802
0
               rel->r_offset,
3803
0
               relocation,
3804
0
               rel->r_addend);
3805
0
        break;
3806
0
      case R_NIOS2_PCREL16:
3807
0
        r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3808
0
               input_section, contents,
3809
0
               rel->r_offset, relocation,
3810
0
               rel->r_addend);
3811
0
        break;
3812
0
      case R_NIOS2_GPREL:
3813
        /* Turns an absolute address into a gp-relative address.  */
3814
0
        if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3815
0
    {
3816
0
      bfd_vma reloc_address;
3817
3818
0
      if (sec && sec->output_section)
3819
0
        reloc_address = (sec->output_section->vma
3820
0
             + sec->output_offset
3821
0
             + rel->r_offset);
3822
0
      else
3823
0
        reloc_address = 0;
3824
3825
0
      format = _("global pointer relative relocation at address "
3826
0
           "%#" PRIx64 " when _gp not defined\n");
3827
0
      msg = bfd_asprintf (format, (uint64_t) reloc_address);
3828
0
      r = bfd_reloc_dangerous;
3829
0
    }
3830
0
        else
3831
0
    {
3832
0
      bfd_vma symbol_address = rel->r_addend + relocation;
3833
0
      relocation = symbol_address - gp;
3834
0
      rel->r_addend = 0;
3835
0
      if (((signed) relocation < -32768
3836
0
           || (signed) relocation > 32767)
3837
0
          && (!h
3838
0
        || h->root.type == bfd_link_hash_defined
3839
0
        || h->root.type == bfd_link_hash_defweak))
3840
0
        {
3841
0
          if (h)
3842
0
      name = h->root.root.string;
3843
0
          else
3844
0
      {
3845
0
        name = (bfd_elf_string_from_elf_section
3846
0
          (input_bfd, symtab_hdr->sh_link,
3847
0
           sym->st_name));
3848
0
        if (name == NULL || *name == '\0')
3849
0
          name = bfd_section_name (sec);
3850
0
      }
3851
          /* xgettext:c-format */
3852
0
          format = _("unable to reach %s (at %#" PRIx64 ") from "
3853
0
         "the global pointer (at %#" PRIx64 ") "
3854
0
         "because the offset (%" PRId64 ") is out of "
3855
0
         "the allowed range, -32678 to 32767\n" );
3856
0
          msg = bfd_asprintf (format, name,
3857
0
            (uint64_t) symbol_address,
3858
0
            (uint64_t) gp,
3859
0
            (int64_t) relocation);
3860
0
          r = bfd_reloc_outofrange;
3861
0
        }
3862
0
      else
3863
0
        r = _bfd_final_link_relocate (howto, input_bfd,
3864
0
              input_section, contents,
3865
0
              rel->r_offset, relocation,
3866
0
              rel->r_addend);
3867
0
    }
3868
0
        break;
3869
0
      case R_NIOS2_UJMP:
3870
0
        r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3871
0
            input_section,
3872
0
            contents, rel->r_offset,
3873
0
            relocation, rel->r_addend);
3874
0
        break;
3875
0
      case R_NIOS2_CJMP:
3876
0
        r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3877
0
            input_section,
3878
0
            contents, rel->r_offset,
3879
0
            relocation, rel->r_addend);
3880
0
        break;
3881
0
      case R_NIOS2_CALLR:
3882
0
        r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3883
0
             input_section, contents,
3884
0
             rel->r_offset, relocation,
3885
0
             rel->r_addend);
3886
0
        break;
3887
0
      case R_NIOS2_CALL26:
3888
0
      case R_NIOS2_CALL26_NOAT:
3889
        /* If we have a call to an undefined weak symbol, we just want
3890
     to stuff a zero in the bits of the call instruction and
3891
     bypass the normal call26 relocation handling, because it'll
3892
     diagnose an overflow error if address 0 isn't in the same
3893
     256MB segment as the call site.  Presumably the call
3894
     should be guarded by a null check anyway.  */
3895
0
        if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3896
0
    {
3897
0
      BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3898
0
      r = _bfd_final_link_relocate (howto, input_bfd,
3899
0
            input_section, contents,
3900
0
            rel->r_offset, relocation,
3901
0
            rel->r_addend);
3902
0
      break;
3903
0
    }
3904
        /* Handle relocations which should use the PLT entry.
3905
     NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3906
     which may point to a PLT entry, but we don't need to handle
3907
     that here.  If we created a PLT entry, all branches in this
3908
     object should go to it.  */
3909
0
        if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3910
0
    {
3911
      /* If we've created a .plt section, and assigned a PLT entry
3912
         to this function, it should not be known to bind locally.
3913
         If it were, we would have cleared the PLT entry.  */
3914
0
      BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3915
3916
0
      relocation = (splt->output_section->vma
3917
0
        + splt->output_offset
3918
0
        + h->plt.offset);
3919
3920
0
      unresolved_reloc = false;
3921
0
    }
3922
        /* Detect R_NIOS2_CALL26 relocations that would overflow the
3923
     256MB segment.  Replace the target with a reference to a
3924
     trampoline instead.
3925
     Note that htab->stub_group is null if relaxation has been
3926
     disabled by the --no-relax linker command-line option, so
3927
     we can use that to skip this processing entirely.  */
3928
0
        if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3929
0
    {
3930
0
      bfd_vma dest = relocation + rel->r_addend;
3931
0
      enum elf32_nios2_stub_type stub_type;
3932
3933
0
      eh = (struct elf32_nios2_link_hash_entry *)h;
3934
0
      stub_type = nios2_type_of_stub (input_section, rel, eh,
3935
0
              htab, dest, NULL);
3936
3937
0
      if (stub_type != nios2_stub_none)
3938
0
        {
3939
0
          struct elf32_nios2_stub_hash_entry *hsh;
3940
3941
0
          hsh = nios2_get_stub_entry (input_section, sec,
3942
0
              eh, rel, htab, stub_type);
3943
0
          if (hsh == NULL)
3944
0
      {
3945
0
        r = bfd_reloc_undefined;
3946
0
        break;
3947
0
      }
3948
3949
0
          dest = (hsh->stub_offset
3950
0
            + hsh->stub_sec->output_offset
3951
0
            + hsh->stub_sec->output_section->vma);
3952
0
          r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3953
0
                input_section,
3954
0
                contents,
3955
0
                rel->r_offset,
3956
0
                dest, 0);
3957
0
          break;
3958
0
        }
3959
0
    }
3960
3961
        /* Normal case.  */
3962
0
        r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3963
0
              input_section, contents,
3964
0
              rel->r_offset, relocation,
3965
0
              rel->r_addend);
3966
0
        break;
3967
0
      case R_NIOS2_ALIGN:
3968
0
        r = bfd_reloc_ok;
3969
        /* For symmetry this would be
3970
     r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3971
              input_section, contents,
3972
              rel->r_offset, relocation,
3973
              rel->r_addend);
3974
    but do_ignore_reloc would do no more than return
3975
    bfd_reloc_ok. */
3976
0
        break;
3977
3978
0
      case R_NIOS2_GOT16:
3979
0
      case R_NIOS2_CALL16:
3980
0
      case R_NIOS2_GOT_LO:
3981
0
      case R_NIOS2_GOT_HA:
3982
0
      case R_NIOS2_CALL_LO:
3983
0
      case R_NIOS2_CALL_HA:
3984
        /* Relocation is to the entry for this symbol in the
3985
     global offset table.  */
3986
0
        if (sgot == NULL)
3987
0
    {
3988
0
      r = bfd_reloc_notsupported;
3989
0
      break;
3990
0
    }
3991
3992
0
        use_plt = 0;
3993
3994
0
        if (h != NULL)
3995
0
    {
3996
0
      bool dyn;
3997
3998
0
      eh = (struct elf32_nios2_link_hash_entry *)h;
3999
0
      use_plt = (eh->got_types_used == CALL_USED
4000
0
           && h->plt.offset != (bfd_vma) -1);
4001
4002
0
      off = h->got.offset;
4003
0
      BFD_ASSERT (off != (bfd_vma) -1);
4004
0
      dyn = htab->root.dynamic_sections_created;
4005
0
      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4006
0
               bfd_link_pic (info),
4007
0
               h)
4008
0
          || (bfd_link_pic (info)
4009
0
        && SYMBOL_REFERENCES_LOCAL (info, h))
4010
0
          || ((ELF_ST_VISIBILITY (h->other)
4011
0
         || resolved_to_zero)
4012
0
        && h->root.type == bfd_link_hash_undefweak))
4013
0
        {
4014
          /* This is actually a static link, or it is a -Bsymbolic
4015
       link and the symbol is defined locally.  We must
4016
       initialize this entry in the global offset table.
4017
       Since the offset must always be a multiple of 4, we
4018
       use the least significant bit to record whether we
4019
       have initialized it already.
4020
4021
       When doing a dynamic link, we create a .rela.got
4022
       relocation entry to initialize the value.  This is
4023
       done in the finish_dynamic_symbol routine.  */
4024
0
          if ((off & 1) != 0)
4025
0
      off &= ~1;
4026
0
          else
4027
0
      {
4028
0
        bfd_put_32 (output_bfd, relocation,
4029
0
              sgot->contents + off);
4030
0
        h->got.offset |= 1;
4031
0
      }
4032
0
        }
4033
0
      else
4034
0
        unresolved_reloc = false;
4035
0
    }
4036
0
        else
4037
0
    {
4038
0
      BFD_ASSERT (local_got_offsets != NULL
4039
0
            && local_got_offsets[r_symndx] != (bfd_vma) -1);
4040
4041
0
      off = local_got_offsets[r_symndx];
4042
4043
      /* The offset must always be a multiple of 4.  We use the
4044
         least significant bit to record whether we have already
4045
         generated the necessary reloc.  */
4046
0
      if ((off & 1) != 0)
4047
0
        off &= ~1;
4048
0
      else
4049
0
        {
4050
0
          bfd_put_32 (output_bfd, relocation,
4051
0
          sgot->contents + off);
4052
4053
0
          if (bfd_link_pic (info))
4054
0
      {
4055
0
        asection *srelgot;
4056
0
        Elf_Internal_Rela outrel;
4057
0
        bfd_byte *loc;
4058
4059
0
        srelgot = htab->root.srelgot;
4060
0
        BFD_ASSERT (srelgot != NULL);
4061
4062
0
        outrel.r_addend = relocation;
4063
0
        outrel.r_offset = (sgot->output_section->vma
4064
0
               + sgot->output_offset
4065
0
               + off);
4066
0
        outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4067
0
        loc = srelgot->contents;
4068
0
        loc += (srelgot->reloc_count++ *
4069
0
          sizeof (Elf32_External_Rela));
4070
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4071
0
      }
4072
4073
0
          local_got_offsets[r_symndx] |= 1;
4074
0
        }
4075
0
    }
4076
4077
0
        if (use_plt && bfd_link_pic (info))
4078
0
    {
4079
0
      off = ((h->plt.offset - 24) / 12 + 3) * 4;
4080
0
      relocation = (htab->root.sgotplt->output_offset + off
4081
0
        - got_base);
4082
0
    }
4083
0
        else
4084
0
    relocation = sgot->output_offset + off - got_base;
4085
4086
        /* This relocation does not use the addend.  */
4087
0
        rel->r_addend = 0;
4088
4089
0
        switch (howto->type)
4090
0
    {
4091
0
    case R_NIOS2_GOT_LO:
4092
0
    case R_NIOS2_CALL_LO:
4093
0
      r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4094
0
                input_section, contents,
4095
0
                rel->r_offset, relocation,
4096
0
                rel->r_addend);
4097
0
      break;
4098
0
    case R_NIOS2_GOT_HA:
4099
0
    case R_NIOS2_CALL_HA:
4100
0
      r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4101
0
                   input_section, contents,
4102
0
                   rel->r_offset,
4103
0
                   relocation,
4104
0
                   rel->r_addend);
4105
0
      break;
4106
0
    default:
4107
0
      r = _bfd_final_link_relocate (howto, input_bfd,
4108
0
            input_section, contents,
4109
0
            rel->r_offset, relocation,
4110
0
            rel->r_addend);
4111
0
      break;
4112
0
    }
4113
0
        break;
4114
4115
0
      case R_NIOS2_GOTOFF_LO:
4116
0
      case R_NIOS2_GOTOFF_HA:
4117
0
      case R_NIOS2_GOTOFF:
4118
        /* Relocation is relative to the global offset table pointer.  */
4119
4120
0
        BFD_ASSERT (sgot != NULL);
4121
0
        if (sgot == NULL)
4122
0
    {
4123
0
      r = bfd_reloc_notsupported;
4124
0
      break;
4125
0
    }
4126
4127
        /* Note that sgot->output_offset is not involved in this
4128
     calculation.  We always want the start of .got.  */
4129
0
        relocation -= sgot->output_section->vma;
4130
4131
        /* Now we adjust the relocation to be relative to the GOT pointer
4132
     (the _gp_got symbol), which possibly contains the 0x8000 bias.  */
4133
0
        relocation -= got_base;
4134
4135
0
        switch (howto->type)
4136
0
    {
4137
0
    case R_NIOS2_GOTOFF_LO:
4138
0
      r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4139
0
                input_section, contents,
4140
0
                rel->r_offset, relocation,
4141
0
                rel->r_addend);
4142
0
      break;
4143
0
    case R_NIOS2_GOTOFF_HA:
4144
0
      r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4145
0
                   input_section, contents,
4146
0
                   rel->r_offset,
4147
0
                   relocation,
4148
0
                   rel->r_addend);
4149
0
      break;
4150
0
    default:
4151
0
      r = _bfd_final_link_relocate (howto, input_bfd,
4152
0
            input_section, contents,
4153
0
            rel->r_offset, relocation,
4154
0
            rel->r_addend);
4155
0
      break;
4156
0
    }
4157
0
        break;
4158
4159
0
      case R_NIOS2_TLS_LDO16:
4160
0
        relocation -= dtpoff_base (info) + DTP_OFFSET;
4161
4162
0
        r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4163
0
              contents, rel->r_offset,
4164
0
              relocation, rel->r_addend);
4165
0
        break;
4166
0
      case R_NIOS2_TLS_LDM16:
4167
0
        if (htab->root.sgot == NULL)
4168
0
    abort ();
4169
4170
0
        off = htab->tls_ldm_got.offset;
4171
4172
0
        if ((off & 1) != 0)
4173
0
    off &= ~1;
4174
0
        else
4175
0
    {
4176
      /* If we don't know the module number, create a relocation
4177
         for it.  */
4178
0
      if (bfd_link_pic (info))
4179
0
        {
4180
0
          Elf_Internal_Rela outrel;
4181
0
          bfd_byte *loc;
4182
4183
0
          if (htab->root.srelgot == NULL)
4184
0
      abort ();
4185
4186
0
          outrel.r_addend = 0;
4187
0
          outrel.r_offset = (htab->root.sgot->output_section->vma
4188
0
           + htab->root.sgot->output_offset
4189
0
           + off);
4190
0
          outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4191
4192
0
          loc = htab->root.srelgot->contents;
4193
0
          loc += (htab->root.srelgot->reloc_count++
4194
0
            * sizeof (Elf32_External_Rela));
4195
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4196
0
        }
4197
0
      else
4198
0
        bfd_put_32 (output_bfd, 1,
4199
0
        htab->root.sgot->contents + off);
4200
4201
0
      htab->tls_ldm_got.offset |= 1;
4202
0
    }
4203
4204
0
        relocation = htab->root.sgot->output_offset + off - got_base;
4205
4206
0
        r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4207
0
              contents, rel->r_offset,
4208
0
              relocation, rel->r_addend);
4209
4210
0
        break;
4211
0
      case R_NIOS2_TLS_GD16:
4212
0
      case R_NIOS2_TLS_IE16:
4213
0
        {
4214
0
    int indx;
4215
0
    char tls_type;
4216
4217
0
    if (htab->root.sgot == NULL)
4218
0
      abort ();
4219
4220
0
    indx = 0;
4221
0
    if (h != NULL)
4222
0
      {
4223
0
        bool dyn;
4224
0
        dyn = htab->root.dynamic_sections_created;
4225
0
        if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4226
0
               bfd_link_pic (info),
4227
0
               h)
4228
0
      && (!bfd_link_pic (info)
4229
0
          || !SYMBOL_REFERENCES_LOCAL (info, h)))
4230
0
          {
4231
0
      unresolved_reloc = false;
4232
0
      indx = h->dynindx;
4233
0
          }
4234
0
        off = h->got.offset;
4235
0
        tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4236
0
        ->tls_type);
4237
0
      }
4238
0
    else
4239
0
      {
4240
0
        if (local_got_offsets == NULL)
4241
0
          abort ();
4242
0
        off = local_got_offsets[r_symndx];
4243
0
        tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4244
0
        [r_symndx]);
4245
0
      }
4246
4247
0
    if (tls_type == GOT_UNKNOWN)
4248
0
      abort ();
4249
4250
0
    if ((off & 1) != 0)
4251
0
      off &= ~1;
4252
0
    else
4253
0
      {
4254
0
        bool need_relocs = false;
4255
0
        Elf_Internal_Rela outrel;
4256
0
        bfd_byte *loc = NULL;
4257
0
        int cur_off = off;
4258
4259
        /* The GOT entries have not been initialized yet.  Do it
4260
           now, and emit any relocations.  If both an IE GOT and a
4261
           GD GOT are necessary, we emit the GD first.  */
4262
4263
0
        if ((bfd_link_pic (info) || indx != 0)
4264
0
      && (h == NULL
4265
0
          || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4266
0
        && !resolved_to_zero)
4267
0
          || h->root.type != bfd_link_hash_undefweak))
4268
0
          {
4269
0
      need_relocs = true;
4270
0
      if (htab->root.srelgot == NULL)
4271
0
        abort ();
4272
0
      loc = htab->root.srelgot->contents;
4273
0
      loc += (htab->root.srelgot->reloc_count *
4274
0
        sizeof (Elf32_External_Rela));
4275
0
          }
4276
4277
0
        if (tls_type & GOT_TLS_GD)
4278
0
          {
4279
0
      if (need_relocs)
4280
0
        {
4281
0
          outrel.r_addend = 0;
4282
0
          outrel.r_offset = (htab->root.sgot->output_section->vma
4283
0
                 + htab->root.sgot->output_offset
4284
0
                 + cur_off);
4285
0
          outrel.r_info = ELF32_R_INFO (indx,
4286
0
                R_NIOS2_TLS_DTPMOD);
4287
4288
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4289
0
                   loc);
4290
0
          htab->root.srelgot->reloc_count++;
4291
0
          loc += sizeof (Elf32_External_Rela);
4292
4293
0
          if (indx == 0)
4294
0
            bfd_put_32 (output_bfd,
4295
0
            (relocation - dtpoff_base (info) -
4296
0
             DTP_OFFSET),
4297
0
            htab->root.sgot->contents + cur_off + 4);
4298
0
          else
4299
0
            {
4300
0
        outrel.r_addend = 0;
4301
0
        outrel.r_info = ELF32_R_INFO (indx,
4302
0
          R_NIOS2_TLS_DTPREL);
4303
0
        outrel.r_offset += 4;
4304
4305
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4306
0
                 loc);
4307
0
        htab->root.srelgot->reloc_count++;
4308
0
        loc += sizeof (Elf32_External_Rela);
4309
0
            }
4310
0
        }
4311
0
      else
4312
0
        {
4313
          /* If we are not emitting relocations for a
4314
             general dynamic reference, then we must be in a
4315
             static link or an executable link with the
4316
             symbol binding locally.  Mark it as belonging
4317
             to module 1, the executable.  */
4318
0
          bfd_put_32 (output_bfd, 1,
4319
0
          htab->root.sgot->contents + cur_off);
4320
0
          bfd_put_32 (output_bfd, (relocation -
4321
0
                 dtpoff_base (info) -
4322
0
                 DTP_OFFSET),
4323
0
          htab->root.sgot->contents + cur_off + 4);
4324
0
        }
4325
4326
0
      cur_off += 8;
4327
0
          }
4328
4329
0
        if (tls_type & GOT_TLS_IE)
4330
0
          {
4331
0
      if (need_relocs)
4332
0
        {
4333
0
          if (indx == 0)
4334
0
            outrel.r_addend = (relocation -
4335
0
             dtpoff_base (info));
4336
0
          else
4337
0
            outrel.r_addend = 0;
4338
0
          outrel.r_offset = (htab->root.sgot->output_section->vma
4339
0
                 + htab->root.sgot->output_offset
4340
0
                 + cur_off);
4341
0
          outrel.r_info = ELF32_R_INFO (indx,
4342
0
                R_NIOS2_TLS_TPREL);
4343
4344
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4345
0
                   loc);
4346
0
          htab->root.srelgot->reloc_count++;
4347
0
          loc += sizeof (Elf32_External_Rela);
4348
0
        }
4349
0
      else
4350
0
        bfd_put_32 (output_bfd, (tpoff (info, relocation)
4351
0
               - TP_OFFSET),
4352
0
              htab->root.sgot->contents + cur_off);
4353
0
      cur_off += 4;
4354
0
          }
4355
4356
0
        if (h != NULL)
4357
0
          h->got.offset |= 1;
4358
0
        else
4359
0
          local_got_offsets[r_symndx] |= 1;
4360
0
      }
4361
4362
0
    if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4363
0
      off += 8;
4364
0
    relocation = htab->root.sgot->output_offset + off - got_base;
4365
4366
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4367
0
                contents, rel->r_offset,
4368
0
                relocation, rel->r_addend);
4369
0
        }
4370
4371
0
        break;
4372
0
      case R_NIOS2_TLS_LE16:
4373
0
        if (bfd_link_dll (info))
4374
0
    {
4375
0
      _bfd_error_handler
4376
        /* xgettext:c-format */
4377
0
        (_("%pB(%pA+%#" PRIx64 "): %s relocation not "
4378
0
           "permitted in shared object"),
4379
0
         input_bfd, input_section,
4380
0
         (uint64_t) rel->r_offset, howto->name);
4381
0
      return false;
4382
0
    }
4383
0
        else
4384
0
    relocation = tpoff (info, relocation) - TP_OFFSET;
4385
4386
0
        r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4387
0
              contents, rel->r_offset,
4388
0
              relocation, rel->r_addend);
4389
0
        break;
4390
4391
0
      case R_NIOS2_BFD_RELOC_32:
4392
0
        if (bfd_link_pic (info)
4393
0
      && (input_section->flags & SEC_ALLOC) != 0
4394
0
      && (h == NULL
4395
0
          || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4396
0
        && !resolved_to_zero)
4397
0
          || h->root.type != bfd_link_hash_undefweak))
4398
0
    {
4399
0
      Elf_Internal_Rela outrel;
4400
0
      bfd_byte *loc;
4401
0
      bool skip, relocate;
4402
4403
      /* When generating a shared object, these relocations
4404
         are copied into the output file to be resolved at run
4405
         time.  */
4406
4407
0
      skip = false;
4408
0
      relocate = false;
4409
4410
0
      outrel.r_offset
4411
0
        = _bfd_elf_section_offset (output_bfd, info,
4412
0
                 input_section, rel->r_offset);
4413
0
      if (outrel.r_offset == (bfd_vma) -1)
4414
0
        skip = true;
4415
0
      else if (outrel.r_offset == (bfd_vma) -2)
4416
0
        skip = true, relocate = true;
4417
0
      outrel.r_offset += (input_section->output_section->vma
4418
0
              + input_section->output_offset);
4419
4420
0
      if (skip)
4421
0
        memset (&outrel, 0, sizeof outrel);
4422
0
      else if (h != NULL
4423
0
         && h->dynindx != -1
4424
0
         && (!bfd_link_pic (info)
4425
0
             || !SYMBOLIC_BIND (info, h)
4426
0
             || !h->def_regular))
4427
0
        {
4428
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4429
0
          outrel.r_addend = rel->r_addend;
4430
0
        }
4431
0
      else
4432
0
        {
4433
          /* This symbol is local, or marked to become local.  */
4434
0
          outrel.r_addend = relocation + rel->r_addend;
4435
0
          relocate = true;
4436
0
          outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4437
0
        }
4438
4439
0
      sreloc = elf_section_data (input_section)->sreloc;
4440
0
      if (sreloc == NULL)
4441
0
        abort ();
4442
4443
0
      loc = sreloc->contents;
4444
0
      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4445
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4446
4447
      /* This reloc will be computed at runtime, so there's no
4448
         need to do anything now, except for R_NIOS2_BFD_RELOC_32
4449
         relocations that have been turned into
4450
         R_NIOS2_RELATIVE.  */
4451
0
      if (!relocate)
4452
0
        break;
4453
0
    }
4454
4455
0
        r = _bfd_final_link_relocate (howto, input_bfd,
4456
0
              input_section, contents,
4457
0
              rel->r_offset, relocation,
4458
0
              rel->r_addend);
4459
0
        break;
4460
4461
0
      case R_NIOS2_TLS_DTPREL:
4462
0
        relocation -= dtpoff_base (info);
4463
        /* Fall through.  */
4464
4465
0
      default:
4466
0
        r = _bfd_final_link_relocate (howto, input_bfd,
4467
0
              input_section, contents,
4468
0
              rel->r_offset, relocation,
4469
0
              rel->r_addend);
4470
0
        break;
4471
0
      }
4472
0
  }
4473
0
      else
4474
0
  r = bfd_reloc_notsupported;
4475
4476
0
      if (r != bfd_reloc_ok)
4477
0
  {
4478
0
    if (h != NULL)
4479
0
      name = h->root.root.string;
4480
0
    else
4481
0
      {
4482
0
        name = bfd_elf_string_from_elf_section (input_bfd,
4483
0
                  symtab_hdr->sh_link,
4484
0
                  sym->st_name);
4485
0
        if (name == NULL || *name == '\0')
4486
0
    name = bfd_section_name (sec);
4487
0
      }
4488
4489
0
    switch (r)
4490
0
      {
4491
0
      case bfd_reloc_overflow:
4492
0
        (*info->callbacks->reloc_overflow) (info, NULL, name,
4493
0
              howto->name, (bfd_vma) 0,
4494
0
              input_bfd, input_section,
4495
0
              rel->r_offset);
4496
0
        break;
4497
4498
0
      case bfd_reloc_undefined:
4499
0
        (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4500
0
                input_section,
4501
0
                rel->r_offset, true);
4502
0
        break;
4503
4504
0
      case bfd_reloc_outofrange:
4505
0
        if (msg == NULL)
4506
0
    msg = _("relocation out of range");
4507
0
        break;
4508
4509
0
      case bfd_reloc_notsupported:
4510
0
        if (msg == NULL)
4511
0
    msg = _("unsupported relocation");
4512
0
        break;
4513
4514
0
      case bfd_reloc_dangerous:
4515
0
        if (msg == NULL)
4516
0
    msg = _("dangerous relocation");
4517
0
        break;
4518
4519
0
      default:
4520
0
        if (msg == NULL)
4521
0
    msg = _("unknown error");
4522
0
        break;
4523
0
      }
4524
4525
0
    if (msg)
4526
0
      {
4527
0
        (*info->callbacks->warning) (info, msg, name, input_bfd,
4528
0
             input_section, rel->r_offset);
4529
0
        return false;
4530
0
      }
4531
0
  }
4532
0
    }
4533
0
  return true;
4534
0
}
4535
4536
/* Implement elf-backend_section_flags:
4537
   Convert NIOS2 specific section flags to bfd internal section flags.  */
4538
static bool
4539
nios2_elf32_section_flags (const Elf_Internal_Shdr *hdr)
4540
0
{
4541
0
  if (hdr->sh_flags & SHF_NIOS2_GPREL)
4542
0
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
4543
4544
0
  return true;
4545
0
}
4546
4547
/* Implement elf_backend_fake_sections:
4548
   Set the correct type for an NIOS2 ELF section.  We do this by the
4549
   section name, which is a hack, but ought to work.  */
4550
static bool
4551
nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4552
         Elf_Internal_Shdr *hdr, asection *sec)
4553
0
{
4554
0
  const char *name = bfd_section_name (sec);
4555
4556
0
  if ((sec->flags & SEC_SMALL_DATA)
4557
0
      || strcmp (name, ".sdata") == 0
4558
0
      || strcmp (name, ".sbss") == 0
4559
0
      || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4560
0
    hdr->sh_flags |= SHF_NIOS2_GPREL;
4561
4562
0
  return true;
4563
0
}
4564
4565
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4566
   shortcuts to them in our hash table.  */
4567
static bool
4568
create_got_section (bfd *dynobj, struct bfd_link_info *info)
4569
0
{
4570
0
  struct elf32_nios2_link_hash_table *htab;
4571
0
  struct elf_link_hash_entry *h;
4572
4573
0
  htab = elf32_nios2_hash_table (info);
4574
4575
0
  if (! _bfd_elf_create_got_section (dynobj, info))
4576
0
    return false;
4577
4578
  /* In order for the two loads in .PLTresolve to share the same %hiadj,
4579
     _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary.  */
4580
0
  if (!bfd_set_section_alignment (htab->root.sgotplt, 4))
4581
0
    return false;
4582
4583
  /* The Nios II ABI specifies that GOT-relative relocations are relative
4584
     to the linker-created symbol _gp_got, rather than using
4585
     _GLOBAL_OFFSET_TABLE_ directly.  In particular, the latter always
4586
     points to the base of the GOT while _gp_got may include a bias.  */
4587
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4588
0
           "_gp_got");
4589
0
  htab->h_gp_got = h;
4590
0
  if (h == NULL)
4591
0
    return false;
4592
4593
0
  return true;
4594
0
}
4595
4596
/* Implement elf_backend_create_dynamic_sections:
4597
   Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4598
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4599
   hash table.  */
4600
static bool
4601
nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4602
0
{
4603
0
  struct elf32_nios2_link_hash_table *htab;
4604
4605
0
  htab = elf32_nios2_hash_table (info);
4606
0
  if (!htab->root.sgot && !create_got_section (dynobj, info))
4607
0
    return false;
4608
4609
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4610
0
    return false;
4611
4612
  /* In order for the two loads in a shared object .PLTresolve to share the
4613
     same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4614
     to a 16-byte boundary.  This is because the addresses for these loads
4615
     include the -(.plt+4) PIC correction.  */
4616
0
  return bfd_set_section_alignment (htab->root.splt, 4);
4617
0
}
4618
4619
/* Implement elf_backend_copy_indirect_symbol:
4620
   Copy the extra info we tack onto an elf_link_hash_entry.  */
4621
static void
4622
nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4623
          struct elf_link_hash_entry *dir,
4624
          struct elf_link_hash_entry *ind)
4625
0
{
4626
0
  struct elf32_nios2_link_hash_entry *edir, *eind;
4627
4628
0
  edir = (struct elf32_nios2_link_hash_entry *) dir;
4629
0
  eind = (struct elf32_nios2_link_hash_entry *) ind;
4630
4631
0
  if (ind->root.type == bfd_link_hash_indirect
4632
0
      && dir->got.refcount <= 0)
4633
0
    {
4634
0
      edir->tls_type = eind->tls_type;
4635
0
      eind->tls_type = GOT_UNKNOWN;
4636
0
    }
4637
4638
0
  edir->got_types_used |= eind->got_types_used;
4639
4640
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4641
0
}
4642
4643
/* Set the right machine number for a NIOS2 ELF file.  */
4644
4645
static bool
4646
nios2_elf32_object_p (bfd *abfd)
4647
0
{
4648
0
  unsigned long mach;
4649
4650
0
  mach = elf_elfheader (abfd)->e_flags;
4651
4652
0
  switch (mach)
4653
0
    {
4654
0
    default:
4655
0
    case EF_NIOS2_ARCH_R1:
4656
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4657
0
      break;
4658
0
    case EF_NIOS2_ARCH_R2:
4659
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4660
0
      break;
4661
0
    }
4662
4663
0
  return true;
4664
0
}
4665
4666
/* Implement elf_backend_check_relocs:
4667
   Look through the relocs for a section during the first phase.  */
4668
static bool
4669
nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4670
        asection *sec, const Elf_Internal_Rela *relocs)
4671
0
{
4672
0
  Elf_Internal_Shdr *symtab_hdr;
4673
0
  struct elf_link_hash_entry **sym_hashes;
4674
0
  const Elf_Internal_Rela *rel;
4675
0
  const Elf_Internal_Rela *rel_end;
4676
0
  struct elf32_nios2_link_hash_table *htab;
4677
0
  asection *sreloc = NULL;
4678
0
  bfd_signed_vma *local_got_refcounts;
4679
4680
0
  if (bfd_link_relocatable (info))
4681
0
    return true;
4682
4683
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4684
0
  sym_hashes = elf_sym_hashes (abfd);
4685
0
  local_got_refcounts = elf_local_got_refcounts (abfd);
4686
4687
0
  htab = elf32_nios2_hash_table (info);
4688
4689
0
  rel_end = relocs + sec->reloc_count;
4690
0
  for (rel = relocs; rel < rel_end; rel++)
4691
0
    {
4692
0
      unsigned int r_type;
4693
0
      struct elf_link_hash_entry *h;
4694
0
      unsigned long r_symndx;
4695
4696
0
      r_symndx = ELF32_R_SYM (rel->r_info);
4697
0
      if (r_symndx < symtab_hdr->sh_info)
4698
0
  h = NULL;
4699
0
      else
4700
0
  {
4701
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4702
0
    while (h->root.type == bfd_link_hash_indirect
4703
0
     || h->root.type == bfd_link_hash_warning)
4704
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4705
0
  }
4706
4707
0
      r_type = ELF32_R_TYPE (rel->r_info);
4708
4709
0
      switch (r_type)
4710
0
  {
4711
0
  case R_NIOS2_GOT16:
4712
0
  case R_NIOS2_GOT_LO:
4713
0
  case R_NIOS2_GOT_HA:
4714
0
  case R_NIOS2_CALL16:
4715
0
  case R_NIOS2_CALL_LO:
4716
0
  case R_NIOS2_CALL_HA:
4717
0
  case R_NIOS2_TLS_GD16:
4718
0
  case R_NIOS2_TLS_IE16:
4719
    /* This symbol requires a global offset table entry.  */
4720
0
    {
4721
0
      int tls_type, old_tls_type;
4722
4723
0
      switch (r_type)
4724
0
        {
4725
0
        default:
4726
0
        case R_NIOS2_GOT16:
4727
0
        case R_NIOS2_GOT_LO:
4728
0
        case R_NIOS2_GOT_HA:
4729
0
        case R_NIOS2_CALL16:
4730
0
        case R_NIOS2_CALL_LO:
4731
0
        case R_NIOS2_CALL_HA:
4732
0
    tls_type = GOT_NORMAL;
4733
0
    break;
4734
0
        case R_NIOS2_TLS_GD16:
4735
0
    tls_type = GOT_TLS_GD;
4736
0
    break;
4737
0
        case R_NIOS2_TLS_IE16:
4738
0
    tls_type = GOT_TLS_IE;
4739
0
    break;
4740
0
        }
4741
4742
0
      if (h != NULL)
4743
0
        {
4744
0
    struct elf32_nios2_link_hash_entry *eh
4745
0
      = (struct elf32_nios2_link_hash_entry *)h;
4746
0
    h->got.refcount++;
4747
0
    old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
4748
0
    if (r_type == R_NIOS2_CALL16
4749
0
        || r_type == R_NIOS2_CALL_LO
4750
0
        || r_type == R_NIOS2_CALL_HA)
4751
0
      {
4752
        /* Make sure a plt entry is created for this symbol if
4753
           it turns out to be a function defined by a dynamic
4754
           object.  */
4755
0
        h->plt.refcount++;
4756
0
        h->needs_plt = 1;
4757
0
        h->type = STT_FUNC;
4758
0
        eh->got_types_used |= CALL_USED;
4759
0
      }
4760
0
    else
4761
0
      eh->got_types_used |= GOT_USED;
4762
0
        }
4763
0
      else
4764
0
        {
4765
    /* This is a global offset table entry for a local symbol.  */
4766
0
    if (local_got_refcounts == NULL)
4767
0
      {
4768
0
        bfd_size_type size;
4769
4770
0
        size = symtab_hdr->sh_info;
4771
0
        size *= (sizeof (bfd_signed_vma) + sizeof (char));
4772
0
        local_got_refcounts
4773
0
          = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4774
0
        if (local_got_refcounts == NULL)
4775
0
          return false;
4776
0
        elf_local_got_refcounts (abfd) = local_got_refcounts;
4777
0
        elf32_nios2_local_got_tls_type (abfd)
4778
0
          = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4779
0
      }
4780
0
    local_got_refcounts[r_symndx]++;
4781
0
    old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4782
0
        }
4783
4784
      /* We will already have issued an error message if there is a
4785
         TLS / non-TLS mismatch, based on the symbol type.  We don't
4786
         support any linker relaxations.  So just combine any TLS
4787
         types needed.  */
4788
0
      if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4789
0
    && tls_type != GOT_NORMAL)
4790
0
        tls_type |= old_tls_type;
4791
4792
0
      if (old_tls_type != tls_type)
4793
0
        {
4794
0
    if (h != NULL)
4795
0
      elf32_nios2_hash_entry (h)->tls_type = tls_type;
4796
0
    else
4797
0
      elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4798
0
        }
4799
0
    }
4800
0
  make_got:
4801
0
    if (htab->root.sgot == NULL)
4802
0
      {
4803
0
        if (htab->root.dynobj == NULL)
4804
0
    htab->root.dynobj = abfd;
4805
0
        if (!create_got_section (htab->root.dynobj, info))
4806
0
    return false;
4807
0
      }
4808
0
    break;
4809
4810
0
  case R_NIOS2_TLS_LDM16:
4811
0
    htab->tls_ldm_got.refcount++;
4812
0
    goto make_got;
4813
4814
    /* This relocation describes the C++ object vtable hierarchy.
4815
       Reconstruct it for later use during GC.  */
4816
0
  case R_NIOS2_GNU_VTINHERIT:
4817
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4818
0
      return false;
4819
0
    break;
4820
4821
    /* This relocation describes which C++ vtable entries are actually
4822
       used.  Record for later use during GC.  */
4823
0
  case R_NIOS2_GNU_VTENTRY:
4824
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4825
0
      return false;
4826
0
    break;
4827
4828
0
  case R_NIOS2_BFD_RELOC_32:
4829
0
  case R_NIOS2_CALL26:
4830
0
  case R_NIOS2_CALL26_NOAT:
4831
0
  case R_NIOS2_HIADJ16:
4832
0
  case R_NIOS2_LO16:
4833
4834
0
    if (h != NULL)
4835
0
      {
4836
        /* If this reloc is in a read-only section, we might
4837
       need a copy reloc.  We can't check reliably at this
4838
       stage whether the section is read-only, as input
4839
       sections have not yet been mapped to output sections.
4840
       Tentatively set the flag for now, and correct in
4841
       adjust_dynamic_symbol.  */
4842
0
        if (!bfd_link_pic (info))
4843
0
    h->non_got_ref = 1;
4844
4845
        /* Make sure a plt entry is created for this symbol if it
4846
     turns out to be a function defined by a dynamic object.  */
4847
0
        h->plt.refcount++;
4848
4849
0
        if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
4850
0
    h->needs_plt = 1;
4851
0
      }
4852
4853
    /* If we are creating a shared library, we need to copy the
4854
       reloc into the shared library.  */
4855
0
    if (bfd_link_pic (info)
4856
0
        && (sec->flags & SEC_ALLOC) != 0
4857
0
        && (r_type == R_NIOS2_BFD_RELOC_32
4858
0
      || (h != NULL && ! h->needs_plt
4859
0
          && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
4860
0
      {
4861
0
        struct elf_dyn_relocs *p;
4862
0
        struct elf_dyn_relocs **head;
4863
4864
        /* When creating a shared object, we must copy these
4865
     reloc types into the output file.  We create a reloc
4866
     section in dynobj and make room for this reloc.  */
4867
0
        if (sreloc == NULL)
4868
0
    {
4869
0
      if (htab->root.dynobj == NULL)
4870
0
        htab->root.dynobj = abfd;
4871
4872
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
4873
0
        (sec, htab->root.dynobj, 2, abfd, true);
4874
0
      if (sreloc == NULL)
4875
0
        return false;
4876
0
    }
4877
4878
        /* If this is a global symbol, we count the number of
4879
     relocations we need for this symbol.  */
4880
0
        if (h != NULL)
4881
0
    head = &h->dyn_relocs;
4882
0
        else
4883
0
    {
4884
      /* Track dynamic relocs needed for local syms too.
4885
         We really need local syms available to do this
4886
         easily.  Oh well.  */
4887
4888
0
      asection *s;
4889
0
      void *vpp;
4890
0
      Elf_Internal_Sym *isym;
4891
4892
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
4893
0
            abfd, r_symndx);
4894
0
      if (isym == NULL)
4895
0
        return false;
4896
4897
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4898
0
      if (s == NULL)
4899
0
        s = sec;
4900
4901
0
      vpp = &elf_section_data (s)->local_dynrel;
4902
0
      head = (struct elf_dyn_relocs **) vpp;
4903
0
    }
4904
4905
0
        p = *head;
4906
0
        if (p == NULL || p->sec != sec)
4907
0
    {
4908
0
      size_t amt = sizeof *p;
4909
0
      p = ((struct elf_dyn_relocs *)
4910
0
           bfd_alloc (htab->root.dynobj, amt));
4911
0
      if (p == NULL)
4912
0
        return false;
4913
0
      p->next = *head;
4914
0
      *head = p;
4915
0
      p->sec = sec;
4916
0
      p->count = 0;
4917
0
      p->pc_count = 0;
4918
0
    }
4919
4920
0
        p->count += 1;
4921
4922
0
      }
4923
0
    break;
4924
0
  }
4925
0
    }
4926
4927
0
  return true;
4928
0
}
4929
4930
4931
/* Implement elf_backend_gc_mark_hook:
4932
   Return the section that should be marked against GC for a given
4933
   relocation.  */
4934
static asection *
4935
nios2_elf32_gc_mark_hook (asection *sec,
4936
        struct bfd_link_info *info,
4937
        Elf_Internal_Rela *rel,
4938
        struct elf_link_hash_entry *h,
4939
        Elf_Internal_Sym *sym)
4940
0
{
4941
0
  if (h != NULL)
4942
0
    switch (ELF32_R_TYPE (rel->r_info))
4943
0
      {
4944
0
      case R_NIOS2_GNU_VTINHERIT:
4945
0
      case R_NIOS2_GNU_VTENTRY:
4946
0
  return NULL;
4947
0
      }
4948
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4949
0
}
4950
4951
/* Implement elf_backend_finish_dynamic_symbols:
4952
   Finish up dynamic symbol handling.  We set the contents of various
4953
   dynamic sections here.  */
4954
static bool
4955
nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
4956
           struct bfd_link_info *info,
4957
           struct elf_link_hash_entry *h,
4958
           Elf_Internal_Sym *sym)
4959
0
{
4960
0
  struct elf32_nios2_link_hash_table *htab;
4961
0
  struct elf32_nios2_link_hash_entry *eh
4962
0
    = (struct elf32_nios2_link_hash_entry *)h;
4963
0
  int use_plt;
4964
4965
0
  htab = elf32_nios2_hash_table (info);
4966
4967
0
  if (h->plt.offset != (bfd_vma) -1)
4968
0
    {
4969
0
      asection *splt;
4970
0
      asection *sgotplt;
4971
0
      asection *srela;
4972
0
      bfd_vma plt_index;
4973
0
      bfd_vma got_offset;
4974
0
      Elf_Internal_Rela rela;
4975
0
      bfd_byte *loc;
4976
0
      bfd_vma got_address;
4977
4978
      /* This symbol has an entry in the procedure linkage table.  Set
4979
   it up.  */
4980
0
      BFD_ASSERT (h->dynindx != -1);
4981
0
      splt = htab->root.splt;
4982
0
      sgotplt = htab->root.sgotplt;
4983
0
      srela = htab->root.srelplt;
4984
0
      BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
4985
4986
      /* Emit the PLT entry.  */
4987
0
      if (bfd_link_pic (info))
4988
0
  {
4989
0
    nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
4990
0
            3);
4991
0
    plt_index = (h->plt.offset - 24) / 12;
4992
0
    got_offset = (plt_index + 3) * 4;
4993
0
    nios2_elf32_install_imm16 (splt, h->plt.offset,
4994
0
             hiadj(plt_index * 4));
4995
0
    nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
4996
0
             (plt_index * 4) & 0xffff);
4997
0
    nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
4998
0
             0xfff4 - h->plt.offset);
4999
0
    got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5000
0
       + got_offset);
5001
5002
    /* Fill in the entry in the global offset table.  There are no
5003
       res_n slots for a shared object PLT, instead the .got.plt entries
5004
       point to the PLT entries.  */
5005
0
    bfd_put_32 (output_bfd,
5006
0
          splt->output_section->vma + splt->output_offset
5007
0
          + h->plt.offset, sgotplt->contents + got_offset);
5008
0
  }
5009
0
      else
5010
0
  {
5011
0
    plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5012
0
    got_offset = (plt_index + 3) * 4;
5013
5014
0
    nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5015
0
    got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5016
0
       + got_offset);
5017
0
    nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5018
0
    nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5019
0
             got_address & 0xffff);
5020
5021
    /* Fill in the entry in the global offset table.  */
5022
0
    bfd_put_32 (output_bfd,
5023
0
          splt->output_section->vma + splt->output_offset
5024
0
          + plt_index * 4, sgotplt->contents + got_offset);
5025
0
  }
5026
5027
      /* Fill in the entry in the .rela.plt section.  */
5028
0
      rela.r_offset = got_address;
5029
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5030
0
      rela.r_addend = 0;
5031
0
      loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5032
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5033
5034
0
      if (!h->def_regular)
5035
0
  {
5036
    /* Mark the symbol as undefined, rather than as defined in
5037
       the .plt section.  Leave the value alone.  */
5038
0
    sym->st_shndx = SHN_UNDEF;
5039
    /* If the symbol is weak, we do need to clear the value.
5040
       Otherwise, the PLT entry would provide a definition for
5041
       the symbol even if the symbol wasn't defined anywhere,
5042
       and so the symbol would never be NULL.  */
5043
0
    if (!h->ref_regular_nonweak)
5044
0
      sym->st_value = 0;
5045
0
  }
5046
0
    }
5047
5048
0
  use_plt = (eh->got_types_used == CALL_USED
5049
0
       && h->plt.offset != (bfd_vma) -1);
5050
5051
0
  if (!use_plt && h->got.offset != (bfd_vma) -1
5052
0
      && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5053
0
      && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5054
0
    {
5055
0
      asection *sgot;
5056
0
      asection *srela;
5057
0
      Elf_Internal_Rela rela;
5058
0
      bfd_byte *loc;
5059
0
      bfd_vma offset;
5060
5061
      /* This symbol has an entry in the global offset table.  Set it
5062
   up.  */
5063
0
      sgot = htab->root.sgot;
5064
0
      srela = htab->root.srelgot;
5065
0
      BFD_ASSERT (sgot != NULL && srela != NULL);
5066
5067
0
      offset = (h->got.offset & ~(bfd_vma) 1);
5068
0
      rela.r_offset = (sgot->output_section->vma
5069
0
           + sgot->output_offset + offset);
5070
5071
      /* If this is a -Bsymbolic link, and the symbol is defined
5072
   locally, we just want to emit a RELATIVE reloc.  Likewise if
5073
   the symbol was forced to be local because of a version file.
5074
   The entry in the global offset table will already have been
5075
   initialized in the relocate_section function.  */
5076
5077
0
      if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
5078
0
  {
5079
0
    rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5080
0
    rela.r_addend = bfd_get_signed_32 (output_bfd,
5081
0
               (sgot->contents + offset));
5082
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5083
0
  }
5084
0
      else
5085
0
  {
5086
0
    bfd_put_32 (output_bfd, (bfd_vma) 0,
5087
0
          sgot->contents + offset);
5088
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5089
0
    rela.r_addend = 0;
5090
0
  }
5091
5092
0
      loc = srela->contents;
5093
0
      loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5094
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5095
0
    }
5096
5097
0
  if (use_plt && h->got.offset != (bfd_vma) -1)
5098
0
    {
5099
0
      bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5100
0
      asection *sgot = htab->root.sgot;
5101
0
      asection *splt = htab->root.splt;
5102
0
      bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5103
0
             + h->plt.offset),
5104
0
      sgot->contents + offset);
5105
0
    }
5106
5107
0
  if (h->needs_copy)
5108
0
    {
5109
0
      asection *s;
5110
0
      Elf_Internal_Rela rela;
5111
0
      bfd_byte *loc;
5112
5113
      /* This symbol needs a copy reloc.  Set it up.  */
5114
0
      BFD_ASSERT (h->dynindx != -1
5115
0
      && (h->root.type == bfd_link_hash_defined
5116
0
          || h->root.type == bfd_link_hash_defweak));
5117
5118
0
      rela.r_offset = (h->root.u.def.value
5119
0
           + h->root.u.def.section->output_section->vma
5120
0
           + h->root.u.def.section->output_offset);
5121
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5122
0
      rela.r_addend = 0;
5123
0
      if (h->root.u.def.section == htab->root.sdynrelro)
5124
0
  s = htab->root.sreldynrelro;
5125
0
      else
5126
0
  s = htab->root.srelbss;
5127
0
      BFD_ASSERT (s != NULL);
5128
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5129
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5130
0
    }
5131
5132
  /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute.  */
5133
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5134
0
      || h == htab->root.hgot
5135
0
      || h == htab->h_gp_got)
5136
0
    sym->st_shndx = SHN_ABS;
5137
5138
0
  return true;
5139
0
}
5140
5141
/* Implement elf_backend_finish_dynamic_sections.  */
5142
static bool
5143
nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5144
             struct bfd_link_info *info)
5145
0
{
5146
0
  asection *sgotplt;
5147
0
  asection *sdyn;
5148
0
  struct elf32_nios2_link_hash_table *htab;
5149
5150
0
  htab = elf32_nios2_hash_table (info);
5151
0
  sgotplt = htab->root.sgotplt;
5152
0
  sdyn = NULL;
5153
5154
0
  if (htab->root.dynamic_sections_created)
5155
0
    {
5156
0
      asection *splt;
5157
0
      Elf32_External_Dyn *dyncon, *dynconend;
5158
5159
0
      splt = htab->root.splt;
5160
0
      sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
5161
0
      BFD_ASSERT (splt != NULL && sdyn != NULL && sgotplt != NULL);
5162
5163
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
5164
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5165
0
      for (; dyncon < dynconend; dyncon++)
5166
0
  {
5167
0
    Elf_Internal_Dyn dyn;
5168
0
    asection *s;
5169
5170
0
    bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
5171
5172
0
    switch (dyn.d_tag)
5173
0
      {
5174
0
      default:
5175
0
        break;
5176
5177
0
      case DT_PLTGOT:
5178
0
        s = htab->root.sgotplt;
5179
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5180
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5181
0
        break;
5182
5183
0
      case DT_JMPREL:
5184
0
        s = htab->root.srelplt;
5185
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5186
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5187
0
        break;
5188
5189
0
      case DT_PLTRELSZ:
5190
0
        s = htab->root.srelplt;
5191
0
        dyn.d_un.d_val = s->size;
5192
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5193
0
        break;
5194
5195
0
      case DT_NIOS2_GP:
5196
0
        s = htab->root.sgotplt;
5197
0
        dyn.d_un.d_ptr
5198
0
    = s->output_section->vma + s->output_offset + 0x7ff0;
5199
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5200
0
        break;
5201
0
      }
5202
0
  }
5203
5204
      /* Fill in the first entry in the procedure linkage table.  */
5205
0
      if (splt->size > 0)
5206
0
  {
5207
0
    bfd_vma got_address = (sgotplt->output_section->vma
5208
0
         + sgotplt->output_offset);
5209
0
    if (bfd_link_pic (info))
5210
0
      {
5211
0
        bfd_vma got_pcrel = got_address - (splt->output_section->vma
5212
0
             + splt->output_offset);
5213
        /* Both GOT and PLT must be aligned to a 16-byte boundary
5214
     for the two loads to share the %hiadj part.  The 4-byte
5215
     offset for nextpc is accounted for in the %lo offsets
5216
     on the loads.  */
5217
0
        BFD_ASSERT ((got_pcrel & 0xf) == 0);
5218
0
        nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
5219
0
        nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5220
0
        nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5221
0
        nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
5222
0
      }
5223
0
    else
5224
0
      {
5225
        /* Divide by 4 here, not 3 because we already corrected for the
5226
     res_N branches.  */
5227
0
        bfd_vma res_size = (splt->size - 28) / 4;
5228
0
        bfd_vma res_start = (splt->output_section->vma
5229
0
           + splt->output_offset);
5230
0
        bfd_vma res_offset;
5231
5232
0
        for (res_offset = 0; res_offset < res_size; res_offset += 4)
5233
0
    bfd_put_32 (output_bfd,
5234
0
          6 | ((res_size - (res_offset + 4)) << 6),
5235
0
          splt->contents + res_offset);
5236
5237
        /* The GOT must be aligned to a 16-byte boundary for the
5238
     two loads to share the same %hiadj part.  */
5239
0
        BFD_ASSERT ((got_address & 0xf) == 0);
5240
5241
0
        nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5242
0
        nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5243
0
        nios2_elf32_install_imm16 (splt, res_size + 4,
5244
0
           res_start & 0xffff);
5245
0
        nios2_elf32_install_imm16 (splt, res_size + 12,
5246
0
           hiadj (got_address));
5247
0
        nios2_elf32_install_imm16 (splt, res_size + 16,
5248
0
           (got_address + 4) & 0xffff);
5249
0
        nios2_elf32_install_imm16 (splt, res_size + 20,
5250
0
           (got_address + 8) & 0xffff);
5251
0
      }
5252
0
  }
5253
0
    }
5254
5255
  /* Fill in the first three entries in the global offset table.  */
5256
0
  if (sgotplt != NULL && sgotplt->size > 0)
5257
0
    {
5258
0
      if (sdyn == NULL)
5259
0
  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5260
0
      else
5261
0
  bfd_put_32 (output_bfd,
5262
0
        sdyn->output_section->vma + sdyn->output_offset,
5263
0
        sgotplt->contents);
5264
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5265
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
5266
5267
0
      if (sgotplt->output_section != bfd_abs_section_ptr)
5268
0
  elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5269
0
    }
5270
5271
0
  return true;
5272
0
}
5273
5274
/* Implement elf_backend_adjust_dynamic_symbol:
5275
   Adjust a symbol defined by a dynamic object and referenced by a
5276
   regular object.  The current definition is in some section of the
5277
   dynamic object, but we're not including those sections.  We have to
5278
   change the definition to something the rest of the link can
5279
   understand.  */
5280
static bool
5281
nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5282
           struct elf_link_hash_entry *h)
5283
0
{
5284
0
  struct elf32_nios2_link_hash_table *htab;
5285
0
  bfd *dynobj;
5286
0
  asection *s, *srel;
5287
0
  unsigned align2;
5288
5289
0
  htab = elf32_nios2_hash_table (info);
5290
0
  dynobj = htab->root.dynobj;
5291
5292
  /* Make sure we know what is going on here.  */
5293
0
  BFD_ASSERT (dynobj != NULL
5294
0
        && (h->needs_plt
5295
0
      || h->is_weakalias
5296
0
      || (h->def_dynamic
5297
0
          && h->ref_regular
5298
0
          && !h->def_regular)));
5299
5300
  /* If this is a function, put it in the procedure linkage table.  We
5301
     will fill in the contents of the procedure linkage table later,
5302
     when we know the address of the .got section.  */
5303
0
  if (h->type == STT_FUNC || h->needs_plt)
5304
0
    {
5305
0
      if (h->plt.refcount <= 0
5306
0
    || SYMBOL_CALLS_LOCAL (info, h)
5307
0
    || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5308
0
        && h->root.type == bfd_link_hash_undefweak))
5309
0
  {
5310
    /* This case can occur if we saw a PLT reloc in an input
5311
       file, but the symbol was never referred to by a dynamic
5312
       object, or if all references were garbage collected.  In
5313
       such a case, we don't actually need to build a procedure
5314
       linkage table, and we can just do a PCREL reloc instead.  */
5315
0
    h->plt.offset = (bfd_vma) -1;
5316
0
    h->needs_plt = 0;
5317
0
  }
5318
5319
0
      return true;
5320
0
    }
5321
5322
  /* Reinitialize the plt offset now that it is not used as a reference
5323
     count any more.  */
5324
0
  h->plt.offset = (bfd_vma) -1;
5325
5326
  /* If this is a weak symbol, and there is a real definition, the
5327
     processor independent code will have arranged for us to see the
5328
     real definition first, and we can just use the same value.  */
5329
0
  if (h->is_weakalias)
5330
0
    {
5331
0
      struct elf_link_hash_entry *def = weakdef (h);
5332
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5333
0
      h->root.u.def.section = def->root.u.def.section;
5334
0
      h->root.u.def.value = def->root.u.def.value;
5335
0
      return true;
5336
0
    }
5337
5338
  /* If there are no non-GOT references, we do not need a copy
5339
     relocation.  */
5340
0
  if (!h->non_got_ref)
5341
0
    return true;
5342
5343
  /* This is a reference to a symbol defined by a dynamic object which
5344
     is not a function.
5345
     If we are creating a shared library, we must presume that the
5346
     only references to the symbol are via the global offset table.
5347
     For such cases we need not do anything here; the relocations will
5348
     be handled correctly by relocate_section.  */
5349
0
  if (bfd_link_pic (info))
5350
0
    return true;
5351
5352
0
  if (h->size == 0)
5353
0
    {
5354
0
      _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5355
0
        h->root.root.string);
5356
0
      return true;
5357
0
    }
5358
5359
  /* We must allocate the symbol in our .dynbss section, which will
5360
     become part of the .bss section of the executable.  There will be
5361
     an entry for this symbol in the .dynsym section.  The dynamic
5362
     object will contain position independent code, so all references
5363
     from the dynamic object to this symbol will go through the global
5364
     offset table.  The dynamic linker will use the .dynsym entry to
5365
     determine the address it must put in the global offset table, so
5366
     both the dynamic object and the regular object will refer to the
5367
     same memory location for the variable.  */
5368
  /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5369
     copy the initial value out of the dynamic object and into the
5370
     runtime process image.  We need to remember the offset into the
5371
     .rela.bss section we are going to use.  */
5372
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
5373
0
    {
5374
0
      s = htab->root.sdynrelro;
5375
0
      srel = htab->root.sreldynrelro;
5376
0
    }
5377
0
  else
5378
0
    {
5379
0
      s = htab->root.sdynbss;
5380
0
      srel = htab->root.srelbss;
5381
0
    }
5382
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5383
0
    {
5384
0
      srel->size += sizeof (Elf32_External_Rela);
5385
0
      h->needs_copy = 1;
5386
0
    }
5387
5388
0
  align2 = bfd_log2 (h->size);
5389
0
  if (align2 > h->root.u.def.section->alignment_power)
5390
0
    align2 = h->root.u.def.section->alignment_power;
5391
5392
  /* Align dynbss.  */
5393
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
5394
0
  if (align2 > bfd_section_alignment (s)
5395
0
      && !bfd_set_section_alignment (s, align2))
5396
0
    return false;
5397
5398
  /* Define the symbol as being at this point in the section.  */
5399
0
  h->root.u.def.section = s;
5400
0
  h->root.u.def.value = s->size;
5401
5402
  /* Increment the section size to make room for the symbol.  */
5403
0
  s->size += h->size;
5404
5405
0
  return true;
5406
0
}
5407
5408
/* Worker function for nios2_elf32_size_dynamic_sections.  */
5409
static bool
5410
adjust_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5411
0
{
5412
0
  struct bfd_link_info *info;
5413
0
  struct elf32_nios2_link_hash_table *htab;
5414
5415
0
  if (h->root.type == bfd_link_hash_indirect)
5416
0
    return true;
5417
5418
0
  if (h->root.type == bfd_link_hash_warning)
5419
    /* When warning symbols are created, they **replace** the "real"
5420
       entry in the hash table, thus we never get to see the real
5421
       symbol in a hash traversal.  So look at it now.  */
5422
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5423
5424
0
  info = (struct bfd_link_info *) inf;
5425
0
  htab = elf32_nios2_hash_table (info);
5426
5427
0
  if (h->plt.offset != (bfd_vma)-1)
5428
0
    h->plt.offset += htab->res_n_size;
5429
0
  if (htab->root.splt == h->root.u.def.section)
5430
0
    h->root.u.def.value += htab->res_n_size;
5431
5432
0
  return true;
5433
0
}
5434
5435
/* Another worker function for nios2_elf32_size_dynamic_sections.
5436
   Allocate space in .plt, .got and associated reloc sections for
5437
   dynamic relocs.  */
5438
static bool
5439
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5440
0
{
5441
0
  struct bfd_link_info *info;
5442
0
  struct elf32_nios2_link_hash_table *htab;
5443
0
  struct elf32_nios2_link_hash_entry *eh;
5444
0
  struct elf_dyn_relocs *p;
5445
0
  int use_plt;
5446
5447
0
  if (h->root.type == bfd_link_hash_indirect)
5448
0
    return true;
5449
5450
0
  if (h->root.type == bfd_link_hash_warning)
5451
    /* When warning symbols are created, they **replace** the "real"
5452
       entry in the hash table, thus we never get to see the real
5453
       symbol in a hash traversal.  So look at it now.  */
5454
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5455
5456
0
  info = (struct bfd_link_info *) inf;
5457
0
  htab = elf32_nios2_hash_table (info);
5458
5459
0
  if (htab->root.dynamic_sections_created
5460
0
      && h->plt.refcount > 0)
5461
0
    {
5462
      /* Make sure this symbol is output as a dynamic symbol.
5463
   Undefined weak syms won't yet be marked as dynamic.  */
5464
0
      if (h->dynindx == -1
5465
0
    && !h->forced_local
5466
0
    && !bfd_elf_link_record_dynamic_symbol (info, h))
5467
0
  return false;
5468
5469
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
5470
0
  {
5471
0
    asection *s = htab->root.splt;
5472
5473
    /* Allocate room for the header.  */
5474
0
    if (s->size == 0)
5475
0
      {
5476
0
        if (bfd_link_pic (info))
5477
0
    s->size = 24;
5478
0
        else
5479
0
    s->size = 28;
5480
0
      }
5481
5482
0
    h->plt.offset = s->size;
5483
5484
    /* If this symbol is not defined in a regular file, and we are
5485
       not generating a shared library, then set the symbol to this
5486
       location in the .plt.  This is required to make function
5487
       pointers compare as equal between the normal executable and
5488
       the shared library.  */
5489
0
    if (! bfd_link_pic (info)
5490
0
        && !h->def_regular)
5491
0
      {
5492
0
        h->root.u.def.section = s;
5493
0
        h->root.u.def.value = h->plt.offset;
5494
0
      }
5495
5496
    /* Make room for this entry.  */
5497
0
    s->size += 12;
5498
5499
    /* We also need to make an entry in the .rela.plt section.  */
5500
0
    htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5501
5502
    /* And the .got.plt section.  */
5503
0
    htab->root.sgotplt->size += 4;
5504
0
  }
5505
0
      else
5506
0
  {
5507
0
    h->plt.offset = (bfd_vma) -1;
5508
0
    h->needs_plt = 0;
5509
0
  }
5510
0
    }
5511
0
  else
5512
0
    {
5513
0
      h->plt.offset = (bfd_vma) -1;
5514
0
      h->needs_plt = 0;
5515
0
    }
5516
5517
0
  eh = (struct elf32_nios2_link_hash_entry *) h;
5518
0
  use_plt = (eh->got_types_used == CALL_USED
5519
0
       && h->plt.offset != (bfd_vma) -1);
5520
5521
0
  if (h->got.refcount > 0)
5522
0
    {
5523
0
      asection *s;
5524
0
      bool dyn;
5525
0
      int tls_type = eh->tls_type;
5526
0
      int indx;
5527
5528
      /* Make sure this symbol is output as a dynamic symbol.
5529
   Undefined weak syms won't yet be marked as dynamic.  */
5530
0
      if (h->dynindx == -1
5531
0
    && !h->forced_local
5532
0
    && !bfd_elf_link_record_dynamic_symbol (info, h))
5533
0
  return false;
5534
5535
0
      s = htab->root.sgot;
5536
0
      h->got.offset = s->size;
5537
5538
0
      if (tls_type == GOT_UNKNOWN)
5539
0
  abort ();
5540
5541
0
      if (tls_type == GOT_NORMAL)
5542
  /* Non-TLS symbols need one GOT slot.  */
5543
0
  s->size += 4;
5544
0
      else
5545
0
  {
5546
0
    if (tls_type & GOT_TLS_GD)
5547
      /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots.  */
5548
0
      s->size += 8;
5549
0
    if (tls_type & GOT_TLS_IE)
5550
      /* R_NIOS2_TLS_IE16 needs one GOT slot.  */
5551
0
      s->size += 4;
5552
0
  }
5553
5554
0
      dyn = htab->root.dynamic_sections_created;
5555
5556
0
      indx = 0;
5557
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5558
0
    && (!bfd_link_pic (info)
5559
0
        || !SYMBOL_REFERENCES_LOCAL (info, h)))
5560
0
  indx = h->dynindx;
5561
5562
0
      if (tls_type != GOT_NORMAL
5563
0
    && (bfd_link_pic (info) || indx != 0)
5564
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5565
0
        || h->root.type != bfd_link_hash_undefweak))
5566
0
  {
5567
0
    if (tls_type & GOT_TLS_IE)
5568
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5569
5570
0
    if (tls_type & GOT_TLS_GD)
5571
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5572
5573
0
    if ((tls_type & GOT_TLS_GD) && indx != 0)
5574
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5575
0
  }
5576
0
      else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5577
0
    || h->root.type != bfd_link_hash_undefweak)
5578
0
         && !use_plt
5579
0
         && (bfd_link_pic (info)
5580
0
       || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5581
0
  htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5582
0
    }
5583
0
  else
5584
0
    h->got.offset = (bfd_vma) -1;
5585
5586
0
  if (h->dyn_relocs == NULL)
5587
0
    return true;
5588
5589
  /* In the shared -Bsymbolic case, discard space allocated for
5590
     dynamic pc-relative relocs against symbols which turn out to be
5591
     defined in regular objects.  For the normal shared case, discard
5592
     space for pc-relative relocs that have become local due to symbol
5593
     visibility changes.  */
5594
5595
0
  if (bfd_link_pic (info))
5596
0
    {
5597
0
      if (h->def_regular
5598
0
    && (h->forced_local || SYMBOLIC_BIND (info, h)))
5599
0
  {
5600
0
    struct elf_dyn_relocs **pp;
5601
5602
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
5603
0
      {
5604
0
        p->count -= p->pc_count;
5605
0
        p->pc_count = 0;
5606
0
        if (p->count == 0)
5607
0
    *pp = p->next;
5608
0
        else
5609
0
    pp = &p->next;
5610
0
      }
5611
0
  }
5612
5613
      /* Also discard relocs on undefined weak syms with non-default
5614
   visibility.  */
5615
0
      if (h->dyn_relocs != NULL
5616
0
    && h->root.type == bfd_link_hash_undefweak)
5617
0
  {
5618
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5619
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
5620
0
      h->dyn_relocs = NULL;
5621
5622
    /* Make sure undefined weak symbols are output as a dynamic
5623
       symbol in PIEs.  */
5624
0
    else if (h->dynindx == -1
5625
0
       && !h->forced_local
5626
0
       && !bfd_elf_link_record_dynamic_symbol (info, h))
5627
0
      return false;
5628
0
  }
5629
0
    }
5630
0
  else
5631
0
    {
5632
      /* For the non-shared case, discard space for relocs against
5633
   symbols which turn out to need copy relocs or are not
5634
   dynamic.  */
5635
5636
0
      if (!h->non_got_ref
5637
0
    && ((h->def_dynamic && !h->def_regular)
5638
0
        || (htab->root.dynamic_sections_created
5639
0
      && (h->root.type == bfd_link_hash_undefweak
5640
0
          || h->root.type == bfd_link_hash_undefined))))
5641
0
  {
5642
    /* Make sure this symbol is output as a dynamic symbol.
5643
       Undefined weak syms won't yet be marked as dynamic.  */
5644
0
    if (h->dynindx == -1
5645
0
        && !h->forced_local
5646
0
        && !bfd_elf_link_record_dynamic_symbol (info, h))
5647
0
      return false;
5648
5649
    /* If that succeeded, we know we'll be keeping all the
5650
       relocs.  */
5651
0
    if (h->dynindx != -1)
5652
0
      goto keep;
5653
0
  }
5654
5655
0
      h->dyn_relocs = NULL;
5656
5657
0
    keep: ;
5658
0
    }
5659
5660
  /* Finally, allocate space.  */
5661
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
5662
0
    {
5663
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
5664
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
5665
0
    }
5666
5667
0
  return true;
5668
0
}
5669
5670
/* Implement elf_backend_size_dynamic_sections:
5671
   Set the sizes of the dynamic sections.  */
5672
static bool
5673
nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5674
           struct bfd_link_info *info)
5675
0
{
5676
0
  bfd *dynobj;
5677
0
  asection *s;
5678
0
  bool relocs;
5679
0
  bfd *ibfd;
5680
0
  struct elf32_nios2_link_hash_table *htab;
5681
5682
0
  htab = elf32_nios2_hash_table (info);
5683
0
  dynobj = htab->root.dynobj;
5684
0
  BFD_ASSERT (dynobj != NULL);
5685
5686
0
  htab->res_n_size = 0;
5687
0
  if (htab->root.dynamic_sections_created)
5688
0
    {
5689
      /* Set the contents of the .interp section to the interpreter.  */
5690
0
      if (bfd_link_executable (info) && !info->nointerp)
5691
0
  {
5692
0
    s = bfd_get_linker_section (dynobj, ".interp");
5693
0
    BFD_ASSERT (s != NULL);
5694
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5695
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5696
0
  }
5697
0
    }
5698
0
  else
5699
0
    {
5700
      /* We may have created entries in the .rela.got section.
5701
   However, if we are not creating the dynamic sections, we will
5702
   not actually use these entries.  Reset the size of .rela.got,
5703
   which will cause it to get stripped from the output file
5704
   below.  */
5705
0
      s = htab->root.srelgot;
5706
0
      if (s != NULL)
5707
0
  s->size = 0;
5708
0
    }
5709
5710
  /* Set up .got offsets for local syms, and space for local dynamic
5711
     relocs.  */
5712
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5713
0
    {
5714
0
      bfd_signed_vma *local_got;
5715
0
      bfd_signed_vma *end_local_got;
5716
0
      char *local_tls_type;
5717
0
      bfd_size_type locsymcount;
5718
0
      Elf_Internal_Shdr *symtab_hdr;
5719
0
      asection *srel;
5720
5721
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5722
0
  continue;
5723
5724
0
      for (s = ibfd->sections; s != NULL; s = s->next)
5725
0
  {
5726
0
    struct elf_dyn_relocs *p;
5727
5728
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5729
0
      {
5730
0
        if (!bfd_is_abs_section (p->sec)
5731
0
      && bfd_is_abs_section (p->sec->output_section))
5732
0
    {
5733
      /* Input section has been discarded, either because
5734
         it is a copy of a linkonce section or due to
5735
         linker script /DISCARD/, so we'll be discarding
5736
         the relocs too.  */
5737
0
    }
5738
0
        else if (p->count != 0)
5739
0
    {
5740
0
      srel = elf_section_data (p->sec)->sreloc;
5741
0
      srel->size += p->count * sizeof (Elf32_External_Rela);
5742
0
    }
5743
0
      }
5744
0
  }
5745
5746
0
      local_got = elf_local_got_refcounts (ibfd);
5747
0
      if (!local_got)
5748
0
  continue;
5749
5750
0
      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5751
0
      locsymcount = symtab_hdr->sh_info;
5752
0
      end_local_got = local_got + locsymcount;
5753
0
      local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5754
0
      s = htab->root.sgot;
5755
0
      srel = htab->root.srelgot;
5756
0
      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5757
0
  {
5758
0
    if (*local_got > 0)
5759
0
      {
5760
0
        *local_got = s->size;
5761
0
        if (*local_tls_type & GOT_TLS_GD)
5762
    /* TLS_GD relocs need an 8-byte structure in the GOT.  */
5763
0
    s->size += 8;
5764
0
        if (*local_tls_type & GOT_TLS_IE)
5765
0
    s->size += 4;
5766
0
        if (*local_tls_type == GOT_NORMAL)
5767
0
    s->size += 4;
5768
5769
0
        if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
5770
0
    srel->size += sizeof (Elf32_External_Rela);
5771
0
      }
5772
0
    else
5773
0
      *local_got = (bfd_vma) -1;
5774
0
  }
5775
0
    }
5776
5777
0
  if (htab->tls_ldm_got.refcount > 0)
5778
0
    {
5779
      /* Allocate two GOT entries and one dynamic relocation (if necessary)
5780
   for R_NIOS2_TLS_LDM16 relocations.  */
5781
0
      htab->tls_ldm_got.offset = htab->root.sgot->size;
5782
0
      htab->root.sgot->size += 8;
5783
0
      if (bfd_link_pic (info))
5784
0
  htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5785
0
    }
5786
0
  else
5787
0
    htab->tls_ldm_got.offset = -1;
5788
5789
  /* Allocate global sym .plt and .got entries, and space for global
5790
     sym dynamic relocs.  */
5791
0
  elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5792
5793
0
  if (htab->root.dynamic_sections_created)
5794
0
    {
5795
      /* If the .got section is more than 0x8000 bytes, we add
5796
   0x8000 to the value of _gp_got, so that 16-bit relocations
5797
   have a greater chance of working. */
5798
0
      if (htab->root.sgot->size >= 0x8000
5799
0
    && htab->h_gp_got->root.u.def.value == 0)
5800
0
  htab->h_gp_got->root.u.def.value = 0x8000;
5801
0
    }
5802
5803
  /* The check_relocs and adjust_dynamic_symbol entry points have
5804
     determined the sizes of the various dynamic sections.  Allocate
5805
     memory for them.  */
5806
0
  relocs = false;
5807
0
  for (s = dynobj->sections; s != NULL; s = s->next)
5808
0
    {
5809
0
      const char *name;
5810
5811
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
5812
0
  continue;
5813
5814
      /* It's OK to base decisions on the section name, because none
5815
   of the dynobj section names depend upon the input files.  */
5816
0
      name = bfd_section_name (s);
5817
5818
0
      if (startswith (name, ".rela"))
5819
0
  {
5820
0
    if (s->size != 0)
5821
0
      {
5822
0
        if (s != htab->root.srelplt)
5823
0
    relocs = true;
5824
5825
        /* We use the reloc_count field as a counter if we need
5826
     to copy relocs into the output file.  */
5827
0
        s->reloc_count = 0;
5828
0
      }
5829
0
  }
5830
0
      else if (s == htab->root.splt)
5831
0
  {
5832
    /* Correct for the number of res_N branches.  */
5833
0
    if (s->size != 0 && !bfd_link_pic (info))
5834
0
      {
5835
0
        htab->res_n_size = (s->size - 28) / 3;
5836
0
        s->size += htab->res_n_size;
5837
0
      }
5838
0
  }
5839
0
      else if (s != htab->sbss
5840
0
         && s != htab->root.sgot
5841
0
         && s != htab->root.sgotplt
5842
0
         && s != htab->root.sdynbss
5843
0
         && s != htab->root.sdynrelro)
5844
  /* It's not one of our sections, so don't allocate space.  */
5845
0
  continue;
5846
5847
0
      if (s->size == 0)
5848
0
  {
5849
0
    s->flags |= SEC_EXCLUDE;
5850
0
    continue;
5851
0
  }
5852
5853
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
5854
0
  continue;
5855
5856
      /* Allocate memory for the section contents.  */
5857
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5858
0
      if (s->contents == NULL)
5859
0
  return false;
5860
0
    }
5861
5862
  /* Adjust dynamic symbols that point to the plt to account for the
5863
     now-known number of resN slots.  */
5864
0
  if (htab->res_n_size)
5865
0
    elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
5866
5867
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
5868
0
}
5869
5870
/* Free the derived linker hash table.  */
5871
static void
5872
nios2_elf32_link_hash_table_free (bfd *obfd)
5873
0
{
5874
0
  struct elf32_nios2_link_hash_table *htab
5875
0
    = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
5876
5877
0
  bfd_hash_table_free (&htab->bstab);
5878
0
  _bfd_elf_link_hash_table_free (obfd);
5879
0
}
5880
5881
/* Implement bfd_elf32_bfd_link_hash_table_create.  */
5882
static struct bfd_link_hash_table *
5883
nios2_elf32_link_hash_table_create (bfd *abfd)
5884
0
{
5885
0
  struct elf32_nios2_link_hash_table *ret;
5886
0
  size_t amt = sizeof (struct elf32_nios2_link_hash_table);
5887
5888
0
  ret = bfd_zmalloc (amt);
5889
0
  if (ret == NULL)
5890
0
    return NULL;
5891
5892
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
5893
0
              link_hash_newfunc,
5894
0
              sizeof (struct
5895
0
                elf32_nios2_link_hash_entry),
5896
0
              NIOS2_ELF_DATA))
5897
0
    {
5898
0
      free (ret);
5899
0
      return NULL;
5900
0
    }
5901
5902
  /* Init the stub hash table too.  */
5903
0
  if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
5904
0
          sizeof (struct elf32_nios2_stub_hash_entry)))
5905
0
    {
5906
0
      _bfd_elf_link_hash_table_free (abfd);
5907
0
      return NULL;
5908
0
    }
5909
0
  ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
5910
5911
0
  return &ret->root.root;
5912
0
}
5913
5914
/* Implement elf_backend_reloc_type_class.  */
5915
static enum elf_reloc_type_class
5916
nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5917
            const asection *rel_sec ATTRIBUTE_UNUSED,
5918
            const Elf_Internal_Rela *rela)
5919
0
{
5920
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
5921
0
    {
5922
0
    case R_NIOS2_RELATIVE:
5923
0
      return reloc_class_relative;
5924
0
    case R_NIOS2_JUMP_SLOT:
5925
0
      return reloc_class_plt;
5926
0
    case R_NIOS2_COPY:
5927
0
      return reloc_class_copy;
5928
0
    default:
5929
0
      return reloc_class_normal;
5930
0
    }
5931
0
}
5932
5933
/* Return 1 if target is one of ours.  */
5934
static bool
5935
is_nios2_elf_target (const struct bfd_target *targ)
5936
0
{
5937
0
  return (targ == &nios2_elf32_le_vec
5938
0
    || targ == &nios2_elf32_be_vec);
5939
0
}
5940
5941
/* Implement elf_backend_add_symbol_hook.
5942
   This hook is called by the linker when adding symbols from an object
5943
   file.  We use it to put .comm items in .sbss, and not .bss.  */
5944
static bool
5945
nios2_elf_add_symbol_hook (bfd *abfd,
5946
         struct bfd_link_info *info,
5947
         Elf_Internal_Sym *sym,
5948
         const char **namep ATTRIBUTE_UNUSED,
5949
         flagword *flagsp ATTRIBUTE_UNUSED,
5950
         asection **secp,
5951
         bfd_vma *valp)
5952
0
{
5953
0
  if (sym->st_shndx == SHN_COMMON
5954
0
      && !bfd_link_relocatable (info)
5955
0
      && sym->st_size <= elf_gp_size (abfd)
5956
0
      && is_nios2_elf_target (info->output_bfd->xvec))
5957
0
    {
5958
      /* Common symbols less than or equal to -G nn bytes are automatically
5959
   put into .sbss.  */
5960
0
      struct elf32_nios2_link_hash_table *htab;
5961
5962
0
      htab = elf32_nios2_hash_table (info);
5963
0
      if (htab->sbss == NULL)
5964
0
  {
5965
0
    flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
5966
5967
0
    if (htab->root.dynobj == NULL)
5968
0
      htab->root.dynobj = abfd;
5969
5970
0
    htab->sbss = bfd_make_section_anyway_with_flags (htab->root.dynobj,
5971
0
                 ".sbss", flags);
5972
0
    if (htab->sbss == NULL)
5973
0
      return false;
5974
0
  }
5975
5976
0
      *secp = htab->sbss;
5977
0
      *valp = sym->st_size;
5978
0
    }
5979
5980
0
  return true;
5981
0
}
5982
5983
/* Implement elf_backend_can_make_relative_eh_frame:
5984
   Decide whether to attempt to turn absptr or lsda encodings in
5985
   shared libraries into pcrel within the given input section.  */
5986
static bool
5987
nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
5988
          struct bfd_link_info *info
5989
          ATTRIBUTE_UNUSED,
5990
          asection *eh_frame_section
5991
          ATTRIBUTE_UNUSED)
5992
0
{
5993
  /* We can't use PC-relative encodings in the .eh_frame section.  */
5994
0
  return false;
5995
0
}
5996
5997
/* Implement elf_backend_special_sections.  */
5998
const struct bfd_elf_special_section elf32_nios2_special_sections[] =
5999
{
6000
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,
6001
    SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6002
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6003
    SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6004
  { NULL,         0,  0, 0,        0 }
6005
};
6006
6007
#define ELF_ARCH      bfd_arch_nios2
6008
#define ELF_TARGET_ID     NIOS2_ELF_DATA
6009
#define ELF_MACHINE_CODE    EM_ALTERA_NIOS2
6010
6011
/* The Nios II MMU uses a 4K page size.  */
6012
6013
#define ELF_MAXPAGESIZE     0x1000
6014
6015
#define bfd_elf32_bfd_link_hash_table_create \
6016
            nios2_elf32_link_hash_table_create
6017
6018
#define bfd_elf32_bfd_merge_private_bfd_data \
6019
            nios2_elf32_merge_private_bfd_data
6020
6021
/* Relocation table lookup macros.  */
6022
6023
#define bfd_elf32_bfd_reloc_type_lookup   nios2_elf32_bfd_reloc_type_lookup
6024
#define bfd_elf32_bfd_reloc_name_lookup   nios2_elf32_bfd_reloc_name_lookup
6025
6026
/* JUMP_TABLE_LINK macros.  */
6027
6028
/* elf_info_to_howto (using RELA relocations).  */
6029
6030
#define elf_info_to_howto     nios2_elf32_info_to_howto
6031
6032
/* elf backend functions.  */
6033
6034
#define elf_backend_can_gc_sections 1
6035
#define elf_backend_can_refcount  1
6036
#define elf_backend_plt_readonly  1
6037
#define elf_backend_want_got_plt  1
6038
#define elf_backend_want_dynrelro 1
6039
#define elf_backend_rela_normal   1
6040
#define elf_backend_dtrel_excludes_plt  1
6041
6042
#define elf_backend_relocate_section    nios2_elf32_relocate_section
6043
#define elf_backend_section_flags   nios2_elf32_section_flags
6044
#define elf_backend_fake_sections   nios2_elf32_fake_sections
6045
#define elf_backend_check_relocs    nios2_elf32_check_relocs
6046
6047
#define elf_backend_gc_mark_hook    nios2_elf32_gc_mark_hook
6048
#define elf_backend_create_dynamic_sections \
6049
            nios2_elf32_create_dynamic_sections
6050
#define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6051
#define elf_backend_finish_dynamic_sections \
6052
            nios2_elf32_finish_dynamic_sections
6053
#define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6054
#define elf_backend_reloc_type_class    nios2_elf32_reloc_type_class
6055
#define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6056
#define elf_backend_add_symbol_hook   nios2_elf_add_symbol_hook
6057
#define elf_backend_copy_indirect_symbol  nios2_elf32_copy_indirect_symbol
6058
#define elf_backend_object_p      nios2_elf32_object_p
6059
6060
#define elf_backend_grok_prstatus   nios2_grok_prstatus
6061
#define elf_backend_grok_psinfo     nios2_grok_psinfo
6062
6063
#undef elf_backend_can_make_relative_eh_frame
6064
#define elf_backend_can_make_relative_eh_frame \
6065
            nios2_elf32_can_make_relative_eh_frame
6066
6067
#define elf_backend_special_sections    elf32_nios2_special_sections
6068
6069
#define TARGET_LITTLE_SYM   nios2_elf32_le_vec
6070
#define TARGET_LITTLE_NAME    "elf32-littlenios2"
6071
#define TARGET_BIG_SYM      nios2_elf32_be_vec
6072
#define TARGET_BIG_NAME     "elf32-bignios2"
6073
6074
#define elf_backend_got_header_size 12
6075
#define elf_backend_default_execstack 0
6076
6077
#include "elf32-target.h"