llvm-readobj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. // -stackmap
  156. cl::opt<bool>
  157. PrintStackMap("stackmap",
  158. cl::desc("Display contents of stackmap section"));
  159. } // namespace opts
  160. namespace llvm {
  161. void reportError(Twine Msg) {
  162. outs() << Msg << "\n";
  163. outs().flush();
  164. exit(1);
  165. }
  166. void error(std::error_code EC) {
  167. if (!EC)
  168. return;
  169. reportError(Twine("\nError reading file: ") + EC.message() + ".");
  170. }
  171. bool relocAddressLess(RelocationRef a, RelocationRef b) {
  172. return a.getOffset() < b.getOffset();
  173. }
  174. } // namespace llvm
  175. static void reportError(StringRef Input, std::error_code EC) {
  176. if (Input == "-")
  177. Input = "<stdin>";
  178. reportError(Twine(Input) + ": " + EC.message());
  179. }
  180. static void reportError(StringRef Input, StringRef Message) {
  181. if (Input == "-")
  182. Input = "<stdin>";
  183. reportError(Twine(Input) + ": " + Message);
  184. }
  185. static bool isMipsArch(unsigned Arch) {
  186. switch (Arch) {
  187. case llvm::Triple::mips:
  188. case llvm::Triple::mipsel:
  189. case llvm::Triple::mips64:
  190. case llvm::Triple::mips64el:
  191. return true;
  192. default:
  193. return false;
  194. }
  195. }
  196. /// @brief Creates an format-specific object file dumper.
  197. static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
  198. std::unique_ptr<ObjDumper> &Result) {
  199. if (!Obj)
  200. return readobj_error::unsupported_file_format;
  201. if (Obj->isCOFF())
  202. return createCOFFDumper(Obj, Writer, Result);
  203. if (Obj->isELF())
  204. return createELFDumper(Obj, Writer, Result);
  205. if (Obj->isMachO())
  206. return createMachODumper(Obj, Writer, Result);
  207. return readobj_error::unsupported_obj_file_format;
  208. }
  209. /// @brief Dumps the specified object file.
  210. static void dumpObject(const ObjectFile *Obj) {
  211. StreamWriter Writer(outs());
  212. std::unique_ptr<ObjDumper> Dumper;
  213. if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
  214. reportError(Obj->getFileName(), EC);
  215. return;
  216. }
  217. outs() << '\n';
  218. outs() << "File: " << Obj->getFileName() << "\n";
  219. outs() << "Format: " << Obj->getFileFormatName() << "\n";
  220. outs() << "Arch: "
  221. << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
  222. << "\n";
  223. outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
  224. Dumper->printLoadName();
  225. if (opts::FileHeaders)
  226. Dumper->printFileHeaders();
  227. if (opts::Sections)
  228. Dumper->printSections();
  229. if (opts::Relocations)
  230. Dumper->printRelocations();
  231. if (opts::DynRelocs)
  232. Dumper->printDynamicRelocations();
  233. if (opts::Symbols)
  234. Dumper->printSymbols();
  235. if (opts::DynamicSymbols)
  236. Dumper->printDynamicSymbols();
  237. if (opts::UnwindInfo)
  238. Dumper->printUnwindInfo();
  239. if (opts::DynamicTable)
  240. Dumper->printDynamicTable();
  241. if (opts::NeededLibraries)
  242. Dumper->printNeededLibraries();
  243. if (opts::ProgramHeaders)
  244. Dumper->printProgramHeaders();
  245. if (opts::HashTable)
  246. Dumper->printHashTable();
  247. if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
  248. if (opts::ARMAttributes)
  249. Dumper->printAttributes();
  250. if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
  251. if (opts::MipsPLTGOT)
  252. Dumper->printMipsPLTGOT();
  253. if (opts::MipsABIFlags)
  254. Dumper->printMipsABIFlags();
  255. if (opts::MipsReginfo)
  256. Dumper->printMipsReginfo();
  257. }
  258. if (Obj->isCOFF()) {
  259. if (opts::COFFImports)
  260. Dumper->printCOFFImports();
  261. if (opts::COFFExports)
  262. Dumper->printCOFFExports();
  263. if (opts::COFFDirectives)
  264. Dumper->printCOFFDirectives();
  265. if (opts::COFFBaseRelocs)
  266. Dumper->printCOFFBaseReloc();
  267. }
  268. if (opts::PrintStackMap)
  269. Dumper->printStackMap();
  270. }
  271. /// @brief Dumps each object file in \a Arc;
  272. static void dumpArchive(const Archive *Arc) {
  273. for (Archive::child_iterator ArcI = Arc->child_begin(),
  274. ArcE = Arc->child_end();
  275. ArcI != ArcE; ++ArcI) {
  276. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
  277. if (std::error_code EC = ChildOrErr.getError()) {
  278. // Ignore non-object files.
  279. if (EC != object_error::invalid_file_type)
  280. reportError(Arc->getFileName(), EC.message());
  281. continue;
  282. }
  283. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  284. dumpObject(Obj);
  285. else
  286. reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  287. }
  288. }
  289. /// @brief Dumps each object file in \a MachO Universal Binary;
  290. static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
  291. for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
  292. ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
  293. if (ObjOrErr)
  294. dumpObject(&*ObjOrErr.get());
  295. else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
  296. dumpArchive(&*AOrErr.get());
  297. else
  298. reportError(UBinary->getFileName(), ObjOrErr.getError().message());
  299. }
  300. }
  301. /// @brief Opens \a File and dumps it.
  302. static void dumpInput(StringRef File) {
  303. // If file isn't stdin, check that it exists.
  304. if (File != "-" && !sys::fs::exists(File)) {
  305. reportError(File, readobj_error::file_not_found);
  306. return;
  307. }
  308. // Attempt to open the binary.
  309. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
  310. if (std::error_code EC = BinaryOrErr.getError()) {
  311. reportError(File, EC);
  312. return;
  313. }
  314. Binary &Binary = *BinaryOrErr.get().getBinary();
  315. if (Archive *Arc = dyn_cast<Archive>(&Binary))
  316. dumpArchive(Arc);
  317. else if (MachOUniversalBinary *UBinary =
  318. dyn_cast<MachOUniversalBinary>(&Binary))
  319. dumpMachOUniversalBinary(UBinary);
  320. else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  321. dumpObject(Obj);
  322. else
  323. reportError(File, readobj_error::unrecognized_file_format);
  324. }
  325. int main(int argc, const char *argv[]) {
  326. sys::PrintStackTraceOnErrorSignal();
  327. PrettyStackTraceProgram X(argc, argv);
  328. llvm_shutdown_obj Y;
  329. // Register the target printer for --version.
  330. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  331. cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
  332. // Default to stdin if no filename is specified.
  333. if (opts::InputFilenames.size() == 0)
  334. opts::InputFilenames.push_back("-");
  335. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  336. dumpInput);
  337. return 0;
  338. }