| 1 |
//===- SymversParser.cpp - Parse linux Modules.symvers files ---*- C++ --*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Parse linux Modules.symvers files. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/** SymversParse |
| 15 |
* |
| 16 |
* The Module.symvers file contains all exported functions from the kernel |
| 17 |
* (vmlinux) and module that were built with the kernel. Parsing these functions |
| 18 |
* helps to check whether a symbol needs to be externalized or not. |
| 19 |
* |
| 20 |
* In the case of a symbol that exists in a different module, we need to |
| 21 |
* externalize it, since a kernel livepatch module cannot depend on another |
| 22 |
* modules. |
| 23 |
* |
| 24 |
* If the symbol resides in the vmlinux, we don't need to externalize it, since |
| 25 |
* the symbol will always be there. |
| 26 |
* |
| 27 |
* Author: Marcos Paulo de Souza |
| 28 |
*/ |
| 29 |
|
| 30 |
#pragma once |
| 31 |
|
| 32 |
#include "Parser.hh" |
| 33 |
|
| 34 |
#include <cassert> |
| 35 |
#include <string> |
| 36 |
#include <iterator> |
| 37 |
#include <unordered_map> |
| 38 |
|
| 39 |
/** @brief Kernel symbol |
| 40 |
* |
| 41 |
* The symbol is question is always related to a module, or to vmlinux |
| 42 |
*/ |
| 43 |
class Symbol |
| 44 |
{ |
| 45 |
public: |
| 46 |
Symbol(const std::string &name, const std::string &mod) |
| 47 |
: name(name), |
| 48 |
mod(mod) |
| 49 |
{ |
| 50 |
// Ensure that we have a Symbol name |
| 51 |
assert(!name.empty()); |
| 52 |
} |
| 53 |
|
| 54 |
std::string GetName() |
| 55 |
{ |
| 56 |
return name; |
| 57 |
} |
| 58 |
|
| 59 |
const std::string &GetName() const |
| 60 |
{ |
| 61 |
return name; |
| 62 |
} |
| 63 |
|
| 64 |
std::string GetModule() |
| 65 |
{ |
| 66 |
return mod; |
| 67 |
} |
| 68 |
|
| 69 |
const std::string &GetModule() const |
| 70 |
{ |
| 71 |
return mod; |
| 72 |
} |
| 73 |
|
| 74 |
private: |
| 75 |
std::string name; |
| 76 |
std::string mod; |
| 77 |
}; |
| 78 |
|
| 79 |
/** Symvers -- Parse and cache Module.symvers symbols content for fast access. |
| 80 |
* |
| 81 |
* Cache the symbols and modules found for fast access. |
| 82 |
**/ |
| 83 |
class Symvers : public Parser |
| 84 |
{ |
| 85 |
public: |
| 86 |
Symvers(const std::string &path); |
| 87 |
|
| 88 |
void Parse(); |
| 89 |
|
| 90 |
/* Get symbol if available in the Symvers file. Or 0 if not available. */ |
| 91 |
inline bool Symbol_Exists(const std::string &sym) |
| 92 |
{ |
| 93 |
auto container = map.find(sym); |
| 94 |
return container != map.end(); |
| 95 |
} |
| 96 |
|
| 97 |
std::vector<std::string> Get_All_Symbols() |
| 98 |
{ |
| 99 |
std::vector<std::string> vec; |
| 100 |
|
| 101 |
for (auto sym : map) |
| 102 |
vec.push_back(sym.first); |
| 103 |
|
| 104 |
return vec; |
| 105 |
} |
| 106 |
|
| 107 |
/* Get symbol if available in the Symvers file. Or 0 if not available. */ |
| 108 |
std::string Get_Symbol_Module(const std::string &sym) |
| 109 |
{ |
| 110 |
auto container = map.find(sym); |
| 111 |
// Symbol not found |
| 112 |
if (container == map.end()) |
| 113 |
return {}; |
| 114 |
return container->second; |
| 115 |
} |
| 116 |
|
| 117 |
// Externalize the symbols that are not part of vmlinux |
| 118 |
bool Needs_Externalization(const std::string &sym_mod) |
| 119 |
{ |
| 120 |
return sym_mod != "vmlinux"; |
| 121 |
} |
| 122 |
|
| 123 |
/* Cache the symbols from a module. */ |
| 124 |
inline void Insert_Symbols_Into_Hash(const Symbol &sym) |
| 125 |
{ |
| 126 |
map[sym.GetName()] = sym.GetModule(); |
| 127 |
} |
| 128 |
|
| 129 |
/** Dump for debugging reasons. */ |
| 130 |
void Dump(void); |
| 131 |
|
| 132 |
/** Dump for debugging reasons. */ |
| 133 |
void Dump_Cache(void); |
| 134 |
|
| 135 |
private: |
| 136 |
/** Hash symbols from symtab into their value. */ |
| 137 |
std::unordered_map<std::string, std::string> map; |
| 138 |
}; |