diff --git a/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h b/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h index 3c4ba1a8795..9ada0871cf0 100644 --- a/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h +++ b/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h @@ -304,8 +304,15 @@ private: private: void buildInitialSymbolTable(const OwnedObject &Obj) { for (auto &Symbol : Obj.getBinary()->symbols()) { - if (Symbol.getFlags() & object::SymbolRef::SF_Undefined) + if (Expected SymbolFlagsOrErr = Symbol.getFlags()) { + if (*SymbolFlagsOrErr & object::SymbolRef::SF_Undefined) + continue; + } else { + // FIXME: Raise an error for bad symbols. + consumeError(SymbolFlagsOrErr.takeError()); continue; + } + Expected SymbolName = Symbol.getName(); // FIXME: Raise an error for bad symbols. if (!SymbolName) { diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index f1386924ecc..3d0afa12bf3 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -896,7 +896,7 @@ protected: uint32_t getSymbolAlignment(DataRefImpl Symb) const override; uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; Expected getSymbolType(DataRefImpl Symb) const override; Expected getSymbolSection(DataRefImpl Symb) const override; void moveSectionNext(DataRefImpl &Sec) const override; diff --git a/include/llvm/Object/COFFImportFile.h b/include/llvm/Object/COFFImportFile.h index 5aa83641111..f38bd898a44 100644 --- a/include/llvm/Object/COFFImportFile.h +++ b/include/llvm/Object/COFFImportFile.h @@ -43,7 +43,7 @@ public: return Error::success(); } - uint32_t getSymbolFlags(DataRefImpl Symb) const override { + Expected getSymbolFlags(DataRefImpl Symb) const override { return SymbolRef::SF_Global; } diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index c6212be6daa..3fbfa9ca42d 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -261,7 +261,7 @@ protected: uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; uint32_t getSymbolAlignment(DataRefImpl Symb) const override; uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; uint8_t getSymbolBinding(DataRefImpl Symb) const override; uint8_t getSymbolOther(DataRefImpl Symb) const override; uint8_t getSymbolELFType(DataRefImpl Symb) const override; @@ -609,7 +609,7 @@ ELFObjectFile::getSymbolType(DataRefImpl Symb) const { } template -uint32_t ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { +Expected ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { const Elf_Sym *ESym = getSymbol(Sym); uint32_t Result = SymbolRef::SF_None; @@ -626,12 +626,23 @@ uint32_t ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION) Result |= SymbolRef::SF_FormatSpecific; - auto DotSymtabSecSyms = EF.symbols(DotSymtabSec); - if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin()) - Result |= SymbolRef::SF_FormatSpecific; - auto DotDynSymSecSyms = EF.symbols(DotDynSymSec); - if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin()) - Result |= SymbolRef::SF_FormatSpecific; + if (Expected SymbolsOrErr = + EF.symbols(DotSymtabSec)) { + // Set the SF_FormatSpecific flag for the 0-index null symbol. + if (ESym == SymbolsOrErr->begin()) + Result |= SymbolRef::SF_FormatSpecific; + } else + // TODO: Test this error. + return SymbolsOrErr.takeError(); + + if (Expected SymbolsOrErr = + EF.symbols(DotDynSymSec)) { + // Set the SF_FormatSpecific flag for the 0-index null symbol. + if (ESym == SymbolsOrErr->begin()) + Result |= SymbolRef::SF_FormatSpecific; + } else + // TODO: Test this error. + return SymbolsOrErr.takeError(); if (EF.getHeader()->e_machine == ELF::EM_ARM) { if (Expected NameOrErr = getSymbolName(Sym)) { diff --git a/include/llvm/Object/IRObjectFile.h b/include/llvm/Object/IRObjectFile.h index 08b92f1bae5..338b1941eca 100644 --- a/include/llvm/Object/IRObjectFile.h +++ b/include/llvm/Object/IRObjectFile.h @@ -38,7 +38,7 @@ public: ~IRObjectFile() override; void moveSymbolNext(DataRefImpl &Symb) const override; Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; basic_symbol_iterator symbol_begin() const override; basic_symbol_iterator symbol_end() const override; diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index 351ee419d6a..f48e0f1dcd5 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -287,7 +287,7 @@ public: uint32_t getSymbolAlignment(DataRefImpl Symb) const override; uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; Expected getSymbolType(DataRefImpl Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; Expected getSymbolSection(DataRefImpl Symb) const override; unsigned getSymbolSectionID(SymbolRef Symb) const; unsigned getSectionID(SectionRef Sec) const; diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index ef82c057012..e7d1dcaec9c 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -296,7 +296,11 @@ public: ObjectFile(const ObjectFile &other) = delete; uint64_t getCommonSymbolSize(DataRefImpl Symb) const { - assert(getSymbolFlags(Symb) & SymbolRef::SF_Common); + Expected SymbolFlagsOrErr = getSymbolFlags(Symb); + if (!SymbolFlagsOrErr) + // TODO: Actually report errors helpfully. + report_fatal_error(SymbolFlagsOrErr.takeError()); + assert(*SymbolFlagsOrErr & SymbolRef::SF_Common); return getCommonSymbolSizeImpl(Symb); } diff --git a/include/llvm/Object/SymbolicFile.h b/include/llvm/Object/SymbolicFile.h index 1398fa134c8..442eeddc583 100644 --- a/include/llvm/Object/SymbolicFile.h +++ b/include/llvm/Object/SymbolicFile.h @@ -129,7 +129,7 @@ public: Error printName(raw_ostream &OS) const; /// Get symbol flags (bitwise OR of SymbolRef::Flags) - uint32_t getFlags() const; + Expected getFlags() const; DataRefImpl getRawDataRefImpl() const; const SymbolicFile *getObject() const; @@ -147,7 +147,7 @@ public: virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const = 0; - virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0; + virtual Expected getSymbolFlags(DataRefImpl Symb) const = 0; virtual basic_symbol_iterator symbol_begin() const = 0; @@ -196,7 +196,7 @@ inline Error BasicSymbolRef::printName(raw_ostream &OS) const { return OwningObject->printSymbolName(OS, SymbolPimpl); } -inline uint32_t BasicSymbolRef::getFlags() const { +inline Expected BasicSymbolRef::getFlags() const { return OwningObject->getSymbolFlags(SymbolPimpl); } diff --git a/include/llvm/Object/TapiFile.h b/include/llvm/Object/TapiFile.h index bc2e04e1cc9..b20cd1a37aa 100644 --- a/include/llvm/Object/TapiFile.h +++ b/include/llvm/Object/TapiFile.h @@ -33,7 +33,7 @@ public: Error printSymbolName(raw_ostream &OS, DataRefImpl DRI) const override; - uint32_t getSymbolFlags(DataRefImpl DRI) const override; + Expected getSymbolFlags(DataRefImpl DRI) const override; basic_symbol_iterator symbol_begin() const override; diff --git a/include/llvm/Object/Wasm.h b/include/llvm/Object/Wasm.h index 87d82558562..dc90c891ab9 100644 --- a/include/llvm/Object/Wasm.h +++ b/include/llvm/Object/Wasm.h @@ -154,7 +154,7 @@ public: uint32_t getNumSections() const { return Sections.size(); } void moveSymbolNext(DataRefImpl &Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; basic_symbol_iterator symbol_begin() const override; diff --git a/include/llvm/Object/XCOFFObjectFile.h b/include/llvm/Object/XCOFFObjectFile.h index 84596b7a97f..47aa861b379 100644 --- a/include/llvm/Object/XCOFFObjectFile.h +++ b/include/llvm/Object/XCOFFObjectFile.h @@ -268,7 +268,7 @@ public: // Interface inherited from base classes. void moveSymbolNext(DataRefImpl &Symb) const override; - uint32_t getSymbolFlags(DataRefImpl Symb) const override; + Expected getSymbolFlags(DataRefImpl Symb) const override; basic_symbol_iterator symbol_begin() const override; basic_symbol_iterator symbol_end() const override; diff --git a/lib/ExecutionEngine/Orc/Mangling.cpp b/lib/ExecutionEngine/Orc/Mangling.cpp index b85f7290fd1..606304741cf 100644 --- a/lib/ExecutionEngine/Orc/Mangling.cpp +++ b/lib/ExecutionEngine/Orc/Mangling.cpp @@ -93,12 +93,17 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) { SymbolFlagsMap SymbolFlags; for (auto &Sym : (*Obj)->symbols()) { + Expected SymFlagsOrErr = Sym.getFlags(); + if (!SymFlagsOrErr) + // TODO: Test this error. + return SymFlagsOrErr.takeError(); + // Skip symbols not defined in this object file. - if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) + if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined) continue; // Skip symbols that are not global. - if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) + if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) continue; // Skip symbols that have type SF_File. diff --git a/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp b/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp index e0582f73b2f..5ded369f3bd 100644 --- a/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp +++ b/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp @@ -125,8 +125,16 @@ void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R, return; } + Expected SymFlagsOrErr = Sym.getFlags(); + if (!SymFlagsOrErr) { + // TODO: Test this error. + ES.reportError(SymFlagsOrErr.takeError()); + R.failMaterialization(); + return; + } + // Don't include symbols that aren't global. - if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) { + if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) { if (auto SymName = Sym.getName()) InternalSymbols->insert(*SymName); else { @@ -200,7 +208,9 @@ Error RTDyldObjectLinkingLayer::onObjLoad( // check whether the symbol is in a comdat section and if so mark it as // weak. for (auto &Sym : COFFObj->symbols()) { - if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) + // getFlags() on COFF symbols can't fail. + uint32_t SymFlags = cantFail(Sym.getFlags()); + if (SymFlags & object::BasicSymbolRef::SF_Undefined) continue; auto Name = Sym.getName(); if (!Name) diff --git a/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp b/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp index 031a055a795..b445d3d49d7 100644 --- a/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp @@ -55,12 +55,17 @@ JITSymbolFlags llvm::JITSymbolFlags::fromSummary(GlobalValueSummary *S) { Expected llvm::JITSymbolFlags::fromObjectSymbol(const object::SymbolRef &Symbol) { + Expected SymbolFlagsOrErr = Symbol.getFlags(); + if (!SymbolFlagsOrErr) + // TODO: Test this error. + return SymbolFlagsOrErr.takeError(); + JITSymbolFlags Flags = JITSymbolFlags::None; - if (Symbol.getFlags() & object::BasicSymbolRef::SF_Weak) + if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Weak) Flags |= JITSymbolFlags::Weak; - if (Symbol.getFlags() & object::BasicSymbolRef::SF_Common) + if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Common) Flags |= JITSymbolFlags::Common; - if (Symbol.getFlags() & object::BasicSymbolRef::SF_Exported) + if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Exported) Flags |= JITSymbolFlags::Exported; auto SymbolType = Symbol.getType(); @@ -75,8 +80,12 @@ llvm::JITSymbolFlags::fromObjectSymbol(const object::SymbolRef &Symbol) { ARMJITSymbolFlags llvm::ARMJITSymbolFlags::fromObjectSymbol(const object::SymbolRef &Symbol) { + Expected SymbolFlagsOrErr = Symbol.getFlags(); + if (!SymbolFlagsOrErr) + // TODO: Actually report errors helpfully. + report_fatal_error(SymbolFlagsOrErr.takeError()); ARMJITSymbolFlags Flags; - if (Symbol.getFlags() & object::BasicSymbolRef::SF_Thumb) + if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Thumb) Flags |= ARMJITSymbolFlags::Thumb; return Flags; } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 5cc8ef58e90..7e9b0690cce 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -214,8 +214,12 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { { JITSymbolResolver::LookupSet Symbols; for (auto &Sym : Obj.symbols()) { - uint32_t Flags = Sym.getFlags(); - if ((Flags & SymbolRef::SF_Common) || (Flags & SymbolRef::SF_Weak)) { + Expected FlagsOrErr = Sym.getFlags(); + if (!FlagsOrErr) + // TODO: Test this error. + return FlagsOrErr.takeError(); + if ((*FlagsOrErr & SymbolRef::SF_Common) || + (*FlagsOrErr & SymbolRef::SF_Weak)) { // Get symbol name. if (auto NameOrErr = Sym.getName()) Symbols.insert(*NameOrErr); @@ -234,10 +238,13 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { LLVM_DEBUG(dbgs() << "Parse symbols:\n"); for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; ++I) { - uint32_t Flags = I->getFlags(); + Expected FlagsOrErr = I->getFlags(); + if (!FlagsOrErr) + // TODO: Test this error. + return FlagsOrErr.takeError(); // Skip undefined symbols. - if (Flags & SymbolRef::SF_Undefined) + if (*FlagsOrErr & SymbolRef::SF_Undefined) continue; // Get the symbol type. @@ -287,7 +294,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { } } - if (Flags & SymbolRef::SF_Absolute && + if (*FlagsOrErr & SymbolRef::SF_Absolute && SymType != object::SymbolRef::ST_File) { uint64_t Addr = 0; if (auto AddrOrErr = I->getAddress()) @@ -300,7 +307,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { LLVM_DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name << " SID: " << SectionID << " Offset: " << format("%p", (uintptr_t)Addr) - << " flags: " << Flags << "\n"); + << " flags: " << *FlagsOrErr << "\n"); GlobalSymbolTable[Name] = SymbolTableEntry(SectionID, Addr, *JITSymFlags); } else if (SymType == object::SymbolRef::ST_Function || SymType == object::SymbolRef::ST_Data || @@ -332,7 +339,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { LLVM_DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << " SID: " << SectionID << " Offset: " << format("%p", (uintptr_t)SectOffset) - << " flags: " << Flags << "\n"); + << " flags: " << *FlagsOrErr << "\n"); GlobalSymbolTable[Name] = SymbolTableEntry(SectionID, SectOffset, *JITSymFlags); } @@ -592,8 +599,11 @@ Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, uint32_t CommonAlign = 1; for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; ++I) { - uint32_t Flags = I->getFlags(); - if (Flags & SymbolRef::SF_Common) { + Expected FlagsOrErr = I->getFlags(); + if (!FlagsOrErr) + // TODO: Test this error. + return FlagsOrErr.takeError(); + if (*FlagsOrErr & SymbolRef::SF_Common) { // Add the common symbols to a list. We'll allocate them all below. uint64_t Size = I->getCommonSize(); uint32_t Align = I->getAlignment(); diff --git a/lib/Object/ArchiveWriter.cpp b/lib/Object/ArchiveWriter.cpp index 7e3416feba4..6f92c547164 100644 --- a/lib/Object/ArchiveWriter.cpp +++ b/lib/Object/ArchiveWriter.cpp @@ -264,12 +264,15 @@ static sys::TimePoint now(bool Deterministic) { } static bool isArchiveSymbol(const object::BasicSymbolRef &S) { - uint32_t Symflags = S.getFlags(); - if (Symflags & object::SymbolRef::SF_FormatSpecific) + Expected SymFlagsOrErr = S.getFlags(); + if (!SymFlagsOrErr) + // TODO: Actually report errors helpfully. + report_fatal_error(SymFlagsOrErr.takeError()); + if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific) return false; - if (!(Symflags & object::SymbolRef::SF_Global)) + if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global)) return false; - if (Symflags & object::SymbolRef::SF_Undefined) + if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined) return false; return true; } diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 1fd95451e72..ebb29ff8c9f 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -209,7 +209,7 @@ Expected COFFObjectFile::getSymbolType(DataRefImpl Ref) const { return SymbolRef::ST_Other; } -uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { +Expected COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { COFFSymbolRef Symb = getCOFFSymbol(Ref); uint32_t Result = SymbolRef::SF_None; diff --git a/lib/Object/IRObjectFile.cpp b/lib/Object/IRObjectFile.cpp index 931e842edb4..befba5d5712 100644 --- a/lib/Object/IRObjectFile.cpp +++ b/lib/Object/IRObjectFile.cpp @@ -47,7 +47,7 @@ Error IRObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const { return Error::success(); } -uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { +Expected IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { return SymTab.getSymbolFlags(getSym(Symb)); } diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index c0aa0b8b3ee..4d85e6f40ec 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -1804,8 +1804,8 @@ Expected MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const { } uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const { - uint32_t flags = getSymbolFlags(DRI); - if (flags & SymbolRef::SF_Common) { + uint32_t Flags = cantFail(getSymbolFlags(DRI)); + if (Flags & SymbolRef::SF_Common) { MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI); return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); } @@ -1840,7 +1840,7 @@ MachOObjectFile::getSymbolType(DataRefImpl Symb) const { return SymbolRef::ST_Other; } -uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { +Expected MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI); uint8_t MachOType = Entry.n_type; diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index 1453f9d8813..fe903781144 100644 --- a/lib/Object/ObjectFile.cpp +++ b/lib/Object/ObjectFile.cpp @@ -55,11 +55,14 @@ bool SectionRef::containsSymbol(SymbolRef S) const { } uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const { - uint32_t Flags = getSymbolFlags(Ref); - if (Flags & SymbolRef::SF_Undefined) - return 0; - if (Flags & SymbolRef::SF_Common) - return getCommonSymbolSize(Ref); + if (Expected FlagsOrErr = getSymbolFlags(Ref)) { + if (*FlagsOrErr & SymbolRef::SF_Undefined) + return 0; + if (*FlagsOrErr & SymbolRef::SF_Common) + return getCommonSymbolSize(Ref); + } else + // TODO: Actually report errors helpfully. + report_fatal_error(FlagsOrErr.takeError()); return getSymbolValueImpl(Ref); } diff --git a/lib/Object/TapiFile.cpp b/lib/Object/TapiFile.cpp index c409bd8e599..bbc341295db 100644 --- a/lib/Object/TapiFile.cpp +++ b/lib/Object/TapiFile.cpp @@ -86,7 +86,7 @@ Error TapiFile::printSymbolName(raw_ostream &OS, DataRefImpl DRI) const { return Error::success(); } -uint32_t TapiFile::getSymbolFlags(DataRefImpl DRI) const { +Expected TapiFile::getSymbolFlags(DataRefImpl DRI) const { const auto *Sym = reinterpret_cast(DRI.p); return Sym->Flags; } diff --git a/lib/Object/WasmObjectFile.cpp b/lib/Object/WasmObjectFile.cpp index 2993fd1d555..3608d95a2d2 100644 --- a/lib/Object/WasmObjectFile.cpp +++ b/lib/Object/WasmObjectFile.cpp @@ -1266,7 +1266,7 @@ const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; } -uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { +Expected WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { uint32_t Result = SymbolRef::SF_None; const WasmSymbol &Sym = getWasmSymbol(Symb); diff --git a/lib/Object/XCOFFObjectFile.cpp b/lib/Object/XCOFFObjectFile.cpp index 3ec19c09616..4d2b7f3372b 100644 --- a/lib/Object/XCOFFObjectFile.cpp +++ b/lib/Object/XCOFFObjectFile.cpp @@ -392,7 +392,7 @@ void XCOFFObjectFile::getRelocationTypeName( Result.append(Res.begin(), Res.end()); } -uint32_t XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { +Expected XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { uint32_t Result = 0; llvm_unreachable("Not yet implemented!"); return Result; diff --git a/tools/dsymutil/DebugMap.cpp b/tools/dsymutil/DebugMap.cpp index 41067106e02..3cd1bb0f7b3 100644 --- a/tools/dsymutil/DebugMap.cpp +++ b/tools/dsymutil/DebugMap.cpp @@ -256,9 +256,12 @@ MappingTraits::YamlDMO::denormalize(IO &IO) { for (const auto &Sym : Object->symbols()) { uint64_t Address = Sym.getValue(); Expected Name = Sym.getName(); - if (!Name || (Sym.getFlags() & - (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) { + Expected FlagsOrErr = Sym.getFlags(); + if (!Name || !FlagsOrErr || + (*FlagsOrErr & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) { // TODO: Actually report errors helpfully. + if (!FlagsOrErr) + consumeError(FlagsOrErr.takeError()); if (!Name) consumeError(Name.takeError()); continue; diff --git a/tools/dsymutil/MachODebugMapParser.cpp b/tools/dsymutil/MachODebugMapParser.cpp index a9d6c876860..42b3f2ecb2b 100644 --- a/tools/dsymutil/MachODebugMapParser.cpp +++ b/tools/dsymutil/MachODebugMapParser.cpp @@ -493,7 +493,7 @@ void MachODebugMapParser::loadCurrentObjectFileSymbols( // relocations will use the symbol itself, and won't need an // object file address. The object file address field is optional // in the DebugMap, leave it unassigned for these symbols. - uint32_t Flags = Sym.getFlags(); + uint32_t Flags = cantFail(Sym.getFlags()); if (Flags & SymbolRef::SF_Absolute) { CurrentObjectAddresses[*Name] = None; } else if (Flags & SymbolRef::SF_Common) { diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index e0485514d93..4e8840569fa 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -306,13 +306,17 @@ struct NMSymbol { static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) { bool ADefined; + // Symbol flags have been checked in the caller. + uint32_t AFlags = cantFail(A.Sym.getFlags()); if (A.Sym.getRawDataRefImpl().p) - ADefined = !(A.Sym.getFlags() & SymbolRef::SF_Undefined); + ADefined = !(AFlags & SymbolRef::SF_Undefined); else ADefined = A.TypeChar != 'U'; bool BDefined; + // Symbol flags have been checked in the caller. + uint32_t BFlags = cantFail(B.Sym.getFlags()); if (B.Sym.getRawDataRefImpl().p) - BDefined = !(B.Sym.getFlags() & SymbolRef::SF_Undefined); + BDefined = !(BFlags & SymbolRef::SF_Undefined); else BDefined = B.TypeChar != 'U'; return std::make_tuple(ADefined, A.Address, A.Name, A.Size) < @@ -366,7 +370,7 @@ static void darwinPrintSymbol(SymbolicFile &Obj, const NMSymbol &S, uint64_t NValue = 0; MachOObjectFile *MachO = dyn_cast(&Obj); if (Obj.isIR()) { - uint32_t SymFlags = S.Sym.getFlags(); + uint32_t SymFlags = cantFail(S.Sym.getFlags()); if (SymFlags & SymbolRef::SF_Global) NType |= MachO::N_EXT; if (SymFlags & SymbolRef::SF_Hidden) @@ -794,9 +798,15 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName, if (Optional Opt = demangle(S.Name, MachO)) Name = *Opt; } - if (S.Sym.getRawDataRefImpl().p) - SymFlags = S.Sym.getFlags(); - else + if (S.Sym.getRawDataRefImpl().p) { + Expected SymFlagsOrErr = S.Sym.getFlags(); + if (!SymFlagsOrErr) { + // TODO: Test this error. + error(SymFlagsOrErr.takeError(), Obj.getFileName()); + return; + } + SymFlags = *SymFlagsOrErr; + } else SymFlags = S.SymFlags; bool Undefined = SymFlags & SymbolRef::SF_Undefined; @@ -1037,14 +1047,14 @@ static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) { } static char getSymbolNMTypeChar(WasmObjectFile &Obj, basic_symbol_iterator I) { - uint32_t Flags = I->getFlags(); + uint32_t Flags = cantFail(I->getFlags()); if (Flags & SymbolRef::SF_Executable) return 't'; return 'd'; } static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) { - uint32_t Flags = I->getFlags(); + uint32_t Flags = cantFail(I->getFlags()); // FIXME: should we print 'b'? At the IR level we cannot be sure if this // will be in bss or not, but we could approximate. if (Flags & SymbolRef::SF_Executable) @@ -1076,7 +1086,8 @@ static StringRef getNMTypeName(SymbolicFile &Obj, basic_symbol_iterator I) { // section and name, to be used in format=sysv output. static char getNMSectionTagAndName(SymbolicFile &Obj, basic_symbol_iterator I, StringRef &SecName) { - uint32_t Symflags = I->getFlags(); + // Symbol Flags have been checked in the caller. + uint32_t Symflags = cantFail(I->getFlags()); if (ELFObjectFileBase *ELFObj = dyn_cast(&Obj)) { if (Symflags & object::SymbolRef::SF_Absolute) SecName = "*ABS*"; @@ -1205,10 +1216,15 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, } if (!(MachO && DyldInfoOnly)) { for (BasicSymbolRef Sym : Symbols) { - uint32_t SymFlags = Sym.getFlags(); - if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific)) + Expected SymFlagsOrErr = Sym.getFlags(); + if (!SymFlagsOrErr) { + // TODO: Test this error. + error(SymFlagsOrErr.takeError(), Obj.getFileName()); + return; + } + if (!DebugSyms && (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific)) continue; - if (WithoutAliases && (SymFlags & SymbolRef::SF_Indirect)) + if (WithoutAliases && (*SymFlagsOrErr & SymbolRef::SF_Indirect)) continue; // If a "-s segname sectname" option was specified and this is a Mach-O // file and this section appears in this file, Nsect will be non-zero then diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index aa71998c2f6..6a66a16b700 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -7562,7 +7562,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, symbolTableWorked = true; DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); - bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb; + uint32_t SymbolFlags = cantFail(MachOOF->getSymbolFlags(Symb)); + bool IsThumb = SymbolFlags & SymbolRef::SF_Thumb; // We only need the dedicated Thumb target if there's a real choice // (i.e. we're not targeting M-class) and the function is Thumb. diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index fc4de6be1f9..c264ea6461b 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -1929,7 +1929,8 @@ void printSymbol(const ObjectFile *O, const SymbolRef &Symbol, return; SymbolRef::Type Type = unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName); - uint32_t Flags = Symbol.getFlags(); + uint32_t Flags = + unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName); // Don't ask a Mach-O STAB symbol for its section unless you know that // STAB symbol's section field refers to a valid section index. Otherwise diff --git a/tools/llvm-size/llvm-size.cpp b/tools/llvm-size/llvm-size.cpp index 46ece5a6f0c..87fe4693b56 100644 --- a/tools/llvm-size/llvm-size.cpp +++ b/tools/llvm-size/llvm-size.cpp @@ -196,11 +196,17 @@ static bool considerForSize(ObjectFile *Obj, SectionRef Section) { } /// Total size of all ELF common symbols -static uint64_t getCommonSize(ObjectFile *Obj) { +static Expected getCommonSize(ObjectFile *Obj) { uint64_t TotalCommons = 0; - for (auto &Sym : Obj->symbols()) - if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common) + for (auto &Sym : Obj->symbols()) { + Expected SymFlagsOrErr = + Obj->getSymbolFlags(Sym.getRawDataRefImpl()); + if (!SymFlagsOrErr) + // TODO: Test this error. + return SymFlagsOrErr.takeError(); + if (*SymFlagsOrErr & SymbolRef::SF_Common) TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl()); + } return TotalCommons; } @@ -435,10 +441,14 @@ static void printObjectSectionSizes(ObjectFile *Obj) { } if (ELFCommons) { - uint64_t CommonSize = getCommonSize(Obj); - total += CommonSize; - outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(), - CommonSize, static_cast(0)); + if (Expected CommonSizeOrErr = getCommonSize(Obj)) { + total += *CommonSizeOrErr; + outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(), + *CommonSizeOrErr, static_cast(0)); + } else { + error(CommonSizeOrErr.takeError(), Obj->getFileName()); + return; + } } // Print total. @@ -469,8 +479,14 @@ static void printObjectSectionSizes(ObjectFile *Obj) { total_bss += size; } - if (ELFCommons) - total_bss += getCommonSize(Obj); + if (ELFCommons) { + if (Expected CommonSizeOrErr = getCommonSize(Obj)) + total_bss += *CommonSizeOrErr; + else { + error(CommonSizeOrErr.takeError(), Obj->getFileName()); + return; + } + } total = total_text + total_data + total_bss; diff --git a/tools/sancov/sancov.cpp b/tools/sancov/sancov.cpp index ffdf36f4e0b..ed384a27100 100644 --- a/tools/sancov/sancov.cpp +++ b/tools/sancov/sancov.cpp @@ -657,7 +657,12 @@ findSanitizerCovFunctions(const object::ObjectFile &O) { failIfError(NameOrErr); StringRef Name = NameOrErr.get(); - if (!(Symbol.getFlags() & object::BasicSymbolRef::SF_Undefined) && + Expected FlagsOrErr = Symbol.getFlags(); + // TODO: Test this error. + failIfError(FlagsOrErr); + uint32_t Flags = FlagsOrErr.get(); + + if (!(Flags & object::BasicSymbolRef::SF_Undefined) && isCoveragePointSymbol(Name)) { Result.insert(Address); }