From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Douglas RAILLARD <douglas.raillard@arm.com>
Cc: acme@redhat.com, dwarves@vger.kernel.org
Subject: Re: [PATCH v3 3/6] btf_loader.c: Infer alignment info
Date: Thu, 28 Oct 2021 10:15:07 -0300 [thread overview]
Message-ID: <YXqiW/8AM69NT2sx@kernel.org> (raw)
In-Reply-To: <20211028122710.881181-4-douglas.raillard@arm.com>
Em Thu, Oct 28, 2021 at 01:27:07PM +0100, Douglas RAILLARD escreveu:
> From: Douglas Raillard <douglas.raillard@arm.com>
>
> BTF does not carry alignment information, but it carries the offset in
> structs. This allows inferring the original alignment, yielding a C
> header dump that is not identical to the original C code, but is
> guaranteed to lead to the same memory layout.
>
> This allows using the output of pahole in another program to poke at
> memory, with the assurance that we will not read garbage.
>
> Note: Since the alignment is inferred from the offset, it sometimes
> happens that the offset was already correctly aligned, which means the
> inferred alignment will be smaller than in the original source. This
> does not impact the ability to read existing structs, but it could
> impact creating such struct if other client code expects higher
> alignment than the one exposed in the generated header.
This as well was already applied, I split it in two:
"core: Export tag__natural_alignment()"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?h=next&id=4db65fe0cd02b3cc49d4fc8ff6c4c21a9ddb3642
"btf_loader: Infer alignment info"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?h=next&id=836c139fdf6f2b13ec2e5513df881272bb78aeb4
> Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
> ---
> btf_loader.c | 36 ++++++++++++++++++++++++++++++++++++
> dwarves.c | 2 +-
> dwarves.h | 2 ++
> 3 files changed, 39 insertions(+), 1 deletion(-)
>
> diff --git a/btf_loader.c b/btf_loader.c
> index 9c2daee..2885252 100644
> --- a/btf_loader.c
> +++ b/btf_loader.c
> @@ -471,10 +471,37 @@ static int btf__load_sections(struct btf *btf, struct cu *cu)
> return btf__load_types(btf, cu);
> }
>
> +static uint32_t class__infer_alignment(uint32_t byte_offset,
> + uint32_t natural_alignment,
> + uint32_t smallest_offset)
> +{
> + uint32_t alignment = 0;
> + uint32_t offset_delta = byte_offset - smallest_offset;
> +
> + if (offset_delta) {
> + if (byte_offset % 2 == 0) {
> + /* Find the power of 2 immediately higher than
> + * offset_delta
> + */
> + alignment = 1 << (8 * sizeof(offset_delta) -
> + __builtin_clz(offset_delta));
> + } else {
> + alignment = 0;
> + }
> + }
> +
> + /* Natural alignment, nothing to do */
> + if (alignment <= natural_alignment || alignment == 1)
> + alignment = 0;
> +
> + return alignment;
> +}
> +
> static int class__fixup_btf_bitfields(struct tag *tag, struct cu *cu)
> {
> struct class_member *pos;
> struct type *tag_type = tag__type(tag);
> + uint32_t smallest_offset = 0;
>
> type__for_each_data_member(tag_type, pos) {
> struct tag *type = tag__strip_typedefs_and_modifiers(&pos->tag, cu);
> @@ -508,8 +535,17 @@ static int class__fixup_btf_bitfields(struct tag *tag, struct cu *cu)
> pos->byte_offset = pos->bit_offset / 8;
> }
> }
> +
> + pos->alignment = class__infer_alignment(pos->byte_offset,
> + tag__natural_alignment(type, cu),
> + smallest_offset);
> + smallest_offset = pos->byte_offset + pos->byte_size;
> }
>
> + tag_type->alignment = class__infer_alignment(tag_type->size,
> + tag__natural_alignment(tag, cu),
> + smallest_offset);
> +
> return 0;
> }
>
> diff --git a/dwarves.c b/dwarves.c
> index b6f2489..bb8af5b 100644
> --- a/dwarves.c
> +++ b/dwarves.c
> @@ -1515,7 +1515,7 @@ void class__find_holes(struct class *class)
>
> static size_t type__natural_alignment(struct type *type, const struct cu *cu);
>
> -static size_t tag__natural_alignment(struct tag *tag, const struct cu *cu)
> +size_t tag__natural_alignment(struct tag *tag, const struct cu *cu)
> {
> size_t natural_alignment = 1;
>
> diff --git a/dwarves.h b/dwarves.h
> index 30d33fa..c2fea0a 100644
> --- a/dwarves.h
> +++ b/dwarves.h
> @@ -1002,6 +1002,8 @@ struct type {
>
> void __type__init(struct type *type);
>
> +size_t tag__natural_alignment(struct tag *tag, const struct cu *cu);
> +
> static inline struct class *type__class(const struct type *type)
> {
> return (struct class *)type;
> --
> 2.25.1
--
- Arnaldo
next prev parent reply other threads:[~2021-10-28 13:15 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-28 12:27 [PATCH v3 0/6] Infer BTF alignment Douglas RAILLARD
2021-10-28 12:27 ` [PATCH v3 1/6] fprintf: Fix nested struct printing Douglas RAILLARD
2021-10-28 12:40 ` Arnaldo Carvalho de Melo
2021-10-28 12:27 ` [PATCH v3 2/6] btf_loader.c: Refactor class__fixup_btf_bitfields Douglas RAILLARD
2021-10-28 13:15 ` Arnaldo Carvalho de Melo
2021-10-28 12:27 ` [PATCH v3 3/6] btf_loader.c: Infer alignment info Douglas RAILLARD
2021-10-28 13:15 ` Arnaldo Carvalho de Melo [this message]
2021-10-28 12:27 ` [PATCH v3 4/6] dwarves_fprintf: Move cacheline_size into struct conf_fprintf Douglas RAILLARD
2021-10-28 13:18 ` Arnaldo Carvalho de Melo
2021-10-28 12:27 ` [PATCH v3 5/6] btf_loader.c: Propagate struct conf_load Douglas RAILLARD
2021-10-28 13:19 ` Arnaldo Carvalho de Melo
2021-10-28 12:27 ` [PATCH v3 6/6] btf_loader.c: Use cacheline size to infer alignment Douglas RAILLARD
2021-10-28 13:24 ` Arnaldo Carvalho de Melo
2021-10-28 14:12 ` Douglas Raillard
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=YXqiW/8AM69NT2sx@kernel.org \
--to=acme@kernel.org \
--cc=acme@redhat.com \
--cc=douglas.raillard@arm.com \
--cc=dwarves@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).