File indexing completed on 2026-05-10 08:44:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_LTO_LEGACY_LTOMODULE_H
0014 #define LLVM_LTO_LEGACY_LTOMODULE_H
0015
0016 #include "llvm-c/lto.h"
0017 #include "llvm/ADT/StringMap.h"
0018 #include "llvm/ADT/StringSet.h"
0019 #include "llvm/IR/Module.h"
0020 #include "llvm/LTO/LTO.h"
0021 #include "llvm/Object/IRObjectFile.h"
0022 #include "llvm/Object/ModuleSymbolTable.h"
0023 #include "llvm/Target/TargetMachine.h"
0024 #include <string>
0025 #include <vector>
0026
0027
0028 namespace llvm {
0029 class Function;
0030 class GlobalValue;
0031 class MemoryBuffer;
0032 class TargetOptions;
0033 class Value;
0034
0035
0036
0037
0038 struct LTOModule {
0039 private:
0040 struct NameAndAttributes {
0041 StringRef name;
0042 uint32_t attributes = 0;
0043 bool isFunction = false;
0044 const GlobalValue *symbol = nullptr;
0045 };
0046
0047 std::unique_ptr<LLVMContext> OwnedContext;
0048
0049 std::string LinkerOpts;
0050
0051 std::unique_ptr<Module> Mod;
0052 MemoryBufferRef MBRef;
0053 ModuleSymbolTable SymTab;
0054 std::unique_ptr<TargetMachine> _target;
0055 std::vector<NameAndAttributes> _symbols;
0056
0057
0058 StringSet<> _defines;
0059 StringMap<NameAndAttributes> _undefines;
0060 std::vector<StringRef> _asm_undefines;
0061
0062 LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
0063 TargetMachine *TM);
0064
0065 public:
0066 ~LTOModule();
0067
0068
0069 static bool isBitcodeFile(const void *mem, size_t length);
0070 static bool isBitcodeFile(StringRef path);
0071
0072
0073 bool isThinLTO();
0074
0075
0076
0077 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
0078 StringRef triplePrefix);
0079
0080
0081
0082
0083 static std::string getProducerString(MemoryBuffer *Buffer);
0084
0085
0086 static std::unique_ptr<MemoryBuffer>
0087 makeBuffer(const void *mem, size_t length, StringRef name = "");
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 static ErrorOr<std::unique_ptr<LTOModule>>
0098 createFromFile(LLVMContext &Context, StringRef path,
0099 const TargetOptions &options);
0100 static ErrorOr<std::unique_ptr<LTOModule>>
0101 createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
0102 const TargetOptions &options);
0103 static ErrorOr<std::unique_ptr<LTOModule>>
0104 createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
0105 size_t map_size, off_t offset,
0106 const TargetOptions &options);
0107 static ErrorOr<std::unique_ptr<LTOModule>>
0108 createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
0109 const TargetOptions &options, StringRef path = "");
0110 static ErrorOr<std::unique_ptr<LTOModule>>
0111 createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
0112 size_t length, const TargetOptions &options,
0113 StringRef path);
0114
0115 const Module &getModule() const { return *Mod; }
0116 Module &getModule() { return *Mod; }
0117
0118 std::unique_ptr<Module> takeModule() { return std::move(Mod); }
0119
0120
0121 const std::string &getTargetTriple() {
0122 return getModule().getTargetTriple();
0123 }
0124
0125
0126 void setTargetTriple(StringRef Triple) {
0127 getModule().setTargetTriple(Triple);
0128 }
0129
0130
0131 uint32_t getSymbolCount() {
0132 return _symbols.size();
0133 }
0134
0135
0136 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
0137 if (index < _symbols.size())
0138 return lto_symbol_attributes(_symbols[index].attributes);
0139 return lto_symbol_attributes(0);
0140 }
0141
0142
0143 StringRef getSymbolName(uint32_t index) {
0144 if (index < _symbols.size())
0145 return _symbols[index].name;
0146 return StringRef();
0147 }
0148
0149 const GlobalValue *getSymbolGV(uint32_t index) {
0150 if (index < _symbols.size())
0151 return _symbols[index].symbol;
0152 return nullptr;
0153 }
0154
0155 StringRef getLinkerOpts() { return LinkerOpts; }
0156
0157 const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
0158
0159 static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
0160 const char *path, std::string &out_error);
0161
0162 static size_t getDependentLibraryCount(lto::InputFile *input);
0163
0164 static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
0165
0166 Expected<uint32_t> getMachOCPUType() const;
0167
0168 Expected<uint32_t> getMachOCPUSubType() const;
0169
0170
0171
0172 bool hasCtorDtor() const;
0173
0174 private:
0175
0176
0177
0178 void parseMetadata();
0179
0180
0181
0182 void parseSymbols();
0183
0184
0185 void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
0186 bool isFunc);
0187
0188
0189 void addDefinedSymbol(StringRef Name, const GlobalValue *def,
0190 bool isFunction);
0191
0192
0193 void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
0194 void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
0195
0196
0197 void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
0198 void addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F);
0199
0200
0201 void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
0202
0203
0204 void addAsmGlobalSymbolUndef(StringRef);
0205
0206
0207 void addObjCClass(const GlobalVariable *clgv);
0208
0209
0210 void addObjCCategory(const GlobalVariable *clgv);
0211
0212
0213 void addObjCClassRef(const GlobalVariable *clgv);
0214
0215
0216 bool objcClassNameFromExpression(const Constant *c, std::string &name);
0217
0218
0219 static ErrorOr<std::unique_ptr<LTOModule>>
0220 makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
0221 LLVMContext &Context, bool ShouldBeLazy);
0222 };
0223 }
0224 #endif