use of com.oracle.truffle.llvm.parser.coff.CoffFile in project graal by oracle.
the class BinaryParser method parseBitcode.
private ByteSequence parseBitcode(ByteSequence bytes, Source source) {
BitStream b = BitStream.create(bytes);
Magic magicWord = getMagic(b);
if (source != null) {
libraryName = source.getName();
}
switch(magicWord) {
case BC_MAGIC_WORD:
return bytes;
case WRAPPER_MAGIC_WORD:
// 0: magic word
// 32: version
// 64: offset32
long offset = b.read(64, Integer.SIZE);
// 96: size32
long size = b.read(96, Integer.SIZE);
return bytes.subSequence((int) offset, (int) (offset + size));
case ELF_MAGIC_WORD:
ElfFile elfFile = ElfFile.create(bytes);
ElfSectionHeaderTable.Entry llvmbc = elfFile.getSectionHeaderTable().getEntry(".llvmbc");
if (llvmbc == null) {
// ELF File does not contain an .llvmbc section
return null;
}
ElfDynamicSection dynamicSection = elfFile.getDynamicSection();
if (dynamicSection != null) {
List<String> elfLibraries = dynamicSection.getDTNeeded();
libraries.addAll(elfLibraries);
String soName = dynamicSection.getDTSOName();
if (soName != null) {
libraryName = soName;
}
locator = new ElfLibraryLocator(elfFile, source);
}
long elfOffset = llvmbc.getOffset();
long elfSize = llvmbc.getSize();
return bytes.subSequence((int) elfOffset, (int) (elfOffset + elfSize));
case MH_MAGIC:
case MH_CIGAM:
case MH_MAGIC_64:
case MH_CIGAM_64:
MachOFile machOFile = MachOFile.create(bytes);
String origin = getOrigin(source);
List<String> machoLibraries = machOFile.getDyLibs(origin);
locator = new MachOLibraryLocator(machOFile, source);
libraries.addAll(machoLibraries);
ByteSequence machoBitcode = machOFile.extractBitcode();
if (machoBitcode == null) {
return null;
}
return parseBitcode(machoBitcode, source);
case XAR_MAGIC:
Xar xarFile = Xar.create(bytes);
ByteSequence xarBitcode = xarFile.extractBitcode();
if (xarBitcode == null) {
return null;
}
return parseBitcode(xarBitcode, source);
case COFF_INTEL_AMD64:
CoffFile coffFile = CoffFile.create(bytes);
ImageSectionHeader coffBcSection = coffFile.getSection(".llvmbc");
if (coffBcSection == null) {
// COFF File does not contain an .llvmbc section
return null;
}
return parseBitcode(coffBcSection.getData(), source);
case MS_DOS:
PEFile peFile = PEFile.create(bytes);
ImageSectionHeader peBcSection = peFile.getCoffFile().getSection(".llvmbc");
if (peBcSection == null) {
// PE/COFF File does not contain an .llvmbc section
return null;
}
return parseBitcode(peBcSection.getData(), source);
default:
return null;
}
}
Aggregations