PageRenderTime 1894ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/llvm-readobj/llvm-readobj.cpp

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