1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License. You may obtain a copy of the License at
9 //
10 // https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Aleksei Vetrov
19
20 #include <string_view>
21
22 #include <catch2/catch.hpp>
23 #include "elf_loader.h"
24 #include "elf_reader.h"
25
26 namespace Test {
27
28 using SymbolTable = stg::elf::internal::SymbolTable;
29 using SymbolTableEntry = stg::elf::SymbolTableEntry;
30
MakeSymbol(std::string_view name)31 SymbolTableEntry MakeSymbol(std::string_view name) {
32 return {
33 .name = name,
34 .value = 0,
35 .size = 0,
36 .symbol_type = SymbolTableEntry::SymbolType::OBJECT,
37 .binding = SymbolTableEntry::Binding::GLOBAL,
38 .visibility = SymbolTableEntry::Visibility::DEFAULT,
39 .section_index = 0,
40 .value_type = SymbolTableEntry::ValueType::RELATIVE_TO_SECTION,
41 };
42 }
43
44
45 TEST_CASE("GetKsymtabSymbols") {
46 const SymbolTable all_symbols = {
47 MakeSymbol("foo"),
48 MakeSymbol("__ksymtab_foo"),
49 MakeSymbol("bar"),
50 };
51 const auto ksymtab = stg::elf::internal::GetKsymtabSymbols(all_symbols);
52 REQUIRE(ksymtab.size() == 1);
53 CHECK(*ksymtab.begin() == "foo");
54 }
55
56 } // namespace Test
57