Skip to content

Ghidra is a software reverse engineering (SRE) framework

changed PefLoader to use filebytes

ghidravore8576bd66e940
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java
index 888447425b..926106d31b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java
@@ -27,6 +27,7 @@ import ghidra.program.model.address.AddressOverflowException;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.*;
import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
import ghidra.util.exception.DuplicateNameException;
import ghidra.util.task.TaskMonitor;
@@ -107,6 +108,8 @@ public class MemoryBlockUtils {
return null;
}
+
+
/**
* Creates a new bit mapped memory block. (A bit mapped block is a block where each byte value
* is either 1 or 0 and the value is taken from a bit in a byte at some other address in memory)
@@ -189,6 +192,9 @@ public class MemoryBlockUtils {
/**
* Creates a new initialized block in memory using the bytes from a {@link FileBytes} object.
+ * If there is a conflict when creating this block (some other block occupies at least some
+ * of the addresses that would be occupied by the new block), then an attempt will be made
+ * to create the new block in an overlay.
*
* @param program the program in which to create the block.
* @param isOverlay if true, the block will be created in a new overlay space for that block
@@ -206,7 +212,6 @@ public class MemoryBlockUtils {
* @return the new created block
* @throws AddressOverflowException if the address
*/
-
public static MemoryBlock createInitializedBlock(Program program, boolean isOverlay,
String name, Address start, FileBytes fileBytes, long offset, long length,
String comment, String source, boolean r, boolean w, boolean x, MessageLog log)
@@ -218,21 +223,74 @@ public class MemoryBlockUtils {
}
MemoryBlock block;
try {
- block = program.getMemory().createInitializedBlock(name, start, fileBytes, offset,
- length, isOverlay);
- }
- catch (MemoryConflictException e) {
try {
+ block = program.getMemory().createInitializedBlock(name, start, fileBytes, offset,
+ length, isOverlay);
+ }
+ catch (MemoryConflictException e) {
block = program.getMemory().createInitializedBlock(name, start, fileBytes, offset,
length, true);
}
- catch (LockException | DuplicateNameException | MemoryConflictException e1) {
- throw new RuntimeException(e);
+ }
+ catch (LockException | DuplicateNameException | MemoryConflictException e) {
+ throw new RuntimeException(e);
+ }
+
+ setBlockAttributes(block, comment, source, r, w, x);
+ adjustFragment(program, block.getStart(), name);
+ return block;
+ }
+
+ /**
+ * Creates a new initialized block in memory using the bytes from the given input stream.
+ * If there is a conflict when creating this block (some other block occupies at least some
+ * of the addresses that would be occupied by the new block), then an attempt will be made
+ * to create the new block in an overlay.
+ *
+ * @param program the program in which to create the block.
+ * @param isOverlay if true, the block will be created in a new overlay space for that block
+ * @param name the name of the new block.
+ * @param start the starting address of the new block.
+ * @param dataInput the {@link InputStream} object that supplies the bytes for this block.
+ * @param dataLength the length of the new block
+ * @param comment the comment text to associate with the new block.
+ * @param source the source of the block (This field is not well defined - currently another comment)
+ * @param r the read permission for the new block.
+ * @param w the write permission for the new block.
+ * @param x the execute permission for the new block.
+ * @param log a {@link MessageLog} for appending error messages
+ * @param monitor the monitor for canceling this potentially long running operation.
+ * @return the new created block
+ * @throws AddressOverflowException if the address
+ */
+ public static MemoryBlock createInitializedBlock(Program program, boolean isOverlay,
+ String name, Address start, InputStream dataInput, long dataLength, String comment,
+ String source, boolean r, boolean w, boolean x, MessageLog log, TaskMonitor monitor)
+ throws AddressOverflowException {
+
+ if (!program.hasExclusiveAccess()) {
+ log.appendMsg("Failed to create memory block: exclusive access/checkout required");
+ return null;
+ }
+
+ Memory memory = program.getMemory();
+ MemoryBlock block;
+ try {
+ try {
+ block = memory.createInitializedBlock(name, start, dataInput, dataLength,
+ monitor, isOverlay);
+ }
+ catch (MemoryConflictException e) {
+ block = memory.createInitializedBlock(name, start, dataInput, dataLength,
+ monitor, true);
}
}
- catch (LockException | DuplicateNameException e) {
+ catch (LockException | DuplicateNameException | MemoryConflictException e) {
throw new RuntimeException(e);
}
+ catch (CancelledException e) {
+ return null;
+ }
setBlockAttributes(block, comment, source, r, w, x);
adjustFragment(program, block.getStart(), name);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PefLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PefLoader.java
index 067ee9547d..1facfd3bde 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PefLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PefLoader.java
@@ -15,17 +15,18 @@
*/
package ghidra.app.util.opinion;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.util.*;
import ghidra.app.cmd.data.CreateDataCmd;
import ghidra.app.cmd.label.AddUniqueLabelCmd;
-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.pef.*;
import ghidra.app.util.importer.MessageLog;
+import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.*;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.listing.Data;
@@ -69,6 +70,8 @@ public class PefLoader extends AbstractLibrarySupportLoader {
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
Program program, TaskMonitor monitor, MessageLog log) throws IOException {
+ FileBytes fileBytes = MemoryBlockUtils.createFileBytes(program, provider);
+
ImportStateCache importState = null;
try {
ContainerHeader header = new ContainerHeader(provider);
@@ -81,7 +84,7 @@ public class PefLoader extends AbstractLibrarySupportLoader {
program.setExecutableFormat(getName());
- processSections(header, program, importState, log, monitor);
+ processSections(header, program, fileBytes, importState, log, monitor);
processExports(header, program, importState, log, monitor);
processImports(header, program, importState, log, monitor);
processRelocations(header, program, importState, log, monitor);
@@ -367,12 +370,10 @@ public class PefLoader extends AbstractLibrarySupportLoader {
}
}
- private void processSections(ContainerHeader header, Program program,
+ private void processSections(ContainerHeader header, Program program, FileBytes fileBytes,
ImportStateCache importState, MessageLog log, TaskMonitor monitor)
throws AddressOverflowException, IOException {
- MemoryBlockUtil mbu = new MemoryBlockUtil(program);
-
List<SectionHeader> sections = header.getSections();
for (SectionHeader section : sections) {
if (monitor.isCancelled()) {
@@ -388,16 +389,17 @@ public class PefLoader extends AbstractLibrarySupportLoader {
}
if (section.getSectionKind() == SectionKind.PackedData) {
- mbu.createInitializedBlock(section.getName(), start,
- section.getUnpackedData(monitor), section.getSectionKind().toString(), null,
- section.isRead(), section.isWrite(), section.isExecute(), monitor);
+ byte[] unpackedData = section.getUnpackedData(monitor);
+ ByteArrayInputStream is = new ByteArrayInputStream(unpackedData);
+ MemoryBlockUtils.createInitializedBlock(program, false, section.getName(), start,
+ is, unpackedData.length, section.getSectionKind().toString(), null,
+ section.isRead(), section.isWrite(), section.isExecute(), log, monitor);
}
else {
- try (InputStream sectionDataStream = section.getData()) {
- mbu.createInitializedBlock(section.getName(), start, sectionDataStream,
- section.getUnpackedLength(), section.getSectionKind().toString(), null,
- section.isRead(), section.isWrite(), section.isExecute(), monitor);
- }
+ MemoryBlockUtils.createInitializedBlock(program, false, section.getName(), start,
+ fileBytes, section.getContainerOffset(), section.getUnpackedLength(),
+ section.getSectionKind().toString(), null, section.isRead(), section.isWrite(),
+ section.isExecute(), log);
}
importState.setMemoryBlockForSection(section, program.getMemory().getBlock(start));
@@ -405,15 +407,12 @@ public class PefLoader extends AbstractLibrarySupportLoader {
if (section.getUnpackedLength() < section.getTotalLength()) {
start = start.add(section.getUnpackedLength());
- mbu.createUninitializedBlock(false, section.getName(), start,
+ MemoryBlockUtils.createUninitializedBlock(program, false, section.getName(), start,
section.getTotalLength() - section.getUnpackedLength(),
section.getSectionKind().toString(), null, section.isRead(), section.isWrite(),
- section.isExecute());
+ section.isExecute(), log);
}
}
-
- log.appendMsg(mbu.getMessages());
- mbu.dispose();
}
private Address getSectionAddressAligned(SectionHeader section, Program program) {