-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoff-sh.c
3240 lines (2794 loc) · 92 KB
/
coff-sh.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BFD back-end for Renesas Super-H COFF binaries.
Copyright (C) 1993-2024 Free Software Foundation, Inc.
Contributed by Cygnus Support.
Written by Steve Chamberlain, <sac@cygnus.com>.
Relaxing code written by Ian Lance Taylor, <ian@cygnus.com>.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "libiberty.h"
#include "libbfd.h"
#include "bfdlink.h"
#include "coff/sh.h"
#include "coff/internal.h"
#undef bfd_pe_print_pdata
#ifdef COFF_WITH_PE
#include "coff/pe.h"
#ifndef COFF_IMAGE_WITH_PE
static bool sh_align_load_span
(bfd *, asection *, bfd_byte *,
bool (*) (bfd *, asection *, void *, bfd_byte *, bfd_vma),
void *, bfd_vma **, bfd_vma *, bfd_vma, bfd_vma, bool *);
#define _bfd_sh_align_load_span sh_align_load_span
#endif
#define bfd_pe_print_pdata _bfd_pe_print_ce_compressed_pdata
#else
#define bfd_pe_print_pdata NULL
#endif /* COFF_WITH_PE. */
#include "libcoff.h"
/* Internal functions. */
#ifdef COFF_WITH_PE
/* Can't build import tables with 2**4 alignment. */
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
#else
/* Default section alignment to 2**4. */
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 4
#endif
#ifdef COFF_IMAGE_WITH_PE
/* Align PE executables. */
#define COFF_PAGE_SIZE 0x1000
#endif
/* Generate long file names. */
#define COFF_LONG_FILENAMES
#ifdef COFF_WITH_PE
/* Return TRUE if this relocation should
appear in the output .reloc section. */
static bool
in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
reloc_howto_type * howto)
{
return ! howto->pc_relative && howto->type != R_SH_IMAGEBASE;
}
#endif
static bfd_reloc_status_type
sh_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bool
sh_relocate_section (bfd *, struct bfd_link_info *, bfd *, asection *,
bfd_byte *, struct internal_reloc *,
struct internal_syment *, asection **);
static bool
sh_align_loads (bfd *, asection *, struct internal_reloc *,
bfd_byte *, bool *);
/* The supported relocations. There are a lot of relocations defined
in coff/internal.h which we do not expect to ever see. */
static reloc_howto_type sh_coff_howtos[] =
{
EMPTY_HOWTO (0),
EMPTY_HOWTO (1),
#ifdef COFF_WITH_PE
/* Windows CE */
HOWTO (R_SH_IMM32CE, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_imm32ce", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
#else
EMPTY_HOWTO (2),
#endif
EMPTY_HOWTO (3), /* R_SH_PCREL8 */
EMPTY_HOWTO (4), /* R_SH_PCREL16 */
EMPTY_HOWTO (5), /* R_SH_HIGH8 */
EMPTY_HOWTO (6), /* R_SH_IMM24 */
EMPTY_HOWTO (7), /* R_SH_LOW16 */
EMPTY_HOWTO (8),
EMPTY_HOWTO (9), /* R_SH_PCDISP8BY4 */
HOWTO (R_SH_PCDISP8BY2, /* type */
1, /* rightshift */
2, /* size */
8, /* bitsize */
true, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_pcdisp8by2", /* name */
true, /* partial_inplace */
0xff, /* src_mask */
0xff, /* dst_mask */
true), /* pcrel_offset */
EMPTY_HOWTO (11), /* R_SH_PCDISP8 */
HOWTO (R_SH_PCDISP, /* type */
1, /* rightshift */
2, /* size */
12, /* bitsize */
true, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_pcdisp12by2", /* name */
true, /* partial_inplace */
0xfff, /* src_mask */
0xfff, /* dst_mask */
true), /* pcrel_offset */
EMPTY_HOWTO (13),
HOWTO (R_SH_IMM32, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_imm32", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
EMPTY_HOWTO (15),
#ifdef COFF_WITH_PE
HOWTO (R_SH_IMAGEBASE, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"rva32", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
#else
EMPTY_HOWTO (16), /* R_SH_IMM8 */
#endif
EMPTY_HOWTO (17), /* R_SH_IMM8BY2 */
EMPTY_HOWTO (18), /* R_SH_IMM8BY4 */
EMPTY_HOWTO (19), /* R_SH_IMM4 */
EMPTY_HOWTO (20), /* R_SH_IMM4BY2 */
EMPTY_HOWTO (21), /* R_SH_IMM4BY4 */
HOWTO (R_SH_PCRELIMM8BY2, /* type */
1, /* rightshift */
2, /* size */
8, /* bitsize */
true, /* pc_relative */
0, /* bitpos */
complain_overflow_unsigned, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_pcrelimm8by2", /* name */
true, /* partial_inplace */
0xff, /* src_mask */
0xff, /* dst_mask */
true), /* pcrel_offset */
HOWTO (R_SH_PCRELIMM8BY4, /* type */
2, /* rightshift */
2, /* size */
8, /* bitsize */
true, /* pc_relative */
0, /* bitpos */
complain_overflow_unsigned, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_pcrelimm8by4", /* name */
true, /* partial_inplace */
0xff, /* src_mask */
0xff, /* dst_mask */
true), /* pcrel_offset */
HOWTO (R_SH_IMM16, /* type */
0, /* rightshift */
2, /* size */
16, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_imm16", /* name */
true, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_SWITCH16, /* type */
0, /* rightshift */
2, /* size */
16, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_switch16", /* name */
true, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_SWITCH32, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_switch32", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_USES, /* type */
0, /* rightshift */
2, /* size */
16, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_uses", /* name */
true, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_COUNT, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_count", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_ALIGN, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_align", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_CODE, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_code", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_DATA, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_data", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_LABEL, /* type */
0, /* rightshift */
4, /* size */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_label", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false), /* pcrel_offset */
HOWTO (R_SH_SWITCH8, /* type */
0, /* rightshift */
1, /* size */
8, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
sh_reloc, /* special_function */
"r_switch8", /* name */
true, /* partial_inplace */
0xff, /* src_mask */
0xff, /* dst_mask */
false) /* pcrel_offset */
};
#define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
/* Check for a bad magic number. */
#define BADMAG(x) SHBADMAG(x)
/* Customize coffcode.h (this is not currently used). */
#define SH 1
/* FIXME: This should not be set here. */
#define __A_MAGIC_SET__
#ifndef COFF_WITH_PE
/* Swap the r_offset field in and out. */
#define SWAP_IN_RELOC_OFFSET H_GET_32
#define SWAP_OUT_RELOC_OFFSET H_PUT_32
/* Swap out extra information in the reloc structure. */
#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
do \
{ \
dst->r_stuff[0] = 'S'; \
dst->r_stuff[1] = 'C'; \
} \
while (0)
#endif
/* Get the value of a symbol, when performing a relocation. */
static long
get_symbol_value (asymbol *symbol)
{
bfd_vma relocation;
if (bfd_is_com_section (symbol->section))
relocation = 0;
else
relocation = (symbol->value +
symbol->section->output_section->vma +
symbol->section->output_offset);
return relocation;
}
#ifdef COFF_WITH_PE
/* Convert an rtype to howto for the COFF backend linker.
Copied from coff-i386. */
#define coff_rtype_to_howto coff_sh_rtype_to_howto
static reloc_howto_type *
coff_sh_rtype_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
asection * sec,
struct internal_reloc * rel,
struct coff_link_hash_entry * h,
struct internal_syment * sym,
bfd_vma * addendp)
{
reloc_howto_type * howto;
howto = sh_coff_howtos + rel->r_type;
*addendp = 0;
if (howto->pc_relative)
*addendp += sec->vma;
if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
{
/* This is a common symbol. The section contents include the
size (sym->n_value) as an addend. The relocate_section
function will be adding in the final value of the symbol. We
need to subtract out the current size in order to get the
correct result. */
BFD_ASSERT (h != NULL);
}
if (howto->pc_relative)
{
*addendp -= 4;
/* If the symbol is defined, then the generic code is going to
add back the symbol value in order to cancel out an
adjustment it made to the addend. However, we set the addend
to 0 at the start of this function. We need to adjust here,
to avoid the adjustment the generic code will make. FIXME:
This is getting a bit hackish. */
if (sym != NULL && sym->n_scnum != 0)
*addendp -= sym->n_value;
}
if (rel->r_type == R_SH_IMAGEBASE)
*addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
return howto;
}
#endif /* COFF_WITH_PE */
/* This structure is used to map BFD reloc codes to SH PE relocs. */
struct shcoff_reloc_map
{
bfd_reloc_code_real_type bfd_reloc_val;
unsigned char shcoff_reloc_val;
};
#ifdef COFF_WITH_PE
/* An array mapping BFD reloc codes to SH PE relocs. */
static const struct shcoff_reloc_map sh_reloc_map[] =
{
{ BFD_RELOC_32, R_SH_IMM32CE },
{ BFD_RELOC_RVA, R_SH_IMAGEBASE },
{ BFD_RELOC_CTOR, R_SH_IMM32CE },
};
#else
/* An array mapping BFD reloc codes to SH PE relocs. */
static const struct shcoff_reloc_map sh_reloc_map[] =
{
{ BFD_RELOC_32, R_SH_IMM32 },
{ BFD_RELOC_CTOR, R_SH_IMM32 },
};
#endif
/* Given a BFD reloc code, return the howto structure for the
corresponding SH PE reloc. */
#define coff_bfd_reloc_type_lookup sh_coff_reloc_type_lookup
#define coff_bfd_reloc_name_lookup sh_coff_reloc_name_lookup
static reloc_howto_type *
sh_coff_reloc_type_lookup (bfd *abfd,
bfd_reloc_code_real_type code)
{
unsigned int i;
for (i = ARRAY_SIZE (sh_reloc_map); i--;)
if (sh_reloc_map[i].bfd_reloc_val == code)
return &sh_coff_howtos[(int) sh_reloc_map[i].shcoff_reloc_val];
_bfd_error_handler (_("%pB: unsupported relocation type %#x"),
abfd, (unsigned int) code);
return NULL;
}
static reloc_howto_type *
sh_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
const char *r_name)
{
unsigned int i;
for (i = 0; i < sizeof (sh_coff_howtos) / sizeof (sh_coff_howtos[0]); i++)
if (sh_coff_howtos[i].name != NULL
&& strcasecmp (sh_coff_howtos[i].name, r_name) == 0)
return &sh_coff_howtos[i];
return NULL;
}
/* This macro is used in coffcode.h to get the howto corresponding to
an internal reloc. */
#define RTYPE2HOWTO(relent, internal) \
((relent)->howto = \
((internal)->r_type < SH_COFF_HOWTO_COUNT \
? &sh_coff_howtos[(internal)->r_type] \
: (reloc_howto_type *) NULL))
/* This is the same as the macro in coffcode.h, except that it copies
r_offset into reloc_entry->addend for some relocs. */
#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
{ \
coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
coffsym = (obj_symbols (abfd) \
+ (cache_ptr->sym_ptr_ptr - symbols)); \
else if (ptr) \
coffsym = coff_symbol_from (ptr); \
if (coffsym != (coff_symbol_type *) NULL \
&& coffsym->native->u.syment.n_scnum == 0) \
cache_ptr->addend = 0; \
else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
&& ptr->section != (asection *) NULL) \
cache_ptr->addend = - (ptr->section->vma \
+ COFF_PE_ADDEND_BIAS (ptr)); \
else \
cache_ptr->addend = 0; \
if ((reloc).r_type == R_SH_SWITCH8 \
|| (reloc).r_type == R_SH_SWITCH16 \
|| (reloc).r_type == R_SH_SWITCH32 \
|| (reloc).r_type == R_SH_USES \
|| (reloc).r_type == R_SH_COUNT \
|| (reloc).r_type == R_SH_ALIGN) \
cache_ptr->addend = (reloc).r_offset; \
}
/* This is the howto function for the SH relocations. */
static bfd_reloc_status_type
sh_reloc (bfd * abfd,
arelent * reloc_entry,
asymbol * symbol_in,
void * data,
asection * input_section,
bfd * output_bfd,
char ** error_message ATTRIBUTE_UNUSED)
{
bfd_vma insn;
bfd_vma sym_value;
unsigned short r_type;
bfd_vma addr = reloc_entry->address;
bfd_byte *hit_data = addr + (bfd_byte *) data;
r_type = reloc_entry->howto->type;
if (output_bfd != NULL)
{
/* Partial linking--do nothing. */
reloc_entry->address += input_section->output_offset;
return bfd_reloc_ok;
}
/* Almost all relocs have to do with relaxing. If any work must be
done for them, it has been done in sh_relax_section. */
if (r_type != R_SH_IMM32
#ifdef COFF_WITH_PE
&& r_type != R_SH_IMM32CE
&& r_type != R_SH_IMAGEBASE
#endif
&& (r_type != R_SH_PCDISP
|| (symbol_in->flags & BSF_LOCAL) != 0))
return bfd_reloc_ok;
if (symbol_in != NULL
&& bfd_is_und_section (symbol_in->section))
return bfd_reloc_undefined;
if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
addr))
return bfd_reloc_outofrange;
sym_value = get_symbol_value (symbol_in);
switch (r_type)
{
case R_SH_IMM32:
#ifdef COFF_WITH_PE
case R_SH_IMM32CE:
#endif
insn = bfd_get_32 (abfd, hit_data);
insn += sym_value + reloc_entry->addend;
bfd_put_32 (abfd, insn, hit_data);
break;
#ifdef COFF_WITH_PE
case R_SH_IMAGEBASE:
insn = bfd_get_32 (abfd, hit_data);
insn += sym_value + reloc_entry->addend;
insn -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
bfd_put_32 (abfd, insn, hit_data);
break;
#endif
case R_SH_PCDISP:
insn = bfd_get_16 (abfd, hit_data);
sym_value += reloc_entry->addend;
sym_value -= (input_section->output_section->vma
+ input_section->output_offset
+ addr
+ 4);
sym_value += (((insn & 0xfff) ^ 0x800) - 0x800) << 1;
insn = (insn & 0xf000) | ((sym_value >> 1) & 0xfff);
bfd_put_16 (abfd, insn, hit_data);
if (sym_value + 0x1000 >= 0x2000 || (sym_value & 1) != 0)
return bfd_reloc_overflow;
break;
default:
abort ();
break;
}
return bfd_reloc_ok;
}
#define coff_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
/* We can do relaxing. */
#define coff_bfd_relax_section sh_relax_section
/* We use the special COFF backend linker. */
#define coff_relocate_section sh_relocate_section
/* When relaxing, we need to use special code to get the relocated
section contents. */
#define coff_bfd_get_relocated_section_contents \
sh_coff_get_relocated_section_contents
#include "coffcode.h"
static bool
sh_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
/* This function handles relaxing on the SH.
Function calls on the SH look like this:
movl L1,r0
...
jsr @r0
...
L1:
.long function
The compiler and assembler will cooperate to create R_SH_USES
relocs on the jsr instructions. The r_offset field of the
R_SH_USES reloc is the PC relative offset to the instruction which
loads the register (the r_offset field is computed as though it
were a jump instruction, so the offset value is actually from four
bytes past the instruction). The linker can use this reloc to
determine just which function is being called, and thus decide
whether it is possible to replace the jsr with a bsr.
If multiple function calls are all based on a single register load
(i.e., the same function is called multiple times), the compiler
guarantees that each function call will have an R_SH_USES reloc.
Therefore, if the linker is able to convert each R_SH_USES reloc
which refers to that address, it can safely eliminate the register
load.
When the assembler creates an R_SH_USES reloc, it examines it to
determine which address is being loaded (L1 in the above example).
It then counts the number of references to that address, and
creates an R_SH_COUNT reloc at that address. The r_offset field of
the R_SH_COUNT reloc will be the number of references. If the
linker is able to eliminate a register load, it can use the
R_SH_COUNT reloc to see whether it can also eliminate the function
address.
SH relaxing also handles another, unrelated, matter. On the SH, if
a load or store instruction is not aligned on a four byte boundary,
the memory cycle interferes with the 32 bit instruction fetch,
causing a one cycle bubble in the pipeline. Therefore, we try to
align load and store instructions on four byte boundaries if we
can, by swapping them with one of the adjacent instructions. */
static bool
sh_relax_section (bfd *abfd,
asection *sec,
struct bfd_link_info *link_info,
bool *again)
{
struct internal_reloc *internal_relocs;
bool have_code;
struct internal_reloc *irel, *irelend;
bfd_byte *contents = NULL;
*again = false;
if (bfd_link_relocatable (link_info)
|| (sec->flags & SEC_HAS_CONTENTS) == 0
|| (sec->flags & SEC_RELOC) == 0
|| sec->reloc_count == 0)
return true;
if (coff_section_data (abfd, sec) == NULL)
{
size_t amt = sizeof (struct coff_section_tdata);
sec->used_by_bfd = bfd_zalloc (abfd, amt);
if (sec->used_by_bfd == NULL)
return false;
}
internal_relocs = (_bfd_coff_read_internal_relocs
(abfd, sec, link_info->keep_memory,
(bfd_byte *) NULL, false,
(struct internal_reloc *) NULL));
if (internal_relocs == NULL)
goto error_return;
have_code = false;
irelend = internal_relocs + sec->reloc_count;
for (irel = internal_relocs; irel < irelend; irel++)
{
bfd_vma laddr, paddr, symval;
unsigned short insn;
struct internal_reloc *irelfn, *irelscan, *irelcount;
struct internal_syment sym;
bfd_signed_vma foff;
if (irel->r_type == R_SH_CODE)
have_code = true;
if (irel->r_type != R_SH_USES)
continue;
/* Get the section contents. */
if (contents == NULL)
{
if (coff_section_data (abfd, sec)->contents != NULL)
contents = coff_section_data (abfd, sec)->contents;
else
{
if (!bfd_malloc_and_get_section (abfd, sec, &contents))
goto error_return;
}
}
/* The r_offset field of the R_SH_USES reloc will point us to
the register load. The 4 is because the r_offset field is
computed as though it were a jump offset, which are based
from 4 bytes after the jump instruction. */
laddr = irel->r_vaddr - sec->vma + 4;
/* Careful to sign extend the 32-bit offset. */
laddr += ((irel->r_offset & 0xffffffff) ^ 0x80000000) - 0x80000000;
if (laddr >= sec->size)
{
/* xgettext: c-format */
_bfd_error_handler
(_("%pB: %#" PRIx64 ": warning: bad R_SH_USES offset"),
abfd, (uint64_t) irel->r_vaddr);
continue;
}
insn = bfd_get_16 (abfd, contents + laddr);
/* If the instruction is not mov.l NN,rN, we don't know what to do. */
if ((insn & 0xf000) != 0xd000)
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: %#" PRIx64 ": warning: R_SH_USES points to unrecognized insn %#x"),
abfd, (uint64_t) irel->r_vaddr, insn);
continue;
}
/* Get the address from which the register is being loaded. The
displacement in the mov.l instruction is quadrupled. It is a
displacement from four bytes after the movl instruction, but,
before adding in the PC address, two least significant bits
of the PC are cleared. We assume that the section is aligned
on a four byte boundary. */
paddr = insn & 0xff;
paddr *= 4;
paddr += (laddr + 4) &~ (bfd_vma) 3;
if (paddr >= sec->size)
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: %#" PRIx64 ": warning: bad R_SH_USES load offset"),
abfd, (uint64_t) irel->r_vaddr);
continue;
}
/* Get the reloc for the address from which the register is
being loaded. This reloc will tell us which function is
actually being called. */
paddr += sec->vma;
for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
if (irelfn->r_vaddr == paddr
#ifdef COFF_WITH_PE
&& (irelfn->r_type == R_SH_IMM32
|| irelfn->r_type == R_SH_IMM32CE
|| irelfn->r_type == R_SH_IMAGEBASE)
#else
&& irelfn->r_type == R_SH_IMM32
#endif
)
break;
if (irelfn >= irelend)
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: %#" PRIx64 ": warning: could not find expected reloc"),
abfd, (uint64_t) paddr);
continue;
}
/* Get the value of the symbol referred to by the reloc. */
if (! _bfd_coff_get_external_symbols (abfd))
goto error_return;
bfd_coff_swap_sym_in (abfd,
((bfd_byte *) obj_coff_external_syms (abfd)
+ (irelfn->r_symndx
* bfd_coff_symesz (abfd))),
&sym);
if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: %#" PRIx64 ": warning: symbol in unexpected section"),
abfd, (uint64_t) paddr);
continue;
}
if (sym.n_sclass != C_EXT)
{
symval = (sym.n_value
- sec->vma
+ sec->output_section->vma
+ sec->output_offset);
}
else
{
struct coff_link_hash_entry *h;
h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
BFD_ASSERT (h != NULL);
if (h->root.type != bfd_link_hash_defined
&& h->root.type != bfd_link_hash_defweak)
{
/* This appears to be a reference to an undefined
symbol. Just ignore it--it will be caught by the
regular reloc processing. */
continue;
}
symval = (h->root.u.def.value
+ h->root.u.def.section->output_section->vma
+ h->root.u.def.section->output_offset);
}
symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
/* See if this function call can be shortened. */
foff = (symval
- (irel->r_vaddr
- sec->vma
+ sec->output_section->vma
+ sec->output_offset
+ 4));
if (foff < -0x1000 || foff >= 0x1000)
{
/* After all that work, we can't shorten this function call. */
continue;
}
/* Shorten the function call. */
/* For simplicity of coding, we are going to modify the section
contents, the section relocs, and the BFD symbol table. We
must tell the rest of the code not to free up this
information. It would be possible to instead create a table
of changes which have to be made, as is done in coff-mips.c;
that would be more work, but would require less memory when
the linker is run. */
coff_section_data (abfd, sec)->relocs = internal_relocs;
coff_section_data (abfd, sec)->contents = contents;
/* Replace the jsr with a bsr. */
/* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
replace the jsr with a bsr. */
irel->r_type = R_SH_PCDISP;
irel->r_symndx = irelfn->r_symndx;
if (sym.n_sclass != C_EXT)
{
/* If this needs to be changed because of future relaxing,
it will be handled here like other internal PCDISP
relocs. */
bfd_put_16 (abfd,
(bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
contents + irel->r_vaddr - sec->vma);
}
else
{
/* We can't fully resolve this yet, because the external
symbol value may be changed by future relaxing. We let
the final link phase handle it. */
bfd_put_16 (abfd, (bfd_vma) 0xb000,
contents + irel->r_vaddr - sec->vma);
}
/* See if there is another R_SH_USES reloc referring to the same
register load. */
for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
if (irelscan->r_type == R_SH_USES
&& laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
break;
if (irelscan < irelend)
{
/* Some other function call depends upon this register load,
and we have not yet converted that function call.
Indeed, we may never be able to convert it. There is
nothing else we can do at this point. */
continue;
}
/* Look for a R_SH_COUNT reloc on the location where the
function address is stored. Do this before deleting any
bytes, to avoid confusion about the address. */
for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
if (irelcount->r_vaddr == paddr
&& irelcount->r_type == R_SH_COUNT)
break;
/* Delete the register load. */
if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
goto error_return;
/* That will change things, so, just in case it permits some
other function call to come within range, we should relax
again. Note that this is not required, and it may be slow. */
*again = true;
/* Now check whether we got a COUNT reloc. */
if (irelcount >= irelend)
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: %#" PRIx64 ": warning: could not find expected COUNT reloc"),
abfd, (uint64_t) paddr);
continue;
}
/* The number of uses is stored in the r_offset field. We've
just deleted one. */
if (irelcount->r_offset == 0)
{
/* xgettext: c-format */
_bfd_error_handler (_("%pB: %#" PRIx64 ": warning: bad count"),
abfd, (uint64_t) paddr);
continue;
}
--irelcount->r_offset;
/* If there are no more uses, we can delete the address. Reload
the address from irelfn, in case it was changed by the
previous call to sh_relax_delete_bytes. */
if (irelcount->r_offset == 0)
{
if (! sh_relax_delete_bytes (abfd, sec,
irelfn->r_vaddr - sec->vma, 4))
goto error_return;
}
/* We've done all we can with that function call. */