[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: https://github.com/llvm/llvm-project/pull/90270
This commit is contained in:
@@ -1939,11 +1939,12 @@ uint16_t convertArchNameToEMachine(StringRef Arch);
|
||||
/// Convert an ELF's e_machine value into an architecture name.
|
||||
StringRef convertEMachineToArchName(uint16_t EMachine);
|
||||
|
||||
/// Convert a OS into ELF's EI_OSABI value.
|
||||
uint8_t convertOSToOSAbi(StringRef OS);
|
||||
// Convert a lowercase string identifier into an OSABI value.
|
||||
uint8_t convertNameToOSABI(StringRef Name);
|
||||
|
||||
/// Convert an ELF's e_machine value into an architecture name.
|
||||
StringRef convertOSAbiToOS(uint8_t OSAbi);
|
||||
// Convert an OSABI value into a string that identifies the OS- or ABI-
|
||||
// specific ELF extension.
|
||||
StringRef convertOSABIToName(uint8_t OSABI);
|
||||
|
||||
} // end namespace ELF
|
||||
} // end namespace llvm
|
||||
|
||||
@@ -568,12 +568,11 @@ StringRef ELF::convertEMachineToArchName(uint16_t EMachine) {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ELF::convertOSToOSAbi(StringRef OS) {
|
||||
std::string LowerOS = OS.lower();
|
||||
return StringSwitch<uint16_t>(LowerOS)
|
||||
uint8_t ELF::convertNameToOSABI(StringRef Name) {
|
||||
return StringSwitch<uint16_t>(Name)
|
||||
.StartsWith("hpux", ELFOSABI_HPUX)
|
||||
.StartsWith("netbsd", ELFOSABI_NETBSD)
|
||||
.StartsWith("linux", ELFOSABI_LINUX)
|
||||
.StartsWith("gnu", ELFOSABI_GNU)
|
||||
.StartsWith("hurd", ELFOSABI_HURD)
|
||||
.StartsWith("solaris", ELFOSABI_SOLARIS)
|
||||
.StartsWith("aix", ELFOSABI_AIX)
|
||||
@@ -597,14 +596,14 @@ uint8_t ELF::convertOSToOSAbi(StringRef OS) {
|
||||
.Default(ELFOSABI_NONE);
|
||||
}
|
||||
|
||||
StringRef ELF::convertOSAbiToOS(uint8_t OSAbi) {
|
||||
switch (OSAbi) {
|
||||
StringRef ELF::convertOSABIToName(uint8_t OSABI) {
|
||||
switch (OSABI) {
|
||||
case ELFOSABI_HPUX:
|
||||
return "hpux";
|
||||
case ELFOSABI_NETBSD:
|
||||
return "netbsd";
|
||||
case ELFOSABI_LINUX:
|
||||
return "linux";
|
||||
case ELFOSABI_GNU:
|
||||
return "gnu";
|
||||
case ELFOSABI_HURD:
|
||||
return "hurd";
|
||||
case ELFOSABI_SOLARIS:
|
||||
|
||||
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
|
||||
|
||||
add_llvm_unittest(BinaryFormatTests
|
||||
DwarfTest.cpp
|
||||
ELFTest.cpp
|
||||
MachOTest.cpp
|
||||
MsgPackDocumentTest.cpp
|
||||
MsgPackReaderTest.cpp
|
||||
|
||||
32
llvm/unittests/BinaryFormat/ELFTest.cpp
Normal file
32
llvm/unittests/BinaryFormat/ELFTest.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//===- ELFTest.cpp --------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/BinaryFormat/ELF.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace llvm;
|
||||
using namespace llvm::ELF;
|
||||
|
||||
namespace {
|
||||
TEST(ELFTest, OSABI) {
|
||||
EXPECT_EQ(ELFOSABI_GNU, convertNameToOSABI("gnu"));
|
||||
EXPECT_EQ(ELFOSABI_FREEBSD, convertNameToOSABI("freebsd"));
|
||||
EXPECT_EQ(ELFOSABI_STANDALONE, convertNameToOSABI("standalone"));
|
||||
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI("none"));
|
||||
// Test unrecognized strings.
|
||||
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI(""));
|
||||
EXPECT_EQ(ELFOSABI_NONE, convertNameToOSABI("linux"));
|
||||
|
||||
EXPECT_EQ("gnu", convertOSABIToName(ELFOSABI_GNU));
|
||||
EXPECT_EQ("freebsd", convertOSABIToName(ELFOSABI_FREEBSD));
|
||||
EXPECT_EQ("standalone", convertOSABIToName(ELFOSABI_STANDALONE));
|
||||
EXPECT_EQ("none", convertOSABIToName(ELFOSABI_NONE));
|
||||
// Test unrecognized values.
|
||||
EXPECT_EQ("none", convertOSABIToName(0xfe));
|
||||
}
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user