Re: [PATCH 3/5] objtool: Avoid O(bloody terrible) behaviour -- an ode to libelf

From: Josh Poimboeuf
Date: Wed Nov 02 2022 - 18:22:30 EST


On Fri, Oct 28, 2022 at 09:40:25PM +0200, Peter Zijlstra wrote:
> Due to how gelf_update_sym*() requires an Elf_Data pointer, and how
> libelf keeps Elf_Data in a linked list per section,
> elf_update_symbol() ends up having to iterate this list on each
> update to find the correct Elf_Data for the index'ed symbol.
>
> By allocating one Elf_Data per new symbol, the list grows per new
> symbol, giving an effective O(n^2) insertion time. This is obviously
> bloody terrible.
>
> Therefore over-allocate the Elf_Data when an extention is needed.
> Except it turns out libelf disregards Elf_Scn::sh_size in favour of
> the sum of Elf_Data::d_size. IOW it will happily write out all the
> unused space and fill it with:
>
> 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
>
> entries (aka zeros). Which obviously violates the STB_LOCAL placement
> rule, and is a general pain in the backside for not being the desired
> behaviour.
>
> Manually fix-up the Elf_Data size to avoid this problem before calling
> elf_update().
>
> This significantly improves performance when adding a significant
> number of symbols.
>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>

Instead of going through libelf to add each symbol, and
adjusting/shifting/reallocating the d_buf one symbol at a time, it would
probably be a lot easier (and faster) to just manually do all that at
the end, just before writing the file.

See for example what kpatch does:

https://github.com/dynup/kpatch/blob/master/kpatch-build/kpatch-elf.c#L725

--
Josh