Skip to content

Commit e183340

Browse files
committed
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. llvm-svn: 360892
1 parent 1b93a24 commit e183340

File tree

27 files changed

+166
-144
lines changed

27 files changed

+166
-144
lines changed

clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,14 @@ ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
337337
StringRef Name;
338338
Section.getName(Name);
339339
if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
340-
Section.getContents(PCH);
341-
return PCH;
340+
if (Expected<StringRef> E = Section.getContents())
341+
return *E;
342+
else {
343+
handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
344+
EIB.log(llvm::errs());
345+
});
346+
return "";
347+
}
342348
}
343349
}
344350
}

clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -462,13 +462,16 @@ class ObjectFileHandler final : public FileHandler {
462462
// TODO: Instead of copying the input file as is, deactivate the section
463463
// that is no longer needed.
464464

465-
StringRef Content;
466-
CurrentSection->getContents(Content);
465+
Expected<StringRef> Content = CurrentSection->getContents();
466+
if (!Content) {
467+
consumeError(Content.takeError());
468+
return;
469+
}
467470

468-
if (Content.size() < 2)
471+
if (Content->size() < 2)
469472
OS.write(Input.getBufferStart(), Input.getBufferSize());
470473
else
471-
OS.write(Content.data(), Content.size());
474+
OS.write(Content->data(), Content->size());
472475
}
473476

474477
void WriteHeader(raw_fd_ostream &OS,

llvm/include/llvm/Object/ObjectFile.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class SectionRef {
9898
uint64_t getAddress() const;
9999
uint64_t getIndex() const;
100100
uint64_t getSize() const;
101-
std::error_code getContents(StringRef &Result) const;
101+
Expected<StringRef> getContents() const;
102102

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

457-
inline std::error_code SectionRef::getContents(StringRef &Result) const {
457+
inline Expected<StringRef> SectionRef::getContents() const {
458458
Expected<ArrayRef<uint8_t>> Res =
459459
OwningObject->getSectionContents(SectionPimpl);
460460
if (!Res)
461-
return errorToErrorCode(Res.takeError());
462-
Result = StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
463-
return std::error_code();
461+
return Res.takeError();
462+
return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
464463
}
465464

466465
inline uint64_t SectionRef::getAlignment() const {

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,14 @@ class DWARFObjInMemory final : public DWARFObject {
14101410
// Try to obtain an already relocated version of this section.
14111411
// Else use the unrelocated section from the object file. We'll have to
14121412
// apply relocations ourselves later.
1413-
if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1414-
Section.getContents(Data);
1413+
if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
1414+
Expected<StringRef> E = Section.getContents();
1415+
if (E)
1416+
Data = *E;
1417+
else
1418+
// maybeDecompress below will error.
1419+
consumeError(E.takeError());
1420+
}
14151421

14161422
if (auto Err = maybeDecompress(Section, Name, Data)) {
14171423
ErrorPolicy EP = HandleError(createError(

llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ SymbolizableObjectFile::create(object::ObjectFile *Obj,
5353
if (Obj->getArch() == Triple::ppc64) {
5454
for (section_iterator Section : Obj->sections()) {
5555
StringRef Name;
56-
StringRef Data;
5756
if (auto EC = Section->getName(Name))
5857
return EC;
5958
if (Name == ".opd") {
60-
if (auto EC = Section->getContents(Data))
61-
return EC;
62-
OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
59+
Expected<StringRef> E = Section->getContents();
60+
if (!E)
61+
return errorToErrorCode(E.takeError());
62+
OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
6363
Obj->getBytesInAddress()));
6464
OpdAddress = Section->getAddress();
6565
break;

llvm/lib/DebugInfo/Symbolize/Symbolize.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
221221
Section.getName(Name);
222222
Name = Name.substr(Name.find_first_not_of("._"));
223223
if (Name == "gnu_debuglink") {
224-
StringRef Data;
225-
Section.getContents(Data);
226-
DataExtractor DE(Data, Obj->isLittleEndian(), 0);
224+
Expected<StringRef> ContentsOrErr = Section.getContents();
225+
if (!ContentsOrErr) {
226+
consumeError(ContentsOrErr.takeError());
227+
return false;
228+
}
229+
DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
227230
uint32_t Offset = 0;
228231
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
229232
// 4-byte align the offset.

llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ Error MachOAtomGraphBuilder::parseSections() {
136136

137137
if (!SecRef.isVirtual()) {
138138
// If this section has content then record it.
139-
StringRef Content;
140-
if (auto EC = SecRef.getContents(Content))
141-
return errorCodeToError(EC);
142-
if (Content.size() != SecRef.getSize())
139+
Expected<StringRef> Content = SecRef.getContents();
140+
if (!Content)
141+
return Content.takeError();
142+
if (Content->size() != SecRef.getSize())
143143
return make_error<JITLinkError>("Section content size does not match "
144144
"declared size for " +
145145
Name);
146-
MachOSec.setContent(Content);
146+
MachOSec.setContent(*Content);
147147
} else {
148148
// If this is a zero-fill section then just record the size.
149149
MachOSec.setZeroFill(SecRef.getSize());

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,10 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
792792
if (!IsVirtual && !IsZeroInit) {
793793
// In either case, set the location of the unrelocated section in memory,
794794
// since we still process relocations for it even if we're not applying them.
795-
if (auto EC = Section.getContents(data))
796-
return errorCodeToError(EC);
795+
if (Expected<StringRef> E = Section.getContents())
796+
data = *E;
797+
else
798+
return E.takeError();
797799
pData = data.data();
798800
}
799801

llvm/lib/Object/ELFObjectFile.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,13 @@ ELFObjectFileBase::getPltAddresses() const {
377377
}
378378
if (!Plt || !RelaPlt || !GotPlt)
379379
return {};
380-
StringRef PltContents;
381-
if (Plt->getContents(PltContents))
380+
Expected<StringRef> PltContents = Plt->getContents();
381+
if (!PltContents) {
382+
consumeError(PltContents.takeError());
382383
return {};
383-
ArrayRef<uint8_t> PltBytes((const uint8_t *)PltContents.data(),
384-
Plt->getSize());
385-
auto PltEntries = MIA->findPltEntries(Plt->getAddress(), PltBytes,
384+
}
385+
auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
386+
arrayRefFromStringRef(*PltContents),
386387
GotPlt->getAddress(), Triple);
387388
// Build a map from GOT entry virtual address to PLT entry virtual address.
388389
DenseMap<uint64_t, uint64_t> GotToPlt;

llvm/lib/Object/IRObjectFile.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ Expected<MemoryBufferRef>
7474
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
7575
for (const SectionRef &Sec : Obj.sections()) {
7676
if (Sec.isBitcode()) {
77-
StringRef SecContents;
78-
if (std::error_code EC = Sec.getContents(SecContents))
79-
return errorCodeToError(EC);
80-
if (SecContents.size() <= 1)
77+
Expected<StringRef> Contents = Sec.getContents();
78+
if (!Contents)
79+
return Contents.takeError();
80+
if (Contents->size() <= 1)
8181
return errorCodeToError(object_error::bitcode_section_not_found);
82-
return MemoryBufferRef(SecContents, Obj.getFileName());
82+
return MemoryBufferRef(*Contents, Obj.getFileName());
8383
}
8484
}
8585

llvm/lib/Object/Object.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
247247
}
248248

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

256256
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {

llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,10 @@ Expected<bool> RawCoverageMappingDummyChecker::isDummy() {
348348
}
349349

350350
Error InstrProfSymtab::create(SectionRef &Section) {
351-
if (auto EC = Section.getContents(Data))
352-
return errorCodeToError(EC);
351+
Expected<StringRef> DataOrErr = Section.getContents();
352+
if (!DataOrErr)
353+
return DataOrErr.takeError();
354+
Data = *DataOrErr;
353355
Address = Section.getAddress();
354356

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

689691
// Get the contents of the given sections.
690-
if (auto EC = CoverageSection->getContents(CoverageMapping))
691-
return errorCodeToError(EC);
692+
if (Expected<StringRef> E = CoverageSection->getContents())
693+
CoverageMapping = *E;
694+
else
695+
return E.takeError();
696+
692697
if (Error E = ProfileNames.create(*NamesSection))
693698
return E;
694699

llvm/lib/XRay/InstrumentationMap.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
7878
"Failed to find XRay instrumentation map.",
7979
std::make_error_code(std::errc::executable_format_error));
8080

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

8586
RelocMap Relocs;
8687
if (ObjFile.getBinary()->isELF()) {

llvm/tools/dsymutil/DwarfLinker.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,13 @@ static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
436436
void DwarfLinker::RelocationManager::findValidRelocsMachO(
437437
const object::SectionRef &Section, const object::MachOObjectFile &Obj,
438438
const DebugMapObject &DMO) {
439-
StringRef Contents;
440-
Section.getContents(Contents);
441-
DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
439+
Expected<StringRef> ContentsOrErr = Section.getContents();
440+
if (!ContentsOrErr) {
441+
consumeError(ContentsOrErr.takeError());
442+
Linker.reportWarning("error reading section", DMO);
443+
return;
444+
}
445+
DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
442446
bool SkipNext = false;
443447

444448
for (const object::RelocationRef &Reloc : Section.relocations()) {

llvm/tools/dsymutil/DwarfStreamer.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,12 @@ void DwarfStreamer::translateLineTable(DataExtractor Data, uint32_t Offset,
667667

668668
static void emitSectionContents(const object::ObjectFile &Obj,
669669
StringRef SecName, MCStreamer *MS) {
670-
StringRef Contents;
671-
if (auto Sec = getSectionByName(Obj, SecName))
672-
if (!Sec->getContents(Contents))
673-
MS->EmitBytes(Contents);
670+
if (auto Sec = getSectionByName(Obj, SecName)) {
671+
if (Expected<StringRef> E = Sec->getContents())
672+
MS->EmitBytes(*E);
673+
else
674+
consumeError(E.takeError());
675+
}
674676
}
675677

676678
void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {

llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,11 @@ Error FileAnalysis::parseCodeSections() {
453453
if (!Section.getName(SectionName) && SectionName == ".plt")
454454
continue;
455455

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

461-
ArrayRef<uint8_t> SectionBytes((const uint8_t *)SectionContents.data(),
462-
Section.getSize());
463461
parseSectionContents(SectionBytes,
464462
{Section.getAddress(), Section.getIndex()});
465463
}

llvm/tools/llvm-cov/TestingSupport.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@ int convertForTestingMain(int argc, const char *argv[]) {
6969
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
7070
StringRef CoverageMappingData;
7171
StringRef ProfileNamesData;
72-
if (CoverageMapping.getContents(CoverageMappingData) ||
73-
ProfileNames.getContents(ProfileNamesData))
72+
if (Expected<StringRef> E = CoverageMapping.getContents())
73+
CoverageMappingData = *E;
74+
else {
75+
consumeError(E.takeError());
7476
return 1;
77+
}
78+
if (Expected<StringRef> E = ProfileNames.getContents())
79+
ProfileNamesData = *E;
80+
else {
81+
consumeError(E.takeError());
82+
return 1;
83+
}
7584

7685
int FD;
7786
if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {

llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,20 @@ static void error(std::error_code EC) {
4848
exit(1);
4949
}
5050

51-
static void error(Error Err) {
52-
if (!Err)
53-
return;
51+
LLVM_ATTRIBUTE_NORETURN static void error(Error Err) {
5452
logAllUnhandledErrors(std::move(Err), WithColor::error(outs()),
5553
"reading file: ");
5654
outs().flush();
5755
exit(1);
5856
}
5957

58+
template <typename T>
59+
T unwrapOrError(Expected<T> EO) {
60+
if (!EO)
61+
error(EO.takeError());
62+
return std::move(*EO);
63+
}
64+
6065
} // namespace llvm
6166

6267
static void reportError(StringRef Input, StringRef Message) {
@@ -195,8 +200,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
195200
// Skip virtual or BSS sections.
196201
if (Sec.isBSS() || Sec.isVirtual())
197202
continue;
198-
StringRef SecContents;
199-
error(Sec.getContents(SecContents));
203+
StringRef SecContents = unwrapOrError(Sec.getContents());
200204
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
201205
error(errorToErrorCode(SymAddressOrErr.takeError()));
202206
uint64_t SymAddress = *SymAddressOrErr;
@@ -510,7 +514,8 @@ static void dumpArchive(const Archive *Arc) {
510514
else
511515
reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
512516
}
513-
error(std::move(Err));
517+
if (Err)
518+
error(std::move(Err));
514519
}
515520

516521
static void dumpInput(StringRef File) {

llvm/tools/llvm-dwp/llvm-dwp.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ static Error handleSection(
410410
if (std::error_code Err = Section.getName(Name))
411411
return errorCodeToError(Err);
412412

413-
StringRef Contents;
414-
if (auto Err = Section.getContents(Contents))
415-
return errorCodeToError(Err);
413+
Expected<StringRef> ContentsOrErr = Section.getContents();
414+
if (!ContentsOrErr)
415+
return ContentsOrErr.takeError();
416+
StringRef Contents = *ContentsOrErr;
416417

417418
if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
418419
return Err;

0 commit comments

Comments
 (0)