LLVM 18.0.0git
IRObjectFile.cpp
Go to the documentation of this file.
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
14#include "llvm/ADT/ArrayRef.h"
18#include "llvm/IR/Module.h"
20using namespace llvm;
21using namespace object;
22
23namespace llvm {
24class LLVMContext;
25class raw_ostream;
26} // namespace llvm
27
28IRObjectFile::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
36
38 return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
39}
40
42 Symb.p += sizeof(ModuleSymbolTable::Symbol);
43}
44
46 SymTab.printSymbolName(OS, getSym(Symb));
47 return Error::success();
48}
49
51 return SymTab.getSymbolFlags(getSym(Symb));
52}
53
55 DataRefImpl Ret;
56 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
57 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
58}
59
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
68 // Each module must have the same target triple, so we arbitrarily access the
69 // first one.
70 return Mods[0]->getTargetTriple();
71}
72
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)
82 return MemoryBufferRef(*Contents, Obj.getFileName());
83 }
84 }
85
87}
88
91 file_magic Type = identify_magic(Object.getBuffer());
92 switch (Type) {
94 return Object;
101 if (!ObjFile)
102 return ObjFile.takeError();
103 return findBitcodeInObject(*ObjFile->get());
104 }
105 default:
107 }
108}
109
113 if (!BCOrErr)
114 return BCOrErr.takeError();
115
117 getBitcodeModuleList(*BCOrErr);
118 if (!BMsOrErr)
119 return BMsOrErr.takeError();
120
121 std::vector<std::unique_ptr<Module>> Mods;
122 for (auto BM : *BMsOrErr) {
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
140 if (!BCOrErr)
141 return BCOrErr.takeError();
142
144 if (!BFCOrErr)
145 return BFCOrErr.takeError();
146
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}
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
This file defines the PointerUnion class, which is a discriminated union of pointer types.
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void printSymbolName(raw_ostream &OS, Symbol S) const
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
uint32_t getSymbolFlags(Symbol S) const
ArrayRef< Symbol > symbols() const
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:103
StringRef getFileName() const
Definition: Binary.cpp:41
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
StringRef getTargetTriple() const
basic_symbol_iterator symbol_begin() const override
static Expected< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found.
basic_symbol_iterator symbol_end() const override
void moveSymbolNext(DataRefImpl &Symb) const override
static Expected< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
This class is the base class for all object file types.
Definition: ObjectFile.h:229
section_iterator_range sections() const
Definition: ObjectFile.h:328
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:198
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:411
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:143
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1854
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:103
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:52
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
@ coff_object
COFF object file.
Definition: Magic.h:46
The contents of a bitcode file and its irsymtab.
Definition: IRObjectFile.h:76