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