btf_encoder: handle .BTF_ids section endianness
Eduard Zingerman
maintainerv3 cover btf_encoder: handle .BTF_ids section endianness
Currently, pahole does not generate kfunc declaration tags when generating BTF for ELF files with an endianness different from the host system. For example, this issue occurs when processing a vmlinux built for s390 on an x86 host.
To reproduce the bug:
- follow the instructions in [0] to build an s390 vmlinux;
- generate BTF requesting declaration tags for kfuncs:
$ pahole --btf_features_strict=decl_tag_kfuncs,decl_tag \
--btf_encode_detached=test.btf vmlinux
- observe that no kfuncs are generated:
$ bpftool btf dump file test.btf format c | grep __ksymThis patch resolves the issue by adding the necessary byte-swapping operations.
Changelog:
- v1 [1] -> v2:
- avoid modifying the 'idlist' Elf_Data object directly.
Instead, use struct local_elf_data (suggested by Jiri);
- update the description of the .BTF_ids section (suggested by Jiri).
- v2 [2] -> v3:
- use elf_getdata_rawchunk() instead of direct conversion loop
(suggested by Andrii);- removed Jiri's acked-by and Vadim's reviewed-by, as patch had changed significantly.
[0] https://docs.kernel.org/bpf/s390.html
[1] https://lore.kernel.org/dwarves/[email protected]/
[2] https://lore.kernel.org/dwarves/[email protected]/
Eduard Zingerman (1):
btf_encoder: handle .BTF_ids section endianness
btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
Signature · 1 line
2.47.0
Eduard Zingerman
maintainerPATCH v3 1/1 btf_encoder: handle .BTF_ids section endianness
btf_encoder__tag_kfuncs() reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays of uint32_t elements;
- arrays of records with the following structure:
struct btf_id_and_flag {
uint32_t id;
uint32_t flags;};
When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using elf_getdata_rawchunk() function to read .BTF_ids section data. When called with ELF_T_WORD as 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).
--- btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
btf_encoder.c +20 -6
@@ -1904,18 +1904,32 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) goto out; } - data =elf_getdata (scn,0 ); -if (!data) { -elf_error ("Failed to get ELF section(%d) data" , i); - goto out; - } -if (shdr.sh_type ==SHT_SYMTAB ) { + data =elf_getdata (scn,0 ); +if (!data) { +elf_error ("Failed to get ELF section(%d) data" , i); + goto out; + } + symbols_shndx = i; symscn = scn; symbols = data; strtabidx = shdr.sh_link; }else if (!strcmp (secname,BTF_IDS_SECTION )) { +/* .BTF_ids section consists of uint32_t elements, +* and thus might need byte order conversion. +* However, it has type PROGBITS, hence elf_getdata() +* won't automatically do the conversion. +* Use elf_getdata_rawchunk() instead, +* ELF_T_WORD tells it to do the necessary conversion. +*/ + data =elf_getdata_rawchunk (elf, shdr.sh_offset, shdr.sh_size,ELF_T_WORD ); +if (!data) { +elf_error ("Failed to get %s ELF section(%d) data" , +BTF_IDS_SECTION , i); + goto out; + } + idlist_shndx = i; idlist_addr = shdr.sh_addr; idlist = data;
Signature · 1 line
2.47.0
Quoted 56 lines · Eduard Zingerman
btf_encoder__tag_kfuncs()reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays ofuint32_telements;
- arrays of records with the following structure:struct btf_id_and_flag { uint32_t id; uint32_t flags;};
When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using
elf_getdata_rawchunk()function to read .BTF_ids section data. When called withELF_T_WORDas 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).Cc Alan Maguire <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Xu <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kumar Kartikeya Dwivedi <[email protected]>Cc Vadim Fedorenko <[email protected]>Fixes 72e88f29c6f7 ("pahole: Inject kfunc decl tags into BTF")Signed-off-by Eduard Zingerman <[email protected]>--- btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)btf_encoder.c +6
-6@@ -1904,18 +1904,32 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) goto out; } - data =elf_getdata (scn,0 ); -if (!data) { -elf_error ("Failed to get ELF section(%d) data" , i); - goto out; - } -if (shdr.sh_type ==SHT_SYMTAB ) { + data =elf_getdata (scn,0 ); +if (!data) { +elf_error ("Failed to get ELF section(%d) data" , i); + goto out; + } + symbols_shndx = i; symscn = scn; symbols = data;
btf_encoder.c static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) · line 1926
Quoted 10 lines
btf_encoder.c +8
-0strtabidx = shdr.sh_link; }else if (!strcmp (secname,BTF_IDS_SECTION )) { +/* .BTF_ids section consists of uint32_t elements, +* and thus might need byte order conversion. +* However, it has type PROGBITS, hence elf_getdata() +* won't automatically do the conversion. +* Use elf_getdata_rawchunk() instead, +* ELF_T_WORD tells it to do the necessary conversion. +*/ + data =elf_getdata_rawchunk (elf, shdr.sh_offset, shdr.sh_size,ELF_T_WORD );
looks good, I'm just curious about one thing..
so ELF_T_WORD enum has this comment: /* Elf32_Word, Elf64_Word, ... */
I did just quick check, ***so I might be easily wrong***, but I wonder the code in __elf_xfctstom (which I assume is the one called for conversion) chooses to swap 32/64 bits values based on elf->class .. so for 64bit ELF class we swap 64bit values? ... while .BTF_ids has always 32 bit values
thanks,
jirka
Quoted 12 lines
btf_encoder.c +6
-0+if (!data) { +elf_error ("Failed to get %s ELF section(%d) data" , +BTF_IDS_SECTION , i); + goto out; + } + idlist_shndx = i; idlist_addr = shdr.sh_addr; idlist = data;Signature · 1 line
2.47.0
Quoted 70 lines · Jiri Olsa
Quoted 56 lines · Eduard Zingerman
btf_encoder__tag_kfuncs()reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays ofuint32_telements;
- arrays of records with the following structure:struct btf_id_and_flag { uint32_t id; uint32_t flags; };When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using
elf_getdata_rawchunk()function to read .BTF_ids section data. When called withELF_T_WORDas 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).Cc Alan Maguire <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Xu <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kumar Kartikeya Dwivedi <[email protected]>Cc Vadim Fedorenko <[email protected]>Fixes 72e88f29c6f7 ("pahole: Inject kfunc decl tags into BTF")Signed-off-by Eduard Zingerman <[email protected]>--- btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)btf_encoder.c +6
-6@@ -1904,18 +1904,32 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) goto out; } - data =elf_getdata (scn,0 ); -if (!data) { -elf_error ("Failed to get ELF section(%d) data" , i); - goto out; - } -if (shdr.sh_type ==SHT_SYMTAB ) { + data =elf_getdata (scn,0 ); +if (!data) { +elf_error ("Failed to get ELF section(%d) data" , i); + goto out; + } + symbols_shndx = i; symscn = scn; symbols = data;btf_encoder.c static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) · line 1926
Quoted 10 lines
btf_encoder.c +8
-0strtabidx = shdr.sh_link; }else if (!strcmp (secname,BTF_IDS_SECTION )) { +/* .BTF_ids section consists of uint32_t elements, +* and thus might need byte order conversion. +* However, it has type PROGBITS, hence elf_getdata() +* won't automatically do the conversion. +* Use elf_getdata_rawchunk() instead, +* ELF_T_WORD tells it to do the necessary conversion. +*/ + data =elf_getdata_rawchunk (elf, shdr.sh_offset, shdr.sh_size,ELF_T_WORD );looks good, I'm just curious about one thing..
Quoted 6 lines
so
ELF_T_WORDenum has this comment: /*Elf32_Word,Elf64_Word, ... */I did just quick check, ***so I might be easily wrong***, but I wonder the code in
__elf_xfctstom(which I assume is the one called for conversion) chooses to swap 32/64 bits values based onelf->class.. so for 64bit ELF class we swap 64bit values? ... while .BTF_ids has always 32 bit values
Well according to the doc:
ELF_T_WORD Unsigned 32-bit words.
ELF_T_XWORD Unsigned 64-bit words.
It shouldn't use 64 bits swap:
const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
....
[ELF_T_WORD] = ElfW2(Bits, cvt_Word), [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword),
...
Are you looking somewhere else?
Quoted 16 lines
thanks,
jirkaQuoted 12 lines
btf_encoder.c +6
-0+if (!data) { +elf_error ("Failed to get %s ELF section(%d) data" , +BTF_IDS_SECTION , i); + goto out; + } + idlist_shndx = i; idlist_addr = shdr.sh_addr; idlist = data;Signature · 1 line
2.47.0
Jiri Olsa
Acked-byQuoted 84 lines · Vadim Fedorenko
Quoted 69 lines · Jiri Olsa
Quoted 55 lines · Eduard Zingerman
btf_encoder__tag_kfuncs()reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays ofuint32_telements;
- arrays of records with the following structure:struct btf_id_and_flag { uint32_t id; uint32_t flags; };When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using
elf_getdata_rawchunk()function to read .BTF_ids section data. When called withELF_T_WORDas 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).Cc Alan Maguire <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Xu <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kumar Kartikeya Dwivedi <[email protected]>Cc Vadim Fedorenko <[email protected]>Fixes 72e88f29c6f7 ("pahole: Inject kfunc decl tags into BTF")Signed-off-by Eduard Zingerman <[email protected]>--- btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)btf_encoder.c +6
-6@@ -1904,18 +1904,32 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) goto out; } - data =elf_getdata (scn,0 ); -if (!data) { -elf_error ("Failed to get ELF section(%d) data" , i); - goto out; - } -if (shdr.sh_type ==SHT_SYMTAB ) { + data =elf_getdata (scn,0 ); +if (!data) { +elf_error ("Failed to get ELF section(%d) data" , i); + goto out; + } + symbols_shndx = i; symscn = scn; symbols = data;btf_encoder.c static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) · line 1925
Quoted 10 lines
btf_encoder.c +8
-0strtabidx = shdr.sh_link; }else if (!strcmp (secname,BTF_IDS_SECTION )) { +/* .BTF_ids section consists of uint32_t elements, +* and thus might need byte order conversion. +* However, it has type PROGBITS, hence elf_getdata() +* won't automatically do the conversion. +* Use elf_getdata_rawchunk() instead, +* ELF_T_WORD tells it to do the necessary conversion. +*/ + data =elf_getdata_rawchunk (elf, shdr.sh_offset, shdr.sh_size,ELF_T_WORD );looks good, I'm just curious about one thing..
Quoted 6 lines
so
ELF_T_WORDenum has this comment: /*Elf32_Word,Elf64_Word, ... */I did just quick check, ***so I might be easily wrong***, but I wonder the code in
__elf_xfctstom(which I assume is the one called for conversion) chooses to swap 32/64 bits values based onelf->class.. so for 64bit ELF class we swap 64bit values? ... while .BTF_ids has always 32 bit valuesWell according to the doc:
ELF_T_WORDUnsigned 32-bit words.
ELF_T_XWORDUnsigned 64-bit words.It shouldn't use 64 bits swap:
Quoted 8 lines
const
xfct_t__elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
....[ELF_T_WORD] = ElfW2(Bits, cvt_Word), [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword),...
Are you looking somewhere else?
nah I guess I got confused with Elf64_Word, which is still 32bits, seems fine, sorry for noise
thanks,
jirka
Quoted 65 lines · Eduard Zingerman
btf_encoder__tag_kfuncs()reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays ofuint32_telements;
- arrays of records with the following structure:struct btf_id_and_flag { uint32_t id; uint32_t flags; };When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using
elf_getdata_rawchunk()function to read .BTF_ids section data. When called withELF_T_WORDas 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).Cc Alan Maguire <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Xu <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kumar Kartikeya Dwivedi <[email protected]>Cc Vadim Fedorenko <[email protected]>Fixes 72e88f29c6f7 ("pahole: Inject kfunc decl tags into BTF")Signed-off-by Eduard Zingerman <[email protected]>--- btf_encoder.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)btf_encoder.c +13
-6@@ -1904,18 +1904,32 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) goto out; } - data =elf_getdata (scn,0 ); -if (!data) { -elf_error ("Failed to get ELF section(%d) data" , i); - goto out; - } -if (shdr.sh_type ==SHT_SYMTAB ) { + data =elf_getdata (scn,0 ); +if (!data) { +elf_error ("Failed to get ELF section(%d) data" , i); + goto out; + } + symbols_shndx = i; symscn = scn; symbols = data; strtabidx = shdr.sh_link; }else if (!strcmp (secname,BTF_IDS_SECTION )) { +/* .BTF_ids section consists of uint32_t elements, +* and thus might need byte order conversion. +* However, it has type PROGBITS, hence elf_getdata() +* won't automatically do the conversion. +* Use elf_getdata_rawchunk() instead, +* ELF_T_WORD tells it to do the necessary conversion. +*/
btf_encoder.c static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) · line 1935
Quoted 10 lines
btf_encoder.c +7
-0+ data =elf_getdata_rawchunk (elf, shdr.sh_offset, shdr.sh_size,ELF_T_WORD ); +if (!data) { +elf_error ("Failed to get %s ELF section(%d) data" , +BTF_IDS_SECTION , i); + goto out; + } + idlist_shndx = i; idlist_addr = shdr.sh_addr; idlist = data;
Eduard Zingerman
maintainerOn Wed, 2024-11-27 at 12:03 +0000, Vadim Fedorenko wrote:
[...]
Quoted 16 lines
Quoted 8 lines
looks good, I'm just curious about one thing..
so
ELF_T_WORDenum has this comment: /*Elf32_Word,Elf64_Word, ... */I did just quick check, ***so I might be easily wrong***, but I wonder the code in
__elf_xfctstom(which I assume is the one called for conversion) chooses to swap 32/64 bits values based onelf->class.. so for 64bit ELF class we swap 64bit values? ... while .BTF_ids has always 32 bit valuesWell according to the doc:
ELF_T_WORDUnsigned 32-bit words.
ELF_T_XWORDUnsigned 64-bit words.It shouldn't use 64 bits swap:
Quoted 8 lines
const
xfct_t__elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
....[ELF_T_WORD] = ElfW2(Bits, cvt_Word), [ELF_T_XWORD] = ElfW2(Bits, cvt_Xword),...
Are you looking somewhere else?
Right, thank you, Vadim.
[...]
Quoted 30 lines · Vadim Fedorenko
Quoted 20 lines · Eduard Zingerman
btf_encoder__tag_kfuncs()reads .BTF_ids section to identify a set of kfuncs present in the ELF file being processed.
This section consists of:
- arrays ofuint32_telements;
- arrays of records with the following structure:struct btf_id_and_flag { uint32_t id; uint32_t flags; };When endianness of a binary operated by pahole differs from the host system's endianness, these fields require byte-swapping before use. Currently, this byte-swapping does not occur, resulting in kfuncs not being marked with declaration tags.
This commit resolves the issue by using
elf_getdata_rawchunk()function to read .BTF_ids section data. When called withELF_T_WORDas 'type' parameter it does necessary byte order conversion (only if host and elf endianness do not match).Quoted 8 lines
Cc Alan Maguire <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Xu <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kumar Kartikeya Dwivedi <[email protected]>Cc Vadim Fedorenko <[email protected]>Fixes 72e88f29c6f7 ("pahole: Inject kfunc decl tags into BTF")Signed-off-by Eduard Zingerman <[email protected]>
Quoted 1 line
Reviewed-by Vadim Fedorenko <[email protected]>
Tested:
acme@number:~/git/pahole$ tests/tests 1: Validation of BTF encoding of functions; this may take some time: Ok 2: Default BTF on a system without BTF: Ok 3: Flexible arrays accounting: Ok 4: Pretty printing of files using DWARF type information: Ok 5: Parallel reproducible DWARF Loading/Serial BTF encoding: Ok /home/acme/git/pahole acme@number:~/git/pahole$
And applied.
- Arnaldo
Eduard Zingerman
maintainerOn Thu, 2024-11-28 at 17:26 -0300, Arnaldo Carvalho de Melo wrote:
[...]
Quoted 11 lines
Tested:
acme@number:~/git/pahole$ tests/tests 1: Validation of BTF encoding of functions; this may take some time: Ok 2: Default BTF on a system without BTF: Ok 3: Flexible arrays accounting: Ok 4: Pretty printing of files using DWARF type information: Ok 5: Parallel reproducible DWARF Loading/Serial BTF encoding: Ok /home/acme/git/pahole acme@number:~/git/pahole$
Quoted 1 line
And applied.
Great, thank you!