Skip to content

Commit 0f99384

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
tools/bpftool: Generate data section struct with conservative alignment
The comment in the code describes this in good details. Generate such a memory layout that would work both on 32-bit and 64-bit architectures for user-space. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5705d70 commit 0f99384

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ static int codegen_datasec_def(struct bpf_object *obj,
143143
var_name, align);
144144
return -EINVAL;
145145
}
146+
/* Assume 32-bit architectures when generating data section
147+
* struct memory layout. Given bpftool can't know which target
148+
* host architecture it's emitting skeleton for, we need to be
149+
* conservative and assume 32-bit one to ensure enough padding
150+
* bytes are generated for pointer and long types. This will
151+
* still work correctly for 64-bit architectures, because in
152+
* the worst case we'll generate unnecessary padding field,
153+
* which on 64-bit architectures is not strictly necessary and
154+
* would be handled by natural 8-byte alignment. But it still
155+
* will be a correct memory layout, based on recorded offsets
156+
* in BTF.
157+
*/
158+
if (align > 4)
159+
align = 4;
146160

147161
align_off = (off + align - 1) / align * align;
148162
if (align_off != need_off) {

0 commit comments

Comments
 (0)