// Apply public and static COFF names retained in an lld-link map. // @category Iggy import ghidra.app.script.GhidraScript; import ghidra.program.model.address.Address; import ghidra.program.model.mem.MemoryBlock; import ghidra.program.model.listing.Function; import ghidra.program.model.symbol.SourceType; import ghidra.program.model.symbol.Symbol; import ghidra.util.exception.DuplicateNameException; import ghidra.util.exception.InvalidInputException; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ApplyLinkMap extends GhidraScript { private static final Pattern MAP_SYMBOL = Pattern.compile( "^\\s*[0-9A-Fa-f]{4}:[0-9A-Fa-f]{8}\\s+(\\S+)\\s+([0-9A-Fa-f]{16})\\s+(.+)$" ); @Override protected void run() throws Exception { String[] args = getScriptArgs(); if (args.length != 1) { throw new IllegalArgumentException("expected lld-link map file"); } int functionsApplied = 0; int dataApplied = 0; try (BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])))) { String line; while ((line = reader.readLine()) != null) { Matcher matcher = MAP_SYMBOL.matcher(line); if (!matcher.matches()) { continue; } String originalName = matcher.group(1); if (originalName.startsWith("$") || originalName.startsWith("__real@") || originalName.startsWith("??_C@")) { continue; } Address address = toAddr(matcher.group(2)); Function function = currentProgram.getFunctionManager().getFunctionAt(address); String safeName = makeSafeName(originalName); if (function != null) { if (!function.getName().startsWith("FUN_")) { continue; } try { function.setName(safeName, SourceType.IMPORTED); } catch (DuplicateNameException exception) { function.setName(safeName + "_" + matcher.group(2), SourceType.IMPORTED); } catch (InvalidInputException exception) { printerr("Could not apply " + originalName + " at " + matcher.group(2)); continue; } function.setComment("Recovered linker-map symbol: " + originalName + " (" + matcher.group(3) + ")"); functionsApplied++; continue; } MemoryBlock block = currentProgram.getMemory().getBlock(address); if (block == null || block.isExecute() || currentProgram.getFunctionManager().getFunctionContaining(address) != null) { continue; } try { Symbol symbol = currentProgram.getSymbolTable().createLabel( address, safeName, SourceType.IMPORTED ); symbol.setPrimary(); } catch (InvalidInputException exception) { Symbol symbol = currentProgram.getSymbolTable().createLabel( address, safeName + "_" + matcher.group(2), SourceType.IMPORTED ); symbol.setPrimary(); } dataApplied++; } } println("Applied " + functionsApplied + " linker-map function names and " + dataApplied + " data names to " + currentProgram.getName()); } private String makeSafeName(String originalName) { String safeName = originalName.replace("::", "__") .replaceAll("[^A-Za-z0-9_$]", "_"); if (safeName.isEmpty() || Character.isDigit(safeName.charAt(0))) { safeName = "recovered_" + safeName; } return safeName; } }