Skip to content

Commit a5cc951

Browse files
authored
[BinaryFormat] Adjust OSABI functions and add unittests
Adjust #89280: * ELFOSABI_LINUX is a historical alias that should not be used in new code. readelf -h displays "UNIX - GNU" instead of "Linux". * "OS" is inappropriate. Some values are architecture-specific, e.g. ELFOSABI_ARM. * Drop lowercase, which seems a job of the caller. Add some unittests. Pull Request: #90270
1 parent 326657f commit a5cc951

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,11 +1939,12 @@ uint16_t convertArchNameToEMachine(StringRef Arch);
19391939
/// Convert an ELF's e_machine value into an architecture name.
19401940
StringRef convertEMachineToArchName(uint16_t EMachine);
19411941

1942-
/// Convert a OS into ELF's EI_OSABI value.
1943-
uint8_t convertOSToOSAbi(StringRef OS);
1942+
// Convert a lowercase string identifier into an OSABI value.
1943+
uint8_t convertNameToOSABI(StringRef Name);
19441944

1945-
/// Convert an ELF's e_machine value into an architecture name.
1946-
StringRef convertOSAbiToOS(uint8_t OSAbi);
1945+
// Convert an OSABI value into a string that identifies the OS- or ABI-
1946+
// specific ELF extension.
1947+
StringRef convertOSABIToName(uint8_t OSABI);
19471948

19481949
} // end namespace ELF
19491950
} // end namespace llvm

llvm/lib/BinaryFormat/ELF.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,11 @@ StringRef ELF::convertEMachineToArchName(uint16_t EMachine) {
568568
}
569569
}
570570

571-
uint8_t ELF::convertOSToOSAbi(StringRef OS) {
572-
std::string LowerOS = OS.lower();
573-
return StringSwitch<uint16_t>(LowerOS)
571+
uint8_t ELF::convertNameToOSABI(StringRef Name) {
572+
return StringSwitch<uint16_t>(Name)
574573
.StartsWith("hpux", ELFOSABI_HPUX)
575574
.StartsWith("netbsd", ELFOSABI_NETBSD)
576-
.StartsWith("linux", ELFOSABI_LINUX)
575+
.StartsWith("gnu", ELFOSABI_GNU)
577576
.StartsWith("hurd", ELFOSABI_HURD)
578577
.StartsWith("solaris", ELFOSABI_SOLARIS)
579578
.StartsWith("aix", ELFOSABI_AIX)
@@ -597,14 +596,14 @@ uint8_t ELF::convertOSToOSAbi(StringRef OS) {
597596
.Default(ELFOSABI_NONE);
598597
}
599598

600-
StringRef ELF::convertOSAbiToOS(uint8_t OSAbi) {
601-
switch (OSAbi) {
599+
StringRef ELF::convertOSABIToName(uint8_t OSABI) {
600+
switch (OSABI) {
602601
case ELFOSABI_HPUX:
603602
return "hpux";
604603
case ELFOSABI_NETBSD:
605604
return "netbsd";
606-
case ELFOSABI_LINUX:
607-
return "linux";
605+
case ELFOSABI_GNU:
606+
return "gnu";
608607
case ELFOSABI_HURD:
609608
return "hurd";
610609
case ELFOSABI_SOLARIS:

llvm/unittests/BinaryFormat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55

66
add_llvm_unittest(BinaryFormatTests
77
DwarfTest.cpp
8+
ELFTest.cpp
89
MachOTest.cpp
910
MsgPackDocumentTest.cpp
1011
MsgPackReaderTest.cpp
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===- ELFTest.cpp --------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/BinaryFormat/ELF.h"
10+
#include "gtest/gtest.h"
11+
12+
using namespace llvm;
13+
using namespace llvm::ELF;
14+
15+
namespace {
16+
TEST(ELFTest, OSABI) {
17+
EXPECT_EQ(ELFOSABI_GNU, convertNameToOSABI("gnu"));
18+
EXPECT_EQ(ELFOSABI_FREEBSD, convertNameToOSABI("freebsd"));
19+
EXPECT_EQ(ELFOSABI_STANDALONE, convertNameToOSABI("standalone"));
20+
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI("none"));
21+
// Test unrecognized strings.
22+
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI(""));
23+
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI("linux"));
24+
25+
EXPECT_EQ("gnu", convertOSABIToName(ELFOSABI_GNU));
26+
EXPECT_EQ("freebsd", convertOSABIToName(ELFOSABI_FREEBSD));
27+
EXPECT_EQ("standalone", convertOSABIToName(ELFOSABI_STANDALONE));
28+
EXPECT_EQ("none", convertOSABIToName(ELFOSABI_NONE));
29+
// Test unrecognized values.
30+
EXPECT_EQ("none", convertOSABIToName(0xfe));
31+
}
32+
} // namespace

0 commit comments

Comments
 (0)