Created
December 10, 2016 23:57
-
-
Save AndrewPardoe/4f6770fee4853ccffb792e8f3e7951f1 to your computer and use it in GitHub Desktop.
Strip functions from MSVC /FAsc that don't exist in a dumpbin /symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $filename = @ARGV[0]; | |
system ("cl /Ox /Zc:inline /EHsc /FAsc /c $filename.cpp"); | |
system ("link /dump /symbols $filename.obj > $filename.dmp"); | |
# Collect the function names that are in the object binary | |
open DMP, "<./$filename.dmp"; | |
# Find the COFF symbol table, in case there's more in the object dump | |
while (<DMP>) | |
{ | |
last if (index($_, "COFF SYMBOL TABLE") != -1); | |
} | |
my %binary_funcs; | |
while (<DMP>) | |
{ | |
if (/^[[:xdigit:]]{3,8}\s+[[:xdigit:]]{8}\s+SECT[[:xdigit:]]+\s+notype\s\(\).*\|\s([\w\?@\$]+)/) | |
{ | |
@binary_funcs{$1} = $1; | |
} | |
} | |
close DMP; | |
#print join("\n", keys %binary_funcs); # What names were found in the dump? | |
# Find the same function names in text segments in the COD listing. | |
open COD, "<./$filename.cod"; | |
while (<COD>) | |
{ | |
if (/^_TEXT\s+SEGMENT$/) | |
{ | |
while (<COD>) | |
{ | |
last if /^_TEXT\s+ENDS$/; # stop if we're out of the text segment | |
if (/^([\w\?@\$]+)\s+PROC/) | |
{ | |
my $function = $1; | |
if (exists($binary_funcs{$function})) | |
{ | |
print; # print function name, e.g., _main PROC | |
while (<COD>) | |
{ | |
last if (/^$function\s+ENDP$/); | |
print; | |
} | |
print; # print function ENDP, e.g., __main ENDP | |
print "\n"; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment