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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 4 additions & 5 deletions
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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 5 additions & 5 deletions
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

0 commit comments

Comments
 (0)