[PATCH v2] elf: Add optimization barrier for __ehdr_start and _end
Florian Weimer
fweimer@redhat.com
Mon Jun 16 06:58:17 GMT 2025
* Jakub Jelinek:
> 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));
This still does not prevent loading &__ehdr_start and &_end into a
vector register if there is sufficient register pressure, I think.
Thanks,
Florian
More information about the Libc-alpha
mailing list