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 @@
11const std = @import("../../../std.zig");
22
3const magic = 0xeb9f;
4const version = 1;
3pub const magic = 0xeb9f;
4pub const version = 1;
55
66pub const ext = @import("btf_ext.zig");
77
88/// All offsets are in bytes relative to the end of this header
9pub const Header = packed struct {
9pub const Header = extern struct {
1010 magic: u16,
1111 version: u8,
1212 flags: u8,
......@@ -34,15 +34,15 @@ pub const max_name_offset = 0xffffff;
3434/// Max number of struct/union/enum member of func args
3535pub const max_vlen = 0xffff;
3636
37pub const Type = packed struct {
37pub const Type = extern struct {
3838 name_off: u32,
39 info: packed struct {
39 info: packed struct(u32) {
4040 /// number of struct's members
4141 vlen: u16,
4242
4343 unused_1: u8,
4444 kind: Kind,
45 unused_2: u3,
45 unused_2: u2,
4646
4747 /// used by Struct, Union, and Fwd
4848 kind_flag: bool,
......@@ -53,31 +53,35 @@ pub const Type = packed struct {
5353 ///
5454 /// type is used by Ptr, Typedef, Volatile, Const, Restrict, Func,
5555 /// 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 },
5757};
5858
5959/// For some kinds, Type is immediately followed by extra data
60pub const Kind = enum(u4) {
60pub const Kind = enum(u5) {
6161 unknown,
6262 int,
6363 ptr,
6464 array,
65 structure,
66 kind_union,
67 enumeration,
65 @"struct",
66 @"union",
67 @"enum",
6868 fwd,
6969 typedef,
70 kind_volatile,
71 constant,
70 @"volatile",
71 @"const",
7272 restrict,
7373 func,
74 funcProto,
75 variable,
76 dataSec,
74 func_proto,
75 @"var",
76 datasec,
77 float,
78 decl_tag,
79 type_tag,
80 enum64,
7781};
7882
79/// Int kind is followed by this struct
80pub const IntInfo = packed struct {
83/// int kind is followed by this struct
84pub const IntInfo = packed struct(u32) {
8185 bits: u8,
8286 unused: u8,
8387 offset: u8,
......@@ -92,36 +96,43 @@ test "IntInfo is 32 bits" {
9296 try std.testing.expectEqual(@bitSizeOf(IntInfo), 32);
9397}
9498
95/// Enum kind is followed by this struct
96pub const Enum = packed struct {
99/// enum kind is followed by this struct
100pub const Enum = extern struct {
97101 name_off: u32,
98102 val: i32,
99103};
100104
101/// Array kind is followd by this struct
102pub const Array = packed struct {
105/// enum64 kind is followed by this 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 {
103114 typ: u32,
104115 index_type: u32,
105116 nelems: u32,
106117};
107118
108/// Struct and Union kinds are followed by multiple Member structs. The exact
119/// struct and union kinds are followed by multiple Member structs. The exact
109120/// number is stored in vlen
110pub const Member = packed struct {
121pub const Member = extern struct {
111122 name_off: u32,
112123 typ: u32,
113124
114125 /// if the kind_flag is set, offset contains both member bitfield size and
115126 /// bit offset, the bitfield size is set for bitfield members. If the type
116127 /// info kind_flag is not set, the offset contains only bit offset
117 offset: packed struct {
128 offset: packed struct(u32) {
118129 bit: u24,
119130 bitfield_size: u8,
120131 },
121132};
122133
123/// FuncProto is followed by multiple Params, the exact number is stored in vlen
124pub const Param = packed struct {
134/// func_proto is followed by multiple Params, the exact number is stored in vlen
135pub const Param = extern struct {
125136 name_off: u32,
126137 typ: u32,
127138};
......@@ -138,16 +149,26 @@ pub const FuncLinkage = enum {
138149 external,
139150};
140151
141/// Var kind is followd by a single Var struct to describe additional
152/// var kind is followd by a single Var struct to describe additional
142153/// information related to the variable such as its linkage
143pub const Var = packed struct {
154pub const Var = extern struct {
144155 linkage: u32,
145156};
146157
147/// Datasec kind is followed by multible VarSecInfo to describe all Var kind
158/// datasec kind is followed by multible VarSecInfo to describe all Var kind
148159/// types it contains along with it's in-section offset as well as size.
149pub const VarSecInfo = packed struct {
160pub const VarSecInfo = extern struct {
150161 typ: u32,
151162 offset: u32,
152163 size: u32,
153164};
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};