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
1209 codegen("\1209 codegen("\
1210 \n\1210 \n\
1211 \n\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 \n\1213 \n\
1214 obj->skeleton = s; \n\1214 obj->skeleton = s; \n\
1215 return 0; \n\1215 return 0; \n\
@@ -1218,12 +1218,12 @@ static int do_skeleton(int argc, char **argv) · 2 unmodified lines
1218 return err; \n\1218 return err; \n\
1219 } \n\1219 } \n\
1220 \n\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 { \n\1222 { \n\
1223 *sz = %1$d; \n\1223 static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
1224 return (const void *)\"\\ \n\1224 ",
1225 "1225 obj_name
1226 , file_sz, obj_name);1226 );
1227 1227
1228 /* embed contents of BPF object file */1228 /* embed contents of BPF object file */
1229 print_hex(obj_data, file_sz);1229 print_hex(obj_data, file_sz);
@@ -1231,6 +1231,9 @@ static int do_skeleton(int argc, char **argv) · 1 unmodified line
1231 codegen("\1231 codegen("\
1232 \n\1232 \n\
1233 \"; \n\1233 \"; \n\
1234 \n\
1235 *sz = sizeof(data) - 1; \n\
1236 return (const void *)data; \n\
1234 } \n\1237 } \n\
1235 \n\1238 \n\
1236 #ifdef __cplusplus \n\1239 #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