Skip to content

Commit 4a1c9e5

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: remove linux/unaligned.h dependency for libbpf_sha256()
linux/unaligned.h include dependency is causing issues for libbpf's Github mirror due to {get,put}_unaligned_be32() usage. So get rid of it by implementing custom variants of those macros that will work both in kernel and Github mirror repos. Also switch round_up() to roundup(), as the former is not available in Github mirror (and is just a subtly more specific variant of roundup() anyways). Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20251001171326.3883055-6-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent a7f36f8 commit 4a1c9e5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

tools/lib/bpf/libbpf_utils.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <errno.h>
1414
#include <inttypes.h>
1515
#include <linux/kernel.h>
16-
#include <linux/unaligned.h>
1716

1817
#include "libbpf.h"
1918
#include "libbpf_internal.h"
@@ -149,6 +148,16 @@ const char *libbpf_errstr(int err)
149148
}
150149
}
151150

151+
#pragma GCC diagnostic push
152+
#pragma GCC diagnostic ignored "-Wpacked"
153+
struct __packed_u32 { __u32 __val; } __attribute__((packed));
154+
#pragma GCC diagnostic pop
155+
156+
#define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val))
157+
#define put_unaligned_be32(v, p) do { \
158+
((struct __packed_u32 *)(p))->__val = cpu_to_be32(v); \
159+
} while (0)
160+
152161
#define SHA256_BLOCK_LENGTH 64
153162
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
154163
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
@@ -232,7 +241,7 @@ void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
232241

233242
memcpy(final_data, data + len - final_len, final_len);
234243
final_data[final_len] = 0x80;
235-
final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
244+
final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
236245
memcpy(&final_data[final_len - 8], &bitcount, 8);
237246

238247
sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);

0 commit comments

Comments
 (0)