LLVM 20.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"
17#include "llvm/IR/Module.h"
19using namespace llvm;
20using namespace object;
21
22namespace llvm {
23class LLVMContext;
24class raw_ostream;
25} // namespace llvm
26
27IRObjectFile::IRObjectFile(MemoryBufferRef Object,
28 std::vector<std::unique_ptr<Module>> Mods)
29 : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
30 for (auto &M : this->Mods)
31 SymTab.addModule(M.get());
32}
33
35
37 return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
38}
39
41 Symb.p += sizeof(ModuleSymbolTable::Symbol);
42}
43
45 SymTab.printSymbolName(OS, getSym(Symb));
46 return Error::success();
47}
48
50 return SymTab.getSymbolFlags(getSym(Symb));
51}
52
54 DataRefImpl Ret;
55 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
56 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
57}
58
60 DataRefImpl Ret;
61 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
62 SymTab.symbols().size());
63 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
64}
65
67 // Each module must have the same target triple, so we arbitrarily access the
68 // first one.
69 return Mods[0]->getTargetTriple();
70}
71
74 for (const SectionRef &Sec : Obj.sections()) {
75 if (Sec.isBitcode()) {
76 Expected<StringRef> Contents = Sec.getContents();
77 if (!Contents)
78 return Contents.takeError();
79 if (Contents->size() <= 1)
81 return MemoryBufferRef(*Contents, Obj.getFileName());
82 }
83 }
84
86}
87
90 file_magic Type = identify_magic(Object.getBuffer());
91 switch (Type) {
93 return Object;
100 if (!ObjFile)
101 return ObjFile.takeError();
102 return findBitcodeInObject(*ObjFile->get());
103 }
104 default:
106 }
107}
108
112 if (!BCOrErr)
113 return BCOrErr.takeError();
114
116 getBitcodeModuleList(*BCOrErr);
117 if (!BMsOrErr)
118 return BMsOrErr.takeError();
119
120 std::vector<std::unique_ptr<Module>> Mods;
121 for (auto BM : *BMsOrErr) {
123 BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
124 /*IsImporting*/ false);
125 if (!MOrErr)
126 return MOrErr.takeError();
127
128 Mods.push_back(std::move(*MOrErr));
129 }
130
131 return std::unique_ptr<IRObjectFile>(
132 new IRObjectFile(*BCOrErr, std::move(Mods)));
133}
134
139 if (!BCOrErr)
140 return BCOrErr.takeError();
141
143 if (!BFCOrErr)
144 return BFCOrErr.takeError();
145
147 if (!FCOrErr)
148 return FCOrErr.takeError();
149
150 F.Mods = std::move(BFCOrErr->Mods);
151 F.Symtab = std::move(FCOrErr->Symtab);
152 F.Strtab = std::move(FCOrErr->Strtab);
153 F.TheReader = std::move(FCOrErr->TheReader);
154 return std::move(F);
155}
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
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:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
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:51
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:329
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:209
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:417
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:1873
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
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:27
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:53
@ macho_object
Mach-O Object file.
Definition: Magic.h:32
@ coff_object
COFF object file.
Definition: Magic.h:47
The contents of a bitcode file and its irsymtab.
Definition: IRObjectFile.h:76