Skip to content

Commit 4e2e785

Browse files
author
Georgii Rymar
committed
[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
1 parent 4b6f294 commit 4e2e785

File tree

4 files changed

+32
-70
lines changed

4 files changed

+32
-70
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,17 @@ struct Elf64_Ehdr {
107107
unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
108108
};
109109

110-
// File types
110+
// File types.
111+
// See current registered ELF types at:
112+
// http://www.sco.com/developers/gabi/latest/ch4.eheader.html
111113
enum {
112114
ET_NONE = 0, // No file type
113115
ET_REL = 1, // Relocatable file
114116
ET_EXEC = 2, // Executable file
115117
ET_DYN = 3, // Shared object file
116118
ET_CORE = 4, // Core file
119+
ET_LOOS = 0xfe00, // Beginning of operating system-specific codes
120+
ET_HIOS = 0xfeff, // Operating system-specific
117121
ET_LOPROC = 0xff00, // Beginning of processor-specific codes
118122
ET_HIPROC = 0xffff // Processor-specific
119123
};

llvm/test/tools/llvm-readobj/ELF/file-types.test

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ FileHeader:
5656
# GNU-CORE: ELF Header:
5757
# GNU-CORE: Type: CORE (Core file)
5858

59+
## Test what we dump for an arbitrary unknown ELF type.
60+
# RUN: yaml2obj %s -DTYPE=0xFDFF -o %t.unknown
61+
# RUN: llvm-readobj -h %t.unknown | FileCheck %s --match-full-lines --check-prefix LLVM-UNNKNOWN
62+
# RUN: llvm-readelf -h %t.unknown | FileCheck %s --match-full-lines --check-prefix GNU-UNNKNOWN
63+
64+
# LLVM-UNNKNOWN: ElfHeader {
65+
# LLVM-UNNKNOWN: Type: 0xFDFF
66+
67+
# GNU-UNNKNOWN: ELF Header:
68+
# GNU-UNNKNOWN: Type: <unknown>: fdff
69+
5970
# RUN: yaml2obj %s -DTYPE=0xfe00 -o %t6
6071
# RUN: llvm-readobj -h %t6 | FileCheck %s --match-full-lines --check-prefix LLVM-LOOS
6172
# RUN: llvm-readelf -h %t6 | FileCheck %s --match-full-lines --check-prefix GNU-LOOS
@@ -64,7 +75,7 @@ FileHeader:
6475
# LLVM-LOOS: Type: 0xFE00
6576

6677
# GNU-LOOS: ELF Header:
67-
# GNU-LOOS: Type: fe00
78+
# GNU-LOOS: Type: OS Specific: (fe00)
6879

6980
# RUN: yaml2obj %s -DTYPE=0xfeff -o %t7
7081
# RUN: llvm-readobj -h %t7 | FileCheck %s --match-full-lines --check-prefix LLVM-HIOS
@@ -74,7 +85,7 @@ FileHeader:
7485
# LLVM-HIOS: Type: 0xFEFF
7586

7687
# GNU-HIOS: ELF Header:
77-
# GNU-HIOS: Type: feff
88+
# GNU-HIOS: Type: OS Specific: (feff)
7889

7990
# RUN: yaml2obj %s -DTYPE=0xff00 -o %t8
8091
# RUN: llvm-readobj -h %t8 | FileCheck %s --match-full-lines --check-prefix LLVM-LOPROC
@@ -84,7 +95,7 @@ FileHeader:
8495
# LLVM-LOPROC: Type: 0xFF00
8596

8697
# GNU-LOPROC: ELF Header:
87-
# GNU-LOPROC: Type: ff00
98+
# GNU-LOPROC: Type: Processor Specific: (ff00)
8899

89100
# RUN: yaml2obj %s -DTYPE=0xffff -o %t9
90101
# RUN: llvm-readobj -h %t9 | FileCheck %s --match-full-lines --check-prefix LLVM-HIPROC
@@ -94,4 +105,4 @@ FileHeader:
94105
# LLVM-HIPROC: Type: 0xFFFF
95106

96107
# GNU-HIPROC: ELF Header:
97-
# GNU-HIPROC: Type: ffff
108+
# GNU-HIPROC: Type: Processor Specific: (ffff)

llvm/test/tools/llvm-readobj/ELF/types.test

Lines changed: 0 additions & 65 deletions
This file was deleted.

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,8 +3526,20 @@ template <class ELFT> void GNUStyle<ELFT>::printFileHeaders() {
35263526
printFields(OS, "OS/ABI:", Str);
35273527
printFields(OS,
35283528
"ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
3529+
35293530
Str = printEnum(e.e_type, makeArrayRef(ElfObjectFileType));
3531+
if (e.e_type >= ET_LOPROC) {
3532+
Str = "Processor Specific: (" + Str + ")";
3533+
} else if (e.e_type >= ET_LOOS) {
3534+
Str = "OS Specific: (" + Str + ")";
3535+
} else if (makeArrayRef(ElfObjectFileType).end() ==
3536+
llvm::find_if(ElfObjectFileType,
3537+
[&](const EnumEntry<unsigned> &E) {
3538+
return E.Value == e.e_type;
3539+
}))
3540+
Str = "<unknown>: " + Str;
35303541
printFields(OS, "Type:", Str);
3542+
35313543
Str = printEnum(e.e_machine, makeArrayRef(ElfMachineType));
35323544
printFields(OS, "Machine:", Str);
35333545
Str = "0x" + to_hexString(e.e_version);

0 commit comments

Comments
 (0)