LLVM  14.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/STLExtras.h"
17 #include "llvm/IR/GVMaterializer.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Object/ObjectFile.h"
25 using namespace llvm;
26 using namespace object;
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() {}
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 {
56  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
58 }
59 
60 basic_symbol_iterator IRObjectFile::symbol_end() const {
62  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
63  SymTab.symbols().size());
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 
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 
90 IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
91  file_magic Type = identify_magic(Object.getBuffer());
92  switch (Type) {
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: {
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 
111 IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
112  Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
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 
137  IRSymtabFile F;
138  Expected<MemoryBufferRef> BCOrErr =
139  IRObjectFile::findBitcodeInMemBuffer(MBRef);
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 }
MemoryBuffer.h
llvm::object::IRSymtabFile
The contents of a bitcode file and its irsymtab.
Definition: IRObjectFile.h:78
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
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::object::readIRSymtab
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
Definition: IRObjectFile.cpp:136
bitcode
write bitcode
Definition: BitcodeWriterPass.cpp:74
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::msgpack::Type::Binary
@ Binary
getSym
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Definition: IRObjectFile.cpp:37
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
F
#define F(x, y, z)
Definition: MD5.cpp:56
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
Magic.h
llvm::getBitcodeFileContents
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
Definition: BitcodeReader.cpp:6644
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
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:101
GVMaterializer.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::object::DataRefImpl::p
uintptr_t p
Definition: SymbolicFile.h:39
llvm::irsymtab::readBitcode
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:391
llvm::OutputFileType::Object
@ Object
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:80
IRObjectFile.h
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
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:1605
llvm::object::IRObjectFile
Definition: IRObjectFile.h:31
ObjectFile.h
llvm::object::content_iterator
Definition: SymbolicFile.h:67
Mangler.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:569
llvm::object::DataRefImpl
Definition: SymbolicFile.h:33
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:149
std
Definition: BitVector.h:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:42
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition: ObjectFile.h:322
llvm::getBitcodeModuleList
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
Definition: BitcodeReader.cpp:6636
LLVMContext.h
BitcodeReader.h
raw_ostream.h
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:35
TargetRegistry.h
llvm::object::basic_symbol_iterator
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:141