Ghidra is a software reverse engineering (SRE) framework
GP-921: Fixing byte-level dynamic<->static translation
Dan33ae47e646ef
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/listing/DebuggerListingProvider.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/listing/DebuggerListingProvider.javaindex cdc6637163..3b2e552a4a 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/listing/DebuggerListingProvider.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/listing/DebuggerListingProvider.java@@ -50,8 +50,7 @@ import ghidra.app.plugin.core.debug.gui.action.*;import ghidra.app.plugin.core.debug.gui.action.AutoReadMemorySpec.AutoReadMemorySpecConfigFieldCodec;import ghidra.app.plugin.core.debug.gui.action.LocationTrackingSpec.TrackingSpecConfigFieldCodec;import ghidra.app.plugin.core.debug.gui.modules.DebuggerMissingModuleActionContext;-import ghidra.app.plugin.core.debug.utils.BackgroundUtils;-import ghidra.app.plugin.core.debug.utils.ProgramURLUtils;+import ghidra.app.plugin.core.debug.utils.*;import ghidra.app.plugin.core.exporter.ExporterDialog;import ghidra.app.plugin.processors.sleigh.SleighLanguage;import ghidra.app.services.*;@@ -870,7 +869,7 @@ public class DebuggerListingProvider extends CodeViewerProvider implements Listipublic void programLocationChanged(ProgramLocation location, EventTrigger trigger) {updateLocationLabel();if (traceManager != null) {- location = traceManager.fixLocation(location, false);+ location = ProgramLocationUtils.fixLocation(location, false);}super.programLocationChanged(location, trigger);if (trigger == EventTrigger.GUI_ACTION) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.javaindex a639ab81a8..db35964926 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java@@ -1118,7 +1118,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin@Overridepublic ProgramLocation getStaticLocationFromDynamic(ProgramLocation loc) {- loc = traceManager.fixLocation(loc, true);+ loc = ProgramLocationUtils.fixLocation(loc, true);TraceProgramView view = (TraceProgramView) loc.getProgram();Trace trace = view.getTrace();TraceLocation tloc = new DefaultTraceLocation(trace, null,diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.javaindex 8935725d04..cf3d8695b2 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java@@ -31,7 +31,6 @@ import ghidra.app.plugin.core.debug.DebuggerCoordinates;import ghidra.app.plugin.core.debug.DebuggerPluginPackage;import ghidra.app.plugin.core.debug.event.*;import ghidra.app.plugin.core.debug.gui.DebuggerResources.*;-import ghidra.app.plugin.core.debug.utils.ProgramLocationUtils;import ghidra.app.services.*;import ghidra.async.AsyncConfigFieldCodec.BooleanAsyncConfigFieldCodec;import ghidra.async.AsyncReference;@@ -47,8 +46,6 @@ import ghidra.framework.plugintool.annotation.AutoConfigStateField;import ghidra.framework.plugintool.annotation.AutoServiceConsumed;import ghidra.framework.plugintool.util.PluginStatus;import ghidra.lifecycle.Internal;-import ghidra.program.model.listing.Program;-import ghidra.program.util.ProgramLocation;import ghidra.trace.model.Trace;import ghidra.trace.model.Trace.TraceThreadChangeType;import ghidra.trace.model.TraceDomainObjectListener;@@ -1147,23 +1144,6 @@ public class DebuggerTraceManagerServicePlugin extends PluginautoCloseOnTerminate.removeChangeListener(listener);}- @Override- // TODO: Move this into some static util, now that canonical view is a trace concept- public ProgramLocation fixLocation(ProgramLocation location, boolean matchSnap) {- Program program = location.getProgram();- if (!(program instanceof TraceProgramView)) {- return location;- }- TraceProgramView itsView = (TraceProgramView) program;- Trace trace = itsView.getTrace();- TraceProgramView canonicalView = trace.getProgramView();- if (canonicalView == itsView ||- (matchSnap && canonicalView.getSnap() != itsView.getSnap())) {- return location;- }- return ProgramLocationUtils.replaceProgram(location, canonicalView);- }-@Overridepublic boolean canClose() {if (isSaveTracesByDefault()) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/utils/ProgramLocationUtils.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/utils/ProgramLocationUtils.javaindex c641d70aa4..dadbdd614b 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/utils/ProgramLocationUtils.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/utils/ProgramLocationUtils.java@@ -18,20 +18,48 @@ package ghidra.app.plugin.core.debug.utils;import ghidra.framework.options.SaveState;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Program;+import ghidra.program.util.BytesFieldLocation;import ghidra.program.util.ProgramLocation;+import ghidra.trace.model.Trace;+import ghidra.trace.model.program.TraceProgramView;public enum ProgramLocationUtils {;public static ProgramLocation replaceAddress(ProgramLocation loc, Program program,Address address) {- // HACK: ... and a half- SaveState state = new SaveState("LOC");- loc.saveState(state);- state.putString("_ADDRESS", address.toString());- state.putString("_BYTE_ADDR",- address.add(loc.getByteAddress().subtract(loc.getAddress())).toString());- return ProgramLocation.getLocation(program, state);+ // Outside of byte fields, I really don't care+ if (loc instanceof BytesFieldLocation) {+ return new BytesFieldLocation(program, address);+ }+ return new ProgramLocation(program, address);+ }++ /**+ * Swap out the trace view of a {@link ProgramLocation} if it is not the canonical view+ *+ * <p>+ * If the program location is not associated with a trace, the same location is returned.+ * Otherwise, this ensures that the given view is the canonical one for the same trace. If+ * matchSnap is true, the view is only replaced when the replacement shares the same snap.+ *+ * @param location a location possibly in a trace view+ * @param matchSnap true to only replace is snap matches, false to always replace+ * @return the adjusted location+ */+ public static ProgramLocation fixLocation(ProgramLocation loc, boolean matchSnap) {+ Program program = loc.getProgram();+ if (!(program instanceof TraceProgramView)) {+ return loc;+ }+ TraceProgramView itsView = (TraceProgramView) program;+ Trace trace = itsView.getTrace();+ TraceProgramView canonicalView = trace.getProgramView();+ if (canonicalView == itsView ||+ (matchSnap && canonicalView.getSnap() != itsView.getSnap())) {+ return loc;+ }+ return replaceProgram(loc, canonicalView);}public static ProgramLocation replaceProgram(ProgramLocation loc, Program program) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerTraceManagerService.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerTraceManagerService.javaindex 722912e048..7ef5c3d2aa 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerTraceManagerService.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerTraceManagerService.java@@ -22,7 +22,6 @@ import ghidra.app.plugin.core.debug.DebuggerCoordinates;import ghidra.app.plugin.core.debug.service.tracemgr.DebuggerTraceManagerServicePlugin;import ghidra.framework.model.DomainFile;import ghidra.framework.plugintool.ServiceInfo;-import ghidra.program.util.ProgramLocation;import ghidra.trace.model.Trace;import ghidra.trace.model.program.TraceProgramView;import ghidra.trace.model.thread.TraceThread;@@ -195,21 +194,6 @@ public interface DebuggerTraceManagerService {void removeAutoCloseOnTerminateChangeListener(BooleanChangeAdapter listener);- /**- * Swap out the trace view of a {@link ProgramLocation} if it is not the debugger's view- *- * <p>- * If the program location is not associated with a trace, the same location is returned.- * Otherwise, this ensures that the given view is the one found in the debugger plugin for the- * same trace. If matchSnap is true, the view is only replaced when the replacement shares the- * same snap.- *- * @param location a location possibly in a trace view- * @param matchSnap true to only replace is snap matches, false to always replace- * @return the adjusted location- */- ProgramLocation fixLocation(ProgramLocation location, boolean matchSnap);-/*** Fill in an incomplete coordinate specification, using the manager's "best judgement"*