llvm-objdump.cpp 85 KB

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