llvm-objdump.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  1. //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This program is a utility that works like binutils "objdump", that is, it
  10. // dumps out a plethora of information about an object file depending on the
  11. // flags.
  12. //
  13. // The flags and output of this program should be near identical to those of
  14. // binutils objdump.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm-objdump.h"
  18. #include "COFFDump.h"
  19. #include "ELFDump.h"
  20. #include "MachODump.h"
  21. #include "WasmDump.h"
  22. #include "XCOFFDump.h"
  23. #include "llvm/ADT/IndexedMap.h"
  24. #include "llvm/ADT/Optional.h"
  25. #include "llvm/ADT/SmallSet.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SetOperations.h"
  28. #include "llvm/ADT/StringExtras.h"
  29. #include "llvm/ADT/StringSet.h"
  30. #include "llvm/ADT/Triple.h"
  31. #include "llvm/ADT/Twine.h"
  32. #include "llvm/CodeGen/FaultMaps.h"
  33. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  34. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  35. #include "llvm/Demangle/Demangle.h"
  36. #include "llvm/MC/MCAsmInfo.h"
  37. #include "llvm/MC/MCContext.h"
  38. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  39. #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
  40. #include "llvm/MC/MCInst.h"
  41. #include "llvm/MC/MCInstPrinter.h"
  42. #include "llvm/MC/MCInstrAnalysis.h"
  43. #include "llvm/MC/MCInstrInfo.h"
  44. #include "llvm/MC/MCObjectFileInfo.h"
  45. #include "llvm/MC/MCRegisterInfo.h"
  46. #include "llvm/MC/MCSubtargetInfo.h"
  47. #include "llvm/MC/MCTargetOptions.h"
  48. #include "llvm/Object/Archive.h"
  49. #include "llvm/Object/COFF.h"
  50. #include "llvm/Object/COFFImportFile.h"
  51. #include "llvm/Object/ELFObjectFile.h"
  52. #include "llvm/Object/MachO.h"
  53. #include "llvm/Object/MachOUniversal.h"
  54. #include "llvm/Object/ObjectFile.h"
  55. #include "llvm/Object/Wasm.h"
  56. #include "llvm/Support/Casting.h"
  57. #include "llvm/Support/CommandLine.h"
  58. #include "llvm/Support/Debug.h"
  59. #include "llvm/Support/Errc.h"
  60. #include "llvm/Support/FileSystem.h"
  61. #include "llvm/Support/Format.h"
  62. #include "llvm/Support/FormatVariadic.h"
  63. #include "llvm/Support/GraphWriter.h"
  64. #include "llvm/Support/Host.h"
  65. #include "llvm/Support/InitLLVM.h"
  66. #include "llvm/Support/MemoryBuffer.h"
  67. #include "llvm/Support/SourceMgr.h"
  68. #include "llvm/Support/StringSaver.h"
  69. #include "llvm/Support/TargetRegistry.h"
  70. #include "llvm/Support/TargetSelect.h"
  71. #include "llvm/Support/WithColor.h"
  72. #include "llvm/Support/raw_ostream.h"
  73. #include <algorithm>
  74. #include <cctype>
  75. #include <cstring>
  76. #include <system_error>
  77. #include <unordered_map>
  78. #include <utility>
  79. using namespace llvm;
  80. using namespace llvm::object;
  81. using namespace llvm::objdump;
  82. #define DEBUG_TYPE "objdump"
  83. static cl::OptionCategory ObjdumpCat("llvm-objdump Options");
  84. static cl::opt<uint64_t> AdjustVMA(
  85. "adjust-vma",
  86. cl::desc("Increase the displayed address by the specified offset"),
  87. cl::value_desc("offset"), cl::init(0), cl::cat(ObjdumpCat));
  88. static cl::opt<bool>
  89. AllHeaders("all-headers",
  90. cl::desc("Display all available header information"),
  91. cl::cat(ObjdumpCat));
  92. static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"),
  93. cl::NotHidden, cl::Grouping,
  94. cl::aliasopt(AllHeaders));
  95. static cl::opt<std::string>
  96. ArchName("arch-name",
  97. cl::desc("Target arch to disassemble for, "
  98. "see --version for available targets"),
  99. cl::cat(ObjdumpCat));
  100. cl::opt<bool>
  101. objdump::ArchiveHeaders("archive-headers",
  102. cl::desc("Display archive header information"),
  103. cl::cat(ObjdumpCat));
  104. static cl::alias ArchiveHeadersShort("a",
  105. cl::desc("Alias for --archive-headers"),
  106. cl::NotHidden, cl::Grouping,
  107. cl::aliasopt(ArchiveHeaders));
  108. cl::opt<bool> objdump::Demangle("demangle", cl::desc("Demangle symbols names"),
  109. cl::init(false), cl::cat(ObjdumpCat));
  110. static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"),
  111. cl::NotHidden, cl::Grouping,
  112. cl::aliasopt(Demangle));
  113. cl::opt<bool> objdump::Disassemble(
  114. "disassemble",
  115. cl::desc("Display assembler mnemonics for the machine instructions"),
  116. cl::cat(ObjdumpCat));
  117. static cl::alias DisassembleShort("d", cl::desc("Alias for --disassemble"),
  118. cl::NotHidden, cl::Grouping,
  119. cl::aliasopt(Disassemble));
  120. cl::opt<bool> objdump::DisassembleAll(
  121. "disassemble-all",
  122. cl::desc("Display assembler mnemonics for the machine instructions"),
  123. cl::cat(ObjdumpCat));
  124. static cl::alias DisassembleAllShort("D",
  125. cl::desc("Alias for --disassemble-all"),
  126. cl::NotHidden, cl::Grouping,
  127. cl::aliasopt(DisassembleAll));
  128. cl::opt<bool> objdump::SymbolDescription(
  129. "symbol-description",
  130. cl::desc("Add symbol description for disassembly. This "
  131. "option is for XCOFF files only"),
  132. cl::init(false), cl::cat(ObjdumpCat));
  133. static cl::list<std::string>
  134. DisassembleSymbols("disassemble-symbols", cl::CommaSeparated,
  135. cl::desc("List of symbols to disassemble. "
  136. "Accept demangled names when --demangle is "
  137. "specified, otherwise accept mangled names"),
  138. cl::cat(ObjdumpCat));
  139. static cl::opt<bool> DisassembleZeroes(
  140. "disassemble-zeroes",
  141. cl::desc("Do not skip blocks of zeroes when disassembling"),
  142. cl::cat(ObjdumpCat));
  143. static cl::alias
  144. DisassembleZeroesShort("z", cl::desc("Alias for --disassemble-zeroes"),
  145. cl::NotHidden, cl::Grouping,
  146. cl::aliasopt(DisassembleZeroes));
  147. static cl::list<std::string>
  148. DisassemblerOptions("disassembler-options",
  149. cl::desc("Pass target specific disassembler options"),
  150. cl::value_desc("options"), cl::CommaSeparated,
  151. cl::cat(ObjdumpCat));
  152. static cl::alias
  153. DisassemblerOptionsShort("M", cl::desc("Alias for --disassembler-options"),
  154. cl::NotHidden, cl::Grouping, cl::Prefix,
  155. cl::CommaSeparated,
  156. cl::aliasopt(DisassemblerOptions));
  157. cl::opt<DIDumpType> objdump::DwarfDumpType(
  158. "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
  159. cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame")),
  160. cl::cat(ObjdumpCat));
  161. static cl::opt<bool> DynamicRelocations(
  162. "dynamic-reloc",
  163. cl::desc("Display the dynamic relocation entries in the file"),
  164. cl::cat(ObjdumpCat));
  165. static cl::alias DynamicRelocationShort("R",
  166. cl::desc("Alias for --dynamic-reloc"),
  167. cl::NotHidden, cl::Grouping,
  168. cl::aliasopt(DynamicRelocations));
  169. static cl::opt<bool>
  170. FaultMapSection("fault-map-section",
  171. cl::desc("Display contents of faultmap section"),
  172. cl::cat(ObjdumpCat));
  173. static cl::opt<bool>
  174. FileHeaders("file-headers",
  175. cl::desc("Display the contents of the overall file header"),
  176. cl::cat(ObjdumpCat));
  177. static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"),
  178. cl::NotHidden, cl::Grouping,
  179. cl::aliasopt(FileHeaders));
  180. cl::opt<bool>
  181. objdump::SectionContents("full-contents",
  182. cl::desc("Display the content of each section"),
  183. cl::cat(ObjdumpCat));
  184. static cl::alias SectionContentsShort("s",
  185. cl::desc("Alias for --full-contents"),
  186. cl::NotHidden, cl::Grouping,
  187. cl::aliasopt(SectionContents));
  188. static cl::list<std::string> InputFilenames(cl::Positional,
  189. cl::desc("<input object files>"),
  190. cl::ZeroOrMore,
  191. cl::cat(ObjdumpCat));
  192. static cl::opt<bool>
  193. PrintLines("line-numbers",
  194. cl::desc("Display source line numbers with "
  195. "disassembly. Implies disassemble object"),
  196. cl::cat(ObjdumpCat));
  197. static cl::alias PrintLinesShort("l", cl::desc("Alias for --line-numbers"),
  198. cl::NotHidden, cl::Grouping,
  199. cl::aliasopt(PrintLines));
  200. static cl::opt<bool> MachOOpt("macho",
  201. cl::desc("Use MachO specific object file parser"),
  202. cl::cat(ObjdumpCat));
  203. static cl::alias MachOm("m", cl::desc("Alias for --macho"), cl::NotHidden,
  204. cl::Grouping, cl::aliasopt(MachOOpt));
  205. cl::opt<std::string> objdump::MCPU(
  206. "mcpu", cl::desc("Target a specific cpu type (--mcpu=help for details)"),
  207. cl::value_desc("cpu-name"), cl::init(""), cl::cat(ObjdumpCat));
  208. cl::list<std::string> objdump::MAttrs(
  209. "mattr", cl::CommaSeparated,
  210. cl::desc("Target specific attributes (--mattr=help for details)"),
  211. cl::value_desc("a1,+a2,-a3,..."), cl::cat(ObjdumpCat));
  212. cl::opt<bool> objdump::NoShowRawInsn(
  213. "no-show-raw-insn",
  214. cl::desc(
  215. "When disassembling instructions, do not print the instruction bytes."),
  216. cl::cat(ObjdumpCat));
  217. cl::opt<bool> objdump::NoLeadingAddr("no-leading-addr",
  218. cl::desc("Print no leading address"),
  219. cl::cat(ObjdumpCat));
  220. static cl::opt<bool> RawClangAST(
  221. "raw-clang-ast",
  222. cl::desc("Dump the raw binary contents of the clang AST section"),
  223. cl::cat(ObjdumpCat));
  224. cl::opt<bool>
  225. objdump::Relocations("reloc",
  226. cl::desc("Display the relocation entries in the file"),
  227. cl::cat(ObjdumpCat));
  228. static cl::alias RelocationsShort("r", cl::desc("Alias for --reloc"),
  229. cl::NotHidden, cl::Grouping,
  230. cl::aliasopt(Relocations));
  231. cl::opt<bool>
  232. objdump::PrintImmHex("print-imm-hex",
  233. cl::desc("Use hex format for immediate values"),
  234. cl::cat(ObjdumpCat));
  235. cl::opt<bool>
  236. objdump::PrivateHeaders("private-headers",
  237. cl::desc("Display format specific file headers"),
  238. cl::cat(ObjdumpCat));
  239. static cl::alias PrivateHeadersShort("p",
  240. cl::desc("Alias for --private-headers"),
  241. cl::NotHidden, cl::Grouping,
  242. cl::aliasopt(PrivateHeaders));
  243. cl::list<std::string>
  244. objdump::FilterSections("section",
  245. cl::desc("Operate on the specified sections only. "
  246. "With --macho dump segment,section"),
  247. cl::cat(ObjdumpCat));
  248. static cl::alias FilterSectionsj("j", cl::desc("Alias for --section"),
  249. cl::NotHidden, cl::Grouping, cl::Prefix,
  250. cl::aliasopt(FilterSections));
  251. cl::opt<bool> objdump::SectionHeaders(
  252. "section-headers",
  253. cl::desc("Display summaries of the headers for each section."),
  254. cl::cat(ObjdumpCat));
  255. static cl::alias SectionHeadersShort("headers",
  256. cl::desc("Alias for --section-headers"),
  257. cl::NotHidden,
  258. cl::aliasopt(SectionHeaders));
  259. static cl::alias SectionHeadersShorter("h",
  260. cl::desc("Alias for --section-headers"),
  261. cl::NotHidden, cl::Grouping,
  262. cl::aliasopt(SectionHeaders));
  263. static cl::opt<bool>
  264. ShowLMA("show-lma",
  265. cl::desc("Display LMA column when dumping ELF section headers"),
  266. cl::cat(ObjdumpCat));
  267. static cl::opt<bool> PrintSource(
  268. "source",
  269. cl::desc(
  270. "Display source inlined with disassembly. Implies disassemble object"),
  271. cl::cat(ObjdumpCat));
  272. static cl::alias PrintSourceShort("S", cl::desc("Alias for --source"),
  273. cl::NotHidden, cl::Grouping,
  274. cl::aliasopt(PrintSource));
  275. static cl::opt<uint64_t>
  276. StartAddress("start-address", cl::desc("Disassemble beginning at address"),
  277. cl::value_desc("address"), cl::init(0), cl::cat(ObjdumpCat));
  278. static cl::opt<uint64_t> StopAddress("stop-address",
  279. cl::desc("Stop disassembly at address"),
  280. cl::value_desc("address"),
  281. cl::init(UINT64_MAX), cl::cat(ObjdumpCat));
  282. cl::opt<bool> objdump::SymbolTable("syms", cl::desc("Display the symbol table"),
  283. cl::cat(ObjdumpCat));
  284. static cl::alias SymbolTableShort("t", cl::desc("Alias for --syms"),
  285. cl::NotHidden, cl::Grouping,
  286. cl::aliasopt(SymbolTable));
  287. static cl::opt<bool> SymbolizeOperands(
  288. "symbolize-operands",
  289. cl::desc("Symbolize instruction operands when disassembling"),
  290. cl::cat(ObjdumpCat));
  291. static cl::opt<bool> DynamicSymbolTable(
  292. "dynamic-syms",
  293. cl::desc("Display the contents of the dynamic symbol table"),
  294. cl::cat(ObjdumpCat));
  295. static cl::alias DynamicSymbolTableShort("T",
  296. cl::desc("Alias for --dynamic-syms"),
  297. cl::NotHidden, cl::Grouping,
  298. cl::aliasopt(DynamicSymbolTable));
  299. cl::opt<std::string>
  300. objdump::TripleName("triple",
  301. cl::desc("Target triple to disassemble for, see "
  302. "--version for available targets"),
  303. cl::cat(ObjdumpCat));
  304. cl::opt<bool> objdump::UnwindInfo("unwind-info",
  305. cl::desc("Display unwind information"),
  306. cl::cat(ObjdumpCat));
  307. static cl::alias UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
  308. cl::NotHidden, cl::Grouping,
  309. cl::aliasopt(UnwindInfo));
  310. static cl::opt<bool>
  311. Wide("wide", cl::desc("Ignored for compatibility with GNU objdump"),
  312. cl::cat(ObjdumpCat));
  313. static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide));
  314. cl::opt<std::string> objdump::Prefix("prefix",
  315. cl::desc("Add prefix to absolute paths"),
  316. cl::cat(ObjdumpCat));
  317. enum DebugVarsFormat {
  318. DVDisabled,
  319. DVUnicode,
  320. DVASCII,
  321. };
  322. static cl::opt<DebugVarsFormat> DbgVariables(
  323. "debug-vars", cl::init(DVDisabled),
  324. cl::desc("Print the locations (in registers or memory) of "
  325. "source-level variables alongside disassembly"),
  326. cl::ValueOptional,
  327. cl::values(clEnumValN(DVUnicode, "", "unicode"),
  328. clEnumValN(DVUnicode, "unicode", "unicode"),
  329. clEnumValN(DVASCII, "ascii", "unicode")),
  330. cl::cat(ObjdumpCat));
  331. static cl::opt<int>
  332. DbgIndent("debug-vars-indent", cl::init(40),
  333. cl::desc("Distance to indent the source-level variable display, "
  334. "relative to the start of the disassembly"),
  335. cl::cat(ObjdumpCat));
  336. static cl::extrahelp
  337. HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
  338. static StringSet<> DisasmSymbolSet;
  339. StringSet<> objdump::FoundSectionSet;
  340. static StringRef ToolName;
  341. namespace {
  342. struct FilterResult {
  343. // True if the section should not be skipped.
  344. bool Keep;
  345. // True if the index counter should be incremented, even if the section should
  346. // be skipped. For example, sections may be skipped if they are not included
  347. // in the --section flag, but we still want those to count toward the section
  348. // count.
  349. bool IncrementIndex;
  350. };
  351. } // namespace
  352. static FilterResult checkSectionFilter(object::SectionRef S) {
  353. if (FilterSections.empty())
  354. return {/*Keep=*/true, /*IncrementIndex=*/true};
  355. Expected<StringRef> SecNameOrErr = S.getName();
  356. if (!SecNameOrErr) {
  357. consumeError(SecNameOrErr.takeError());
  358. return {/*Keep=*/false, /*IncrementIndex=*/false};
  359. }
  360. StringRef SecName = *SecNameOrErr;
  361. // StringSet does not allow empty key so avoid adding sections with
  362. // no name (such as the section with index 0) here.
  363. if (!SecName.empty())
  364. FoundSectionSet.insert(SecName);
  365. // Only show the section if it's in the FilterSections list, but always
  366. // increment so the indexing is stable.
  367. return {/*Keep=*/is_contained(FilterSections, SecName),
  368. /*IncrementIndex=*/true};
  369. }
  370. SectionFilter objdump::ToolSectionFilter(object::ObjectFile const &O,
  371. uint64_t *Idx) {
  372. // Start at UINT64_MAX so that the first index returned after an increment is
  373. // zero (after the unsigned wrap).
  374. if (Idx)
  375. *Idx = UINT64_MAX;
  376. return SectionFilter(
  377. [Idx](object::SectionRef S) {
  378. FilterResult Result = checkSectionFilter(S);
  379. if (Idx != nullptr && Result.IncrementIndex)
  380. *Idx += 1;
  381. return Result.Keep;
  382. },
  383. O);
  384. }
  385. std::string objdump::getFileNameForError(const object::Archive::Child &C,
  386. unsigned Index) {
  387. Expected<StringRef> NameOrErr = C.getName();
  388. if (NameOrErr)
  389. return std::string(NameOrErr.get());
  390. // If we have an error getting the name then we print the index of the archive
  391. // member. Since we are already in an error state, we just ignore this error.
  392. consumeError(NameOrErr.takeError());
  393. return "<file index: " + std::to_string(Index) + ">";
  394. }
  395. void objdump::reportWarning(const Twine &Message, StringRef File) {
  396. // Output order between errs() and outs() matters especially for archive
  397. // files where the output is per member object.
  398. outs().flush();
  399. WithColor::warning(errs(), ToolName)
  400. << "'" << File << "': " << Message << "\n";
  401. }
  402. LLVM_ATTRIBUTE_NORETURN void objdump::reportError(StringRef File,
  403. const Twine &Message) {
  404. outs().flush();
  405. WithColor::error(errs(), ToolName) << "'" << File << "': " << Message << "\n";
  406. exit(1);
  407. }
  408. LLVM_ATTRIBUTE_NORETURN void objdump::reportError(Error E, StringRef FileName,
  409. StringRef ArchiveName,
  410. StringRef ArchitectureName) {
  411. assert(E);
  412. outs().flush();
  413. WithColor::error(errs(), ToolName);
  414. if (ArchiveName != "")
  415. errs() << ArchiveName << "(" << FileName << ")";
  416. else
  417. errs() << "'" << FileName << "'";
  418. if (!ArchitectureName.empty())
  419. errs() << " (for architecture " << ArchitectureName << ")";
  420. errs() << ": ";
  421. logAllUnhandledErrors(std::move(E), errs());
  422. exit(1);
  423. }
  424. static void reportCmdLineWarning(const Twine &Message) {
  425. WithColor::warning(errs(), ToolName) << Message << "\n";
  426. }
  427. LLVM_ATTRIBUTE_NORETURN static void reportCmdLineError(const Twine &Message) {
  428. WithColor::error(errs(), ToolName) << Message << "\n";
  429. exit(1);
  430. }
  431. static void warnOnNoMatchForSections() {
  432. SetVector<StringRef> MissingSections;
  433. for (StringRef S : FilterSections) {
  434. if (FoundSectionSet.count(S))
  435. return;
  436. // User may specify a unnamed section. Don't warn for it.
  437. if (!S.empty())
  438. MissingSections.insert(S);
  439. }
  440. // Warn only if no section in FilterSections is matched.
  441. for (StringRef S : MissingSections)
  442. reportCmdLineWarning("section '" + S +
  443. "' mentioned in a -j/--section option, but not "
  444. "found in any input file");
  445. }
  446. static const Target *getTarget(const ObjectFile *Obj) {
  447. // Figure out the target triple.
  448. Triple TheTriple("unknown-unknown-unknown");
  449. if (TripleName.empty()) {
  450. TheTriple = Obj->makeTriple();
  451. } else {
  452. TheTriple.setTriple(Triple::normalize(TripleName));
  453. auto Arch = Obj->getArch();
  454. if (Arch == Triple::arm || Arch == Triple::armeb)
  455. Obj->setARMSubArch(TheTriple);
  456. }
  457. // Get the target specific parser.
  458. std::string Error;
  459. const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
  460. Error);
  461. if (!TheTarget)
  462. reportError(Obj->getFileName(), "can't find target: " + Error);
  463. // Update the triple name and return the found target.
  464. TripleName = TheTriple.getTriple();
  465. return TheTarget;
  466. }
  467. bool objdump::isRelocAddressLess(RelocationRef A, RelocationRef B) {
  468. return A.getOffset() < B.getOffset();
  469. }
  470. static Error getRelocationValueString(const RelocationRef &Rel,
  471. SmallVectorImpl<char> &Result) {
  472. const ObjectFile *Obj = Rel.getObject();
  473. if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
  474. return getELFRelocationValueString(ELF, Rel, Result);
  475. if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
  476. return getCOFFRelocationValueString(COFF, Rel, Result);
  477. if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
  478. return getWasmRelocationValueString(Wasm, Rel, Result);
  479. if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
  480. return getMachORelocationValueString(MachO, Rel, Result);
  481. if (auto *XCOFF = dyn_cast<XCOFFObjectFile>(Obj))
  482. return getXCOFFRelocationValueString(XCOFF, Rel, Result);
  483. llvm_unreachable("unknown object file format");
  484. }
  485. /// Indicates whether this relocation should hidden when listing
  486. /// relocations, usually because it is the trailing part of a multipart
  487. /// relocation that will be printed as part of the leading relocation.
  488. static bool getHidden(RelocationRef RelRef) {
  489. auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject());
  490. if (!MachO)
  491. return false;
  492. unsigned Arch = MachO->getArch();
  493. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  494. uint64_t Type = MachO->getRelocationType(Rel);
  495. // On arches that use the generic relocations, GENERIC_RELOC_PAIR
  496. // is always hidden.
  497. if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc)
  498. return Type == MachO::GENERIC_RELOC_PAIR;
  499. if (Arch == Triple::x86_64) {
  500. // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
  501. // an X86_64_RELOC_SUBTRACTOR.
  502. if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
  503. DataRefImpl RelPrev = Rel;
  504. RelPrev.d.a--;
  505. uint64_t PrevType = MachO->getRelocationType(RelPrev);
  506. if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. namespace {
  513. /// Get the column at which we want to start printing the instruction
  514. /// disassembly, taking into account anything which appears to the left of it.
  515. unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
  516. return NoShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
  517. }
  518. /// Stores a single expression representing the location of a source-level
  519. /// variable, along with the PC range for which that expression is valid.
  520. struct LiveVariable {
  521. DWARFLocationExpression LocExpr;
  522. const char *VarName;
  523. DWARFUnit *Unit;
  524. const DWARFDie FuncDie;
  525. LiveVariable(const DWARFLocationExpression &LocExpr, const char *VarName,
  526. DWARFUnit *Unit, const DWARFDie FuncDie)
  527. : LocExpr(LocExpr), VarName(VarName), Unit(Unit), FuncDie(FuncDie) {}
  528. bool liveAtAddress(object::SectionedAddress Addr) {
  529. if (LocExpr.Range == None)
  530. return false;
  531. return LocExpr.Range->SectionIndex == Addr.SectionIndex &&
  532. LocExpr.Range->LowPC <= Addr.Address &&
  533. LocExpr.Range->HighPC > Addr.Address;
  534. }
  535. void print(raw_ostream &OS, const MCRegisterInfo &MRI) const {
  536. DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()},
  537. Unit->getContext().isLittleEndian(), 0);
  538. DWARFExpression Expression(Data, Unit->getAddressByteSize());
  539. Expression.printCompact(OS, MRI);
  540. }
  541. };
  542. /// Helper class for printing source variable locations alongside disassembly.
  543. class LiveVariablePrinter {
  544. // Information we want to track about one column in which we are printing a
  545. // variable live range.
  546. struct Column {
  547. unsigned VarIdx = NullVarIdx;
  548. bool LiveIn = false;
  549. bool LiveOut = false;
  550. bool MustDrawLabel = false;
  551. bool isActive() const { return VarIdx != NullVarIdx; }
  552. static constexpr unsigned NullVarIdx = std::numeric_limits<unsigned>::max();
  553. };
  554. // All live variables we know about in the object/image file.
  555. std::vector<LiveVariable> LiveVariables;
  556. // The columns we are currently drawing.
  557. IndexedMap<Column> ActiveCols;
  558. const MCRegisterInfo &MRI;
  559. const MCSubtargetInfo &STI;
  560. void addVariable(DWARFDie FuncDie, DWARFDie VarDie) {
  561. uint64_t FuncLowPC, FuncHighPC, SectionIndex;
  562. FuncDie.getLowAndHighPC(FuncLowPC, FuncHighPC, SectionIndex);
  563. const char *VarName = VarDie.getName(DINameKind::ShortName);
  564. DWARFUnit *U = VarDie.getDwarfUnit();
  565. Expected<DWARFLocationExpressionsVector> Locs =
  566. VarDie.getLocations(dwarf::DW_AT_location);
  567. if (!Locs) {
  568. // If the variable doesn't have any locations, just ignore it. We don't
  569. // report an error or warning here as that could be noisy on optimised
  570. // code.
  571. consumeError(Locs.takeError());
  572. return;
  573. }
  574. for (const DWARFLocationExpression &LocExpr : *Locs) {
  575. if (LocExpr.Range) {
  576. LiveVariables.emplace_back(LocExpr, VarName, U, FuncDie);
  577. } else {
  578. // If the LocExpr does not have an associated range, it is valid for
  579. // the whole of the function.
  580. // TODO: technically it is not valid for any range covered by another
  581. // LocExpr, does that happen in reality?
  582. DWARFLocationExpression WholeFuncExpr{
  583. DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex),
  584. LocExpr.Expr};
  585. LiveVariables.emplace_back(WholeFuncExpr, VarName, U, FuncDie);
  586. }
  587. }
  588. }
  589. void addFunction(DWARFDie D) {
  590. for (const DWARFDie &Child : D.children()) {
  591. if (Child.getTag() == dwarf::DW_TAG_variable ||
  592. Child.getTag() == dwarf::DW_TAG_formal_parameter)
  593. addVariable(D, Child);
  594. else
  595. addFunction(Child);
  596. }
  597. }
  598. // Get the column number (in characters) at which the first live variable
  599. // line should be printed.
  600. unsigned getIndentLevel() const {
  601. return DbgIndent + getInstStartColumn(STI);
  602. }
  603. // Indent to the first live-range column to the right of the currently
  604. // printed line, and return the index of that column.
  605. // TODO: formatted_raw_ostream uses "column" to mean a number of characters
  606. // since the last \n, and we use it to mean the number of slots in which we
  607. // put live variable lines. Pick a less overloaded word.
  608. unsigned moveToFirstVarColumn(formatted_raw_ostream &OS) {
  609. // Logical column number: column zero is the first column we print in, each
  610. // logical column is 2 physical columns wide.
  611. unsigned FirstUnprintedLogicalColumn =
  612. std::max((int)(OS.getColumn() - getIndentLevel() + 1) / 2, 0);
  613. // Physical column number: the actual column number in characters, with
  614. // zero being the left-most side of the screen.
  615. unsigned FirstUnprintedPhysicalColumn =
  616. getIndentLevel() + FirstUnprintedLogicalColumn * 2;
  617. if (FirstUnprintedPhysicalColumn > OS.getColumn())
  618. OS.PadToColumn(FirstUnprintedPhysicalColumn);
  619. return FirstUnprintedLogicalColumn;
  620. }
  621. unsigned findFreeColumn() {
  622. for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)
  623. if (!ActiveCols[ColIdx].isActive())
  624. return ColIdx;
  625. size_t OldSize = ActiveCols.size();
  626. ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));
  627. return OldSize;
  628. }
  629. public:
  630. LiveVariablePrinter(const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
  631. : LiveVariables(), ActiveCols(Column()), MRI(MRI), STI(STI) {}
  632. void dump() const {
  633. for (const LiveVariable &LV : LiveVariables) {
  634. dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";
  635. LV.print(dbgs(), MRI);
  636. dbgs() << "\n";
  637. }
  638. }
  639. void addCompileUnit(DWARFDie D) {
  640. if (D.getTag() == dwarf::DW_TAG_subprogram)
  641. addFunction(D);
  642. else
  643. for (const DWARFDie &Child : D.children())
  644. addFunction(Child);
  645. }
  646. /// Update to match the state of the instruction between ThisAddr and
  647. /// NextAddr. In the common case, any live range active at ThisAddr is
  648. /// live-in to the instruction, and any live range active at NextAddr is
  649. /// live-out of the instruction. If IncludeDefinedVars is false, then live
  650. /// ranges starting at NextAddr will be ignored.
  651. void update(object::SectionedAddress ThisAddr,
  652. object::SectionedAddress NextAddr, bool IncludeDefinedVars) {
  653. // First, check variables which have already been assigned a column, so
  654. // that we don't change their order.
  655. SmallSet<unsigned, 8> CheckedVarIdxs;
  656. for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
  657. if (!ActiveCols[ColIdx].isActive())
  658. continue;
  659. CheckedVarIdxs.insert(ActiveCols[ColIdx].VarIdx);
  660. LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];
  661. ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(ThisAddr);
  662. ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(NextAddr);
  663. LLVM_DEBUG(dbgs() << "pass 1, " << ThisAddr.Address << "-"
  664. << NextAddr.Address << ", " << LV.VarName << ", Col "
  665. << ColIdx << ": LiveIn=" << ActiveCols[ColIdx].LiveIn
  666. << ", LiveOut=" << ActiveCols[ColIdx].LiveOut << "\n");
  667. if (!ActiveCols[ColIdx].LiveIn && !ActiveCols[ColIdx].LiveOut)
  668. ActiveCols[ColIdx].VarIdx = Column::NullVarIdx;
  669. }
  670. // Next, look for variables which don't already have a column, but which
  671. // are now live.
  672. if (IncludeDefinedVars) {
  673. for (unsigned VarIdx = 0, End = LiveVariables.size(); VarIdx < End;
  674. ++VarIdx) {
  675. if (CheckedVarIdxs.count(VarIdx))
  676. continue;
  677. LiveVariable &LV = LiveVariables[VarIdx];
  678. bool LiveIn = LV.liveAtAddress(ThisAddr);
  679. bool LiveOut = LV.liveAtAddress(NextAddr);
  680. if (!LiveIn && !LiveOut)
  681. continue;
  682. unsigned ColIdx = findFreeColumn();
  683. LLVM_DEBUG(dbgs() << "pass 2, " << ThisAddr.Address << "-"
  684. << NextAddr.Address << ", " << LV.VarName << ", Col "
  685. << ColIdx << ": LiveIn=" << LiveIn
  686. << ", LiveOut=" << LiveOut << "\n");
  687. ActiveCols[ColIdx].VarIdx = VarIdx;
  688. ActiveCols[ColIdx].LiveIn = LiveIn;
  689. ActiveCols[ColIdx].LiveOut = LiveOut;
  690. ActiveCols[ColIdx].MustDrawLabel = true;
  691. }
  692. }
  693. }
  694. enum class LineChar {
  695. RangeStart,
  696. RangeMid,
  697. RangeEnd,
  698. LabelVert,
  699. LabelCornerNew,
  700. LabelCornerActive,
  701. LabelHoriz,
  702. };
  703. const char *getLineChar(LineChar C) const {
  704. bool IsASCII = DbgVariables == DVASCII;
  705. switch (C) {
  706. case LineChar::RangeStart:
  707. return IsASCII ? "^" : (const char *)u8"\u2548";
  708. case LineChar::RangeMid:
  709. return IsASCII ? "|" : (const char *)u8"\u2503";
  710. case LineChar::RangeEnd:
  711. return IsASCII ? "v" : (const char *)u8"\u253b";
  712. case LineChar::LabelVert:
  713. return IsASCII ? "|" : (const char *)u8"\u2502";
  714. case LineChar::LabelCornerNew:
  715. return IsASCII ? "/" : (const char *)u8"\u250c";
  716. case LineChar::LabelCornerActive:
  717. return IsASCII ? "|" : (const char *)u8"\u2520";
  718. case LineChar::LabelHoriz:
  719. return IsASCII ? "-" : (const char *)u8"\u2500";
  720. }
  721. llvm_unreachable("Unhandled LineChar enum");
  722. }
  723. /// Print live ranges to the right of an existing line. This assumes the
  724. /// line is not an instruction, so doesn't start or end any live ranges, so
  725. /// we only need to print active ranges or empty columns. If AfterInst is
  726. /// true, this is being printed after the last instruction fed to update(),
  727. /// otherwise this is being printed before it.
  728. void printAfterOtherLine(formatted_raw_ostream &OS, bool AfterInst) {
  729. if (ActiveCols.size()) {
  730. unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
  731. for (size_t ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
  732. ColIdx < End; ++ColIdx) {
  733. if (ActiveCols[ColIdx].isActive()) {
  734. if ((AfterInst && ActiveCols[ColIdx].LiveOut) ||
  735. (!AfterInst && ActiveCols[ColIdx].LiveIn))
  736. OS << getLineChar(LineChar::RangeMid);
  737. else if (!AfterInst && ActiveCols[ColIdx].LiveOut)
  738. OS << getLineChar(LineChar::LabelVert);
  739. else
  740. OS << " ";
  741. }
  742. OS << " ";
  743. }
  744. }
  745. OS << "\n";
  746. }
  747. /// Print any live variable range info needed to the right of a
  748. /// non-instruction line of disassembly. This is where we print the variable
  749. /// names and expressions, with thin line-drawing characters connecting them
  750. /// to the live range which starts at the next instruction. If MustPrint is
  751. /// true, we have to print at least one line (with the continuation of any
  752. /// already-active live ranges) because something has already been printed
  753. /// earlier on this line.
  754. void printBetweenInsts(formatted_raw_ostream &OS, bool MustPrint) {
  755. bool PrintedSomething = false;
  756. for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
  757. if (ActiveCols[ColIdx].isActive() && ActiveCols[ColIdx].MustDrawLabel) {
  758. // First we need to print the live range markers for any active
  759. // columns to the left of this one.
  760. OS.PadToColumn(getIndentLevel());
  761. for (unsigned ColIdx2 = 0; ColIdx2 < ColIdx; ++ColIdx2) {
  762. if (ActiveCols[ColIdx2].isActive()) {
  763. if (ActiveCols[ColIdx2].MustDrawLabel &&
  764. !ActiveCols[ColIdx2].LiveIn)
  765. OS << getLineChar(LineChar::LabelVert) << " ";
  766. else
  767. OS << getLineChar(LineChar::RangeMid) << " ";
  768. } else
  769. OS << " ";
  770. }
  771. // Then print the variable name and location of the new live range,
  772. // with box drawing characters joining it to the live range line.
  773. OS << getLineChar(ActiveCols[ColIdx].LiveIn
  774. ? LineChar::LabelCornerActive
  775. : LineChar::LabelCornerNew)
  776. << getLineChar(LineChar::LabelHoriz) << " ";
  777. WithColor(OS, raw_ostream::GREEN)
  778. << LiveVariables[ActiveCols[ColIdx].VarIdx].VarName;
  779. OS << " = ";
  780. {
  781. WithColor ExprColor(OS, raw_ostream::CYAN);
  782. LiveVariables[ActiveCols[ColIdx].VarIdx].print(OS, MRI);
  783. }
  784. // If there are any columns to the right of the expression we just
  785. // printed, then continue their live range lines.
  786. unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
  787. for (unsigned ColIdx2 = FirstUnprintedColumn, End = ActiveCols.size();
  788. ColIdx2 < End; ++ColIdx2) {
  789. if (ActiveCols[ColIdx2].isActive() && ActiveCols[ColIdx2].LiveIn)
  790. OS << getLineChar(LineChar::RangeMid) << " ";
  791. else
  792. OS << " ";
  793. }
  794. OS << "\n";
  795. PrintedSomething = true;
  796. }
  797. }
  798. for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx)
  799. if (ActiveCols[ColIdx].isActive())
  800. ActiveCols[ColIdx].MustDrawLabel = false;
  801. // If we must print something (because we printed a line/column number),
  802. // but don't have any new variables to print, then print a line which
  803. // just continues any existing live ranges.
  804. if (MustPrint && !PrintedSomething)
  805. printAfterOtherLine(OS, false);
  806. }
  807. /// Print the live variable ranges to the right of a disassembled instruction.
  808. void printAfterInst(formatted_raw_ostream &OS) {
  809. if (!ActiveCols.size())
  810. return;
  811. unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
  812. for (unsigned ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
  813. ColIdx < End; ++ColIdx) {
  814. if (!ActiveCols[ColIdx].isActive())
  815. OS << " ";
  816. else if (ActiveCols[ColIdx].LiveIn && ActiveCols[ColIdx].LiveOut)
  817. OS << getLineChar(LineChar::RangeMid) << " ";
  818. else if (ActiveCols[ColIdx].LiveOut)
  819. OS << getLineChar(LineChar::RangeStart) << " ";
  820. else if (ActiveCols[ColIdx].LiveIn)
  821. OS << getLineChar(LineChar::RangeEnd) << " ";
  822. else
  823. llvm_unreachable("var must be live in or out!");
  824. }
  825. }
  826. };
  827. class SourcePrinter {
  828. protected:
  829. DILineInfo OldLineInfo;
  830. const ObjectFile *Obj = nullptr;
  831. std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
  832. // File name to file contents of source.
  833. std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
  834. // Mark the line endings of the cached source.
  835. std::unordered_map<std::string, std::vector<StringRef>> LineCache;
  836. // Keep track of missing sources.
  837. StringSet<> MissingSources;
  838. // Only emit 'invalid debug info' warning once.
  839. bool WarnedInvalidDebugInfo = false;
  840. private:
  841. bool cacheSource(const DILineInfo& LineInfoFile);
  842. void printLines(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
  843. StringRef Delimiter, LiveVariablePrinter &LVP);
  844. void printSources(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
  845. StringRef ObjectFilename, StringRef Delimiter,
  846. LiveVariablePrinter &LVP);
  847. public:
  848. SourcePrinter() = default;
  849. SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) {
  850. symbolize::LLVMSymbolizer::Options SymbolizerOpts;
  851. SymbolizerOpts.PrintFunctions =
  852. DILineInfoSpecifier::FunctionNameKind::LinkageName;
  853. SymbolizerOpts.Demangle = Demangle;
  854. SymbolizerOpts.DefaultArch = std::string(DefaultArch);
  855. Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
  856. }
  857. virtual ~SourcePrinter() = default;
  858. virtual void printSourceLine(formatted_raw_ostream &OS,
  859. object::SectionedAddress Address,
  860. StringRef ObjectFilename,
  861. LiveVariablePrinter &LVP,
  862. StringRef Delimiter = "; ");
  863. };
  864. bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
  865. std::unique_ptr<MemoryBuffer> Buffer;
  866. if (LineInfo.Source) {
  867. Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
  868. } else {
  869. auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
  870. if (!BufferOrError) {
  871. if (MissingSources.insert(LineInfo.FileName).second)
  872. reportWarning("failed to find source " + LineInfo.FileName,
  873. Obj->getFileName());
  874. return false;
  875. }
  876. Buffer = std::move(*BufferOrError);
  877. }
  878. // Chomp the file to get lines
  879. const char *BufferStart = Buffer->getBufferStart(),
  880. *BufferEnd = Buffer->getBufferEnd();
  881. std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];
  882. const char *Start = BufferStart;
  883. for (const char *I = BufferStart; I != BufferEnd; ++I)
  884. if (*I == '\n') {
  885. Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));
  886. Start = I + 1;
  887. }
  888. if (Start < BufferEnd)
  889. Lines.emplace_back(Start, BufferEnd - Start);
  890. SourceCache[LineInfo.FileName] = std::move(Buffer);
  891. return true;
  892. }
  893. void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,
  894. object::SectionedAddress Address,
  895. StringRef ObjectFilename,
  896. LiveVariablePrinter &LVP,
  897. StringRef Delimiter) {
  898. if (!Symbolizer)
  899. return;
  900. DILineInfo LineInfo = DILineInfo();
  901. Expected<DILineInfo> ExpectedLineInfo =
  902. Symbolizer->symbolizeCode(*Obj, Address);
  903. std::string ErrorMessage;
  904. if (ExpectedLineInfo) {
  905. LineInfo = *ExpectedLineInfo;
  906. } else if (!WarnedInvalidDebugInfo) {
  907. WarnedInvalidDebugInfo = true;
  908. // TODO Untested.
  909. reportWarning("failed to parse debug information: " +
  910. toString(ExpectedLineInfo.takeError()),
  911. ObjectFilename);
  912. }
  913. if (!Prefix.empty() && sys::path::is_absolute_gnu(LineInfo.FileName)) {
  914. SmallString<128> FilePath;
  915. sys::path::append(FilePath, Prefix, LineInfo.FileName);
  916. LineInfo.FileName = std::string(FilePath);
  917. }
  918. if (PrintLines)
  919. printLines(OS, LineInfo, Delimiter, LVP);
  920. if (PrintSource)
  921. printSources(OS, LineInfo, ObjectFilename, Delimiter, LVP);
  922. OldLineInfo = LineInfo;
  923. }
  924. void SourcePrinter::printLines(formatted_raw_ostream &OS,
  925. const DILineInfo &LineInfo, StringRef Delimiter,
  926. LiveVariablePrinter &LVP) {
  927. bool PrintFunctionName = LineInfo.FunctionName != DILineInfo::BadString &&
  928. LineInfo.FunctionName != OldLineInfo.FunctionName;
  929. if (PrintFunctionName) {
  930. OS << Delimiter << LineInfo.FunctionName;
  931. // If demangling is successful, FunctionName will end with "()". Print it
  932. // only if demangling did not run or was unsuccessful.
  933. if (!StringRef(LineInfo.FunctionName).endswith("()"))
  934. OS << "()";
  935. OS << ":\n";
  936. }
  937. if (LineInfo.FileName != DILineInfo::BadString && LineInfo.Line != 0 &&
  938. (OldLineInfo.Line != LineInfo.Line ||
  939. OldLineInfo.FileName != LineInfo.FileName || PrintFunctionName)) {
  940. OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line;
  941. LVP.printBetweenInsts(OS, true);
  942. }
  943. }
  944. void SourcePrinter::printSources(formatted_raw_ostream &OS,
  945. const DILineInfo &LineInfo,
  946. StringRef ObjectFilename, StringRef Delimiter,
  947. LiveVariablePrinter &LVP) {
  948. if (LineInfo.FileName == DILineInfo::BadString || LineInfo.Line == 0 ||
  949. (OldLineInfo.Line == LineInfo.Line &&
  950. OldLineInfo.FileName == LineInfo.FileName))
  951. return;
  952. if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
  953. if (!cacheSource(LineInfo))
  954. return;
  955. auto LineBuffer = LineCache.find(LineInfo.FileName);
  956. if (LineBuffer != LineCache.end()) {
  957. if (LineInfo.Line > LineBuffer->second.size()) {
  958. reportWarning(
  959. formatv(
  960. "debug info line number {0} exceeds the number of lines in {1}",
  961. LineInfo.Line, LineInfo.FileName),
  962. ObjectFilename);
  963. return;
  964. }
  965. // Vector begins at 0, line numbers are non-zero
  966. OS << Delimiter << LineBuffer->second[LineInfo.Line - 1];
  967. LVP.printBetweenInsts(OS, true);
  968. }
  969. }
  970. static bool isAArch64Elf(const ObjectFile *Obj) {
  971. const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
  972. return Elf && Elf->getEMachine() == ELF::EM_AARCH64;
  973. }
  974. static bool isArmElf(const ObjectFile *Obj) {
  975. const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
  976. return Elf && Elf->getEMachine() == ELF::EM_ARM;
  977. }
  978. static bool hasMappingSymbols(const ObjectFile *Obj) {
  979. return isArmElf(Obj) || isAArch64Elf(Obj);
  980. }
  981. static void printRelocation(formatted_raw_ostream &OS, StringRef FileName,
  982. const RelocationRef &Rel, uint64_t Address,
  983. bool Is64Bits) {
  984. StringRef Fmt = Is64Bits ? "\t\t%016" PRIx64 ": " : "\t\t\t%08" PRIx64 ": ";
  985. SmallString<16> Name;
  986. SmallString<32> Val;
  987. Rel.getTypeName(Name);
  988. if (Error E = getRelocationValueString(Rel, Val))
  989. reportError(std::move(E), FileName);
  990. OS << format(Fmt.data(), Address) << Name << "\t" << Val;
  991. }
  992. class PrettyPrinter {
  993. public:
  994. virtual ~PrettyPrinter() = default;
  995. virtual void
  996. printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  997. object::SectionedAddress Address, formatted_raw_ostream &OS,
  998. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  999. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  1000. LiveVariablePrinter &LVP) {
  1001. if (SP && (PrintSource || PrintLines))
  1002. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  1003. LVP.printBetweenInsts(OS, false);
  1004. size_t Start = OS.tell();
  1005. if (!NoLeadingAddr)
  1006. OS << format("%8" PRIx64 ":", Address.Address);
  1007. if (!NoShowRawInsn) {
  1008. OS << ' ';
  1009. dumpBytes(Bytes, OS);
  1010. }
  1011. // The output of printInst starts with a tab. Print some spaces so that
  1012. // the tab has 1 column and advances to the target tab stop.
  1013. unsigned TabStop = getInstStartColumn(STI);
  1014. unsigned Column = OS.tell() - Start;
  1015. OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
  1016. if (MI) {
  1017. // See MCInstPrinter::printInst. On targets where a PC relative immediate
  1018. // is relative to the next instruction and the length of a MCInst is
  1019. // difficult to measure (x86), this is the address of the next
  1020. // instruction.
  1021. uint64_t Addr =
  1022. Address.Address + (STI.getTargetTriple().isX86() ? Bytes.size() : 0);
  1023. IP.printInst(MI, Addr, "", STI, OS);
  1024. } else
  1025. OS << "\t<unknown>";
  1026. }
  1027. };
  1028. PrettyPrinter PrettyPrinterInst;
  1029. class HexagonPrettyPrinter : public PrettyPrinter {
  1030. public:
  1031. void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
  1032. formatted_raw_ostream &OS) {
  1033. uint32_t opcode =
  1034. (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
  1035. if (!NoLeadingAddr)
  1036. OS << format("%8" PRIx64 ":", Address);
  1037. if (!NoShowRawInsn) {
  1038. OS << "\t";
  1039. dumpBytes(Bytes.slice(0, 4), OS);
  1040. OS << format("\t%08" PRIx32, opcode);
  1041. }
  1042. }
  1043. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  1044. object::SectionedAddress Address, formatted_raw_ostream &OS,
  1045. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  1046. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  1047. LiveVariablePrinter &LVP) override {
  1048. if (SP && (PrintSource || PrintLines))
  1049. SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
  1050. if (!MI) {
  1051. printLead(Bytes, Address.Address, OS);
  1052. OS << " <unknown>";
  1053. return;
  1054. }
  1055. std::string Buffer;
  1056. {
  1057. raw_string_ostream TempStream(Buffer);
  1058. IP.printInst(MI, Address.Address, "", STI, TempStream);
  1059. }
  1060. StringRef Contents(Buffer);
  1061. // Split off bundle attributes
  1062. auto PacketBundle = Contents.rsplit('\n');
  1063. // Split off first instruction from the rest
  1064. auto HeadTail = PacketBundle.first.split('\n');
  1065. auto Preamble = " { ";
  1066. auto Separator = "";
  1067. // Hexagon's packets require relocations to be inline rather than
  1068. // clustered at the end of the packet.
  1069. std::vector<RelocationRef>::const_iterator RelCur = Rels->begin();
  1070. std::vector<RelocationRef>::const_iterator RelEnd = Rels->end();
  1071. auto PrintReloc = [&]() -> void {
  1072. while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address.Address)) {
  1073. if (RelCur->getOffset() == Address.Address) {
  1074. printRelocation(OS, ObjectFilename, *RelCur, Address.Address, false);
  1075. return;
  1076. }
  1077. ++RelCur;
  1078. }
  1079. };
  1080. while (!HeadTail.first.empty()) {
  1081. OS << Separator;
  1082. Separator = "\n";
  1083. if (SP && (PrintSource || PrintLines))
  1084. SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
  1085. printLead(Bytes, Address.Address, OS);
  1086. OS << Preamble;
  1087. Preamble = " ";
  1088. StringRef Inst;
  1089. auto Duplex = HeadTail.first.split('\v');
  1090. if (!Duplex.second.empty()) {
  1091. OS << Duplex.first;
  1092. OS << "; ";
  1093. Inst = Duplex.second;
  1094. }
  1095. else
  1096. Inst = HeadTail.first;
  1097. OS << Inst;
  1098. HeadTail = HeadTail.second.split('\n');
  1099. if (HeadTail.first.empty())
  1100. OS << " } " << PacketBundle.second;
  1101. PrintReloc();
  1102. Bytes = Bytes.slice(4);
  1103. Address.Address += 4;
  1104. }
  1105. }
  1106. };
  1107. HexagonPrettyPrinter HexagonPrettyPrinterInst;
  1108. class AMDGCNPrettyPrinter : public PrettyPrinter {
  1109. public:
  1110. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  1111. object::SectionedAddress Address, formatted_raw_ostream &OS,
  1112. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  1113. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  1114. LiveVariablePrinter &LVP) override {
  1115. if (SP && (PrintSource || PrintLines))
  1116. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  1117. if (MI) {
  1118. SmallString<40> InstStr;
  1119. raw_svector_ostream IS(InstStr);
  1120. IP.printInst(MI, Address.Address, "", STI, IS);
  1121. OS << left_justify(IS.str(), 60);
  1122. } else {
  1123. // an unrecognized encoding - this is probably data so represent it
  1124. // using the .long directive, or .byte directive if fewer than 4 bytes
  1125. // remaining
  1126. if (Bytes.size() >= 4) {
  1127. OS << format("\t.long 0x%08" PRIx32 " ",
  1128. support::endian::read32<support::little>(Bytes.data()));
  1129. OS.indent(42);
  1130. } else {
  1131. OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
  1132. for (unsigned int i = 1; i < Bytes.size(); i++)
  1133. OS << format(", 0x%02" PRIx8, Bytes[i]);
  1134. OS.indent(55 - (6 * Bytes.size()));
  1135. }
  1136. }
  1137. OS << format("// %012" PRIX64 ":", Address.Address);
  1138. if (Bytes.size() >= 4) {
  1139. // D should be casted to uint32_t here as it is passed by format to
  1140. // snprintf as vararg.
  1141. for (uint32_t D : makeArrayRef(
  1142. reinterpret_cast<const support::little32_t *>(Bytes.data()),
  1143. Bytes.size() / 4))
  1144. OS << format(" %08" PRIX32, D);
  1145. } else {
  1146. for (unsigned char B : Bytes)
  1147. OS << format(" %02" PRIX8, B);
  1148. }
  1149. if (!Annot.empty())
  1150. OS << " // " << Annot;
  1151. }
  1152. };
  1153. AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
  1154. class BPFPrettyPrinter : public PrettyPrinter {
  1155. public:
  1156. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  1157. object::SectionedAddress Address, formatted_raw_ostream &OS,
  1158. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  1159. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  1160. LiveVariablePrinter &LVP) override {
  1161. if (SP && (PrintSource || PrintLines))
  1162. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  1163. if (!NoLeadingAddr)
  1164. OS << format("%8" PRId64 ":", Address.Address / 8);
  1165. if (!NoShowRawInsn) {
  1166. OS << "\t";
  1167. dumpBytes(Bytes, OS);
  1168. }
  1169. if (MI)
  1170. IP.printInst(MI, Address.Address, "", STI, OS);
  1171. else
  1172. OS << "\t<unknown>";
  1173. }
  1174. };
  1175. BPFPrettyPrinter BPFPrettyPrinterInst;
  1176. PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
  1177. switch(Triple.getArch()) {
  1178. default:
  1179. return PrettyPrinterInst;
  1180. case Triple::hexagon:
  1181. return HexagonPrettyPrinterInst;
  1182. case Triple::amdgcn:
  1183. return AMDGCNPrettyPrinterInst;
  1184. case Triple::bpfel:
  1185. case Triple::bpfeb:
  1186. return BPFPrettyPrinterInst;
  1187. }
  1188. }
  1189. }
  1190. static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
  1191. assert(Obj->isELF());
  1192. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  1193. return unwrapOrError(Elf32LEObj->getSymbol(Sym.getRawDataRefImpl()),
  1194. Obj->getFileName())
  1195. ->getType();
  1196. if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  1197. return unwrapOrError(Elf64LEObj->getSymbol(Sym.getRawDataRefImpl()),
  1198. Obj->getFileName())
  1199. ->getType();
  1200. if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  1201. return unwrapOrError(Elf32BEObj->getSymbol(Sym.getRawDataRefImpl()),
  1202. Obj->getFileName())
  1203. ->getType();
  1204. if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
  1205. return unwrapOrError(Elf64BEObj->getSymbol(Sym.getRawDataRefImpl()),
  1206. Obj->getFileName())
  1207. ->getType();
  1208. llvm_unreachable("Unsupported binary format");
  1209. }
  1210. template <class ELFT> static void
  1211. addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj,
  1212. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  1213. for (auto Symbol : Obj->getDynamicSymbolIterators()) {
  1214. uint8_t SymbolType = Symbol.getELFType();
  1215. if (SymbolType == ELF::STT_SECTION)
  1216. continue;
  1217. uint64_t Address = unwrapOrError(Symbol.getAddress(), Obj->getFileName());
  1218. // ELFSymbolRef::getAddress() returns size instead of value for common
  1219. // symbols which is not desirable for disassembly output. Overriding.
  1220. if (SymbolType == ELF::STT_COMMON)
  1221. Address = unwrapOrError(Obj->getSymbol(Symbol.getRawDataRefImpl()),
  1222. Obj->getFileName())
  1223. ->st_value;
  1224. StringRef Name = unwrapOrError(Symbol.getName(), Obj->getFileName());
  1225. if (Name.empty())
  1226. continue;
  1227. section_iterator SecI =
  1228. unwrapOrError(Symbol.getSection(), Obj->getFileName());
  1229. if (SecI == Obj->section_end())
  1230. continue;
  1231. AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
  1232. }
  1233. }
  1234. static void
  1235. addDynamicElfSymbols(const ObjectFile *Obj,
  1236. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  1237. assert(Obj->isELF());
  1238. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  1239. addDynamicElfSymbols(Elf32LEObj, AllSymbols);
  1240. else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  1241. addDynamicElfSymbols(Elf64LEObj, AllSymbols);
  1242. else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  1243. addDynamicElfSymbols(Elf32BEObj, AllSymbols);
  1244. else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
  1245. addDynamicElfSymbols(Elf64BEObj, AllSymbols);
  1246. else
  1247. llvm_unreachable("Unsupported binary format");
  1248. }
  1249. static void addPltEntries(const ObjectFile *Obj,
  1250. std::map<SectionRef, SectionSymbolsTy> &AllSymbols,
  1251. StringSaver &Saver) {
  1252. Optional<SectionRef> Plt = None;
  1253. for (const SectionRef &Section : Obj->sections()) {
  1254. Expected<StringRef> SecNameOrErr = Section.getName();
  1255. if (!SecNameOrErr) {
  1256. consumeError(SecNameOrErr.takeError());
  1257. continue;
  1258. }
  1259. if (*SecNameOrErr == ".plt")
  1260. Plt = Section;
  1261. }
  1262. if (!Plt)
  1263. return;
  1264. if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(Obj)) {
  1265. for (auto PltEntry : ElfObj->getPltAddresses()) {
  1266. if (PltEntry.first) {
  1267. SymbolRef Symbol(*PltEntry.first, ElfObj);
  1268. uint8_t SymbolType = getElfSymbolType(Obj, Symbol);
  1269. if (Expected<StringRef> NameOrErr = Symbol.getName()) {
  1270. if (!NameOrErr->empty())
  1271. AllSymbols[*Plt].emplace_back(
  1272. PltEntry.second, Saver.save((*NameOrErr + "@plt").str()),
  1273. SymbolType);
  1274. continue;
  1275. } else {
  1276. // The warning has been reported in disassembleObject().
  1277. consumeError(NameOrErr.takeError());
  1278. }
  1279. }
  1280. reportWarning("PLT entry at 0x" + Twine::utohexstr(PltEntry.second) +
  1281. " references an invalid symbol",
  1282. Obj->getFileName());
  1283. }
  1284. }
  1285. }
  1286. // Normally the disassembly output will skip blocks of zeroes. This function
  1287. // returns the number of zero bytes that can be skipped when dumping the
  1288. // disassembly of the instructions in Buf.
  1289. static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
  1290. // Find the number of leading zeroes.
  1291. size_t N = 0;
  1292. while (N < Buf.size() && !Buf[N])
  1293. ++N;
  1294. // We may want to skip blocks of zero bytes, but unless we see
  1295. // at least 8 of them in a row.
  1296. if (N < 8)
  1297. return 0;
  1298. // We skip zeroes in multiples of 4 because do not want to truncate an
  1299. // instruction if it starts with a zero byte.
  1300. return N & ~0x3;
  1301. }
  1302. // Returns a map from sections to their relocations.
  1303. static std::map<SectionRef, std::vector<RelocationRef>>
  1304. getRelocsMap(object::ObjectFile const &Obj) {
  1305. std::map<SectionRef, std::vector<RelocationRef>> Ret;
  1306. uint64_t I = (uint64_t)-1;
  1307. for (SectionRef Sec : Obj.sections()) {
  1308. ++I;
  1309. Expected<section_iterator> RelocatedOrErr = Sec.getRelocatedSection();
  1310. if (!RelocatedOrErr)
  1311. reportError(Obj.getFileName(),
  1312. "section (" + Twine(I) +
  1313. "): failed to get a relocated section: " +
  1314. toString(RelocatedOrErr.takeError()));
  1315. section_iterator Relocated = *RelocatedOrErr;
  1316. if (Relocated == Obj.section_end() || !checkSectionFilter(*Relocated).Keep)
  1317. continue;
  1318. std::vector<RelocationRef> &V = Ret[*Relocated];
  1319. for (const RelocationRef &R : Sec.relocations())
  1320. V.push_back(R);
  1321. // Sort relocations by address.
  1322. llvm::stable_sort(V, isRelocAddressLess);
  1323. }
  1324. return Ret;
  1325. }
  1326. // Used for --adjust-vma to check if address should be adjusted by the
  1327. // specified value for a given section.
  1328. // For ELF we do not adjust non-allocatable sections like debug ones,
  1329. // because they are not loadable.
  1330. // TODO: implement for other file formats.
  1331. static bool shouldAdjustVA(const SectionRef &Section) {
  1332. const ObjectFile *Obj = Section.getObject();
  1333. if (Obj->isELF())
  1334. return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
  1335. return false;
  1336. }
  1337. typedef std::pair<uint64_t, char> MappingSymbolPair;
  1338. static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
  1339. uint64_t Address) {
  1340. auto It =
  1341. partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
  1342. return Val.first <= Address;
  1343. });
  1344. // Return zero for any address before the first mapping symbol; this means
  1345. // we should use the default disassembly mode, depending on the target.
  1346. if (It == MappingSymbols.begin())
  1347. return '\x00';
  1348. return (It - 1)->second;
  1349. }
  1350. static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
  1351. uint64_t End, const ObjectFile *Obj,
  1352. ArrayRef<uint8_t> Bytes,
  1353. ArrayRef<MappingSymbolPair> MappingSymbols,
  1354. raw_ostream &OS) {
  1355. support::endianness Endian =
  1356. Obj->isLittleEndian() ? support::little : support::big;
  1357. OS << format("%8" PRIx64 ":\t", SectionAddr + Index);
  1358. if (Index + 4 <= End) {
  1359. dumpBytes(Bytes.slice(Index, 4), OS);
  1360. OS << "\t.word\t"
  1361. << format_hex(support::endian::read32(Bytes.data() + Index, Endian),
  1362. 10);
  1363. return 4;
  1364. }
  1365. if (Index + 2 <= End) {
  1366. dumpBytes(Bytes.slice(Index, 2), OS);
  1367. OS << "\t\t.short\t"
  1368. << format_hex(support::endian::read16(Bytes.data() + Index, Endian),
  1369. 6);
  1370. return 2;
  1371. }
  1372. dumpBytes(Bytes.slice(Index, 1), OS);
  1373. OS << "\t\t.byte\t" << format_hex(Bytes[0], 4);
  1374. return 1;
  1375. }
  1376. static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
  1377. ArrayRef<uint8_t> Bytes) {
  1378. // print out data up to 8 bytes at a time in hex and ascii
  1379. uint8_t AsciiData[9] = {'\0'};
  1380. uint8_t Byte;
  1381. int NumBytes = 0;
  1382. for (; Index < End; ++Index) {
  1383. if (NumBytes == 0)
  1384. outs() << format("%8" PRIx64 ":", SectionAddr + Index);
  1385. Byte = Bytes.slice(Index)[0];
  1386. outs() << format(" %02x", Byte);
  1387. AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
  1388. uint8_t IndentOffset = 0;
  1389. NumBytes++;
  1390. if (Index == End - 1 || NumBytes > 8) {
  1391. // Indent the space for less than 8 bytes data.
  1392. // 2 spaces for byte and one for space between bytes
  1393. IndentOffset = 3 * (8 - NumBytes);
  1394. for (int Excess = NumBytes; Excess < 8; Excess++)
  1395. AsciiData[Excess] = '\0';
  1396. NumBytes = 8;
  1397. }
  1398. if (NumBytes == 8) {
  1399. AsciiData[8] = '\0';
  1400. outs() << std::string(IndentOffset, ' ') << " ";
  1401. outs() << reinterpret_cast<char *>(AsciiData);
  1402. outs() << '\n';
  1403. NumBytes = 0;
  1404. }
  1405. }
  1406. }
  1407. SymbolInfoTy objdump::createSymbolInfo(const ObjectFile *Obj,
  1408. const SymbolRef &Symbol) {
  1409. const StringRef FileName = Obj->getFileName();
  1410. const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
  1411. const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
  1412. if (Obj->isXCOFF() && SymbolDescription) {
  1413. const auto *XCOFFObj = cast<XCOFFObjectFile>(Obj);
  1414. DataRefImpl SymbolDRI = Symbol.getRawDataRefImpl();
  1415. const uint32_t SymbolIndex = XCOFFObj->getSymbolIndex(SymbolDRI.p);
  1416. Optional<XCOFF::StorageMappingClass> Smc =
  1417. getXCOFFSymbolCsectSMC(XCOFFObj, Symbol);
  1418. return SymbolInfoTy(Addr, Name, Smc, SymbolIndex,
  1419. isLabel(XCOFFObj, Symbol));
  1420. } else
  1421. return SymbolInfoTy(Addr, Name,
  1422. Obj->isELF() ? getElfSymbolType(Obj, Symbol)
  1423. : (uint8_t)ELF::STT_NOTYPE);
  1424. }
  1425. static SymbolInfoTy createDummySymbolInfo(const ObjectFile *Obj,
  1426. const uint64_t Addr, StringRef &Name,
  1427. uint8_t Type) {
  1428. if (Obj->isXCOFF() && SymbolDescription)
  1429. return SymbolInfoTy(Addr, Name, None, None, false);
  1430. else
  1431. return SymbolInfoTy(Addr, Name, Type);
  1432. }
  1433. static void
  1434. collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, const MCInstrAnalysis *MIA,
  1435. MCDisassembler *DisAsm, MCInstPrinter *IP,
  1436. const MCSubtargetInfo *STI, uint64_t SectionAddr,
  1437. uint64_t Start, uint64_t End,
  1438. std::unordered_map<uint64_t, std::string> &Labels) {
  1439. // So far only supports X86.
  1440. if (!STI->getTargetTriple().isX86())
  1441. return;
  1442. Labels.clear();
  1443. unsigned LabelCount = 0;
  1444. Start += SectionAddr;
  1445. End += SectionAddr;
  1446. uint64_t Index = Start;
  1447. while (Index < End) {
  1448. // Disassemble a real instruction and record function-local branch labels.
  1449. MCInst Inst;
  1450. uint64_t Size;
  1451. bool Disassembled = DisAsm->getInstruction(
  1452. Inst, Size, Bytes.slice(Index - SectionAddr), Index, nulls());
  1453. if (Size == 0)
  1454. Size = 1;
  1455. if (Disassembled && MIA) {
  1456. uint64_t Target;
  1457. bool TargetKnown = MIA->evaluateBranch(Inst, Index, Size, Target);
  1458. if (TargetKnown && (Target >= Start && Target < End) &&
  1459. !Labels.count(Target))
  1460. Labels[Target] = ("L" + Twine(LabelCount++)).str();
  1461. }
  1462. Index += Size;
  1463. }
  1464. }
  1465. static StringRef getSegmentName(const MachOObjectFile *MachO,
  1466. const SectionRef &Section) {
  1467. if (MachO) {
  1468. DataRefImpl DR = Section.getRawDataRefImpl();
  1469. StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
  1470. return SegmentName;
  1471. }
  1472. return "";
  1473. }
  1474. static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
  1475. MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
  1476. MCDisassembler *SecondaryDisAsm,
  1477. const MCInstrAnalysis *MIA, MCInstPrinter *IP,
  1478. const MCSubtargetInfo *PrimarySTI,
  1479. const MCSubtargetInfo *SecondarySTI,
  1480. PrettyPrinter &PIP,
  1481. SourcePrinter &SP, bool InlineRelocs) {
  1482. const MCSubtargetInfo *STI = PrimarySTI;
  1483. MCDisassembler *DisAsm = PrimaryDisAsm;
  1484. bool PrimaryIsThumb = false;
  1485. if (isArmElf(Obj))
  1486. PrimaryIsThumb = STI->checkFeatures("+thumb-mode");
  1487. std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
  1488. if (InlineRelocs)
  1489. RelocMap = getRelocsMap(*Obj);
  1490. bool Is64Bits = Obj->getBytesInAddress() > 4;
  1491. // Create a mapping from virtual address to symbol name. This is used to
  1492. // pretty print the symbols while disassembling.
  1493. std::map<SectionRef, SectionSymbolsTy> AllSymbols;
  1494. SectionSymbolsTy AbsoluteSymbols;
  1495. const StringRef FileName = Obj->getFileName();
  1496. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
  1497. for (const SymbolRef &Symbol : Obj->symbols()) {
  1498. Expected<StringRef> NameOrErr = Symbol.getName();
  1499. if (!NameOrErr) {
  1500. reportWarning(toString(NameOrErr.takeError()), FileName);
  1501. continue;
  1502. }
  1503. if (NameOrErr->empty() && !(Obj->isXCOFF() && SymbolDescription))
  1504. continue;
  1505. if (Obj->isELF() && getElfSymbolType(Obj, Symbol) == ELF::STT_SECTION)
  1506. continue;
  1507. // Don't ask a Mach-O STAB symbol for its section unless you know that
  1508. // STAB symbol's section field refers to a valid section index. Otherwise
  1509. // the symbol may error trying to load a section that does not exist.
  1510. if (MachO) {
  1511. DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
  1512. uint8_t NType = (MachO->is64Bit() ?
  1513. MachO->getSymbol64TableEntry(SymDRI).n_type:
  1514. MachO->getSymbolTableEntry(SymDRI).n_type);
  1515. if (NType & MachO::N_STAB)
  1516. continue;
  1517. }
  1518. section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
  1519. if (SecI != Obj->section_end())
  1520. AllSymbols[*SecI].push_back(createSymbolInfo(Obj, Symbol));
  1521. else
  1522. AbsoluteSymbols.push_back(createSymbolInfo(Obj, Symbol));
  1523. }
  1524. if (AllSymbols.empty() && Obj->isELF())
  1525. addDynamicElfSymbols(Obj, AllSymbols);
  1526. BumpPtrAllocator A;
  1527. StringSaver Saver(A);
  1528. addPltEntries(Obj, AllSymbols, Saver);
  1529. // Create a mapping from virtual address to section. An empty section can
  1530. // cause more than one section at the same address. Sort such sections to be
  1531. // before same-addressed non-empty sections so that symbol lookups prefer the
  1532. // non-empty section.
  1533. std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
  1534. for (SectionRef Sec : Obj->sections())
  1535. SectionAddresses.emplace_back(Sec.getAddress(), Sec);
  1536. llvm::stable_sort(SectionAddresses, [](const auto &LHS, const auto &RHS) {
  1537. if (LHS.first != RHS.first)
  1538. return LHS.first < RHS.first;
  1539. return LHS.second.getSize() < RHS.second.getSize();
  1540. });
  1541. // Linked executables (.exe and .dll files) typically don't include a real
  1542. // symbol table but they might contain an export table.
  1543. if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
  1544. for (const auto &ExportEntry : COFFObj->export_directories()) {
  1545. StringRef Name;
  1546. if (Error E = ExportEntry.getSymbolName(Name))
  1547. reportError(std::move(E), Obj->getFileName());
  1548. if (Name.empty())
  1549. continue;
  1550. uint32_t RVA;
  1551. if (Error E = ExportEntry.getExportRVA(RVA))
  1552. reportError(std::move(E), Obj->getFileName());
  1553. uint64_t VA = COFFObj->getImageBase() + RVA;
  1554. auto Sec = partition_point(
  1555. SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
  1556. return O.first <= VA;
  1557. });
  1558. if (Sec != SectionAddresses.begin()) {
  1559. --Sec;
  1560. AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
  1561. } else
  1562. AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
  1563. }
  1564. }
  1565. // Sort all the symbols, this allows us to use a simple binary search to find
  1566. // Multiple symbols can have the same address. Use a stable sort to stabilize
  1567. // the output.
  1568. StringSet<> FoundDisasmSymbolSet;
  1569. for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
  1570. llvm::stable_sort(SecSyms.second);
  1571. llvm::stable_sort(AbsoluteSymbols);
  1572. std::unique_ptr<DWARFContext> DICtx;
  1573. LiveVariablePrinter LVP(*Ctx.getRegisterInfo(), *STI);
  1574. if (DbgVariables != DVDisabled) {
  1575. DICtx = DWARFContext::create(*Obj);
  1576. for (const std::unique_ptr<DWARFUnit> &CU : DICtx->compile_units())
  1577. LVP.addCompileUnit(CU->getUnitDIE(false));
  1578. }
  1579. LLVM_DEBUG(LVP.dump());
  1580. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  1581. if (FilterSections.empty() && !DisassembleAll &&
  1582. (!Section.isText() || Section.isVirtual()))
  1583. continue;
  1584. uint64_t SectionAddr = Section.getAddress();
  1585. uint64_t SectSize = Section.getSize();
  1586. if (!SectSize)
  1587. continue;
  1588. // Get the list of all the symbols in this section.
  1589. SectionSymbolsTy &Symbols = AllSymbols[Section];
  1590. std::vector<MappingSymbolPair> MappingSymbols;
  1591. if (hasMappingSymbols(Obj)) {
  1592. for (const auto &Symb : Symbols) {
  1593. uint64_t Address = Symb.Addr;
  1594. StringRef Name = Symb.Name;
  1595. if (Name.startswith("$d"))
  1596. MappingSymbols.emplace_back(Address - SectionAddr, 'd');
  1597. if (Name.startswith("$x"))
  1598. MappingSymbols.emplace_back(Address - SectionAddr, 'x');
  1599. if (Name.startswith("$a"))
  1600. MappingSymbols.emplace_back(Address - SectionAddr, 'a');
  1601. if (Name.startswith("$t"))
  1602. MappingSymbols.emplace_back(Address - SectionAddr, 't');
  1603. }
  1604. }
  1605. llvm::sort(MappingSymbols);
  1606. if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
  1607. // AMDGPU disassembler uses symbolizer for printing labels
  1608. std::unique_ptr<MCRelocationInfo> RelInfo(
  1609. TheTarget->createMCRelocationInfo(TripleName, Ctx));
  1610. if (RelInfo) {
  1611. std::unique_ptr<MCSymbolizer> Symbolizer(
  1612. TheTarget->createMCSymbolizer(
  1613. TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
  1614. DisAsm->setSymbolizer(std::move(Symbolizer));
  1615. }
  1616. }
  1617. StringRef SegmentName = getSegmentName(MachO, Section);
  1618. StringRef SectionName = unwrapOrError(Section.getName(), Obj->getFileName());
  1619. // If the section has no symbol at the start, just insert a dummy one.
  1620. if (Symbols.empty() || Symbols[0].Addr != 0) {
  1621. Symbols.insert(Symbols.begin(),
  1622. createDummySymbolInfo(Obj, SectionAddr, SectionName,
  1623. Section.isText() ? ELF::STT_FUNC
  1624. : ELF::STT_OBJECT));
  1625. }
  1626. SmallString<40> Comments;
  1627. raw_svector_ostream CommentStream(Comments);
  1628. ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
  1629. unwrapOrError(Section.getContents(), Obj->getFileName()));
  1630. uint64_t VMAAdjustment = 0;
  1631. if (shouldAdjustVA(Section))
  1632. VMAAdjustment = AdjustVMA;
  1633. uint64_t Size;
  1634. uint64_t Index;
  1635. bool PrintedSection = false;
  1636. std::vector<RelocationRef> Rels = RelocMap[Section];
  1637. std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
  1638. std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
  1639. // Disassemble symbol by symbol.
  1640. for (unsigned SI = 0, SE = Symbols.size(); SI != SE; ++SI) {
  1641. std::string SymbolName = Symbols[SI].Name.str();
  1642. if (Demangle)
  1643. SymbolName = demangle(SymbolName);
  1644. // Skip if --disassemble-symbols is not empty and the symbol is not in
  1645. // the list.
  1646. if (!DisasmSymbolSet.empty() && !DisasmSymbolSet.count(SymbolName))
  1647. continue;
  1648. uint64_t Start = Symbols[SI].Addr;
  1649. if (Start < SectionAddr || StopAddress <= Start)
  1650. continue;
  1651. else
  1652. FoundDisasmSymbolSet.insert(SymbolName);
  1653. // The end is the section end, the beginning of the next symbol, or
  1654. // --stop-address.
  1655. uint64_t End = std::min<uint64_t>(SectionAddr + SectSize, StopAddress);
  1656. if (SI + 1 < SE)
  1657. End = std::min(End, Symbols[SI + 1].Addr);
  1658. if (Start >= End || End <= StartAddress)
  1659. continue;
  1660. Start -= SectionAddr;
  1661. End -= SectionAddr;
  1662. if (!PrintedSection) {
  1663. PrintedSection = true;
  1664. outs() << "\nDisassembly of section ";
  1665. if (!SegmentName.empty())
  1666. outs() << SegmentName << ",";
  1667. outs() << SectionName << ":\n";
  1668. }
  1669. outs() << '\n';
  1670. if (!NoLeadingAddr)
  1671. outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
  1672. SectionAddr + Start + VMAAdjustment);
  1673. if (Obj->isXCOFF() && SymbolDescription) {
  1674. outs() << getXCOFFSymbolDescription(Symbols[SI], SymbolName) << ":\n";
  1675. } else
  1676. outs() << '<' << SymbolName << ">:\n";
  1677. // Don't print raw contents of a virtual section. A virtual section
  1678. // doesn't have any contents in the file.
  1679. if (Section.isVirtual()) {
  1680. outs() << "...\n";
  1681. continue;
  1682. }
  1683. auto Status = DisAsm->onSymbolStart(Symbols[SI], Size,
  1684. Bytes.slice(Start, End - Start),
  1685. SectionAddr + Start, CommentStream);
  1686. // To have round trippable disassembly, we fall back to decoding the
  1687. // remaining bytes as instructions.
  1688. //
  1689. // If there is a failure, we disassemble the failed region as bytes before
  1690. // falling back. The target is expected to print nothing in this case.
  1691. //
  1692. // If there is Success or SoftFail i.e no 'real' failure, we go ahead by
  1693. // Size bytes before falling back.
  1694. // So if the entire symbol is 'eaten' by the target:
  1695. // Start += Size // Now Start = End and we will never decode as
  1696. // // instructions
  1697. //
  1698. // Right now, most targets return None i.e ignore to treat a symbol
  1699. // separately. But WebAssembly decodes preludes for some symbols.
  1700. //
  1701. if (Status.hasValue()) {
  1702. if (Status.getValue() == MCDisassembler::Fail) {
  1703. outs() << "// Error in decoding " << SymbolName
  1704. << " : Decoding failed region as bytes.\n";
  1705. for (uint64_t I = 0; I < Size; ++I) {
  1706. outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
  1707. << "\n";
  1708. }
  1709. }
  1710. } else {
  1711. Size = 0;
  1712. }
  1713. Start += Size;
  1714. Index = Start;
  1715. if (SectionAddr < StartAddress)
  1716. Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
  1717. // If there is a data/common symbol inside an ELF text section and we are
  1718. // only disassembling text (applicable all architectures), we are in a
  1719. // situation where we must print the data and not disassemble it.
  1720. if (Obj->isELF() && !DisassembleAll && Section.isText()) {
  1721. uint8_t SymTy = Symbols[SI].Type;
  1722. if (SymTy == ELF::STT_OBJECT || SymTy == ELF::STT_COMMON) {
  1723. dumpELFData(SectionAddr, Index, End, Bytes);
  1724. Index = End;
  1725. }
  1726. }
  1727. bool CheckARMELFData = hasMappingSymbols(Obj) &&
  1728. Symbols[SI].Type != ELF::STT_OBJECT &&
  1729. !DisassembleAll;
  1730. bool DumpARMELFData = false;
  1731. formatted_raw_ostream FOS(outs());
  1732. std::unordered_map<uint64_t, std::string> AllLabels;
  1733. if (SymbolizeOperands)
  1734. collectLocalBranchTargets(Bytes, MIA, DisAsm, IP, PrimarySTI,
  1735. SectionAddr, Index, End, AllLabels);
  1736. while (Index < End) {
  1737. // ARM and AArch64 ELF binaries can interleave data and text in the
  1738. // same section. We rely on the markers introduced to understand what
  1739. // we need to dump. If the data marker is within a function, it is
  1740. // denoted as a word/short etc.
  1741. if (CheckARMELFData) {
  1742. char Kind = getMappingSymbolKind(MappingSymbols, Index);
  1743. DumpARMELFData = Kind == 'd';
  1744. if (SecondarySTI) {
  1745. if (Kind == 'a') {
  1746. STI = PrimaryIsThumb ? SecondarySTI : PrimarySTI;
  1747. DisAsm = PrimaryIsThumb ? SecondaryDisAsm : PrimaryDisAsm;
  1748. } else if (Kind == 't') {
  1749. STI = PrimaryIsThumb ? PrimarySTI : SecondarySTI;
  1750. DisAsm = PrimaryIsThumb ? PrimaryDisAsm : SecondaryDisAsm;
  1751. }
  1752. }
  1753. }
  1754. if (DumpARMELFData) {
  1755. Size = dumpARMELFData(SectionAddr, Index, End, Obj, Bytes,
  1756. MappingSymbols, FOS);
  1757. } else {
  1758. // When -z or --disassemble-zeroes are given we always dissasemble
  1759. // them. Otherwise we might want to skip zero bytes we see.
  1760. if (!DisassembleZeroes) {
  1761. uint64_t MaxOffset = End - Index;
  1762. // For --reloc: print zero blocks patched by relocations, so that
  1763. // relocations can be shown in the dump.
  1764. if (RelCur != RelEnd)
  1765. MaxOffset = RelCur->getOffset() - Index;
  1766. if (size_t N =
  1767. countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) {
  1768. FOS << "\t\t..." << '\n';
  1769. Index += N;
  1770. continue;
  1771. }
  1772. }
  1773. // Print local label if there's any.
  1774. auto Iter = AllLabels.find(SectionAddr + Index);
  1775. if (Iter != AllLabels.end())
  1776. FOS << "<" << Iter->second << ">:\n";
  1777. // Disassemble a real instruction or a data when disassemble all is
  1778. // provided
  1779. MCInst Inst;
  1780. bool Disassembled =
  1781. DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
  1782. SectionAddr + Index, CommentStream);
  1783. if (Size == 0)
  1784. Size = 1;
  1785. LVP.update({Index, Section.getIndex()},
  1786. {Index + Size, Section.getIndex()}, Index + Size != End);
  1787. PIP.printInst(
  1788. *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
  1789. {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
  1790. "", *STI, &SP, Obj->getFileName(), &Rels, LVP);
  1791. FOS << CommentStream.str();
  1792. Comments.clear();
  1793. // If disassembly has failed, avoid analysing invalid/incomplete
  1794. // instruction information. Otherwise, try to resolve the target
  1795. // address (jump target or memory operand address) and print it on the
  1796. // right of the instruction.
  1797. if (Disassembled && MIA) {
  1798. uint64_t Target;
  1799. bool PrintTarget =
  1800. MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
  1801. if (!PrintTarget)
  1802. if (Optional<uint64_t> MaybeTarget =
  1803. MIA->evaluateMemoryOperandAddress(
  1804. Inst, SectionAddr + Index, Size)) {
  1805. Target = *MaybeTarget;
  1806. PrintTarget = true;
  1807. // Do not print real address when symbolizing.
  1808. if (!SymbolizeOperands)
  1809. FOS << " # " << Twine::utohexstr(Target);
  1810. }
  1811. if (PrintTarget) {
  1812. // In a relocatable object, the target's section must reside in
  1813. // the same section as the call instruction or it is accessed
  1814. // through a relocation.
  1815. //
  1816. // In a non-relocatable object, the target may be in any section.
  1817. // In that case, locate the section(s) containing the target
  1818. // address and find the symbol in one of those, if possible.
  1819. //
  1820. // N.B. We don't walk the relocations in the relocatable case yet.
  1821. std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
  1822. if (!Obj->isRelocatableObject()) {
  1823. auto It = llvm::partition_point(
  1824. SectionAddresses,
  1825. [=](const std::pair<uint64_t, SectionRef> &O) {
  1826. return O.first <= Target;
  1827. });
  1828. uint64_t TargetSecAddr = 0;
  1829. while (It != SectionAddresses.begin()) {
  1830. --It;
  1831. if (TargetSecAddr == 0)
  1832. TargetSecAddr = It->first;
  1833. if (It->first != TargetSecAddr)
  1834. break;
  1835. TargetSectionSymbols.push_back(&AllSymbols[It->second]);
  1836. }
  1837. } else {
  1838. TargetSectionSymbols.push_back(&Symbols);
  1839. }
  1840. TargetSectionSymbols.push_back(&AbsoluteSymbols);
  1841. // Find the last symbol in the first candidate section whose
  1842. // offset is less than or equal to the target. If there are no
  1843. // such symbols, try in the next section and so on, before finally
  1844. // using the nearest preceding absolute symbol (if any), if there
  1845. // are no other valid symbols.
  1846. const SymbolInfoTy *TargetSym = nullptr;
  1847. for (const SectionSymbolsTy *TargetSymbols :
  1848. TargetSectionSymbols) {
  1849. auto It = llvm::partition_point(
  1850. *TargetSymbols,
  1851. [=](const SymbolInfoTy &O) { return O.Addr <= Target; });
  1852. if (It != TargetSymbols->begin()) {
  1853. TargetSym = &*(It - 1);
  1854. break;
  1855. }
  1856. }
  1857. // Print the labels corresponding to the target if there's any.
  1858. bool LabelAvailable = AllLabels.count(Target);
  1859. if (TargetSym != nullptr) {
  1860. uint64_t TargetAddress = TargetSym->Addr;
  1861. uint64_t Disp = Target - TargetAddress;
  1862. std::string TargetName = TargetSym->Name.str();
  1863. if (Demangle)
  1864. TargetName = demangle(TargetName);
  1865. FOS << " <";
  1866. if (!Disp) {
  1867. // Always Print the binary symbol precisely corresponding to
  1868. // the target address.
  1869. FOS << TargetName;
  1870. } else if (!LabelAvailable) {
  1871. // Always Print the binary symbol plus an offset if there's no
  1872. // local label corresponding to the target address.
  1873. FOS << TargetName << "+0x" << Twine::utohexstr(Disp);
  1874. } else {
  1875. FOS << AllLabels[Target];
  1876. }
  1877. FOS << ">";
  1878. } else if (LabelAvailable) {
  1879. FOS << " <" << AllLabels[Target] << ">";
  1880. }
  1881. }
  1882. }
  1883. }
  1884. LVP.printAfterInst(FOS);
  1885. FOS << "\n";
  1886. // Hexagon does this in pretty printer
  1887. if (Obj->getArch() != Triple::hexagon) {
  1888. // Print relocation for instruction and data.
  1889. while (RelCur != RelEnd) {
  1890. uint64_t Offset = RelCur->getOffset();
  1891. // If this relocation is hidden, skip it.
  1892. if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) {
  1893. ++RelCur;
  1894. continue;
  1895. }
  1896. // Stop when RelCur's offset is past the disassembled
  1897. // instruction/data. Note that it's possible the disassembled data
  1898. // is not the complete data: we might see the relocation printed in
  1899. // the middle of the data, but this matches the binutils objdump
  1900. // output.
  1901. if (Offset >= Index + Size)
  1902. break;
  1903. // When --adjust-vma is used, update the address printed.
  1904. if (RelCur->getSymbol() != Obj->symbol_end()) {
  1905. Expected<section_iterator> SymSI =
  1906. RelCur->getSymbol()->getSection();
  1907. if (SymSI && *SymSI != Obj->section_end() &&
  1908. shouldAdjustVA(**SymSI))
  1909. Offset += AdjustVMA;
  1910. }
  1911. printRelocation(FOS, Obj->getFileName(), *RelCur,
  1912. SectionAddr + Offset, Is64Bits);
  1913. LVP.printAfterOtherLine(FOS, true);
  1914. ++RelCur;
  1915. }
  1916. }
  1917. Index += Size;
  1918. }
  1919. }
  1920. }
  1921. StringSet<> MissingDisasmSymbolSet =
  1922. set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
  1923. for (StringRef Sym : MissingDisasmSymbolSet.keys())
  1924. reportWarning("failed to disassemble missing symbol " + Sym, FileName);
  1925. }
  1926. static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
  1927. const Target *TheTarget = getTarget(Obj);
  1928. // Package up features to be passed to target/subtarget
  1929. SubtargetFeatures Features = Obj->getFeatures();
  1930. if (!MAttrs.empty())
  1931. for (unsigned I = 0; I != MAttrs.size(); ++I)
  1932. Features.AddFeature(MAttrs[I]);
  1933. std::unique_ptr<const MCRegisterInfo> MRI(
  1934. TheTarget->createMCRegInfo(TripleName));
  1935. if (!MRI)
  1936. reportError(Obj->getFileName(),
  1937. "no register info for target " + TripleName);
  1938. // Set up disassembler.
  1939. MCTargetOptions MCOptions;
  1940. std::unique_ptr<const MCAsmInfo> AsmInfo(
  1941. TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
  1942. if (!AsmInfo)
  1943. reportError(Obj->getFileName(),
  1944. "no assembly info for target " + TripleName);
  1945. if (MCPU.empty())
  1946. MCPU = Obj->tryGetCPUName().getValueOr("").str();
  1947. std::unique_ptr<const MCSubtargetInfo> STI(
  1948. TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
  1949. if (!STI)
  1950. reportError(Obj->getFileName(),
  1951. "no subtarget info for target " + TripleName);
  1952. std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
  1953. if (!MII)
  1954. reportError(Obj->getFileName(),
  1955. "no instruction info for target " + TripleName);
  1956. MCObjectFileInfo MOFI;
  1957. MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
  1958. // FIXME: for now initialize MCObjectFileInfo with default values
  1959. MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
  1960. std::unique_ptr<MCDisassembler> DisAsm(
  1961. TheTarget->createMCDisassembler(*STI, Ctx));
  1962. if (!DisAsm)
  1963. reportError(Obj->getFileName(), "no disassembler for target " + TripleName);
  1964. // If we have an ARM object file, we need a second disassembler, because
  1965. // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode.
  1966. // We use mapping symbols to switch between the two assemblers, where
  1967. // appropriate.
  1968. std::unique_ptr<MCDisassembler> SecondaryDisAsm;
  1969. std::unique_ptr<const MCSubtargetInfo> SecondarySTI;
  1970. if (isArmElf(Obj) && !STI->checkFeatures("+mclass")) {
  1971. if (STI->checkFeatures("+thumb-mode"))
  1972. Features.AddFeature("-thumb-mode");
  1973. else
  1974. Features.AddFeature("+thumb-mode");
  1975. SecondarySTI.reset(TheTarget->createMCSubtargetInfo(TripleName, MCPU,
  1976. Features.getString()));
  1977. SecondaryDisAsm.reset(TheTarget->createMCDisassembler(*SecondarySTI, Ctx));
  1978. }
  1979. std::unique_ptr<const MCInstrAnalysis> MIA(
  1980. TheTarget->createMCInstrAnalysis(MII.get()));
  1981. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  1982. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  1983. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
  1984. if (!IP)
  1985. reportError(Obj->getFileName(),
  1986. "no instruction printer for target " + TripleName);
  1987. IP->setPrintImmHex(PrintImmHex);
  1988. IP->setPrintBranchImmAsAddress(true);
  1989. IP->setSymbolizeOperands(SymbolizeOperands);
  1990. IP->setMCInstrAnalysis(MIA.get());
  1991. PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
  1992. SourcePrinter SP(Obj, TheTarget->getName());
  1993. for (StringRef Opt : DisassemblerOptions)
  1994. if (!IP->applyTargetSpecificCLOption(Opt))
  1995. reportError(Obj->getFileName(),
  1996. "Unrecognized disassembler option: " + Opt);
  1997. disassembleObject(TheTarget, Obj, Ctx, DisAsm.get(), SecondaryDisAsm.get(),
  1998. MIA.get(), IP.get(), STI.get(), SecondarySTI.get(), PIP,
  1999. SP, InlineRelocs);
  2000. }
  2001. void objdump::printRelocations(const ObjectFile *Obj) {
  2002. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
  2003. "%08" PRIx64;
  2004. // Regular objdump doesn't print relocations in non-relocatable object
  2005. // files.
  2006. if (!Obj->isRelocatableObject())
  2007. return;
  2008. // Build a mapping from relocation target to a vector of relocation
  2009. // sections. Usually, there is an only one relocation section for
  2010. // each relocated section.
  2011. MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
  2012. uint64_t Ndx;
  2013. for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
  2014. if (Section.relocation_begin() == Section.relocation_end())
  2015. continue;
  2016. Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
  2017. if (!SecOrErr)
  2018. reportError(Obj->getFileName(),
  2019. "section (" + Twine(Ndx) +
  2020. "): unable to get a relocation target: " +
  2021. toString(SecOrErr.takeError()));
  2022. SecToRelSec[**SecOrErr].push_back(Section);
  2023. }
  2024. for (std::pair<SectionRef, std::vector<SectionRef>> &P : SecToRelSec) {
  2025. StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName());
  2026. outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n";
  2027. uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
  2028. uint32_t TypePadding = 24;
  2029. outs() << left_justify("OFFSET", OffsetPadding) << " "
  2030. << left_justify("TYPE", TypePadding) << " "
  2031. << "VALUE\n";
  2032. for (SectionRef Section : P.second) {
  2033. for (const RelocationRef &Reloc : Section.relocations()) {
  2034. uint64_t Address = Reloc.getOffset();
  2035. SmallString<32> RelocName;
  2036. SmallString<32> ValueStr;
  2037. if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
  2038. continue;
  2039. Reloc.getTypeName(RelocName);
  2040. if (Error E = getRelocationValueString(Reloc, ValueStr))
  2041. reportError(std::move(E), Obj->getFileName());
  2042. outs() << format(Fmt.data(), Address) << " "
  2043. << left_justify(RelocName, TypePadding) << " " << ValueStr
  2044. << "\n";
  2045. }
  2046. }
  2047. outs() << "\n";
  2048. }
  2049. }
  2050. void objdump::printDynamicRelocations(const ObjectFile *Obj) {
  2051. // For the moment, this option is for ELF only
  2052. if (!Obj->isELF())
  2053. return;
  2054. const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
  2055. if (!Elf || Elf->getEType() != ELF::ET_DYN) {
  2056. reportError(Obj->getFileName(), "not a dynamic object");
  2057. return;
  2058. }
  2059. std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
  2060. if (DynRelSec.empty())
  2061. return;
  2062. outs() << "DYNAMIC RELOCATION RECORDS\n";
  2063. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2064. for (const SectionRef &Section : DynRelSec)
  2065. for (const RelocationRef &Reloc : Section.relocations()) {
  2066. uint64_t Address = Reloc.getOffset();
  2067. SmallString<32> RelocName;
  2068. SmallString<32> ValueStr;
  2069. Reloc.getTypeName(RelocName);
  2070. if (Error E = getRelocationValueString(Reloc, ValueStr))
  2071. reportError(std::move(E), Obj->getFileName());
  2072. outs() << format(Fmt.data(), Address) << " " << RelocName << " "
  2073. << ValueStr << "\n";
  2074. }
  2075. }
  2076. // Returns true if we need to show LMA column when dumping section headers. We
  2077. // show it only when the platform is ELF and either we have at least one section
  2078. // whose VMA and LMA are different and/or when --show-lma flag is used.
  2079. static bool shouldDisplayLMA(const ObjectFile *Obj) {
  2080. if (!Obj->isELF())
  2081. return false;
  2082. for (const SectionRef &S : ToolSectionFilter(*Obj))
  2083. if (S.getAddress() != getELFSectionLMA(S))
  2084. return true;
  2085. return ShowLMA;
  2086. }
  2087. static size_t getMaxSectionNameWidth(const ObjectFile *Obj) {
  2088. // Default column width for names is 13 even if no names are that long.
  2089. size_t MaxWidth = 13;
  2090. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  2091. StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
  2092. MaxWidth = std::max(MaxWidth, Name.size());
  2093. }
  2094. return MaxWidth;
  2095. }
  2096. void objdump::printSectionHeaders(const ObjectFile *Obj) {
  2097. size_t NameWidth = getMaxSectionNameWidth(Obj);
  2098. size_t AddressWidth = 2 * Obj->getBytesInAddress();
  2099. bool HasLMAColumn = shouldDisplayLMA(Obj);
  2100. if (HasLMAColumn)
  2101. outs() << "Sections:\n"
  2102. "Idx "
  2103. << left_justify("Name", NameWidth) << " Size "
  2104. << left_justify("VMA", AddressWidth) << " "
  2105. << left_justify("LMA", AddressWidth) << " Type\n";
  2106. else
  2107. outs() << "Sections:\n"
  2108. "Idx "
  2109. << left_justify("Name", NameWidth) << " Size "
  2110. << left_justify("VMA", AddressWidth) << " Type\n";
  2111. uint64_t Idx;
  2112. for (const SectionRef &Section : ToolSectionFilter(*Obj, &Idx)) {
  2113. StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
  2114. uint64_t VMA = Section.getAddress();
  2115. if (shouldAdjustVA(Section))
  2116. VMA += AdjustVMA;
  2117. uint64_t Size = Section.getSize();
  2118. std::string Type = Section.isText() ? "TEXT" : "";
  2119. if (Section.isData())
  2120. Type += Type.empty() ? "DATA" : " DATA";
  2121. if (Section.isBSS())
  2122. Type += Type.empty() ? "BSS" : " BSS";
  2123. if (HasLMAColumn)
  2124. outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
  2125. Name.str().c_str(), Size)
  2126. << format_hex_no_prefix(VMA, AddressWidth) << " "
  2127. << format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
  2128. << " " << Type << "\n";
  2129. else
  2130. outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
  2131. Name.str().c_str(), Size)
  2132. << format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
  2133. }
  2134. outs() << "\n";
  2135. }
  2136. void objdump::printSectionContents(const ObjectFile *Obj) {
  2137. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
  2138. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  2139. StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
  2140. uint64_t BaseAddr = Section.getAddress();
  2141. uint64_t Size = Section.getSize();
  2142. if (!Size)
  2143. continue;
  2144. outs() << "Contents of section ";
  2145. StringRef SegmentName = getSegmentName(MachO, Section);
  2146. if (!SegmentName.empty())
  2147. outs() << SegmentName << ",";
  2148. outs() << Name << ":\n";
  2149. if (Section.isBSS()) {
  2150. outs() << format("<skipping contents of bss section at [%04" PRIx64
  2151. ", %04" PRIx64 ")>\n",
  2152. BaseAddr, BaseAddr + Size);
  2153. continue;
  2154. }
  2155. StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
  2156. // Dump out the content as hex and printable ascii characters.
  2157. for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
  2158. outs() << format(" %04" PRIx64 " ", BaseAddr + Addr);
  2159. // Dump line of hex.
  2160. for (std::size_t I = 0; I < 16; ++I) {
  2161. if (I != 0 && I % 4 == 0)
  2162. outs() << ' ';
  2163. if (Addr + I < End)
  2164. outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true)
  2165. << hexdigit(Contents[Addr + I] & 0xF, true);
  2166. else
  2167. outs() << " ";
  2168. }
  2169. // Print ascii.
  2170. outs() << " ";
  2171. for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) {
  2172. if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF))
  2173. outs() << Contents[Addr + I];
  2174. else
  2175. outs() << ".";
  2176. }
  2177. outs() << "\n";
  2178. }
  2179. }
  2180. }
  2181. void objdump::printSymbolTable(const ObjectFile *O, StringRef ArchiveName,
  2182. StringRef ArchitectureName, bool DumpDynamic) {
  2183. if (O->isCOFF() && !DumpDynamic) {
  2184. outs() << "SYMBOL TABLE:\n";
  2185. printCOFFSymbolTable(cast<const COFFObjectFile>(O));
  2186. return;
  2187. }
  2188. const StringRef FileName = O->getFileName();
  2189. if (!DumpDynamic) {
  2190. outs() << "SYMBOL TABLE:\n";
  2191. for (auto I = O->symbol_begin(); I != O->symbol_end(); ++I)
  2192. printSymbol(O, *I, FileName, ArchiveName, ArchitectureName, DumpDynamic);
  2193. return;
  2194. }
  2195. outs() << "DYNAMIC SYMBOL TABLE:\n";
  2196. if (!O->isELF()) {
  2197. reportWarning(
  2198. "this operation is not currently supported for this file format",
  2199. FileName);
  2200. return;
  2201. }
  2202. const ELFObjectFileBase *ELF = cast<const ELFObjectFileBase>(O);
  2203. for (auto I = ELF->getDynamicSymbolIterators().begin();
  2204. I != ELF->getDynamicSymbolIterators().end(); ++I)
  2205. printSymbol(O, *I, FileName, ArchiveName, ArchitectureName, DumpDynamic);
  2206. }
  2207. void objdump::printSymbol(const ObjectFile *O, const SymbolRef &Symbol,
  2208. StringRef FileName, StringRef ArchiveName,
  2209. StringRef ArchitectureName, bool DumpDynamic) {
  2210. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(O);
  2211. uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName, ArchiveName,
  2212. ArchitectureName);
  2213. if ((Address < StartAddress) || (Address > StopAddress))
  2214. return;
  2215. SymbolRef::Type Type =
  2216. unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
  2217. uint32_t Flags =
  2218. unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
  2219. // Don't ask a Mach-O STAB symbol for its section unless you know that
  2220. // STAB symbol's section field refers to a valid section index. Otherwise
  2221. // the symbol may error trying to load a section that does not exist.
  2222. bool IsSTAB = false;
  2223. if (MachO) {
  2224. DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
  2225. uint8_t NType =
  2226. (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
  2227. : MachO->getSymbolTableEntry(SymDRI).n_type);
  2228. if (NType & MachO::N_STAB)
  2229. IsSTAB = true;
  2230. }
  2231. section_iterator Section = IsSTAB
  2232. ? O->section_end()
  2233. : unwrapOrError(Symbol.getSection(), FileName,
  2234. ArchiveName, ArchitectureName);
  2235. StringRef Name;
  2236. if (Type == SymbolRef::ST_Debug && Section != O->section_end()) {
  2237. if (Expected<StringRef> NameOrErr = Section->getName())
  2238. Name = *NameOrErr;
  2239. else
  2240. consumeError(NameOrErr.takeError());
  2241. } else {
  2242. Name = unwrapOrError(Symbol.getName(), FileName, ArchiveName,
  2243. ArchitectureName);
  2244. }
  2245. bool Global = Flags & SymbolRef::SF_Global;
  2246. bool Weak = Flags & SymbolRef::SF_Weak;
  2247. bool Absolute = Flags & SymbolRef::SF_Absolute;
  2248. bool Common = Flags & SymbolRef::SF_Common;
  2249. bool Hidden = Flags & SymbolRef::SF_Hidden;
  2250. char GlobLoc = ' ';
  2251. if ((Section != O->section_end() || Absolute) && !Weak)
  2252. GlobLoc = Global ? 'g' : 'l';
  2253. char IFunc = ' ';
  2254. if (O->isELF()) {
  2255. if (ELFSymbolRef(Symbol).getELFType() == ELF::STT_GNU_IFUNC)
  2256. IFunc = 'i';
  2257. if (ELFSymbolRef(Symbol).getBinding() == ELF::STB_GNU_UNIQUE)
  2258. GlobLoc = 'u';
  2259. }
  2260. char Debug = ' ';
  2261. if (DumpDynamic)
  2262. Debug = 'D';
  2263. else if (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
  2264. Debug = 'd';
  2265. char FileFunc = ' ';
  2266. if (Type == SymbolRef::ST_File)
  2267. FileFunc = 'f';
  2268. else if (Type == SymbolRef::ST_Function)
  2269. FileFunc = 'F';
  2270. else if (Type == SymbolRef::ST_Data)
  2271. FileFunc = 'O';
  2272. const char *Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2273. outs() << format(Fmt, Address) << " "
  2274. << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
  2275. << (Weak ? 'w' : ' ') // Weak?
  2276. << ' ' // Constructor. Not supported yet.
  2277. << ' ' // Warning. Not supported yet.
  2278. << IFunc // Indirect reference to another symbol.
  2279. << Debug // Debugging (d) or dynamic (D) symbol.
  2280. << FileFunc // Name of function (F), file (f) or object (O).
  2281. << ' ';
  2282. if (Absolute) {
  2283. outs() << "*ABS*";
  2284. } else if (Common) {
  2285. outs() << "*COM*";
  2286. } else if (Section == O->section_end()) {
  2287. outs() << "*UND*";
  2288. } else {
  2289. StringRef SegmentName = getSegmentName(MachO, *Section);
  2290. if (!SegmentName.empty())
  2291. outs() << SegmentName << ",";
  2292. StringRef SectionName = unwrapOrError(Section->getName(), FileName);
  2293. outs() << SectionName;
  2294. }
  2295. if (Common || O->isELF()) {
  2296. uint64_t Val =
  2297. Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
  2298. outs() << '\t' << format(Fmt, Val);
  2299. }
  2300. if (O->isELF()) {
  2301. uint8_t Other = ELFSymbolRef(Symbol).getOther();
  2302. switch (Other) {
  2303. case ELF::STV_DEFAULT:
  2304. break;
  2305. case ELF::STV_INTERNAL:
  2306. outs() << " .internal";
  2307. break;
  2308. case ELF::STV_HIDDEN:
  2309. outs() << " .hidden";
  2310. break;
  2311. case ELF::STV_PROTECTED:
  2312. outs() << " .protected";
  2313. break;
  2314. default:
  2315. outs() << format(" 0x%02x", Other);
  2316. break;
  2317. }
  2318. } else if (Hidden) {
  2319. outs() << " .hidden";
  2320. }
  2321. if (Demangle)
  2322. outs() << ' ' << demangle(std::string(Name)) << '\n';
  2323. else
  2324. outs() << ' ' << Name << '\n';
  2325. }
  2326. static void printUnwindInfo(const ObjectFile *O) {
  2327. outs() << "Unwind info:\n\n";
  2328. if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O))
  2329. printCOFFUnwindInfo(Coff);
  2330. else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O))
  2331. printMachOUnwindInfo(MachO);
  2332. else
  2333. // TODO: Extract DWARF dump tool to objdump.
  2334. WithColor::error(errs(), ToolName)
  2335. << "This operation is only currently supported "
  2336. "for COFF and MachO object files.\n";
  2337. }
  2338. /// Dump the raw contents of the __clangast section so the output can be piped
  2339. /// into llvm-bcanalyzer.
  2340. static void printRawClangAST(const ObjectFile *Obj) {
  2341. if (outs().is_displayed()) {
  2342. WithColor::error(errs(), ToolName)
  2343. << "The -raw-clang-ast option will dump the raw binary contents of "
  2344. "the clang ast section.\n"
  2345. "Please redirect the output to a file or another program such as "
  2346. "llvm-bcanalyzer.\n";
  2347. return;
  2348. }
  2349. StringRef ClangASTSectionName("__clangast");
  2350. if (Obj->isCOFF()) {
  2351. ClangASTSectionName = "clangast";
  2352. }
  2353. Optional<object::SectionRef> ClangASTSection;
  2354. for (auto Sec : ToolSectionFilter(*Obj)) {
  2355. StringRef Name;
  2356. if (Expected<StringRef> NameOrErr = Sec.getName())
  2357. Name = *NameOrErr;
  2358. else
  2359. consumeError(NameOrErr.takeError());
  2360. if (Name == ClangASTSectionName) {
  2361. ClangASTSection = Sec;
  2362. break;
  2363. }
  2364. }
  2365. if (!ClangASTSection)
  2366. return;
  2367. StringRef ClangASTContents = unwrapOrError(
  2368. ClangASTSection.getValue().getContents(), Obj->getFileName());
  2369. outs().write(ClangASTContents.data(), ClangASTContents.size());
  2370. }
  2371. static void printFaultMaps(const ObjectFile *Obj) {
  2372. StringRef FaultMapSectionName;
  2373. if (Obj->isELF()) {
  2374. FaultMapSectionName = ".llvm_faultmaps";
  2375. } else if (Obj->isMachO()) {
  2376. FaultMapSectionName = "__llvm_faultmaps";
  2377. } else {
  2378. WithColor::error(errs(), ToolName)
  2379. << "This operation is only currently supported "
  2380. "for ELF and Mach-O executable files.\n";
  2381. return;
  2382. }
  2383. Optional<object::SectionRef> FaultMapSection;
  2384. for (auto Sec : ToolSectionFilter(*Obj)) {
  2385. StringRef Name;
  2386. if (Expected<StringRef> NameOrErr = Sec.getName())
  2387. Name = *NameOrErr;
  2388. else
  2389. consumeError(NameOrErr.takeError());
  2390. if (Name == FaultMapSectionName) {
  2391. FaultMapSection = Sec;
  2392. break;
  2393. }
  2394. }
  2395. outs() << "FaultMap table:\n";
  2396. if (!FaultMapSection.hasValue()) {
  2397. outs() << "<not found>\n";
  2398. return;
  2399. }
  2400. StringRef FaultMapContents =
  2401. unwrapOrError(FaultMapSection.getValue().getContents(), Obj->getFileName());
  2402. FaultMapParser FMP(FaultMapContents.bytes_begin(),
  2403. FaultMapContents.bytes_end());
  2404. outs() << FMP;
  2405. }
  2406. static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) {
  2407. if (O->isELF()) {
  2408. printELFFileHeader(O);
  2409. printELFDynamicSection(O);
  2410. printELFSymbolVersionInfo(O);
  2411. return;
  2412. }
  2413. if (O->isCOFF())
  2414. return printCOFFFileHeader(O);
  2415. if (O->isWasm())
  2416. return printWasmFileHeader(O);
  2417. if (O->isMachO()) {
  2418. printMachOFileHeader(O);
  2419. if (!OnlyFirst)
  2420. printMachOLoadCommands(O);
  2421. return;
  2422. }
  2423. reportError(O->getFileName(), "Invalid/Unsupported object file format");
  2424. }
  2425. static void printFileHeaders(const ObjectFile *O) {
  2426. if (!O->isELF() && !O->isCOFF())
  2427. reportError(O->getFileName(), "Invalid/Unsupported object file format");
  2428. Triple::ArchType AT = O->getArch();
  2429. outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
  2430. uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
  2431. StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2432. outs() << "start address: "
  2433. << "0x" << format(Fmt.data(), Address) << "\n\n";
  2434. }
  2435. static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
  2436. Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
  2437. if (!ModeOrErr) {
  2438. WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n";
  2439. consumeError(ModeOrErr.takeError());
  2440. return;
  2441. }
  2442. sys::fs::perms Mode = ModeOrErr.get();
  2443. outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
  2444. outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
  2445. outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
  2446. outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
  2447. outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
  2448. outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
  2449. outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
  2450. outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
  2451. outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
  2452. outs() << " ";
  2453. outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
  2454. unwrapOrError(C.getGID(), Filename),
  2455. unwrapOrError(C.getRawSize(), Filename));
  2456. StringRef RawLastModified = C.getRawLastModified();
  2457. unsigned Seconds;
  2458. if (RawLastModified.getAsInteger(10, Seconds))
  2459. outs() << "(date: \"" << RawLastModified
  2460. << "\" contains non-decimal chars) ";
  2461. else {
  2462. // Since ctime(3) returns a 26 character string of the form:
  2463. // "Sun Sep 16 01:03:52 1973\n\0"
  2464. // just print 24 characters.
  2465. time_t t = Seconds;
  2466. outs() << format("%.24s ", ctime(&t));
  2467. }
  2468. StringRef Name = "";
  2469. Expected<StringRef> NameOrErr = C.getName();
  2470. if (!NameOrErr) {
  2471. consumeError(NameOrErr.takeError());
  2472. Name = unwrapOrError(C.getRawName(), Filename);
  2473. } else {
  2474. Name = NameOrErr.get();
  2475. }
  2476. outs() << Name << "\n";
  2477. }
  2478. // For ELF only now.
  2479. static bool shouldWarnForInvalidStartStopAddress(ObjectFile *Obj) {
  2480. if (const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj)) {
  2481. if (Elf->getEType() != ELF::ET_REL)
  2482. return true;
  2483. }
  2484. return false;
  2485. }
  2486. static void checkForInvalidStartStopAddress(ObjectFile *Obj,
  2487. uint64_t Start, uint64_t Stop) {
  2488. if (!shouldWarnForInvalidStartStopAddress(Obj))
  2489. return;
  2490. for (const SectionRef &Section : Obj->sections())
  2491. if (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC) {
  2492. uint64_t BaseAddr = Section.getAddress();
  2493. uint64_t Size = Section.getSize();
  2494. if ((Start < BaseAddr + Size) && Stop > BaseAddr)
  2495. return;
  2496. }
  2497. if (StartAddress.getNumOccurrences() == 0)
  2498. reportWarning("no section has address less than 0x" +
  2499. Twine::utohexstr(Stop) + " specified by --stop-address",
  2500. Obj->getFileName());
  2501. else if (StopAddress.getNumOccurrences() == 0)
  2502. reportWarning("no section has address greater than or equal to 0x" +
  2503. Twine::utohexstr(Start) + " specified by --start-address",
  2504. Obj->getFileName());
  2505. else
  2506. reportWarning("no section overlaps the range [0x" +
  2507. Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) +
  2508. ") specified by --start-address/--stop-address",
  2509. Obj->getFileName());
  2510. }
  2511. static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
  2512. const Archive::Child *C = nullptr) {
  2513. // Avoid other output when using a raw option.
  2514. if (!RawClangAST) {
  2515. outs() << '\n';
  2516. if (A)
  2517. outs() << A->getFileName() << "(" << O->getFileName() << ")";
  2518. else
  2519. outs() << O->getFileName();
  2520. outs() << ":\tfile format " << O->getFileFormatName().lower() << "\n\n";
  2521. }
  2522. if (StartAddress.getNumOccurrences() || StopAddress.getNumOccurrences())
  2523. checkForInvalidStartStopAddress(O, StartAddress, StopAddress);
  2524. // Note: the order here matches GNU objdump for compatability.
  2525. StringRef ArchiveName = A ? A->getFileName() : "";
  2526. if (ArchiveHeaders && !MachOOpt && C)
  2527. printArchiveChild(ArchiveName, *C);
  2528. if (FileHeaders)
  2529. printFileHeaders(O);
  2530. if (PrivateHeaders || FirstPrivateHeader)
  2531. printPrivateFileHeaders(O, FirstPrivateHeader);
  2532. if (SectionHeaders)
  2533. printSectionHeaders(O);
  2534. if (SymbolTable)
  2535. printSymbolTable(O, ArchiveName);
  2536. if (DynamicSymbolTable)
  2537. printSymbolTable(O, ArchiveName, /*ArchitectureName=*/"",
  2538. /*DumpDynamic=*/true);
  2539. if (DwarfDumpType != DIDT_Null) {
  2540. std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O);
  2541. // Dump the complete DWARF structure.
  2542. DIDumpOptions DumpOpts;
  2543. DumpOpts.DumpType = DwarfDumpType;
  2544. DICtx->dump(outs(), DumpOpts);
  2545. }
  2546. if (Relocations && !Disassemble)
  2547. printRelocations(O);
  2548. if (DynamicRelocations)
  2549. printDynamicRelocations(O);
  2550. if (SectionContents)
  2551. printSectionContents(O);
  2552. if (Disassemble)
  2553. disassembleObject(O, Relocations);
  2554. if (UnwindInfo)
  2555. printUnwindInfo(O);
  2556. // Mach-O specific options:
  2557. if (ExportsTrie)
  2558. printExportsTrie(O);
  2559. if (Rebase)
  2560. printRebaseTable(O);
  2561. if (Bind)
  2562. printBindTable(O);
  2563. if (LazyBind)
  2564. printLazyBindTable(O);
  2565. if (WeakBind)
  2566. printWeakBindTable(O);
  2567. // Other special sections:
  2568. if (RawClangAST)
  2569. printRawClangAST(O);
  2570. if (FaultMapSection)
  2571. printFaultMaps(O);
  2572. }
  2573. static void dumpObject(const COFFImportFile *I, const Archive *A,
  2574. const Archive::Child *C = nullptr) {
  2575. StringRef ArchiveName = A ? A->getFileName() : "";
  2576. // Avoid other output when using a raw option.
  2577. if (!RawClangAST)
  2578. outs() << '\n'
  2579. << ArchiveName << "(" << I->getFileName() << ")"
  2580. << ":\tfile format COFF-import-file"
  2581. << "\n\n";
  2582. if (ArchiveHeaders && !MachOOpt && C)
  2583. printArchiveChild(ArchiveName, *C);
  2584. if (SymbolTable)
  2585. printCOFFSymbolTable(I);
  2586. }
  2587. /// Dump each object file in \a a;
  2588. static void dumpArchive(const Archive *A) {
  2589. Error Err = Error::success();
  2590. unsigned I = -1;
  2591. for (auto &C : A->children(Err)) {
  2592. ++I;
  2593. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2594. if (!ChildOrErr) {
  2595. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2596. reportError(std::move(E), getFileNameForError(C, I), A->getFileName());
  2597. continue;
  2598. }
  2599. if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  2600. dumpObject(O, A, &C);
  2601. else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
  2602. dumpObject(I, A, &C);
  2603. else
  2604. reportError(errorCodeToError(object_error::invalid_file_type),
  2605. A->getFileName());
  2606. }
  2607. if (Err)
  2608. reportError(std::move(Err), A->getFileName());
  2609. }
  2610. /// Open file and figure out how to dump it.
  2611. static void dumpInput(StringRef file) {
  2612. // If we are using the Mach-O specific object file parser, then let it parse
  2613. // the file and process the command line options. So the -arch flags can
  2614. // be used to select specific slices, etc.
  2615. if (MachOOpt) {
  2616. parseInputMachO(file);
  2617. return;
  2618. }
  2619. // Attempt to open the binary.
  2620. OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
  2621. Binary &Binary = *OBinary.getBinary();
  2622. if (Archive *A = dyn_cast<Archive>(&Binary))
  2623. dumpArchive(A);
  2624. else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary))
  2625. dumpObject(O);
  2626. else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
  2627. parseInputMachO(UB);
  2628. else
  2629. reportError(errorCodeToError(object_error::invalid_file_type), file);
  2630. }
  2631. int main(int argc, char **argv) {
  2632. using namespace llvm;
  2633. InitLLVM X(argc, argv);
  2634. const cl::OptionCategory *OptionFilters[] = {&ObjdumpCat, &MachOCat};
  2635. cl::HideUnrelatedOptions(OptionFilters);
  2636. // Initialize targets and assembly printers/parsers.
  2637. InitializeAllTargetInfos();
  2638. InitializeAllTargetMCs();
  2639. InitializeAllDisassemblers();
  2640. // Register the target printer for --version.
  2641. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  2642. cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n", nullptr,
  2643. /*EnvVar=*/nullptr,
  2644. /*LongOptionsUseDoubleDash=*/true);
  2645. if (StartAddress >= StopAddress)
  2646. reportCmdLineError("start address should be less than stop address");
  2647. ToolName = argv[0];
  2648. // Defaults to a.out if no filenames specified.
  2649. if (InputFilenames.empty())
  2650. InputFilenames.push_back("a.out");
  2651. // Removes trailing separators from prefix.
  2652. while (!Prefix.empty() && sys::path::is_separator(Prefix.back()))
  2653. Prefix.pop_back();
  2654. if (AllHeaders)
  2655. ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
  2656. SectionHeaders = SymbolTable = true;
  2657. if (DisassembleAll || PrintSource || PrintLines ||
  2658. !DisassembleSymbols.empty())
  2659. Disassemble = true;
  2660. if (!ArchiveHeaders && !Disassemble && DwarfDumpType == DIDT_Null &&
  2661. !DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
  2662. !Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
  2663. !DynamicSymbolTable && !UnwindInfo && !FaultMapSection &&
  2664. !(MachOOpt &&
  2665. (Bind || DataInCode || DylibId || DylibsUsed || ExportsTrie ||
  2666. FirstPrivateHeader || IndirectSymbols || InfoPlist || LazyBind ||
  2667. LinkOptHints || ObjcMetaData || Rebase || UniversalHeaders ||
  2668. WeakBind || !FilterSections.empty()))) {
  2669. cl::PrintHelpMessage();
  2670. return 2;
  2671. }
  2672. DisasmSymbolSet.insert(DisassembleSymbols.begin(), DisassembleSymbols.end());
  2673. llvm::for_each(InputFilenames, dumpInput);
  2674. warnOnNoMatchForSections();
  2675. return EXIT_SUCCESS;
  2676. }