Skip to content

Commit

Permalink
Revert r360876 "[Object] Change object::SectionRef::getContents() to …
Browse files Browse the repository at this point in the history
…return Expected<StringRef>"

It broke the Clang build, see llvm-commits thread.

> Expected<ArrayRef<uint8_t>> may be better but use Expected<StringRef> for now.
>
> Follow-up of D61781.

llvm-svn: 360878
  • Loading branch information
zmodem authored and MrSidims committed May 24, 2019
1 parent 8c1c696 commit a2ddf54
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 151 deletions.
9 changes: 5 additions & 4 deletions llvm/include/llvm/Object/ObjectFile.h
Expand Up @@ -98,7 +98,7 @@ class SectionRef {
uint64_t getAddress() const;
uint64_t getIndex() const;
uint64_t getSize() const;
Expected<StringRef> getContents() const;
std::error_code getContents(StringRef &Result) const;

/// Get the alignment of this section as the actual value (not log 2).
uint64_t getAlignment() const;
Expand Down Expand Up @@ -454,12 +454,13 @@ inline uint64_t SectionRef::getSize() const {
return OwningObject->getSectionSize(SectionPimpl);
}

inline Expected<StringRef> SectionRef::getContents() const {
inline std::error_code SectionRef::getContents(StringRef &Result) const {
Expected<ArrayRef<uint8_t>> Res =
OwningObject->getSectionContents(SectionPimpl);
if (!Res)
return Res.takeError();
return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
return errorToErrorCode(Res.takeError());
Result = StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
return std::error_code();
}

inline uint64_t SectionRef::getAlignment() const {
Expand Down
10 changes: 2 additions & 8 deletions llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
Expand Up @@ -1410,14 +1410,8 @@ class DWARFObjInMemory final : public DWARFObject {
// 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)) {
Expected<StringRef> E = Section.getContents();
if (E)
Data = *E;
else
// maybeDecompress below will error.
consumeError(E.takeError());
}
if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
Section.getContents(Data);

if (auto Err = maybeDecompress(Section, Name, Data)) {
ErrorPolicy EP = HandleError(createError(
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
Expand Up @@ -53,13 +53,13 @@ SymbolizableObjectFile::create(object::ObjectFile *Obj,
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") {
Expected<StringRef> E = Section->getContents();
if (!E)
return errorToErrorCode(E.takeError());
OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
if (auto EC = Section->getContents(Data))
return EC;
OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
Obj->getBytesInAddress()));
OpdAddress = Section->getAddress();
break;
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/DebugInfo/Symbolize/Symbolize.cpp
Expand Up @@ -221,12 +221,9 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
Section.getName(Name);
Name = Name.substr(Name.find_first_not_of("._"));
if (Name == "gnu_debuglink") {
Expected<StringRef> ContentsOrErr = Section.getContents();
if (!ContentsOrErr) {
consumeError(ContentsOrErr.takeError());
return false;
}
DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
StringRef Data;
Section.getContents(Data);
DataExtractor DE(Data, Obj->isLittleEndian(), 0);
uint32_t Offset = 0;
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
// 4-byte align the offset.
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp
Expand Up @@ -136,14 +136,14 @@ Error MachOAtomGraphBuilder::parseSections() {

if (!SecRef.isVirtual()) {
// If this section has content then record it.
Expected<StringRef> Content = SecRef.getContents();
if (!Content)
return Content.takeError();
if (Content->size() != SecRef.getSize())
StringRef Content;
if (auto EC = SecRef.getContents(Content))
return errorCodeToError(EC);
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());
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
Expand Up @@ -792,10 +792,8 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
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 (Expected<StringRef> E = Section.getContents())
data = *E;
else
return E.takeError();
if (auto EC = Section.getContents(data))
return errorCodeToError(EC);
pData = data.data();
}

Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/Object/ELFObjectFile.cpp
Expand Up @@ -377,13 +377,12 @@ ELFObjectFileBase::getPltAddresses() const {
}
if (!Plt || !RelaPlt || !GotPlt)
return {};
Expected<StringRef> PltContents = Plt->getContents();
if (!PltContents) {
consumeError(PltContents.takeError());
StringRef PltContents;
if (Plt->getContents(PltContents))
return {};
}
auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
arrayRefFromStringRef(*PltContents),
ArrayRef<uint8_t> PltBytes((const uint8_t *)PltContents.data(),
Plt->getSize());
auto PltEntries = MIA->findPltEntries(Plt->getAddress(), PltBytes,
GotPlt->getAddress(), Triple);
// Build a map from GOT entry virtual address to PLT entry virtual address.
DenseMap<uint64_t, uint64_t> GotToPlt;
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Object/IRObjectFile.cpp
Expand Up @@ -74,12 +74,12 @@ Expected<MemoryBufferRef>
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
for (const SectionRef &Sec : Obj.sections()) {
if (Sec.isBitcode()) {
Expected<StringRef> Contents = Sec.getContents();
if (!Contents)
return Contents.takeError();
if (Contents->size() <= 1)
StringRef SecContents;
if (std::error_code EC = Sec.getContents(SecContents))
return errorCodeToError(EC);
if (SecContents.size() <= 1)
return errorCodeToError(object_error::bitcode_section_not_found);
return MemoryBufferRef(*Contents, Obj.getFileName());
return MemoryBufferRef(SecContents, Obj.getFileName());
}
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Object/Object.cpp
Expand Up @@ -247,10 +247,10 @@ uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
}

const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
if (Expected<StringRef> E = (*unwrap(SI))->getContents())
return E->data();
else
report_fatal_error(E.takeError());
StringRef ret;
if (std::error_code ec = (*unwrap(SI))->getContents(ret))
report_fatal_error(ec.message());
return ret.data();
}

uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
Expand Down
13 changes: 4 additions & 9 deletions llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
Expand Up @@ -348,10 +348,8 @@ Expected<bool> RawCoverageMappingDummyChecker::isDummy() {
}

Error InstrProfSymtab::create(SectionRef &Section) {
Expected<StringRef> DataOrErr = Section.getContents();
if (!DataOrErr)
return DataOrErr.takeError();
Data = *DataOrErr;
if (auto EC = Section.getContents(Data))
return errorCodeToError(EC);
Address = Section.getAddress();

// If this is a linked PE/COFF file, then we have to skip over the null byte
Expand Down Expand Up @@ -689,11 +687,8 @@ static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer,
return E;

// Get the contents of the given sections.
if (Expected<StringRef> E = CoverageSection->getContents())
CoverageMapping = *E;
else
return E.takeError();

if (auto EC = CoverageSection->getContents(CoverageMapping))
return errorCodeToError(EC);
if (Error E = ProfileNames.create(*NamesSection))
return E;

Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/XRay/InstrumentationMap.cpp
Expand Up @@ -78,10 +78,9 @@ loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
"Failed to find XRay instrumentation map.",
std::make_error_code(std::errc::executable_format_error));

if (Expected<StringRef> E = I->getContents())
Contents = *E;
else
return E.takeError();
if (I->getContents(Contents))
return errorCodeToError(
std::make_error_code(std::errc::executable_format_error));

RelocMap Relocs;
if (ObjFile.getBinary()->isELF()) {
Expand Down
10 changes: 3 additions & 7 deletions llvm/tools/dsymutil/DwarfLinker.cpp
Expand Up @@ -436,13 +436,9 @@ static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
void DwarfLinker::RelocationManager::findValidRelocsMachO(
const object::SectionRef &Section, const object::MachOObjectFile &Obj,
const DebugMapObject &DMO) {
Expected<StringRef> ContentsOrErr = Section.getContents();
if (!ContentsOrErr) {
consumeError(ContentsOrErr.takeError());
Linker.reportWarning("error reading section", DMO);
return;
}
DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
StringRef Contents;
Section.getContents(Contents);
DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
bool SkipNext = false;

for (const object::RelocationRef &Reloc : Section.relocations()) {
Expand Down
10 changes: 4 additions & 6 deletions llvm/tools/dsymutil/DwarfStreamer.cpp
Expand Up @@ -667,12 +667,10 @@ void DwarfStreamer::translateLineTable(DataExtractor Data, uint32_t Offset,

static void emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName, MCStreamer *MS) {
if (auto Sec = getSectionByName(Obj, SecName)) {
if (Expected<StringRef> E = Sec->getContents())
MS->EmitBytes(*E);
else
consumeError(E.takeError());
}
StringRef Contents;
if (auto Sec = getSectionByName(Obj, SecName))
if (!Sec->getContents(Contents))
MS->EmitBytes(Contents);
}

void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {
Expand Down
10 changes: 6 additions & 4 deletions llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp
Expand Up @@ -453,11 +453,13 @@ Error FileAnalysis::parseCodeSections() {
if (!Section.getName(SectionName) && SectionName == ".plt")
continue;

Expected<StringRef> Contents = Section.getContents();
if (!Contents)
return Contents.takeError();
ArrayRef<uint8_t> SectionBytes = arrayRefFromStringRef(*Contents);
StringRef SectionContents;
if (Section.getContents(SectionContents))
return make_error<StringError>("Failed to retrieve section contents",
inconvertibleErrorCode());

ArrayRef<uint8_t> SectionBytes((const uint8_t *)SectionContents.data(),
Section.getSize());
parseSectionContents(SectionBytes,
{Section.getAddress(), Section.getIndex()});
}
Expand Down
13 changes: 2 additions & 11 deletions llvm/tools/llvm-cov/TestingSupport.cpp
Expand Up @@ -69,18 +69,9 @@ int convertForTestingMain(int argc, const char *argv[]) {
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
StringRef CoverageMappingData;
StringRef ProfileNamesData;
if (Expected<StringRef> E = CoverageMapping.getContents())
CoverageMappingData = *E;
else {
consumeError(E.takeError());
if (CoverageMapping.getContents(CoverageMappingData) ||
ProfileNames.getContents(ProfileNamesData))
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)) {
Expand Down
17 changes: 6 additions & 11 deletions llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp
Expand Up @@ -48,20 +48,15 @@ static void error(std::error_code EC) {
exit(1);
}

LLVM_ATTRIBUTE_NORETURN static void error(Error Err) {
static void error(Error Err) {
if (!Err)
return;
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) {
Expand Down Expand Up @@ -200,7 +195,8 @@ static void dumpCXXData(const ObjectFile *Obj) {
// Skip virtual or BSS sections.
if (Sec.isBSS() || Sec.isVirtual())
continue;
StringRef SecContents = unwrapOrError(Sec.getContents());
StringRef SecContents;
error(Sec.getContents(SecContents));
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
error(errorToErrorCode(SymAddressOrErr.takeError()));
uint64_t SymAddress = *SymAddressOrErr;
Expand Down Expand Up @@ -514,8 +510,7 @@ static void dumpArchive(const Archive *Arc) {
else
reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
}
if (Err)
error(std::move(Err));
error(std::move(Err));
}

static void dumpInput(StringRef File) {
Expand Down
7 changes: 3 additions & 4 deletions llvm/tools/llvm-dwp/llvm-dwp.cpp
Expand Up @@ -410,10 +410,9 @@ static Error handleSection(
if (std::error_code Err = Section.getName(Name))
return errorCodeToError(Err);

Expected<StringRef> ContentsOrErr = Section.getContents();
if (!ContentsOrErr)
return ContentsOrErr.takeError();
StringRef Contents = *ContentsOrErr;
StringRef Contents;
if (auto Err = Section.getContents(Contents))
return errorCodeToError(Err);

if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
return Err;
Expand Down

0 comments on commit a2ddf54

Please sign in to comment.