1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Part of the IRObjectFile class implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/IRObjectFile.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/PointerUnion.h" 16 #include "llvm/BinaryFormat/Magic.h" 17 #include "llvm/Bitcode/BitcodeReader.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/Object/ObjectFile.h" 20 using namespace llvm; 21 using namespace object; 22 23 namespace llvm { 24 class LLVMContext; 25 class raw_ostream; 26 } // namespace llvm 27 28 IRObjectFile::IRObjectFile(MemoryBufferRef Object, 29 std::vector<std::unique_ptr<Module>> Mods) 30 : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) { 31 for (auto &M : this->Mods) 32 SymTab.addModule(M.get()); 33 } 34 35 IRObjectFile::~IRObjectFile() = default; 36 37 static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) { 38 return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p); 39 } 40 41 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 42 Symb.p += sizeof(ModuleSymbolTable::Symbol); 43 } 44 45 Error IRObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const { 46 SymTab.printSymbolName(OS, getSym(Symb)); 47 return Error::success(); 48 } 49 50 Expected<uint32_t> IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 51 return SymTab.getSymbolFlags(getSym(Symb)); 52 } 53 54 basic_symbol_iterator IRObjectFile::symbol_begin() const { 55 DataRefImpl Ret; 56 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data()); 57 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 58 } 59 60 basic_symbol_iterator IRObjectFile::symbol_end() const { 61 DataRefImpl Ret; 62 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() + 63 SymTab.symbols().size()); 64 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 65 } 66 67 StringRef IRObjectFile::getTargetTriple() const { 68 // Each module must have the same target triple, so we arbitrarily access the 69 // first one. 70 return Mods[0]->getTargetTriple(); 71 } 72 73 Expected<MemoryBufferRef> 74 IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) { 75 for (const SectionRef &Sec : Obj.sections()) { 76 if (Sec.isBitcode()) { 77 Expected<StringRef> Contents = Sec.getContents(); 78 if (!Contents) 79 return Contents.takeError(); 80 if (Contents->size() <= 1) 81 return errorCodeToError(object_error::bitcode_section_not_found); 82 return MemoryBufferRef(*Contents, Obj.getFileName()); 83 } 84 } 85 86 return errorCodeToError(object_error::bitcode_section_not_found); 87 } 88 89 Expected<MemoryBufferRef> 90 IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) { 91 file_magic Type = identify_magic(Object.getBuffer()); 92 switch (Type) { 93 case file_magic::bitcode: 94 return Object; 95 case file_magic::elf_relocatable: 96 case file_magic::macho_object: 97 case file_magic::wasm_object: 98 case file_magic::coff_object: { 99 Expected<std::unique_ptr<ObjectFile>> ObjFile = 100 ObjectFile::createObjectFile(Object, Type); 101 if (!ObjFile) 102 return ObjFile.takeError(); 103 return findBitcodeInObject(*ObjFile->get()); 104 } 105 default: 106 return errorCodeToError(object_error::invalid_file_type); 107 } 108 } 109 110 Expected<std::unique_ptr<IRObjectFile>> 111 IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) { 112 Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object); 113 if (!BCOrErr) 114 return BCOrErr.takeError(); 115 116 Expected<std::vector<BitcodeModule>> BMsOrErr = 117 getBitcodeModuleList(*BCOrErr); 118 if (!BMsOrErr) 119 return BMsOrErr.takeError(); 120 121 std::vector<std::unique_ptr<Module>> Mods; 122 for (auto BM : *BMsOrErr) { 123 Expected<std::unique_ptr<Module>> MOrErr = 124 BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true, 125 /*IsImporting*/ false); 126 if (!MOrErr) 127 return MOrErr.takeError(); 128 129 Mods.push_back(std::move(*MOrErr)); 130 } 131 132 return std::unique_ptr<IRObjectFile>( 133 new IRObjectFile(*BCOrErr, std::move(Mods))); 134 } 135 136 Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) { 137 IRSymtabFile F; 138 Expected<MemoryBufferRef> BCOrErr = 139 IRObjectFile::findBitcodeInMemBuffer(MBRef); 140 if (!BCOrErr) 141 return BCOrErr.takeError(); 142 143 Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr); 144 if (!BFCOrErr) 145 return BFCOrErr.takeError(); 146 147 Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr); 148 if (!FCOrErr) 149 return FCOrErr.takeError(); 150 151 F.Mods = std::move(BFCOrErr->Mods); 152 F.Symtab = std::move(FCOrErr->Symtab); 153 F.Strtab = std::move(FCOrErr->Strtab); 154 F.TheReader = std::move(FCOrErr->TheReader); 155 return std::move(F); 156 } 157