Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-536: Fixed parsing of architecture and sections with GDB 10.1

Dan6d15c4e00e11
diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbModuleImpl.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbModuleImpl.java
index 9603f6c43a..1af9421c5f 100644
--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbModuleImpl.java
+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbModuleImpl.java
@@ -30,13 +30,26 @@ import ghidra.util.Msg;
public class GdbModuleImpl implements GdbModule {
protected static final Pattern OBJECT_FILE_LINE_PATTERN =
Pattern.compile("\\s*Object file: (?<name>.*)");
- protected static final Pattern OBJECT_SECTION_LINE_PATTERN = Pattern.compile(
+
+ // Pattern observed in GDB 8 (probably applies to previous, too)
+ protected static final Pattern OBJECT_SECTION_LINE_PATTERN_V8 = Pattern.compile(
+ "\\s*" + //
+ "0x(?<vmaS>[0-9A-Fa-f]+)\\s*->\\s*" + //
+ "0x(?<vmaE>[0-9A-Fa-f]+)\\s+at\\s+" + //
+ "0x(?<offset>[0-9A-Fa-f]+)\\s*:\\s*" + //
+ "(?<name>\\S+)\\s+" + //
+ "(?<attrs>.*)");
+
+ // Pattern observed in GDB 10 (may apply in 9, too)
+ protected static final Pattern OBJECT_SECTION_LINE_PATTERN_V10 = Pattern.compile(
"\\s*" + //
+ "\\[\\s*(?<idx>\\d+)\\]\\s+" + //
"0x(?<vmaS>[0-9A-Fa-f]+)\\s*->\\s*" + //
"0x(?<vmaE>[0-9A-Fa-f]+)\\s+at\\s+" + //
"0x(?<offset>[0-9A-Fa-f]+)\\s*:\\s*" + //
"(?<name>\\S+)\\s+" + //
"(?<attrs>.*)");
+
protected static final Pattern MSYMBOL_LINE_PATTERN = Pattern.compile(
"\\s*" + //
"\\[\\s*(?<idx>\\d+)\\]\\s+" + //
@@ -50,6 +63,8 @@ public class GdbModuleImpl implements GdbModule {
protected Long base = null;
protected Long max = null;
+ protected Pattern sectionLinePattern = OBJECT_SECTION_LINE_PATTERN_V10;
+
protected final Map<String, GdbModuleSectionImpl> sections = new LinkedHashMap<>();
protected final Map<String, GdbModuleSection> unmodifiableSections =
Collections.unmodifiableMap(sections);
@@ -143,8 +158,32 @@ public class GdbModuleImpl implements GdbModule {
return minimalSymbols.request();
}
+ protected Matcher matchSectionLine(Pattern pattern, String line) {
+ Matcher matcher = pattern.matcher(line);
+ if (matcher.matches()) {
+ sectionLinePattern = pattern;
+ }
+ return matcher;
+ }
+
+ protected Matcher matchSectionLine(String line) {
+ Matcher matcher = sectionLinePattern.matcher(line);
+ if (matcher.matches()) {
+ return matcher;
+ }
+ matcher = matchSectionLine(OBJECT_SECTION_LINE_PATTERN_V10, line);
+ if (matcher.matches()) {
+ return matcher;
+ }
+ matcher = matchSectionLine(OBJECT_SECTION_LINE_PATTERN_V8, line);
+ if (matcher.matches()) {
+ return matcher;
+ }
+ return matcher;
+ }
+
protected void processSectionLine(String line) {
- Matcher matcher = OBJECT_SECTION_LINE_PATTERN.matcher(line);
+ Matcher matcher = matchSectionLine(line);
if (matcher.matches()) {
try {
long vmaStart = Long.parseLong(matcher.group("vmaS"), 16);
diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetEnvironment.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetEnvironment.java
index 62aa4d7277..2dfaf1a868 100644
--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetEnvironment.java
+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetEnvironment.java
@@ -74,11 +74,15 @@ public class GdbModelTargetEnvironment
String[] tokens = out.split("\\s+");
@SuppressWarnings("hiding")
String arch = tokens[tokens.length - 1].trim();
- if (arch.endsWith(")")) {
+ while (arch.endsWith(".") || arch.endsWith(")") || arch.endsWith("\"")) {
arch = arch.substring(0, arch.length() - 1);
}
+ while (arch.startsWith("\"")) {
+ arch = arch.substring(1);
+ }
// e.g., The target architecture is set automatically (currently i386)
// e.g., The target architecture is assumed to be i386
+ // e.g., The target architecture is set to "auto" (currently "i386").
// TODO: I don't have a way to detect if this parsing strategy fails.
// TODO: I could search using a list of support architectures
// Use "set architecture" to get "Valid arguments"