[llvm] a5cc951 - [BinaryFormat] Adjust OSABI functions and add unittests

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 29 13:12:02 PDT 2024


Author: Fangrui Song
Date: 2024-04-29T13:11:58-07:00
New Revision: a5cc95147ed549e850b4e9641e84641e781e0ceb

URL: https://github.com/llvm/llvm-project/commit/a5cc95147ed549e850b4e9641e84641e781e0ceb
DIFF: https://github.com/llvm/llvm-project/commit/a5cc95147ed549e850b4e9641e84641e781e0ceb.diff

LOG: [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

Added: 
    llvm/unittests/BinaryFormat/ELFTest.cpp

Modified: 
    llvm/include/llvm/BinaryFormat/ELF.h
    llvm/lib/BinaryFormat/ELF.cpp
    llvm/unittests/BinaryFormat/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 56b5d4e399c636..f296acc2ca4bb9 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -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

diff  --git a/llvm/lib/BinaryFormat/ELF.cpp b/llvm/lib/BinaryFormat/ELF.cpp
index 8c10ed1a980bbc..9878f5769087ea 100644
--- a/llvm/lib/BinaryFormat/ELF.cpp
+++ b/llvm/lib/BinaryFormat/ELF.cpp
@@ -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:

diff  --git a/llvm/unittests/BinaryFormat/CMakeLists.txt b/llvm/unittests/BinaryFormat/CMakeLists.txt
index f0c42a0dd02b8e..40d3bc4dca0b66 100644
--- a/llvm/unittests/BinaryFormat/CMakeLists.txt
+++ b/llvm/unittests/BinaryFormat/CMakeLists.txt
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_unittest(BinaryFormatTests
   DwarfTest.cpp
+  ELFTest.cpp
   MachOTest.cpp
   MsgPackDocumentTest.cpp
   MsgPackReaderTest.cpp

diff  --git a/llvm/unittests/BinaryFormat/ELFTest.cpp b/llvm/unittests/BinaryFormat/ELFTest.cpp
new file mode 100644
index 00000000000000..5dbf6ff8d5c900
--- /dev/null
+++ b/llvm/unittests/BinaryFormat/ELFTest.cpp
@@ -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


        


More information about the llvm-commits mailing list