mirror of
https://github.com/dobin/SuperMega
synced 2026-06-02 17:27:10 +00:00
refactor: make asm cleanup/fixup work in memory
This commit is contained in:
+3
-6
@@ -8,10 +8,9 @@ from model.carrier import Carrier, DataReuseEntry, IatRequest
|
|||||||
logger = logging.getLogger("AsmParser")
|
logger = logging.getLogger("AsmParser")
|
||||||
|
|
||||||
|
|
||||||
def parse_asm_file(carrier, filename):
|
def parse_asm_file(carrier: Carrier, asm_text: str) -> List[str]:
|
||||||
lines_out = []
|
lines_out = []
|
||||||
with open(filename, 'r', encoding='utf-8') as asmfile:
|
lines = asm_text.split("\n")
|
||||||
lines = asmfile.readlines()
|
|
||||||
|
|
||||||
current_segment = None
|
current_segment = None
|
||||||
current_datareuse_entry= None
|
current_datareuse_entry= None
|
||||||
@@ -143,9 +142,7 @@ def parse_asm_file(carrier, filename):
|
|||||||
|
|
||||||
lines_out.append(line)
|
lines_out.append(line)
|
||||||
|
|
||||||
with open(filename, "w") as f:
|
return lines_out
|
||||||
for line in lines_out:
|
|
||||||
f.write(line + "\n")
|
|
||||||
|
|
||||||
|
|
||||||
def convert_asm_db_to_bytes(line: str) -> bytes:
|
def convert_asm_db_to_bytes(line: str) -> bytes:
|
||||||
|
|||||||
+17
-44
@@ -8,7 +8,7 @@ from helper import *
|
|||||||
from config import config
|
from config import config
|
||||||
from observer import observer
|
from observer import observer
|
||||||
from model import *
|
from model import *
|
||||||
from phases.masmshc import process_file, Params
|
from phases.masmshc import masm_shc, Params
|
||||||
from model.carrier import Carrier
|
from model.carrier import Carrier
|
||||||
from model.exehost import ExeHost
|
from model.exehost import ExeHost
|
||||||
from phases.asmparser import parse_asm_file
|
from phases.asmparser import parse_asm_file
|
||||||
@@ -35,37 +35,23 @@ def compile_dev(
|
|||||||
])
|
])
|
||||||
if not os.path.isfile(asm_out):
|
if not os.path.isfile(asm_out):
|
||||||
raise Exception("Error: Compiling failed")
|
raise Exception("Error: Compiling failed")
|
||||||
file_to_lf(asm_out)
|
|
||||||
observer.add_text_file("carrier_asm_orig", file_readall_text(asm_out))
|
|
||||||
|
|
||||||
# Assembly cleanup (masm_shc)
|
asm_text: str = file_readall_text(asm_out)
|
||||||
asm_clean_file = asm_out + ".clean"
|
observer.add_text_file("carrier_asm_orig", asm_text)
|
||||||
|
|
||||||
logger.info("---[ ASM masm_shc: {} ".format(asm_out))
|
logger.info("---[ ASM masm_shc: {} ".format(asm_out))
|
||||||
params = Params(asm_out, asm_clean_file,
|
asm_text_lines: List[str] = parse_asm_file(Carrier(), asm_text)
|
||||||
inline_strings=False, # not for DATA_REUSE
|
asm_text = masm_shc(asm_text_lines)
|
||||||
remove_crt=True,
|
observer.add_text_file("carrier_asm_cleanup", asm_text)
|
||||||
append_rsp_stub=True) # required atm
|
|
||||||
process_file(params)
|
|
||||||
|
|
||||||
if not os.path.isfile(asm_clean_file):
|
with open(asm_out, "w") as f:
|
||||||
raise Exception("Error: Cleaned up ASM file {} was not created".format(
|
f.write(asm_text)
|
||||||
asm_clean_file
|
|
||||||
))
|
|
||||||
|
|
||||||
# Move to destination we expect
|
|
||||||
shutil.move(asm_clean_file, asm_out)
|
|
||||||
if config.debug:
|
|
||||||
observer.add_text_file("carrier_asm_cleanup", file_readall_text(asm_out))
|
|
||||||
|
|
||||||
|
|
||||||
def compile(
|
def compile(
|
||||||
c_in: FilePath,
|
c_in: FilePath,
|
||||||
asm_out: FilePath,
|
asm_out: FilePath,
|
||||||
payload_len: int,
|
|
||||||
carrier: Carrier,
|
carrier: Carrier,
|
||||||
source_style: FunctionInvokeStyle,
|
|
||||||
exe_host: ExeHost,
|
|
||||||
short_call_patching: bool = False,
|
|
||||||
):
|
):
|
||||||
logger.info("--[ Compile C to ASM: {} -> {} ".format(c_in, asm_out))
|
logger.info("--[ Compile C to ASM: {} -> {} ".format(c_in, asm_out))
|
||||||
|
|
||||||
@@ -80,26 +66,13 @@ def compile(
|
|||||||
])
|
])
|
||||||
if not os.path.isfile(asm_out):
|
if not os.path.isfile(asm_out):
|
||||||
raise Exception("Error: Compiling failed")
|
raise Exception("Error: Compiling failed")
|
||||||
file_to_lf(asm_out)
|
asm_text = file_readall_text(asm_out)
|
||||||
observer.add_text_file("carrier_asm_orig", file_readall_text(asm_out))
|
observer.add_text_file("carrier_asm_orig", asm_text)
|
||||||
|
|
||||||
# Fixup assembly file
|
asm_text_lines = parse_asm_file(carrier, asm_text) # Fixup assembly file
|
||||||
parse_asm_file(carrier, asm_out)
|
asm_text = masm_shc(asm_text_lines) # Cleanup assembly file
|
||||||
|
observer.add_text_file("carrier_asm_final", asm_text)
|
||||||
|
|
||||||
# Cleanup assembly file
|
# write back. Next step would be compiling this file
|
||||||
asm_clean_file = asm_out + ".clean"
|
with open(asm_out, "w") as f:
|
||||||
logger.info("---[ ASM masm_shc: {} ".format(asm_out))
|
f.write(asm_text)
|
||||||
params = Params(asm_out, asm_clean_file,
|
|
||||||
inline_strings=False, # not for DATA_REUSE
|
|
||||||
remove_crt=True,
|
|
||||||
append_rsp_stub=True) # required atm
|
|
||||||
process_file(params)
|
|
||||||
if not os.path.isfile(asm_clean_file):
|
|
||||||
raise Exception("Error: Cleaned up ASM file {} was not created".format(
|
|
||||||
asm_clean_file
|
|
||||||
))
|
|
||||||
# Move to destination we expect
|
|
||||||
shutil.move(asm_clean_file, asm_out)
|
|
||||||
|
|
||||||
# Log result
|
|
||||||
observer.add_text_file("carrier_asm_cleanup", file_readall_text(asm_out))
|
|
||||||
|
|||||||
+16
-19
@@ -1,10 +1,13 @@
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import io
|
||||||
|
from typing import List
|
||||||
|
|
||||||
logger = logging.getLogger("masmshc")
|
logger = logging.getLogger("masmshc")
|
||||||
|
|
||||||
g_is32bit = False
|
# original source: https://github.com/hasherezade/masm_shc/blob/master/masm_shc/main.cpp
|
||||||
|
# Converted to python by chatgpt, with some manual fixups
|
||||||
|
|
||||||
|
|
||||||
class Params:
|
class Params:
|
||||||
@@ -71,21 +74,23 @@ _TEXT ENDS
|
|||||||
"""
|
"""
|
||||||
ofile.write(stub)
|
ofile.write(stub)
|
||||||
|
|
||||||
def process_file(params):
|
def masm_shc(asm_text_lines: List[str]) -> str:
|
||||||
global g_is32bit
|
g_is32bit = False
|
||||||
try:
|
|
||||||
with open(params.infile, "r") as file, open(params.outfile, "w") as ofile:
|
|
||||||
consts_lines = {}
|
consts_lines = {}
|
||||||
seg_name = ""
|
seg_name = ""
|
||||||
const_name = ""
|
const_name = ""
|
||||||
code_start = False
|
code_start = False
|
||||||
|
|
||||||
line_count = 0
|
params = Params("", "",
|
||||||
for line in file.readlines():
|
inline_strings=False, # not for DATA_REUSE
|
||||||
#for line_count, line in enumerate(file):
|
remove_crt=True,
|
||||||
tokens = split_to_tokens(line)
|
append_rsp_stub=True) # required atm
|
||||||
|
ofile = io.StringIO()
|
||||||
|
|
||||||
#print("Tokens: {}".format(" ".join(tokens)))
|
line_count = 0
|
||||||
|
for line in asm_text_lines:
|
||||||
|
line = line + "\n" # lol
|
||||||
|
tokens = split_to_tokens(line)
|
||||||
|
|
||||||
if not tokens:
|
if not tokens:
|
||||||
ofile.write(line)
|
ofile.write(line)
|
||||||
@@ -177,15 +182,7 @@ def process_file(params):
|
|||||||
|
|
||||||
ofile.write(line) # copy line
|
ofile.write(line) # copy line
|
||||||
|
|
||||||
except FileNotFoundError as e:
|
|
||||||
print(f"[ERROR] {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
if params.inline_strings:
|
if params.inline_strings:
|
||||||
print("[INFO] Strings have been inlined. It may require to change some short jumps (jmp SHORT) into jumps (jmp)")
|
print("[INFO] Strings have been inlined. It may require to change some short jumps (jmp SHORT) into jumps (jmp)")
|
||||||
return True
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
return ofile.getvalue()
|
||||||
# Example usage
|
|
||||||
params = Params("test.asm", "testout.asm", True, True, True)
|
|
||||||
process_file(params)
|
|
||||||
|
|||||||
+4
-5
@@ -109,6 +109,9 @@ def start(settings: Settings) -> int:
|
|||||||
prepare_project("default", settings)
|
prepare_project("default", settings)
|
||||||
|
|
||||||
# Do the thing and catch the errors
|
# Do the thing and catch the errors
|
||||||
|
if False:
|
||||||
|
start_real(settings)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
start_real(settings)
|
start_real(settings)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -146,11 +149,7 @@ def start_real(settings: Settings):
|
|||||||
phases.compiler.compile(
|
phases.compiler.compile(
|
||||||
c_in = settings.main_c_path,
|
c_in = settings.main_c_path,
|
||||||
asm_out = settings.main_asm_path,
|
asm_out = settings.main_asm_path,
|
||||||
payload_len = project.payload.len,
|
carrier = project.carrier)
|
||||||
carrier = project.carrier,
|
|
||||||
source_style = project.settings.source_style,
|
|
||||||
exe_host = project.exe_host,
|
|
||||||
short_call_patching = project.settings.short_call_patching)
|
|
||||||
|
|
||||||
# Assemble: Assemble .asm to .shc (ASM -> SHC)
|
# Assemble: Assemble .asm to .shc (ASM -> SHC)
|
||||||
if settings.generate_shc_from_asm:
|
if settings.generate_shc_from_asm:
|
||||||
|
|||||||
Reference in New Issue
Block a user