llvm-nm.cpp 85 KB

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