LLVM  4.0.0
ObjectFile.cpp
Go to the documentation of this file.
1 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a file format independent ObjectFile class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/Object/COFF.h"
16 #include "llvm/Object/MachO.h"
17 #include "llvm/Object/Wasm.h"
22 #include <system_error>
23 
24 using namespace llvm;
25 using namespace object;
26 
27 void ObjectFile::anchor() { }
28 
29 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
30  : SymbolicFile(Type, Source) {}
31 
34  if (!SymSec) {
35  // TODO: Actually report errors helpfully.
36  consumeError(SymSec.takeError());
37  return false;
38  }
39  return *this == **SymSec;
40 }
41 
44  if (Flags & SymbolRef::SF_Undefined)
45  return 0;
46  if (Flags & SymbolRef::SF_Common)
47  return getCommonSymbolSize(Ref);
48  return getSymbolValueImpl(Ref);
49 }
50 
52  DataRefImpl Symb) const {
54  if (!Name)
55  return errorToErrorCode(Name.takeError());
56  OS << *Name;
57  return std::error_code();
58 }
59 
61 
63  StringRef SectName;
64  if (!getSectionName(Sec, SectName))
65  return SectName == ".llvmbc";
66  return false;
67 }
68 
70  return section_iterator(SectionRef(Sec, this));
71 }
72 
75  StringRef Data = Object.getBuffer();
76  if (Type == sys::fs::file_magic::unknown)
77  Type = sys::fs::identify_magic(Data);
78 
79  switch (Type) {
92  return errorOrToExpected(createELFObjectFile(Object));
104  return createMachOObjectFile(Object);
108  return errorOrToExpected(createCOFFObjectFile(Object));
110  return createWasmObjectFile(Object);
111  }
112  llvm_unreachable("Unexpected Object File Type");
113 }
114 
118  MemoryBuffer::getFile(ObjectPath);
119  if (std::error_code EC = FileOrErr.getError())
120  return errorCodeToError(EC);
121  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
122 
124  createObjectFile(Buffer->getMemBufferRef());
125  if (Error Err = ObjOrErr.takeError())
126  return std::move(Err);
127  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
128 
129  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
130 }
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:116
std::error_code getError() const
Definition: ErrorOr.h:169
Represents either an error or a value T.
Definition: ErrorOr.h:68
WebAssembly Object file.
Definition: FileSystem.h:267
friend class SectionRef
Definition: ObjectFile.h:211
static ErrorOr< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
ELF Relocatable object file.
Definition: FileSystem.h:246
virtual std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const =0
static ErrorOr< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
Error takeError()
Take ownership of the stored error.
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
struct fuzzer::@269 Flags
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Tagged union holding either a T or a Error.
ELF dynamically linked shared lib.
Definition: FileSystem.h:248
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:32
Windows compiled resource file (.rc)
Definition: FileSystem.h:266
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:36
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
ar style archive file
Definition: FileSystem.h:244
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Microsoft cl.exe's intermediate code file.
Definition: FileSystem.h:262
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
void consumeError(Error Err)
Consume a Error without doing anything.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:999
std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:51
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0)
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
MemoryBufferRef Data
Definition: Binary.h:37
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:69
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:116
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:336
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:240
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:42
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:243
virtual bool isSectionBitcode(DataRefImpl Sec) const
Definition: ObjectFile.cpp:62
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
reference get()
Definition: ErrorOr.h:166
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:60