llvm-objdump.cpp 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  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 "ObjdumpOptID.h"
  22. #include "OffloadDump.h"
  23. #include "SourcePrinter.h"
  24. #include "WasmDump.h"
  25. #include "XCOFFDump.h"
  26. #include "llvm/ADT/IndexedMap.h"
  27. #include "llvm/ADT/STLExtras.h"
  28. #include "llvm/ADT/SetOperations.h"
  29. #include "llvm/ADT/SmallSet.h"
  30. #include "llvm/ADT/StringExtras.h"
  31. #include "llvm/ADT/StringSet.h"
  32. #include "llvm/ADT/Triple.h"
  33. #include "llvm/ADT/Twine.h"
  34. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  35. #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
  36. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  37. #include "llvm/Debuginfod/BuildIDFetcher.h"
  38. #include "llvm/Debuginfod/Debuginfod.h"
  39. #include "llvm/Debuginfod/HTTPClient.h"
  40. #include "llvm/Demangle/Demangle.h"
  41. #include "llvm/MC/MCAsmInfo.h"
  42. #include "llvm/MC/MCContext.h"
  43. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  44. #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
  45. #include "llvm/MC/MCInst.h"
  46. #include "llvm/MC/MCInstPrinter.h"
  47. #include "llvm/MC/MCInstrAnalysis.h"
  48. #include "llvm/MC/MCInstrInfo.h"
  49. #include "llvm/MC/MCObjectFileInfo.h"
  50. #include "llvm/MC/MCRegisterInfo.h"
  51. #include "llvm/MC/MCSubtargetInfo.h"
  52. #include "llvm/MC/MCTargetOptions.h"
  53. #include "llvm/MC/TargetRegistry.h"
  54. #include "llvm/Object/Archive.h"
  55. #include "llvm/Object/BuildID.h"
  56. #include "llvm/Object/COFF.h"
  57. #include "llvm/Object/COFFImportFile.h"
  58. #include "llvm/Object/ELFObjectFile.h"
  59. #include "llvm/Object/ELFTypes.h"
  60. #include "llvm/Object/FaultMapParser.h"
  61. #include "llvm/Object/MachO.h"
  62. #include "llvm/Object/MachOUniversal.h"
  63. #include "llvm/Object/ObjectFile.h"
  64. #include "llvm/Object/OffloadBinary.h"
  65. #include "llvm/Object/Wasm.h"
  66. #include "llvm/Option/Arg.h"
  67. #include "llvm/Option/ArgList.h"
  68. #include "llvm/Option/Option.h"
  69. #include "llvm/Support/Casting.h"
  70. #include "llvm/Support/Debug.h"
  71. #include "llvm/Support/Errc.h"
  72. #include "llvm/Support/FileSystem.h"
  73. #include "llvm/Support/Format.h"
  74. #include "llvm/Support/FormatVariadic.h"
  75. #include "llvm/Support/GraphWriter.h"
  76. #include "llvm/Support/Host.h"
  77. #include "llvm/Support/InitLLVM.h"
  78. #include "llvm/Support/MemoryBuffer.h"
  79. #include "llvm/Support/SourceMgr.h"
  80. #include "llvm/Support/StringSaver.h"
  81. #include "llvm/Support/TargetSelect.h"
  82. #include "llvm/Support/WithColor.h"
  83. #include "llvm/Support/raw_ostream.h"
  84. #include <algorithm>
  85. #include <cctype>
  86. #include <cstring>
  87. #include <optional>
  88. #include <system_error>
  89. #include <unordered_map>
  90. #include <utility>
  91. using namespace llvm;
  92. using namespace llvm::object;
  93. using namespace llvm::objdump;
  94. using namespace llvm::opt;
  95. namespace {
  96. class CommonOptTable : public opt::GenericOptTable {
  97. public:
  98. CommonOptTable(ArrayRef<Info> OptionInfos, const char *Usage,
  99. const char *Description)
  100. : opt::GenericOptTable(OptionInfos), Usage(Usage),
  101. Description(Description) {
  102. setGroupedShortOptions(true);
  103. }
  104. void printHelp(StringRef Argv0, bool ShowHidden = false) const {
  105. Argv0 = sys::path::filename(Argv0);
  106. opt::GenericOptTable::printHelp(outs(), (Argv0 + Usage).str().c_str(),
  107. Description, ShowHidden, ShowHidden);
  108. // TODO Replace this with OptTable API once it adds extrahelp support.
  109. outs() << "\nPass @FILE as argument to read options from FILE.\n";
  110. }
  111. private:
  112. const char *Usage;
  113. const char *Description;
  114. };
  115. // ObjdumpOptID is in ObjdumpOptID.h
  116. namespace objdump_opt {
  117. #define PREFIX(NAME, VALUE) \
  118. static constexpr StringLiteral NAME##_init[] = VALUE; \
  119. static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
  120. std::size(NAME##_init) - 1);
  121. #include "ObjdumpOpts.inc"
  122. #undef PREFIX
  123. static constexpr opt::OptTable::Info ObjdumpInfoTable[] = {
  124. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  125. HELPTEXT, METAVAR, VALUES) \
  126. {PREFIX, NAME, HELPTEXT, \
  127. METAVAR, OBJDUMP_##ID, opt::Option::KIND##Class, \
  128. PARAM, FLAGS, OBJDUMP_##GROUP, \
  129. OBJDUMP_##ALIAS, ALIASARGS, VALUES},
  130. #include "ObjdumpOpts.inc"
  131. #undef OPTION
  132. };
  133. } // namespace objdump_opt
  134. class ObjdumpOptTable : public CommonOptTable {
  135. public:
  136. ObjdumpOptTable()
  137. : CommonOptTable(objdump_opt::ObjdumpInfoTable,
  138. " [options] <input object files>",
  139. "llvm object file dumper") {}
  140. };
  141. enum OtoolOptID {
  142. OTOOL_INVALID = 0, // This is not an option ID.
  143. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  144. HELPTEXT, METAVAR, VALUES) \
  145. OTOOL_##ID,
  146. #include "OtoolOpts.inc"
  147. #undef OPTION
  148. };
  149. namespace otool {
  150. #define PREFIX(NAME, VALUE) \
  151. static constexpr StringLiteral NAME##_init[] = VALUE; \
  152. static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
  153. std::size(NAME##_init) - 1);
  154. #include "OtoolOpts.inc"
  155. #undef PREFIX
  156. static constexpr opt::OptTable::Info OtoolInfoTable[] = {
  157. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  158. HELPTEXT, METAVAR, VALUES) \
  159. {PREFIX, NAME, HELPTEXT, \
  160. METAVAR, OTOOL_##ID, opt::Option::KIND##Class, \
  161. PARAM, FLAGS, OTOOL_##GROUP, \
  162. OTOOL_##ALIAS, ALIASARGS, VALUES},
  163. #include "OtoolOpts.inc"
  164. #undef OPTION
  165. };
  166. } // namespace otool
  167. class OtoolOptTable : public CommonOptTable {
  168. public:
  169. OtoolOptTable()
  170. : CommonOptTable(otool::OtoolInfoTable, " [option...] [file...]",
  171. "Mach-O object file displaying tool") {}
  172. };
  173. } // namespace
  174. #define DEBUG_TYPE "objdump"
  175. static uint64_t AdjustVMA;
  176. static bool AllHeaders;
  177. static std::string ArchName;
  178. bool objdump::ArchiveHeaders;
  179. bool objdump::Demangle;
  180. bool objdump::Disassemble;
  181. bool objdump::DisassembleAll;
  182. bool objdump::SymbolDescription;
  183. static std::vector<std::string> DisassembleSymbols;
  184. static bool DisassembleZeroes;
  185. static std::vector<std::string> DisassemblerOptions;
  186. DIDumpType objdump::DwarfDumpType;
  187. static bool DynamicRelocations;
  188. static bool FaultMapSection;
  189. static bool FileHeaders;
  190. bool objdump::SectionContents;
  191. static std::vector<std::string> InputFilenames;
  192. bool objdump::PrintLines;
  193. static bool MachOOpt;
  194. std::string objdump::MCPU;
  195. std::vector<std::string> objdump::MAttrs;
  196. bool objdump::ShowRawInsn;
  197. bool objdump::LeadingAddr;
  198. static bool Offloading;
  199. static bool RawClangAST;
  200. bool objdump::Relocations;
  201. bool objdump::PrintImmHex;
  202. bool objdump::PrivateHeaders;
  203. std::vector<std::string> objdump::FilterSections;
  204. bool objdump::SectionHeaders;
  205. static bool ShowAllSymbols;
  206. static bool ShowLMA;
  207. bool objdump::PrintSource;
  208. static uint64_t StartAddress;
  209. static bool HasStartAddressFlag;
  210. static uint64_t StopAddress = UINT64_MAX;
  211. static bool HasStopAddressFlag;
  212. bool objdump::SymbolTable;
  213. static bool SymbolizeOperands;
  214. static bool DynamicSymbolTable;
  215. std::string objdump::TripleName;
  216. bool objdump::UnwindInfo;
  217. static bool Wide;
  218. std::string objdump::Prefix;
  219. uint32_t objdump::PrefixStrip;
  220. DebugVarsFormat objdump::DbgVariables = DVDisabled;
  221. int objdump::DbgIndent = 52;
  222. static StringSet<> DisasmSymbolSet;
  223. StringSet<> objdump::FoundSectionSet;
  224. static StringRef ToolName;
  225. std::unique_ptr<BuildIDFetcher> BIDFetcher;
  226. ExitOnError ExitOnErr;
  227. namespace {
  228. struct FilterResult {
  229. // True if the section should not be skipped.
  230. bool Keep;
  231. // True if the index counter should be incremented, even if the section should
  232. // be skipped. For example, sections may be skipped if they are not included
  233. // in the --section flag, but we still want those to count toward the section
  234. // count.
  235. bool IncrementIndex;
  236. };
  237. } // namespace
  238. static FilterResult checkSectionFilter(object::SectionRef S) {
  239. if (FilterSections.empty())
  240. return {/*Keep=*/true, /*IncrementIndex=*/true};
  241. Expected<StringRef> SecNameOrErr = S.getName();
  242. if (!SecNameOrErr) {
  243. consumeError(SecNameOrErr.takeError());
  244. return {/*Keep=*/false, /*IncrementIndex=*/false};
  245. }
  246. StringRef SecName = *SecNameOrErr;
  247. // StringSet does not allow empty key so avoid adding sections with
  248. // no name (such as the section with index 0) here.
  249. if (!SecName.empty())
  250. FoundSectionSet.insert(SecName);
  251. // Only show the section if it's in the FilterSections list, but always
  252. // increment so the indexing is stable.
  253. return {/*Keep=*/is_contained(FilterSections, SecName),
  254. /*IncrementIndex=*/true};
  255. }
  256. SectionFilter objdump::ToolSectionFilter(object::ObjectFile const &O,
  257. uint64_t *Idx) {
  258. // Start at UINT64_MAX so that the first index returned after an increment is
  259. // zero (after the unsigned wrap).
  260. if (Idx)
  261. *Idx = UINT64_MAX;
  262. return SectionFilter(
  263. [Idx](object::SectionRef S) {
  264. FilterResult Result = checkSectionFilter(S);
  265. if (Idx != nullptr && Result.IncrementIndex)
  266. *Idx += 1;
  267. return Result.Keep;
  268. },
  269. O);
  270. }
  271. std::string objdump::getFileNameForError(const object::Archive::Child &C,
  272. unsigned Index) {
  273. Expected<StringRef> NameOrErr = C.getName();
  274. if (NameOrErr)
  275. return std::string(NameOrErr.get());
  276. // If we have an error getting the name then we print the index of the archive
  277. // member. Since we are already in an error state, we just ignore this error.
  278. consumeError(NameOrErr.takeError());
  279. return "<file index: " + std::to_string(Index) + ">";
  280. }
  281. void objdump::reportWarning(const Twine &Message, StringRef File) {
  282. // Output order between errs() and outs() matters especially for archive
  283. // files where the output is per member object.
  284. outs().flush();
  285. WithColor::warning(errs(), ToolName)
  286. << "'" << File << "': " << Message << "\n";
  287. }
  288. [[noreturn]] void objdump::reportError(StringRef File, const Twine &Message) {
  289. outs().flush();
  290. WithColor::error(errs(), ToolName) << "'" << File << "': " << Message << "\n";
  291. exit(1);
  292. }
  293. [[noreturn]] void objdump::reportError(Error E, StringRef FileName,
  294. StringRef ArchiveName,
  295. StringRef ArchitectureName) {
  296. assert(E);
  297. outs().flush();
  298. WithColor::error(errs(), ToolName);
  299. if (ArchiveName != "")
  300. errs() << ArchiveName << "(" << FileName << ")";
  301. else
  302. errs() << "'" << FileName << "'";
  303. if (!ArchitectureName.empty())
  304. errs() << " (for architecture " << ArchitectureName << ")";
  305. errs() << ": ";
  306. logAllUnhandledErrors(std::move(E), errs());
  307. exit(1);
  308. }
  309. static void reportCmdLineWarning(const Twine &Message) {
  310. WithColor::warning(errs(), ToolName) << Message << "\n";
  311. }
  312. [[noreturn]] static void reportCmdLineError(const Twine &Message) {
  313. WithColor::error(errs(), ToolName) << Message << "\n";
  314. exit(1);
  315. }
  316. static void warnOnNoMatchForSections() {
  317. SetVector<StringRef> MissingSections;
  318. for (StringRef S : FilterSections) {
  319. if (FoundSectionSet.count(S))
  320. return;
  321. // User may specify a unnamed section. Don't warn for it.
  322. if (!S.empty())
  323. MissingSections.insert(S);
  324. }
  325. // Warn only if no section in FilterSections is matched.
  326. for (StringRef S : MissingSections)
  327. reportCmdLineWarning("section '" + S +
  328. "' mentioned in a -j/--section option, but not "
  329. "found in any input file");
  330. }
  331. static const Target *getTarget(const ObjectFile *Obj) {
  332. // Figure out the target triple.
  333. Triple TheTriple("unknown-unknown-unknown");
  334. if (TripleName.empty()) {
  335. TheTriple = Obj->makeTriple();
  336. } else {
  337. TheTriple.setTriple(Triple::normalize(TripleName));
  338. auto Arch = Obj->getArch();
  339. if (Arch == Triple::arm || Arch == Triple::armeb)
  340. Obj->setARMSubArch(TheTriple);
  341. }
  342. // Get the target specific parser.
  343. std::string Error;
  344. const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
  345. Error);
  346. if (!TheTarget)
  347. reportError(Obj->getFileName(), "can't find target: " + Error);
  348. // Update the triple name and return the found target.
  349. TripleName = TheTriple.getTriple();
  350. return TheTarget;
  351. }
  352. bool objdump::isRelocAddressLess(RelocationRef A, RelocationRef B) {
  353. return A.getOffset() < B.getOffset();
  354. }
  355. static Error getRelocationValueString(const RelocationRef &Rel,
  356. SmallVectorImpl<char> &Result) {
  357. const ObjectFile *Obj = Rel.getObject();
  358. if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
  359. return getELFRelocationValueString(ELF, Rel, Result);
  360. if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
  361. return getCOFFRelocationValueString(COFF, Rel, Result);
  362. if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
  363. return getWasmRelocationValueString(Wasm, Rel, Result);
  364. if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
  365. return getMachORelocationValueString(MachO, Rel, Result);
  366. if (auto *XCOFF = dyn_cast<XCOFFObjectFile>(Obj))
  367. return getXCOFFRelocationValueString(*XCOFF, Rel, Result);
  368. llvm_unreachable("unknown object file format");
  369. }
  370. /// Indicates whether this relocation should hidden when listing
  371. /// relocations, usually because it is the trailing part of a multipart
  372. /// relocation that will be printed as part of the leading relocation.
  373. static bool getHidden(RelocationRef RelRef) {
  374. auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject());
  375. if (!MachO)
  376. return false;
  377. unsigned Arch = MachO->getArch();
  378. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  379. uint64_t Type = MachO->getRelocationType(Rel);
  380. // On arches that use the generic relocations, GENERIC_RELOC_PAIR
  381. // is always hidden.
  382. if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc)
  383. return Type == MachO::GENERIC_RELOC_PAIR;
  384. if (Arch == Triple::x86_64) {
  385. // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
  386. // an X86_64_RELOC_SUBTRACTOR.
  387. if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
  388. DataRefImpl RelPrev = Rel;
  389. RelPrev.d.a--;
  390. uint64_t PrevType = MachO->getRelocationType(RelPrev);
  391. if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
  392. return true;
  393. }
  394. }
  395. return false;
  396. }
  397. namespace {
  398. /// Get the column at which we want to start printing the instruction
  399. /// disassembly, taking into account anything which appears to the left of it.
  400. unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
  401. return !ShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
  402. }
  403. static bool isAArch64Elf(const ObjectFile &Obj) {
  404. const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
  405. return Elf && Elf->getEMachine() == ELF::EM_AARCH64;
  406. }
  407. static bool isArmElf(const ObjectFile &Obj) {
  408. const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
  409. return Elf && Elf->getEMachine() == ELF::EM_ARM;
  410. }
  411. static bool isCSKYElf(const ObjectFile &Obj) {
  412. const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
  413. return Elf && Elf->getEMachine() == ELF::EM_CSKY;
  414. }
  415. static bool hasMappingSymbols(const ObjectFile &Obj) {
  416. return isArmElf(Obj) || isAArch64Elf(Obj) || isCSKYElf(Obj) ;
  417. }
  418. static bool isMappingSymbol(const SymbolInfoTy &Sym) {
  419. return Sym.Name.startswith("$d") || Sym.Name.startswith("$x") ||
  420. Sym.Name.startswith("$a") || Sym.Name.startswith("$t");
  421. }
  422. static void printRelocation(formatted_raw_ostream &OS, StringRef FileName,
  423. const RelocationRef &Rel, uint64_t Address,
  424. bool Is64Bits) {
  425. StringRef Fmt = Is64Bits ? "%016" PRIx64 ": " : "%08" PRIx64 ": ";
  426. SmallString<16> Name;
  427. SmallString<32> Val;
  428. Rel.getTypeName(Name);
  429. if (Error E = getRelocationValueString(Rel, Val))
  430. reportError(std::move(E), FileName);
  431. OS << (Is64Bits || !LeadingAddr ? "\t\t" : "\t\t\t");
  432. if (LeadingAddr)
  433. OS << format(Fmt.data(), Address);
  434. OS << Name << "\t" << Val;
  435. }
  436. static void AlignToInstStartColumn(size_t Start, const MCSubtargetInfo &STI,
  437. raw_ostream &OS) {
  438. // The output of printInst starts with a tab. Print some spaces so that
  439. // the tab has 1 column and advances to the target tab stop.
  440. unsigned TabStop = getInstStartColumn(STI);
  441. unsigned Column = OS.tell() - Start;
  442. OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
  443. }
  444. class PrettyPrinter {
  445. public:
  446. virtual ~PrettyPrinter() = default;
  447. virtual void
  448. printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  449. object::SectionedAddress Address, formatted_raw_ostream &OS,
  450. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  451. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  452. LiveVariablePrinter &LVP) {
  453. if (SP && (PrintSource || PrintLines))
  454. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  455. LVP.printBetweenInsts(OS, false);
  456. size_t Start = OS.tell();
  457. if (LeadingAddr)
  458. OS << format("%8" PRIx64 ":", Address.Address);
  459. if (ShowRawInsn) {
  460. OS << ' ';
  461. dumpBytes(Bytes, OS);
  462. }
  463. AlignToInstStartColumn(Start, STI, OS);
  464. if (MI) {
  465. // See MCInstPrinter::printInst. On targets where a PC relative immediate
  466. // is relative to the next instruction and the length of a MCInst is
  467. // difficult to measure (x86), this is the address of the next
  468. // instruction.
  469. uint64_t Addr =
  470. Address.Address + (STI.getTargetTriple().isX86() ? Bytes.size() : 0);
  471. IP.printInst(MI, Addr, "", STI, OS);
  472. } else
  473. OS << "\t<unknown>";
  474. }
  475. };
  476. PrettyPrinter PrettyPrinterInst;
  477. class HexagonPrettyPrinter : public PrettyPrinter {
  478. public:
  479. void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
  480. formatted_raw_ostream &OS) {
  481. uint32_t opcode =
  482. (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
  483. if (LeadingAddr)
  484. OS << format("%8" PRIx64 ":", Address);
  485. if (ShowRawInsn) {
  486. OS << "\t";
  487. dumpBytes(Bytes.slice(0, 4), OS);
  488. OS << format("\t%08" PRIx32, opcode);
  489. }
  490. }
  491. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  492. object::SectionedAddress Address, formatted_raw_ostream &OS,
  493. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  494. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  495. LiveVariablePrinter &LVP) override {
  496. if (SP && (PrintSource || PrintLines))
  497. SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
  498. if (!MI) {
  499. printLead(Bytes, Address.Address, OS);
  500. OS << " <unknown>";
  501. return;
  502. }
  503. std::string Buffer;
  504. {
  505. raw_string_ostream TempStream(Buffer);
  506. IP.printInst(MI, Address.Address, "", STI, TempStream);
  507. }
  508. StringRef Contents(Buffer);
  509. // Split off bundle attributes
  510. auto PacketBundle = Contents.rsplit('\n');
  511. // Split off first instruction from the rest
  512. auto HeadTail = PacketBundle.first.split('\n');
  513. auto Preamble = " { ";
  514. auto Separator = "";
  515. // Hexagon's packets require relocations to be inline rather than
  516. // clustered at the end of the packet.
  517. std::vector<RelocationRef>::const_iterator RelCur = Rels->begin();
  518. std::vector<RelocationRef>::const_iterator RelEnd = Rels->end();
  519. auto PrintReloc = [&]() -> void {
  520. while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address.Address)) {
  521. if (RelCur->getOffset() == Address.Address) {
  522. printRelocation(OS, ObjectFilename, *RelCur, Address.Address, false);
  523. return;
  524. }
  525. ++RelCur;
  526. }
  527. };
  528. while (!HeadTail.first.empty()) {
  529. OS << Separator;
  530. Separator = "\n";
  531. if (SP && (PrintSource || PrintLines))
  532. SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
  533. printLead(Bytes, Address.Address, OS);
  534. OS << Preamble;
  535. Preamble = " ";
  536. StringRef Inst;
  537. auto Duplex = HeadTail.first.split('\v');
  538. if (!Duplex.second.empty()) {
  539. OS << Duplex.first;
  540. OS << "; ";
  541. Inst = Duplex.second;
  542. }
  543. else
  544. Inst = HeadTail.first;
  545. OS << Inst;
  546. HeadTail = HeadTail.second.split('\n');
  547. if (HeadTail.first.empty())
  548. OS << " } " << PacketBundle.second;
  549. PrintReloc();
  550. Bytes = Bytes.slice(4);
  551. Address.Address += 4;
  552. }
  553. }
  554. };
  555. HexagonPrettyPrinter HexagonPrettyPrinterInst;
  556. class AMDGCNPrettyPrinter : public PrettyPrinter {
  557. public:
  558. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  559. object::SectionedAddress Address, formatted_raw_ostream &OS,
  560. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  561. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  562. LiveVariablePrinter &LVP) override {
  563. if (SP && (PrintSource || PrintLines))
  564. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  565. if (MI) {
  566. SmallString<40> InstStr;
  567. raw_svector_ostream IS(InstStr);
  568. IP.printInst(MI, Address.Address, "", STI, IS);
  569. OS << left_justify(IS.str(), 60);
  570. } else {
  571. // an unrecognized encoding - this is probably data so represent it
  572. // using the .long directive, or .byte directive if fewer than 4 bytes
  573. // remaining
  574. if (Bytes.size() >= 4) {
  575. OS << format("\t.long 0x%08" PRIx32 " ",
  576. support::endian::read32<support::little>(Bytes.data()));
  577. OS.indent(42);
  578. } else {
  579. OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
  580. for (unsigned int i = 1; i < Bytes.size(); i++)
  581. OS << format(", 0x%02" PRIx8, Bytes[i]);
  582. OS.indent(55 - (6 * Bytes.size()));
  583. }
  584. }
  585. OS << format("// %012" PRIX64 ":", Address.Address);
  586. if (Bytes.size() >= 4) {
  587. // D should be casted to uint32_t here as it is passed by format to
  588. // snprintf as vararg.
  589. for (uint32_t D :
  590. ArrayRef(reinterpret_cast<const support::little32_t *>(Bytes.data()),
  591. Bytes.size() / 4))
  592. OS << format(" %08" PRIX32, D);
  593. } else {
  594. for (unsigned char B : Bytes)
  595. OS << format(" %02" PRIX8, B);
  596. }
  597. if (!Annot.empty())
  598. OS << " // " << Annot;
  599. }
  600. };
  601. AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
  602. class BPFPrettyPrinter : public PrettyPrinter {
  603. public:
  604. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  605. object::SectionedAddress Address, formatted_raw_ostream &OS,
  606. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  607. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  608. LiveVariablePrinter &LVP) override {
  609. if (SP && (PrintSource || PrintLines))
  610. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  611. if (LeadingAddr)
  612. OS << format("%8" PRId64 ":", Address.Address / 8);
  613. if (ShowRawInsn) {
  614. OS << "\t";
  615. dumpBytes(Bytes, OS);
  616. }
  617. if (MI)
  618. IP.printInst(MI, Address.Address, "", STI, OS);
  619. else
  620. OS << "\t<unknown>";
  621. }
  622. };
  623. BPFPrettyPrinter BPFPrettyPrinterInst;
  624. class ARMPrettyPrinter : public PrettyPrinter {
  625. public:
  626. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  627. object::SectionedAddress Address, formatted_raw_ostream &OS,
  628. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  629. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  630. LiveVariablePrinter &LVP) override {
  631. if (SP && (PrintSource || PrintLines))
  632. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  633. LVP.printBetweenInsts(OS, false);
  634. size_t Start = OS.tell();
  635. if (LeadingAddr)
  636. OS << format("%8" PRIx64 ":", Address.Address);
  637. if (ShowRawInsn) {
  638. size_t Pos = 0, End = Bytes.size();
  639. if (STI.checkFeatures("+thumb-mode")) {
  640. for (; Pos + 2 <= End; Pos += 2)
  641. OS << ' '
  642. << format_hex_no_prefix(
  643. llvm::support::endian::read<uint16_t>(
  644. Bytes.data() + Pos, InstructionEndianness),
  645. 4);
  646. } else {
  647. for (; Pos + 4 <= End; Pos += 4)
  648. OS << ' '
  649. << format_hex_no_prefix(
  650. llvm::support::endian::read<uint32_t>(
  651. Bytes.data() + Pos, InstructionEndianness),
  652. 8);
  653. }
  654. if (Pos < End) {
  655. OS << ' ';
  656. dumpBytes(Bytes.slice(Pos), OS);
  657. }
  658. }
  659. AlignToInstStartColumn(Start, STI, OS);
  660. if (MI) {
  661. IP.printInst(MI, Address.Address, "", STI, OS);
  662. } else
  663. OS << "\t<unknown>";
  664. }
  665. void setInstructionEndianness(llvm::support::endianness Endianness) {
  666. InstructionEndianness = Endianness;
  667. }
  668. private:
  669. llvm::support::endianness InstructionEndianness = llvm::support::little;
  670. };
  671. ARMPrettyPrinter ARMPrettyPrinterInst;
  672. class AArch64PrettyPrinter : public PrettyPrinter {
  673. public:
  674. void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
  675. object::SectionedAddress Address, formatted_raw_ostream &OS,
  676. StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
  677. StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
  678. LiveVariablePrinter &LVP) override {
  679. if (SP && (PrintSource || PrintLines))
  680. SP->printSourceLine(OS, Address, ObjectFilename, LVP);
  681. LVP.printBetweenInsts(OS, false);
  682. size_t Start = OS.tell();
  683. if (LeadingAddr)
  684. OS << format("%8" PRIx64 ":", Address.Address);
  685. if (ShowRawInsn) {
  686. size_t Pos = 0, End = Bytes.size();
  687. for (; Pos + 4 <= End; Pos += 4)
  688. OS << ' '
  689. << format_hex_no_prefix(
  690. llvm::support::endian::read<uint32_t>(Bytes.data() + Pos,
  691. llvm::support::little),
  692. 8);
  693. if (Pos < End) {
  694. OS << ' ';
  695. dumpBytes(Bytes.slice(Pos), OS);
  696. }
  697. }
  698. AlignToInstStartColumn(Start, STI, OS);
  699. if (MI) {
  700. IP.printInst(MI, Address.Address, "", STI, OS);
  701. } else
  702. OS << "\t<unknown>";
  703. }
  704. };
  705. AArch64PrettyPrinter AArch64PrettyPrinterInst;
  706. PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
  707. switch(Triple.getArch()) {
  708. default:
  709. return PrettyPrinterInst;
  710. case Triple::hexagon:
  711. return HexagonPrettyPrinterInst;
  712. case Triple::amdgcn:
  713. return AMDGCNPrettyPrinterInst;
  714. case Triple::bpfel:
  715. case Triple::bpfeb:
  716. return BPFPrettyPrinterInst;
  717. case Triple::arm:
  718. case Triple::armeb:
  719. case Triple::thumb:
  720. case Triple::thumbeb:
  721. return ARMPrettyPrinterInst;
  722. case Triple::aarch64:
  723. case Triple::aarch64_be:
  724. case Triple::aarch64_32:
  725. return AArch64PrettyPrinterInst;
  726. }
  727. }
  728. }
  729. static uint8_t getElfSymbolType(const ObjectFile &Obj, const SymbolRef &Sym) {
  730. assert(Obj.isELF());
  731. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
  732. return unwrapOrError(Elf32LEObj->getSymbol(Sym.getRawDataRefImpl()),
  733. Obj.getFileName())
  734. ->getType();
  735. if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
  736. return unwrapOrError(Elf64LEObj->getSymbol(Sym.getRawDataRefImpl()),
  737. Obj.getFileName())
  738. ->getType();
  739. if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
  740. return unwrapOrError(Elf32BEObj->getSymbol(Sym.getRawDataRefImpl()),
  741. Obj.getFileName())
  742. ->getType();
  743. if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
  744. return unwrapOrError(Elf64BEObj->getSymbol(Sym.getRawDataRefImpl()),
  745. Obj.getFileName())
  746. ->getType();
  747. llvm_unreachable("Unsupported binary format");
  748. }
  749. template <class ELFT>
  750. static void
  751. addDynamicElfSymbols(const ELFObjectFile<ELFT> &Obj,
  752. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  753. for (auto Symbol : Obj.getDynamicSymbolIterators()) {
  754. uint8_t SymbolType = Symbol.getELFType();
  755. if (SymbolType == ELF::STT_SECTION)
  756. continue;
  757. uint64_t Address = unwrapOrError(Symbol.getAddress(), Obj.getFileName());
  758. // ELFSymbolRef::getAddress() returns size instead of value for common
  759. // symbols which is not desirable for disassembly output. Overriding.
  760. if (SymbolType == ELF::STT_COMMON)
  761. Address = unwrapOrError(Obj.getSymbol(Symbol.getRawDataRefImpl()),
  762. Obj.getFileName())
  763. ->st_value;
  764. StringRef Name = unwrapOrError(Symbol.getName(), Obj.getFileName());
  765. if (Name.empty())
  766. continue;
  767. section_iterator SecI =
  768. unwrapOrError(Symbol.getSection(), Obj.getFileName());
  769. if (SecI == Obj.section_end())
  770. continue;
  771. AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
  772. }
  773. }
  774. static void
  775. addDynamicElfSymbols(const ELFObjectFileBase &Obj,
  776. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  777. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
  778. addDynamicElfSymbols(*Elf32LEObj, AllSymbols);
  779. else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
  780. addDynamicElfSymbols(*Elf64LEObj, AllSymbols);
  781. else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
  782. addDynamicElfSymbols(*Elf32BEObj, AllSymbols);
  783. else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
  784. addDynamicElfSymbols(*Elf64BEObj, AllSymbols);
  785. else
  786. llvm_unreachable("Unsupported binary format");
  787. }
  788. static std::optional<SectionRef> getWasmCodeSection(const WasmObjectFile &Obj) {
  789. for (auto SecI : Obj.sections()) {
  790. const WasmSection &Section = Obj.getWasmSection(SecI);
  791. if (Section.Type == wasm::WASM_SEC_CODE)
  792. return SecI;
  793. }
  794. return std::nullopt;
  795. }
  796. static void
  797. addMissingWasmCodeSymbols(const WasmObjectFile &Obj,
  798. std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
  799. std::optional<SectionRef> Section = getWasmCodeSection(Obj);
  800. if (!Section)
  801. return;
  802. SectionSymbolsTy &Symbols = AllSymbols[*Section];
  803. std::set<uint64_t> SymbolAddresses;
  804. for (const auto &Sym : Symbols)
  805. SymbolAddresses.insert(Sym.Addr);
  806. for (const wasm::WasmFunction &Function : Obj.functions()) {
  807. uint64_t Address = Function.CodeSectionOffset;
  808. // Only add fallback symbols for functions not already present in the symbol
  809. // table.
  810. if (SymbolAddresses.count(Address))
  811. continue;
  812. // This function has no symbol, so it should have no SymbolName.
  813. assert(Function.SymbolName.empty());
  814. // We use DebugName for the name, though it may be empty if there is no
  815. // "name" custom section, or that section is missing a name for this
  816. // function.
  817. StringRef Name = Function.DebugName;
  818. Symbols.emplace_back(Address, Name, ELF::STT_NOTYPE);
  819. }
  820. }
  821. static void addPltEntries(const ObjectFile &Obj,
  822. std::map<SectionRef, SectionSymbolsTy> &AllSymbols,
  823. StringSaver &Saver) {
  824. std::optional<SectionRef> Plt;
  825. for (const SectionRef &Section : Obj.sections()) {
  826. Expected<StringRef> SecNameOrErr = Section.getName();
  827. if (!SecNameOrErr) {
  828. consumeError(SecNameOrErr.takeError());
  829. continue;
  830. }
  831. if (*SecNameOrErr == ".plt")
  832. Plt = Section;
  833. }
  834. if (!Plt)
  835. return;
  836. if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(&Obj)) {
  837. for (auto PltEntry : ElfObj->getPltAddresses()) {
  838. if (PltEntry.first) {
  839. SymbolRef Symbol(*PltEntry.first, ElfObj);
  840. uint8_t SymbolType = getElfSymbolType(Obj, Symbol);
  841. if (Expected<StringRef> NameOrErr = Symbol.getName()) {
  842. if (!NameOrErr->empty())
  843. AllSymbols[*Plt].emplace_back(
  844. PltEntry.second, Saver.save((*NameOrErr + "@plt").str()),
  845. SymbolType);
  846. continue;
  847. } else {
  848. // The warning has been reported in disassembleObject().
  849. consumeError(NameOrErr.takeError());
  850. }
  851. }
  852. reportWarning("PLT entry at 0x" + Twine::utohexstr(PltEntry.second) +
  853. " references an invalid symbol",
  854. Obj.getFileName());
  855. }
  856. }
  857. }
  858. // Normally the disassembly output will skip blocks of zeroes. This function
  859. // returns the number of zero bytes that can be skipped when dumping the
  860. // disassembly of the instructions in Buf.
  861. static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
  862. // Find the number of leading zeroes.
  863. size_t N = 0;
  864. while (N < Buf.size() && !Buf[N])
  865. ++N;
  866. // We may want to skip blocks of zero bytes, but unless we see
  867. // at least 8 of them in a row.
  868. if (N < 8)
  869. return 0;
  870. // We skip zeroes in multiples of 4 because do not want to truncate an
  871. // instruction if it starts with a zero byte.
  872. return N & ~0x3;
  873. }
  874. // Returns a map from sections to their relocations.
  875. static std::map<SectionRef, std::vector<RelocationRef>>
  876. getRelocsMap(object::ObjectFile const &Obj) {
  877. std::map<SectionRef, std::vector<RelocationRef>> Ret;
  878. uint64_t I = (uint64_t)-1;
  879. for (SectionRef Sec : Obj.sections()) {
  880. ++I;
  881. Expected<section_iterator> RelocatedOrErr = Sec.getRelocatedSection();
  882. if (!RelocatedOrErr)
  883. reportError(Obj.getFileName(),
  884. "section (" + Twine(I) +
  885. "): failed to get a relocated section: " +
  886. toString(RelocatedOrErr.takeError()));
  887. section_iterator Relocated = *RelocatedOrErr;
  888. if (Relocated == Obj.section_end() || !checkSectionFilter(*Relocated).Keep)
  889. continue;
  890. std::vector<RelocationRef> &V = Ret[*Relocated];
  891. append_range(V, Sec.relocations());
  892. // Sort relocations by address.
  893. llvm::stable_sort(V, isRelocAddressLess);
  894. }
  895. return Ret;
  896. }
  897. // Used for --adjust-vma to check if address should be adjusted by the
  898. // specified value for a given section.
  899. // For ELF we do not adjust non-allocatable sections like debug ones,
  900. // because they are not loadable.
  901. // TODO: implement for other file formats.
  902. static bool shouldAdjustVA(const SectionRef &Section) {
  903. const ObjectFile *Obj = Section.getObject();
  904. if (Obj->isELF())
  905. return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
  906. return false;
  907. }
  908. typedef std::pair<uint64_t, char> MappingSymbolPair;
  909. static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
  910. uint64_t Address) {
  911. auto It =
  912. partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
  913. return Val.first <= Address;
  914. });
  915. // Return zero for any address before the first mapping symbol; this means
  916. // we should use the default disassembly mode, depending on the target.
  917. if (It == MappingSymbols.begin())
  918. return '\x00';
  919. return (It - 1)->second;
  920. }
  921. static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
  922. uint64_t End, const ObjectFile &Obj,
  923. ArrayRef<uint8_t> Bytes,
  924. ArrayRef<MappingSymbolPair> MappingSymbols,
  925. const MCSubtargetInfo &STI, raw_ostream &OS) {
  926. support::endianness Endian =
  927. Obj.isLittleEndian() ? support::little : support::big;
  928. size_t Start = OS.tell();
  929. OS << format("%8" PRIx64 ": ", SectionAddr + Index);
  930. if (Index + 4 <= End) {
  931. dumpBytes(Bytes.slice(Index, 4), OS);
  932. AlignToInstStartColumn(Start, STI, OS);
  933. OS << "\t.word\t"
  934. << format_hex(support::endian::read32(Bytes.data() + Index, Endian),
  935. 10);
  936. return 4;
  937. }
  938. if (Index + 2 <= End) {
  939. dumpBytes(Bytes.slice(Index, 2), OS);
  940. AlignToInstStartColumn(Start, STI, OS);
  941. OS << "\t.short\t"
  942. << format_hex(support::endian::read16(Bytes.data() + Index, Endian), 6);
  943. return 2;
  944. }
  945. dumpBytes(Bytes.slice(Index, 1), OS);
  946. AlignToInstStartColumn(Start, STI, OS);
  947. OS << "\t.byte\t" << format_hex(Bytes[Index], 4);
  948. return 1;
  949. }
  950. static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
  951. ArrayRef<uint8_t> Bytes) {
  952. // print out data up to 8 bytes at a time in hex and ascii
  953. uint8_t AsciiData[9] = {'\0'};
  954. uint8_t Byte;
  955. int NumBytes = 0;
  956. for (; Index < End; ++Index) {
  957. if (NumBytes == 0)
  958. outs() << format("%8" PRIx64 ":", SectionAddr + Index);
  959. Byte = Bytes.slice(Index)[0];
  960. outs() << format(" %02x", Byte);
  961. AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
  962. uint8_t IndentOffset = 0;
  963. NumBytes++;
  964. if (Index == End - 1 || NumBytes > 8) {
  965. // Indent the space for less than 8 bytes data.
  966. // 2 spaces for byte and one for space between bytes
  967. IndentOffset = 3 * (8 - NumBytes);
  968. for (int Excess = NumBytes; Excess < 8; Excess++)
  969. AsciiData[Excess] = '\0';
  970. NumBytes = 8;
  971. }
  972. if (NumBytes == 8) {
  973. AsciiData[8] = '\0';
  974. outs() << std::string(IndentOffset, ' ') << " ";
  975. outs() << reinterpret_cast<char *>(AsciiData);
  976. outs() << '\n';
  977. NumBytes = 0;
  978. }
  979. }
  980. }
  981. SymbolInfoTy objdump::createSymbolInfo(const ObjectFile &Obj,
  982. const SymbolRef &Symbol) {
  983. const StringRef FileName = Obj.getFileName();
  984. const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
  985. const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
  986. if (Obj.isXCOFF() && SymbolDescription) {
  987. const auto &XCOFFObj = cast<XCOFFObjectFile>(Obj);
  988. DataRefImpl SymbolDRI = Symbol.getRawDataRefImpl();
  989. const uint32_t SymbolIndex = XCOFFObj.getSymbolIndex(SymbolDRI.p);
  990. std::optional<XCOFF::StorageMappingClass> Smc =
  991. getXCOFFSymbolCsectSMC(XCOFFObj, Symbol);
  992. return SymbolInfoTy(Addr, Name, Smc, SymbolIndex,
  993. isLabel(XCOFFObj, Symbol));
  994. } else if (Obj.isXCOFF()) {
  995. const SymbolRef::Type SymType = unwrapOrError(Symbol.getType(), FileName);
  996. return SymbolInfoTy(Addr, Name, SymType, true);
  997. } else
  998. return SymbolInfoTy(Addr, Name,
  999. Obj.isELF() ? getElfSymbolType(Obj, Symbol)
  1000. : (uint8_t)ELF::STT_NOTYPE);
  1001. }
  1002. static SymbolInfoTy createDummySymbolInfo(const ObjectFile &Obj,
  1003. const uint64_t Addr, StringRef &Name,
  1004. uint8_t Type) {
  1005. if (Obj.isXCOFF() && SymbolDescription)
  1006. return SymbolInfoTy(Addr, Name, std::nullopt, std::nullopt, false);
  1007. else
  1008. return SymbolInfoTy(Addr, Name, Type);
  1009. }
  1010. static void
  1011. collectBBAddrMapLabels(const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAddrMap,
  1012. uint64_t SectionAddr, uint64_t Start, uint64_t End,
  1013. std::unordered_map<uint64_t, std::vector<std::string>> &Labels) {
  1014. if (AddrToBBAddrMap.empty())
  1015. return;
  1016. Labels.clear();
  1017. uint64_t StartAddress = SectionAddr + Start;
  1018. uint64_t EndAddress = SectionAddr + End;
  1019. auto Iter = AddrToBBAddrMap.find(StartAddress);
  1020. if (Iter == AddrToBBAddrMap.end())
  1021. return;
  1022. for (unsigned I = 0, Size = Iter->second.BBEntries.size(); I < Size; ++I) {
  1023. uint64_t BBAddress = Iter->second.BBEntries[I].Offset + Iter->second.Addr;
  1024. if (BBAddress >= EndAddress)
  1025. continue;
  1026. Labels[BBAddress].push_back(("BB" + Twine(I)).str());
  1027. }
  1028. }
  1029. static void collectLocalBranchTargets(
  1030. ArrayRef<uint8_t> Bytes, const MCInstrAnalysis *MIA, MCDisassembler *DisAsm,
  1031. MCInstPrinter *IP, const MCSubtargetInfo *STI, uint64_t SectionAddr,
  1032. uint64_t Start, uint64_t End, std::unordered_map<uint64_t, std::string> &Labels) {
  1033. // So far only supports PowerPC and X86.
  1034. if (!STI->getTargetTriple().isPPC() && !STI->getTargetTriple().isX86())
  1035. return;
  1036. Labels.clear();
  1037. unsigned LabelCount = 0;
  1038. Start += SectionAddr;
  1039. End += SectionAddr;
  1040. uint64_t Index = Start;
  1041. while (Index < End) {
  1042. // Disassemble a real instruction and record function-local branch labels.
  1043. MCInst Inst;
  1044. uint64_t Size;
  1045. ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index - SectionAddr);
  1046. bool Disassembled =
  1047. DisAsm->getInstruction(Inst, Size, ThisBytes, Index, nulls());
  1048. if (Size == 0)
  1049. Size = std::min<uint64_t>(ThisBytes.size(),
  1050. DisAsm->suggestBytesToSkip(ThisBytes, Index));
  1051. if (Disassembled && MIA) {
  1052. uint64_t Target;
  1053. bool TargetKnown = MIA->evaluateBranch(Inst, Index, Size, Target);
  1054. // On PowerPC, if the address of a branch is the same as the target, it
  1055. // means that it's a function call. Do not mark the label for this case.
  1056. if (TargetKnown && (Target >= Start && Target < End) &&
  1057. !Labels.count(Target) &&
  1058. !(STI->getTargetTriple().isPPC() && Target == Index))
  1059. Labels[Target] = ("L" + Twine(LabelCount++)).str();
  1060. }
  1061. Index += Size;
  1062. }
  1063. }
  1064. // Create an MCSymbolizer for the target and add it to the MCDisassembler.
  1065. // This is currently only used on AMDGPU, and assumes the format of the
  1066. // void * argument passed to AMDGPU's createMCSymbolizer.
  1067. static void addSymbolizer(
  1068. MCContext &Ctx, const Target *Target, StringRef TripleName,
  1069. MCDisassembler *DisAsm, uint64_t SectionAddr, ArrayRef<uint8_t> Bytes,
  1070. SectionSymbolsTy &Symbols,
  1071. std::vector<std::unique_ptr<std::string>> &SynthesizedLabelNames) {
  1072. std::unique_ptr<MCRelocationInfo> RelInfo(
  1073. Target->createMCRelocationInfo(TripleName, Ctx));
  1074. if (!RelInfo)
  1075. return;
  1076. std::unique_ptr<MCSymbolizer> Symbolizer(Target->createMCSymbolizer(
  1077. TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
  1078. MCSymbolizer *SymbolizerPtr = &*Symbolizer;
  1079. DisAsm->setSymbolizer(std::move(Symbolizer));
  1080. if (!SymbolizeOperands)
  1081. return;
  1082. // Synthesize labels referenced by branch instructions by
  1083. // disassembling, discarding the output, and collecting the referenced
  1084. // addresses from the symbolizer.
  1085. for (size_t Index = 0; Index != Bytes.size();) {
  1086. MCInst Inst;
  1087. uint64_t Size;
  1088. ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index);
  1089. const uint64_t ThisAddr = SectionAddr + Index;
  1090. DisAsm->getInstruction(Inst, Size, ThisBytes, ThisAddr, nulls());
  1091. if (Size == 0)
  1092. Size = std::min<uint64_t>(ThisBytes.size(),
  1093. DisAsm->suggestBytesToSkip(ThisBytes, Index));
  1094. Index += Size;
  1095. }
  1096. ArrayRef<uint64_t> LabelAddrsRef = SymbolizerPtr->getReferencedAddresses();
  1097. // Copy and sort to remove duplicates.
  1098. std::vector<uint64_t> LabelAddrs;
  1099. LabelAddrs.insert(LabelAddrs.end(), LabelAddrsRef.begin(),
  1100. LabelAddrsRef.end());
  1101. llvm::sort(LabelAddrs);
  1102. LabelAddrs.resize(std::unique(LabelAddrs.begin(), LabelAddrs.end()) -
  1103. LabelAddrs.begin());
  1104. // Add the labels.
  1105. for (unsigned LabelNum = 0; LabelNum != LabelAddrs.size(); ++LabelNum) {
  1106. auto Name = std::make_unique<std::string>();
  1107. *Name = (Twine("L") + Twine(LabelNum)).str();
  1108. SynthesizedLabelNames.push_back(std::move(Name));
  1109. Symbols.push_back(SymbolInfoTy(
  1110. LabelAddrs[LabelNum], *SynthesizedLabelNames.back(), ELF::STT_NOTYPE));
  1111. }
  1112. llvm::stable_sort(Symbols);
  1113. // Recreate the symbolizer with the new symbols list.
  1114. RelInfo.reset(Target->createMCRelocationInfo(TripleName, Ctx));
  1115. Symbolizer.reset(Target->createMCSymbolizer(
  1116. TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
  1117. DisAsm->setSymbolizer(std::move(Symbolizer));
  1118. }
  1119. static StringRef getSegmentName(const MachOObjectFile *MachO,
  1120. const SectionRef &Section) {
  1121. if (MachO) {
  1122. DataRefImpl DR = Section.getRawDataRefImpl();
  1123. StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
  1124. return SegmentName;
  1125. }
  1126. return "";
  1127. }
  1128. static void emitPostInstructionInfo(formatted_raw_ostream &FOS,
  1129. const MCAsmInfo &MAI,
  1130. const MCSubtargetInfo &STI,
  1131. StringRef Comments,
  1132. LiveVariablePrinter &LVP) {
  1133. do {
  1134. if (!Comments.empty()) {
  1135. // Emit a line of comments.
  1136. StringRef Comment;
  1137. std::tie(Comment, Comments) = Comments.split('\n');
  1138. // MAI.getCommentColumn() assumes that instructions are printed at the
  1139. // position of 8, while getInstStartColumn() returns the actual position.
  1140. unsigned CommentColumn =
  1141. MAI.getCommentColumn() - 8 + getInstStartColumn(STI);
  1142. FOS.PadToColumn(CommentColumn);
  1143. FOS << MAI.getCommentString() << ' ' << Comment;
  1144. }
  1145. LVP.printAfterInst(FOS);
  1146. FOS << '\n';
  1147. } while (!Comments.empty());
  1148. FOS.flush();
  1149. }
  1150. static void createFakeELFSections(ObjectFile &Obj) {
  1151. assert(Obj.isELF());
  1152. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
  1153. Elf32LEObj->createFakeSections();
  1154. else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
  1155. Elf64LEObj->createFakeSections();
  1156. else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
  1157. Elf32BEObj->createFakeSections();
  1158. else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
  1159. Elf64BEObj->createFakeSections();
  1160. else
  1161. llvm_unreachable("Unsupported binary format");
  1162. }
  1163. // Tries to fetch a more complete version of the given object file using its
  1164. // Build ID. Returns std::nullopt if nothing was found.
  1165. static std::optional<OwningBinary<Binary>>
  1166. fetchBinaryByBuildID(const ObjectFile &Obj) {
  1167. std::optional<object::BuildIDRef> BuildID = getBuildID(&Obj);
  1168. if (!BuildID)
  1169. return std::nullopt;
  1170. std::optional<std::string> Path = BIDFetcher->fetch(*BuildID);
  1171. if (!Path)
  1172. return std::nullopt;
  1173. Expected<OwningBinary<Binary>> DebugBinary = createBinary(*Path);
  1174. if (!DebugBinary) {
  1175. reportWarning(toString(DebugBinary.takeError()), *Path);
  1176. return std::nullopt;
  1177. }
  1178. return std::move(*DebugBinary);
  1179. }
  1180. static void disassembleObject(const Target *TheTarget, ObjectFile &Obj,
  1181. const ObjectFile &DbgObj, MCContext &Ctx,
  1182. MCDisassembler *PrimaryDisAsm,
  1183. MCDisassembler *SecondaryDisAsm,
  1184. const MCInstrAnalysis *MIA, MCInstPrinter *IP,
  1185. const MCSubtargetInfo *PrimarySTI,
  1186. const MCSubtargetInfo *SecondarySTI,
  1187. PrettyPrinter &PIP, SourcePrinter &SP,
  1188. bool InlineRelocs) {
  1189. const MCSubtargetInfo *STI = PrimarySTI;
  1190. MCDisassembler *DisAsm = PrimaryDisAsm;
  1191. bool PrimaryIsThumb = false;
  1192. if (isArmElf(Obj))
  1193. PrimaryIsThumb = STI->checkFeatures("+thumb-mode");
  1194. std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
  1195. if (InlineRelocs)
  1196. RelocMap = getRelocsMap(Obj);
  1197. bool Is64Bits = Obj.getBytesInAddress() > 4;
  1198. // Create a mapping from virtual address to symbol name. This is used to
  1199. // pretty print the symbols while disassembling.
  1200. std::map<SectionRef, SectionSymbolsTy> AllSymbols;
  1201. SectionSymbolsTy AbsoluteSymbols;
  1202. const StringRef FileName = Obj.getFileName();
  1203. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&Obj);
  1204. for (const SymbolRef &Symbol : Obj.symbols()) {
  1205. Expected<StringRef> NameOrErr = Symbol.getName();
  1206. if (!NameOrErr) {
  1207. reportWarning(toString(NameOrErr.takeError()), FileName);
  1208. continue;
  1209. }
  1210. if (NameOrErr->empty() && !(Obj.isXCOFF() && SymbolDescription))
  1211. continue;
  1212. if (Obj.isELF() && getElfSymbolType(Obj, Symbol) == ELF::STT_SECTION)
  1213. continue;
  1214. if (MachO) {
  1215. // __mh_(execute|dylib|dylinker|bundle|preload|object)_header are special
  1216. // symbols that support MachO header introspection. They do not bind to
  1217. // code locations and are irrelevant for disassembly.
  1218. if (NameOrErr->startswith("__mh_") && NameOrErr->endswith("_header"))
  1219. continue;
  1220. // Don't ask a Mach-O STAB symbol for its section unless you know that
  1221. // STAB symbol's section field refers to a valid section index. Otherwise
  1222. // the symbol may error trying to load a section that does not exist.
  1223. DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
  1224. uint8_t NType = (MachO->is64Bit() ?
  1225. MachO->getSymbol64TableEntry(SymDRI).n_type:
  1226. MachO->getSymbolTableEntry(SymDRI).n_type);
  1227. if (NType & MachO::N_STAB)
  1228. continue;
  1229. }
  1230. section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
  1231. if (SecI != Obj.section_end())
  1232. AllSymbols[*SecI].push_back(createSymbolInfo(Obj, Symbol));
  1233. else
  1234. AbsoluteSymbols.push_back(createSymbolInfo(Obj, Symbol));
  1235. }
  1236. if (AllSymbols.empty() && Obj.isELF())
  1237. addDynamicElfSymbols(cast<ELFObjectFileBase>(Obj), AllSymbols);
  1238. if (Obj.isWasm())
  1239. addMissingWasmCodeSymbols(cast<WasmObjectFile>(Obj), AllSymbols);
  1240. if (Obj.isELF() && Obj.sections().empty())
  1241. createFakeELFSections(Obj);
  1242. BumpPtrAllocator A;
  1243. StringSaver Saver(A);
  1244. addPltEntries(Obj, AllSymbols, Saver);
  1245. // Create a mapping from virtual address to section. An empty section can
  1246. // cause more than one section at the same address. Sort such sections to be
  1247. // before same-addressed non-empty sections so that symbol lookups prefer the
  1248. // non-empty section.
  1249. std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
  1250. for (SectionRef Sec : Obj.sections())
  1251. SectionAddresses.emplace_back(Sec.getAddress(), Sec);
  1252. llvm::stable_sort(SectionAddresses, [](const auto &LHS, const auto &RHS) {
  1253. if (LHS.first != RHS.first)
  1254. return LHS.first < RHS.first;
  1255. return LHS.second.getSize() < RHS.second.getSize();
  1256. });
  1257. // Linked executables (.exe and .dll files) typically don't include a real
  1258. // symbol table but they might contain an export table.
  1259. if (const auto *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
  1260. for (const auto &ExportEntry : COFFObj->export_directories()) {
  1261. StringRef Name;
  1262. if (Error E = ExportEntry.getSymbolName(Name))
  1263. reportError(std::move(E), Obj.getFileName());
  1264. if (Name.empty())
  1265. continue;
  1266. uint32_t RVA;
  1267. if (Error E = ExportEntry.getExportRVA(RVA))
  1268. reportError(std::move(E), Obj.getFileName());
  1269. uint64_t VA = COFFObj->getImageBase() + RVA;
  1270. auto Sec = partition_point(
  1271. SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
  1272. return O.first <= VA;
  1273. });
  1274. if (Sec != SectionAddresses.begin()) {
  1275. --Sec;
  1276. AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
  1277. } else
  1278. AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
  1279. }
  1280. }
  1281. // Sort all the symbols, this allows us to use a simple binary search to find
  1282. // Multiple symbols can have the same address. Use a stable sort to stabilize
  1283. // the output.
  1284. StringSet<> FoundDisasmSymbolSet;
  1285. for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
  1286. llvm::stable_sort(SecSyms.second);
  1287. llvm::stable_sort(AbsoluteSymbols);
  1288. std::unique_ptr<DWARFContext> DICtx;
  1289. LiveVariablePrinter LVP(*Ctx.getRegisterInfo(), *STI);
  1290. if (DbgVariables != DVDisabled) {
  1291. DICtx = DWARFContext::create(DbgObj);
  1292. for (const std::unique_ptr<DWARFUnit> &CU : DICtx->compile_units())
  1293. LVP.addCompileUnit(CU->getUnitDIE(false));
  1294. }
  1295. LLVM_DEBUG(LVP.dump());
  1296. std::unordered_map<uint64_t, BBAddrMap> AddrToBBAddrMap;
  1297. auto ReadBBAddrMap = [&](std::optional<unsigned> SectionIndex =
  1298. std::nullopt) {
  1299. AddrToBBAddrMap.clear();
  1300. if (const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj)) {
  1301. auto BBAddrMapsOrErr = Elf->readBBAddrMap(SectionIndex);
  1302. if (!BBAddrMapsOrErr)
  1303. reportWarning(toString(BBAddrMapsOrErr.takeError()), Obj.getFileName());
  1304. for (auto &FunctionBBAddrMap : *BBAddrMapsOrErr)
  1305. AddrToBBAddrMap.emplace(FunctionBBAddrMap.Addr,
  1306. std::move(FunctionBBAddrMap));
  1307. }
  1308. };
  1309. // For non-relocatable objects, Read all LLVM_BB_ADDR_MAP sections into a
  1310. // single mapping, since they don't have any conflicts.
  1311. if (SymbolizeOperands && !Obj.isRelocatableObject())
  1312. ReadBBAddrMap();
  1313. for (const SectionRef &Section : ToolSectionFilter(Obj)) {
  1314. if (FilterSections.empty() && !DisassembleAll &&
  1315. (!Section.isText() || Section.isVirtual()))
  1316. continue;
  1317. uint64_t SectionAddr = Section.getAddress();
  1318. uint64_t SectSize = Section.getSize();
  1319. if (!SectSize)
  1320. continue;
  1321. // For relocatable object files, read the LLVM_BB_ADDR_MAP section
  1322. // corresponding to this section, if present.
  1323. if (SymbolizeOperands && Obj.isRelocatableObject())
  1324. ReadBBAddrMap(Section.getIndex());
  1325. // Get the list of all the symbols in this section.
  1326. SectionSymbolsTy &Symbols = AllSymbols[Section];
  1327. std::vector<MappingSymbolPair> MappingSymbols;
  1328. if (hasMappingSymbols(Obj)) {
  1329. for (const auto &Symb : Symbols) {
  1330. uint64_t Address = Symb.Addr;
  1331. StringRef Name = Symb.Name;
  1332. if (Name.startswith("$d"))
  1333. MappingSymbols.emplace_back(Address - SectionAddr, 'd');
  1334. if (Name.startswith("$x"))
  1335. MappingSymbols.emplace_back(Address - SectionAddr, 'x');
  1336. if (Name.startswith("$a"))
  1337. MappingSymbols.emplace_back(Address - SectionAddr, 'a');
  1338. if (Name.startswith("$t"))
  1339. MappingSymbols.emplace_back(Address - SectionAddr, 't');
  1340. }
  1341. }
  1342. llvm::sort(MappingSymbols);
  1343. ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
  1344. unwrapOrError(Section.getContents(), Obj.getFileName()));
  1345. std::vector<std::unique_ptr<std::string>> SynthesizedLabelNames;
  1346. if (Obj.isELF() && Obj.getArch() == Triple::amdgcn) {
  1347. // AMDGPU disassembler uses symbolizer for printing labels
  1348. addSymbolizer(Ctx, TheTarget, TripleName, DisAsm, SectionAddr, Bytes,
  1349. Symbols, SynthesizedLabelNames);
  1350. }
  1351. StringRef SegmentName = getSegmentName(MachO, Section);
  1352. StringRef SectionName = unwrapOrError(Section.getName(), Obj.getFileName());
  1353. // If the section has no symbol at the start, just insert a dummy one.
  1354. if (Symbols.empty() || Symbols[0].Addr != 0) {
  1355. Symbols.insert(Symbols.begin(),
  1356. createDummySymbolInfo(Obj, SectionAddr, SectionName,
  1357. Section.isText() ? ELF::STT_FUNC
  1358. : ELF::STT_OBJECT));
  1359. }
  1360. SmallString<40> Comments;
  1361. raw_svector_ostream CommentStream(Comments);
  1362. uint64_t VMAAdjustment = 0;
  1363. if (shouldAdjustVA(Section))
  1364. VMAAdjustment = AdjustVMA;
  1365. // In executable and shared objects, r_offset holds a virtual address.
  1366. // Subtract SectionAddr from the r_offset field of a relocation to get
  1367. // the section offset.
  1368. uint64_t RelAdjustment = Obj.isRelocatableObject() ? 0 : SectionAddr;
  1369. uint64_t Size;
  1370. uint64_t Index;
  1371. bool PrintedSection = false;
  1372. std::vector<RelocationRef> Rels = RelocMap[Section];
  1373. std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
  1374. std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
  1375. // Loop over each chunk of code between two points where at least
  1376. // one symbol is defined.
  1377. for (size_t SI = 0, SE = Symbols.size(); SI != SE;) {
  1378. // Advance SI past all the symbols starting at the same address,
  1379. // and make an ArrayRef of them.
  1380. unsigned FirstSI = SI;
  1381. uint64_t Start = Symbols[SI].Addr;
  1382. ArrayRef<SymbolInfoTy> SymbolsHere;
  1383. while (SI != SE && Symbols[SI].Addr == Start)
  1384. ++SI;
  1385. SymbolsHere = ArrayRef<SymbolInfoTy>(&Symbols[FirstSI], SI - FirstSI);
  1386. // Get the demangled names of all those symbols. We end up with a vector
  1387. // of StringRef that holds the names we're going to use, and a vector of
  1388. // std::string that stores the new strings returned by demangle(), if
  1389. // any. If we don't call demangle() then that vector can stay empty.
  1390. std::vector<StringRef> SymNamesHere;
  1391. std::vector<std::string> DemangledSymNamesHere;
  1392. if (Demangle) {
  1393. // Fetch the demangled names and store them locally.
  1394. for (const SymbolInfoTy &Symbol : SymbolsHere)
  1395. DemangledSymNamesHere.push_back(demangle(Symbol.Name.str()));
  1396. // Now we've finished modifying that vector, it's safe to make
  1397. // a vector of StringRefs pointing into it.
  1398. SymNamesHere.insert(SymNamesHere.begin(), DemangledSymNamesHere.begin(),
  1399. DemangledSymNamesHere.end());
  1400. } else {
  1401. for (const SymbolInfoTy &Symbol : SymbolsHere)
  1402. SymNamesHere.push_back(Symbol.Name);
  1403. }
  1404. // Distinguish ELF data from code symbols, which will be used later on to
  1405. // decide whether to 'disassemble' this chunk as a data declaration via
  1406. // dumpELFData(), or whether to treat it as code.
  1407. //
  1408. // If data _and_ code symbols are defined at the same address, the code
  1409. // takes priority, on the grounds that disassembling code is our main
  1410. // purpose here, and it would be a worse failure to _not_ interpret
  1411. // something that _was_ meaningful as code than vice versa.
  1412. //
  1413. // Any ELF symbol type that is not clearly data will be regarded as code.
  1414. // In particular, one of the uses of STT_NOTYPE is for branch targets
  1415. // inside functions, for which STT_FUNC would be inaccurate.
  1416. //
  1417. // So here, we spot whether there's any non-data symbol present at all,
  1418. // and only set the DisassembleAsData flag if there isn't. Also, we use
  1419. // this distinction to inform the decision of which symbol to print at
  1420. // the head of the section, so that if we're printing code, we print a
  1421. // code-related symbol name to go with it.
  1422. bool DisassembleAsData = false;
  1423. size_t DisplaySymIndex = SymbolsHere.size() - 1;
  1424. if (Obj.isELF() && !DisassembleAll && Section.isText()) {
  1425. DisassembleAsData = true; // unless we find a code symbol below
  1426. for (size_t i = 0; i < SymbolsHere.size(); ++i) {
  1427. uint8_t SymTy = SymbolsHere[i].Type;
  1428. if (SymTy != ELF::STT_OBJECT && SymTy != ELF::STT_COMMON) {
  1429. DisassembleAsData = false;
  1430. DisplaySymIndex = i;
  1431. }
  1432. }
  1433. }
  1434. // Decide which symbol(s) from this collection we're going to print.
  1435. std::vector<bool> SymsToPrint(SymbolsHere.size(), false);
  1436. // If the user has given the --disassemble-symbols option, then we must
  1437. // display every symbol in that set, and no others.
  1438. if (!DisasmSymbolSet.empty()) {
  1439. bool FoundAny = false;
  1440. for (size_t i = 0; i < SymbolsHere.size(); ++i) {
  1441. if (DisasmSymbolSet.count(SymNamesHere[i])) {
  1442. SymsToPrint[i] = true;
  1443. FoundAny = true;
  1444. }
  1445. }
  1446. // And if none of the symbols here is one that the user asked for, skip
  1447. // disassembling this entire chunk of code.
  1448. if (!FoundAny)
  1449. continue;
  1450. } else {
  1451. // Otherwise, print whichever symbol at this location is last in the
  1452. // Symbols array, because that array is pre-sorted in a way intended to
  1453. // correlate with priority of which symbol to display.
  1454. SymsToPrint[DisplaySymIndex] = true;
  1455. }
  1456. // Now that we know we're disassembling this section, override the choice
  1457. // of which symbols to display by printing _all_ of them at this address
  1458. // if the user asked for all symbols.
  1459. //
  1460. // That way, '--show-all-symbols --disassemble-symbol=foo' will print
  1461. // only the chunk of code headed by 'foo', but also show any other
  1462. // symbols defined at that address, such as aliases for 'foo', or the ARM
  1463. // mapping symbol preceding its code.
  1464. if (ShowAllSymbols) {
  1465. for (size_t i = 0; i < SymbolsHere.size(); ++i)
  1466. SymsToPrint[i] = true;
  1467. }
  1468. if (Start < SectionAddr || StopAddress <= Start)
  1469. continue;
  1470. for (size_t i = 0; i < SymbolsHere.size(); ++i)
  1471. FoundDisasmSymbolSet.insert(SymNamesHere[i]);
  1472. // The end is the section end, the beginning of the next symbol, or
  1473. // --stop-address.
  1474. uint64_t End = std::min<uint64_t>(SectionAddr + SectSize, StopAddress);
  1475. if (SI < SE)
  1476. End = std::min(End, Symbols[SI].Addr);
  1477. if (Start >= End || End <= StartAddress)
  1478. continue;
  1479. Start -= SectionAddr;
  1480. End -= SectionAddr;
  1481. if (!PrintedSection) {
  1482. PrintedSection = true;
  1483. outs() << "\nDisassembly of section ";
  1484. if (!SegmentName.empty())
  1485. outs() << SegmentName << ",";
  1486. outs() << SectionName << ":\n";
  1487. }
  1488. outs() << '\n';
  1489. for (size_t i = 0; i < SymbolsHere.size(); ++i) {
  1490. if (!SymsToPrint[i])
  1491. continue;
  1492. const SymbolInfoTy &Symbol = SymbolsHere[i];
  1493. const StringRef SymbolName = SymNamesHere[i];
  1494. if (LeadingAddr)
  1495. outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
  1496. SectionAddr + Start + VMAAdjustment);
  1497. if (Obj.isXCOFF() && SymbolDescription) {
  1498. outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
  1499. } else
  1500. outs() << '<' << SymbolName << ">:\n";
  1501. }
  1502. // Don't print raw contents of a virtual section. A virtual section
  1503. // doesn't have any contents in the file.
  1504. if (Section.isVirtual()) {
  1505. outs() << "...\n";
  1506. continue;
  1507. }
  1508. // See if any of the symbols defined at this location triggers target-
  1509. // specific disassembly behavior, e.g. of special descriptors or function
  1510. // prelude information.
  1511. //
  1512. // We stop this loop at the first symbol that triggers some kind of
  1513. // interesting behavior (if any), on the assumption that if two symbols
  1514. // defined at the same address trigger two conflicting symbol handlers,
  1515. // the object file is probably confused anyway, and it would make even
  1516. // less sense to present the output of _both_ handlers, because that
  1517. // would describe the same data twice.
  1518. for (size_t SHI = 0; SHI < SymbolsHere.size(); ++SHI) {
  1519. SymbolInfoTy Symbol = SymbolsHere[SHI];
  1520. auto Status =
  1521. DisAsm->onSymbolStart(Symbol, Size, Bytes.slice(Start, End - Start),
  1522. SectionAddr + Start, CommentStream);
  1523. if (!Status) {
  1524. // If onSymbolStart returns std::nullopt, that means it didn't trigger
  1525. // any interesting handling for this symbol. Try the other symbols
  1526. // defined at this address.
  1527. continue;
  1528. }
  1529. if (*Status == MCDisassembler::Fail) {
  1530. // If onSymbolStart returns Fail, that means it identified some kind
  1531. // of special data at this address, but wasn't able to disassemble it
  1532. // meaningfully. So we fall back to disassembling the failed region
  1533. // as bytes, assuming that the target detected the failure before
  1534. // printing anything.
  1535. //
  1536. // Return values Success or SoftFail (i.e no 'real' failure) are
  1537. // expected to mean that the target has emitted its own output.
  1538. //
  1539. // Either way, 'Size' will have been set to the amount of data
  1540. // covered by whatever prologue the target identified. So we advance
  1541. // our own position to beyond that. Sometimes that will be the entire
  1542. // distance to the next symbol, and sometimes it will be just a
  1543. // prologue and we should start disassembling instructions from where
  1544. // it left off.
  1545. outs() << "// Error in decoding " << SymNamesHere[SHI]
  1546. << " : Decoding failed region as bytes.\n";
  1547. for (uint64_t I = 0; I < Size; ++I) {
  1548. outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
  1549. << "\n";
  1550. }
  1551. }
  1552. Start += Size;
  1553. break;
  1554. }
  1555. Index = Start;
  1556. if (SectionAddr < StartAddress)
  1557. Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
  1558. if (DisassembleAsData) {
  1559. dumpELFData(SectionAddr, Index, End, Bytes);
  1560. Index = End;
  1561. continue;
  1562. }
  1563. bool DumpARMELFData = false;
  1564. formatted_raw_ostream FOS(outs());
  1565. std::unordered_map<uint64_t, std::string> AllLabels;
  1566. std::unordered_map<uint64_t, std::vector<std::string>> BBAddrMapLabels;
  1567. if (SymbolizeOperands) {
  1568. collectLocalBranchTargets(Bytes, MIA, DisAsm, IP, PrimarySTI,
  1569. SectionAddr, Index, End, AllLabels);
  1570. collectBBAddrMapLabels(AddrToBBAddrMap, SectionAddr, Index, End,
  1571. BBAddrMapLabels);
  1572. }
  1573. while (Index < End) {
  1574. // ARM and AArch64 ELF binaries can interleave data and text in the
  1575. // same section. We rely on the markers introduced to understand what
  1576. // we need to dump. If the data marker is within a function, it is
  1577. // denoted as a word/short etc.
  1578. if (!MappingSymbols.empty()) {
  1579. char Kind = getMappingSymbolKind(MappingSymbols, Index);
  1580. DumpARMELFData = Kind == 'd';
  1581. if (SecondarySTI) {
  1582. if (Kind == 'a') {
  1583. STI = PrimaryIsThumb ? SecondarySTI : PrimarySTI;
  1584. DisAsm = PrimaryIsThumb ? SecondaryDisAsm : PrimaryDisAsm;
  1585. } else if (Kind == 't') {
  1586. STI = PrimaryIsThumb ? PrimarySTI : SecondarySTI;
  1587. DisAsm = PrimaryIsThumb ? PrimaryDisAsm : SecondaryDisAsm;
  1588. }
  1589. }
  1590. }
  1591. if (DumpARMELFData) {
  1592. Size = dumpARMELFData(SectionAddr, Index, End, Obj, Bytes,
  1593. MappingSymbols, *STI, FOS);
  1594. } else {
  1595. // When -z or --disassemble-zeroes are given we always dissasemble
  1596. // them. Otherwise we might want to skip zero bytes we see.
  1597. if (!DisassembleZeroes) {
  1598. uint64_t MaxOffset = End - Index;
  1599. // For --reloc: print zero blocks patched by relocations, so that
  1600. // relocations can be shown in the dump.
  1601. if (RelCur != RelEnd)
  1602. MaxOffset = std::min(RelCur->getOffset() - RelAdjustment - Index,
  1603. MaxOffset);
  1604. if (size_t N =
  1605. countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) {
  1606. FOS << "\t\t..." << '\n';
  1607. Index += N;
  1608. continue;
  1609. }
  1610. }
  1611. // Print local label if there's any.
  1612. auto Iter1 = BBAddrMapLabels.find(SectionAddr + Index);
  1613. if (Iter1 != BBAddrMapLabels.end()) {
  1614. for (StringRef Label : Iter1->second)
  1615. FOS << "<" << Label << ">:\n";
  1616. } else {
  1617. auto Iter2 = AllLabels.find(SectionAddr + Index);
  1618. if (Iter2 != AllLabels.end())
  1619. FOS << "<" << Iter2->second << ">:\n";
  1620. }
  1621. // Disassemble a real instruction or a data when disassemble all is
  1622. // provided
  1623. MCInst Inst;
  1624. ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index);
  1625. uint64_t ThisAddr = SectionAddr + Index;
  1626. bool Disassembled = DisAsm->getInstruction(Inst, Size, ThisBytes,
  1627. ThisAddr, CommentStream);
  1628. if (Size == 0)
  1629. Size = std::min<uint64_t>(
  1630. ThisBytes.size(),
  1631. DisAsm->suggestBytesToSkip(ThisBytes, ThisAddr));
  1632. LVP.update({Index, Section.getIndex()},
  1633. {Index + Size, Section.getIndex()}, Index + Size != End);
  1634. IP->setCommentStream(CommentStream);
  1635. PIP.printInst(
  1636. *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
  1637. {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
  1638. "", *STI, &SP, Obj.getFileName(), &Rels, LVP);
  1639. IP->setCommentStream(llvm::nulls());
  1640. // If disassembly has failed, avoid analysing invalid/incomplete
  1641. // instruction information. Otherwise, try to resolve the target
  1642. // address (jump target or memory operand address) and print it on the
  1643. // right of the instruction.
  1644. if (Disassembled && MIA) {
  1645. // Branch targets are printed just after the instructions.
  1646. llvm::raw_ostream *TargetOS = &FOS;
  1647. uint64_t Target;
  1648. bool PrintTarget =
  1649. MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
  1650. if (!PrintTarget)
  1651. if (std::optional<uint64_t> MaybeTarget =
  1652. MIA->evaluateMemoryOperandAddress(
  1653. Inst, STI, SectionAddr + Index, Size)) {
  1654. Target = *MaybeTarget;
  1655. PrintTarget = true;
  1656. // Do not print real address when symbolizing.
  1657. if (!SymbolizeOperands) {
  1658. // Memory operand addresses are printed as comments.
  1659. TargetOS = &CommentStream;
  1660. *TargetOS << "0x" << Twine::utohexstr(Target);
  1661. }
  1662. }
  1663. if (PrintTarget) {
  1664. // In a relocatable object, the target's section must reside in
  1665. // the same section as the call instruction or it is accessed
  1666. // through a relocation.
  1667. //
  1668. // In a non-relocatable object, the target may be in any section.
  1669. // In that case, locate the section(s) containing the target
  1670. // address and find the symbol in one of those, if possible.
  1671. //
  1672. // N.B. We don't walk the relocations in the relocatable case yet.
  1673. std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
  1674. if (!Obj.isRelocatableObject()) {
  1675. auto It = llvm::partition_point(
  1676. SectionAddresses,
  1677. [=](const std::pair<uint64_t, SectionRef> &O) {
  1678. return O.first <= Target;
  1679. });
  1680. uint64_t TargetSecAddr = 0;
  1681. while (It != SectionAddresses.begin()) {
  1682. --It;
  1683. if (TargetSecAddr == 0)
  1684. TargetSecAddr = It->first;
  1685. if (It->first != TargetSecAddr)
  1686. break;
  1687. TargetSectionSymbols.push_back(&AllSymbols[It->second]);
  1688. }
  1689. } else {
  1690. TargetSectionSymbols.push_back(&Symbols);
  1691. }
  1692. TargetSectionSymbols.push_back(&AbsoluteSymbols);
  1693. // Find the last symbol in the first candidate section whose
  1694. // offset is less than or equal to the target. If there are no
  1695. // such symbols, try in the next section and so on, before finally
  1696. // using the nearest preceding absolute symbol (if any), if there
  1697. // are no other valid symbols.
  1698. const SymbolInfoTy *TargetSym = nullptr;
  1699. for (const SectionSymbolsTy *TargetSymbols :
  1700. TargetSectionSymbols) {
  1701. auto It = llvm::partition_point(
  1702. *TargetSymbols,
  1703. [=](const SymbolInfoTy &O) { return O.Addr <= Target; });
  1704. while (It != TargetSymbols->begin()) {
  1705. --It;
  1706. // Skip mapping symbols to avoid possible ambiguity as they
  1707. // do not allow uniquely identifying the target address.
  1708. if (!hasMappingSymbols(Obj) || !isMappingSymbol(*It)) {
  1709. TargetSym = &*It;
  1710. break;
  1711. }
  1712. }
  1713. if (TargetSym)
  1714. break;
  1715. }
  1716. // Print the labels corresponding to the target if there's any.
  1717. bool BBAddrMapLabelAvailable = BBAddrMapLabels.count(Target);
  1718. bool LabelAvailable = AllLabels.count(Target);
  1719. if (TargetSym != nullptr) {
  1720. uint64_t TargetAddress = TargetSym->Addr;
  1721. uint64_t Disp = Target - TargetAddress;
  1722. std::string TargetName = TargetSym->Name.str();
  1723. if (Demangle)
  1724. TargetName = demangle(TargetName);
  1725. *TargetOS << " <";
  1726. if (!Disp) {
  1727. // Always Print the binary symbol precisely corresponding to
  1728. // the target address.
  1729. *TargetOS << TargetName;
  1730. } else if (BBAddrMapLabelAvailable) {
  1731. *TargetOS << BBAddrMapLabels[Target].front();
  1732. } else if (LabelAvailable) {
  1733. *TargetOS << AllLabels[Target];
  1734. } else {
  1735. // Always Print the binary symbol plus an offset if there's no
  1736. // local label corresponding to the target address.
  1737. *TargetOS << TargetName << "+0x" << Twine::utohexstr(Disp);
  1738. }
  1739. *TargetOS << ">";
  1740. } else if (BBAddrMapLabelAvailable) {
  1741. *TargetOS << " <" << BBAddrMapLabels[Target].front() << ">";
  1742. } else if (LabelAvailable) {
  1743. *TargetOS << " <" << AllLabels[Target] << ">";
  1744. }
  1745. // By convention, each record in the comment stream should be
  1746. // terminated.
  1747. if (TargetOS == &CommentStream)
  1748. *TargetOS << "\n";
  1749. }
  1750. }
  1751. }
  1752. assert(Ctx.getAsmInfo());
  1753. emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI,
  1754. CommentStream.str(), LVP);
  1755. Comments.clear();
  1756. // Hexagon does this in pretty printer
  1757. if (Obj.getArch() != Triple::hexagon) {
  1758. // Print relocation for instruction and data.
  1759. while (RelCur != RelEnd) {
  1760. uint64_t Offset = RelCur->getOffset() - RelAdjustment;
  1761. // If this relocation is hidden, skip it.
  1762. if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) {
  1763. ++RelCur;
  1764. continue;
  1765. }
  1766. // Stop when RelCur's offset is past the disassembled
  1767. // instruction/data. Note that it's possible the disassembled data
  1768. // is not the complete data: we might see the relocation printed in
  1769. // the middle of the data, but this matches the binutils objdump
  1770. // output.
  1771. if (Offset >= Index + Size)
  1772. break;
  1773. // When --adjust-vma is used, update the address printed.
  1774. if (RelCur->getSymbol() != Obj.symbol_end()) {
  1775. Expected<section_iterator> SymSI =
  1776. RelCur->getSymbol()->getSection();
  1777. if (SymSI && *SymSI != Obj.section_end() &&
  1778. shouldAdjustVA(**SymSI))
  1779. Offset += AdjustVMA;
  1780. }
  1781. printRelocation(FOS, Obj.getFileName(), *RelCur,
  1782. SectionAddr + Offset, Is64Bits);
  1783. LVP.printAfterOtherLine(FOS, true);
  1784. ++RelCur;
  1785. }
  1786. }
  1787. Index += Size;
  1788. }
  1789. }
  1790. }
  1791. StringSet<> MissingDisasmSymbolSet =
  1792. set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
  1793. for (StringRef Sym : MissingDisasmSymbolSet.keys())
  1794. reportWarning("failed to disassemble missing symbol " + Sym, FileName);
  1795. }
  1796. static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
  1797. // If information useful for showing the disassembly is missing, try to find a
  1798. // more complete binary and disassemble that instead.
  1799. OwningBinary<Binary> FetchedBinary;
  1800. if (Obj->symbols().empty()) {
  1801. if (std::optional<OwningBinary<Binary>> FetchedBinaryOpt =
  1802. fetchBinaryByBuildID(*Obj)) {
  1803. if (auto *O = dyn_cast<ObjectFile>(FetchedBinaryOpt->getBinary())) {
  1804. if (!O->symbols().empty() ||
  1805. (!O->sections().empty() && Obj->sections().empty())) {
  1806. FetchedBinary = std::move(*FetchedBinaryOpt);
  1807. Obj = O;
  1808. }
  1809. }
  1810. }
  1811. }
  1812. const Target *TheTarget = getTarget(Obj);
  1813. // Package up features to be passed to target/subtarget
  1814. Expected<SubtargetFeatures> FeaturesValue = Obj->getFeatures();
  1815. if (!FeaturesValue)
  1816. reportError(FeaturesValue.takeError(), Obj->getFileName());
  1817. SubtargetFeatures Features = *FeaturesValue;
  1818. if (!MAttrs.empty()) {
  1819. for (unsigned I = 0; I != MAttrs.size(); ++I)
  1820. Features.AddFeature(MAttrs[I]);
  1821. } else if (MCPU.empty() && Obj->getArch() == llvm::Triple::aarch64) {
  1822. Features.AddFeature("+all");
  1823. }
  1824. std::unique_ptr<const MCRegisterInfo> MRI(
  1825. TheTarget->createMCRegInfo(TripleName));
  1826. if (!MRI)
  1827. reportError(Obj->getFileName(),
  1828. "no register info for target " + TripleName);
  1829. // Set up disassembler.
  1830. MCTargetOptions MCOptions;
  1831. std::unique_ptr<const MCAsmInfo> AsmInfo(
  1832. TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
  1833. if (!AsmInfo)
  1834. reportError(Obj->getFileName(),
  1835. "no assembly info for target " + TripleName);
  1836. if (MCPU.empty())
  1837. MCPU = Obj->tryGetCPUName().value_or("").str();
  1838. if (isArmElf(*Obj)) {
  1839. // When disassembling big-endian Arm ELF, the instruction endianness is
  1840. // determined in a complex way. In relocatable objects, AAELF32 mandates
  1841. // that instruction endianness matches the ELF file endianness; in
  1842. // executable images, that's true unless the file header has the EF_ARM_BE8
  1843. // flag, in which case instructions are little-endian regardless of data
  1844. // endianness.
  1845. //
  1846. // We must set the big-endian-instructions SubtargetFeature to make the
  1847. // disassembler read the instructions the right way round, and also tell
  1848. // our own prettyprinter to retrieve the encodings the same way to print in
  1849. // hex.
  1850. const auto *Elf32BE = dyn_cast<ELF32BEObjectFile>(Obj);
  1851. if (Elf32BE && (Elf32BE->isRelocatableObject() ||
  1852. !(Elf32BE->getPlatformFlags() & ELF::EF_ARM_BE8))) {
  1853. Features.AddFeature("+big-endian-instructions");
  1854. ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::big);
  1855. } else {
  1856. ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::little);
  1857. }
  1858. }
  1859. std::unique_ptr<const MCSubtargetInfo> STI(
  1860. TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
  1861. if (!STI)
  1862. reportError(Obj->getFileName(),
  1863. "no subtarget info for target " + TripleName);
  1864. std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
  1865. if (!MII)
  1866. reportError(Obj->getFileName(),
  1867. "no instruction info for target " + TripleName);
  1868. MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
  1869. // FIXME: for now initialize MCObjectFileInfo with default values
  1870. std::unique_ptr<MCObjectFileInfo> MOFI(
  1871. TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
  1872. Ctx.setObjectFileInfo(MOFI.get());
  1873. std::unique_ptr<MCDisassembler> DisAsm(
  1874. TheTarget->createMCDisassembler(*STI, Ctx));
  1875. if (!DisAsm)
  1876. reportError(Obj->getFileName(), "no disassembler for target " + TripleName);
  1877. // If we have an ARM object file, we need a second disassembler, because
  1878. // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode.
  1879. // We use mapping symbols to switch between the two assemblers, where
  1880. // appropriate.
  1881. std::unique_ptr<MCDisassembler> SecondaryDisAsm;
  1882. std::unique_ptr<const MCSubtargetInfo> SecondarySTI;
  1883. if (isArmElf(*Obj) && !STI->checkFeatures("+mclass")) {
  1884. if (STI->checkFeatures("+thumb-mode"))
  1885. Features.AddFeature("-thumb-mode");
  1886. else
  1887. Features.AddFeature("+thumb-mode");
  1888. SecondarySTI.reset(TheTarget->createMCSubtargetInfo(TripleName, MCPU,
  1889. Features.getString()));
  1890. SecondaryDisAsm.reset(TheTarget->createMCDisassembler(*SecondarySTI, Ctx));
  1891. }
  1892. std::unique_ptr<const MCInstrAnalysis> MIA(
  1893. TheTarget->createMCInstrAnalysis(MII.get()));
  1894. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  1895. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  1896. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
  1897. if (!IP)
  1898. reportError(Obj->getFileName(),
  1899. "no instruction printer for target " + TripleName);
  1900. IP->setPrintImmHex(PrintImmHex);
  1901. IP->setPrintBranchImmAsAddress(true);
  1902. IP->setSymbolizeOperands(SymbolizeOperands);
  1903. IP->setMCInstrAnalysis(MIA.get());
  1904. PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
  1905. const ObjectFile *DbgObj = Obj;
  1906. if (!FetchedBinary.getBinary() && !Obj->hasDebugInfo()) {
  1907. if (std::optional<OwningBinary<Binary>> DebugBinaryOpt =
  1908. fetchBinaryByBuildID(*Obj)) {
  1909. if (auto *FetchedObj =
  1910. dyn_cast<const ObjectFile>(DebugBinaryOpt->getBinary())) {
  1911. if (FetchedObj->hasDebugInfo()) {
  1912. FetchedBinary = std::move(*DebugBinaryOpt);
  1913. DbgObj = FetchedObj;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. std::unique_ptr<object::Binary> DSYMBinary;
  1919. std::unique_ptr<MemoryBuffer> DSYMBuf;
  1920. if (!DbgObj->hasDebugInfo()) {
  1921. if (const MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*Obj)) {
  1922. DbgObj = objdump::getMachODSymObject(MachOOF, Obj->getFileName(),
  1923. DSYMBinary, DSYMBuf);
  1924. if (!DbgObj)
  1925. return;
  1926. }
  1927. }
  1928. SourcePrinter SP(DbgObj, TheTarget->getName());
  1929. for (StringRef Opt : DisassemblerOptions)
  1930. if (!IP->applyTargetSpecificCLOption(Opt))
  1931. reportError(Obj->getFileName(),
  1932. "Unrecognized disassembler option: " + Opt);
  1933. disassembleObject(TheTarget, *Obj, *DbgObj, Ctx, DisAsm.get(),
  1934. SecondaryDisAsm.get(), MIA.get(), IP.get(), STI.get(),
  1935. SecondarySTI.get(), PIP, SP, InlineRelocs);
  1936. }
  1937. void objdump::printRelocations(const ObjectFile *Obj) {
  1938. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
  1939. "%08" PRIx64;
  1940. // Build a mapping from relocation target to a vector of relocation
  1941. // sections. Usually, there is an only one relocation section for
  1942. // each relocated section.
  1943. MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
  1944. uint64_t Ndx;
  1945. for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
  1946. if (Obj->isELF() && (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC))
  1947. continue;
  1948. if (Section.relocation_begin() == Section.relocation_end())
  1949. continue;
  1950. Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
  1951. if (!SecOrErr)
  1952. reportError(Obj->getFileName(),
  1953. "section (" + Twine(Ndx) +
  1954. "): unable to get a relocation target: " +
  1955. toString(SecOrErr.takeError()));
  1956. SecToRelSec[**SecOrErr].push_back(Section);
  1957. }
  1958. for (std::pair<SectionRef, std::vector<SectionRef>> &P : SecToRelSec) {
  1959. StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName());
  1960. outs() << "\nRELOCATION RECORDS FOR [" << SecName << "]:\n";
  1961. uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
  1962. uint32_t TypePadding = 24;
  1963. outs() << left_justify("OFFSET", OffsetPadding) << " "
  1964. << left_justify("TYPE", TypePadding) << " "
  1965. << "VALUE\n";
  1966. for (SectionRef Section : P.second) {
  1967. for (const RelocationRef &Reloc : Section.relocations()) {
  1968. uint64_t Address = Reloc.getOffset();
  1969. SmallString<32> RelocName;
  1970. SmallString<32> ValueStr;
  1971. if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
  1972. continue;
  1973. Reloc.getTypeName(RelocName);
  1974. if (Error E = getRelocationValueString(Reloc, ValueStr))
  1975. reportError(std::move(E), Obj->getFileName());
  1976. outs() << format(Fmt.data(), Address) << " "
  1977. << left_justify(RelocName, TypePadding) << " " << ValueStr
  1978. << "\n";
  1979. }
  1980. }
  1981. }
  1982. }
  1983. void objdump::printDynamicRelocations(const ObjectFile *Obj) {
  1984. // For the moment, this option is for ELF only
  1985. if (!Obj->isELF())
  1986. return;
  1987. const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
  1988. if (!Elf || !any_of(Elf->sections(), [](const ELFSectionRef Sec) {
  1989. return Sec.getType() == ELF::SHT_DYNAMIC;
  1990. })) {
  1991. reportError(Obj->getFileName(), "not a dynamic object");
  1992. return;
  1993. }
  1994. std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
  1995. if (DynRelSec.empty())
  1996. return;
  1997. outs() << "\nDYNAMIC RELOCATION RECORDS\n";
  1998. const uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
  1999. const uint32_t TypePadding = 24;
  2000. outs() << left_justify("OFFSET", OffsetPadding) << ' '
  2001. << left_justify("TYPE", TypePadding) << " VALUE\n";
  2002. StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2003. for (const SectionRef &Section : DynRelSec)
  2004. for (const RelocationRef &Reloc : Section.relocations()) {
  2005. uint64_t Address = Reloc.getOffset();
  2006. SmallString<32> RelocName;
  2007. SmallString<32> ValueStr;
  2008. Reloc.getTypeName(RelocName);
  2009. if (Error E = getRelocationValueString(Reloc, ValueStr))
  2010. reportError(std::move(E), Obj->getFileName());
  2011. outs() << format(Fmt.data(), Address) << ' '
  2012. << left_justify(RelocName, TypePadding) << ' ' << ValueStr << '\n';
  2013. }
  2014. }
  2015. // Returns true if we need to show LMA column when dumping section headers. We
  2016. // show it only when the platform is ELF and either we have at least one section
  2017. // whose VMA and LMA are different and/or when --show-lma flag is used.
  2018. static bool shouldDisplayLMA(const ObjectFile &Obj) {
  2019. if (!Obj.isELF())
  2020. return false;
  2021. for (const SectionRef &S : ToolSectionFilter(Obj))
  2022. if (S.getAddress() != getELFSectionLMA(S))
  2023. return true;
  2024. return ShowLMA;
  2025. }
  2026. static size_t getMaxSectionNameWidth(const ObjectFile &Obj) {
  2027. // Default column width for names is 13 even if no names are that long.
  2028. size_t MaxWidth = 13;
  2029. for (const SectionRef &Section : ToolSectionFilter(Obj)) {
  2030. StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
  2031. MaxWidth = std::max(MaxWidth, Name.size());
  2032. }
  2033. return MaxWidth;
  2034. }
  2035. void objdump::printSectionHeaders(ObjectFile &Obj) {
  2036. if (Obj.isELF() && Obj.sections().empty())
  2037. createFakeELFSections(Obj);
  2038. size_t NameWidth = getMaxSectionNameWidth(Obj);
  2039. size_t AddressWidth = 2 * Obj.getBytesInAddress();
  2040. bool HasLMAColumn = shouldDisplayLMA(Obj);
  2041. outs() << "\nSections:\n";
  2042. if (HasLMAColumn)
  2043. outs() << "Idx " << left_justify("Name", NameWidth) << " Size "
  2044. << left_justify("VMA", AddressWidth) << " "
  2045. << left_justify("LMA", AddressWidth) << " Type\n";
  2046. else
  2047. outs() << "Idx " << left_justify("Name", NameWidth) << " Size "
  2048. << left_justify("VMA", AddressWidth) << " Type\n";
  2049. uint64_t Idx;
  2050. for (const SectionRef &Section : ToolSectionFilter(Obj, &Idx)) {
  2051. StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
  2052. uint64_t VMA = Section.getAddress();
  2053. if (shouldAdjustVA(Section))
  2054. VMA += AdjustVMA;
  2055. uint64_t Size = Section.getSize();
  2056. std::string Type = Section.isText() ? "TEXT" : "";
  2057. if (Section.isData())
  2058. Type += Type.empty() ? "DATA" : ", DATA";
  2059. if (Section.isBSS())
  2060. Type += Type.empty() ? "BSS" : ", BSS";
  2061. if (Section.isDebugSection())
  2062. Type += Type.empty() ? "DEBUG" : ", DEBUG";
  2063. if (HasLMAColumn)
  2064. outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
  2065. Name.str().c_str(), Size)
  2066. << format_hex_no_prefix(VMA, AddressWidth) << " "
  2067. << format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
  2068. << " " << Type << "\n";
  2069. else
  2070. outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
  2071. Name.str().c_str(), Size)
  2072. << format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
  2073. }
  2074. }
  2075. void objdump::printSectionContents(const ObjectFile *Obj) {
  2076. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
  2077. for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
  2078. StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
  2079. uint64_t BaseAddr = Section.getAddress();
  2080. uint64_t Size = Section.getSize();
  2081. if (!Size)
  2082. continue;
  2083. outs() << "Contents of section ";
  2084. StringRef SegmentName = getSegmentName(MachO, Section);
  2085. if (!SegmentName.empty())
  2086. outs() << SegmentName << ",";
  2087. outs() << Name << ":\n";
  2088. if (Section.isBSS()) {
  2089. outs() << format("<skipping contents of bss section at [%04" PRIx64
  2090. ", %04" PRIx64 ")>\n",
  2091. BaseAddr, BaseAddr + Size);
  2092. continue;
  2093. }
  2094. StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
  2095. // Dump out the content as hex and printable ascii characters.
  2096. for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
  2097. outs() << format(" %04" PRIx64 " ", BaseAddr + Addr);
  2098. // Dump line of hex.
  2099. for (std::size_t I = 0; I < 16; ++I) {
  2100. if (I != 0 && I % 4 == 0)
  2101. outs() << ' ';
  2102. if (Addr + I < End)
  2103. outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true)
  2104. << hexdigit(Contents[Addr + I] & 0xF, true);
  2105. else
  2106. outs() << " ";
  2107. }
  2108. // Print ascii.
  2109. outs() << " ";
  2110. for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) {
  2111. if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF))
  2112. outs() << Contents[Addr + I];
  2113. else
  2114. outs() << ".";
  2115. }
  2116. outs() << "\n";
  2117. }
  2118. }
  2119. }
  2120. void objdump::printSymbolTable(const ObjectFile &O, StringRef ArchiveName,
  2121. StringRef ArchitectureName, bool DumpDynamic) {
  2122. if (O.isCOFF() && !DumpDynamic) {
  2123. outs() << "\nSYMBOL TABLE:\n";
  2124. printCOFFSymbolTable(cast<const COFFObjectFile>(O));
  2125. return;
  2126. }
  2127. const StringRef FileName = O.getFileName();
  2128. if (!DumpDynamic) {
  2129. outs() << "\nSYMBOL TABLE:\n";
  2130. for (auto I = O.symbol_begin(); I != O.symbol_end(); ++I)
  2131. printSymbol(O, *I, {}, FileName, ArchiveName, ArchitectureName,
  2132. DumpDynamic);
  2133. return;
  2134. }
  2135. outs() << "\nDYNAMIC SYMBOL TABLE:\n";
  2136. if (!O.isELF()) {
  2137. reportWarning(
  2138. "this operation is not currently supported for this file format",
  2139. FileName);
  2140. return;
  2141. }
  2142. const ELFObjectFileBase *ELF = cast<const ELFObjectFileBase>(&O);
  2143. auto Symbols = ELF->getDynamicSymbolIterators();
  2144. Expected<std::vector<VersionEntry>> SymbolVersionsOrErr =
  2145. ELF->readDynsymVersions();
  2146. if (!SymbolVersionsOrErr) {
  2147. reportWarning(toString(SymbolVersionsOrErr.takeError()), FileName);
  2148. SymbolVersionsOrErr = std::vector<VersionEntry>();
  2149. (void)!SymbolVersionsOrErr;
  2150. }
  2151. for (auto &Sym : Symbols)
  2152. printSymbol(O, Sym, *SymbolVersionsOrErr, FileName, ArchiveName,
  2153. ArchitectureName, DumpDynamic);
  2154. }
  2155. void objdump::printSymbol(const ObjectFile &O, const SymbolRef &Symbol,
  2156. ArrayRef<VersionEntry> SymbolVersions,
  2157. StringRef FileName, StringRef ArchiveName,
  2158. StringRef ArchitectureName, bool DumpDynamic) {
  2159. const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&O);
  2160. uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName, ArchiveName,
  2161. ArchitectureName);
  2162. if ((Address < StartAddress) || (Address > StopAddress))
  2163. return;
  2164. SymbolRef::Type Type =
  2165. unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
  2166. uint32_t Flags =
  2167. unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
  2168. // Don't ask a Mach-O STAB symbol for its section unless you know that
  2169. // STAB symbol's section field refers to a valid section index. Otherwise
  2170. // the symbol may error trying to load a section that does not exist.
  2171. bool IsSTAB = false;
  2172. if (MachO) {
  2173. DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
  2174. uint8_t NType =
  2175. (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
  2176. : MachO->getSymbolTableEntry(SymDRI).n_type);
  2177. if (NType & MachO::N_STAB)
  2178. IsSTAB = true;
  2179. }
  2180. section_iterator Section = IsSTAB
  2181. ? O.section_end()
  2182. : unwrapOrError(Symbol.getSection(), FileName,
  2183. ArchiveName, ArchitectureName);
  2184. StringRef Name;
  2185. if (Type == SymbolRef::ST_Debug && Section != O.section_end()) {
  2186. if (Expected<StringRef> NameOrErr = Section->getName())
  2187. Name = *NameOrErr;
  2188. else
  2189. consumeError(NameOrErr.takeError());
  2190. } else {
  2191. Name = unwrapOrError(Symbol.getName(), FileName, ArchiveName,
  2192. ArchitectureName);
  2193. }
  2194. bool Global = Flags & SymbolRef::SF_Global;
  2195. bool Weak = Flags & SymbolRef::SF_Weak;
  2196. bool Absolute = Flags & SymbolRef::SF_Absolute;
  2197. bool Common = Flags & SymbolRef::SF_Common;
  2198. bool Hidden = Flags & SymbolRef::SF_Hidden;
  2199. char GlobLoc = ' ';
  2200. if ((Section != O.section_end() || Absolute) && !Weak)
  2201. GlobLoc = Global ? 'g' : 'l';
  2202. char IFunc = ' ';
  2203. if (O.isELF()) {
  2204. if (ELFSymbolRef(Symbol).getELFType() == ELF::STT_GNU_IFUNC)
  2205. IFunc = 'i';
  2206. if (ELFSymbolRef(Symbol).getBinding() == ELF::STB_GNU_UNIQUE)
  2207. GlobLoc = 'u';
  2208. }
  2209. char Debug = ' ';
  2210. if (DumpDynamic)
  2211. Debug = 'D';
  2212. else if (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
  2213. Debug = 'd';
  2214. char FileFunc = ' ';
  2215. if (Type == SymbolRef::ST_File)
  2216. FileFunc = 'f';
  2217. else if (Type == SymbolRef::ST_Function)
  2218. FileFunc = 'F';
  2219. else if (Type == SymbolRef::ST_Data)
  2220. FileFunc = 'O';
  2221. const char *Fmt = O.getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2222. outs() << format(Fmt, Address) << " "
  2223. << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
  2224. << (Weak ? 'w' : ' ') // Weak?
  2225. << ' ' // Constructor. Not supported yet.
  2226. << ' ' // Warning. Not supported yet.
  2227. << IFunc // Indirect reference to another symbol.
  2228. << Debug // Debugging (d) or dynamic (D) symbol.
  2229. << FileFunc // Name of function (F), file (f) or object (O).
  2230. << ' ';
  2231. if (Absolute) {
  2232. outs() << "*ABS*";
  2233. } else if (Common) {
  2234. outs() << "*COM*";
  2235. } else if (Section == O.section_end()) {
  2236. if (O.isXCOFF()) {
  2237. XCOFFSymbolRef XCOFFSym = cast<const XCOFFObjectFile>(O).toSymbolRef(
  2238. Symbol.getRawDataRefImpl());
  2239. if (XCOFF::N_DEBUG == XCOFFSym.getSectionNumber())
  2240. outs() << "*DEBUG*";
  2241. else
  2242. outs() << "*UND*";
  2243. } else
  2244. outs() << "*UND*";
  2245. } else {
  2246. StringRef SegmentName = getSegmentName(MachO, *Section);
  2247. if (!SegmentName.empty())
  2248. outs() << SegmentName << ",";
  2249. StringRef SectionName = unwrapOrError(Section->getName(), FileName);
  2250. outs() << SectionName;
  2251. if (O.isXCOFF()) {
  2252. std::optional<SymbolRef> SymRef =
  2253. getXCOFFSymbolContainingSymbolRef(cast<XCOFFObjectFile>(O), Symbol);
  2254. if (SymRef) {
  2255. Expected<StringRef> NameOrErr = SymRef->getName();
  2256. if (NameOrErr) {
  2257. outs() << " (csect:";
  2258. std::string SymName(NameOrErr.get());
  2259. if (Demangle)
  2260. SymName = demangle(SymName);
  2261. if (SymbolDescription)
  2262. SymName = getXCOFFSymbolDescription(createSymbolInfo(O, *SymRef),
  2263. SymName);
  2264. outs() << ' ' << SymName;
  2265. outs() << ") ";
  2266. } else
  2267. reportWarning(toString(NameOrErr.takeError()), FileName);
  2268. }
  2269. }
  2270. }
  2271. if (Common)
  2272. outs() << '\t' << format(Fmt, static_cast<uint64_t>(Symbol.getAlignment()));
  2273. else if (O.isXCOFF())
  2274. outs() << '\t'
  2275. << format(Fmt, cast<XCOFFObjectFile>(O).getSymbolSize(
  2276. Symbol.getRawDataRefImpl()));
  2277. else if (O.isELF())
  2278. outs() << '\t' << format(Fmt, ELFSymbolRef(Symbol).getSize());
  2279. if (O.isELF()) {
  2280. if (!SymbolVersions.empty()) {
  2281. const VersionEntry &Ver =
  2282. SymbolVersions[Symbol.getRawDataRefImpl().d.b - 1];
  2283. std::string Str;
  2284. if (!Ver.Name.empty())
  2285. Str = Ver.IsVerDef ? ' ' + Ver.Name : '(' + Ver.Name + ')';
  2286. outs() << ' ' << left_justify(Str, 12);
  2287. }
  2288. uint8_t Other = ELFSymbolRef(Symbol).getOther();
  2289. switch (Other) {
  2290. case ELF::STV_DEFAULT:
  2291. break;
  2292. case ELF::STV_INTERNAL:
  2293. outs() << " .internal";
  2294. break;
  2295. case ELF::STV_HIDDEN:
  2296. outs() << " .hidden";
  2297. break;
  2298. case ELF::STV_PROTECTED:
  2299. outs() << " .protected";
  2300. break;
  2301. default:
  2302. outs() << format(" 0x%02x", Other);
  2303. break;
  2304. }
  2305. } else if (Hidden) {
  2306. outs() << " .hidden";
  2307. }
  2308. std::string SymName(Name);
  2309. if (Demangle)
  2310. SymName = demangle(SymName);
  2311. if (O.isXCOFF() && SymbolDescription)
  2312. SymName = getXCOFFSymbolDescription(createSymbolInfo(O, Symbol), SymName);
  2313. outs() << ' ' << SymName << '\n';
  2314. }
  2315. static void printUnwindInfo(const ObjectFile *O) {
  2316. outs() << "Unwind info:\n\n";
  2317. if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O))
  2318. printCOFFUnwindInfo(Coff);
  2319. else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O))
  2320. printMachOUnwindInfo(MachO);
  2321. else
  2322. // TODO: Extract DWARF dump tool to objdump.
  2323. WithColor::error(errs(), ToolName)
  2324. << "This operation is only currently supported "
  2325. "for COFF and MachO object files.\n";
  2326. }
  2327. /// Dump the raw contents of the __clangast section so the output can be piped
  2328. /// into llvm-bcanalyzer.
  2329. static void printRawClangAST(const ObjectFile *Obj) {
  2330. if (outs().is_displayed()) {
  2331. WithColor::error(errs(), ToolName)
  2332. << "The -raw-clang-ast option will dump the raw binary contents of "
  2333. "the clang ast section.\n"
  2334. "Please redirect the output to a file or another program such as "
  2335. "llvm-bcanalyzer.\n";
  2336. return;
  2337. }
  2338. StringRef ClangASTSectionName("__clangast");
  2339. if (Obj->isCOFF()) {
  2340. ClangASTSectionName = "clangast";
  2341. }
  2342. std::optional<object::SectionRef> ClangASTSection;
  2343. for (auto Sec : ToolSectionFilter(*Obj)) {
  2344. StringRef Name;
  2345. if (Expected<StringRef> NameOrErr = Sec.getName())
  2346. Name = *NameOrErr;
  2347. else
  2348. consumeError(NameOrErr.takeError());
  2349. if (Name == ClangASTSectionName) {
  2350. ClangASTSection = Sec;
  2351. break;
  2352. }
  2353. }
  2354. if (!ClangASTSection)
  2355. return;
  2356. StringRef ClangASTContents =
  2357. unwrapOrError(ClangASTSection->getContents(), Obj->getFileName());
  2358. outs().write(ClangASTContents.data(), ClangASTContents.size());
  2359. }
  2360. static void printFaultMaps(const ObjectFile *Obj) {
  2361. StringRef FaultMapSectionName;
  2362. if (Obj->isELF()) {
  2363. FaultMapSectionName = ".llvm_faultmaps";
  2364. } else if (Obj->isMachO()) {
  2365. FaultMapSectionName = "__llvm_faultmaps";
  2366. } else {
  2367. WithColor::error(errs(), ToolName)
  2368. << "This operation is only currently supported "
  2369. "for ELF and Mach-O executable files.\n";
  2370. return;
  2371. }
  2372. std::optional<object::SectionRef> FaultMapSection;
  2373. for (auto Sec : ToolSectionFilter(*Obj)) {
  2374. StringRef Name;
  2375. if (Expected<StringRef> NameOrErr = Sec.getName())
  2376. Name = *NameOrErr;
  2377. else
  2378. consumeError(NameOrErr.takeError());
  2379. if (Name == FaultMapSectionName) {
  2380. FaultMapSection = Sec;
  2381. break;
  2382. }
  2383. }
  2384. outs() << "FaultMap table:\n";
  2385. if (!FaultMapSection) {
  2386. outs() << "<not found>\n";
  2387. return;
  2388. }
  2389. StringRef FaultMapContents =
  2390. unwrapOrError(FaultMapSection->getContents(), Obj->getFileName());
  2391. FaultMapParser FMP(FaultMapContents.bytes_begin(),
  2392. FaultMapContents.bytes_end());
  2393. outs() << FMP;
  2394. }
  2395. static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) {
  2396. if (O->isELF()) {
  2397. printELFFileHeader(O);
  2398. printELFDynamicSection(O);
  2399. printELFSymbolVersionInfo(O);
  2400. return;
  2401. }
  2402. if (O->isCOFF())
  2403. return printCOFFFileHeader(cast<object::COFFObjectFile>(*O));
  2404. if (O->isWasm())
  2405. return printWasmFileHeader(O);
  2406. if (O->isMachO()) {
  2407. printMachOFileHeader(O);
  2408. if (!OnlyFirst)
  2409. printMachOLoadCommands(O);
  2410. return;
  2411. }
  2412. reportError(O->getFileName(), "Invalid/Unsupported object file format");
  2413. }
  2414. static void printFileHeaders(const ObjectFile *O) {
  2415. if (!O->isELF() && !O->isCOFF())
  2416. reportError(O->getFileName(), "Invalid/Unsupported object file format");
  2417. Triple::ArchType AT = O->getArch();
  2418. outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
  2419. uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
  2420. StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
  2421. outs() << "start address: "
  2422. << "0x" << format(Fmt.data(), Address) << "\n";
  2423. }
  2424. static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
  2425. Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
  2426. if (!ModeOrErr) {
  2427. WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n";
  2428. consumeError(ModeOrErr.takeError());
  2429. return;
  2430. }
  2431. sys::fs::perms Mode = ModeOrErr.get();
  2432. outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
  2433. outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
  2434. outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
  2435. outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
  2436. outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
  2437. outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
  2438. outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
  2439. outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
  2440. outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
  2441. outs() << " ";
  2442. outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
  2443. unwrapOrError(C.getGID(), Filename),
  2444. unwrapOrError(C.getRawSize(), Filename));
  2445. StringRef RawLastModified = C.getRawLastModified();
  2446. unsigned Seconds;
  2447. if (RawLastModified.getAsInteger(10, Seconds))
  2448. outs() << "(date: \"" << RawLastModified
  2449. << "\" contains non-decimal chars) ";
  2450. else {
  2451. // Since ctime(3) returns a 26 character string of the form:
  2452. // "Sun Sep 16 01:03:52 1973\n\0"
  2453. // just print 24 characters.
  2454. time_t t = Seconds;
  2455. outs() << format("%.24s ", ctime(&t));
  2456. }
  2457. StringRef Name = "";
  2458. Expected<StringRef> NameOrErr = C.getName();
  2459. if (!NameOrErr) {
  2460. consumeError(NameOrErr.takeError());
  2461. Name = unwrapOrError(C.getRawName(), Filename);
  2462. } else {
  2463. Name = NameOrErr.get();
  2464. }
  2465. outs() << Name << "\n";
  2466. }
  2467. // For ELF only now.
  2468. static bool shouldWarnForInvalidStartStopAddress(ObjectFile *Obj) {
  2469. if (const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj)) {
  2470. if (Elf->getEType() != ELF::ET_REL)
  2471. return true;
  2472. }
  2473. return false;
  2474. }
  2475. static void checkForInvalidStartStopAddress(ObjectFile *Obj,
  2476. uint64_t Start, uint64_t Stop) {
  2477. if (!shouldWarnForInvalidStartStopAddress(Obj))
  2478. return;
  2479. for (const SectionRef &Section : Obj->sections())
  2480. if (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC) {
  2481. uint64_t BaseAddr = Section.getAddress();
  2482. uint64_t Size = Section.getSize();
  2483. if ((Start < BaseAddr + Size) && Stop > BaseAddr)
  2484. return;
  2485. }
  2486. if (!HasStartAddressFlag)
  2487. reportWarning("no section has address less than 0x" +
  2488. Twine::utohexstr(Stop) + " specified by --stop-address",
  2489. Obj->getFileName());
  2490. else if (!HasStopAddressFlag)
  2491. reportWarning("no section has address greater than or equal to 0x" +
  2492. Twine::utohexstr(Start) + " specified by --start-address",
  2493. Obj->getFileName());
  2494. else
  2495. reportWarning("no section overlaps the range [0x" +
  2496. Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) +
  2497. ") specified by --start-address/--stop-address",
  2498. Obj->getFileName());
  2499. }
  2500. static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
  2501. const Archive::Child *C = nullptr) {
  2502. // Avoid other output when using a raw option.
  2503. if (!RawClangAST) {
  2504. outs() << '\n';
  2505. if (A)
  2506. outs() << A->getFileName() << "(" << O->getFileName() << ")";
  2507. else
  2508. outs() << O->getFileName();
  2509. outs() << ":\tfile format " << O->getFileFormatName().lower() << "\n";
  2510. }
  2511. if (HasStartAddressFlag || HasStopAddressFlag)
  2512. checkForInvalidStartStopAddress(O, StartAddress, StopAddress);
  2513. // Note: the order here matches GNU objdump for compatability.
  2514. StringRef ArchiveName = A ? A->getFileName() : "";
  2515. if (ArchiveHeaders && !MachOOpt && C)
  2516. printArchiveChild(ArchiveName, *C);
  2517. if (FileHeaders)
  2518. printFileHeaders(O);
  2519. if (PrivateHeaders || FirstPrivateHeader)
  2520. printPrivateFileHeaders(O, FirstPrivateHeader);
  2521. if (SectionHeaders)
  2522. printSectionHeaders(*O);
  2523. if (SymbolTable)
  2524. printSymbolTable(*O, ArchiveName);
  2525. if (DynamicSymbolTable)
  2526. printSymbolTable(*O, ArchiveName, /*ArchitectureName=*/"",
  2527. /*DumpDynamic=*/true);
  2528. if (DwarfDumpType != DIDT_Null) {
  2529. std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O);
  2530. // Dump the complete DWARF structure.
  2531. DIDumpOptions DumpOpts;
  2532. DumpOpts.DumpType = DwarfDumpType;
  2533. DICtx->dump(outs(), DumpOpts);
  2534. }
  2535. if (Relocations && !Disassemble)
  2536. printRelocations(O);
  2537. if (DynamicRelocations)
  2538. printDynamicRelocations(O);
  2539. if (SectionContents)
  2540. printSectionContents(O);
  2541. if (Disassemble)
  2542. disassembleObject(O, Relocations);
  2543. if (UnwindInfo)
  2544. printUnwindInfo(O);
  2545. // Mach-O specific options:
  2546. if (ExportsTrie)
  2547. printExportsTrie(O);
  2548. if (Rebase)
  2549. printRebaseTable(O);
  2550. if (Bind)
  2551. printBindTable(O);
  2552. if (LazyBind)
  2553. printLazyBindTable(O);
  2554. if (WeakBind)
  2555. printWeakBindTable(O);
  2556. // Other special sections:
  2557. if (RawClangAST)
  2558. printRawClangAST(O);
  2559. if (FaultMapSection)
  2560. printFaultMaps(O);
  2561. if (Offloading)
  2562. dumpOffloadBinary(*O);
  2563. }
  2564. static void dumpObject(const COFFImportFile *I, const Archive *A,
  2565. const Archive::Child *C = nullptr) {
  2566. StringRef ArchiveName = A ? A->getFileName() : "";
  2567. // Avoid other output when using a raw option.
  2568. if (!RawClangAST)
  2569. outs() << '\n'
  2570. << ArchiveName << "(" << I->getFileName() << ")"
  2571. << ":\tfile format COFF-import-file"
  2572. << "\n\n";
  2573. if (ArchiveHeaders && !MachOOpt && C)
  2574. printArchiveChild(ArchiveName, *C);
  2575. if (SymbolTable)
  2576. printCOFFSymbolTable(*I);
  2577. }
  2578. /// Dump each object file in \a a;
  2579. static void dumpArchive(const Archive *A) {
  2580. Error Err = Error::success();
  2581. unsigned I = -1;
  2582. for (auto &C : A->children(Err)) {
  2583. ++I;
  2584. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2585. if (!ChildOrErr) {
  2586. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2587. reportError(std::move(E), getFileNameForError(C, I), A->getFileName());
  2588. continue;
  2589. }
  2590. if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  2591. dumpObject(O, A, &C);
  2592. else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
  2593. dumpObject(I, A, &C);
  2594. else
  2595. reportError(errorCodeToError(object_error::invalid_file_type),
  2596. A->getFileName());
  2597. }
  2598. if (Err)
  2599. reportError(std::move(Err), A->getFileName());
  2600. }
  2601. /// Open file and figure out how to dump it.
  2602. static void dumpInput(StringRef file) {
  2603. // If we are using the Mach-O specific object file parser, then let it parse
  2604. // the file and process the command line options. So the -arch flags can
  2605. // be used to select specific slices, etc.
  2606. if (MachOOpt) {
  2607. parseInputMachO(file);
  2608. return;
  2609. }
  2610. // Attempt to open the binary.
  2611. OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
  2612. Binary &Binary = *OBinary.getBinary();
  2613. if (Archive *A = dyn_cast<Archive>(&Binary))
  2614. dumpArchive(A);
  2615. else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary))
  2616. dumpObject(O);
  2617. else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
  2618. parseInputMachO(UB);
  2619. else if (OffloadBinary *OB = dyn_cast<OffloadBinary>(&Binary))
  2620. dumpOffloadSections(*OB);
  2621. else
  2622. reportError(errorCodeToError(object_error::invalid_file_type), file);
  2623. }
  2624. template <typename T>
  2625. static void parseIntArg(const llvm::opt::InputArgList &InputArgs, int ID,
  2626. T &Value) {
  2627. if (const opt::Arg *A = InputArgs.getLastArg(ID)) {
  2628. StringRef V(A->getValue());
  2629. if (!llvm::to_integer(V, Value, 0)) {
  2630. reportCmdLineError(A->getSpelling() +
  2631. ": expected a non-negative integer, but got '" + V +
  2632. "'");
  2633. }
  2634. }
  2635. }
  2636. static object::BuildID parseBuildIDArg(const opt::Arg *A) {
  2637. StringRef V(A->getValue());
  2638. std::string Bytes;
  2639. if (!tryGetFromHex(V, Bytes))
  2640. reportCmdLineError(A->getSpelling() + ": expected a build ID, but got '" +
  2641. V + "'");
  2642. ArrayRef<uint8_t> BuildID(reinterpret_cast<const uint8_t *>(Bytes.data()),
  2643. Bytes.size());
  2644. return object::BuildID(BuildID.begin(), BuildID.end());
  2645. }
  2646. void objdump::invalidArgValue(const opt::Arg *A) {
  2647. reportCmdLineError("'" + StringRef(A->getValue()) +
  2648. "' is not a valid value for '" + A->getSpelling() + "'");
  2649. }
  2650. static std::vector<std::string>
  2651. commaSeparatedValues(const llvm::opt::InputArgList &InputArgs, int ID) {
  2652. std::vector<std::string> Values;
  2653. for (StringRef Value : InputArgs.getAllArgValues(ID)) {
  2654. llvm::SmallVector<StringRef, 2> SplitValues;
  2655. llvm::SplitString(Value, SplitValues, ",");
  2656. for (StringRef SplitValue : SplitValues)
  2657. Values.push_back(SplitValue.str());
  2658. }
  2659. return Values;
  2660. }
  2661. static void parseOtoolOptions(const llvm::opt::InputArgList &InputArgs) {
  2662. MachOOpt = true;
  2663. FullLeadingAddr = true;
  2664. PrintImmHex = true;
  2665. ArchName = InputArgs.getLastArgValue(OTOOL_arch).str();
  2666. LinkOptHints = InputArgs.hasArg(OTOOL_C);
  2667. if (InputArgs.hasArg(OTOOL_d))
  2668. FilterSections.push_back("__DATA,__data");
  2669. DylibId = InputArgs.hasArg(OTOOL_D);
  2670. UniversalHeaders = InputArgs.hasArg(OTOOL_f);
  2671. DataInCode = InputArgs.hasArg(OTOOL_G);
  2672. FirstPrivateHeader = InputArgs.hasArg(OTOOL_h);
  2673. IndirectSymbols = InputArgs.hasArg(OTOOL_I);
  2674. ShowRawInsn = InputArgs.hasArg(OTOOL_j);
  2675. PrivateHeaders = InputArgs.hasArg(OTOOL_l);
  2676. DylibsUsed = InputArgs.hasArg(OTOOL_L);
  2677. MCPU = InputArgs.getLastArgValue(OTOOL_mcpu_EQ).str();
  2678. ObjcMetaData = InputArgs.hasArg(OTOOL_o);
  2679. DisSymName = InputArgs.getLastArgValue(OTOOL_p).str();
  2680. InfoPlist = InputArgs.hasArg(OTOOL_P);
  2681. Relocations = InputArgs.hasArg(OTOOL_r);
  2682. if (const Arg *A = InputArgs.getLastArg(OTOOL_s)) {
  2683. auto Filter = (A->getValue(0) + StringRef(",") + A->getValue(1)).str();
  2684. FilterSections.push_back(Filter);
  2685. }
  2686. if (InputArgs.hasArg(OTOOL_t))
  2687. FilterSections.push_back("__TEXT,__text");
  2688. Verbose = InputArgs.hasArg(OTOOL_v) || InputArgs.hasArg(OTOOL_V) ||
  2689. InputArgs.hasArg(OTOOL_o);
  2690. SymbolicOperands = InputArgs.hasArg(OTOOL_V);
  2691. if (InputArgs.hasArg(OTOOL_x))
  2692. FilterSections.push_back(",__text");
  2693. LeadingAddr = LeadingHeaders = !InputArgs.hasArg(OTOOL_X);
  2694. ChainedFixups = InputArgs.hasArg(OTOOL_chained_fixups);
  2695. DyldInfo = InputArgs.hasArg(OTOOL_dyld_info);
  2696. InputFilenames = InputArgs.getAllArgValues(OTOOL_INPUT);
  2697. if (InputFilenames.empty())
  2698. reportCmdLineError("no input file");
  2699. for (const Arg *A : InputArgs) {
  2700. const Option &O = A->getOption();
  2701. if (O.getGroup().isValid() && O.getGroup().getID() == OTOOL_grp_obsolete) {
  2702. reportCmdLineWarning(O.getPrefixedName() +
  2703. " is obsolete and not implemented");
  2704. }
  2705. }
  2706. }
  2707. static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
  2708. parseIntArg(InputArgs, OBJDUMP_adjust_vma_EQ, AdjustVMA);
  2709. AllHeaders = InputArgs.hasArg(OBJDUMP_all_headers);
  2710. ArchName = InputArgs.getLastArgValue(OBJDUMP_arch_name_EQ).str();
  2711. ArchiveHeaders = InputArgs.hasArg(OBJDUMP_archive_headers);
  2712. Demangle = InputArgs.hasArg(OBJDUMP_demangle);
  2713. Disassemble = InputArgs.hasArg(OBJDUMP_disassemble);
  2714. DisassembleAll = InputArgs.hasArg(OBJDUMP_disassemble_all);
  2715. SymbolDescription = InputArgs.hasArg(OBJDUMP_symbol_description);
  2716. DisassembleSymbols =
  2717. commaSeparatedValues(InputArgs, OBJDUMP_disassemble_symbols_EQ);
  2718. DisassembleZeroes = InputArgs.hasArg(OBJDUMP_disassemble_zeroes);
  2719. if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_dwarf_EQ)) {
  2720. DwarfDumpType = StringSwitch<DIDumpType>(A->getValue())
  2721. .Case("frames", DIDT_DebugFrame)
  2722. .Default(DIDT_Null);
  2723. if (DwarfDumpType == DIDT_Null)
  2724. invalidArgValue(A);
  2725. }
  2726. DynamicRelocations = InputArgs.hasArg(OBJDUMP_dynamic_reloc);
  2727. FaultMapSection = InputArgs.hasArg(OBJDUMP_fault_map_section);
  2728. Offloading = InputArgs.hasArg(OBJDUMP_offloading);
  2729. FileHeaders = InputArgs.hasArg(OBJDUMP_file_headers);
  2730. SectionContents = InputArgs.hasArg(OBJDUMP_full_contents);
  2731. PrintLines = InputArgs.hasArg(OBJDUMP_line_numbers);
  2732. InputFilenames = InputArgs.getAllArgValues(OBJDUMP_INPUT);
  2733. MachOOpt = InputArgs.hasArg(OBJDUMP_macho);
  2734. MCPU = InputArgs.getLastArgValue(OBJDUMP_mcpu_EQ).str();
  2735. MAttrs = commaSeparatedValues(InputArgs, OBJDUMP_mattr_EQ);
  2736. ShowRawInsn = !InputArgs.hasArg(OBJDUMP_no_show_raw_insn);
  2737. LeadingAddr = !InputArgs.hasArg(OBJDUMP_no_leading_addr);
  2738. RawClangAST = InputArgs.hasArg(OBJDUMP_raw_clang_ast);
  2739. Relocations = InputArgs.hasArg(OBJDUMP_reloc);
  2740. PrintImmHex =
  2741. InputArgs.hasFlag(OBJDUMP_print_imm_hex, OBJDUMP_no_print_imm_hex, true);
  2742. PrivateHeaders = InputArgs.hasArg(OBJDUMP_private_headers);
  2743. FilterSections = InputArgs.getAllArgValues(OBJDUMP_section_EQ);
  2744. SectionHeaders = InputArgs.hasArg(OBJDUMP_section_headers);
  2745. ShowAllSymbols = InputArgs.hasArg(OBJDUMP_show_all_symbols);
  2746. ShowLMA = InputArgs.hasArg(OBJDUMP_show_lma);
  2747. PrintSource = InputArgs.hasArg(OBJDUMP_source);
  2748. parseIntArg(InputArgs, OBJDUMP_start_address_EQ, StartAddress);
  2749. HasStartAddressFlag = InputArgs.hasArg(OBJDUMP_start_address_EQ);
  2750. parseIntArg(InputArgs, OBJDUMP_stop_address_EQ, StopAddress);
  2751. HasStopAddressFlag = InputArgs.hasArg(OBJDUMP_stop_address_EQ);
  2752. SymbolTable = InputArgs.hasArg(OBJDUMP_syms);
  2753. SymbolizeOperands = InputArgs.hasArg(OBJDUMP_symbolize_operands);
  2754. DynamicSymbolTable = InputArgs.hasArg(OBJDUMP_dynamic_syms);
  2755. TripleName = InputArgs.getLastArgValue(OBJDUMP_triple_EQ).str();
  2756. UnwindInfo = InputArgs.hasArg(OBJDUMP_unwind_info);
  2757. Wide = InputArgs.hasArg(OBJDUMP_wide);
  2758. Prefix = InputArgs.getLastArgValue(OBJDUMP_prefix).str();
  2759. parseIntArg(InputArgs, OBJDUMP_prefix_strip, PrefixStrip);
  2760. if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_debug_vars_EQ)) {
  2761. DbgVariables = StringSwitch<DebugVarsFormat>(A->getValue())
  2762. .Case("ascii", DVASCII)
  2763. .Case("unicode", DVUnicode)
  2764. .Default(DVInvalid);
  2765. if (DbgVariables == DVInvalid)
  2766. invalidArgValue(A);
  2767. }
  2768. parseIntArg(InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);
  2769. parseMachOOptions(InputArgs);
  2770. // Parse -M (--disassembler-options) and deprecated
  2771. // --x86-asm-syntax={att,intel}.
  2772. //
  2773. // Note, for x86, the asm dialect (AssemblerDialect) is initialized when the
  2774. // MCAsmInfo is constructed. MCInstPrinter::applyTargetSpecificCLOption is
  2775. // called too late. For now we have to use the internal cl::opt option.
  2776. const char *AsmSyntax = nullptr;
  2777. for (const auto *A : InputArgs.filtered(OBJDUMP_disassembler_options_EQ,
  2778. OBJDUMP_x86_asm_syntax_att,
  2779. OBJDUMP_x86_asm_syntax_intel)) {
  2780. switch (A->getOption().getID()) {
  2781. case OBJDUMP_x86_asm_syntax_att:
  2782. AsmSyntax = "--x86-asm-syntax=att";
  2783. continue;
  2784. case OBJDUMP_x86_asm_syntax_intel:
  2785. AsmSyntax = "--x86-asm-syntax=intel";
  2786. continue;
  2787. }
  2788. SmallVector<StringRef, 2> Values;
  2789. llvm::SplitString(A->getValue(), Values, ",");
  2790. for (StringRef V : Values) {
  2791. if (V == "att")
  2792. AsmSyntax = "--x86-asm-syntax=att";
  2793. else if (V == "intel")
  2794. AsmSyntax = "--x86-asm-syntax=intel";
  2795. else
  2796. DisassemblerOptions.push_back(V.str());
  2797. }
  2798. }
  2799. if (AsmSyntax) {
  2800. const char *Argv[] = {"llvm-objdump", AsmSyntax};
  2801. llvm::cl::ParseCommandLineOptions(2, Argv);
  2802. }
  2803. // Look up any provided build IDs, then append them to the input filenames.
  2804. for (const opt::Arg *A : InputArgs.filtered(OBJDUMP_build_id)) {
  2805. object::BuildID BuildID = parseBuildIDArg(A);
  2806. std::optional<std::string> Path = BIDFetcher->fetch(BuildID);
  2807. if (!Path) {
  2808. reportCmdLineError(A->getSpelling() + ": could not find build ID '" +
  2809. A->getValue() + "'");
  2810. }
  2811. InputFilenames.push_back(std::move(*Path));
  2812. }
  2813. // objdump defaults to a.out if no filenames specified.
  2814. if (InputFilenames.empty())
  2815. InputFilenames.push_back("a.out");
  2816. }
  2817. int main(int argc, char **argv) {
  2818. using namespace llvm;
  2819. InitLLVM X(argc, argv);
  2820. ToolName = argv[0];
  2821. std::unique_ptr<CommonOptTable> T;
  2822. OptSpecifier Unknown, HelpFlag, HelpHiddenFlag, VersionFlag;
  2823. StringRef Stem = sys::path::stem(ToolName);
  2824. auto Is = [=](StringRef Tool) {
  2825. // We need to recognize the following filenames:
  2826. //
  2827. // llvm-objdump -> objdump
  2828. // llvm-otool-10.exe -> otool
  2829. // powerpc64-unknown-freebsd13-objdump -> objdump
  2830. auto I = Stem.rfind_insensitive(Tool);
  2831. return I != StringRef::npos &&
  2832. (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
  2833. };
  2834. if (Is("otool")) {
  2835. T = std::make_unique<OtoolOptTable>();
  2836. Unknown = OTOOL_UNKNOWN;
  2837. HelpFlag = OTOOL_help;
  2838. HelpHiddenFlag = OTOOL_help_hidden;
  2839. VersionFlag = OTOOL_version;
  2840. } else {
  2841. T = std::make_unique<ObjdumpOptTable>();
  2842. Unknown = OBJDUMP_UNKNOWN;
  2843. HelpFlag = OBJDUMP_help;
  2844. HelpHiddenFlag = OBJDUMP_help_hidden;
  2845. VersionFlag = OBJDUMP_version;
  2846. }
  2847. BumpPtrAllocator A;
  2848. StringSaver Saver(A);
  2849. opt::InputArgList InputArgs =
  2850. T->parseArgs(argc, argv, Unknown, Saver,
  2851. [&](StringRef Msg) { reportCmdLineError(Msg); });
  2852. if (InputArgs.size() == 0 || InputArgs.hasArg(HelpFlag)) {
  2853. T->printHelp(ToolName);
  2854. return 0;
  2855. }
  2856. if (InputArgs.hasArg(HelpHiddenFlag)) {
  2857. T->printHelp(ToolName, /*ShowHidden=*/true);
  2858. return 0;
  2859. }
  2860. // Initialize targets and assembly printers/parsers.
  2861. InitializeAllTargetInfos();
  2862. InitializeAllTargetMCs();
  2863. InitializeAllDisassemblers();
  2864. if (InputArgs.hasArg(VersionFlag)) {
  2865. cl::PrintVersionMessage();
  2866. if (!Is("otool")) {
  2867. outs() << '\n';
  2868. TargetRegistry::printRegisteredTargetsForVersion(outs());
  2869. }
  2870. return 0;
  2871. }
  2872. // Initialize debuginfod.
  2873. const bool ShouldUseDebuginfodByDefault =
  2874. InputArgs.hasArg(OBJDUMP_build_id) || canUseDebuginfod();
  2875. std::vector<std::string> DebugFileDirectories =
  2876. InputArgs.getAllArgValues(OBJDUMP_debug_file_directory);
  2877. if (InputArgs.hasFlag(OBJDUMP_debuginfod, OBJDUMP_no_debuginfod,
  2878. ShouldUseDebuginfodByDefault)) {
  2879. HTTPClient::initialize();
  2880. BIDFetcher =
  2881. std::make_unique<DebuginfodFetcher>(std::move(DebugFileDirectories));
  2882. } else {
  2883. BIDFetcher =
  2884. std::make_unique<BuildIDFetcher>(std::move(DebugFileDirectories));
  2885. }
  2886. if (Is("otool"))
  2887. parseOtoolOptions(InputArgs);
  2888. else
  2889. parseObjdumpOptions(InputArgs);
  2890. if (StartAddress >= StopAddress)
  2891. reportCmdLineError("start address should be less than stop address");
  2892. // Removes trailing separators from prefix.
  2893. while (!Prefix.empty() && sys::path::is_separator(Prefix.back()))
  2894. Prefix.pop_back();
  2895. if (AllHeaders)
  2896. ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
  2897. SectionHeaders = SymbolTable = true;
  2898. if (DisassembleAll || PrintSource || PrintLines ||
  2899. !DisassembleSymbols.empty())
  2900. Disassemble = true;
  2901. if (!ArchiveHeaders && !Disassemble && DwarfDumpType == DIDT_Null &&
  2902. !DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
  2903. !Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
  2904. !DynamicSymbolTable && !UnwindInfo && !FaultMapSection && !Offloading &&
  2905. !(MachOOpt &&
  2906. (Bind || DataInCode || ChainedFixups || DyldInfo || DylibId ||
  2907. DylibsUsed || ExportsTrie || FirstPrivateHeader ||
  2908. FunctionStartsType != FunctionStartsMode::None || IndirectSymbols ||
  2909. InfoPlist || LazyBind || LinkOptHints || ObjcMetaData || Rebase ||
  2910. Rpaths || UniversalHeaders || WeakBind || !FilterSections.empty()))) {
  2911. T->printHelp(ToolName);
  2912. return 2;
  2913. }
  2914. DisasmSymbolSet.insert(DisassembleSymbols.begin(), DisassembleSymbols.end());
  2915. llvm::for_each(InputFilenames, dumpInput);
  2916. warnOnNoMatchForSections();
  2917. return EXIT_SUCCESS;
  2918. }