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

Alexander Monakov amonakov@ispras.ru
Mon Jun 16 06:42:54 GMT 2025



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.

Alexander


More information about the Libc-alpha mailing list