Skip to content

Commit 7bd8d99

Browse files
committed
Thread Expected<...> up from libObject’s getType() for symbols to allow llvm-objdump to produce a good error message.
Produce another specific error message for a malformed Mach-O file when a symbol’s section index is more than the number of sections. The existing test case in test/Object/macho-invalid.test for macho-invalid-section-index-getSectionRawName now reports the error with the message indicating that a symbol at a specific index has a bad section index and that bad section index value. Again converting interfaces to Expected<> from ErrorOr<> does involve touching a number of places. Where the existing code reported the error with a string message or an error code it was converted to do the same. Also there some were bugs in the existing code that did not deal with the old ErrorOr<> return values.  So now with Expected<> since they must be checked and the error handled, I added a TODO and a comment: "// TODO: Actually report errors helpfully" and a call something like consumeError(NameOrErr.takeError()) so the buggy code will not crash since needed to deal with the Error. llvm-svn: 268298
1 parent 34c549e commit 7bd8d99

26 files changed

+229
-106
lines changed

llvm/include/llvm/Object/COFF.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ class COFFObjectFile : public ObjectFile {
684684
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
685685
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
686686
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
687-
ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
688-
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
687+
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
688+
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
689689
void moveSectionNext(DataRefImpl &Sec) const override;
690690
std::error_code getSectionName(DataRefImpl Sec,
691691
StringRef &Res) const override;

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
200200
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
201201
uint8_t getSymbolOther(DataRefImpl Symb) const override;
202202
uint8_t getSymbolELFType(DataRefImpl Symb) const override;
203-
ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
204-
ErrorOr<section_iterator> getSymbolSection(const Elf_Sym *Symb,
205-
const Elf_Shdr *SymTab) const;
206-
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
203+
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
204+
Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
205+
const Elf_Shdr *SymTab) const;
206+
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
207207

208208
void moveSectionNext(DataRefImpl &Sec) const override;
209209
std::error_code getSectionName(DataRefImpl Sec,
@@ -440,7 +440,7 @@ uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
440440
}
441441

442442
template <class ELFT>
443-
ErrorOr<SymbolRef::Type>
443+
Expected<SymbolRef::Type>
444444
ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
445445
const Elf_Sym *ESym = getSymbol(Symb);
446446

@@ -512,12 +512,12 @@ uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
512512
}
513513

514514
template <class ELFT>
515-
ErrorOr<section_iterator>
515+
Expected<section_iterator>
516516
ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
517517
const Elf_Shdr *SymTab) const {
518518
ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
519519
if (std::error_code EC = ESecOrErr.getError())
520-
return EC;
520+
return errorCodeToError(EC);
521521

522522
const Elf_Shdr *ESec = *ESecOrErr;
523523
if (!ESec)
@@ -529,7 +529,7 @@ ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
529529
}
530530

531531
template <class ELFT>
532-
ErrorOr<section_iterator>
532+
Expected<section_iterator>
533533
ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
534534
const Elf_Sym *Sym = getSymbol(Symb);
535535
const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a);

llvm/include/llvm/Object/MachO.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ class MachOObjectFile : public ObjectFile {
208208
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
209209
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
210210
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
211-
ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
211+
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
212212
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
213-
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
213+
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
214214
unsigned getSymbolSectionID(SymbolRef Symb) const;
215215
unsigned getSectionID(SectionRef Sec) const;
216216

llvm/include/llvm/Object/ObjectFile.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ class SymbolRef : public BasicSymbolRef {
142142
/// @brief Get the alignment of this symbol as the actual value (not log 2).
143143
uint32_t getAlignment() const;
144144
uint64_t getCommonSize() const;
145-
ErrorOr<SymbolRef::Type> getType() const;
145+
Expected<SymbolRef::Type> getType() const;
146146

147147
/// @brief Get section this symbol is defined in reference to. Result is
148148
/// end_sections() if it is undefined or is an absolute symbol.
149-
ErrorOr<section_iterator> getSection() const;
149+
Expected<section_iterator> getSection() const;
150150

151151
const ObjectFile *getObject() const;
152152
};
@@ -200,8 +200,8 @@ class ObjectFile : public SymbolicFile {
200200
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
201201
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
202202
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
203-
virtual ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
204-
virtual ErrorOr<section_iterator>
203+
virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
204+
virtual Expected<section_iterator>
205205
getSymbolSection(DataRefImpl Symb) const = 0;
206206

207207
// Same as above for SectionRef.
@@ -324,11 +324,11 @@ inline uint64_t SymbolRef::getCommonSize() const {
324324
return getObject()->getCommonSymbolSize(getRawDataRefImpl());
325325
}
326326

327-
inline ErrorOr<section_iterator> SymbolRef::getSection() const {
327+
inline Expected<section_iterator> SymbolRef::getSection() const {
328328
return getObject()->getSymbolSection(getRawDataRefImpl());
329329
}
330330

331-
inline ErrorOr<SymbolRef::Type> SymbolRef::getType() const {
331+
inline Expected<SymbolRef::Type> SymbolRef::getType() const {
332332
return getObject()->getSymbolType(getRawDataRefImpl());
333333
}
334334

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,17 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
763763
}
764764
SymAddr = *SymAddrOrErr;
765765
// Also remember what section this symbol is in for later
766-
RSec = *Sym->getSection();
766+
auto SectOrErr = Sym->getSection();
767+
if (!SectOrErr) {
768+
std::string Buf;
769+
raw_string_ostream OS(Buf);
770+
logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
771+
OS.flush();
772+
errs() << "error: failed to get symbol section: "
773+
<< Buf << '\n';
774+
continue;
775+
}
776+
RSec = *SectOrErr;
767777
} else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
768778
// MachO also has relocations that point to sections and
769779
// scattered relocations.

llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
119119
uint64_t SymbolSize,
120120
DataExtractor *OpdExtractor,
121121
uint64_t OpdAddress) {
122-
ErrorOr<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
123-
if (auto EC = SymbolTypeOrErr.getError())
124-
return EC;
122+
Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
123+
if (!SymbolTypeOrErr)
124+
return errorToErrorCode(SymbolTypeOrErr.takeError());
125125
SymbolRef::Type SymbolType = *SymbolTypeOrErr;
126126
if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
127127
return std::error_code();

llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ void IntelJITEventListener::NotifyObjectEmitted(
113113
std::vector<LineNumberInfo> LineInfo;
114114
std::string SourceFileName;
115115

116-
ErrorOr<SymbolRef::Type> SymTypeOrErr = Sym.getType();
117-
if (!SymTypeOrErr)
116+
Expected<SymbolRef::Type> SymTypeOrErr = Sym.getType();
117+
if (!SymTypeOrErr) {
118+
// TODO: Actually report errors helpfully.
119+
consumeError(SymTypeOrErr.takeError());
118120
continue;
121+
}
119122
SymbolRef::Type SymType = *SymTypeOrErr;
120123
if (SymType != SymbolRef::ST_Function)
121124
continue;

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
211211
if (auto SymTypeOrErr = I->getType())
212212
SymType = *SymTypeOrErr;
213213
else
214-
return errorCodeToError(SymTypeOrErr.getError());
214+
return SymTypeOrErr.takeError();
215215

216216
// Get symbol name.
217217
StringRef Name;
@@ -252,7 +252,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
252252
if (auto SIOrErr = I->getSection())
253253
SI = *SIOrErr;
254254
else
255-
return errorCodeToError(SIOrErr.getError());
255+
return SIOrErr.takeError();
256256

257257
if (SI == Obj.section_end())
258258
continue;

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
877877
if (auto TSIOrErr = TargetSymbol->getSection())
878878
TSI = *TSIOrErr;
879879
else
880-
return errorCodeToError(TSIOrErr.getError());
880+
return TSIOrErr.takeError();
881881
assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
882882

883883
bool IsCode = TSI->isText();
@@ -1210,9 +1210,14 @@ RuntimeDyldELF::processRelocationRef(
12101210
RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
12111211
if (Symbol != Obj.symbol_end()) {
12121212
gsi = GlobalSymbolTable.find(TargetName.data());
1213-
ErrorOr<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1214-
if (std::error_code EC = SymTypeOrErr.getError())
1215-
report_fatal_error(EC.message());
1213+
Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1214+
if (!SymTypeOrErr) {
1215+
std::string Buf;
1216+
raw_string_ostream OS(Buf);
1217+
logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, "");
1218+
OS.flush();
1219+
report_fatal_error(Buf);
1220+
}
12161221
SymType = *SymTypeOrErr;
12171222
}
12181223
if (gsi != GlobalSymbolTable.end()) {
@@ -1226,7 +1231,15 @@ RuntimeDyldELF::processRelocationRef(
12261231
// TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
12271232
// and can be changed by another developers. Maybe best way is add
12281233
// a new symbol type ST_Section to SymbolRef and use it.
1229-
section_iterator si = *Symbol->getSection();
1234+
auto SectionOrErr = Symbol->getSection();
1235+
if (!SectionOrErr) {
1236+
std::string Buf;
1237+
raw_string_ostream OS(Buf);
1238+
logAllUnhandledErrors(SectionOrErr.takeError(), OS, "");
1239+
OS.flush();
1240+
report_fatal_error(Buf);
1241+
}
1242+
section_iterator si = *SectionOrErr;
12301243
if (si == Obj.section_end())
12311244
llvm_unreachable("Symbol section not found, bad object file format!");
12321245
DEBUG(dbgs() << "\t\tThis is section symbol\n");

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
5555
}
5656
StringRef TargetName = *TargetNameOrErr;
5757

58-
auto Section = *Symbol->getSection();
58+
auto SectionOrErr = Symbol->getSection();
59+
if (!SectionOrErr) {
60+
std::string Buf;
61+
raw_string_ostream OS(Buf);
62+
logAllUnhandledErrors(SectionOrErr.takeError(), OS, "");
63+
OS.flush();
64+
report_fatal_error(Buf);
65+
}
66+
auto Section = *SectionOrErr;
5967

6068
uint64_t RelType = RelI->getType();
6169
uint64_t Offset = RelI->getOffset();

0 commit comments

Comments
 (0)