Merge remote-tracking branch

'origin/GP-3960_ryanmkurtz_ElfLoader_swift_go--SQUASHED' (Closes #8795)
This commit is contained in:
Ryan Kurtz
2025-12-23 19:23:04 -05:00
6 changed files with 47 additions and 6 deletions

View File

@@ -1070,7 +1070,7 @@ public class ElfHeader implements StructConverter {
return offset >= 0 && (offset + length) <= provider.length();
}
protected void parseSectionHeaders() throws IOException {
public void parseSectionHeaders() throws IOException {
if (reader == null) {
throw new IOException("ELF binary reader is null!");
}

View File

@@ -20,8 +20,10 @@ import java.util.*;
import ghidra.app.util.Option;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.format.elf.ElfException;
import ghidra.app.util.bin.format.elf.ElfHeader;
import ghidra.app.util.bin.format.elf.*;
import ghidra.app.util.bin.format.golang.GoConstants;
import ghidra.app.util.bin.format.golang.rtti.GoRttiMapper;
import ghidra.app.util.bin.format.swift.SwiftUtils;
import ghidra.framework.model.DomainObject;
import ghidra.framework.model.ProjectData;
import ghidra.framework.options.Options;
@@ -102,8 +104,13 @@ public class ElfLoader extends AbstractLibrarySupportLoader {
try {
ElfHeader elf = new ElfHeader(provider, null);
List<QueryResult> results =
QueryOpinionService.query(getName(), elf.getMachineName(), elf.getFlags());
Set<QueryResult> results = new HashSet<>();
String machine = elf.getMachineName();
String compiler = detectCompilerName(elf);
if (compiler != null) {
results.addAll(QueryOpinionService.query(getName(), machine, compiler));
}
results.addAll(QueryOpinionService.query(getName(), machine, elf.getFlags()));
for (QueryResult result : results) {
boolean add = true;
// Some languages are defined with sizes smaller than 32
@@ -170,4 +177,25 @@ public class ElfLoader extends AbstractLibrarySupportLoader {
public String getName() {
return ELF_NAME;
}
/**
* Attempts to detect a more specific compiler from the ELF
*
* @param elf The {@link ElfHeader}
* @return The detected compiler name, or {@code null} if one couldn't be detected
* @throws IOException if an IO-related error occurred
*/
private String detectCompilerName(ElfHeader elf) throws IOException {
elf.parseSectionHeaders();
List<String> sectionNames = Arrays.stream(elf.getSections())
.map(ElfSectionHeader::getNameAsString)
.toList();
if (SwiftUtils.isSwift(sectionNames)) {
return SwiftUtils.SWIFT_COMPILER;
}
if (GoRttiMapper.hasGolangSections(sectionNames)) {
return GoConstants.GOLANG_CSPEC_NAME;
}
return null;
}
}

View File

@@ -531,7 +531,7 @@ public class MachoLoader extends AbstractLibrarySupportLoader {
* Attempts to detect a more specific compiler from the Mach-O
*
* @param machHeader The {@link MachHeader}
* @return The detected compiler name, or {@code null} if one could be detected
* @return The detected compiler name, or {@code null} if one couldn't be detected
* @throws IOException if an IO-related error occurred
*/
private String detectCompilerName(MachHeader machHeader) throws IOException {

View File

@@ -13,6 +13,7 @@
<compiler name="default" spec="AARCH64.cspec" id="default"/>
<compiler name="Visual Studio" spec="AARCH64_win.cspec" id="windows"/>
<compiler name="golang" spec="AARCH64_golang.cspec" id="golang"/>
<compiler name="Swift" spec="AARCH64_swift.cspec" id="swift"/>
<external_name tool="gnu" name="aarch64"/>
<external_name tool="DWARF.register.mapping.file" name="AARCH64.dwarf"/>
<external_name tool="Golang.register.info.file" name="AARCH64_golang.register.info"/>

View File

@@ -5,6 +5,12 @@
<constraint loader="Executable and Linking Format (ELF)" compilerSpecID="default">
<constraint primary="183" processor="AARCH64" size="32" variant="ilp32" />
</constraint>
<constraint loader="Executable and Linking Format (ELF)" compilerSpecID="golang">
<constraint primary="183" secondary="golang" processor="AARCH64" endian="little" size="64" variant="v8A" />
</constraint>
<constraint loader="Executable and Linking Format (ELF)" compilerSpecID="swift">
<constraint primary="183" secondary="swift" processor="AARCH64" endian="little" size="64" variant="v8A" />
</constraint>
<constraint loader="Mac OS X Mach-O" compilerSpecID="default">
<constraint primary="16777228" processor="AARCH64" endian="little" size="64" variant="AppleSilicon" />
<constraint primary="33554444" processor="AARCH64" endian="little" size="32" variant="ilp32" />

View File

@@ -42,6 +42,12 @@
<constraint primary="3" processor="x86" endian="little" size="32" />
<constraint primary="62" processor="x86" endian="little" size="64" variant="default" />
</constraint>
<constraint compilerSpecID="swift">
<constraint primary="62" secondary="swift" processor="x86" endian="little" size="64" variant="default" />
</constraint>
<constraint compilerSpecID="golang">
<constraint primary="62" secondary="golang" processor="x86" endian="little" size="64" variant="default" />
</constraint>
</constraint>
<constraint loader="Module Definition (DEF)" compilerSpecID="windows">
<constraint primary="0" processor="x86" endian="little" size="32" />