[llvm-branch-commits] [llvm] 4e2e785 - [llvm-readelf] - Improve ELF type field dumping.
Georgii Rymar via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Dec 14 00:45:20 PST 2020
Author: Georgii Rymar
Date: 2020-12-14T11:24:08+03:00
New Revision: 4e2e785ddd35c421a4df9453f17b8317d6b62b2c
URL: https://github.com/llvm/llvm-project/commit/4e2e785ddd35c421a4df9453f17b8317d6b62b2c
DIFF: https://github.com/llvm/llvm-project/commit/4e2e785ddd35c421a4df9453f17b8317d6b62b2c.diff
LOG: [llvm-readelf] - Improve ELF type field dumping.
This is related to https://bugs.llvm.org/show_bug.cgi?id=40868.
Currently we don't print `OS Specific`/``Processor Specific`/`<unknown>`
prefixes when dumping the ELF file type. This is not consistent
with GNU readelf. The patch fixes it.
Also, this patch removes the `types.test`, because we already have
`file-types.test`, which tests more cases and this patch revealed that
we have such a duplicate.
Differential revision: https://reviews.llvm.org/D93096
Added:
Modified:
llvm/include/llvm/BinaryFormat/ELF.h
llvm/test/tools/llvm-readobj/ELF/file-types.test
llvm/tools/llvm-readobj/ELFDumper.cpp
Removed:
llvm/test/tools/llvm-readobj/ELF/types.test
################################################################################
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index d49760c5d4c1..1552303b610c 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -107,13 +107,17 @@ struct Elf64_Ehdr {
unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
};
-// File types
+// File types.
+// See current registered ELF types at:
+// http://www.sco.com/developers/gabi/latest/ch4.eheader.html
enum {
ET_NONE = 0, // No file type
ET_REL = 1, // Relocatable file
ET_EXEC = 2, // Executable file
ET_DYN = 3, // Shared object file
ET_CORE = 4, // Core file
+ ET_LOOS = 0xfe00, // Beginning of operating system-specific codes
+ ET_HIOS = 0xfeff, // Operating system-specific
ET_LOPROC = 0xff00, // Beginning of processor-specific codes
ET_HIPROC = 0xffff // Processor-specific
};
diff --git a/llvm/test/tools/llvm-readobj/ELF/file-types.test b/llvm/test/tools/llvm-readobj/ELF/file-types.test
index 8c8dc43d6bcc..767ce4d646ff 100644
--- a/llvm/test/tools/llvm-readobj/ELF/file-types.test
+++ b/llvm/test/tools/llvm-readobj/ELF/file-types.test
@@ -56,6 +56,17 @@ FileHeader:
# GNU-CORE: ELF Header:
# GNU-CORE: Type: CORE (Core file)
+## Test what we dump for an arbitrary unknown ELF type.
+# RUN: yaml2obj %s -DTYPE=0xFDFF -o %t.unknown
+# RUN: llvm-readobj -h %t.unknown | FileCheck %s --match-full-lines --check-prefix LLVM-UNNKNOWN
+# RUN: llvm-readelf -h %t.unknown | FileCheck %s --match-full-lines --check-prefix GNU-UNNKNOWN
+
+# LLVM-UNNKNOWN: ElfHeader {
+# LLVM-UNNKNOWN: Type: 0xFDFF
+
+# GNU-UNNKNOWN: ELF Header:
+# GNU-UNNKNOWN: Type: <unknown>: fdff
+
# RUN: yaml2obj %s -DTYPE=0xfe00 -o %t6
# RUN: llvm-readobj -h %t6 | FileCheck %s --match-full-lines --check-prefix LLVM-LOOS
# RUN: llvm-readelf -h %t6 | FileCheck %s --match-full-lines --check-prefix GNU-LOOS
@@ -64,7 +75,7 @@ FileHeader:
# LLVM-LOOS: Type: 0xFE00
# GNU-LOOS: ELF Header:
-# GNU-LOOS: Type: fe00
+# GNU-LOOS: Type: OS Specific: (fe00)
# RUN: yaml2obj %s -DTYPE=0xfeff -o %t7
# RUN: llvm-readobj -h %t7 | FileCheck %s --match-full-lines --check-prefix LLVM-HIOS
@@ -74,7 +85,7 @@ FileHeader:
# LLVM-HIOS: Type: 0xFEFF
# GNU-HIOS: ELF Header:
-# GNU-HIOS: Type: feff
+# GNU-HIOS: Type: OS Specific: (feff)
# RUN: yaml2obj %s -DTYPE=0xff00 -o %t8
# RUN: llvm-readobj -h %t8 | FileCheck %s --match-full-lines --check-prefix LLVM-LOPROC
@@ -84,7 +95,7 @@ FileHeader:
# LLVM-LOPROC: Type: 0xFF00
# GNU-LOPROC: ELF Header:
-# GNU-LOPROC: Type: ff00
+# GNU-LOPROC: Type: Processor Specific: (ff00)
# RUN: yaml2obj %s -DTYPE=0xffff -o %t9
# RUN: llvm-readobj -h %t9 | FileCheck %s --match-full-lines --check-prefix LLVM-HIPROC
@@ -94,4 +105,4 @@ FileHeader:
# LLVM-HIPROC: Type: 0xFFFF
# GNU-HIPROC: ELF Header:
-# GNU-HIPROC: Type: ffff
+# GNU-HIPROC: Type: Processor Specific: (ffff)
diff --git a/llvm/test/tools/llvm-readobj/ELF/types.test b/llvm/test/tools/llvm-readobj/ELF/types.test
deleted file mode 100644
index ffc3ec34aea4..000000000000
--- a/llvm/test/tools/llvm-readobj/ELF/types.test
+++ /dev/null
@@ -1,65 +0,0 @@
-# Show that llvm-readobj can handle all standard ELF types.
-# RUN: yaml2obj %s -DTYPE=ET_NONE -o %t.none
-# RUN: llvm-readobj --file-headers %t.none | FileCheck %s --check-prefix=LLVM-NONE
-# RUN: llvm-readelf --file-headers %t.none | FileCheck %s --check-prefix=GNU-NONE
-
-# LLVM-NONE: Type: None (0x0)
-# GNU-NONE: Type: NONE (none)
-
---- !ELF
-FileHeader:
- Class: ELFCLASS64
- Data: ELFDATA2LSB
- Type: [[TYPE]]
-
-# RUN: yaml2obj %s -DTYPE=ET_REL -o %t.rel
-# RUN: llvm-readobj --file-headers %t.rel | FileCheck %s --check-prefix=LLVM-REL
-# RUN: llvm-readelf --file-headers %t.rel | FileCheck %s --check-prefix=GNU-REL
-
-# LLVM-REL: Type: Relocatable (0x1)
-# GNU-REL: Type: REL (Relocatable file)
-
-# RUN: yaml2obj %s -DTYPE=ET_EXEC -o %t.exec
-# RUN: llvm-readobj --file-headers %t.exec | FileCheck %s --check-prefix=LLVM-EXEC
-# RUN: llvm-readelf --file-headers %t.exec | FileCheck %s --check-prefix=GNU-EXEC
-
-# LLVM-EXEC: Type: Executable (0x2)
-# GNU-EXEC: Type: EXEC (Executable file)
-
-# RUN: yaml2obj %s -DTYPE=ET_DYN -o %t.so
-# RUN: llvm-readobj --file-headers %t.so | FileCheck %s --check-prefix=LLVM-SO
-# RUN: llvm-readelf --file-headers %t.so | FileCheck %s --check-prefix=GNU-SO
-
-# LLVM-SO: Type: SharedObject (0x3)
-# GNU-SO: Type: DYN (Shared object file)
-
-# RUN: yaml2obj %s -DTYPE=ET_CORE -o %t.core
-# RUN: llvm-readobj --file-headers %t.core | FileCheck %s --check-prefix=LLVM-CORE
-# RUN: llvm-readelf --file-headers %t.core | FileCheck %s --check-prefix=GNU-CORE
-
-# LLVM-CORE: Type: Core (0x4)
-# GNU-CORE: Type: CORE (Core file)
-
-# Show that llvm-readobj can handle an unknown ELF type.
-# RUN: yaml2obj %s -DTYPE=0x42 -o %t.unknown
-# RUN: llvm-readobj --file-headers %t.unknown | FileCheck %s --check-prefix=LLVM-UNKNOWN
-# RUN: llvm-readelf --file-headers %t.unknown | FileCheck %s --check-prefix=GNU-UNKNOWN
-
-# LLVM-UNKNOWN: Type: 0x42
-# GNU-UNKNOWN: Type: 42
-
-# Show that llvm-readobj can handle an unknown OS-specific ELF type.
-# RUN: yaml2obj %s -DTYPE=0xfe00 -o %t.os
-# RUN: llvm-readobj --file-headers %t.os | FileCheck %s --check-prefix=LLVM-OS
-# RUN: llvm-readelf --file-headers %t.os | FileCheck %s --check-prefix=GNU-OS
-
-# LLVM-OS: Type: 0xFE00
-# GNU-OS: Type: fe00
-
-# Show that llvm-readobj can handle an unknown machine-specific ELF type.
-# RUN: yaml2obj %s -DTYPE=0xff00 -o %t.proc
-# RUN: llvm-readobj --file-headers %t.proc | FileCheck %s --check-prefix=LLVM-PROC
-# RUN: llvm-readelf --file-headers %t.proc | FileCheck %s --check-prefix=GNU-PROC
-
-# LLVM-PROC: Type: 0xFF00
-# GNU-PROC: Type: ff00
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 279e406397d5..a13a505b1368 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -3526,8 +3526,20 @@ template <class ELFT> void GNUStyle<ELFT>::printFileHeaders() {
printFields(OS, "OS/ABI:", Str);
printFields(OS,
"ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
+
Str = printEnum(e.e_type, makeArrayRef(ElfObjectFileType));
+ if (e.e_type >= ET_LOPROC) {
+ Str = "Processor Specific: (" + Str + ")";
+ } else if (e.e_type >= ET_LOOS) {
+ Str = "OS Specific: (" + Str + ")";
+ } else if (makeArrayRef(ElfObjectFileType).end() ==
+ llvm::find_if(ElfObjectFileType,
+ [&](const EnumEntry<unsigned> &E) {
+ return E.Value == e.e_type;
+ }))
+ Str = "<unknown>: " + Str;
printFields(OS, "Type:", Str);
+
Str = printEnum(e.e_machine, makeArrayRef(ElfMachineType));
printFields(OS, "Machine:", Str);
Str = "0x" + to_hexString(e.e_version);
More information about the llvm-branch-commits
mailing list