Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- IRObjectFile.h - LLVM IR object file implementation ------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares the IRObjectFile template class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_OBJECT_IROBJECTFILE_H |
| 14 | #define LLVM_OBJECT_IROBJECTFILE_H |
| 15 | |
| 16 | #include "llvm/ADT/PointerUnion.h" |
| 17 | #include "llvm/Object/IRSymtab.h" |
| 18 | #include "llvm/Object/ModuleSymbolTable.h" |
| 19 | #include "llvm/Object/SymbolicFile.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | class BitcodeModule; |
| 23 | class Mangler; |
| 24 | class Module; |
| 25 | class GlobalValue; |
| 26 | class Triple; |
| 27 | |
| 28 | namespace object { |
| 29 | class ObjectFile; |
| 30 | |
| 31 | class IRObjectFile : public SymbolicFile { |
| 32 | std::vector<std::unique_ptr<Module>> Mods; |
| 33 | ModuleSymbolTable SymTab; |
| 34 | IRObjectFile(MemoryBufferRef Object, |
| 35 | std::vector<std::unique_ptr<Module>> Mods); |
| 36 | |
| 37 | public: |
| 38 | ~IRObjectFile() override; |
| 39 | void moveSymbolNext(DataRefImpl &Symb) const override; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 40 | Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | basic_symbol_iterator symbol_begin() const override; |
| 43 | basic_symbol_iterator symbol_end() const override; |
| 44 | |
| 45 | StringRef getTargetTriple() const; |
| 46 | |
| 47 | static bool classof(const Binary *v) { |
| 48 | return v->isIR(); |
| 49 | } |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | using module_iterator = |
| 52 | pointee_iterator<std::vector<std::unique_ptr<Module>>::const_iterator, |
| 53 | const Module>; |
| 54 | |
| 55 | module_iterator module_begin() const { return module_iterator(Mods.begin()); } |
| 56 | module_iterator module_end() const { return module_iterator(Mods.end()); } |
| 57 | |
| 58 | iterator_range<module_iterator> modules() const { |
| 59 | return make_range(module_begin(), module_end()); |
| 60 | } |
| 61 | |
| 62 | /// Finds and returns bitcode embedded in the given object file, or an |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 63 | /// error code if not found. |
| 64 | static Expected<MemoryBufferRef> findBitcodeInObject(const ObjectFile &Obj); |
| 65 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | /// Finds and returns bitcode in the given memory buffer (which may |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | /// be either a bitcode file or a native object file with embedded bitcode), |
| 68 | /// or an error code if not found. |
| 69 | static Expected<MemoryBufferRef> |
| 70 | findBitcodeInMemBuffer(MemoryBufferRef Object); |
| 71 | |
| 72 | static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object, |
| 73 | LLVMContext &Context); |
| 74 | }; |
| 75 | |
| 76 | /// The contents of a bitcode file and its irsymtab. Any underlying data |
| 77 | /// for the irsymtab are owned by Symtab and Strtab. |
| 78 | struct IRSymtabFile { |
| 79 | std::vector<BitcodeModule> Mods; |
| 80 | SmallVector<char, 0> Symtab, Strtab; |
| 81 | irsymtab::Reader TheReader; |
| 82 | }; |
| 83 | |
| 84 | /// Reads a bitcode file, creating its irsymtab if necessary. |
| 85 | Expected<IRSymtabFile> readIRSymtab(MemoryBufferRef MBRef); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | #endif |