llvm-readobj.cpp revision 76e70f340c09ba759ad96d8dfe416b64f24bc287
1ee37b6e4cd53b8e2412c522eeab4e39b434f00c6David Meyer//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
25c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//
35c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//                     The LLVM Compiler Infrastructure
45c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//
55c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer// This file is distributed under the University of Illinois Open Source
65c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer// License. See LICENSE.TXT for details.
75c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//
85c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//===----------------------------------------------------------------------===//
95c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer//
10dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// This is a tool similar to readelf, except it works on multiple object file
11dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// formats. The main purpose of this tool is to provide detailed output suitable
12dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// for FileCheck.
13ee37b6e4cd53b8e2412c522eeab4e39b434f00c6David Meyer//
14dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// Flags should be similar to readelf where supported, but the output format
15dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// does not need to be identical. The point is to not make users learn yet
16dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// another set of flags.
17ee37b6e4cd53b8e2412c522eeab4e39b434f00c6David Meyer//
18dd3aa9eab2b40ea1573a5482472bbb89e6f1038dMichael J. Spencer// Output should be specialized for each format where appropriate.
19ee37b6e4cd53b8e2412c522eeab4e39b434f00c6David Meyer//
20ee37b6e4cd53b8e2412c522eeab4e39b434f00c6David Meyer//===----------------------------------------------------------------------===//
215c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
22d326d05fb9c794e93fc7fc0601028f196600f7e2Michael J. Spencer#include "llvm-readobj.h"
23d326d05fb9c794e93fc7fc0601028f196600f7e2Michael J. Spencer
2476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "Error.h"
2576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "ObjDumper.h"
2676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "StreamWriter.h"
2776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
2876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Object/Archive.h"
29f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Object/ObjectFile.h"
3076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/Casting.h"
315c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer#include "llvm/Support/CommandLine.h"
3276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/DataTypes.h"
335c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer#include "llvm/Support/Debug.h"
3476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/FileSystem.h"
3576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/ManagedStatic.h"
36f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/PrettyStackTrace.h"
37f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/Signals.h"
3876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/TargetRegistry.h"
3976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/TargetSelect.h"
4076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/system_error.h"
4176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
4276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include <string>
4376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
445c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
455c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyerusing namespace llvm;
465c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyerusing namespace llvm::object;
475c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
4876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christophernamespace opts {
4976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::list<std::string> InputFilenames(cl::Positional,
5076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("<input object files>"),
5176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::ZeroOrMore);
5276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
5376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -file-headers, -h
5476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> FileHeaders("file-headers",
5576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display file headers "));
5676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias FileHeadersShort("h",
5776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --file-headers"),
5876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(FileHeaders));
5976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
6076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -sections, -s
6176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> Sections("sections",
6276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display all sections."));
6376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias SectionsShort("s",
6476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --sections"),
6576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(Sections));
6676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
6776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -section-relocations, -sr
6876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> SectionRelocations("section-relocations",
6976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display relocations for each section shown."));
7076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias SectionRelocationsShort("sr",
7176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --section-relocations"),
7276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(SectionRelocations));
7376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
7476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -section-symbols, -st
7576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> SectionSymbols("section-symbols",
7676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display symbols for each section shown."));
7776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias SectionSymbolsShort("st",
7876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --section-symbols"),
7976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(SectionSymbols));
8076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
8176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -section-data, -sd
8276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> SectionData("section-data",
8376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display section data for each section shown."));
8476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias SectionDataShort("sd",
8576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --section-data"),
8676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(SectionData));
8776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
8876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -relocations, -r
8976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> Relocations("relocations",
9076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display the relocation entries in the file"));
9176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias RelocationsShort("r",
9276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --relocations"),
9376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(Relocations));
9476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
9576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -symbols, -t
9676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> Symbols("symbols",
9776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display the symbol table"));
9876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias SymbolsShort("t",
9976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --symbols"),
10076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(Symbols));
10176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
10276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -dyn-symbols, -dt
10376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> DynamicSymbols("dyn-symbols",
10476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display the dynamic symbol table"));
10576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias DynamicSymbolsShort("dt",
10676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --dyn-symbols"),
10776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(DynamicSymbols));
10876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
10976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -unwind, -u
11076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> UnwindInfo("unwind",
11176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display unwind information"));
11276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::alias UnwindInfoShort("u",
11376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Alias for --unwind"),
11476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::aliasopt(UnwindInfo));
11576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
11676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -dynamic-table
11776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> DynamicTable("dynamic-table",
11876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display the ELF .dynamic section table"));
11976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
12076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // -needed-libs
12176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::opt<bool> NeededLibraries("needed-libs",
12276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    cl::desc("Display the needed libraries"));
12376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher} // namespace opts
12476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
12576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christophernamespace llvm {
12676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
12776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherbool error(error_code EC) {
12876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (!EC)
12976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return false;
13076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
13176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << "\nError reading file: " << EC.message() << ".\n";
13276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs().flush();
13376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  return true;
134148ee4f224be6448834bf039807c70bb1a7c78f5Rafael Espindola}
135148ee4f224be6448834bf039807c70bb1a7c78f5Rafael Espindola
13676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherbool relocAddressLess(RelocationRef a, RelocationRef b) {
13776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  uint64_t a_addr, b_addr;
13876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (error(a.getAddress(a_addr))) return false;
13976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (error(b.getAddress(b_addr))) return false;
14076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  return a_addr < b_addr;
141fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola}
142fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
14376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher} // namespace llvm
1445c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
145ad784790ad28023ef5041beac6c23a8250778f3fRafael Espindola
14676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic void reportError(StringRef Input, error_code EC) {
14776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Input == "-")
14876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Input = "<stdin>";
149fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
15076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  errs() << Input << ": " << EC.message() << "\n";
15176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  errs().flush();
152fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola}
153fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
15476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic void reportError(StringRef Input, StringRef Message) {
15576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Input == "-")
15676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Input = "<stdin>";
1575c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
15876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  errs() << Input << ": " << Message << "\n";
1595c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer}
1605c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
16176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher/// @brief Creates an format-specific object file dumper.
16276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic error_code createDumper(const ObjectFile *Obj,
16376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher                               StreamWriter &Writer,
16476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher                               OwningPtr<ObjDumper> &Result) {
16576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (!Obj)
16676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return readobj_error::unsupported_file_format;
16776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
16876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Obj->isCOFF())
16976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return createCOFFDumper(Obj, Writer, Result);
17076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Obj->isELF())
17176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return createELFDumper(Obj, Writer, Result);
17276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Obj->isMachO())
17376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return createMachODumper(Obj, Writer, Result);
17476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
17576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  return readobj_error::unsupported_obj_file_format;
1765c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer}
1775c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
178fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
17976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher/// @brief Dumps the specified object file.
18076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic void dumpObject(const ObjectFile *Obj) {
18176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  StreamWriter Writer(outs());
18276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  OwningPtr<ObjDumper> Dumper;
18376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (error_code EC = createDumper(Obj, Writer, Dumper)) {
18476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    reportError(Obj->getFileName(), EC);
18576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return;
186fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola  }
187fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
18876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << '\n';
18976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << "File: " << Obj->getFileName() << "\n";
19076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << "Format: " << Obj->getFileFormatName() << "\n";
19176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << "Arch: "
19276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher         << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
19397f7787bfb56ad31fe20ec0bb9c3c9f3253d14fbDavid Meyer         << "\n";
19476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
19576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Obj->isELF())
19676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    outs() << "LoadName: " << Obj->getLoadName() << "\n";
19776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
19876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::FileHeaders)
19976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printFileHeaders();
20076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::Sections)
20176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printSections();
20276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::Relocations)
20376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printRelocations();
20476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::Symbols)
20576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printSymbols();
20676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::DynamicSymbols)
20776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printDynamicSymbols();
20876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::UnwindInfo)
20976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printUnwindInfo();
21076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::DynamicTable)
21176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printDynamicTable();
21276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::NeededLibraries)
21376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    Dumper->printNeededLibraries();
21497f7787bfb56ad31fe20ec0bb9c3c9f3253d14fbDavid Meyer}
21597f7787bfb56ad31fe20ec0bb9c3c9f3253d14fbDavid Meyer
2165c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
21776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher/// @brief Dumps each object file in \a Arc;
21876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic void dumpArchive(const Archive *Arc) {
21976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  for (Archive::child_iterator ArcI = Arc->begin_children(),
22076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher                               ArcE = Arc->end_children();
22176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher                               ArcI != ArcE; ++ArcI) {
22276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    OwningPtr<Binary> child;
22376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    if (error_code EC = ArcI->getAsBinary(child)) {
22476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher      // Ignore non-object files.
22576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher      if (EC != object_error::invalid_file_type)
22676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher        reportError(Arc->getFileName(), EC.message());
22776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher      continue;
22876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    }
2295c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
23076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    if (ObjectFile *Obj = dyn_cast<ObjectFile>(child.get()))
23176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher      dumpObject(Obj);
23276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    else
23376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher      reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
2345c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer  }
23576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher}
23676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
2375c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
23876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher/// @brief Opens \a File and dumps it.
23976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherstatic void dumpInput(StringRef File) {
24076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // If file isn't stdin, check that it exists.
24176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (File != "-" && !sys::fs::exists(File)) {
24276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    reportError(File, readobj_error::file_not_found);
24376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return;
2445c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer  }
2455c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
24676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // Attempt to open the binary.
24776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  OwningPtr<Binary> Binary;
24876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (error_code EC = createBinary(File, Binary)) {
24976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    reportError(File, EC);
25076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return;
2515c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer  }
2525c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer
25376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
25476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    dumpArchive(Arc);
25576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
25676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    dumpObject(Obj);
25776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  else
25876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    reportError(File, readobj_error::unrecognized_file_format);
25976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher}
26087b47ccc5afcdffb1fa7f04b27fca926ec7fb344Rafael Espindola
26187b47ccc5afcdffb1fa7f04b27fca926ec7fb344Rafael Espindola
26276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherint main(int argc, const char *argv[]) {
26376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  sys::PrintStackTraceOnErrorSignal();
26476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  PrettyStackTraceProgram X(argc, argv);
26576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  llvm_shutdown_obj Y;
266fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
26776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // Initialize targets.
26876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  llvm::InitializeAllTargetInfos();
269fc73847d8fa79d310b26b7a80275dc48755ec2e3Rafael Espindola
27076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // Register the target printer for --version.
27176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
27276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
27376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
274d326d05fb9c794e93fc7fc0601028f196600f7e2Michael J. Spencer
27576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  // Default to stdin if no filename is specified.
27676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  if (opts::InputFilenames.size() == 0)
27776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    opts::InputFilenames.push_back("-");
27876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
27976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
28076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher                dumpInput);
28187b47ccc5afcdffb1fa7f04b27fca926ec7fb344Rafael Espindola
2825c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer  return 0;
2835c2b4ea73c8f48bb5f96c86fe437385b8fb3dcdaDavid Meyer}
284