LLVM  6.0.0svn
Go to the documentation of this file.
1 //===- ObjectFile.cpp - File format independent object file ---------------===//
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 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Object/Wasm.h"
22 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorOr.h"
28 #include <algorithm>
29 #include <cstdint>
30 #include <memory>
31 #include <system_error>
33 using namespace llvm;
34 using namespace object;
36 void ObjectFile::anchor() {}
39  : SymbolicFile(Type, Source) {}
43  if (!SymSec) {
44  // TODO: Actually report errors helpfully.
45  consumeError(SymSec.takeError());
46  return false;
47  }
48  return *this == **SymSec;
49 }
52  uint32_t Flags = getSymbolFlags(Ref);
53  if (Flags & SymbolRef::SF_Undefined)
54  return 0;
55  if (Flags & SymbolRef::SF_Common)
56  return getCommonSymbolSize(Ref);
57  return getSymbolValueImpl(Ref);
58 }
61  DataRefImpl Symb) const {
63  if (!Name)
64  return errorToErrorCode(Name.takeError());
65  OS << *Name;
66  return std::error_code();
67 }
72  StringRef SectName;
73  if (!getSectionName(Sec, SectName))
74  return SectName == ".llvmbc";
75  return false;
76 }
78 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
81  return section_iterator(SectionRef(Sec, this));
82 }
85  Triple TheTriple;
86  auto Arch = getArch();
87  TheTriple.setArch(Triple::ArchType(Arch));
89  // For ARM targets, try to use the build attributes to build determine
90  // the build target. Target features are also added, but later during
91  // disassembly.
92  if (Arch == Triple::arm || Arch == Triple::armeb)
93  setARMSubArch(TheTriple);
95  // TheTriple defaults to ELF, and COFF doesn't have an environment:
96  // the best we can do here is indicate that it is mach-o.
97  if (isMachO())
98  TheTriple.setObjectFormat(Triple::MachO);
100  if (isCOFF()) {
101  const auto COFFObj = dyn_cast<COFFObjectFile>(this);
102  if (COFFObj->getArch() == Triple::thumb)
103  TheTriple.setTriple("thumbv7-windows");
104  }
106  return TheTriple;
107 }
111  StringRef Data = Object.getBuffer();
112  if (Type == file_magic::unknown)
113  Type = identify_magic(Data);
115  switch (Type) {
116  case file_magic::unknown:
117  case file_magic::bitcode:
119  case file_magic::archive:
123  case file_magic::elf:
128  return createELFObjectFile(Object);
140  return createMachOObjectFile(Object);
144  return createCOFFObjectFile(Object);
146  return createWasmObjectFile(Object);
147  }
148  llvm_unreachable("Unexpected Object File Type");
149 }
154  MemoryBuffer::getFile(ObjectPath);
155  if (std::error_code EC = FileOrErr.getError())
156  return errorCodeToError(EC);
157  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
160  createObjectFile(Buffer->getMemBufferRef());
161  if (Error Err = ObjOrErr.takeError())
162  return std::move(Err);
163  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
165  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
166 }
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:152
Represents either an error or a value T.
Definition: ErrorOr.h:69
Mach-O Object file.
Definition: Magic.h:31
ELF core image.
Definition: Magic.h:30
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:29
friend class SectionRef
Definition: ObjectFile.h:221
Triple makeTriple() const
Create a triple from the data in this object file.
Definition: ObjectFile.cpp:84
Windows compiled resource file (.res)
Definition: Magic.h:47
virtual std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const =0
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:69
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
virtual bool isSectionStripped(DataRefImpl Sec) const
Definition: ObjectFile.cpp:78
Mach-O Bundle file.
Definition: Magic.h:38
Mach-O Preloaded Executable.
Definition: Magic.h:35
virtual unsigned getArch() const =0
Bitcode file.
Definition: Magic.h:24
bool isCOFF() const
Definition: Binary.h:117
StringRef getBuffer() const
Definition: MemoryBuffer.h:170
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
The Mach-O dynamic linker.
Definition: Magic.h:37
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ELF Relocatable object file.
Definition: Magic.h:27
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
COFF import library.
Definition: Magic.h:45
virtual bool isSectionBitcode(DataRefImpl Sec) const
Definition: ObjectFile.cpp:71
Mach-O universal binary.
Definition: Magic.h:42
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:363
Mach-O Core File.
Definition: Magic.h:34
Mach-O dSYM companion file.
Definition: Magic.h:40
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
void setObjectFormat(ObjectFormatType Kind)
setObjectFormat - Set the object file format
Definition: Triple.cpp:1139
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1115
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
std::error_code getError() const
Definition: ErrorOr.h:172
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:41
PECOFF executable file.
Definition: Magic.h:46
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:962
COFF object file.
Definition: Magic.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Mach-O kext bundle file.
Definition: Magic.h:41
ar style archive file
Definition: Magic.h:25
std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:60
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0)
virtual void setARMSubArch(Triple &TheTriple) const
Definition: ObjectFile.h:284
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:51
ELF Unknown type.
Definition: Magic.h:26
ELF Executable image.
Definition: Magic.h:28
MemoryBufferRef Data
Definition: Binary.h:37
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:128
WebAssembly Object file.
Definition: Magic.h:48
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:259
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:80
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
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:49
bool isMachO() const
Definition: Binary.h:113
Mach-O Executable.
Definition: Magic.h:32
Unrecognized file.
Definition: Magic.h:23
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:84
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1119
reference get()
Definition: ErrorOr.h:169
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:21