llvm-objdump.cpp 72 KB

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