Changes: no changes. A fix for the clang code will be landed right on top.
Original commit message:
SectionRef::getName() returns std::error_code now.
Returning Expected<> instead has multiple benefits.
For example, it forces user to check the error returned.
Also Expected<> may keep a valuable string error message,
what is more useful than having a error code.
(Object\invalid.test was updated to show the new messages printed.)
This patch makes a change for all users to switch to Expected<> version.
Note: in a few places the error returned was ignored before my changes.
In such places I left them ignored. My intention was to convert the interface
used, and not to improve and/or the existent users in this patch.
(Though I think this is good idea for a follow-ups to revisit such places
and either remove consumeError calls or comment each of them to clarify why
it is OK to have them).
Differential revision: https://reviews.llvm.org/D66089
llvm-svn: 368826
if (!SymStrTabOrErr)
return SymStrTabOrErr.takeError();
Expected<StringRef> 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<section_iterator> Sec = getSymbolSection(Sym);
- if (Sec && !(*Sec)->getName(SecName))
- return SecName;
+ if (ESym->getType() == ELF::STT_SECTION) {
+ if (Expected<section_iterator> SecOrErr = getSymbolSection(Sym)) {
+ consumeError(Name.takeError());
+ return (*SecOrErr)->getName();
+ }
}
return Name;
}
void moveNext();
- std::error_code getName(StringRef &Result) const;
+ Expected<StringRef> getName() const;
uint64_t getAddress() const;
uint64_t getIndex() const;
uint64_t getSize() const;
return OwningObject->moveSectionNext(SectionPimpl);
}
-inline std::error_code SectionRef::getName(StringRef &Result) const {
- Expected<StringRef> NameOrErr = OwningObject->getSectionName(SectionPimpl);
- if (!NameOrErr)
- return errorToErrorCode(NameOrErr.takeError());
- Result = *NameOrErr;
- return std::error_code();
+inline Expected<StringRef> SectionRef::getName() const {
+ return OwningObject->getSectionName(SectionPimpl);
}
inline uint64_t SectionRef::getAddress() const {
StringMap<unsigned> 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 });
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;
// 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<StringRef> NameOrErr = Section->getName();
+ if (!NameOrErr)
+ return errorToErrorCode(NameOrErr.takeError());
+
+ if (*NameOrErr == ".opd") {
Expected<StringRef> E = Section->getContents();
if (!E)
return errorToErrorCode(E.takeError());
return false;
for (const SectionRef &Section : Obj->sections()) {
StringRef Name;
- Section.getName(Name);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
Name = Name.substr(Name.find_first_not_of("._"));
if (Name == "gnu_debuglink") {
Expected<StringRef> ContentsOrErr = Section.getContents();
assert((SecRef.getAlignment() <= std::numeric_limits<uint32_t>::max()) &&
"Section alignment does not fit in 32 bits");
- StringRef Name;
- if (auto EC = SecRef.getName(Name))
- return errorCodeToError(EC);
+ Expected<StringRef> NameOrErr = SecRef.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef Name = *NameOrErr;
unsigned SectionIndex = SecRef.getIndex() + 1;
bool IsCode = Section.isText();
bool IsReadOnly = isReadOnlyData(Section);
- StringRef Name;
- if (auto EC = Section.getName(Name))
- return errorCodeToError(EC);
+ Expected<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef Name = *NameOrErr;
uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
// 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<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef Name = *NameOrErr;
StubBufSize = computeSectionStubBufSize(Obj, Section);
// 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<StringRef> NameOrErr = Sec.getName();
+ if (!NameOrErr) {
+ consumeError(NameOrErr.takeError());
+ continue;
+ }
+
+ if (*NameOrErr != "") {
DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
// 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<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef SectionName = *NameOrErr;
if (SectionName == ".got"
|| SectionName == ".toc"
if (RelSecI == Obj.section_end())
continue;
- StringRef RelSectionName;
- if (auto EC = RelSecI->getName(RelSectionName))
- return errorCodeToError(EC);
+ Expected<StringRef> NameOrErr = RelSecI->getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef RelSectionName = *NameOrErr;
if (RelSectionName != ".opd")
continue;
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<StringRef> NameOrErr = Section.getName();
+ if (NameOrErr)
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == ".eh_frame") {
UnregisteredEHFrameSections.push_back(i->second);
break;
for (const auto &Section : Obj.sections()) {
StringRef Name;
- Section.getName(Name);
+ if (Expected<StringRef> 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
// 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<StringRef> 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();
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
const SectionRef &Section) {
StringRef Name;
- Section.getName(Name);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
if (Name == "__nl_symbol_ptr")
return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
const SectionRef &Section) {
StringRef Name;
- Section.getName(Name);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
if (Name == "__jump_table")
return populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
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();
}
}
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<StringRef> SecNameOrErr = Section.getName();
+ if (SecNameOrErr)
+ return isGnuStyle(*SecNameOrErr);
+
+ consumeError(SecNameOrErr.takeError());
+ return false;
}
bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) {
return {};
Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None;
for (const SectionRef &Section : sections()) {
- StringRef Name;
- if (Section.getName(Name))
+ Expected<StringRef> 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")
}
Expected<SectionRef> 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);
}
uint64_t CurSegAddress;
for (const SectionRef &Section : Obj->sections()) {
SectionInfo Info;
- Section.getName(Info.SectionName);
+ Expected<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ consumeError(NameOrErr.takeError());
+ else
+ Info.SectionName = *NameOrErr;
Info.Address = Section.getAddress();
Info.Size = Section.getSize();
Info.SegmentName =
// 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) {
};
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<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ if (stripSuffix(*NameOrErr) == Name)
return Section;
}
return make_error<CoverageMapError>(coveragemap_error::no_data_found);
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<StringRef> NameOrErr = Section.getName();
+ if (NameOrErr)
+ return *NameOrErr == "xray_instr_map";
+ consumeError(NameOrErr.takeError());
+ return false;
});
if (I == Sections.end())
## .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:
## .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:
## 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.
## 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:
// Find the debug_info section.
for (const object::SectionRef &Section : Obj.sections()) {
StringRef SectionName;
- Section.getName(SectionName);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
if (SectionName != "debug_info")
continue;
getSectionByName(const object::ObjectFile &Obj, StringRef SecName) {
for (const object::SectionRef &Section : Obj.sections()) {
StringRef SectionName;
- Section.getName(SectionName);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
if (SectionName != SecName)
continue;
// Avoid checking the PLT since it produces spurious failures on AArch64
// when ignoring DWARF data.
- StringRef SectionName;
- if (!Section.getName(SectionName) && SectionName == ".plt")
+ Expected<StringRef> NameOrErr = Section.getName();
+ if (NameOrErr && *NameOrErr == ".plt")
continue;
+ consumeError(NameOrErr.takeError());
Expected<StringRef> Contents = Section.getContents();
if (!Contents)
auto ObjFormat = OF->getTripleObjectFormat();
for (const auto &Section : OF->sections()) {
StringRef Name;
- if (Section.getName(Name))
+ if (Expected<StringRef> NameOrErr = Section.getName()) {
+ Name = *NameOrErr;
+ } else {
+ consumeError(NameOrErr.takeError());
return 1;
+ }
+
if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
/*AddSegmentInfo=*/false)) {
ProfileNames = Section;
if (Section.isVirtual())
return Error::success();
- StringRef Name;
- if (std::error_code Err = Section.getName(Name))
- return errorCodeToError(Err);
+ Expected<StringRef> NameOrErr = Section.getName();
+ if (!NameOrErr)
+ return NameOrErr.takeError();
+ StringRef Name = *NameOrErr;
Expected<StringRef> ContentsOrErr = Section.getContents();
if (!ContentsOrErr)
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';
consumeError(SecIOrErr.takeError());
return '?';
}
- elf_section_iterator secT = *SecIOrErr;
- secT->getName(SecName);
+
+ Expected<StringRef> NameOrErr = (*SecIOrErr)->getName();
+ if (!NameOrErr) {
+ consumeError(SecIOrErr.takeError());
+ return '?';
+ }
+ SecName = *NameOrErr;
}
}
StringRef SectionName = StringRef();
for (const SectionRef &Section : MachO->sections()) {
S.NSect++;
- Section.getName(SectionName);
+
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
SegmentName = MachO->getSectionFinalSegmentName(
Section.getRawDataRefImpl());
if (S.Address >= Section.getAddress() &&
StringRef SegmentName = StringRef();
StringRef SectionName = StringRef();
for (const SectionRef &Section : MachO->sections()) {
- Section.getName(SectionName);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
SegmentName = MachO->getSectionFinalSegmentName(
Section.getRawDataRefImpl());
F.NSect++;
std::vector<RelocationRef> &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;
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()) {
// 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;
}
--I;
advance(SI, 1);
}
- if (SI == O->section_end())
+ if (SI == O->section_end()) {
Fmt << Val << " (?,?)";
- else
- SI->getName(S);
+ } else {
+ if (Expected<StringRef> NameOrErr = SI->getName())
+ S = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+ }
}
Fmt << S;
uint64_t SectSize = Sect->getSize();
StringRef SectName;
- Sect->getName(SectName);
+ Expected<StringRef> SectNameOrErr = Sect->getName();
+ if (SectNameOrErr)
+ SectName = *SectNameOrErr;
+ else
+ consumeError(SectNameOrErr.takeError());
+
DataRefImpl Ref = Sect->getRawDataRefImpl();
StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
outs() << SegmentName << ":" << SectName << ":";
}
for (const SectionRef &Section : O->sections()) {
StringRef SectName;
- Section.getName(SectName);
+ Expected<StringRef> 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) &&
MachOObjectFile *O) {
for (const SectionRef &Section : O->sections()) {
StringRef SectName;
- Section.getName(SectName);
+ Expected<StringRef> 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") {
if (DisassembleAll) {
for (const SectionRef &Section : MachOOF->sections()) {
StringRef SectName;
- Section.getName(SectName);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (SectName.equals("__text")) {
DataRefImpl Ref = Section.getRawDataRefImpl();
StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
continue;
if (objc_only) {
StringRef SectName;
- ((*(info->Sections))[SectIdx]).getName(SectName);
+ Expected<StringRef> 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")
const char *sectname) {
for (const SectionRef &Section : O->sections()) {
StringRef SectName;
- Section.getName(SectName);
+ Expected<StringRef> 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)
return;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
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";
return;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
return;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
return;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
return;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
const char *r;
StringRef SectName;
- S.getName(SectName);
+ Expected<StringRef> 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";
CreateSymbolAddressMap(O, &AddrMap);
std::vector<SectionRef> 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);
CreateSymbolAddressMap(O, &AddrMap);
std::vector<SectionRef> 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);
CreateSymbolAddressMap(O, &AddrMap);
std::vector<SectionRef> 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);
CreateSymbolAddressMap(O, &AddrMap);
std::vector<SectionRef> 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);
outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
- StringRef SectName;
- if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
+ Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName();
+ if (!SecNameOrErr) {
+ consumeError(SecNameOrErr.takeError());
+ continue;
+ }
+ if (*SecNameOrErr != DisSectName)
continue;
DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
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<StringRef> NameOrErr = RelocSection.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
Addend = Addr - SectionAddr;
return;
}
// 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<StringRef> NameOrErr = RelocSection.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
Addend = Addr - SectionAddr;
}
for (const SectionRef &Section : Obj->sections()) {
StringRef SectName;
- Section.getName(SectName);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ SectName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (SectName == "__compact_unwind")
printMachOCompactUnwindSection(Obj, Symbols, Section);
else if (SectName == "__unwind_info")
static bool shouldKeep(object::SectionRef S) {
if (FilterSections.empty())
return true;
- StringRef SecName;
- std::error_code error = S.getName(SecName);
- if (error)
+
+ Expected<StringRef> 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())
StringSaver &Saver) {
Optional<SectionRef> Plt = None;
for (const SectionRef &Section : Obj->sections()) {
- StringRef Name;
- if (Section.getName(Name))
+ Expected<StringRef> SecNameOrErr = Section.getName();
+ if (!SecNameOrErr) {
+ consumeError(SecNameOrErr.takeError());
continue;
- if (Name == ".plt")
+ }
+ if (*SecNameOrErr == ".plt")
Plt = Section;
}
if (!Plt)
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(
}
for (std::pair<SectionRef, std::vector<SectionRef>> &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) {
"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;
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)
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<StringRef> 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;
StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
outs() << SegmentName << ",";
}
- StringRef SectionName;
- error(Section->getName(SectionName));
+ StringRef SectionName =
+ unwrapOrError(Section->getName(), O->getFileName());
outs() << SectionName;
}
Optional<object::SectionRef> ClangASTSection;
for (auto Sec : ToolSectionFilter(*Obj)) {
StringRef Name;
- Sec.getName(Name);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == ClangASTSectionName) {
ClangASTSection = Sec;
break;
for (auto Sec : ToolSectionFilter(*Obj)) {
StringRef Name;
- Sec.getName(Name);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == FaultMapSectionName) {
FaultMapSection = Sec;
break;
LazyRandomTypeCollection Types(100);
for (const auto &S : getObj().sections()) {
- StringRef SectionName;
- if (auto EC = S.getName(SectionName))
- return errorCodeToError(EC);
+ Expected<StringRef> 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.
static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
StringRef Name,
BinaryStreamReader &Reader) {
- StringRef SectionName;
- if (Section.getName(SectionName))
- return false;
-
- if (SectionName != Name)
+ if (Expected<StringRef> NameOrErr = Section.getName()) {
+ if (*NameOrErr != Name)
+ return false;
+ } else {
+ consumeError(NameOrErr.takeError());
return false;
+ }
Expected<StringRef> ContentsOrErr = Section.getContents();
if (!ContentsOrErr) {
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);
}
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;
++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);
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()) {
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;
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;
object::SectionRef StackMapSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
- Sec.getName(Name);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == ".llvm_stackmaps") {
StackMapSection = Sec;
break;
object::SectionRef AddrsigSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
- Sec.getName(Name);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == ".llvm_addrsig") {
AddrsigSection = Sec;
break;
StringRef FileStr = Obj->getFileName();
for (const SectionRef &Sec : Obj->sections()) {
StringRef SectionName;
- Sec.getName(SectionName);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ SectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
if (!SectionName.startswith(".stack_sizes"))
continue;
for (const SectionRef &Sec : Obj->sections()) {
StringRef SectionName;
- Sec.getName(SectionName);
+ if (Expected<StringRef> 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"))
// Warn about stack size sections without a relocation section.
StringRef StackSizeSectionName;
- StackSizesSec.getName(StackSizeSectionName);
+ if (Expected<StringRef> NameOrErr = StackSizesSec.getName())
+ StackSizeSectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (RelocSec == NullSection) {
reportWarning(" '" + FileStr + "': section " + StackSizeSectionName +
" does not have a corresponding "
for (const RelocationRef &Reloc : RelocSec.relocations()) {
if (!IsSupportedFn(Reloc.getType())) {
StringRef RelocSectionName;
- RelocSec.getName(RelocSectionName);
+ Expected<StringRef> NameOrErr = RelocSec.getName();
+ if (NameOrErr)
+ RelocSectionName = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
StringRef RelocName = EF->getRelocationTypeName(Reloc.getType());
reportError(
createStringError(object_error::parse_failed,
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<char> RawName = Obj->getSectionRawName(DR);
StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
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) {
}
} 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 = "-";
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);
object::SectionRef StackMapSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
- Sec.getName(Name);
+ if (Expected<StringRef> NameOrErr = Sec.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
+
if (Name == "__llvm_stackmaps") {
StackMapSection = Sec;
break;
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;
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;
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;
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()) {
void Dumper::printData(const Context &Ctx) {
for (const auto &Section : Ctx.COFF.sections()) {
StringRef Name;
- Section.getName(Name);
+ if (Expected<StringRef> NameOrErr = Section.getName())
+ Name = *NameOrErr;
+ else
+ consumeError(NameOrErr.takeError());
if (Name != ".pdata" && !Name.startswith(".pdata$"))
continue;
continue;
}
object::section_iterator Sec = *SecOrErr;
- StringRef SecName;
- Sec->getName(SecName);
Address.SectionIndex = Sec->getIndex();
uint64_t SectionLoadAddress =
LoadedObjInfo->getSectionLoadAddress(*Sec);
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";
uint64_t size = Section.getSize();
total += size;
- StringRef name;
- if (error(Section.getName(name)))
+ Expected<StringRef> 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));
}
for (const SectionRef &Section : Obj->sections()) {
if (!considerForSize(Obj, Section))
continue;
- StringRef name;
- if (error(Section.getName(name)))
+
+ Expected<StringRef> 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) {
if (SC.hasStrings() && SC.hasChecksums())
break;
- StringRef SectionName;
- S.getName(SectionName);
+ Expected<StringRef> SectionNameOrErr = S.getName();
+ if (!SectionNameOrErr) {
+ consumeError(SectionNameOrErr.takeError());
+ continue;
+ }
+
ArrayRef<uint8_t> sectionData;
- if (SectionName != ".debug$S")
+ if ((*SectionNameOrErr) != ".debug$S")
continue;
const object::coff_section *COFFSection = Obj.getCOFFSection(S);
for (const auto &ObjSection : Obj.sections()) {
const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
COFFYAML::Section NewYAMLSection;
- ObjSection.getName(NewYAMLSection.Name);
+
+ if (Expected<StringRef> 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;