Skip to content
This repository has been archived by the owner on Jul 4, 2021. It is now read-only.

Latest commit

 

History

History
4113 lines (3458 loc) · 117 KB

elf32-m68k.c

File metadata and controls

4113 lines (3458 loc) · 117 KB
 
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
/* Motorola 68k series support for 32-bit ELF
Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 "bfdlink.h"
#include "libbfd.h"
#include "elf-bfd.h"
#include "elf/m68k.h"
#include "opcode/m68k.h"
static reloc_howto_type *reloc_type_lookup
PARAMS ((bfd *, bfd_reloc_code_real_type));
static void rtype_to_howto
PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
PARAMS ((bfd *));
static bfd_boolean elf_m68k_check_relocs
PARAMS ((bfd *, struct bfd_link_info *, asection *,
const Elf_Internal_Rela *));
static bfd_boolean elf_m68k_adjust_dynamic_symbol
PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
static bfd_boolean elf_m68k_size_dynamic_sections
PARAMS ((bfd *, struct bfd_link_info *));
static bfd_boolean elf_m68k_discard_copies
PARAMS ((struct elf_link_hash_entry *, PTR));
static bfd_boolean elf_m68k_relocate_section
PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
static bfd_boolean elf_m68k_finish_dynamic_symbol
PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
Elf_Internal_Sym *));
static bfd_boolean elf_m68k_finish_dynamic_sections
PARAMS ((bfd *, struct bfd_link_info *));
static bfd_boolean elf32_m68k_set_private_flags
PARAMS ((bfd *, flagword));
static bfd_boolean elf32_m68k_merge_private_bfd_data
PARAMS ((bfd *, bfd *));
static bfd_boolean elf32_m68k_print_private_bfd_data
PARAMS ((bfd *, PTR));
static enum elf_reloc_type_class elf32_m68k_reloc_type_class
PARAMS ((const Elf_Internal_Rela *));
static reloc_howto_type howto_table[] = {
HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_GLOB_DAT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_GLOB_DAT", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_JMP_SLOT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_JMP_SLOT", FALSE, 0, 0xffffffff,FALSE),
HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
/* GNU extension to record C++ vtable hierarchy. */
HOWTO (R_68K_GNU_VTINHERIT, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
0, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* complain_on_overflow */
NULL, /* special_function */
"R_68K_GNU_VTINHERIT", /* name */
FALSE, /* partial_inplace */
0, /* src_mask */
0, /* dst_mask */
FALSE),
/* GNU extension to record C++ vtable member usage. */
HOWTO (R_68K_GNU_VTENTRY, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
0, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* complain_on_overflow */
_bfd_elf_rel_vtable_reloc_fn, /* special_function */
"R_68K_GNU_VTENTRY", /* name */
FALSE, /* partial_inplace */
0, /* src_mask */
0, /* dst_mask */
FALSE),
};
static void
rtype_to_howto (abfd, cache_ptr, dst)
bfd *abfd ATTRIBUTE_UNUSED;
arelent *cache_ptr;
Elf_Internal_Rela *dst;
{
BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
}
#define elf_info_to_howto rtype_to_howto
static const struct
{
bfd_reloc_code_real_type bfd_val;
int elf_val;
} reloc_map[] = {
{ BFD_RELOC_NONE, R_68K_NONE },
{ BFD_RELOC_32, R_68K_32 },
{ BFD_RELOC_16, R_68K_16 },
{ BFD_RELOC_8, R_68K_8 },
{ BFD_RELOC_32_PCREL, R_68K_PC32 },
{ BFD_RELOC_16_PCREL, R_68K_PC16 },
{ BFD_RELOC_8_PCREL, R_68K_PC8 },
{ BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
{ BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
{ BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
{ BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
{ BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
{ BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
{ BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
{ BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
{ BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
{ BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
{ BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
{ BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
{ BFD_RELOC_NONE, R_68K_COPY },
{ BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
{ BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
{ BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
{ BFD_RELOC_CTOR, R_68K_32 },
{ BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
{ BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
};
static reloc_howto_type *
reloc_type_lookup (abfd, code)
bfd *abfd ATTRIBUTE_UNUSED;
bfd_reloc_code_real_type code;
{
unsigned int i;
for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
{
if (reloc_map[i].bfd_val == code)
return &howto_table[reloc_map[i].elf_val];
}
return 0;
}
static reloc_howto_type *
reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
{
unsigned int i;
for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
if (howto_table[i].name != NULL
&& strcasecmp (howto_table[i].name, r_name) == 0)
return &howto_table[i];
return NULL;
}
#define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
#define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
#define ELF_ARCH bfd_arch_m68k
/* Functions for the m68k ELF linker. */
/* The name of the dynamic interpreter. This is put in the .interp
section. */
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
/* Describes one of the various PLT styles. */
struct elf_m68k_plt_info
{
/* The size of each PLT entry. */
bfd_vma size;
/* The template for the first PLT entry. */
const bfd_byte *plt0_entry;
/* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
The comments by each member indicate the value that the relocation
is against. */
struct {
unsigned int got4; /* .got + 4 */
unsigned int got8; /* .got + 8 */
} plt0_relocs;
/* The template for a symbol's PLT entry. */
const bfd_byte *symbol_entry;
/* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
The comments by each member indicate the value that the relocation
is against. */
struct {
unsigned int got; /* the symbol's .got.plt entry */
unsigned int plt; /* .plt */
} symbol_relocs;
/* The offset of the resolver stub from the start of SYMBOL_ENTRY.
The stub starts with "move.l #relocoffset,%d0". */
bfd_vma symbol_resolve_entry;
};
/* The size in bytes of an entry in the procedure linkage table. */
#define PLT_ENTRY_SIZE 20
/* The first entry in a procedure linkage table looks like this. See
the SVR4 ABI m68k supplement to see how this works. */
static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
{
0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
0, 0, 0, 2, /* + (.got + 4) - . */
0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
0, 0, 0, 2, /* + (.got + 8) - . */
0, 0, 0, 0 /* pad out to 20 bytes. */
};
/* Subsequent entries in a procedure linkage table look like this. */
static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
{
0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
0, 0, 0, 2, /* + (.got.plt entry) - . */
0x2f, 0x3c, /* move.l #offset,-(%sp) */
0, 0, 0, 0, /* + reloc index */
0x60, 0xff, /* bra.l .plt */
0, 0, 0, 0 /* + .plt - . */
};
static const struct elf_m68k_plt_info elf_m68k_plt_info = {
PLT_ENTRY_SIZE,
elf_m68k_plt0_entry, { 4, 12 },
elf_m68k_plt_entry, { 4, 16 }, 8
};
#define ISAB_PLT_ENTRY_SIZE 24
static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
{
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* + (.got + 4) - . */
0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* + (.got + 8) - . */
0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
0x4e, 0xd0, /* jmp (%a0) */
0x4e, 0x71 /* nop */
};
/* Subsequent entries in a procedure linkage table look like this. */
static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
{
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* + (.got.plt entry) - . */
0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
0x4e, 0xd0, /* jmp (%a0) */
0x2f, 0x3c, /* move.l #offset,-(%sp) */
0, 0, 0, 0, /* + reloc index */
0x60, 0xff, /* bra.l .plt */
0, 0, 0, 0 /* + .plt - . */
};
static const struct elf_m68k_plt_info elf_isab_plt_info = {
ISAB_PLT_ENTRY_SIZE,
elf_isab_plt0_entry, { 2, 12 },
elf_isab_plt_entry, { 2, 20 }, 12
};
#define ISAC_PLT_ENTRY_SIZE 24
static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
{
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* replaced with .got + 4 - . */
0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* replaced with .got + 8 - . */
0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
0x4e, 0xd0, /* jmp (%a0) */
0x4e, 0x71 /* nop */
};
/* Subsequent entries in a procedure linkage table look like this. */
static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
{
0x20, 0x3c, /* move.l #offset,%d0 */
0, 0, 0, 0, /* replaced with (.got entry) - . */
0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
0x4e, 0xd0, /* jmp (%a0) */
0x2f, 0x3c, /* move.l #offset,-(%sp) */
0, 0, 0, 0, /* replaced with offset into relocation table */
0x61, 0xff, /* bsr.l .plt */
0, 0, 0, 0 /* replaced with .plt - . */
};
static const struct elf_m68k_plt_info elf_isac_plt_info = {
ISAC_PLT_ENTRY_SIZE,
elf_isac_plt0_entry, { 2, 12},
elf_isac_plt_entry, { 2, 20 }, 12
};
#define CPU32_PLT_ENTRY_SIZE 24
/* Procedure linkage table entries for the cpu32 */
static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
{
0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
0, 0, 0, 2, /* + (.got + 4) - . */
0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
0, 0, 0, 2, /* + (.got + 8) - . */
0x4e, 0xd1, /* jmp %a1@ */
0, 0, 0, 0, /* pad out to 24 bytes. */
0, 0
};
static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
{
0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
0, 0, 0, 2, /* + (.got.plt entry) - . */
0x4e, 0xd1, /* jmp %a1@ */
0x2f, 0x3c, /* move.l #offset,-(%sp) */
0, 0, 0, 0, /* + reloc index */
0x60, 0xff, /* bra.l .plt */
0, 0, 0, 0, /* + .plt - . */
0, 0
};
static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
CPU32_PLT_ENTRY_SIZE,
elf_cpu32_plt0_entry, { 4, 12 },
elf_cpu32_plt_entry, { 4, 18 }, 10
};
/* The m68k linker needs to keep track of the number of relocs that it
decides to copy in check_relocs for each symbol. This is so that it
can discard PC relative relocs if it doesn't need them when linking
with -Bsymbolic. We store the information in a field extending the
regular ELF linker hash table. */
/* This structure keeps track of the number of PC relative relocs we have
copied for a given symbol. */
struct elf_m68k_pcrel_relocs_copied
{
/* Next section. */
struct elf_m68k_pcrel_relocs_copied *next;
/* A section in dynobj. */
asection *section;
/* Number of relocs copied in this section. */
bfd_size_type count;
};
/* Forward declaration. */
struct elf_m68k_got_entry;
/* m68k ELF linker hash entry. */
struct elf_m68k_link_hash_entry
{
struct elf_link_hash_entry root;
/* Number of PC relative relocs copied for this symbol. */
struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
/* Key to got_entries. */
unsigned long got_entry_key;
/* List of GOT entries for this symbol. This list is build during
offset finalization and is used within elf_m68k_finish_dynamic_symbol
to traverse all GOT entries for a particular symbol.
??? We could've used root.got.glist field instead, but having
a separate field is cleaner. */
struct elf_m68k_got_entry *glist;
};
#define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
/* Key part of GOT entry in hashtable. */
struct elf_m68k_got_entry_key
{
/* BFD in which this symbol was defined. NULL for global symbols. */
const bfd *bfd;
/* Symbol index. Either local symbol index or h->got_entry_key. */
unsigned long symndx;
};
/* Entry of the GOT. */
struct elf_m68k_got_entry
{
/* GOT entries are put into a got->entries hashtable. This is the key. */
struct elf_m68k_got_entry_key key_;
/* GOT entry data. We need s1 before offset finalization and s2 after. */
union
{
struct
{
/* Number of times this entry is referenced. It is used to
filter out unnecessary GOT slots in elf_m68k_gc_sweep_hook. */
bfd_vma refcount;
/* Type is one of R_68K_GOT8O, R_68K_GOT16O or R_68K_GOT32O. */
int type;
} s1;
struct
{
/* Offset from the start of .got section. To calculate offset relative
to GOT pointer one should substract got->offset from this value. */
bfd_vma offset;
/* Pointer to the next GOT entry for this global symbol.
Symbols have at most one entry in one GOT, but might
have entries in more than one GOT.
Root of this list is h->glist.
NULL for local symbols. */
struct elf_m68k_got_entry *next;
} s2;
} u;
};
/* Data structure representing a single GOT. */
struct elf_m68k_got
{
/* Hashtable of 'struct elf_m68k_got_entry's.
Starting size of this table is the maximum number of
R_68K_GOT8O entries. */
htab_t entries;
/* Number of R_68K_GOT8O entries in this GOT.
This is used to detect the overflow of number of such entries. */
bfd_vma rel_8o_n_entries;
/* Cumulative count of R_68K_GOT8O and R_68K_GOT16O entries in this GOT.
This is used to detect the overflow of number of such entries. */
bfd_vma rel_8o_16o_n_entries;
/* Number of local (entry->key_.h == NULL) entries in this GOT.
This is only used to properly calculate size of .rela.got section;
see elf_m68k_partition_multi_got. */
bfd_vma local_n_entries;
/* Offset of this GOT relative to beginning of .got section. */
bfd_vma offset;
};
/* BFD and its GOT. This is an entry in multi_got->bfd2got hashtable. */
struct elf_m68k_bfd2got_entry
{
/* BFD. */
const bfd *bfd;
/* Assigned GOT. Before partitioning multi-GOT each BFD has its own
GOT structure. After partitioning several BFD's might [and often do]
share a single GOT. */
struct elf_m68k_got *got;
};
/* The main data structure holding all the pieces. */
struct elf_m68k_multi_got
{
/* Hashtable mapping each BFD to its GOT. If a BFD doesn't have an entry
here, then it doesn't need a GOT (this includes the case of a BFD
having an empty GOT).
??? This hashtable can be replaced by an array indexed by bfd->id. */
htab_t bfd2got;
/* Next symndx to assign a global symbol.
h->got_entry_key is initialized from this counter. */
unsigned long global_symndx;
};
/* m68k ELF linker hash table. */
struct elf_m68k_link_hash_table
{
struct elf_link_hash_table root;
/* Small local sym to section mapping cache. */
struct sym_sec_cache sym_sec;
/* The PLT format used by this link, or NULL if the format has not
yet been chosen. */
const struct elf_m68k_plt_info *plt_info;
/* True, if GP is loaded within each function which uses it.
Set to TRUE when GOT negative offsets or multi-GOT is enabled. */
bfd_boolean local_gp_p;
/* Switch controlling use of negative offsets to double the size of GOTs. */
bfd_boolean use_neg_got_offsets_p;
/* Switch controlling generation of multiple GOTs. */
bfd_boolean allow_multigot_p;
/* Multi-GOT data structure. */
struct elf_m68k_multi_got multi_got_;
};
/* Get the m68k ELF linker hash table from a link_info structure. */
#define elf_m68k_hash_table(p) \
((struct elf_m68k_link_hash_table *) (p)->hash)
/* Shortcut to multi-GOT data. */
#define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
/* Create an entry in an m68k ELF linker hash table. */
static struct bfd_hash_entry *
elf_m68k_link_hash_newfunc (entry, table, string)
struct bfd_hash_entry *entry;
struct bfd_hash_table *table;
const char *string;
{
struct bfd_hash_entry *ret = entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == NULL)
ret = bfd_hash_allocate (table,
sizeof (struct elf_m68k_link_hash_entry));
if (ret == NULL)
return ret;
/* Call the allocation method of the superclass. */
ret = _bfd_elf_link_hash_newfunc (ret, table, string);
if (ret != NULL)
{
elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
elf_m68k_hash_entry (ret)->got_entry_key = 0;
elf_m68k_hash_entry (ret)->glist = NULL;
}
return ret;
}
/* Create an m68k ELF linker hash table. */
static struct bfd_link_hash_table *
elf_m68k_link_hash_table_create (abfd)
bfd *abfd;
{
struct elf_m68k_link_hash_table *ret;
bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
if (ret == (struct elf_m68k_link_hash_table *) NULL)
return NULL;
if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
elf_m68k_link_hash_newfunc,
sizeof (struct elf_m68k_link_hash_entry)))
{
free (ret);
return NULL;
}
ret->sym_sec.abfd = NULL;
ret->plt_info = NULL;
ret->local_gp_p = FALSE;
ret->use_neg_got_offsets_p = FALSE;
ret->allow_multigot_p = FALSE;
ret->multi_got_.bfd2got = NULL;
ret->multi_got_.global_symndx = 1;
return &ret->root.root;
}
/* Destruct local data. */
static void
elf_m68k_link_hash_table_free (struct bfd_link_hash_table *_htab)
{
struct elf_m68k_link_hash_table *htab;
htab = (struct elf_m68k_link_hash_table *) _htab;
if (htab->multi_got_.bfd2got != NULL)
{
htab_delete (htab->multi_got_.bfd2got);
htab->multi_got_.bfd2got = NULL;
}
}
/* Set the right machine number. */
static bfd_boolean
elf32_m68k_object_p (bfd *abfd)
{
unsigned int mach = 0;
unsigned features = 0;
flagword eflags = elf_elfheader (abfd)->e_flags;
if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
features |= m68000;
else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
features |= cpu32;
else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
features |= fido_a;
else
{
switch (eflags & EF_M68K_CF_ISA_MASK)
{
case EF_M68K_CF_ISA_A_NODIV:
features |= mcfisa_a;
break;
case EF_M68K_CF_ISA_A:
features |= mcfisa_a|mcfhwdiv;
break;
case EF_M68K_CF_ISA_A_PLUS:
features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
break;
case EF_M68K_CF_ISA_B_NOUSP:
features |= mcfisa_a|mcfisa_b|mcfhwdiv;
break;
case EF_M68K_CF_ISA_B:
features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
break;
case EF_M68K_CF_ISA_C:
features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
break;
case EF_M68K_CF_ISA_C_NODIV:
features |= mcfisa_a|mcfisa_c|mcfusp;
break;
}
switch (eflags & EF_M68K_CF_MAC_MASK)
{
case EF_M68K_CF_MAC:
features |= mcfmac;
break;
case EF_M68K_CF_EMAC:
features |= mcfemac;
break;
}
if (eflags & EF_M68K_CF_FLOAT)
features |= cfloat;
}
mach = bfd_m68k_features_to_mach (features);
bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
return TRUE;
}
/* Keep m68k-specific flags in the ELF header. */
static bfd_boolean
elf32_m68k_set_private_flags (abfd, flags)
bfd *abfd;
flagword flags;
{
elf_elfheader (abfd)->e_flags = flags;
elf_flags_init (abfd) = TRUE;
return TRUE;
}
/* Merge backend specific data from an object file to the output
object file when linking. */
static bfd_boolean
elf32_m68k_merge_private_bfd_data (ibfd, obfd)
bfd *ibfd;
bfd *obfd;
{
flagword out_flags;
flagword in_flags;
flagword out_isa;
flagword in_isa;
const bfd_arch_info_type *arch_info;
if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
|| bfd_get_flavour (obfd) != bfd_target_elf_flavour)
return FALSE;
/* Get the merged machine. This checks for incompatibility between
Coldfire & non-Coldfire flags, incompability between different
Coldfire ISAs, and incompability between different MAC types. */
arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
if (!arch_info)
return FALSE;
bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
in_flags = elf_elfheader (ibfd)->e_flags;
if (!elf_flags_init (obfd))
{
elf_flags_init (obfd) = TRUE;
out_flags = in_flags;
}
else
{
out_flags = elf_elfheader (obfd)->e_flags;
unsigned int variant_mask;
if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
variant_mask = 0;
else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
variant_mask = 0;
else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
variant_mask = 0;
else
variant_mask = EF_M68K_CF_ISA_MASK;
in_isa = (in_flags & variant_mask);
out_isa = (out_flags & variant_mask);
if (in_isa > out_isa)
out_flags ^= in_isa ^ out_isa;
if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
&& (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
|| ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
&& (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
out_flags = EF_M68K_FIDO;
else
out_flags |= in_flags ^ in_isa;
}
elf_elfheader (obfd)->e_flags = out_flags;
return TRUE;
}
/* Display the flags field. */
static bfd_boolean
elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
{
FILE *file = (FILE *) ptr;
flagword eflags = elf_elfheader (abfd)->e_flags;
BFD_ASSERT (abfd != NULL && ptr != NULL);
/* Print normal ELF private data. */
_bfd_elf_print_private_bfd_data (abfd, ptr);
/* Ignore init flag - it may not be set, despite the flags field containing valid data. */
/* xgettext:c-format */
fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
fprintf (file, " [m68000]");
else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
fprintf (file, " [cpu32]");
else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
fprintf (file, " [fido]");
else
{
if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
fprintf (file, " [cfv4e]");
if (eflags & EF_M68K_CF_ISA_MASK)
{
char const *isa = _("unknown");
char const *mac = _("unknown");
char const *additional = "";
switch (eflags & EF_M68K_CF_ISA_MASK)
{
case EF_M68K_CF_ISA_A_NODIV:
isa = "A";
additional = " [nodiv]";
break;
case EF_M68K_CF_ISA_A:
isa = "A";
break;
case EF_M68K_CF_ISA_A_PLUS:
isa = "A+";
break;
case EF_M68K_CF_ISA_B_NOUSP:
isa = "B";
additional = " [nousp]";
break;
case EF_M68K_CF_ISA_B:
isa = "B";
break;
case EF_M68K_CF_ISA_C:
isa = "C";
break;
case EF_M68K_CF_ISA_C_NODIV:
isa = "C";
additional = " [nodiv]";
break;
}
fprintf (file, " [isa %s]%s", isa, additional);
if (eflags & EF_M68K_CF_FLOAT)
fprintf (file, " [float]");
switch (eflags & EF_M68K_CF_MAC_MASK)
{
case 0:
mac = NULL;
break;
case EF_M68K_CF_MAC:
mac = "mac";
break;
case EF_M68K_CF_EMAC:
mac = "emac";
break;
}
if (mac)
fprintf (file, " [%s]", mac);
}
}
fputc ('\n', file);
return TRUE;
}
/* Multi-GOT support implementation design:
Multi-GOT starts in check_relocs hook. There we scan all
relocations of a BFD and build a local GOT (struct elf_m68k_got)
for it. If a single BFD appears to require too many GOT slots with
R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
to user.
After check_relocs has been invoked for each input BFD, we have
constructed a GOT for each input BFD.
To minimize total number of GOTs required for a particular output BFD
(as some environments support only 1 GOT per output object) we try
to merge some of the GOTs to share an offset space. Ideally [and in most
cases] we end up with a single GOT. In cases when there are too many
restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
several GOTs, assuming the environment can handle them.
Partitioning is done in elf_m68k_partition_multi_got. We start with
an empty GOT and traverse bfd2got hashtable putting got_entries from
local GOTs to the new 'big' one. We do that by constructing an
intermediate GOT holding all the entries the local GOT has and the big
GOT lacks. Then we check if there is room in the big GOT to accomodate
all the entries from diff. On success we add those entries to the big
GOT; on failure we start the new 'big' GOT and retry the adding of
entries from the local GOT. Note that this retry will always succeed as
each local GOT doesn't overflow the limits. After partitioning we
end up with each bfd assigned one of the big GOTs. GOT entries in the
big GOTs are initialized with GOT offsets. Note that big GOTs are
positioned consequently in program space and represent a single huge GOT
to the outside world.
After that we get to elf_m68k_relocate_section. There we
adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
relocations to refer to appropriate [assigned to current input_bfd]
big GOT.
Notes:
GOT entry type: We have 3 types of GOT entries.
* R_68K_GOT8O type is used in entries for symbols that have
at least one R_68K_GOT8O relocation. We can have at most 0x40
such entries in one GOT.
* R_68K_GOT16O type is used in entries for symbols that have
at least one R_68K_GOT16O relocation and no R_68K_GOT8O relocations.
We can have at most 0x4000 such entries in one GOT.
* R_68K_GOT32O type is used in all other cases. We can have as many
such entries in one GOT as we like.
When counting relocations we have to include the count of the smaller
ranged relocations in the counts of the larger ranged ones in order
to correctly detect overflow.
Sorting the GOT: In each GOT starting offsets are assigned to
R_68K_GOT8O entries, which are followed by R_68K_GOT16O entries, and
R_68K_GOT32O entries go at the end. See finalize_got_offsets for details.
Negative GOT offsets: To double usable offset range of GOTs we use
negative offsets. As we assign entries with GOT offsets relative to
start of .got section, the offset values are positive. They become
negative only in relocate_section where got->offset value is
subtracted from them.
3 special GOT entries: There are 3 special GOT entries used internally
by loader. These entries happen to be placed to .got.plt section,
so we don't do anything about them in multi-GOT support.
Memory management: All data except for hashtables
multi_got->bfd2got and got->entries are allocated on
elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
to most functions), so we don't need to care to free them. At the
moment of allocation hashtables are being linked into main data
structure (multi_got), all pieces of which are reachable from
elf_m68k_multi_got (info). We deallocate them in
elf_m68k_link_hash_table_free. */
/* Initialize GOT. */
static void
elf_m68k_init_got (struct elf_m68k_got *got,
htab_t entries,
bfd_vma rel_8o_n_entries,
bfd_vma rel_8o_16o_n_entries,
bfd_vma local_n_entries,
bfd_vma offset)
{
got->entries = entries;
got->rel_8o_n_entries = rel_8o_n_entries;
got->rel_8o_16o_n_entries = rel_8o_16o_n_entries;
got->local_n_entries = local_n_entries;
got->offset = offset;
}
/* Destruct GOT. */
static void
elf_m68k_clear_got (struct elf_m68k_got *got)
{
if (got->entries != NULL)
{
htab_delete (got->entries);
got->entries = NULL;
}
}
/* Create and empty GOT structure. INFO is the context where memory
should be allocated. */
static struct elf_m68k_got *
elf_m68k_create_empty_got (struct bfd_link_info *info)
{
struct elf_m68k_got *got;
got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
if (got == NULL)
return NULL;
elf_m68k_init_got (got, NULL, 0, 0, 0, (bfd_vma) -1);
return got;
}
/* Initialize KEY. */
static void
elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
struct elf_link_hash_entry *h,
const bfd *abfd, unsigned long symndx)
{
if (h != NULL)
{
key->bfd = NULL;
key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
BFD_ASSERT (key->symndx != 0);
}
else
{
key->bfd = abfd;
key->symndx = symndx;
}
}
/* Calculate hash of got_entry.
??? Is it good? */
static hashval_t
elf_m68k_got_entry_hash (const void *_entry)
{
const struct elf_m68k_got_entry_key *key;
key = &((const struct elf_m68k_got_entry *) _entry)->key_;
return key->symndx + (key->bfd != NULL
? (int) key->bfd->id
: -1);
}