llvm-objdump.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  1. //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This program is a utility that works like binutils "objdump", that is, it
  11. // dumps out a plethora of information about an object file depending on the
  12. // flags.
  13. //
  14. // The flags and output of this program should be near identical to those of
  15. // binutils objdump.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm-objdump.h"
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/StringExtras.h"
  22. #include "llvm/ADT/Triple.h"
  23. #include "llvm/CodeGen/FaultMaps.h"
  24. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  25. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  26. #include "llvm/MC/MCAsmInfo.h"
  27. #include "llvm/MC/MCContext.h"
  28. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  29. #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
  30. #include "llvm/MC/MCInst.h"
  31. #include "llvm/MC/MCInstPrinter.h"
  32. #include "llvm/MC/MCInstrAnalysis.h"
  33. #include "llvm/MC/MCInstrInfo.h"
  34. #include "llvm/MC/MCObjectFileInfo.h"
  35. #include "llvm/MC/MCRegisterInfo.h"
  36. #include "llvm/MC/MCSubtargetInfo.h"
  37. #include "llvm/Object/Archive.h"
  38. #include "llvm/Object/COFF.h"
  39. #include "llvm/Object/COFFImportFile.h"
  40. #include "llvm/Object/ELFObjectFile.h"
  41. #include "llvm/Object/MachO.h"
  42. #include "llvm/Object/ObjectFile.h"
  43. #include "llvm/Object/Wasm.h"
  44. #include "llvm/Support/Casting.h"
  45. #include "llvm/Support/CommandLine.h"
  46. #include "llvm/Support/Debug.h"
  47. #include "llvm/Support/Errc.h"
  48. #include "llvm/Support/FileSystem.h"
  49. #include "llvm/Support/Format.h"
  50. #include "llvm/Support/GraphWriter.h"
  51. #include "llvm/Support/Host.h"
  52. #include "llvm/Support/ManagedStatic.h"
  53. #include "llvm/Support/MemoryBuffer.h"
  54. #include "llvm/Support/PrettyStackTrace.h"
  55. #include "llvm/Support/Signals.h"
  56. #include "llvm/Support/SourceMgr.h"
  57. #include "llvm/Support/TargetRegistry.h"
  58. #include "llvm/Support/TargetSelect.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include <algorithm>
  61. #include <cctype>
  62. #include <cstring>
  63. #include <system_error>
  64. #include <unordered_map>
  65. #include <utility>
  66. using namespace llvm;
  67. using namespace object;
  68. static cl::list<std::string>
  69. InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
  70. cl::opt<bool>
  71. llvm::Disassemble("disassemble",
  72. cl::desc("Display assembler mnemonics for the machine instructions"));
  73. static cl::alias
  74. Disassembled("d", cl::desc("Alias for --disassemble"),
  75. cl::aliasopt(Disassemble));
  76. cl::opt<bool>
  77. llvm::DisassembleAll("disassemble-all",
  78. cl::desc("Display assembler mnemonics for the machine instructions"));
  79. static cl::alias
  80. DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
  81. cl::aliasopt(DisassembleAll));
  82. cl::opt<bool>
  83. llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
  84. cl::opt<bool>
  85. llvm::SectionContents("s", cl::desc("Display the content of each section"));
  86. cl::opt<bool>
  87. llvm::SymbolTable("t", cl::desc("Display the symbol table"));
  88. cl::opt<bool>
  89. llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
  90. cl::opt<bool>
  91. llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
  92. cl::opt<bool>
  93. llvm::Bind("bind", cl::desc("Display mach-o binding info"));
  94. cl::opt<bool>
  95. llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
  96. cl::opt<bool>
  97. llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
  98. cl::opt<bool>
  99. llvm::RawClangAST("raw-clang-ast",
  100. cl::desc("Dump the raw binary contents of the clang AST section"));
  101. static cl::opt<bool>
  102. MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
  103. static cl::alias
  104. MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
  105. cl::opt<std::string>
  106. llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
  107. "see -version for available targets"));
  108. cl::opt<std::string>
  109. llvm::MCPU("mcpu",
  110. cl::desc("Target a specific cpu type (-mcpu=help for details)"),
  111. cl::value_desc("cpu-name"),
  112. cl::init(""));
  113. cl::opt<std::string>
  114. llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
  115. "see -version for available targets"));
  116. cl::opt<bool>
  117. llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
  118. "headers for each section."));
  119. static cl::alias
  120. SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
  121. cl::aliasopt(SectionHeaders));
  122. static cl::alias
  123. SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
  124. cl::aliasopt(SectionHeaders));
  125. cl::list<std::string>
  126. llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
  127. "With -macho dump segment,section"));
  128. cl::alias
  129. static FilterSectionsj("j", cl::desc("Alias for --section"),
  130. cl::aliasopt(llvm::FilterSections));
  131. cl::list<std::string>
  132. llvm::MAttrs("mattr",
  133. cl::CommaSeparated,
  134. cl::desc("Target specific attributes"),
  135. cl::value_desc("a1,+a2,-a3,..."));
  136. cl::opt<bool>
  137. llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
  138. "instructions, do not print "
  139. "the instruction bytes."));
  140. cl::opt<bool>
  141. llvm::NoLeadingAddr("no-leading-addr", cl::desc("Print no leading address"));
  142. cl::opt<bool>
  143. llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
  144. static cl::alias
  145. UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
  146. cl::aliasopt(UnwindInfo));
  147. cl::opt<bool>
  148. llvm::PrivateHeaders("private-headers",
  149. cl::desc("Display format specific file headers"));
  150. cl::opt<bool>
  151. llvm::FirstPrivateHeader("private-header",
  152. cl::desc("Display only the first format specific file "
  153. "header"));
  154. static cl::alias
  155. PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
  156. cl::aliasopt(PrivateHeaders));
  157. cl::opt<bool>
  158. llvm::PrintImmHex("print-imm-hex",
  159. cl::desc("Use hex format for immediate values"));
  160. cl::opt<bool> PrintFaultMaps("fault-map-section",
  161. cl::desc("Display contents of faultmap section"));
  162. cl::opt<DIDumpType> llvm::DwarfDumpType(
  163. "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
  164. cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame")));
  165. cl::opt<bool> PrintSource(
  166. "source",
  167. cl::desc(
  168. "Display source inlined with disassembly. Implies disassmble object"));
  169. cl::alias PrintSourceShort("S", cl::desc("Alias for -source"),
  170. cl::aliasopt(PrintSource));
  171. cl::opt<bool> PrintLines("line-numbers",
  172. cl::desc("Display source line numbers with "
  173. "disassembly. Implies disassemble object"));
  174. cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"),
  175. cl::aliasopt(PrintLines));
  176. cl::opt<unsigned long long>
  177. StartAddress("start-address", cl::desc("Disassemble beginning at address"),
  178. cl::value_desc("address"), cl::init(0));
  179. cl::opt<unsigned long long>
  180. StopAddress("stop-address", cl::desc("Stop disassembly at address"),
  181. cl::value_desc("address"), cl::init(UINT64_MAX));
  182. static StringRef ToolName;
  183. typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
  184. namespace {
  185. typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
  186. class SectionFilterIterator {
  187. public:
  188. SectionFilterIterator(FilterPredicate P,
  189. llvm::object::section_iterator const &I,
  190. llvm::object::section_iterator const &E)
  191. : Predicate(std::move(P)), Iterator(I), End(E) {
  192. ScanPredicate();
  193. }
  194. const llvm::object::SectionRef &operator*() const { return *Iterator; }
  195. SectionFilterIterator &operator++() {
  196. ++Iterator;
  197. ScanPredicate();
  198. return *this;
  199. }
  200. bool operator!=(SectionFilterIterator const &Other) const {
  201. return Iterator != Other.Iterator;
  202. }
  203. private:
  204. void ScanPredicate() {
  205. while (Iterator != End && !Predicate(*Iterator)) {
  206. ++Iterator;
  207. }
  208. }
  209. FilterPredicate Predicate;
  210. llvm::object::section_iterator Iterator;
  211. llvm::object::section_iterator End;
  212. };
  213. class SectionFilter {
  214. public:
  215. SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
  216. : Predicate(std::move(P)), Object(O) {}
  217. SectionFilterIterator begin() {
  218. return SectionFilterIterator(Predicate, Object.section_begin(),
  219. Object.section_end());
  220. }
  221. SectionFilterIterator end() {
  222. return SectionFilterIterator(Predicate, Object.section_end(),
  223. Object.section_end());
  224. }
  225. private:
  226. FilterPredicate Predicate;
  227. llvm::object::ObjectFile const &Object;
  228. };
  229. SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
  230. return SectionFilter(
  231. [](llvm::object::SectionRef const &S) {
  232. if (FilterSections.empty())
  233. return true;
  234. llvm::StringRef String;
  235. std::error_code error = S.getName(String);
  236. if (error)
  237. return false;
  238. return is_contained(FilterSections, String);
  239. },
  240. O);
  241. }
  242. }
  243. void llvm::error(std::error_code EC) {
  244. if (!EC)
  245. return;
  246. errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
  247. errs().flush();
  248. exit(1);
  249. }
  250. LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
  251. errs() << ToolName << ": " << Message << ".\n";
  252. errs().flush();
  253. exit(1);
  254. }
  255. LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
  256. Twine Message) {
  257. errs() << ToolName << ": '" << File << "': " << Message << ".\n";
  258. exit(1);
  259. }
  260. LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
  261. std::error_code EC) {
  262. assert(EC);
  263. errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
  264. exit(1);
  265. }
  266. LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
  267. llvm::Error E) {
  268. assert(E);
  269. std::string Buf;
  270. raw_string_ostream OS(Buf);
  271. logAllUnhandledErrors(std::move(E), OS, "");
  272. OS.flush();
  273. errs() << ToolName << ": '" << File << "': " << Buf;
  274. exit(1);
  275. }
  276. LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
  277. StringRef FileName,
  278. llvm::Error E,
  279. StringRef ArchitectureName) {
  280. assert(E);
  281. errs() << ToolName << ": ";
  282. if (ArchiveName != "")
  283. errs() << ArchiveName << "(" << FileName << ")";
  284. else
  285. errs() << "'" << FileName << "'";
  286. if (!ArchitectureName.empty())
  287. errs() << " (for architecture " << ArchitectureName << ")";
  288. std::string Buf;
  289. raw_string_ostream OS(Buf);
  290. logAllUnhandledErrors(std::move(E), OS, "");
  291. OS.flush();
  292. errs() << ": " << Buf;
  293. exit(1);
  294. }
  295. LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
  296. const object::Archive::Child &C,
  297. llvm::Error E,
  298. StringRef ArchitectureName) {
  299. Expected<StringRef> NameOrErr = C.getName();
  300. // TODO: if we have a error getting the name then it would be nice to print
  301. // the index of which archive member this is and or its offset in the
  302. // archive instead of "???" as the name.
  303. if (!NameOrErr) {
  304. consumeError(NameOrErr.takeError());
  305. llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
  306. } else
  307. llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
  308. ArchitectureName);
  309. }
  310. static const Target *getTarget(const ObjectFile *Obj = nullptr) {
  311. // Figure out the target triple.
  312. llvm::Triple TheTriple("unknown-unknown-unknown");
  313. if (TripleName.empty()) {
  314. if (Obj) {
  315. TheTriple = Obj->makeTriple();
  316. }
  317. } else {
  318. TheTriple.setTriple(Triple::normalize(TripleName));
  319. // Use the triple, but also try to combine with ARM build attributes.
  320. if (Obj) {
  321. auto Arch = Obj->getArch();
  322. if (Arch == Triple::arm || Arch == Triple::armeb) {
  323. Obj->setARMSubArch(TheTriple);
  324. }
  325. }
  326. }
  327. // Get the target specific parser.
  328. std::string Error;
  329. const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
  330. Error);
  331. if (!TheTarget) {
  332. if (Obj)
  333. report_error(Obj->getFileName(), "can't find target: " + Error);
  334. else
  335. error("can't find target: " + Error);
  336. }
  337. // Update the triple name and return the found target.
  338. TripleName = TheTriple.getTriple();
  339. return TheTarget;
  340. }
  341. bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
  342. return a.getOffset() < b.getOffset();
  343. }
  344. namespace {
  345. class SourcePrinter {
  346. protected:
  347. DILineInfo OldLineInfo;
  348. const ObjectFile *Obj = nullptr;
  349. std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
  350. // File name to file contents of source
  351. std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
  352. // Mark the line endings of the cached source
  353. std::unordered_map<std::string, std::vector<StringRef>> LineCache;
  354. private:
  355. bool cacheSource(const std::string& File);
  356. public:
  357. SourcePrinter() = default;
  358. SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) {
  359. symbolize::LLVMSymbolizer::Options SymbolizerOpts(
  360. DILineInfoSpecifier::FunctionNameKind::None, true, false, false,
  361. DefaultArch);
  362. Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
  363. }
  364. virtual ~SourcePrinter() = default;
  365. virtual void printSourceLine(raw_ostream &OS, uint64_t Address,
  366. StringRef Delimiter = "; ");
  367. };
  368. bool SourcePrinter::cacheSource(const std::string& File) {
  369. auto BufferOrError = MemoryBuffer::getFile(File);
  370. if (!BufferOrError)
  371. return false;
  372. // Chomp the file to get lines
  373. size_t BufferSize = (*BufferOrError)->getBufferSize();
  374. const char *BufferStart = (*BufferOrError)->getBufferStart();
  375. for (const char *Start = BufferStart, *End = BufferStart;
  376. End < BufferStart + BufferSize; End++)
  377. if (*End == '\n' || End == BufferStart + BufferSize - 1 ||
  378. (*End == '\r' && *(End + 1) == '\n')) {
  379. LineCache[File].push_back(StringRef(Start, End - Start));
  380. if (*End == '\r')
  381. End++;
  382. Start = End + 1;
  383. }
  384. SourceCache[File] = std::move(*BufferOrError);
  385. return true;
  386. }
  387. void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address,
  388. StringRef Delimiter) {
  389. if (!Symbolizer)
  390. return;
  391. DILineInfo LineInfo = DILineInfo();
  392. auto ExpectecLineInfo =
  393. Symbolizer->symbolizeCode(Obj->getFileName(), Address);
  394. if (!ExpectecLineInfo)
  395. consumeError(ExpectecLineInfo.takeError());
  396. else
  397. LineInfo = *ExpectecLineInfo;
  398. if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line ||
  399. LineInfo.Line == 0)
  400. return;
  401. if (PrintLines)
  402. OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n";
  403. if (PrintSource) {
  404. if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
  405. if (!cacheSource(LineInfo.FileName))
  406. return;
  407. auto FileBuffer = SourceCache.find(LineInfo.FileName);
  408. if (FileBuffer != SourceCache.end()) {
  409. auto LineBuffer = LineCache.find(LineInfo.FileName);
  410. if (LineBuffer != LineCache.end()) {
  411. if (LineInfo.Line > LineBuffer->second.size())
  412. return;
  413. // Vector begins at 0, line numbers are non-zero
  414. OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim()
  415. << "\n";
  416. }
  417. }
  418. }
  419. OldLineInfo = LineInfo;
  420. }
  421. static bool isArmElf(const ObjectFile *Obj) {
  422. return (Obj->isELF() &&
  423. (Obj->getArch() == Triple::aarch64 ||
  424. Obj->getArch() == Triple::aarch64_be ||
  425. Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb ||
  426. Obj->getArch() == Triple::thumb ||
  427. Obj->getArch() == Triple::thumbeb));
  428. }
  429. class PrettyPrinter {
  430. public:
  431. virtual ~PrettyPrinter() = default;
  432. virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
  433. ArrayRef<uint8_t> Bytes, uint64_t Address,
  434. raw_ostream &OS, StringRef Annot,
  435. MCSubtargetInfo const &STI, SourcePrinter *SP) {
  436. if (SP && (PrintSource || PrintLines))
  437. SP->printSourceLine(OS, Address);
  438. if (!NoLeadingAddr)
  439. OS << format("%8" PRIx64 ":", Address);
  440. if (!NoShowRawInsn) {
  441. OS << "\t";
  442. dumpBytes(Bytes, OS);
  443. }
  444. if (MI)
  445. IP.printInst(MI, OS, "", STI);
  446. else
  447. OS << " <unknown>";
  448. }
  449. };
  450. PrettyPrinter PrettyPrinterInst;
  451. class HexagonPrettyPrinter : public PrettyPrinter {
  452. public:
  453. void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
  454. raw_ostream &OS) {
  455. uint32_t opcode =
  456. (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
  457. if (!NoLeadingAddr)
  458. OS << format("%8" PRIx64 ":", Address);
  459. if (!NoShowRawInsn) {
  460. OS << "\t";
  461. dumpBytes(Bytes.slice(0, 4), OS);
  462. OS << format("%08" PRIx32, opcode);
  463. }
  464. }
  465. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  466. uint64_t Address, raw_ostream &OS, StringRef Annot,
  467. MCSubtargetInfo const &STI, SourcePrinter *SP) override {
  468. if (SP && (PrintSource || PrintLines))
  469. SP->printSourceLine(OS, Address, "");
  470. if (!MI) {
  471. printLead(Bytes, Address, OS);
  472. OS << " <unknown>";
  473. return;
  474. }
  475. std::string Buffer;
  476. {
  477. raw_string_ostream TempStream(Buffer);
  478. IP.printInst(MI, TempStream, "", STI);
  479. }
  480. StringRef Contents(Buffer);
  481. // Split off bundle attributes
  482. auto PacketBundle = Contents.rsplit('\n');
  483. // Split off first instruction from the rest
  484. auto HeadTail = PacketBundle.first.split('\n');
  485. auto Preamble = " { ";
  486. auto Separator = "";
  487. while(!HeadTail.first.empty()) {
  488. OS << Separator;
  489. Separator = "\n";
  490. if (SP && (PrintSource || PrintLines))
  491. SP->printSourceLine(OS, Address, "");
  492. printLead(Bytes, Address, OS);
  493. OS << Preamble;
  494. Preamble = " ";
  495. StringRef Inst;
  496. auto Duplex = HeadTail.first.split('\v');
  497. if(!Duplex.second.empty()){
  498. OS << Duplex.first;
  499. OS << "; ";
  500. Inst = Duplex.second;
  501. }
  502. else
  503. Inst = HeadTail.first;
  504. OS << Inst;
  505. Bytes = Bytes.slice(4);
  506. Address += 4;
  507. HeadTail = HeadTail.second.split('\n');
  508. }
  509. OS << " } " << PacketBundle.second;
  510. }
  511. };
  512. HexagonPrettyPrinter HexagonPrettyPrinterInst;
  513. class AMDGCNPrettyPrinter : public PrettyPrinter {
  514. public:
  515. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  516. uint64_t Address, raw_ostream &OS, StringRef Annot,
  517. MCSubtargetInfo const &STI, SourcePrinter *SP) override {
  518. if (SP && (PrintSource || PrintLines))
  519. SP->printSourceLine(OS, Address);
  520. if (!MI) {
  521. OS << " <unknown>";
  522. return;
  523. }
  524. SmallString<40> InstStr;
  525. raw_svector_ostream IS(InstStr);
  526. IP.printInst(MI, IS, "", STI);
  527. OS << left_justify(IS.str(), 60) << format("// %012" PRIX64 ": ", Address);
  528. typedef support::ulittle32_t U32;
  529. for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()),
  530. Bytes.size() / sizeof(U32)))
  531. // D should be explicitly casted to uint32_t here as it is passed
  532. // by format to snprintf as vararg.
  533. OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D));
  534. if (!Annot.empty())
  535. OS << "// " << Annot;
  536. }
  537. };
  538. AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
  539. class BPFPrettyPrinter : public PrettyPrinter {
  540. public:
  541. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  542. uint64_t Address, raw_ostream &OS, StringRef Annot,
  543. MCSubtargetInfo const &STI, SourcePrinter *SP) override {
  544. if (SP && (PrintSource || PrintLines))
  545. SP->printSourceLine(OS, Address);
  546. if (!NoLeadingAddr)
  547. OS << format("%8" PRId64 ":", Address / 8);
  548. if (!NoShowRawInsn) {
  549. OS << "\t";
  550. dumpBytes(Bytes, OS);
  551. }
  552. if (MI)
  553. IP.printInst(MI, OS, "", STI);
  554. else
  555. OS << " <unknown>";
  556. }
  557. };
  558. BPFPrettyPrinter BPFPrettyPrinterInst;
  559. PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
  560. switch(Triple.getArch()) {
  561. default:
  562. return PrettyPrinterInst;
  563. case Triple::hexagon:
  564. return HexagonPrettyPrinterInst;
  565. case Triple::amdgcn:
  566. return AMDGCNPrettyPrinterInst;
  567. case Triple::bpfel:
  568. case Triple::bpfeb:
  569. return BPFPrettyPrinterInst;
  570. }
  571. }
  572. }
  573. template <class ELFT>
  574. static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
  575. const RelocationRef &RelRef,
  576. SmallVectorImpl<char> &Result) {
  577. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  578. typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
  579. typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
  580. typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
  581. const ELFFile<ELFT> &EF = *Obj->getELFFile();
  582. auto SecOrErr = EF.getSection(Rel.d.a);
  583. if (!SecOrErr)
  584. return errorToErrorCode(SecOrErr.takeError());
  585. const Elf_Shdr *Sec = *SecOrErr;
  586. auto SymTabOrErr = EF.getSection(Sec->sh_link);
  587. if (!SymTabOrErr)
  588. return errorToErrorCode(SymTabOrErr.takeError());
  589. const Elf_Shdr *SymTab = *SymTabOrErr;
  590. assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
  591. SymTab->sh_type == ELF::SHT_DYNSYM);
  592. auto StrTabSec = EF.getSection(SymTab->sh_link);
  593. if (!StrTabSec)
  594. return errorToErrorCode(StrTabSec.takeError());
  595. auto StrTabOrErr = EF.getStringTable(*StrTabSec);
  596. if (!StrTabOrErr)
  597. return errorToErrorCode(StrTabOrErr.takeError());
  598. StringRef StrTab = *StrTabOrErr;
  599. uint8_t type = RelRef.getType();
  600. StringRef res;
  601. int64_t addend = 0;
  602. switch (Sec->sh_type) {
  603. default:
  604. return object_error::parse_failed;
  605. case ELF::SHT_REL: {
  606. // TODO: Read implicit addend from section data.
  607. break;
  608. }
  609. case ELF::SHT_RELA: {
  610. const Elf_Rela *ERela = Obj->getRela(Rel);
  611. addend = ERela->r_addend;
  612. break;
  613. }
  614. }
  615. symbol_iterator SI = RelRef.getSymbol();
  616. const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
  617. StringRef Target;
  618. if (symb->getType() == ELF::STT_SECTION) {
  619. Expected<section_iterator> SymSI = SI->getSection();
  620. if (!SymSI)
  621. return errorToErrorCode(SymSI.takeError());
  622. const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
  623. auto SecName = EF.getSectionName(SymSec);
  624. if (!SecName)
  625. return errorToErrorCode(SecName.takeError());
  626. Target = *SecName;
  627. } else {
  628. Expected<StringRef> SymName = symb->getName(StrTab);
  629. if (!SymName)
  630. return errorToErrorCode(SymName.takeError());
  631. Target = *SymName;
  632. }
  633. switch (EF.getHeader()->e_machine) {
  634. case ELF::EM_X86_64:
  635. switch (type) {
  636. case ELF::R_X86_64_PC8:
  637. case ELF::R_X86_64_PC16:
  638. case ELF::R_X86_64_PC32: {
  639. std::string fmtbuf;
  640. raw_string_ostream fmt(fmtbuf);
  641. fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
  642. fmt.flush();
  643. Result.append(fmtbuf.begin(), fmtbuf.end());
  644. } break;
  645. case ELF::R_X86_64_8:
  646. case ELF::R_X86_64_16:
  647. case ELF::R_X86_64_32:
  648. case ELF::R_X86_64_32S:
  649. case ELF::R_X86_64_64: {
  650. std::string fmtbuf;
  651. raw_string_ostream fmt(fmtbuf);
  652. fmt << Target << (addend < 0 ? "" : "+") << addend;
  653. fmt.flush();
  654. Result.append(fmtbuf.begin(), fmtbuf.end());
  655. } break;
  656. default:
  657. res = "Unknown";
  658. }
  659. break;
  660. case ELF::EM_LANAI:
  661. case ELF::EM_AVR:
  662. case ELF::EM_AARCH64: {
  663. std::string fmtbuf;
  664. raw_string_ostream fmt(fmtbuf);
  665. fmt << Target;
  666. if (addend != 0)
  667. fmt << (addend < 0 ? "" : "+") << addend;
  668. fmt.flush();
  669. Result.append(fmtbuf.begin(), fmtbuf.end());
  670. break;
  671. }
  672. case ELF::EM_386:
  673. case ELF::EM_IAMCU:
  674. case ELF::EM_ARM:
  675. case ELF::EM_HEXAGON:
  676. case ELF::EM_MIPS:
  677. case ELF::EM_BPF:
  678. case ELF::EM_RISCV:
  679. res = Target;
  680. break;
  681. case ELF::EM_WEBASSEMBLY:
  682. switch (type) {
  683. case ELF::R_WEBASSEMBLY_DATA: {
  684. std::string fmtbuf;
  685. raw_string_ostream fmt(fmtbuf);
  686. fmt << Target << (addend < 0 ? "" : "+") << addend;
  687. fmt.flush();
  688. Result.append(fmtbuf.begin(), fmtbuf.end());
  689. break;
  690. }
  691. case ELF::R_WEBASSEMBLY_FUNCTION:
  692. res = Target;
  693. break;
  694. default:
  695. res = "Unknown";
  696. }
  697. break;
  698. default:
  699. res = "Unknown";
  700. }
  701. if (Result.empty())
  702. Result.append(res.begin(), res.end());
  703. return std::error_code();
  704. }
  705. static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
  706. const RelocationRef &Rel,
  707. SmallVectorImpl<char> &Result) {
  708. if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
  709. return getRelocationValueString(ELF32LE, Rel, Result);
  710. if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
  711. return getRelocationValueString(ELF64LE, Rel, Result);
  712. if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
  713. return getRelocationValueString(ELF32BE, Rel, Result);
  714. auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
  715. return getRelocationValueString(ELF64BE, Rel, Result);
  716. }
  717. static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
  718. const RelocationRef &Rel,
  719. SmallVectorImpl<char> &Result) {
  720. symbol_iterator SymI = Rel.getSymbol();
  721. Expected<StringRef> SymNameOrErr = SymI->getName();
  722. if (!SymNameOrErr)
  723. return errorToErrorCode(SymNameOrErr.takeError());
  724. StringRef SymName = *SymNameOrErr;
  725. Result.append(SymName.begin(), SymName.end());
  726. return std::error_code();
  727. }
  728. static void printRelocationTargetName(const MachOObjectFile *O,
  729. const MachO::any_relocation_info &RE,
  730. raw_string_ostream &fmt) {
  731. bool IsScattered = O->isRelocationScattered(RE);
  732. // Target of a scattered relocation is an address. In the interest of
  733. // generating pretty output, scan through the symbol table looking for a
  734. // symbol that aligns with that address. If we find one, print it.
  735. // Otherwise, we just print the hex address of the target.
  736. if (IsScattered) {
  737. uint32_t Val = O->getPlainRelocationSymbolNum(RE);
  738. for (const SymbolRef &Symbol : O->symbols()) {
  739. std::error_code ec;
  740. Expected<uint64_t> Addr = Symbol.getAddress();
  741. if (!Addr)
  742. report_error(O->getFileName(), Addr.takeError());
  743. if (*Addr != Val)
  744. continue;
  745. Expected<StringRef> Name = Symbol.getName();
  746. if (!Name)
  747. report_error(O->getFileName(), Name.takeError());
  748. fmt << *Name;
  749. return;
  750. }
  751. // If we couldn't find a symbol that this relocation refers to, try
  752. // to find a section beginning instead.
  753. for (const SectionRef &Section : ToolSectionFilter(*O)) {
  754. std::error_code ec;
  755. StringRef Name;
  756. uint64_t Addr = Section.getAddress();
  757. if (Addr != Val)
  758. continue;
  759. if ((ec = Section.getName(Name)))
  760. report_error(O->getFileName(), ec);
  761. fmt << Name;
  762. return;
  763. }
  764. fmt << format("0x%x", Val);
  765. return;
  766. }
  767. StringRef S;
  768. bool isExtern = O->getPlainRelocationExternal(RE);
  769. uint64_t Val = O->getPlainRelocationSymbolNum(RE);
  770. if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
  771. fmt << format("0x%0" PRIx64, Val);
  772. return;
  773. } else if (isExtern) {
  774. symbol_iterator SI = O->symbol_begin();
  775. advance(SI, Val);
  776. Expected<StringRef> SOrErr = SI->getName();
  777. if (!SOrErr)
  778. report_error(O->getFileName(), SOrErr.takeError());
  779. S = *SOrErr;
  780. } else {
  781. section_iterator SI = O->section_begin();
  782. // Adjust for the fact that sections are 1-indexed.
  783. if (Val == 0) {
  784. fmt << "0 (?,?)";
  785. return;
  786. }
  787. uint32_t i = Val - 1;
  788. while (i != 0 && SI != O->section_end()) {
  789. i--;
  790. advance(SI, 1);
  791. }
  792. if (SI == O->section_end())
  793. fmt << Val << " (?,?)";
  794. else
  795. SI->getName(S);
  796. }
  797. fmt << S;
  798. }
  799. static std::error_code getRelocationValueString(const WasmObjectFile *Obj,
  800. const RelocationRef &RelRef,
  801. SmallVectorImpl<char> &Result) {
  802. const wasm::WasmRelocation& Rel = Obj->getWasmRelocation(RelRef);
  803. std::string fmtbuf;
  804. raw_string_ostream fmt(fmtbuf);
  805. fmt << Rel.Index << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
  806. fmt.flush();
  807. Result.append(fmtbuf.begin(), fmtbuf.end());
  808. return std::error_code();
  809. }
  810. static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
  811. const RelocationRef &RelRef,
  812. SmallVectorImpl<char> &Result) {
  813. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  814. MachO::any_relocation_info RE = Obj->getRelocation(Rel);
  815. unsigned Arch = Obj->getArch();
  816. std::string fmtbuf;
  817. raw_string_ostream fmt(fmtbuf);
  818. unsigned Type = Obj->getAnyRelocationType(RE);
  819. bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
  820. // Determine any addends that should be displayed with the relocation.
  821. // These require decoding the relocation type, which is triple-specific.
  822. // X86_64 has entirely custom relocation types.
  823. if (Arch == Triple::x86_64) {
  824. bool isPCRel = Obj->getAnyRelocationPCRel(RE);
  825. switch (Type) {
  826. case MachO::X86_64_RELOC_GOT_LOAD:
  827. case MachO::X86_64_RELOC_GOT: {
  828. printRelocationTargetName(Obj, RE, fmt);
  829. fmt << "@GOT";
  830. if (isPCRel)
  831. fmt << "PCREL";
  832. break;
  833. }
  834. case MachO::X86_64_RELOC_SUBTRACTOR: {
  835. DataRefImpl RelNext = Rel;
  836. Obj->moveRelocationNext(RelNext);
  837. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  838. // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
  839. // X86_64_RELOC_UNSIGNED.
  840. // NOTE: Scattered relocations don't exist on x86_64.
  841. unsigned RType = Obj->getAnyRelocationType(RENext);
  842. if (RType != MachO::X86_64_RELOC_UNSIGNED)
  843. report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
  844. "X86_64_RELOC_SUBTRACTOR.");
  845. // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
  846. // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
  847. printRelocationTargetName(Obj, RENext, fmt);
  848. fmt << "-";
  849. printRelocationTargetName(Obj, RE, fmt);
  850. break;
  851. }
  852. case MachO::X86_64_RELOC_TLV:
  853. printRelocationTargetName(Obj, RE, fmt);
  854. fmt << "@TLV";
  855. if (isPCRel)
  856. fmt << "P";
  857. break;
  858. case MachO::X86_64_RELOC_SIGNED_1:
  859. printRelocationTargetName(Obj, RE, fmt);
  860. fmt << "-1";
  861. break;
  862. case MachO::X86_64_RELOC_SIGNED_2:
  863. printRelocationTargetName(Obj, RE, fmt);
  864. fmt << "-2";
  865. break;
  866. case MachO::X86_64_RELOC_SIGNED_4:
  867. printRelocationTargetName(Obj, RE, fmt);
  868. fmt << "-4";
  869. break;
  870. default:
  871. printRelocationTargetName(Obj, RE, fmt);
  872. break;
  873. }
  874. // X86 and ARM share some relocation types in common.
  875. } else if (Arch == Triple::x86 || Arch == Triple::arm ||
  876. Arch == Triple::ppc) {
  877. // Generic relocation types...
  878. switch (Type) {
  879. case MachO::GENERIC_RELOC_PAIR: // prints no info
  880. return std::error_code();
  881. case MachO::GENERIC_RELOC_SECTDIFF: {
  882. DataRefImpl RelNext = Rel;
  883. Obj->moveRelocationNext(RelNext);
  884. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  885. // X86 sect diff's must be followed by a relocation of type
  886. // GENERIC_RELOC_PAIR.
  887. unsigned RType = Obj->getAnyRelocationType(RENext);
  888. if (RType != MachO::GENERIC_RELOC_PAIR)
  889. report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
  890. "GENERIC_RELOC_SECTDIFF.");
  891. printRelocationTargetName(Obj, RE, fmt);
  892. fmt << "-";
  893. printRelocationTargetName(Obj, RENext, fmt);
  894. break;
  895. }
  896. }
  897. if (Arch == Triple::x86 || Arch == Triple::ppc) {
  898. switch (Type) {
  899. case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
  900. DataRefImpl RelNext = Rel;
  901. Obj->moveRelocationNext(RelNext);
  902. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  903. // X86 sect diff's must be followed by a relocation of type
  904. // GENERIC_RELOC_PAIR.
  905. unsigned RType = Obj->getAnyRelocationType(RENext);
  906. if (RType != MachO::GENERIC_RELOC_PAIR)
  907. report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
  908. "GENERIC_RELOC_LOCAL_SECTDIFF.");
  909. printRelocationTargetName(Obj, RE, fmt);
  910. fmt << "-";
  911. printRelocationTargetName(Obj, RENext, fmt);
  912. break;
  913. }
  914. case MachO::GENERIC_RELOC_TLV: {
  915. printRelocationTargetName(Obj, RE, fmt);
  916. fmt << "@TLV";
  917. if (IsPCRel)
  918. fmt << "P";
  919. break;
  920. }
  921. default:
  922. printRelocationTargetName(Obj, RE, fmt);
  923. }
  924. } else { // ARM-specific relocations
  925. switch (Type) {
  926. case MachO::ARM_RELOC_HALF:
  927. case MachO::ARM_RELOC_HALF_SECTDIFF: {
  928. // Half relocations steal a bit from the length field to encode
  929. // whether this is an upper16 or a lower16 relocation.
  930. bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
  931. if (isUpper)
  932. fmt << ":upper16:(";
  933. else
  934. fmt << ":lower16:(";
  935. printRelocationTargetName(Obj, RE, fmt);
  936. DataRefImpl RelNext = Rel;
  937. Obj->moveRelocationNext(RelNext);
  938. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  939. // ARM half relocs must be followed by a relocation of type
  940. // ARM_RELOC_PAIR.
  941. unsigned RType = Obj->getAnyRelocationType(RENext);
  942. if (RType != MachO::ARM_RELOC_PAIR)
  943. report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
  944. "ARM_RELOC_HALF");
  945. // NOTE: The half of the target virtual address is stashed in the
  946. // address field of the secondary relocation, but we can't reverse
  947. // engineer the constant offset from it without decoding the movw/movt
  948. // instruction to find the other half in its immediate field.
  949. // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
  950. // symbol/section pointer of the follow-on relocation.
  951. if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  952. fmt << "-";
  953. printRelocationTargetName(Obj, RENext, fmt);
  954. }
  955. fmt << ")";
  956. break;
  957. }
  958. default: { printRelocationTargetName(Obj, RE, fmt); }
  959. }
  960. }
  961. } else
  962. printRelocationTargetName(Obj, RE, fmt);
  963. fmt.flush();
  964. Result.append(fmtbuf.begin(), fmtbuf.end());
  965. return std::error_code();
  966. }
  967. static std::error_code getRelocationValueString(const RelocationRef &Rel,
  968. SmallVectorImpl<char> &Result) {
  969. const ObjectFile *Obj = Rel.getObject();
  970. if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
  971. return getRelocationValueString(ELF, Rel, Result);
  972. if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
  973. return getRelocationValueString(COFF, Rel, Result);
  974. if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
  975. return getRelocationValueString(Wasm, Rel, Result);
  976. if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
  977. return getRelocationValueString(MachO, Rel, Result);
  978. llvm_unreachable("unknown object file format");
  979. }
  980. /// @brief Indicates whether this relocation should hidden when listing
  981. /// relocations, usually because it is the trailing part of a multipart
  982. /// relocation that will be printed as part of the leading relocation.
  983. static bool getHidden(RelocationRef RelRef) {
  984. const ObjectFile *Obj = RelRef.getObject();
  985. auto *MachO = dyn_cast<MachOObjectFile>(Obj);
  986. if (!MachO)
  987. return false;
  988. unsigned Arch = MachO->getArch();
  989. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  990. uint64_t Type = MachO->getRelocationType(Rel);
  991. // On arches that use the generic relocations, GENERIC_RELOC_PAIR
  992. // is always hidden.
  993. if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
  994. if (Type == MachO::GENERIC_RELOC_PAIR)
  995. return true;
  996. } else if (Arch == Triple::x86_64) {
  997. // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
  998. // an X86_64_RELOC_SUBTRACTOR.
  999. if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
  1000. DataRefImpl RelPrev = Rel;
  1001. RelPrev.d.a--;
  1002. uint64_t PrevType = MachO->getRelocationType(RelPrev);
  1003. if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
  1004. return true;
  1005. }
  1006. }
  1007. return false;
  1008. }
  1009. static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
  1010. assert(Obj->isELF());
  1011. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  1012. return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
  1013. if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  1014. return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
  1015. if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  1016. return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
  1017. if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
  1018. return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
  1019. llvm_unreachable("Unsupported binary format");
  1020. }
  1021. template <class ELFT> static void
  1022. addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj,
  1023. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  1024. for (auto Symbol : Obj->getDynamicSymbolIterators()) {
  1025. uint8_t SymbolType = Symbol.getELFType();
  1026. if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0)
  1027. continue;
  1028. Expected<uint64_t> AddressOrErr = Symbol.getAddress();
  1029. if (!AddressOrErr)
  1030. report_error(Obj->getFileName(), AddressOrErr.takeError());
  1031. uint64_t Address = *AddressOrErr;
  1032. Expected<StringRef> Name = Symbol.getName();
  1033. if (!Name)
  1034. report_error(Obj->getFileName(), Name.takeError());
  1035. if (Name->empty())
  1036. continue;
  1037. Expected<section_iterator> SectionOrErr = Symbol.getSection();
  1038. if (!SectionOrErr)
  1039. report_error(Obj->getFileName(), SectionOrErr.takeError());
  1040. section_iterator SecI = *SectionOrErr;
  1041. if (SecI == Obj->section_end())
  1042. continue;
  1043. AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
  1044. }
  1045. }
  1046. static void
  1047. addDynamicElfSymbols(const ObjectFile *Obj,
  1048. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  1049. assert(Obj->isELF());
  1050. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  1051. addDynamicElfSymbols(Elf32LEObj, AllSymbols);
  1052. else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  1053. addDynamicElfSymbols(Elf64LEObj, AllSymbols);
  1054. else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  1055. addDynamicElfSymbols(Elf32BEObj, AllSymbols);
  1056. else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
  1057. addDynamicElfSymbols(Elf64BEObj, AllSymbols);
  1058. else
  1059. llvm_unreachable("Unsupported binary format");
  1060. }
  1061. static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
  1062. if (StartAddress > StopAddress)
  1063. error("Start address should be less than stop address");
  1064. const Target *TheTarget = getTarget(Obj);
  1065. // Package up features to be passed to target/subtarget
  1066. SubtargetFeatures Features = Obj->getFeatures();
  1067. if (MAttrs.size()) {
  1068. for (unsigned i = 0; i != MAttrs.size(); ++i)
  1069. Features.AddFeature(MAttrs[i]);
  1070. }
  1071. std::unique_ptr<const MCRegisterInfo> MRI(
  1072. TheTarget->createMCRegInfo(TripleName));
  1073. if (!MRI)
  1074. report_error(Obj->getFileName(), "no register info for target " +
  1075. TripleName);
  1076. // Set up disassembler.
  1077. std::unique_ptr<const MCAsmInfo> AsmInfo(
  1078. TheTarget->createMCAsmInfo(*MRI, TripleName));
  1079. if (!AsmInfo)
  1080. report_error(Obj->getFileName(), "no assembly info for target " +
  1081. TripleName);
  1082. std::unique_ptr<const MCSubtargetInfo> STI(
  1083. TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
  1084. if (!STI)
  1085. report_error(Obj->getFileName(), "no subtarget info for target " +
  1086. TripleName);
  1087. std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
  1088. if (!MII)
  1089. report_error(Obj->getFileName(), "no instruction info for target " +
  1090. TripleName);
  1091. MCObjectFileInfo MOFI;
  1092. MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
  1093. // FIXME: for now initialize MCObjectFileInfo with default values
  1094. MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
  1095. std::unique_ptr<MCDisassembler> DisAsm(
  1096. TheTarget->createMCDisassembler(*STI, Ctx));
  1097. if (!DisAsm)
  1098. report_error(Obj->getFileName(), "no disassembler for target " +
  1099. TripleName);
  1100. std::unique_ptr<const MCInstrAnalysis> MIA(
  1101. TheTarget->createMCInstrAnalysis(MII.get()));
  1102. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  1103. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  1104. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
  1105. if (!IP)
  1106. report_error(Obj->getFileName(), "no instruction printer for target " +
  1107. TripleName);
  1108. IP->setPrintImmHex(PrintImmHex);
  1109. PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
  1110. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
  1111. "\t\t\t%08" PRIx64 ": ";
  1112. SourcePrinter SP(Obj, TheTarget->getName());
  1113. // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
  1114. // in RelocSecs contain the relocations for section S.
  1115. std::error_code EC;
  1116. std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
  1117. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1118. section_iterator Sec2 = Section.getRelocatedSection();
  1119. if (Sec2 != Obj->section_end())
  1120. SectionRelocMap[*Sec2].push_back(Section);
  1121. }
  1122. // Create a mapping from virtual address to symbol name. This is used to
  1123. // pretty print the symbols while disassembling.
  1124. std::map<SectionRef, SectionSymbolsTy> AllSymbols;
  1125. for (const SymbolRef &Symbol : Obj->symbols()) {
  1126. Expected<uint64_t> AddressOrErr = Symbol.getAddress();
  1127. if (!AddressOrErr)
  1128. report_error(Obj->getFileName(), AddressOrErr.takeError());
  1129. uint64_t Address = *AddressOrErr;
  1130. Expected<StringRef> Name = Symbol.getName();
  1131. if (!Name)
  1132. report_error(Obj->getFileName(), Name.takeError());
  1133. if (Name->empty())
  1134. continue;
  1135. Expected<section_iterator> SectionOrErr = Symbol.getSection();
  1136. if (!SectionOrErr)
  1137. report_error(Obj->getFileName(), SectionOrErr.takeError());
  1138. section_iterator SecI = *SectionOrErr;
  1139. if (SecI == Obj->section_end())
  1140. continue;
  1141. uint8_t SymbolType = ELF::STT_NOTYPE;
  1142. if (Obj->isELF())
  1143. SymbolType = getElfSymbolType(Obj, Symbol);
  1144. AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
  1145. }
  1146. if (AllSymbols.empty() && Obj->isELF())
  1147. addDynamicElfSymbols(Obj, AllSymbols);
  1148. // Create a mapping from virtual address to section.
  1149. std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
  1150. for (SectionRef Sec : Obj->sections())
  1151. SectionAddresses.emplace_back(Sec.getAddress(), Sec);
  1152. array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
  1153. // Linked executables (.exe and .dll files) typically don't include a real
  1154. // symbol table but they might contain an export table.
  1155. if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
  1156. for (const auto &ExportEntry : COFFObj->export_directories()) {
  1157. StringRef Name;
  1158. error(ExportEntry.getSymbolName(Name));
  1159. if (Name.empty())
  1160. continue;
  1161. uint32_t RVA;
  1162. error(ExportEntry.getExportRVA(RVA));
  1163. uint64_t VA = COFFObj->getImageBase() + RVA;
  1164. auto Sec = std::upper_bound(
  1165. SectionAddresses.begin(), SectionAddresses.end(), VA,
  1166. [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
  1167. return LHS < RHS.first;
  1168. });
  1169. if (Sec != SectionAddresses.begin())
  1170. --Sec;
  1171. else
  1172. Sec = SectionAddresses.end();
  1173. if (Sec != SectionAddresses.end())
  1174. AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
  1175. }
  1176. }
  1177. // Sort all the symbols, this allows us to use a simple binary search to find
  1178. // a symbol near an address.
  1179. for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
  1180. array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
  1181. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1182. if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
  1183. continue;
  1184. uint64_t SectionAddr = Section.getAddress();
  1185. uint64_t SectSize = Section.getSize();
  1186. if (!SectSize)
  1187. continue;
  1188. // Get the list of all the symbols in this section.
  1189. SectionSymbolsTy &Symbols = AllSymbols[Section];
  1190. std::vector<uint64_t> DataMappingSymsAddr;
  1191. std::vector<uint64_t> TextMappingSymsAddr;
  1192. if (isArmElf(Obj)) {
  1193. for (const auto &Symb : Symbols) {
  1194. uint64_t Address = std::get<0>(Symb);
  1195. StringRef Name = std::get<1>(Symb);
  1196. if (Name.startswith("$d"))
  1197. DataMappingSymsAddr.push_back(Address - SectionAddr);
  1198. if (Name.startswith("$x"))
  1199. TextMappingSymsAddr.push_back(Address - SectionAddr);
  1200. if (Name.startswith("$a"))
  1201. TextMappingSymsAddr.push_back(Address - SectionAddr);
  1202. if (Name.startswith("$t"))
  1203. TextMappingSymsAddr.push_back(Address - SectionAddr);
  1204. }
  1205. }
  1206. std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
  1207. std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
  1208. if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
  1209. // AMDGPU disassembler uses symbolizer for printing labels
  1210. std::unique_ptr<MCRelocationInfo> RelInfo(
  1211. TheTarget->createMCRelocationInfo(TripleName, Ctx));
  1212. if (RelInfo) {
  1213. std::unique_ptr<MCSymbolizer> Symbolizer(
  1214. TheTarget->createMCSymbolizer(
  1215. TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
  1216. DisAsm->setSymbolizer(std::move(Symbolizer));
  1217. }
  1218. }
  1219. // Make a list of all the relocations for this section.
  1220. std::vector<RelocationRef> Rels;
  1221. if (InlineRelocs) {
  1222. for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
  1223. for (const RelocationRef &Reloc : RelocSec.relocations()) {
  1224. Rels.push_back(Reloc);
  1225. }
  1226. }
  1227. }
  1228. // Sort relocations by address.
  1229. std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
  1230. StringRef SegmentName = "";
  1231. if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
  1232. DataRefImpl DR = Section.getRawDataRefImpl();
  1233. SegmentName = MachO->getSectionFinalSegmentName(DR);
  1234. }
  1235. StringRef name;
  1236. error(Section.getName(name));
  1237. if ((SectionAddr <= StopAddress) &&
  1238. (SectionAddr + SectSize) >= StartAddress) {
  1239. outs() << "Disassembly of section ";
  1240. if (!SegmentName.empty())
  1241. outs() << SegmentName << ",";
  1242. outs() << name << ':';
  1243. }
  1244. // If the section has no symbol at the start, just insert a dummy one.
  1245. if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) {
  1246. Symbols.insert(Symbols.begin(),
  1247. std::make_tuple(SectionAddr, name, Section.isText()
  1248. ? ELF::STT_FUNC
  1249. : ELF::STT_OBJECT));
  1250. }
  1251. SmallString<40> Comments;
  1252. raw_svector_ostream CommentStream(Comments);
  1253. StringRef BytesStr;
  1254. error(Section.getContents(BytesStr));
  1255. ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
  1256. BytesStr.size());
  1257. uint64_t Size;
  1258. uint64_t Index;
  1259. std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
  1260. std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
  1261. // Disassemble symbol by symbol.
  1262. for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
  1263. uint64_t Start = std::get<0>(Symbols[si]) - SectionAddr;
  1264. // The end is either the section end or the beginning of the next
  1265. // symbol.
  1266. uint64_t End =
  1267. (si == se - 1) ? SectSize : std::get<0>(Symbols[si + 1]) - SectionAddr;
  1268. // Don't try to disassemble beyond the end of section contents.
  1269. if (End > SectSize)
  1270. End = SectSize;
  1271. // If this symbol has the same address as the next symbol, then skip it.
  1272. if (Start >= End)
  1273. continue;
  1274. // Check if we need to skip symbol
  1275. // Skip if the symbol's data is not between StartAddress and StopAddress
  1276. if (End + SectionAddr < StartAddress ||
  1277. Start + SectionAddr > StopAddress) {
  1278. continue;
  1279. }
  1280. // Stop disassembly at the stop address specified
  1281. if (End + SectionAddr > StopAddress)
  1282. End = StopAddress - SectionAddr;
  1283. if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
  1284. // make size 4 bytes folded
  1285. End = Start + ((End - Start) & ~0x3ull);
  1286. if (std::get<2>(Symbols[si]) == ELF::STT_AMDGPU_HSA_KERNEL) {
  1287. // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes)
  1288. Start += 256;
  1289. }
  1290. if (si == se - 1 ||
  1291. std::get<2>(Symbols[si + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) {
  1292. // cut trailing zeroes at the end of kernel
  1293. // cut up to 256 bytes
  1294. const uint64_t EndAlign = 256;
  1295. const auto Limit = End - (std::min)(EndAlign, End - Start);
  1296. while (End > Limit &&
  1297. *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
  1298. End -= 4;
  1299. }
  1300. }
  1301. outs() << '\n' << std::get<1>(Symbols[si]) << ":\n";
  1302. #ifndef NDEBUG
  1303. raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
  1304. #else
  1305. raw_ostream &DebugOut = nulls();
  1306. #endif
  1307. for (Index = Start; Index < End; Index += Size) {
  1308. MCInst Inst;
  1309. if (Index + SectionAddr < StartAddress ||
  1310. Index + SectionAddr > StopAddress) {
  1311. // skip byte by byte till StartAddress is reached
  1312. Size = 1;
  1313. continue;
  1314. }
  1315. // AArch64 ELF binaries can interleave data and text in the
  1316. // same section. We rely on the markers introduced to
  1317. // understand what we need to dump. If the data marker is within a
  1318. // function, it is denoted as a word/short etc
  1319. if (isArmElf(Obj) && std::get<2>(Symbols[si]) != ELF::STT_OBJECT &&
  1320. !DisassembleAll) {
  1321. uint64_t Stride = 0;
  1322. auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
  1323. DataMappingSymsAddr.end(), Index);
  1324. if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
  1325. // Switch to data.
  1326. while (Index < End) {
  1327. outs() << format("%8" PRIx64 ":", SectionAddr + Index);
  1328. outs() << "\t";
  1329. if (Index + 4 <= End) {
  1330. Stride = 4;
  1331. dumpBytes(Bytes.slice(Index, 4), outs());
  1332. outs() << "\t.word\t";
  1333. uint32_t Data = 0;
  1334. if (Obj->isLittleEndian()) {
  1335. const auto Word =
  1336. reinterpret_cast<const support::ulittle32_t *>(
  1337. Bytes.data() + Index);
  1338. Data = *Word;
  1339. } else {
  1340. const auto Word = reinterpret_cast<const support::ubig32_t *>(
  1341. Bytes.data() + Index);
  1342. Data = *Word;
  1343. }
  1344. outs() << "0x" << format("%08" PRIx32, Data);
  1345. } else if (Index + 2 <= End) {
  1346. Stride = 2;
  1347. dumpBytes(Bytes.slice(Index, 2), outs());
  1348. outs() << "\t\t.short\t";
  1349. uint16_t Data = 0;
  1350. if (Obj->isLittleEndian()) {
  1351. const auto Short =
  1352. reinterpret_cast<const support::ulittle16_t *>(
  1353. Bytes.data() + Index);
  1354. Data = *Short;
  1355. } else {
  1356. const auto Short =
  1357. reinterpret_cast<const support::ubig16_t *>(Bytes.data() +
  1358. Index);
  1359. Data = *Short;
  1360. }
  1361. outs() << "0x" << format("%04" PRIx16, Data);
  1362. } else {
  1363. Stride = 1;
  1364. dumpBytes(Bytes.slice(Index, 1), outs());
  1365. outs() << "\t\t.byte\t";
  1366. outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]);
  1367. }
  1368. Index += Stride;
  1369. outs() << "\n";
  1370. auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
  1371. TextMappingSymsAddr.end(), Index);
  1372. if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
  1373. break;
  1374. }
  1375. }
  1376. }
  1377. // If there is a data symbol inside an ELF text section and we are only
  1378. // disassembling text (applicable all architectures),
  1379. // we are in a situation where we must print the data and not
  1380. // disassemble it.
  1381. if (Obj->isELF() && std::get<2>(Symbols[si]) == ELF::STT_OBJECT &&
  1382. !DisassembleAll && Section.isText()) {
  1383. // print out data up to 8 bytes at a time in hex and ascii
  1384. uint8_t AsciiData[9] = {'\0'};
  1385. uint8_t Byte;
  1386. int NumBytes = 0;
  1387. for (Index = Start; Index < End; Index += 1) {
  1388. if (((SectionAddr + Index) < StartAddress) ||
  1389. ((SectionAddr + Index) > StopAddress))
  1390. continue;
  1391. if (NumBytes == 0) {
  1392. outs() << format("%8" PRIx64 ":", SectionAddr + Index);
  1393. outs() << "\t";
  1394. }
  1395. Byte = Bytes.slice(Index)[0];
  1396. outs() << format(" %02x", Byte);
  1397. AsciiData[NumBytes] = isprint(Byte) ? Byte : '.';
  1398. uint8_t IndentOffset = 0;
  1399. NumBytes++;
  1400. if (Index == End - 1 || NumBytes > 8) {
  1401. // Indent the space for less than 8 bytes data.
  1402. // 2 spaces for byte and one for space between bytes
  1403. IndentOffset = 3 * (8 - NumBytes);
  1404. for (int Excess = 8 - NumBytes; Excess < 8; Excess++)
  1405. AsciiData[Excess] = '\0';
  1406. NumBytes = 8;
  1407. }
  1408. if (NumBytes == 8) {
  1409. AsciiData[8] = '\0';
  1410. outs() << std::string(IndentOffset, ' ') << " ";
  1411. outs() << reinterpret_cast<char *>(AsciiData);
  1412. outs() << '\n';
  1413. NumBytes = 0;
  1414. }
  1415. }
  1416. }
  1417. if (Index >= End)
  1418. break;
  1419. // Disassemble a real instruction or a data when disassemble all is
  1420. // provided
  1421. bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
  1422. SectionAddr + Index, DebugOut,
  1423. CommentStream);
  1424. if (Size == 0)
  1425. Size = 1;
  1426. PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
  1427. Bytes.slice(Index, Size), SectionAddr + Index, outs(), "",
  1428. *STI, &SP);
  1429. outs() << CommentStream.str();
  1430. Comments.clear();
  1431. // Try to resolve the target of a call, tail call, etc. to a specific
  1432. // symbol.
  1433. if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
  1434. MIA->isConditionalBranch(Inst))) {
  1435. uint64_t Target;
  1436. if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
  1437. // In a relocatable object, the target's section must reside in
  1438. // the same section as the call instruction or it is accessed
  1439. // through a relocation.
  1440. //
  1441. // In a non-relocatable object, the target may be in any section.
  1442. //
  1443. // N.B. We don't walk the relocations in the relocatable case yet.
  1444. auto *TargetSectionSymbols = &Symbols;
  1445. if (!Obj->isRelocatableObject()) {
  1446. auto SectionAddress = std::upper_bound(
  1447. SectionAddresses.begin(), SectionAddresses.end(), Target,
  1448. [](uint64_t LHS,
  1449. const std::pair<uint64_t, SectionRef> &RHS) {
  1450. return LHS < RHS.first;
  1451. });
  1452. if (SectionAddress != SectionAddresses.begin()) {
  1453. --SectionAddress;
  1454. TargetSectionSymbols = &AllSymbols[SectionAddress->second];
  1455. } else {
  1456. TargetSectionSymbols = nullptr;
  1457. }
  1458. }
  1459. // Find the first symbol in the section whose offset is less than
  1460. // or equal to the target.
  1461. if (TargetSectionSymbols) {
  1462. auto TargetSym = std::upper_bound(
  1463. TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
  1464. Target, [](uint64_t LHS,
  1465. const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
  1466. return LHS < std::get<0>(RHS);
  1467. });
  1468. if (TargetSym != TargetSectionSymbols->begin()) {
  1469. --TargetSym;
  1470. uint64_t TargetAddress = std::get<0>(*TargetSym);
  1471. StringRef TargetName = std::get<1>(*TargetSym);
  1472. outs() << " <" << TargetName;
  1473. uint64_t Disp = Target - TargetAddress;
  1474. if (Disp)
  1475. outs() << "+0x" << utohexstr(Disp);
  1476. outs() << '>';
  1477. }
  1478. }
  1479. }
  1480. }
  1481. outs() << "\n";
  1482. // Print relocation for instruction.
  1483. while (rel_cur != rel_end) {
  1484. bool hidden = getHidden(*rel_cur);
  1485. uint64_t addr = rel_cur->getOffset();
  1486. SmallString<16> name;
  1487. SmallString<32> val;
  1488. // If this relocation is hidden, skip it.
  1489. if (hidden || ((SectionAddr + addr) < StartAddress)) {
  1490. ++rel_cur;
  1491. continue;
  1492. }
  1493. // Stop when rel_cur's address is past the current instruction.
  1494. if (addr >= Index + Size) break;
  1495. rel_cur->getTypeName(name);
  1496. error(getRelocationValueString(*rel_cur, val));
  1497. outs() << format(Fmt.data(), SectionAddr + addr) << name
  1498. << "\t" << val << "\n";
  1499. ++rel_cur;
  1500. }
  1501. }
  1502. }
  1503. }
  1504. }
  1505. void llvm::PrintRelocations(const ObjectFile *Obj) {
  1506. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
  1507. "%08" PRIx64;
  1508. // Regular objdump doesn't print relocations in non-relocatable object
  1509. // files.
  1510. if (!Obj->isRelocatableObject())
  1511. return;
  1512. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1513. if (Section.relocation_begin() == Section.relocation_end())
  1514. continue;
  1515. StringRef secname;
  1516. error(Section.getName(secname));
  1517. outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
  1518. for (const RelocationRef &Reloc : Section.relocations()) {
  1519. bool hidden = getHidden(Reloc);
  1520. uint64_t address = Reloc.getOffset();
  1521. SmallString<32> relocname;
  1522. SmallString<32> valuestr;
  1523. if (address < StartAddress || address > StopAddress || hidden)
  1524. continue;
  1525. Reloc.getTypeName(relocname);
  1526. error(getRelocationValueString(Reloc, valuestr));
  1527. outs() << format(Fmt.data(), address) << " " << relocname << " "
  1528. << valuestr << "\n";
  1529. }
  1530. outs() << "\n";
  1531. }
  1532. }
  1533. void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
  1534. outs() << "Sections:\n"
  1535. "Idx Name Size Address Type\n";
  1536. unsigned i = 0;
  1537. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1538. StringRef Name;
  1539. error(Section.getName(Name));
  1540. uint64_t Address = Section.getAddress();
  1541. uint64_t Size = Section.getSize();
  1542. bool Text = Section.isText();
  1543. bool Data = Section.isData();
  1544. bool BSS = Section.isBSS();
  1545. std::string Type = (std::string(Text ? "TEXT " : "") +
  1546. (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
  1547. outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
  1548. Name.str().c_str(), Size, Address, Type.c_str());
  1549. ++i;
  1550. }
  1551. }
  1552. void llvm::PrintSectionContents(const ObjectFile *Obj) {
  1553. std::error_code EC;
  1554. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1555. StringRef Name;
  1556. StringRef Contents;
  1557. error(Section.getName(Name));
  1558. uint64_t BaseAddr = Section.getAddress();
  1559. uint64_t Size = Section.getSize();
  1560. if (!Size)
  1561. continue;
  1562. outs() << "Contents of section " << Name << ":\n";
  1563. if (Section.isBSS()) {
  1564. outs() << format("<skipping contents of bss section at [%04" PRIx64
  1565. ", %04" PRIx64 ")>\n",
  1566. BaseAddr, BaseAddr + Size);
  1567. continue;
  1568. }
  1569. error(Section.getContents(Contents));
  1570. // Dump out the content as hex and printable ascii characters.
  1571. for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
  1572. outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
  1573. // Dump line of hex.
  1574. for (std::size_t i = 0; i < 16; ++i) {
  1575. if (i != 0 && i % 4 == 0)
  1576. outs() << ' ';
  1577. if (addr + i < end)
  1578. outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
  1579. << hexdigit(Contents[addr + i] & 0xF, true);
  1580. else
  1581. outs() << " ";
  1582. }
  1583. // Print ascii.
  1584. outs() << " ";
  1585. for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
  1586. if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
  1587. outs() << Contents[addr + i];
  1588. else
  1589. outs() << ".";
  1590. }
  1591. outs() << "\n";
  1592. }
  1593. }
  1594. }
  1595. void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
  1596. StringRef ArchitectureName) {
  1597. outs() << "SYMBOL TABLE:\n";
  1598. if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
  1599. printCOFFSymbolTable(coff);
  1600. return;
  1601. }
  1602. for (const SymbolRef &Symbol : o->symbols()) {
  1603. Expected<uint64_t> AddressOrError = Symbol.getAddress();
  1604. if (!AddressOrError)
  1605. report_error(ArchiveName, o->getFileName(), AddressOrError.takeError(),
  1606. ArchitectureName);
  1607. uint64_t Address = *AddressOrError;
  1608. if ((Address < StartAddress) || (Address > StopAddress))
  1609. continue;
  1610. Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
  1611. if (!TypeOrError)
  1612. report_error(ArchiveName, o->getFileName(), TypeOrError.takeError(),
  1613. ArchitectureName);
  1614. SymbolRef::Type Type = *TypeOrError;
  1615. uint32_t Flags = Symbol.getFlags();
  1616. Expected<section_iterator> SectionOrErr = Symbol.getSection();
  1617. if (!SectionOrErr)
  1618. report_error(ArchiveName, o->getFileName(), SectionOrErr.takeError(),
  1619. ArchitectureName);
  1620. section_iterator Section = *SectionOrErr;
  1621. StringRef Name;
  1622. if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
  1623. Section->getName(Name);
  1624. } else {
  1625. Expected<StringRef> NameOrErr = Symbol.getName();
  1626. if (!NameOrErr)
  1627. report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
  1628. ArchitectureName);
  1629. Name = *NameOrErr;
  1630. }
  1631. bool Global = Flags & SymbolRef::SF_Global;
  1632. bool Weak = Flags & SymbolRef::SF_Weak;
  1633. bool Absolute = Flags & SymbolRef::SF_Absolute;
  1634. bool Common = Flags & SymbolRef::SF_Common;
  1635. bool Hidden = Flags & SymbolRef::SF_Hidden;
  1636. char GlobLoc = ' ';
  1637. if (Type != SymbolRef::ST_Unknown)
  1638. GlobLoc = Global ? 'g' : 'l';
  1639. char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
  1640. ? 'd' : ' ';
  1641. char FileFunc = ' ';
  1642. if (Type == SymbolRef::ST_File)
  1643. FileFunc = 'f';
  1644. else if (Type == SymbolRef::ST_Function)
  1645. FileFunc = 'F';
  1646. const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
  1647. "%08" PRIx64;
  1648. outs() << format(Fmt, Address) << " "
  1649. << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
  1650. << (Weak ? 'w' : ' ') // Weak?
  1651. << ' ' // Constructor. Not supported yet.
  1652. << ' ' // Warning. Not supported yet.
  1653. << ' ' // Indirect reference to another symbol.
  1654. << Debug // Debugging (d) or dynamic (D) symbol.
  1655. << FileFunc // Name of function (F), file (f) or object (O).
  1656. << ' ';
  1657. if (Absolute) {
  1658. outs() << "*ABS*";
  1659. } else if (Common) {
  1660. outs() << "*COM*";
  1661. } else if (Section == o->section_end()) {
  1662. outs() << "*UND*";
  1663. } else {
  1664. if (const MachOObjectFile *MachO =
  1665. dyn_cast<const MachOObjectFile>(o)) {
  1666. DataRefImpl DR = Section->getRawDataRefImpl();
  1667. StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
  1668. outs() << SegmentName << ",";
  1669. }
  1670. StringRef SectionName;
  1671. error(Section->getName(SectionName));
  1672. outs() << SectionName;
  1673. }
  1674. outs() << '\t';
  1675. if (Common || isa<ELFObjectFileBase>(o)) {
  1676. uint64_t Val =
  1677. Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
  1678. outs() << format("\t %08" PRIx64 " ", Val);
  1679. }
  1680. if (Hidden) {
  1681. outs() << ".hidden ";
  1682. }
  1683. outs() << Name
  1684. << '\n';
  1685. }
  1686. }
  1687. static void PrintUnwindInfo(const ObjectFile *o) {
  1688. outs() << "Unwind info:\n\n";
  1689. if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
  1690. printCOFFUnwindInfo(coff);
  1691. } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1692. printMachOUnwindInfo(MachO);
  1693. else {
  1694. // TODO: Extract DWARF dump tool to objdump.
  1695. errs() << "This operation is only currently supported "
  1696. "for COFF and MachO object files.\n";
  1697. return;
  1698. }
  1699. }
  1700. void llvm::printExportsTrie(const ObjectFile *o) {
  1701. outs() << "Exports trie:\n";
  1702. if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1703. printMachOExportsTrie(MachO);
  1704. else {
  1705. errs() << "This operation is only currently supported "
  1706. "for Mach-O executable files.\n";
  1707. return;
  1708. }
  1709. }
  1710. void llvm::printRebaseTable(ObjectFile *o) {
  1711. outs() << "Rebase table:\n";
  1712. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1713. printMachORebaseTable(MachO);
  1714. else {
  1715. errs() << "This operation is only currently supported "
  1716. "for Mach-O executable files.\n";
  1717. return;
  1718. }
  1719. }
  1720. void llvm::printBindTable(ObjectFile *o) {
  1721. outs() << "Bind table:\n";
  1722. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1723. printMachOBindTable(MachO);
  1724. else {
  1725. errs() << "This operation is only currently supported "
  1726. "for Mach-O executable files.\n";
  1727. return;
  1728. }
  1729. }
  1730. void llvm::printLazyBindTable(ObjectFile *o) {
  1731. outs() << "Lazy bind table:\n";
  1732. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1733. printMachOLazyBindTable(MachO);
  1734. else {
  1735. errs() << "This operation is only currently supported "
  1736. "for Mach-O executable files.\n";
  1737. return;
  1738. }
  1739. }
  1740. void llvm::printWeakBindTable(ObjectFile *o) {
  1741. outs() << "Weak bind table:\n";
  1742. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  1743. printMachOWeakBindTable(MachO);
  1744. else {
  1745. errs() << "This operation is only currently supported "
  1746. "for Mach-O executable files.\n";
  1747. return;
  1748. }
  1749. }
  1750. /// Dump the raw contents of the __clangast section so the output can be piped
  1751. /// into llvm-bcanalyzer.
  1752. void llvm::printRawClangAST(const ObjectFile *Obj) {
  1753. if (outs().is_displayed()) {
  1754. errs() << "The -raw-clang-ast option will dump the raw binary contents of "
  1755. "the clang ast section.\n"
  1756. "Please redirect the output to a file or another program such as "
  1757. "llvm-bcanalyzer.\n";
  1758. return;
  1759. }
  1760. StringRef ClangASTSectionName("__clangast");
  1761. if (isa<COFFObjectFile>(Obj)) {
  1762. ClangASTSectionName = "clangast";
  1763. }
  1764. Optional<object::SectionRef> ClangASTSection;
  1765. for (auto Sec : ToolSectionFilter(*Obj)) {
  1766. StringRef Name;
  1767. Sec.getName(Name);
  1768. if (Name == ClangASTSectionName) {
  1769. ClangASTSection = Sec;
  1770. break;
  1771. }
  1772. }
  1773. if (!ClangASTSection)
  1774. return;
  1775. StringRef ClangASTContents;
  1776. error(ClangASTSection.getValue().getContents(ClangASTContents));
  1777. outs().write(ClangASTContents.data(), ClangASTContents.size());
  1778. }
  1779. static void printFaultMaps(const ObjectFile *Obj) {
  1780. const char *FaultMapSectionName = nullptr;
  1781. if (isa<ELFObjectFileBase>(Obj)) {
  1782. FaultMapSectionName = ".llvm_faultmaps";
  1783. } else if (isa<MachOObjectFile>(Obj)) {
  1784. FaultMapSectionName = "__llvm_faultmaps";
  1785. } else {
  1786. errs() << "This operation is only currently supported "
  1787. "for ELF and Mach-O executable files.\n";
  1788. return;
  1789. }
  1790. Optional<object::SectionRef> FaultMapSection;
  1791. for (auto Sec : ToolSectionFilter(*Obj)) {
  1792. StringRef Name;
  1793. Sec.getName(Name);
  1794. if (Name == FaultMapSectionName) {
  1795. FaultMapSection = Sec;
  1796. break;
  1797. }
  1798. }
  1799. outs() << "FaultMap table:\n";
  1800. if (!FaultMapSection.hasValue()) {
  1801. outs() << "<not found>\n";
  1802. return;
  1803. }
  1804. StringRef FaultMapContents;
  1805. error(FaultMapSection.getValue().getContents(FaultMapContents));
  1806. FaultMapParser FMP(FaultMapContents.bytes_begin(),
  1807. FaultMapContents.bytes_end());
  1808. outs() << FMP;
  1809. }
  1810. static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) {
  1811. if (o->isELF())
  1812. return printELFFileHeader(o);
  1813. if (o->isCOFF())
  1814. return printCOFFFileHeader(o);
  1815. if (o->isWasm())
  1816. return printWasmFileHeader(o);
  1817. if (o->isMachO()) {
  1818. printMachOFileHeader(o);
  1819. if (!onlyFirst)
  1820. printMachOLoadCommands(o);
  1821. return;
  1822. }
  1823. report_error(o->getFileName(), "Invalid/Unsupported object file format");
  1824. }
  1825. static void DumpObject(ObjectFile *o, const Archive *a = nullptr) {
  1826. StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
  1827. // Avoid other output when using a raw option.
  1828. if (!RawClangAST) {
  1829. outs() << '\n';
  1830. if (a)
  1831. outs() << a->getFileName() << "(" << o->getFileName() << ")";
  1832. else
  1833. outs() << o->getFileName();
  1834. outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
  1835. }
  1836. if (Disassemble)
  1837. DisassembleObject(o, Relocations);
  1838. if (Relocations && !Disassemble)
  1839. PrintRelocations(o);
  1840. if (SectionHeaders)
  1841. PrintSectionHeaders(o);
  1842. if (SectionContents)
  1843. PrintSectionContents(o);
  1844. if (SymbolTable)
  1845. PrintSymbolTable(o, ArchiveName);
  1846. if (UnwindInfo)
  1847. PrintUnwindInfo(o);
  1848. if (PrivateHeaders || FirstPrivateHeader)
  1849. printPrivateFileHeaders(o, FirstPrivateHeader);
  1850. if (ExportsTrie)
  1851. printExportsTrie(o);
  1852. if (Rebase)
  1853. printRebaseTable(o);
  1854. if (Bind)
  1855. printBindTable(o);
  1856. if (LazyBind)
  1857. printLazyBindTable(o);
  1858. if (WeakBind)
  1859. printWeakBindTable(o);
  1860. if (RawClangAST)
  1861. printRawClangAST(o);
  1862. if (PrintFaultMaps)
  1863. printFaultMaps(o);
  1864. if (DwarfDumpType != DIDT_Null) {
  1865. std::unique_ptr<DIContext> DICtx = DWARFContext::create(*o);
  1866. // Dump the complete DWARF structure.
  1867. DIDumpOptions DumpOpts;
  1868. DumpOpts.DumpType = DwarfDumpType;
  1869. DICtx->dump(outs(), DumpOpts);
  1870. }
  1871. }
  1872. static void DumpObject(const COFFImportFile *I, const Archive *A) {
  1873. StringRef ArchiveName = A ? A->getFileName() : "";
  1874. // Avoid other output when using a raw option.
  1875. if (!RawClangAST)
  1876. outs() << '\n'
  1877. << ArchiveName << "(" << I->getFileName() << ")"
  1878. << ":\tfile format COFF-import-file"
  1879. << "\n\n";
  1880. if (SymbolTable)
  1881. printCOFFSymbolTable(I);
  1882. }
  1883. /// @brief Dump each object file in \a a;
  1884. static void DumpArchive(const Archive *a) {
  1885. Error Err = Error::success();
  1886. for (auto &C : a->children(Err)) {
  1887. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  1888. if (!ChildOrErr) {
  1889. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1890. report_error(a->getFileName(), C, std::move(E));
  1891. continue;
  1892. }
  1893. if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  1894. DumpObject(o, a);
  1895. else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
  1896. DumpObject(I, a);
  1897. else
  1898. report_error(a->getFileName(), object_error::invalid_file_type);
  1899. }
  1900. if (Err)
  1901. report_error(a->getFileName(), std::move(Err));
  1902. }
  1903. /// @brief Open file and figure out how to dump it.
  1904. static void DumpInput(StringRef file) {
  1905. // If we are using the Mach-O specific object file parser, then let it parse
  1906. // the file and process the command line options. So the -arch flags can
  1907. // be used to select specific slices, etc.
  1908. if (MachOOpt) {
  1909. ParseInputMachO(file);
  1910. return;
  1911. }
  1912. // Attempt to open the binary.
  1913. Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
  1914. if (!BinaryOrErr)
  1915. report_error(file, BinaryOrErr.takeError());
  1916. Binary &Binary = *BinaryOrErr.get().getBinary();
  1917. if (Archive *a = dyn_cast<Archive>(&Binary))
  1918. DumpArchive(a);
  1919. else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
  1920. DumpObject(o);
  1921. else
  1922. report_error(file, object_error::invalid_file_type);
  1923. }
  1924. int main(int argc, char **argv) {
  1925. // Print a stack trace if we signal out.
  1926. sys::PrintStackTraceOnErrorSignal(argv[0]);
  1927. PrettyStackTraceProgram X(argc, argv);
  1928. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  1929. // Initialize targets and assembly printers/parsers.
  1930. llvm::InitializeAllTargetInfos();
  1931. llvm::InitializeAllTargetMCs();
  1932. llvm::InitializeAllDisassemblers();
  1933. // Register the target printer for --version.
  1934. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  1935. cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
  1936. TripleName = Triple::normalize(TripleName);
  1937. ToolName = argv[0];
  1938. // Defaults to a.out if no filenames specified.
  1939. if (InputFilenames.size() == 0)
  1940. InputFilenames.push_back("a.out");
  1941. if (DisassembleAll || PrintSource || PrintLines)
  1942. Disassemble = true;
  1943. if (!Disassemble
  1944. && !Relocations
  1945. && !SectionHeaders
  1946. && !SectionContents
  1947. && !SymbolTable
  1948. && !UnwindInfo
  1949. && !PrivateHeaders
  1950. && !FirstPrivateHeader
  1951. && !ExportsTrie
  1952. && !Rebase
  1953. && !Bind
  1954. && !LazyBind
  1955. && !WeakBind
  1956. && !RawClangAST
  1957. && !(UniversalHeaders && MachOOpt)
  1958. && !(ArchiveHeaders && MachOOpt)
  1959. && !(IndirectSymbols && MachOOpt)
  1960. && !(DataInCode && MachOOpt)
  1961. && !(LinkOptHints && MachOOpt)
  1962. && !(InfoPlist && MachOOpt)
  1963. && !(DylibsUsed && MachOOpt)
  1964. && !(DylibId && MachOOpt)
  1965. && !(ObjcMetaData && MachOOpt)
  1966. && !(FilterSections.size() != 0 && MachOOpt)
  1967. && !PrintFaultMaps
  1968. && DwarfDumpType == DIDT_Null) {
  1969. cl::PrintHelpMessage();
  1970. return 2;
  1971. }
  1972. llvm::for_each(InputFilenames, DumpInput);
  1973. return EXIT_SUCCESS;
  1974. }