1 /* 2 * Copyright (c) 2019 Facebook, 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 #ifndef BCC_BTF_H 18 #define BCC_BTF_H 19 20 #include <stdbool.h> 21 #include <stdint.h> 22 #include <string> 23 #include <map> 24 #include <vector> 25 26 #include "bpf_module.h" 27 28 struct btf; 29 struct btf_type; 30 31 namespace btf_ext_vendored { 32 33 /* 34 * The .BTF.ext ELF section layout defined as 35 * struct btf_ext_header 36 * func_info subsection 37 * 38 * The func_info subsection layout: 39 * record size for struct bpf_func_info in the func_info subsection 40 * struct btf_sec_func_info for section #1 41 * a list of bpf_func_info records for section #1 42 * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h 43 * but may not be identical 44 * struct btf_sec_func_info for section #2 45 * a list of bpf_func_info records for section #2 46 * ...... 47 * 48 * Note that the bpf_func_info record size in .BTF.ext may not 49 * be the same as the one defined in include/uapi/linux/bpf.h. 50 * The loader should ensure that record_size meets minimum 51 * requirement and pass the record as is to the kernel. The 52 * kernel will handle the func_info properly based on its contents. 53 */ 54 struct btf_ext_header { 55 uint16_t magic; 56 uint8_t version; 57 uint8_t flags; 58 uint32_t hdr_len; 59 60 /* All offsets are in bytes relative to the end of this header */ 61 uint32_t func_info_off; 62 uint32_t func_info_len; 63 uint32_t line_info_off; 64 uint32_t line_info_len; 65 66 /* optional part of .BTF.ext header */ 67 uint32_t core_relo_off; 68 uint32_t core_relo_len; 69 }; 70 71 struct btf_ext_info { 72 /* 73 * info points to the individual info section (e.g. func_info and 74 * line_info) from the .BTF.ext. It does not include the __u32 rec_size. 75 */ 76 void *info; 77 uint32_t rec_size; 78 uint32_t len; 79 }; 80 81 struct btf_ext { 82 union { 83 struct btf_ext_header *hdr; 84 void *data; 85 }; 86 struct btf_ext_info func_info; 87 struct btf_ext_info line_info; 88 struct btf_ext_info core_relo_info; 89 uint32_t data_size; 90 }; 91 92 struct btf_ext_info_sec { 93 uint32_t sec_name_off; 94 uint32_t num_info; 95 /* Followed by num_info * record_size number of bytes */ 96 uint8_t data[]; 97 }; 98 99 struct btf_ext *btf_ext__new(const uint8_t *data, uint32_t size); 100 void btf_ext__free(struct btf_ext *btf_ext); 101 int btf_ext__reloc_func_info(const struct btf *btf, 102 const struct btf_ext *btf_ext, 103 const char *sec_name, uint32_t insns_cnt, 104 void **func_info, uint32_t *cnt); 105 int btf_ext__reloc_line_info(const struct btf *btf, 106 const struct btf_ext *btf_ext, 107 const char *sec_name, uint32_t insns_cnt, 108 void **line_info, uint32_t *cnt); 109 110 } // namespace btf_ext_vendored 111 112 namespace ebpf { 113 114 class BTFStringTable { 115 private: 116 uint32_t Size; 117 uint32_t OrigTblLen; 118 std::map<uint32_t, uint32_t> OffsetToIdMap; 119 std::vector<std::string> Table; 120 121 public: BTFStringTable(uint32_t TblLen)122 BTFStringTable(uint32_t TblLen): Size(0), OrigTblLen(TblLen) {} getSize()123 uint32_t getSize() { return Size; } getTable()124 std::vector<std::string> &getTable() { return Table; } 125 int32_t addString(std::string Str); 126 }; 127 128 class BTF { 129 public: 130 BTF(bool debug, sec_map_def §ions); 131 ~BTF(); 132 int load(uint8_t *btf_sec, uintptr_t btf_sec_size, 133 uint8_t *btf_ext_sec, uintptr_t btf_ext_sec_size, 134 std::map<std::string, std::string> &remapped_sources); 135 int get_fd(); 136 int get_btf_info(const char *fname, 137 void **func_info, unsigned *func_info_cnt, 138 unsigned *finfo_rec_size, 139 void **line_info, unsigned *line_info_cnt, 140 unsigned *linfo_rec_size); 141 int get_map_tids(std::string map_name, 142 unsigned expected_ksize, unsigned expected_vsize, 143 unsigned *key_tid, unsigned *value_tid); 144 145 private: 146 void fixup_btf(uint8_t *type_sec, uintptr_t type_sec_size, char *strings); 147 void adjust(uint8_t *btf_sec, uintptr_t btf_sec_size, 148 uint8_t *btf_ext_sec, uintptr_t btf_ext_sec_size, 149 std::map<std::string, std::string> &remapped_sources, 150 uint8_t **new_btf_sec, uintptr_t *new_btf_sec_size); 151 void warning(const char *format, ...); 152 153 private: 154 bool debug_; 155 struct btf *btf_; 156 struct btf_ext_vendored::btf_ext *btf_ext_; 157 sec_map_def §ions_; 158 }; 159 160 } // namespace ebpf 161 162 #endif 163