Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1756: Changed new Go To File Offset feature to use "file(n)" instead of "file:n" to avoid precedence issues with common overlay address spaces

Ryan Kurtzb26ede3a7e4b
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm b/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm
index 2e44767784..a2e4f4d0d6 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm
@@ -221,10 +221,10 @@
</H3>
<BLOCKQUOTE>
- <P>Enter <B>file:</B> followed by a file offset of the program's source file bytes (at
- time of import) into the text area of the dialog. The file offset entered will be assumed
- to be in decimal, unless it is preceeded by <B>0x</B>. That is, "file:0x1000" and
- "file:1000" are different values.</P>
+ <P>Enter <B>file(n)</B>, where <B>n</b> is a file offset of the program's source file bytes
+ (at time of import), into the text area of the dialog. The file offset entered will be
+ assumed to be in decimal unless it is preceeded by <B>0x</B>. That is, "file(0x1000)" and
+ "file(1000)" are different values.</P>
<P><I><IMG src="../../shared/note.png" border="0">Ghidra does not support storing source
file bytes for all file formats. Searching for a file offset in these programs will always
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java
index 4d46ad4a75..133aaad0a8 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java
@@ -17,11 +17,11 @@ package ghidra.app.util.navigation;
import java.awt.Component;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.swing.SwingUtilities;
-import org.apache.commons.lang3.StringUtils;
-
import docking.widgets.table.threaded.ThreadedTableModelListener;
import ghidra.GhidraOptions;
import ghidra.app.nav.Navigatable;
@@ -50,7 +50,13 @@ import ghidra.util.table.GhidraProgramTableModel;
import ghidra.util.task.TaskMonitor;
public class GoToQuery {
- private final String FILE_OFFSET_PREFIX = "file:";
+
+ /**
+ * Regex used for going to a file offset. We expect something of the form <code>file(n)</code>,
+ * where <code>n</code> can be hex or decimal. Spaces should be ignored.
+ */
+ private Pattern FILE_OFFSET_REGEX = Pattern
+ .compile("file\\s*\\(\\s*((0x[0-9a-fA-F]+|[0-9]+))\\s*\\)", Pattern.CASE_INSENSITIVE);
private QueryData queryData;
private Address fromAddress;
@@ -363,9 +369,10 @@ public class GoToQuery {
private boolean processFileOffset() {
String input = queryData.getQueryString();
- if (StringUtils.startsWithIgnoreCase(input, FILE_OFFSET_PREFIX)) {
+ Matcher matcher = FILE_OFFSET_REGEX.matcher(input);
+ if (matcher.matches()) {
try {
- long offset = Long.decode(input.substring(FILE_OFFSET_PREFIX.length()));
+ long offset = Long.decode(matcher.group(1));
// NOTE: Addresses are parsed via AbstractAddressSpace.parseString(String addr)
Program currentProgram = navigatable.getProgram();
Memory mem = currentProgram.getMemory();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToAddressLabelPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToAddressLabelPluginTest.java
index 8b0c2285f6..cf5432725d 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToAddressLabelPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/GoToAddressLabelPluginTest.java
@@ -250,31 +250,37 @@ public class GoToAddressLabelPluginTest extends AbstractGhidraHeadedIntegrationT
});
// Test decimal
- setText("file:0");
+ setText("file(0)");
showDialog();
performOkCallback();
assertEquals(addr1, cbPlugin.getCurrentAddress());
// Test hex
- setText("file:0x1");
+ setText("file(0x1)");
showDialog();
performOkCallback();
assertEquals(addr1.add(1), cbPlugin.getCurrentAddress());
// Test "case"
- setText("FILE:2");
+ setText("FILe(0X2)");
showDialog();
performOkCallback();
assertEquals(addr1.add(2), cbPlugin.getCurrentAddress());
+ // Test spaces
+ setText("file ( 0 )");
+ showDialog();
+ performOkCallback();
+ assertEquals(addr1, cbPlugin.getCurrentAddress());
+
// Test not found
- setText("file:0x100");
+ setText("file(0x100)");
showDialog();
performOkCallback();
assertNotEquals(addr1.add(0x100), cbPlugin.getCurrentAddress());
// Test multiple results
- setText("file:3");
+ setText("file(3)");
showDialog();
performOkCallback();
GhidraProgramTableModel<?> model = waitForModel();