|
| 1 | +//===- MCBTFContext.h ---------------------------------------- *- C++ --*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +// This header file contains two parts. The first part is the BTF ELF |
| 9 | +// specification in C format, and the second part is the various |
| 10 | +// C++ classes to manipulate the data structure in order to generate |
| 11 | +// the BTF related ELF sections. |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | +#ifndef LLVM_MC_MCBTFCONTEXT_H |
| 14 | +#define LLVM_MC_MCBTFCONTEXT_H |
| 15 | + |
| 16 | +#include <linux/types.h> |
| 17 | + |
| 18 | +#define BTF_MAGIC 0xeB9F |
| 19 | +#define BTF_VERSION 1 |
| 20 | + |
| 21 | +struct btf_header { |
| 22 | + __u16 magic; |
| 23 | + __u8 version; |
| 24 | + __u8 flags; |
| 25 | + __u32 hdr_len; |
| 26 | + |
| 27 | + /* All offsets are in bytes relative to the end of this header */ |
| 28 | + __u32 type_off; /* offset of type section */ |
| 29 | + __u32 type_len; /* length of type section */ |
| 30 | + __u32 str_off; /* offset of string section */ |
| 31 | + __u32 str_len; /* length of string section */ |
| 32 | +}; |
| 33 | + |
| 34 | +/* Max # of type identifier */ |
| 35 | +#define BTF_MAX_TYPE 0x0000ffff |
| 36 | +/* Max offset into the string section */ |
| 37 | +#define BTF_MAX_NAME_OFFSET 0x0000ffff |
| 38 | +/* Max # of struct/union/enum members or func args */ |
| 39 | +#define BTF_MAX_VLEN 0xffff |
| 40 | + |
| 41 | +struct btf_type { |
| 42 | + __u32 name_off; |
| 43 | + /* "info" bits arrangement |
| 44 | + * bits 0-15: vlen (e.g. # of struct's members) |
| 45 | + * bits 16-23: unused |
| 46 | + * bits 24-27: kind (e.g. int, ptr, array...etc) |
| 47 | + * bits 28-31: unused |
| 48 | + */ |
| 49 | + __u32 info; |
| 50 | + /* "size" is used by INT, ENUM, STRUCT and UNION. |
| 51 | + * "size" tells the size of the type it is describing. |
| 52 | + * |
| 53 | + * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, |
| 54 | + * FUNC and FUNC_PROTO. |
| 55 | + * "type" is a type_id referring to another type. |
| 56 | + */ |
| 57 | + union { |
| 58 | + __u32 size; |
| 59 | + __u32 type; |
| 60 | + }; |
| 61 | +}; |
| 62 | + |
| 63 | +#define BTF_INFO_KIND(info) (((info) >> 24) & 0x0f) |
| 64 | +#define BTF_INFO_VLEN(info) ((info) & 0xffff) |
| 65 | + |
| 66 | +#define BTF_KIND_UNKN 0 /* Unknown */ |
| 67 | +#define BTF_KIND_INT 1 /* Integer */ |
| 68 | +#define BTF_KIND_PTR 2 /* Pointer */ |
| 69 | +#define BTF_KIND_ARRAY 3 /* Array */ |
| 70 | +#define BTF_KIND_STRUCT 4 /* Struct */ |
| 71 | +#define BTF_KIND_UNION 5 /* Union */ |
| 72 | +#define BTF_KIND_ENUM 6 /* Enumeration */ |
| 73 | +#define BTF_KIND_FWD 7 /* Forward */ |
| 74 | +#define BTF_KIND_TYPEDEF 8 /* Typedef */ |
| 75 | +#define BTF_KIND_VOLATILE 9 /* Volatile */ |
| 76 | +#define BTF_KIND_CONST 10 /* Const */ |
| 77 | +#define BTF_KIND_RESTRICT 11 /* Restrict */ |
| 78 | +#define BTF_KIND_FUNC 12 /* Function */ |
| 79 | +#define BTF_KIND_FUNC_PROTO 13 /* Function Prototype */ |
| 80 | +#define BTF_KIND_MAX 13 |
| 81 | +#define NR_BTF_KINDS 14 |
| 82 | + |
| 83 | +/* For some specific BTF_KIND, "struct btf_type" is immediately |
| 84 | + * followed by extra data. |
| 85 | + */ |
| 86 | + |
| 87 | +/* BTF_KIND_INT is followed by a u32 and the following |
| 88 | + * is the 32 bits arrangement: |
| 89 | + */ |
| 90 | +#define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) |
| 91 | +#define BTF_INT_OFFSET(VAL) (((VAL & 0x00ff0000)) >> 16) |
| 92 | +#define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) |
| 93 | + |
| 94 | +/* Attributes stored in the BTF_INT_ENCODING */ |
| 95 | +#define BTF_INT_SIGNED (1 << 0) |
| 96 | +#define BTF_INT_CHAR (1 << 1) |
| 97 | +#define BTF_INT_BOOL (1 << 2) |
| 98 | + |
| 99 | +/* BTF_KIND_ENUM is followed by multiple "struct btf_enum". |
| 100 | + * The exact number of btf_enum is stored in the vlen (of the |
| 101 | + * info in "struct btf_type"). |
| 102 | + */ |
| 103 | +struct btf_enum { |
| 104 | + __u32 name_off; |
| 105 | + __s32 val; |
| 106 | +}; |
| 107 | + |
| 108 | +/* BTF_KIND_ARRAY is followed by one "struct btf_array" */ |
| 109 | +struct btf_array { |
| 110 | + __u32 type; |
| 111 | + __u32 index_type; |
| 112 | + __u32 nelems; |
| 113 | +}; |
| 114 | + |
| 115 | +/* BTF_KIND_STRUCT and BTF_KIND_UNION are followed |
| 116 | + * by multiple "struct btf_member". The exact number |
| 117 | + * of btf_member is stored in the vlen (of the info in |
| 118 | + * "struct btf_type"). |
| 119 | + */ |
| 120 | +struct btf_member { |
| 121 | + __u32 name_off; |
| 122 | + __u32 type; |
| 123 | + __u32 offset; /* offset in bits */ |
| 124 | +}; |
| 125 | + |
| 126 | +/* .BTF.ext section contains func_info and line_info. |
| 127 | + */ |
| 128 | +struct btf_ext_header { |
| 129 | + __u16 magic; |
| 130 | + __u8 version; |
| 131 | + __u8 flags; |
| 132 | + __u32 hdr_len; |
| 133 | + |
| 134 | + __u32 func_info_off; |
| 135 | + __u32 func_info_len; |
| 136 | + __u32 line_info_off; |
| 137 | + __u32 line_info_len; |
| 138 | +}; |
| 139 | + |
| 140 | +struct bpf_func_info { |
| 141 | + __u32 insn_offset; |
| 142 | + __u32 type_id; |
| 143 | +}; |
| 144 | + |
| 145 | +struct btf_sec_func_info { |
| 146 | + __u32 sec_name_off; |
| 147 | + __u32 num_func_info; |
| 148 | +}; |
| 149 | + |
| 150 | +struct bpf_line_info { |
| 151 | + __u32 insn_offset; |
| 152 | + __u32 file_name_off; |
| 153 | + __u32 line_off; |
| 154 | + __u32 line_col; /* line num: line_col >> 10, col num: line_col & 0x3ff */ |
| 155 | +}; |
| 156 | + |
| 157 | +struct btf_sec_line_info { |
| 158 | + __u32 sec_name_off; |
| 159 | + __u32 num_line_info; |
| 160 | +}; |
| 161 | + |
| 162 | +namespace llvm { |
| 163 | + |
| 164 | +const char *const btf_kind_str[NR_BTF_KINDS] = { |
| 165 | + [BTF_KIND_UNKN] = "UNKNOWN", |
| 166 | + [BTF_KIND_INT] = "INT", |
| 167 | + [BTF_KIND_PTR] = "PTR", |
| 168 | + [BTF_KIND_ARRAY] = "ARRAY", |
| 169 | + [BTF_KIND_STRUCT] = "STRUCT", |
| 170 | + [BTF_KIND_UNION] = "UNION", |
| 171 | + [BTF_KIND_ENUM] = "ENUM", |
| 172 | + [BTF_KIND_FWD] = "FWD", |
| 173 | + [BTF_KIND_TYPEDEF] = "TYPEDEF", |
| 174 | + [BTF_KIND_VOLATILE] = "VOLATILE", |
| 175 | + [BTF_KIND_CONST] = "CONST", |
| 176 | + [BTF_KIND_RESTRICT] = "RESTRICT", |
| 177 | + [BTF_KIND_FUNC] = "FUNC", |
| 178 | + [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", |
| 179 | +}; |
| 180 | + |
| 181 | +#include "llvm/ADT/SmallVector.h" |
| 182 | +#include <map> |
| 183 | + |
| 184 | +class MCBTFContext; |
| 185 | +class MCObjectStreamer; |
| 186 | + |
| 187 | +// This is base class of all BTF KIND. It is also used directly |
| 188 | +// by the reference kinds: |
| 189 | +// BTF_KIND_CONST, BTF_KIND_PTR, BTF_KIND_VOLATILE, |
| 190 | +// BTF_KIND_TYPEDEF, BTF_KIND_RESTRICT, and BTF_KIND_FWD |
| 191 | +class BTFTypeEntry { |
| 192 | +protected: |
| 193 | + size_t Id; /* type index in the BTF list, started from 1 */ |
| 194 | + struct btf_type BTFType; |
| 195 | + |
| 196 | +public: |
| 197 | + BTFTypeEntry(size_t id, struct btf_type &type) : |
| 198 | + Id(id), BTFType(type) {} |
| 199 | + unsigned char getKind() { return BTF_INFO_KIND(BTFType.info); } |
| 200 | + void setId(size_t Id) { this->Id = Id; } |
| 201 | + size_t getId() { return Id; } |
| 202 | + void setNameOff(unsigned NameOff) { BTFType.name_off = NameOff; } |
| 203 | + |
| 204 | + unsigned getTypeIndex() { return BTFType.type; } |
| 205 | + unsigned getNameOff() { return BTFType.name_off; } |
| 206 | + virtual size_t getSize() { return sizeof(struct btf_type); } |
| 207 | + virtual void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 208 | + virtual void emitData(MCObjectStreamer *MCOS); |
| 209 | +}; |
| 210 | + |
| 211 | +// BTF_KIND_INT |
| 212 | +class BTFTypeEntryInt : public BTFTypeEntry { |
| 213 | + unsigned IntVal; // encoding, offset, bits |
| 214 | + |
| 215 | +public: |
| 216 | + BTFTypeEntryInt(size_t id, struct btf_type &type, unsigned intval) : |
| 217 | + BTFTypeEntry(id, type), IntVal(intval) {} |
| 218 | + size_t getSize() { return BTFTypeEntry::getSize() + sizeof(unsigned); } |
| 219 | + void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 220 | + void emitData(MCObjectStreamer *MCOS); |
| 221 | +}; |
| 222 | + |
| 223 | +// BTF_KIND_ENUM |
| 224 | +class BTFTypeEntryEnum : public BTFTypeEntry { |
| 225 | + std::vector<struct btf_enum> EnumValues; |
| 226 | + |
| 227 | +public: |
| 228 | + BTFTypeEntryEnum(size_t id, struct btf_type &type, |
| 229 | + std::vector<struct btf_enum> &values) : |
| 230 | + BTFTypeEntry(id, type), EnumValues(values) {} |
| 231 | + size_t getSize() { |
| 232 | + return BTFTypeEntry::getSize() + |
| 233 | + BTF_INFO_VLEN(BTFType.info) * sizeof(struct btf_enum); |
| 234 | + } |
| 235 | + void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 236 | + void emitData(MCObjectStreamer *MCOS); |
| 237 | +}; |
| 238 | + |
| 239 | +// BTF_KIND_ARRAY |
| 240 | +class BTFTypeEntryArray : public BTFTypeEntry { |
| 241 | + struct btf_array ArrayInfo; |
| 242 | + |
| 243 | +public: |
| 244 | + BTFTypeEntryArray(size_t id, struct btf_type &type, |
| 245 | + struct btf_array &arrayinfo) : |
| 246 | + BTFTypeEntry(id, type), ArrayInfo(arrayinfo) {} |
| 247 | + size_t getSize() { |
| 248 | + return BTFTypeEntry::getSize() + sizeof(struct btf_array); |
| 249 | + } |
| 250 | + void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 251 | + void emitData(MCObjectStreamer *MCOS); |
| 252 | +}; |
| 253 | + |
| 254 | +// BTF_KIND_STRUCT and BTF_KIND_UNION |
| 255 | +class BTFTypeEntryStruct : public BTFTypeEntry { |
| 256 | + std::vector<struct btf_member> Members; |
| 257 | + |
| 258 | +public: |
| 259 | + BTFTypeEntryStruct(size_t id, struct btf_type &type, |
| 260 | + std::vector<struct btf_member> &members) : |
| 261 | + BTFTypeEntry(id, type), Members(members) {} |
| 262 | + size_t getSize() { |
| 263 | + return BTFTypeEntry::getSize() + |
| 264 | + BTF_INFO_VLEN(BTFType.info) * sizeof(struct btf_member); |
| 265 | + } |
| 266 | + void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 267 | + void emitData(MCObjectStreamer *MCOS); |
| 268 | +}; |
| 269 | + |
| 270 | +// BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO |
| 271 | +class BTFTypeEntryFunc : public BTFTypeEntry { |
| 272 | + std::vector<unsigned> Parameters; |
| 273 | + |
| 274 | +public: |
| 275 | + BTFTypeEntryFunc(size_t id, struct btf_type &type, |
| 276 | + std::vector<unsigned> ¶ms) : |
| 277 | + BTFTypeEntry(id, type), Parameters(params) {} |
| 278 | + size_t getSize() { |
| 279 | + return BTFTypeEntry::getSize() + |
| 280 | + BTF_INFO_VLEN(BTFType.info) * sizeof(unsigned); |
| 281 | + } |
| 282 | + void print(raw_ostream &s, MCBTFContext& BTFContext); |
| 283 | + void emitData(MCObjectStreamer *MCOS); |
| 284 | +}; |
| 285 | + |
| 286 | +class BTFStringTable { |
| 287 | + size_t Size; // total size in bytes |
| 288 | + std::map<size_t, unsigned> OffsetToIdMap; |
| 289 | + std::vector<std::string> Table; |
| 290 | + |
| 291 | + public: |
| 292 | + BTFStringTable() : Size(0) {} |
| 293 | + size_t getSize() { return Size; } |
| 294 | + std::vector<std::string> &getTable() { return Table; } |
| 295 | + size_t addString(std::string S) { |
| 296 | + // check whether the string already exists |
| 297 | + for (auto &OffsetM : OffsetToIdMap) { |
| 298 | + if (Table[OffsetM.second] == S) |
| 299 | + return OffsetM.first; |
| 300 | + } |
| 301 | + // not find, add to the string table |
| 302 | + size_t Offset = Size; |
| 303 | + OffsetToIdMap[Offset] = Table.size(); |
| 304 | + Table.push_back(S); |
| 305 | + Size += S.size() + 1; |
| 306 | + return Offset; |
| 307 | + } |
| 308 | + std::string &getStringAtOffset(size_t Offset) { |
| 309 | + return Table[OffsetToIdMap[Offset]]; |
| 310 | + } |
| 311 | + void showTable(raw_ostream &OS) { |
| 312 | + for (auto OffsetM : OffsetToIdMap) |
| 313 | + OS << OffsetM.first << " : " << Table[OffsetM.second] |
| 314 | + << "\n"; |
| 315 | + } |
| 316 | +}; |
| 317 | + |
| 318 | +struct BTFFuncInfo { |
| 319 | + const MCSymbol *Label; |
| 320 | + unsigned int TypeId; |
| 321 | +}; |
| 322 | + |
| 323 | +struct BTFLineInfo { |
| 324 | + MCSymbol *Label; |
| 325 | + unsigned int FileNameOff; |
| 326 | + unsigned int LineOff; |
| 327 | + unsigned int LineNum; |
| 328 | + unsigned int ColumnNum; |
| 329 | +}; |
| 330 | + |
| 331 | +class MCBTFContext { |
| 332 | + std::vector<std::unique_ptr<BTFTypeEntry>> TypeEntries; |
| 333 | + BTFStringTable StringTable; |
| 334 | + std::map<unsigned, std::vector<BTFFuncInfo>> FuncInfoTable; |
| 335 | + std::map<unsigned, std::vector<BTFLineInfo>> LineInfoTable; |
| 336 | + |
| 337 | + friend class BTFTypeEntry; |
| 338 | + friend class BTFTypeEntryInt; |
| 339 | + friend class BTFTypeEntryEnum; |
| 340 | + friend class BTFTypeEntryArray; |
| 341 | + friend class BTFTypeEntryStruct; |
| 342 | + friend class BTFTypeEntryFunc; |
| 343 | + |
| 344 | +public: |
| 345 | + void dump(raw_ostream& OS); |
| 346 | + void emitAll(MCObjectStreamer *MCOS); |
| 347 | + void emitCommonHeader(MCObjectStreamer *MCOS); |
| 348 | + void emitBTFSection(MCObjectStreamer *MCOS); |
| 349 | + void emitBTFExtSection(MCObjectStreamer *MCOS); |
| 350 | + |
| 351 | + size_t addString(std::string S) { |
| 352 | + return StringTable.addString(S); |
| 353 | + } |
| 354 | + void addTypeEntry(std::unique_ptr<BTFTypeEntry> Entry); |
| 355 | + void addFuncInfo(unsigned SecNameOff, BTFFuncInfo Info) { |
| 356 | + FuncInfoTable[SecNameOff].push_back(Info); |
| 357 | + } |
| 358 | + void addLineInfo(unsigned SecNameOff, BTFLineInfo Info) { |
| 359 | + LineInfoTable[SecNameOff].push_back(Info); |
| 360 | + } |
| 361 | +}; |
| 362 | + |
| 363 | +} |
| 364 | +#endif |
0 commit comments