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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
|
/* $XFree86: xc/programs/Xserver/hw/xfree86/loader/coffloader.c,v 1.21tsi Exp $ */
/*
*
* Copyright 1995,96 by Metro Link, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Metro Link, Inc. not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Metro Link, Inc. makes no
* representations about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty.
*
* METRO LINK, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL METRO LINK, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef __QNX__
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#include <sys/stat.h>
#ifdef DBMALLOC
#include <debug/malloc.h>
#define Xalloc(size) malloc(size)
#define Xcalloc(size) calloc(1,(size))
#define Xfree(size) free(size)
#endif
#include <X11/Xos.h>
#include "os.h"
#include "coff.h"
#include "sym.h"
#include "loader.h"
#include "coffloader.h"
#include "compiler.h"
#ifndef LOADERDEBUG
#define LOADERDEBUG 0
#endif
#if LOADERDEBUG
#define COFFDEBUG ErrorF
#endif
/*
* This structure contains all of the information about a module
* that has been loaded.
*/
typedef struct {
int handle;
long module; /* Id of the module used to find inter module calls */
int fd;
loader_funcs *funcs;
FILHDR *header; /* file header */
AOUTHDR *optheader; /* optional file header */
unsigned short numsh;
SCNHDR *sections; /* Start address of the section table */
int secsize; /* size of the section table */
unsigned char **saddr; /* Start addresss of the sections table */
unsigned char **reladdr; /* Start addresss of the relocation table */
unsigned char *strtab; /* Start address of the string table */
int strsize; /* size of the string table */
unsigned char *text; /* Start address of the .text section */
int txtndx; /* index of the .text section */
long txtaddr; /* offset of the .text section */
int txtsize; /* size of the .text section */
int txtrelsize; /* size of the .rel.text section */
unsigned char *data; /* Start address of the .data section */
int datndx; /* index of the .data section */
long dataddr; /* offset of the .data section */
int datsize; /* size of the .data section */
int datrelsize; /* size of the .rel.data section */
unsigned char *bss; /* Start address of the .bss section */
int bssndx; /* index of the .bss section */
long bssaddr; /* offset of the .bss section */
int bsssize; /* size of the .bss section */
SYMENT *symtab; /* Start address of the .symtab section */
int symndx; /* index of the .symtab section */
int symsize; /* size of the .symtab section */
unsigned char *common; /* Start address of the .common section */
int comsize; /* size of the .common section */
long toc; /* Offset of the TOC csect */
unsigned char *tocaddr; /* Address of the TOC csect */
} COFFModuleRec, *COFFModulePtr;
/*
* If any relocation is unable to be satisfied, then put it on a list
* to try later after more modules have been loaded.
*/
typedef struct _coff_reloc {
COFFModulePtr file;
RELOC *rel;
int secndx;
struct _coff_reloc *next;
} COFFRelocRec;
/*
* Symbols with a section number of 0 (N_UNDEF) but a value of non-zero
* need to have space allocated for them.
*
* Gather all of these symbols together, and allocate one chunk when we
* are done.
*/
typedef struct _coff_COMMON {
SYMENT *sym;
int index;
struct _coff_COMMON *next;
} COFFCommonRec;
static COFFCommonPtr listCOMMON = NULL;
/* Prototypes for static functions */
static int COFFhashCleanOut(void *, itemPtr);
static char *COFFGetSymbolName(COFFModulePtr, int);
static COFFCommonPtr COFFAddCOMMON(SYMENT *, int);
static LOOKUP *COFFCreateCOMMON(COFFModulePtr);
static COFFRelocPtr COFFDelayRelocation(COFFModulePtr, int, RELOC *);
static SYMENT *COFFGetSymbol(COFFModulePtr, int);
#if defined(i386) || defined(__powerpc__)
static unsigned char *COFFGetSymbolValue(COFFModulePtr, int);
#endif
static COFFRelocPtr COFF_RelocateEntry(COFFModulePtr, int, RELOC *);
static LOOKUP *COFF_GetSymbols(COFFModulePtr);
static void COFFCollectSections(COFFModulePtr);
static COFFRelocPtr COFFCollectRelocations(COFFModulePtr);
/*
* Utility Functions
*/
static int
COFFhashCleanOut(void *voidptr, itemPtr item)
{
COFFModulePtr module = (COFFModulePtr) voidptr;
return (module->handle == item->handle);
}
/*
* Manage listResolv
*/
static COFFRelocPtr
COFFDelayRelocation(COFFModulePtr cofffile, int secndx, RELOC *rel)
{
COFFRelocPtr reloc;
if ((reloc = xf86loadermalloc(sizeof(COFFRelocRec))) == NULL) {
ErrorF("COFFDelayRelocation() Unable to allocate memory!!!!\n");
return 0;
}
reloc->file = cofffile;
reloc->secndx = secndx;
reloc->rel = rel;
reloc->next = 0;
return reloc;
}
/*
* Manage listCOMMON
*/
static COFFCommonPtr
COFFAddCOMMON(SYMENT *sym, int index)
{
COFFCommonPtr common;
if ((common = xf86loadermalloc(sizeof(COFFCommonRec))) == NULL) {
ErrorF("COFFAddCOMMON() Unable to allocate memory!!!!\n");
return 0;
}
common->sym = sym;
common->index = index;
common->next = 0;
return common;
}
static LOOKUP *
COFFCreateCOMMON(COFFModulePtr cofffile)
{
int numsyms = 0, size = 0, l = 0;
int offset = 0;
LOOKUP *lookup;
COFFCommonPtr common;
if (listCOMMON == NULL)
return NULL;
common = listCOMMON;
for (common = listCOMMON; common; common = common->next) {
/* Ensure long word alignment */
if (common->sym->n_value != 2 && common->sym->n_value != 1) /* But not for short and char ;-)(mr) */
if (common->sym->n_value % 4 != 0)
common->sym->n_value += 4 - (common->sym->n_value % 4);
/* accumulate the sizes */
size += common->sym->n_value;
numsyms++;
}
#ifdef COFFDEBUG
COFFDEBUG("COFFCreateCOMMON() %d entries (%d bytes) of COMMON data\n",
numsyms, size);
#endif
if ((lookup = xf86loadermalloc((numsyms + 1) * sizeof(LOOKUP))) == NULL) {
ErrorF("COFFCreateCOMMON() Unable to allocate memory!!!!\n");
return NULL;
}
cofffile->comsize = size;
if ((cofffile->common = xf86loadercalloc(1, size)) == NULL) {
ErrorF("COFFCreateCOMMON() Unable to allocate memory!!!!\n");
return NULL;
}
/* Traverse the common list and create a lookup table with all the
* common symbols. Destroy the common list in the process.
* See also ResolveSymbols.
*/
while (listCOMMON) {
common = listCOMMON;
lookup[l].symName = COFFGetSymbolName(cofffile, common->index);
lookup[l].offset = (funcptr) (cofffile->common + offset);
#ifdef COFFDEBUG
COFFDEBUG("Adding %p %s\n", (void *)lookup[l].offset,
lookup[l].symName);
#endif
listCOMMON = common->next;
offset += common->sym->n_value;
xf86loaderfree(common);
l++;
}
/* listCOMMON == NULL */
lookup[l].symName = NULL; /* Terminate the list */
return lookup;
}
/*
* Symbol Table
*/
/*
* Get symbol name
*/
static char *
COFFGetSymbolName(COFFModulePtr cofffile, int index)
{
char *name;
SYMENT *sym;
sym = (SYMENT *) (((unsigned char *)cofffile->symtab) + (index * SYMESZ));
#ifdef COFFDEBUG
COFFDEBUG("COFFGetSymbolName(%p,%x) %lx", (void *)cofffile, index,
sym->n_zeroes);
#endif
name = xf86loadermalloc(sym->n_zeroes ? SYMNMLEN + 1
: strlen((const char *)&cofffile->
strtab[(int)sym->n_offset - 4]) + 1);
if (!name)
FatalError("COFFGetSymbolName: Out of memory\n");
if (sym->n_zeroes) {
strncpy(name, sym->n_name, SYMNMLEN);
name[SYMNMLEN] = '\000';
} else {
strcpy(name, (const char *)&cofffile->strtab[(int)sym->n_offset - 4]);
}
#ifdef COFFDEBUG
COFFDEBUG(" %s\n", name);
#endif
return name;
}
static SYMENT *
COFFGetSymbol(COFFModulePtr file, int index)
{
return (SYMENT *) (((unsigned char *)file->symtab) + (index * SYMESZ));
}
#if defined(i386) || defined(__powerpc__)
static unsigned char *
COFFGetSymbolValue(COFFModulePtr cofffile, int index)
{
unsigned char *symval = 0; /* value of the indicated symbol */
itemPtr symbol; /* name/value of symbol */
char *symname;
symname = COFFGetSymbolName(cofffile, index);
#ifdef COFFDEBUG
COFFDEBUG("COFFGetSymbolValue() for %s=", symname);
#endif
symbol = LoaderHashFind(symname);
if (symbol)
symval = (unsigned char *)symbol->address;
#ifdef COFFDEBUG
COFFDEBUG("%p\n", symval);
#endif
xf86loaderfree(symname);
return symval;
}
#endif
#if defined(__powerpc__)
/*
* This function returns the address of the glink routine for a symbol. This
* address is used in cases where the function being called is not in the
* same module as the calling function.
*/
static unsigned char *
COFFGetSymbolGlinkValue(COFFModulePtr cofffile, int index)
{
unsigned char *symval = 0; /* value of the indicated symbol */
itemPtr symbol; /* name/value of symbol */
char *name;
name = COFFGetSymbolName(cofffile, index);
#ifdef COFFDEBUG
COFFDEBUG("COFFGetSymbolGlinkValue() for %s=", name);
#endif
symbol = LoaderHashFind(name + 1); /* Eat the '.' so we get the
* Function descriptor instead */
/* Here we are building up a glink function that will change the TOC
* pointer before calling a function that resides in a different module.
* The following code is being used to implement this.
1 00000000 3d80xxxx lis r12,hi16(funcdesc)
2 00000004 618cxxxx ori r12,r12,lo16(funcdesc)
3 00000008 90410014 st r2,20(r1) # save old TOC pointer
4 0000000c 804c0000 l r2,0(r12) # Get address of functions
5 00000010 7c4903a6 mtctr r2 # load destination address
6 00000014 804c0004 l r2,4(r12) # get TOC of function
7 00000018 4e800420 bctr # branch to it
*/
if (symbol) {
symval = (unsigned char *)&symbol->code.glink;
#ifdef COFFDEBUG
COFFDEBUG("%x\n", symval);
COFFDEBUG("glink_%s=%x\n", name, symval);
#endif
symbol->code.glink[0] = 0x3d80; /* lis r12 */
symbol->code.glink[1] =
((unsigned long)symbol->address & 0xffff0000) >> 16;
symbol->code.glink[2] = 0x618c; /* ori r12 */
symbol->code.glink[3] = ((unsigned long)symbol->address & 0x0000ffff);
symbol->code.glink[4] = 0x9041; /* st r2,20(r1) */
symbol->code.glink[5] = 0x0014;
symbol->code.glink[6] = 0x804c; /* l r2,0(r12) */
symbol->code.glink[7] = 0x0000;
symbol->code.glink[8] = 0x7c49; /* mtctr r2 */
symbol->code.glink[9] = 0x03a6;
symbol->code.glink[10] = 0x804c; /* l r2,4(r12) */
symbol->code.glink[11] = 0x0004;
symbol->code.glink[12] = 0x4e80; /* bctr */
symbol->code.glink[13] = 0x0420;
ppc_flush_icache(&symbol->code.glink[0]);
ppc_flush_icache(&symbol->code.glink[12]);
}
xf86loaderfree(name);
return symval;
}
#endif /* __powerpc__ */
/*
* Fix all of the relocation for the given section.
*/
static COFFRelocPtr
COFF_RelocateEntry(COFFModulePtr cofffile, int secndx, RELOC *rel)
{
SYMENT *symbol; /* value of the indicated symbol */
unsigned long *dest32; /* address of the place being modified */
#if defined(__powerpc__)
unsigned short *dest16; /* address of the place being modified */
itemPtr symitem; /* symbol structure from has table */
char *name;
#endif
unsigned char *symval; /* value of the indicated symbol */
/*
* Note: Section numbers are 1 biased, while the cofffile->saddr[] array
* of pointer is 0 biased, so alway have to account for the difference.
*/
/*
* Reminder: secndx is the section to which the relocation is applied.
* symbol->n_scnum is the section in which the symbol value resides.
*/
#ifdef COFFDEBUG
COFFDEBUG("%lx %ld %o ", (unsigned long)rel->r_vaddr,
rel->r_symndx, rel->r_type);
#if defined(__powerpc__)
COFFDEBUG("[%x %x %x] ",
RELOC_RSIGN(*rel), RELOC_RFIXUP(*rel), RELOC_RLEN(*rel));
#endif
#endif
symbol = COFFGetSymbol(cofffile, rel->r_symndx);
#ifdef COFFDEBUG
COFFDEBUG("%d %lx %d-%d\n", symbol->n_sclass, symbol->n_value,
symbol->n_scnum, secndx);
#endif
/*
* Check to see if the relocation offset is part of the .text segment.
* If not, we must change the offset to be relative to the .data section
* which is NOT contiguous.
*/
switch (secndx + 1) { /* change the bias */
case N_TEXT:
if ((long)rel->r_vaddr < cofffile->txtaddr ||
(long)rel->r_vaddr >
(long)(cofffile->txtaddr + cofffile->txtsize)) {
FatalError("Relocation against N_TEXT not in .text section\n");
}
dest32 = (unsigned long *)((long)(cofffile->saddr[secndx]) +
((unsigned char *)rel->r_vaddr -
cofffile->txtaddr));
break;
case N_DATA:
if ((long)rel->r_vaddr < cofffile->dataddr ||
(long)rel->r_vaddr >
(long)(cofffile->dataddr + cofffile->datsize)) {
FatalError("Relocation against N_DATA not in .data section\n");
}
dest32 = (unsigned long *)((long)(cofffile->saddr[secndx]) +
((unsigned char *)rel->r_vaddr -
cofffile->dataddr));
break;
case N_BSS:
if ((long)rel->r_vaddr < cofffile->bssaddr ||
(long)rel->r_vaddr >
(long)(cofffile->bssaddr + cofffile->bsssize)) {
FatalError("Relocation against N_TEXT not in .bss section\n");
}
dest32 = (unsigned long *)((long)(cofffile->saddr[secndx]) +
((unsigned char *)rel->r_vaddr -
cofffile->bssaddr));
break;
default:
FatalError("Relocation against unknown section %d\n", secndx);
}
if (symbol->n_sclass == 0) {
symval = (unsigned char *)(symbol->n_value + (*dest32) -
symbol->n_type);
#ifdef COFFDEBUG
COFFDEBUG("symbol->n_sclass==0\n");
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
*dest32 = (unsigned long)symval;
return 0;
}
switch (rel->r_type) {
#if defined(i386)
case R_DIR32:
symval = COFFGetSymbolValue(cofffile, rel->r_symndx);
if (symval) {
#ifdef COFFDEBUG
char *namestr;
COFFDEBUG("R_DIR32 %s\n",
namestr = COFFGetSymbolName(cofffile, rel->r_symndx));
xf86loaderfree(namestr);
COFFDEBUG("txtsize=%x\t", cofffile->txtsize);
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
*dest32 = (unsigned long)(symval + (*dest32) - symbol->n_value);
} else {
switch (symbol->n_scnum) {
case N_UNDEF:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_UNDEF\n");
#endif
return COFFDelayRelocation(cofffile, secndx, rel);
case N_ABS:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_ABS\n");
#endif
return 0;
case N_DEBUG:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_DEBUG\n");
#endif
return 0;
case N_COMMENT:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_COMMENT\n");
#endif
return 0;
case N_TEXT:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_TEXT\n");
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
(unsigned long)(cofffile->
saddr[N_TEXT - 1]));
break;
case N_DATA:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_DATA\n");
COFFDEBUG("txtsize=%x\t", cofffile->txtsize);
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
((unsigned long)(cofffile->
saddr[N_DATA -
1])) -
cofffile->dataddr);
break;
case N_BSS:
#ifdef COFFDEBUG
COFFDEBUG("R_DIR32 N_BSS\n");
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
(unsigned long)(cofffile->
saddr[N_BSS - 1]) -
(cofffile->bssaddr));
break;
default:
ErrorF("R_DIR32 with unexpected section %d\n",
symbol->n_scnum);
}
}
#ifdef COFFDEBUG
COFFDEBUG("*dest32=%8.8lx\n", *dest32);
#endif
break;
case R_PCRLONG:
if (symbol->n_scnum == N_TEXT)
break;
symval = COFFGetSymbolValue(cofffile, rel->r_symndx);
#ifdef COFFDEBUG
COFFDEBUG("R_PCRLONG ");
COFFDEBUG("dest32=%p\t", (void *)dest32);
COFFDEBUG("symval=%p\t", symval);
COFFDEBUG("*dest32=%8.8lx\t", *dest32);
#endif
if (symval == 0) {
#ifdef COFFDEBUG
char *name;
COFFDEBUG("***Unable to resolve symbol %s\n",
name = COFFGetSymbolName(cofffile, rel->r_symndx));
xf86loaderfree(name);
#endif
return COFFDelayRelocation(cofffile, secndx, rel);
}
*dest32 = (unsigned long)(symval - ((long)dest32 + sizeof(long)));
#ifdef COFFDEBUG
COFFDEBUG("*dest32=%8.8lx\n", *dest32);
#endif
break;
case R_ABS:
/*
* Nothing to really do here.
* Usually, a dummy relocation for .file
*/
break;
#endif /* i386 */
#if defined(__powerpc__)
case R_POS:
/*
* Positive Relocation
*/
if (RELOC_RLEN(*rel) != 0x1f)
FatalError("R_POS with size != 32 bits");
symval = COFFGetSymbolValue(cofffile, rel->r_symndx);
if (symval) {
#ifdef COFFDEBUG
COFFDEBUG("R_POS ");
COFFDEBUG("dest32=%x\t", dest32);
COFFDEBUG("symval=%x\t", symval);
COFFDEBUG("*dest32=%8.8x\t", *dest32);
#endif
*dest32 = (unsigned long)(symval + (*dest32) - symbol->n_value);
ppc_flush_icache(dest32);
} else {
switch (symbol->n_scnum) {
case N_UNDEF:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_UNDEF\n");
#endif
return COFFDelayRelocation(cofffile, secndx, rel);
case N_ABS:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_ABS\n");
#endif
return 0;
case N_DEBUG:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_DEBUG\n");
#endif
return 0;
case N_COMMENT:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_COMMENT\n");
#endif
return 0;
case N_TEXT:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_TEXT\n");
COFFDEBUG("dest32=%x\t", dest32);
COFFDEBUG("symval=%x\t", symval);
COFFDEBUG("*dest32=%8.8x\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
((unsigned long)(cofffile->
saddr[N_TEXT -
1])) -
cofffile->txtaddr);
ppc_flush_icache(dest32);
break;
case N_DATA:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_DATA\n");
COFFDEBUG("txtsize=%x\t", cofffile->txtsize);
COFFDEBUG("dest32=%x\t", dest32);
COFFDEBUG("symval=%x\t", symval);
COFFDEBUG("*dest32=%8.8x\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
((unsigned long)(cofffile->
saddr[N_DATA -
1])) -
cofffile->dataddr);
ppc_flush_icache(dest32);
break;
case N_BSS:
#ifdef COFFDEBUG
COFFDEBUG("R_POS N_BSS\n");
COFFDEBUG("dest32=%x\t", dest32);
COFFDEBUG("symval=%x\t", symval);
COFFDEBUG("*dest32=%8.8x\t", *dest32);
#endif
*dest32 = (unsigned long)((*dest32) +
(unsigned long)(cofffile->
saddr[N_BSS - 1]) -
(cofffile->bssaddr));
ppc_flush_icache(dest32);
break;
default:
ErrorF("R_POS with unexpected section %d\n", symbol->n_scnum);
}
}
#ifdef COFFDEBUG
COFFDEBUG("*dest32=%8.8x\t", *dest32);
COFFDEBUG("\n");
#endif
break;
case R_TOC:
/*
* Relative to TOC
*/
{
dest16 = (unsigned short *)dest32;
if (RELOC_RLEN(*rel) != 0x0f)
FatalError("R_TOC with size != 16 bits");
#ifdef COFFDEBUG
COFFDEBUG("R_TOC ");
COFFDEBUG("dest16=%x\t", dest16);
COFFDEBUG("symbol=%x\t", symbol);
COFFDEBUG("symbol->n_value=%x\t", symbol->n_value);
COFFDEBUG("cofffile->toc=%x\t", cofffile->toc);
COFFDEBUG("*dest16=%8.8x\t", *dest16);
#endif
*dest16 = (unsigned long)((symbol->n_value - cofffile->toc));
ppc_flush_icache(dest16);
}
#ifdef COFFDEBUG
COFFDEBUG("*dest16=%8.8x\t", *dest16);
COFFDEBUG("\n");
#endif
break;
case R_BR:
/*
* Branch relative to self, non-modifiable
*/
if (RELOC_RLEN(*rel) != 0x19)
FatalError("R_BR with size != 24 bits");
name = COFFGetSymbolName(cofffile, rel->r_symndx);
symitem = LoaderHashFind(name);
if (symitem == 0) {
name++;
symitem = LoaderHashFind(name);
}
if (symitem && cofffile->module != symitem->module) {
#ifdef COFFDEBUG
COFFDEBUG("Symbol module %d != file module %d\n",
symitem->module, cofffile->module);
#endif
symval = COFFGetSymbolGlinkValue(cofffile, rel->r_symndx);
} else
symval = COFFGetSymbolValue(cofffile, rel->r_symndx);
if (symval == 0) {
#ifdef COFFDEBUG
char *name;
COFFDEBUG("***Unable to resolve symbol %s\n",
name = COFFGetSymbolName(cofffile, rel->r_symndx));
xf86loaderfree(name);
#endif
return COFFDelayRelocation(cofffile, secndx, rel);
}
#ifdef COFFDEBUG
COFFDEBUG("R_BR ");
COFFDEBUG("dest32=%x\t", dest32);
COFFDEBUG("symval=%x\t", symval);
COFFDEBUG("*dest32=%8.8x\t", *dest32);
#endif
{
unsigned long val;
val = ((unsigned long)symval - (unsigned long)dest32);
#ifdef COFFDEBUG
COFFDEBUG("val=%8.8x\n", val);
#endif
val = val >> 2;
if ((val & 0x3f000000) != 0x3f000000 &&
(val & 0x3f000000) != 0x00000000) {
FatalError("R_BR offset %x too large\n", val << 2);
break;
}
val &= 0x00ffffff;
#ifdef COFFDEBUG
COFFDEBUG("val=%8.8x\n", val);
#endif
/*
* The address part contains the offset to the beginning
* of the .text section. Disreguard this since we have
* calculated the correct offset already.
*/
(*dest32) = ((*dest32) & 0xfc000003) | (val << 2);
#ifdef COFFDEBUG
COFFDEBUG("*dest32=%8.8x\n", *dest32);
#endif
if (cofffile->module != symitem->module) {
(*++dest32) = 0x80410014; /* lwz r2,20(r1) */
}
ppc_flush_icache(--dest32);
}
break;
#endif /* __powerpc__ */
default:
ErrorF("COFF_RelocateEntry() Unsupported relocation type %o\n",
rel->r_type);
break;
}
return 0;
}
static COFFRelocPtr
COFFCollectRelocations(COFFModulePtr cofffile)
{
unsigned short i, j;
RELOC *rel;
SCNHDR *sec;
COFFRelocPtr reloc_head = NULL;
COFFRelocPtr tmp;
for (i = 0; i < cofffile->numsh; i++) {
if (cofffile->saddr[i] == NULL)
continue; /* Section not loaded!! */
sec = &(cofffile->sections[i]);
for (j = 0; j < sec->s_nreloc; j++) {
rel = (RELOC *) (cofffile->reladdr[i] + (j * RELSZ));
tmp = COFFDelayRelocation(cofffile, i, rel);
tmp->next = reloc_head;
reloc_head = tmp;
}
}
return reloc_head;
}
/*
* COFF_GetSymbols()
*
* add the symbols to the symbol table maintained by the loader.
*/
static LOOKUP *
COFF_GetSymbols(COFFModulePtr cofffile)
{
SYMENT *sym;
AUXENT *aux = NULL;
int i, l, numsyms;
LOOKUP *lookup, *lookup_common, *p;
char *symname;
/*
* Load the symbols into memory
*/
numsyms = cofffile->header->f_nsyms;
#ifdef COFFDEBUG
COFFDEBUG("COFF_GetSymbols(): %d symbols\n", numsyms);
#endif
cofffile->symsize = (numsyms * SYMESZ);
cofffile->symtab =
(SYMENT *) _LoaderFileToMem(cofffile->fd,
cofffile->header->f_symptr,
(numsyms * SYMESZ), "symbols");
if ((lookup = xf86loadermalloc((numsyms + 1) * sizeof(LOOKUP))) == NULL)
return NULL;
for (i = 0, l = 0; i < numsyms; i++) {
sym = (SYMENT *) (((unsigned char *)cofffile->symtab) + (i * SYMESZ));
symname = COFFGetSymbolName(cofffile, i);
if (sym->n_numaux > 0)
aux = (AUXENT *) (((unsigned char *)cofffile->symtab) +
((i + 1) * SYMESZ));
else
aux = NULL;
#ifdef COFFDEBUG
COFFDEBUG("\t%d %d %lx %x %d %d %s\n",
i, sym->n_scnum, sym->n_value, sym->n_type,
sym->n_sclass, sym->n_numaux, symname);
if (aux)
COFFDEBUG("aux=\t%ld %lx %x %x %x %lx %x\n",
aux->x_scnlen, aux->x_parmhash, aux->x_snhash,
aux->x_smtyp, aux->x_smclas, aux->x_stab,
aux->x_snstab);
#endif
i += sym->n_numaux;
/*
* check for TOC csect before discarding C_HIDEXT below
*/
if (aux && aux->x_smclas == XMC_TC0) {
if (sym->n_scnum != N_DATA)
FatalError("TOC not in N_DATA section");
cofffile->toc = sym->n_value;
cofffile->tocaddr = (cofffile->saddr[sym->n_scnum - 1] +
sym->n_value - (cofffile->dataddr));
#ifdef COFFDEBUG
COFFDEBUG("TOC=%lx\n", cofffile->toc);
COFFDEBUG("TOCaddr=%p\n", cofffile->tocaddr);
#endif
continue;
}
if (sym->n_sclass == C_HIDEXT) {
/*
&& aux && !(aux->x_smclas == XMC_DS
&& aux->x_smtyp == XTY_SD) ) ) {
*/
#ifdef COFFDEBUG
COFFDEBUG("Skipping C_HIDEXT class symbol %s\n", symname);
#endif
continue;
}
switch (sym->n_scnum) {
case N_UNDEF:
if (sym->n_value != 0) {
char *name;
COFFCommonPtr tmp;
name = COFFGetSymbolName(cofffile, i);
#ifdef COFFDEBUG
COFFDEBUG("Adding COMMON space for %s\n", name);
#endif
if (!LoaderHashFind(name)) {
tmp = COFFAddCOMMON(sym, i);
if (tmp) {
tmp->next = listCOMMON;
listCOMMON = tmp;
}
}
xf86loaderfree(name);
}
xf86loaderfree(symname);
break;
case N_ABS:
case N_DEBUG:
case N_COMMENT:
#ifdef COFFDEBUG
COFFDEBUG("Freeing %s, section %d\n", symname, sym->n_scnum);
#endif
xf86loaderfree(symname);
break;
case N_TEXT:
if ((sym->n_sclass == C_EXT || sym->n_sclass == C_HIDEXT)
&& cofffile->saddr[sym->n_scnum - 1]) {
lookup[l].symName = symname;
lookup[l].offset = (funcptr)
(cofffile->saddr[sym->n_scnum - 1] +
sym->n_value - cofffile->txtaddr);
#ifdef COFFDEBUG
COFFDEBUG("Adding %p %s\n",
(void *)lookup[l].offset, lookup[l].symName);
#endif
l++;
} else {
#ifdef COFFDEBUG
COFFDEBUG("TEXT Section not loaded %d\n", sym->n_scnum - 1);
#endif
xf86loaderfree(symname);
}
break;
case N_DATA:
/*
* Note: COFF expects .data to be contiguous with
* .data, so that offsets for .data are relative to
* .text. We need to adjust for this, and make them
* relative to .data so that the relocation can be
* properly applied. This is needed becasue we allocate
* .data seperately from .text.
*/
if ((sym->n_sclass == C_EXT || sym->n_sclass == C_HIDEXT)
&& cofffile->saddr[sym->n_scnum - 1]) {
lookup[l].symName = symname;
lookup[l].offset = (funcptr)
(cofffile->saddr[sym->n_scnum - 1] +
sym->n_value - cofffile->dataddr);
#ifdef COFFDEBUG
COFFDEBUG("Adding %p %s\n",
(void *)lookup[l].offset, lookup[l].symName);
#endif
l++;
} else {
#ifdef COFFDEBUG
COFFDEBUG("DATA Section not loaded %d\n", sym->n_scnum - 1);
#endif
xf86loaderfree(symname);
}
break;
case N_BSS:
/*
* Note: COFF expects .bss to be contiguous with
* .data, so that offsets for .bss are relative to
* .text. We need to adjust for this, and make them
* relative to .bss so that the relocation can be
* properly applied. This is needed becasue we allocate
* .bss seperately from .text and .data.
*/
if ((sym->n_sclass == C_EXT || sym->n_sclass == C_HIDEXT)
&& cofffile->saddr[sym->n_scnum - 1]) {
lookup[l].symName = symname;
lookup[l].offset = (funcptr)
(cofffile->saddr[sym->n_scnum - 1] +
sym->n_value - cofffile->bssaddr);
#ifdef COFFDEBUG
COFFDEBUG("Adding %p %s\n",
(void *)lookup[l].offset, lookup[l].symName);
#endif
l++;
} else {
#ifdef COFFDEBUG
COFFDEBUG("BSS Section not loaded %d\n", sym->n_scnum - 1);
#endif
xf86loaderfree(symname);
}
break;
default:
ErrorF("Unknown Section number %d\n", sym->n_scnum);
xf86loaderfree(symname);
break;
}
}
lookup[l].symName = NULL; /* Terminate the list */
lookup_common = COFFCreateCOMMON(cofffile);
if (lookup_common) {
for (i = 0, p = lookup_common; p->symName; i++, p++) ;
memcpy(&(lookup[l]), lookup_common, i * sizeof(LOOKUP));
xf86loaderfree(lookup_common);
l += i;
lookup[l].symName = NULL;
}
/*
* remove the COFF symbols that will show up in every module
*/
for (i = 0, p = lookup; p->symName; i++, p++) {
while (p->symName && (!strcmp(lookup[i].symName, ".text")
|| !strcmp(lookup[i].symName, ".data")
|| !strcmp(lookup[i].symName, ".bss")
)) {
memmove(&(lookup[i]), &(lookup[i + 1]),
(l-- - i) * sizeof(LOOKUP));
}
}
return lookup;
}
#define SecOffset(index) cofffile->sections[index].s_scnptr
#define SecSize(index) cofffile->sections[index].s_size
#define SecAddr(index) cofffile->sections[index].s_paddr
#define RelOffset(index) cofffile->sections[index].s_relptr
#define RelSize(index) (cofffile->sections[index].s_nreloc*RELSZ)
/*
* COFFCollectSections
*
* Do the work required to load each section into memory.
*/
static void
COFFCollectSections(COFFModulePtr cofffile)
{
unsigned short i;
/*
* Find and identify all of the Sections
*/
#ifdef COFFDEBUG
COFFDEBUG("COFFCollectSections(): %d sections\n", cofffile->numsh);
#endif
for (i = 0; i < cofffile->numsh; i++) {
#ifdef COFFDEBUG
COFFDEBUG("%d %s\n", i, cofffile->sections[i].s_name);
#endif
/* .text */
if (strcmp(cofffile->sections[i].s_name, ".text") == 0) {
cofffile->text = _LoaderFileToMem(cofffile->fd,
SecOffset(i), SecSize(i),
".text");
cofffile->saddr[i] = cofffile->text;
cofffile->txtndx = i;
cofffile->txtaddr = SecAddr(i);
cofffile->txtsize = SecSize(i);
cofffile->txtrelsize = RelSize(i);
cofffile->reladdr[i] = _LoaderFileToMem(cofffile->fd,
RelOffset(i), RelSize(i),
".rel.text");
#ifdef COFFDEBUG
COFFDEBUG(".text starts at %p (%x bytes)\n", cofffile->text,
cofffile->txtsize);
#endif
continue;
}
/* .data */
if (strcmp(cofffile->sections[i].s_name, ".data") == 0) {
cofffile->data = _LoaderFileToMem(cofffile->fd,
SecOffset(i), SecSize(i),
".data");
cofffile->saddr[i] = cofffile->data;
cofffile->datndx = i;
cofffile->dataddr = SecAddr(i);
cofffile->datsize = SecSize(i);
cofffile->datrelsize = RelSize(i);
cofffile->reladdr[i] = _LoaderFileToMem(cofffile->fd,
RelOffset(i), RelSize(i),
".rel.data");
#ifdef COFFDEBUG
COFFDEBUG(".data starts at %p (%x bytes)\n", cofffile->data,
cofffile->datsize);
#endif
continue;
}
/* .bss */
if (strcmp(cofffile->sections[i].s_name, ".bss") == 0) {
if (SecSize(i))
cofffile->bss = xf86loadercalloc(1, SecSize(i));
else
cofffile->bss = NULL;
cofffile->saddr[i] = cofffile->bss;
cofffile->bssndx = i;
cofffile->bssaddr = SecAddr(i);
cofffile->bsssize = SecSize(i);
#ifdef COFFDEBUG
COFFDEBUG(".bss starts at %p (%x bytes)\n", cofffile->bss,
cofffile->bsssize);
#endif
continue;
}
/* .comment */
if (strncmp(cofffile->sections[i].s_name,
".comment", strlen(".comment")) == 0) {
continue;
}
/* .stab */
if (strcmp(cofffile->sections[i].s_name, ".stab") == 0) {
continue;
}
/* .stabstr */
if (strcmp(cofffile->sections[i].s_name, ".stabstr") == 0) {
continue;
}
/* .stab.* */
if (strncmp(cofffile->sections[i].s_name,
".stab.", strlen(".stab.")) == 0) {
continue;
}
ErrorF("COFF: Not loading %s\n", cofffile->sections[i].s_name);
}
}
/*
* Public API for the COFF implementation of the loader.
*/
void *
COFFLoadModule(loaderPtr modrec, int cofffd, LOOKUP **ppLookup)
{
COFFModulePtr cofffile;
FILHDR *header;
int stroffset; /* offset of string table */
COFFRelocPtr coff_reloc, tail;
void *v;
#ifdef COFFDEBUG
COFFDEBUG("COFFLoadModule(%s,%x,%x)\n", modrec->name, modrec->handle,
cofffd);
#endif
if ((cofffile = xf86loadercalloc(1, sizeof(COFFModuleRec))) == NULL) {
ErrorF("Unable to allocate COFFModuleRec\n");
return NULL;
}
cofffile->handle = modrec->handle;
cofffile->module = modrec->module;
cofffile->fd = cofffd;
v = cofffile->funcs = modrec->funcs;
/*
* Get the COFF header
*/
cofffile->header =
(FILHDR *) _LoaderFileToMem(cofffd, 0, sizeof(FILHDR), "header");
header = (FILHDR *) cofffile->header;
if (header->f_symptr == 0 || header->f_nsyms == 0) {
ErrorF("No symbols found in module\n");
_LoaderFreeFileMem(header, sizeof(FILHDR));
xf86loaderfree(cofffile);
return NULL;
}
/*
* Get the section table
*/
cofffile->numsh = header->f_nscns;
cofffile->secsize = (header->f_nscns * SCNHSZ);
cofffile->sections =
(SCNHDR *) _LoaderFileToMem(cofffd, FILHSZ + header->f_opthdr,
cofffile->secsize, "sections");
cofffile->saddr =
xf86loadercalloc(cofffile->numsh, sizeof(unsigned char *));
cofffile->reladdr =
xf86loadercalloc(cofffile->numsh, sizeof(unsigned char *));
/*
* Load the optional header if we need it ?????
*/
/*
* Load the rest of the desired sections
*/
COFFCollectSections(cofffile);
/*
* load the string table (must be done before we process symbols).
*/
stroffset = header->f_symptr + (header->f_nsyms * SYMESZ);
_LoaderFileRead(cofffd, stroffset, &(cofffile->strsize), sizeof(int));
stroffset += 4; /* Move past the size */
cofffile->strsize -= sizeof(int); /* size includes itself, so reduce by 4 */
cofffile->strtab =
_LoaderFileToMem(cofffd, stroffset, cofffile->strsize, "strings");
/*
* add symbols
*/
*ppLookup = COFF_GetSymbols(cofffile);
/*
* Do relocations
*/
coff_reloc = COFFCollectRelocations(cofffile);
if (coff_reloc) {
for (tail = coff_reloc; tail->next; tail = tail->next) ;
tail->next = _LoaderGetRelocations(v)->coff_reloc;
_LoaderGetRelocations(v)->coff_reloc = coff_reloc;
}
return (void *)cofffile;
}
void
COFFResolveSymbols(void *mod)
{
COFFRelocPtr newlist, p, tmp;
/* Try to relocate everything. Build a new list containing entries
* which we failed to relocate. Destroy the old list in the process.
*/
newlist = 0;
for (p = _LoaderGetRelocations(mod)->coff_reloc; p;) {
tmp = COFF_RelocateEntry(p->file, p->secndx, p->rel);
if (tmp) {
/* Failed to relocate. Keep it in the list. */
tmp->next = newlist;
newlist = tmp;
}
tmp = p;
p = p->next;
xf86loaderfree(tmp);
}
_LoaderGetRelocations(mod)->coff_reloc = newlist;
}
int
COFFCheckForUnresolved(void *mod)
{
char *name;
COFFRelocPtr crel;
int flag, fatalsym = 0;
if ((crel = _LoaderGetRelocations(mod)->coff_reloc) == NULL)
return 0;
while (crel) {
name = COFFGetSymbolName(crel->file, crel->rel->r_symndx);
flag = _LoaderHandleUnresolved(name,
_LoaderHandleToName(crel->file->
handle));
if (flag)
fatalsym = 1;
xf86loaderfree(name);
crel = crel->next;
}
return fatalsym;
}
void
COFFUnloadModule(void *modptr)
{
COFFModulePtr cofffile = (COFFModulePtr) modptr;
COFFRelocPtr relptr, reltptr, *brelptr;
/*
* Delete any unresolved relocations
*/
relptr = _LoaderGetRelocations(cofffile->funcs)->coff_reloc;
brelptr = &(_LoaderGetRelocations(cofffile->funcs)->coff_reloc);
while (relptr) {
if (relptr->file == cofffile) {
*brelptr = relptr->next; /* take it out of the list */
reltptr = relptr; /* save pointer to this node */
relptr = relptr->next; /* advance the pointer */
xf86loaderfree(reltptr); /* free the node */
} else {
brelptr = &(relptr->next);
relptr = relptr->next; /* advance the pointer */
}
}
/*
* Delete any symbols in the symbols table.
*/
LoaderHashTraverse((void *)cofffile, COFFhashCleanOut);
/*
* Free the sections that were allocated.
*/
#define CheckandFree(ptr,size) if(ptr) _LoaderFreeFileMem((ptr),(size))
CheckandFree(cofffile->strtab, cofffile->strsize);
CheckandFree(cofffile->symtab, cofffile->symsize);
CheckandFree(cofffile->text, cofffile->txtsize);
CheckandFree(cofffile->reladdr[cofffile->txtndx], cofffile->txtrelsize);
CheckandFree(cofffile->data, cofffile->datsize);
CheckandFree(cofffile->reladdr[cofffile->datndx], cofffile->datrelsize);
CheckandFree(cofffile->bss, cofffile->bsssize);
if (cofffile->common)
xf86loaderfree(cofffile->common);
/*
* Free the section table, and section pointer array
*/
_LoaderFreeFileMem(cofffile->sections, cofffile->secsize);
xf86loaderfree(cofffile->saddr);
xf86loaderfree(cofffile->reladdr);
_LoaderFreeFileMem(cofffile->header, sizeof(FILHDR));
/*
* Free the COFFModuleRec
*/
xf86loaderfree(cofffile);
return;
}
char *
COFFAddressToSection(void *modptr, unsigned long address)
{
COFFModulePtr cofffile = (COFFModulePtr) modptr;
int i;
for (i = 1; i < cofffile->numsh; i++) {
if (address >= (unsigned long)cofffile->saddr[i] &&
address <= (unsigned long)cofffile->saddr[i] + SecSize(i)) {
return cofffile->sections[i].s_name;
}
}
return NULL;
}
|