authorgravatar for mattnite@protonmail.comMatt Knight <mattnite@protonmail.com> 2020-09-06 21:44:16-07:00
committergravatar for mattnite@protonmail.comMatt Knight <mattnite@protonmail.com> 2020-09-06 21:44:16-07:00
logcaaa9ad2afe7ca21ff48b9fd5c82be99057b46e4
treea0d30c8cd50b0a0447a16a5a603f5589e32de469
parentd7268cbb242d6bea6b6e4d929a8d6b99608b640a
signaturelock-open Commit is signed but in an unrecognized format.

added btf


3 files changed, 172 insertions(+), 0 deletions(-)

lib/std/os/linux/bpf.zig+2
......@@ -7,6 +7,8 @@ usingnamespace std.os;
77const std = @import("../../std.zig");
88const expectEqual = std.testing.expectEqual;
99
10pub const btf = @import("bpf/btf.zig");
11
1012// instruction classes
1113pub const LD = 0x00;
1214pub const LDX = 0x01;
lib/std/os/linux/bpf/btf.zig created+151
......@@ -0,0 +1,151 @@
1const magic = 0xeb9f;
2const version = 1;
3
4pub const ext = @import("ext.zig");
5
6/// All offsets are in bytes relative to the end of this header
7pub const Header = packed struct {
8 magic: u16,
9 version: u8,
10 flags: u8,
11 hdr_len: u32,
12
13 /// offset of type section
14 type_off: u32,
15
16 /// length of type section
17 type_len: u32,
18
19 /// offset of string section
20 str_off: u32,
21
22 /// length of string section
23 str_len: u32,
24};
25
26/// Max number of type identifiers
27pub const max_type = 0xfffff;
28
29/// Max offset into string section
30pub const max_name_offset = 0xffffff;
31
32/// Max number of struct/union/enum member of func args
33pub const max_vlen = 0xffff;
34
35pub const Type = packed struct {
36 name_off: u32,
37 info: struct {
38 /// number of struct's members
39 vlen: u16,
40
41 unused_1: u8,
42 kind: Kind,
43 unused_2: u3,
44
45 /// used by Struct, Union, and Fwd
46 kind_flag: bool,
47 },
48
49 /// size is used by Int, Enum, Struct, Union, and DataSec, it tells the size
50 /// of the type it is describing
51 ///
52 /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func,
53 /// FuncProto, and Var. It is a type_id referring to another type
54 size_type: union { size: u32, typ: u32 },
55};
56
57/// For some kinds, Type is immediately followed by extra data
58pub const Kind = enum(u4) {
59 Unknown,
60 Int,
61 Ptr,
62 Array,
63 Struct,
64 Union,
65 Enum,
66 Fwd,
67 Typedef,
68 Volatile,
69 Const,
70 Restrict,
71 Func,
72 FuncProto,
73 Var,
74 DataSec,
75};
76
77/// Int kind is followed by this struct
78pub const IntInfo = packed struct {
79 bits: u8,
80 unused: u8,
81 offset: u8,
82 encoding: enum(u4) {
83 Signed = 1 << 0,
84 Char = 1 << 1,
85 Bool = 1 << 2,
86 },
87};
88
89test "IntInfo is 32 bits" {
90 std.testing.expectEqual(@bitSizeOf(IntInfo), 32);
91}
92
93/// Enum kind is followed by this struct
94pub const Enum = packed struct {
95 name_off: u32,
96 val: i32,
97};
98
99/// Array kind is followd by this struct
100pub const Array = packed struct {
101 typ: u32,
102 index_type: u32,
103 nelems: u32,
104};
105
106/// Struct and Union kinds are followed by multiple Member structs. The exact
107/// number is stored in vlen
108pub const Member = packed struct {
109 name_off: u32,
110 typ: u32,
111
112 /// if the kind_flag is set, offset contains both member bitfield size and
113 /// bit offset, the bitfield size is set for bitfield members. If the type
114 /// info kind_flag is not set, the offset contains only bit offset
115 offset: packed struct {
116 bit: u24,
117 bitfield_size: u8,
118 },
119};
120
121/// FuncProto is followed by multiple Params, the exact number is stored in vlen
122pub const Param = packed struct {
123 name_off: u32,
124 typ: u32,
125};
126
127pub const VarLinkage = enum {
128 Static,
129 GlobalAllocated,
130 GlobalExtern,
131};
132
133pub const FuncLinkage = enum {
134 Static,
135 Global,
136 Extern,
137};
138
139/// Var kind is followd by a single Var struct to describe additional
140/// information related to the variable such as its linkage
141pub const Var = packed struct {
142 linkage: u32,
143};
144
145/// Datasec kind is followed by multible VarSecInfo to describe all Var kind
146/// types it contains along with it's in-section offset as well as size.
147pub const VarSecInfo = packed struct {
148 typ: u32,
149 offset: u32,
150 size: u32,
151};
lib/std/os/linux/bpf/btf_ext.zig created+19
......@@ -0,0 +1,19 @@
1pub const Header = packed struct {
2 magic: u16,
3 version: u8,
4 flags: u8,
5 hdr_len: u32,
6
7 /// All offsets are in bytes relative to the end of this header
8 func_info_off: u32,
9 func_info_len: u32,
10 line_info_off: u32,
11 line_info_len: u32,
12};
13
14pub const InfoSec = packed struct {
15 sec_name_off: u32,
16 num_info: u32,
17 // TODO: communicate that there is data here
18 //data: [0]u8,
19};