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.
Unified · Split
| @@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv) · 1208 unmodified lines | ||
| 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,12 +1218,12 @@ static int do_skeleton(int argc, char **argv) · 2 unmodified lines | ||
| 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,6 +1231,9 @@ static int do_skeleton(int argc, char **argv) · 1 unmodified line | ||
| 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\ |