LLVM  10.0.0
ObjectFile.cpp
Go to the documentation of this file.
1 //===- ObjectFile.cpp - File format independent object file ---------------===//
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 // This file defines a file format independent ObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Object/Binary.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Object/Wasm.h"
21 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorOr.h"
27 #include <algorithm>
28 #include <cstdint>
29 #include <memory>
30 #include <system_error>
31 
32 using namespace llvm;
33 using namespace object;
34 
36  OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
38  OS << ", " << Addr.SectionIndex;
39  return OS << "}";
40 }
41 
42 void ObjectFile::anchor() {}
43 
45  : SymbolicFile(Type, Source) {}
46 
49  if (!SymSec) {
50  // TODO: Actually report errors helpfully.
51  consumeError(SymSec.takeError());
52  return false;
53  }
54  return *this == **SymSec;
55 }
56 
58  uint32_t Flags = getSymbolFlags(Ref);
59  if (Flags & SymbolRef::SF_Undefined)
60  return 0;
61  if (Flags & SymbolRef::SF_Common)
62  return getCommonSymbolSize(Ref);
63  return getSymbolValueImpl(Ref);
64 }
65 
68  if (!Name)
69  return Name.takeError();
70  OS << *Name;
71  return Error::success();
72 }
73 
75 
77  Expected<StringRef> NameOrErr = getSectionName(Sec);
78  if (NameOrErr)
79  return *NameOrErr == ".llvmbc";
80  consumeError(NameOrErr.takeError());
81  return false;
82 }
83 
84 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
85 
87  return isSectionText(Sec);
88 }
89 
91  return isSectionData(Sec);
92 }
93 
96  return section_iterator(SectionRef(Sec, this));
97 }
98 
100  Triple TheTriple;
101  auto Arch = getArch();
102  TheTriple.setArch(Triple::ArchType(Arch));
103 
104  // For ARM targets, try to use the build attributes to build determine
105  // the build target. Target features are also added, but later during
106  // disassembly.
107  if (Arch == Triple::arm || Arch == Triple::armeb)
108  setARMSubArch(TheTriple);
109 
110  // TheTriple defaults to ELF, and COFF doesn't have an environment:
111  // the best we can do here is indicate that it is mach-o.
112  if (isMachO())
113  TheTriple.setObjectFormat(Triple::MachO);
114 
115  if (isCOFF()) {
116  const auto COFFObj = cast<COFFObjectFile>(this);
117  if (COFFObj->getArch() == Triple::thumb)
118  TheTriple.setTriple("thumbv7-windows");
119  }
120 
121  return TheTriple;
122 }
123 
126  StringRef Data = Object.getBuffer();
127  if (Type == file_magic::unknown)
128  Type = identify_magic(Data);
129 
130  switch (Type) {
131  case file_magic::unknown:
132  case file_magic::bitcode:
134  case file_magic::archive:
137  case file_magic::pdb:
142  case file_magic::elf:
147  return createELFObjectFile(Object);
159  return createMachOObjectFile(Object);
163  return createCOFFObjectFile(Object);
169  return createWasmObjectFile(Object);
170  }
171  llvm_unreachable("Unexpected Object File Type");
172 }
173 
177  MemoryBuffer::getFile(ObjectPath);
178  if (std::error_code EC = FileOrErr.getError())
179  return errorCodeToError(EC);
180  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
181 
183  createObjectFile(Buffer->getMemBufferRef());
184  if (Error Err = ObjOrErr.takeError())
185  return std::move(Err);
186  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
187 
188  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
189 }
raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:35
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:175
32-bit XCOFF object file
Definition: Magic.h:48
Represents either an error or a value T.
Definition: ErrorOr.h:56
Mach-O Object file.
Definition: Magic.h:30
ELF core image.
Definition: Magic.h:29
virtual bool isBerkeleyData(DataRefImpl Sec) const
Definition: ObjectFile.cpp:90
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:28
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
friend class SectionRef
Definition: ObjectFile.h:255
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
virtual Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:95
Triple makeTriple() const
Create a triple from the data in this object file.
Definition: ObjectFile.cpp:99
Windows compiled resource file (.res)
Definition: Magic.h:47
static const uint64_t UndefSection
Definition: ObjectFile.h:140
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static Expected< std::unique_ptr< ObjectFile > > createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType)
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:74
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
virtual bool isSectionStripped(DataRefImpl Sec) const
Definition: ObjectFile.cpp:84
Mach-O Bundle file.
Definition: Magic.h:37
Mach-O Preloaded Executable.
Definition: Magic.h:34
Bitcode file.
Definition: Magic.h:23
bool isCOFF() const
Definition: Binary.h:126
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
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:36
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
64-bit XCOFF object file
Definition: Magic.h:49
ELF Relocatable object file.
Definition: Magic.h:26
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
Windows minidump file.
Definition: Magic.h:42
COFF import library.
Definition: Magic.h:45
virtual bool isSectionBitcode(DataRefImpl Sec) const
Definition: ObjectFile.cpp:76
Mach-O universal binary.
Definition: Magic.h:41
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:397
Mach-O Core File.
Definition: Magic.h:33
Mach-O dSYM companion file.
Definition: Magic.h:39
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
void setObjectFormat(ObjectFormatType Kind)
setObjectFormat - Set the object file format
Definition: Triple.cpp:1193
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:87
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1169
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
std::error_code getError() const
Definition: ErrorOr.h:159
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:47
PECOFF executable file.
Definition: Magic.h:46
virtual Expected< StringRef > getSectionName(DataRefImpl Sec) const =0
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
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:43
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Mach-O kext bundle file.
Definition: Magic.h:40
ar style archive file
Definition: Magic.h:24
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
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:323
virtual Triple::ArchType getArch() const =0
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:57
ELF Unknown type.
Definition: Magic.h:25
ELF Executable image.
Definition: Magic.h:27
MemoryBufferRef Data
Definition: Binary.h:37
virtual bool isSectionText(DataRefImpl Sec) const =0
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:162
WebAssembly Object file.
Definition: Magic.h:50
virtual bool isBerkeleyText(DataRefImpl Sec) const
Definition: ObjectFile.cpp:86
virtual bool isSectionData(DataRefImpl Sec) const =0
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:294
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:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
bool isMachO() const
Definition: Binary.h:122
Mach-O Executable.
Definition: Magic.h:31
Unrecognized file.
Definition: Magic.h:22
Windows PDB debug info file.
Definition: Magic.h:51
Text-based Dynamic Library Stub file.
Definition: Magic.h:52
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1173
reference get()
Definition: ErrorOr.h:156
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:66
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20