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

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 14:06:06 PDT 2024


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

Adjust #89280:
ELFOSABI_LINUX is a historical alias that should not be used in new
code. readelf -h displays "UNIX - GNU" instead of "Linux". Add some
unittests.


>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] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=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



More information about the llvm-commits mailing list