2
0

llvm-readobj.cpp 13 KB

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