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.RethrowContinuesFactory;
|
||||
import ghidra.app.util.MemoryBlockUtil;
|
||||
import ghidra.app.util.MemoryBlockUtils;
|
||||
import ghidra.app.util.Option;
|
||||
import ghidra.app.util.bin.ByteProvider;
|
||||
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.MessageLogContinuesFactory;
|
||||
import ghidra.framework.store.LockException;
|
||||
import ghidra.program.database.mem.FileBytes;
|
||||
import ghidra.program.model.address.*;
|
||||
import ghidra.program.model.lang.Register;
|
||||
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,
|
||||
TaskMonitor monitor, MessageLog log) throws IOException {
|
||||
|
||||
FileBytes fileBytes = MemoryBlockUtils.createFileBytes(prog, provider);
|
||||
AddressFactory af = prog.getAddressFactory();
|
||||
if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) {
|
||||
throw new IOException("Selected Language must have a segmented address space.");
|
||||
|
@ -99,55 +101,43 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
|||
Memory memory = prog.getMemory();
|
||||
|
||||
ContinuesFactory factory = MessageLogContinuesFactory.create(log);
|
||||
MemoryBlockUtil mbu = new MemoryBlockUtil(prog);
|
||||
try {
|
||||
OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
|
||||
DOSHeader dos = ose.getDOSHeader();
|
||||
FactoryBundledWithBinaryReader reader = ose.getBinaryReader();
|
||||
OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
|
||||
DOSHeader dos = ose.getDOSHeader();
|
||||
FactoryBundledWithBinaryReader reader = ose.getBinaryReader();
|
||||
|
||||
if (monitor.isCancelled()) {
|
||||
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);
|
||||
if (monitor.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
finally {
|
||||
monitor.setMessage("Processing segments...");
|
||||
processSegments(prog, fileBytes, space, reader, dos, log, monitor);
|
||||
|
||||
String messages = mbu.getMessages();
|
||||
if (messages.length() != 0) {
|
||||
log.appendMsg(messages);
|
||||
}
|
||||
|
||||
mbu.dispose();
|
||||
mbu = null;
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
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,
|
||||
TaskMonitor monitor) {
|
||||
try {
|
||||
|
@ -328,12 +318,12 @@ public class MzLoader extends AbstractLibrarySupportLoader {
|
|||
}
|
||||
if (numBytes > 0) {
|
||||
bytes = reader.readByteArray(readLoc, numBytes);
|
||||
mbu.createInitializedBlock("Seg_" + i, start, bytes, "", "mz", true, true, true,
|
||||
monitor);
|
||||
MemoryBlockUtils.createInitializedBlock(program, false, "Seg_" + i, start,
|
||||
fileBytes, readLoc, numBytes, "", "mz", true, true, true, log);
|
||||
}
|
||||
if (numUninitBytes > 0) {
|
||||
mbu.createUninitializedBlock(false, "Seg_" + i + "u", start.add(numBytes),
|
||||
numUninitBytes, "", "mz", true, true, false);
|
||||
MemoryBlockUtils.createUninitializedBlock(program, false, "Seg_" + i + "u",
|
||||
start.add(numBytes), numUninitBytes, "", "mz", true, true, false, log);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import java.util.Arrays;
|
|||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import ghidra.app.util.MemoryBlockUtil;
|
||||
import ghidra.app.util.MemoryBlockUtils;
|
||||
import ghidra.app.util.importer.MessageLog;
|
||||
import ghidra.program.database.mem.SourceInfo;
|
||||
import ghidra.program.model.address.*;
|
||||
|
@ -56,29 +56,23 @@ class MemoryMapXmlMgr {
|
|||
String directory)
|
||||
throws SAXParseException, FileNotFoundException, CancelledException {
|
||||
|
||||
MemoryBlockUtil mbu = new MemoryBlockUtil(program);
|
||||
try {
|
||||
XmlElement element = parser.next();
|
||||
XmlElement element = parser.next();
|
||||
element = parser.next();
|
||||
while (element.getName().equals("MEMORY_SECTION")) {
|
||||
if (monitor.isCancelled()) {
|
||||
throw new CancelledException();
|
||||
}
|
||||
processMemoryBlock(element, parser, directory, program, monitor);
|
||||
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 {
|
||||
mbu.dispose();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
private void processMemoryBlock(XmlElement memorySectionElement, XmlPullParser parser,
|
||||
String directory, MemoryBlockUtil mbu, TaskMonitor monitor)
|
||||
String directory, Program program, TaskMonitor monitor)
|
||||
throws FileNotFoundException {
|
||||
|
||||
String name = memorySectionElement.getAttribute("NAME");
|
||||
|
@ -132,8 +126,9 @@ class MemoryMapXmlMgr {
|
|||
}
|
||||
if (overlayName != null) {
|
||||
MemoryBlock block =
|
||||
mbu.createOverlayBlock(overlayName, addr, new ByteArrayInputStream(bytes),
|
||||
bytes.length, comment, null, r, w, x, monitor);
|
||||
MemoryBlockUtils.createInitializedBlock(program, true, overlayName, addr,
|
||||
new ByteArrayInputStream(bytes),
|
||||
bytes.length, comment, null, r, w, x, log, monitor);
|
||||
if (block != null) {
|
||||
block.setVolatile(isVolatile);
|
||||
if (!name.equals(overlayName)) {
|
||||
|
@ -142,26 +137,38 @@ class MemoryMapXmlMgr {
|
|||
}
|
||||
}
|
||||
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) {
|
||||
block.setVolatile(isVolatile);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (element.getName().equals("BIT_MAPPED") ||
|
||||
element.getName().equals("BYTE_MAPPED")) {
|
||||
else if (element.getName().equals("BIT_MAPPED")) {
|
||||
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) {
|
||||
block.setVolatile(isVolatile);
|
||||
}
|
||||
parser.next(); // consume end of Bit_mapped
|
||||
}
|
||||
else {
|
||||
MemoryBlock block = mbu.createUninitializedBlock(overlayName != null, name, addr,
|
||||
length, comment, null, r, w, x);
|
||||
MemoryBlock block = MemoryBlockUtils.createUninitializedBlock(program,
|
||||
overlayName != null, name, addr, length, comment, null, r, w, x, log);
|
||||
if (block != null) {
|
||||
block.setVolatile(isVolatile);
|
||||
if (overlayName != null && !name.equals(overlayName)) {
|
||||
|
|
|
@ -26,8 +26,9 @@
|
|||
import java.util.Iterator;
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.app.util.MemoryBlockUtil;
|
||||
import ghidra.app.util.MemoryBlockUtils;
|
||||
import ghidra.app.util.NamespaceUtils;
|
||||
import ghidra.app.util.importer.MessageLog;
|
||||
import ghidra.program.model.listing.*;
|
||||
import ghidra.program.model.mem.*;
|
||||
import ghidra.program.model.symbol.*;
|
||||
|
@ -241,45 +242,29 @@ public class MergeTwoProgramsScript extends GhidraScript {
|
|||
|
||||
private void mergeMemory( Program currProgram, Program otherProgram ) throws Exception {
|
||||
monitor.setMessage( "Merging memory..." );
|
||||
MemoryBlockUtil mbu = new MemoryBlockUtil(currProgram);
|
||||
try {
|
||||
Memory otherMemory = otherProgram.getMemory();
|
||||
MemoryBlock [] otherBlocks = otherMemory.getBlocks();
|
||||
for ( MemoryBlock otherBlock : otherBlocks ) {
|
||||
if ( monitor.isCancelled() ) {
|
||||
break;
|
||||
}
|
||||
if ( otherBlock.getType() != MemoryBlockType.DEFAULT ) {
|
||||
printerr( "Unhandled memory block type: " + otherBlock.getName() );
|
||||
continue;
|
||||
}
|
||||
if ( otherBlock.isInitialized() ) {
|
||||
mbu.createInitializedBlock( otherBlock.getName(),
|
||||
otherBlock.getStart(),
|
||||
otherBlock.getData(),
|
||||
otherBlock.getSize(),
|
||||
otherBlock.getComment(),
|
||||
otherBlock.getSourceName(),
|
||||
otherBlock.isRead(),
|
||||
otherBlock.isWrite(),
|
||||
otherBlock.isExecute(),
|
||||
monitor );
|
||||
}
|
||||
else {
|
||||
mbu.createUninitializedBlock( false,
|
||||
otherBlock.getName(),
|
||||
otherBlock.getStart(),
|
||||
otherBlock.getSize(),
|
||||
otherBlock.getComment(),
|
||||
otherBlock.getSourceName(),
|
||||
otherBlock.isRead(),
|
||||
otherBlock.isWrite(),
|
||||
otherBlock.isExecute() );
|
||||
}
|
||||
Memory otherMemory = otherProgram.getMemory();
|
||||
MemoryBlock[] otherBlocks = otherMemory.getBlocks();
|
||||
MessageLog log = new MessageLog();
|
||||
for (MemoryBlock otherBlock : otherBlocks) {
|
||||
if (monitor.isCancelled()) {
|
||||
break;
|
||||
}
|
||||
if (otherBlock.getType() != MemoryBlockType.DEFAULT) {
|
||||
printerr("Unhandled memory block type: " + otherBlock.getName());
|
||||
continue;
|
||||
}
|
||||
if (otherBlock.isInitialized()) {
|
||||
MemoryBlockUtils.createInitializedBlock(currProgram, false, otherBlock.getName(),
|
||||
otherBlock.getStart(), otherBlock.getData(), otherBlock.getSize(),
|
||||
otherBlock.getComment(), otherBlock.getSourceName(), otherBlock.isRead(),
|
||||
otherBlock.isWrite(), otherBlock.isExecute(), log, monitor);
|
||||
}
|
||||
else {
|
||||
MemoryBlockUtils.createUninitializedBlock(currProgram, false, otherBlock.getName(),
|
||||
otherBlock.getStart(), otherBlock.getSize(), otherBlock.getComment(),
|
||||
otherBlock.getSourceName(), otherBlock.isRead(), otherBlock.isWrite(),
|
||||
otherBlock.isExecute(), log);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
mbu.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue