[PATCH v8 3/6] elf: Introduce _dl_map_segment_align hook for segment alignment tuning
WANG Rui
wangrui@loongson.cn
Fri Apr 10 03:18:56 GMT 2026
On Mon, Apr 6, 2026 at 4:03 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sun, Apr 5, 2026 at 11:54 AM WANG Rui <wangrui@loongson.cn> wrote:
> >
> > Introduce a new helper function, _dl_map_segment_align, to allow
> > architecture-specific adjustment of ELF load segment alignment during
> > object mapping.
> >
> > The generic ELF loader now calls this hook when determining the maximum
> > segment alignment. The generic implementation is a no-op and preserves
> > existing behavior.
> >
> > This provides a well-defined extension point for architectures that
> > need to adjust segment alignment policies (for example, to improve
> > mapping efficiency or enable platform-specific optimizations) without
> > embedding such logic directly in the generic loader.
> >
> > Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
> > Signed-off-by: WANG Rui <wangrui@loongson.cn>
> > ---
> > elf/dl-load.c | 4 ++++
> > sysdeps/generic/dl-map-segment-align.h | 26 ++++++++++++++++++++++++++
> > 2 files changed, 30 insertions(+)
> > create mode 100644 sysdeps/generic/dl-map-segment-align.h
> >
> > diff --git a/elf/dl-load.c b/elf/dl-load.c
> > index 7355eef8e76..f3d943e99c0 100644
> > --- a/elf/dl-load.c
> > +++ b/elf/dl-load.c
> > @@ -71,6 +71,7 @@ struct filebuf
> > #include <dl-dst.h>
> > #include <dl-load.h>
> > #include <dl-map-segments.h>
> > +#include <dl-map-segment-align.h>
> > #include <dl-unmap-segments.h>
> > #include <dl-machine-reject-phdr.h>
> > #include <dl-prop.h>
> > @@ -1171,6 +1172,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> >
> > /* Optimize a common case. */
> > c->prot = pf_to_prot (ph->p_flags);
> > +
> > + /* Architecture-specific adjustment of segment alignment. */
> > + p_align_max = _dl_map_segment_align (c, p_align_max);
> > break;
>
> Can you make THP to work for
>
> Program Headers:
> Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
> PHDR 0x000034 0x00400034 0x00400034 0x001a0 0x001a0 R 0x4
> INTERP 0x0001f8 0x004001f8 0x004001f8 0x0001a 0x0001a R 0x1
> [Requesting program interpreter: /libx32/ld-linux-x32.so.2]
> LOAD 0x000000 0x00400000 0x00400000 0x16d110 0x16d110 R 0x1000
> LOAD 0x16e000 0x0056e000 0x0056e000 0x1055d9 0x1055d9 R E 0x1000
> LOAD 0x273ebc 0x00674ebc 0x00674ebc 0x02540 0x039fc RW 0x1000
> DYNAMIC 0x273ec8 0x00674ec8 0x00674ec8 0x00110 0x00110 RW 0x4
> NOTE 0x0001d4 0x004001d4 0x004001d4 0x00024 0x00024 R 0x4
> NOTE 0x16d0b0 0x0056d0b0 0x0056d0b0 0x00060 0x00060 R 0x4
> TLS 0x273ebc 0x00674ebc 0x00674ebc 0x00000 0x0000c R 0x4
> GNU_PROPERTY 0x16d0b0 0x0056d0b0 0x0056d0b0 0x00040 0x00040 R 0x4
> GNU_EH_FRAME 0x1493e0 0x005493e0 0x005493e0 0x05b94 0x05b94 R 0x4
> GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x10
> GNU_RELRO 0x273ebc 0x00674ebc 0x00674ebc 0x00144 0x00144 R 0x1
>
> As an option, we can combine
>
> LOAD 0x000000 0x00400000 0x00400000 0x16d110 0x16d110 R 0x1000
> LOAD 0x16e000 0x0056e000 0x0056e000 0x1055d9 0x1055d9 R E 0x1000
>
> into a single RE THP map.
>
In GNU ld, x86 is the only target where separate-code is enabled by
default. As as result, there is a large body of existing binaries that
were built with this configuration, and their RE LOAD segments are not
2MB-aligned.
The condition for enabling the opt does not require the file offset or
virtual address to be zero, it only requires hugepge alignment. Zero
just happens to satisfy any alignment requirement perfectly. So
technically, merging the first R segment with the following RE segment
could create such an opportunity.
But that would obviously change the loading semantics expressed by the
ELF itself, since we would be merging the access permission of two
segments. At that point, it is no longer just about adjusting
aligiment. So the question is whether the loader should or even can do
that.
Thanks,
Rui
More information about the Libc-alpha
mailing list