History

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.

Ian Rogers committed 3 years ago

Commit 23671f41 parent 0d7ae061 file · +9 −6Browse files

Unified · Split

tools/bpf/bpftool/gen.c+9−6
@@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv) · 1208 unmodified lines
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,12 +1218,12 @@ static int do_skeleton(int argc, char **argv) · 2 unmodified lines
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);
@@ -1231,6 +1231,9 @@ static int do_skeleton(int argc, char **argv) · 1 unmodified line
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\

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t