mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 10:19:23 +02:00
more changes for filebytes
This commit is contained in:
parent
8576bd66e9
commit
a8934fbd95
3 changed files with 100 additions and 118 deletions
|
@ -21,7 +21,7 @@ import java.util.*;
|
||||||
|
|
||||||
import generic.continues.ContinuesFactory;
|
import generic.continues.ContinuesFactory;
|
||||||
import generic.continues.RethrowContinuesFactory;
|
import generic.continues.RethrowContinuesFactory;
|
||||||
import ghidra.app.util.MemoryBlockUtil;
|
import ghidra.app.util.MemoryBlockUtils;
|
||||||
import ghidra.app.util.Option;
|
import ghidra.app.util.Option;
|
||||||
import ghidra.app.util.bin.ByteProvider;
|
import ghidra.app.util.bin.ByteProvider;
|
||||||
import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
|
import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
|
||||||
|
@ -30,6 +30,7 @@ import ghidra.app.util.bin.format.mz.OldStyleExecutable;
|
||||||
import ghidra.app.util.importer.MessageLog;
|
import ghidra.app.util.importer.MessageLog;
|
||||||
import ghidra.app.util.importer.MessageLogContinuesFactory;
|
import ghidra.app.util.importer.MessageLogContinuesFactory;
|
||||||
import ghidra.framework.store.LockException;
|
import ghidra.framework.store.LockException;
|
||||||
|
import ghidra.program.database.mem.FileBytes;
|
||||||
import ghidra.program.model.address.*;
|
import ghidra.program.model.address.*;
|
||||||
import ghidra.program.model.lang.Register;
|
import ghidra.program.model.lang.Register;
|
||||||
import ghidra.program.model.listing.*;
|
import ghidra.program.model.listing.*;
|
||||||
|
@ -88,6 +89,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
||||||
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
|
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
|
||||||
TaskMonitor monitor, MessageLog log) throws IOException {
|
TaskMonitor monitor, MessageLog log) throws IOException {
|
||||||
|
|
||||||
|
FileBytes fileBytes = MemoryBlockUtils.createFileBytes(prog, provider);
|
||||||
AddressFactory af = prog.getAddressFactory();
|
AddressFactory af = prog.getAddressFactory();
|
||||||
if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) {
|
if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) {
|
||||||
throw new IOException("Selected Language must have a segmented address space.");
|
throw new IOException("Selected Language must have a segmented address space.");
|
||||||
|
@ -99,55 +101,43 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
||||||
Memory memory = prog.getMemory();
|
Memory memory = prog.getMemory();
|
||||||
|
|
||||||
ContinuesFactory factory = MessageLogContinuesFactory.create(log);
|
ContinuesFactory factory = MessageLogContinuesFactory.create(log);
|
||||||
MemoryBlockUtil mbu = new MemoryBlockUtil(prog);
|
OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
|
||||||
try {
|
DOSHeader dos = ose.getDOSHeader();
|
||||||
OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
|
FactoryBundledWithBinaryReader reader = ose.getBinaryReader();
|
||||||
DOSHeader dos = ose.getDOSHeader();
|
|
||||||
FactoryBundledWithBinaryReader reader = ose.getBinaryReader();
|
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
if (monitor.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
monitor.setMessage("Processing segments...");
|
|
||||||
processSegments(mbu, space, reader, dos, log, monitor);
|
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
monitor.setMessage("Adjusting segments...");
|
|
||||||
adjustSegmentStarts(prog);
|
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
monitor.setMessage("Processing relocations...");
|
|
||||||
doRelocations(prog, reader, dos);
|
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
monitor.setMessage("Processing symbols...");
|
|
||||||
createSymbols(space, symbolTable, dos);
|
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
monitor.setMessage("Setting registers...");
|
|
||||||
|
|
||||||
Symbol entrySymbol = SymbolUtilities.getLabelOrFunctionSymbol(prog, ENTRY_NAME,
|
|
||||||
err -> log.error("MZ", err));
|
|
||||||
setRegisters(context, entrySymbol, memory.getBlocks(), dos);
|
|
||||||
}
|
}
|
||||||
finally {
|
monitor.setMessage("Processing segments...");
|
||||||
|
processSegments(prog, fileBytes, space, reader, dos, log, monitor);
|
||||||
|
|
||||||
String messages = mbu.getMessages();
|
if (monitor.isCancelled()) {
|
||||||
if (messages.length() != 0) {
|
return;
|
||||||
log.appendMsg(messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
mbu.dispose();
|
|
||||||
mbu = null;
|
|
||||||
}
|
}
|
||||||
|
monitor.setMessage("Adjusting segments...");
|
||||||
|
adjustSegmentStarts(prog);
|
||||||
|
|
||||||
|
if (monitor.isCancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
monitor.setMessage("Processing relocations...");
|
||||||
|
doRelocations(prog, reader, dos);
|
||||||
|
|
||||||
|
if (monitor.isCancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
monitor.setMessage("Processing symbols...");
|
||||||
|
createSymbols(space, symbolTable, dos);
|
||||||
|
|
||||||
|
if (monitor.isCancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
monitor.setMessage("Setting registers...");
|
||||||
|
|
||||||
|
Symbol entrySymbol =
|
||||||
|
SymbolUtilities.getLabelOrFunctionSymbol(prog, ENTRY_NAME, err -> log.error("MZ", err));
|
||||||
|
setRegisters(context, entrySymbol, memory.getBlocks(), dos);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setRegisters(ProgramContext context, Symbol entry, MemoryBlock[] blocks,
|
private void setRegisters(ProgramContext context, Symbol entry, MemoryBlock[] blocks,
|
||||||
|
@ -259,7 +249,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processSegments(MemoryBlockUtil mbu, SegmentedAddressSpace space,
|
private void processSegments(Program program, FileBytes fileBytes, SegmentedAddressSpace space,
|
||||||
FactoryBundledWithBinaryReader reader, DOSHeader dos, MessageLog log,
|
FactoryBundledWithBinaryReader reader, DOSHeader dos, MessageLog log,
|
||||||
TaskMonitor monitor) {
|
TaskMonitor monitor) {
|
||||||
try {
|
try {
|
||||||
|
@ -328,12 +318,12 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
||||||
}
|
}
|
||||||
if (numBytes > 0) {
|
if (numBytes > 0) {
|
||||||
bytes = reader.readByteArray(readLoc, numBytes);
|
bytes = reader.readByteArray(readLoc, numBytes);
|
||||||
mbu.createInitializedBlock("Seg_" + i, start, bytes, "", "mz", true, true, true,
|
MemoryBlockUtils.createInitializedBlock(program, false, "Seg_" + i, start,
|
||||||
monitor);
|
fileBytes, readLoc, numBytes, "", "mz", true, true, true, log);
|
||||||
}
|
}
|
||||||
if (numUninitBytes > 0) {
|
if (numUninitBytes > 0) {
|
||||||
mbu.createUninitializedBlock(false, "Seg_" + i + "u", start.add(numBytes),
|
MemoryBlockUtils.createUninitializedBlock(program, false, "Seg_" + i + "u",
|
||||||
numUninitBytes, "", "mz", true, true, false);
|
start.add(numBytes), numUninitBytes, "", "mz", true, true, false, log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ import java.util.Arrays;
|
||||||
|
|
||||||
import org.xml.sax.SAXParseException;
|
import org.xml.sax.SAXParseException;
|
||||||
|
|
||||||
import ghidra.app.util.MemoryBlockUtil;
|
import ghidra.app.util.MemoryBlockUtils;
|
||||||
import ghidra.app.util.importer.MessageLog;
|
import ghidra.app.util.importer.MessageLog;
|
||||||
import ghidra.program.database.mem.SourceInfo;
|
import ghidra.program.database.mem.SourceInfo;
|
||||||
import ghidra.program.model.address.*;
|
import ghidra.program.model.address.*;
|
||||||
|
@ -56,29 +56,23 @@ class MemoryMapXmlMgr {
|
||||||
String directory)
|
String directory)
|
||||||
throws SAXParseException, FileNotFoundException, CancelledException {
|
throws SAXParseException, FileNotFoundException, CancelledException {
|
||||||
|
|
||||||
MemoryBlockUtil mbu = new MemoryBlockUtil(program);
|
XmlElement element = parser.next();
|
||||||
try {
|
element = parser.next();
|
||||||
XmlElement element = parser.next();
|
while (element.getName().equals("MEMORY_SECTION")) {
|
||||||
|
if (monitor.isCancelled()) {
|
||||||
|
throw new CancelledException();
|
||||||
|
}
|
||||||
|
processMemoryBlock(element, parser, directory, program, monitor);
|
||||||
element = parser.next();
|
element = parser.next();
|
||||||
while (element.getName().equals("MEMORY_SECTION")) {
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
throw new CancelledException();
|
|
||||||
}
|
|
||||||
processMemoryBlock(element, parser, directory, mbu, monitor);
|
|
||||||
element = parser.next();
|
|
||||||
}
|
|
||||||
if (element.isStart() || !element.getName().equals("MEMORY_MAP")) {
|
|
||||||
throw new SAXParseException("Expected MEMORY_MAP end tag, got " + element.getName(),
|
|
||||||
null, null, parser.getLineNumber(), parser.getColumnNumber());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finally {
|
if (element.isStart() || !element.getName().equals("MEMORY_MAP")) {
|
||||||
mbu.dispose();
|
throw new SAXParseException("Expected MEMORY_MAP end tag, got " + element.getName(),
|
||||||
|
null, null, parser.getLineNumber(), parser.getColumnNumber());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processMemoryBlock(XmlElement memorySectionElement, XmlPullParser parser,
|
private void processMemoryBlock(XmlElement memorySectionElement, XmlPullParser parser,
|
||||||
String directory, MemoryBlockUtil mbu, TaskMonitor monitor)
|
String directory, Program program, TaskMonitor monitor)
|
||||||
throws FileNotFoundException {
|
throws FileNotFoundException {
|
||||||
|
|
||||||
String name = memorySectionElement.getAttribute("NAME");
|
String name = memorySectionElement.getAttribute("NAME");
|
||||||
|
@ -132,8 +126,9 @@ class MemoryMapXmlMgr {
|
||||||
}
|
}
|
||||||
if (overlayName != null) {
|
if (overlayName != null) {
|
||||||
MemoryBlock block =
|
MemoryBlock block =
|
||||||
mbu.createOverlayBlock(overlayName, addr, new ByteArrayInputStream(bytes),
|
MemoryBlockUtils.createInitializedBlock(program, true, overlayName, addr,
|
||||||
bytes.length, comment, null, r, w, x, monitor);
|
new ByteArrayInputStream(bytes),
|
||||||
|
bytes.length, comment, null, r, w, x, log, monitor);
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
block.setVolatile(isVolatile);
|
block.setVolatile(isVolatile);
|
||||||
if (!name.equals(overlayName)) {
|
if (!name.equals(overlayName)) {
|
||||||
|
@ -142,26 +137,38 @@ class MemoryMapXmlMgr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MemoryBlock block = mbu.createInitializedBlock(name, addr, bytes, comment, null,
|
|
||||||
r, w, x, monitor);
|
MemoryBlock block = MemoryBlockUtils.createInitializedBlock(program, false,
|
||||||
|
name, addr, new ByteArrayInputStream(bytes), bytes.length, comment, null,
|
||||||
|
r, w, x, log, monitor);
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
block.setVolatile(isVolatile);
|
block.setVolatile(isVolatile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (element.getName().equals("BIT_MAPPED") ||
|
else if (element.getName().equals("BIT_MAPPED")) {
|
||||||
element.getName().equals("BYTE_MAPPED")) {
|
|
||||||
Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS"));
|
Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS"));
|
||||||
MemoryBlock block = mbu.createMappedBlock(element.getName().equals("BIT_MAPPED"),
|
|
||||||
name, addr, sourceAddr, length, comment, null, r, w, x);
|
MemoryBlock block = MemoryBlockUtils.createBitMappedBlock(program, overlayName,
|
||||||
|
addr, sourceAddr, length, comment, comment, r, w, x, log);
|
||||||
|
if (block != null) {
|
||||||
|
block.setVolatile(isVolatile);
|
||||||
|
}
|
||||||
|
parser.next(); // consume end of Bit_mapped
|
||||||
|
}
|
||||||
|
else if (element.getName().equals("BYTE_MAPPED")) {
|
||||||
|
Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS"));
|
||||||
|
|
||||||
|
MemoryBlock block = MemoryBlockUtils.createByteMappedBlock(program, overlayName,
|
||||||
|
addr, sourceAddr, length, comment, comment, r, w, x, log);
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
block.setVolatile(isVolatile);
|
block.setVolatile(isVolatile);
|
||||||
}
|
}
|
||||||
parser.next(); // consume end of Bit_mapped
|
parser.next(); // consume end of Bit_mapped
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MemoryBlock block = mbu.createUninitializedBlock(overlayName != null, name, addr,
|
MemoryBlock block = MemoryBlockUtils.createUninitializedBlock(program,
|
||||||
length, comment, null, r, w, x);
|
overlayName != null, name, addr, length, comment, null, r, w, x, log);
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
block.setVolatile(isVolatile);
|
block.setVolatile(isVolatile);
|
||||||
if (overlayName != null && !name.equals(overlayName)) {
|
if (overlayName != null && !name.equals(overlayName)) {
|
||||||
|
|
|
@ -26,8 +26,9 @@
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import ghidra.app.script.GhidraScript;
|
import ghidra.app.script.GhidraScript;
|
||||||
import ghidra.app.util.MemoryBlockUtil;
|
import ghidra.app.util.MemoryBlockUtils;
|
||||||
import ghidra.app.util.NamespaceUtils;
|
import ghidra.app.util.NamespaceUtils;
|
||||||
|
import ghidra.app.util.importer.MessageLog;
|
||||||
import ghidra.program.model.listing.*;
|
import ghidra.program.model.listing.*;
|
||||||
import ghidra.program.model.mem.*;
|
import ghidra.program.model.mem.*;
|
||||||
import ghidra.program.model.symbol.*;
|
import ghidra.program.model.symbol.*;
|
||||||
|
@ -241,45 +242,29 @@ public class MergeTwoProgramsScript extends GhidraScript {
|
||||||
|
|
||||||
private void mergeMemory( Program currProgram, Program otherProgram ) throws Exception {
|
private void mergeMemory( Program currProgram, Program otherProgram ) throws Exception {
|
||||||
monitor.setMessage( "Merging memory..." );
|
monitor.setMessage( "Merging memory..." );
|
||||||
MemoryBlockUtil mbu = new MemoryBlockUtil(currProgram);
|
Memory otherMemory = otherProgram.getMemory();
|
||||||
try {
|
MemoryBlock[] otherBlocks = otherMemory.getBlocks();
|
||||||
Memory otherMemory = otherProgram.getMemory();
|
MessageLog log = new MessageLog();
|
||||||
MemoryBlock [] otherBlocks = otherMemory.getBlocks();
|
for (MemoryBlock otherBlock : otherBlocks) {
|
||||||
for ( MemoryBlock otherBlock : otherBlocks ) {
|
if (monitor.isCancelled()) {
|
||||||
if ( monitor.isCancelled() ) {
|
break;
|
||||||
break;
|
}
|
||||||
}
|
if (otherBlock.getType() != MemoryBlockType.DEFAULT) {
|
||||||
if ( otherBlock.getType() != MemoryBlockType.DEFAULT ) {
|
printerr("Unhandled memory block type: " + otherBlock.getName());
|
||||||
printerr( "Unhandled memory block type: " + otherBlock.getName() );
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
if (otherBlock.isInitialized()) {
|
||||||
if ( otherBlock.isInitialized() ) {
|
MemoryBlockUtils.createInitializedBlock(currProgram, false, otherBlock.getName(),
|
||||||
mbu.createInitializedBlock( otherBlock.getName(),
|
otherBlock.getStart(), otherBlock.getData(), otherBlock.getSize(),
|
||||||
otherBlock.getStart(),
|
otherBlock.getComment(), otherBlock.getSourceName(), otherBlock.isRead(),
|
||||||
otherBlock.getData(),
|
otherBlock.isWrite(), otherBlock.isExecute(), log, monitor);
|
||||||
otherBlock.getSize(),
|
}
|
||||||
otherBlock.getComment(),
|
else {
|
||||||
otherBlock.getSourceName(),
|
MemoryBlockUtils.createUninitializedBlock(currProgram, false, otherBlock.getName(),
|
||||||
otherBlock.isRead(),
|
otherBlock.getStart(), otherBlock.getSize(), otherBlock.getComment(),
|
||||||
otherBlock.isWrite(),
|
otherBlock.getSourceName(), otherBlock.isRead(), otherBlock.isWrite(),
|
||||||
otherBlock.isExecute(),
|
otherBlock.isExecute(), log);
|
||||||
monitor );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mbu.createUninitializedBlock( false,
|
|
||||||
otherBlock.getName(),
|
|
||||||
otherBlock.getStart(),
|
|
||||||
otherBlock.getSize(),
|
|
||||||
otherBlock.getComment(),
|
|
||||||
otherBlock.getSourceName(),
|
|
||||||
otherBlock.isRead(),
|
|
||||||
otherBlock.isWrite(),
|
|
||||||
otherBlock.isExecute() );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
finally {
|
|
||||||
mbu.dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue