diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index c4527b3435ff3..8dac02c56dcd7 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -461,13 +461,15 @@ Expected ELFObjectFile::getSymbolName(DataRefImpl Sym) const { if (!SymStrTabOrErr) return SymStrTabOrErr.takeError(); Expected Name = ESym->getName(*SymStrTabOrErr); + if (Name && !Name->empty()) + return Name; // If the symbol name is empty use the section name. - if ((!Name || Name->empty()) && ESym->getType() == ELF::STT_SECTION) { - StringRef SecName; - Expected Sec = getSymbolSection(Sym); - if (Sec && !(*Sec)->getName(SecName)) - return SecName; + if (ESym->getType() == ELF::STT_SECTION) { + if (Expected SecOrErr = getSymbolSection(Sym)) { + consumeError(Name.takeError()); + return (*SecOrErr)->getName(); + } } return Name; } diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 483a3486bd724..a4b4f27c28d66 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -94,7 +94,7 @@ class SectionRef { void moveNext(); - std::error_code getName(StringRef &Result) const; + Expected getName() const; uint64_t getAddress() const; uint64_t getIndex() const; uint64_t getSize() const; @@ -434,12 +434,8 @@ inline void SectionRef::moveNext() { return OwningObject->moveSectionNext(SectionPimpl); } -inline std::error_code SectionRef::getName(StringRef &Result) const { - Expected NameOrErr = OwningObject->getSectionName(SectionPimpl); - if (!NameOrErr) - return errorToErrorCode(NameOrErr.takeError()); - Result = *NameOrErr; - return std::error_code(); +inline Expected SectionRef::getName() const { + return OwningObject->getSectionName(SectionPimpl); } inline uint64_t SectionRef::getAddress() const { diff --git a/lib/DebugInfo/DWARF/DWARFContext.cpp b/lib/DebugInfo/DWARF/DWARFContext.cpp index 4b66a305a3da9..2379ba66aa3cd 100644 --- a/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1506,7 +1506,11 @@ class DWARFObjInMemory final : public DWARFObject { StringMap SectionAmountMap; for (const SectionRef &Section : Obj.sections()) { StringRef Name; - Section.getName(Name); + if (auto NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + ++SectionAmountMap[Name]; SectionNames.push_back({ Name, true }); @@ -1571,12 +1575,15 @@ class DWARFObjInMemory final : public DWARFObject { continue; StringRef RelSecName; - StringRef RelSecData; - RelocatedSection->getName(RelSecName); + if (auto NameOrErr = RelocatedSection->getName()) + RelSecName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); // If the section we're relocating was relocated already by the JIT, // then we used the relocated version above, so we do not need to process // relocations for it now. + StringRef RelSecData; if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) continue; diff --git a/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp index bc4070b1d81db..b4d49d9ff9582 100644 --- a/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -54,10 +54,11 @@ SymbolizableObjectFile::create(const object::ObjectFile *Obj, // PowerPC64 ELF. if (Obj->getArch() == Triple::ppc64) { for (section_iterator Section : Obj->sections()) { - StringRef Name; - if (auto EC = Section->getName(Name)) - return EC; - if (Name == ".opd") { + Expected NameOrErr = Section->getName(); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); + + if (*NameOrErr == ".opd") { Expected E = Section->getContents(); if (!E) return errorToErrorCode(E.takeError()); diff --git a/lib/DebugInfo/Symbolize/Symbolize.cpp b/lib/DebugInfo/Symbolize/Symbolize.cpp index 271d0cc6f4467..a5559373b05b8 100644 --- a/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -259,7 +259,11 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, return false; for (const SectionRef &Section : Obj->sections()) { StringRef Name; - Section.getName(Name); + if (Expected NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + Name = Name.substr(Name.find_first_not_of("._")); if (Name == "gnu_debuglink") { Expected ContentsOrErr = Section.getContents(); diff --git a/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp b/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp index 1501c7ad0bc56..92c7e49039a9d 100644 --- a/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp +++ b/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp @@ -96,9 +96,10 @@ Error MachOAtomGraphBuilder::parseSections() { assert((SecRef.getAlignment() <= std::numeric_limits::max()) && "Section alignment does not fit in 32 bits"); - StringRef Name; - if (auto EC = SecRef.getName(Name)) - return errorCodeToError(EC); + Expected NameOrErr = SecRef.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; unsigned SectionIndex = SecRef.getIndex() + 1; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index f73d1c61edf47..4b328624ccd9b 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -535,9 +535,10 @@ Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, bool IsCode = Section.isText(); bool IsReadOnly = isReadOnlyData(Section); - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); @@ -777,9 +778,10 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj, // anyway, so we should guarantee that the alignment is always at least 1. Alignment = std::max(1u, Alignment); - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; StubBufSize = computeSectionStubBufSize(Obj, Section); diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 60041a45e2b8e..e3ace265f9c43 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -160,9 +160,13 @@ createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject, // Iterate over all sections in the object. auto SI = SourceObject.section_begin(); for (const auto &Sec : Obj->sections()) { - StringRef SectionName; - Sec.getName(SectionName); - if (SectionName != "") { + Expected NameOrErr = Sec.getName(); + if (!NameOrErr) { + consumeError(NameOrErr.takeError()); + continue; + } + + if (*NameOrErr != "") { DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); Elf_Shdr *shdr = const_cast( reinterpret_cast(ShdrRef.p)); @@ -567,10 +571,11 @@ Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj, // The TOC consists of sections .got, .toc, .tocbss, .plt in that // order. The TOC starts where the first of these sections starts. - for (auto &Section: Obj.sections()) { - StringRef SectionName; - if (auto EC = Section.getName(SectionName)) - return errorCodeToError(EC); + for (auto &Section : Obj.sections()) { + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef SectionName = *NameOrErr; if (SectionName == ".got" || SectionName == ".toc" @@ -605,9 +610,10 @@ Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj, if (RelSecI == Obj.section_end()) continue; - StringRef RelSectionName; - if (auto EC = RelSecI->getName(RelSectionName)) - return errorCodeToError(EC); + Expected NameOrErr = RelSecI->getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef RelSectionName = *NameOrErr; if (RelSectionName != ".opd") continue; @@ -1879,8 +1885,14 @@ Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap::iterator i, e; for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { const SectionRef &Section = i->first; + StringRef Name; - Section.getName(Name); + Expected NameOrErr = Section.getName(); + if (NameOrErr) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == ".eh_frame") { UnregisteredEHFrameSections.push_back(i->second); break; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 202c3ca1c507b..fbdfb8d5c3aaa 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -233,7 +233,10 @@ RuntimeDyldMachOCRTPBase::finalizeLoad(const ObjectFile &Obj, for (const auto &Section : Obj.sections()) { StringRef Name; - Section.getName(Name); + if (Expected NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); // Force emission of the __text, __eh_frame, and __gcc_except_tab sections // if they're present. Otherwise call down to the impl to handle other diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h index d2d74534cf902..dc4af08583de7 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h @@ -284,14 +284,14 @@ class RuntimeDyldCOFFX86_64 : public RuntimeDyldCOFF { // Look for and record the EH frame section IDs. for (const auto &SectionPair : SectionMap) { const object::SectionRef &Section = SectionPair.first; - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); // Note unwind info is stored in .pdata but often points to .xdata // with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager // that keeps sections ordered in relation to __ImageBase is necessary. - if (Name == ".pdata") + if ((*NameOrErr) == ".pdata") UnregisteredEHFrameSections.push_back(SectionPair.second); } return Error::success(); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h index 3bec8b979f7df..a76958a9e2c2a 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h @@ -289,7 +289,10 @@ class RuntimeDyldMachOARM Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section) { StringRef Name; - Section.getName(Name); + if (Expected NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); if (Name == "__nl_symbol_ptr") return populateIndirectSymbolPointersSection(cast(Obj), diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h index f0de27ba14bbc..523deb29b723e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h @@ -128,7 +128,10 @@ class RuntimeDyldMachOI386 Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section) { StringRef Name; - Section.getName(Name); + if (Expected NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); if (Name == "__jump_table") return populateJumpTable(cast(Obj), Section, SectionID); diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 854664e679df4..afd68694debe3 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -994,11 +994,12 @@ std::error_code COFFObjectFile::getSection(int32_t Index, std::error_code COFFObjectFile::getSection(StringRef SectionName, const coff_section *&Result) const { Result = nullptr; - StringRef SecName; for (const SectionRef &Section : sections()) { - if (std::error_code E = Section.getName(SecName)) - return E; - if (SecName == SectionName) { + auto NameOrErr = Section.getName(); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); + + if (*NameOrErr == SectionName) { Result = getCOFFSection(Section); return std::error_code(); } diff --git a/lib/Object/Decompressor.cpp b/lib/Object/Decompressor.cpp index 53b8e846e5fbe..11efd857d1a16 100644 --- a/lib/Object/Decompressor.cpp +++ b/lib/Object/Decompressor.cpp @@ -77,10 +77,15 @@ bool Decompressor::isGnuStyle(StringRef Name) { } bool Decompressor::isCompressed(const object::SectionRef &Section) { - StringRef Name; - if (Section.getName(Name)) - return false; - return Section.isCompressed() || isGnuStyle(Name); + if (Section.isCompressed()) + return true; + + Expected SecNameOrErr = Section.getName(); + if (SecNameOrErr) + return isGnuStyle(*SecNameOrErr); + + consumeError(SecNameOrErr.takeError()); + return false; } bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) { diff --git a/lib/Object/ELFObjectFile.cpp b/lib/Object/ELFObjectFile.cpp index 9945018d957da..f3b0347088a4f 100644 --- a/lib/Object/ELFObjectFile.cpp +++ b/lib/Object/ELFObjectFile.cpp @@ -392,9 +392,13 @@ ELFObjectFileBase::getPltAddresses() const { return {}; Optional Plt = None, RelaPlt = None, GotPlt = None; for (const SectionRef &Section : sections()) { - StringRef Name; - if (Section.getName(Name)) + Expected NameOrErr = Section.getName(); + if (!NameOrErr) { + consumeError(NameOrErr.takeError()); continue; + } + StringRef Name = *NameOrErr; + if (Name == ".plt") Plt = Section; else if (Name == ".rela.plt" || Name == ".rel.plt") diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 3bfbfe0694432..179166ddbd39d 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -1986,13 +1986,12 @@ Expected MachOObjectFile::getSection(unsigned SectionIndex) const { } Expected MachOObjectFile::getSection(StringRef SectionName) const { - StringRef SecName; for (const SectionRef &Section : sections()) { - if (std::error_code E = Section.getName(SecName)) - return errorCodeToError(E); - if (SecName == SectionName) { + auto NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + if (*NameOrErr == SectionName) return Section; - } } return errorCodeToError(object_error::parse_failed); } @@ -3995,7 +3994,11 @@ BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) { uint64_t CurSegAddress; for (const SectionRef &Section : Obj->sections()) { SectionInfo Info; - Section.getName(Info.SectionName); + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + consumeError(NameOrErr.takeError()); + else + Info.SectionName = *NameOrErr; Info.Address = Section.getAddress(); Info.Size = Section.getSize(); Info.SegmentName = diff --git a/lib/Object/Object.cpp b/lib/Object/Object.cpp index d84798cc6dd0f..b518e2d02c9c9 100644 --- a/lib/Object/Object.cpp +++ b/lib/Object/Object.cpp @@ -251,10 +251,10 @@ void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) { // SectionRef accessors const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) { - StringRef ret; - if (std::error_code ec = (*unwrap(SI))->getName(ret)) - report_fatal_error(ec.message()); - return ret.data(); + auto NameOrErr = (*unwrap(SI))->getName(); + if (!NameOrErr) + report_fatal_error(NameOrErr.takeError()); + return NameOrErr->data(); } uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { diff --git a/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/lib/ProfileData/Coverage/CoverageMappingReader.cpp index e193e10f91d9f..e70bc318b84cc 100644 --- a/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -666,11 +666,11 @@ static Expected lookupSection(ObjectFile &OF, StringRef Name) { }; Name = stripSuffix(Name); - StringRef FoundName; for (const auto &Section : OF.sections()) { - if (auto EC = Section.getName(FoundName)) - return errorCodeToError(EC); - if (stripSuffix(FoundName) == Name) + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + if (stripSuffix(*NameOrErr) == Name) return Section; } return make_error(coveragemap_error::no_data_found); diff --git a/lib/XRay/InstrumentationMap.cpp b/lib/XRay/InstrumentationMap.cpp index 7de953d46b7bd..7453613c70380 100644 --- a/lib/XRay/InstrumentationMap.cpp +++ b/lib/XRay/InstrumentationMap.cpp @@ -67,10 +67,11 @@ loadObj(StringRef Filename, object::OwningBinary &ObjFile, StringRef Contents = ""; const auto &Sections = ObjFile.getBinary()->sections(); auto I = llvm::find_if(Sections, [&](object::SectionRef Section) { - StringRef Name = ""; - if (Section.getName(Name)) - return false; - return Name == "xray_instr_map"; + Expected NameOrErr = Section.getName(); + if (NameOrErr) + return *NameOrErr == "xray_instr_map"; + consumeError(NameOrErr.takeError()); + return false; }); if (I == Sections.end()) diff --git a/test/Object/invalid.test b/test/Object/invalid.test index cddc0b9c342e2..16fae95df95ca 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -2,9 +2,9 @@ ## .shstrtab has an invalid type. # RUN: yaml2obj %s --docnum=1 -o %t1 -# RUN: not llvm-objdump -s %t1 2>&1 | FileCheck %s --check-prefix=INVALIDERR +# RUN: not llvm-objdump -s %t1 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=INVALID-SHTYPE -# INVALIDERR: error: reading file: Invalid data was encountered while parsing the file +# INVALID-SHTYPE: error: '[[FILE]]': invalid sh_type for string table section [index 1]: expected SHT_STRTAB, but got SHT_PROGBITS --- !ELF FileHeader: @@ -20,7 +20,9 @@ Sections: ## .shstrtab has an invalid zero-size. # RUN: yaml2obj %s --docnum=2 -o %t2 -# RUN: not llvm-objdump -s %t2 2>&1 | FileCheck %s --check-prefix=INVALIDERR +# RUN: not llvm-objdump -s %t2 2>&1 | FileCheck %s -DFILE=%t2 --check-prefix=STRTAB-EMPTY + +# STRTAB-EMPTY: error: '[[FILE]]': SHT_STRTAB string table section [index 1] is empty --- !ELF FileHeader: @@ -37,7 +39,9 @@ Sections: ## size that goes past the end of the file. # RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-size.elf 2>&1 \ -# RUN: | FileCheck %s --check-prefix=INVALIDERR +# RUN: | FileCheck %s -DFILE=%p/Inputs/invalid-strtab-size.elf --check-prefix=INVALID-STRTAB-SIZE + +# INVALID-STRTAB-SIZE: error: '[[FILE]]': section [index 1] has a sh_offset (0x70) + sh_size (0x16777215) that cannot be represented ## Check that llvm-dwarfdump reports an error during relocation resolution ## when instead of expected SHT_RELA section it locates a section of a different type. @@ -72,7 +76,9 @@ Sections: ## and .shstrtab is not null-terminated. # RUN: yaml2obj %s --docnum=4 -o %t4 -# RUN: not llvm-objdump -s %t4 2>&1 | FileCheck --check-prefix=INVALIDERR %s +# RUN: not llvm-objdump -s %t4 2>&1 | FileCheck -DFILE=%t4 --check-prefix=SHSTRTAB-NON-TERM %s + +# SHSTRTAB-NON-TERM: error: '[[FILE]]': SHT_STRTAB string table section [index 1] is non-null terminated --- !ELF FileHeader: diff --git a/tools/dsymutil/DwarfLinker.cpp b/tools/dsymutil/DwarfLinker.cpp index e97e0e6489b24..f8fee696717e2 100644 --- a/tools/dsymutil/DwarfLinker.cpp +++ b/tools/dsymutil/DwarfLinker.cpp @@ -538,7 +538,11 @@ bool DwarfLinker::RelocationManager::findValidRelocsInDebugInfo( // Find the debug_info section. for (const object::SectionRef &Section : Obj.sections()) { StringRef SectionName; - Section.getName(SectionName); + if (Expected NameOrErr = Section.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + SectionName = SectionName.substr(SectionName.find_first_not_of("._")); if (SectionName != "debug_info") continue; diff --git a/tools/dsymutil/DwarfStreamer.cpp b/tools/dsymutil/DwarfStreamer.cpp index 9461f4de819bf..8fc2985178623 100644 --- a/tools/dsymutil/DwarfStreamer.cpp +++ b/tools/dsymutil/DwarfStreamer.cpp @@ -31,7 +31,11 @@ static Optional getSectionByName(const object::ObjectFile &Obj, StringRef SecName) { for (const object::SectionRef &Section : Obj.sections()) { StringRef SectionName; - Section.getName(SectionName); + if (Expected NameOrErr = Section.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + SectionName = SectionName.substr(SectionName.find_first_not_of("._")); if (SectionName != SecName) continue; diff --git a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp index 4bf308f87c288..ce67ab4eb8dc2 100644 --- a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp +++ b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp @@ -449,9 +449,10 @@ Error FileAnalysis::parseCodeSections() { // Avoid checking the PLT since it produces spurious failures on AArch64 // when ignoring DWARF data. - StringRef SectionName; - if (!Section.getName(SectionName) && SectionName == ".plt") + Expected NameOrErr = Section.getName(); + if (NameOrErr && *NameOrErr == ".plt") continue; + consumeError(NameOrErr.takeError()); Expected Contents = Section.getContents(); if (!Contents) diff --git a/tools/llvm-cov/TestingSupport.cpp b/tools/llvm-cov/TestingSupport.cpp index 3ee318c9c640e..39bdade127477 100644 --- a/tools/llvm-cov/TestingSupport.cpp +++ b/tools/llvm-cov/TestingSupport.cpp @@ -50,8 +50,13 @@ int convertForTestingMain(int argc, const char *argv[]) { auto ObjFormat = OF->getTripleObjectFormat(); for (const auto &Section : OF->sections()) { StringRef Name; - if (Section.getName(Name)) + if (Expected NameOrErr = Section.getName()) { + Name = *NameOrErr; + } else { + consumeError(NameOrErr.takeError()); return 1; + } + if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat, /*AddSegmentInfo=*/false)) { ProfileNames = Section; diff --git a/tools/llvm-dwp/llvm-dwp.cpp b/tools/llvm-dwp/llvm-dwp.cpp index 05042ec97b863..0c629f506f4f6 100644 --- a/tools/llvm-dwp/llvm-dwp.cpp +++ b/tools/llvm-dwp/llvm-dwp.cpp @@ -406,9 +406,10 @@ static Error handleSection( if (Section.isVirtual()) return Error::success(); - StringRef Name; - if (std::error_code Err = Section.getName(Name)) - return errorCodeToError(Err); + Expected NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; Expected ContentsOrErr = Section.getContents(); if (!ContentsOrErr) diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index aa62e6f0209b4..465f7de6857d5 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -913,10 +913,12 @@ static char getSymbolNMTypeChar(ELFObjectFileBase &Obj, if (Flags & ELF::SHF_ALLOC) return Flags & ELF::SHF_WRITE ? 'd' : 'r'; - StringRef SecName; - if (SecI->getName(SecName)) + auto NameOrErr = SecI->getName(); + if (!NameOrErr) { + consumeError(NameOrErr.takeError()); return '?'; - if (SecName.startswith(".debug")) + } + if ((*NameOrErr).startswith(".debug")) return 'N'; if (!(Flags & ELF::SHF_WRITE)) return 'n'; @@ -1090,8 +1092,13 @@ static char getNMSectionTagAndName(SymbolicFile &Obj, basic_symbol_iterator I, consumeError(SecIOrErr.takeError()); return '?'; } - elf_section_iterator secT = *SecIOrErr; - secT->getName(SecName); + + Expected NameOrErr = (*SecIOrErr)->getName(); + if (!NameOrErr) { + consumeError(SecIOrErr.takeError()); + return '?'; + } + SecName = *NameOrErr; } } @@ -1347,7 +1354,12 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, StringRef SectionName = StringRef(); for (const SectionRef &Section : MachO->sections()) { S.NSect++; - Section.getName(SectionName); + + if (Expected NameOrErr = Section.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + SegmentName = MachO->getSectionFinalSegmentName( Section.getRawDataRefImpl()); if (S.Address >= Section.getAddress() && @@ -1667,7 +1679,11 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, StringRef SegmentName = StringRef(); StringRef SectionName = StringRef(); for (const SectionRef &Section : MachO->sections()) { - Section.getName(SectionName); + if (Expected NameOrErr = Section.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + SegmentName = MachO->getSectionFinalSegmentName( Section.getRawDataRefImpl()); F.NSect++; diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp index 1ba0a68902c92..3fb9285bf273f 100644 --- a/tools/llvm-objdump/COFFDump.cpp +++ b/tools/llvm-objdump/COFFDump.cpp @@ -442,8 +442,7 @@ static bool getPDataSection(const COFFObjectFile *Obj, std::vector &Rels, const RuntimeFunction *&RFStart, int &NumRFs) { for (const SectionRef &Section : Obj->sections()) { - StringRef Name; - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName()); if (Name != ".pdata") continue; diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index a3f3a9df4c124..c4fa6b5c2a2ae 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -371,11 +371,8 @@ static void getSectionsAndSymbols(MachOObjectFile *MachOObj, Symbols.push_back(Symbol); } - for (const SectionRef &Section : MachOObj->sections()) { - StringRef SectName; - Section.getName(SectName); + for (const SectionRef &Section : MachOObj->sections()) Sections.push_back(Section); - } bool BaseSegmentAddressSet = false; for (const auto &Command : MachOObj->load_commands()) { @@ -449,13 +446,11 @@ static void printRelocationTargetName(const MachOObjectFile *O, // If we couldn't find a symbol that this relocation refers to, try // to find a section beginning instead. for (const SectionRef &Section : ToolSectionFilter(*O)) { - StringRef Name; uint64_t Addr = Section.getAddress(); if (Addr != Val) continue; - if (std::error_code EC = Section.getName(Name)) - report_error(errorCodeToError(EC), O->getFileName()); - Fmt << Name; + StringRef NameOrErr = unwrapOrError(Section.getName(), O->getFileName()); + Fmt << NameOrErr; return; } @@ -488,10 +483,14 @@ static void printRelocationTargetName(const MachOObjectFile *O, --I; advance(SI, 1); } - if (SI == O->section_end()) + if (SI == O->section_end()) { Fmt << Val << " (?,?)"; - else - SI->getName(S); + } else { + if (Expected NameOrErr = SI->getName()) + S = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + } } Fmt << S; @@ -1531,7 +1530,12 @@ static void DumpLiteralPointerSection(MachOObjectFile *O, uint64_t SectSize = Sect->getSize(); StringRef SectName; - Sect->getName(SectName); + Expected SectNameOrErr = Sect->getName(); + if (SectNameOrErr) + SectName = *SectNameOrErr; + else + consumeError(SectNameOrErr.takeError()); + DataRefImpl Ref = Sect->getRawDataRefImpl(); StringRef SegmentName = O->getSectionFinalSegmentName(Ref); outs() << SegmentName << ":" << SectName << ":"; @@ -1743,7 +1747,12 @@ static void DumpSectionContents(StringRef Filename, MachOObjectFile *O, } for (const SectionRef &Section : O->sections()) { StringRef SectName; - Section.getName(SectName); + Expected SecNameOrErr = Section.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = Section.getRawDataRefImpl(); StringRef SegName = O->getSectionFinalSegmentName(Ref); if ((DumpSegName.empty() || SegName == DumpSegName) && @@ -1839,7 +1848,12 @@ static void DumpInfoPlistSectionContents(StringRef Filename, MachOObjectFile *O) { for (const SectionRef &Section : O->sections()) { StringRef SectName; - Section.getName(SectName); + Expected SecNameOrErr = Section.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = Section.getRawDataRefImpl(); StringRef SegName = O->getSectionFinalSegmentName(Ref); if (SegName == "__TEXT" && SectName == "__info_plist") { @@ -1936,7 +1950,11 @@ static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF, if (DisassembleAll) { for (const SectionRef &Section : MachOOF->sections()) { StringRef SectName; - Section.getName(SectName); + if (Expected NameOrErr = Section.getName()) + SectName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (SectName.equals("__text")) { DataRefImpl Ref = Section.getRawDataRefImpl(); StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref); @@ -3247,7 +3265,13 @@ static const char *get_pointer_64(uint64_t Address, uint32_t &offset, continue; if (objc_only) { StringRef SectName; - ((*(info->Sections))[SectIdx]).getName(SectName); + Expected SecNameOrErr = + ((*(info->Sections))[SectIdx]).getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); if (SegName != "__OBJC" && SectName != "__cstring") @@ -4039,7 +4063,12 @@ static const SectionRef get_section(MachOObjectFile *O, const char *segname, const char *sectname) { for (const SectionRef &Section : O->sections()) { StringRef SectName; - Section.getName(SectName); + Expected SecNameOrErr = Section.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = Section.getRawDataRefImpl(); StringRef SegName = O->getSectionFinalSegmentName(Ref); if (SegName == segname && SectName == sectname) @@ -4056,7 +4085,12 @@ walk_pointer_list_64(const char *listname, const SectionRef S, return; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -4105,8 +4139,7 @@ walk_pointer_list_32(const char *listname, const SectionRef S, if (S == SectionRef()) return; - StringRef SectName; - S.getName(SectName); + StringRef SectName = unwrapOrError(S.getName(), O->getFileName()); DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -5780,7 +5813,12 @@ static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) { return; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -5843,7 +5881,12 @@ static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) { return; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -5889,7 +5932,12 @@ static void print_image_info64(SectionRef S, struct DisassembleInfo *info) { return; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -5946,7 +5994,12 @@ static void print_image_info32(SectionRef S, struct DisassembleInfo *info) { return; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -5996,7 +6049,12 @@ static void print_image_info(SectionRef S, struct DisassembleInfo *info) { const char *r; StringRef SectName; - S.getName(SectName); + Expected SecNameOrErr = S.getName(); + if (SecNameOrErr) + SectName = *SecNameOrErr; + else + consumeError(SecNameOrErr.takeError()); + DataRefImpl Ref = S.getRawDataRefImpl(); StringRef SegName = info->O->getSectionFinalSegmentName(Ref); outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; @@ -6031,11 +6089,8 @@ static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) { CreateSymbolAddressMap(O, &AddrMap); std::vector Sections; - for (const SectionRef &Section : O->sections()) { - StringRef SectName; - Section.getName(SectName); + for (const SectionRef &Section : O->sections()) Sections.push_back(Section); - } struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); @@ -6116,11 +6171,8 @@ static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) { CreateSymbolAddressMap(O, &AddrMap); std::vector Sections; - for (const SectionRef &Section : O->sections()) { - StringRef SectName; - Section.getName(SectName); + for (const SectionRef &Section : O->sections()) Sections.push_back(Section); - } struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); @@ -6214,11 +6266,8 @@ static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) { CreateSymbolAddressMap(O, &AddrMap); std::vector Sections; - for (const SectionRef &Section : O->sections()) { - StringRef SectName; - Section.getName(SectName); + for (const SectionRef &Section : O->sections()) Sections.push_back(Section); - } struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); @@ -6375,11 +6424,8 @@ static void DumpProtocolSection(MachOObjectFile *O, const char *sect, CreateSymbolAddressMap(O, &AddrMap); std::vector Sections; - for (const SectionRef &Section : O->sections()) { - StringRef SectName; - Section.getName(SectName); + for (const SectionRef &Section : O->sections()) Sections.push_back(Section); - } struct DisassembleInfo info(O, &AddrMap, &Sections, true); @@ -7344,8 +7390,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, outs() << "(" << DisSegName << "," << DisSectName << ") section\n"; for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { - StringRef SectName; - if (Sections[SectIdx].getName(SectName) || SectName != DisSectName) + Expected SecNameOrErr = Sections[SectIdx].getName(); + if (!SecNameOrErr) { + consumeError(SecNameOrErr.takeError()); + continue; + } + if (*SecNameOrErr != DisSectName) continue; DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); @@ -7742,8 +7792,12 @@ static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, auto Sym = Symbols.upper_bound(Addr); if (Sym == Symbols.begin()) { // The first symbol in the object is after this reference, the best we can - // do is section-relative notation. - RelocSection.getName(Name); + // do is section-relative notation. + if (Expected NameOrErr = RelocSection.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + Addend = Addr - SectionAddr; return; } @@ -7762,7 +7816,11 @@ static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, // There is a symbol before this reference, but it's in a different // section. Probably not helpful to mention it, so use the section name. - RelocSection.getName(Name); + if (Expected NameOrErr = RelocSection.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + Addend = Addr - SectionAddr; } @@ -8127,7 +8185,11 @@ void printMachOUnwindInfo(const MachOObjectFile *Obj) { for (const SectionRef &Section : Obj->sections()) { StringRef SectName; - Section.getName(SectName); + if (Expected NameOrErr = Section.getName()) + SectName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (SectName == "__compact_unwind") printMachOCompactUnwindSection(Obj, Symbols, Section); else if (SectName == "__unwind_info") diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 93833858282a3..7a04e1195ed0a 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -344,10 +344,14 @@ typedef std::vector> SectionSymbolsTy; static bool shouldKeep(object::SectionRef S) { if (FilterSections.empty()) return true; - StringRef SecName; - std::error_code error = S.getName(SecName); - if (error) + + Expected SecNameOrErr = S.getName(); + if (!SecNameOrErr) { + consumeError(SecNameOrErr.takeError()); return false; + } + StringRef SecName = *SecNameOrErr; + // StringSet does not allow empty key so avoid adding sections with // no name (such as the section with index 0) here. if (!SecName.empty()) @@ -920,10 +924,12 @@ static void addPltEntries(const ObjectFile *Obj, StringSaver &Saver) { Optional Plt = None; for (const SectionRef &Section : Obj->sections()) { - StringRef Name; - if (Section.getName(Name)) + Expected SecNameOrErr = Section.getName(); + if (!SecNameOrErr) { + consumeError(SecNameOrErr.takeError()); continue; - if (Name == ".plt") + } + if (*SecNameOrErr == ".plt") Plt = Section; } if (!Plt) @@ -1206,9 +1212,8 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, DataRefImpl DR = Section.getRawDataRefImpl(); SegmentName = MachO->getSectionFinalSegmentName(DR); } - StringRef SectionName; - error(Section.getName(SectionName)); + StringRef SectionName = unwrapOrError(Section.getName(), Obj->getFileName()); // If the section has no symbol at the start, just insert a dummy one. if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) { Symbols.insert( @@ -1581,8 +1586,7 @@ void printRelocations(const ObjectFile *Obj) { } for (std::pair> &P : SecToRelSec) { - StringRef SecName; - error(P.first.getName(SecName)); + StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName()); outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n"; for (SectionRef Section : P.second) { @@ -1654,8 +1658,7 @@ void printSectionHeaders(const ObjectFile *Obj) { "Idx Name Size VMA Type\n"; for (const SectionRef &Section : ToolSectionFilter(*Obj)) { - StringRef Name; - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName()); uint64_t VMA = Section.getAddress(); if (shouldAdjustVA(Section)) VMA += AdjustVMA; @@ -1682,8 +1685,7 @@ void printSectionHeaders(const ObjectFile *Obj) { void printSectionContents(const ObjectFile *Obj) { for (const SectionRef &Section : ToolSectionFilter(*Obj)) { - StringRef Name; - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName()); uint64_t BaseAddr = Section.getAddress(); uint64_t Size = Section.getSize(); if (!Size) @@ -1747,11 +1749,16 @@ void printSymbolTable(const ObjectFile *O, StringRef ArchiveName, section_iterator Section = unwrapOrError(Symbol.getSection(), ArchiveName, FileName, ArchitectureName); StringRef Name; - if (Type == SymbolRef::ST_Debug && Section != O->section_end()) - Section->getName(Name); - else + if (Type == SymbolRef::ST_Debug && Section != O->section_end()) { + if (Expected NameOrErr = Section->getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + + } else { Name = unwrapOrError(Symbol.getName(), ArchiveName, FileName, ArchitectureName); + } bool Global = Flags & SymbolRef::SF_Global; bool Weak = Flags & SymbolRef::SF_Weak; @@ -1797,8 +1804,8 @@ void printSymbolTable(const ObjectFile *O, StringRef ArchiveName, StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); outs() << SegmentName << ","; } - StringRef SectionName; - error(Section->getName(SectionName)); + StringRef SectionName = + unwrapOrError(Section->getName(), O->getFileName()); outs() << SectionName; } @@ -1871,7 +1878,11 @@ void printRawClangAST(const ObjectFile *Obj) { Optional ClangASTSection; for (auto Sec : ToolSectionFilter(*Obj)) { StringRef Name; - Sec.getName(Name); + if (Expected NameOrErr = Sec.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == ClangASTSectionName) { ClangASTSection = Sec; break; @@ -1903,7 +1914,11 @@ static void printFaultMaps(const ObjectFile *Obj) { for (auto Sec : ToolSectionFilter(*Obj)) { StringRef Name; - Sec.getName(Name); + if (Expected NameOrErr = Sec.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == FaultMapSectionName) { FaultMapSection = Sec; break; diff --git a/tools/llvm-pdbutil/DumpOutputStyle.cpp b/tools/llvm-pdbutil/DumpOutputStyle.cpp index 962d4cf88a8a6..288e76cc17396 100644 --- a/tools/llvm-pdbutil/DumpOutputStyle.cpp +++ b/tools/llvm-pdbutil/DumpOutputStyle.cpp @@ -1369,9 +1369,10 @@ Error DumpOutputStyle::dumpTypesFromObjectFile() { LazyRandomTypeCollection Types(100); for (const auto &S : getObj().sections()) { - StringRef SectionName; - if (auto EC = S.getName(SectionName)) - return errorCodeToError(EC); + Expected NameOrErr = S.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef SectionName = *NameOrErr; // .debug$T is a standard CodeView type section, while .debug$P is the same // format but used for MSVC precompiled header object files. diff --git a/tools/llvm-pdbutil/InputFile.cpp b/tools/llvm-pdbutil/InputFile.cpp index bd23bfdbe31a2..8380054b63113 100644 --- a/tools/llvm-pdbutil/InputFile.cpp +++ b/tools/llvm-pdbutil/InputFile.cpp @@ -66,12 +66,13 @@ getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index) { static inline bool isCodeViewDebugSubsection(object::SectionRef Section, StringRef Name, BinaryStreamReader &Reader) { - StringRef SectionName; - if (Section.getName(SectionName)) - return false; - - if (SectionName != Name) + if (Expected NameOrErr = Section.getName()) { + if (*NameOrErr != Name) + return false; + } else { + consumeError(NameOrErr.takeError()); return false; + } Expected ContentsOrErr = Section.getContents(); if (!ContentsOrErr) { diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index d9c45cbdc528b..c7ccbe9ee4b26 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -891,16 +891,14 @@ void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} void COFFDumper::printCodeViewDebugInfo() { // Print types first to build CVUDTNames, then print symbols. for (const SectionRef &S : Obj->sections()) { - StringRef SectionName; - error(S.getName(SectionName)); + StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); // .debug$T is a standard CodeView type section, while .debug$P is the same // format but used for MSVC precompiled header object files. if (SectionName == ".debug$T" || SectionName == ".debug$P") printCodeViewTypeSection(SectionName, S); } for (const SectionRef &S : Obj->sections()) { - StringRef SectionName; - error(S.getName(SectionName)); + StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); if (SectionName == ".debug$S") printCodeViewSymbolSection(SectionName, S); } @@ -1242,8 +1240,7 @@ void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs, GlobalTypeTableBuilder &GlobalCVTypes, bool GHash) { for (const SectionRef &S : Obj->sections()) { - StringRef SectionName; - error(S.getName(SectionName)); + StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); if (SectionName == ".debug$T") { StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents()); uint32_t Magic; @@ -1311,8 +1308,7 @@ void COFFDumper::printSectionHeaders() { ++SectionNumber; const coff_section *Section = Obj->getCOFFSection(Sec); - StringRef Name; - error(Sec.getName(Name)); + StringRef Name = unwrapOrError(Obj->getFileName(), Sec.getName()); DictScope D(W, "Section"); W.printNumber("Number", SectionNumber); @@ -1359,8 +1355,7 @@ void COFFDumper::printRelocations() { int SectionNumber = 0; for (const SectionRef &Section : Obj->sections()) { ++SectionNumber; - StringRef Name; - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); bool PrintedGroup = false; for (const RelocationRef &Reloc : Section.relocations()) { @@ -1689,9 +1684,7 @@ void COFFDumper::printCOFFExports() { void COFFDumper::printCOFFDirectives() { for (const SectionRef &Section : Obj->sections()) { - StringRef Name; - - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); if (Name != ".drectve") continue; @@ -1730,8 +1723,7 @@ void COFFDumper::printCOFFBaseReloc() { void COFFDumper::printCOFFResources() { ListScope ResourcesD(W, "Resources"); for (const SectionRef &S : Obj->sections()) { - StringRef Name; - error(S.getName(Name)); + StringRef Name = unwrapOrError(Obj->getFileName(), S.getName()); if (!Name.startswith(".rsrc")) continue; @@ -1855,7 +1847,11 @@ void COFFDumper::printStackMap() const { object::SectionRef StackMapSection; for (auto Sec : Obj->sections()) { StringRef Name; - Sec.getName(Name); + if (Expected NameOrErr = Sec.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == ".llvm_stackmaps") { StackMapSection = Sec; break; @@ -1882,7 +1878,11 @@ void COFFDumper::printAddrsig() { object::SectionRef AddrsigSection; for (auto Sec : Obj->sections()) { StringRef Name; - Sec.getName(Name); + if (Expected NameOrErr = Sec.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == ".llvm_addrsig") { AddrsigSection = Sec; break; diff --git a/tools/llvm-readobj/ELFDumper.cpp b/tools/llvm-readobj/ELFDumper.cpp index 67ee182539dd5..08e511a00c43c 100644 --- a/tools/llvm-readobj/ELFDumper.cpp +++ b/tools/llvm-readobj/ELFDumper.cpp @@ -4669,7 +4669,11 @@ void DumpStyle::printNonRelocatableStackSizes( StringRef FileStr = Obj->getFileName(); for (const SectionRef &Sec : Obj->sections()) { StringRef SectionName; - Sec.getName(SectionName); + if (Expected NameOrErr = Sec.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl()); if (!SectionName.startswith(".stack_sizes")) continue; @@ -4717,7 +4721,11 @@ void DumpStyle::printRelocatableStackSizes( for (const SectionRef &Sec : Obj->sections()) { StringRef SectionName; - Sec.getName(SectionName); + if (Expected NameOrErr = Sec.getName()) + SectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + // A stack size section that we haven't encountered yet is mapped to the // null section until we find its corresponding relocation section. if (SectionName.startswith(".stack_sizes")) @@ -4754,7 +4762,11 @@ void DumpStyle::printRelocatableStackSizes( // Warn about stack size sections without a relocation section. StringRef StackSizeSectionName; - StackSizesSec.getName(StackSizeSectionName); + if (Expected NameOrErr = StackSizesSec.getName()) + StackSizeSectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (RelocSec == NullSection) { reportWarning(" '" + FileStr + "': section " + StackSizeSectionName + " does not have a corresponding " @@ -4782,7 +4794,12 @@ void DumpStyle::printRelocatableStackSizes( for (const RelocationRef &Reloc : RelocSec.relocations()) { if (!IsSupportedFn(Reloc.getType())) { StringRef RelocSectionName; - RelocSec.getName(RelocSectionName); + Expected NameOrErr = RelocSec.getName(); + if (NameOrErr) + RelocSectionName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + StringRef RelocName = EF->getRelocationTypeName(Reloc.getType()); reportError( createStringError(object_error::parse_failed, diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 17883df49e585..d1c88c2a90e68 100644 --- a/tools/llvm-readobj/MachODumper.cpp +++ b/tools/llvm-readobj/MachODumper.cpp @@ -440,10 +440,7 @@ void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) { MachOSection MOSection; getSection(Obj, Section.getRawDataRefImpl(), MOSection); DataRefImpl DR = Section.getRawDataRefImpl(); - - StringRef Name; - error(Section.getName(Name)); - + StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); ArrayRef RawName = Obj->getSectionRawName(DR); StringRef SegmentName = Obj->getSectionFinalSegmentName(DR); ArrayRef RawSegmentName = Obj->getSectionRawFinalSegmentName(DR); @@ -494,9 +491,7 @@ void MachODumper::printRelocations() { std::error_code EC; for (const SectionRef &Section : Obj->sections()) { - StringRef Name; - error(Section.getName(Name)); - + StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); bool PrintedGroup = false; for (const RelocationRef &Reloc : Section.relocations()) { if (!PrintedGroup) { @@ -541,9 +536,8 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj, } } else if (!IsScattered) { section_iterator SecI = Obj->getRelocationSection(DR); - if (SecI != Obj->section_end()) { - error(SecI->getName(TargetName)); - } + if (SecI != Obj->section_end()) + TargetName = unwrapOrError(Obj->getFileName(), SecI->getName()); } if (TargetName.empty()) TargetName = "-"; @@ -614,7 +608,7 @@ void MachODumper::printSymbol(const SymbolRef &Symbol) { error(errorToErrorCode(SecIOrErr.takeError())); section_iterator SecI = *SecIOrErr; if (SecI != Obj->section_end()) - error(SecI->getName(SectionName)); + SectionName = unwrapOrError(Obj->getFileName(), SecI->getName()); DictScope D(W, "Symbol"); W.printNumber("Name", SymbolName, MOSymbol.StringIndex); @@ -644,7 +638,11 @@ void MachODumper::printStackMap() const { object::SectionRef StackMapSection; for (auto Sec : Obj->sections()) { StringRef Name; - Sec.getName(Name); + if (Expected NameOrErr = Sec.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == "__llvm_stackmaps") { StackMapSection = Sec; break; diff --git a/tools/llvm-readobj/ObjDumper.cpp b/tools/llvm-readobj/ObjDumper.cpp index c16b7ec081a2c..a055e6e239dc9 100644 --- a/tools/llvm-readobj/ObjDumper.cpp +++ b/tools/llvm-readobj/ObjDumper.cpp @@ -49,8 +49,7 @@ getSectionRefsByNameOrIndex(const object::ObjectFile *Obj, SecIndex = Obj->isELF() ? 0 : 1; for (object::SectionRef SecRef : Obj->sections()) { - StringRef SecName; - error(SecRef.getName(SecName)); + StringRef SecName = unwrapOrError(Obj->getFileName(), SecRef.getName()); auto NameIt = SecNames.find(SecName); if (NameIt != SecNames.end()) NameIt->second = true; @@ -77,8 +76,9 @@ void ObjDumper::printSectionsAsString(const object::ObjectFile *Obj, bool First = true; for (object::SectionRef Section : getSectionRefsByNameOrIndex(Obj, Sections)) { - StringRef SectionName; - error(Section.getName(SectionName)); + StringRef SectionName = + unwrapOrError(Obj->getFileName(), Section.getName()); + if (!First) W.startLine() << '\n'; First = false; @@ -111,8 +111,9 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile *Obj, bool First = true; for (object::SectionRef Section : getSectionRefsByNameOrIndex(Obj, Sections)) { - StringRef SectionName; - error(Section.getName(SectionName)); + StringRef SectionName = + unwrapOrError(Obj->getFileName(), Section.getName()); + if (!First) W.startLine() << '\n'; First = false; diff --git a/tools/llvm-readobj/WasmDumper.cpp b/tools/llvm-readobj/WasmDumper.cpp index 2ffc74b3398fb..1516e53911832 100644 --- a/tools/llvm-readobj/WasmDumper.cpp +++ b/tools/llvm-readobj/WasmDumper.cpp @@ -133,8 +133,8 @@ void WasmDumper::printRelocations() { int SectionNumber = 0; for (const SectionRef &Section : Obj->sections()) { bool PrintedGroup = false; - StringRef Name; - error(Section.getName(Name)); + StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); + ++SectionNumber; for (const RelocationRef &Reloc : Section.relocations()) { diff --git a/tools/llvm-readobj/Win64EHDumper.cpp b/tools/llvm-readobj/Win64EHDumper.cpp index 9311a061f6aac..fa268ce9d4348 100644 --- a/tools/llvm-readobj/Win64EHDumper.cpp +++ b/tools/llvm-readobj/Win64EHDumper.cpp @@ -306,7 +306,10 @@ void Dumper::printRuntimeFunction(const Context &Ctx, void Dumper::printData(const Context &Ctx) { for (const auto &Section : Ctx.COFF.sections()) { StringRef Name; - Section.getName(Name); + if (Expected NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); if (Name != ".pdata" && !Name.startswith(".pdata$")) continue; diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index a7cc1deb8cf63..818670a2088c5 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -441,8 +441,6 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { continue; } object::section_iterator Sec = *SecOrErr; - StringRef SecName; - Sec->getName(SecName); Address.SectionIndex = Sec->getIndex(); uint64_t SectionLoadAddress = LoadedObjInfo->getSectionLoadAddress(*Sec); diff --git a/tools/llvm-size/llvm-size.cpp b/tools/llvm-size/llvm-size.cpp index 89b89f0627508..87df1264fbca2 100644 --- a/tools/llvm-size/llvm-size.cpp +++ b/tools/llvm-size/llvm-size.cpp @@ -106,17 +106,6 @@ static bool HadError = false; static std::string ToolName; -/// If ec is not success, print the error and return true. -static bool error(std::error_code ec) { - if (!ec) - return false; - - HadError = true; - errs() << ToolName << ": error reading file: " << ec.message() << ".\n"; - errs().flush(); - return true; -} - static bool error(Twine Message) { HadError = true; errs() << ToolName << ": " << Message << ".\n"; @@ -397,11 +386,14 @@ static void printObjectSectionSizes(ObjectFile *Obj) { uint64_t size = Section.getSize(); total += size; - StringRef name; - if (error(Section.getName(name))) + Expected name_or_err = Section.getName(); + if (!name_or_err) { + error(name_or_err.takeError(), Obj->getFileName()); return; + } + uint64_t addr = Section.getAddress(); - max_name_len = std::max(max_name_len, name.size()); + max_name_len = std::max(max_name_len, name_or_err->size()); max_size_len = std::max(max_size_len, getNumLengthAsString(size)); max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr)); } @@ -431,14 +423,16 @@ static void printObjectSectionSizes(ObjectFile *Obj) { for (const SectionRef &Section : Obj->sections()) { if (!considerForSize(Obj, Section)) continue; - StringRef name; - if (error(Section.getName(name))) + + Expected name_or_err = Section.getName(); + if (!name_or_err) { + error(name_or_err.takeError(), Obj->getFileName()); return; + } + uint64_t size = Section.getSize(); uint64_t addr = Section.getAddress(); - std::string namestr = name; - - outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr); + outs() << format(fmt.str().c_str(), name_or_err->str().c_str(), size, addr); } if (ELFCommons) { diff --git a/tools/obj2yaml/coff2yaml.cpp b/tools/obj2yaml/coff2yaml.cpp index a05840f80bb68..e2b18807cda55 100644 --- a/tools/obj2yaml/coff2yaml.cpp +++ b/tools/obj2yaml/coff2yaml.cpp @@ -112,10 +112,14 @@ initializeFileAndStringTable(const llvm::object::COFFObjectFile &Obj, if (SC.hasStrings() && SC.hasChecksums()) break; - StringRef SectionName; - S.getName(SectionName); + Expected SectionNameOrErr = S.getName(); + if (!SectionNameOrErr) { + consumeError(SectionNameOrErr.takeError()); + continue; + } + ArrayRef sectionData; - if (SectionName != ".debug$S") + if ((*SectionNameOrErr) != ".debug$S") continue; const object::coff_section *COFFSection = Obj.getCOFFSection(S); @@ -155,7 +159,12 @@ void COFFDumper::dumpSections(unsigned NumSections) { for (const auto &ObjSection : Obj.sections()) { const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection); COFFYAML::Section NewYAMLSection; - ObjSection.getName(NewYAMLSection.Name); + + if (Expected NameOrErr = ObjSection.getName()) + NewYAMLSection.Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + NewYAMLSection.Header.Characteristics = COFFSection->Characteristics; NewYAMLSection.Header.VirtualAddress = COFFSection->VirtualAddress; NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;