Movatterモバイル変換


[0]ホーム

URL:


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
13#include "llvm/Object/IRObjectFile.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/BinaryFormat/Magic.h"
16#include "llvm/Bitcode/BitcodeReader.h"
17#include "llvm/IR/Module.h"
18#include "llvm/Object/ObjectFile.h"
19using namespacellvm;
20using namespaceobject;
21
22namespacellvm {
23classLLVMContext;
24classraw_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)) {
30for (auto &M : this->Mods)
31 SymTab.addModule(M.get());
32}
33
34IRObjectFile::~IRObjectFile() =default;
35
36staticModuleSymbolTable::SymbolgetSym(DataRefImpl &Symb) {
37return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
38}
39
40voidIRObjectFile::moveSymbolNext(DataRefImpl &Symb) const{
41 Symb.p +=sizeof(ModuleSymbolTable::Symbol);
42}
43
44ErrorIRObjectFile::printSymbolName(raw_ostream &OS,DataRefImpl Symb) const{
45 SymTab.printSymbolName(OS,getSym(Symb));
46returnError::success();
47}
48
49Expected<uint32_t>IRObjectFile::getSymbolFlags(DataRefImpl Symb) const{
50return SymTab.getSymbolFlags(getSym(Symb));
51}
52
53basic_symbol_iteratorIRObjectFile::symbol_begin() const{
54DataRefImpl Ret;
55 Ret.p =reinterpret_cast<uintptr_t>(SymTab.symbols().data());
56returnbasic_symbol_iterator(BasicSymbolRef(Ret,this));
57}
58
59basic_symbol_iteratorIRObjectFile::symbol_end() const{
60DataRefImpl Ret;
61 Ret.p =reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
62 SymTab.symbols().size());
63returnbasic_symbol_iterator(BasicSymbolRef(Ret,this));
64}
65
66StringRefIRObjectFile::getTargetTriple() const{
67// Each module must have the same target triple, so we arbitrarily access the
68// first one.
69return Mods[0]->getTargetTriple();
70}
71
72Expected<MemoryBufferRef>
73IRObjectFile::findBitcodeInObject(constObjectFile &Obj) {
74for (constSectionRef &Sec : Obj.sections()) {
75if (Sec.isBitcode()) {
76Expected<StringRef> Contents = Sec.getContents();
77if (!Contents)
78return Contents.takeError();
79if (Contents->size() <= 1)
80returnerrorCodeToError(object_error::bitcode_section_not_found);
81returnMemoryBufferRef(*Contents, Obj.getFileName());
82 }
83 }
84
85returnerrorCodeToError(object_error::bitcode_section_not_found);
86}
87
88Expected<MemoryBufferRef>
89IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
90file_magicType =identify_magic(Object.getBuffer());
91switch (Type) {
92casefile_magic::bitcode:
93return Object;
94casefile_magic::elf_relocatable:
95casefile_magic::macho_object:
96casefile_magic::wasm_object:
97casefile_magic::coff_object: {
98Expected<std::unique_ptr<ObjectFile>> ObjFile =
99ObjectFile::createObjectFile(Object,Type);
100if (!ObjFile)
101return ObjFile.takeError();
102returnfindBitcodeInObject(*ObjFile->get());
103 }
104default:
105returnerrorCodeToError(object_error::invalid_file_type);
106 }
107}
108
109Expected<std::unique_ptr<IRObjectFile>>
110IRObjectFile::create(MemoryBufferRef Object,LLVMContext &Context) {
111Expected<MemoryBufferRef> BCOrErr =findBitcodeInMemBuffer(Object);
112if (!BCOrErr)
113return BCOrErr.takeError();
114
115Expected<std::vector<BitcodeModule>> BMsOrErr =
116getBitcodeModuleList(*BCOrErr);
117if (!BMsOrErr)
118return BMsOrErr.takeError();
119
120 std::vector<std::unique_ptr<Module>> Mods;
121for (auto BM : *BMsOrErr) {
122Expected<std::unique_ptr<Module>> MOrErr =
123 BM.getLazyModule(Context,/*ShouldLazyLoadMetadata*/true,
124/*IsImporting*/false);
125if (!MOrErr)
126return MOrErr.takeError();
127
128 Mods.push_back(std::move(*MOrErr));
129 }
130
131return std::unique_ptr<IRObjectFile>(
132newIRObjectFile(*BCOrErr, std::move(Mods)));
133}
134
135Expected<IRSymtabFile>object::readIRSymtab(MemoryBufferRef MBRef) {
136IRSymtabFileF;
137Expected<MemoryBufferRef> BCOrErr =
138IRObjectFile::findBitcodeInMemBuffer(MBRef);
139if (!BCOrErr)
140return BCOrErr.takeError();
141
142Expected<BitcodeFileContents> BFCOrErr =getBitcodeFileContents(*BCOrErr);
143if (!BFCOrErr)
144return BFCOrErr.takeError();
145
146Expected<irsymtab::FileContents> FCOrErr =irsymtab::readBitcode(*BFCOrErr);
147if (!FCOrErr)
148return FCOrErr.takeError();
149
150F.Mods = std::move(BFCOrErr->Mods);
151F.Symtab = std::move(FCOrErr->Symtab);
152F.Strtab = std::move(FCOrErr->Strtab);
153F.TheReader = std::move(FCOrErr->TheReader);
154return std::move(F);
155}
ArrayRef.h
BitcodeReader.h
getSym
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Definition:IRObjectFile.cpp:36
IRObjectFile.h
Module.h
Module.h This file contains the declarations for the Module class.
F
#define F(x, y, z)
Definition:MD5.cpp:55
Magic.h
ObjectFile.h
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::Expected
Tagged union holding either a T or a Error.
Definition:Error.h:481
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition:Error.h:608
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition:Error.h:578
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::MemoryBufferRef
Definition:MemoryBufferRef.h:22
llvm::ModuleSymbolTable::printSymbolName
void printSymbolName(raw_ostream &OS, Symbol S) const
Definition:ModuleSymbolTable.cpp:200
llvm::ModuleSymbolTable::Symbol
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
Definition:ModuleSymbolTable.h:36
llvm::ModuleSymbolTable::getSymbolFlags
uint32_t getSymbolFlags(Symbol S) const
Definition:ModuleSymbolTable.cpp:213
llvm::ModuleSymbolTable::symbols
ArrayRef< Symbol > symbols() const
Definition:ModuleSymbolTable.h:46
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition:PointerUnion.h:118
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::object::BasicSymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition:SymbolicFile.h:103
llvm::object::Binary
Definition:Binary.h:32
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition:Binary.cpp:41
llvm::object::IRObjectFile
Definition:IRObjectFile.h:27
llvm::object::IRObjectFile::getSymbolFlags
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
Definition:IRObjectFile.cpp:49
llvm::object::IRObjectFile::printSymbolName
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition:IRObjectFile.cpp:44
llvm::object::IRObjectFile::getTargetTriple
StringRef getTargetTriple() const
Definition:IRObjectFile.cpp:66
llvm::object::IRObjectFile::symbol_begin
basic_symbol_iterator symbol_begin() const override
Definition:IRObjectFile.cpp:53
llvm::object::IRObjectFile::findBitcodeInObject
static Expected< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found.
Definition:IRObjectFile.cpp:73
llvm::object::IRObjectFile::symbol_end
basic_symbol_iterator symbol_end() const override
Definition:IRObjectFile.cpp:59
llvm::object::IRObjectFile::moveSymbolNext
void moveSymbolNext(DataRefImpl &Symb) const override
Definition:IRObjectFile.cpp:40
llvm::object::IRObjectFile::~IRObjectFile
~IRObjectFile() override
llvm::object::IRObjectFile::findBitcodeInMemBuffer
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 ...
Definition:IRObjectFile.cpp:89
llvm::object::IRObjectFile::create
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
Definition:IRObjectFile.cpp:110
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition:ObjectFile.h:229
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition:ObjectFile.h:329
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition:ObjectFile.cpp:209
llvm::object::SectionRef
This is a value type class that represents a single section in the list of sections in the object fil...
Definition:ObjectFile.h:81
llvm::object::SymbolicFile
Definition:SymbolicFile.h:145
llvm::object::content_iterator
Definition:SymbolicFile.h:69
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::ARM::ProfileKind::M
@ M
llvm::ifs::IFSSymbolType::Object
@ Object
llvm::irsymtab::readBitcode
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition:IRSymtab.cpp:417
llvm::object::readIRSymtab
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
Definition:IRObjectFile.cpp:135
llvm::object::object_error::bitcode_section_not_found
@ bitcode_section_not_found
llvm::object::object_error::invalid_file_type
@ invalid_file_type
llvm::object::basic_symbol_iterator
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition:SymbolicFile.h:143
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::identify_magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition:Magic.cpp:33
llvm::getBitcodeFileContents
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
Definition:BitcodeReader.cpp:8372
llvm::getBitcodeModuleList
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
Definition:BitcodeReader.cpp:8364
llvm::move
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
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition:Error.cpp:111
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858
llvm::file_magic
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition:Magic.h:20
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition:Magic.h:27
llvm::file_magic::bitcode
@ bitcode
Bitcode file.
Definition:Magic.h:23
llvm::file_magic::wasm_object
@ wasm_object
WebAssembly Object file.
Definition:Magic.h:53
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition:Magic.h:32
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition:Magic.h:47
llvm::object::IRSymtabFile
The contents of a bitcode file and its irsymtab.
Definition:IRObjectFile.h:76
llvm::object::DataRefImpl
Definition:SymbolicFile.h:35
llvm::object::DataRefImpl::p
uintptr_t p
Definition:SymbolicFile.h:41

Generated on Fri Jul 18 2025 11:45:53 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp