llvm-readobj.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/ObjectFile.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/DataTypes.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/FileSystem.h"
  33. #include "llvm/Support/ManagedStatic.h"
  34. #include "llvm/Support/PrettyStackTrace.h"
  35. #include "llvm/Support/Signals.h"
  36. #include "llvm/Support/TargetRegistry.h"
  37. #include "llvm/Support/TargetSelect.h"
  38. #include <string>
  39. #include <system_error>
  40. using namespace llvm;
  41. using namespace llvm::object;
  42. namespace opts {
  43. cl::list<std::string> InputFilenames(cl::Positional,
  44. cl::desc("<input object files>"),
  45. cl::ZeroOrMore);
  46. // -file-headers, -h
  47. cl::opt<bool> FileHeaders("file-headers",
  48. cl::desc("Display file headers "));
  49. cl::alias FileHeadersShort("h",
  50. cl::desc("Alias for --file-headers"),
  51. cl::aliasopt(FileHeaders));
  52. // -sections, -s
  53. cl::opt<bool> Sections("sections",
  54. cl::desc("Display all sections."));
  55. cl::alias SectionsShort("s",
  56. cl::desc("Alias for --sections"),
  57. cl::aliasopt(Sections));
  58. // -section-relocations, -sr
  59. cl::opt<bool> SectionRelocations("section-relocations",
  60. cl::desc("Display relocations for each section shown."));
  61. cl::alias SectionRelocationsShort("sr",
  62. cl::desc("Alias for --section-relocations"),
  63. cl::aliasopt(SectionRelocations));
  64. // -section-symbols, -st
  65. cl::opt<bool> SectionSymbols("section-symbols",
  66. cl::desc("Display symbols for each section shown."));
  67. cl::alias SectionSymbolsShort("st",
  68. cl::desc("Alias for --section-symbols"),
  69. cl::aliasopt(SectionSymbols));
  70. // -section-data, -sd
  71. cl::opt<bool> SectionData("section-data",
  72. cl::desc("Display section data for each section shown."));
  73. cl::alias SectionDataShort("sd",
  74. cl::desc("Alias for --section-data"),
  75. cl::aliasopt(SectionData));
  76. // -relocations, -r
  77. cl::opt<bool> Relocations("relocations",
  78. cl::desc("Display the relocation entries in the file"));
  79. cl::alias RelocationsShort("r",
  80. cl::desc("Alias for --relocations"),
  81. cl::aliasopt(Relocations));
  82. // -symbols, -t
  83. cl::opt<bool> Symbols("symbols",
  84. cl::desc("Display the symbol table"));
  85. cl::alias SymbolsShort("t",
  86. cl::desc("Alias for --symbols"),
  87. cl::aliasopt(Symbols));
  88. // -dyn-symbols, -dt
  89. cl::opt<bool> DynamicSymbols("dyn-symbols",
  90. cl::desc("Display the dynamic symbol table"));
  91. cl::alias DynamicSymbolsShort("dt",
  92. cl::desc("Alias for --dyn-symbols"),
  93. cl::aliasopt(DynamicSymbols));
  94. // -unwind, -u
  95. cl::opt<bool> UnwindInfo("unwind",
  96. cl::desc("Display unwind information"));
  97. cl::alias UnwindInfoShort("u",
  98. cl::desc("Alias for --unwind"),
  99. cl::aliasopt(UnwindInfo));
  100. // -dynamic-table
  101. cl::opt<bool> DynamicTable("dynamic-table",
  102. cl::desc("Display the ELF .dynamic section table"));
  103. // -needed-libs
  104. cl::opt<bool> NeededLibraries("needed-libs",
  105. cl::desc("Display the needed libraries"));
  106. // -program-headers
  107. cl::opt<bool> ProgramHeaders("program-headers",
  108. cl::desc("Display ELF program headers"));
  109. // -expand-relocs
  110. cl::opt<bool> ExpandRelocs("expand-relocs",
  111. cl::desc("Expand each shown relocation to multiple lines"));
  112. // -codeview-linetables
  113. cl::opt<bool> CodeViewLineTables("codeview-linetables",
  114. cl::desc("Display CodeView line table information"));
  115. // -arm-attributes, -a
  116. cl::opt<bool> ARMAttributes("arm-attributes",
  117. cl::desc("Display the ARM attributes section"));
  118. cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
  119. cl::aliasopt(ARMAttributes));
  120. // -mips-plt-got
  121. cl::opt<bool>
  122. MipsPLTGOT("mips-plt-got",
  123. cl::desc("Display the MIPS GOT and PLT GOT sections"));
  124. // -coff-imports
  125. cl::opt<bool>
  126. COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
  127. // -coff-exports
  128. cl::opt<bool>
  129. COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
  130. // -coff-directives
  131. cl::opt<bool>
  132. COFFDirectives("coff-directives",
  133. cl::desc("Display the PE/COFF .drectve section"));
  134. // -coff-basereloc
  135. cl::opt<bool>
  136. COFFBaseRelocs("coff-basereloc",
  137. cl::desc("Display the PE/COFF .reloc section"));
  138. } // namespace opts
  139. static int ReturnValue = EXIT_SUCCESS;
  140. namespace llvm {
  141. bool error(std::error_code EC) {
  142. if (!EC)
  143. return false;
  144. ReturnValue = EXIT_FAILURE;
  145. outs() << "\nError reading file: " << EC.message() << ".\n";
  146. outs().flush();
  147. return true;
  148. }
  149. bool relocAddressLess(RelocationRef a, RelocationRef b) {
  150. uint64_t a_addr, b_addr;
  151. if (error(a.getOffset(a_addr))) exit(ReturnValue);
  152. if (error(b.getOffset(b_addr))) exit(ReturnValue);
  153. return a_addr < b_addr;
  154. }
  155. } // namespace llvm
  156. static void reportError(StringRef Input, std::error_code EC) {
  157. if (Input == "-")
  158. Input = "<stdin>";
  159. errs() << Input << ": " << EC.message() << "\n";
  160. errs().flush();
  161. ReturnValue = EXIT_FAILURE;
  162. }
  163. static void reportError(StringRef Input, StringRef Message) {
  164. if (Input == "-")
  165. Input = "<stdin>";
  166. errs() << Input << ": " << Message << "\n";
  167. ReturnValue = EXIT_FAILURE;
  168. }
  169. static bool isMipsArch(unsigned Arch) {
  170. switch (Arch) {
  171. case llvm::Triple::mips:
  172. case llvm::Triple::mipsel:
  173. case llvm::Triple::mips64:
  174. case llvm::Triple::mips64el:
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. /// @brief Creates an format-specific object file dumper.
  181. static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
  182. std::unique_ptr<ObjDumper> &Result) {
  183. if (!Obj)
  184. return readobj_error::unsupported_file_format;
  185. if (Obj->isCOFF())
  186. return createCOFFDumper(Obj, Writer, Result);
  187. if (Obj->isELF())
  188. return createELFDumper(Obj, Writer, Result);
  189. if (Obj->isMachO())
  190. return createMachODumper(Obj, Writer, Result);
  191. return readobj_error::unsupported_obj_file_format;
  192. }
  193. static StringRef getLoadName(const ObjectFile *Obj) {
  194. if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
  195. return ELF->getLoadName();
  196. if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
  197. return ELF->getLoadName();
  198. if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
  199. return ELF->getLoadName();
  200. if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
  201. return ELF->getLoadName();
  202. llvm_unreachable("Not ELF");
  203. }
  204. /// @brief Dumps the specified object file.
  205. static void dumpObject(const ObjectFile *Obj) {
  206. StreamWriter Writer(outs());
  207. std::unique_ptr<ObjDumper> Dumper;
  208. if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
  209. reportError(Obj->getFileName(), EC);
  210. return;
  211. }
  212. outs() << '\n';
  213. outs() << "File: " << Obj->getFileName() << "\n";
  214. outs() << "Format: " << Obj->getFileFormatName() << "\n";
  215. outs() << "Arch: "
  216. << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
  217. << "\n";
  218. outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
  219. if (Obj->isELF())
  220. outs() << "LoadName: " << getLoadName(Obj) << "\n";
  221. if (opts::FileHeaders)
  222. Dumper->printFileHeaders();
  223. if (opts::Sections)
  224. Dumper->printSections();
  225. if (opts::Relocations)
  226. Dumper->printRelocations();
  227. if (opts::Symbols)
  228. Dumper->printSymbols();
  229. if (opts::DynamicSymbols)
  230. Dumper->printDynamicSymbols();
  231. if (opts::UnwindInfo)
  232. Dumper->printUnwindInfo();
  233. if (opts::DynamicTable)
  234. Dumper->printDynamicTable();
  235. if (opts::NeededLibraries)
  236. Dumper->printNeededLibraries();
  237. if (opts::ProgramHeaders)
  238. Dumper->printProgramHeaders();
  239. if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
  240. if (opts::ARMAttributes)
  241. Dumper->printAttributes();
  242. if (isMipsArch(Obj->getArch()) && Obj->isELF())
  243. if (opts::MipsPLTGOT)
  244. Dumper->printMipsPLTGOT();
  245. if (opts::COFFImports)
  246. Dumper->printCOFFImports();
  247. if (opts::COFFExports)
  248. Dumper->printCOFFExports();
  249. if (opts::COFFDirectives)
  250. Dumper->printCOFFDirectives();
  251. if (opts::COFFBaseRelocs)
  252. Dumper->printCOFFBaseReloc();
  253. }
  254. /// @brief Dumps each object file in \a Arc;
  255. static void dumpArchive(const Archive *Arc) {
  256. for (Archive::child_iterator ArcI = Arc->child_begin(),
  257. ArcE = Arc->child_end();
  258. ArcI != ArcE; ++ArcI) {
  259. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
  260. if (std::error_code EC = ChildOrErr.getError()) {
  261. // Ignore non-object files.
  262. if (EC != object_error::invalid_file_type)
  263. reportError(Arc->getFileName(), EC.message());
  264. continue;
  265. }
  266. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  267. dumpObject(Obj);
  268. else
  269. reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  270. }
  271. }
  272. /// @brief Opens \a File and dumps it.
  273. static void dumpInput(StringRef File) {
  274. // If file isn't stdin, check that it exists.
  275. if (File != "-" && !sys::fs::exists(File)) {
  276. reportError(File, readobj_error::file_not_found);
  277. return;
  278. }
  279. // Attempt to open the binary.
  280. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
  281. if (std::error_code EC = BinaryOrErr.getError()) {
  282. reportError(File, EC);
  283. return;
  284. }
  285. Binary &Binary = *BinaryOrErr.get().getBinary();
  286. if (Archive *Arc = dyn_cast<Archive>(&Binary))
  287. dumpArchive(Arc);
  288. else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  289. dumpObject(Obj);
  290. else
  291. reportError(File, readobj_error::unrecognized_file_format);
  292. }
  293. int main(int argc, const char *argv[]) {
  294. sys::PrintStackTraceOnErrorSignal();
  295. PrettyStackTraceProgram X(argc, argv);
  296. llvm_shutdown_obj Y;
  297. // Initialize targets.
  298. llvm::InitializeAllTargetInfos();
  299. // Register the target printer for --version.
  300. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  301. cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
  302. // Default to stdin if no filename is specified.
  303. if (opts::InputFilenames.size() == 0)
  304. opts::InputFilenames.push_back("-");
  305. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  306. dumpInput);
  307. return ReturnValue;
  308. }