[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:
Fangrui Song
2024-04-29 13:11:58 -07:00
committed by GitHub
parent 326657f567
commit a5cc95147e
4 changed files with 45 additions and 12 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(BinaryFormatTests
DwarfTest.cpp
ELFTest.cpp
MachOTest.cpp
MsgPackDocumentTest.cpp
MsgPackReaderTest.cpp

View 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