LLVM  14.0.0git
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);
37  if (Addr.SectionIndex != SectionedAddress::UndefSection)
38  OS << ", " << Addr.SectionIndex;
39  return OS << "}";
40 }
41 
42 void ObjectFile::anchor() {}
43 
45  : SymbolicFile(Type, Source) {}
46 
48  Expected<section_iterator> SymSec = S.getSection();
49  if (!SymSec) {
50  // TODO: Actually report errors helpfully.
51  consumeError(SymSec.takeError());
52  return false;
53  }
54  return *this == **SymSec;
55 }
56 
58  if (Expected<uint32_t> FlagsOrErr = getSymbolFlags(Ref)) {
59  if (*FlagsOrErr & SymbolRef::SF_Undefined)
60  return 0;
61  if (*FlagsOrErr & SymbolRef::SF_Common)
62  return getCommonSymbolSize(Ref);
63  } else
64  // TODO: Test this error.
65  return FlagsOrErr.takeError();
66  return getSymbolValueImpl(Ref);
67 }
68 
71  if (!Name)
72  return Name.takeError();
73  OS << *Name;
74  return Error::success();
75 }
76 
78 
80  Expected<StringRef> NameOrErr = getSectionName(Sec);
81  if (NameOrErr)
82  return *NameOrErr == ".llvmbc";
83  consumeError(NameOrErr.takeError());
84  return false;
85 }
86 
87 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
88 
90  return isSectionText(Sec);
91 }
92 
94  return isSectionData(Sec);
95 }
96 
97 bool ObjectFile::isDebugSection(DataRefImpl Sec) const { return false; }
98 
101  return section_iterator(SectionRef(Sec, this));
102 }
103 
105  Triple TheTriple;
106  auto Arch = getArch();
107  TheTriple.setArch(Triple::ArchType(Arch));
108 
109  // For ARM targets, try to use the build attributes to build determine
110  // the build target. Target features are also added, but later during
111  // disassembly.
112  if (Arch == Triple::arm || Arch == Triple::armeb)
113  setARMSubArch(TheTriple);
114 
115  // TheTriple defaults to ELF, and COFF doesn't have an environment:
116  // something we can do here is indicate that it is mach-o.
117  if (isMachO()) {
118  TheTriple.setObjectFormat(Triple::MachO);
119  } else if (isCOFF()) {
120  const auto COFFObj = cast<COFFObjectFile>(this);
121  if (COFFObj->getArch() == Triple::thumb)
122  TheTriple.setTriple("thumbv7-windows");
123  } else if (isXCOFF()) {
124  // XCOFF implies AIX.
125  TheTriple.setOS(Triple::AIX);
126  TheTriple.setObjectFormat(Triple::XCOFF);
127  }
128 
129  return TheTriple;
130 }
131 
134  bool InitContent) {
135  StringRef Data = Object.getBuffer();
136  if (Type == file_magic::unknown)
138 
139  switch (Type) {
140  case file_magic::unknown:
141  case file_magic::bitcode:
143  case file_magic::archive:
146  case file_magic::pdb:
152  case file_magic::elf:
157  return createELFObjectFile(Object, InitContent);
169  return createMachOObjectFile(Object);
173  return createCOFFObjectFile(Object);
179  return createWasmObjectFile(Object);
180  }
181  llvm_unreachable("Unexpected Object File Type");
182 }
183 
187  MemoryBuffer::getFile(ObjectPath);
188  if (std::error_code EC = FileOrErr.getError())
189  return errorCodeToError(EC);
190  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
191 
193  createObjectFile(Buffer->getMemBufferRef());
194  if (Error Err = ObjOrErr.takeError())
195  return std::move(Err);
196  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
197 
198  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
199 }
MemoryBuffer.h
llvm::Triple::setTriple
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1204
llvm::file_magic::unknown
@ unknown
Unrecognized file.
Definition: Magic.h:22
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::object::ObjectFile::getSectionName
virtual Expected< StringRef > getSectionName(DataRefImpl Sec) const =0
llvm::object::SectionRef::containsSymbol
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:47
FileSystem.h
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::Binary::isXCOFF
bool isXCOFF() const
Definition: Binary.h:132
StringRef.h
llvm::file_magic::elf_core
@ elf_core
ELF core image.
Definition: Magic.h:29
llvm::object::BasicSymbolRef::SF_Common
@ SF_Common
Definition: SymbolicFile.h:112
llvm::file_magic::pdb
@ pdb
Windows PDB debug info file.
Definition: Magic.h:52
llvm::object::ObjectFile::createWasmObjectFile
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Definition: WasmObjectFile.cpp:58
llvm::file_magic::macho_dynamic_linker
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:37
ErrorHandling.h
llvm::object::ObjectFile::createMachOObjectFile
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0)
Definition: MachOObjectFile.cpp:4657
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::file_magic::elf_shared_object
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:28
Error.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::object::ObjectFile::getSymbolValue
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:57
llvm::file_magic::goff_object
@ goff_object
GOFF object file.
Definition: Magic.h:30
llvm::object::ObjectFile::setARMSubArch
virtual void setARMSubArch(Triple &TheTriple) const
Definition: ObjectFile.h:334
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
llvm::object::ObjectFile::createCOFFObjectFile
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
Definition: COFFObjectFile.cpp:1655
llvm::object::ObjectFile::getArch
virtual Triple::ArchType getArch() const =0
llvm::Triple::XCOFF
@ XCOFF
Definition: Triple.h:239
llvm::file_magic::minidump
@ minidump
Windows minidump file.
Definition: Magic.h:43
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
MachO.h
llvm::object::ObjectFile::getCommonSymbolSize
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:300
llvm::object::ObjectFile::isDebugSection
virtual bool isDebugSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:97
llvm::file_magic::bitcode
@ bitcode
Bitcode file.
Definition: Magic.h:23
llvm::object::SymbolicFile::getSymbolFlags
virtual Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const =0
llvm::object::ObjectFile::isSectionText
virtual bool isSectionText(DataRefImpl Sec) const =0
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:246
llvm::object::ObjectFile::isSectionData
virtual bool isSectionData(DataRefImpl Sec) const =0
llvm::object::ObjectFile::isBerkeleyText
virtual bool isBerkeleyText(DataRefImpl Sec) const
Definition: ObjectFile.cpp:89
Error.h
Magic.h
llvm::Triple::ArchType
ArchType
Definition: Triple.h:47
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
llvm::file_magic::xcoff_object_64
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:50
llvm::file_magic::macho_preload_executable
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:35
llvm::file_magic::macho_bundle
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:38
llvm::file_magic::macho_core
@ macho_core
Mach-O Core File.
Definition: Magic.h:34
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
llvm::file_magic::macho_dsym_companion
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:40
llvm::object::ObjectFile::ObjectFile
ObjectFile()=delete
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::ObjectFile::getSymbolValueImpl
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
llvm::object::object_error::invalid_file_type
@ invalid_file_type
llvm::object::ObjectFile::createELFObjectFile
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Definition: ELFObjectFile.cpp:72
Wasm.h
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
llvm::file_magic::coff_import_library
@ coff_import_library
COFF import library.
Definition: Magic.h:46
llvm::OutputFileType::Object
@ Object
llvm::object::operator<<
raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:35
llvm::file_magic::pecoff_executable
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:47
llvm::file_magic::macho_universal_binary
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:42
llvm::object::ObjectFile::isBerkeleyData
virtual bool isBerkeleyData(DataRefImpl Sec) const
Definition: ObjectFile.cpp:93
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::file_magic::macho_dynamically_linked_shared_lib
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:36
llvm::file_magic::macho_dynamically_linked_shared_lib_stub
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:39
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
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::object::ObjectFile::SectionRef
friend class SectionRef
Definition: ObjectFile.h:260
llvm::object::SectionedAddress::UndefSection
const static uint64_t UndefSection
Definition: ObjectFile.h:145
ErrorOr.h
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition: Magic.h:45
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:108
llvm::object::Binary::ID_XCOFF32
@ ID_XCOFF32
Definition: Binary.h:57
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:185
llvm::object::ObjectFile::printSymbolName
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:69
ObjectFile.h
llvm::object::Binary::ID_XCOFF64
@ ID_XCOFF64
Definition: Binary.h:58
llvm::object::ObjectFile::getSymbolName
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
llvm::Triple::armeb
@ armeb
Definition: Triple.h:51
llvm::object::ObjectFile::createXCOFFObjectFile
static Expected< std::unique_ptr< ObjectFile > > createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType)
Definition: XCOFFObjectFile.cpp:792
llvm::object::ObjectFile::makeTriple
Triple makeTriple() const
Create a triple from the data in this object file.
Definition: ObjectFile.cpp:104
llvm::object::Binary::isCOFF
bool isCOFF() const
Definition: Binary.h:128
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::Triple::arm
@ arm
Definition: Triple.h:50
llvm::object::section_iterator
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::file_magic::macho_kext_bundle
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:41
llvm::Triple::setArch
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1208
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::object::ObjectFile::getRelocatedSection
virtual Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:100
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:569
uint32_t
llvm::Triple::thumb
@ thumb
Definition: Triple.h:81
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::object::DataRefImpl
Definition: SymbolicFile.h:33
llvm::file_magic::macho_fixed_virtual_memory_shared_lib
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:33
llvm::object::ObjectFile::getSymbolAlignment
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:77
llvm::Triple::setOS
void setOS(OSType Kind)
setOS - Set the operating system (third) component of the triple to a known type.
Definition: Triple.cpp:1216
llvm::object::ObjectFile::isSectionStripped
virtual bool isSectionStripped(DataRefImpl Sec) const
Definition: ObjectFile.cpp:87
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::file_magic::elf
@ elf
ELF Unknown type.
Definition: Magic.h:25
llvm::file_magic::archive
@ archive
ar style archive file
Definition: Magic.h:24
llvm::Triple::MachO
@ MachO
Definition: Triple.h:237
llvm::file_magic::elf_executable
@ elf_executable
ELF Executable image.
Definition: Magic.h:27
llvm::Triple::AIX
@ AIX
Definition: Triple.h:187
llvm::object::Binary::isMachO
bool isMachO() const
Definition: Binary.h:124
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
Binary.h
COFF.h
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::object::SectionedAddress
Definition: ObjectFile.h:144
llvm::file_magic::wasm_object
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:51
llvm::file_magic::coff_cl_gl_object
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:44
llvm::object::SymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:167
llvm::file_magic::macho_executable
@ macho_executable
Mach-O Executable.
Definition: Magic.h:32
llvm::object::ObjectFile::isSectionBitcode
virtual bool isSectionBitcode(DataRefImpl Sec) const
Definition: ObjectFile.cpp:79
llvm::file_magic::tapi_file
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:53
raw_ostream.h
llvm::object::SymbolicFile
Definition: SymbolicFile.h:143
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
llvm::format_hex
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
llvm::file_magic::windows_resource
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:48
llvm::Triple::setObjectFormat
void setObjectFormat(ObjectFormatType Kind)
setObjectFormat - Set the object file format
Definition: Triple.cpp:1228
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
llvm::file_magic::xcoff_object_32
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:49