Improve ld.lld-21+ compatibility when building i386-pc target
v2 cover Improve ld.lld-21+ compatibility when building i386-pc target
Starting with ld.llvm-21, any attempt create a non-relocatable binary and set one more secton addresses below 0x400000 results in a linker error. Furthermore, the differences between ld.bfd and ld.lld made finding a proper set of command-line flags tht worked with both linkers and bypass the image base address restriction difficult. Therefore, the approach of using a custom linker script was adopted to solve the issue.
This approach was tested using:
../configure CC=clang CXX=clang++ LDFLAGS="-fuse-ld=lld" TARGET_LDFLAGS="-fuse-ld=lld" --with-platform=pc ../configure CC=clang CXX=clang++ --with-platform=pc (both with ld.lld as the default and ld.bfd as the default) ../configure CC=gcc CXX=g++ --with-platform=pc
and a VM was used for testing. To build the disk images the VM was booted with, the following scripts were used:
EFI disk:
efi_disk_image.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh + +set -xe + +dd if=/dev/zero of=grub.img bs=1M count=100 status=progress + +sfdisk --force --no-reread --no-tell-kernel grub.img <<EOF +label: gpt +label-id: ABEB6772-65C7-4391-BF21-B616916286B9 +size=1M, type=21686148-6449-6E6F-744E-656564454649, uuid=9892A604-439E-4401-A372-AAD5E99EADBB +type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=FCE5EBB3-C887-4FDE-93C5-7670CC3914BF +EOF + +LOOP_DEV=$(losetup --show -fP grub.img) +sleep 1 + +mkfs.ext4 -F "${LOOP_DEV}p2" + +mount "${LOOP_DEV}p2" /mnt/gentoo +./grub-install -v --directory ./grub-core --locale-directory /usr/share/locale --boot-directory=/mnt/gentoo "${LOOP_DEV}" + +umount "${LOOP_DEV}p2" +losetup -d "${LOOP_DEV}"
Signature · 64 lines
MBR image:
--- /dev/null 2026-01-18 13:24:44.262332704 -0500
+++ mbr_disk_image.sh 2026-01-25 11:17:24.129208591 -0500
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+set -xe
+
+dd if=/dev/zero of=grub.img bs=1M count=100 status=progress
+
+sfdisk --force --no-reread --no-tell-kernel grub.img <<EOF
+label: dos
+label-id: 0x12345678
+type=83, bootable
+EOF
+
+LOOP_DEV=$(losetup --show -fP grub.img)
+sleep 1
+
+mkfs.ext4 -F -O ^has_journal "${LOOP_DEV}p1"
+
+mount "${LOOP_DEV}p1" /mnt/gentoo
+./grub-install -v --directory ./grub-core --locale-directory /usr/share/locale --boot-directory=/mnt/gentoo "${LOOP_DEV}"
+
+umount "${LOOP_DEV}p1"
+losetup -d "${LOOP_DEV}"
--
In all cases, the VM successfully booted to the standard GRUB prompt.
Nicholas Vinson (8):
i386/pc/int.h: conditionally apply regparm attr.
grub-core: Update kernel image generation
i386-cygwin-img-ld.sc -> i386-cygwin-img.lds
Revert "configure: Print a more helpful error if autoconf-archive is
not installed"
Revert "configure: Check linker for --image-base support"
Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
configure: drop -Ttext checks for i386-pc
C23 fixes: fix strchr() and strrchr() handling
INSTALL | 1 -
acinclude.m4 | 18 ++++---
conf/Makefile.extra-dist | 3 +-
...6-cygwin-img-ld.sc => i386-cygwin-img.lds} | 0
conf/i386-pc-kernel.lds | 51 +++++++++++++++++++
configure.ac | 42 +++++++--------
grub-core/Makefile.core.def | 24 ++++++---
grub-core/osdep/linux/ofpath.c | 14 ++---
include/grub/i386/pc/int.h | 5 +-
util/probe.c | 6 +--
util/resolve.c | 10 ++--
11 files changed, 123 insertions(+), 51 deletions(-)
rename conf/{i386-cygwin-img-ld.sc => i386-cygwin-img.lds} (100%)
create mode 100644 conf/i386-pc-kernel.lds
--
2.53.0
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-develPATCH v2 1/8 i386/pc/int.h: conditionally apply regparm attr.
Modern compilers are becoming more strict and are starting to warn when certain attributs are ignored. The regparam attribute is such an attribute.
Update the code so the regparam attribute is only appled when building against i386 targets as that is the only scenario when it is not ignored.
--- include/grub/i386/pc/int.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/grub/i386/pc/int.h +4 -1
@@ -24,7 +24,10 @@void EXPORT_FUNC (grub_bios_interrupt ) (grub_uint8_t intno, struct grub_bios_int_registers *regs) - __attribute__ ((regparm (3 ))); +#if defined(__i386__) && !defined(__x86_64__) + __attribute__ ((regparm(3))) +#endif +;#ifdef GRUB_MACHINE_PCBIOS extern struct grub_i386_idt *EXPORT_VAR (grub_realidt );
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 2/8 grub-core: Update kernel image generation
In reply to Nicholas VinsonThe i386-pc kernel image fails to build because of changes made to address ld.lld-21 and newer linking issues. Specifically, with ld.lld-21, if you try to set the text section address below image base address when linking a non-relocatable binary, ld.lld wil fail to link.
Switching to using a customized linker script solves the issue and is a more robost solution to supporting multiple linkers than attempting to find a set of command-line flags that satisified all supported linkers and links the kernel properly. In the worst case, continued use of command-line flags could result in having to create code branches to support various linkers.
For example, when dealing with just ld.bfd and ld.lld, the behavioral differences between the two made finding a proper subset of flags that worked impossible. The previous attempt dropped -Ttext for --image-base, which has been proven not to work. The simplest correction, -Wl,--image-base=0 -Wl,-Ttext, did not work because that option resulted in a GRUB kernel that entered into a tight infinite refresh loop.
Moreover, ld.lld does not order the sections the same way ld.bfd does, and that results in object files with gaps or holes when sections are stripped. I suspect, but did not investigate, that this plays a role in the infinite refresh loop I mentioned earlier.
The easiest way to resolve all this is to use customized linker scripts. These scripts, by default, override any default configuration the linker would otherwise impose, allow for complete control in aligning sections, and allows GRUB to specify where in the load segment each section goes.
This change does require linkers to support the --defsym flag, which requires its argument to be of the form 'sym=address' as 'sym address' is not supported.
--- conf/Makefile.extra-dist | 1 + conf/i386-pc-kernel.lds | 51 +++++++++++++++++++++++++++++++++++++ grub-core/Makefile.core.def | 24 +++++++++++------ 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 conf/i386-pc-kernel.lds
conf/Makefile.extra-dist +1 -0
@@ -17,6 +17,7 @@ EXTRA_DIST += docs/grub.cfg EXTRA_DIST += docs/osdetect.cfg EXTRA_DIST += conf/i386-cygwin-img-ld.sc +EXTRA_DIST += conf/i386-pc-kernel.lds EXTRA_DIST += grub-core/Makefile.core.def EXTRA_DIST += grub-core/Makefile.gcry.def
conf/i386-pc-kernel.lds new file +51 -0
51 lines not on this page · raw message
grub-core/Makefile.core.def +16 -8
@@ -10,6 +10,7 @@ transform_data = { installdir = noinst; name = genmod.sh; common = genmod.sh.in; + i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; }; transform_data = {
@@ -82,7 +83,8 @@ kernel = { riscv64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame'; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x9000'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x9000'; i386_qemu_ldflags = '$(TARGET_IMG_LDFLAGS)'; i386_qemu_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x9000'; i386_coreboot_ldflags = '$(TARGET_IMG_LDFLAGS)';
@@ -450,7 +452,8 @@ image = { sparc64_ieee1275 = boot/sparc64/ieee1275/boot.S; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7C00'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x7C00'; i386_qemu_ldflags = '$(TARGET_IMG_LDFLAGS)'; i386_qemu_ldflags = '$(TARGET_IMG_BASE_LDOPT),$(GRUB_BOOT_MACHINE_LINK_ADDR)';
@@ -478,7 +481,8 @@ image = { cppflags = '-DHYBRID_BOOT=1'; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7C00'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x7C00'; objcopyflags = '-O binary'; enable = i386_pc;
@@ -489,7 +493,8 @@ image = { i386_pc = boot/i386/pc/cdboot.S; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7C00'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x7C00'; sparc64_ieee1275 = boot/sparc64/ieee1275/boot.S;
@@ -509,7 +514,8 @@ image = { i386_pc = boot/i386/pc/pxeboot.S; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7C00'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x7C00'; objcopyflags = '-O binary'; enable = i386_pc;
@@ -520,7 +526,8 @@ image = { i386_pc = boot/i386/pc/diskboot.S; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x8000'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x8000'; sparc64_ieee1275 = boot/sparc64/ieee1275/diskboot.S; sparc64_ieee1275_ldflags = '-Wl,-Ttext=0x4200';
@@ -536,7 +543,8 @@ image = { i386_pc = boot/i386/pc/lnxboot.S; i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)'; - i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x6000'; + i386_pc_ldflags = '-Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds'; + i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT)=0x6000'; objcopyflags = '-O binary'; enable = i386_pc;
@@ -578,7 +586,7 @@ image = { i386_pc_nodist = rs_decoder.h; objcopyflags = '-O binary'; - ldflags = '$(TARGET_IMG_LDFLAGS) $(TARGET_IMG_BASE_LDOPT),0x8200'; + ldflags = '$(TARGET_IMG_LDFLAGS) -Wl,-T$(top_srcdir)/conf/i386-pc-kernel.lds $(TARGET_IMG_BASE_LDOPT)=0x8200'; enable = i386_pc; };
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 3/8 i386-cygwin-img-ld.sc -> i386-cygwin-img.lds
In reply to Nicholas VinsonRename i386-cygwin-img-ld.sc to i386-cygwin-img.lds as 'lds' is the preferred extension for linker scripts.
---
conf/Makefile.extra-dist | 2 +-
conf/{i386-cygwin-img-ld.sc => i386-cygwin-img.lds} | 0
configure.ac | 4 ++--
3 files changed, 3 insertions(+), 3 deletions(-)
rename conf/{i386-cygwin-img-ld.sc => i386-cygwin-img.lds} (100%)conf/Makefile.extra-dist +1 -1
@@ -16,7 +16,7 @@ EXTRA_DIST += docs/autoiso.cfg EXTRA_DIST += docs/grub.cfg EXTRA_DIST += docs/osdetect.cfg -EXTRA_DIST += conf/i386-cygwin-img-ld.sc +EXTRA_DIST += conf/i386-cygwin-img.lds EXTRA_DIST += conf/i386-pc-kernel.lds EXTRA_DIST += grub-core/Makefile.core.def
conf/i386-cygwin-img.lds renamed from conf/i386-cygwin-img-ld.sc +0 -0
configure.ac +2 -2
@@ -1458,9 +1458,9 @@ if test x$grub_cv_target_cc_link_format = x-arch,i386 || test x$grub_cv_target_c elif test x$grub_cv_target_cc_link_format = x-mi386pe || test x$grub_cv_target_cc_link_format = x-mi386pep ; then TARGET_APPLE_LINKER=0 TARGET_LDFLAGS_OLDMAGIC="-Wl,-N" - TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/conf/i386-cygwin-img-ld.sc" + TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/conf/i386-cygwin-img.lds" TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}" - TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-cygwin-img-ld.sc" + TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-cygwin-img.lds" TARGET_IMG_CFLAGS= else TARGET_APPLE_LINKER=0
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 4/8 Revert "configure: Print a more helpful error if autoconf-archive is not installed"
In reply to Nicholas VinsonThis reverts commit ac042f3f58d33ce9cd5ff61750f06da1a1d7b0eb.
--- configure.ac | 5 ----- 1 file changed, 5 deletions(-)
configure.ac +0 -5
@@ -1792,11 +1792,6 @@ LIBS="" # Defined in acinclude.m4. grub_ASM_USCORE grub_PROG_TARGET_CC - -# The error message produced by autoconf if autoconf-archive is not installed is -# quite misleading and not very helpful. So, try point people in the right direction. -m4_ifndef([AX_CHECK_LINK_FLAG], [m4_fatal([autoconf-archive is missing. You must install it to generate the configure script.])]) - if test "x$TARGET_APPLE_LINKER" != x1 ; then AX_CHECK_LINK_FLAG([-Wl,--image-base,0x400000], [TARGET_IMG_BASE_LDOPT="-Wl,--image-base"],
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 5/8 Revert "configure: Check linker for --image-base support"
In reply to Nicholas VinsonThis reverts commit 1a5417f39a0ccefcdd5440f2a67f84d2d2e26960.
--- acinclude.m4 | 5 ----- configure.ac | 14 ++------------ 2 files changed, 2 insertions(+), 17 deletions(-)
acinclude.m4 +0 -5
@@ -79,11 +79,6 @@ AC_DEFUN([grub_PROG_OBJCOPY_ABSOLUTE], [AC_MSG_CHECKING([whether ${TARGET_OBJCOPY} works for absolute addresses]) AC_CACHE_VAL(grub_cv_prog_objcopy_absolute, [cat > conftest.c <<\EOF -asm ( - ".globl start, _start, __start\n" - ".ifdef cmain; .set start = _start = __start = cmain\n.endif\n" - ".ifdef _cmain; .set start = _start = __start = _cmain\n.endif\n" -); void cmain (void); void cmain (void)
configure.ac +2 -12
@@ -1461,6 +1461,7 @@ elif test x$grub_cv_target_cc_link_format = x-mi386pe || test x$grub_cv_target_c TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/conf/i386-cygwin-img.lds" TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}" TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-cygwin-img.lds" + TARGET_IMG_BASE_LDOPT="-Wl,-Ttext" TARGET_IMG_CFLAGS= else TARGET_APPLE_LINKER=0
@@ -1468,6 +1469,7 @@ else TARGET_IMG_LDSCRIPT= TARGET_IMG_LDFLAGS='-Wl,-N' TARGET_IMG_LDFLAGS_AC='-Wl,-N' + TARGET_IMG_BASE_LDOPT="-Wl,-Ttext" TARGET_IMG_CFLAGS= fi
@@ -1793,18 +1795,6 @@ LIBS="" grub_ASM_USCORE grub_PROG_TARGET_CC if test "x$TARGET_APPLE_LINKER" != x1 ; then -AX_CHECK_LINK_FLAG([-Wl,--image-base,0x400000], - [TARGET_IMG_BASE_LDOPT="-Wl,--image-base"], - [TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"], - [], - [AC_LANG_SOURCE([ -asm (".globl start; start:"); -asm (".globl _start; _start:"); -asm (".globl __start; __start:"); -void __main (void); -void __main (void) {} -int main (void); - ])]) grub_PROG_OBJCOPY_ABSOLUTE fi grub_PROG_LD_BUILD_ID_NONE
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 6/8 Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
In reply to Nicholas VinsonThis reverts commit a90ccbac677db62fc0c1940cda4388370ac8a04b.
--- INSTALL | 1 - 1 file changed, 1 deletion(-)
INSTALL +0 -1
@@ -56,7 +56,6 @@ need the following. * Python 3 (NOTE: python 2.6 should still work, but it's not tested) * Autoconf 2.64 or later * Automake 1.14 or later -* GNU Autoconf Archive (autoconf-archive on Debian) Your distro may package cross-compiling toolchains such as the following incomplete list on Debian: gcc-aarch64-linux-gnu, gcc-arm-linux-gnueabihf,
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 7/8 configure: drop -Ttext checks for i386-pc
In reply to Nicholas VinsonThe i386-pc target now uses a linker script, so -Ttext is no longer required.
--- acinclude.m4 | 13 ++++++++++++- configure.ac | 21 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-)
acinclude.m4 +12 -1
@@ -93,7 +93,18 @@ else fi grub_cv_prog_objcopy_absolute=yes for link_addr in 0x2000 0x8000 0x7C00; do - if AC_TRY_COMMAND([${CC-cc} ${TARGET_CFLAGS} ${TARGET_LDFLAGS} -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${TARGET_IMG_BASE_LDOPT},$link_addr conftest.o -o conftest.exec]); then : + + target_img_base_ld="${TARGET_IMG_BASE_LDOPT}" + case "$target_img_base_ld" in + *_grub_text_base) + target_img_base_ld="${target_img_base_ld}=$link_addr" + ;; + *) + target_img_base_ld="${target_img_base_ld},$link_addr" + ;; + esac + + if AC_TRY_COMMAND([${CC-cc} ${TARGET_CFLAGS} ${TARGET_LDFLAGS} -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${target_img_base_ld} conftest.o -o conftest.exec]); then : else AC_MSG_ERROR([${CC-cc} cannot link at address $link_addr]) fi
configure.ac +19 -2
@@ -1469,7 +1469,13 @@ else TARGET_IMG_LDSCRIPT= TARGET_IMG_LDFLAGS='-Wl,-N' TARGET_IMG_LDFLAGS_AC='-Wl,-N' - TARGET_IMG_BASE_LDOPT="-Wl,-Ttext" + if test "x$target_cpu-$platform" != "xi386-pc"; then + TARGET_IMG_BASE_LDOPT="-Wl,-Ttext" + else + TARGET_IMG_BASE_LDOPT="-Wl,--defsym,_grub_text_base" + TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/conf/i386-pc-kernel.lds" + TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-pc-kernel.lds" + fi TARGET_IMG_CFLAGS= fi
@@ -1802,7 +1808,18 @@ if test "x$target_cpu" = xi386; then if test "$platform" != emu && test "x$TARGET_APPLE_LINKER" != x1 ; then if test ! -z "$TARGET_IMG_LDSCRIPT"; then # Check symbols provided by linker script. - CFLAGS="$TARGET_CFLAGS -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${TARGET_IMG_BASE_LDOPT},0x8000" + target_img_base_ld="${TARGET_IMG_BASE_LDOPT}" + case "$target_img_base_ld" in + *_grub_text_base) + target_img_base_ld="${target_img_base_ld}=" + ;; + *) + target_img_base_ld="${target_img_base_ld}," + ;; + esac + target_img_base_ld="${target_img_base_ld}0x8000" + CFLAGS="$TARGET_CFLAGS -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${target_img_base_ld}" + target_img_base_ld="" fi grub_CHECK_BSS_START_SYMBOL grub_CHECK_END_SYMBOL
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v2 8/8 C23 fixes: fix strchr() and strrchr() handling
In reply to Nicholas VinsonWith C23, strchr() and strrchr() can now return return constant char * *if* the first argument to the function is a const char *.
This change is implemented in glibc-2.43.
--- grub-core/osdep/linux/ofpath.c | 14 ++++++++------ util/probe.c | 6 +++--- util/resolve.c | 10 +++++----- 3 files changed, 16 insertions(+), 14 deletions(-)
grub-core/osdep/linux/ofpath.c +8 -6
@@ -488,8 +488,10 @@ check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id)static void check_sas (const char *sysfs_path,int *tgt,unsigned long int *sas_address) { -char *ed =strstr (sysfs_path,"end_device" ); -char *p, *q, *path; +const char *ed =strstr (sysfs_path,"end_device" ); +int ed_len; +const char *q; +char *p, *path;char phy[21 ];int fd;size_t path_size;
@@ -506,12 +508,12 @@ check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address) q = ed;while (*q && *q !='/' ) q++; - *q ='\0' ; + ed_len = (int )(q - ed); - path_size = (strlen (p) +strlen (ed) + path_size = (strlen (p) + ed_len +sizeof ("%s/sas_device/%s/phy_identifier" )); path =xmalloc (path_size); -snprintf (path, path_size,"%s/sas_device/%s/phy_identifier" , p, ed); +snprintf (path, path_size,"%s/sas_device/%.*s/phy_identifier" , p, ed_len, ed); fd =open (path,O_RDONLY );if (fd <0 )grub_util_error (_ ("cannot open `%s': %s" ), path,strerror (errno));
@@ -524,7 +526,7 @@ check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)sscanf (phy,"%d" , tgt); -snprintf (path, path_size,"%s/sas_device/%s/sas_address" , p, ed); +snprintf (path, path_size,"%s/sas_device/%.*s/sas_address" , p, ed_len, ed); fd =open (path,O_RDONLY );if (fd <0 )grub_util_error (_ ("cannot open `%s': %s" ), path,strerror (errno));
util/probe.c +3 -3
@@ -70,7 +70,7 @@ char * grub_util_guess_bios_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
@@ -99,7 +99,7 @@ char * grub_util_guess_efi_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
@@ -128,7 +128,7 @@ char * grub_util_guess_baremetal_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
util/resolve.c +5 -5
@@ -138,12 +138,12 @@ read_dep_list (FILE *fp)static char *get_module_name (const char *str) { -char *base; -char *ext; +const char *base; +const char *ext; base =strrchr (str,'/' );if (! base) - base = (char *) str; + base = str;else base++;
@@ -164,9 +164,9 @@ get_module_name (const char *str)static char *get_module_path (const char *prefix,const char *str) { -char *dir; +const char *dir;char *base; -char *ext; +const char *ext;char *ret; ext =strrchr (str,'.' );
Signature · 7 lines
2.53.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 15 lines · Nicholas Vinson
Starting with ld.llvm-21, any attempt create a non-relocatable binary and set one more secton addresses below
0x400000results in a linker error. Furthermore, the differences between ld.bfd and ld.lld made finding a proper set of command-line flags tht worked with both linkers and bypass the image base address restriction difficult. Therefore, the approach of using a custom linker script was adopted to solve the issue.This approach was tested using:
../configure CC=clang CXX=clang++ LDFLAGS="-fuse-ld=lld" TARGET_LDFLAGS="-fuse-ld=lld" --with-platform=pc ../configure CC=clang CXX=clang++ --with-platform=pc (both with ld.lld as the default and ld.bfd as the default) ../configure CC=gcc CXX=g++ --with-platform=pc
and a VM was used for testing. To build the disk images the VM was booted with, the following scripts were used:
[...]
Quoted 11 lines
In all cases, the VM successfully booted to the standard GRUB prompt.
Nicholas Vinson (8):
i386/pc/int.h: conditionally apply regparm attr.
grub-core: Update kernel image generation i386-cygwin-img-ld.sc ->i386-cygwin-img.lds
Revert "configure: Print a more helpful error if autoconf-archive is
not installed"
Revert "configure: Check linker for --image-base support" Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
configure: drop -Ttext checks for i386-pc
For all these patches Reviewed-by: Daniel Kiper <[email protected]>...
Quoted 1 line
C23 fixes: fix
strchr()andstrrchr()handling
It seems to me that this patch does more than commit message says. I think it has to be split into more parts or commit message has to be improved.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 31 lines · Daniel Kiper
Quoted 15 lines · Nicholas Vinson
Starting with ld.llvm-21, any attempt create a non-relocatable binary and set one more secton addresses below
0x400000results in a linker error. Furthermore, the differences between ld.bfd and ld.lld made finding a proper set of command-line flags tht worked with both linkers and bypass the image base address restriction difficult. Therefore, the approach of using a custom linker script was adopted to solve the issue.This approach was tested using:
../configure CC=clang CXX=clang++ LDFLAGS="-fuse-ld=lld" TARGET_LDFLAGS="-fuse-ld=lld" --with-platform=pc ../configure CC=clang CXX=clang++ --with-platform=pc (both with ld.lld as the default and ld.bfd as the default) ../configure CC=gcc CXX=g++ --with-platform=pc
and a VM was used for testing. To build the disk images the VM was booted with, the following scripts were used:
[...]
Quoted 11 lines
In all cases, the VM successfully booted to the standard GRUB prompt.
Nicholas Vinson (8):
i386/pc/int.h: conditionally apply regparm attr.
grub-core: Update kernel image generation i386-cygwin-img-ld.sc ->i386-cygwin-img.lds
Revert "configure: Print a more helpful error if autoconf-archive is
not installed"
Revert "configure: Check linker for --image-base support" Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
configure: drop -Ttext checks for i386-pc
Quoted 7 lines
For all these patches Reviewed-by: Daniel Kiper <[email protected]>...
Quoted 1 line
C23 fixes: fix
strchr()andstrrchr()handlingIt seems to me that this patch does more than commit message says. I think it has to be split into more parts or commit message has to be improved.
I admit the message is a bit terse, but I kept the changes to a minimum. I did, however, forget to mention strstr().
Put simply, with C23, if the first argument to strstr(), strchr(), or strrchar() is a const char *, the result is a const char *. C23 also made this change to about 9 or so other functions.
As a result, most of the changes were just changing a variable from 'char *' to 'const char *'.
The changes for ofpath.c are a bit more involved because the original code would modify the string 'ed' pointed to. With C23, ed needs to be a 'const char *' because sysfs_path is a 'const char *'. This also means that the line "*q = '\0'" is no longer valid because you cannot safely modify a const char string.
I saw two options to fix this, the first was to modify the line to be q = strndup(...);, add the requisite error checking, and then make sure to free q just before every subsequent return. The second option, is what I did, find the difference between q and ed, save it off, and then replace the strlen(ed) calls with that difference.
I believe the second approach is the better of the two. It did have the ancillary effect of updating a few snprintf() calls and the path_size calculation; however, these changes are required with this approach.
If you'd prefer, I can re-submit patch 8 with these details in the commit message. Alternatively, if you would prefer the strndup() approach, I will respin this patch series without patch 8, and then submit a re-worked C23 patch as a separate request.
Thanks,
Nicholas Vinson
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 34 lines · Nicholas Vinson
Quoted 33 lines · Daniel Kiper
Quoted 15 lines · Nicholas Vinson
Starting with ld.llvm-21, any attempt create a non-relocatable binary and set one more secton addresses below
0x400000results in a linker error. Furthermore, the differences between ld.bfd and ld.lld made finding a proper set of command-line flags tht worked with both linkers and bypass the image base address restriction difficult. Therefore, the approach of using a custom linker script was adopted to solve the issue.This approach was tested using:
../configure CC=clang CXX=clang++ LDFLAGS="-fuse-ld=lld" TARGET_LDFLAGS="-fuse-ld=lld" --with-platform=pc ../configure CC=clang CXX=clang++ --with-platform=pc (both with ld.lld as the default and ld.bfd as the default) ../configure CC=gcc CXX=g++ --with-platform=pc
and a VM was used for testing. To build the disk images the VM was booted with, the following scripts were used:
[...]
Quoted 11 lines
In all cases, the VM successfully booted to the standard GRUB prompt.
Nicholas Vinson (8):
i386/pc/int.h: conditionally apply regparm attr.
grub-core: Update kernel image generation i386-cygwin-img-ld.sc ->i386-cygwin-img.lds
Revert "configure: Print a more helpful error if autoconf-archive is
not installed"
Revert "configure: Check linker for --image-base support" Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
configure: drop -Ttext checks for i386-pcFor all these patches Reviewed-by: Daniel Kiper <[email protected]>...
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
C23 fixes: fix
strchr()andstrrchr()handlingIt seems to me that this patch does more than commit message says. I think it has to be split into more parts or commit message has to be improved.
I admit the message is a bit terse, but I kept the changes to a minimum. I did, however, forget to mention
strstr().
If you mention strstr() I am OK with it...
Quoted 19 lines
Put simply, with C23, if the first argument to
strstr(),strchr(), orstrrchar()is a const char *, the result is a const char *. C23 also made this change to about 9 or so other functions.As a result, most of the changes were just changing a variable from 'char *' to 'const char *'.
The changes for
ofpath.care a bit more involved because the original code would modify the string 'ed' pointed to. With C23, ed needs to be a 'const char *' becausesysfs_pathis a 'const char *'. This also means that the line "*q = '\0'" is no longer valid because you cannot safely modify a const char string.I saw two options to fix this, the first was to modify the line to be q = strndup(...);, add the requisite error checking, and then make sure to free q just before every subsequent return. The second option, is what I did, find the difference between q and ed, save it off, and then replace the strlen(ed) calls with that difference.
Quoted 3 lines
I believe the second approach is the better of the two. It did have the ancillary effect of updating a few
snprintf()calls and thepath_sizecalculation; however, these changes are required with this approach.
I am OK with second approach but I think this change should go to separate patch, with an explanation like above, because it is more complicated than just adding const.
Quoted 4 lines
If you'd prefer, I can re-submit patch 8 with these details in the commit message. Alternatively, if you would prefer the
strndup()approach, I will respin this patch series without patch 8, and then submit a re-worked C23 patch as a separate request.
Just split patch #8 as I suggested above and repost C23 as separate series.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
On Wed, Feb 11, 2026 at 1:28 AM Nicholas Vinson <nvinso in a linker error. Furthermore,
Quoted 5 lines
The changes for
ofpath.care a bit more involved because the original code would modify the string 'ed' pointed to. With C23, ed needs to be a 'const char *' becausesysfs_pathis a 'const char *'. This also means that the line "*q = '\0'" is no longer valid because you cannot safely modify a const char string.
I may be completely missing something important here, but I run into the same issue trying to build against glibc 2.43 and my fix for ofpath.c was less invasive. I'm sending my patch in a reply, please have a look.
Regards,
Rado
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 8 lines · Radoslav Kolev
On Wed, Feb 11, 2026 at 1:28 AM Nicholas Vinson <nvinso in a linker error. Furthermore,
Quoted 5 lines
The changes for
ofpath.care a bit more involved because the original code would modify the string 'ed' pointed to. With C23, ed needs to be a 'const char *' becausesysfs_pathis a 'const char *'. This also means that the line "*q = '\0'" is no longer valid because you cannot safely modify a const char string.
Quoted 4 lines
I may be completely missing something important here, but I run into the same issue trying to build against glibc 2.43 and my fix for
ofpath.cwas less invasive. I'm sending my patch in a reply, please have a look.
I think the issue is I missed the xstrdup() call on my first pass through. That said, on re-evaluation, I find the xstrdup() call to be superfluous and recommend removing it. It's removal would allow the function to execute without reduced dynamic memory allocation (only space for path is needed) and reduces the total number of strstr() calls to 1.
I want to review my commit messages before publishing the revised patch (which I'll do later).
Thanks,
Nicholas Vinson
Quoted 3 lines
Regards,
Rado
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/1 Fix build with glibc 2.43 after new ISO C23 changes
In reply to Nicholas VinsonFor ISO C23, the functions bsearch, memchr, strchr, strpbrk, strrchr, strstr, wcschr, wcspbrk, wcsrchr, wcsstr and wmemchr that return pointers into their input arrays now have definitions as macros that return a pointer to a const-qualified type when the input argument is a pointer to a const-qualified type.
--- grub-core/osdep/linux/ofpath.c | 4 ++-- util/probe.c | 6 +++--- util/resolve.c | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-)
grub-core/osdep/linux/ofpath.c +2 -2
@@ -488,13 +488,13 @@ check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id)static void check_sas (const char *sysfs_path,int *tgt,unsigned long int *sas_address) { -char *ed =strstr (sysfs_path,"end_device" ); +char *ed;char *p, *q, *path;char phy[21 ];int fd;size_t path_size; -if (!ed) +if (!strstr (sysfs_path,"end_device" ))return ;/* SAS devices are identified using disk@$PHY_ID */
util/probe.c +3 -3
@@ -70,7 +70,7 @@ char * grub_util_guess_bios_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
@@ -99,7 +99,7 @@ char * grub_util_guess_efi_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
@@ -128,7 +128,7 @@ char * grub_util_guess_baremetal_drive (const char *orig_path) {char *canon; -char *ptr; +const char *ptr; canon =grub_canonicalize_file_name (orig_path);if (!canon)return NULL ;
util/resolve.c +4 -4
@@ -138,8 +138,8 @@ read_dep_list (FILE *fp)static char *get_module_name (const char *str) { -char *base; -char *ext; +const char *base; +const char *ext; base =strrchr (str,'/' );if (! base)
@@ -164,9 +164,9 @@ get_module_name (const char *str)static char *get_module_path (const char *prefix,const char *str) { -char *dir; +const char *dir;char *base; -char *ext; +const char *ext;char *ret; ext =strrchr (str,'.' );
Signature · 7 lines
2.52.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 31 lines · Daniel Kiper
Quoted 15 lines · Nicholas Vinson
Starting with ld.llvm-21, any attempt create a non-relocatable binary and set one more secton addresses below
0x400000results in a linker error. Furthermore, the differences between ld.bfd and ld.lld made finding a proper set of command-line flags tht worked with both linkers and bypass the image base address restriction difficult. Therefore, the approach of using a custom linker script was adopted to solve the issue.This approach was tested using:
../configure CC=clang CXX=clang++ LDFLAGS="-fuse-ld=lld" TARGET_LDFLAGS="-fuse-ld=lld" --with-platform=pc ../configure CC=clang CXX=clang++ --with-platform=pc (both with ld.lld as the default and ld.bfd as the default) ../configure CC=gcc CXX=g++ --with-platform=pc
and a VM was used for testing. To build the disk images the VM was booted with, the following scripts were used:
[...]
Quoted 11 lines
In all cases, the VM successfully booted to the standard GRUB prompt.
Nicholas Vinson (8):
i386/pc/int.h: conditionally apply regparm attr.
grub-core: Update kernel image generation i386-cygwin-img-ld.sc ->i386-cygwin-img.lds
Revert "configure: Print a more helpful error if autoconf-archive is
not installed"
Revert "configure: Check linker for --image-base support" Revert "INSTALL: Add note that the GNU Autoconf Archive may be needed"
configure: drop -Ttext checks for i386-pc
Quoted 1 line
For all these patches Reviewed-by: Daniel Kiper <[email protected]>...
Sadly your patch set breaks Windows build... :-(
$ ./configure --build=x86_64-linux-gnu --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-platform=pc
$ make
[...]
i686-w64-mingw32-gcc -std=gnu99 -fno-common -Os -m32 -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -march=i386 -mrtd -mregparm=3 -falign-functions=1 -falign-loops=1 -falign-jumps=1 -freg-struct-return -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -msoft-float -fno-omit-frame-pointer -fno-dwarf2-cfi-asm -fno-reorder-functions -mno-stack-arg-probe -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-ident -fno-stack-protector -Wtrampolines -Werror -DGRUB_HAS_PCI -fno-builtin -m32 -Wl,-mi386pe -nostdlib -Wl,-N -Wl,-S -Wl,-T../conf/i386-cygwin-img.lds -Wl,-T../conf/i386-pc-kernel.lds -Wl,-Ttext=0x8200 -o lzma_decompress.image.exe boot/i386/pc/lzma_decompress_image-startup_raw.o
/usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol _grub_text_base' referenced in expression /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol _grub_text_base' referenced in expression
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol `_grub_text_base' referenced in expression make[3]: *** [Makefile:33123: boot.image.exe] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: *** [Makefile:33145: boot_hybrid.image.exe] Error 1
collect2: error: ld returned 1 exit status /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol `_grub_text_base' referenced in expression
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:33276: cdboot.image.exe] Error 1
make[3]: *** [Makefile:36161: pxeboot.image.exe] Error 1 /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol _grub_text_base' referenced in expression /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol _grub_text_base' referenced in expression
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol `_grub_text_base' referenced in expression
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:35303: lnxboot.image.exe] Error 1
make[3]: *** [Makefile:33499: diskboot.image.exe] Error 1
make[3]: *** [Makefile:35449: lzma_decompress.image.exe] Error 1 /usr/bin/i686-w64-mingw32-ld:../conf/i386-pc-kernel.lds:19: undefined symbol `_grub_text_base' referenced in expression
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:35180: kernel.exec.exe] Error 1
I suppose that the problem is due to inclusion in the command line two linker scripts...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel