Fangrui Song a5cc95147e
[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
2024-04-29 13:11:58 -07:00

33 lines
1.2 KiB
C++

//===- 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