xref: /aosp_15_r20/external/stg/elf_loader.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*-
3*9e3b08aeSAndroid Build Coastguard Worker //
4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2022 Google LLC
5*9e3b08aeSAndroid Build Coastguard Worker //
6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the
8*9e3b08aeSAndroid Build Coastguard Worker // License.  You may obtain a copy of the License at
9*9e3b08aeSAndroid Build Coastguard Worker //
10*9e3b08aeSAndroid Build Coastguard Worker //     https://llvm.org/LICENSE.txt
11*9e3b08aeSAndroid Build Coastguard Worker //
12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License.
17*9e3b08aeSAndroid Build Coastguard Worker //
18*9e3b08aeSAndroid Build Coastguard Worker // Author: Aleksei Vetrov
19*9e3b08aeSAndroid Build Coastguard Worker 
20*9e3b08aeSAndroid Build Coastguard Worker #ifndef STG_ELF_LOADER_H_
21*9e3b08aeSAndroid Build Coastguard Worker #define STG_ELF_LOADER_H_
22*9e3b08aeSAndroid Build Coastguard Worker 
23*9e3b08aeSAndroid Build Coastguard Worker #include <libelf.h>
24*9e3b08aeSAndroid Build Coastguard Worker 
25*9e3b08aeSAndroid Build Coastguard Worker #include <cstddef>
26*9e3b08aeSAndroid Build Coastguard Worker #include <cstdint>
27*9e3b08aeSAndroid Build Coastguard Worker #include <ostream>
28*9e3b08aeSAndroid Build Coastguard Worker #include <string_view>
29*9e3b08aeSAndroid Build Coastguard Worker #include <vector>
30*9e3b08aeSAndroid Build Coastguard Worker 
31*9e3b08aeSAndroid Build Coastguard Worker #include "graph.h"
32*9e3b08aeSAndroid Build Coastguard Worker 
33*9e3b08aeSAndroid Build Coastguard Worker namespace stg {
34*9e3b08aeSAndroid Build Coastguard Worker namespace elf {
35*9e3b08aeSAndroid Build Coastguard Worker 
36*9e3b08aeSAndroid Build Coastguard Worker struct SymbolTableEntry {
37*9e3b08aeSAndroid Build Coastguard Worker   enum class SymbolType {
38*9e3b08aeSAndroid Build Coastguard Worker     NOTYPE = 0,
39*9e3b08aeSAndroid Build Coastguard Worker     OBJECT,
40*9e3b08aeSAndroid Build Coastguard Worker     FUNCTION,
41*9e3b08aeSAndroid Build Coastguard Worker     SECTION,
42*9e3b08aeSAndroid Build Coastguard Worker     FILE,
43*9e3b08aeSAndroid Build Coastguard Worker     COMMON,
44*9e3b08aeSAndroid Build Coastguard Worker     TLS,
45*9e3b08aeSAndroid Build Coastguard Worker     GNU_IFUNC
46*9e3b08aeSAndroid Build Coastguard Worker   };
47*9e3b08aeSAndroid Build Coastguard Worker 
48*9e3b08aeSAndroid Build Coastguard Worker   enum class ValueType {
49*9e3b08aeSAndroid Build Coastguard Worker     UNDEFINED = 0,
50*9e3b08aeSAndroid Build Coastguard Worker     ABSOLUTE,
51*9e3b08aeSAndroid Build Coastguard Worker     COMMON,
52*9e3b08aeSAndroid Build Coastguard Worker     RELATIVE_TO_SECTION,
53*9e3b08aeSAndroid Build Coastguard Worker   };
54*9e3b08aeSAndroid Build Coastguard Worker 
55*9e3b08aeSAndroid Build Coastguard Worker   using Binding = ElfSymbol::Binding;
56*9e3b08aeSAndroid Build Coastguard Worker   using Visibility = ElfSymbol::Visibility;
57*9e3b08aeSAndroid Build Coastguard Worker 
58*9e3b08aeSAndroid Build Coastguard Worker   std::string_view name;
59*9e3b08aeSAndroid Build Coastguard Worker   uint64_t value;
60*9e3b08aeSAndroid Build Coastguard Worker   uint64_t size;
61*9e3b08aeSAndroid Build Coastguard Worker   SymbolType symbol_type;
62*9e3b08aeSAndroid Build Coastguard Worker   Binding binding;
63*9e3b08aeSAndroid Build Coastguard Worker   Visibility visibility;
64*9e3b08aeSAndroid Build Coastguard Worker   size_t section_index;
65*9e3b08aeSAndroid Build Coastguard Worker   ValueType value_type;
66*9e3b08aeSAndroid Build Coastguard Worker };
67*9e3b08aeSAndroid Build Coastguard Worker 
68*9e3b08aeSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, SymbolTableEntry::SymbolType type);
69*9e3b08aeSAndroid Build Coastguard Worker 
70*9e3b08aeSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, SymbolTableEntry::ValueType type);
71*9e3b08aeSAndroid Build Coastguard Worker 
72*9e3b08aeSAndroid Build Coastguard Worker std::string_view UnwrapCFISymbolName(std::string_view cfi_name);
73*9e3b08aeSAndroid Build Coastguard Worker 
74*9e3b08aeSAndroid Build Coastguard Worker class ElfLoader final {
75*9e3b08aeSAndroid Build Coastguard Worker  public:
76*9e3b08aeSAndroid Build Coastguard Worker   explicit ElfLoader(Elf& elf);
77*9e3b08aeSAndroid Build Coastguard Worker 
78*9e3b08aeSAndroid Build Coastguard Worker   std::string_view GetSectionRawData(const char* name) const;
79*9e3b08aeSAndroid Build Coastguard Worker   std::vector<SymbolTableEntry> GetElfSymbols() const;
80*9e3b08aeSAndroid Build Coastguard Worker   std::vector<SymbolTableEntry> GetCFISymbols() const;
81*9e3b08aeSAndroid Build Coastguard Worker   ElfSymbol::CRC GetElfSymbolCRC(const SymbolTableEntry& symbol) const;
82*9e3b08aeSAndroid Build Coastguard Worker   std::string_view GetElfSymbolNamespace(const SymbolTableEntry& symbol) const;
83*9e3b08aeSAndroid Build Coastguard Worker   size_t GetAbsoluteAddress(const SymbolTableEntry& symbol) const;
84*9e3b08aeSAndroid Build Coastguard Worker   bool IsLinuxKernelBinary() const;
85*9e3b08aeSAndroid Build Coastguard Worker   bool IsLittleEndianBinary() const;
86*9e3b08aeSAndroid Build Coastguard Worker 
87*9e3b08aeSAndroid Build Coastguard Worker  private:
88*9e3b08aeSAndroid Build Coastguard Worker   void InitializeElfInformation();
89*9e3b08aeSAndroid Build Coastguard Worker 
90*9e3b08aeSAndroid Build Coastguard Worker   Elf* elf_;
91*9e3b08aeSAndroid Build Coastguard Worker   bool is_linux_kernel_binary_;
92*9e3b08aeSAndroid Build Coastguard Worker   bool is_relocatable_;
93*9e3b08aeSAndroid Build Coastguard Worker   bool is_little_endian_binary_;
94*9e3b08aeSAndroid Build Coastguard Worker };
95*9e3b08aeSAndroid Build Coastguard Worker 
96*9e3b08aeSAndroid Build Coastguard Worker }  // namespace elf
97*9e3b08aeSAndroid Build Coastguard Worker }  // namespace stg
98*9e3b08aeSAndroid Build Coastguard Worker 
99*9e3b08aeSAndroid Build Coastguard Worker #endif  // STG_ELF_LOADER_H_
100