[llvm] [BinaryFormat] Adjust OSABI functions and add unittests (PR #90270)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 15:23:54 PDT 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/90270

>From 34228d1497a235664a2542ce0521bcb287fce162 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 26 Apr 2024 14:05:56 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 llvm/lib/BinaryFormat/ELF.cpp              |  6 ++--
 llvm/unittests/BinaryFormat/CMakeLists.txt |  1 +
 llvm/unittests/BinaryFormat/ELFTest.cpp    | 32 ++++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 llvm/unittests/BinaryFormat/ELFTest.cpp

diff --git a/llvm/lib/BinaryFormat/ELF.cpp b/llvm/lib/BinaryFormat/ELF.cpp
index 8c10ed1a980bbc..a07e5423a2ab66 100644
--- a/llvm/lib/BinaryFormat/ELF.cpp
+++ b/llvm/lib/BinaryFormat/ELF.cpp
@@ -573,7 +573,7 @@ uint8_t ELF::convertOSToOSAbi(StringRef OS) {
   return StringSwitch<uint16_t>(LowerOS)
       .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)
@@ -603,8 +603,8 @@ StringRef ELF::convertOSAbiToOS(uint8_t OSAbi) {
     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..29793bf99c6000
--- /dev/null
+++ b/llvm/unittests/BinaryFormat/ELFTest.cpp
@@ -0,0 +1,32 @@
+//===- ELFTest.cpp ----------------------------------------------*- C++ -*-===//
+//
+// 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, convertOSToOSAbi("gnu"));
+  EXPECT_EQ(ELFOSABI_FREEBSD, convertOSToOSAbi("freebsd"));
+  EXPECT_EQ(ELFOSABI_STANDALONE, convertOSToOSAbi("standalone"));
+  EXPECT_EQ(ELFOSABI_NONE, convertOSToOSAbi("none"));
+  // Test unrecognized strings.
+  EXPECT_EQ(ELFOSABI_NONE, convertOSToOSAbi(""));
+  EXPECT_EQ(ELFOSABI_NONE, convertOSToOSAbi("linux"));
+
+  EXPECT_EQ("gnu", convertOSAbiToOS(ELFOSABI_GNU));
+  EXPECT_EQ("freebsd", convertOSAbiToOS(ELFOSABI_FREEBSD));
+  EXPECT_EQ("standalone", convertOSAbiToOS(ELFOSABI_STANDALONE));
+  EXPECT_EQ("none", convertOSAbiToOS(ELFOSABI_NONE));
+  // Test unrecognized values.
+  EXPECT_EQ("none", convertOSAbiToOS(0xfe));
+}
+} // namespace

>From 0ca8e7c317ba2053f67ff4908c51be13f401cf89 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 26 Apr 2024 15:23:44 -0700
Subject: [PATCH 2/2] .

Created using spr 1.3.5-bogner
---
 llvm/include/llvm/BinaryFormat/ELF.h    |  9 +++++----
 llvm/lib/BinaryFormat/ELF.cpp           |  7 +++----
 llvm/unittests/BinaryFormat/ELFTest.cpp | 22 +++++++++++-----------
 3 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 56b5d4e399c636..6045af4eeb991a 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 a07e5423a2ab66..a95a65c8f25d88 100644
--- a/llvm/lib/BinaryFormat/ELF.cpp
+++ b/llvm/lib/BinaryFormat/ELF.cpp
@@ -568,9 +568,8 @@ 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("gnu", ELFOSABI_GNU)
@@ -597,7 +596,7 @@ uint8_t ELF::convertOSToOSAbi(StringRef OS) {
       .Default(ELFOSABI_NONE);
 }
 
-StringRef ELF::convertOSAbiToOS(uint8_t OSAbi) {
+StringRef ELF::convertOSAbiToName(uint8_t OSAbi) {
   switch (OSAbi) {
   case ELFOSABI_HPUX:
     return "hpux";
diff --git a/llvm/unittests/BinaryFormat/ELFTest.cpp b/llvm/unittests/BinaryFormat/ELFTest.cpp
index 29793bf99c6000..c0558ff0677ead 100644
--- a/llvm/unittests/BinaryFormat/ELFTest.cpp
+++ b/llvm/unittests/BinaryFormat/ELFTest.cpp
@@ -14,19 +14,19 @@ using namespace llvm::ELF;
 
 namespace {
 TEST(ELFTest, OSAbi) {
-  EXPECT_EQ(ELFOSABI_GNU, convertOSToOSAbi("gnu"));
-  EXPECT_EQ(ELFOSABI_FREEBSD, convertOSToOSAbi("freebsd"));
-  EXPECT_EQ(ELFOSABI_STANDALONE, convertOSToOSAbi("standalone"));
-  EXPECT_EQ(ELFOSABI_NONE, convertOSToOSAbi("none"));
+  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, convertOSToOSAbi(""));
-  EXPECT_EQ(ELFOSABI_NONE, convertOSToOSAbi("linux"));
+  EXPECT_EQ(ELFOSABI_NONE, convertNameToOSAbi(""));
+  EXPECT_EQ(ELFOSABI_NONE, convertNameToOSAbi("linux"));
 
-  EXPECT_EQ("gnu", convertOSAbiToOS(ELFOSABI_GNU));
-  EXPECT_EQ("freebsd", convertOSAbiToOS(ELFOSABI_FREEBSD));
-  EXPECT_EQ("standalone", convertOSAbiToOS(ELFOSABI_STANDALONE));
-  EXPECT_EQ("none", convertOSAbiToOS(ELFOSABI_NONE));
+  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", convertOSAbiToOS(0xfe));
+  EXPECT_EQ("none", convertOSAbiToName(0xfe));
 }
 } // namespace



More information about the llvm-commits mailing list