| 1 | const std = @import("../../../std.zig"); |
| 2 | |
| 3 | pub const magic = 0xeb9f; |
| 4 | pub const version = 1; |
| 5 | |
| 6 | pub const ext = @import("btf_ext.zig"); |
| 7 | |
| 8 | /// All offsets are in bytes relative to the end of this header |
| 9 | pub const Header = extern struct { |
| 10 | magic: u16, |
| 11 | version: u8, |
| 12 | flags: u8, |
| 13 | hdr_len: u32, |
| 14 | |
| 15 | /// offset of type section |
| 16 | type_off: u32, |
| 17 | |
| 18 | /// length of type section |
| 19 | type_len: u32, |
| 20 | |
| 21 | /// offset of string section |
| 22 | str_off: u32, |
| 23 | |
| 24 | /// length of string section |
| 25 | str_len: u32, |
| 26 | }; |
| 27 | |
| 28 | /// Max number of type identifiers |
| 29 | pub const max_type = 0xfffff; |
| 30 | |
| 31 | /// Max offset into string section |
| 32 | pub const max_name_offset = 0xffffff; |
| 33 | |
| 34 | /// Max number of struct/union/enum member of func args |
| 35 | pub const max_vlen = 0xffff; |
| 36 | |
| 37 | pub const Type = extern struct { |
| 38 | name_off: u32, |
| 39 | info: packed struct(u32) { |
| 40 | /// number of struct's members |
| 41 | vlen: u16, |
| 42 | |
| 43 | unused_1: u8, |
| 44 | kind: Kind, |
| 45 | unused_2: u2, |
| 46 | |
| 47 | /// used by Struct, Union, and Fwd |
| 48 | kind_flag: bool, |
| 49 | }, |
| 50 | |
| 51 | /// size is used by Int, Enum, Struct, Union, and DataSec, it tells the size |
| 52 | /// of the type it is describing |
| 53 | /// |
| 54 | /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func, |
| 55 | /// FuncProto, and Var. It is a type_id referring to another type |
| 56 | size_type: extern union { size: u32, typ: u32 }, |
| 57 | }; |
| 58 | |
| 59 | /// For some kinds, Type is immediately followed by extra data |
| 60 | pub const Kind = enum(u5) { |
| 61 | unknown, |
| 62 | int, |
| 63 | ptr, |
| 64 | array, |
| 65 | @"struct", |
| 66 | @"union", |
| 67 | @"enum", |
| 68 | fwd, |
| 69 | typedef, |
| 70 | @"volatile", |
| 71 | @"const", |
| 72 | restrict, |
| 73 | func, |
| 74 | func_proto, |
| 75 | @"var", |
| 76 | datasec, |
| 77 | float, |
| 78 | decl_tag, |
| 79 | type_tag, |
| 80 | enum64, |
| 81 | }; |
| 82 | |
| 83 | /// int kind is followed by this struct |
| 84 | pub const IntInfo = packed struct(u32) { |
| 85 | bits: u8, |
| 86 | reserved_1: u8, |
| 87 | offset: u8, |
| 88 | encoding: enum(u4) { |
| 89 | signed = 1 << 0, |
| 90 | char = 1 << 1, |
| 91 | boolean = 1 << 2, |
| 92 | }, |
| 93 | reserved_2: u4, |
| 94 | }; |
| 95 | |
| 96 | test "IntInfo is 32 bits" { |
| 97 | try std.testing.expectEqual(@bitSizeOf(IntInfo), 32); |
| 98 | } |
| 99 | |
| 100 | /// enum kind is followed by this struct |
| 101 | pub const Enum = extern struct { |
| 102 | name_off: u32, |
| 103 | val: i32, |
| 104 | }; |
| 105 | |
| 106 | /// enum64 kind is followed by this struct |
| 107 | pub const Enum64 = extern struct { |
| 108 | name_off: u32, |
| 109 | val_lo32: i32, |
| 110 | val_hi32: i32, |
| 111 | }; |
| 112 | |
| 113 | /// array kind is followed by this struct |
| 114 | pub const Array = extern struct { |
| 115 | typ: u32, |
| 116 | index_type: u32, |
| 117 | nelems: u32, |
| 118 | }; |
| 119 | |
| 120 | /// struct and union kinds are followed by multiple Member structs. The exact |
| 121 | /// number is stored in vlen |
| 122 | pub const Member = extern struct { |
| 123 | name_off: u32, |
| 124 | typ: u32, |
| 125 | |
| 126 | /// if the kind_flag is set, offset contains both member bitfield size and |
| 127 | /// bit offset, the bitfield size is set for bitfield members. If the type |
| 128 | /// info kind_flag is not set, the offset contains only bit offset |
| 129 | offset: packed struct(u32) { |
| 130 | bit: u24, |
| 131 | bitfield_size: u8, |
| 132 | }, |
| 133 | }; |
| 134 | |
| 135 | /// func_proto is followed by multiple Params, the exact number is stored in vlen |
| 136 | pub const Param = extern struct { |
| 137 | name_off: u32, |
| 138 | typ: u32, |
| 139 | }; |
| 140 | |
| 141 | pub const VarLinkage = enum { |
| 142 | static, |
| 143 | global_allocated, |
| 144 | global_extern, |
| 145 | }; |
| 146 | |
| 147 | pub const FuncLinkage = enum { |
| 148 | static, |
| 149 | global, |
| 150 | external, |
| 151 | }; |
| 152 | |
| 153 | /// var kind is followed by a single Var struct to describe additional |
| 154 | /// information related to the variable such as its linkage |
| 155 | pub const Var = extern struct { |
| 156 | linkage: u32, |
| 157 | }; |
| 158 | |
| 159 | /// datasec kind is followed by multiple VarSecInfo to describe all Var kind |
| 160 | /// types it contains along with it's in-section offset as well as size. |
| 161 | pub const VarSecInfo = extern struct { |
| 162 | typ: u32, |
| 163 | offset: u32, |
| 164 | size: u32, |
| 165 | }; |
| 166 | |
| 167 | // decl_tag kind is followed by a single DeclTag struct to describe |
| 168 | // additional information related to the tag applied location. |
| 169 | // If component_idx == -1, the tag is applied to a struct, union, |
| 170 | // variable or function. Otherwise, it is applied to a struct/union |
| 171 | // member or a func argument, and component_idx indicates which member |
| 172 | // or argument (0 ... vlen-1). |
| 173 | pub const DeclTag = extern struct { |
| 174 | component_idx: u32, |
| 175 | }; |