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 <map> 20 #include <memory> 21 #include <string> 22 23 #include "table_storage.h" 24 25 namespace llvm { 26 class Module; 27 class LLVMContext; 28 class MemoryBuffer; 29 } 30 31 namespace ebpf { 32 33 class FuncSource { 34 class SourceCode { 35 public: src_(s1)36 SourceCode(const std::string& s1 = "", const std::string& s2 = ""): src_(s1), src_rewritten_(s2) {} 37 std::string src_; 38 std::string src_rewritten_; 39 }; 40 std::map<std::string, SourceCode> funcs_; 41 public: FuncSource()42 FuncSource() {} clear()43 void clear() { funcs_.clear(); } 44 const char * src(const std::string& name); 45 const char * src_rewritten(const std::string& name); 46 void set_src(const std::string& name, const std::string& src); 47 void set_src_rewritten(const std::string& name, const std::string& src); 48 }; 49 50 class ClangLoader { 51 public: 52 explicit ClangLoader(llvm::LLVMContext *ctx, unsigned flags); 53 ~ClangLoader(); 54 int parse(std::unique_ptr<llvm::Module> *mod, TableStorage &ts, 55 const std::string &file, bool in_memory, const char *cflags[], 56 int ncflags, const std::string &id, FuncSource &func_src, 57 std::string &mod_src, const std::string &maps_ns); 58 59 private: 60 int do_compile(std::unique_ptr<llvm::Module> *mod, TableStorage &ts, 61 bool in_memory, const std::vector<const char *> &flags_cstr_in, 62 const std::vector<const char *> &flags_cstr_rem, 63 const std::string &main_path, 64 const std::unique_ptr<llvm::MemoryBuffer> &main_buf, 65 const std::string &id, FuncSource &func_src, 66 std::string &mod_src, bool use_internal_bpfh, 67 const std::string &maps_ns); 68 69 private: 70 std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_headers_; 71 std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_footers_; 72 llvm::LLVMContext *ctx_; 73 unsigned flags_; 74 }; 75 76 } // namespace ebpf 77