Debian Codegraph @

libcextract/DscFileGenerator.cpp

raw
1 //===- DscFileGenerator.cpp - Generate the libpulp .dsc file ----*- 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 /// Generate the libulp .dsc file for a single userspace livepatch.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #include "DscFileGenerator.hh"
17 #include "PrettyPrint.hh"
18 #include "Error.hh"
19 #include "LLVMMisc.hh"
20
21 #include <stdexcept>
22
23 DscFileGenerator::DscFileGenerator(
24 const std::string &output,
25 ASTUnit *ast,
26 const std::vector<std::string> &funcs_to_extract,
27 const std::vector<ExternalizerLogEntry> &log,
28 InlineAnalysis &ia)
29 : OutputPath(output),
30 Out(OutputPath, EC),
31 AST(ast),
32 FuncsToExtract(funcs_to_extract),
33 ExternalizerLog(log),
34 IA(ia)
35 {
36 std::error_condition ok;
37 if (EC != ok) {
38 throw std::runtime_error("unable to open file " + OutputPath + " for writing: " + EC.message());
39 }
40
41 Run_Analysis();
42 }
43
44 void DscFileGenerator::Target_ELF(void)
45 {
46 if (IA.Have_Debuginfo()) {
47 Out << "LIVEPATH_CONTAINER"
48 << "\n@" << IA.Get_Debuginfo_Path();
49 } else {
50 Out << "LIVEPATH_CONTAINER"
51 << "\n@LIVEPATCH_TARGET";
52
53 DiagsClass::Emit_Warn("No target ELF given. Generated libpulp .dsc is incomplete.");
54 }
55 }
56
57 void DscFileGenerator::Global_Functions(void)
58 {
59 TranslationUnitDecl *tu = AST->getASTContext().getTranslationUnitDecl();
60 IdentifierTable &idtbl = AST->getPreprocessor().getIdentifierTable();
61
62 bool have_renamed_symbols = false;
63 for (const ExternalizerLogEntry &entry : ExternalizerLog) {
64 if (entry.Type == ExternalizationType::RENAME) {
65 have_renamed_symbols = true;
66 break;
67 }
68 }
69
70 if (have_renamed_symbols) {
71 for (const ExternalizerLogEntry &entry : ExternalizerLog) {
72 if (entry.Type == ExternalizationType::RENAME) {
73
74 DeclContext::lookup_result decls = tu->lookup(
75 DeclarationName(&idtbl.get(entry.NewName)));
76 if (decls.empty()) {
77 throw std::runtime_error("Unable to find symbol " + entry.NewName);
78 }
79 Out << '\n' << entry.OldName << ":" << entry.NewName;
80 }
81 }
82 } else {
83 for (const std::string &func_name : FuncsToExtract) {
84 DeclContext::lookup_result decls = tu->lookup(
85 DeclarationName(&idtbl.get(func_name)));
86
87 if (decls.empty()) {
88 throw std::runtime_error("Unable to find symbol " + func_name + " in the AST");
89 }
90 Out << '\n' << func_name << ":" << func_name;
91 }
92 }
93 }
94
95 static bool Is_TLS(const DeclContext::lookup_result &decls)
96 {
97 bool is_tls = false;
98
99 for (auto it = decls.begin(); it != decls.end(); ++it) {
100 if (VarDecl *vdecl = dyn_cast<VarDecl>(*it)) {
101 is_tls |= Is_TLS(vdecl);
102 }
103 }
104
105 return is_tls;
106 }
107
108 static std::string Get_TLS_Token(bool predicate)
109 {
110 return predicate == true ? "%" : "";
111 }
112
113 void DscFileGenerator::Local_Symbols(void)
114 {
115 TranslationUnitDecl *tu = AST->getASTContext().getTranslationUnitDecl();
116 IdentifierTable &idtbl = AST->getPreprocessor().getIdentifierTable();
117
118 for (const ExternalizerLogEntry &entry : ExternalizerLog) {
119 if (entry.Type == ExternalizationType::STRONG) {
120 DeclContext::lookup_result decls = tu->lookup(
121 DeclarationName(&idtbl.get(entry.NewName)));
122 if (decls.empty()) {
123 throw std::runtime_error("Unable to find symbol " + entry.NewName +
124 " in the AST");
125 }
126 Out << "\n#" << Get_TLS_Token(Is_TLS(decls)) << entry.OldName << ":" <<
127 entry.NewName;
128 std::string mod = IA.Get_Symbol_Module(entry.OldName);
129 if (!mod.empty())
130 Out << ":" << mod;
131 }
132 }
133 }
134
135 void DscFileGenerator::Run_Analysis(void)
136 {
137 Target_ELF();
138 Global_Functions();
139 Local_Symbols();
140 }