authorgravatar for bjorn.linse@gmail.combfredl <bjorn.linse@gmail.com> 2022-11-05 10:52:36+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 13:55:53-07:00
loga0dd11c479a93a32cdaad4a5aca69c1021d6b195
treef11ae4dd0d98fa97945b254c32df281ed7264e44
parenteb21b20949c8760ab1d992d584c44ae320e91f80

Fixes to linux/bpf/btf.zig

- the meaning of packed structs changed in zig 0.10. adjust accordingly. Use "extern struct" for the cases that directly map to C structs. - Add new type info kinds, like enum64 and DeclTag - change the Type enum to use the canonical names from libbpf. This is more predictable when comparing with external BPF documentation (than invented synonyms that need to be guessed)

1 files changed, 52 insertions(+), 31 deletions(-)

lib/std/os/linux/bpf/btf.zig+52-31
...@@ -1,12 +1,12 @@...@@ -1,12 +1,12 @@
1const std = @import("../../../std.zig");1const std = @import("../../../std.zig");
22
3const magic = 0xeb9f;3pub const magic = 0xeb9f;
4const version = 1;4pub const version = 1;
55
6pub const ext = @import("btf_ext.zig");6pub const ext = @import("btf_ext.zig");
77
8/// All offsets are in bytes relative to the end of this header8/// All offsets are in bytes relative to the end of this header
9pub const Header = packed struct {9pub const Header = extern struct {
10 magic: u16,10 magic: u16,
11 version: u8,11 version: u8,
12 flags: u8,12 flags: u8,
...@@ -34,15 +34,15 @@ pub const max_name_offset = 0xffffff;...@@ -34,15 +34,15 @@ pub const max_name_offset = 0xffffff;
34/// Max number of struct/union/enum member of func args34/// Max number of struct/union/enum member of func args
35pub const max_vlen = 0xffff;35pub const max_vlen = 0xffff;
3636
37pub const Type = packed struct {37pub const Type = extern struct {
38 name_off: u32,38 name_off: u32,
39 info: packed struct {39 info: packed struct(u32) {
40 /// number of struct's members40 /// number of struct's members
41 vlen: u16,41 vlen: u16,
4242
43 unused_1: u8,43 unused_1: u8,
44 kind: Kind,44 kind: Kind,
45 unused_2: u3,45 unused_2: u2,
4646
47 /// used by Struct, Union, and Fwd47 /// used by Struct, Union, and Fwd
48 kind_flag: bool,48 kind_flag: bool,
...@@ -53,31 +53,35 @@ pub const Type = packed struct {...@@ -53,31 +53,35 @@ pub const Type = packed struct {
53 ///53 ///
54 /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func,54 /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func,
55 /// FuncProto, and Var. It is a type_id referring to another type55 /// FuncProto, and Var. It is a type_id referring to another type
56 size_type: union { size: u32, typ: u32 },56 size_type: extern union { size: u32, typ: u32 },
57};57};
5858
59/// For some kinds, Type is immediately followed by extra data59/// For some kinds, Type is immediately followed by extra data
60pub const Kind = enum(u4) {60pub const Kind = enum(u5) {
61 unknown,61 unknown,
62 int,62 int,
63 ptr,63 ptr,
64 array,64 array,
65 structure,65 @"struct",
66 kind_union,66 @"union",
67 enumeration,67 @"enum",
68 fwd,68 fwd,
69 typedef,69 typedef,
70 kind_volatile,70 @"volatile",
71 constant,71 @"const",
72 restrict,72 restrict,
73 func,73 func,
74 funcProto,74 func_proto,
75 variable,75 @"var",
76 dataSec,76 datasec,
77 float,
78 decl_tag,
79 type_tag,
80 enum64,
77};81};
7882
79/// Int kind is followed by this struct83/// int kind is followed by this struct
80pub const IntInfo = packed struct {84pub const IntInfo = packed struct(u32) {
81 bits: u8,85 bits: u8,
82 unused: u8,86 unused: u8,
83 offset: u8,87 offset: u8,
...@@ -92,36 +96,43 @@ test "IntInfo is 32 bits" {...@@ -92,36 +96,43 @@ test "IntInfo is 32 bits" {
92 try std.testing.expectEqual(@bitSizeOf(IntInfo), 32);96 try std.testing.expectEqual(@bitSizeOf(IntInfo), 32);
93}97}
9498
95/// Enum kind is followed by this struct99/// enum kind is followed by this struct
96pub const Enum = packed struct {100pub const Enum = extern struct {
97 name_off: u32,101 name_off: u32,
98 val: i32,102 val: i32,
99};103};
100104
101/// Array kind is followd by this struct105/// enum64 kind is followed by this struct
102pub const Array = packed struct {106pub const Enum64 = extern struct {
107 name_off: u32,
108 val_lo32: i32,
109 val_hi32: i32,
110};
111
112/// array kind is followd by this struct
113pub const Array = extern struct {
103 typ: u32,114 typ: u32,
104 index_type: u32,115 index_type: u32,
105 nelems: u32,116 nelems: u32,
106};117};
107118
108/// Struct and Union kinds are followed by multiple Member structs. The exact119/// struct and union kinds are followed by multiple Member structs. The exact
109/// number is stored in vlen120/// number is stored in vlen
110pub const Member = packed struct {121pub const Member = extern struct {
111 name_off: u32,122 name_off: u32,
112 typ: u32,123 typ: u32,
113124
114 /// if the kind_flag is set, offset contains both member bitfield size and125 /// if the kind_flag is set, offset contains both member bitfield size and
115 /// bit offset, the bitfield size is set for bitfield members. If the type126 /// bit offset, the bitfield size is set for bitfield members. If the type
116 /// info kind_flag is not set, the offset contains only bit offset127 /// info kind_flag is not set, the offset contains only bit offset
117 offset: packed struct {128 offset: packed struct(u32) {
118 bit: u24,129 bit: u24,
119 bitfield_size: u8,130 bitfield_size: u8,
120 },131 },
121};132};
122133
123/// FuncProto is followed by multiple Params, the exact number is stored in vlen134/// func_proto is followed by multiple Params, the exact number is stored in vlen
124pub const Param = packed struct {135pub const Param = extern struct {
125 name_off: u32,136 name_off: u32,
126 typ: u32,137 typ: u32,
127};138};
...@@ -138,16 +149,26 @@ pub const FuncLinkage = enum {...@@ -138,16 +149,26 @@ pub const FuncLinkage = enum {
138 external,149 external,
139};150};
140151
141/// Var kind is followd by a single Var struct to describe additional152/// var kind is followd by a single Var struct to describe additional
142/// information related to the variable such as its linkage153/// information related to the variable such as its linkage
143pub const Var = packed struct {154pub const Var = extern struct {
144 linkage: u32,155 linkage: u32,
145};156};
146157
147/// Datasec kind is followed by multible VarSecInfo to describe all Var kind158/// datasec kind is followed by multible VarSecInfo to describe all Var kind
148/// types it contains along with it's in-section offset as well as size.159/// types it contains along with it's in-section offset as well as size.
149pub const VarSecInfo = packed struct {160pub const VarSecInfo = extern struct {
150 typ: u32,161 typ: u32,
151 offset: u32,162 offset: u32,
152 size: u32,163 size: u32,
153};164};
165
166// decl_tag kind is followed by a single DeclTag struct to describe
167// additional information related to the tag applied location.
168// If component_idx == -1, the tag is applied to a struct, union,
169// variable or function. Otherwise, it is applied to a struct/union
170// member or a func argument, and component_idx indicates which member
171// or argument (0 ... vlen-1).
172pub const DeclTag = extern struct {
173 component_idx: u32,
174};