llvm-readobj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is a tool similar to readelf, except it works on multiple object file
  11. // formats. The main purpose of this tool is to provide detailed output suitable
  12. // for FileCheck.
  13. //
  14. // Flags should be similar to readelf where supported, but the output format
  15. // does not need to be identical. The point is to not make users learn yet
  16. // another set of flags.
  17. //
  18. // Output should be specialized for each format where appropriate.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm-readobj.h"
  22. #include "Error.h"
  23. #include "ObjDumper.h"
  24. #include "StreamWriter.h"
  25. #include "llvm/Object/Archive.h"
  26. #include "llvm/Object/ELFObjectFile.h"
  27. #include "llvm/Object/MachOUniversal.h"
  28. #include "llvm/Object/ObjectFile.h"
  29. #include "llvm/Support/Casting.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/DataTypes.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/FileSystem.h"
  34. #include "llvm/Support/ManagedStatic.h"
  35. #include "llvm/Support/PrettyStackTrace.h"
  36. #include "llvm/Support/Signals.h"
  37. #include "llvm/Support/TargetRegistry.h"
  38. #include "llvm/Support/TargetSelect.h"
  39. #include <string>
  40. #include <system_error>
  41. using namespace llvm;
  42. using namespace llvm::object;
  43. namespace opts {
  44. cl::list<std::string> InputFilenames(cl::Positional,
  45. cl::desc("<input object files>"),
  46. cl::ZeroOrMore);
  47. // -file-headers, -h
  48. cl::opt<bool> FileHeaders("file-headers",
  49. cl::desc("Display file headers "));
  50. cl::alias FileHeadersShort("h",
  51. cl::desc("Alias for --file-headers"),
  52. cl::aliasopt(FileHeaders));
  53. // -sections, -s
  54. cl::opt<bool> Sections("sections",
  55. cl::desc("Display all sections."));
  56. cl::alias SectionsShort("s",
  57. cl::desc("Alias for --sections"),
  58. cl::aliasopt(Sections));
  59. // -section-relocations, -sr
  60. cl::opt<bool> SectionRelocations("section-relocations",
  61. cl::desc("Display relocations for each section shown."));
  62. cl::alias SectionRelocationsShort("sr",
  63. cl::desc("Alias for --section-relocations"),
  64. cl::aliasopt(SectionRelocations));
  65. // -section-symbols, -st
  66. cl::opt<bool> SectionSymbols("section-symbols",
  67. cl::desc("Display symbols for each section shown."));
  68. cl::alias SectionSymbolsShort("st",
  69. cl::desc("Alias for --section-symbols"),
  70. cl::aliasopt(SectionSymbols));
  71. // -section-data, -sd
  72. cl::opt<bool> SectionData("section-data",
  73. cl::desc("Display section data for each section shown."));
  74. cl::alias SectionDataShort("sd",
  75. cl::desc("Alias for --section-data"),
  76. cl::aliasopt(SectionData));
  77. // -relocations, -r
  78. cl::opt<bool> Relocations("relocations",
  79. cl::desc("Display the relocation entries in the file"));
  80. cl::alias RelocationsShort("r",
  81. cl::desc("Alias for --relocations"),
  82. cl::aliasopt(Relocations));
  83. // -dyn-relocations
  84. cl::opt<bool> DynRelocs("dyn-relocations",
  85. cl::desc("Display the dynamic relocation entries in the file"));
  86. // -symbols, -t
  87. cl::opt<bool> Symbols("symbols",
  88. cl::desc("Display the symbol table"));
  89. cl::alias SymbolsShort("t",
  90. cl::desc("Alias for --symbols"),
  91. cl::aliasopt(Symbols));
  92. // -dyn-symbols, -dt
  93. cl::opt<bool> DynamicSymbols("dyn-symbols",
  94. cl::desc("Display the dynamic symbol table"));
  95. cl::alias DynamicSymbolsShort("dt",
  96. cl::desc("Alias for --dyn-symbols"),
  97. cl::aliasopt(DynamicSymbols));
  98. // -unwind, -u
  99. cl::opt<bool> UnwindInfo("unwind",
  100. cl::desc("Display unwind information"));
  101. cl::alias UnwindInfoShort("u",
  102. cl::desc("Alias for --unwind"),
  103. cl::aliasopt(UnwindInfo));
  104. // -dynamic-table
  105. cl::opt<bool> DynamicTable("dynamic-table",
  106. cl::desc("Display the ELF .dynamic section table"));
  107. // -needed-libs
  108. cl::opt<bool> NeededLibraries("needed-libs",
  109. cl::desc("Display the needed libraries"));
  110. // -program-headers
  111. cl::opt<bool> ProgramHeaders("program-headers",
  112. cl::desc("Display ELF program headers"));
  113. // -hash-table
  114. cl::opt<bool> HashTable("hash-table",
  115. cl::desc("Display ELF hash table"));
  116. // -expand-relocs
  117. cl::opt<bool> ExpandRelocs("expand-relocs",
  118. cl::desc("Expand each shown relocation to multiple lines"));
  119. // -codeview
  120. cl::opt<bool> CodeView("codeview",
  121. cl::desc("Display CodeView debug information"));
  122. // -codeview-subsection-bytes
  123. cl::opt<bool> CodeViewSubsectionBytes(
  124. "codeview-subsection-bytes",
  125. cl::desc("Dump raw contents of codeview debug sections and records"));
  126. // -arm-attributes, -a
  127. cl::opt<bool> ARMAttributes("arm-attributes",
  128. cl::desc("Display the ARM attributes section"));
  129. cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
  130. cl::aliasopt(ARMAttributes));
  131. // -mips-plt-got
  132. cl::opt<bool>
  133. MipsPLTGOT("mips-plt-got",
  134. cl::desc("Display the MIPS GOT and PLT GOT sections"));
  135. // -mips-abi-flags
  136. cl::opt<bool> MipsABIFlags("mips-abi-flags",
  137. cl::desc("Display the MIPS.abiflags section"));
  138. // -mips-reginfo
  139. cl::opt<bool> MipsReginfo("mips-reginfo",
  140. cl::desc("Display the MIPS .reginfo section"));
  141. // -coff-imports
  142. cl::opt<bool>
  143. COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
  144. // -coff-exports
  145. cl::opt<bool>
  146. COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
  147. // -coff-directives
  148. cl::opt<bool>
  149. COFFDirectives("coff-directives",
  150. cl::desc("Display the PE/COFF .drectve section"));
  151. // -coff-basereloc
  152. cl::opt<bool>
  153. COFFBaseRelocs("coff-basereloc",
  154. cl::desc("Display the PE/COFF .reloc section"));
  155. // -macho-data-in-code
  156. cl::opt<bool>
  157. MachODataInCode("macho-data-in-code",
  158. cl::desc("Display MachO Data in Code command"));
  159. // -macho-version-min
  160. cl::opt<bool>
  161. MachOVersionMin("macho-version-min",
  162. cl::desc("Display MachO version min command"));
  163. // -stackmap
  164. cl::opt<bool>
  165. PrintStackMap("stackmap",
  166. cl::desc("Display contents of stackmap section"));
  167. } // namespace opts
  168. namespace llvm {
  169. void reportError(Twine Msg) {
  170. outs() << "\nError reading file: " << Msg << ".\n";
  171. outs().flush();
  172. exit(1);
  173. }
  174. void error(std::error_code EC) {
  175. if (!EC)
  176. return;
  177. reportError(EC.message());
  178. }
  179. bool relocAddressLess(RelocationRef a, RelocationRef b) {
  180. return a.getOffset() < b.getOffset();
  181. }
  182. } // namespace llvm
  183. static void reportError(StringRef Input, std::error_code EC) {
  184. if (Input == "-")
  185. Input = "<stdin>";
  186. reportError(Twine(Input) + ": " + EC.message());
  187. }
  188. static void reportError(StringRef Input, StringRef Message) {
  189. if (Input == "-")
  190. Input = "<stdin>";
  191. reportError(Twine(Input) + ": " + Message);
  192. }
  193. static bool isMipsArch(unsigned Arch) {
  194. switch (Arch) {
  195. case llvm::Triple::mips:
  196. case llvm::Triple::mipsel:
  197. case llvm::Triple::mips64:
  198. case llvm::Triple::mips64el:
  199. return true;
  200. default:
  201. return false;
  202. }
  203. }
  204. /// @brief Creates an format-specific object file dumper.
  205. static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
  206. std::unique_ptr<ObjDumper> &Result) {
  207. if (!Obj)
  208. return readobj_error::unsupported_file_format;
  209. if (Obj->isCOFF())
  210. return createCOFFDumper(Obj, Writer, Result);
  211. if (Obj->isELF())
  212. return createELFDumper(Obj, Writer, Result);
  213. if (Obj->isMachO())
  214. return createMachODumper(Obj, Writer, Result);
  215. return readobj_error::unsupported_obj_file_format;
  216. }
  217. /// @brief Dumps the specified object file.
  218. static void dumpObject(const ObjectFile *Obj) {
  219. StreamWriter Writer(outs());
  220. std::unique_ptr<ObjDumper> Dumper;
  221. if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
  222. reportError(Obj->getFileName(), EC);
  223. return;
  224. }
  225. outs() << '\n';
  226. outs() << "File: " << Obj->getFileName() << "\n";
  227. outs() << "Format: " << Obj->getFileFormatName() << "\n";
  228. outs() << "Arch: "
  229. << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
  230. << "\n";
  231. outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
  232. Dumper->printLoadName();
  233. if (opts::FileHeaders)
  234. Dumper->printFileHeaders();
  235. if (opts::Sections)
  236. Dumper->printSections();
  237. if (opts::Relocations)
  238. Dumper->printRelocations();
  239. if (opts::DynRelocs)
  240. Dumper->printDynamicRelocations();
  241. if (opts::Symbols)
  242. Dumper->printSymbols();
  243. if (opts::DynamicSymbols)
  244. Dumper->printDynamicSymbols();
  245. if (opts::UnwindInfo)
  246. Dumper->printUnwindInfo();
  247. if (opts::DynamicTable)
  248. Dumper->printDynamicTable();
  249. if (opts::NeededLibraries)
  250. Dumper->printNeededLibraries();
  251. if (opts::ProgramHeaders)
  252. Dumper->printProgramHeaders();
  253. if (opts::HashTable)
  254. Dumper->printHashTable();
  255. if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
  256. if (opts::ARMAttributes)
  257. Dumper->printAttributes();
  258. if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
  259. if (opts::MipsPLTGOT)
  260. Dumper->printMipsPLTGOT();
  261. if (opts::MipsABIFlags)
  262. Dumper->printMipsABIFlags();
  263. if (opts::MipsReginfo)
  264. Dumper->printMipsReginfo();
  265. }
  266. if (Obj->isCOFF()) {
  267. if (opts::COFFImports)
  268. Dumper->printCOFFImports();
  269. if (opts::COFFExports)
  270. Dumper->printCOFFExports();
  271. if (opts::COFFDirectives)
  272. Dumper->printCOFFDirectives();
  273. if (opts::COFFBaseRelocs)
  274. Dumper->printCOFFBaseReloc();
  275. }
  276. if (Obj->isMachO())
  277. if (opts::MachODataInCode)
  278. Dumper->printMachODataInCode();
  279. if (opts::MachOVersionMin)
  280. Dumper->printMachOVersionMin();
  281. if (opts::PrintStackMap)
  282. Dumper->printStackMap();
  283. }
  284. /// @brief Dumps each object file in \a Arc;
  285. static void dumpArchive(const Archive *Arc) {
  286. for (const auto &Child : Arc->children()) {
  287. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
  288. if (std::error_code EC = ChildOrErr.getError()) {
  289. // Ignore non-object files.
  290. if (EC != object_error::invalid_file_type)
  291. reportError(Arc->getFileName(), EC.message());
  292. continue;
  293. }
  294. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  295. dumpObject(Obj);
  296. else
  297. reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  298. }
  299. }
  300. /// @brief Dumps each object file in \a MachO Universal Binary;
  301. static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
  302. for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
  303. ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
  304. if (ObjOrErr)
  305. dumpObject(&*ObjOrErr.get());
  306. else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
  307. dumpArchive(&*AOrErr.get());
  308. else
  309. reportError(UBinary->getFileName(), ObjOrErr.getError().message());
  310. }
  311. }
  312. /// @brief Opens \a File and dumps it.
  313. static void dumpInput(StringRef File) {
  314. // If file isn't stdin, check that it exists.
  315. if (File != "-" && !sys::fs::exists(File)) {
  316. reportError(File, readobj_error::file_not_found);
  317. return;
  318. }
  319. // Attempt to open the binary.
  320. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
  321. if (std::error_code EC = BinaryOrErr.getError()) {
  322. reportError(File, EC);
  323. return;
  324. }
  325. Binary &Binary = *BinaryOrErr.get().getBinary();
  326. if (Archive *Arc = dyn_cast<Archive>(&Binary))
  327. dumpArchive(Arc);
  328. else if (MachOUniversalBinary *UBinary =
  329. dyn_cast<MachOUniversalBinary>(&Binary))
  330. dumpMachOUniversalBinary(UBinary);
  331. else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  332. dumpObject(Obj);
  333. else
  334. reportError(File, readobj_error::unrecognized_file_format);
  335. }
  336. int main(int argc, const char *argv[]) {
  337. sys::PrintStackTraceOnErrorSignal();
  338. PrettyStackTraceProgram X(argc, argv);
  339. llvm_shutdown_obj Y;
  340. // Register the target printer for --version.
  341. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  342. cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
  343. // Default to stdin if no filename is specified.
  344. if (opts::InputFilenames.size() == 0)
  345. opts::InputFilenames.push_back("-");
  346. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  347. dumpInput);
  348. return 0;
  349. }