Subject: Re: [PATCH bpf-next] tools/resolve_btfids: Fix sections with wrong alignment

From: Jiri Olsa <[email-masked]>
Date: Thu, 20 Aug 2020 12:18:10 +0200

On Wed, Aug 19, 2020 at 08:23:10PM -0700, Yonghong Song wrote:
> 
> 
> On 8/19/20 7:27 PM, Fāng-ruì Sòng wrote:
> > > > >     section(36) .comment, size 44, link 0, flags 30, type=1
> > > > >     section(37) .debug_aranges, size 45684, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 16, expected 8
> > > > >     section(38) .debug_info, size 129104957, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 1, expected 8
> > > > >     section(39) .debug_abbrev, size 1152583, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 1, expected 8
> > > > >     section(40) .debug_line, size 7374522, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 1, expected 8
> > > > >     section(41) .debug_frame, size 702463, link 0, flags 800, type=1
> > > > >     section(42) .debug_str, size 1017571, link 0, flags 830, type=1
> > > > >      - fixing wrong alignment sh_addralign 1, expected 8
> > > > >     section(43) .debug_loc, size 3019453, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 1, expected 8
> > > > >     section(44) .debug_ranges, size 1744583, link 0, flags 800, type=1
> > > > >      - fixing wrong alignment sh_addralign 16, expected 8
> > > > >     section(45) .symtab, size 2955888, link 46, flags 0, type=2
> > > > >     section(46) .strtab, size 2613072, link 0, flags 0, type=3
> > 
> > I think this is resolve_btfids's bug. GNU ld and LLD are innocent.
> > These .debug_* sections work fine if their sh_addralign is 1.
> > When the section flag SHF_COMPRESSED is set, the meaningful alignment
> > is Elf64_Chdr::ch_addralign, after the header is uncompressed.
> > 
> > On Wed, Aug 19, 2020 at 2:30 PM Yonghong Song <[email-masked]> wrote:
> > > 
> > > 
> > > 
> > > On 8/19/20 11:16 AM, Nick Desaulniers wrote:
> > > > On Wed, Aug 19, 2020 at 10:36 AM Jiri Olsa <[email-masked]> wrote:
> > > > > 
> > > > > On Wed, Aug 19, 2020 at 08:31:51AM -0700, Yonghong Song wrote:
> > > > > > 
> > > > > > 
> > > > > > On 8/19/20 2:23 AM, Jiri Olsa wrote:
> > > > > > > The data of compressed section should be aligned to 4
> > > > > > > (for 32bit) or 8 (for 64 bit) bytes.
> > > > > > > 
> > > > > > > The binutils ld sets sh_addralign to 1, which makes libelf
> > > > > > > fail with misaligned section error during the update as
> > > > > > > reported by Jesper:
> > > > > > > 
> > > > > > >       FAILED elf_update(WRITE): invalid section alignment
> 
> Jiri,
> 
> Since Fangrui mentioned this is not a ld/lld bug, then changing
> alighment from 1 to 4 might have some adverse effect for the binary,
> I guess.

not sure about that.. Mark? ;-)

> 
> Do you think we could skip these .debug_* sections somehow in elf parsing in
> resolve_btfids? resolve_btfids does not need to read
> these sections. This way, no need to change their alignment either.

I'm don't think libelf interface allows for that, will check

jirka