Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7899: Fixing issue with Mach-O stubs when local or absolute symbols are present in the dynamic symbol table. Also fixing first entry of string table on dyld extractions. (Closes #7899)

Ryan Kurtz718987e811ac
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/NList.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/NList.java
index 12b1eb4ae6..ce529b29b6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/NList.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/NList.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -217,7 +217,7 @@ public class NList implements StructConverter {
*/
public static int getSize(List<NList> nlists) {
if (!nlists.isEmpty()) {
- int totalStringSize = 0;
+ int totalStringSize = 1; // First byte should always be 0
for (NList nlist : nlists) {
totalStringSize += nlist.getString().length() + 1; // Add 1 for null terminator
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
index 2463965617..062dc8c9d1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
@@ -689,16 +689,16 @@ public class MachoProgramBuilder {
}
int symbolIndex = indirectSymbols[i];
NList symbol = symbolTableCommand.getSymbolAt(symbolIndex);
- if (symbol == null) {
- continue;
- }
- String name = SymbolUtilities.replaceInvalidChars(symbol.getString(), true);
- if (name != null && name.length() > 0) {
- Function stubFunc = createOneByteFunction(name, startAddr);
- if (stubFunc != null) {
- ExternalLocation loc = program.getExternalManager()
- .addExtLocation(Library.UNKNOWN, name, null, SourceType.IMPORTED);
- stubFunc.setThunkedFunction(loc.createFunction());
+ if (symbol != null) {
+ String name = SymbolUtilities.replaceInvalidChars(symbol.getString(), true);
+ if (name != null && name.length() > 0) {
+ Function stubFunc = createOneByteFunction(name, startAddr);
+ if (stubFunc != null) {
+ ExternalLocation loc = program.getExternalManager()
+ .addExtLocation(Library.UNKNOWN, name, null,
+ SourceType.IMPORTED);
+ stubFunc.setThunkedFunction(loc.createFunction());
+ }
}
}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/ExtractedMacho.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/ExtractedMacho.java
index abf05f5677..613b7f3576 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/ExtractedMacho.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/ExtractedMacho.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -225,8 +225,8 @@ public class ExtractedMacho {
List<NList> symbols = symbolTable.getSymbols();
byte[] packedSymbolStringTable = new byte[NList.getSize(symbols)];
int nlistIndex = 0;
- int stringIndex = symbols.get(0).getSize() * symbols.size();
- int stringIndexOrig = stringIndex;
+ int stringIndexOrig = symbols.get(0).getSize() * symbols.size();
+ int stringIndex = stringIndexOrig + 1; // First byte is always 0
for (NList nlist : symbols) {
byte[] nlistArray = nlistToArray(nlist, stringIndex - stringIndexOrig);
byte[] stringArray = nlist.getString().getBytes(StandardCharsets.US_ASCII);