authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-08 12:59:31-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-08 12:59:31-04:00
logf6f0e09456002a4f4a333cb7462540c39df78d56
tree24c86c8800bc55d8412dc153bfe2ac3056973007
parentb8b68cb279e8f51a8aa7c67aa42cefc644375b74
parentb878a64a5f44072b11dd0f73fc2b808c41f0d172
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6267 from mattnite/btf

BPF: add BTF

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

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