Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <irogers@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20231007044439.25171-1-irogers@google.com

authored by

Ian Rogers and committed by
Andrii Nakryiko
23671f4d 0d7ae068

+9 -6
+9 -6
tools/bpf/bpftool/gen.c
··· 1209 1209 codegen("\ 1210 1210 \n\ 1211 1211 \n\ 1212 - s->data = %2$s__elf_bytes(&s->data_sz); \n\ 1212 + s->data = %1$s__elf_bytes(&s->data_sz); \n\ 1213 1213 \n\ 1214 1214 obj->skeleton = s; \n\ 1215 1215 return 0; \n\ ··· 1218 1218 return err; \n\ 1219 1219 } \n\ 1220 1220 \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\ 1222 1222 { \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 + ); 1227 1227 1228 1228 /* embed contents of BPF object file */ 1229 1229 print_hex(obj_data, file_sz); ··· 1231 1231 codegen("\ 1232 1232 \n\ 1233 1233 \"; \n\ 1234 + \n\ 1235 + *sz = sizeof(data) - 1; \n\ 1236 + return (const void *)data; \n\ 1234 1237 } \n\ 1235 1238 \n\ 1236 1239 #ifdef __cplusplus \n\