xref: /aosp_15_r20/external/bcc/src/cc/frontends/clang/loader.h (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker /*
2*387f9dfdSAndroid Build Coastguard Worker  * Copyright (c) 2015 PLUMgrid, Inc.
3*387f9dfdSAndroid Build Coastguard Worker  *
4*387f9dfdSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*387f9dfdSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*387f9dfdSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*387f9dfdSAndroid Build Coastguard Worker  *
8*387f9dfdSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*387f9dfdSAndroid Build Coastguard Worker  *
10*387f9dfdSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*387f9dfdSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*387f9dfdSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*387f9dfdSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*387f9dfdSAndroid Build Coastguard Worker  * limitations under the License.
15*387f9dfdSAndroid Build Coastguard Worker  */
16*387f9dfdSAndroid Build Coastguard Worker 
17*387f9dfdSAndroid Build Coastguard Worker #pragma once
18*387f9dfdSAndroid Build Coastguard Worker 
19*387f9dfdSAndroid Build Coastguard Worker #include <clang/Frontend/CompilerInvocation.h>
20*387f9dfdSAndroid Build Coastguard Worker 
21*387f9dfdSAndroid Build Coastguard Worker #include <functional>
22*387f9dfdSAndroid Build Coastguard Worker #include <map>
23*387f9dfdSAndroid Build Coastguard Worker #include <memory>
24*387f9dfdSAndroid Build Coastguard Worker #include <string>
25*387f9dfdSAndroid Build Coastguard Worker 
26*387f9dfdSAndroid Build Coastguard Worker #include "table_storage.h"
27*387f9dfdSAndroid Build Coastguard Worker #include "vendor/optional.hpp"
28*387f9dfdSAndroid Build Coastguard Worker 
29*387f9dfdSAndroid Build Coastguard Worker using std::experimental::nullopt;
30*387f9dfdSAndroid Build Coastguard Worker using std::experimental::optional;
31*387f9dfdSAndroid Build Coastguard Worker 
32*387f9dfdSAndroid Build Coastguard Worker namespace llvm {
33*387f9dfdSAndroid Build Coastguard Worker class Module;
34*387f9dfdSAndroid Build Coastguard Worker class LLVMContext;
35*387f9dfdSAndroid Build Coastguard Worker class MemoryBuffer;
36*387f9dfdSAndroid Build Coastguard Worker }
37*387f9dfdSAndroid Build Coastguard Worker 
38*387f9dfdSAndroid Build Coastguard Worker namespace ebpf {
39*387f9dfdSAndroid Build Coastguard Worker 
40*387f9dfdSAndroid Build Coastguard Worker struct FuncInfo {
41*387f9dfdSAndroid Build Coastguard Worker   uint8_t *start_ = nullptr;
42*387f9dfdSAndroid Build Coastguard Worker   size_t size_ = 0;
43*387f9dfdSAndroid Build Coastguard Worker   std::string section_;
44*387f9dfdSAndroid Build Coastguard Worker   std::string src_;
45*387f9dfdSAndroid Build Coastguard Worker   std::string src_rewritten_;
46*387f9dfdSAndroid Build Coastguard Worker   // dummy constructor so emplace() works
FuncInfoFuncInfo47*387f9dfdSAndroid Build Coastguard Worker   FuncInfo(int i) {}
48*387f9dfdSAndroid Build Coastguard Worker };
49*387f9dfdSAndroid Build Coastguard Worker 
50*387f9dfdSAndroid Build Coastguard Worker class ProgFuncInfo {
51*387f9dfdSAndroid Build Coastguard Worker  public:
ProgFuncInfo()52*387f9dfdSAndroid Build Coastguard Worker   ProgFuncInfo() {}
clear()53*387f9dfdSAndroid Build Coastguard Worker   void clear() {
54*387f9dfdSAndroid Build Coastguard Worker     funcs_.clear();
55*387f9dfdSAndroid Build Coastguard Worker     func_idx_.clear();
56*387f9dfdSAndroid Build Coastguard Worker   }
57*387f9dfdSAndroid Build Coastguard Worker   optional<FuncInfo &> get_func(std::string name);
58*387f9dfdSAndroid Build Coastguard Worker   optional<FuncInfo &> get_func(size_t id);
59*387f9dfdSAndroid Build Coastguard Worker   optional<std::string &> func_name(size_t id);
60*387f9dfdSAndroid Build Coastguard Worker   optional<FuncInfo &> add_func(std::string name);
num_funcs()61*387f9dfdSAndroid Build Coastguard Worker   size_t num_funcs() { return funcs_.size(); }
62*387f9dfdSAndroid Build Coastguard Worker   void for_each_func(std::function<void(std::string, FuncInfo &)> cb);
63*387f9dfdSAndroid Build Coastguard Worker 
64*387f9dfdSAndroid Build Coastguard Worker  private:
65*387f9dfdSAndroid Build Coastguard Worker   std::map<std::string, FuncInfo> funcs_;
66*387f9dfdSAndroid Build Coastguard Worker   std::map<uint32_t, std::string> func_idx_;
67*387f9dfdSAndroid Build Coastguard Worker };
68*387f9dfdSAndroid Build Coastguard Worker 
69*387f9dfdSAndroid Build Coastguard Worker class ClangLoader {
70*387f9dfdSAndroid Build Coastguard Worker  public:
71*387f9dfdSAndroid Build Coastguard Worker   explicit ClangLoader(llvm::LLVMContext *ctx, unsigned flags);
72*387f9dfdSAndroid Build Coastguard Worker   ~ClangLoader();
73*387f9dfdSAndroid Build Coastguard Worker   int parse(std::unique_ptr<llvm::Module> *mod, TableStorage &ts,
74*387f9dfdSAndroid Build Coastguard Worker             const std::string &file, bool in_memory, const char *cflags[],
75*387f9dfdSAndroid Build Coastguard Worker             int ncflags, const std::string &id, ProgFuncInfo &prog_func_info,
76*387f9dfdSAndroid Build Coastguard Worker             std::string &mod_src, const std::string &maps_ns,
77*387f9dfdSAndroid Build Coastguard Worker             fake_fd_map_def &fake_fd_map,
78*387f9dfdSAndroid Build Coastguard Worker             std::map<std::string, std::vector<std::string>> &perf_events);
79*387f9dfdSAndroid Build Coastguard Worker 
80*387f9dfdSAndroid Build Coastguard Worker  private:
81*387f9dfdSAndroid Build Coastguard Worker   int do_compile(std::unique_ptr<llvm::Module> *mod, TableStorage &ts,
82*387f9dfdSAndroid Build Coastguard Worker                  bool in_memory, const std::vector<const char *> &flags_cstr_in,
83*387f9dfdSAndroid Build Coastguard Worker                  const std::vector<const char *> &flags_cstr_rem,
84*387f9dfdSAndroid Build Coastguard Worker                  const std::string &main_path,
85*387f9dfdSAndroid Build Coastguard Worker                  const std::unique_ptr<llvm::MemoryBuffer> &main_buf,
86*387f9dfdSAndroid Build Coastguard Worker                  const std::string &id, ProgFuncInfo &prog_func_info,
87*387f9dfdSAndroid Build Coastguard Worker                  std::string &mod_src, bool use_internal_bpfh,
88*387f9dfdSAndroid Build Coastguard Worker                  const std::string &maps_ns, fake_fd_map_def &fake_fd_map,
89*387f9dfdSAndroid Build Coastguard Worker                  std::map<std::string, std::vector<std::string>> &perf_events);
90*387f9dfdSAndroid Build Coastguard Worker   void add_remapped_includes(clang::CompilerInvocation& invocation);
91*387f9dfdSAndroid Build Coastguard Worker   void add_main_input(clang::CompilerInvocation& invocation,
92*387f9dfdSAndroid Build Coastguard Worker                       const std::string& main_path,
93*387f9dfdSAndroid Build Coastguard Worker                       llvm::MemoryBuffer *main_buf);
94*387f9dfdSAndroid Build Coastguard Worker 
95*387f9dfdSAndroid Build Coastguard Worker  private:
96*387f9dfdSAndroid Build Coastguard Worker   std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_headers_;
97*387f9dfdSAndroid Build Coastguard Worker   std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_footers_;
98*387f9dfdSAndroid Build Coastguard Worker   llvm::LLVMContext *ctx_;
99*387f9dfdSAndroid Build Coastguard Worker   unsigned flags_;
100*387f9dfdSAndroid Build Coastguard Worker };
101*387f9dfdSAndroid Build Coastguard Worker 
102*387f9dfdSAndroid Build Coastguard Worker }  // namespace ebpf
103