[llvm-nm] Simplify and fix a buffer overflow

* char SymbolAddrStr[18] can't hold "%" PRIo64 which may need 22 characters.
* Use range-based for
* Delete unnecessary typedef
* format(...).print(Str, sizeof(Str)) + outs() << Str => outs() << format(...)
* Use cascading outs() << .. << ..
* Use iterator_range(Container &&c)
* (A & B) == B => A & B   if B is a power of 2
* replace null sentinel in constants with makeArrayRef

llvm-svn: 359416
This commit is contained in:
Fangrui Song 2019-04-29 05:38:22 +00:00
parent 43a015ab81
commit b521d1b887
1 changed files with 107 additions and 149 deletions

View File

@ -344,8 +344,7 @@ static char isSymbolList64Bit(SymbolicFile &Obj) {
} }
static StringRef CurrentFilename; static StringRef CurrentFilename;
typedef std::vector<NMSymbol> SymbolListT; static std::vector<NMSymbol> SymbolList;
static SymbolListT SymbolList;
static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I); static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
@ -354,9 +353,10 @@ static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
// the darwin format it produces the same output as darwin's nm(1) -m output // the darwin format it produces the same output as darwin's nm(1) -m output
// and when printing Mach-O symbols in hex it produces the same output as // and when printing Mach-O symbols in hex it produces the same output as
// darwin's nm(1) -x format. // darwin's nm(1) -x format.
static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I, static void darwinPrintSymbol(SymbolicFile &Obj, const NMSymbol &S,
char *SymbolAddrStr, const char *printBlanks, char *SymbolAddrStr, const char *printBlanks,
const char *printDashes, const char *printFormat) { const char *printDashes,
const char *printFormat) {
MachO::mach_header H; MachO::mach_header H;
MachO::mach_header_64 H_64; MachO::mach_header_64 H_64;
uint32_t Filetype = MachO::MH_OBJECT; uint32_t Filetype = MachO::MH_OBJECT;
@ -368,7 +368,7 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
uint64_t NValue = 0; uint64_t NValue = 0;
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj); MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
if (Obj.isIR()) { if (Obj.isIR()) {
uint32_t SymFlags = I->Sym.getFlags(); uint32_t SymFlags = S.Sym.getFlags();
if (SymFlags & SymbolRef::SF_Global) if (SymFlags & SymbolRef::SF_Global)
NType |= MachO::N_EXT; NType |= MachO::N_EXT;
if (SymFlags & SymbolRef::SF_Hidden) if (SymFlags & SymbolRef::SF_Hidden)
@ -390,7 +390,7 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
if (SymFlags & SymbolRef::SF_Weak) if (SymFlags & SymbolRef::SF_Weak)
NDesc |= MachO::N_WEAK_DEF; NDesc |= MachO::N_WEAK_DEF;
} else { } else {
DataRefImpl SymDRI = I->Sym.getRawDataRefImpl(); DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
if (MachO->is64Bit()) { if (MachO->is64Bit()) {
H_64 = MachO->MachOObjectFile::getHeader64(); H_64 = MachO->MachOObjectFile::getHeader64();
Filetype = H_64.filetype; Filetype = H_64.filetype;
@ -403,11 +403,11 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
NStrx = STE_64.n_strx; NStrx = STE_64.n_strx;
NValue = STE_64.n_value; NValue = STE_64.n_value;
} else { } else {
NType = I->NType; NType = S.NType;
NSect = I->NSect; NSect = S.NSect;
NDesc = I->NDesc; NDesc = S.NDesc;
NStrx = 0; NStrx = 0;
NValue = I->Address; NValue = S.Address;
} }
} else { } else {
H = MachO->MachOObjectFile::getHeader(); H = MachO->MachOObjectFile::getHeader();
@ -421,42 +421,31 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
NStrx = STE.n_strx; NStrx = STE.n_strx;
NValue = STE.n_value; NValue = STE.n_value;
} else { } else {
NType = I->NType; NType = S.NType;
NSect = I->NSect; NSect = S.NSect;
NDesc = I->NDesc; NDesc = S.NDesc;
NStrx = 0; NStrx = 0;
NValue = I->Address; NValue = S.Address;
} }
} }
} }
// If we are printing Mach-O symbols in hex do that and return. // If we are printing Mach-O symbols in hex do that and return.
if (FormatMachOasHex) { if (FormatMachOasHex) {
char Str[18] = ""; outs() << format(printFormat, NValue) << ' '
format(printFormat, NValue).print(Str, sizeof(Str)); << format("%02x %02x %04x %08x", NType, NSect, NDesc, NStrx) << ' '
outs() << Str << ' '; << S.Name;
format("%02x", NType).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%02x", NSect).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%04x", NDesc).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%08x", NStrx).print(Str, sizeof(Str));
outs() << Str << ' ';
outs() << I->Name;
if ((NType & MachO::N_TYPE) == MachO::N_INDR) { if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
outs() << " (indirect for "; outs() << " (indirect for ";
format(printFormat, NValue).print(Str, sizeof(Str)); outs() << format(printFormat, NValue) << ' ';
outs() << Str << ' ';
StringRef IndirectName; StringRef IndirectName;
if (I->Sym.getRawDataRefImpl().p) { if (S.Sym.getRawDataRefImpl().p) {
if (MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName)) if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
outs() << "?)"; outs() << "?)";
else else
outs() << IndirectName << ")"; outs() << IndirectName << ")";
} } else
else outs() << S.IndirectName << ")";
outs() << I->IndirectName << ")";
} }
outs() << "\n"; outs() << "\n";
return; return;
@ -515,9 +504,9 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
break; break;
} }
section_iterator Sec = SectionRef(); section_iterator Sec = SectionRef();
if (I->Sym.getRawDataRefImpl().p) { if (S.Sym.getRawDataRefImpl().p) {
Expected<section_iterator> SecOrErr = Expected<section_iterator> SecOrErr =
MachO->getSymbolSection(I->Sym.getRawDataRefImpl()); MachO->getSymbolSection(S.Sym.getRawDataRefImpl());
if (!SecOrErr) { if (!SecOrErr) {
consumeError(SecOrErr.takeError()); consumeError(SecOrErr.takeError());
outs() << "(?,?) "; outs() << "(?,?) ";
@ -529,7 +518,7 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
break; break;
} }
} else { } else {
Sec = I->Section; Sec = S.Section;
} }
DataRefImpl Ref = Sec->getRawDataRefImpl(); DataRefImpl Ref = Sec->getRawDataRefImpl();
StringRef SectionName; StringRef SectionName;
@ -569,44 +558,36 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
outs() << "non-external "; outs() << "non-external ";
} }
if (Filetype == MachO::MH_OBJECT && if (Filetype == MachO::MH_OBJECT) {
(NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP) if (NDesc & MachO::N_NO_DEAD_STRIP)
outs() << "[no dead strip] "; outs() << "[no dead strip] ";
if ((NType & MachO::N_TYPE) != MachO::N_UNDF &&
if (Filetype == MachO::MH_OBJECT && NDesc & MachO::N_SYMBOL_RESOLVER)
((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
(NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
outs() << "[symbol resolver] "; outs() << "[symbol resolver] ";
if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_ALT_ENTRY)
if (Filetype == MachO::MH_OBJECT &&
((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
(NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
outs() << "[alt entry] "; outs() << "[alt entry] ";
if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_COLD_FUNC)
if (Filetype == MachO::MH_OBJECT &&
((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
(NDesc & MachO::N_COLD_FUNC) == MachO::N_COLD_FUNC)
outs() << "[cold func] "; outs() << "[cold func] ";
}
if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF) if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
outs() << "[Thumb] "; outs() << "[Thumb] ";
if ((NType & MachO::N_TYPE) == MachO::N_INDR) { if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
outs() << I->Name << " (for "; outs() << S.Name << " (for ";
StringRef IndirectName; StringRef IndirectName;
if (MachO) { if (MachO) {
if (I->Sym.getRawDataRefImpl().p) { if (S.Sym.getRawDataRefImpl().p) {
if (MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName)) if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
outs() << "?)"; outs() << "?)";
else else
outs() << IndirectName << ")"; outs() << IndirectName << ")";
} } else
else outs() << S.IndirectName << ")";
outs() << I->IndirectName << ")";
} else } else
outs() << "?)"; outs() << "?)";
} else } else
outs() << I->Name; outs() << S.Name;
if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL && if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
(((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) || (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
@ -668,25 +649,24 @@ static const struct DarwinStabName DarwinStabNames[] = {
{MachO::N_ECOMM, "ECOMM"}, {MachO::N_ECOMM, "ECOMM"},
{MachO::N_ECOML, "ECOML"}, {MachO::N_ECOML, "ECOML"},
{MachO::N_LENG, "LENG"}, {MachO::N_LENG, "LENG"},
{0, nullptr}}; };
static const char *getDarwinStabString(uint8_t NType) { static const char *getDarwinStabString(uint8_t NType) {
for (unsigned i = 0; DarwinStabNames[i].Name; i++) { for (auto I : makeArrayRef(DarwinStabNames))
if (DarwinStabNames[i].NType == NType) if (I.NType == NType)
return DarwinStabNames[i].Name; return I.Name;
}
return nullptr; return nullptr;
} }
// darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
// a stab n_type value in a Mach-O file. // a stab n_type value in a Mach-O file.
static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) { static void darwinPrintStab(MachOObjectFile *MachO, const NMSymbol &S) {
MachO::nlist_64 STE_64; MachO::nlist_64 STE_64;
MachO::nlist STE; MachO::nlist STE;
uint8_t NType; uint8_t NType;
uint8_t NSect; uint8_t NSect;
uint16_t NDesc; uint16_t NDesc;
DataRefImpl SymDRI = I->Sym.getRawDataRefImpl(); DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
if (MachO->is64Bit()) { if (MachO->is64Bit()) {
STE_64 = MachO->getSymbol64TableEntry(SymDRI); STE_64 = MachO->getSymbol64TableEntry(SymDRI);
NType = STE_64.n_type; NType = STE_64.n_type;
@ -699,16 +679,11 @@ static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
NDesc = STE.n_desc; NDesc = STE.n_desc;
} }
char Str[18] = ""; outs() << format(" %02x %04x ", NSect, NDesc);
format("%02x", NSect).print(Str, sizeof(Str));
outs() << ' ' << Str << ' ';
format("%04x", NDesc).print(Str, sizeof(Str));
outs() << Str << ' ';
if (const char *stabString = getDarwinStabString(NType)) if (const char *stabString = getDarwinStabString(NType))
format("%5.5s", stabString).print(Str, sizeof(Str)); outs() << format("%5.5s", stabString);
else else
format(" %02x", NType).print(Str, sizeof(Str)); outs() << format(" %02x", NType);
outs() << Str;
} }
static Optional<std::string> demangle(StringRef Name, bool StripUnderscore) { static Optional<std::string> demangle(StringRef Name, bool StripUnderscore) {
@ -813,19 +788,18 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
errs() << "no symbols\n"; errs() << "no symbols\n";
} }
for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end(); for (const NMSymbol &S : SymbolList) {
I != E; ++I) {
uint32_t SymFlags; uint32_t SymFlags;
std::string Name = I->Name.str(); std::string Name = S.Name.str();
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj); MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
if (Demangle) { if (Demangle) {
if (Optional<std::string> Opt = demangle(I->Name, MachO)) if (Optional<std::string> Opt = demangle(S.Name, MachO))
Name = *Opt; Name = *Opt;
} }
if (I->Sym.getRawDataRefImpl().p) if (S.Sym.getRawDataRefImpl().p)
SymFlags = I->Sym.getFlags(); SymFlags = S.Sym.getFlags();
else else
SymFlags = I->SymFlags; SymFlags = S.SymFlags;
bool Undefined = SymFlags & SymbolRef::SF_Undefined; bool Undefined = SymFlags & SymbolRef::SF_Undefined;
bool Global = SymFlags & SymbolRef::SF_Global; bool Global = SymFlags & SymbolRef::SF_Global;
@ -842,32 +816,30 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
continue; continue;
} }
char SymbolAddrStr[18] = ""; char SymbolAddrStr[23], SymbolSizeStr[23];
char SymbolSizeStr[18] = "";
// If the format is SysV or the symbol isn't defined, then print spaces. // If the format is SysV or the symbol isn't defined, then print spaces.
if (OutputFormat == sysv || !symbolIsDefined(*I)) { if (OutputFormat == sysv || !symbolIsDefined(S)) {
if (OutputFormat == posix) { if (OutputFormat == posix) {
format(printFormat, I->Address) format(printFormat, S.Address)
.print(SymbolAddrStr, sizeof(SymbolAddrStr)); .print(SymbolAddrStr, sizeof(SymbolAddrStr));
format(printFormat, I->Size) format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
.print(SymbolSizeStr, sizeof(SymbolSizeStr));
} else { } else {
strcpy(SymbolAddrStr, printBlanks); strcpy(SymbolAddrStr, printBlanks);
strcpy(SymbolSizeStr, printBlanks); strcpy(SymbolSizeStr, printBlanks);
} }
} }
if (symbolIsDefined(S)) {
// Otherwise, print the symbol address and size. // Otherwise, print the symbol address and size.
if (symbolIsDefined(*I)) {
if (Obj.isIR()) if (Obj.isIR())
strcpy(SymbolAddrStr, printDashes); strcpy(SymbolAddrStr, printDashes);
else if(MachO && I->TypeChar == 'I') else if (MachO && S.TypeChar == 'I')
strcpy(SymbolAddrStr, printBlanks); strcpy(SymbolAddrStr, printBlanks);
else else
format(printFormat, I->Address) format(printFormat, S.Address)
.print(SymbolAddrStr, sizeof(SymbolAddrStr)); .print(SymbolAddrStr, sizeof(SymbolAddrStr));
format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr)); format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
} }
// If OutputFormat is darwin or we are printing Mach-O symbols in hex and // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
@ -876,47 +848,36 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
// printing Mach-O symbols in hex and not a Mach-O object fall back to // printing Mach-O symbols in hex and not a Mach-O object fall back to
// OutputFormat bsd (see below). // OutputFormat bsd (see below).
if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) { if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
darwinPrintSymbol(Obj, I, SymbolAddrStr, printBlanks, printDashes, darwinPrintSymbol(Obj, S, SymbolAddrStr, printBlanks, printDashes,
printFormat); printFormat);
} else if (OutputFormat == posix) { } else if (OutputFormat == posix) {
outs() << Name << " " << I->TypeChar << " "; outs() << Name << " " << S.TypeChar << " " << SymbolAddrStr << " "
if (MachO) << (MachO ? "0" : SymbolSizeStr) << "\n";
outs() << SymbolAddrStr << " " << "0" /* SymbolSizeStr */ << "\n";
else
outs() << SymbolAddrStr << " " << SymbolSizeStr << "\n";
} else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) { } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
if (PrintAddress) if (PrintAddress)
outs() << SymbolAddrStr << ' '; outs() << SymbolAddrStr << ' ';
if (PrintSize) { if (PrintSize)
outs() << SymbolSizeStr; outs() << SymbolSizeStr << ' ';
outs() << ' '; outs() << S.TypeChar;
} if (S.TypeChar == '-' && MachO)
outs() << I->TypeChar; darwinPrintStab(MachO, S);
if (I->TypeChar == '-' && MachO)
darwinPrintStab(MachO, I);
outs() << " " << Name; outs() << " " << Name;
if (I->TypeChar == 'I' && MachO) { if (S.TypeChar == 'I' && MachO) {
outs() << " (indirect for "; outs() << " (indirect for ";
if (I->Sym.getRawDataRefImpl().p) { if (S.Sym.getRawDataRefImpl().p) {
StringRef IndirectName; StringRef IndirectName;
if (MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName)) if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
outs() << "?)"; outs() << "?)";
else else
outs() << IndirectName << ")"; outs() << IndirectName << ")";
} else } else
outs() << I->IndirectName << ")"; outs() << S.IndirectName << ")";
} }
outs() << "\n"; outs() << "\n";
} else if (OutputFormat == sysv) { } else if (OutputFormat == sysv) {
std::string PaddedName(Name); outs() << left_justify(Name, 20) << "|" << SymbolAddrStr << "| "
while (PaddedName.length() < 20) << S.TypeChar << " |" << right_justify(S.TypeName, 18) << "|"
PaddedName += " "; << SymbolSizeStr << "| |" << S.SectionName << "\n";
std::string TypeName = I->TypeName;
if (TypeName.size() < 18)
TypeName = std::string(18-TypeName.size(), ' ') + TypeName;
outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
<< " |" << TypeName << "|" << SymbolSizeStr << "| |"
<< I->SectionName << "\n";
} }
} }
@ -1207,9 +1168,7 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
error("File format has no dynamic symbol table", Obj.getFileName()); error("File format has no dynamic symbol table", Obj.getFileName());
return; return;
} }
auto DynSymbols = E->getDynamicSymbolIterators(); Symbols = E->getDynamicSymbolIterators();
Symbols =
make_range<basic_symbol_iterator>(DynSymbols.begin(), DynSymbols.end());
} }
std::string NameBuffer; std::string NameBuffer;
raw_string_ostream OS(NameBuffer); raw_string_ostream OS(NameBuffer);
@ -1321,10 +1280,11 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
bool found = false; bool found = false;
bool ReExport = false; bool ReExport = false;
if (!DyldInfoOnly) { if (!DyldInfoOnly) {
for (unsigned J = 0; J < SymbolList.size() && !found; ++J) { for (const NMSymbol &S : SymbolList)
if (SymbolList[J].Address == Entry.address() + BaseSegmentAddress && if (S.Address == Entry.address() + BaseSegmentAddress &&
SymbolList[J].Name == Entry.name()) S.Name == Entry.name()) {
found = true; found = true;
break;
} }
} }
if (!found) { if (!found) {
@ -1999,10 +1959,8 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
// Either all architectures have been specified or none have been specified // Either all architectures have been specified or none have been specified
// and this does not contain the host architecture so dump all the slices. // and this does not contain the host architecture so dump all the slices.
bool moreThanOneArch = UB->getNumberOfObjects() > 1; bool moreThanOneArch = UB->getNumberOfObjects() > 1;
for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), for (const MachOUniversalBinary::ObjectForArch &O : UB->objects()) {
E = UB->end_objects(); Expected<std::unique_ptr<ObjectFile>> ObjOrErr = O.getAsObjectFile();
I != E; ++I) {
Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::string ArchiveName; std::string ArchiveName;
std::string ArchitectureName; std::string ArchitectureName;
ArchiveName.clear(); ArchiveName.clear();
@ -2011,23 +1969,23 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
ObjectFile &Obj = *ObjOrErr.get(); ObjectFile &Obj = *ObjOrErr.get();
if (PrintFileName) { if (PrintFileName) {
if (isa<MachOObjectFile>(Obj) && moreThanOneArch) if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
ArchitectureName = I->getArchFlagName(); ArchitectureName = O.getArchFlagName();
} else { } else {
if (moreThanOneArch) if (moreThanOneArch)
outs() << "\n"; outs() << "\n";
outs() << Obj.getFileName(); outs() << Obj.getFileName();
if (isa<MachOObjectFile>(Obj) && moreThanOneArch) if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
outs() << " (for architecture " << I->getArchFlagName() << ")"; outs() << " (for architecture " << O.getArchFlagName() << ")";
outs() << ":\n"; outs() << ":\n";
} }
dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName); dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
} else if (auto E = isNotObjectErrorInvalidFileType( } else if (auto E = isNotObjectErrorInvalidFileType(
ObjOrErr.takeError())) { ObjOrErr.takeError())) {
error(std::move(E), Filename, moreThanOneArch ? error(std::move(E), Filename, moreThanOneArch ?
StringRef(I->getArchFlagName()) : StringRef()); StringRef(O.getArchFlagName()) : StringRef());
continue; continue;
} else if (Expected<std::unique_ptr<Archive>> AOrErr = } else if (Expected<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) { O.getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr; std::unique_ptr<Archive> &A = *AOrErr;
Error Err = Error::success(); Error Err = Error::success();
for (auto &C : A->children(Err)) { for (auto &C : A->children(Err)) {
@ -2040,23 +1998,23 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
StringRef(ArchitectureName) : StringRef()); StringRef(ArchitectureName) : StringRef());
continue; continue;
} }
if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) { if (SymbolicFile *F = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
if (PrintFileName) { if (PrintFileName) {
ArchiveName = A->getFileName(); ArchiveName = A->getFileName();
if (isa<MachOObjectFile>(O) && moreThanOneArch) if (isa<MachOObjectFile>(F) && moreThanOneArch)
ArchitectureName = I->getArchFlagName(); ArchitectureName = O.getArchFlagName();
} else { } else {
outs() << "\n" << A->getFileName(); outs() << "\n" << A->getFileName();
if (isa<MachOObjectFile>(O)) { if (isa<MachOObjectFile>(F)) {
outs() << "(" << O->getFileName() << ")"; outs() << "(" << F->getFileName() << ")";
if (moreThanOneArch) if (moreThanOneArch)
outs() << " (for architecture " << I->getArchFlagName() outs() << " (for architecture " << O.getArchFlagName()
<< ")"; << ")";
} else } else
outs() << ":" << O->getFileName(); outs() << ":" << F->getFileName();
outs() << ":\n"; outs() << ":\n";
} }
dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName); dumpSymbolNamesFromObject(*F, false, ArchiveName, ArchitectureName);
} }
} }
if (Err) if (Err)
@ -2064,7 +2022,7 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
} else { } else {
consumeError(AOrErr.takeError()); consumeError(AOrErr.takeError());
error(Filename + " for architecture " + error(Filename + " for architecture " +
StringRef(I->getArchFlagName()) + StringRef(O.getArchFlagName()) +
" is not a Mach-O file or an archive file", " is not a Mach-O file or an archive file",
"Mach-O universal file"); "Mach-O universal file");
} }
@ -2074,7 +2032,7 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) { if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) { if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
WithColor::warning(errs(), ToolName) WithColor::warning(errs(), ToolName)
<< "sizes with -print-size for Mach-O files are always zero.\n"; << "sizes with --print-size for Mach-O files are always zero.\n";
MachOPrintSizeWarning = true; MachOPrintSizeWarning = true;
} }
if (!checkMachOAndArchFlags(O, Filename)) if (!checkMachOAndArchFlags(O, Filename))
@ -2127,7 +2085,7 @@ int main(int argc, char **argv) {
} else { } else {
if (!MachOObjectFile::isValidArch(ArchFlags[i])) if (!MachOObjectFile::isValidArch(ArchFlags[i]))
error("Unknown architecture named '" + ArchFlags[i] + "'", error("Unknown architecture named '" + ArchFlags[i] + "'",
"for the -arch option"); "for the --arch option");
} }
} }
@ -2136,7 +2094,7 @@ int main(int argc, char **argv) {
"for the -s option"); "for the -s option");
if (NoDyldInfo && (AddDyldInfo || DyldInfoOnly)) if (NoDyldInfo && (AddDyldInfo || DyldInfoOnly))
error("-no-dyldinfo can't be used with -add-dyldinfo or -dyldinfo-only"); error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
llvm::for_each(InputFilenames, dumpSymbolNamesFromFile); llvm::for_each(InputFilenames, dumpSymbolNamesFromFile);