[llvm] r336284 - [llvm-objdump] Add --file-headers (-f) option
Paul Semel via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 4 08:25:03 PDT 2018
Author: paulsemel
Date: Wed Jul 4 08:25:03 2018
New Revision: 336284
URL: http://llvm.org/viewvc/llvm-project?rev=336284&view=rev
Log:
[llvm-objdump] Add --file-headers (-f) option
Added:
llvm/trunk/test/tools/llvm-objdump/file-headers-coff.test
llvm/trunk/test/tools/llvm-objdump/file-headers-elf.test
llvm/trunk/test/tools/llvm-objdump/file-headers-pe.test
llvm/trunk/test/tools/llvm-objdump/file-headers-unsupported.test
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/include/llvm/Object/ELFObjectFile.h
llvm/trunk/include/llvm/Object/ObjectFile.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.h
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Wed Jul 4 08:25:03 2018
@@ -928,6 +928,7 @@ public:
uint8_t getBytesInAddress() const override;
StringRef getFileFormatName() const override;
Triple::ArchType getArch() const override;
+ Expected<uint64_t> getStartAddress() const override;
SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
import_directory_iterator import_directory_begin() const;
Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Wed Jul 4 08:25:03 2018
@@ -373,6 +373,7 @@ public:
uint8_t getBytesInAddress() const override;
StringRef getFileFormatName() const override;
Triple::ArchType getArch() const override;
+ Expected<uint64_t> getStartAddress() const override;
unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
@@ -1144,6 +1145,11 @@ template <class ELFT> Triple::ArchType E
}
template <class ELFT>
+Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
+ return EF.getHeader()->e_entry;
+}
+
+template <class ELFT>
ELFObjectFileBase::elf_symbol_iterator_range
ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Wed Jul 4 08:25:03 2018
@@ -287,6 +287,9 @@ public:
virtual Triple::ArchType getArch() const = 0;
virtual SubtargetFeatures getFeatures() const = 0;
virtual void setARMSubArch(Triple &TheTriple) const { }
+ virtual Expected<uint64_t> getStartAddress() const {
+ return errorCodeToError(object_error::parse_failed);
+ };
/// Create a triple from the data in this object file.
Triple makeTriple() const;
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Wed Jul 4 08:25:03 2018
@@ -910,6 +910,12 @@ Triple::ArchType COFFObjectFile::getArch
}
}
+Expected<uint64_t> COFFObjectFile::getStartAddress() const {
+ if (PE32Header)
+ return PE32Header->AddressOfEntryPoint;
+ return 0;
+}
+
iterator_range<import_directory_iterator>
COFFObjectFile::import_directories() const {
return make_range(import_directory_begin(), import_directory_end());
Added: llvm/trunk/test/tools/llvm-objdump/file-headers-coff.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/file-headers-coff.test?rev=336284&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/file-headers-coff.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/file-headers-coff.test Wed Jul 4 08:25:03 2018
@@ -0,0 +1,13 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objdump -f %t | FileCheck %s
+# RUN: llvm-objdump -file-headers %t | FileCheck %s
+
+!COFF
+header: !Header
+ Machine: IMAGE_FILE_MACHINE_I386 # (0x14c)
+ Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
+sections:
+symbols:
+
+# CHECK: architecture: i386
+# CHECK: start address: 0x0000
Added: llvm/trunk/test/tools/llvm-objdump/file-headers-elf.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/file-headers-elf.test?rev=336284&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/file-headers-elf.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/file-headers-elf.test Wed Jul 4 08:25:03 2018
@@ -0,0 +1,14 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objdump -f %t | FileCheck %s
+# RUN: llvm-objdump -file-headers %t | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+ Entry: 0x123456
+
+# CHECK: architecture: x86_64
+# CHECK: start address: 0x00123456
Added: llvm/trunk/test/tools/llvm-objdump/file-headers-pe.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/file-headers-pe.test?rev=336284&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/file-headers-pe.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/file-headers-pe.test Wed Jul 4 08:25:03 2018
@@ -0,0 +1,33 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objdump -f %t | FileCheck %s
+# RUN: llvm-objdump -file-headers %t | FileCheck %s
+
+!COFF
+header: !Header
+ Machine: IMAGE_FILE_MACHINE_I386
+ Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
+OptionalHeader:
+ AddressOfEntryPoint: 0x1234
+# Unfortunately, all these flags are mandatory to set AddressOfEntryPoint.
+# All the values are randomly picked. They can't interfere in what
+# we are testing here.
+ SizeOfHeapCommit: 1024
+ SizeOfHeapReserve: 1024
+ SizeOfStackCommit: 1024
+ SizeOfStackReserve: 1024
+ DLLCharacteristics: [ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ]
+ Subsystem: IMAGE_SUBSYSTEM_NATIVE
+ MinorSubsystemVersion: 1
+ MajorSubsystemVersion: 1
+ MinorImageVersion: 1
+ MajorImageVersion: 1
+ MinorOperatingSystemVersion: 1
+ MajorOperatingSystemVersion: 1
+ FileAlignment: 8
+ SectionAlignment: 8
+ ImageBase: 0x12
+sections:
+symbols:
+
+# CHECK: architecture: i386
+# CHECK: start address: 0x1234
Added: llvm/trunk/test/tools/llvm-objdump/file-headers-unsupported.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/file-headers-unsupported.test?rev=336284&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/file-headers-unsupported.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/file-headers-unsupported.test Wed Jul 4 08:25:03 2018
@@ -0,0 +1,3 @@
+# RUN: not llvm-objdump -file-headers %p/Inputs/trivial.obj.wasm 2>&1 | FileCheck %s
+
+# CHECK: Invalid/Unsupported object file format
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Jul 4 08:25:03 2018
@@ -200,6 +200,13 @@ static cl::alias
PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
cl::aliasopt(PrivateHeaders));
+cl::opt<bool> llvm::FileHeaders(
+ "file-headers",
+ cl::desc("Display the contents of the overall file header"));
+
+static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"),
+ cl::aliasopt(FileHeaders));
+
cl::opt<bool>
llvm::PrintImmHex("print-imm-hex",
cl::desc("Use hex format for immediate values"));
@@ -2123,6 +2130,20 @@ static void printPrivateFileHeaders(cons
report_error(o->getFileName(), "Invalid/Unsupported object file format");
}
+static void printFileHeaders(const ObjectFile *o) {
+ if (!o->isELF() && !o->isCOFF())
+ report_error(o->getFileName(), "Invalid/Unsupported object file format");
+
+ Triple::ArchType AT = o->getArch();
+ outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
+ Expected<uint64_t> StartAddrOrErr = o->getStartAddress();
+ if (!StartAddrOrErr)
+ report_error(o->getFileName(), StartAddrOrErr.takeError());
+ outs() << "start address: "
+ << format("0x%0*x", o->getBytesInAddress(), StartAddrOrErr.get())
+ << "\n";
+}
+
static void DumpObject(ObjectFile *o, const Archive *a = nullptr) {
StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
// Avoid other output when using a raw option.
@@ -2151,6 +2172,8 @@ static void DumpObject(ObjectFile *o, co
PrintUnwindInfo(o);
if (PrivateHeaders || FirstPrivateHeader)
printPrivateFileHeaders(o, FirstPrivateHeader);
+ if (FileHeaders)
+ printFileHeaders(o);
if (ExportsTrie)
printExportsTrie(o);
if (Rebase)
@@ -2267,6 +2290,7 @@ int main(int argc, char **argv) {
&& !SymbolTable
&& !UnwindInfo
&& !PrivateHeaders
+ && !FileHeaders
&& !FirstPrivateHeader
&& !ExportsTrie
&& !Rebase
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.h?rev=336284&r1=336283&r2=336284&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.h (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.h Wed Jul 4 08:25:03 2018
@@ -38,6 +38,7 @@ extern cl::opt<bool> DisassembleAll;
extern cl::opt<bool> NoShowRawInsn;
extern cl::opt<bool> NoLeadingAddr;
extern cl::opt<bool> PrivateHeaders;
+extern cl::opt<bool> FileHeaders;
extern cl::opt<bool> FirstPrivateHeader;
extern cl::opt<bool> ExportsTrie;
extern cl::opt<bool> Rebase;
More information about the llvm-commits
mailing list