49 #include <system_error>
51 using namespace llvm::object;
53 LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
55 : IRFile(std::move(Obj)), _target(TM) {}
57 LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
59 std::unique_ptr<LLVMContext> Context)
60 : OwnedContext(std::move(Context)), IRFile(std::move(Obj)), _target(TM) {}
79 BufferOrErr.
get()->getMemBufferRef());
95 std::string &errMsg) {
98 if (std::error_code EC = BufferOrErr.
getError()) {
99 errMsg = EC.message();
102 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
103 return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg,
109 std::string &errMsg) {
114 size_t map_size, off_t offset,
116 std::string &errMsg) {
119 if (std::error_code EC = BufferOrErr.
getError()) {
120 errMsg = EC.message();
123 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
124 return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg,
148 return makeLTOModule(Buffer, options, errMsg, Context);
154 std::string &ErrMsg) {
159 if (std::error_code EC = MBOrErr.
getError()) {
160 ErrMsg = EC.message();
164 std::function<void(const DiagnosticInfo &)> DiagnosticHandler =
177 return std::move(*M);
181 std::unique_ptr<MemoryBuffer> LightweightBuf =
185 DiagnosticHandler,
true );
188 return std::move(*M);
194 std::unique_ptr<LLVMContext> OwnedContext;
196 OwnedContext = llvm::make_unique<LLVMContext>();
197 Context = OwnedContext.get();
204 static_cast<bool>(OwnedContext), errMsg);
208 std::string TripleStr = M->getTargetTriple();
209 if (TripleStr.empty())
221 std::string FeatureStr = Features.
getString();
237 std::unique_ptr<object::IRObjectFile> IRObj(
242 Ret =
new LTOModule(std::move(IRObj), target, std::move(OwnedContext));
244 Ret =
new LTOModule(std::move(IRObj), target);
246 if (Ret->parseSymbols(errMsg)) {
251 Ret->parseMetadata();
257 std::unique_ptr<MemoryBuffer>
259 const char *startPtr = (
const char*)mem;
265 LTOModule::objcClassNameFromExpression(
const Constant *c, std::string &
name) {
266 if (
const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
269 Constant *cn = gvn->getInitializer();
271 if (ca->isCString()) {
272 name = (
".objc_class_name_" + ca->getAsCString()).str();
287 std::string superclassName;
288 if (objcClassNameFromExpression(c->
getOperand(1), superclassName)) {
290 _undefines.
insert(std::make_pair(superclassName, NameAndAttributes()));
291 if (IterBool.second) {
292 NameAndAttributes &
info = IterBool.first->second;
293 info.name = IterBool.first->first().data();
295 info.isFunction =
false;
301 std::string className;
302 if (objcClassNameFromExpression(c->
getOperand(2), className)) {
303 auto Iter = _defines.
insert(className).first;
305 NameAndAttributes
info;
306 info.name = Iter->first().data();
309 info.isFunction =
false;
311 _symbols.push_back(info);
321 std::string targetclassName;
322 if (!objcClassNameFromExpression(c->
getOperand(1), targetclassName))
326 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
328 if (!IterBool.second)
331 NameAndAttributes &info = IterBool.first->second;
332 info.name = IterBool.first->first().data();
334 info.isFunction =
false;
340 std::string targetclassName;
341 if (!objcClassNameFromExpression(clgv->
getInitializer(), targetclassName))
345 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
347 if (!IterBool.second)
350 NameAndAttributes &info = IterBool.first->second;
351 info.name = IterBool.first->first().data();
353 info.isFunction =
false;
365 addDefinedDataSymbol(Buffer.
c_str(), V);
368 void LTOModule::addDefinedDataSymbol(
const char *
Name,
const GlobalValue *v) {
370 addDefinedSymbol(Name, v,
false);
397 if (Section.compare(0, 15,
"__OBJC,__class,") == 0) {
404 else if (Section.compare(0, 18,
"__OBJC,__category,") == 0) {
411 else if (Section.compare(0, 18,
"__OBJC,__cls_refs,") == 0) {
427 addDefinedFunctionSymbol(Buffer.
c_str(),
F);
430 void LTOModule::addDefinedFunctionSymbol(
const char *Name,
const Function *F) {
432 addDefinedSymbol(Name, F,
true);
435 void LTOModule::addDefinedSymbol(
const char *Name,
const GlobalValue *def,
476 if (isa<GlobalAlias>(def))
479 auto Iter = _defines.
insert(Name).first;
482 NameAndAttributes
info;
484 info.name = NameRef.
data();
485 assert(info.name[NameRef.
size()] ==
'\0');
486 info.attributes = attr;
487 info.isFunction = isFunction;
491 _symbols.push_back(info);
496 void LTOModule::addAsmGlobalSymbol(
const char *name,
498 auto IterBool = _defines.
insert(name);
501 if (!IterBool.second)
504 NameAndAttributes &info = _undefines[IterBool.first->first().data()];
506 if (info.symbol ==
nullptr) {
516 info.name = IterBool.first->first().data();
519 info.isFunction =
false;
520 info.symbol =
nullptr;
523 _symbols.push_back(info);
528 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
530 addDefinedDataSymbol(info.name, info.symbol);
533 _symbols.back().attributes |= scope;
538 void LTOModule::addAsmGlobalSymbolUndef(
const char *name) {
539 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
541 _asm_undefines.push_back(IterBool.first->first().data());
544 if (!IterBool.second)
549 NameAndAttributes &info = IterBool.first->second;
550 info.name = IterBool.first->first().data();
551 info.attributes = attr;
552 info.isFunction =
false;
553 info.symbol =
nullptr;
565 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
568 if (!IterBool.second)
571 NameAndAttributes &info = IterBool.first->second;
573 info.name = IterBool.first->first().data();
582 info.isFunction = isFunc;
588 bool LTOModule::parseSymbols(std::string &errMsg) {
589 for (
auto &Sym : IRFile->symbols()) {
603 const char *Name = Buffer.
c_str();
606 addAsmGlobalSymbolUndef(Name);
616 addPotentialUndefinedSymbol(Sym, F !=
nullptr);
621 addDefinedFunctionSymbol(Sym);
625 if (isa<GlobalVariable>(GV)) {
626 addDefinedDataSymbol(Sym);
630 assert(isa<GlobalAlias>(GV));
631 addDefinedDataSymbol(Sym);
636 e = _undefines.
end(); u != e; ++u) {
639 if (_defines.
count(u->getKey()))
continue;
640 NameAndAttributes info = u->getValue();
641 _symbols.push_back(info);
648 void LTOModule::parseMetadata() {
653 MDNode *LinkerOptions = cast<MDNode>(Val);
654 for (
unsigned i = 0, e = LinkerOptions->
getNumOperands(); i != e; ++i) {
656 for (
unsigned ii = 0, ie = MDOptions->
getNumOperands(); ii != ie; ++ii) {
665 for (
const NameAndAttributes &Sym : _symbols) {
668 _target->getObjFileLowering()->emitLinkerFlagsForGlobal(OS, Sym.symbol,
MemoryBufferRef getMemBufferRef() const
std::error_code getError() const
static LTOModule * createInLocalContext(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path)
Represents either an error or a value T.
DataRefImpl getRawDataRefImpl() const
size_t size() const
size - Get the string size.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
bool canBeOmittedFromSymbolTable(const GlobalValue *GV)
unsigned getNumOperands() const
Return number of MDNode operands.
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...
const FeatureBitset Features
A raw_ostream that writes to an SmallVector or SmallString.
const char * getSection() const
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
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
const Module & getModule() const
ConstantExpr - a constant value that is initialized with an expression using other constant values...
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
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 ...
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
This is the base abstract class for diagnostic reporting in the backend.
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...
ConstantDataArray - An array constant whose element type is a simple 1/2/4/8-byte integer or float/do...
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...
ErrorOr< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the specified bitcode file, returning the module.
Value * getOperand(unsigned i) const
static std::unique_ptr< Module > parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldBeLazy, std::string &ErrMsg)
std::error_code printName(raw_ostream &OS) const
C++ class which implements the opaque lto_module_t type.
bool hasWeakLinkage() const
std::string getString() const
Features string accessors.
std::pair< typename base::iterator, bool > insert(StringRef Key)
Triple - Helper class for working with autoconf configuration names.
uint32_t getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
bool hasExternalWeakLinkage() const
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
Module.h This file contains the declarations for the Module class.
std::string getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the header of the specified bitcode buffer and extract just the triple information.
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"...
static LTOModule * createInContext(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path, LLVMContext *Context)
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...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Basic diagnostic printer that uses an underlying raw_ostream.
bool hasLinkOnceLinkage() const
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.
This is a value type class that represents a single symbol in the list of symbols in the object file...
bool hasProtectedVisibility() const
static LTOModule * createFromFile(const char *path, TargetOptions options, std::string &errMsg)
Create an LTOModule.
static LTOModule * createFromOpenFileSlice(int fd, const char *path, size_t map_size, off_t offset, TargetOptions options, std::string &errMsg)
bool hasLocalLinkage() const
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
static LTOModule * createFromBuffer(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path="")
unsigned getAlignment() const
A raw_ostream that writes to an std::string.
ErrorOr< std::unique_ptr< Module > > getLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr, bool ShouldLazyLoadMetadata=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
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< 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.
LLVMContext & getGlobalContext()
getGlobalContext - Returns a global context.
This file describes how to lower LLVM code to machine code.
static LTOModule * createFromOpenFile(int fd, const char *path, size_t size, TargetOptions options, std::string &errMsg)