diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp --- a/lld/COFF/Chunks.cpp +++ b/lld/COFF/Chunks.cpp @@ -586,7 +586,7 @@ ArrayRef SectionChunk::getContents() const { ArrayRef A; - File->getCOFFObj()->getSectionContents(Header, A); + cantFail(File->getCOFFObj()->getSectionContents(Header, A)); return A; } diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -863,7 +863,7 @@ ArrayRef Syms = Obj->getSymbols(); if (Obj->AddrsigSec) { ArrayRef Contents; - Obj->getCOFFObj()->getSectionContents(Obj->AddrsigSec, Contents); + cantFail(Obj->getCOFFObj()->getSectionContents(Obj->AddrsigSec, Contents)); const uint8_t *Cur = Contents.begin(); while (Cur != Contents.end()) { unsigned Size; diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h --- a/llvm/include/llvm/Object/COFF.h +++ b/llvm/include/llvm/Object/COFF.h @@ -901,8 +901,8 @@ 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> + 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 @@ Expected getSectionName(const coff_section *Sec) const; uint64_t getSectionSize(const coff_section *Sec) const; - std::error_code getSectionContents(const coff_section *Sec, - ArrayRef &Res) const; + Error getSectionContents(const coff_section *Sec, + ArrayRef &Res) const; uint64_t getImageBase() const; std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const; diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -264,8 +264,8 @@ 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> + 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 @@ } template -std::error_code -ELFObjectFile::getSectionContents(DataRefImpl Sec, - StringRef &Result) const { +Expected> +ELFObjectFile::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 diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -297,8 +297,8 @@ 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> + getSectionContents(DataRefImpl Sec) const override; uint64_t getSectionAlignment(DataRefImpl Sec) const override; Expected getSection(unsigned SectionIndex) const; Expected getSection(StringRef SectionName) const; diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h --- a/llvm/include/llvm/Object/ObjectFile.h +++ b/llvm/include/llvm/Object/ObjectFile.h @@ -262,8 +262,8 @@ 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> + 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 std::error_code SectionRef::getContents(StringRef &Result) const { - return OwningObject->getSectionContents(SectionPimpl, Result); + Expected> Res = + OwningObject->getSectionContents(SectionPimpl); + if (!Res) + return errorToErrorCode(Res.takeError()); + Result = StringRef(reinterpret_cast(Res->data()), Res->size()); + return std::error_code(); } inline uint64_t SectionRef::getAlignment() const { diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -175,8 +175,8 @@ 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> + getSectionContents(DataRefImpl Sec) const override; uint64_t getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h --- a/llvm/include/llvm/Object/XCOFFObjectFile.h +++ b/llvm/include/llvm/Object/XCOFFObjectFile.h @@ -90,8 +90,8 @@ 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> + getSectionContents(DataRefImpl Sec) const override; uint64_t getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -292,13 +292,13 @@ return getSectionSize(toSec(Ref)); } -std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, - StringRef &Result) const { +Expected> +COFFObjectFile::getSectionContents(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); ArrayRef Res; - std::error_code EC = getSectionContents(Sec, Res); - Result = StringRef(reinterpret_cast(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 @@ return Sec->SizeOfRawData; } -std::error_code -COFFObjectFile::getSectionContents(const coff_section *Sec, - ArrayRef &Res) const { +Error COFFObjectFile::getSectionContents(const coff_section *Sec, + ArrayRef &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(); Res = makeArrayRef(reinterpret_cast(ConStart), SectionSize); - return std::error_code(); + return Error::success(); } const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -1907,8 +1907,8 @@ return SectSize; } -std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec, - StringRef &Res) const { +Expected> +MachOObjectFile::getSectionContents(DataRefImpl Sec) const { uint32_t Offset; uint64_t Size; @@ -1922,8 +1922,7 @@ 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 { diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1427,14 +1427,11 @@ return S.Content.size(); } -std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, - StringRef &Res) const { +Expected> 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(S.Content.data()), - S.Content.size()); - return std::error_code(); + return S.Content; } uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { diff --git a/llvm/lib/Object/XCOFFObjectFile.cpp b/llvm/lib/Object/XCOFFObjectFile.cpp --- a/llvm/lib/Object/XCOFFObjectFile.cpp +++ b/llvm/lib/Object/XCOFFObjectFile.cpp @@ -137,10 +137,9 @@ return toSection(Sec)->SectionSize; } -std::error_code XCOFFObjectFile::getSectionContents(DataRefImpl Sec, - StringRef &Res) const { +Expected> +XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const { llvm_unreachable("Not yet implemented!"); - return std::error_code(); } uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { diff --git a/llvm/tools/llvm-objcopy/COFF/Reader.cpp b/llvm/tools/llvm-objcopy/COFF/Reader.cpp --- a/llvm/tools/llvm-objcopy/COFF/Reader.cpp +++ b/llvm/tools/llvm-objcopy/COFF/Reader.cpp @@ -69,8 +69,8 @@ Section &S = Sections.back(); S.Header = *Sec; ArrayRef Contents; - if (auto EC = COFFObj.getSectionContents(Sec, Contents)) - return errorCodeToError(EC); + if (Error E = COFFObj.getSectionContents(Sec, Contents)) + return E; S.setContentsRef(Contents); ArrayRef Relocs = COFFObj.getRelocations(Sec); for (const coff_relocation &R : Relocs) diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp --- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp +++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp @@ -85,11 +85,12 @@ 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> E = + MachOObj.getSectionContents(SecRef->getRawDataRefImpl())) + S.Content = + StringRef(reinterpret_cast(E->data()), E->size()); + else + reportError(MachOObj.getFileName(), E.takeError()); S.Relocations.reserve(S.NReloc); for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()), diff --git a/llvm/tools/llvm-objdump/COFFDump.cpp b/llvm/tools/llvm-objdump/COFFDump.cpp --- a/llvm/tools/llvm-objdump/COFFDump.cpp +++ b/llvm/tools/llvm-objdump/COFFDump.cpp @@ -198,9 +198,7 @@ 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