mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-05 08:09:07 +08:00
libbpf: Fix dumping big-endian bitfields
On big-endian arches not only bytes, but also bits are numbered in reverse order (see e.g. S/390 ELF ABI Supplement, but this is also true for other big-endian arches as well). Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20211013160902.428340-3-iii@linux.ibm.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
50028712c4
commit
fa93001e85
@@ -1562,29 +1562,28 @@ static int btf_dump_get_bitfield_value(struct btf_dump *d,
|
|||||||
__u64 *value)
|
__u64 *value)
|
||||||
{
|
{
|
||||||
__u16 left_shift_bits, right_shift_bits;
|
__u16 left_shift_bits, right_shift_bits;
|
||||||
__u8 nr_copy_bits, nr_copy_bytes;
|
|
||||||
const __u8 *bytes = data;
|
const __u8 *bytes = data;
|
||||||
int sz = t->size;
|
__u8 nr_copy_bits;
|
||||||
__u64 num = 0;
|
__u64 num = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Maximum supported bitfield size is 64 bits */
|
/* Maximum supported bitfield size is 64 bits */
|
||||||
if (sz > 8) {
|
if (t->size > 8) {
|
||||||
pr_warn("unexpected bitfield size %d\n", sz);
|
pr_warn("unexpected bitfield size %d\n", t->size);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bitfield value retrieval is done in two steps; first relevant bytes are
|
/* Bitfield value retrieval is done in two steps; first relevant bytes are
|
||||||
* stored in num, then we left/right shift num to eliminate irrelevant bits.
|
* stored in num, then we left/right shift num to eliminate irrelevant bits.
|
||||||
*/
|
*/
|
||||||
nr_copy_bits = bit_sz + bits_offset;
|
|
||||||
nr_copy_bytes = t->size;
|
|
||||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||||
for (i = nr_copy_bytes - 1; i >= 0; i--)
|
for (i = t->size - 1; i >= 0; i--)
|
||||||
num = num * 256 + bytes[i];
|
num = num * 256 + bytes[i];
|
||||||
|
nr_copy_bits = bit_sz + bits_offset;
|
||||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||||
for (i = 0; i < nr_copy_bytes; i++)
|
for (i = 0; i < t->size; i++)
|
||||||
num = num * 256 + bytes[i];
|
num = num * 256 + bytes[i];
|
||||||
|
nr_copy_bits = t->size * 8 - bits_offset;
|
||||||
#else
|
#else
|
||||||
# error "Unrecognized __BYTE_ORDER__"
|
# error "Unrecognized __BYTE_ORDER__"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user