Re: [PATCH 3/3] btf_encoder: Set .BTF section alignment to 16

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jan 21, 2021 at 3:07 AM Giuliano Procida <gprocida@xxxxxxxxxx> wrote:
>
> Hi.
>
> On Thu, 21 Jan 2021 at 07:16, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
>>
>> On Mon, Jan 18, 2021 at 8:01 AM Giuliano Procida <gprocida@xxxxxxxxxx> wrote:
>> >
>> > This is to avoid misaligned access when memory-mapping ELF sections.
>> >
>> > Signed-off-by: Giuliano Procida <gprocida@xxxxxxxxxx>
>> > ---
>> >  libbtf.c | 8 ++++++++
>> >  1 file changed, 8 insertions(+)
>> >
>> > diff --git a/libbtf.c b/libbtf.c
>> > index 7552d8e..2f12d53 100644
>> > --- a/libbtf.c
>> > +++ b/libbtf.c
>> > @@ -797,6 +797,14 @@ static int btf_elf__write(const char *filename, struct btf *btf)
>> >                         goto unlink;
>> >                 }
>> >
>> > +               snprintf(cmd, sizeof(cmd), "%s --set-section-alignment .BTF=16 %s",
>> > +                        llvm_objcopy, filename);
>>
>> does it align inside the ELF file to 16 bytes, or does it request the
>> linker to align it at 16 byte alignment in memory? Given .BTF section
>> is not loadable, trying to understand the implications.
>>
>
> We have a tool that loads BTF from ELF files. It uses mmap and "parses" the BTF as structs in memory. The ELF file is mapped with page alignment but the BTF section within it has no alignment at all. Using MSAN (IIRC) we get warnings about misaligned accesses. Everything within BTF itself is naturally aligned, so it makes sense to align the section within ELF as well. There are probably some architectures where this makes the difference between working and SIGBUS.
>

Right, ok, thanks for explaining!

> I did try to get objcopy to set alignment at the point the section is added. However, this didn't work.
>
>>
>>
>> > +               if (system(cmd)) {
>>
>> Also curious, if objcopy emits error (saying that
>> --set-section-alignment argument is not recognized), will that error
>> be shown in stdout? or system() consumes it without redirecting it to
>> stdout?
>>
>
> I believe it goes to stderr. I would need to check. system() will not consume this. I'm not keen to write stderr (or stdout) post-processing code in plain C.
>

You can use popen() to capture/hide output, this is a better
alternative to system() in this case. We don't want "expected
warnings" in kernel build process.

>>
>> > +                       /* non-fatal, this is a nice-to-have and it's only supported from LLVM 10 */
>> > +                       fprintf(stderr, "%s: warning: failed to align .BTF section in '%s': %d!\n",
>> > +                               __func__, filename, errno);
>>
>> Probably better to emit this warning only in verbose mode, otherwise
>> lots of people will start complaining that they get some new warnings
>> from pahole.
>>
>
> It may be better to just use POSIX and ELF APIs directly instead of objcopy. This way the section can be added with the right alignment directly. pahole is already linked against libelf and if we could get rid of the external dependency on objcopy it would be a win in more than one way.

This would be great, yes. At some point I remember giving it a try,
but for some reason I couldn't make libelf flush data and update
section headers properly. Maybe you'll have better luck. Though I
think I was trying to mark section loadable, and eventually I probably
managed to do that, but still abandoned it (it's not enough to mark
section loadable, you have to assign it to ELF segment as well, which
libelf doesn't allow to do and you need linker support). Anyways, give
it a try, it should work.

>
>>
>>
>> > +               }
>> > +
>> >                 err = 0;
>> >         unlink:
>> >                 unlink(tmp_fn);
>> > --
>> > 2.30.0.284.gd98b1dd5eaa7-goog
>> >
>
>
> I'll see if I can spend a little time on this idea instead.
>
> Regards,
> Giuliano.




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux