Skip to content

Commit

Permalink
btf_encoder: handle .BTF_ids section endianness
Browse files Browse the repository at this point in the history
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).

Fixes: 72e88f2 ("pahole: Inject kfunc decl tags into BTF")
Reviewed-by: Vadim Fedorenko <[email protected]>
Signed-off-by: Eduard Zingerman <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Alan Maguire <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Andrii Nakryiko <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Cc: Daniel Xu <[email protected]>
Cc: [email protected]
Cc: Kumar Kartikeya Dwivedi <[email protected]>
Cc: Vadim Fedorenko <[email protected]>
Cc: Yonghong Song <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
eddyz87 authored and acmel committed Nov 28, 2024
1 parent 12ca112 commit 3ddadc1
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions btf_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3ddadc1

Please sign in to comment.