| 1 |
//===- InlineAnalysis.hh - Find out which functions got inlined -*- 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 |
/// Use the information from debuginfo, ipa-clones and symvers to decide if |
| 11 |
/// symbol is inlined, private or public in the target livepatch binary. |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
/* Author: Giuliano Belinassi */ |
| 16 |
|
| 17 |
/** InlineAnalysis: Do an analysis to check if symbols are inlined. |
| 18 |
* |
| 19 |
* This class works by merging content from the GCC ipa-clones dumps and the |
| 20 |
* debuginfo of the program, the first content being required and the second |
| 21 |
* being optional, only to improve the analysis quality. |
| 22 |
*/ |
| 23 |
|
| 24 |
#pragma once |
| 25 |
|
| 26 |
#include "ElfCXX.hh" |
| 27 |
#include "IpaClonesParser.hh" |
| 28 |
#include "SymversParser.hh" |
| 29 |
|
| 30 |
#include <set> |
| 31 |
#include <string> |
| 32 |
#include <vector> |
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
struct IpaCloneNode; |
| 36 |
|
| 37 |
enum ExternalizationType { |
| 38 |
NONE = 0, |
| 39 |
WEAK, |
| 40 |
STRONG, |
| 41 |
RENAME=100, /* Used to indicate that the function only requires a rename. */ |
| 42 |
}; |
| 43 |
|
| 44 |
class InlineAnalysis |
| 45 |
{ |
| 46 |
public: |
| 47 |
/** Build the analysis class. elf_path can be NULL if there is no debuginfo |
| 48 |
available, and ipaclone_path can be a directory full of many ipa-clones |
| 49 |
generated through LTO or not. Symvers can be NULL is we are creating a |
| 50 |
userspace livepatch */ |
| 51 |
InlineAnalysis(const std::vector<std::string> &elfs_path, |
| 52 |
const char *ipaclone_path, const char *symvers_path, |
| 53 |
bool is_kernel); |
| 54 |
|
| 55 |
InlineAnalysis(const char *elf_path, const char *ipaclone_path, |
| 56 |
const char *symvers_path, bool is_kernel) |
| 57 |
: InlineAnalysis(elf_path == nullptr ? |
| 58 |
std::vector<std::string>() : |
| 59 |
std::vector<std::string>({elf_path}), |
| 60 |
ipaclone_path, symvers_path, is_kernel) |
| 61 |
{} |
| 62 |
|
| 63 |
~InlineAnalysis(void); |
| 64 |
|
| 65 |
/** Get a set of all functions that are inlined into `asm_name`. */ |
| 66 |
std::set<std::string> Get_Inline_Closure_Of_Symbol(const std::string &asm_name); |
| 67 |
|
| 68 |
/** Get a set of all functions that are inlined into the set of functions `syms`. */ |
| 69 |
std::set<std::string> Get_Inline_Closure_Of_Symbols(const std::vector<std::string> &syms); |
| 70 |
|
| 71 |
/** Get a set of all functions where a given function was inlined. */ |
| 72 |
std::set<std::string> Get_Where_Symbol_Is_Inlined(const std::string &asm_name); |
| 73 |
|
| 74 |
/** Get a set of all functions where a given set of functions was inlined. */ |
| 75 |
std::set<std::string> Get_Where_Symbols_Is_Inlined(const std::vector<std::string> &syms); |
| 76 |
|
| 77 |
/** Build a graphviz .dot file showing where the `functions` got inlined. */ |
| 78 |
void Get_Graphviz_Of_Inlines(const std::vector<std::string> &functions, |
| 79 |
const char *output_path); |
| 80 |
|
| 81 |
/** Build a graphviz .dot file showing which functions got inlined into |
| 82 |
`functions`. */ |
| 83 |
void Get_Graphviz_Of_Inline_Closure(const std::vector<std::string> &functions, |
| 84 |
const char *output_path); |
| 85 |
|
| 86 |
/** Get the ELF info of a symbol. */ |
| 87 |
|
| 88 |
std::pair<unsigned char, ElfSymtabType> |
| 89 |
Get_Symbol_Info(const std::string &sym); |
| 90 |
|
| 91 |
ExternalizationType Needs_Externalization(const std::string &sym); |
| 92 |
|
| 93 |
/** Check if symbol is externally visible. */ |
| 94 |
bool Is_Externally_Visible(const std::string &sym); |
| 95 |
|
| 96 |
/* Print the symbol set in a table-like format, for terminal output. */ |
| 97 |
void Print_Symbol_Set(const std::set<std::string> &set, bool csv=false, FILE *out=stdout); |
| 98 |
|
| 99 |
/* Get all symbols available to this class. Look into debuginfo and |
| 100 |
ipa-clones. */ |
| 101 |
std::set<std::string> Get_All_Symbols(void); |
| 102 |
|
| 103 |
/** True if this class was built with debuginfo enabled. */ |
| 104 |
inline bool Have_Debuginfo(void) |
| 105 |
{ |
| 106 |
return ElfCache; |
| 107 |
} |
| 108 |
|
| 109 |
inline const std::string &Get_Debuginfo_Path(void) |
| 110 |
{ |
| 111 |
if (ElfCache) { |
| 112 |
return ElfCache->Get_Debuginfo_Path(); |
| 113 |
} |
| 114 |
|
| 115 |
static const std::string empty; |
| 116 |
return empty; |
| 117 |
} |
| 118 |
|
| 119 |
inline const std::string &Get_Object_Path(void) |
| 120 |
{ |
| 121 |
if (ElfCache) { |
| 122 |
return ElfCache->Get_Object_Path(); |
| 123 |
} |
| 124 |
|
| 125 |
static const std::string empty; |
| 126 |
return empty; |
| 127 |
} |
| 128 |
|
| 129 |
/** Check if we have Ipa-clones information. */ |
| 130 |
inline bool Have_IPA(void) |
| 131 |
{ |
| 132 |
return Ipa; |
| 133 |
} |
| 134 |
|
| 135 |
inline bool Have_Symvers(void) |
| 136 |
{ |
| 137 |
return Symv; |
| 138 |
} |
| 139 |
|
| 140 |
inline bool Can_Decide_Visibility(void) |
| 141 |
{ |
| 142 |
return Have_Debuginfo() || Have_Symvers(); |
| 143 |
} |
| 144 |
|
| 145 |
/** Dump for debugging concerns. */ |
| 146 |
void Dump(void); |
| 147 |
|
| 148 |
|
| 149 |
/** Demangle C++ symbol. |
| 150 |
|
| 151 |
C++ symbols are mangled in order to be unique for the linker. For example, |
| 152 |
given: |
| 153 |
_ZNK7Point3D5Get_ZEv |
| 154 |
turns into: |
| 155 |
Point3D::Get_Z() const |
| 156 |
|
| 157 |
NOTE: returned value must be free. |
| 158 |
*/ |
| 159 |
static const char *Demangle_Symbol(const char *symbol); |
| 160 |
static inline const char *Demangle_Symbol(const std::string &symbol) |
| 161 |
{ |
| 162 |
return Demangle_Symbol(symbol.c_str()); |
| 163 |
} |
| 164 |
|
| 165 |
std::string Get_Symbol_Module(std::string sym); |
| 166 |
|
| 167 |
private: |
| 168 |
/** Put color information in the graphviz .DOT file. */ |
| 169 |
void Print_Node_Colors(const std::set<IpaCloneNode *> &set, FILE *fp); |
| 170 |
|
| 171 |
ElfSymbolCache *ElfCache; |
| 172 |
IpaClones *Ipa; |
| 173 |
Symvers *Symv; |
| 174 |
bool Kernel; |
| 175 |
}; |