[PATCH v2] elf: Add optimization barrier for __ehdr_start and _end

Alexander Monakov amonakov@ispras.ru
Mon Jun 16 06:55:38 GMT 2025


On Mon, 16 Jun 2025, Jakub Jelinek wrote:

> On Mon, Jun 16, 2025 at 09:42:54AM +0300, Alexander Monakov wrote:
> > 
> > 
> > On Mon, 16 Jun 2025, Jakub Jelinek wrote:
> > 
> > > On Mon, Jun 16, 2025 at 09:23:49AM +0300, Alexander Monakov wrote:
> > > > But you are applying it after the assignment into _dl_rtld_map, which is too
> > > > late.  As written, it doesn't prevent vectorization.  I think Jakub's suggestion
> > > > was to use an asm to implement a non-transparent copy of the value (before use).
> > > 
> > > That is indeed what I meant initially, but I think "+g" on the memory is
> > > actually better, it does prevent vectorization of the stores because one
> > > address has to be stored before the other one, but in a way which will allow
> > > on some architectures e.g. direct store of the __ehdr_start address to the
> > > memory.
> > 
> > The patch is
> > 
> >    _dl_rtld_map.l_map_start = (ElfW(Addr)) &__ehdr_start;
> > +  asm ("" : "+g" (_dl_rtld_map.l_map_start));
> >    _dl_rtld_map.l_map_end = (ElfW(Addr)) _end;
> > +  asm ("" : "+g" (_dl_rtld_map.l_map_end));
> > 
> > Computing the address of _end is independent of storing into l_map_start, so
> > this is functionally identical to
> > 
> >    _dl_rtld_map.l_map_start = (ElfW(Addr)) &__ehdr_start;
> >    _dl_rtld_map.l_map_end = (ElfW(Addr)) _end;
> > +  asm ("" : "+g" (_dl_rtld_map.l_map_start));
> > +  asm ("" : "+g" (_dl_rtld_map.l_map_end));
> > 
> > and hence vectorization of stores is not prevented.
> 
> So just make it
>    _dl_rtld_map.l_map_start = (ElfW(Addr)) &__ehdr_start;
> +  asm ("" : "=m" (_dl_rtld_map) : "m" (_dl_rtld_map));
>    _dl_rtld_map.l_map_end = (ElfW(Addr)) _end;
> +  asm ("" : "=m" (_dl_rtld_map) : "m" (_dl_rtld_map));

In this case, either just the first asm is sufficient to break up this
vectorizable pair, or additional such asm is necessary in the beginning to
prevent forming a pair with l_map_start and the preceding field, just like
the second asm prevents forming a pair out of l_map_end and the next field.

Alexander


More information about the Libc-alpha mailing list