use of com.oracle.truffle.llvm.parser.scanner.BitStream in project sulong by graalvm.
the class MDString method createFromBlob.
public static MDString[] createFromBlob(long[] args) {
// the number of strings in the attached blob
final long count = args[STRINGS_ARGINDEX_COUNT];
final MDString[] strings = new MDString[Math.toIntExact(count)];
final BitStream blob = BitStream.createFromBlob(args, STRINGS_ARGOFFSET_BLOB);
long strOffset = args[STRINGS_ARGINDEX_STROFFSET] * Byte.SIZE;
long lenOffset = 0;
for (int i = 0; i < count; i++) {
long size = blob.readVBR(lenOffset, STRINGS_SIZE_WIDTH);
lenOffset += BitStream.widthVBR(size, STRINGS_SIZE_WIDTH);
final byte[] bytes = new byte[(int) (size)];
int j = 0;
while (size > 0) {
bytes[j++] = (byte) blob.read(strOffset, Byte.SIZE);
size--;
strOffset += Byte.SIZE;
}
strings[i] = new MDString(new String(bytes));
}
return strings;
}
use of com.oracle.truffle.llvm.parser.scanner.BitStream 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;
}
}
use of com.oracle.truffle.llvm.parser.scanner.BitStream in project graal by oracle.
the class MDString method createFromBlob.
public static MDString[] createFromBlob(long[] args) {
// the number of strings in the attached blob
final long count = args[STRINGS_ARGINDEX_COUNT];
final MDString[] strings = new MDString[Math.toIntExact(count)];
final BitStream blob = BitStream.createFromBlob(args, STRINGS_ARGOFFSET_BLOB);
long strOffset = args[STRINGS_ARGINDEX_STROFFSET] * Byte.SIZE;
long lenOffset = 0;
for (int i = 0; i < count; i++) {
long size = blob.readVBR(lenOffset, STRINGS_SIZE_WIDTH);
lenOffset += BitStream.widthVBR(size, STRINGS_SIZE_WIDTH);
final byte[] bytes = new byte[(int) (size)];
int j = 0;
while (size > 0) {
bytes[j++] = (byte) blob.read(strOffset, Byte.SIZE);
size--;
strOffset += Byte.SIZE;
}
strings[i] = new MDString(new String(bytes));
}
return strings;
}
Aggregations