157 lines
6.4 KiB
Java
157 lines
6.4 KiB
Java
// Export named non-code symbols as a machine-readable TSV file.
|
|
// @category Iggy
|
|
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.program.model.address.Address;
|
|
import ghidra.program.model.listing.Data;
|
|
import ghidra.program.model.mem.MemoryBlock;
|
|
import ghidra.program.model.symbol.ReferenceIterator;
|
|
import ghidra.program.model.symbol.SourceType;
|
|
import ghidra.program.model.symbol.Symbol;
|
|
import ghidra.program.model.symbol.SymbolIterator;
|
|
import ghidra.program.model.symbol.SymbolType;
|
|
|
|
import java.io.File;
|
|
import java.io.PrintWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
|
|
public class ExportDataSymbols extends GhidraScript {
|
|
private static class DataSymbol {
|
|
private final Symbol symbol;
|
|
private final MemoryBlock block;
|
|
private final Data data;
|
|
|
|
DataSymbol(Symbol symbol, MemoryBlock block, Data data) {
|
|
this.symbol = symbol;
|
|
this.block = block;
|
|
this.data = data;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void run() throws Exception {
|
|
String[] args = getScriptArgs();
|
|
if (args.length != 1) {
|
|
throw new IllegalArgumentException("expected output directory");
|
|
}
|
|
|
|
File outputDirectory = new File(args[0]);
|
|
if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
|
|
throw new IllegalStateException("could not create " + outputDirectory);
|
|
}
|
|
|
|
List<DataSymbol> symbols = collectSymbols();
|
|
String outputName = currentProgram.getName().replaceAll("(?i)\\.obj$", "") + ".tsv";
|
|
File output = new File(outputDirectory, outputName);
|
|
try (PrintWriter writer = new PrintWriter(output, "UTF-8")) {
|
|
writer.println("address\tname\tsection\tinitialized\tdata_type\t" +
|
|
"defined_data_length\tspan_upper_bound\tsymbol_source\treference_count");
|
|
for (int index = 0; index < symbols.size(); index++) {
|
|
DataSymbol entry = symbols.get(index);
|
|
writer.print(entry.symbol.getAddress());
|
|
writer.print('\t');
|
|
writer.print(clean(entry.symbol.getName(true)));
|
|
writer.print('\t');
|
|
writer.print(clean(entry.block.getName()));
|
|
writer.print('\t');
|
|
writer.print(entry.block.isInitialized() ? "1" : "0");
|
|
writer.print('\t');
|
|
writer.print(entry.data == null ? "" : clean(entry.data.getDataType().getDisplayName()));
|
|
writer.print('\t');
|
|
writer.print(entry.data == null ? 0 : entry.data.getLength());
|
|
writer.print('\t');
|
|
writer.print(spanUpperBound(symbols, index));
|
|
writer.print('\t');
|
|
writer.print(entry.symbol.getSource());
|
|
writer.print('\t');
|
|
writer.println(referenceCount(entry.symbol.getAddress()));
|
|
}
|
|
}
|
|
|
|
println("Exported " + symbols.size() + " data symbols from " +
|
|
currentProgram.getName() + " to " + output);
|
|
}
|
|
|
|
private List<DataSymbol> collectSymbols() {
|
|
List<DataSymbol> result = new ArrayList<>();
|
|
SymbolIterator iterator = currentProgram.getSymbolTable().getAllSymbols(true);
|
|
while (iterator.hasNext()) {
|
|
Symbol symbol = iterator.next();
|
|
if (!isRecoverableDataSymbol(symbol)) {
|
|
continue;
|
|
}
|
|
MemoryBlock block = currentProgram.getMemory().getBlock(symbol.getAddress());
|
|
result.add(new DataSymbol(
|
|
symbol,
|
|
block,
|
|
currentProgram.getListing().getDataAt(symbol.getAddress())
|
|
));
|
|
}
|
|
result.sort(Comparator.comparing(entry -> entry.symbol.getAddress()));
|
|
return result;
|
|
}
|
|
|
|
private boolean isRecoverableDataSymbol(Symbol symbol) {
|
|
if (symbol.getSymbolType() != SymbolType.LABEL || !symbol.isPrimary() ||
|
|
symbol.isDynamic() || symbol.getSource() == SourceType.DEFAULT) {
|
|
return false;
|
|
}
|
|
Address address = symbol.getAddress();
|
|
MemoryBlock block = currentProgram.getMemory().getBlock(address);
|
|
if (block == null || block.isExecute() || isMetadataBlock(block) ||
|
|
currentProgram.getFunctionManager().getFunctionContaining(address) != null) {
|
|
return false;
|
|
}
|
|
String name = symbol.getName();
|
|
return !name.startsWith("DAT_") && !name.startsWith("LAB_") &&
|
|
!name.startsWith("PTR_") && !name.startsWith("switchD_") &&
|
|
!name.startsWith("s_") && !name.startsWith("u_") &&
|
|
!name.startsWith("__real@") && !name.startsWith("__xmm@") &&
|
|
!name.startsWith("??_C@") && !name.startsWith("$SG");
|
|
}
|
|
|
|
private boolean isMetadataBlock(MemoryBlock block) {
|
|
String name = block.getName().toLowerCase();
|
|
return name.equals("external") || name.startsWith(".pdata") ||
|
|
name.startsWith(".xdata") ||
|
|
name.startsWith(".eh_frame") || name.startsWith(".rela") ||
|
|
name.startsWith(".rel") || name.startsWith(".debug") ||
|
|
name.startsWith(".comment") || name.startsWith(".note") ||
|
|
name.startsWith(".symtab") || name.startsWith(".strtab") ||
|
|
name.startsWith(".dyn") || name.startsWith(".hash") ||
|
|
name.startsWith(".gnu") || name.startsWith(".plt") ||
|
|
name.startsWith(".got") || name.startsWith(".opd") ||
|
|
name.startsWith(".toc") || name.startsWith(".branch_lt");
|
|
}
|
|
|
|
private long spanUpperBound(List<DataSymbol> symbols, int index) {
|
|
DataSymbol current = symbols.get(index);
|
|
if (index + 1 < symbols.size()) {
|
|
DataSymbol next = symbols.get(index + 1);
|
|
if (current.block.equals(next.block)) {
|
|
return next.symbol.getAddress().subtract(current.symbol.getAddress());
|
|
}
|
|
}
|
|
return current.block.getEnd().subtract(current.symbol.getAddress()) + 1;
|
|
}
|
|
|
|
private int referenceCount(Address address) {
|
|
int count = 0;
|
|
ReferenceIterator references = currentProgram.getReferenceManager().getReferencesTo(address);
|
|
while (references.hasNext()) {
|
|
references.next();
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
private String clean(String value) {
|
|
if (value == null) {
|
|
return "";
|
|
}
|
|
return value.replace('\t', ' ').replace('\r', ' ').replace('\n', ' ');
|
|
}
|
|
}
|