LLVM  3.7.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/COFF.h"
15 #include "llvm/Object/MachO.h"
16 #include "llvm/Object/ObjectFile.h"
21 #include <system_error>
22 
23 using namespace llvm;
24 using namespace object;
25 
26 void ObjectFile::anchor() { }
27 
28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29  : SymbolicFile(Type, Source) {}
30 
33  if (S.getSection(SymSec))
34  return false;
35  return *this == *SymSec;
36 }
37 
39  uint32_t Flags = getSymbolFlags(Ref);
40  if (Flags & SymbolRef::SF_Undefined)
41  return 0;
42  if (Flags & SymbolRef::SF_Common)
43  return getCommonSymbolSize(Ref);
44  return getSymbolValueImpl(Ref);
45 }
46 
48  DataRefImpl Symb) const {
50  if (std::error_code EC = Name.getError())
51  return EC;
52  OS << *Name;
53  return std::error_code();
54 }
55 
56 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
57 
59  return section_iterator(SectionRef(Sec, this));
60 }
61 
64  StringRef Data = Object.getBuffer();
65  if (Type == sys::fs::file_magic::unknown)
66  Type = sys::fs::identify_magic(Data);
67 
68  switch (Type) {
80  return createELFObjectFile(Object);
92  return createMachOObjectFile(Object);
96  return createCOFFObjectFile(Object);
97  }
98  llvm_unreachable("Unexpected Object File Type");
99 }
100 
104  MemoryBuffer::getFile(ObjectPath);
105  if (std::error_code EC = FileOrErr.getError())
106  return EC;
107  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
108 
110  createObjectFile(Buffer->getMemBufferRef());
111  if (std::error_code EC = ObjOrErr.getError())
112  return EC;
113  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
114 
115  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
116 }
static ErrorOr< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object)
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
friend class SectionRef
Definition: ObjectFile.h:209
static ErrorOr< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
ELF Relocatable object file.
Definition: FileSystem.h:230
static ErrorOr< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
ELF dynamically linked shared lib.
Definition: FileSystem.h:232
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:31
Windows compiled resource file (.rc)
Definition: FileSystem.h:249
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:35
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
ar style archive file
Definition: FileSystem.h:228
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
const ObjectFile * getObject() const
Definition: ObjectFile.h:414
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:900
std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:47
StringRef getBuffer() const
Definition: MemoryBuffer.h:157
MemoryBufferRef Data
Definition: Binary.h:37
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:58
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:114
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:224
virtual section_iterator section_end() const =0
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:38
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:239
virtual ErrorOr< StringRef > getSymbolName(DataRefImpl Symb) const =0
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
std::error_code getSection(section_iterator &Result) const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:326
reference get()
Definition: ErrorOr.h:175
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:56