PyGhidra: Constructing a Program #7343
-
Follow-up to #7333 - via PyGhidra I now have access to the script instance, and can create a project and empty program, but attempting to do anything with the created program object seems to yield:
I've tried mimicking the steps that a loader performs, using MotorolaHexLoader.java as an example, but am definitely missing something. Any suggestions? The (slightly condensed) example script: log = MessageLog()
offset = 0x100000
test_file = Path("/home/user1/files/file.bin")
monitor = TaskMonitor.DUMMY
si = PyGhidraScriptProvider().getScriptInstance(None, None)
proj_location = Path.joinpath(test_file.parent, "gproj")
if not proj_location.exists():
os.makedirs(proj_location)
proj = GhidraProject.createProject(proj_location, "testproject", False)
# Note the call to proj.getProject(), it's subtle, but important
state = GhidraState(None, proj.getProject(), None, None, None, None)
si.set(state, TaskMonitor.DUMMY, PrintWriter(System.out))
program = si.createProgram("test_program", LanguageID("x86:LE:64:default"))
program.setEventsEnabled(False)
# Failing here with:
# java.lang.java.lang.IllegalStateException: java.lang.IllegalStateException: Database is closed
tid = program.startTransaction("Starting memory block creation")
if program.isClosed():
print("program is not open, exiting")
exit()
state.setCurrentProgram(program)
si.set(state, TaskMonitor.DUMMY, PrintWriter(System.out))
with test_file.open("rb") as f:
f.seek(offset)
data = f.read(0x1000)
in_stream = ByteArrayInputStream(data)
MemoryBlockUtils.createInitializedBlock(
program,
False,
"test_block",
si.toAddr(offset),
in_stream,
0x1000,
"test_comment",
"test_source",
True,
False,
True,
log,
monitor,
)
program.endTransaction(tid, True)
program.setEventsEnabled(True)
if program.canSave():
program.save("Saving changes before packing", monitor)
proj.save(program)
proj.close()
dom_file = program.getDomainFile()
outfile = File(f"test_project.gzf")
dom_file.packFile(outfile, monitor) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The root of the issue is here: ghidra/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScript.java Lines 3683 to 3693 in 20285e2 The first param of your |
Beta Was this translation helpful? Give feedback.
-
How about just proj = GhidraProject.createProject(proj_location, "testproject", False)
root_folder = proj.getRootFolder();
language = DefaultLanguageService.getLanguageService().getLanguage(LanguageID("x86:LE:64:default"))
spec = language.getDefaultCompilerSpec()
consumer = Object()
program = ProgramDB("test_program", language, spec, consumer);
program.setEventsEnabled(False)
tid = program.startTransaction("Starting memory block creation") |
Beta Was this translation helpful? Give feedback.
How about just