[glibc] thp: Limit THP code padding to 2 * THP page size

"H.J. Lu via Glibc-cvs" <[email protected]> Tue, 23 Jun 2026 22:19:03 +0000 (GMT)
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d00085d6d0975715607e74945d4fde66a1823052

commit d00085d6d0975715607e74945d4fde66a1823052
Author: H.J. Lu <[email protected]>
Date:   Tue Jun 23 08:10:05 2026 +0800

    thp: Limit THP code padding to 2 * THP page size
    
    Since R_ARC_S25W_PCREL range is limited, 32MB THP code padding leads to
    relocation overflow:
    
    .../support/libsupport_nonshared.a(support_test_main.oS):
    in function `support_test_main':
    .../support/support_test_main.c:396:(.text+0x650): relocation truncated to fit: R_ARC_S25W_PCREL_PLT against symbol `atexit' defined in .text section in
    ../glibc/libc_nonshared.a(atexit.oS)
    .../install/compilers/arc-linux-gnu/arc-glibc-linux-gnu/bin/ld: final link failed
    collect2: error: ld returned 1 exit status
    
    Limit THP code padding to 2 * THP page size to avoid R_ARC_S25W_PCREL
    relocation overflow.
    
    Tested natively for Linux/x86-64 and Linux/i686 as well as with
    build-many-glibcs.py for arc-linux-gnu.
    
    This fixes BZ #34311
    
    Signed-off-by: H.J. Lu <[email protected]>
    Reviewed-by: Adhemerval Zanella  <[email protected]>

Diff:
---
 sysdeps/unix/sysv/linux/Makefile       | 15 +++++++++------
 sysdeps/unix/sysv/linux/tst-thp-size.S | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index a048dbc732..1efeac5213 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -737,6 +737,7 @@ ifndef THP-PAGE-SIZE
 THP-PAGE-SIZE = 0x200000
 endif
 
+THP-PAGE-SIZE-CFLAGS = -DTHP_PAGE_SIZE=$(THP-PAGE-SIZE)
 THP-PAGE-SIZE-LDFLAGS = -Wl,-z,max-page-size=$(THP-PAGE-SIZE)
 
 # -Wl,-z,max-page-size=$(THP-PAGE-SIZE) alone doesn't work for PDE when
@@ -786,9 +787,11 @@ ifneq (yes,$(enable-static-pie))
 LDFLAGS-tst-thp-1-no-s-code-static += $(LOAD-THP-ADDRESS-LDFLAGS)
 endif
 
-$(objpfx)tst-thp-1-no-s-code: $(objpfx)tst-thp-size-mod.o
-$(objpfx)tst-thp-1-no-s-code-pde: $(objpfx)tst-thp-size-mod.o
-$(objpfx)tst-thp-1-no-s-code-static: $(objpfx)tst-thp-size-mod.o
+ASFLAGS-tst-thp-size.S = $(THP-PAGE-SIZE-CFLAGS)
+
+$(objpfx)tst-thp-1-no-s-code: $(objpfx)tst-thp-size.o
+$(objpfx)tst-thp-1-no-s-code-pde: $(objpfx)tst-thp-size.o
+$(objpfx)tst-thp-1-no-s-code-static: $(objpfx)tst-thp-size.o
 
 tst-thp-1-no-s-code-ENV = GLIBC_TUNABLES=glibc.elf.thp=1
 tst-thp-1-no-s-code-pde-ENV = GLIBC_TUNABLES=glibc.elf.thp=1
@@ -800,9 +803,9 @@ tst-thp-1-ENV = GLIBC_TUNABLES=glibc.elf.thp=1
 tst-thp-1-pde-ENV = GLIBC_TUNABLES=glibc.elf.thp=1
 tst-thp-1-static-ENV = GLIBC_TUNABLES=glibc.elf.thp=1
 
-$(objpfx)tst-thp-1: $(objpfx)tst-thp-size-mod.o
-$(objpfx)tst-thp-1-pde: $(objpfx)tst-thp-size-mod.o
-$(objpfx)tst-thp-1-static: $(objpfx)tst-thp-size-mod.o
+$(objpfx)tst-thp-1: $(objpfx)tst-thp-size.o
+$(objpfx)tst-thp-1-pde: $(objpfx)tst-thp-size.o
+$(objpfx)tst-thp-1-static: $(objpfx)tst-thp-size.o
 
 tst-thp-1-pde-no-pie = yes
 
diff --git a/sysdeps/unix/sysv/linux/tst-thp-size.S b/sysdeps/unix/sysv/linux/tst-thp-size.S
new file mode 100644
index 0000000000..29c5f4e750
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-thp-size.S
@@ -0,0 +1,20 @@
+/* A file that provides THP code size for testing.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+	.text
+	.space 2 * THP_PAGE_SIZE