[llvm] r360892 - Recommit [Object] Change object::SectionRef::getContents() to return Expected<StringRef>
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu May 16 06:24:04 PDT 2019
Author: maskray
Date: Thu May 16 06:24:04 2019
New Revision: 360892
URL: http://llvm.org/viewvc/llvm-project?rev=360892&view=rev
Log:
Recommit [Object] Change object::SectionRef::getContents() to return Expected<StringRef>
r360876 didn't fix 2 call sites in clang.
Expected<ArrayRef<uint8_t>> may be better but use Expected<StringRef> for now.
Follow-up of D61781.
Modified:
llvm/trunk/include/llvm/Object/ObjectFile.h
llvm/trunk/lib/DebugInfo/DWARF/DWARFContext.cpp
llvm/trunk/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp
llvm/trunk/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp
llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
llvm/trunk/lib/Object/ELFObjectFile.cpp
llvm/trunk/lib/Object/IRObjectFile.cpp
llvm/trunk/lib/Object/Object.cpp
llvm/trunk/lib/ProfileData/Coverage/CoverageMappingReader.cpp
llvm/trunk/lib/XRay/InstrumentationMap.cpp
llvm/trunk/tools/dsymutil/DwarfLinker.cpp
llvm/trunk/tools/dsymutil/DwarfStreamer.cpp
llvm/trunk/tools/llvm-cfi-verify/lib/FileAnalysis.cpp
llvm/trunk/tools/llvm-cov/TestingSupport.cpp
llvm/trunk/tools/llvm-cxxdump/llvm-cxxdump.cpp
llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp
llvm/trunk/tools/llvm-objdump/MachODump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
llvm/trunk/tools/llvm-pdbutil/DumpOutputStyle.cpp
llvm/trunk/tools/llvm-pdbutil/InputFile.cpp
llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
llvm/trunk/tools/llvm-readobj/MachODumper.cpp
llvm/trunk/tools/llvm-readobj/ObjDumper.cpp
llvm/trunk/tools/sancov/sancov.cpp
Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Thu May 16 06:24:04 2019
@@ -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;
@@ -454,13 +454,12 @@ inline uint64_t SectionRef::getSize() co
return OwningObject->getSectionSize(SectionPimpl);
}
-inline std::error_code SectionRef::getContents(StringRef &Result) const {
+inline Expected<StringRef> SectionRef::getContents() const {
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();
+ return Res.takeError();
+ return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
}
inline uint64_t SectionRef::getAlignment() const {
Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFContext.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFContext.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFContext.cpp Thu May 16 06:24:04 2019
@@ -1410,8 +1410,14 @@ public:
// Try to obtain an already relocated version of this section.
// Else use the unrelocated section from the object file. We'll have to
// apply relocations ourselves later.
- if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
- Section.getContents(Data);
+ if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
+ Expected<StringRef> E = Section.getContents();
+ if (E)
+ Data = *E;
+ else
+ // maybeDecompress below will error.
+ consumeError(E.takeError());
+ }
if (auto Err = maybeDecompress(Section, Name, Data)) {
ErrorPolicy EP = HandleError(createError(
Modified: llvm/trunk/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp (original)
+++ llvm/trunk/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp Thu May 16 06:24:04 2019
@@ -53,13 +53,13 @@ SymbolizableObjectFile::create(object::O
if (Obj->getArch() == Triple::ppc64) {
for (section_iterator Section : Obj->sections()) {
StringRef Name;
- StringRef Data;
if (auto EC = Section->getName(Name))
return EC;
if (Name == ".opd") {
- if (auto EC = Section->getContents(Data))
- return EC;
- OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
+ Expected<StringRef> E = Section->getContents();
+ if (!E)
+ return errorToErrorCode(E.takeError());
+ OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
Obj->getBytesInAddress()));
OpdAddress = Section->getAddress();
break;
Modified: llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp (original)
+++ llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp Thu May 16 06:24:04 2019
@@ -221,9 +221,12 @@ bool getGNUDebuglinkContents(const Objec
Section.getName(Name);
Name = Name.substr(Name.find_first_not_of("._"));
if (Name == "gnu_debuglink") {
- StringRef Data;
- Section.getContents(Data);
- DataExtractor DE(Data, Obj->isLittleEndian(), 0);
+ Expected<StringRef> ContentsOrErr = Section.getContents();
+ if (!ContentsOrErr) {
+ consumeError(ContentsOrErr.takeError());
+ return false;
+ }
+ DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
uint32_t Offset = 0;
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
// 4-byte align the offset.
Modified: llvm/trunk/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp Thu May 16 06:24:04 2019
@@ -136,14 +136,14 @@ Error MachOAtomGraphBuilder::parseSectio
if (!SecRef.isVirtual()) {
// If this section has content then record it.
- StringRef Content;
- if (auto EC = SecRef.getContents(Content))
- return errorCodeToError(EC);
- if (Content.size() != SecRef.getSize())
+ Expected<StringRef> Content = SecRef.getContents();
+ if (!Content)
+ return Content.takeError();
+ if (Content->size() != SecRef.getSize())
return make_error<JITLinkError>("Section content size does not match "
"declared size for " +
Name);
- MachOSec.setContent(Content);
+ MachOSec.setContent(*Content);
} else {
// If this is a zero-fill section then just record the size.
MachOSec.setZeroFill(SecRef.getSize());
Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp Thu May 16 06:24:04 2019
@@ -792,8 +792,10 @@ RuntimeDyldImpl::emitSection(const Objec
if (!IsVirtual && !IsZeroInit) {
// In either case, set the location of the unrelocated section in memory,
// since we still process relocations for it even if we're not applying them.
- if (auto EC = Section.getContents(data))
- return errorCodeToError(EC);
+ if (Expected<StringRef> E = Section.getContents())
+ data = *E;
+ else
+ return E.takeError();
pData = data.data();
}
Modified: llvm/trunk/lib/Object/ELFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/ELFObjectFile.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/Object/ELFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/ELFObjectFile.cpp Thu May 16 06:24:04 2019
@@ -377,12 +377,13 @@ ELFObjectFileBase::getPltAddresses() con
}
if (!Plt || !RelaPlt || !GotPlt)
return {};
- StringRef PltContents;
- if (Plt->getContents(PltContents))
+ Expected<StringRef> PltContents = Plt->getContents();
+ if (!PltContents) {
+ consumeError(PltContents.takeError());
return {};
- ArrayRef<uint8_t> PltBytes((const uint8_t *)PltContents.data(),
- Plt->getSize());
- auto PltEntries = MIA->findPltEntries(Plt->getAddress(), PltBytes,
+ }
+ auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
+ arrayRefFromStringRef(*PltContents),
GotPlt->getAddress(), Triple);
// Build a map from GOT entry virtual address to PLT entry virtual address.
DenseMap<uint64_t, uint64_t> GotToPlt;
Modified: llvm/trunk/lib/Object/IRObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/IRObjectFile.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/Object/IRObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/IRObjectFile.cpp Thu May 16 06:24:04 2019
@@ -74,12 +74,12 @@ Expected<MemoryBufferRef>
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
for (const SectionRef &Sec : Obj.sections()) {
if (Sec.isBitcode()) {
- StringRef SecContents;
- if (std::error_code EC = Sec.getContents(SecContents))
- return errorCodeToError(EC);
- if (SecContents.size() <= 1)
+ Expected<StringRef> Contents = Sec.getContents();
+ if (!Contents)
+ return Contents.takeError();
+ if (Contents->size() <= 1)
return errorCodeToError(object_error::bitcode_section_not_found);
- return MemoryBufferRef(SecContents, Obj.getFileName());
+ return MemoryBufferRef(*Contents, Obj.getFileName());
}
}
Modified: llvm/trunk/lib/Object/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Object.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Object.cpp (original)
+++ llvm/trunk/lib/Object/Object.cpp Thu May 16 06:24:04 2019
@@ -247,10 +247,10 @@ uint64_t LLVMGetSectionSize(LLVMSectionI
}
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
- StringRef ret;
- if (std::error_code ec = (*unwrap(SI))->getContents(ret))
- report_fatal_error(ec.message());
- return ret.data();
+ if (Expected<StringRef> E = (*unwrap(SI))->getContents())
+ return E->data();
+ else
+ report_fatal_error(E.takeError());
}
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
Modified: llvm/trunk/lib/ProfileData/Coverage/CoverageMappingReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/Coverage/CoverageMappingReader.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/Coverage/CoverageMappingReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/Coverage/CoverageMappingReader.cpp Thu May 16 06:24:04 2019
@@ -348,8 +348,10 @@ Expected<bool> RawCoverageMappingDummyCh
}
Error InstrProfSymtab::create(SectionRef &Section) {
- if (auto EC = Section.getContents(Data))
- return errorCodeToError(EC);
+ Expected<StringRef> DataOrErr = Section.getContents();
+ if (!DataOrErr)
+ return DataOrErr.takeError();
+ Data = *DataOrErr;
Address = Section.getAddress();
// If this is a linked PE/COFF file, then we have to skip over the null byte
@@ -687,8 +689,11 @@ static Error loadBinaryFormat(MemoryBuff
return E;
// Get the contents of the given sections.
- if (auto EC = CoverageSection->getContents(CoverageMapping))
- return errorCodeToError(EC);
+ if (Expected<StringRef> E = CoverageSection->getContents())
+ CoverageMapping = *E;
+ else
+ return E.takeError();
+
if (Error E = ProfileNames.create(*NamesSection))
return E;
Modified: llvm/trunk/lib/XRay/InstrumentationMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/XRay/InstrumentationMap.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/lib/XRay/InstrumentationMap.cpp (original)
+++ llvm/trunk/lib/XRay/InstrumentationMap.cpp Thu May 16 06:24:04 2019
@@ -78,9 +78,10 @@ loadObj(StringRef Filename, object::Owni
"Failed to find XRay instrumentation map.",
std::make_error_code(std::errc::executable_format_error));
- if (I->getContents(Contents))
- return errorCodeToError(
- std::make_error_code(std::errc::executable_format_error));
+ if (Expected<StringRef> E = I->getContents())
+ Contents = *E;
+ else
+ return E.takeError();
RelocMap Relocs;
if (ObjFile.getBinary()->isELF()) {
Modified: llvm/trunk/tools/dsymutil/DwarfLinker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfLinker.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfLinker.cpp (original)
+++ llvm/trunk/tools/dsymutil/DwarfLinker.cpp Thu May 16 06:24:04 2019
@@ -436,9 +436,13 @@ static bool isMachOPairedReloc(uint64_t
void DwarfLinker::RelocationManager::findValidRelocsMachO(
const object::SectionRef &Section, const object::MachOObjectFile &Obj,
const DebugMapObject &DMO) {
- StringRef Contents;
- Section.getContents(Contents);
- DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
+ Expected<StringRef> ContentsOrErr = Section.getContents();
+ if (!ContentsOrErr) {
+ consumeError(ContentsOrErr.takeError());
+ Linker.reportWarning("error reading section", DMO);
+ return;
+ }
+ DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
bool SkipNext = false;
for (const object::RelocationRef &Reloc : Section.relocations()) {
Modified: llvm/trunk/tools/dsymutil/DwarfStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfStreamer.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfStreamer.cpp (original)
+++ llvm/trunk/tools/dsymutil/DwarfStreamer.cpp Thu May 16 06:24:04 2019
@@ -667,10 +667,12 @@ void DwarfStreamer::translateLineTable(D
static void emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName, MCStreamer *MS) {
- StringRef Contents;
- if (auto Sec = getSectionByName(Obj, SecName))
- if (!Sec->getContents(Contents))
- MS->EmitBytes(Contents);
+ if (auto Sec = getSectionByName(Obj, SecName)) {
+ if (Expected<StringRef> E = Sec->getContents())
+ MS->EmitBytes(*E);
+ else
+ consumeError(E.takeError());
+ }
}
void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {
Modified: llvm/trunk/tools/llvm-cfi-verify/lib/FileAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cfi-verify/lib/FileAnalysis.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cfi-verify/lib/FileAnalysis.cpp (original)
+++ llvm/trunk/tools/llvm-cfi-verify/lib/FileAnalysis.cpp Thu May 16 06:24:04 2019
@@ -453,13 +453,11 @@ Error FileAnalysis::parseCodeSections()
if (!Section.getName(SectionName) && SectionName == ".plt")
continue;
- StringRef SectionContents;
- if (Section.getContents(SectionContents))
- return make_error<StringError>("Failed to retrieve section contents",
- inconvertibleErrorCode());
+ Expected<StringRef> Contents = Section.getContents();
+ if (!Contents)
+ return Contents.takeError();
+ ArrayRef<uint8_t> SectionBytes = arrayRefFromStringRef(*Contents);
- ArrayRef<uint8_t> SectionBytes((const uint8_t *)SectionContents.data(),
- Section.getSize());
parseSectionContents(SectionBytes,
{Section.getAddress(), Section.getIndex()});
}
Modified: llvm/trunk/tools/llvm-cov/TestingSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/TestingSupport.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/TestingSupport.cpp (original)
+++ llvm/trunk/tools/llvm-cov/TestingSupport.cpp Thu May 16 06:24:04 2019
@@ -69,9 +69,18 @@ int convertForTestingMain(int argc, cons
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
StringRef CoverageMappingData;
StringRef ProfileNamesData;
- if (CoverageMapping.getContents(CoverageMappingData) ||
- ProfileNames.getContents(ProfileNamesData))
+ if (Expected<StringRef> E = CoverageMapping.getContents())
+ CoverageMappingData = *E;
+ else {
+ consumeError(E.takeError());
return 1;
+ }
+ if (Expected<StringRef> E = ProfileNames.getContents())
+ ProfileNamesData = *E;
+ else {
+ consumeError(E.takeError());
+ return 1;
+ }
int FD;
if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
Modified: llvm/trunk/tools/llvm-cxxdump/llvm-cxxdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cxxdump/llvm-cxxdump.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cxxdump/llvm-cxxdump.cpp (original)
+++ llvm/trunk/tools/llvm-cxxdump/llvm-cxxdump.cpp Thu May 16 06:24:04 2019
@@ -48,15 +48,20 @@ static void error(std::error_code EC) {
exit(1);
}
-static void error(Error Err) {
- if (!Err)
- return;
+LLVM_ATTRIBUTE_NORETURN static void error(Error Err) {
logAllUnhandledErrors(std::move(Err), WithColor::error(outs()),
"reading file: ");
outs().flush();
exit(1);
}
+template <typename T>
+T unwrapOrError(Expected<T> EO) {
+ if (!EO)
+ error(EO.takeError());
+ return std::move(*EO);
+}
+
} // namespace llvm
static void reportError(StringRef Input, StringRef Message) {
@@ -195,8 +200,7 @@ static void dumpCXXData(const ObjectFile
// Skip virtual or BSS sections.
if (Sec.isBSS() || Sec.isVirtual())
continue;
- StringRef SecContents;
- error(Sec.getContents(SecContents));
+ StringRef SecContents = unwrapOrError(Sec.getContents());
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
error(errorToErrorCode(SymAddressOrErr.takeError()));
uint64_t SymAddress = *SymAddressOrErr;
@@ -510,7 +514,8 @@ static void dumpArchive(const Archive *A
else
reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
}
- error(std::move(Err));
+ if (Err)
+ error(std::move(Err));
}
static void dumpInput(StringRef File) {
Modified: llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp (original)
+++ llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp Thu May 16 06:24:04 2019
@@ -410,9 +410,10 @@ static Error handleSection(
if (std::error_code Err = Section.getName(Name))
return errorCodeToError(Err);
- StringRef Contents;
- if (auto Err = Section.getContents(Contents))
- return errorCodeToError(Err);
+ Expected<StringRef> ContentsOrErr = Section.getContents();
+ if (!ContentsOrErr)
+ return ContentsOrErr.takeError();
+ StringRef Contents = *ContentsOrErr;
if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
return Err;
Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Thu May 16 06:24:04 2019
@@ -1482,8 +1482,8 @@ static void DumpLiteralPointerSection(Ma
section_type = Sec.flags & MachO::SECTION_TYPE;
}
- StringRef BytesStr;
- Sect->getContents(BytesStr);
+ StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
+
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
switch (section_type) {
@@ -1697,8 +1697,8 @@ static void DumpSectionContents(StringRe
}
uint32_t section_type = section_flags & MachO::SECTION_TYPE;
- StringRef BytesStr;
- Section.getContents(BytesStr);
+ StringRef BytesStr =
+ unwrapOrError(Section.getContents(), O->getFileName());
const char *sect = reinterpret_cast<const char *>(BytesStr.data());
uint32_t sect_size = BytesStr.size();
uint64_t sect_addr = Section.getAddress();
@@ -1782,8 +1782,8 @@ static void DumpInfoPlistSectionContents
if (SegName == "__TEXT" && SectName == "__info_plist") {
if (!NoLeadingHeaders)
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr;
- Section.getContents(BytesStr);
+ StringRef BytesStr =
+ unwrapOrError(Section.getContents(), O->getFileName());
const char *sect = reinterpret_cast<const char *>(BytesStr.data());
outs() << format("%.*s", BytesStr.size(), sect) << "\n";
return;
@@ -3194,8 +3194,8 @@ static const char *get_pointer_64(uint64
S = (*(info->Sections))[SectIdx];
offset = Address - SectAddress;
left = SectSize - offset;
- StringRef SectContents;
- ((*(info->Sections))[SectIdx]).getContents(SectContents);
+ StringRef SectContents = unwrapOrError(
+ ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
return SectContents.data() + offset;
}
}
@@ -3998,8 +3998,7 @@ walk_pointer_list_64(const char *listnam
StringRef SegName = O->getSectionFinalSegmentName(Ref);
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr;
- S.getContents(BytesStr);
+ StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
@@ -4049,8 +4048,7 @@ walk_pointer_list_32(const char *listnam
StringRef SegName = O->getSectionFinalSegmentName(Ref);
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr;
- S.getContents(BytesStr);
+ StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
@@ -7242,8 +7240,8 @@ static void DisassembleMachO(StringRef F
if (SegmentName != DisSegName)
continue;
- StringRef BytesStr;
- Sections[SectIdx].getContents(BytesStr);
+ StringRef BytesStr =
+ unwrapOrError(Sections[SectIdx].getContents(), Filename);
ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
uint64_t SectAddress = Sections[SectIdx].getAddress();
@@ -7696,9 +7694,8 @@ printMachOCompactUnwindSection(const Mac
uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
- StringRef Contents;
- CompactUnwind.getContents(Contents);
-
+ StringRef Contents =
+ unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
// First populate the initial raw offsets, encodings and so on from the entry.
@@ -7839,8 +7836,8 @@ static void printMachOUnwindInfoSection(
outs() << "Contents of __unwind_info section:\n";
- StringRef Contents;
- UnwindInfo.getContents(Contents);
+ StringRef Contents =
+ unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
ptrdiff_t Pos = 0;
//===----------------------------------
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Thu May 16 06:24:04 2019
@@ -1126,9 +1126,8 @@ static void disassembleObject(const Targ
SmallString<40> Comments;
raw_svector_ostream CommentStream(Comments);
- StringRef BytesStr;
- error(Section.getContents(BytesStr));
- ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
+ ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
+ unwrapOrError(Section.getContents(), Obj->getFileName()));
uint64_t VMAAdjustment = 0;
if (shouldAdjustVA(Section))
@@ -1561,7 +1560,6 @@ void printSectionHeaders(const ObjectFil
void printSectionContents(const ObjectFile *Obj) {
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
StringRef Name;
- StringRef Contents;
error(Section.getName(Name));
uint64_t BaseAddr = Section.getAddress();
uint64_t Size = Section.getSize();
@@ -1576,7 +1574,7 @@ void printSectionContents(const ObjectFi
continue;
}
- error(Section.getContents(Contents));
+ StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
// Dump out the content as hex and printable ascii characters.
for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
@@ -1764,8 +1762,8 @@ void printRawClangAST(const ObjectFile *
if (!ClangASTSection)
return;
- StringRef ClangASTContents;
- error(ClangASTSection.getValue().getContents(ClangASTContents));
+ StringRef ClangASTContents = unwrapOrError(
+ ClangASTSection.getValue().getContents(), Obj->getFileName());
outs().write(ClangASTContents.data(), ClangASTContents.size());
}
@@ -1801,9 +1799,8 @@ static void printFaultMaps(const ObjectF
return;
}
- StringRef FaultMapContents;
- error(FaultMapSection.getValue().getContents(FaultMapContents));
-
+ StringRef FaultMapContents =
+ unwrapOrError(FaultMapSection.getValue().getContents(), Obj->getFileName());
FaultMapParser FMP(FaultMapContents.bytes_begin(),
FaultMapContents.bytes_end());
Modified: llvm/trunk/tools/llvm-pdbutil/DumpOutputStyle.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-pdbutil/DumpOutputStyle.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-pdbutil/DumpOutputStyle.cpp (original)
+++ llvm/trunk/tools/llvm-pdbutil/DumpOutputStyle.cpp Thu May 16 06:24:04 2019
@@ -1376,12 +1376,12 @@ Error DumpOutputStyle::dumpTypesFromObje
else
continue;
- StringRef Contents;
- if (auto EC = S.getContents(Contents))
- return errorCodeToError(EC);
+ Expected<StringRef> ContentsOrErr = S.getContents();
+ if (!ContentsOrErr)
+ return ContentsOrErr.takeError();
uint32_t Magic;
- BinaryStreamReader Reader(Contents, llvm::support::little);
+ BinaryStreamReader Reader(*ContentsOrErr, llvm::support::little);
if (auto EC = Reader.readInteger(Magic))
return EC;
if (Magic != COFF::DEBUG_SECTION_MAGIC)
Modified: llvm/trunk/tools/llvm-pdbutil/InputFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-pdbutil/InputFile.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-pdbutil/InputFile.cpp (original)
+++ llvm/trunk/tools/llvm-pdbutil/InputFile.cpp Thu May 16 06:24:04 2019
@@ -66,17 +66,20 @@ getModuleDebugStream(PDBFile &File, Stri
static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
StringRef Name,
BinaryStreamReader &Reader) {
- StringRef SectionName, Contents;
+ StringRef SectionName;
if (Section.getName(SectionName))
return false;
if (SectionName != Name)
return false;
- if (Section.getContents(Contents))
+ Expected<StringRef> ContentsOrErr = Section.getContents();
+ if (!ContentsOrErr) {
+ consumeError(ContentsOrErr.takeError());
return false;
+ }
- Reader = BinaryStreamReader(Contents, support::little);
+ Reader = BinaryStreamReader(*ContentsOrErr, support::little);
uint32_t Magic;
if (Reader.bytesRemaining() < sizeof(uint32_t))
return false;
Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Thu May 16 06:24:04 2019
@@ -933,8 +933,7 @@ void COFFDumper::initializeFileAndString
void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
const SectionRef &Section) {
- StringRef SectionContents;
- error(Section.getContents(SectionContents));
+ StringRef SectionContents = unwrapOrError(Section.getContents());
StringRef Data = SectionContents;
SmallVector<StringRef, 10> FunctionNames;
@@ -1218,8 +1217,7 @@ void COFFDumper::mergeCodeViewTypes(Merg
StringRef SectionName;
error(S.getName(SectionName));
if (SectionName == ".debug$T") {
- StringRef Data;
- error(S.getContents(Data));
+ StringRef Data = unwrapOrError(S.getContents());
uint32_t Magic;
error(consume(Data, Magic));
if (Magic != 4)
@@ -1255,8 +1253,7 @@ void COFFDumper::printCodeViewTypeSectio
ListScope D(W, "CodeViewTypes");
W.printNumber("Section", SectionName, Obj->getSectionID(Section));
- StringRef Data;
- error(Section.getContents(Data));
+ StringRef Data = unwrapOrError(Section.getContents());
if (opts::CodeViewSubsectionBytes)
W.printBinaryBlock("Data", Data);
@@ -1316,9 +1313,7 @@ void COFFDumper::printSectionHeaders() {
if (opts::SectionData &&
!(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
- StringRef Data;
- error(Sec.getContents(Data));
-
+ StringRef Data = unwrapOrError(Sec.getContents());
W.printBinaryBlock("SectionData", Data);
}
}
@@ -1660,15 +1655,13 @@ void COFFDumper::printCOFFExports() {
void COFFDumper::printCOFFDirectives() {
for (const SectionRef &Section : Obj->sections()) {
- StringRef Contents;
StringRef Name;
error(Section.getName(Name));
if (Name != ".drectve")
continue;
- error(Section.getContents(Contents));
-
+ StringRef Contents = unwrapOrError(Section.getContents());
W.printString("Directive(s)", Contents);
}
}
@@ -1707,8 +1700,7 @@ void COFFDumper::printCOFFResources() {
if (!Name.startswith(".rsrc"))
continue;
- StringRef Ref;
- error(S.getContents(Ref));
+ StringRef Ref = unwrapOrError(S.getContents());
if ((Name == ".rsrc") || (Name == ".rsrc$01")) {
ResourceSectionRef RSF(Ref);
@@ -1834,8 +1826,7 @@ void COFFDumper::printStackMap() const {
if (StackMapSection == object::SectionRef())
return;
- StringRef StackMapContents;
- StackMapSection.getContents(StackMapContents);
+ StringRef StackMapContents = unwrapOrError(StackMapSection.getContents());
ArrayRef<uint8_t> StackMapContentsArray =
arrayRefFromStringRef(StackMapContents);
@@ -1861,8 +1852,7 @@ void COFFDumper::printAddrsig() {
if (AddrsigSection == object::SectionRef())
return;
- StringRef AddrsigContents;
- AddrsigSection.getContents(AddrsigContents);
+ StringRef AddrsigContents = unwrapOrError(AddrsigSection.getContents());
ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(),
AddrsigContents.size());
Modified: llvm/trunk/tools/llvm-readobj/MachODumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/MachODumper.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/MachODumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/MachODumper.cpp Thu May 16 06:24:04 2019
@@ -483,15 +483,8 @@ void MachODumper::printSectionHeaders(co
}
}
- if (opts::SectionData) {
- bool IsBSS = Section.isBSS();
- if (!IsBSS) {
- StringRef Data;
- error(Section.getContents(Data));
-
- W.printBinaryBlock("SectionData", Data);
- }
- }
+ if (opts::SectionData && !Section.isBSS())
+ W.printBinaryBlock("SectionData", unwrapOrError(Section.getContents()));
}
}
@@ -660,8 +653,7 @@ void MachODumper::printStackMap() const
if (StackMapSection == object::SectionRef())
return;
- StringRef StackMapContents;
- StackMapSection.getContents(StackMapContents);
+ StringRef StackMapContents = unwrapOrError(StackMapSection.getContents());
ArrayRef<uint8_t> StackMapContentsArray =
arrayRefFromStringRef(StackMapContents);
Modified: llvm/trunk/tools/llvm-readobj/ObjDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ObjDumper.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ObjDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ObjDumper.cpp Thu May 16 06:24:04 2019
@@ -73,8 +73,7 @@ void ObjDumper::printSectionAsString(con
error(E);
W.startLine() << "String dump of section '" << SectionName << "':\n";
- StringRef SectionContent;
- Section.getContents(SectionContent);
+ StringRef SectionContent = unwrapOrError(Section.getContents());
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *CurrentWord = SecContent;
@@ -107,8 +106,7 @@ void ObjDumper::printSectionAsHex(const
error(E);
W.startLine() << "Hex dump of section '" << SectionName << "':\n";
- StringRef SectionContent;
- Section.getContents(SectionContent);
+ StringRef SectionContent = unwrapOrError(Section.getContents());
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *SecEnd = SecContent + SectionContent.size();
Modified: llvm/trunk/tools/sancov/sancov.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/sancov/sancov.cpp?rev=360892&r1=360891&r2=360892&view=diff
==============================================================================
--- llvm/trunk/tools/sancov/sancov.cpp (original)
+++ llvm/trunk/tools/sancov/sancov.cpp Thu May 16 06:24:04 2019
@@ -841,9 +841,9 @@ static void getObjectCoveragePoints(cons
if (!SectSize)
continue;
- StringRef BytesStr;
- failIfError(Section.getContents(BytesStr));
- ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
+ Expected<StringRef> BytesStr = Section.getContents();
+ failIfError(BytesStr);
+ ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(*BytesStr);
for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
Index += Size) {
More information about the llvm-commits
mailing list