+// darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
+// the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
+// 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
+// darwin's nm(1) -x format.
+static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
+ char *SymbolAddrStr, const char *printBlanks) {
+ MachO::mach_header H;
+ MachO::mach_header_64 H_64;
+ uint32_t Filetype, Flags;
+ MachO::nlist_64 STE_64;
+ MachO::nlist STE;
+ uint8_t NType;
+ uint8_t NSect;
+ uint16_t NDesc;
+ uint32_t NStrx;
+ uint64_t NValue;
+ DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
+ if (MachO->is64Bit()) {
+ H_64 = MachO->MachOObjectFile::getHeader64();
+ Filetype = H_64.filetype;
+ Flags = H_64.flags;
+ STE_64 = MachO->getSymbol64TableEntry(SymDRI);
+ NType = STE_64.n_type;
+ NSect = STE_64.n_sect;
+ NDesc = STE_64.n_desc;
+ NStrx = STE_64.n_strx;
+ NValue = STE_64.n_value;
+ } else {
+ H = MachO->MachOObjectFile::getHeader();
+ Filetype = H.filetype;
+ Flags = H.flags;
+ STE = MachO->getSymbolTableEntry(SymDRI);
+ NType = STE.n_type;
+ NSect = STE.n_sect;
+ NDesc = STE.n_desc;
+ NStrx = STE.n_strx;
+ NValue = STE.n_value;
+ }
+
+ // If we are printing Mach-O symbols in hex do that and return.
+ if (FormatMachOasHex) {
+ char Str[18] = "";
+ const char *printFormat;
+ if (MachO->is64Bit())
+ printFormat = "%016" PRIx64;
+ else
+ printFormat = "%08" PRIx64;
+ format(printFormat, NValue).print(Str, sizeof(Str));
+ outs() << Str << ' ';
+ 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 << "\n";
+ return;
+ }
+
+ if (PrintAddress) {
+ if ((NType & MachO::N_TYPE) == MachO::N_INDR)
+ strcpy(SymbolAddrStr, printBlanks);
+ outs() << SymbolAddrStr << ' ';
+ }
+
+ switch (NType & MachO::N_TYPE) {
+ case MachO::N_UNDF:
+ if (NValue != 0) {
+ outs() << "(common) ";
+ if (MachO::GET_COMM_ALIGN(NDesc) != 0)
+ outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
+ } else {
+ if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
+ outs() << "(prebound ";
+ else
+ outs() << "(";
+ if ((NDesc & MachO::REFERENCE_TYPE) ==
+ MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
+ outs() << "undefined [lazy bound]) ";
+ else if ((NDesc & MachO::REFERENCE_TYPE) ==
+ MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
+ outs() << "undefined [private lazy bound]) ";
+ else if ((NDesc & MachO::REFERENCE_TYPE) ==
+ MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
+ outs() << "undefined [private]) ";
+ else
+ outs() << "undefined) ";
+ }
+ break;
+ case MachO::N_ABS:
+ outs() << "(absolute) ";
+ break;
+ case MachO::N_INDR:
+ outs() << "(indirect) ";
+ break;
+ case MachO::N_SECT: {
+ section_iterator Sec = *MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
+ DataRefImpl Ref = Sec->getRawDataRefImpl();
+ StringRef SectionName;
+ MachO->getSectionName(Ref, SectionName);
+ StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
+ outs() << "(" << SegmentName << "," << SectionName << ") ";
+ break;
+ }
+ default:
+ outs() << "(?) ";
+ break;
+ }
+
+ if (NType & MachO::N_EXT) {
+ if (NDesc & MachO::REFERENCED_DYNAMICALLY)
+ outs() << "[referenced dynamically] ";
+ if (NType & MachO::N_PEXT) {
+ if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
+ outs() << "weak private external ";
+ else
+ outs() << "private external ";
+ } else {
+ if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
+ (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
+ if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
+ (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
+ outs() << "weak external automatically hidden ";
+ else
+ outs() << "weak external ";
+ } else
+ outs() << "external ";
+ }
+ } else {
+ if (NType & MachO::N_PEXT)
+ outs() << "non-external (was a private external) ";
+ else
+ outs() << "non-external ";
+ }
+
+ if (Filetype == MachO::MH_OBJECT &&
+ (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
+ outs() << "[no dead strip] ";
+
+ if (Filetype == MachO::MH_OBJECT &&
+ ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
+ (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
+ outs() << "[symbol resolver] ";
+
+ if (Filetype == MachO::MH_OBJECT &&
+ ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
+ (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
+ outs() << "[alt entry] ";
+
+ if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
+ outs() << "[Thumb] ";
+
+ if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
+ outs() << I->Name << " (for ";
+ StringRef IndirectName;
+ if (MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName))
+ outs() << "?)";
+ else
+ outs() << IndirectName << ")";
+ } else
+ outs() << I->Name;
+
+ if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
+ (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
+ (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
+ uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
+ if (LibraryOrdinal != 0) {
+ if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
+ outs() << " (from executable)";
+ else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
+ outs() << " (dynamically looked up)";
+ else {
+ StringRef LibraryName;
+ if (MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
+ outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
+ else
+ outs() << " (from " << LibraryName << ")";
+ }
+ }
+ }
+
+ outs() << "\n";
+}
+
+// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
+struct DarwinStabName {
+ uint8_t NType;
+ const char *Name;
+};
+static const struct DarwinStabName DarwinStabNames[] = {
+ {MachO::N_GSYM, "GSYM"},
+ {MachO::N_FNAME, "FNAME"},
+ {MachO::N_FUN, "FUN"},
+ {MachO::N_STSYM, "STSYM"},
+ {MachO::N_LCSYM, "LCSYM"},
+ {MachO::N_BNSYM, "BNSYM"},
+ {MachO::N_PC, "PC"},
+ {MachO::N_AST, "AST"},
+ {MachO::N_OPT, "OPT"},
+ {MachO::N_RSYM, "RSYM"},
+ {MachO::N_SLINE, "SLINE"},
+ {MachO::N_ENSYM, "ENSYM"},
+ {MachO::N_SSYM, "SSYM"},
+ {MachO::N_SO, "SO"},
+ {MachO::N_OSO, "OSO"},
+ {MachO::N_LSYM, "LSYM"},
+ {MachO::N_BINCL, "BINCL"},
+ {MachO::N_SOL, "SOL"},
+ {MachO::N_PARAMS, "PARAM"},
+ {MachO::N_VERSION, "VERS"},
+ {MachO::N_OLEVEL, "OLEV"},
+ {MachO::N_PSYM, "PSYM"},
+ {MachO::N_EINCL, "EINCL"},
+ {MachO::N_ENTRY, "ENTRY"},
+ {MachO::N_LBRAC, "LBRAC"},
+ {MachO::N_EXCL, "EXCL"},
+ {MachO::N_RBRAC, "RBRAC"},
+ {MachO::N_BCOMM, "BCOMM"},
+ {MachO::N_ECOMM, "ECOMM"},
+ {MachO::N_ECOML, "ECOML"},
+ {MachO::N_LENG, "LENG"},
+ {0, nullptr}};
+
+static const char *getDarwinStabString(uint8_t NType) {
+ for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
+ if (DarwinStabNames[i].NType == NType)
+ return DarwinStabNames[i].Name;
+ }
+ return nullptr;
+}
+
+// darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
+// a stab n_type value in a Mach-O file.
+static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
+ MachO::nlist_64 STE_64;
+ MachO::nlist STE;
+ uint8_t NType;
+ uint8_t NSect;
+ uint16_t NDesc;
+ DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
+ if (MachO->is64Bit()) {
+ STE_64 = MachO->getSymbol64TableEntry(SymDRI);
+ NType = STE_64.n_type;
+ NSect = STE_64.n_sect;
+ NDesc = STE_64.n_desc;
+ } else {
+ STE = MachO->getSymbolTableEntry(SymDRI);
+ NType = STE.n_type;
+ NSect = STE.n_sect;
+ NDesc = STE.n_desc;
+ }
+
+ char Str[18] = "";
+ format("%02x", NSect).print(Str, sizeof(Str));
+ outs() << ' ' << Str << ' ';
+ format("%04x", NDesc).print(Str, sizeof(Str));
+ outs() << Str << ' ';
+ if (const char *stabString = getDarwinStabString(NType))
+ format("%5.5s", stabString).print(Str, sizeof(Str));
+ else
+ format(" %02x", NType).print(Str, sizeof(Str));
+ outs() << Str;
+}
+
+static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
+ std::string ArchiveName,
+ std::string ArchitectureName) {