aboutsummaryrefslogtreecommitdiff
path: root/elf_loader.h
blob: 267915500a79d5f227636f35a77485864afada33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- mode: C++ -*-
//
// Copyright 2022 Google LLC
//
// Licensed under the Apache License v2.0 with LLVM Exceptions (the
// "License"); you may not use this file except in compliance with the
// License.  You may obtain a copy of the License at
//
//     https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Aleksei Vetrov

#ifndef STG_ELF_LOADER_H_
#define STG_ELF_LOADER_H_

#include <libelf.h>

#include <cstddef>
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>

#include "graph.h"

namespace stg {
namespace elf {

struct SymbolTableEntry {
  enum class SymbolType {
    NOTYPE = 0,
    OBJECT,
    FUNCTION,
    SECTION,
    FILE,
    COMMON,
    TLS,
    GNU_IFUNC
  };

  enum class ValueType {
    UNDEFINED = 0,
    ABSOLUTE,
    COMMON,
    RELATIVE_TO_SECTION,
  };

  using Binding = ElfSymbol::Binding;
  using Visibility = ElfSymbol::Visibility;

  std::string_view name;
  uint64_t value;
  uint64_t size;
  SymbolType symbol_type;
  Binding binding;
  Visibility visibility;
  size_t section_index;
  ValueType value_type;
};

std::ostream& operator<<(std::ostream& os, SymbolTableEntry::SymbolType type);

std::ostream& operator<<(std::ostream& os, SymbolTableEntry::ValueType type);

std::string_view UnwrapCFISymbolName(std::string_view cfi_name);

class ElfLoader final {
 public:
  explicit ElfLoader(Elf* elf, bool verbose = false);

  std::string_view GetBtfRawData() const;
  std::vector<SymbolTableEntry> GetElfSymbols() const;
  std::vector<SymbolTableEntry> GetCFISymbols() const;
  ElfSymbol::CRC GetElfSymbolCRC(const SymbolTableEntry& symbol) const;
  std::string_view GetElfSymbolNamespace(const SymbolTableEntry& symbol) const;
  size_t GetAbsoluteAddress(const SymbolTableEntry& symbol) const;
  bool IsLinuxKernelBinary() const;
  bool IsLittleEndianBinary() const;

 private:
  void InitializeElfInformation();

  const bool verbose_;
  Elf* elf_;
  bool is_linux_kernel_binary_;
  bool is_relocatable_;
  bool is_little_endian_binary_;
};

}  // namespace elf
}  // namespace stg

#endif  // STG_ELF_LOADER_H_