forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoff-ppc.c
2598 lines (2222 loc) · 77.5 KB
/
coff-ppc.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 PowerPC Microsoft Portable Executable files.
Copyright (C) 1990-2016 Free Software Foundation, Inc.
Original version pieced together by Kim Knuttila (krk@cygnus.com)
There is nothing new under the sun. This file draws a lot on other
coff files, in particular, those for the rs/6000, alpha, mips, and
intel backends, and the PE work for the arm.
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, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
/* Current State:
- objdump works
- relocs generated by gas
- ld will link files, but they do not run.
- dlltool will not produce correct output in some .reloc cases, and will
not produce the right glue code for dll function calls. */
#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "coff/powerpc.h"
#include "coff/internal.h"
#include "coff/pe.h"
#ifdef BADMAG
#undef BADMAG
#endif
#define BADMAG(x) PPCBADMAG(x)
#include "libcoff.h"
/* This file is compiled more than once, but we only compile the
final_link routine once. */
extern bfd_boolean ppc_bfd_coff_final_link (bfd *, struct bfd_link_info *);
extern void dump_toc (void *);
/* The toc is a set of bfd_vma fields. We use the fact that valid
addresses are even (i.e. the bit representing "1" is off) to allow
us to encode a little extra information in the field
- Unallocated addresses are initialized to 1.
- Allocated addresses are even numbers.
The first time we actually write a reference to the toc in the bfd,
we want to record that fact in a fixup file (if it is asked for), so
we keep track of whether or not an address has been written by marking
the low order bit with a "1" upon writing. */
#define SET_UNALLOCATED(x) ((x) = 1)
#define IS_UNALLOCATED(x) ((x) == 1)
#define IS_WRITTEN(x) ((x) & 1)
#define MARK_AS_WRITTEN(x) ((x) |= 1)
#define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
/* Turn on this check if you suspect something amiss in the hash tables. */
#ifdef DEBUG_HASH
/* Need a 7 char string for an eye catcher. */
#define EYE "krkjunk"
#define HASH_CHECK_DCL char eye_catcher[8];
#define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
#define HASH_CHECK(addr) \
if (strcmp (addr->eye_catcher, EYE) != 0) \
{ \
fprintf (stderr,\
/* xgettext: c-format */ \
_("File %s, line %d, Hash check failure, bad eye %8s\n"), \
__FILE__, __LINE__, addr->eye_catcher); \
abort (); \
}
#else
#define HASH_CHECK_DCL
#define HASH_CHECK_INIT(ret)
#define HASH_CHECK(addr)
#endif
/* In order not to add an int to every hash table item for every coff
linker, we define our own hash table, derived from the coff one. */
/* PE linker hash table entries. */
struct ppc_coff_link_hash_entry
{
struct coff_link_hash_entry root; /* First entry, as required. */
/* As we wonder around the relocs, we'll keep the assigned toc_offset
here. */
bfd_vma toc_offset; /* Our addition, as required. */
int symbol_is_glue;
unsigned long int glue_insn;
HASH_CHECK_DCL
};
/* PE linker hash table. */
struct ppc_coff_link_hash_table
{
struct coff_link_hash_table root; /* First entry, as required. */
};
/* Routine to create an entry in the link hash table. */
static struct bfd_hash_entry *
ppc_coff_link_hash_newfunc (struct bfd_hash_entry * entry,
struct bfd_hash_table * table,
const char * string)
{
struct ppc_coff_link_hash_entry *ret =
(struct ppc_coff_link_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct ppc_coff_link_hash_entry *) NULL)
ret = (struct ppc_coff_link_hash_entry *)
bfd_hash_allocate (table,
sizeof (struct ppc_coff_link_hash_entry));
if (ret == (struct ppc_coff_link_hash_entry *) NULL)
return NULL;
/* Call the allocation method of the superclass. */
ret = ((struct ppc_coff_link_hash_entry *)
_bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
table, string));
if (ret)
{
/* Initialize the local fields. */
SET_UNALLOCATED (ret->toc_offset);
ret->symbol_is_glue = 0;
ret->glue_insn = 0;
HASH_CHECK_INIT (ret);
}
return (struct bfd_hash_entry *) ret;
}
/* Initialize a PE linker hash table. */
static bfd_boolean
ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
bfd *abfd,
struct bfd_hash_entry *(*newfunc)
(struct bfd_hash_entry *,
struct bfd_hash_table *,
const char *),
unsigned int entsize)
{
return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
}
/* Create a PE linker hash table. */
static struct bfd_link_hash_table *
ppc_coff_link_hash_table_create (bfd *abfd)
{
struct ppc_coff_link_hash_table *ret;
bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
if (ret == NULL)
return NULL;
if (!ppc_coff_link_hash_table_init (ret, abfd,
ppc_coff_link_hash_newfunc,
sizeof (struct ppc_coff_link_hash_entry)))
{
free (ret);
return (struct bfd_link_hash_table *) NULL;
}
return &ret->root.root;
}
/* Now, tailor coffcode.h to use our hash stuff. */
#define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
/* The nt loader points the toc register to &toc + 32768, in order to
use the complete range of a 16-bit displacement. We have to adjust
for this when we fix up loads displaced off the toc reg. */
#define TOC_LOAD_ADJUSTMENT (-32768)
#define TOC_SECTION_NAME ".private.toc"
/* The main body of code is in coffcode.h. */
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
from smaller values. Start with zero, widen, *then* decrement. */
#define MINUS_ONE (((bfd_vma)0) - 1)
/* These should definitely go in a header file somewhere... */
/* NOP */
#define IMAGE_REL_PPC_ABSOLUTE 0x0000
/* 64-bit address */
#define IMAGE_REL_PPC_ADDR64 0x0001
/* 32-bit address */
#define IMAGE_REL_PPC_ADDR32 0x0002
/* 26-bit address, shifted left 2 (branch absolute) */
#define IMAGE_REL_PPC_ADDR24 0x0003
/* 16-bit address */
#define IMAGE_REL_PPC_ADDR16 0x0004
/* 16-bit address, shifted left 2 (load doubleword) */
#define IMAGE_REL_PPC_ADDR14 0x0005
/* 26-bit PC-relative offset, shifted left 2 (branch relative) */
#define IMAGE_REL_PPC_REL24 0x0006
/* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
#define IMAGE_REL_PPC_REL14 0x0007
/* 16-bit offset from TOC base */
#define IMAGE_REL_PPC_TOCREL16 0x0008
/* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
#define IMAGE_REL_PPC_TOCREL14 0x0009
/* 32-bit addr w/o image base */
#define IMAGE_REL_PPC_ADDR32NB 0x000A
/* va of containing section (as in an image sectionhdr) */
#define IMAGE_REL_PPC_SECREL 0x000B
/* sectionheader number */
#define IMAGE_REL_PPC_SECTION 0x000C
/* substitute TOC restore instruction iff symbol is glue code */
#define IMAGE_REL_PPC_IFGLUE 0x000D
/* symbol is glue code; virtual address is TOC restore instruction */
#define IMAGE_REL_PPC_IMGLUE 0x000E
/* va of containing section (limited to 16 bits) */
#define IMAGE_REL_PPC_SECREL16 0x000F
/* Stuff to handle immediate data when the number of bits in the
data is greater than the number of bits in the immediate field
We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
#define IMAGE_REL_PPC_REFHI 0x0010
#define IMAGE_REL_PPC_REFLO 0x0011
#define IMAGE_REL_PPC_PAIR 0x0012
/* This is essentially the same as tocrel16, with TOCDEFN assumed. */
#define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
/* Flag bits in IMAGE_RELOCATION.TYPE. */
/* Subtract reloc value rather than adding it. */
#define IMAGE_REL_PPC_NEG 0x0100
/* Fix branch prediction bit to predict branch taken. */
#define IMAGE_REL_PPC_BRTAKEN 0x0200
/* Fix branch prediction bit to predict branch not taken. */
#define IMAGE_REL_PPC_BRNTAKEN 0x0400
/* TOC slot defined in file (or, data in toc). */
#define IMAGE_REL_PPC_TOCDEFN 0x0800
/* Masks to isolate above values in IMAGE_RELOCATION.Type. */
#define IMAGE_REL_PPC_TYPEMASK 0x00FF
#define IMAGE_REL_PPC_FLAGMASK 0x0F00
#define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
#define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
#define EXTRACT_JUNK(x) \
((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
/* Static helper functions to make relocation work. */
/* (Work In Progress) */
static bfd_reloc_status_type ppc_refhi_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type ppc_pair_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type ppc_toc16_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type ppc_section_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type ppc_secrel_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type ppc_imglue_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
/* FIXME: It'll take a while to get through all of these. I only need a few to
get us started, so those I'll make sure work. Those marked FIXME are either
completely unverified or have a specific unknown marked in the comment. */
/* Relocation entries for Windows/NT on PowerPC.
From the document "" we find the following listed as used relocs:
ABSOLUTE : The noop
ADDR[64|32|16] : fields that hold addresses in data fields or the
16 bit displacement field on a load/store.
ADDR[24|14] : fields that hold addresses in branch and cond
branches. These represent [26|16] bit addresses.
The low order 2 bits are preserved.
REL[24|14] : branches relative to the Instruction Address
register. These represent [26|16] bit addresses,
as before. The instruction field will be zero, and
the address of the SYM will be inserted at link time.
TOCREL16 : 16 bit displacement field referring to a slot in
toc.
TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
ADDR32NB : 32 bit address relative to the virtual origin.
(On the alpha, this is always a linker generated thunk)
(i.e. 32bit addr relative to the image base)
SECREL : The value is relative to the start of the section
containing the symbol.
SECTION : access to the header containing the item. Supports the
codeview debugger.
In particular, note that the document does not indicate that the
relocations listed in the header file are used. */
static reloc_howto_type ppc_coff_howto_table[] =
{
/* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
/* Unused: */
HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
0, /* rightshift */
0, /* size (0 = byte, 1 = short, 2 = long) */
0, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* dont complain_on_overflow */
0, /* special_function */
"ABSOLUTE", /* name */
FALSE, /* partial_inplace */
0x00, /* src_mask */
0x00, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
/* Unused: */
HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
0, /* rightshift */
3, /* size (0 = byte, 1 = short, 2 = long) */
64, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
0, /* special_function */
"ADDR64", /* name */
TRUE, /* partial_inplace */
MINUS_ONE, /* src_mask */
MINUS_ONE, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
/* Used: */
HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
0, /* special_function */
"ADDR32", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
/* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
/* Of course, That's the IBM approved bit numbering, which is not what */
/* anyone else uses.... The li field is in bit 2 thru 25 */
/* Used: */
HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
26, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
0, /* special_function */
"ADDR24", /* name */
TRUE, /* partial_inplace */
0x07fffffc, /* src_mask */
0x07fffffc, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
/* Used: */
HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"ADDR16", /* name */
TRUE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR14 0x0005 */
/* 16-bit address, shifted left 2 (load doubleword) */
/* FIXME: the mask is likely wrong, and the bit position may be as well */
/* Unused: */
HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
1, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"ADDR16", /* name */
TRUE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_REL24 0x0006 */
/* 26-bit PC-relative offset, shifted left 2 (branch relative) */
/* Used: */
HOWTO (IMAGE_REL_PPC_REL24, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
26, /* bitsize */
TRUE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"REL24", /* name */
TRUE, /* partial_inplace */
0x3fffffc, /* src_mask */
0x3fffffc, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_REL14 0x0007 */
/* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
/* FIXME: the mask is likely wrong, and the bit position may be as well */
/* FIXME: how does it know how far to shift? */
/* Unused: */
HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
1, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"ADDR16", /* name */
TRUE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
TRUE), /* pcrel_offset */
/* IMAGE_REL_PPC_TOCREL16 0x0008 */
/* 16-bit offset from TOC base */
/* Used: */
HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* complain_on_overflow */
ppc_toc16_reloc, /* special_function */
"TOCREL16", /* name */
FALSE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_TOCREL14 0x0009 */
/* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
/* Unused: */
HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
1, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"TOCREL14", /* name */
FALSE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_ADDR32NB 0x000A */
/* 32-bit addr w/ image base */
/* Unused: */
HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"ADDR32NB", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_SECREL 0x000B */
/* va of containing section (as in an image sectionhdr) */
/* Unused: */
HOWTO (IMAGE_REL_PPC_SECREL,/* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
ppc_secrel_reloc, /* special_function */
"SECREL", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
TRUE), /* pcrel_offset */
/* IMAGE_REL_PPC_SECTION 0x000C */
/* sectionheader number */
/* Unused: */
HOWTO (IMAGE_REL_PPC_SECTION,/* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
ppc_section_reloc, /* special_function */
"SECTION", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
TRUE), /* pcrel_offset */
/* IMAGE_REL_PPC_IFGLUE 0x000D */
/* substitute TOC restore instruction iff symbol is glue code */
/* Used: */
HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"IFGLUE", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_IMGLUE 0x000E */
/* symbol is glue code; virtual address is TOC restore instruction */
/* Unused: */
HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* complain_on_overflow */
ppc_imglue_reloc, /* special_function */
"IMGLUE", /* name */
FALSE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_SECREL16 0x000F */
/* va of containing section (limited to 16 bits) */
/* Unused: */
HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"SECREL16", /* name */
TRUE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
TRUE), /* pcrel_offset */
/* IMAGE_REL_PPC_REFHI 0x0010 */
/* Unused: */
HOWTO (IMAGE_REL_PPC_REFHI, /* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
ppc_refhi_reloc, /* special_function */
"REFHI", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_REFLO 0x0011 */
/* Unused: */
HOWTO (IMAGE_REL_PPC_REFLO, /* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
ppc_refhi_reloc, /* special_function */
"REFLO", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_PAIR 0x0012 */
/* Unused: */
HOWTO (IMAGE_REL_PPC_PAIR, /* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
ppc_pair_reloc, /* special_function */
"PAIR", /* name */
TRUE, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
FALSE), /* pcrel_offset */
/* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
/* 16-bit offset from TOC base, without causing a definition */
/* Used: */
HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
0, /* rightshift */
1, /* size (0 = byte, 1 = short, 2 = long) */
16, /* bitsize */
FALSE, /* pc_relative */
0, /* bitpos */
complain_overflow_dont, /* complain_on_overflow */
0, /* special_function */
"TOCREL16, TOCDEFN", /* name */
FALSE, /* partial_inplace */
0xffff, /* src_mask */
0xffff, /* dst_mask */
FALSE), /* pcrel_offset */
};
/* Some really cheezy macros that can be turned on to test stderr :-) */
#ifdef DEBUG_RELOC
#define UN_IMPL(x) \
{ \
static int i; \
if (i == 0) \
{ \
i = 1; \
fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
} \
}
#define DUMP_RELOC(n,r) \
{ \
fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
n, (*(r->sym_ptr_ptr))->name, \
r->address, r->addend); \
}
/* Given a reloc name, n, and a pointer to an internal_reloc,
dump out interesting information on the contents
#define n_name _n._n_name
#define n_zeroes _n._n_n._n_zeroes
#define n_offset _n._n_n._n_offset */
#define DUMP_RELOC2(n,r) \
{ \
fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
n, r->r_symndx, r->r_vaddr, \
(((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
?" ":" TOCDEFN" ); \
}
#else
#define UN_IMPL(x)
#define DUMP_RELOC(n,r)
#define DUMP_RELOC2(n,r)
#endif
/* TOC construction and management routines. */
/* This file is compiled twice, and these variables are defined in one
of the compilations. FIXME: This is confusing and weird. Also,
BFD should not use global variables. */
extern bfd * bfd_of_toc_owner;
extern long int global_toc_size;
extern long int import_table_size;
extern long int first_thunk_address;
extern long int thunk_size;
enum toc_type
{
default_toc,
toc_32,
toc_64
};
enum ref_category
{
priv,
pub,
tocdata
};
struct list_ele
{
struct list_ele *next;
bfd_vma addr;
enum ref_category cat;
int offset;
const char *name;
};
extern struct list_ele *head;
extern struct list_ele *tail;
static void
record_toc (asection *toc_section,
bfd_signed_vma our_toc_offset,
enum ref_category cat,
const char *name)
{
/* Add this entry to our toc addr-offset-name list. */
bfd_size_type amt = sizeof (struct list_ele);
struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
if (t == NULL)
abort ();
t->next = 0;
t->offset = our_toc_offset;
t->name = name;
t->cat = cat;
t->addr = toc_section->output_offset + our_toc_offset;
if (head == 0)
{
head = t;
tail = t;
}
else
{
tail->next = t;
tail = t;
}
}
#ifdef COFF_IMAGE_WITH_PE
/* Record a toc offset against a symbol. */
static bfd_boolean
ppc_record_toc_entry (bfd *abfd,
struct bfd_link_info *info ATTRIBUTE_UNUSED,
asection *sec ATTRIBUTE_UNUSED,
int sym,
enum toc_type toc_kind ATTRIBUTE_UNUSED)
{
struct ppc_coff_link_hash_entry *h;
int *local_syms;
h = 0;
h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
if (h != 0)
{
HASH_CHECK(h);
}
if (h == 0)
{
local_syms = obj_coff_local_toc_table(abfd);
if (local_syms == 0)
{
unsigned int i;
bfd_size_type amt;
/* allocate a table */
amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
local_syms = (int *) bfd_zalloc (abfd, amt);
if (local_syms == 0)
return FALSE;
obj_coff_local_toc_table (abfd) = local_syms;
for (i = 0; i < obj_raw_syment_count (abfd); ++i)
{
SET_UNALLOCATED (local_syms[i]);
}
}
if (IS_UNALLOCATED(local_syms[sym]))
{
local_syms[sym] = global_toc_size;
global_toc_size += 4;
/* The size must fit in a 16-bit displacement. */
if (global_toc_size > 65535)
{
_bfd_error_handler (_("TOC overflow"));
bfd_set_error (bfd_error_file_too_big);
return FALSE;
}
}
}
else
{
/* Check to see if there's a toc slot allocated. If not, do it
here. It will be used in relocate_section. */
if (IS_UNALLOCATED(h->toc_offset))
{
h->toc_offset = global_toc_size;
global_toc_size += 4;
/* The size must fit in a 16-bit displacement. */
if (global_toc_size >= 65535)
{
_bfd_error_handler (_("TOC overflow"));
bfd_set_error (bfd_error_file_too_big);
return FALSE;
}
}
}
return TRUE;
}
/* Record a toc offset against a symbol. */
static void
ppc_mark_symbol_as_glue (bfd *abfd,
int sym,
struct internal_reloc *rel)
{
struct ppc_coff_link_hash_entry *h;
h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
HASH_CHECK(h);
h->symbol_is_glue = 1;
h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
return;
}
#endif /* COFF_IMAGE_WITH_PE */
/* Return TRUE if this relocation should
appear in the output .reloc section. */
static bfd_boolean
in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
reloc_howto_type *howto)
{
return
(! howto->pc_relative)
&& (howto->type != IMAGE_REL_PPC_ADDR32NB)
&& (howto->type != IMAGE_REL_PPC_TOCREL16)
&& (howto->type != IMAGE_REL_PPC_IMGLUE)
&& (howto->type != IMAGE_REL_PPC_IFGLUE)
&& (howto->type != IMAGE_REL_PPC_SECREL)
&& (howto->type != IMAGE_REL_PPC_SECTION)
&& (howto->type != IMAGE_REL_PPC_SECREL16)
&& (howto->type != IMAGE_REL_PPC_REFHI)
&& (howto->type != IMAGE_REL_PPC_REFLO)
&& (howto->type != IMAGE_REL_PPC_PAIR)
&& (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
}
static bfd_boolean
write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
{
if (coff_data (obfd)->pe)
addr -= pe_data (obfd)->pe_opthdr.ImageBase;
if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
return TRUE;
bfd_set_error (bfd_error_system_call);
return FALSE;
}
/* The reloc processing routine for the optimized COFF linker. */
static bfd_boolean
coff_ppc_relocate_section (bfd *output_bfd,
struct bfd_link_info *info,
bfd *input_bfd,
asection *input_section,
bfd_byte *contents,
struct internal_reloc *relocs,
struct internal_syment *syms,
asection **sections)
{
struct internal_reloc *rel;
struct internal_reloc *relend;
asection *toc_section = 0;
bfd_vma relocation;
reloc_howto_type *howto = 0;
/* If we are performing a relocatable link, we don't need to do a
thing. The caller will take care of adjusting the reloc
addresses and symbol indices. */
if (bfd_link_relocatable (info))
return TRUE;
rel = relocs;
relend = rel + input_section->reloc_count;
for (; rel < relend; rel++)
{
long symndx;
struct ppc_coff_link_hash_entry *h;
struct internal_syment *sym;
bfd_vma val;
asection *sec;
bfd_reloc_status_type rstat;
bfd_byte *loc;
unsigned short r_type = EXTRACT_TYPE (rel->r_type);
unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
symndx = rel->r_symndx;
loc = contents + rel->r_vaddr - input_section->vma;
/* FIXME: check bounds on r_type */
howto = ppc_coff_howto_table + r_type;
if (symndx == -1)
{
h = NULL;
sym = NULL;
}
else
{
h = (struct ppc_coff_link_hash_entry *)
(obj_coff_sym_hashes (input_bfd)[symndx]);
if (h != 0)
{
HASH_CHECK(h);
}
sym = syms + symndx;
}
if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
{
/* An IMGLUE reloc must have a name. Something is very wrong. */
abort ();
}
sec = NULL;
val = 0;