[llvm] r360648 - [Object] Change ObjectFile::getSectionContents to return Expected<ArrayRef<uint8_t>>
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon May 13 21:22:51 PDT 2019
Author: maskray
Date: Mon May 13 21:22:51 2019
New Revision: 360648
URL: http://llvm.org/viewvc/llvm-project?rev=360648&view=rev
Log:
[Object] Change ObjectFile::getSectionContents to return Expected<ArrayRef<uint8_t>>
Change
std::error_code getSectionContents(DataRefImpl, StringRef &) const;
to
Expected<ArrayRef<uint8_t>> getSectionContents(DataRefImpl) const;
Many object formats use ArrayRef<uint8_t> as the underlying type, which
is generally better than StringRef to represent binary data, so change
the type to decrease the number of type conversions.
Reviewed By: ruiu, sbc100
Differential Revision: https://reviews.llvm.org/D61781
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/include/llvm/Object/ELFObjectFile.h
llvm/trunk/include/llvm/Object/MachO.h
llvm/trunk/include/llvm/Object/ObjectFile.h
llvm/trunk/include/llvm/Object/Wasm.h
llvm/trunk/include/llvm/Object/XCOFFObjectFile.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
llvm/trunk/lib/Object/MachOObjectFile.cpp
llvm/trunk/lib/Object/WasmObjectFile.cpp
llvm/trunk/lib/Object/XCOFFObjectFile.cpp
llvm/trunk/tools/llvm-objcopy/COFF/Reader.cpp
llvm/trunk/tools/llvm-objcopy/MachO/MachOReader.cpp
llvm/trunk/tools/llvm-objdump/COFFDump.cpp
llvm/trunk/tools/obj2yaml/coff2yaml.cpp
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Mon May 13 21:22:51 2019
@@ -901,8 +901,8 @@ protected:
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
- std::error_code getSectionContents(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<ArrayRef<uint8_t>>
+ getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
@@ -1034,8 +1034,8 @@ public:
Expected<StringRef> getSectionName(const coff_section *Sec) const;
uint64_t getSectionSize(const coff_section *Sec) const;
- std::error_code getSectionContents(const coff_section *Sec,
- ArrayRef<uint8_t> &Res) const;
+ Error getSectionContents(const coff_section *Sec,
+ ArrayRef<uint8_t> &Res) const;
uint64_t getImageBase() const;
std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Mon May 13 21:22:51 2019
@@ -264,8 +264,8 @@ protected:
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
- std::error_code getSectionContents(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<ArrayRef<uint8_t>>
+ getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
@@ -701,16 +701,15 @@ uint64_t ELFObjectFile<ELFT>::getSection
}
template <class ELFT>
-std::error_code
-ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
- StringRef &Result) const {
+Expected<ArrayRef<uint8_t>>
+ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
const Elf_Shdr *EShdr = getSection(Sec);
if (std::error_code EC =
checkOffset(getMemoryBufferRef(),
(uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
- return EC;
- Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
- return std::error_code();
+ return errorCodeToError(EC);
+ return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
+ EShdr->sh_size);
}
template <class ELFT>
Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Mon May 13 21:22:51 2019
@@ -297,8 +297,8 @@ public:
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
- std::error_code getSectionContents(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<ArrayRef<uint8_t>>
+ getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
Expected<SectionRef> getSection(unsigned SectionIndex) const;
Expected<SectionRef> getSection(StringRef SectionName) const;
Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Mon May 13 21:22:51 2019
@@ -262,8 +262,8 @@ protected:
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;
@@ -455,7 +455,12 @@ inline uint64_t SectionRef::getSize() co
}
inline std::error_code SectionRef::getContents(StringRef &Result) const {
- return OwningObject->getSectionContents(SectionPimpl, Result);
+ Expected<ArrayRef<uint8_t>> Res =
+ OwningObject->getSectionContents(SectionPimpl);
+ if (!Res)
+ return errorToErrorCode(Res.takeError());
+ Result = StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
+ return std::error_code();
}
inline uint64_t SectionRef::getAlignment() const {
Modified: llvm/trunk/include/llvm/Object/Wasm.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Wasm.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Wasm.h (original)
+++ llvm/trunk/include/llvm/Object/Wasm.h Mon May 13 21:22:51 2019
@@ -175,8 +175,8 @@ public:
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
- std::error_code getSectionContents(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<ArrayRef<uint8_t>>
+ getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
Modified: llvm/trunk/include/llvm/Object/XCOFFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/XCOFFObjectFile.h?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/XCOFFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/XCOFFObjectFile.h Mon May 13 21:22:51 2019
@@ -90,8 +90,8 @@ public:
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
- std::error_code getSectionContents(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<ArrayRef<uint8_t>>
+ getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Mon May 13 21:22:51 2019
@@ -292,13 +292,13 @@ uint64_t COFFObjectFile::getSectionSize(
return getSectionSize(toSec(Ref));
}
-std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
- StringRef &Result) const {
+Expected<ArrayRef<uint8_t>>
+COFFObjectFile::getSectionContents(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref);
ArrayRef<uint8_t> Res;
- std::error_code EC = getSectionContents(Sec, Res);
- Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
- return EC;
+ if (Error E = getSectionContents(Sec, Res))
+ return std::move(E);
+ return Res;
}
uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
@@ -1118,22 +1118,21 @@ uint64_t COFFObjectFile::getSectionSize(
return Sec->SizeOfRawData;
}
-std::error_code
-COFFObjectFile::getSectionContents(const coff_section *Sec,
- ArrayRef<uint8_t> &Res) const {
+Error COFFObjectFile::getSectionContents(const coff_section *Sec,
+ ArrayRef<uint8_t> &Res) const {
// In COFF, a virtual section won't have any in-file
// content, so the file pointer to the content will be zero.
if (Sec->PointerToRawData == 0)
- return std::error_code();
+ return Error::success();
// The only thing that we need to verify is that the contents is contained
// within the file bounds. We don't need to make sure it doesn't cover other
// data, as there's nothing that says that is not allowed.
uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
uint32_t SectionSize = getSectionSize(Sec);
if (checkOffset(Data, ConStart, SectionSize))
- return object_error::parse_failed;
+ return make_error<BinaryError>();
Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
- return std::error_code();
+ return Error::success();
}
const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Mon May 13 21:22:51 2019
@@ -1907,8 +1907,8 @@ uint64_t MachOObjectFile::getSectionSize
return SectSize;
}
-std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
- StringRef &Res) const {
+Expected<ArrayRef<uint8_t>>
+MachOObjectFile::getSectionContents(DataRefImpl Sec) const {
uint32_t Offset;
uint64_t Size;
@@ -1922,8 +1922,7 @@ std::error_code MachOObjectFile::getSect
Size = Sect.size;
}
- Res = this->getData().substr(Offset, Size);
- return std::error_code();
+ return arrayRefFromStringRef(getData().substr(Offset, Size));
}
uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
Modified: llvm/trunk/lib/Object/WasmObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/WasmObjectFile.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/lib/Object/WasmObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/WasmObjectFile.cpp Mon May 13 21:22:51 2019
@@ -1427,14 +1427,12 @@ uint64_t WasmObjectFile::getSectionSize(
return S.Content.size();
}
-std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
- StringRef &Res) const {
+Expected<ArrayRef<uint8_t>>
+WasmObjectFile::getSectionContents(DataRefImpl Sec) const {
const WasmSection &S = Sections[Sec.d.a];
// This will never fail since wasm sections can never be empty (user-sections
// must have a name and non-user sections each have a defined structure).
- Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
- S.Content.size());
- return std::error_code();
+ return S.Content;
}
uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
Modified: llvm/trunk/lib/Object/XCOFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/XCOFFObjectFile.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/lib/Object/XCOFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/XCOFFObjectFile.cpp Mon May 13 21:22:51 2019
@@ -137,10 +137,9 @@ uint64_t XCOFFObjectFile::getSectionSize
return toSection(Sec)->SectionSize;
}
-std::error_code XCOFFObjectFile::getSectionContents(DataRefImpl Sec,
- StringRef &Res) const {
+Expected<ArrayRef<uint8_t>>
+XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
llvm_unreachable("Not yet implemented!");
- return std::error_code();
}
uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
Modified: llvm/trunk/tools/llvm-objcopy/COFF/Reader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/COFF/Reader.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/COFF/Reader.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/COFF/Reader.cpp Mon May 13 21:22:51 2019
@@ -69,8 +69,8 @@ Error COFFReader::readSections(Object &O
Section &S = Sections.back();
S.Header = *Sec;
ArrayRef<uint8_t> Contents;
- if (auto EC = COFFObj.getSectionContents(Sec, Contents))
- return errorCodeToError(EC);
+ if (Error E = COFFObj.getSectionContents(Sec, Contents))
+ return E;
S.setContentsRef(Contents);
ArrayRef<coff_relocation> Relocs = COFFObj.getRelocations(Sec);
for (const coff_relocation &R : Relocs)
Modified: llvm/trunk/tools/llvm-objcopy/MachO/MachOReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/MachO/MachOReader.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/MachO/MachOReader.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/MachO/MachOReader.cpp Mon May 13 21:22:51 2019
@@ -85,11 +85,12 @@ extractSections(const object::MachOObjec
if (!SecRef)
reportError(MachOObj.getFileName(), SecRef.takeError());
- StringRef Content;
- if (auto EC =
- MachOObj.getSectionContents(SecRef->getRawDataRefImpl(), Content))
- reportError(MachOObj.getFileName(), std::move(EC));
- S.Content = Content;
+ if (Expected<ArrayRef<uint8_t>> E =
+ MachOObj.getSectionContents(SecRef->getRawDataRefImpl()))
+ S.Content =
+ StringRef(reinterpret_cast<const char *>(E->data()), E->size());
+ else
+ reportError(MachOObj.getFileName(), E.takeError());
S.Relocations.reserve(S.NReloc);
for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
Modified: llvm/trunk/tools/llvm-objdump/COFFDump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/COFFDump.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/COFFDump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/COFFDump.cpp Mon May 13 21:22:51 2019
@@ -198,9 +198,7 @@ getSectionContents(const COFFObjectFile
const coff_section *Section;
if (Error E = resolveSectionAndAddress(Obj, Sym, Section, Addr))
return E;
- if (std::error_code EC = Obj->getSectionContents(Section, Contents))
- return errorCodeToError(EC);
- return Error::success();
+ return Obj->getSectionContents(Section, Contents);
}
// Given a vector of relocations for a section and an offset into this section
Modified: llvm/trunk/tools/obj2yaml/coff2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/coff2yaml.cpp?rev=360648&r1=360647&r2=360648&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/coff2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/coff2yaml.cpp Mon May 13 21:22:51 2019
@@ -120,7 +120,7 @@ initializeFileAndStringTable(const llvm:
const object::coff_section *COFFSection = Obj.getCOFFSection(S);
- Obj.getSectionContents(COFFSection, sectionData);
+ cantFail(Obj.getSectionContents(COFFSection, sectionData));
BinaryStreamReader Reader(sectionData, support::little);
uint32_t Magic;
@@ -175,7 +175,7 @@ void COFFDumper::dumpSections(unsigned N
ArrayRef<uint8_t> sectionData;
if (!ObjSection.isBSS())
- Obj.getSectionContents(COFFSection, sectionData);
+ cantFail(Obj.getSectionContents(COFFSection, sectionData));
NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
if (NewYAMLSection.Name == ".debug$S")
More information about the llvm-commits
mailing list