mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-05-24 03:09:36 +08:00
GP-1186 added method for finding addresses where a specific byte from a fileBytes object was loaded into memory
This commit is contained in:
+26
-2
@@ -17,8 +17,7 @@ package ghidra.program.model.mem;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import ghidra.framework.store.LockException;
|
||||
import ghidra.program.database.mem.*;
|
||||
@@ -847,4 +846,29 @@ public interface Memory extends AddressSetView {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of addresses where the byte at the given offset
|
||||
* from the given FileBytes was loaded into memory.
|
||||
* @param offset the file offset in the given FileBytes of the byte that is to be
|
||||
* located in memory
|
||||
* @param fileBytes the FileBytesobject whose byte is to be located in memory
|
||||
* @return a list of addresses that are associated with the given
|
||||
* FileBytes and offset
|
||||
*/
|
||||
public default List<Address> locateAddressesForFileBytesOffset(FileBytes fileBytes, long offset) {
|
||||
List<Address> list = new ArrayList<>();
|
||||
for (MemoryBlock memBlock : getBlocks()) {
|
||||
for (MemoryBlockSourceInfo info : memBlock.getSourceInfos()) {
|
||||
Optional<FileBytes> blockFileBytes = info.getFileBytes();
|
||||
if (blockFileBytes.isPresent() && blockFileBytes.get().equals(fileBytes)) {
|
||||
Address addr = info.locateAddressForFileOffset(offset);
|
||||
if (addr != null) {
|
||||
list.add(addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -294,6 +294,25 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAddressForFileBytesAndOffset() throws Exception {
|
||||
FileBytes fileBytes = createFileBytes();
|
||||
mem.createInitializedBlock("test1", addr(100), fileBytes, 10, 50, false);
|
||||
mem.createInitializedBlock("test2", addr(200), fileBytes, 40, 50, false);
|
||||
|
||||
List<Address> addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 0);
|
||||
assertTrue(addresses.isEmpty());
|
||||
|
||||
addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 10);
|
||||
assertEquals(1, addresses.size());
|
||||
assertEquals(addr(100), addresses.get(0));
|
||||
|
||||
addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 40);
|
||||
assertEquals(2, addresses.size());
|
||||
assertTrue(addresses.contains(addr(130)));
|
||||
assertTrue(addresses.contains(addr(200)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user