diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 05ad63c32fb..60186554a1f 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -134,6 +134,9 @@ public: }; SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); + SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) { + assert(isa(BasicSymbolRef::getObject())); + } std::error_code getName(StringRef &Result) const; /// Returns the symbol virtual address (i.e. address at which it will be diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 15dbc8c9bea..3ef909aa574 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -869,8 +869,8 @@ static unsigned getNsectForSegSect(MachOObjectFile *Obj) { // It is called once for each symbol in a Mach-O file from // dumpSymbolNamesFromObject() and returns the section number for that symbol // if it is in a section, else it returns 0. -static unsigned getNsectInMachO(MachOObjectFile &Obj, basic_symbol_iterator I) { - DataRefImpl Symb = I->getRawDataRefImpl(); +static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) { + DataRefImpl Symb = Sym.getRawDataRefImpl(); if (Obj.is64Bit()) { MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb); if ((STE.n_type & MachO::N_TYPE) == MachO::N_SECT) @@ -887,17 +887,16 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, std::string ArchiveName = std::string(), std::string ArchitectureName = std::string()) { - basic_symbol_iterator IBegin = Obj.symbol_begin(); - basic_symbol_iterator IEnd = Obj.symbol_end(); + auto Symbols = Obj.symbols(); if (DynamicSyms) { const auto *E = dyn_cast(&Obj); if (!E) { error("File format has no dynamic symbol table", Obj.getFileName()); return; } - auto IDyn = E->getDynamicSymbolIterators(); - IBegin = IDyn.begin(); - IEnd = IDyn.end(); + auto DynSymbols = E->getDynamicSymbolIterators(); + Symbols = + make_range(DynSymbols.begin(), DynSymbols.end()); } std::string NameBuffer; raw_string_ostream OS(NameBuffer); @@ -911,13 +910,13 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, if (Nsect == 0) return; } - for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) { - uint32_t SymFlags = I->getFlags(); + for (BasicSymbolRef Sym : Symbols) { + uint32_t SymFlags = Sym.getFlags(); if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific)) continue; if (WithoutAliases) { if (IRObjectFile *IR = dyn_cast(&Obj)) { - const GlobalValue *GV = IR->getSymbolGV(I->getRawDataRefImpl()); + const GlobalValue *GV = IR->getSymbolGV(Sym.getRawDataRefImpl()); if (GV && isa(GV)) continue; } @@ -925,25 +924,24 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName, // 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 // see if this symbol is a symbol from that section and if not skip it. - if (Nsect && Nsect != getNsectInMachO(*MachO, I)) + if (Nsect && Nsect != getNsectInMachO(*MachO, Sym)) continue; NMSymbol S; S.Size = 0; S.Address = UnknownAddress; if (PrintSize) { - if (auto *E = dyn_cast(&Obj)) { - symbol_iterator SymI = I; - S.Size = E->getSymbolSize(*SymI); - } + if (auto *E = dyn_cast(&Obj)) + S.Size = E->getSymbolSize(Sym); } - if (PrintAddress && isa(Obj)) - if (error(symbol_iterator(I)->getAddress(S.Address))) + if (PrintAddress && isa(Obj)) { + if (error(SymbolRef(Sym).getAddress(S.Address))) break; - S.TypeChar = getNMTypeChar(Obj, I); - if (error(I->printName(OS))) + } + S.TypeChar = getNMTypeChar(Obj, Sym); + if (error(Sym.printName(OS))) break; OS << '\0'; - S.Symb = I->getRawDataRefImpl(); + S.Symb = Sym.getRawDataRefImpl(); SymbolList.push_back(S); }