Skip to content

Commit

Permalink
fix(ELF): precompute set of symbol names targeted by a relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
boricj committed May 8, 2024
1 parent a1f7180 commit e82b80f
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -64,7 +65,6 @@
import ghidra.program.model.symbol.Symbol;
import ghidra.util.classfinder.ClassSearcher;
import ghidra.util.task.TaskMonitor;

import ghidra_delinker_extension.BuildConfig;

/**
Expand Down Expand Up @@ -96,6 +96,7 @@ public class ElfRelocatableObjectExporter extends Exporter {

private Map<String, ElfRelocatableSymbol> symbolsByName;
private List<Section> sections = new ArrayList<>();
private Set<String> symbolNamesRelocationFileSet;

private static final String OPTION_GROUP_ELF_HEADER = "ELF header";
private static final String OPTION_GROUP_SYMBOLS = "Symbols";
Expand Down Expand Up @@ -469,14 +470,9 @@ private boolean isSymbolInteresting(Symbol symbol, RelocationTable relocationTab
if (symbol.isDynamic() && !includeDynamicSymbols) {
// Even if we don't want dynamic symbols, we still need them for internal relocations.
// FIXME: investigate section-relative relocations for internal relocations with dynamic symbols.
Iterable<Relocation> itRelocations =
() -> relocationTable.getRelocations(fileSet, predicateRelocation);
Stream<Relocation> relocations =
StreamSupport.stream(itRelocations.spliterator(), false);
String symbolName = getSymbolName(symbol.getName());

return relocations
.anyMatch(r -> getSymbolName(r.getSymbolName()).equals(symbolName));
return symbolNamesRelocationFileSet.contains(symbolName);
}

return true;
Expand Down Expand Up @@ -618,6 +614,12 @@ public boolean export(File file, DomainObject domainObj, AddressSetView fileSet,
Predicate<Relocation> predicateRelocation =
relocationTable.predicateInterestingRelocations(fileSet);

for (MemoryBlock memoryBlock : program.getMemory().getBlocks()) {
addSectionForMemoryBlock(memoryBlock, predicateRelocation);
}

taskMonitor.setIndeterminate(true);

try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
elf = new ElfRelocatableObject.Builder(file.getName())
.setType(ElfConstants.ET_REL)
Expand All @@ -626,13 +628,16 @@ public boolean export(File file, DomainObject domainObj, AddressSetView fileSet,
.setData(e_ident_data)
.build();

if (generateSectionComment) {
addSectionComment();
}

if (generateStringAndSymbolTables) {
addStringAndSymbolTables();
}

for (MemoryBlock memoryBlock : program.getMemory().getBlocks()) {
addSectionForMemoryBlock(memoryBlock, predicateRelocation);
}
taskMonitor.setMessage("Compute file symbol set...");
computeSymbolNamesRelocationFileSet(relocationTable, predicateRelocation);

for (Section section : sections) {
taskMonitor.setMessage(String.format("Creating section %s...", section.getName()));
Expand All @@ -657,10 +662,6 @@ public boolean export(File file, DomainObject domainObj, AddressSetView fileSet,
}
}

if (generateSectionComment) {
addSectionComment();
}

if (generateSectionNamesStringTable) {
taskMonitor.setMessage("Generating section names string table...");
addSectionNameStringTable();
Expand All @@ -683,6 +684,16 @@ public static Program getProgram(DomainObject domainObj) {
return (Program) domainObj;
}

private void computeSymbolNamesRelocationFileSet(RelocationTable relocationTable,
Predicate<Relocation> predicateRelocation) {
Iterable<Relocation> itRelocations =
() -> relocationTable.getRelocations(fileSet, predicateRelocation);
Stream<Relocation> relocations =
StreamSupport.stream(itRelocations.spliterator(), false);
symbolNamesRelocationFileSet =
relocations.map(r -> r.getSymbolName()).collect(Collectors.toSet());
}

private void addStringAndSymbolTables() {
strtab = new ElfRelocatableSectionStringTable(elf, ElfSectionHeaderConstants.dot_strtab);
symtab =
Expand Down

0 comments on commit e82b80f

Please sign in to comment.