llvm-nm.cpp 81 KB

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