2013-08-09 18:31:14 +08:00
|
|
|
//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
|
2012-03-01 09:36:50 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-02-06 04:27:22 +08:00
|
|
|
// This is a tool similar to readelf, except it works on multiple object file
|
|
|
|
// formats. The main purpose of this tool is to provide detailed output suitable
|
|
|
|
// for FileCheck.
|
2012-03-03 07:43:51 +08:00
|
|
|
//
|
2013-02-06 04:27:22 +08:00
|
|
|
// Flags should be similar to readelf where supported, but the output format
|
|
|
|
// does not need to be identical. The point is to not make users learn yet
|
|
|
|
// another set of flags.
|
2012-03-03 07:43:51 +08:00
|
|
|
//
|
2013-02-06 04:27:22 +08:00
|
|
|
// Output should be specialized for each format where appropriate.
|
2012-03-03 07:43:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-03-01 09:36:50 +08:00
|
|
|
|
2013-02-20 10:37:12 +08:00
|
|
|
#include "llvm-readobj.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "Error.h"
|
|
|
|
#include "ObjDumper.h"
|
2016-11-09 06:24:53 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-08-28 15:40:30 +08:00
|
|
|
#include "llvm/Object/COFFImportFile.h"
|
2014-08-09 00:39:22 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2015-03-25 04:26:55 +08:00
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2012-03-01 09:36:50 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-03-01 09:36:50 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2016-05-03 08:28:04 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include <string>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2012-03-01 09:36:50 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
namespace opts {
|
|
|
|
cl::list<std::string> InputFilenames(cl::Positional,
|
|
|
|
cl::desc("<input object files>"),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
|
|
|
// -file-headers, -h
|
|
|
|
cl::opt<bool> FileHeaders("file-headers",
|
|
|
|
cl::desc("Display file headers "));
|
|
|
|
cl::alias FileHeadersShort("h",
|
|
|
|
cl::desc("Alias for --file-headers"),
|
|
|
|
cl::aliasopt(FileHeaders));
|
|
|
|
|
2017-07-19 07:35:22 +08:00
|
|
|
// -sections, -s, -S
|
|
|
|
// Note: In GNU readelf, -s means --symbols!
|
2013-04-04 02:31:38 +08:00
|
|
|
cl::opt<bool> Sections("sections",
|
|
|
|
cl::desc("Display all sections."));
|
|
|
|
cl::alias SectionsShort("s",
|
|
|
|
cl::desc("Alias for --sections"),
|
|
|
|
cl::aliasopt(Sections));
|
2017-07-19 07:35:22 +08:00
|
|
|
cl::alias SectionsShortUpper("S",
|
|
|
|
cl::desc("Alias for --sections"),
|
|
|
|
cl::aliasopt(Sections));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
// -section-relocations, -sr
|
|
|
|
cl::opt<bool> SectionRelocations("section-relocations",
|
|
|
|
cl::desc("Display relocations for each section shown."));
|
|
|
|
cl::alias SectionRelocationsShort("sr",
|
|
|
|
cl::desc("Alias for --section-relocations"),
|
|
|
|
cl::aliasopt(SectionRelocations));
|
|
|
|
|
|
|
|
// -section-symbols, -st
|
|
|
|
cl::opt<bool> SectionSymbols("section-symbols",
|
|
|
|
cl::desc("Display symbols for each section shown."));
|
|
|
|
cl::alias SectionSymbolsShort("st",
|
|
|
|
cl::desc("Alias for --section-symbols"),
|
|
|
|
cl::aliasopt(SectionSymbols));
|
|
|
|
|
|
|
|
// -section-data, -sd
|
|
|
|
cl::opt<bool> SectionData("section-data",
|
|
|
|
cl::desc("Display section data for each section shown."));
|
|
|
|
cl::alias SectionDataShort("sd",
|
|
|
|
cl::desc("Alias for --section-data"),
|
|
|
|
cl::aliasopt(SectionData));
|
|
|
|
|
|
|
|
// -relocations, -r
|
|
|
|
cl::opt<bool> Relocations("relocations",
|
|
|
|
cl::desc("Display the relocation entries in the file"));
|
|
|
|
cl::alias RelocationsShort("r",
|
|
|
|
cl::desc("Alias for --relocations"),
|
|
|
|
cl::aliasopt(Relocations));
|
|
|
|
|
2016-08-31 02:52:02 +08:00
|
|
|
// -notes, -n
|
|
|
|
cl::opt<bool> Notes("notes", cl::desc("Display the ELF notes in the file"));
|
|
|
|
cl::alias NotesShort("n", cl::desc("Alias for --notes"), cl::aliasopt(Notes));
|
|
|
|
|
2015-06-26 05:47:32 +08:00
|
|
|
// -dyn-relocations
|
|
|
|
cl::opt<bool> DynRelocs("dyn-relocations",
|
|
|
|
cl::desc("Display the dynamic relocation entries in the file"));
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
// -symbols, -t
|
|
|
|
cl::opt<bool> Symbols("symbols",
|
|
|
|
cl::desc("Display the symbol table"));
|
|
|
|
cl::alias SymbolsShort("t",
|
|
|
|
cl::desc("Alias for --symbols"),
|
|
|
|
cl::aliasopt(Symbols));
|
|
|
|
|
|
|
|
// -dyn-symbols, -dt
|
|
|
|
cl::opt<bool> DynamicSymbols("dyn-symbols",
|
|
|
|
cl::desc("Display the dynamic symbol table"));
|
|
|
|
cl::alias DynamicSymbolsShort("dt",
|
|
|
|
cl::desc("Alias for --dyn-symbols"),
|
|
|
|
cl::aliasopt(DynamicSymbols));
|
|
|
|
|
|
|
|
// -unwind, -u
|
|
|
|
cl::opt<bool> UnwindInfo("unwind",
|
|
|
|
cl::desc("Display unwind information"));
|
|
|
|
cl::alias UnwindInfoShort("u",
|
|
|
|
cl::desc("Alias for --unwind"),
|
|
|
|
cl::aliasopt(UnwindInfo));
|
|
|
|
|
|
|
|
// -dynamic-table
|
|
|
|
cl::opt<bool> DynamicTable("dynamic-table",
|
|
|
|
cl::desc("Display the ELF .dynamic section table"));
|
2016-07-20 09:16:28 +08:00
|
|
|
cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
|
|
|
|
cl::aliasopt(DynamicTable));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
// -needed-libs
|
|
|
|
cl::opt<bool> NeededLibraries("needed-libs",
|
|
|
|
cl::desc("Display the needed libraries"));
|
2013-04-12 12:01:52 +08:00
|
|
|
|
2013-04-12 12:07:39 +08:00
|
|
|
// -program-headers
|
|
|
|
cl::opt<bool> ProgramHeaders("program-headers",
|
|
|
|
cl::desc("Display ELF program headers"));
|
2016-07-20 09:16:28 +08:00
|
|
|
cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
|
|
|
|
cl::aliasopt(ProgramHeaders));
|
2013-04-12 12:07:39 +08:00
|
|
|
|
2015-07-10 06:32:24 +08:00
|
|
|
// -hash-table
|
|
|
|
cl::opt<bool> HashTable("hash-table",
|
|
|
|
cl::desc("Display ELF hash table"));
|
|
|
|
|
2015-10-14 20:11:50 +08:00
|
|
|
// -gnu-hash-table
|
|
|
|
cl::opt<bool> GnuHashTable("gnu-hash-table",
|
|
|
|
cl::desc("Display ELF .gnu.hash section"));
|
|
|
|
|
2013-04-12 12:01:52 +08:00
|
|
|
// -expand-relocs
|
|
|
|
cl::opt<bool> ExpandRelocs("expand-relocs",
|
|
|
|
cl::desc("Expand each shown relocation to multiple lines"));
|
2013-12-19 19:37:14 +08:00
|
|
|
|
2015-02-19 03:32:05 +08:00
|
|
|
// -codeview
|
|
|
|
cl::opt<bool> CodeView("codeview",
|
|
|
|
cl::desc("Display CodeView debug information"));
|
|
|
|
|
2016-05-14 08:02:53 +08:00
|
|
|
// -codeview-merged-types
|
|
|
|
cl::opt<bool>
|
|
|
|
CodeViewMergedTypes("codeview-merged-types",
|
|
|
|
cl::desc("Display the merged CodeView type stream"));
|
|
|
|
|
2015-02-19 03:32:05 +08:00
|
|
|
// -codeview-subsection-bytes
|
|
|
|
cl::opt<bool> CodeViewSubsectionBytes(
|
|
|
|
"codeview-subsection-bytes",
|
|
|
|
cl::desc("Dump raw contents of codeview debug sections and records"));
|
2014-01-30 12:46:33 +08:00
|
|
|
|
|
|
|
// -arm-attributes, -a
|
|
|
|
cl::opt<bool> ARMAttributes("arm-attributes",
|
|
|
|
cl::desc("Display the ARM attributes section"));
|
2016-12-24 07:54:52 +08:00
|
|
|
cl::alias ARMAttributesShort("a", cl::desc("Alias for --arm-attributes"),
|
2014-01-30 12:46:33 +08:00
|
|
|
cl::aliasopt(ARMAttributes));
|
2014-06-18 16:47:09 +08:00
|
|
|
|
|
|
|
// -mips-plt-got
|
|
|
|
cl::opt<bool>
|
|
|
|
MipsPLTGOT("mips-plt-got",
|
|
|
|
cl::desc("Display the MIPS GOT and PLT GOT sections"));
|
2014-10-03 01:02:18 +08:00
|
|
|
|
2015-05-07 23:40:35 +08:00
|
|
|
// -mips-abi-flags
|
|
|
|
cl::opt<bool> MipsABIFlags("mips-abi-flags",
|
|
|
|
cl::desc("Display the MIPS.abiflags section"));
|
|
|
|
|
2015-06-17 05:47:43 +08:00
|
|
|
// -mips-reginfo
|
|
|
|
cl::opt<bool> MipsReginfo("mips-reginfo",
|
|
|
|
cl::desc("Display the MIPS .reginfo section"));
|
|
|
|
|
2016-05-04 13:58:57 +08:00
|
|
|
// -mips-options
|
|
|
|
cl::opt<bool> MipsOptions("mips-options",
|
|
|
|
cl::desc("Display the MIPS .MIPS.options section"));
|
|
|
|
|
2017-03-23 06:32:22 +08:00
|
|
|
// -amdgpu-code-object-metadata
|
|
|
|
cl::opt<bool> AMDGPUCodeObjectMetadata(
|
|
|
|
"amdgpu-code-object-metadata",
|
|
|
|
cl::desc("Display AMDGPU code object metadata"));
|
2016-12-15 01:16:52 +08:00
|
|
|
|
2014-10-03 01:02:18 +08:00
|
|
|
// -coff-imports
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
|
2014-10-08 03:37:52 +08:00
|
|
|
|
2015-01-04 05:35:09 +08:00
|
|
|
// -coff-exports
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
|
|
|
|
|
2014-10-08 03:37:52 +08:00
|
|
|
// -coff-directives
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFDirectives("coff-directives",
|
2014-11-19 08:18:07 +08:00
|
|
|
cl::desc("Display the PE/COFF .drectve section"));
|
|
|
|
|
|
|
|
// -coff-basereloc
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFBaseRelocs("coff-basereloc",
|
|
|
|
cl::desc("Display the PE/COFF .reloc section"));
|
2015-06-27 07:56:53 +08:00
|
|
|
|
2016-06-03 01:10:43 +08:00
|
|
|
// -coff-debug-directory
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFDebugDirectory("coff-debug-directory",
|
|
|
|
cl::desc("Display the PE/COFF debug directory"));
|
|
|
|
|
2017-04-28 03:38:38 +08:00
|
|
|
// -coff-resources
|
|
|
|
cl::opt<bool> COFFResources("coff-resources",
|
|
|
|
cl::desc("Display the PE/COFF .rsrc section"));
|
|
|
|
|
2017-06-22 09:10:29 +08:00
|
|
|
// -coff-load-config
|
|
|
|
cl::opt<bool>
|
|
|
|
COFFLoadConfig("coff-load-config",
|
|
|
|
cl::desc("Display the PE/COFF load config"));
|
|
|
|
|
2015-08-22 04:28:30 +08:00
|
|
|
// -macho-data-in-code
|
|
|
|
cl::opt<bool>
|
|
|
|
MachODataInCode("macho-data-in-code",
|
|
|
|
cl::desc("Display MachO Data in Code command"));
|
|
|
|
|
2015-09-04 02:10:28 +08:00
|
|
|
// -macho-indirect-symbols
|
|
|
|
cl::opt<bool>
|
|
|
|
MachOIndirectSymbols("macho-indirect-symbols",
|
|
|
|
cl::desc("Display MachO indirect symbols"));
|
|
|
|
|
2015-09-09 08:21:18 +08:00
|
|
|
// -macho-linker-options
|
|
|
|
cl::opt<bool>
|
|
|
|
MachOLinkerOptions("macho-linker-options",
|
|
|
|
cl::desc("Display MachO linker options"));
|
|
|
|
|
2015-09-03 00:24:24 +08:00
|
|
|
// -macho-segment
|
|
|
|
cl::opt<bool>
|
|
|
|
MachOSegment("macho-segment",
|
|
|
|
cl::desc("Display MachO Segment command"));
|
|
|
|
|
2015-08-27 23:11:32 +08:00
|
|
|
// -macho-version-min
|
|
|
|
cl::opt<bool>
|
|
|
|
MachOVersionMin("macho-version-min",
|
|
|
|
cl::desc("Display MachO version min command"));
|
2015-09-01 03:32:31 +08:00
|
|
|
|
|
|
|
// -macho-dysymtab
|
|
|
|
cl::opt<bool>
|
|
|
|
MachODysymtab("macho-dysymtab",
|
|
|
|
cl::desc("Display MachO Dysymtab command"));
|
|
|
|
|
2015-06-27 07:56:53 +08:00
|
|
|
// -stackmap
|
|
|
|
cl::opt<bool>
|
|
|
|
PrintStackMap("stackmap",
|
|
|
|
cl::desc("Display contents of stackmap section"));
|
|
|
|
|
2015-10-17 07:19:01 +08:00
|
|
|
// -version-info
|
|
|
|
cl::opt<bool>
|
|
|
|
VersionInfo("version-info",
|
|
|
|
cl::desc("Display ELF version sections (if present)"));
|
|
|
|
cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
|
|
|
|
cl::aliasopt(VersionInfo));
|
2016-01-27 03:46:39 +08:00
|
|
|
|
|
|
|
cl::opt<bool> SectionGroups("elf-section-groups",
|
|
|
|
cl::desc("Display ELF section group contents"));
|
|
|
|
cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
|
|
|
|
cl::aliasopt(SectionGroups));
|
2016-04-12 01:15:30 +08:00
|
|
|
cl::opt<bool> HashHistogram(
|
|
|
|
"elf-hash-histogram",
|
|
|
|
cl::desc("Display bucket list histogram for hash sections"));
|
|
|
|
cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
|
|
|
|
cl::aliasopt(HashHistogram));
|
2016-01-27 03:46:39 +08:00
|
|
|
|
2016-03-02 05:45:22 +08:00
|
|
|
cl::opt<OutputStyleTy>
|
2016-02-11 04:40:55 +08:00
|
|
|
Output("elf-output-style", cl::desc("Specify ELF dump style"),
|
|
|
|
cl::values(clEnumVal(LLVM, "LLVM default style"),
|
2016-10-09 03:41:06 +08:00
|
|
|
clEnumVal(GNU, "GNU readelf style")),
|
2016-02-11 04:40:55 +08:00
|
|
|
cl::init(LLVM));
|
2013-04-04 02:31:38 +08:00
|
|
|
} // namespace opts
|
|
|
|
|
2015-07-20 11:38:17 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2015-12-05 03:29:49 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
|
2015-12-24 03:29:34 +08:00
|
|
|
errs() << "\nError reading file: " << Msg << ".\n";
|
|
|
|
errs().flush();
|
2015-07-20 11:23:55 +08:00
|
|
|
exit(1);
|
2015-07-20 11:01:49 +08:00
|
|
|
}
|
|
|
|
|
[CodeView] Decouple record deserialization from visitor dispatch.
Until now, our use case for the visitor has been to take a stream of bytes
representing a type stream, deserialize the records in sequence, and do
something with them, where "something" is determined by how the user
implements a particular set of callbacks on an abstract class.
For actually writing PDBs, however, we want to do the reverse. We have
some kind of description of the list of records in their in-memory format,
and we want to process each one. Perhaps by serializing them to a byte
stream, or perhaps by converting them from one description format (Yaml)
to another (in-memory representation).
This was difficult in the current model because deserialization and
invoking the callbacks were tightly coupled.
With this patch we change this so that TypeDeserializer is itself an
implementation of the particular set of callbacks. This decouples
deserialization from the iteration over a list of records and invocation
of the callbacks. TypeDeserializer is initialized with another
implementation of the callback interface, so that upon deserialization it
can pass the deserialized record through to the next set of callbacks. In
a sense this is like an implementation of the Decorator design pattern,
where the Deserializer is a decorator.
This will be useful for writing Pdbs from yaml, where we have a
description of the type records in Yaml format. In this case, the visitor
implementation would have each visitation callback method implemented in
such a way as to extract the proper set of fields from the Yaml, and it
could maintain state that builds up a list of these records. Finally at
the end we can pass this information through to another set of callbacks
which serializes them into a byte stream.
Reviewed By: majnemer, ruiu, rnk
Differential Revision: https://reviews.llvm.org/D23177
llvm-svn: 277871
2016-08-06 05:45:34 +08:00
|
|
|
void error(Error EC) {
|
2016-08-05 03:39:55 +08:00
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
handleAllErrors(std::move(EC),
|
[CodeView] Decouple record deserialization from visitor dispatch.
Until now, our use case for the visitor has been to take a stream of bytes
representing a type stream, deserialize the records in sequence, and do
something with them, where "something" is determined by how the user
implements a particular set of callbacks on an abstract class.
For actually writing PDBs, however, we want to do the reverse. We have
some kind of description of the list of records in their in-memory format,
and we want to process each one. Perhaps by serializing them to a byte
stream, or perhaps by converting them from one description format (Yaml)
to another (in-memory representation).
This was difficult in the current model because deserialization and
invoking the callbacks were tightly coupled.
With this patch we change this so that TypeDeserializer is itself an
implementation of the particular set of callbacks. This decouples
deserialization from the iteration over a list of records and invocation
of the callbacks. TypeDeserializer is initialized with another
implementation of the callback interface, so that upon deserialization it
can pass the deserialized record through to the next set of callbacks. In
a sense this is like an implementation of the Decorator design pattern,
where the Deserializer is a decorator.
This will be useful for writing Pdbs from yaml, where we have a
description of the type records in Yaml format. In this case, the visitor
implementation would have each visitation callback method implemented in
such a way as to extract the proper set of fields from the Yaml, and it
could maintain state that builds up a list of these records. Finally at
the end we can pass this information through to another set of callbacks
which serializes them into a byte stream.
Reviewed By: majnemer, ruiu, rnk
Differential Revision: https://reviews.llvm.org/D23177
llvm-svn: 277871
2016-08-06 05:45:34 +08:00
|
|
|
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
|
2016-08-05 03:39:55 +08:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:23:55 +08:00
|
|
|
void error(std::error_code EC) {
|
2013-04-04 02:31:38 +08:00
|
|
|
if (!EC)
|
2015-07-20 11:23:55 +08:00
|
|
|
return;
|
2015-08-07 05:54:37 +08:00
|
|
|
reportError(EC.message());
|
2013-01-01 00:05:21 +08:00
|
|
|
}
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
bool relocAddressLess(RelocationRef a, RelocationRef b) {
|
2015-07-06 23:53:43 +08:00
|
|
|
return a.getOffset() < b.getOffset();
|
2013-01-01 00:29:44 +08:00
|
|
|
}
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
} // namespace llvm
|
2012-03-01 09:36:50 +08:00
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
static void reportError(StringRef Input, std::error_code EC) {
|
2013-04-04 02:31:38 +08:00
|
|
|
if (Input == "-")
|
|
|
|
Input = "<stdin>";
|
2013-01-01 00:29:44 +08:00
|
|
|
|
2015-07-20 11:01:49 +08:00
|
|
|
reportError(Twine(Input) + ": " + EC.message());
|
2013-01-01 00:29:44 +08:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:24:01 +08:00
|
|
|
static void reportError(StringRef Input, Error Err) {
|
|
|
|
if (Input == "-")
|
|
|
|
Input = "<stdin>";
|
|
|
|
std::string ErrMsg;
|
|
|
|
{
|
|
|
|
raw_string_ostream ErrStream(ErrMsg);
|
|
|
|
logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
|
|
|
|
}
|
|
|
|
reportError(ErrMsg);
|
|
|
|
}
|
|
|
|
|
2014-06-18 16:47:09 +08:00
|
|
|
static bool isMipsArch(unsigned Arch) {
|
|
|
|
switch (Arch) {
|
|
|
|
case llvm::Triple::mips:
|
|
|
|
case llvm::Triple::mipsel:
|
|
|
|
case llvm::Triple::mips64:
|
|
|
|
case llvm::Triple::mips64el:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 01:46:17 +08:00
|
|
|
namespace {
|
2016-11-09 06:24:53 +08:00
|
|
|
struct ReadObjTypeTableBuilder {
|
2017-03-25 01:26:38 +08:00
|
|
|
ReadObjTypeTableBuilder()
|
|
|
|
: Allocator(), IDTable(Allocator), TypeTable(Allocator) {}
|
2014-06-18 16:47:09 +08:00
|
|
|
|
2016-09-10 01:46:17 +08:00
|
|
|
llvm::BumpPtrAllocator Allocator;
|
2017-03-25 01:26:38 +08:00
|
|
|
llvm::codeview::TypeTableBuilder IDTable;
|
|
|
|
llvm::codeview::TypeTableBuilder TypeTable;
|
2016-09-10 01:46:17 +08:00
|
|
|
};
|
|
|
|
}
|
2016-11-09 06:24:53 +08:00
|
|
|
static ReadObjTypeTableBuilder CVTypes;
|
2016-05-14 08:02:53 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
/// @brief Creates an format-specific object file dumper.
|
2016-05-03 08:28:04 +08:00
|
|
|
static std::error_code createDumper(const ObjectFile *Obj,
|
|
|
|
ScopedPrinter &Writer,
|
2014-06-13 11:07:50 +08:00
|
|
|
std::unique_ptr<ObjDumper> &Result) {
|
2013-04-04 02:31:38 +08:00
|
|
|
if (!Obj)
|
|
|
|
return readobj_error::unsupported_file_format;
|
|
|
|
|
|
|
|
if (Obj->isCOFF())
|
|
|
|
return createCOFFDumper(Obj, Writer, Result);
|
|
|
|
if (Obj->isELF())
|
|
|
|
return createELFDumper(Obj, Writer, Result);
|
|
|
|
if (Obj->isMachO())
|
|
|
|
return createMachODumper(Obj, Writer, Result);
|
2017-01-31 07:30:52 +08:00
|
|
|
if (Obj->isWasm())
|
|
|
|
return createWasmDumper(Obj, Writer, Result);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
return readobj_error::unsupported_obj_file_format;
|
2012-03-01 09:36:50 +08:00
|
|
|
}
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
/// @brief Dumps the specified object file.
|
|
|
|
static void dumpObject(const ObjectFile *Obj) {
|
2016-05-03 08:28:04 +08:00
|
|
|
ScopedPrinter Writer(outs());
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<ObjDumper> Dumper;
|
2015-12-06 07:36:52 +08:00
|
|
|
if (std::error_code EC = createDumper(Obj, Writer, Dumper))
|
2013-04-04 02:31:38 +08:00
|
|
|
reportError(Obj->getFileName(), EC);
|
2013-01-01 00:29:44 +08:00
|
|
|
|
2016-02-11 04:40:55 +08:00
|
|
|
if (opts::Output == opts::LLVM) {
|
|
|
|
outs() << '\n';
|
|
|
|
outs() << "File: " << Obj->getFileName() << "\n";
|
|
|
|
outs() << "Format: " << Obj->getFileFormatName() << "\n";
|
|
|
|
outs() << "Arch: " << Triple::getArchTypeName(
|
|
|
|
(llvm::Triple::ArchType)Obj->getArch()) << "\n";
|
|
|
|
outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n";
|
|
|
|
Dumper->printLoadName();
|
|
|
|
}
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
if (opts::FileHeaders)
|
|
|
|
Dumper->printFileHeaders();
|
|
|
|
if (opts::Sections)
|
|
|
|
Dumper->printSections();
|
|
|
|
if (opts::Relocations)
|
|
|
|
Dumper->printRelocations();
|
2015-06-26 05:47:32 +08:00
|
|
|
if (opts::DynRelocs)
|
|
|
|
Dumper->printDynamicRelocations();
|
2013-04-04 02:31:38 +08:00
|
|
|
if (opts::Symbols)
|
|
|
|
Dumper->printSymbols();
|
|
|
|
if (opts::DynamicSymbols)
|
|
|
|
Dumper->printDynamicSymbols();
|
|
|
|
if (opts::UnwindInfo)
|
|
|
|
Dumper->printUnwindInfo();
|
|
|
|
if (opts::DynamicTable)
|
|
|
|
Dumper->printDynamicTable();
|
|
|
|
if (opts::NeededLibraries)
|
|
|
|
Dumper->printNeededLibraries();
|
2013-04-12 12:07:39 +08:00
|
|
|
if (opts::ProgramHeaders)
|
|
|
|
Dumper->printProgramHeaders();
|
2015-07-10 06:32:24 +08:00
|
|
|
if (opts::HashTable)
|
|
|
|
Dumper->printHashTable();
|
2015-10-14 20:11:50 +08:00
|
|
|
if (opts::GnuHashTable)
|
|
|
|
Dumper->printGnuHashTable();
|
2015-10-17 07:19:01 +08:00
|
|
|
if (opts::VersionInfo)
|
|
|
|
Dumper->printVersionInfo();
|
2016-01-27 03:46:39 +08:00
|
|
|
if (Obj->isELF()) {
|
|
|
|
if (Obj->getArch() == llvm::Triple::arm)
|
|
|
|
if (opts::ARMAttributes)
|
|
|
|
Dumper->printAttributes();
|
|
|
|
if (isMipsArch(Obj->getArch())) {
|
|
|
|
if (opts::MipsPLTGOT)
|
|
|
|
Dumper->printMipsPLTGOT();
|
|
|
|
if (opts::MipsABIFlags)
|
|
|
|
Dumper->printMipsABIFlags();
|
|
|
|
if (opts::MipsReginfo)
|
|
|
|
Dumper->printMipsReginfo();
|
2016-05-04 13:58:57 +08:00
|
|
|
if (opts::MipsOptions)
|
|
|
|
Dumper->printMipsOptions();
|
2016-01-27 03:46:39 +08:00
|
|
|
}
|
2016-12-15 01:16:52 +08:00
|
|
|
if (Obj->getArch() == llvm::Triple::amdgcn)
|
2017-03-23 06:32:22 +08:00
|
|
|
if (opts::AMDGPUCodeObjectMetadata)
|
|
|
|
Dumper->printAMDGPUCodeObjectMetadata();
|
2016-01-27 03:46:39 +08:00
|
|
|
if (opts::SectionGroups)
|
|
|
|
Dumper->printGroupSections();
|
2016-04-12 01:15:30 +08:00
|
|
|
if (opts::HashHistogram)
|
|
|
|
Dumper->printHashHistogram();
|
2016-08-31 02:52:02 +08:00
|
|
|
if (opts::Notes)
|
|
|
|
Dumper->printNotes();
|
2015-05-07 23:40:35 +08:00
|
|
|
}
|
2015-07-24 10:14:20 +08:00
|
|
|
if (Obj->isCOFF()) {
|
|
|
|
if (opts::COFFImports)
|
|
|
|
Dumper->printCOFFImports();
|
|
|
|
if (opts::COFFExports)
|
|
|
|
Dumper->printCOFFExports();
|
|
|
|
if (opts::COFFDirectives)
|
|
|
|
Dumper->printCOFFDirectives();
|
|
|
|
if (opts::COFFBaseRelocs)
|
|
|
|
Dumper->printCOFFBaseReloc();
|
2016-06-03 01:10:43 +08:00
|
|
|
if (opts::COFFDebugDirectory)
|
|
|
|
Dumper->printCOFFDebugDirectory();
|
2017-04-28 03:38:38 +08:00
|
|
|
if (opts::COFFResources)
|
|
|
|
Dumper->printCOFFResources();
|
2017-06-22 09:10:29 +08:00
|
|
|
if (opts::COFFLoadConfig)
|
|
|
|
Dumper->printCOFFLoadConfig();
|
2015-12-17 02:28:12 +08:00
|
|
|
if (opts::CodeView)
|
|
|
|
Dumper->printCodeViewDebugInfo();
|
2016-05-14 08:02:53 +08:00
|
|
|
if (opts::CodeViewMergedTypes)
|
2017-03-25 01:26:38 +08:00
|
|
|
Dumper->mergeCodeViewTypes(CVTypes.IDTable, CVTypes.TypeTable);
|
2015-07-24 10:14:20 +08:00
|
|
|
}
|
2015-09-01 01:12:23 +08:00
|
|
|
if (Obj->isMachO()) {
|
2015-08-22 04:28:30 +08:00
|
|
|
if (opts::MachODataInCode)
|
|
|
|
Dumper->printMachODataInCode();
|
2015-09-04 02:10:28 +08:00
|
|
|
if (opts::MachOIndirectSymbols)
|
|
|
|
Dumper->printMachOIndirectSymbols();
|
2015-09-09 08:21:18 +08:00
|
|
|
if (opts::MachOLinkerOptions)
|
|
|
|
Dumper->printMachOLinkerOptions();
|
2015-09-03 00:24:24 +08:00
|
|
|
if (opts::MachOSegment)
|
|
|
|
Dumper->printMachOSegment();
|
2015-08-27 23:11:32 +08:00
|
|
|
if (opts::MachOVersionMin)
|
|
|
|
Dumper->printMachOVersionMin();
|
2015-09-01 03:32:31 +08:00
|
|
|
if (opts::MachODysymtab)
|
|
|
|
Dumper->printMachODysymtab();
|
2015-09-01 01:12:23 +08:00
|
|
|
}
|
2015-06-27 07:56:53 +08:00
|
|
|
if (opts::PrintStackMap)
|
|
|
|
Dumper->printStackMap();
|
2012-03-02 06:19:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
/// @brief Dumps each object file in \a Arc;
|
|
|
|
static void dumpArchive(const Archive *Arc) {
|
2016-11-11 12:28:40 +08:00
|
|
|
Error Err = Error::success();
|
2016-07-14 10:24:01 +08:00
|
|
|
for (auto &Child : Arc->children(Err)) {
|
2016-05-18 01:10:12 +08:00
|
|
|
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
|
|
|
|
if (!ChildOrErr) {
|
|
|
|
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
|
2017-05-10 22:18:11 +08:00
|
|
|
reportError(Arc->getFileName(), ChildOrErr.takeError());
|
2016-05-18 01:10:12 +08:00
|
|
|
}
|
2013-04-04 02:31:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-17 00:08:36 +08:00
|
|
|
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
|
2013-04-04 02:31:38 +08:00
|
|
|
dumpObject(Obj);
|
2016-08-18 22:32:11 +08:00
|
|
|
else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
|
|
|
|
dumpCOFFImportFile(Imp);
|
2013-04-04 02:31:38 +08:00
|
|
|
else
|
|
|
|
reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
|
2012-03-01 09:36:50 +08:00
|
|
|
}
|
2016-07-14 10:24:01 +08:00
|
|
|
if (Err)
|
|
|
|
reportError(Arc->getFileName(), std::move(Err));
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 04:26:55 +08:00
|
|
|
/// @brief Dumps each object file in \a MachO Universal Binary;
|
|
|
|
static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
|
|
|
|
for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
|
2016-06-01 04:35:34 +08:00
|
|
|
Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
|
2015-04-14 00:05:49 +08:00
|
|
|
if (ObjOrErr)
|
|
|
|
dumpObject(&*ObjOrErr.get());
|
2016-06-01 04:35:34 +08:00
|
|
|
else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
|
2017-05-10 22:18:11 +08:00
|
|
|
reportError(UBinary->getFileName(), ObjOrErr.takeError());
|
2016-06-01 04:35:34 +08:00
|
|
|
}
|
2016-06-29 07:16:13 +08:00
|
|
|
else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
|
2015-04-14 00:05:49 +08:00
|
|
|
dumpArchive(&*AOrErr.get());
|
2015-03-25 04:26:55 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 09:36:50 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
/// @brief Opens \a File and dumps it.
|
|
|
|
static void dumpInput(StringRef File) {
|
2012-03-01 09:36:50 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
// Attempt to open the binary.
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
|
|
|
|
if (!BinaryOrErr)
|
2017-05-10 22:18:11 +08:00
|
|
|
reportError(File, BinaryOrErr.takeError());
|
2014-08-20 02:44:46 +08:00
|
|
|
Binary &Binary = *BinaryOrErr.get().getBinary();
|
2012-03-01 09:36:50 +08:00
|
|
|
|
2014-08-01 22:31:55 +08:00
|
|
|
if (Archive *Arc = dyn_cast<Archive>(&Binary))
|
2013-04-04 02:31:38 +08:00
|
|
|
dumpArchive(Arc);
|
2015-03-25 04:26:55 +08:00
|
|
|
else if (MachOUniversalBinary *UBinary =
|
|
|
|
dyn_cast<MachOUniversalBinary>(&Binary))
|
|
|
|
dumpMachOUniversalBinary(UBinary);
|
2014-08-01 22:31:55 +08:00
|
|
|
else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
|
2013-04-04 02:31:38 +08:00
|
|
|
dumpObject(Obj);
|
2015-08-28 15:40:30 +08:00
|
|
|
else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
|
|
|
|
dumpCOFFImportFile(Import);
|
2013-04-04 02:31:38 +08:00
|
|
|
else
|
|
|
|
reportError(File, readobj_error::unrecognized_file_format);
|
|
|
|
}
|
2013-01-01 00:53:01 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
int main(int argc, const char *argv[]) {
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2013-04-04 02:31:38 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y;
|
2013-01-01 00:29:44 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
// Register the target printer for --version.
|
|
|
|
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
|
2013-02-20 10:37:12 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
// Default to stdin if no filename is specified.
|
|
|
|
if (opts::InputFilenames.size() == 0)
|
|
|
|
opts::InputFilenames.push_back("-");
|
|
|
|
|
|
|
|
std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
|
|
|
|
dumpInput);
|
2013-01-01 00:53:01 +08:00
|
|
|
|
2016-05-14 08:02:53 +08:00
|
|
|
if (opts::CodeViewMergedTypes) {
|
|
|
|
ScopedPrinter W(outs());
|
2017-03-25 01:26:38 +08:00
|
|
|
dumpCodeViewMergedTypes(W, CVTypes.IDTable, CVTypes.TypeTable);
|
2016-05-14 08:02:53 +08:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:23:55 +08:00
|
|
|
return 0;
|
2012-03-01 09:36:50 +08:00
|
|
|
}
|