本文整理汇总了C++中MemoryBufferRef类的典型用法代码示例。如果您正苦于以下问题:C++ MemoryBufferRef类的具体用法?C++ MemoryBufferRef怎么用?C++ MemoryBufferRef使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemoryBufferRef类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert
void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
const object::Archive::Symbol Sym) {
Symbol *S;
bool WasInserted;
std::tie(S, WasInserted) = insert(Sym.getName());
if (WasInserted) {
replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
return;
}
if (!S->body()->isUndefined())
return;
// Weak undefined symbols should not fetch members from archives. If we were
// to keep old symbol we would not know that an archive member was available
// if a strong undefined symbol shows up afterwards in the link. If a strong
// undefined symbol never shows up, this lazy symbol will get to the end of
// the link and must be treated as the weak undefined one. We already marked
// this symbol as used when we added it to the symbol table, but we also need
// to preserve its type. FIXME: Move the Type field to Symbol.
if (S->isWeak()) {
replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
return;
}
MemoryBufferRef MBRef = F->getMember(&Sym);
if (!MBRef.getBuffer().empty())
addFile(createObjectFile(MBRef, F->getName()));
}
示例2: createELFObj
template <class ELFT> static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
std::error_code EC;
ELFFile<ELFT> F(MB.getBuffer(), EC);
if (EC)
fatal(EC, "failed to read " + MB.getBufferIdentifier());
return F;
}
示例3: if
ErrorOr<std::unique_ptr<ObjectFile>>
ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
std::pair<unsigned char, unsigned char> Ident =
getElfArchType(Obj.getBuffer());
std::size_t MaxAlignment =
1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
if (MaxAlignment < 2)
return object_error::parse_failed;
std::error_code EC;
std::unique_ptr<ObjectFile> R;
if (Ident.first == ELF::ELFCLASS32) {
if (Ident.second == ELF::ELFDATA2LSB)
R.reset(new ELFObjectFile<ELFType<support::little, false>>(Obj, EC));
else if (Ident.second == ELF::ELFDATA2MSB)
R.reset(new ELFObjectFile<ELFType<support::big, false>>(Obj, EC));
else
return object_error::parse_failed;
} else if (Ident.first == ELF::ELFCLASS64) {
if (Ident.second == ELF::ELFDATA2LSB)
R.reset(new ELFObjectFile<ELFType<support::little, true>>(Obj, EC));
else if (Ident.second == ELF::ELFDATA2MSB)
R.reset(new ELFObjectFile<ELFType<support::big, true>>(Obj, EC));
else
return object_error::parse_failed;
} else {
return object_error::parse_failed;
}
if (EC)
return EC;
return std::move(R);
}
示例4: parseFileHeaders
Error PDBFile::parseFileHeaders() {
std::error_code EC;
MemoryBufferRef BufferRef = *Context->Buffer;
// Make sure the file is sufficiently large to hold a super block.
// Do this before attempting to read the super block.
if (BufferRef.getBufferSize() < sizeof(SuperBlock))
return make_error<RawError>(raw_error_code::corrupt_file,
"Does not contain superblock");
Context->SB =
reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
const SuperBlock *SB = Context->SB;
// Check the magic bytes.
if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"MSF magic header doesn't match");
// We don't support blocksizes which aren't a multiple of four bytes.
if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"Block size is not multiple of 4.");
switch (SB->BlockSize) {
case 512: case 1024: case 2048: case 4096:
break;
default:
// An invalid block size suggests a corrupt PDB file.
return make_error<RawError>(raw_error_code::corrupt_file,
"Unsupported block size.");
}
if (BufferRef.getBufferSize() % SB->BlockSize != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"File size is not a multiple of block size");
// We don't support directories whose sizes aren't a multiple of four bytes.
if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"Directory size is not multiple of 4.");
// The number of blocks which comprise the directory is a simple function of
// the number of bytes it contains.
uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
// The block map, as we understand it, is a block which consists of a list of
// block numbers.
// It is unclear what would happen if the number of blocks couldn't fit on a
// single block.
if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
return make_error<RawError>(raw_error_code::corrupt_file,
"Too many directory blocks.");
// Make sure the directory block array fits within the file.
if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
return EC;
return Error::success();
}
示例5: createObjectFile
std::unique_ptr<InputFile> LazyArchive::getFile() {
MemoryBufferRef MBRef = File->getMember(&Sym);
// getMember returns an empty buffer if the member was already
// read from the library.
if (MBRef.getBuffer().empty())
return std::unique_ptr<InputFile>(nullptr);
return createObjectFile(MBRef, File->getName());
}
示例6: checkOffset
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) {
return std::make_error_code(std::errc::bad_address);
}
return std::error_code();
}
示例7: checkOffset
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) {
return object_error::unexpected_eof;
}
return std::error_code();
}
示例8: checkOffset
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) {
return make_error<RawError>(raw_error_code::corrupt_file,
"Invalid buffer address");
}
return Error::success();
}
示例9: switch
ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
StringRef Data = Object.getBuffer();
if (Type == sys::fs::file_magic::unknown)
Type = sys::fs::identify_magic(Data);
switch (Type) {
case sys::fs::file_magic::bitcode:
if (Context)
return IRObjectFile::create(Object, *Context);
// Fallthrough
case sys::fs::file_magic::unknown:
case sys::fs::file_magic::archive:
case sys::fs::file_magic::macho_universal_binary:
case sys::fs::file_magic::windows_resource:
return object_error::invalid_file_type;
case sys::fs::file_magic::elf:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
case sys::fs::file_magic::elf_core:
case sys::fs::file_magic::macho_executable:
case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
case sys::fs::file_magic::macho_core:
case sys::fs::file_magic::macho_preload_executable:
case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
case sys::fs::file_magic::macho_dynamic_linker:
case sys::fs::file_magic::macho_bundle:
case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
case sys::fs::file_magic::macho_dsym_companion:
case sys::fs::file_magic::macho_kext_bundle:
case sys::fs::file_magic::pecoff_executable:
return ObjectFile::createObjectFile(Object, Type);
case sys::fs::file_magic::coff_import_library:
return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::coff_object: {
ErrorOr<std::unique_ptr<ObjectFile>> Obj =
ObjectFile::createObjectFile(Object, Type);
if (!Obj || !Context)
return std::move(Obj);
ErrorOr<MemoryBufferRef> BCData =
IRObjectFile::findBitcodeInObject(*Obj->get());
if (!BCData)
return std::move(Obj);
return IRObjectFile::create(
MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
*Context);
}
}
llvm_unreachable("Unexpected Binary File Type");
}
示例10: createFile
static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
// File type is detected by contents, not by file extension.
file_magic Magic = identify_magic(MB.getBuffer());
if (Magic == file_magic::archive)
return std::unique_ptr<InputFile>(new ArchiveFile(MB));
if (Magic == file_magic::bitcode)
return std::unique_ptr<InputFile>(new BitcodeFile(MB));
if (Config->OutputFile == "")
Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
return std::unique_ptr<InputFile>(new ObjectFile<llvm::object::ELF64LE>(MB));
}
示例11: notifyObjectCompiled
void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
const std::string &ModuleID = M->getModuleIdentifier();
std::string CacheName;
if (!getCacheFilename(ModuleID, CacheName))
return;
if (!CacheDir.empty()) { // Create user-defined cache dir.
SmallString<128> dir(sys::path::parent_path(CacheName));
sys::fs::create_directories(Twine(dir));
}
std::error_code EC;
raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
outfile.close();
}
示例12: getBitcodeMachineKind
static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Triple T(getBitcodeTargetTriple(MB, Driver->Context));
switch (T.getArch()) {
case Triple::aarch64:
return EM_AARCH64;
case Triple::arm:
return EM_ARM;
case Triple::mips:
case Triple::mipsel:
case Triple::mips64:
case Triple::mips64el:
return EM_MIPS;
case Triple::ppc:
return EM_PPC;
case Triple::ppc64:
return EM_PPC64;
case Triple::x86:
return T.isOSIAMCU() ? EM_IAMCU : EM_386;
case Triple::x86_64:
return EM_X86_64;
default:
fatal(MB.getBufferIdentifier() +
": could not infer e_machine from bitcode target triple " + T.str());
}
}
示例13: log
Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
log(Path);
auto MBOrErr = MemoryBuffer::getFile(Path);
if (auto EC = MBOrErr.getError()) {
error("cannot open " + Path + ": " + EC.message());
return None;
}
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
MemoryBufferRef MBRef = MB->getMemBufferRef();
make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
if (Tar)
Tar->append(relativeToRoot(Path), MBRef.getBuffer());
return MBRef;
}
示例14: parseAssemblyInto
bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
SourceMgr SM;
std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false);
SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
return LLParser(F.getBuffer(), SM, Err, &M).Run();
}
示例15: checkSize
// Returns false if size is greater than the buffer size. And sets ec.
static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
if (M.getBufferSize() < Size) {
EC = object_error::unexpected_eof;
return false;
}
return true;
}