Search in sources :

Example 1 with CoffFile

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;
    }
}
Also used : BitStream(com.oracle.truffle.llvm.parser.scanner.BitStream) PEFile(com.oracle.truffle.llvm.parser.coff.PEFile) ElfFile(com.oracle.truffle.llvm.parser.elf.ElfFile) ElfDynamicSection(com.oracle.truffle.llvm.parser.elf.ElfDynamicSection) Magic(com.oracle.truffle.llvm.runtime.Magic) CoffFile(com.oracle.truffle.llvm.parser.coff.CoffFile) Xar(com.oracle.truffle.llvm.parser.macho.Xar) ImageSectionHeader(com.oracle.truffle.llvm.parser.coff.CoffFile.ImageSectionHeader) MachOLibraryLocator(com.oracle.truffle.llvm.parser.macho.MachOLibraryLocator) MachOFile(com.oracle.truffle.llvm.parser.macho.MachOFile) ElfLibraryLocator(com.oracle.truffle.llvm.parser.elf.ElfLibraryLocator) ElfSectionHeaderTable(com.oracle.truffle.llvm.parser.elf.ElfSectionHeaderTable) ByteSequence(org.graalvm.polyglot.io.ByteSequence)

Aggregations

CoffFile (com.oracle.truffle.llvm.parser.coff.CoffFile)1 ImageSectionHeader (com.oracle.truffle.llvm.parser.coff.CoffFile.ImageSectionHeader)1 PEFile (com.oracle.truffle.llvm.parser.coff.PEFile)1 ElfDynamicSection (com.oracle.truffle.llvm.parser.elf.ElfDynamicSection)1 ElfFile (com.oracle.truffle.llvm.parser.elf.ElfFile)1 ElfLibraryLocator (com.oracle.truffle.llvm.parser.elf.ElfLibraryLocator)1 ElfSectionHeaderTable (com.oracle.truffle.llvm.parser.elf.ElfSectionHeaderTable)1 MachOFile (com.oracle.truffle.llvm.parser.macho.MachOFile)1 MachOLibraryLocator (com.oracle.truffle.llvm.parser.macho.MachOLibraryLocator)1 Xar (com.oracle.truffle.llvm.parser.macho.Xar)1 BitStream (com.oracle.truffle.llvm.parser.scanner.BitStream)1 Magic (com.oracle.truffle.llvm.runtime.Magic)1 ByteSequence (org.graalvm.polyglot.io.ByteSequence)1