[llvm] [BinaryFormat] Adjust ELFOSABI_GNU and add unittests (PR #90270)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 26 14:06:36 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/90270.diff
3 Files Affected:
- (modified) llvm/lib/BinaryFormat/ELF.cpp (+3-3)
- (modified) llvm/unittests/BinaryFormat/CMakeLists.txt (+1)
- (added) llvm/unittests/BinaryFormat/ELFTest.cpp (+32)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/90270
More information about the llvm-commits
mailing list