-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffsyms.cpp
1038 lines (870 loc) · 23.8 KB
/
coffsyms.cpp
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
#include <windows.h>
#include <assert.h>
#include "helper.h"
#include "coffsyms.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PCOFF_STRING_TABLE STCreate (size_t cbST)
{
PCOFF_STRING_TABLE pST = (PCOFF_STRING_TABLE) malloc (cbST + sizeof(DWORD));
pST->cbST = cbST + sizeof(DWORD);
return pST;
}
PCOFF_STRING_TABLE STDuplicate (PCOFF_STRING_TABLE pOldST)
{
PCOFF_STRING_TABLE pST = STCreate (pOldST->cbST - sizeof(DWORD));
if (pST != NULL)
{
memcpy (&pST->szST[0], &pOldST->szST[0], pOldST->cbST - sizeof(DWORD));
}
return pST;
}
PSZ STGetStr (PCOFF_STRING_TABLE pST, DWORD Offs)
{
return (PSZ)pST + Offs;
}
void STDestroy (PCOFF_STRING_TABLE pST)
{
free (pST);
}
char* STGetFirstString (PCOFF_STRING_TABLE pST)
{
return &pST->szST[0];
}
char* STAddString (PCOFF_STRING_TABLE pST, char* pCurST, const char* szSymName, PDWORD pdwOffset)
{
strcpy (pCurST, szSymName);
*pdwOffset = (char*)pCurST - (char*)pST;
pCurST += strlen(pCurST) + 1;
if (pCurST > (char*)pST + pST->cbST)
__debugbreak();
return pCurST;
}
bool CoffIsSymNameLong (PIMAGE_SYMBOL Sym)
{
return (Sym->N.Name.Short == 0);
}
PSZ CoffSTGetSymbolName (PCOFF_STRING_TABLE pST, PIMAGE_SYMBOL Sym)
{
if (Sym->N.Name.Short)
return strndup ((PSZ)&Sym->N.ShortName[0], 8);
return _strdup(STGetStr (pST, Sym->N.Name.Long));
}
size_t STGetStringsLength (PCOFF_STRING_TABLE pST)
{
return pST->cbST - sizeof(DWORD);
}
size_t STGetFullLength (PCOFF_STRING_TABLE pST)
{
return pST->cbST;
}
DWORD STGetOffset (PCOFF_STRING_TABLE pST, char* szStr)
{
return (PSZ)szStr - (PSZ)pST;
}
void STStore (PCOFF_STRING_TABLE pST, void* pBuffer)
{
memcpy (pBuffer, pST, STGetFullLength (pST));
}
PCOFF_STRING_TABLE STFromStringVector (STRING_VECTOR &strs, OUT OFFSET_VECTOR &offsets)
{
size_t cbST = 0;
for (unsigned i=0; i<strs.size(); i++)
{
if (strs[i].size() > 8)
cbST += strs[i].size() + 1;
}
if (cbST == 0)
{
offsets.clear();
return EMPTY_STRING_TABLE;
}
PCOFF_STRING_TABLE pST = STCreate (cbST);
if (pST != NULL)
{
char* pCurPtr = STGetFirstString (pST);
for (unsigned i=0; i<strs.size(); i++)
{
DWORD offset = 0;
if (strs[i].size() > 8)
pCurPtr = STAddString (pST, pCurPtr, strs[i].c_str(), &offset);
offsets.push_back (offset);
}
}
return pST;
}
PIMAGE_SYMBOL CoffLookupSymbol (
std::vector<IMAGE_SYMBOL> &SymbolTable,
PCOFF_STRING_TABLE pST,
PSZ SymName
)
{
for (unsigned i=0; i<SymbolTable.size(); i++)
{
PIMAGE_SYMBOL Sym = &SymbolTable[i];
char* Name = NULL;
if (Sym->N.Name.Short != 0)
Name = (char*)&Sym->N.ShortName;
else
Name = STGetStr (pST, Sym->N.Name.Long);
if (!strcmp (Name, SymName))
return Sym;
}
return NULL;
}
DWORD
CoffLookupSymbolIndex (
PIMAGE_SYMBOL SymbolTable,
SIZE_T NumberOfSymbols,
PCOFF_STRING_TABLE pST,
PSZ SymName
)
{
for (unsigned i=0; i<NumberOfSymbols; i++)
{
PIMAGE_SYMBOL Sym = &SymbolTable[i];
char *Name = CoffSTGetSymbolName (pST, Sym);
if (strcmp (Name, SymName) != 0)
Sym = NULL;
FreeSymName (Name);
if (Sym != NULL)
return i;
}
return -1;
}
PCOFF_STRING_TABLE STCreatePartialStringTable(
std::vector<IMAGE_SYMBOL> PartialSymbolTable,
PCOFF_STRING_TABLE pOriginalST
)
{
SIZE_T cbST = 0;
for (unsigned i=0; i<PartialSymbolTable.size(); i++)
{
PIMAGE_SYMBOL ImgSym = &PartialSymbolTable[i];
// Add only long-named
if (ImgSym->N.Name.Short == 0)
{
PSZ szSymName = CoffSTGetSymbolName (pOriginalST, ImgSym);
cbST += strlen(szSymName) + 1;
FreeSymName (szSymName);
}
}
PCOFF_STRING_TABLE pST = STCreate (cbST);
if (pST != NULL)
{
char* pCurST = STGetFirstString (pST);
for (unsigned i=0; i<PartialSymbolTable.size(); i++)
{
PIMAGE_SYMBOL ImgSym = &PartialSymbolTable[i];
// Add only long-named
if (ImgSym->N.Name.Short == 0)
{
PSZ szSymName = CoffSTGetSymbolName (pOriginalST, ImgSym);
pCurST = STAddString (pST, pCurST, szSymName, &ImgSym->N.Name.Long);
FreeSymName (szSymName);
}
}
}
return pST;
}
//////////////////////////////////////////////////////////////////////////////
bool CoffBuildSymbolsFromStringMapAndOffsetMap (
SYMBOL_VECTOR &syms,
STRING_VECTOR &strs,
OFFSET_VECTOR &offsets
)
{
bool bEmptyStringTable = false;
if (offsets.size() == 0 && syms.size() != 0)
{
bEmptyStringTable = true;
}
else
{
if (syms.size() != offsets.size())
return false;
}
if (syms.size() != strs.size())
return false;
for (unsigned i=0; i<syms.size(); i++)
{
PIMAGE_SYMBOL Sym = &syms[i];
if (!bEmptyStringTable && offsets[i] != 0)
{
Sym->N.Name.Short = 0;
Sym->N.Name.Long = offsets[i];
}
else
{
strncpy ((char*)&Sym->N.ShortName[0], strs[i].c_str(), IMAGE_SIZEOF_SHORT_NAME);
}
i += Sym->NumberOfAuxSymbols;
}
return true;
}
//void CoffAddSymbols (SYMBOL_VECTOR &syms, PCOFF_STRING_TABLE pST, PCOFF_STRING_TABLE *pNewST, std::vector/
//
// Search symbol with the specified criteria
//
PIMAGE_SYMBOL CoffFindSpecialSymbol (
PIMAGE_SYMBOL SymbolTable, // pointer to symbol table
size_t cSyms, // number of symbols
std::vector<SYMBOL_SEARCH_CRITERIA> &crit, // vector of search criterias
bool bAndOperation , // = true if AND, = false for OR
PIMAGE_SYMBOL SymStartAt // symrec to start search at
)
{
unsigned iStartAt = 0;
if (SymStartAt) iStartAt = (SymStartAt - SymbolTable);
// Enum all symrecs
for (unsigned i=iStartAt; i<cSyms; i++)
{
PIMAGE_SYMBOL Sym = &SymbolTable[i];
bool bCondChainSucceeded = true;
// Enum all search crits
for (unsigned c=0; c<crit.size() && bCondChainSucceeded; c++)
{
PSYMBOL_SEARCH_CRITERIA Crit = &crit[c];
bool bConditionSucceeded = false;
switch (Crit->CritType)
{
case SSCT_VALUE: bConditionSucceeded = (Sym->Value == Crit->Value); break;
case SSCT_SECTION_NUMBER: bConditionSucceeded = (Sym->SectionNumber == Crit->SectionNumber); break;
case SSCT_TYPE: bConditionSucceeded = (Sym->Type == Crit->Type); break;
case SSCT_STORAGE_CLASS: bConditionSucceeded = (Sym->StorageClass == Crit->StorageClass); break;
default: bConditionSucceeded = false;
}
if (bAndOperation)
{
// Stop search when one check fails.
if (!bConditionSucceeded)
{
bCondChainSucceeded = false;
break;
}
}
else
{
// Stop search when one check succeeds.
if (!bCondChainSucceeded && bConditionSucceeded)
{
bCondChainSucceeded = true;
break;
}
}
} // for (criterias)
if (bCondChainSucceeded)
return Sym;
if (Sym->NumberOfAuxSymbols)
i += Sym->NumberOfAuxSymbols;
} // for (symbols)
return NULL;
}
//
// Find first symbol defined for the section
// (usually IMAGE_SYM_CLASS_with Value = 0 and SectionNumber set to section number)
//
PIMAGE_SYMBOL CoffFirstSymbolForSection (
PIMAGE_SYMBOL SymbolTable,
size_t cSyms,
SHORT SectionNumber
)
{
for (unsigned i=0; i<cSyms; i++)
{
PIMAGE_SYMBOL Sym = &SymbolTable[i];
if (Sym->SectionNumber == SectionNumber &&
Sym->StorageClass == IMAGE_SYM_CLASS_STATIC &&
Sym->Value == 0)
{
return Sym;
}
if (Sym->NumberOfAuxSymbols)
i += Sym->NumberOfAuxSymbols;
}
return NULL;
}
//
// Find next symbol defined for the section
//
PIMAGE_SYMBOL CoffNextSymbolForSection(
PIMAGE_SYMBOL SymbolTable,
size_t cSyms,
SHORT SectionNumber,
PIMAGE_SYMBOL Sym
)
{
for (unsigned i=(Sym - SymbolTable + Sym->NumberOfAuxSymbols + 1);
i<cSyms;
i++)
{
Sym = &SymbolTable[i];
if (Sym->SectionNumber == SectionNumber)
return Sym;
if (Sym->NumberOfAuxSymbols)
i += Sym->NumberOfAuxSymbols;
}
return NULL;
}
//
// Find COMDAT corresponding symbol record
//
PIMAGE_SYMBOL CoffFindComdatSymrecForSection (
PIMAGE_SYMBOL SymbolTable,
size_t cSyms,
SHORT SectionNumber,
OUT PIMAGE_SYMBOL *ppSectSym OPTIONAL // optionally return section symrec too
)
{
// Find first symbol for the section. It will be IMAGE_SYM_CLASS_section definition
// with one Aux symbol with section attributes.
PIMAGE_SYMBOL SectSym = CoffFirstSymbolForSection (SymbolTable, cSyms, SectionNumber);
PIMAGE_SYMBOL ComdatSym = NULL;
if (SectSym)
{
// Find second symbol for the section. It's our required symbol (if it's IMAGE_SYM_CLASS_too).
ComdatSym = CoffNextSymbolForSection (SymbolTable, cSyms, SectionNumber, SectSym);
if (ppSectSym)
*ppSectSym = SectSym;
}
return ComdatSym;
}
//
// Get pointer to symbol's aux symbols (if any)
//
PIMAGE_AUX_SYMBOL CoffGetAuxSymbols (PIMAGE_SYMBOL Sym)
{
if (!Sym->NumberOfAuxSymbols)
return NULL;
return (PIMAGE_AUX_SYMBOL)(Sym+1);
}
const char *szSectionAlign[] = {
0,
"1 bytes",
"2 bytes",
"4 bytes",
"8 bytes",
"16 bytes",
"32 bytes",
"64 bytes",
"128 bytes",
"256 bytes",
"512 bytes",
"1k bytes",
"2k bytes",
"4k bytes",
"8k bytes",
0
};
#define ALIGN_SHIFT 20
//
// Align Info: szSectionAlign[(Sect->Characteristics & IMAGE_SCN_ALIGN_MASK) >> ALIGN_SHIFT]
//
//
// Get COFF symbol pointers
//
bool CoffGetPointers (
IN PIMAGE_FILE_HEADER FileHdr,
OUT PIMAGE_SYMBOL *ppSymTbl,
OUT size_t *pcSyms OPTIONAL,
OUT PCOFF_STRING_TABLE *ppST OPTIONAL
)
{
PIMAGE_SYMBOL SymbolTable = NULL;
PCOFF_STRING_TABLE pST = 0;
size_t cSyms = 0;
// Get symbol table
if (FileHdr->NumberOfSymbols && FileHdr->PointerToSymbolTable)
{
cSyms = FileHdr->NumberOfSymbols;
SymbolTable = (PIMAGE_SYMBOL)((PSZ)FileHdr + FileHdr->PointerToSymbolTable);
pST = (PCOFF_STRING_TABLE)(&SymbolTable[cSyms]);
}
*ppSymTbl = SymbolTable;
if (pcSyms)
*pcSyms = cSyms;
if (ppST)
*ppST = pST;
return (SymbolTable != NULL);
}
//
// Dump aux symbols
//
void CoffDumpAuxSymbols (PIMAGE_FILE_HEADER FileHdr, PIMAGE_SYMBOL Sym)
{
PIMAGE_SYMBOL SymbolTable;
size_t cSyms;
PCOFF_STRING_TABLE pST = 0;
assert (CoffGetPointers (FileHdr, &SymbolTable, &cSyms, &pST));
PIMAGE_SECTION_HEADER Sections = (PIMAGE_SECTION_HEADER)(FileHdr+1);
PIMAGE_AUX_SYMBOL AuxSym = CoffGetAuxSymbols (Sym);
if (!AuxSym)
return;
size_t cAuxSyms = Sym->NumberOfAuxSymbols;
printf(" ");
switch (Sym->StorageClass)
{
case IMAGE_SYM_CLASS_EXTERNAL:
if ((Sym->Type & 0x20) // T_FUNCTION
&& Sym->SectionNumber > 0)
{
printf("[Func Def] ");
printf("#.bf-symrec %8x, CodeSize %8x, #LineNumber %8x, #NextFunc %8x\n",
AuxSym->Sym.TagIndex,
AuxSym->Sym.Misc.TotalSize,
AuxSym->Sym.FcnAry.Function.PointerToLinenumber,
AuxSym->Sym.FcnAry.Function.PointerToNextFunction
);
}
else if (Sym->SectionNumber == IMAGE_SYM_UNDEFINED && Sym->Value == 0)
{
printf("[Weak External] ");
static const char* szWeakExternSearchTypes[] = {
0,
"No library search for sym1",
"Library search for sym1",
"sym1 is an alias for sym2"
};
DWORD Type = ((PIMAGE_AUX_SYMBOL_EX)AuxSym)->Sym.WeakSearchType;
printf("#symrec2 %8x, Flags: %s\n", AuxSym->Sym.TagIndex,
(Type > 3) ? "<unknown-search-flags>" : szWeakExternSearchTypes[Type]
);
}
else
{
printf("[type unrecognized]\n");
}
break;
case IMAGE_SYM_CLASS_FUNCTION:
{
char* Name = CoffSTGetSymbolName (pST, Sym);
if (!_stricmp (Name, ".bf") || !_stricmp (Name, ".ef"))
{
printf("[%s funcsym] ", Name);
printf("LineNumber: %8x", AuxSym->Sym.Misc.LnSz.Linenumber);
if (!_stricmp (Name, ".bf"))
{
printf(", #NextFunction: %8x", AuxSym->Sym.FcnAry.Function.PointerToNextFunction);
}
printf("\n");
}
else
{
printf("[type unrecognized]\n");
}
FreeSymName (Name);
}
break;
case IMAGE_SYM_CLASS_FILE:
{
char* Name = strndup ((char*)AuxSym->File.Name, IMAGE_SIZEOF_SYMBOL);
printf("[File] %s\n", Name);
free (Name);
}
break;
case IMAGE_SYM_CLASS_STATIC:
if (Sym->Value == 0 && Sym->SectionNumber > 0)
{
printf("[Section] Length %8x, #relocs: %8x, #LineNums: %8x",
AuxSym->Section.Length,
AuxSym->Section.NumberOfRelocations,
AuxSym->Section.NumberOfLinenumbers);
if ((Sections[Sym->SectionNumber-1].Characteristics & IMAGE_SCN_LNK_COMDAT) && Sym->Type == IMAGE_SYM_TYPE_NULL)
{
printf("; COMDAT: CheckSum: %8x ", AuxSym->Section.CheckSum);
static const char* szComdatSelectionTypes[] = {
"<unknown-0x00>",
"no duplicates",
"any",
"same size",
"exact match",
"associative",
"largest",
"newest"
};
printf("(");
if (AuxSym->Section.Selection <= IMAGE_COMDAT_SELECT_NEWEST)
{
printf("%s", szComdatSelectionTypes[AuxSym->Section.Selection]);
if (AuxSym->Section.Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
{
printf(" for section #%lx", AuxSym->Section.Number);
unsigned iSect = AuxSym->Section.Number-1;
if (iSect < FileHdr->NumberOfSections)
{
char* Name = strndup ((char*)Sections[iSect].Name, IMAGE_SIZEOF_SHORT_NAME);
printf(" name '%s'", Name);
free (Name);
}
} // if (associative)
} // if (valid COMDAT selection)
printf(")");
} // if (COMDAT)
printf("\n");
} // if (section)
else
{
printf("[type unrecognized]\n");
}
break;
default:
printf("[class unrecognized]\n");
break;
}
}
void CoffGetSzSectionNumber (SHORT SectionNumber, OUT char* pBuf)
{
static const char* szHexDigits = "0123456789ABCDEF";
if (SectionNumber < 16)
*(pBuf++) = szHexDigits[(SectionNumber & 0xf)];
else if (SectionNumber < 256)
{
*(pBuf++) = szHexDigits[((SectionNumber >> 4) & 0xf)];
*(pBuf++) = szHexDigits[(SectionNumber & 0xf)];
}
else if (SectionNumber < 4096)
{
*(pBuf++) = szHexDigits[((SectionNumber >> 8) & 0xf)];
*(pBuf++) = szHexDigits[((SectionNumber >> 4) & 0xf)];
*(pBuf++) = szHexDigits[(SectionNumber & 0xf)];
}
else
{
*(pBuf++) = szHexDigits[((SectionNumber >> 12) & 0xf)];
*(pBuf++) = szHexDigits[((SectionNumber >> 8) & 0xf)];
*(pBuf++) = szHexDigits[((SectionNumber >> 4) & 0xf)];
*(pBuf++) = szHexDigits[(SectionNumber & 0xf)];
}
*pBuf = 0;
}
//
// Basic symbol types
//
static const char* szSymBasicTypes[] = {
"notype",
"void",
"char",
"short",
"int",
"long",
"float",
"double",
"struct",
"union",
"enum",
"moe",
"byte",
"word",
"uint",
"dword"
};
// Derived types
static const char* szSymDerivedTypes[] = {
" ",
"* ",
"( )",
"[ ]"
};
// Symbol storage classes
static const char* szSymStorageClasses[] = {
"Null",
"Auto",
"External",
"Static",
"Register",
"ExternalDef",
"Label",
"UndefLabel",
"MemberOfStruct",
"Argument",
"StructTag",
"MemberOfUnion",
"UnionTag",
"TypeDefinition",
"UndefinedStatic",
"EnumTag",
"MemberOfEnum",
"RegisterParam",
"BitField",
};
void CoffDumpSymbolType (
PIMAGE_SYMBOL Sym
)
{
WORD Type = (Sym->Type & ~IMAGE_SYM_TYPE_PCODE);
WORD BasicType = BTYPE(Type);
WORD SpecialType = ((Type & N_TMASK) >> N_BTSHFT);
bool bPCode = !!(Sym->Type & IMAGE_SYM_TYPE_PCODE);
if (bPCode) printf("(*) ");
printf("%-8s %s ", szSymBasicTypes[BasicType], szSymDerivedTypes[SpecialType]);
}
PCSZ CoffGetSymStorageClass (
PIMAGE_SYMBOL Sym
)
{
if (Sym->StorageClass == IMAGE_SYM_CLASS_END_OF_FUNCTION)
return "EndOfFunction";
if (Sym->StorageClass <= IMAGE_SYM_CLASS_BIT_FIELD)
return szSymStorageClasses[Sym->StorageClass];
if (Sym->StorageClass == IMAGE_SYM_CLASS_FAR_EXTERNAL)
return "FarExternal";
if (Sym->StorageClass >= 0x64 && Sym->StorageClass <= 0x6b)
{
static const char* szExtClass[] = {
"Block",
"Function",
"EndOfStruct",
"File",
"Section",
"WeakExternal",
"Unknown-0x6a",
"CLR-token"
};
return szExtClass[Sym->StorageClass - 0x64];
}
return "Unknown";
}
//
// Dump COFF symbol
//
void CoffDumpSymbol (
PIMAGE_FILE_HEADER FileHdr,
PIMAGE_SYMBOL Sym,
unsigned iSym,
PCOFF_STRING_TABLE pST
)
{
char* Name = CoffSTGetSymbolName (pST, Sym);
char *szSectCode = "UNKNOWN";
char szSectIndex[4+4+1] = {'S', 'E', 'C', 'T', ' ', ' ', ' ', ' ', 0};
if (Sym->SectionNumber > 0)
{
CoffGetSzSectionNumber (Sym->SectionNumber, &szSectIndex[4]);
szSectCode = &szSectIndex[0];
}
else if (Sym->SectionNumber == IMAGE_SYM_UNDEFINED)
szSectCode = "UNDEF";
else if (Sym->SectionNumber == IMAGE_SYM_DEBUG)
szSectCode = "DEBUG";
else if (Sym->SectionNumber == IMAGE_SYM_ABSOLUTE)
szSectCode = "ABS";
printf("%8x %08x %-8s ", iSym, Sym->Value, szSectCode);
CoffDumpSymbolType (Sym);
printf("%-15s ", CoffGetSymStorageClass (Sym));
printf("| %s\n", Name);
CoffDumpAuxSymbols (FileHdr, Sym);
FreeSymName (Name);
}
//
// Dump Symbol Table
void CoffDumpSymbolTable (
PIMAGE_FILE_HEADER FileHdr
)
{
PIMAGE_SYMBOL SymbolTable = 0;
size_t cSyms = 0;
PCOFF_STRING_TABLE pST = 0;
assert (CoffGetPointers (FileHdr, &SymbolTable, &cSyms, &pST));
printf ("COFF SYMBOL TABLE\n");
for (unsigned i=0; i<cSyms; i++)
{
PIMAGE_SYMBOL Sym = &SymbolTable[i];
CoffDumpSymbol (FileHdr, Sym, i, pST);
if (Sym->NumberOfAuxSymbols)
i += Sym->NumberOfAuxSymbols;
}
printf("\n");
}
//
// Dump COFF section information
//
void CoffDumpSectionFlags (
PIMAGE_FILE_HEADER FileHdr,
unsigned iSect,
PIMAGE_SECTION_HEADER Sect
)
{
PIMAGE_SYMBOL SymbolTable = NULL;
PCOFF_STRING_TABLE pST = 0;
size_t cSyms = 0;
assert (CoffGetPointers (FileHdr, &SymbolTable, &cSyms, &pST) != false);
ULONG ci = Sect->Characteristics & 0xf0; // content information
ULONG li = Sect->Characteristics & 0x3f00; // linker information
ULONG oi = Sect->Characteristics & 0xFF000000; // options
ULONG ai = Sect->Characteristics & IMAGE_SCN_ALIGN_MASK; // alignment information
if (ci & IMAGE_SCN_CNT_CODE)
printf(" Code\n");
if (ci & IMAGE_SCN_CNT_INITIALIZED_DATA)
printf(" Data\n");
if (ci & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
printf(" Uninit Data\n");
if (li & IMAGE_SCN_LNK_INFO)
printf(" Link info\n");
if (li & IMAGE_SCN_LNK_REMOVE)
printf(" Linker Removeable\n");
if (li & IMAGE_SCN_LNK_COMDAT)
{
printf(" COMDAT ");
PIMAGE_SYMBOL SectSym = 0;
PIMAGE_SYMBOL ComdatSym = CoffFindComdatSymrecForSection (SymbolTable, cSyms, iSect+1, &SectSym);
if (!ComdatSym)
printf("(no symbol)");
else
{
PSZ szComdatSymName = CoffSTGetSymbolName (pST, ComdatSym);
printf("(%s)", szComdatSymName);
FreeSymName (szComdatSymName);
}
printf("\n");
}
if (oi & IMAGE_SCN_MEM_DISCARDABLE)
printf(" Discardable\n");
if (oi & IMAGE_SCN_MEM_NOT_CACHED)
printf(" Not cached\n");
if (oi & IMAGE_SCN_MEM_NOT_PAGED)
printf(" Not paged\n");
if (oi & IMAGE_SCN_MEM_SHARED)
printf(" Shared Section\n");
if (oi & IMAGE_SCN_MEM_EXECUTE)
printf(" Executable\n");
if (oi & IMAGE_SCN_MEM_READ)
printf(" Readable\n");
if (oi & IMAGE_SCN_MEM_WRITE)
printf(" Writeable\n");
if (oi & IMAGE_SCN_LNK_NRELOC_OVFL)
printf(" Extended Relocations\n");
if (oi & IMAGE_SCN_SCALE_INDEX)
printf(" TLS index is scaled\n");
if (ai != 0)
printf(" Alignment: %s\n", szSectionAlign[ai >> ALIGN_SHIFT]);
}
static const char* szRelocTypes[] = {
/* 00 */ "ABSOLUTE",
/* 01 */ "DIR16",
/* 02 */ "REL16",
/* 03 */ "UnkRel-3",
/* 04 */ "UnkRel-4",
/* 05 */ "UnkRel-5",
/* 06 */ "DIR32",
/* 07 */ "DIR32NB",
/* 08 */ "UnkRel-8",
/* 09 */ "SEG12",
/* 0A */ "SECTION",
/* 0B */ "SECREL",
/* 0C */ "TOKEN",
/* 0D */ "SECREL7",
/* 0E */ "UnkRel-E",
/* 0F */ "UnkRel-F",
/* 10 */ "UnkRel-10",
/* 11 */ "UnkRel-11",
/* 12 */ "UnkRel-12",
/* 13 */ "UnkRel-13",
/* 14 */ "REL32",
};
// Dump COFF Relocation
void CoffDumpRelocation (PIMAGE_FILE_HEADER FileHdr, PIMAGE_SECTION_HEADER Sect, unsigned iReloc, PIMAGE_RELOCATION Reloc)
{
printf("%08x %10s %08x %-8x ",
iReloc * sizeof(IMAGE_RELOCATION),
Reloc->Type <= 0x14 ? szRelocTypes[Reloc->Type] : "UnkRel",
Reloc->VirtualAddress,
Reloc->SymbolTableIndex
);
PIMAGE_SYMBOL SymbolTable = NULL;
PCOFF_STRING_TABLE pST = 0;
size_t cSyms = 0;
assert (CoffGetPointers (FileHdr, &SymbolTable, &cSyms, &pST) != false);
PIMAGE_SYMBOL Sym = &SymbolTable[Reloc->SymbolTableIndex];
char* Name = CoffSTGetSymbolName (pST, Sym);
printf("%s\n", Name);
FreeSymName (Name);
}
// Dump COFF Relocations
void CoffDumpRelocations (PIMAGE_FILE_HEADER FileHdr, PIMAGE_SECTION_HEADER Sect)
{
if (Sect->NumberOfRelocations && Sect->PointerToRelocations)
{
PIMAGE_RELOCATION Relocations = (PIMAGE_RELOCATION)((PSZ)FileHdr + Sect->PointerToRelocations);
size_t cRelocs = Sect->NumberOfRelocations;
for (unsigned i=0; i<cRelocs; i++)
{
CoffDumpRelocation (FileHdr, Sect, i, &Relocations[i]);
}
}
}
//
// Dump COFF Section Table
//
void CoffDumpSections (PIMAGE_FILE_HEADER FileHdr)
{
PIMAGE_SYMBOL SymbolTable = NULL;
PCOFF_STRING_TABLE pST = 0;
size_t cSyms = 0;
assert (CoffGetPointers (FileHdr, &SymbolTable, &cSyms, &pST) != false);
PIMAGE_SECTION_HEADER Sections = (PIMAGE_SECTION_HEADER)(FileHdr+1);
size_t cSects = FileHdr->NumberOfSections;
printf("COFF Sections\n"
"-------------\n\n");
for (unsigned i=0; i<cSects; i++)
{
PIMAGE_SECTION_HEADER Sect = &Sections[i];
char* Name = strndup ((char*)&Sect->Name[0], IMAGE_SIZEOF_SHORT_NAME);
printf("SECTION HEADER #%x\n", i);
printf("%s Name\n", Name);
printf("%8x physical address\n", Sect->Misc.PhysicalAddress);
printf("%8x virtual address\n", Sect->VirtualAddress);
printf("%8x size of raw data\n", Sect->SizeOfRawData);
printf("%8x pointer to raw data\n", Sect->PointerToRawData);
printf("%8x pointer to relocations\n", Sect->PointerToRelocations);
printf("%8x pointer to line numbers\n", Sect->PointerToLinenumbers);
printf(" %8x number of relocations\n", Sect->NumberOfRelocations);
printf(" %8x number of line numbers\n", Sect->NumberOfLinenumbers);
printf("%8x flags\n", Sect->Characteristics);
CoffDumpSectionFlags (FileHdr, i, Sect);
printf("\n");
printf("COFF RELOCATIONS\n");
CoffDumpRelocations (FileHdr, Sect);
// printf(" Symbols for this section:\n");
//
// PIMAGE_SYMBOL Sym = CoffFirstSymbolForSection (SymbolTable, cSyms, i+1);
// if (Sym != NULL)
// {
// // Enum all symbols for this section
// for ( ; Sym != NULL; Sym = CoffNextSymbolForSection (SymbolTable, cSyms, i+1, Sym))
// {
// }
// }
}
}