llvm-readobj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // -symbols, -t
  84. cl::opt<bool> Symbols("symbols",
  85. cl::desc("Display the symbol table"));
  86. cl::alias SymbolsShort("t",
  87. cl::desc("Alias for --symbols"),
  88. cl::aliasopt(Symbols));
  89. // -dyn-symbols, -dt
  90. cl::opt<bool> DynamicSymbols("dyn-symbols",
  91. cl::desc("Display the dynamic symbol table"));
  92. cl::alias DynamicSymbolsShort("dt",
  93. cl::desc("Alias for --dyn-symbols"),
  94. cl::aliasopt(DynamicSymbols));
  95. // -unwind, -u
  96. cl::opt<bool> UnwindInfo("unwind",
  97. cl::desc("Display unwind information"));
  98. cl::alias UnwindInfoShort("u",
  99. cl::desc("Alias for --unwind"),
  100. cl::aliasopt(UnwindInfo));
  101. // -dynamic-table
  102. cl::opt<bool> DynamicTable("dynamic-table",
  103. cl::desc("Display the ELF .dynamic section table"));
  104. // -needed-libs
  105. cl::opt<bool> NeededLibraries("needed-libs",
  106. cl::desc("Display the needed libraries"));
  107. // -program-headers
  108. cl::opt<bool> ProgramHeaders("program-headers",
  109. cl::desc("Display ELF program headers"));
  110. // -expand-relocs
  111. cl::opt<bool> ExpandRelocs("expand-relocs",
  112. cl::desc("Expand each shown relocation to multiple lines"));
  113. // -codeview
  114. cl::opt<bool> CodeView("codeview",
  115. cl::desc("Display CodeView debug information"));
  116. // -codeview-subsection-bytes
  117. cl::opt<bool> CodeViewSubsectionBytes(
  118. "codeview-subsection-bytes",
  119. cl::desc("Dump raw contents of codeview debug sections and records"));
  120. // -arm-attributes, -a
  121. cl::opt<bool> ARMAttributes("arm-attributes",
  122. cl::desc("Display the ARM attributes section"));
  123. cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
  124. cl::aliasopt(ARMAttributes));
  125. // -mips-plt-got
  126. cl::opt<bool>
  127. MipsPLTGOT("mips-plt-got",
  128. cl::desc("Display the MIPS GOT and PLT GOT sections"));
  129. // -mips-abi-flags
  130. cl::opt<bool> MipsABIFlags("mips-abi-flags",
  131. cl::desc("Display the MIPS.abiflags section"));
  132. // -mips-reginfo
  133. cl::opt<bool> MipsReginfo("mips-reginfo",
  134. cl::desc("Display the MIPS .reginfo section"));
  135. // -coff-imports
  136. cl::opt<bool>
  137. COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
  138. // -coff-exports
  139. cl::opt<bool>
  140. COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
  141. // -coff-directives
  142. cl::opt<bool>
  143. COFFDirectives("coff-directives",
  144. cl::desc("Display the PE/COFF .drectve section"));
  145. // -coff-basereloc
  146. cl::opt<bool>
  147. COFFBaseRelocs("coff-basereloc",
  148. cl::desc("Display the PE/COFF .reloc section"));
  149. } // namespace opts
  150. static int ReturnValue = EXIT_SUCCESS;
  151. namespace llvm {
  152. bool error(std::error_code EC) {
  153. if (!EC)
  154. return false;
  155. ReturnValue = EXIT_FAILURE;
  156. outs() << "\nError reading file: " << EC.message() << ".\n";
  157. outs().flush();
  158. return true;
  159. }
  160. bool relocAddressLess(RelocationRef a, RelocationRef b) {
  161. uint64_t a_addr, b_addr;
  162. if (error(a.getOffset(a_addr))) exit(ReturnValue);
  163. if (error(b.getOffset(b_addr))) exit(ReturnValue);
  164. return a_addr < b_addr;
  165. }
  166. } // namespace llvm
  167. static void reportError(StringRef Input, std::error_code EC) {
  168. if (Input == "-")
  169. Input = "<stdin>";
  170. errs() << Input << ": " << EC.message() << "\n";
  171. errs().flush();
  172. ReturnValue = EXIT_FAILURE;
  173. }
  174. static void reportError(StringRef Input, StringRef Message) {
  175. if (Input == "-")
  176. Input = "<stdin>";
  177. errs() << Input << ": " << Message << "\n";
  178. ReturnValue = EXIT_FAILURE;
  179. }
  180. static bool isMipsArch(unsigned Arch) {
  181. switch (Arch) {
  182. case llvm::Triple::mips:
  183. case llvm::Triple::mipsel:
  184. case llvm::Triple::mips64:
  185. case llvm::Triple::mips64el:
  186. return true;
  187. default:
  188. return false;
  189. }
  190. }
  191. /// @brief Creates an format-specific object file dumper.
  192. static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
  193. std::unique_ptr<ObjDumper> &Result) {
  194. if (!Obj)
  195. return readobj_error::unsupported_file_format;
  196. if (Obj->isCOFF())
  197. return createCOFFDumper(Obj, Writer, Result);
  198. if (Obj->isELF())
  199. return createELFDumper(Obj, Writer, Result);
  200. if (Obj->isMachO())
  201. return createMachODumper(Obj, Writer, Result);
  202. return readobj_error::unsupported_obj_file_format;
  203. }
  204. static StringRef getLoadName(const ObjectFile *Obj) {
  205. if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
  206. return ELF->getLoadName();
  207. if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
  208. return ELF->getLoadName();
  209. if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
  210. return ELF->getLoadName();
  211. if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
  212. return ELF->getLoadName();
  213. llvm_unreachable("Not ELF");
  214. }
  215. /// @brief Dumps the specified object file.
  216. static void dumpObject(const ObjectFile *Obj) {
  217. StreamWriter Writer(outs());
  218. std::unique_ptr<ObjDumper> Dumper;
  219. if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
  220. reportError(Obj->getFileName(), EC);
  221. return;
  222. }
  223. outs() << '\n';
  224. outs() << "File: " << Obj->getFileName() << "\n";
  225. outs() << "Format: " << Obj->getFileFormatName() << "\n";
  226. outs() << "Arch: "
  227. << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
  228. << "\n";
  229. outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
  230. if (Obj->isELF())
  231. outs() << "LoadName: " << getLoadName(Obj) << "\n";
  232. if (opts::FileHeaders)
  233. Dumper->printFileHeaders();
  234. if (opts::Sections)
  235. Dumper->printSections();
  236. if (opts::Relocations)
  237. Dumper->printRelocations();
  238. if (opts::Symbols)
  239. Dumper->printSymbols();
  240. if (opts::DynamicSymbols)
  241. Dumper->printDynamicSymbols();
  242. if (opts::UnwindInfo)
  243. Dumper->printUnwindInfo();
  244. if (opts::DynamicTable)
  245. Dumper->printDynamicTable();
  246. if (opts::NeededLibraries)
  247. Dumper->printNeededLibraries();
  248. if (opts::ProgramHeaders)
  249. Dumper->printProgramHeaders();
  250. if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
  251. if (opts::ARMAttributes)
  252. Dumper->printAttributes();
  253. if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
  254. if (opts::MipsPLTGOT)
  255. Dumper->printMipsPLTGOT();
  256. if (opts::MipsABIFlags)
  257. Dumper->printMipsABIFlags();
  258. if (opts::MipsReginfo)
  259. Dumper->printMipsReginfo();
  260. }
  261. if (opts::COFFImports)
  262. Dumper->printCOFFImports();
  263. if (opts::COFFExports)
  264. Dumper->printCOFFExports();
  265. if (opts::COFFDirectives)
  266. Dumper->printCOFFDirectives();
  267. if (opts::COFFBaseRelocs)
  268. Dumper->printCOFFBaseReloc();
  269. }
  270. /// @brief Dumps each object file in \a Arc;
  271. static void dumpArchive(const Archive *Arc) {
  272. for (Archive::child_iterator ArcI = Arc->child_begin(),
  273. ArcE = Arc->child_end();
  274. ArcI != ArcE; ++ArcI) {
  275. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
  276. if (std::error_code EC = ChildOrErr.getError()) {
  277. // Ignore non-object files.
  278. if (EC != object_error::invalid_file_type)
  279. reportError(Arc->getFileName(), EC.message());
  280. continue;
  281. }
  282. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  283. dumpObject(Obj);
  284. else
  285. reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  286. }
  287. }
  288. /// @brief Dumps each object file in \a MachO Universal Binary;
  289. static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
  290. for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
  291. ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
  292. if (ObjOrErr)
  293. dumpObject(&*ObjOrErr.get());
  294. else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
  295. dumpArchive(&*AOrErr.get());
  296. else
  297. reportError(UBinary->getFileName(), ObjOrErr.getError().message());
  298. }
  299. }
  300. /// @brief Opens \a File and dumps it.
  301. static void dumpInput(StringRef File) {
  302. // If file isn't stdin, check that it exists.
  303. if (File != "-" && !sys::fs::exists(File)) {
  304. reportError(File, readobj_error::file_not_found);
  305. return;
  306. }
  307. // Attempt to open the binary.
  308. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
  309. if (std::error_code EC = BinaryOrErr.getError()) {
  310. reportError(File, EC);
  311. return;
  312. }
  313. Binary &Binary = *BinaryOrErr.get().getBinary();
  314. if (Archive *Arc = dyn_cast<Archive>(&Binary))
  315. dumpArchive(Arc);
  316. else if (MachOUniversalBinary *UBinary =
  317. dyn_cast<MachOUniversalBinary>(&Binary))
  318. dumpMachOUniversalBinary(UBinary);
  319. else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  320. dumpObject(Obj);
  321. else
  322. reportError(File, readobj_error::unrecognized_file_format);
  323. }
  324. int main(int argc, const char *argv[]) {
  325. sys::PrintStackTraceOnErrorSignal();
  326. PrettyStackTraceProgram X(argc, argv);
  327. llvm_shutdown_obj Y;
  328. // Initialize targets.
  329. llvm::InitializeAllTargetInfos();
  330. // Register the target printer for --version.
  331. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  332. cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
  333. // Default to stdin if no filename is specified.
  334. if (opts::InputFilenames.size() == 0)
  335. opts::InputFilenames.push_back("-");
  336. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  337. dumpInput);
  338. return ReturnValue;
  339. }