All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Daniel Xu <dxu@dxuuu.xyz>
Cc: acme@kernel.org, quentin@isovalent.com,
	andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net,
	bpf@vger.kernel.org
Subject: Re: [PATCH dwarves v2] pahole: Inject kfunc decl tags into BTF
Date: Fri, 12 Jan 2024 17:16:07 +0100	[thread overview]
Message-ID: <ZaFlx89aXd7eEO1P@krava> (raw)
In-Reply-To: <85caea4c48659502544329e6cd8b41c12ab50dfc.1704929857.git.dxu@dxuuu.xyz>

On Wed, Jan 10, 2024 at 06:14:25PM -0700, Daniel Xu wrote:

SNIP

> +
> +static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder)
> +{
> +	const char *filename = encoder->filename;
> +	Elf_Scn *symscn = NULL;
> +	int symbols_shndx = -1;
> +	int fd = -1, err = -1;
> +	int idlist_shndx = -1;
> +	Elf_Scn *scn = NULL;
> +	size_t idlist_addr;
> +	Elf_Data *symbols;
> +	Elf_Data *idlist;
> +	size_t strtabidx;
> +	Elf *elf = NULL;
> +	int set_cnt = 0;
> +	GElf_Shdr shdr;
> +	size_t strndx;
> +	char *secname;
> +	int nr_syms;
> +	int i = 0;

smooth ;-)

SNIP

> +
> +	nr_syms = shdr.sh_size / shdr.sh_entsize;
> +	for (i = 0; i < nr_syms; i++) {
> +		char *kfunc, *name;
> +		int new_set_cnt;
> +		GElf_Sym sym;
> +		int err;
> +
> +		if (!gelf_getsym(symbols, i, &sym)) {
> +			elf_error("Failed to get ELF symbol(%d)", i);
> +			goto out;
> +		}
> +
> +		if (sym.st_shndx != idlist_shndx)
> +			continue;
> +
> +		name = elf_strptr(elf, strtabidx, sym.st_name);
> +		new_set_cnt = get_kfunc_set_cnt(&sym, name, idlist, idlist_addr);
> +		if (new_set_cnt < 0) {
> +			err = new_set_cnt;
> +			goto out;
> +		} else if (new_set_cnt) {
> +			if (set_cnt)
> +				fprintf(stderr, "%s: warning: overlapping set8 '%s'\n",
> +					__func__, name);
> +			set_cnt = new_set_cnt;
> +			continue;
> +		}
> +
> +		if (!set_cnt)
> +			continue;
> +		set_cnt--;
> +
> +		kfunc = get_kfunc_name(name);
> +		if (!kfunc)
> +			continue;
> +
> +		err = btf_encoder__tag_kfunc(encoder, kfunc);

are .BTF_ids records guaranteed to be sorted by address? so we are
sure that the set will be followed by its records?

I thought we'd need to find size for each set and then check each
.BTF_ids record if it belongs to the set

jirka


> +		if (err) {
> +			fprintf(stderr, "%s: failed to tag kfunc '%s'\n", __func__, kfunc);
> +			free(kfunc);
> +			goto out;
> +		}
> +		free(kfunc);
> +	}
> +
> +	err = 0;
> +out:
> +	if (elf)
> +		elf_end(elf);
> +	if (fd != -1)
> +		close(fd);
> +	return err;
> +}
> +
>  int btf_encoder__encode(struct btf_encoder *encoder)
>  {
>  	int err;
> @@ -1366,6 +1681,14 @@ int btf_encoder__encode(struct btf_encoder *encoder)
>  	if (btf__type_cnt(encoder->btf) == 1)
>  		return 0;
>  
> +	/* Note vmlinux may already contain btf_decl_tag's for kfuncs. So
> +	 * take care to call this before btf_dedup().
> +	 */
> +	if (btf_encoder__tag_kfuncs(encoder)) {
> +		fprintf(stderr, "%s: failed to tag kfuncs!\n", __func__);
> +		return -1;
> +	}
> +
>  	if (btf__dedup(encoder->btf, NULL)) {
>  		fprintf(stderr, "%s: btf__dedup failed!\n", __func__);
>  		return -1;
> @@ -1712,6 +2035,7 @@ void btf_encoder__delete(struct btf_encoder *encoder)
>  
>  	btf_encoders__delete(encoder);
>  	__gobuffer__delete(&encoder->percpu_secinfo);
> +	__gobuffer__delete(&encoder->btf_funcs);
>  	zfree(&encoder->filename);
>  	btf__free(encoder->btf);
>  	encoder->btf = NULL;
> -- 
> 2.42.1
> 

  reply	other threads:[~2024-01-12 16:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  1:14 [PATCH dwarves v2] pahole: Inject kfunc decl tags into BTF Daniel Xu
2024-01-12 16:16 ` Jiri Olsa [this message]
2024-01-12 20:41   ` Daniel Xu
2024-01-13  4:31     ` Daniel Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZaFlx89aXd7eEO1P@krava \
    --to=olsajiri@gmail.com \
    --cc=acme@kernel.org \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dxu@dxuuu.xyz \
    --cc=quentin@isovalent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.