mirror of
https://github.com/isledecomp/isle.git
synced 2026-01-24 00:31:16 +00:00
Move Bin and SymInfo into their own files
This commit is contained in:
parent
e5b2adda6b
commit
f779fa1557
@ -1,3 +1,5 @@
|
|||||||
|
from .bin import *
|
||||||
from .dir import *
|
from .dir import *
|
||||||
from .utils import *
|
|
||||||
from .parser import *
|
from .parser import *
|
||||||
|
from .syminfo import *
|
||||||
|
from .utils import *
|
||||||
|
|||||||
47
tools/isledecomp/isledecomp/bin.py
Normal file
47
tools/isledecomp/isledecomp/bin.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
|
# Declare a class that can automatically convert virtual executable addresses
|
||||||
|
# to file addresses
|
||||||
|
class Bin:
|
||||||
|
def __init__(self, filename, logger):
|
||||||
|
self.logger = logger
|
||||||
|
self.logger.debug('Parsing headers of "%s"... ', filename)
|
||||||
|
self.filename = filename
|
||||||
|
self.file = None
|
||||||
|
self.imagebase = None
|
||||||
|
self.textvirt = None
|
||||||
|
self.textraw = None
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.logger.debug(f"Bin {self.filename} Enter")
|
||||||
|
self.file = open(self.filename, "rb")
|
||||||
|
|
||||||
|
# HACK: Strictly, we should be parsing the header, but we know where
|
||||||
|
# everything is in these two files so we just jump straight there
|
||||||
|
|
||||||
|
# Read ImageBase
|
||||||
|
self.file.seek(0xB4)
|
||||||
|
(self.imagebase,) = struct.unpack("<i", self.file.read(4))
|
||||||
|
|
||||||
|
# Read .text VirtualAddress
|
||||||
|
self.file.seek(0x184)
|
||||||
|
(self.textvirt,) = struct.unpack("<i", self.file.read(4))
|
||||||
|
|
||||||
|
# Read .text PointerToRawData
|
||||||
|
self.file.seek(0x18C)
|
||||||
|
(self.textraw,) = struct.unpack("<i", self.file.read(4))
|
||||||
|
self.logger.debug("... Parsing finished")
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||||
|
self.logger.debug(f"Bin {self.filename} Exit")
|
||||||
|
if self.file:
|
||||||
|
self.file.close()
|
||||||
|
|
||||||
|
def get_addr(self, virt):
|
||||||
|
return virt - self.imagebase - self.textvirt + self.textraw
|
||||||
|
|
||||||
|
def read(self, offset, size):
|
||||||
|
self.file.seek(self.get_addr(offset))
|
||||||
|
return self.file.read(size)
|
||||||
138
tools/isledecomp/isledecomp/syminfo.py
Normal file
138
tools/isledecomp/isledecomp/syminfo.py
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
from .utils import get_file_in_script_dir
|
||||||
|
|
||||||
|
|
||||||
|
class RecompiledInfo:
|
||||||
|
addr = None
|
||||||
|
size = None
|
||||||
|
name = None
|
||||||
|
start = None
|
||||||
|
|
||||||
|
|
||||||
|
# Declare a class that parses the output of cvdump for fast access later
|
||||||
|
class SymInfo:
|
||||||
|
funcs = {}
|
||||||
|
lines = {}
|
||||||
|
names = {}
|
||||||
|
|
||||||
|
def __init__(self, pdb, sym_recompfile, sym_logger, sym_wine_path_converter=None):
|
||||||
|
self.logger = sym_logger
|
||||||
|
call = [get_file_in_script_dir("cvdump.exe"), "-l", "-s"]
|
||||||
|
|
||||||
|
if sym_wine_path_converter:
|
||||||
|
# Run cvdump through wine and convert path to Windows-friendly wine path
|
||||||
|
call.insert(0, "wine")
|
||||||
|
call.append(sym_wine_path_converter.get_wine_path(pdb))
|
||||||
|
else:
|
||||||
|
call.append(pdb)
|
||||||
|
|
||||||
|
self.logger.info("Parsing %s ...", pdb)
|
||||||
|
self.logger.debug("Command = %s", call)
|
||||||
|
line_dump = subprocess.check_output(call).decode("utf-8").split("\r\n")
|
||||||
|
|
||||||
|
current_section = None
|
||||||
|
|
||||||
|
self.logger.debug("Parsing output of cvdump.exe ...")
|
||||||
|
|
||||||
|
for i, line in enumerate(line_dump):
|
||||||
|
if line.startswith("***"):
|
||||||
|
current_section = line[4:]
|
||||||
|
|
||||||
|
if current_section == "SYMBOLS" and "S_GPROC32" in line:
|
||||||
|
sym_addr = int(line[26:34], 16)
|
||||||
|
|
||||||
|
info = RecompiledInfo()
|
||||||
|
info.addr = (
|
||||||
|
sym_addr + sym_recompfile.imagebase + sym_recompfile.textvirt
|
||||||
|
)
|
||||||
|
|
||||||
|
use_dbg_offs = False
|
||||||
|
if use_dbg_offs:
|
||||||
|
debug_offs = line_dump[i + 2]
|
||||||
|
debug_start = int(debug_offs[22:30], 16)
|
||||||
|
debug_end = int(debug_offs[43:], 16)
|
||||||
|
|
||||||
|
info.start = debug_start
|
||||||
|
info.size = debug_end - debug_start
|
||||||
|
else:
|
||||||
|
info.start = 0
|
||||||
|
info.size = int(line[41:49], 16)
|
||||||
|
|
||||||
|
info.name = line[77:]
|
||||||
|
|
||||||
|
self.names[info.name] = info
|
||||||
|
self.funcs[sym_addr] = info
|
||||||
|
elif (
|
||||||
|
current_section == "LINES"
|
||||||
|
and line.startswith(" ")
|
||||||
|
and not line.startswith(" ")
|
||||||
|
):
|
||||||
|
sourcepath = line.split()[0]
|
||||||
|
|
||||||
|
if sym_wine_path_converter:
|
||||||
|
# Convert filename to Unix path for file compare
|
||||||
|
sourcepath = sym_wine_path_converter.get_unix_path(sourcepath)
|
||||||
|
|
||||||
|
if sourcepath not in self.lines:
|
||||||
|
self.lines[sourcepath] = {}
|
||||||
|
|
||||||
|
j = i + 2
|
||||||
|
while True:
|
||||||
|
ll = line_dump[j].split()
|
||||||
|
if len(ll) == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
k = 0
|
||||||
|
while k < len(ll):
|
||||||
|
linenum = int(ll[k + 0])
|
||||||
|
address = int(ll[k + 1], 16)
|
||||||
|
if linenum not in self.lines[sourcepath]:
|
||||||
|
self.lines[sourcepath][linenum] = address
|
||||||
|
k += 2
|
||||||
|
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
self.logger.debug("... Parsing output of cvdump.exe finished")
|
||||||
|
|
||||||
|
def get_recompiled_address(self, filename, line):
|
||||||
|
recompiled_addr = None
|
||||||
|
|
||||||
|
self.logger.debug("Looking for %s:%s", filename, line)
|
||||||
|
filename_basename = os.path.basename(filename).lower()
|
||||||
|
|
||||||
|
for fn in self.lines:
|
||||||
|
# Sometimes a PDB is compiled with a relative path while we always have
|
||||||
|
# an absolute path. Therefore we must
|
||||||
|
try:
|
||||||
|
if os.path.basename(
|
||||||
|
fn
|
||||||
|
).lower() == filename_basename and os.path.samefile(fn, filename):
|
||||||
|
filename = fn
|
||||||
|
break
|
||||||
|
except FileNotFoundError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if filename in self.lines and line in self.lines[filename]:
|
||||||
|
recompiled_addr = self.lines[filename][line]
|
||||||
|
|
||||||
|
if recompiled_addr in self.funcs:
|
||||||
|
return self.funcs[recompiled_addr]
|
||||||
|
self.logger.error(
|
||||||
|
"Failed to find function symbol with address: %x", recompiled_addr
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
self.logger.error(
|
||||||
|
"Failed to find function symbol with filename and line: %s:%s",
|
||||||
|
filename,
|
||||||
|
line,
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_recompiled_address_from_name(self, name):
|
||||||
|
self.logger.debug("Looking for %s", name)
|
||||||
|
|
||||||
|
if name in self.names:
|
||||||
|
return self.names[name]
|
||||||
|
self.logger.error("Failed to find function symbol with name: %s", name)
|
||||||
|
return None
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import struct
|
import os
|
||||||
|
import sys
|
||||||
import colorama
|
import colorama
|
||||||
|
|
||||||
|
|
||||||
@ -24,57 +24,8 @@ def print_diff(udiff, plain):
|
|||||||
return has_diff
|
return has_diff
|
||||||
|
|
||||||
|
|
||||||
# Declare a class that can automatically convert virtual executable addresses
|
def get_file_in_script_dir(fn):
|
||||||
# to file addresses
|
return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), fn)
|
||||||
class Bin:
|
|
||||||
def __init__(self, filename, logger):
|
|
||||||
self.logger = logger
|
|
||||||
self.logger.debug('Parsing headers of "%s"... ', filename)
|
|
||||||
self.filename = filename
|
|
||||||
self.file = None
|
|
||||||
self.imagebase = None
|
|
||||||
self.textvirt = None
|
|
||||||
self.textraw = None
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
self.logger.debug(f"Bin {self.filename} Enter")
|
|
||||||
self.file = open(self.filename, "rb")
|
|
||||||
|
|
||||||
# HACK: Strictly, we should be parsing the header, but we know where
|
|
||||||
# everything is in these two files so we just jump straight there
|
|
||||||
|
|
||||||
# Read ImageBase
|
|
||||||
self.file.seek(0xB4)
|
|
||||||
(self.imagebase,) = struct.unpack("<i", self.file.read(4))
|
|
||||||
|
|
||||||
# Read .text VirtualAddress
|
|
||||||
self.file.seek(0x184)
|
|
||||||
(self.textvirt,) = struct.unpack("<i", self.file.read(4))
|
|
||||||
|
|
||||||
# Read .text PointerToRawData
|
|
||||||
self.file.seek(0x18C)
|
|
||||||
(self.textraw,) = struct.unpack("<i", self.file.read(4))
|
|
||||||
self.logger.debug("... Parsing finished")
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
||||||
self.logger.debug(f"Bin {self.filename} Exit")
|
|
||||||
if self.file:
|
|
||||||
self.file.close()
|
|
||||||
|
|
||||||
def get_addr(self, virt):
|
|
||||||
return virt - self.imagebase - self.textvirt + self.textraw
|
|
||||||
|
|
||||||
def read(self, offset, size):
|
|
||||||
self.file.seek(self.get_addr(offset))
|
|
||||||
return self.file.read(size)
|
|
||||||
|
|
||||||
|
|
||||||
class RecompiledInfo:
|
|
||||||
addr = None
|
|
||||||
size = None
|
|
||||||
name = None
|
|
||||||
start = None
|
|
||||||
|
|
||||||
|
|
||||||
class OffsetPlaceholderGenerator:
|
class OffsetPlaceholderGenerator:
|
||||||
|
|||||||
@ -7,15 +7,14 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from isledecomp import (
|
from isledecomp import (
|
||||||
Bin,
|
Bin,
|
||||||
find_code_blocks,
|
find_code_blocks,
|
||||||
|
get_file_in_script_dir,
|
||||||
OffsetPlaceholderGenerator,
|
OffsetPlaceholderGenerator,
|
||||||
print_diff,
|
print_diff,
|
||||||
RecompiledInfo,
|
SymInfo,
|
||||||
walk_source_dir,
|
walk_source_dir,
|
||||||
WinePathConverter,
|
WinePathConverter,
|
||||||
)
|
)
|
||||||
@ -47,138 +46,6 @@
|
|||||||
WORDS = re.compile(r"\w+")
|
WORDS = re.compile(r"\w+")
|
||||||
|
|
||||||
|
|
||||||
def get_file_in_script_dir(fn):
|
|
||||||
return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), fn)
|
|
||||||
|
|
||||||
|
|
||||||
# Declare a class that parses the output of cvdump for fast access later
|
|
||||||
class SymInfo:
|
|
||||||
funcs = {}
|
|
||||||
lines = {}
|
|
||||||
names = {}
|
|
||||||
|
|
||||||
def __init__(self, pdb, sym_recompfile, sym_logger, sym_wine_path_converter=None):
|
|
||||||
self.logger = sym_logger
|
|
||||||
call = [get_file_in_script_dir("cvdump.exe"), "-l", "-s"]
|
|
||||||
|
|
||||||
if sym_wine_path_converter:
|
|
||||||
# Run cvdump through wine and convert path to Windows-friendly wine path
|
|
||||||
call.insert(0, "wine")
|
|
||||||
call.append(sym_wine_path_converter.get_wine_path(pdb))
|
|
||||||
else:
|
|
||||||
call.append(pdb)
|
|
||||||
|
|
||||||
self.logger.info("Parsing %s ...", pdb)
|
|
||||||
self.logger.debug("Command = %s", call)
|
|
||||||
line_dump = subprocess.check_output(call).decode("utf-8").split("\r\n")
|
|
||||||
|
|
||||||
current_section = None
|
|
||||||
|
|
||||||
self.logger.debug("Parsing output of cvdump.exe ...")
|
|
||||||
|
|
||||||
for i, line in enumerate(line_dump):
|
|
||||||
if line.startswith("***"):
|
|
||||||
current_section = line[4:]
|
|
||||||
|
|
||||||
if current_section == "SYMBOLS" and "S_GPROC32" in line:
|
|
||||||
sym_addr = int(line[26:34], 16)
|
|
||||||
|
|
||||||
info = RecompiledInfo()
|
|
||||||
info.addr = (
|
|
||||||
sym_addr + sym_recompfile.imagebase + sym_recompfile.textvirt
|
|
||||||
)
|
|
||||||
|
|
||||||
use_dbg_offs = False
|
|
||||||
if use_dbg_offs:
|
|
||||||
debug_offs = line_dump[i + 2]
|
|
||||||
debug_start = int(debug_offs[22:30], 16)
|
|
||||||
debug_end = int(debug_offs[43:], 16)
|
|
||||||
|
|
||||||
info.start = debug_start
|
|
||||||
info.size = debug_end - debug_start
|
|
||||||
else:
|
|
||||||
info.start = 0
|
|
||||||
info.size = int(line[41:49], 16)
|
|
||||||
|
|
||||||
info.name = line[77:]
|
|
||||||
|
|
||||||
self.names[info.name] = info
|
|
||||||
self.funcs[sym_addr] = info
|
|
||||||
elif (
|
|
||||||
current_section == "LINES"
|
|
||||||
and line.startswith(" ")
|
|
||||||
and not line.startswith(" ")
|
|
||||||
):
|
|
||||||
sourcepath = line.split()[0]
|
|
||||||
|
|
||||||
if sym_wine_path_converter:
|
|
||||||
# Convert filename to Unix path for file compare
|
|
||||||
sourcepath = sym_wine_path_converter.get_unix_path(sourcepath)
|
|
||||||
|
|
||||||
if sourcepath not in self.lines:
|
|
||||||
self.lines[sourcepath] = {}
|
|
||||||
|
|
||||||
j = i + 2
|
|
||||||
while True:
|
|
||||||
ll = line_dump[j].split()
|
|
||||||
if len(ll) == 0:
|
|
||||||
break
|
|
||||||
|
|
||||||
k = 0
|
|
||||||
while k < len(ll):
|
|
||||||
linenum = int(ll[k + 0])
|
|
||||||
address = int(ll[k + 1], 16)
|
|
||||||
if linenum not in self.lines[sourcepath]:
|
|
||||||
self.lines[sourcepath][linenum] = address
|
|
||||||
k += 2
|
|
||||||
|
|
||||||
j += 1
|
|
||||||
|
|
||||||
self.logger.debug("... Parsing output of cvdump.exe finished")
|
|
||||||
|
|
||||||
def get_recompiled_address(self, filename, line):
|
|
||||||
recompiled_addr = None
|
|
||||||
|
|
||||||
self.logger.debug("Looking for %s:%s", filename, line)
|
|
||||||
filename_basename = os.path.basename(filename).lower()
|
|
||||||
|
|
||||||
for fn in self.lines:
|
|
||||||
# Sometimes a PDB is compiled with a relative path while we always have
|
|
||||||
# an absolute path. Therefore we must
|
|
||||||
try:
|
|
||||||
if os.path.basename(
|
|
||||||
fn
|
|
||||||
).lower() == filename_basename and os.path.samefile(fn, filename):
|
|
||||||
filename = fn
|
|
||||||
break
|
|
||||||
except FileNotFoundError:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if filename in self.lines and line in self.lines[filename]:
|
|
||||||
recompiled_addr = self.lines[filename][line]
|
|
||||||
|
|
||||||
if recompiled_addr in self.funcs:
|
|
||||||
return self.funcs[recompiled_addr]
|
|
||||||
self.logger.error(
|
|
||||||
"Failed to find function symbol with address: %x", recompiled_addr
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
self.logger.error(
|
|
||||||
"Failed to find function symbol with filename and line: %s:%s",
|
|
||||||
filename,
|
|
||||||
line,
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_recompiled_address_from_name(self, name):
|
|
||||||
self.logger.debug("Looking for %s", name)
|
|
||||||
|
|
||||||
if name in self.names:
|
|
||||||
return self.names[name]
|
|
||||||
self.logger.error("Failed to find function symbol with name: %s", name)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def sanitize(file, placeholder_generator, mnemonic, op_str):
|
def sanitize(file, placeholder_generator, mnemonic, op_str):
|
||||||
op_str_is_number = False
|
op_str_is_number = False
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user