Skip to content

Commit e8b7df5

Browse files
captain5050qmonnet
authored andcommitted
bpftool: Align output skeleton ELF code
libbpf accesses the ELF data requiring at least 8 byte alignment, however, the data is generated into a C string that doesn't guarantee alignment. Fix this by assigning to an aligned char array. Use sizeof on the array, less one for the \0 terminator, rather than generating a constant. Fixes: a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data") Signed-off-by: Ian Rogers <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Alan Maguire <[email protected]> Acked-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6982b3e commit e8b7df5

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/gen.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv)
12091209
codegen("\
12101210
\n\
12111211
\n\
1212-
s->data = %2$s__elf_bytes(&s->data_sz); \n\
1212+
s->data = %1$s__elf_bytes(&s->data_sz); \n\
12131213
\n\
12141214
obj->skeleton = s; \n\
12151215
return 0; \n\
@@ -1218,19 +1218,22 @@ static int do_skeleton(int argc, char **argv)
12181218
return err; \n\
12191219
} \n\
12201220
\n\
1221-
static inline const void *%2$s__elf_bytes(size_t *sz) \n\
1221+
static inline const void *%1$s__elf_bytes(size_t *sz) \n\
12221222
{ \n\
1223-
*sz = %1$d; \n\
1224-
return (const void *)\"\\ \n\
1225-
"
1226-
, file_sz, obj_name);
1223+
static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
1224+
",
1225+
obj_name
1226+
);
12271227

12281228
/* embed contents of BPF object file */
12291229
print_hex(obj_data, file_sz);
12301230

12311231
codegen("\
12321232
\n\
12331233
\"; \n\
1234+
\n\
1235+
*sz = sizeof(data) - 1; \n\
1236+
return (const void *)data; \n\
12341237
} \n\
12351238
\n\
12361239
#ifdef __cplusplus \n\

0 commit comments

Comments
 (0)