llvm-nm.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This program is a utility that works like traditional Unix "nm", that is, it
  10. // prints out the names of symbols in a bitcode or object file, along with some
  11. // information about each symbol.
  12. //
  13. // This "nm" supports many of the features of GNU "nm", including its different
  14. // output formats.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/ADT/StringSwitch.h"
  18. #include "llvm/BinaryFormat/COFF.h"
  19. #include "llvm/Demangle/Demangle.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/Object/Archive.h"
  23. #include "llvm/Object/COFF.h"
  24. #include "llvm/Object/COFFImportFile.h"
  25. #include "llvm/Object/ELFObjectFile.h"
  26. #include "llvm/Object/IRObjectFile.h"
  27. #include "llvm/Object/MachO.h"
  28. #include "llvm/Object/MachOUniversal.h"
  29. #include "llvm/Object/ObjectFile.h"
  30. #include "llvm/Object/TapiFile.h"
  31. #include "llvm/Object/TapiUniversal.h"
  32. #include "llvm/Object/Wasm.h"
  33. #include "llvm/Object/XCOFFObjectFile.h"
  34. #include "llvm/Option/Arg.h"
  35. #include "llvm/Option/ArgList.h"
  36. #include "llvm/Option/Option.h"
  37. #include "llvm/Support/CommandLine.h"
  38. #include "llvm/Support/FileSystem.h"
  39. #include "llvm/Support/Format.h"
  40. #include "llvm/Support/InitLLVM.h"
  41. #include "llvm/Support/MemoryBuffer.h"
  42. #include "llvm/Support/Program.h"
  43. #include "llvm/Support/Signals.h"
  44. #include "llvm/Support/TargetSelect.h"
  45. #include "llvm/Support/WithColor.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <vector>
  48. using namespace llvm;
  49. using namespace object;
  50. namespace {
  51. using namespace llvm::opt; // for HelpHidden in Opts.inc
  52. enum ID {
  53. OPT_INVALID = 0, // This is not an option ID.
  54. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  55. HELPTEXT, METAVAR, VALUES) \
  56. OPT_##ID,
  57. #include "Opts.inc"
  58. #undef OPTION
  59. };
  60. #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
  61. #include "Opts.inc"
  62. #undef PREFIX
  63. const opt::OptTable::Info InfoTable[] = {
  64. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  65. HELPTEXT, METAVAR, VALUES) \
  66. { \
  67. PREFIX, NAME, HELPTEXT, \
  68. METAVAR, OPT_##ID, opt::Option::KIND##Class, \
  69. PARAM, FLAGS, OPT_##GROUP, \
  70. OPT_##ALIAS, ALIASARGS, VALUES},
  71. #include "Opts.inc"
  72. #undef OPTION
  73. };
  74. class NmOptTable : public opt::OptTable {
  75. public:
  76. NmOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); }
  77. };
  78. enum OutputFormatTy { bsd, sysv, posix, darwin, just_symbols };
  79. } // namespace
  80. static bool ArchiveMap;
  81. static bool DebugSyms;
  82. static bool DefinedOnly;
  83. static bool Demangle;
  84. static bool DynamicSyms;
  85. static bool ExternalOnly;
  86. static OutputFormatTy OutputFormat;
  87. static bool NoLLVMBitcode;
  88. static bool NoSort;
  89. static bool NoWeakSymbols;
  90. static bool NumericSort;
  91. static bool PrintFileName;
  92. static bool PrintSize;
  93. static bool Quiet;
  94. static bool ReverseSort;
  95. static bool SpecialSyms;
  96. static bool SizeSort;
  97. static bool UndefinedOnly;
  98. static bool WithoutAliases;
  99. namespace {
  100. enum Radix { d, o, x };
  101. } // namespace
  102. static Radix AddressRadix;
  103. // Mach-O specific options.
  104. static bool ArchAll = false;
  105. static std::vector<StringRef> ArchFlags;
  106. static bool AddDyldInfo;
  107. static bool AddInlinedInfo;
  108. static bool DyldInfoOnly;
  109. static bool FormatMachOasHex;
  110. static bool NoDyldInfo;
  111. static std::vector<StringRef> SegSect;
  112. static bool MachOPrintSizeWarning = false;
  113. // Miscellaneous states.
  114. static bool PrintAddress = true;
  115. static bool MultipleFiles = false;
  116. static bool HadError = false;
  117. static StringRef ToolName;
  118. static void warn(Error Err, Twine FileName, Twine Context = Twine()) {
  119. assert(Err);
  120. // Flush the standard output so that the warning isn't interleaved with other
  121. // output if stdout and stderr are writing to the same place.
  122. outs().flush();
  123. handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
  124. WithColor::warning(errs(), ToolName)
  125. << FileName << ": " << (Context.str().empty() ? "" : Context + ": ")
  126. << EI.message() << "\n";
  127. });
  128. }
  129. static void error(Twine Message, Twine Path = Twine()) {
  130. HadError = true;
  131. WithColor::error(errs(), ToolName) << Path << ": " << Message << "\n";
  132. }
  133. static bool error(std::error_code EC, Twine Path = Twine()) {
  134. if (EC) {
  135. error(EC.message(), Path);
  136. return true;
  137. }
  138. return false;
  139. }
  140. // This version of error() prints the archive name and member name, for example:
  141. // "libx.a(foo.o)" after the ToolName before the error message. It sets
  142. // HadError but returns allowing the code to move on to other archive members.
  143. static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
  144. StringRef ArchitectureName = StringRef()) {
  145. HadError = true;
  146. WithColor::error(errs(), ToolName) << FileName;
  147. Expected<StringRef> NameOrErr = C.getName();
  148. // TODO: if we have a error getting the name then it would be nice to print
  149. // the index of which archive member this is and or its offset in the
  150. // archive instead of "???" as the name.
  151. if (!NameOrErr) {
  152. consumeError(NameOrErr.takeError());
  153. errs() << "(" << "???" << ")";
  154. } else
  155. errs() << "(" << NameOrErr.get() << ")";
  156. if (!ArchitectureName.empty())
  157. errs() << " (for architecture " << ArchitectureName << ")";
  158. std::string Buf;
  159. raw_string_ostream OS(Buf);
  160. logAllUnhandledErrors(std::move(E), OS);
  161. OS.flush();
  162. errs() << ": " << Buf << "\n";
  163. }
  164. // This version of error() prints the file name and which architecture slice it
  165. // is from, for example: "foo.o (for architecture i386)" after the ToolName
  166. // before the error message. It sets HadError but returns allowing the code to
  167. // move on to other architecture slices.
  168. static void error(llvm::Error E, StringRef FileName,
  169. StringRef ArchitectureName = StringRef()) {
  170. HadError = true;
  171. WithColor::error(errs(), ToolName) << FileName;
  172. if (!ArchitectureName.empty())
  173. errs() << " (for architecture " << ArchitectureName << ")";
  174. std::string Buf;
  175. raw_string_ostream OS(Buf);
  176. logAllUnhandledErrors(std::move(E), OS);
  177. OS.flush();
  178. errs() << ": " << Buf << "\n";
  179. }
  180. namespace {
  181. struct NMSymbol {
  182. uint64_t Address;
  183. uint64_t Size;
  184. char TypeChar;
  185. std::string Name;
  186. StringRef SectionName;
  187. StringRef TypeName;
  188. BasicSymbolRef Sym;
  189. // The Sym field above points to the native symbol in the object file,
  190. // for Mach-O when we are creating symbols from the dyld info the above
  191. // pointer is null as there is no native symbol. In these cases the fields
  192. // below are filled in to represent what would have been a Mach-O nlist
  193. // native symbol.
  194. uint32_t SymFlags;
  195. SectionRef Section;
  196. uint8_t NType;
  197. uint8_t NSect;
  198. uint16_t NDesc;
  199. std::string IndirectName;
  200. };
  201. } // anonymous namespace
  202. static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
  203. bool ADefined;
  204. // Symbol flags have been checked in the caller.
  205. if (A.Sym.getRawDataRefImpl().p) {
  206. uint32_t AFlags = cantFail(A.Sym.getFlags());
  207. ADefined = !(AFlags & SymbolRef::SF_Undefined);
  208. } else {
  209. ADefined = A.TypeChar != 'U';
  210. }
  211. bool BDefined;
  212. // Symbol flags have been checked in the caller.
  213. if (B.Sym.getRawDataRefImpl().p) {
  214. uint32_t BFlags = cantFail(B.Sym.getFlags());
  215. BDefined = !(BFlags & SymbolRef::SF_Undefined);
  216. } else {
  217. BDefined = B.TypeChar != 'U';
  218. }
  219. return std::make_tuple(ADefined, A.Address, A.Name, A.Size) <
  220. std::make_tuple(BDefined, B.Address, B.Name, B.Size);
  221. }
  222. static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
  223. return std::make_tuple(A.Size, A.Name, A.Address) <
  224. std::make_tuple(B.Size, B.Name, B.Address);
  225. }
  226. static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
  227. return std::make_tuple(A.Name, A.Size, A.Address) <
  228. std::make_tuple(B.Name, B.Size, B.Address);
  229. }
  230. static char isSymbolList64Bit(SymbolicFile &Obj) {
  231. if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
  232. return Triple(IRObj->getTargetTriple()).isArch64Bit();
  233. if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj))
  234. return false;
  235. if (XCOFFObjectFile *XCOFFObj = dyn_cast<XCOFFObjectFile>(&Obj))
  236. return XCOFFObj->is64Bit();
  237. if (isa<WasmObjectFile>(Obj))
  238. return false;
  239. if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj))
  240. return Tapi->is64Bit();
  241. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
  242. return MachO->is64Bit();
  243. return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
  244. }
  245. static StringRef CurrentFilename;
  246. static std::vector<NMSymbol> SymbolList;
  247. static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
  248. // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
  249. // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
  250. // the darwin format it produces the same output as darwin's nm(1) -m output
  251. // and when printing Mach-O symbols in hex it produces the same output as
  252. // darwin's nm(1) -x format.
  253. static void darwinPrintSymbol(SymbolicFile &Obj, const NMSymbol &S,
  254. char *SymbolAddrStr, const char *printBlanks,
  255. const char *printDashes,
  256. const char *printFormat) {
  257. MachO::mach_header H;
  258. MachO::mach_header_64 H_64;
  259. uint32_t Filetype = MachO::MH_OBJECT;
  260. uint32_t Flags = 0;
  261. uint8_t NType = 0;
  262. uint8_t NSect = 0;
  263. uint16_t NDesc = 0;
  264. uint32_t NStrx = 0;
  265. uint64_t NValue = 0;
  266. MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
  267. if (Obj.isIR()) {
  268. uint32_t SymFlags = cantFail(S.Sym.getFlags());
  269. if (SymFlags & SymbolRef::SF_Global)
  270. NType |= MachO::N_EXT;
  271. if (SymFlags & SymbolRef::SF_Hidden)
  272. NType |= MachO::N_PEXT;
  273. if (SymFlags & SymbolRef::SF_Undefined)
  274. NType |= MachO::N_EXT | MachO::N_UNDF;
  275. else {
  276. // Here we have a symbol definition. So to fake out a section name we
  277. // use 1, 2 and 3 for section numbers. See below where they are used to
  278. // print out fake section names.
  279. NType |= MachO::N_SECT;
  280. if (SymFlags & SymbolRef::SF_Const)
  281. NSect = 3;
  282. else if (SymFlags & SymbolRef::SF_Executable)
  283. NSect = 1;
  284. else
  285. NSect = 2;
  286. }
  287. if (SymFlags & SymbolRef::SF_Weak)
  288. NDesc |= MachO::N_WEAK_DEF;
  289. } else {
  290. DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
  291. if (MachO->is64Bit()) {
  292. H_64 = MachO->MachOObjectFile::getHeader64();
  293. Filetype = H_64.filetype;
  294. Flags = H_64.flags;
  295. if (SymDRI.p){
  296. MachO::nlist_64 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
  297. NType = STE_64.n_type;
  298. NSect = STE_64.n_sect;
  299. NDesc = STE_64.n_desc;
  300. NStrx = STE_64.n_strx;
  301. NValue = STE_64.n_value;
  302. } else {
  303. NType = S.NType;
  304. NSect = S.NSect;
  305. NDesc = S.NDesc;
  306. NStrx = 0;
  307. NValue = S.Address;
  308. }
  309. } else {
  310. H = MachO->MachOObjectFile::getHeader();
  311. Filetype = H.filetype;
  312. Flags = H.flags;
  313. if (SymDRI.p){
  314. MachO::nlist STE = MachO->getSymbolTableEntry(SymDRI);
  315. NType = STE.n_type;
  316. NSect = STE.n_sect;
  317. NDesc = STE.n_desc;
  318. NStrx = STE.n_strx;
  319. NValue = STE.n_value;
  320. } else {
  321. NType = S.NType;
  322. NSect = S.NSect;
  323. NDesc = S.NDesc;
  324. NStrx = 0;
  325. NValue = S.Address;
  326. }
  327. }
  328. }
  329. // If we are printing Mach-O symbols in hex do that and return.
  330. if (FormatMachOasHex) {
  331. outs() << format(printFormat, NValue) << ' '
  332. << format("%02x %02x %04x %08x", NType, NSect, NDesc, NStrx) << ' '
  333. << S.Name;
  334. if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
  335. outs() << " (indirect for ";
  336. outs() << format(printFormat, NValue) << ' ';
  337. StringRef IndirectName;
  338. if (S.Sym.getRawDataRefImpl().p) {
  339. if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
  340. outs() << "?)";
  341. else
  342. outs() << IndirectName << ")";
  343. } else
  344. outs() << S.IndirectName << ")";
  345. }
  346. outs() << "\n";
  347. return;
  348. }
  349. if (PrintAddress) {
  350. if ((NType & MachO::N_TYPE) == MachO::N_INDR)
  351. strcpy(SymbolAddrStr, printBlanks);
  352. if (Obj.isIR() && (NType & MachO::N_TYPE) == MachO::N_TYPE)
  353. strcpy(SymbolAddrStr, printDashes);
  354. outs() << SymbolAddrStr << ' ';
  355. }
  356. switch (NType & MachO::N_TYPE) {
  357. case MachO::N_UNDF:
  358. if (NValue != 0) {
  359. outs() << "(common) ";
  360. if (MachO::GET_COMM_ALIGN(NDesc) != 0)
  361. outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
  362. } else {
  363. if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
  364. outs() << "(prebound ";
  365. else
  366. outs() << "(";
  367. if ((NDesc & MachO::REFERENCE_TYPE) ==
  368. MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
  369. outs() << "undefined [lazy bound]) ";
  370. else if ((NDesc & MachO::REFERENCE_TYPE) ==
  371. MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
  372. outs() << "undefined [private lazy bound]) ";
  373. else if ((NDesc & MachO::REFERENCE_TYPE) ==
  374. MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
  375. outs() << "undefined [private]) ";
  376. else
  377. outs() << "undefined) ";
  378. }
  379. break;
  380. case MachO::N_ABS:
  381. outs() << "(absolute) ";
  382. break;
  383. case MachO::N_INDR:
  384. outs() << "(indirect) ";
  385. break;
  386. case MachO::N_SECT: {
  387. if (Obj.isIR()) {
  388. // For llvm bitcode files print out a fake section name using the values
  389. // use 1, 2 and 3 for section numbers as set above.
  390. if (NSect == 1)
  391. outs() << "(LTO,CODE) ";
  392. else if (NSect == 2)
  393. outs() << "(LTO,DATA) ";
  394. else if (NSect == 3)
  395. outs() << "(LTO,RODATA) ";
  396. else
  397. outs() << "(?,?) ";
  398. break;
  399. }
  400. section_iterator Sec = SectionRef();
  401. if (S.Sym.getRawDataRefImpl().p) {
  402. Expected<section_iterator> SecOrErr =
  403. MachO->getSymbolSection(S.Sym.getRawDataRefImpl());
  404. if (!SecOrErr) {
  405. consumeError(SecOrErr.takeError());
  406. outs() << "(?,?) ";
  407. break;
  408. }
  409. Sec = *SecOrErr;
  410. if (Sec == MachO->section_end()) {
  411. outs() << "(?,?) ";
  412. break;
  413. }
  414. } else {
  415. Sec = S.Section;
  416. }
  417. DataRefImpl Ref = Sec->getRawDataRefImpl();
  418. StringRef SectionName;
  419. if (Expected<StringRef> NameOrErr = MachO->getSectionName(Ref))
  420. SectionName = *NameOrErr;
  421. StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
  422. outs() << "(" << SegmentName << "," << SectionName << ") ";
  423. break;
  424. }
  425. default:
  426. outs() << "(?) ";
  427. break;
  428. }
  429. if (NType & MachO::N_EXT) {
  430. if (NDesc & MachO::REFERENCED_DYNAMICALLY)
  431. outs() << "[referenced dynamically] ";
  432. if (NType & MachO::N_PEXT) {
  433. if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
  434. outs() << "weak private external ";
  435. else
  436. outs() << "private external ";
  437. } else {
  438. if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
  439. (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
  440. if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
  441. (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
  442. outs() << "weak external automatically hidden ";
  443. else
  444. outs() << "weak external ";
  445. } else
  446. outs() << "external ";
  447. }
  448. } else {
  449. if (NType & MachO::N_PEXT)
  450. outs() << "non-external (was a private external) ";
  451. else
  452. outs() << "non-external ";
  453. }
  454. if (Filetype == MachO::MH_OBJECT) {
  455. if (NDesc & MachO::N_NO_DEAD_STRIP)
  456. outs() << "[no dead strip] ";
  457. if ((NType & MachO::N_TYPE) != MachO::N_UNDF &&
  458. NDesc & MachO::N_SYMBOL_RESOLVER)
  459. outs() << "[symbol resolver] ";
  460. if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_ALT_ENTRY)
  461. outs() << "[alt entry] ";
  462. if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_COLD_FUNC)
  463. outs() << "[cold func] ";
  464. }
  465. if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
  466. outs() << "[Thumb] ";
  467. if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
  468. outs() << S.Name << " (for ";
  469. StringRef IndirectName;
  470. if (MachO) {
  471. if (S.Sym.getRawDataRefImpl().p) {
  472. if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
  473. outs() << "?)";
  474. else
  475. outs() << IndirectName << ")";
  476. } else
  477. outs() << S.IndirectName << ")";
  478. } else
  479. outs() << "?)";
  480. } else
  481. outs() << S.Name;
  482. if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
  483. (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
  484. (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
  485. uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
  486. if (LibraryOrdinal != 0) {
  487. if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
  488. outs() << " (from executable)";
  489. else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
  490. outs() << " (dynamically looked up)";
  491. else {
  492. StringRef LibraryName;
  493. if (!MachO ||
  494. MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
  495. outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
  496. else
  497. outs() << " (from " << LibraryName << ")";
  498. }
  499. }
  500. }
  501. outs() << "\n";
  502. }
  503. // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
  504. struct DarwinStabName {
  505. uint8_t NType;
  506. const char *Name;
  507. };
  508. const struct DarwinStabName DarwinStabNames[] = {
  509. {MachO::N_GSYM, "GSYM"},
  510. {MachO::N_FNAME, "FNAME"},
  511. {MachO::N_FUN, "FUN"},
  512. {MachO::N_STSYM, "STSYM"},
  513. {MachO::N_LCSYM, "LCSYM"},
  514. {MachO::N_BNSYM, "BNSYM"},
  515. {MachO::N_PC, "PC"},
  516. {MachO::N_AST, "AST"},
  517. {MachO::N_OPT, "OPT"},
  518. {MachO::N_RSYM, "RSYM"},
  519. {MachO::N_SLINE, "SLINE"},
  520. {MachO::N_ENSYM, "ENSYM"},
  521. {MachO::N_SSYM, "SSYM"},
  522. {MachO::N_SO, "SO"},
  523. {MachO::N_OSO, "OSO"},
  524. {MachO::N_LSYM, "LSYM"},
  525. {MachO::N_BINCL, "BINCL"},
  526. {MachO::N_SOL, "SOL"},
  527. {MachO::N_PARAMS, "PARAM"},
  528. {MachO::N_VERSION, "VERS"},
  529. {MachO::N_OLEVEL, "OLEV"},
  530. {MachO::N_PSYM, "PSYM"},
  531. {MachO::N_EINCL, "EINCL"},
  532. {MachO::N_ENTRY, "ENTRY"},
  533. {MachO::N_LBRAC, "LBRAC"},
  534. {MachO::N_EXCL, "EXCL"},
  535. {MachO::N_RBRAC, "RBRAC"},
  536. {MachO::N_BCOMM, "BCOMM"},
  537. {MachO::N_ECOMM, "ECOMM"},
  538. {MachO::N_ECOML, "ECOML"},
  539. {MachO::N_LENG, "LENG"},
  540. };
  541. static const char *getDarwinStabString(uint8_t NType) {
  542. for (auto I : makeArrayRef(DarwinStabNames))
  543. if (I.NType == NType)
  544. return I.Name;
  545. return nullptr;
  546. }
  547. // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
  548. // a stab n_type value in a Mach-O file.
  549. static void darwinPrintStab(MachOObjectFile *MachO, const NMSymbol &S) {
  550. MachO::nlist_64 STE_64;
  551. MachO::nlist STE;
  552. uint8_t NType;
  553. uint8_t NSect;
  554. uint16_t NDesc;
  555. DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
  556. if (MachO->is64Bit()) {
  557. STE_64 = MachO->getSymbol64TableEntry(SymDRI);
  558. NType = STE_64.n_type;
  559. NSect = STE_64.n_sect;
  560. NDesc = STE_64.n_desc;
  561. } else {
  562. STE = MachO->getSymbolTableEntry(SymDRI);
  563. NType = STE.n_type;
  564. NSect = STE.n_sect;
  565. NDesc = STE.n_desc;
  566. }
  567. outs() << format(" %02x %04x ", NSect, NDesc);
  568. if (const char *stabString = getDarwinStabString(NType))
  569. outs() << format("%5.5s", stabString);
  570. else
  571. outs() << format(" %02x", NType);
  572. }
  573. static Optional<std::string> demangle(StringRef Name) {
  574. std::string Demangled;
  575. if (nonMicrosoftDemangle(Name.str().c_str(), Demangled))
  576. return Demangled;
  577. return None;
  578. }
  579. static Optional<std::string> demangleXCOFF(StringRef Name) {
  580. if (Name.empty() || Name[0] != '.')
  581. return demangle(Name);
  582. Name = Name.drop_front();
  583. Optional<std::string> DemangledName = demangle(Name);
  584. if (DemangledName)
  585. return "." + *DemangledName;
  586. return None;
  587. }
  588. static Optional<std::string> demangleMachO(StringRef Name) {
  589. if (!Name.empty() && Name[0] == '_')
  590. Name = Name.drop_front();
  591. return demangle(Name);
  592. }
  593. static bool symbolIsDefined(const NMSymbol &Sym) {
  594. return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v';
  595. }
  596. static void writeFileName(raw_ostream &S, StringRef ArchiveName,
  597. StringRef ArchitectureName) {
  598. if (!ArchitectureName.empty())
  599. S << "(for architecture " << ArchitectureName << "):";
  600. if (OutputFormat == posix && !ArchiveName.empty())
  601. S << ArchiveName << "[" << CurrentFilename << "]: ";
  602. else {
  603. if (!ArchiveName.empty())
  604. S << ArchiveName << ":";
  605. S << CurrentFilename << ": ";
  606. }
  607. }
  608. static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
  609. StringRef ArchiveName,
  610. StringRef ArchitectureName) {
  611. if (!NoSort) {
  612. using Comparator = bool (*)(const NMSymbol &, const NMSymbol &);
  613. Comparator Cmp;
  614. if (NumericSort)
  615. Cmp = &compareSymbolAddress;
  616. else if (SizeSort)
  617. Cmp = &compareSymbolSize;
  618. else
  619. Cmp = &compareSymbolName;
  620. if (ReverseSort)
  621. llvm::sort(SymbolList, [=](const NMSymbol &A, const NMSymbol &B) -> bool {
  622. return Cmp(B, A);
  623. });
  624. else
  625. llvm::sort(SymbolList, Cmp);
  626. }
  627. if (!PrintFileName) {
  628. if ((OutputFormat == bsd || OutputFormat == posix ||
  629. OutputFormat == just_symbols) &&
  630. MultipleFiles && printName) {
  631. outs() << '\n' << CurrentFilename << ":\n";
  632. } else if (OutputFormat == sysv) {
  633. outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n";
  634. if (isSymbolList64Bit(Obj))
  635. outs() << "Name Value Class Type"
  636. << " Size Line Section\n";
  637. else
  638. outs() << "Name Value Class Type"
  639. << " Size Line Section\n";
  640. }
  641. }
  642. const char *printBlanks, *printDashes, *printFormat;
  643. if (isSymbolList64Bit(Obj)) {
  644. printBlanks = " ";
  645. printDashes = "----------------";
  646. switch (AddressRadix) {
  647. case Radix::o:
  648. printFormat = OutputFormat == posix ? "%" PRIo64 : "%016" PRIo64;
  649. break;
  650. case Radix::x:
  651. printFormat = OutputFormat == posix ? "%" PRIx64 : "%016" PRIx64;
  652. break;
  653. default:
  654. printFormat = OutputFormat == posix ? "%" PRId64 : "%016" PRId64;
  655. }
  656. } else {
  657. printBlanks = " ";
  658. printDashes = "--------";
  659. switch (AddressRadix) {
  660. case Radix::o:
  661. printFormat = OutputFormat == posix ? "%" PRIo64 : "%08" PRIo64;
  662. break;
  663. case Radix::x:
  664. printFormat = OutputFormat == posix ? "%" PRIx64 : "%08" PRIx64;
  665. break;
  666. default:
  667. printFormat = OutputFormat == posix ? "%" PRId64 : "%08" PRId64;
  668. }
  669. }
  670. for (const NMSymbol &S : SymbolList) {
  671. uint32_t SymFlags;
  672. std::string Name = S.Name;
  673. MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
  674. if (Demangle) {
  675. function_ref<Optional<std::string>(StringRef)> Fn = ::demangle;
  676. if (Obj.isXCOFF())
  677. Fn = demangleXCOFF;
  678. if (Obj.isMachO())
  679. Fn = demangleMachO;
  680. if (Optional<std::string> Opt = Fn(S.Name))
  681. Name = *Opt;
  682. }
  683. if (S.Sym.getRawDataRefImpl().p) {
  684. Expected<uint32_t> SymFlagsOrErr = S.Sym.getFlags();
  685. if (!SymFlagsOrErr) {
  686. // TODO: Test this error.
  687. error(SymFlagsOrErr.takeError(), Obj.getFileName());
  688. return;
  689. }
  690. SymFlags = *SymFlagsOrErr;
  691. } else
  692. SymFlags = S.SymFlags;
  693. bool Undefined = SymFlags & SymbolRef::SF_Undefined;
  694. bool Global = SymFlags & SymbolRef::SF_Global;
  695. bool Weak = SymFlags & SymbolRef::SF_Weak;
  696. bool FormatSpecific = SymFlags & SymbolRef::SF_FormatSpecific;
  697. if ((!Undefined && UndefinedOnly) || (Undefined && DefinedOnly) ||
  698. (!Global && ExternalOnly) || (Weak && NoWeakSymbols) ||
  699. (FormatSpecific && !(SpecialSyms || DebugSyms)))
  700. continue;
  701. if (PrintFileName)
  702. writeFileName(outs(), ArchiveName, ArchitectureName);
  703. if ((OutputFormat == just_symbols ||
  704. (UndefinedOnly && MachO && OutputFormat != darwin)) &&
  705. OutputFormat != posix) {
  706. outs() << Name << "\n";
  707. continue;
  708. }
  709. char SymbolAddrStr[23], SymbolSizeStr[23];
  710. // If the format is SysV or the symbol isn't defined, then print spaces.
  711. if (OutputFormat == sysv || !symbolIsDefined(S)) {
  712. if (OutputFormat == posix) {
  713. format(printFormat, S.Address)
  714. .print(SymbolAddrStr, sizeof(SymbolAddrStr));
  715. format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
  716. } else {
  717. strcpy(SymbolAddrStr, printBlanks);
  718. strcpy(SymbolSizeStr, printBlanks);
  719. }
  720. }
  721. if (symbolIsDefined(S)) {
  722. // Otherwise, print the symbol address and size.
  723. if (Obj.isIR())
  724. strcpy(SymbolAddrStr, printDashes);
  725. else if (MachO && S.TypeChar == 'I')
  726. strcpy(SymbolAddrStr, printBlanks);
  727. else
  728. format(printFormat, S.Address)
  729. .print(SymbolAddrStr, sizeof(SymbolAddrStr));
  730. format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
  731. }
  732. // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
  733. // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
  734. // nm(1) -m output or hex, else if OutputFormat is darwin or we are
  735. // printing Mach-O symbols in hex and not a Mach-O object fall back to
  736. // OutputFormat bsd (see below).
  737. if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
  738. darwinPrintSymbol(Obj, S, SymbolAddrStr, printBlanks, printDashes,
  739. printFormat);
  740. } else if (OutputFormat == posix) {
  741. outs() << Name << " " << S.TypeChar << " " << SymbolAddrStr << " "
  742. << (MachO ? "0" : SymbolSizeStr) << "\n";
  743. } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
  744. if (PrintAddress)
  745. outs() << SymbolAddrStr << ' ';
  746. if (PrintSize)
  747. outs() << SymbolSizeStr << ' ';
  748. outs() << S.TypeChar;
  749. if (S.TypeChar == '-' && MachO)
  750. darwinPrintStab(MachO, S);
  751. outs() << " " << Name;
  752. if (S.TypeChar == 'I' && MachO) {
  753. outs() << " (indirect for ";
  754. if (S.Sym.getRawDataRefImpl().p) {
  755. StringRef IndirectName;
  756. if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
  757. outs() << "?)";
  758. else
  759. outs() << IndirectName << ")";
  760. } else
  761. outs() << S.IndirectName << ")";
  762. }
  763. outs() << "\n";
  764. } else if (OutputFormat == sysv) {
  765. outs() << left_justify(Name, 20) << "|" << SymbolAddrStr << "| "
  766. << S.TypeChar << " |" << right_justify(S.TypeName, 18) << "|"
  767. << SymbolSizeStr << "| |" << S.SectionName << "\n";
  768. }
  769. }
  770. SymbolList.clear();
  771. }
  772. static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
  773. basic_symbol_iterator I) {
  774. // OK, this is ELF
  775. elf_symbol_iterator SymI(I);
  776. Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
  777. if (!SecIOrErr) {
  778. consumeError(SecIOrErr.takeError());
  779. return '?';
  780. }
  781. uint8_t Binding = SymI->getBinding();
  782. if (Binding == ELF::STB_GNU_UNIQUE)
  783. return 'u';
  784. assert(Binding != ELF::STB_WEAK && "STB_WEAK not tested in calling function");
  785. if (Binding != ELF::STB_GLOBAL && Binding != ELF::STB_LOCAL)
  786. return '?';
  787. elf_section_iterator SecI = *SecIOrErr;
  788. if (SecI != Obj.section_end()) {
  789. uint32_t Type = SecI->getType();
  790. uint64_t Flags = SecI->getFlags();
  791. if (Flags & ELF::SHF_EXECINSTR)
  792. return 't';
  793. if (Type == ELF::SHT_NOBITS)
  794. return 'b';
  795. if (Flags & ELF::SHF_ALLOC)
  796. return Flags & ELF::SHF_WRITE ? 'd' : 'r';
  797. auto NameOrErr = SecI->getName();
  798. if (!NameOrErr) {
  799. consumeError(NameOrErr.takeError());
  800. return '?';
  801. }
  802. if ((*NameOrErr).startswith(".debug"))
  803. return 'N';
  804. if (!(Flags & ELF::SHF_WRITE))
  805. return 'n';
  806. }
  807. return '?';
  808. }
  809. static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
  810. COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
  811. // OK, this is COFF.
  812. symbol_iterator SymI(I);
  813. Expected<StringRef> Name = SymI->getName();
  814. if (!Name) {
  815. consumeError(Name.takeError());
  816. return '?';
  817. }
  818. char Ret = StringSwitch<char>(*Name)
  819. .StartsWith(".debug", 'N')
  820. .StartsWith(".sxdata", 'N')
  821. .Default('?');
  822. if (Ret != '?')
  823. return Ret;
  824. uint32_t Characteristics = 0;
  825. if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
  826. Expected<section_iterator> SecIOrErr = SymI->getSection();
  827. if (!SecIOrErr) {
  828. consumeError(SecIOrErr.takeError());
  829. return '?';
  830. }
  831. section_iterator SecI = *SecIOrErr;
  832. const coff_section *Section = Obj.getCOFFSection(*SecI);
  833. Characteristics = Section->Characteristics;
  834. if (Expected<StringRef> NameOrErr = Obj.getSectionName(Section))
  835. if (NameOrErr->startswith(".idata"))
  836. return 'i';
  837. }
  838. switch (Symb.getSectionNumber()) {
  839. case COFF::IMAGE_SYM_DEBUG:
  840. return 'n';
  841. default:
  842. // Check section type.
  843. if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
  844. return 't';
  845. if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
  846. return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
  847. if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
  848. return 'b';
  849. if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
  850. return 'i';
  851. // Check for section symbol.
  852. if (Symb.isSectionDefinition())
  853. return 's';
  854. }
  855. return '?';
  856. }
  857. static char getSymbolNMTypeChar(XCOFFObjectFile &Obj, symbol_iterator I) {
  858. Expected<uint32_t> TypeOrErr = I->getType();
  859. if (!TypeOrErr) {
  860. warn(TypeOrErr.takeError(), Obj.getFileName(),
  861. "for symbol with index " +
  862. Twine(Obj.getSymbolIndex(I->getRawDataRefImpl().p)));
  863. return '?';
  864. }
  865. uint32_t SymType = *TypeOrErr;
  866. if (SymType == SymbolRef::ST_File)
  867. return 'f';
  868. // If the I->getSection() call would return an error, the earlier I->getType()
  869. // call will already have returned the same error first.
  870. section_iterator SecIter = cantFail(I->getSection());
  871. if (SecIter == Obj.section_end())
  872. return '?';
  873. if (Obj.isDebugSection(SecIter->getRawDataRefImpl()))
  874. return 'N';
  875. if (SecIter->isText())
  876. return 't';
  877. if (SecIter->isData())
  878. return 'd';
  879. if (SecIter->isBSS())
  880. return 'b';
  881. return '?';
  882. }
  883. static char getSymbolNMTypeChar(COFFImportFile &Obj) {
  884. switch (Obj.getCOFFImportHeader()->getType()) {
  885. case COFF::IMPORT_CODE:
  886. return 't';
  887. case COFF::IMPORT_DATA:
  888. return 'd';
  889. case COFF::IMPORT_CONST:
  890. return 'r';
  891. }
  892. return '?';
  893. }
  894. static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
  895. DataRefImpl Symb = I->getRawDataRefImpl();
  896. uint8_t NType = Obj.is64Bit() ? Obj.getSymbol64TableEntry(Symb).n_type
  897. : Obj.getSymbolTableEntry(Symb).n_type;
  898. if (NType & MachO::N_STAB)
  899. return '-';
  900. switch (NType & MachO::N_TYPE) {
  901. case MachO::N_ABS:
  902. return 's';
  903. case MachO::N_INDR:
  904. return 'i';
  905. case MachO::N_SECT: {
  906. Expected<section_iterator> SecOrErr = Obj.getSymbolSection(Symb);
  907. if (!SecOrErr) {
  908. consumeError(SecOrErr.takeError());
  909. return 's';
  910. }
  911. section_iterator Sec = *SecOrErr;
  912. if (Sec == Obj.section_end())
  913. return 's';
  914. DataRefImpl Ref = Sec->getRawDataRefImpl();
  915. StringRef SectionName;
  916. if (Expected<StringRef> NameOrErr = Obj.getSectionName(Ref))
  917. SectionName = *NameOrErr;
  918. StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
  919. if (Obj.is64Bit() && Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
  920. SegmentName == "__TEXT_EXEC" && SectionName == "__text")
  921. return 't';
  922. if (SegmentName == "__TEXT" && SectionName == "__text")
  923. return 't';
  924. if (SegmentName == "__DATA" && SectionName == "__data")
  925. return 'd';
  926. if (SegmentName == "__DATA" && SectionName == "__bss")
  927. return 'b';
  928. return 's';
  929. }
  930. }
  931. return '?';
  932. }
  933. static char getSymbolNMTypeChar(TapiFile &Obj, basic_symbol_iterator I) {
  934. return 's';
  935. }
  936. static char getSymbolNMTypeChar(WasmObjectFile &Obj, basic_symbol_iterator I) {
  937. uint32_t Flags = cantFail(I->getFlags());
  938. if (Flags & SymbolRef::SF_Executable)
  939. return 't';
  940. return 'd';
  941. }
  942. static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
  943. uint32_t Flags = cantFail(I->getFlags());
  944. // FIXME: should we print 'b'? At the IR level we cannot be sure if this
  945. // will be in bss or not, but we could approximate.
  946. if (Flags & SymbolRef::SF_Executable)
  947. return 't';
  948. else if (Triple(Obj.getTargetTriple()).isOSDarwin() &&
  949. (Flags & SymbolRef::SF_Const))
  950. return 's';
  951. else
  952. return 'd';
  953. }
  954. static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
  955. return isa<ELFObjectFileBase>(&Obj) &&
  956. elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
  957. }
  958. // For ELF object files, Set TypeName to the symbol typename, to be printed
  959. // in the 'Type' column of the SYSV format output.
  960. static StringRef getNMTypeName(SymbolicFile &Obj, basic_symbol_iterator I) {
  961. if (isa<ELFObjectFileBase>(&Obj)) {
  962. elf_symbol_iterator SymI(I);
  963. return SymI->getELFTypeName();
  964. }
  965. return "";
  966. }
  967. // Return Posix nm class type tag (single letter), but also set SecName and
  968. // section and name, to be used in format=sysv output.
  969. static char getNMSectionTagAndName(SymbolicFile &Obj, basic_symbol_iterator I,
  970. StringRef &SecName) {
  971. // Symbol Flags have been checked in the caller.
  972. uint32_t Symflags = cantFail(I->getFlags());
  973. if (ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj)) {
  974. if (Symflags & object::SymbolRef::SF_Absolute)
  975. SecName = "*ABS*";
  976. else if (Symflags & object::SymbolRef::SF_Common)
  977. SecName = "*COM*";
  978. else if (Symflags & object::SymbolRef::SF_Undefined)
  979. SecName = "*UND*";
  980. else {
  981. elf_symbol_iterator SymI(I);
  982. Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
  983. if (!SecIOrErr) {
  984. consumeError(SecIOrErr.takeError());
  985. return '?';
  986. }
  987. if (*SecIOrErr == ELFObj->section_end())
  988. return '?';
  989. Expected<StringRef> NameOrErr = (*SecIOrErr)->getName();
  990. if (!NameOrErr) {
  991. consumeError(NameOrErr.takeError());
  992. return '?';
  993. }
  994. SecName = *NameOrErr;
  995. }
  996. }
  997. if (Symflags & object::SymbolRef::SF_Undefined) {
  998. if (isa<MachOObjectFile>(Obj) || !(Symflags & object::SymbolRef::SF_Weak))
  999. return 'U';
  1000. return isObject(Obj, I) ? 'v' : 'w';
  1001. }
  1002. if (isa<ELFObjectFileBase>(&Obj))
  1003. if (ELFSymbolRef(*I).getELFType() == ELF::STT_GNU_IFUNC)
  1004. return 'i';
  1005. if (!isa<MachOObjectFile>(Obj) && (Symflags & object::SymbolRef::SF_Weak))
  1006. return isObject(Obj, I) ? 'V' : 'W';
  1007. if (Symflags & object::SymbolRef::SF_Common)
  1008. return 'C';
  1009. char Ret = '?';
  1010. if (Symflags & object::SymbolRef::SF_Absolute)
  1011. Ret = 'a';
  1012. else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj))
  1013. Ret = getSymbolNMTypeChar(*IR, I);
  1014. else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
  1015. Ret = getSymbolNMTypeChar(*COFF, I);
  1016. else if (XCOFFObjectFile *XCOFF = dyn_cast<XCOFFObjectFile>(&Obj))
  1017. Ret = getSymbolNMTypeChar(*XCOFF, I);
  1018. else if (COFFImportFile *COFFImport = dyn_cast<COFFImportFile>(&Obj))
  1019. Ret = getSymbolNMTypeChar(*COFFImport);
  1020. else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
  1021. Ret = getSymbolNMTypeChar(*MachO, I);
  1022. else if (WasmObjectFile *Wasm = dyn_cast<WasmObjectFile>(&Obj))
  1023. Ret = getSymbolNMTypeChar(*Wasm, I);
  1024. else if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj))
  1025. Ret = getSymbolNMTypeChar(*Tapi, I);
  1026. else if (ELFObjectFileBase *ELF = dyn_cast<ELFObjectFileBase>(&Obj)) {
  1027. Ret = getSymbolNMTypeChar(*ELF, I);
  1028. if (ELFSymbolRef(*I).getBinding() == ELF::STB_GNU_UNIQUE)
  1029. return Ret;
  1030. } else
  1031. llvm_unreachable("unknown binary format");
  1032. if (!(Symflags & object::SymbolRef::SF_Global))
  1033. return Ret;
  1034. return toupper(Ret);
  1035. }
  1036. // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
  1037. // option to dump only those symbols from that section in a Mach-O file.
  1038. // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
  1039. // to get the section number for that named section from the command line
  1040. // arguments. It returns the section number for that section in the Mach-O
  1041. // file or zero it is not present.
  1042. static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
  1043. unsigned Nsect = 1;
  1044. for (auto &S : Obj->sections()) {
  1045. DataRefImpl Ref = S.getRawDataRefImpl();
  1046. StringRef SectionName;
  1047. if (Expected<StringRef> NameOrErr = Obj->getSectionName(Ref))
  1048. SectionName = *NameOrErr;
  1049. StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
  1050. if (SegmentName == SegSect[0] && SectionName == SegSect[1])
  1051. return Nsect;
  1052. Nsect++;
  1053. }
  1054. return 0;
  1055. }
  1056. // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
  1057. // option to dump only those symbols from that section in a Mach-O file.
  1058. // It is called once for each symbol in a Mach-O file from
  1059. // dumpSymbolNamesFromObject() and returns the section number for that symbol
  1060. // if it is in a section, else it returns 0.
  1061. static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
  1062. DataRefImpl Symb = Sym.getRawDataRefImpl();
  1063. if (Obj.is64Bit()) {
  1064. MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
  1065. return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
  1066. }
  1067. MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
  1068. return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
  1069. }
  1070. static void dumpSymbolsFromDLInfoMachO(MachOObjectFile &MachO) {
  1071. size_t I = SymbolList.size();
  1072. std::string ExportsNameBuffer;
  1073. raw_string_ostream EOS(ExportsNameBuffer);
  1074. std::string BindsNameBuffer;
  1075. raw_string_ostream BOS(BindsNameBuffer);
  1076. std::string LazysNameBuffer;
  1077. raw_string_ostream LOS(LazysNameBuffer);
  1078. std::string WeaksNameBuffer;
  1079. raw_string_ostream WOS(WeaksNameBuffer);
  1080. std::string FunctionStartsNameBuffer;
  1081. raw_string_ostream FOS(FunctionStartsNameBuffer);
  1082. MachO::mach_header H;
  1083. MachO::mach_header_64 H_64;
  1084. uint32_t HFlags = 0;
  1085. if (MachO.is64Bit()) {
  1086. H_64 = MachO.MachOObjectFile::getHeader64();
  1087. HFlags = H_64.flags;
  1088. } else {
  1089. H = MachO.MachOObjectFile::getHeader();
  1090. HFlags = H.flags;
  1091. }
  1092. uint64_t BaseSegmentAddress = 0;
  1093. for (const auto &Command : MachO.load_commands()) {
  1094. if (Command.C.cmd == MachO::LC_SEGMENT) {
  1095. MachO::segment_command Seg = MachO.getSegmentLoadCommand(Command);
  1096. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  1097. BaseSegmentAddress = Seg.vmaddr;
  1098. break;
  1099. }
  1100. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  1101. MachO::segment_command_64 Seg = MachO.getSegment64LoadCommand(Command);
  1102. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  1103. BaseSegmentAddress = Seg.vmaddr;
  1104. break;
  1105. }
  1106. }
  1107. }
  1108. if (DyldInfoOnly || AddDyldInfo ||
  1109. HFlags & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
  1110. unsigned ExportsAdded = 0;
  1111. Error Err = Error::success();
  1112. for (const llvm::object::ExportEntry &Entry : MachO.exports(Err)) {
  1113. bool found = false;
  1114. bool ReExport = false;
  1115. if (!DyldInfoOnly) {
  1116. for (const NMSymbol &S : SymbolList)
  1117. if (S.Address == Entry.address() + BaseSegmentAddress &&
  1118. S.Name == Entry.name()) {
  1119. found = true;
  1120. break;
  1121. }
  1122. }
  1123. if (!found) {
  1124. NMSymbol S = {};
  1125. S.Address = Entry.address() + BaseSegmentAddress;
  1126. S.Size = 0;
  1127. S.TypeChar = '\0';
  1128. S.Name = Entry.name().str();
  1129. // There is no symbol in the nlist symbol table for this so we set
  1130. // Sym effectivly to null and the rest of code in here must test for
  1131. // it and not do things like Sym.getFlags() for it.
  1132. S.Sym = BasicSymbolRef();
  1133. S.SymFlags = SymbolRef::SF_Global;
  1134. S.Section = SectionRef();
  1135. S.NType = 0;
  1136. S.NSect = 0;
  1137. S.NDesc = 0;
  1138. uint64_t EFlags = Entry.flags();
  1139. bool Abs = ((EFlags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  1140. MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
  1141. bool Resolver = (EFlags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
  1142. ReExport = (EFlags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
  1143. bool WeakDef = (EFlags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
  1144. if (WeakDef)
  1145. S.NDesc |= MachO::N_WEAK_DEF;
  1146. if (Abs) {
  1147. S.NType = MachO::N_EXT | MachO::N_ABS;
  1148. S.TypeChar = 'A';
  1149. } else if (ReExport) {
  1150. S.NType = MachO::N_EXT | MachO::N_INDR;
  1151. S.TypeChar = 'I';
  1152. } else {
  1153. S.NType = MachO::N_EXT | MachO::N_SECT;
  1154. if (Resolver) {
  1155. S.Address = Entry.other() + BaseSegmentAddress;
  1156. if ((S.Address & 1) != 0 && !MachO.is64Bit() &&
  1157. H.cputype == MachO::CPU_TYPE_ARM) {
  1158. S.Address &= ~1LL;
  1159. S.NDesc |= MachO::N_ARM_THUMB_DEF;
  1160. }
  1161. } else {
  1162. S.Address = Entry.address() + BaseSegmentAddress;
  1163. }
  1164. StringRef SegmentName = StringRef();
  1165. StringRef SectionName = StringRef();
  1166. for (const SectionRef &Section : MachO.sections()) {
  1167. S.NSect++;
  1168. if (Expected<StringRef> NameOrErr = Section.getName())
  1169. SectionName = *NameOrErr;
  1170. else
  1171. consumeError(NameOrErr.takeError());
  1172. SegmentName =
  1173. MachO.getSectionFinalSegmentName(Section.getRawDataRefImpl());
  1174. if (S.Address >= Section.getAddress() &&
  1175. S.Address < Section.getAddress() + Section.getSize()) {
  1176. S.Section = Section;
  1177. break;
  1178. } else if (Entry.name() == "__mh_execute_header" &&
  1179. SegmentName == "__TEXT" && SectionName == "__text") {
  1180. S.Section = Section;
  1181. S.NDesc |= MachO::REFERENCED_DYNAMICALLY;
  1182. break;
  1183. }
  1184. }
  1185. if (SegmentName == "__TEXT" && SectionName == "__text")
  1186. S.TypeChar = 'T';
  1187. else if (SegmentName == "__DATA" && SectionName == "__data")
  1188. S.TypeChar = 'D';
  1189. else if (SegmentName == "__DATA" && SectionName == "__bss")
  1190. S.TypeChar = 'B';
  1191. else
  1192. S.TypeChar = 'S';
  1193. }
  1194. SymbolList.push_back(S);
  1195. EOS << Entry.name();
  1196. EOS << '\0';
  1197. ExportsAdded++;
  1198. // For ReExports there are a two more things to do, first add the
  1199. // indirect name and second create the undefined symbol using the
  1200. // referened dynamic library.
  1201. if (ReExport) {
  1202. // Add the indirect name.
  1203. if (Entry.otherName().empty())
  1204. EOS << Entry.name();
  1205. else
  1206. EOS << Entry.otherName();
  1207. EOS << '\0';
  1208. // Now create the undefined symbol using the referened dynamic
  1209. // library.
  1210. NMSymbol U = {};
  1211. U.Address = 0;
  1212. U.Size = 0;
  1213. U.TypeChar = 'U';
  1214. if (Entry.otherName().empty())
  1215. U.Name = Entry.name().str();
  1216. else
  1217. U.Name = Entry.otherName().str();
  1218. // Again there is no symbol in the nlist symbol table for this so
  1219. // we set Sym effectivly to null and the rest of code in here must
  1220. // test for it and not do things like Sym.getFlags() for it.
  1221. U.Sym = BasicSymbolRef();
  1222. U.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
  1223. U.Section = SectionRef();
  1224. U.NType = MachO::N_EXT | MachO::N_UNDF;
  1225. U.NSect = 0;
  1226. U.NDesc = 0;
  1227. // The library ordinal for this undefined symbol is in the export
  1228. // trie Entry.other().
  1229. MachO::SET_LIBRARY_ORDINAL(U.NDesc, Entry.other());
  1230. SymbolList.push_back(U);
  1231. // Finally add the undefined symbol's name.
  1232. if (Entry.otherName().empty())
  1233. EOS << Entry.name();
  1234. else
  1235. EOS << Entry.otherName();
  1236. EOS << '\0';
  1237. ExportsAdded++;
  1238. }
  1239. }
  1240. }
  1241. if (Err)
  1242. error(std::move(Err), MachO.getFileName());
  1243. // Set the symbol names and indirect names for the added symbols.
  1244. if (ExportsAdded) {
  1245. EOS.flush();
  1246. const char *Q = ExportsNameBuffer.c_str();
  1247. for (unsigned K = 0; K < ExportsAdded; K++) {
  1248. SymbolList[I].Name = Q;
  1249. Q += strlen(Q) + 1;
  1250. if (SymbolList[I].TypeChar == 'I') {
  1251. SymbolList[I].IndirectName = Q;
  1252. Q += strlen(Q) + 1;
  1253. }
  1254. I++;
  1255. }
  1256. }
  1257. // Add the undefined symbols from the bind entries.
  1258. unsigned BindsAdded = 0;
  1259. Error BErr = Error::success();
  1260. StringRef LastSymbolName = StringRef();
  1261. for (const llvm::object::MachOBindEntry &Entry : MachO.bindTable(BErr)) {
  1262. bool found = false;
  1263. if (LastSymbolName == Entry.symbolName())
  1264. found = true;
  1265. else if (!DyldInfoOnly) {
  1266. for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
  1267. if (SymbolList[J].Name == Entry.symbolName())
  1268. found = true;
  1269. }
  1270. }
  1271. if (!found) {
  1272. LastSymbolName = Entry.symbolName();
  1273. NMSymbol B = {};
  1274. B.Address = 0;
  1275. B.Size = 0;
  1276. B.TypeChar = 'U';
  1277. // There is no symbol in the nlist symbol table for this so we set
  1278. // Sym effectivly to null and the rest of code in here must test for
  1279. // it and not do things like Sym.getFlags() for it.
  1280. B.Sym = BasicSymbolRef();
  1281. B.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
  1282. B.NType = MachO::N_EXT | MachO::N_UNDF;
  1283. B.NSect = 0;
  1284. B.NDesc = 0;
  1285. MachO::SET_LIBRARY_ORDINAL(B.NDesc, Entry.ordinal());
  1286. B.Name = Entry.symbolName().str();
  1287. SymbolList.push_back(B);
  1288. BOS << Entry.symbolName();
  1289. BOS << '\0';
  1290. BindsAdded++;
  1291. }
  1292. }
  1293. if (BErr)
  1294. error(std::move(BErr), MachO.getFileName());
  1295. // Set the symbol names and indirect names for the added symbols.
  1296. if (BindsAdded) {
  1297. BOS.flush();
  1298. const char *Q = BindsNameBuffer.c_str();
  1299. for (unsigned K = 0; K < BindsAdded; K++) {
  1300. SymbolList[I].Name = Q;
  1301. Q += strlen(Q) + 1;
  1302. if (SymbolList[I].TypeChar == 'I') {
  1303. SymbolList[I].IndirectName = Q;
  1304. Q += strlen(Q) + 1;
  1305. }
  1306. I++;
  1307. }
  1308. }
  1309. // Add the undefined symbols from the lazy bind entries.
  1310. unsigned LazysAdded = 0;
  1311. Error LErr = Error::success();
  1312. LastSymbolName = StringRef();
  1313. for (const llvm::object::MachOBindEntry &Entry :
  1314. MachO.lazyBindTable(LErr)) {
  1315. bool found = false;
  1316. if (LastSymbolName == Entry.symbolName())
  1317. found = true;
  1318. else {
  1319. // Here we must check to see it this symbol is already in the
  1320. // SymbolList as it might have already have been added above via a
  1321. // non-lazy (bind) entry.
  1322. for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
  1323. if (SymbolList[J].Name == Entry.symbolName())
  1324. found = true;
  1325. }
  1326. }
  1327. if (!found) {
  1328. LastSymbolName = Entry.symbolName();
  1329. NMSymbol L = {};
  1330. L.Name = Entry.symbolName().str();
  1331. L.Address = 0;
  1332. L.Size = 0;
  1333. L.TypeChar = 'U';
  1334. // There is no symbol in the nlist symbol table for this so we set
  1335. // Sym effectivly to null and the rest of code in here must test for
  1336. // it and not do things like Sym.getFlags() for it.
  1337. L.Sym = BasicSymbolRef();
  1338. L.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
  1339. L.NType = MachO::N_EXT | MachO::N_UNDF;
  1340. L.NSect = 0;
  1341. // The REFERENCE_FLAG_UNDEFINED_LAZY is no longer used but here it
  1342. // makes sence since we are creating this from a lazy bind entry.
  1343. L.NDesc = MachO::REFERENCE_FLAG_UNDEFINED_LAZY;
  1344. MachO::SET_LIBRARY_ORDINAL(L.NDesc, Entry.ordinal());
  1345. SymbolList.push_back(L);
  1346. LOS << Entry.symbolName();
  1347. LOS << '\0';
  1348. LazysAdded++;
  1349. }
  1350. }
  1351. if (LErr)
  1352. error(std::move(LErr), MachO.getFileName());
  1353. // Set the symbol names and indirect names for the added symbols.
  1354. if (LazysAdded) {
  1355. LOS.flush();
  1356. const char *Q = LazysNameBuffer.c_str();
  1357. for (unsigned K = 0; K < LazysAdded; K++) {
  1358. SymbolList[I].Name = Q;
  1359. Q += strlen(Q) + 1;
  1360. if (SymbolList[I].TypeChar == 'I') {
  1361. SymbolList[I].IndirectName = Q;
  1362. Q += strlen(Q) + 1;
  1363. }
  1364. I++;
  1365. }
  1366. }
  1367. // Add the undefineds symbol from the weak bind entries which are not
  1368. // strong symbols.
  1369. unsigned WeaksAdded = 0;
  1370. Error WErr = Error::success();
  1371. LastSymbolName = StringRef();
  1372. for (const llvm::object::MachOBindEntry &Entry :
  1373. MachO.weakBindTable(WErr)) {
  1374. bool found = false;
  1375. unsigned J = 0;
  1376. if (LastSymbolName == Entry.symbolName() ||
  1377. Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
  1378. found = true;
  1379. } else {
  1380. for (J = 0; J < SymbolList.size() && !found; ++J) {
  1381. if (SymbolList[J].Name == Entry.symbolName()) {
  1382. found = true;
  1383. break;
  1384. }
  1385. }
  1386. }
  1387. if (!found) {
  1388. LastSymbolName = Entry.symbolName();
  1389. NMSymbol W = {};
  1390. W.Name = Entry.symbolName().str();
  1391. W.Address = 0;
  1392. W.Size = 0;
  1393. W.TypeChar = 'U';
  1394. // There is no symbol in the nlist symbol table for this so we set
  1395. // Sym effectivly to null and the rest of code in here must test for
  1396. // it and not do things like Sym.getFlags() for it.
  1397. W.Sym = BasicSymbolRef();
  1398. W.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
  1399. W.NType = MachO::N_EXT | MachO::N_UNDF;
  1400. W.NSect = 0;
  1401. // Odd that we are using N_WEAK_DEF on an undefined symbol but that is
  1402. // what is created in this case by the linker when there are real
  1403. // symbols in the nlist structs.
  1404. W.NDesc = MachO::N_WEAK_DEF;
  1405. SymbolList.push_back(W);
  1406. WOS << Entry.symbolName();
  1407. WOS << '\0';
  1408. WeaksAdded++;
  1409. } else {
  1410. // This is the case the symbol was previously been found and it could
  1411. // have been added from a bind or lazy bind symbol. If so and not
  1412. // a definition also mark it as weak.
  1413. if (SymbolList[J].TypeChar == 'U')
  1414. // See comment above about N_WEAK_DEF.
  1415. SymbolList[J].NDesc |= MachO::N_WEAK_DEF;
  1416. }
  1417. }
  1418. if (WErr)
  1419. error(std::move(WErr), MachO.getFileName());
  1420. // Set the symbol names and indirect names for the added symbols.
  1421. if (WeaksAdded) {
  1422. WOS.flush();
  1423. const char *Q = WeaksNameBuffer.c_str();
  1424. for (unsigned K = 0; K < WeaksAdded; K++) {
  1425. SymbolList[I].Name = Q;
  1426. Q += strlen(Q) + 1;
  1427. if (SymbolList[I].TypeChar == 'I') {
  1428. SymbolList[I].IndirectName = Q;
  1429. Q += strlen(Q) + 1;
  1430. }
  1431. I++;
  1432. }
  1433. }
  1434. // Trying adding symbol from the function starts table and LC_MAIN entry
  1435. // point.
  1436. SmallVector<uint64_t, 8> FoundFns;
  1437. uint64_t lc_main_offset = UINT64_MAX;
  1438. for (const auto &Command : MachO.load_commands()) {
  1439. if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
  1440. // We found a function starts segment, parse the addresses for
  1441. // consumption.
  1442. MachO::linkedit_data_command LLC =
  1443. MachO.getLinkeditDataLoadCommand(Command);
  1444. MachO.ReadULEB128s(LLC.dataoff, FoundFns);
  1445. } else if (Command.C.cmd == MachO::LC_MAIN) {
  1446. MachO::entry_point_command LCmain = MachO.getEntryPointCommand(Command);
  1447. lc_main_offset = LCmain.entryoff;
  1448. }
  1449. }
  1450. // See if these addresses are already in the symbol table.
  1451. unsigned FunctionStartsAdded = 0;
  1452. for (uint64_t f = 0; f < FoundFns.size(); f++) {
  1453. bool found = false;
  1454. for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
  1455. if (SymbolList[J].Address == FoundFns[f] + BaseSegmentAddress)
  1456. found = true;
  1457. }
  1458. // See this address is not already in the symbol table fake up an
  1459. // nlist for it.
  1460. if (!found) {
  1461. NMSymbol F = {};
  1462. F.Name = "<redacted function X>";
  1463. F.Address = FoundFns[f] + BaseSegmentAddress;
  1464. F.Size = 0;
  1465. // There is no symbol in the nlist symbol table for this so we set
  1466. // Sym effectivly to null and the rest of code in here must test for
  1467. // it and not do things like Sym.getFlags() for it.
  1468. F.Sym = BasicSymbolRef();
  1469. F.SymFlags = 0;
  1470. F.NType = MachO::N_SECT;
  1471. F.NSect = 0;
  1472. StringRef SegmentName = StringRef();
  1473. StringRef SectionName = StringRef();
  1474. for (const SectionRef &Section : MachO.sections()) {
  1475. if (Expected<StringRef> NameOrErr = Section.getName())
  1476. SectionName = *NameOrErr;
  1477. else
  1478. consumeError(NameOrErr.takeError());
  1479. SegmentName =
  1480. MachO.getSectionFinalSegmentName(Section.getRawDataRefImpl());
  1481. F.NSect++;
  1482. if (F.Address >= Section.getAddress() &&
  1483. F.Address < Section.getAddress() + Section.getSize()) {
  1484. F.Section = Section;
  1485. break;
  1486. }
  1487. }
  1488. if (SegmentName == "__TEXT" && SectionName == "__text")
  1489. F.TypeChar = 't';
  1490. else if (SegmentName == "__DATA" && SectionName == "__data")
  1491. F.TypeChar = 'd';
  1492. else if (SegmentName == "__DATA" && SectionName == "__bss")
  1493. F.TypeChar = 'b';
  1494. else
  1495. F.TypeChar = 's';
  1496. F.NDesc = 0;
  1497. SymbolList.push_back(F);
  1498. if (FoundFns[f] == lc_main_offset)
  1499. FOS << "<redacted LC_MAIN>";
  1500. else
  1501. FOS << "<redacted function " << f << ">";
  1502. FOS << '\0';
  1503. FunctionStartsAdded++;
  1504. }
  1505. }
  1506. if (FunctionStartsAdded) {
  1507. FOS.flush();
  1508. const char *Q = FunctionStartsNameBuffer.c_str();
  1509. for (unsigned K = 0; K < FunctionStartsAdded; K++) {
  1510. SymbolList[I].Name = Q;
  1511. Q += strlen(Q) + 1;
  1512. if (SymbolList[I].TypeChar == 'I') {
  1513. SymbolList[I].IndirectName = Q;
  1514. Q += strlen(Q) + 1;
  1515. }
  1516. I++;
  1517. }
  1518. }
  1519. }
  1520. }
  1521. static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
  1522. StringRef ArchiveName = {},
  1523. StringRef ArchitectureName = {}) {
  1524. auto Symbols = Obj.symbols();
  1525. std::vector<VersionEntry> SymbolVersions;
  1526. if (DynamicSyms) {
  1527. const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
  1528. if (!E) {
  1529. error("File format has no dynamic symbol table", Obj.getFileName());
  1530. return;
  1531. }
  1532. Symbols = E->getDynamicSymbolIterators();
  1533. if (Expected<std::vector<VersionEntry>> VersionsOrErr =
  1534. E->readDynsymVersions())
  1535. SymbolVersions = std::move(*VersionsOrErr);
  1536. else
  1537. WithColor::warning(errs(), ToolName)
  1538. << "unable to read symbol versions: "
  1539. << toString(VersionsOrErr.takeError()) << "\n";
  1540. }
  1541. // If a "-s segname sectname" option was specified and this is a Mach-O
  1542. // file get the section number for that section in this object file.
  1543. unsigned int Nsect = 0;
  1544. MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
  1545. if (!SegSect.empty() && MachO) {
  1546. Nsect = getNsectForSegSect(MachO);
  1547. // If this section is not in the object file no symbols are printed.
  1548. if (Nsect == 0)
  1549. return;
  1550. }
  1551. if (!(MachO && DyldInfoOnly)) {
  1552. size_t I = -1;
  1553. for (BasicSymbolRef Sym : Symbols) {
  1554. ++I;
  1555. Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
  1556. if (!SymFlagsOrErr) {
  1557. error(SymFlagsOrErr.takeError(), Obj.getFileName());
  1558. return;
  1559. }
  1560. // Don't drop format specifc symbols for ARM and AArch64 ELF targets, they
  1561. // are used to repesent mapping symbols and needed to honor the
  1562. // --special-syms option.
  1563. auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
  1564. if ((!ELFObj || (ELFObj->getEMachine() != ELF::EM_ARM &&
  1565. ELFObj->getEMachine() != ELF::EM_AARCH64)) &&
  1566. !DebugSyms && (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific))
  1567. continue;
  1568. if (WithoutAliases && (*SymFlagsOrErr & SymbolRef::SF_Indirect))
  1569. continue;
  1570. // If a "-s segname sectname" option was specified and this is a Mach-O
  1571. // file and this section appears in this file, Nsect will be non-zero then
  1572. // see if this symbol is a symbol from that section and if not skip it.
  1573. if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
  1574. continue;
  1575. NMSymbol S = {};
  1576. S.Size = 0;
  1577. S.Address = 0;
  1578. if (isa<ELFObjectFileBase>(&Obj))
  1579. S.Size = ELFSymbolRef(Sym).getSize();
  1580. if (const XCOFFObjectFile *XCOFFObj =
  1581. dyn_cast<const XCOFFObjectFile>(&Obj))
  1582. S.Size = XCOFFObj->getSymbolSize(Sym.getRawDataRefImpl());
  1583. if (PrintAddress && isa<ObjectFile>(Obj)) {
  1584. SymbolRef SymRef(Sym);
  1585. Expected<uint64_t> AddressOrErr = SymRef.getAddress();
  1586. if (!AddressOrErr) {
  1587. consumeError(AddressOrErr.takeError());
  1588. break;
  1589. }
  1590. S.Address = *AddressOrErr;
  1591. }
  1592. S.TypeName = getNMTypeName(Obj, Sym);
  1593. S.TypeChar = getNMSectionTagAndName(Obj, Sym, S.SectionName);
  1594. raw_string_ostream OS(S.Name);
  1595. if (Error E = Sym.printName(OS)) {
  1596. if (MachO) {
  1597. OS << "bad string index";
  1598. consumeError(std::move(E));
  1599. } else
  1600. error(std::move(E), Obj.getFileName());
  1601. }
  1602. if (!SymbolVersions.empty() && !SymbolVersions[I].Name.empty())
  1603. S.Name +=
  1604. (SymbolVersions[I].IsVerDef ? "@@" : "@") + SymbolVersions[I].Name;
  1605. S.Sym = Sym;
  1606. SymbolList.push_back(S);
  1607. }
  1608. }
  1609. // If this is a Mach-O file where the nlist symbol table is out of sync
  1610. // with the dyld export trie then look through exports and fake up symbols
  1611. // for the ones that are missing (also done with the -add-dyldinfo flag).
  1612. // This is needed if strip(1) -T is run on a binary containing swift
  1613. // language symbols for example. The option -only-dyldinfo will fake up
  1614. // all symbols from the dyld export trie as well as the bind info.
  1615. if (MachO && !NoDyldInfo)
  1616. dumpSymbolsFromDLInfoMachO(*MachO);
  1617. CurrentFilename = Obj.getFileName();
  1618. if (Symbols.empty() && SymbolList.empty() && !Quiet) {
  1619. writeFileName(errs(), ArchiveName, ArchitectureName);
  1620. errs() << "no symbols\n";
  1621. }
  1622. sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
  1623. }
  1624. // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
  1625. // and if it is and there is a list of architecture flags is specified then
  1626. // check to make sure this Mach-O file is one of those architectures or all
  1627. // architectures was specificed. If not then an error is generated and this
  1628. // routine returns false. Else it returns true.
  1629. static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
  1630. auto *MachO = dyn_cast<MachOObjectFile>(O);
  1631. if (!MachO || ArchAll || ArchFlags.empty())
  1632. return true;
  1633. MachO::mach_header H;
  1634. MachO::mach_header_64 H_64;
  1635. Triple T;
  1636. const char *McpuDefault, *ArchFlag;
  1637. if (MachO->is64Bit()) {
  1638. H_64 = MachO->MachOObjectFile::getHeader64();
  1639. T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
  1640. &McpuDefault, &ArchFlag);
  1641. } else {
  1642. H = MachO->MachOObjectFile::getHeader();
  1643. T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
  1644. &McpuDefault, &ArchFlag);
  1645. }
  1646. const std::string ArchFlagName(ArchFlag);
  1647. if (!llvm::is_contained(ArchFlags, ArchFlagName)) {
  1648. error("No architecture specified", Filename);
  1649. return false;
  1650. }
  1651. return true;
  1652. }
  1653. static void dumpSymbolNamesFromFile(std::string &Filename) {
  1654. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
  1655. MemoryBuffer::getFileOrSTDIN(Filename);
  1656. if (error(BufferOrErr.getError(), Filename))
  1657. return;
  1658. LLVMContext Context;
  1659. LLVMContext *ContextPtr = NoLLVMBitcode ? nullptr : &Context;
  1660. Expected<std::unique_ptr<Binary>> BinaryOrErr =
  1661. createBinary(BufferOrErr.get()->getMemBufferRef(), ContextPtr);
  1662. if (!BinaryOrErr) {
  1663. error(BinaryOrErr.takeError(), Filename);
  1664. return;
  1665. }
  1666. Binary &Bin = *BinaryOrErr.get();
  1667. if (Archive *A = dyn_cast<Archive>(&Bin)) {
  1668. if (ArchiveMap) {
  1669. Archive::symbol_iterator I = A->symbol_begin();
  1670. Archive::symbol_iterator E = A->symbol_end();
  1671. if (I != E) {
  1672. outs() << "Archive map\n";
  1673. for (; I != E; ++I) {
  1674. Expected<Archive::Child> C = I->getMember();
  1675. if (!C) {
  1676. error(C.takeError(), Filename);
  1677. break;
  1678. }
  1679. Expected<StringRef> FileNameOrErr = C->getName();
  1680. if (!FileNameOrErr) {
  1681. error(FileNameOrErr.takeError(), Filename);
  1682. break;
  1683. }
  1684. StringRef SymName = I->getName();
  1685. outs() << SymName << " in " << FileNameOrErr.get() << "\n";
  1686. }
  1687. outs() << "\n";
  1688. }
  1689. }
  1690. {
  1691. Error Err = Error::success();
  1692. for (auto &C : A->children(Err)) {
  1693. Expected<std::unique_ptr<Binary>> ChildOrErr =
  1694. C.getAsBinary(ContextPtr);
  1695. if (!ChildOrErr) {
  1696. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1697. error(std::move(E), Filename, C);
  1698. continue;
  1699. }
  1700. if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
  1701. if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
  1702. WithColor::warning(errs(), ToolName)
  1703. << "sizes with -print-size for Mach-O files are always zero.\n";
  1704. MachOPrintSizeWarning = true;
  1705. }
  1706. if (!checkMachOAndArchFlags(O, Filename))
  1707. return;
  1708. if (!PrintFileName) {
  1709. outs() << "\n";
  1710. if (isa<MachOObjectFile>(O)) {
  1711. outs() << Filename << "(" << O->getFileName() << ")";
  1712. } else
  1713. outs() << O->getFileName();
  1714. outs() << ":\n";
  1715. }
  1716. dumpSymbolNamesFromObject(*O, false, Filename);
  1717. }
  1718. }
  1719. if (Err)
  1720. error(std::move(Err), A->getFileName());
  1721. }
  1722. return;
  1723. }
  1724. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
  1725. // If we have a list of architecture flags specified dump only those.
  1726. if (!ArchAll && !ArchFlags.empty()) {
  1727. // Look for a slice in the universal binary that matches each ArchFlag.
  1728. bool ArchFound;
  1729. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  1730. ArchFound = false;
  1731. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1732. E = UB->end_objects();
  1733. I != E; ++I) {
  1734. if (ArchFlags[i] == I->getArchFlagName()) {
  1735. ArchFound = true;
  1736. Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
  1737. I->getAsObjectFile();
  1738. std::string ArchiveName;
  1739. std::string ArchitectureName;
  1740. ArchiveName.clear();
  1741. ArchitectureName.clear();
  1742. if (ObjOrErr) {
  1743. ObjectFile &Obj = *ObjOrErr.get();
  1744. if (ArchFlags.size() > 1) {
  1745. if (PrintFileName)
  1746. ArchitectureName = I->getArchFlagName();
  1747. else
  1748. outs() << "\n" << Obj.getFileName() << " (for architecture "
  1749. << I->getArchFlagName() << ")"
  1750. << ":\n";
  1751. }
  1752. dumpSymbolNamesFromObject(Obj, false, ArchiveName,
  1753. ArchitectureName);
  1754. } else if (auto E = isNotObjectErrorInvalidFileType(
  1755. ObjOrErr.takeError())) {
  1756. error(std::move(E), Filename, ArchFlags.size() > 1 ?
  1757. StringRef(I->getArchFlagName()) : StringRef());
  1758. continue;
  1759. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1760. I->getAsArchive()) {
  1761. std::unique_ptr<Archive> &A = *AOrErr;
  1762. Error Err = Error::success();
  1763. for (auto &C : A->children(Err)) {
  1764. Expected<std::unique_ptr<Binary>> ChildOrErr =
  1765. C.getAsBinary(ContextPtr);
  1766. if (!ChildOrErr) {
  1767. if (auto E = isNotObjectErrorInvalidFileType(
  1768. ChildOrErr.takeError())) {
  1769. error(std::move(E), Filename, C, ArchFlags.size() > 1 ?
  1770. StringRef(I->getArchFlagName()) : StringRef());
  1771. }
  1772. continue;
  1773. }
  1774. if (SymbolicFile *O =
  1775. dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
  1776. if (PrintFileName) {
  1777. ArchiveName = std::string(A->getFileName());
  1778. if (ArchFlags.size() > 1)
  1779. ArchitectureName = I->getArchFlagName();
  1780. } else {
  1781. outs() << "\n" << A->getFileName();
  1782. outs() << "(" << O->getFileName() << ")";
  1783. if (ArchFlags.size() > 1) {
  1784. outs() << " (for architecture " << I->getArchFlagName()
  1785. << ")";
  1786. }
  1787. outs() << ":\n";
  1788. }
  1789. dumpSymbolNamesFromObject(*O, false, ArchiveName,
  1790. ArchitectureName);
  1791. }
  1792. }
  1793. if (Err)
  1794. error(std::move(Err), A->getFileName());
  1795. } else {
  1796. consumeError(AOrErr.takeError());
  1797. error(Filename + " for architecture " +
  1798. StringRef(I->getArchFlagName()) +
  1799. " is not a Mach-O file or an archive file",
  1800. "Mach-O universal file");
  1801. }
  1802. }
  1803. }
  1804. if (!ArchFound) {
  1805. error(ArchFlags[i],
  1806. "file: " + Filename + " does not contain architecture");
  1807. return;
  1808. }
  1809. }
  1810. return;
  1811. }
  1812. // No architecture flags were specified so if this contains a slice that
  1813. // matches the host architecture dump only that.
  1814. if (!ArchAll) {
  1815. Triple HostTriple = MachOObjectFile::getHostArch();
  1816. StringRef HostArchName = HostTriple.getArchName();
  1817. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1818. E = UB->end_objects();
  1819. I != E; ++I) {
  1820. if (HostArchName == I->getArchFlagName()) {
  1821. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  1822. std::string ArchiveName;
  1823. if (ObjOrErr) {
  1824. ObjectFile &Obj = *ObjOrErr.get();
  1825. dumpSymbolNamesFromObject(Obj, false);
  1826. } else if (auto E = isNotObjectErrorInvalidFileType(
  1827. ObjOrErr.takeError())) {
  1828. error(std::move(E), Filename);
  1829. return;
  1830. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1831. I->getAsArchive()) {
  1832. std::unique_ptr<Archive> &A = *AOrErr;
  1833. Error Err = Error::success();
  1834. for (auto &C : A->children(Err)) {
  1835. Expected<std::unique_ptr<Binary>> ChildOrErr =
  1836. C.getAsBinary(ContextPtr);
  1837. if (!ChildOrErr) {
  1838. if (auto E = isNotObjectErrorInvalidFileType(
  1839. ChildOrErr.takeError()))
  1840. error(std::move(E), Filename, C);
  1841. continue;
  1842. }
  1843. if (SymbolicFile *O =
  1844. dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
  1845. if (PrintFileName)
  1846. ArchiveName = std::string(A->getFileName());
  1847. else
  1848. outs() << "\n" << A->getFileName() << "(" << O->getFileName()
  1849. << ")"
  1850. << ":\n";
  1851. dumpSymbolNamesFromObject(*O, false, ArchiveName);
  1852. }
  1853. }
  1854. if (Err)
  1855. error(std::move(Err), A->getFileName());
  1856. } else {
  1857. consumeError(AOrErr.takeError());
  1858. error(Filename + " for architecture " +
  1859. StringRef(I->getArchFlagName()) +
  1860. " is not a Mach-O file or an archive file",
  1861. "Mach-O universal file");
  1862. }
  1863. return;
  1864. }
  1865. }
  1866. }
  1867. // Either all architectures have been specified or none have been specified
  1868. // and this does not contain the host architecture so dump all the slices.
  1869. bool moreThanOneArch = UB->getNumberOfObjects() > 1;
  1870. for (const MachOUniversalBinary::ObjectForArch &O : UB->objects()) {
  1871. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = O.getAsObjectFile();
  1872. std::string ArchiveName;
  1873. std::string ArchitectureName;
  1874. ArchiveName.clear();
  1875. ArchitectureName.clear();
  1876. if (ObjOrErr) {
  1877. ObjectFile &Obj = *ObjOrErr.get();
  1878. if (PrintFileName) {
  1879. if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
  1880. ArchitectureName = O.getArchFlagName();
  1881. } else {
  1882. if (moreThanOneArch)
  1883. outs() << "\n";
  1884. outs() << Obj.getFileName();
  1885. if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
  1886. outs() << " (for architecture " << O.getArchFlagName() << ")";
  1887. outs() << ":\n";
  1888. }
  1889. dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
  1890. } else if (auto E = isNotObjectErrorInvalidFileType(
  1891. ObjOrErr.takeError())) {
  1892. error(std::move(E), Filename, moreThanOneArch ?
  1893. StringRef(O.getArchFlagName()) : StringRef());
  1894. continue;
  1895. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1896. O.getAsArchive()) {
  1897. std::unique_ptr<Archive> &A = *AOrErr;
  1898. Error Err = Error::success();
  1899. for (auto &C : A->children(Err)) {
  1900. Expected<std::unique_ptr<Binary>> ChildOrErr =
  1901. C.getAsBinary(ContextPtr);
  1902. if (!ChildOrErr) {
  1903. if (auto E = isNotObjectErrorInvalidFileType(
  1904. ChildOrErr.takeError()))
  1905. error(std::move(E), Filename, C, moreThanOneArch ?
  1906. StringRef(ArchitectureName) : StringRef());
  1907. continue;
  1908. }
  1909. if (SymbolicFile *F = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
  1910. if (PrintFileName) {
  1911. ArchiveName = std::string(A->getFileName());
  1912. if (isa<MachOObjectFile>(F) && moreThanOneArch)
  1913. ArchitectureName = O.getArchFlagName();
  1914. } else {
  1915. outs() << "\n" << A->getFileName();
  1916. if (isa<MachOObjectFile>(F)) {
  1917. outs() << "(" << F->getFileName() << ")";
  1918. if (moreThanOneArch)
  1919. outs() << " (for architecture " << O.getArchFlagName()
  1920. << ")";
  1921. } else
  1922. outs() << ":" << F->getFileName();
  1923. outs() << ":\n";
  1924. }
  1925. dumpSymbolNamesFromObject(*F, false, ArchiveName, ArchitectureName);
  1926. }
  1927. }
  1928. if (Err)
  1929. error(std::move(Err), A->getFileName());
  1930. } else {
  1931. consumeError(AOrErr.takeError());
  1932. error(Filename + " for architecture " +
  1933. StringRef(O.getArchFlagName()) +
  1934. " is not a Mach-O file or an archive file",
  1935. "Mach-O universal file");
  1936. }
  1937. }
  1938. return;
  1939. }
  1940. if (TapiUniversal *TU = dyn_cast<TapiUniversal>(&Bin)) {
  1941. for (const TapiUniversal::ObjectForArch &I : TU->objects()) {
  1942. StringRef ArchName = I.getArchFlagName();
  1943. const bool ShowArch =
  1944. ArchFlags.empty() || llvm::is_contained(ArchFlags, ArchName);
  1945. if (!ShowArch)
  1946. continue;
  1947. if (!AddInlinedInfo && !I.isTopLevelLib())
  1948. continue;
  1949. if (auto ObjOrErr = I.getAsObjectFile()) {
  1950. outs() << "\n"
  1951. << I.getInstallName() << " (for architecture " << ArchName << ")"
  1952. << ":\n";
  1953. dumpSymbolNamesFromObject(*ObjOrErr.get(), false, {}, ArchName);
  1954. } else if (Error E =
  1955. isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
  1956. error(std::move(E), Filename, ArchName);
  1957. }
  1958. }
  1959. return;
  1960. }
  1961. if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
  1962. if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
  1963. WithColor::warning(errs(), ToolName)
  1964. << "sizes with --print-size for Mach-O files are always zero.\n";
  1965. MachOPrintSizeWarning = true;
  1966. }
  1967. if (!checkMachOAndArchFlags(O, Filename))
  1968. return;
  1969. dumpSymbolNamesFromObject(*O, true);
  1970. }
  1971. }
  1972. int main(int argc, char **argv) {
  1973. InitLLVM X(argc, argv);
  1974. BumpPtrAllocator A;
  1975. StringSaver Saver(A);
  1976. NmOptTable Tbl;
  1977. ToolName = argv[0];
  1978. opt::InputArgList Args =
  1979. Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
  1980. error(Msg);
  1981. exit(1);
  1982. });
  1983. if (Args.hasArg(OPT_help)) {
  1984. Tbl.printHelp(
  1985. outs(),
  1986. (Twine(ToolName) + " [options] <input object files>").str().c_str(),
  1987. "LLVM symbol table dumper");
  1988. // TODO Replace this with OptTable API once it adds extrahelp support.
  1989. outs() << "\nPass @FILE as argument to read options from FILE.\n";
  1990. return 0;
  1991. }
  1992. if (Args.hasArg(OPT_version)) {
  1993. // This needs to contain the word "GNU", libtool looks for that string.
  1994. outs() << "llvm-nm, compatible with GNU nm" << '\n';
  1995. cl::PrintVersionMessage();
  1996. return 0;
  1997. }
  1998. DebugSyms = Args.hasArg(OPT_debug_syms);
  1999. DefinedOnly = Args.hasArg(OPT_defined_only);
  2000. Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, false);
  2001. DynamicSyms = Args.hasArg(OPT_dynamic);
  2002. ExternalOnly = Args.hasArg(OPT_extern_only);
  2003. StringRef V = Args.getLastArgValue(OPT_format_EQ, "bsd");
  2004. if (V == "bsd")
  2005. OutputFormat = bsd;
  2006. else if (V == "posix")
  2007. OutputFormat = posix;
  2008. else if (V == "sysv")
  2009. OutputFormat = sysv;
  2010. else if (V == "darwin")
  2011. OutputFormat = darwin;
  2012. else if (V == "just-symbols")
  2013. OutputFormat = just_symbols;
  2014. else
  2015. error("--format value should be one of: bsd, posix, sysv, darwin, "
  2016. "just-symbols");
  2017. NoLLVMBitcode = Args.hasArg(OPT_no_llvm_bc);
  2018. NoSort = Args.hasArg(OPT_no_sort);
  2019. NoWeakSymbols = Args.hasArg(OPT_no_weak);
  2020. NumericSort = Args.hasArg(OPT_numeric_sort);
  2021. ArchiveMap = Args.hasArg(OPT_print_armap);
  2022. PrintFileName = Args.hasArg(OPT_print_file_name);
  2023. PrintSize = Args.hasArg(OPT_print_size);
  2024. ReverseSort = Args.hasArg(OPT_reverse_sort);
  2025. Quiet = Args.hasArg(OPT_quiet);
  2026. V = Args.getLastArgValue(OPT_radix_EQ, "x");
  2027. if (V == "o")
  2028. AddressRadix = Radix::o;
  2029. else if (V == "d")
  2030. AddressRadix = Radix::d;
  2031. else if (V == "x")
  2032. AddressRadix = Radix::x;
  2033. else
  2034. error("--radix value should be one of: 'o' (octal), 'd' (decimal), 'x' "
  2035. "(hexadecimal)");
  2036. SizeSort = Args.hasArg(OPT_size_sort);
  2037. SpecialSyms = Args.hasArg(OPT_special_syms);
  2038. UndefinedOnly = Args.hasArg(OPT_undefined_only);
  2039. WithoutAliases = Args.hasArg(OPT_without_aliases);
  2040. // Mach-O specific options.
  2041. FormatMachOasHex = Args.hasArg(OPT_x);
  2042. AddDyldInfo = Args.hasArg(OPT_add_dyldinfo);
  2043. AddInlinedInfo = Args.hasArg(OPT_add_inlinedinfo);
  2044. DyldInfoOnly = Args.hasArg(OPT_dyldinfo_only);
  2045. NoDyldInfo = Args.hasArg(OPT_no_dyldinfo);
  2046. // llvm-nm only reads binary files.
  2047. if (error(sys::ChangeStdinToBinary()))
  2048. return 1;
  2049. // These calls are needed so that we can read bitcode correctly.
  2050. llvm::InitializeAllTargetInfos();
  2051. llvm::InitializeAllTargetMCs();
  2052. llvm::InitializeAllAsmParsers();
  2053. // The relative order of these is important. If you pass --size-sort it should
  2054. // only print out the size. However, if you pass -S --size-sort, it should
  2055. // print out both the size and address.
  2056. if (SizeSort && !PrintSize)
  2057. PrintAddress = false;
  2058. if (OutputFormat == sysv || SizeSort)
  2059. PrintSize = true;
  2060. for (const auto *A : Args.filtered(OPT_arch_EQ)) {
  2061. SmallVector<StringRef, 2> Values;
  2062. llvm::SplitString(A->getValue(), Values, ",");
  2063. for (StringRef V : Values) {
  2064. if (V == "all")
  2065. ArchAll = true;
  2066. else if (MachOObjectFile::isValidArch(V))
  2067. ArchFlags.push_back(V);
  2068. else
  2069. error("Unknown architecture named '" + V + "'",
  2070. "for the --arch option");
  2071. }
  2072. }
  2073. // Mach-O takes -s to accept two arguments. We emulate this by iterating over
  2074. // both OPT_s and OPT_INPUT.
  2075. std::vector<std::string> InputFilenames;
  2076. int SegSectArgs = 0;
  2077. for (opt::Arg *A : Args.filtered(OPT_s, OPT_INPUT)) {
  2078. if (SegSectArgs > 0) {
  2079. --SegSectArgs;
  2080. SegSect.push_back(A->getValue());
  2081. } else if (A->getOption().matches(OPT_s)) {
  2082. SegSectArgs = 2;
  2083. } else {
  2084. InputFilenames.push_back(A->getValue());
  2085. }
  2086. }
  2087. if (!SegSect.empty() && SegSect.size() != 2)
  2088. error("bad number of arguments (must be two arguments)",
  2089. "for the -s option");
  2090. if (InputFilenames.empty())
  2091. InputFilenames.push_back("a.out");
  2092. if (InputFilenames.size() > 1)
  2093. MultipleFiles = true;
  2094. if (NoDyldInfo && (AddDyldInfo || DyldInfoOnly))
  2095. error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
  2096. llvm::for_each(InputFilenames, dumpSymbolNamesFromFile);
  2097. if (HadError)
  2098. return 1;
  2099. }