diff mbox series

[dwarves,v3,5/5] btf_encoder: Align .BTF section to 8 bytes

Message ID 20210205134221.2953163-6-gprocida@google.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series ELF writing changes | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Giuliano Procida Feb. 5, 2021, 1:42 p.m. UTC
This is to avoid misaligned access to BTF type structs when
memory-mapping ELF objects.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 libbtf.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Andrii Nakryiko Feb. 8, 2021, 10:29 p.m. UTC | #1
On Fri, Feb 5, 2021 at 5:42 AM Giuliano Procida <gprocida@google.com> wrote:
>
> This is to avoid misaligned access to BTF type structs when
> memory-mapping ELF objects.
>
> Signed-off-by: Giuliano Procida <gprocida@google.com>
> ---

I trust you did verify that it actually works in cases where
previously .BTF was mis-aligned?

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  libbtf.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/libbtf.c b/libbtf.c
> index 9f4abb3..6754a17 100644
> --- a/libbtf.c
> +++ b/libbtf.c
> @@ -744,6 +744,14 @@ static int btf_elf__write(const char *filename, struct btf *btf)
>                 goto out;
>         }
>
> +       /*
> +        * We'll align .BTF to 8 bytes to cater for all architectures. It'd be
> +        * nice if we could fetch this value from somewhere. The BTF
> +        * specification does not discuss alignment and its trailing string
> +        * table is not currently padded to any particular alignment.
> +        */
> +       const size_t btf_alignment = 8;
> +
>         /*
>          * First we check if there is already a .BTF section present.
>          */
> @@ -821,6 +829,7 @@ static int btf_elf__write(const char *filename, struct btf *btf)
>                 elf_error("elf_getshdr(btf_scn) failed");
>                 goto out;
>         }
> +       btf_shdr.sh_addralign = btf_alignment;
>         btf_shdr.sh_entsize = 0;
>         btf_shdr.sh_flags = 0;
>         if (dot_btf_offset)
> --
> 2.30.0.478.g8a0d178c01-goog
>
Giuliano Procida Feb. 9, 2021, 3:05 p.m. UTC | #2
On Mon, 8 Feb 2021 at 22:30, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Feb 5, 2021 at 5:42 AM Giuliano Procida <gprocida@google.com> wrote:
> >
> > This is to avoid misaligned access to BTF type structs when
> > memory-mapping ELF objects.
> >
> > Signed-off-by: Giuliano Procida <gprocida@google.com>
> > ---
>
> I trust you did verify that it actually works in cases where
> previously .BTF was mis-aligned?
>

Yes. :-)

> Acked-by: Andrii Nakryiko <andrii@kernel.org>
>
> >  libbtf.c | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> >
> > diff --git a/libbtf.c b/libbtf.c
> > index 9f4abb3..6754a17 100644
> > --- a/libbtf.c
> > +++ b/libbtf.c
> > @@ -744,6 +744,14 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >                 goto out;
> >         }
> >
> > +       /*
> > +        * We'll align .BTF to 8 bytes to cater for all architectures. It'd be
> > +        * nice if we could fetch this value from somewhere. The BTF
> > +        * specification does not discuss alignment and its trailing string
> > +        * table is not currently padded to any particular alignment.
> > +        */
> > +       const size_t btf_alignment = 8;
> > +
> >         /*
> >          * First we check if there is already a .BTF section present.
> >          */
> > @@ -821,6 +829,7 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >                 elf_error("elf_getshdr(btf_scn) failed");
> >                 goto out;
> >         }
> > +       btf_shdr.sh_addralign = btf_alignment;
> >         btf_shdr.sh_entsize = 0;
> >         btf_shdr.sh_flags = 0;
> >         if (dot_btf_offset)
> > --
> > 2.30.0.478.g8a0d178c01-goog
> >
>
> --
> To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com.
>
diff mbox series

Patch

diff --git a/libbtf.c b/libbtf.c
index 9f4abb3..6754a17 100644
--- a/libbtf.c
+++ b/libbtf.c
@@ -744,6 +744,14 @@  static int btf_elf__write(const char *filename, struct btf *btf)
 		goto out;
 	}
 
+	/*
+	 * We'll align .BTF to 8 bytes to cater for all architectures. It'd be
+	 * nice if we could fetch this value from somewhere. The BTF
+	 * specification does not discuss alignment and its trailing string
+	 * table is not currently padded to any particular alignment.
+	 */
+	const size_t btf_alignment = 8;
+
 	/*
 	 * First we check if there is already a .BTF section present.
 	 */
@@ -821,6 +829,7 @@  static int btf_elf__write(const char *filename, struct btf *btf)
 		elf_error("elf_getshdr(btf_scn) failed");
 		goto out;
 	}
+	btf_shdr.sh_addralign = btf_alignment;
 	btf_shdr.sh_entsize = 0;
 	btf_shdr.sh_flags = 0;
 	if (dot_btf_offset)