47 #include <system_error>
49 using namespace llvm::object;
53 : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
54 SymTab.addModule(Mod.get());
74 BufferOrErr.
get()->getMemBufferRef());
113 return *ProducerOrErr;
121 if (std::error_code EC = BufferOrErr.
getError()) {
125 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
126 return makeLTOModule(Buffer->getMemBufferRef(), options,
Context,
138 size_t map_size, off_t offset,
142 if (std::error_code EC = BufferOrErr.
getError()) {
146 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
147 return makeLTOModule(Buffer->getMemBufferRef(), options,
Context,
155 StringRef Data((
const char *)mem, length);
157 return makeLTOModule(Buffer, options, Context,
false);
162 const void *mem,
size_t length,
164 StringRef Data((
const char *)mem, length);
169 makeLTOModule(Buffer, options, *Context,
true);
171 (*Ret)->OwnedContext = std::move(Context);
182 if (std::error_code EC = MBOrErr.
getError()) {
204 if (std::error_code EC = MOrErr.
getError())
206 std::unique_ptr<Module> &M = *MOrErr;
208 std::string TripleStr = M->getTargetTriple();
209 if (TripleStr.empty())
217 return std::unique_ptr<LTOModule>(
nullptr);
221 Features.getDefaultSubtargetFeatures(
Triple);
222 std::string FeatureStr = Features.getString();
237 std::unique_ptr<LTOModule>
Ret(
new LTOModule(std::move(M), Buffer, target));
239 Ret->parseMetadata();
241 return std::move(
Ret);
245 std::unique_ptr<MemoryBuffer>
247 const char *startPtr = (
const char*)mem;
253 LTOModule::objcClassNameFromExpression(
const Constant *c, std::string &
name) {
254 if (
const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
257 Constant *cn = gvn->getInitializer();
259 if (ca->isCString()) {
260 name = (
".objc_class_name_" + ca->getAsCString()).str();
275 std::string superclassName;
276 if (objcClassNameFromExpression(c->
getOperand(1), superclassName)) {
278 _undefines.
insert(std::make_pair(superclassName, NameAndAttributes()));
279 if (IterBool.second) {
280 NameAndAttributes &
info = IterBool.first->second;
281 info.name = IterBool.first->first();
283 info.isFunction =
false;
289 std::string className;
290 if (objcClassNameFromExpression(c->
getOperand(2), className)) {
291 auto Iter = _defines.
insert(className).first;
293 NameAndAttributes
info;
294 info.name = Iter->first();
297 info.isFunction =
false;
299 _symbols.push_back(info);
309 std::string targetclassName;
310 if (!objcClassNameFromExpression(c->
getOperand(1), targetclassName))
314 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
316 if (!IterBool.second)
319 NameAndAttributes &info = IterBool.first->second;
320 info.name = IterBool.first->first();
322 info.isFunction =
false;
328 std::string targetclassName;
329 if (!objcClassNameFromExpression(clgv->
getInitializer(), targetclassName))
333 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
335 if (!IterBool.second)
338 NameAndAttributes &info = IterBool.first->second;
339 info.name = IterBool.first->first();
341 info.isFunction =
false;
354 addDefinedDataSymbol(Buffer, V);
359 addDefinedSymbol(Name, v,
false);
386 if (Section.compare(0, 15,
"__OBJC,__class,") == 0) {
393 else if (Section.compare(0, 18,
"__OBJC,__category,") == 0) {
400 else if (Section.compare(0, 18,
"__OBJC,__cls_refs,") == 0) {
416 addDefinedFunctionSymbol(Buffer,
F);
421 addDefinedSymbol(Name, F,
true);
465 if (isa<GlobalAlias>(def))
468 auto Iter = _defines.
insert(Name).first;
471 NameAndAttributes
info;
475 info.attributes = attr;
476 info.isFunction = isFunction;
480 _symbols.push_back(info);
485 void LTOModule::addAsmGlobalSymbol(
StringRef name,
487 auto IterBool = _defines.
insert(name);
490 if (!IterBool.second)
493 NameAndAttributes &info = _undefines[IterBool.first->first()];
495 if (info.symbol ==
nullptr) {
505 info.name = IterBool.first->first();
508 info.isFunction =
false;
509 info.symbol =
nullptr;
512 _symbols.push_back(info);
517 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
519 addDefinedDataSymbol(info.name, info.symbol);
522 _symbols.back().attributes |= scope;
527 void LTOModule::addAsmGlobalSymbolUndef(
StringRef name) {
528 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
530 _asm_undefines.push_back(IterBool.first->first());
533 if (!IterBool.second)
538 NameAndAttributes &info = IterBool.first->second;
539 info.name = IterBool.first->first();
540 info.attributes = attr;
541 info.isFunction =
false;
542 info.symbol =
nullptr;
555 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
558 if (!IterBool.second)
561 NameAndAttributes &info = IterBool.first->second;
563 info.name = IterBool.first->first();
572 info.isFunction = isFunc;
576 void LTOModule::parseSymbols() {
577 for (
auto Sym : SymTab.
symbols()) {
595 addAsmGlobalSymbolUndef(Name);
605 addPotentialUndefinedSymbol(Sym,
F !=
nullptr);
610 addDefinedFunctionSymbol(Sym);
614 if (isa<GlobalVariable>(GV)) {
615 addDefinedDataSymbol(Sym);
619 assert(isa<GlobalAlias>(GV));
620 addDefinedDataSymbol(Sym);
625 e = _undefines.
end(); u != e; ++u) {
628 if (_defines.
count(u->getKey()))
continue;
629 NameAndAttributes info = u->getValue();
630 _symbols.push_back(info);
635 void LTOModule::parseMetadata() {
640 MDNode *LinkerOptions = cast<MDNode>(Val);
643 for (
unsigned ii = 0, ie = MDOptions->
getNumOperands(); ii != ie; ++ii) {
651 for (
const NameAndAttributes &Sym : _symbols) {
654 _target->getObjFileLowering()->emitLinkerFlagsForGlobal(OS, Sym.symbol);
MemoryBufferRef getMemBufferRef() const
std::error_code getError() const
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Expected< std::unique_ptr< Module > > getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
bool canBeOmittedFromSymbolTable(const GlobalValue *GV)
unsigned getNumOperands() const
Return number of MDNode operands.
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
bool isThinLTO()
Returns 'true' if the Module is produced for ThinLTO.
A raw_ostream that writes to an SmallVector or SmallString.
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, size_t map_size, off_t offset, const TargetOptions &options)
static std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Error takeError()
Take ownership of the stored error.
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasCommonLinkage() const
struct fuzzer::@269 Flags
Tagged union holding either a T or a Error.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
const Module & getModule() const
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
A constant value that is initialized with an expression using other constant values.
static ErrorOr< std::unique_ptr< Module > > parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldBeLazy)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
static ErrorOr< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Expected< std::string > getBitcodeProducerString(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the producer string information...
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
This is an important class for using LLVM in a threaded context.
This is an important base class in LLVM.
bool hasHiddenVisibility() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
Value * getOperand(unsigned i) const
C++ class which implements the opaque lto_module_t type.
bool hasWeakLinkage() const
std::pair< typename base::iterator, bool > insert(StringRef Key)
Triple - Helper class for working with autoconf configuration names.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
bool hasExternalWeakLinkage() const
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
T get() const
Returns the value of the specified pointer type.
Module.h This file contains the declarations for the Module class.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Target - Wrapper for Target specific information.
SubtargetFeatures - Manages the enabling and disabling of subtarget specific features.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
uint32_t getSymbolFlags(Symbol S) const
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
bool hasLinkOnceLinkage() const
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
static std::unique_ptr< MemoryBuffer > makeBuffer(const void *mem, size_t length, StringRef name="")
Create a MemoryBuffer from a memory range with an optional name.
ArrayRef< Symbol > symbols() const
bool hasProtectedVisibility() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool hasLocalLinkage() const
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
void printSymbolName(raw_ostream &OS, Symbol S) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getAlignment() const
A raw_ostream that writes to an std::string.
const FeatureBitset Features
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
static ErrorOr< std::unique_ptr< LTOModule > > createInLocalContext(std::unique_ptr< LLVMContext > Context, const void *mem, size_t length, const TargetOptions &options, StringRef path)
Expected< bool > hasGlobalValueSummary(MemoryBufferRef Buffer)
Check if the given bitcode buffer contains a summary block.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
This file describes how to lower LLVM code to machine code.
StringRef getSection() const
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")