diff options
Diffstat (limited to 'include/llvm/Object/ObjectFile.h')
-rw-r--r-- | include/llvm/Object/ObjectFile.h | 90 |
1 files changed, 70 insertions, 20 deletions
diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 036c99cb6baf..483a3486bd72 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -1,9 +1,8 @@ //===- ObjectFile.h - File format independent object file -------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -14,6 +13,7 @@ #ifndef LLVM_OBJECT_OBJECTFILE_H #define LLVM_OBJECT_OBJECTFILE_H +#include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" #include "llvm/ADT/iterator_range.h" @@ -98,7 +98,7 @@ public: uint64_t getAddress() const; uint64_t getIndex() const; uint64_t getSize() const; - std::error_code getContents(StringRef &Result) const; + Expected<StringRef> getContents() const; /// Get the alignment of this section as the actual value (not log 2). uint64_t getAlignment() const; @@ -136,6 +136,30 @@ public: const ObjectFile *getObject() const; }; +struct SectionedAddress { + // TODO: constructors could be removed when C++14 would be adopted. + SectionedAddress() {} + SectionedAddress(uint64_t Addr, uint64_t SectIdx) + : Address(Addr), SectionIndex(SectIdx) {} + + const static uint64_t UndefSection = UINT64_MAX; + + uint64_t Address = 0; + uint64_t SectionIndex = UndefSection; +}; + +inline bool operator<(const SectionedAddress &LHS, + const SectionedAddress &RHS) { + return std::tie(LHS.SectionIndex, LHS.Address) < + std::tie(RHS.SectionIndex, RHS.Address); +} + +inline bool operator==(const SectionedAddress &LHS, + const SectionedAddress &RHS) { + return std::tie(LHS.SectionIndex, LHS.Address) == + std::tie(RHS.SectionIndex, RHS.Address); +} + /// This is a value type class that represents a single symbol in the list of /// symbols in the object file. class SymbolRef : public BasicSymbolRef { @@ -220,7 +244,7 @@ protected: friend class SymbolRef; virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0; - std::error_code printSymbolName(raw_ostream &OS, + Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override; virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0; virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0; @@ -234,13 +258,12 @@ protected: friend class SectionRef; virtual void moveSectionNext(DataRefImpl &Sec) const = 0; - virtual std::error_code getSectionName(DataRefImpl Sec, - StringRef &Res) const = 0; + virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0; virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0; virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0; virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; - virtual std::error_code getSectionContents(DataRefImpl Sec, - StringRef &Res) const = 0; + virtual Expected<ArrayRef<uint8_t>> + getSectionContents(DataRefImpl Sec) const = 0; virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0; virtual bool isSectionCompressed(DataRefImpl Sec) const = 0; virtual bool isSectionText(DataRefImpl Sec) const = 0; @@ -308,11 +331,6 @@ public: /// Create a triple from the data in this object file. Triple makeTriple() const; - virtual std::error_code - getBuildAttributes(ARMAttributeParser &Attributes) const { - return std::error_code(); - } - /// Maps a debug section name to a standard DWARF section name. virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; } @@ -341,6 +359,9 @@ public: createCOFFObjectFile(MemoryBufferRef Object); static Expected<std::unique_ptr<ObjectFile>> + createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType); + + static Expected<std::unique_ptr<ObjectFile>> createELFObjectFile(MemoryBufferRef Object); static Expected<std::unique_ptr<MachOObjectFile>> @@ -396,14 +417,16 @@ inline SectionRef::SectionRef(DataRefImpl SectionP, , OwningObject(Owner) {} inline bool SectionRef::operator==(const SectionRef &Other) const { - return SectionPimpl == Other.SectionPimpl; + return OwningObject == Other.OwningObject && + SectionPimpl == Other.SectionPimpl; } inline bool SectionRef::operator!=(const SectionRef &Other) const { - return SectionPimpl != Other.SectionPimpl; + return !(*this == Other); } inline bool SectionRef::operator<(const SectionRef &Other) const { + assert(OwningObject == Other.OwningObject); return SectionPimpl < Other.SectionPimpl; } @@ -412,7 +435,11 @@ inline void SectionRef::moveNext() { } inline std::error_code SectionRef::getName(StringRef &Result) const { - return OwningObject->getSectionName(SectionPimpl, Result); + Expected<StringRef> NameOrErr = OwningObject->getSectionName(SectionPimpl); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); + Result = *NameOrErr; + return std::error_code(); } inline uint64_t SectionRef::getAddress() const { @@ -427,8 +454,12 @@ inline uint64_t SectionRef::getSize() const { return OwningObject->getSectionSize(SectionPimpl); } -inline std::error_code SectionRef::getContents(StringRef &Result) const { - return OwningObject->getSectionContents(SectionPimpl, Result); +inline Expected<StringRef> SectionRef::getContents() const { + Expected<ArrayRef<uint8_t>> Res = + OwningObject->getSectionContents(SectionPimpl); + if (!Res) + return Res.takeError(); + return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size()); } inline uint64_t SectionRef::getAlignment() const { @@ -531,6 +562,25 @@ inline const ObjectFile *RelocationRef::getObject() const { } // end namespace object +template <> struct DenseMapInfo<object::SectionRef> { + static bool isEqual(const object::SectionRef &A, + const object::SectionRef &B) { + return A == B; + } + static object::SectionRef getEmptyKey() { + return object::SectionRef({}, nullptr); + } + static object::SectionRef getTombstoneKey() { + object::DataRefImpl TS; + TS.p = (uintptr_t)-1; + return object::SectionRef(TS, nullptr); + } + static unsigned getHashValue(const object::SectionRef &Sec) { + object::DataRefImpl Raw = Sec.getRawDataRefImpl(); + return hash_combine(Raw.p, Raw.d.a, Raw.d.b); + } +}; + } // end namespace llvm #endif // LLVM_OBJECT_OBJECTFILE_H |