Skip to content

Commit

Permalink
Merge branch 'GP-1949_ghidra1_MemoryMapDBRefactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidra1 committed May 2, 2022
2 parents 7b7e83b + db27e3c commit 47ad16a
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class MemorySectionResolver {
protected final Program program;

private Set<String> usedBlockNames = new HashSet<>();
private AddressSet physicalLoadedOverlaySet;

private List<MemorySection> sections = new ArrayList<>(); // built-up prior to resolve
private Map<String, Integer> sectionIndexMap = new HashMap<>();
Expand Down Expand Up @@ -267,6 +268,16 @@ public void resolve(TaskMonitor monitor) throws CancelledException {
// build-up mapping of sections to a sequence of memory ranges
sectionMemoryMap = new HashMap<>();

physicalLoadedOverlaySet = new AddressSet();
for (MemoryBlock block : getMemory().getBlocks()) {
Address minAddr = block.getStart();
Address maxAddr = block.getEnd();
if (minAddr.isLoadedMemoryAddress() && minAddr.getAddressSpace().isOverlaySpace()) {
physicalLoadedOverlaySet.add(minAddr.getPhysicalAddress(),
maxAddr.getPhysicalAddress());
}
}

// process sections in reverse order - last-in takes precedence
int sectionCount = sections.size();
monitor.initialize(sectionCount);
Expand Down Expand Up @@ -390,6 +401,10 @@ private List<AddressRange> processSectionRanges(MemorySection section,
minAddr = block.getStart();
maxAddr = block.getEnd();
usedBlockNames.add(blockName);
if (block.isOverlay() && minAddr.isLoadedMemoryAddress()) {
physicalLoadedOverlaySet.add(minAddr.getPhysicalAddress(),
maxAddr.getPhysicalAddress());
}
}
else {
// block may be null due to unexpected conflict or pruning - allow to continue
Expand Down Expand Up @@ -437,19 +452,10 @@ private AddressSet getMemoryConflictSet(Address rangeMin, Address rangeMax) {
}

// Get base memory conflict set
Memory memory = getMemory();
AddressSet rangeSet = new AddressSet(rangeMin, rangeMax);
AddressSet conflictSet = memory.intersect(rangeSet);

// Add in loaded overlay conflicts (use their physical address)
for (MemoryBlock block : memory.getBlocks()) {
Address minAddr = block.getStart();
Address maxAddr = block.getEnd();
if (minAddr.isLoadedMemoryAddress() && minAddr.getAddressSpace().isOverlaySpace()) {
AddressSet intersection = rangeSet.intersectRange(minAddr.getPhysicalAddress(),
maxAddr.getPhysicalAddress());
conflictSet.add(intersection);
}
AddressSet conflictSet = getMemory().intersectRange(rangeMin, rangeMax);
if (!physicalLoadedOverlaySet.isEmpty()) {
// Add in loaded overlay physical address conflicts
conflictSet.add(physicalLoadedOverlaySet.intersectRange(rangeMin, rangeMax));
}

return conflictSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class AddressMapDB implements AddressMap {
private Address[] sortedBaseEndAddrs;
private List<KeyRange> allKeyRanges; // all existing key ranges (includes non-absolute memory, and external space)
private HashMap<Address, Integer> addrToIndexMap = new HashMap<Address, Integer>();
private Address lastBaseAddress;
private int lastIndex;

private long baseImageOffset; // pertains to default address space only
private List<AddressRange> segmentedRanges; // when using segmented memory, this list contains
Expand Down Expand Up @@ -253,6 +255,7 @@ private void init(boolean rebuildAddrToIndexMap) {

@Override
public synchronized void invalidateCache() throws IOException {
lastBaseAddress = null;
if (!readOnly) {
baseAddrs = adapter.getBaseAddresses(true);
init(true);
Expand Down Expand Up @@ -391,8 +394,13 @@ private int getBaseAddressIndex(Address addr, boolean normalize, int indexOperat

AddressSpace space = addr.getAddressSpace();
Address tBase = space.getAddressInThisSpaceOnly(normalizedBaseOffset);
if (tBase.equals(lastBaseAddress)) {
return lastIndex;
}
Integer tIndex = addrToIndexMap.get(tBase);
if (tIndex != null) {
lastBaseAddress = tBase;
lastIndex = tIndex;
return tIndex;
}
else if (indexOperation == INDEX_MATCH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void clearMappedBlockList() {
* @return collection of blocks which map onto this block or null if none identified
*/
Collection<MemoryBlockDB> getMappedBlocks() {
memMap.buildAddressSets(); // updates mappedBlocks if needed
return mappedBlocks;
}

Expand Down Expand Up @@ -261,6 +262,7 @@ public void setExecute(boolean x) {
try {
checkValid();
setPermissionBit(EXECUTE, x);
memMap.blockExecuteChanged(this);
memMap.fireBlockChanged(this);
}
finally {
Expand All @@ -276,6 +278,7 @@ public void setPermissions(boolean read, boolean write, boolean execute) {
setPermissionBit(READ, read);
setPermissionBit(WRITE, write);
setPermissionBit(EXECUTE, execute);
memMap.blockExecuteChanged(this);
memMap.fireBlockChanged(this);
}
finally {
Expand Down
Loading

0 comments on commit 47ad16a

Please sign in to comment.