: Chunk(SectionKind), File(F), Header(H),
Relocs(File->getCOFFObj()->getRelocations(Header)), Repl(this) {
// Initialize SectionName.
- File->getCOFFObj()->getSectionName(Header, SectionName);
+ if (Expected<StringRef> E = File->getCOFFObj()->getSectionName(Header))
+ SectionName = *E;
Alignment = Header->getAlignment();
const coff_section *Sec = getSection(SectionNumber);
StringRef Name;
- if (auto EC = COFFObj->getSectionName(Sec, Name))
+ if (Expected<StringRef> E = COFFObj->getSectionName(Sec))
+ Name = *E;
+ else
fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
- EC.message());
+ toString(E.takeError()));
if (Name == ".drectve") {
ArrayRef<uint8_t> Data;
COFFObj->getSymbolName(Sym, Name);
const coff_section *ParentSec = getSection(ParentIndex);
- COFFObj->getSectionName(ParentSec, ParentName);
+ if (Expected<StringRef> E = COFFObj->getSectionName(ParentSec))
+ ParentName = *E;
error(toString(this) + ": associative comdat " + Name + " (sec " +
Twine(SectionNumber) + ") has invalid reference to section " +
ParentName + " (sec " + Twine(ParentIndex) + ")");
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
- std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
ArrayRef<coff_relocation> getRelocations(const coff_section *Sec) const;
- std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
+ Expected<StringRef> getSectionName(const coff_section *Sec) const;
uint64_t getSectionSize(const coff_section *Sec) const;
std::error_code getSectionContents(const coff_section *Sec,
ArrayRef<uint8_t> &Res) const;
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
- std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
}
template <class ELFT>
-std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
- StringRef &Result) const {
- auto Name = EF.getSectionName(&*getSection(Sec));
- if (!Name)
- return errorToErrorCode(Name.takeError());
- Result = *Name;
- return std::error_code();
+Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const {
+ return EF.getSectionName(&*getSection(Sec));
}
template <class ELFT>
unsigned getSectionID(SectionRef Sec) const;
void moveSectionNext(DataRefImpl &Sec) const override;
- std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
friend class SectionRef;
virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
- virtual std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const = 0;
+ virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0;
virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
}
inline std::error_code SectionRef::getName(StringRef &Result) const {
- return OwningObject->getSectionName(SectionPimpl, Result);
+ Expected<StringRef> NameOrErr = OwningObject->getSectionName(SectionPimpl);
+ if (!NameOrErr)
+ return errorToErrorCode(NameOrErr.takeError());
+ Result = *NameOrErr;
+ return std::error_code();
}
inline uint64_t SectionRef::getAddress() const {
// Overrides from SectionRef.
void moveSectionNext(DataRefImpl &Sec) const override;
- std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
- std::error_code getSectionName(DataRefImpl Sec,
- StringRef &Res) const override;
+ Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
Ref.p = reinterpret_cast<uintptr_t>(Sec);
}
-std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
- StringRef &Result) const {
+Expected<StringRef> COFFObjectFile::getSectionName(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref);
- return getSectionName(Sec, Result);
+ return getSectionName(Sec);
}
uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
return Index;
}
-std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
- StringRef &Res) const {
+Expected<StringRef>
+COFFObjectFile::getSectionName(const coff_section *Sec) const {
StringRef Name;
if (Sec->Name[COFF::NameSize - 1] == 0)
// Null terminated, let ::strlen figure out the length.
uint32_t Offset;
if (Name.startswith("//")) {
if (decodeBase64StringEntry(Name.substr(2), Offset))
- return object_error::parse_failed;
+ return createStringError(object_error::parse_failed,
+ "inalid section name");
} else {
if (Name.substr(1).getAsInteger(10, Offset))
- return object_error::parse_failed;
+ return createStringError(object_error::parse_failed,
+ "invalid section name");
}
if (std::error_code EC = getString(Offset, Name))
- return EC;
+ return errorCodeToError(EC);
}
- Res = Name;
- return std::error_code();
+ return Name;
}
uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
Sec.d.a++;
}
-std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
- StringRef &Result) const {
+Expected<StringRef> MachOObjectFile::getSectionName(DataRefImpl Sec) const {
ArrayRef<char> Raw = getSectionRawName(Sec);
- Result = parseSegmentOrSectionName(Raw.data());
- return std::error_code();
+ return parseSegmentOrSectionName(Raw.data());
}
uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {
StringRef SegmentName = getSectionFinalSegmentName(Sec);
- StringRef SectName;
- if (!getSectionName(Sec, SectName))
- return (SegmentName == "__LLVM" && SectName == "__bitcode");
+ if (Expected<StringRef> NameOrErr = getSectionName(Sec))
+ return (SegmentName == "__LLVM" && *NameOrErr == "__bitcode");
return false;
}
uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
- StringRef SectName;
- if (!getSectionName(Sec, SectName))
- return SectName == ".llvmbc";
+ if (Expected<StringRef> NameOrErr = getSectionName(Sec))
+ return *NameOrErr == ".llvmbc";
return false;
}
void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
-std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
- StringRef &Res) const {
+Expected<StringRef> WasmObjectFile::getSectionName(DataRefImpl Sec) const {
const WasmSection &S = Sections[Sec.d.a];
#define ECase(X) \
case wasm::WASM_SEC_##X: \
- Res = #X; \
- break
+ return #X;
switch (S.Type) {
ECase(TYPE);
ECase(IMPORT);
ECase(DATA);
ECase(DATACOUNT);
case wasm::WASM_SEC_CUSTOM:
- Res = S.Name;
- break;
+ return S.Name;
default:
- return object_error::invalid_section_index;
+ return createStringError(object_error::invalid_section_index, "");
}
#undef ECase
- return std::error_code();
}
uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
}
-std::error_code XCOFFObjectFile::getSectionName(DataRefImpl Sec,
- StringRef &Res) const {
+Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
const char *Name = toSection(Sec)->Name;
auto NulCharPtr =
static_cast<const char *>(memchr(Name, '\0', XCOFF::SectionNameSize));
- Res = NulCharPtr ? StringRef(Name, NulCharPtr - Name)
- : StringRef(Name, XCOFF::SectionNameSize);
- return std::error_code();
+ return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
+ : StringRef(Name, XCOFF::SectionNameSize);
}
uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
}
DataRefImpl Ref = Sec->getRawDataRefImpl();
StringRef SectionName;
- MachO->getSectionName(Ref, SectionName);
+ if (Expected<StringRef> NameOrErr = MachO->getSectionName(Ref))
+ SectionName = *NameOrErr;
StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
outs() << "(" << SegmentName << "," << SectionName << ") ";
break;
section_iterator SecI = *SecIOrErr;
const coff_section *Section = Obj.getCOFFSection(*SecI);
Characteristics = Section->Characteristics;
- StringRef SectionName;
- Obj.getSectionName(Section, SectionName);
- if (SectionName.startswith(".idata"))
- return 'i';
+ if (Expected<StringRef> NameOrErr = Obj.getSectionName(Section))
+ if (NameOrErr->startswith(".idata"))
+ return 'i';
}
switch (Symb.getSectionNumber()) {
return 's';
DataRefImpl Ref = Sec->getRawDataRefImpl();
StringRef SectionName;
- Obj.getSectionName(Ref, SectionName);
+ if (Expected<StringRef> NameOrErr = Obj.getSectionName(Ref))
+ SectionName = *NameOrErr;
StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
if (Obj.is64Bit() && Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
SegmentName == "__TEXT_EXEC" && SectionName == "__text")
for (auto &S : Obj->sections()) {
DataRefImpl Ref = S.getRawDataRefImpl();
StringRef SectionName;
- Obj->getSectionName(Ref, SectionName);
+ if (Expected<StringRef> NameOrErr = Obj->getSectionName(Ref))
+ SectionName = *NameOrErr;
StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
if (SegmentName == SegSect[0] && SectionName == SegSect[1])
return Nsect;
ArrayRef<coff_relocation> Relocs = COFFObj.getRelocations(Sec);
for (const coff_relocation &R : Relocs)
S.Relocs.push_back(R);
- if (auto EC = COFFObj.getSectionName(Sec, S.Name))
- return errorCodeToError(EC);
+ if (Expected<StringRef> NameOrErr = COFFObj.getSectionName(Sec))
+ S.Name = *NameOrErr;
+ else
+ return NameOrErr.takeError();
if (Sec->hasExtendedRelocations())
return createStringError(object_error::parse_failed,
"Extended relocations not supported yet");
object::DataRefImpl DRI;
DRI.d.a = r_symbolnum-1;
StringRef SegName = O->getSectionFinalSegmentName(DRI);
- StringRef SectName;
- if (O->getSectionName(DRI, SectName))
- outs() << "(?,?)\n";
+ if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
+ outs() << "(" << SegName << "," << *NameOrErr << ")\n";
else
- outs() << "(" << SegName << "," << SectName << ")\n";
+ outs() << "(?,?)\n";
}
else {
outs() << "(?,?)\n";
DataRefImpl DRI;
DRI.d.a = J;
const StringRef SegName = O->getSectionFinalSegmentName(DRI);
- StringRef SectName;
- if (O->getSectionName(DRI, SectName))
+ if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
+ outs() << "Relocation information (" << SegName << "," << *NameOrErr
+ << format(") %u entries", Sec.nreloc);
+ else
outs() << "Relocation information (" << SegName << ",?) "
<< format("%u entries", Sec.nreloc);
- else
- outs() << "Relocation information (" << SegName << ","
- << SectName << format(") %u entries", Sec.nreloc);
outs() << "\naddress pcrel length extern type scattered "
"symbolnum/value\n";
PrintRelocationEntries(O, O->section_rel_begin(DRI),
DataRefImpl DRI;
DRI.d.a = J;
const StringRef SegName = O->getSectionFinalSegmentName(DRI);
- StringRef SectName;
- if (O->getSectionName(DRI, SectName))
+ if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
+ outs() << "Relocation information (" << SegName << "," << *NameOrErr
+ << format(") %u entries", Sec.nreloc);
+ else
outs() << "Relocation information (" << SegName << ",?) "
<< format("%u entries", Sec.nreloc);
- else
- outs() << "Relocation information (" << SegName << ","
- << SectName << format(") %u entries", Sec.nreloc);
outs() << "\naddress pcrel length extern type scattered "
"symbolnum/value\n";
PrintRelocationEntries(O, O->section_rel_begin(DRI),
break;
}
-std::error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
+Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
for (const auto &Section : COFF.sections()) {
- StringRef SectionName;
- if (std::error_code EC =
- COFF.getSectionName(COFF.getCOFFSection(Section), SectionName))
- return EC;
+ Expected<StringRef> NameOrErr =
+ COFF.getSectionName(COFF.getCOFFSection(Section));
+ if (!NameOrErr)
+ return NameOrErr.takeError();
- if (SectionName.startswith(".pdata"))
+ if (NameOrErr->startswith(".pdata"))
dumpProcedureData(COFF, Section);
}
- return std::error_code();
+ return Error::success();
}
}
}
Decoder(ScopedPrinter &SW, bool isAArch64) : SW(SW),
OS(SW.getOStream()),
isAArch64(isAArch64) {}
- std::error_code dumpProcedureData(const object::COFFObjectFile &COFF);
+ Error dumpProcedureData(const object::COFFObjectFile &COFF);
};
}
}
void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
-static ErrorOr<StringRef>
+static Expected<StringRef>
getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
const coff_section *Section) {
- if (Section) {
- StringRef SectionName;
- if (std::error_code EC = Obj->getSectionName(Section, SectionName))
- return EC;
- return SectionName;
- }
+ if (Section)
+ return Obj->getSectionName(Section);
if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
return StringRef("IMAGE_SYM_DEBUG");
if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
if (Obj->getSymbolName(Symbol, SymbolName))
SymbolName = "";
- StringRef SectionName = "";
- ErrorOr<StringRef> Res =
- getSectionName(Obj, Symbol.getSectionNumber(), Section);
- if (Res)
- SectionName = *Res;
+ StringRef SectionName;
+ if (Expected<StringRef> NameOrErr =
+ getSectionName(Obj, Symbol.getSectionNumber(), Section))
+ SectionName = *NameOrErr;
W.printString("Name", SymbolName);
W.printNumber("Value", Symbol.getValue());
&& Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
const coff_section *Assoc;
StringRef AssocName = "";
- std::error_code EC = Obj->getSection(AuxNumber, Assoc);
- ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
- if (Res)
- AssocName = *Res;
- if (!EC)
- EC = Res.getError();
- if (EC) {
- AssocName = "";
+ if (std::error_code EC = Obj->getSection(AuxNumber, Assoc))
error(EC);
- }
+ Expected<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
+ if (!Res)
+ error(Res.takeError());
+ AssocName = *Res;
W.printNumber("AssocSection", AssocName, AuxNumber);
}
case COFF::IMAGE_FILE_MACHINE_ARMNT: {
ARM::WinEH::Decoder Decoder(W, Obj->getMachine() ==
COFF::IMAGE_FILE_MACHINE_ARM64);
- Decoder.dumpProcedureData(*Obj);
+ // TODO Propagate the error.
+ consumeError(Decoder.dumpProcedureData(*Obj));
break;
}
default: