[Object] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300779 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eugene Zelenko 2017-04-19 23:02:10 +00:00
parent d89a81f5c9
commit 86bfc787f1
10 changed files with 253 additions and 135 deletions

View File

@ -14,15 +14,20 @@
#ifndef LLVM_OBJECT_ARCHIVE_H #ifndef LLVM_OBJECT_ARCHIVE_H
#define LLVM_OBJECT_ARCHIVE_H #define LLVM_OBJECT_ARCHIVE_H
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/Optional.h" #include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Object/Binary.h" #include "llvm/Object/Binary.h"
#include "llvm/Support/Chrono.h" #include "llvm/Support/Chrono.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
namespace llvm { namespace llvm {
namespace object { namespace object {
@ -32,25 +37,28 @@ class Archive;
class ArchiveMemberHeader { class ArchiveMemberHeader {
public: public:
friend class Archive; friend class Archive;
ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr, ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
uint64_t Size, Error *Err); uint64_t Size, Error *Err);
// ArchiveMemberHeader() = default; // ArchiveMemberHeader() = default;
/// Get the name without looking up long names. /// Get the name without looking up long names.
Expected<llvm::StringRef> getRawName() const; Expected<StringRef> getRawName() const;
/// Get the name looking up long names. /// Get the name looking up long names.
Expected<llvm::StringRef> getName(uint64_t Size) const; Expected<StringRef> getName(uint64_t Size) const;
/// Members are not larger than 4GB. /// Members are not larger than 4GB.
Expected<uint32_t> getSize() const; Expected<uint32_t> getSize() const;
Expected<sys::fs::perms> getAccessMode() const; Expected<sys::fs::perms> getAccessMode() const;
Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const; Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
llvm::StringRef getRawLastModified() const {
StringRef getRawLastModified() const {
return StringRef(ArMemHdr->LastModified, return StringRef(ArMemHdr->LastModified,
sizeof(ArMemHdr->LastModified)).rtrim(' '); sizeof(ArMemHdr->LastModified)).rtrim(' ');
} }
Expected<unsigned> getUID() const; Expected<unsigned> getUID() const;
Expected<unsigned> getGID() const; Expected<unsigned> getGID() const;
@ -75,11 +83,13 @@ private:
class Archive : public Binary { class Archive : public Binary {
virtual void anchor(); virtual void anchor();
public: public:
class Child { class Child {
friend Archive; friend Archive;
const Archive *Parent;
friend ArchiveMemberHeader; friend ArchiveMemberHeader;
const Archive *Parent;
ArchiveMemberHeader Header; ArchiveMemberHeader Header;
/// \brief Includes header but not padding byte. /// \brief Includes header but not padding byte.
StringRef Data; StringRef Data;
@ -103,17 +113,22 @@ public:
Expected<StringRef> getName() const; Expected<StringRef> getName() const;
Expected<std::string> getFullName() const; Expected<std::string> getFullName() const;
Expected<StringRef> getRawName() const { return Header.getRawName(); } Expected<StringRef> getRawName() const { return Header.getRawName(); }
Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const { Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
return Header.getLastModified(); return Header.getLastModified();
} }
StringRef getRawLastModified() const { StringRef getRawLastModified() const {
return Header.getRawLastModified(); return Header.getRawLastModified();
} }
Expected<unsigned> getUID() const { return Header.getUID(); } Expected<unsigned> getUID() const { return Header.getUID(); }
Expected<unsigned> getGID() const { return Header.getGID(); } Expected<unsigned> getGID() const { return Header.getGID(); }
Expected<sys::fs::perms> getAccessMode() const { Expected<sys::fs::perms> getAccessMode() const {
return Header.getAccessMode(); return Header.getAccessMode();
} }
/// \return the size of the archive member without the header or padding. /// \return the size of the archive member without the header or padding.
Expected<uint64_t> getSize() const; Expected<uint64_t> getSize() const;
/// \return the size in the archive header for this member. /// \return the size in the archive header for this member.
@ -130,11 +145,12 @@ public:
class child_iterator { class child_iterator {
Child C; Child C;
Error *E; Error *E = nullptr;
public: public:
child_iterator() : C(Child(nullptr, nullptr, nullptr)), E(nullptr) {} child_iterator() : C(Child(nullptr, nullptr, nullptr)) {}
child_iterator(const Child &C, Error *E) : C(C), E(E) {} child_iterator(const Child &C, Error *E) : C(C), E(E) {}
const Child *operator->() const { return &C; } const Child *operator->() const { return &C; }
const Child &operator*() const { return C; } const Child &operator*() const { return C; }
@ -171,14 +187,15 @@ public:
uint32_t StringIndex; // Extra index to the string. uint32_t StringIndex; // Extra index to the string.
public: public:
bool operator ==(const Symbol &other) const {
return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
}
Symbol(const Archive *p, uint32_t symi, uint32_t stri) Symbol(const Archive *p, uint32_t symi, uint32_t stri)
: Parent(p) : Parent(p)
, SymbolIndex(symi) , SymbolIndex(symi)
, StringIndex(stri) {} , StringIndex(stri) {}
bool operator ==(const Symbol &other) const {
return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
}
StringRef getName() const; StringRef getName() const;
Expected<Child> getMember() const; Expected<Child> getMember() const;
Symbol getNext() const; Symbol getNext() const;
@ -186,8 +203,10 @@ public:
class symbol_iterator { class symbol_iterator {
Symbol symbol; Symbol symbol;
public: public:
symbol_iterator(const Symbol &s) : symbol(s) {} symbol_iterator(const Symbol &s) : symbol(s) {}
const Symbol *operator->() const { return &symbol; } const Symbol *operator->() const { return &symbol; }
const Symbol &operator*() const { return symbol; } const Symbol &operator*() const { return symbol; }
@ -264,7 +283,7 @@ private:
mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers; mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
}; };
} } // end namespace object
} } // end namespace llvm
#endif #endif // LLVM_OBJECT_ARCHIVE_H

View File

@ -15,10 +15,11 @@
#define LLVM_OBJECT_BINARY_H #define LLVM_OBJECT_BINARY_H
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/Object/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <memory>
#include <utility>
namespace llvm { namespace llvm {
@ -29,9 +30,6 @@ namespace object {
class Binary { class Binary {
private: private:
Binary() = delete;
Binary(const Binary &other) = delete;
unsigned int TypeID; unsigned int TypeID;
protected: protected:
@ -80,6 +78,8 @@ protected:
} }
public: public:
Binary() = delete;
Binary(const Binary &other) = delete;
virtual ~Binary(); virtual ~Binary();
StringRef getData() const; StringRef getData() const;
@ -173,7 +173,7 @@ OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
std::unique_ptr<MemoryBuffer> Buf) std::unique_ptr<MemoryBuffer> Buf)
: Bin(std::move(Bin)), Buf(std::move(Buf)) {} : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
template <typename T> OwningBinary<T>::OwningBinary() {} template <typename T> OwningBinary<T>::OwningBinary() = default;
template <typename T> template <typename T>
OwningBinary<T>::OwningBinary(OwningBinary &&Other) OwningBinary<T>::OwningBinary(OwningBinary &&Other)
@ -201,7 +201,9 @@ template <typename T> const T* OwningBinary<T>::getBinary() const {
} }
Expected<OwningBinary<Binary>> createBinary(StringRef Path); Expected<OwningBinary<Binary>> createBinary(StringRef Path);
}
}
#endif } // end namespace object
} // end namespace llvm
#endif // LLVM_OBJECT_BINARY_H

View File

@ -14,28 +14,39 @@
#ifndef LLVM_OBJECT_COFF_H #ifndef LLVM_OBJECT_COFF_H
#define LLVM_OBJECT_COFF_H #define LLVM_OBJECT_COFF_H
#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/iterator_range.h"
#include "llvm/DebugInfo/CodeView/CVDebugRecord.h" #include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include "llvm/Support/COFF.h" #include "llvm/Support/COFF.h"
#include "llvm/Support/Endian.h" #include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h" #include "llvm/Support/ErrorOr.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <system_error>
namespace llvm { namespace llvm {
template <typename T> class ArrayRef; template <typename T> class ArrayRef;
namespace object { namespace object {
class ImportDirectoryEntryRef;
class BaseRelocRef;
class DelayImportDirectoryEntryRef; class DelayImportDirectoryEntryRef;
class ExportDirectoryEntryRef; class ExportDirectoryEntryRef;
class ImportDirectoryEntryRef;
class ImportedSymbolRef; class ImportedSymbolRef;
class BaseRelocRef;
typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator; using import_directory_iterator = content_iterator<ImportDirectoryEntryRef>;
typedef content_iterator<DelayImportDirectoryEntryRef> using delay_import_directory_iterator =
delay_import_directory_iterator; content_iterator<DelayImportDirectoryEntryRef>;
typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator; using export_directory_iterator = content_iterator<ExportDirectoryEntryRef>;
typedef content_iterator<ImportedSymbolRef> imported_symbol_iterator; using imported_symbol_iterator = content_iterator<ImportedSymbolRef>;
typedef content_iterator<BaseRelocRef> base_reloc_iterator; using base_reloc_iterator = content_iterator<BaseRelocRef>;
/// The DOS compatible header at the front of all PE/COFF executables. /// The DOS compatible header at the front of all PE/COFF executables.
struct dos_header { struct dos_header {
@ -190,10 +201,10 @@ struct import_lookup_table_entry {
} }
}; };
typedef import_lookup_table_entry<support::little32_t> using import_lookup_table_entry32 =
import_lookup_table_entry32; import_lookup_table_entry<support::little32_t>;
typedef import_lookup_table_entry<support::little64_t> using import_lookup_table_entry64 =
import_lookup_table_entry64; import_lookup_table_entry<support::little64_t>;
struct delay_import_directory_table_entry { struct delay_import_directory_table_entry {
// dumpbin reports this field as "Characteristics" instead of "Attributes". // dumpbin reports this field as "Characteristics" instead of "Attributes".
@ -226,8 +237,8 @@ union export_address_table_entry {
support::ulittle32_t ForwarderRVA; support::ulittle32_t ForwarderRVA;
}; };
typedef support::ulittle32_t export_name_pointer_table_entry; using export_name_pointer_table_entry = support::ulittle32_t;
typedef support::ulittle16_t export_ordinal_table_entry; using export_ordinal_table_entry = support::ulittle16_t;
struct StringTableOffset { struct StringTableOffset {
support::ulittle32_t Zeroes; support::ulittle32_t Zeroes;
@ -250,8 +261,8 @@ struct coff_symbol {
uint8_t NumberOfAuxSymbols; uint8_t NumberOfAuxSymbols;
}; };
typedef coff_symbol<support::ulittle16_t> coff_symbol16; using coff_symbol16 = coff_symbol<support::ulittle16_t>;
typedef coff_symbol<support::ulittle32_t> coff_symbol32; using coff_symbol32 = coff_symbol<support::ulittle32_t>;
// Contains only common parts of coff_symbol16 and coff_symbol32. // Contains only common parts of coff_symbol16 and coff_symbol32.
struct coff_symbol_generic { struct coff_symbol_generic {
@ -264,9 +275,9 @@ struct coff_symbol_generic {
class COFFSymbolRef { class COFFSymbolRef {
public: public:
COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {} COFFSymbolRef() = default;
COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {} COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS) {}
COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {} COFFSymbolRef(const coff_symbol32 *CS) : CS32(CS) {}
const void *getRawPtr() const { const void *getRawPtr() const {
return CS16 ? static_cast<const void *>(CS16) : CS32; return CS16 ? static_cast<const void *>(CS16) : CS32;
@ -396,8 +407,8 @@ public:
private: private:
bool isSet() const { return CS16 || CS32; } bool isSet() const { return CS16 || CS32; }
const coff_symbol16 *CS16; const coff_symbol16 *CS16 = nullptr;
const coff_symbol32 *CS32; const coff_symbol32 *CS32 = nullptr;
}; };
struct coff_section { struct coff_section {
@ -418,6 +429,7 @@ struct coff_section {
return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) && return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
NumberOfRelocations == UINT16_MAX; NumberOfRelocations == UINT16_MAX;
} }
uint32_t getAlignment() const { uint32_t getAlignment() const {
// The IMAGE_SCN_TYPE_NO_PAD bit is a legacy way of getting to // The IMAGE_SCN_TYPE_NO_PAD bit is a legacy way of getting to
// IMAGE_SCN_ALIGN_1BYTES. // IMAGE_SCN_ALIGN_1BYTES.
@ -508,6 +520,7 @@ struct coff_import_header {
support::ulittle32_t SizeOfData; support::ulittle32_t SizeOfData;
support::ulittle16_t OrdinalHint; support::ulittle16_t OrdinalHint;
support::ulittle16_t TypeInfo; support::ulittle16_t TypeInfo;
int getType() const { return TypeInfo & 0x3; } int getType() const { return TypeInfo & 0x3; }
int getNameType() const { return (TypeInfo >> 2) & 0x7; } int getNameType() const { return (TypeInfo >> 2) & 0x7; }
}; };
@ -518,6 +531,7 @@ struct coff_import_directory_table_entry {
support::ulittle32_t ForwarderChain; support::ulittle32_t ForwarderChain;
support::ulittle32_t NameRVA; support::ulittle32_t NameRVA;
support::ulittle32_t ImportAddressTableRVA; support::ulittle32_t ImportAddressTableRVA;
bool isNull() const { bool isNull() const {
return ImportLookupTableRVA == 0 && TimeDateStamp == 0 && return ImportLookupTableRVA == 0 && TimeDateStamp == 0 &&
ForwarderChain == 0 && NameRVA == 0 && ImportAddressTableRVA == 0; ForwarderChain == 0 && NameRVA == 0 && ImportAddressTableRVA == 0;
@ -532,6 +546,7 @@ struct coff_tls_directory {
IntTy AddressOfCallBacks; IntTy AddressOfCallBacks;
support::ulittle32_t SizeOfZeroFill; support::ulittle32_t SizeOfZeroFill;
support::ulittle32_t Characteristics; support::ulittle32_t Characteristics;
uint32_t getAlignment() const { uint32_t getAlignment() const {
// Bit [20:24] contains section alignment. // Bit [20:24] contains section alignment.
uint32_t Shift = (Characteristics & 0x00F00000) >> 20; uint32_t Shift = (Characteristics & 0x00F00000) >> 20;
@ -541,8 +556,8 @@ struct coff_tls_directory {
} }
}; };
typedef coff_tls_directory<support::little32_t> coff_tls_directory32; using coff_tls_directory32 = coff_tls_directory<support::little32_t>;
typedef coff_tls_directory<support::little64_t> coff_tls_directory64; using coff_tls_directory64 = coff_tls_directory<support::little64_t>;
struct coff_load_configuration32 { struct coff_load_configuration32 {
support::ulittle32_t Characteristics; support::ulittle32_t Characteristics;
@ -603,6 +618,7 @@ struct coff_base_reloc_block_header {
struct coff_base_reloc_block_entry { struct coff_base_reloc_block_entry {
support::ulittle16_t Data; support::ulittle16_t Data;
int getType() const { return Data >> 12; } int getType() const { return Data >> 12; }
int getOffset() const { return Data & ((1 << 12) - 1); } int getOffset() const { return Data & ((1 << 12) - 1); }
}; };
@ -652,6 +668,7 @@ public:
return reinterpret_cast<uintptr_t>(SymbolTable32); return reinterpret_cast<uintptr_t>(SymbolTable32);
return uintptr_t(0); return uintptr_t(0);
} }
uint16_t getMachine() const { uint16_t getMachine() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->Machine; return COFFHeader->Machine;
@ -659,6 +676,7 @@ public:
return COFFBigObjHeader->Machine; return COFFBigObjHeader->Machine;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint16_t getSizeOfOptionalHeader() const { uint16_t getSizeOfOptionalHeader() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->isImportLibrary() ? 0 return COFFHeader->isImportLibrary() ? 0
@ -668,6 +686,7 @@ public:
return 0; return 0;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint16_t getCharacteristics() const { uint16_t getCharacteristics() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics; return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics;
@ -677,6 +696,7 @@ public:
return 0; return 0;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint32_t getTimeDateStamp() const { uint32_t getTimeDateStamp() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->TimeDateStamp; return COFFHeader->TimeDateStamp;
@ -684,6 +704,7 @@ public:
return COFFBigObjHeader->TimeDateStamp; return COFFBigObjHeader->TimeDateStamp;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint32_t getNumberOfSections() const { uint32_t getNumberOfSections() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSections; return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSections;
@ -691,6 +712,7 @@ public:
return COFFBigObjHeader->NumberOfSections; return COFFBigObjHeader->NumberOfSections;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint32_t getPointerToSymbolTable() const { uint32_t getPointerToSymbolTable() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->isImportLibrary() ? 0 return COFFHeader->isImportLibrary() ? 0
@ -699,6 +721,7 @@ public:
return COFFBigObjHeader->PointerToSymbolTable; return COFFBigObjHeader->PointerToSymbolTable;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint32_t getRawNumberOfSymbols() const { uint32_t getRawNumberOfSymbols() const {
if (COFFHeader) if (COFFHeader)
return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSymbols; return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSymbols;
@ -706,11 +729,13 @@ public:
return COFFBigObjHeader->NumberOfSymbols; return COFFBigObjHeader->NumberOfSymbols;
llvm_unreachable("no COFF header!"); llvm_unreachable("no COFF header!");
} }
uint32_t getNumberOfSymbols() const { uint32_t getNumberOfSymbols() const {
if (!SymbolTable16 && !SymbolTable32) if (!SymbolTable16 && !SymbolTable32)
return 0; return 0;
return getRawNumberOfSymbols(); return getRawNumberOfSymbols();
} }
protected: protected:
void moveSymbolNext(DataRefImpl &Symb) const override; void moveSymbolNext(DataRefImpl &Symb) const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override; Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
@ -746,6 +771,7 @@ protected:
public: public:
COFFObjectFile(MemoryBufferRef Object, std::error_code &EC); COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
basic_symbol_iterator symbol_begin() const override; basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override; basic_symbol_iterator symbol_end() const override;
section_iterator section_begin() const override; section_iterator section_begin() const override;
@ -797,6 +823,7 @@ public:
std::error_code getDataDirectory(uint32_t index, std::error_code getDataDirectory(uint32_t index,
const data_directory *&Res) const; const data_directory *&Res) const;
std::error_code getSection(int32_t index, const coff_section *&Res) const; std::error_code getSection(int32_t index, const coff_section *&Res) const;
template <typename coff_symbol_type> template <typename coff_symbol_type>
std::error_code getSymbol(uint32_t Index, std::error_code getSymbol(uint32_t Index,
const coff_symbol_type *&Res) const { const coff_symbol_type *&Res) const {
@ -821,6 +848,7 @@ public:
} }
return object_error::parse_failed; return object_error::parse_failed;
} }
template <typename T> template <typename T>
std::error_code getAuxSymbol(uint32_t index, const T *&Res) const { std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
ErrorOr<COFFSymbolRef> s = getSymbol(index); ErrorOr<COFFSymbolRef> s = getSymbol(index);
@ -829,6 +857,7 @@ public:
Res = reinterpret_cast<const T *>(s->getRawPtr()); Res = reinterpret_cast<const T *>(s->getRawPtr());
return std::error_code(); return std::error_code();
} }
std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const; std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
std::error_code getSymbolName(const coff_symbol_generic *Symbol, std::error_code getSymbolName(const coff_symbol_generic *Symbol,
StringRef &Res) const; StringRef &Res) const;
@ -885,7 +914,7 @@ public:
// The iterator for the import directory table. // The iterator for the import directory table.
class ImportDirectoryEntryRef { class ImportDirectoryEntryRef {
public: public:
ImportDirectoryEntryRef() : OwningObject(nullptr) {} ImportDirectoryEntryRef() = default;
ImportDirectoryEntryRef(const coff_import_directory_table_entry *Table, ImportDirectoryEntryRef(const coff_import_directory_table_entry *Table,
uint32_t I, const COFFObjectFile *Owner) uint32_t I, const COFFObjectFile *Owner)
: ImportTable(Table), Index(I), OwningObject(Owner) {} : ImportTable(Table), Index(I), OwningObject(Owner) {}
@ -911,12 +940,12 @@ public:
private: private:
const coff_import_directory_table_entry *ImportTable; const coff_import_directory_table_entry *ImportTable;
uint32_t Index; uint32_t Index;
const COFFObjectFile *OwningObject; const COFFObjectFile *OwningObject = nullptr;
}; };
class DelayImportDirectoryEntryRef { class DelayImportDirectoryEntryRef {
public: public:
DelayImportDirectoryEntryRef() : OwningObject(nullptr) {} DelayImportDirectoryEntryRef() = default;
DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T, DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
uint32_t I, const COFFObjectFile *Owner) uint32_t I, const COFFObjectFile *Owner)
: Table(T), Index(I), OwningObject(Owner) {} : Table(T), Index(I), OwningObject(Owner) {}
@ -936,13 +965,13 @@ public:
private: private:
const delay_import_directory_table_entry *Table; const delay_import_directory_table_entry *Table;
uint32_t Index; uint32_t Index;
const COFFObjectFile *OwningObject; const COFFObjectFile *OwningObject = nullptr;
}; };
// The iterator for the export directory table entry. // The iterator for the export directory table entry.
class ExportDirectoryEntryRef { class ExportDirectoryEntryRef {
public: public:
ExportDirectoryEntryRef() : OwningObject(nullptr) {} ExportDirectoryEntryRef() = default;
ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I, ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
const COFFObjectFile *Owner) const COFFObjectFile *Owner)
: ExportTable(Table), Index(I), OwningObject(Owner) {} : ExportTable(Table), Index(I), OwningObject(Owner) {}
@ -962,12 +991,12 @@ public:
private: private:
const export_directory_table_entry *ExportTable; const export_directory_table_entry *ExportTable;
uint32_t Index; uint32_t Index;
const COFFObjectFile *OwningObject; const COFFObjectFile *OwningObject = nullptr;
}; };
class ImportedSymbolRef { class ImportedSymbolRef {
public: public:
ImportedSymbolRef() : OwningObject(nullptr) {} ImportedSymbolRef() = default;
ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I, ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
const COFFObjectFile *Owner) const COFFObjectFile *Owner)
: Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {} : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
@ -987,12 +1016,12 @@ private:
const import_lookup_table_entry32 *Entry32; const import_lookup_table_entry32 *Entry32;
const import_lookup_table_entry64 *Entry64; const import_lookup_table_entry64 *Entry64;
uint32_t Index; uint32_t Index;
const COFFObjectFile *OwningObject; const COFFObjectFile *OwningObject = nullptr;
}; };
class BaseRelocRef { class BaseRelocRef {
public: public:
BaseRelocRef() : OwningObject(nullptr) {} BaseRelocRef() = default;
BaseRelocRef(const coff_base_reloc_block_header *Header, BaseRelocRef(const coff_base_reloc_block_header *Header,
const COFFObjectFile *Owner) const COFFObjectFile *Owner)
: Header(Header), Index(0), OwningObject(Owner) {} : Header(Header), Index(0), OwningObject(Owner) {}
@ -1006,7 +1035,7 @@ public:
private: private:
const coff_base_reloc_block_header *Header; const coff_base_reloc_block_header *Header;
uint32_t Index; uint32_t Index;
const COFFObjectFile *OwningObject; const COFFObjectFile *OwningObject = nullptr;
}; };
// Corresponds to `_FPO_DATA` structure in the PE/COFF spec. // Corresponds to `_FPO_DATA` structure in the PE/COFF spec.
@ -1034,6 +1063,7 @@ struct FpoData {
}; };
} // end namespace object } // end namespace object
} // end namespace llvm } // end namespace llvm
#endif #endif // LLVM_OBJECT_COFF_H

View File

@ -14,39 +14,46 @@
#ifndef LLVM_OBJECT_OBJECTFILE_H #ifndef LLVM_OBJECT_OBJECTFILE_H
#define LLVM_OBJECT_OBJECTFILE_H #define LLVM_OBJECT_OBJECTFILE_H
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/MC/SubtargetFeature.h" #include "llvm/MC/SubtargetFeature.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/SymbolicFile.h" #include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include <cstring> #include <cassert>
#include <cstdint>
#include <memory>
#include <system_error>
namespace llvm { namespace llvm {
class ARMAttributeParser; class ARMAttributeParser;
namespace object { namespace object {
class ObjectFile;
class COFFObjectFile; class COFFObjectFile;
class MachOObjectFile; class MachOObjectFile;
class WasmObjectFile; class ObjectFile;
class SectionRef;
class SymbolRef; class SymbolRef;
class symbol_iterator; class symbol_iterator;
class SectionRef; class WasmObjectFile;
typedef content_iterator<SectionRef> section_iterator;
using section_iterator = content_iterator<SectionRef>;
/// This is a value type class that represents a single relocation in the list /// This is a value type class that represents a single relocation in the list
/// of relocations in the object file. /// of relocations in the object file.
class RelocationRef { class RelocationRef {
DataRefImpl RelocationPimpl; DataRefImpl RelocationPimpl;
const ObjectFile *OwningObject; const ObjectFile *OwningObject = nullptr;
public: public:
RelocationRef() : OwningObject(nullptr) { } RelocationRef() = default;
RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
bool operator==(const RelocationRef &Other) const; bool operator==(const RelocationRef &Other) const;
@ -65,18 +72,19 @@ public:
DataRefImpl getRawDataRefImpl() const; DataRefImpl getRawDataRefImpl() const;
const ObjectFile *getObject() const; const ObjectFile *getObject() const;
}; };
typedef content_iterator<RelocationRef> relocation_iterator;
using relocation_iterator = content_iterator<RelocationRef>;
/// This is a value type class that represents a single section in the list of /// This is a value type class that represents a single section in the list of
/// sections in the object file. /// sections in the object file.
class SectionRef { class SectionRef {
friend class SymbolRef; friend class SymbolRef;
DataRefImpl SectionPimpl; DataRefImpl SectionPimpl;
const ObjectFile *OwningObject; const ObjectFile *OwningObject = nullptr;
public: public:
SectionRef() : OwningObject(nullptr) { } SectionRef() = default;
SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
bool operator==(const SectionRef &Other) const; bool operator==(const SectionRef &Other) const;
@ -119,8 +127,6 @@ class SymbolRef : public BasicSymbolRef {
friend class SectionRef; friend class SectionRef;
public: public:
SymbolRef() : BasicSymbolRef() {}
enum Type { enum Type {
ST_Unknown, // Type not specified ST_Unknown, // Type not specified
ST_Data, ST_Data,
@ -130,6 +136,7 @@ public:
ST_Other ST_Other
}; };
SymbolRef() = default;
SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) { SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
assert(isa<ObjectFile>(BasicSymbolRef::getObject())); assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
@ -179,8 +186,6 @@ public:
/// to create. /// to create.
class ObjectFile : public SymbolicFile { class ObjectFile : public SymbolicFile {
virtual void anchor(); virtual void anchor();
ObjectFile() = delete;
ObjectFile(const ObjectFile &other) = delete;
protected: protected:
ObjectFile(unsigned int Type, MemoryBufferRef Source); ObjectFile(unsigned int Type, MemoryBufferRef Source);
@ -198,6 +203,7 @@ protected:
// Implementations assume that the DataRefImpl is valid and has not been // Implementations assume that the DataRefImpl is valid and has not been
// modified externally. It's UB otherwise. // modified externally. It's UB otherwise.
friend class SymbolRef; friend class SymbolRef;
virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0; virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
std::error_code printSymbolName(raw_ostream &OS, std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override; DataRefImpl Symb) const override;
@ -211,6 +217,7 @@ protected:
// Same as above for SectionRef. // Same as above for SectionRef.
friend class SectionRef; friend class SectionRef;
virtual void moveSectionNext(DataRefImpl &Sec) const = 0; virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
virtual std::error_code getSectionName(DataRefImpl Sec, virtual std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const = 0; StringRef &Res) const = 0;
@ -242,12 +249,15 @@ protected:
uint64_t getSymbolValue(DataRefImpl Symb) const; uint64_t getSymbolValue(DataRefImpl Symb) const;
public: public:
ObjectFile() = delete;
ObjectFile(const ObjectFile &other) = delete;
uint64_t getCommonSymbolSize(DataRefImpl Symb) const { uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
assert(getSymbolFlags(Symb) & SymbolRef::SF_Common); assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
return getCommonSymbolSizeImpl(Symb); return getCommonSymbolSizeImpl(Symb);
} }
typedef iterator_range<symbol_iterator> symbol_iterator_range; using symbol_iterator_range = iterator_range<symbol_iterator>;
symbol_iterator_range symbols() const { symbol_iterator_range symbols() const {
return symbol_iterator_range(symbol_begin(), symbol_end()); return symbol_iterator_range(symbol_begin(), symbol_end());
} }
@ -255,7 +265,7 @@ public:
virtual section_iterator section_begin() const = 0; virtual section_iterator section_begin() const = 0;
virtual section_iterator section_end() const = 0; virtual section_iterator section_end() const = 0;
typedef iterator_range<section_iterator> section_iterator_range; using section_iterator_range = iterator_range<section_iterator>;
section_iterator_range sections() const { section_iterator_range sections() const {
return section_iterator_range(section_begin(), section_end()); return section_iterator_range(section_begin(), section_end());
} }
@ -297,7 +307,6 @@ public:
return createObjectFile(Object, sys::fs::file_magic::unknown); return createObjectFile(Object, sys::fs::file_magic::unknown);
} }
static inline bool classof(const Binary *v) { static inline bool classof(const Binary *v) {
return v->isObject(); return v->isObject();
} }
@ -354,7 +363,6 @@ inline const ObjectFile *SymbolRef::getObject() const {
return cast<ObjectFile>(O); return cast<ObjectFile>(O);
} }
/// SectionRef /// SectionRef
inline SectionRef::SectionRef(DataRefImpl SectionP, inline SectionRef::SectionRef(DataRefImpl SectionP,
const ObjectFile *Owner) const ObjectFile *Owner)
@ -479,8 +487,8 @@ inline const ObjectFile *RelocationRef::getObject() const {
return OwningObject; return OwningObject;
} }
} // end namespace object } // end namespace object
} // end namespace llvm } // end namespace llvm
#endif #endif // LLVM_OBJECT_OBJECTFILE_H

View File

@ -14,10 +14,19 @@
#ifndef LLVM_OBJECT_SYMBOLICFILE_H #ifndef LLVM_OBJECT_SYMBOLICFILE_H
#define LLVM_OBJECT_SYMBOLICFILE_H #define LLVM_OBJECT_SYMBOLICFILE_H
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/Binary.h" #include "llvm/Object/Binary.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h" #include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cinttypes> #include <cinttypes>
#include <utility> #include <cstdint>
#include <cstring>
#include <iterator>
#include <memory>
#include <system_error>
namespace llvm { namespace llvm {
namespace object { namespace object {
@ -29,6 +38,7 @@ union DataRefImpl {
uint32_t a, b; uint32_t a, b;
} d; } d;
uintptr_t p; uintptr_t p;
DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); } DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
}; };
@ -87,7 +97,7 @@ class SymbolicFile;
/// symbols in the object file. /// symbols in the object file.
class BasicSymbolRef { class BasicSymbolRef {
DataRefImpl SymbolPimpl; DataRefImpl SymbolPimpl;
const SymbolicFile *OwningObject; const SymbolicFile *OwningObject = nullptr;
public: public:
enum Flags : unsigned { enum Flags : unsigned {
@ -108,7 +118,7 @@ public:
// (IR only) // (IR only)
}; };
BasicSymbolRef() : OwningObject(nullptr) { } BasicSymbolRef() = default;
BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner); BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
bool operator==(const BasicSymbolRef &Other) const; bool operator==(const BasicSymbolRef &Other) const;
@ -125,12 +135,12 @@ public:
const SymbolicFile *getObject() const; const SymbolicFile *getObject() const;
}; };
typedef content_iterator<BasicSymbolRef> basic_symbol_iterator; using basic_symbol_iterator = content_iterator<BasicSymbolRef>;
class SymbolicFile : public Binary { class SymbolicFile : public Binary {
public: public:
~SymbolicFile() override;
SymbolicFile(unsigned int Type, MemoryBufferRef Source); SymbolicFile(unsigned int Type, MemoryBufferRef Source);
~SymbolicFile() override;
// virtual interface. // virtual interface.
virtual void moveSymbolNext(DataRefImpl &Symb) const = 0; virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
@ -145,7 +155,7 @@ public:
virtual basic_symbol_iterator symbol_end() const = 0; virtual basic_symbol_iterator symbol_end() const = 0;
// convenience wrappers. // convenience wrappers.
typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range; using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
basic_symbol_iterator_range symbols() const { basic_symbol_iterator_range symbols() const {
return basic_symbol_iterator_range(symbol_begin(), symbol_end()); return basic_symbol_iterator_range(symbol_begin(), symbol_end());
} }
@ -199,7 +209,7 @@ inline const SymbolicFile *BasicSymbolRef::getObject() const {
return OwningObject; return OwningObject;
} }
} } // end namespace object
} } // end namespace llvm
#endif #endif // LLVM_OBJECT_SYMBOLICFILE_H

View File

@ -1,4 +1,4 @@
//===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===// //===- Archive.cpp - ar File Format implementation ------------------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -11,12 +11,29 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Object/Archive.h" #include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/Endian.h" #include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <system_error>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
@ -25,7 +42,7 @@ using namespace llvm::support::endian;
static const char *const Magic = "!<arch>\n"; static const char *const Magic = "!<arch>\n";
static const char *const ThinMagic = "!<thin>\n"; static const char *const ThinMagic = "!<thin>\n";
void Archive::anchor() { } void Archive::anchor() {}
static Error static Error
malformedError(Twine Msg) { malformedError(Twine Msg) {
@ -61,8 +78,8 @@ ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
if (Err) { if (Err) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(llvm::StringRef(ArMemHdr->Terminator, OS.write_escaped(StringRef(ArMemHdr->Terminator,
sizeof(ArMemHdr->Terminator))); sizeof(ArMemHdr->Terminator)));
OS.flush(); OS.flush();
std::string Msg("terminator characters in archive member \"" + Buf + std::string Msg("terminator characters in archive member \"" + Buf +
"\" not the correct \"`\\n\" values for the archive " "\" not the correct \"`\\n\" values for the archive "
@ -97,13 +114,13 @@ Expected<StringRef> ArchiveMemberHeader::getRawName() const {
EndCond = ' '; EndCond = ' ';
else else
EndCond = '/'; EndCond = '/';
llvm::StringRef::size_type end = StringRef::size_type end =
llvm::StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond); StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
if (end == llvm::StringRef::npos) if (end == StringRef::npos)
end = sizeof(ArMemHdr->Name); end = sizeof(ArMemHdr->Name);
assert(end <= sizeof(ArMemHdr->Name) && end > 0); assert(end <= sizeof(ArMemHdr->Name) && end > 0);
// Don't include the EndCond if there is one. // Don't include the EndCond if there is one.
return llvm::StringRef(ArMemHdr->Name, end); return StringRef(ArMemHdr->Name, end);
} }
// This gets the name looking up long names. Size is the size of the archive // This gets the name looking up long names. Size is the size of the archive
@ -205,12 +222,12 @@ Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
Expected<uint32_t> ArchiveMemberHeader::getSize() const { Expected<uint32_t> ArchiveMemberHeader::getSize() const {
uint32_t Ret; uint32_t Ret;
if (llvm::StringRef(ArMemHdr->Size, if (StringRef(ArMemHdr->Size,
sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) { sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(llvm::StringRef(ArMemHdr->Size, OS.write_escaped(StringRef(ArMemHdr->Size,
sizeof(ArMemHdr->Size)).rtrim(" ")); sizeof(ArMemHdr->Size)).rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
Parent->getData().data(); Parent->getData().data();
@ -227,8 +244,8 @@ Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) { sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(llvm::StringRef(ArMemHdr->AccessMode, OS.write_escaped(StringRef(ArMemHdr->AccessMode,
sizeof(ArMemHdr->AccessMode)).rtrim(" ")); sizeof(ArMemHdr->AccessMode)).rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
Parent->getData().data(); Parent->getData().data();
@ -247,8 +264,8 @@ ArchiveMemberHeader::getLastModified() const {
.getAsInteger(10, Seconds)) { .getAsInteger(10, Seconds)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(llvm::StringRef(ArMemHdr->LastModified, OS.write_escaped(StringRef(ArMemHdr->LastModified,
sizeof(ArMemHdr->LastModified)).rtrim(" ")); sizeof(ArMemHdr->LastModified)).rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
Parent->getData().data(); Parent->getData().data();

View File

@ -1,4 +1,4 @@
//===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===// //===- Binary.cpp - A generic binary file ---------------------------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -11,21 +11,25 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Object/Binary.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
// Include headers for createBinary.
#include "llvm/Object/Archive.h" #include "llvm/Object/Archive.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachOUniversal.h" #include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <memory>
#include <system_error>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
Binary::~Binary() {} Binary::~Binary() = default;
Binary::Binary(unsigned int Type, MemoryBufferRef Source) Binary::Binary(unsigned int Type, MemoryBufferRef Source)
: TypeID(Type), Data(Source) {} : TypeID(Type), Data(Source) {}

View File

@ -1,4 +1,4 @@
//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===// //===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -11,16 +11,28 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Object/COFF.h"
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/ADT/iterator_range.h" #include "llvm/ADT/iterator_range.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/COFF.h" #include "llvm/Support/COFF.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/Error.h"
#include <cctype> #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits> #include <limits>
#include <memory>
#include <system_error>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
@ -116,7 +128,7 @@ const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
# ifndef NDEBUG #ifndef NDEBUG
// Verify that the section points to a valid entry in the section table. // Verify that the section points to a valid entry in the section table.
if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
report_fatal_error("Section was outside of section table."); report_fatal_error("Section was outside of section table.");
@ -124,7 +136,7 @@ const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
assert(Offset % sizeof(coff_section) == 0 && assert(Offset % sizeof(coff_section) == 0 &&
"Section did not point to the beginning of a section"); "Section did not point to the beginning of a section");
# endif #endif
return Addr; return Addr;
} }
@ -985,7 +997,7 @@ COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
if (Symbol.getNumberOfAuxSymbols() > 0) { if (Symbol.getNumberOfAuxSymbols() > 0) {
// AUX data comes immediately after the symbol in COFF // AUX data comes immediately after the symbol in COFF
Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
# ifndef NDEBUG #ifndef NDEBUG
// Verify that the Aux symbol points to a valid entry in the symbol table. // Verify that the Aux symbol points to a valid entry in the symbol table.
uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
if (Offset < getPointerToSymbolTable() || if (Offset < getPointerToSymbolTable() ||
@ -995,7 +1007,7 @@ COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
"Aux Symbol data did not point to the beginning of a symbol"); "Aux Symbol data did not point to the beginning of a symbol");
# endif #endif
} }
return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
} }

View File

@ -1,4 +1,4 @@
//===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===// //===- ObjectFile.cpp - File format independent object file ---------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -11,20 +11,28 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Object/ObjectFile.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/COFF.h" #include "llvm/Object/COFF.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachO.h" #include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/Wasm.h" #include "llvm/Object/Wasm.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <system_error> #include <system_error>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
void ObjectFile::anchor() { } void ObjectFile::anchor() {}
ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
: SymbolicFile(Type, Source) {} : SymbolicFile(Type, Source) {}

View File

@ -1,4 +1,4 @@
//===- SymbolicFile.cpp - Interface that only provides symbols --*- C++ -*-===// //===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -11,12 +11,20 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Object/COFF.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Object/COFFImportFile.h" #include "llvm/Object/COFFImportFile.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/IRObjectFile.h" #include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolicFile.h" #include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <memory>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
@ -24,7 +32,7 @@ using namespace object;
SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source) SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
: Binary(Type, Source) {} : Binary(Type, Source) {}
SymbolicFile::~SymbolicFile() {} SymbolicFile::~SymbolicFile() = default;
Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile( Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) { MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {