public inbox for grub-devel@gnu.org
 help / color / mirror / Atom feed
* [PATCH v2 1/1] Check linker for --image-base support
  2025-11-19  0:38 [PATCH v2 0/1] Check linter " Nicholas Vinson
@ 2025-11-19  0:38 ` Nicholas Vinson
  2025-11-19 20:27   ` Daniel Kiper
                     ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Nicholas Vinson @ 2025-11-19  0:38 UTC (permalink / raw)
  To: grub-devel; +Cc: Nicholas Vinson

In several scenarios, configure tests assume it's safe to use
'-Wl,-Ttext,<address>', but starting with ld.lld-21, blindly using that
flag may result in configure-test failures due to ld.lld failing to
link. The failure is because ld.lld-21 no longer allows the specified
address is less than the base address.

However, ld.lld-21+ and ld.bfd-2.44+ both provide support for the
--image-base flag making it preferable over the older `-Ttext` flag.

Fixes: 67662
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
---
 acinclude.m4 |  5 +++++
 configure.ac | 14 ++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/acinclude.m4 b/acinclude.m4
index fa7840f09..70c1912f8 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -79,6 +79,11 @@ 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)
diff --git a/configure.ac b/configure.ac
index 17937baf4..a282bf7bf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1461,7 +1461,6 @@ 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-ld.sc"
   TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}"
   TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-cygwin-img-ld.sc"
-  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
   TARGET_IMG_CFLAGS=
 else
   TARGET_APPLE_LINKER=0
@@ -1469,7 +1468,6 @@ 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
 
@@ -1795,6 +1793,18 @@ 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
-- 
2.51.2


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/1] Check linker for --image-base support
  2025-11-19  0:38 ` [PATCH v2 1/1] Check linker " Nicholas Vinson
@ 2025-11-19 20:27   ` Daniel Kiper
  2025-11-25  7:33   ` Gary Lin via Grub-devel
  2025-12-01 15:02   ` Christian Hesse via Grub-devel
  2 siblings, 0 replies; 8+ messages in thread
From: Daniel Kiper @ 2025-11-19 20:27 UTC (permalink / raw)
  To: Nicholas Vinson; +Cc: grub-devel

On Tue, Nov 18, 2025 at 07:38:07PM -0500, Nicholas Vinson wrote:
> In several scenarios, configure tests assume it's safe to use
> '-Wl,-Ttext,<address>', but starting with ld.lld-21, blindly using that
> flag may result in configure-test failures due to ld.lld failing to
> link. The failure is because ld.lld-21 no longer allows the specified
> address is less than the base address.
>
> However, ld.lld-21+ and ld.bfd-2.44+ both provide support for the
> --image-base flag making it preferable over the older `-Ttext` flag.
>
> Fixes: 67662
> Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/1] Check linker for --image-base support
  2025-11-19  0:38 ` [PATCH v2 1/1] Check linker " Nicholas Vinson
  2025-11-19 20:27   ` Daniel Kiper
@ 2025-11-25  7:33   ` Gary Lin via Grub-devel
  2025-12-01 15:02   ` Christian Hesse via Grub-devel
  2 siblings, 0 replies; 8+ messages in thread
From: Gary Lin via Grub-devel @ 2025-11-25  7:33 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Gary Lin, Nicholas Vinson

On Tue, Nov 18, 2025 at 07:38:07PM -0500, Nicholas Vinson wrote:
> In several scenarios, configure tests assume it's safe to use
> '-Wl,-Ttext,<address>', but starting with ld.lld-21, blindly using that
> flag may result in configure-test failures due to ld.lld failing to
> link. The failure is because ld.lld-21 no longer allows the specified
> address is less than the base address.
> 
> However, ld.lld-21+ and ld.bfd-2.44+ both provide support for the
> --image-base flag making it preferable over the older `-Ttext` flag.
> 
> Fixes: 67662
> Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>

To save time for people who got the following error as I did after
applying this patch, it means that 'autoconf-archive' is missing in the
system.

configure.ac:1800: error: possibly undefined macro: AC_LANG_SOURCE
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1

The error is completely misleading. The real missing macro is
"AX_CHECK_LINK_FLAG" and 'autoconf' could not detect the error
correctly...

Gary Lin

> ---
>  acinclude.m4 |  5 +++++
>  configure.ac | 14 ++++++++++++--
>  2 files changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/acinclude.m4 b/acinclude.m4
> index fa7840f09..70c1912f8 100644
> --- a/acinclude.m4
> +++ b/acinclude.m4
> @@ -79,6 +79,11 @@ 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)
> diff --git a/configure.ac b/configure.ac
> index 17937baf4..a282bf7bf 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -1461,7 +1461,6 @@ 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-ld.sc"
>    TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}"
>    TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/conf/i386-cygwin-img-ld.sc"
> -  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
>    TARGET_IMG_CFLAGS=
>  else
>    TARGET_APPLE_LINKER=0
> @@ -1469,7 +1468,6 @@ 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
>  
> @@ -1795,6 +1793,18 @@ 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
> -- 
> 2.51.2
> 
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/1] Check linker for --image-base support
  2025-11-19  0:38 ` [PATCH v2 1/1] Check linker " Nicholas Vinson
  2025-11-19 20:27   ` Daniel Kiper
  2025-11-25  7:33   ` Gary Lin via Grub-devel
@ 2025-12-01 15:02   ` Christian Hesse via Grub-devel
  2025-12-01 15:34     ` cpfeiffer--- via Grub-devel
  2 siblings, 1 reply; 8+ messages in thread
From: Christian Hesse via Grub-devel @ 2025-12-01 15:02 UTC (permalink / raw)
  To: Nicholas Vinson; +Cc: Christian Hesse, The development of GNU GRUB, cpfeiffer


[-- Attachment #1.1: Type: text/plain, Size: 1044 bytes --]

Nicholas Vinson <nvinson234@gmail.com> on Tue, 2025/11/18 19:38:
> In several scenarios, configure tests assume it's safe to use
> '-Wl,-Ttext,<address>', but starting with ld.lld-21, blindly using that
> flag may result in configure-test failures due to ld.lld failing to
> link. The failure is because ld.lld-21 no longer allows the specified
> address is less than the base address.
> 
> However, ld.lld-21+ and ld.bfd-2.44+ both provide support for the
> --image-base flag making it preferable over the older `-Ttext` flag.

This breaks the start address in i386-pc/kernel.img, which should be 0x9000,
but became 0x9074.

The error message is:

/usr/bin/grub-install: error: `/usr/lib/grub/i386-pc/kernel.img' is
miscompiled: its start address is 0x9074 instead of 0x9000: ld.gold bug?.
-- 
main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
"CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}

[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v2 1/1] Check linker for --image-base support
  2025-12-01 15:02   ` Christian Hesse via Grub-devel
@ 2025-12-01 15:34     ` cpfeiffer--- via Grub-devel
  0 siblings, 0 replies; 8+ messages in thread
From: cpfeiffer--- via Grub-devel @ 2025-12-01 15:34 UTC (permalink / raw)
  To: 'Christian Hesse', 'Nicholas Vinson'
  Cc: cpfeiffer, 'The development of GNU GRUB'


[-- Attachment #1.1: Type: text/plain, Size: 4714 bytes --]

The issue seems to be that --image-base sets the base address of the ELF image 
(i.e. the text segment), whereas -Ttext sets the actual section start of it. 
See the documentation here 
https://sourceware.org/binutils/docs/ld/Options.html#index-image-base-address_002c-cmd-line
Given that .text is offset from the start of the file due to the ELF headers, 
it puts the actual address of .text to 0x9000 + offset, whereas -Ttext puts 
.text at 0x9000.

This becomes pretty clear in the readelf -e output:

--------------------------

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x9074
  Start of program headers:          52 (bytes into file)
  Start of section headers:          37336 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         8
  Section header string table index: 7

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf 
Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0 
0
  [ 1] .text             PROGBITS        00009074 000074 006cb0 00 WAX  0   0 
1
  [ 2] .rodata           PROGBITS        0000fd40 006d40 001ae3 00   A  0   0 
32
  [ 3] .module_license   PROGBITS        00011824 008824 00000f 00   A  0   0 
4
  [ 4] .data             PROGBITS        00011840 008840 00093c 00  WA  0   0 
32
  [ 5] .bss              NOBITS          00012180 00917c 0077c4 00  WA  0   0 
32
  [ 6] .gnu_debuglink    PROGBITS        00000000 00917c 000018 00      0   0 
4
  [ 7] .shstrtab         STRTAB          00000000 009194 000043 00      0   0 
1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), p (processor specific)

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000074 0x00009074 0x00009074 0x09108 0x108d0 RWE 0x20
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10

 Section to Segment mapping:
  Segment Sections...
   00     .text .rodata .module_license .data .bss
   01


------------------------------

0x74 is 116, which is precisely 52 + 2 * 32. Expected is the following layout 
though:

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000080 0x00009000 0x00009000 0x090fc 0x108c4 RWE 0x20
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10

 Section to Segment mapping:
  Segment Sections...
   00     .text .rodata .module_license .data .bss
   01

In this case, 0x74 gets increased to 0x20 to fit the alignment.

> -----Original Message-----
> From: Christian Hesse <list@eworm.de>
> Sent: 01 December 2025 16:03
> To: Nicholas Vinson <nvinson234@gmail.com>
> Cc: The development of GNU GRUB <grub-devel@gnu.org>; cpfeiffer@rev-
> crew.info
> Subject: Re: [PATCH v2 1/1] Check linker for --image-base support
>
> Nicholas Vinson <nvinson234@gmail.com> on Tue, 2025/11/18 19:38:
> > In several scenarios, configure tests assume it's safe to use
> > '-Wl,-Ttext,<address>', but starting with ld.lld-21, blindly using
> > that flag may result in configure-test failures due to ld.lld failing
> > to link. The failure is because ld.lld-21 no longer allows the
> > specified address is less than the base address.
> >
> > However, ld.lld-21+ and ld.bfd-2.44+ both provide support for the
> > --image-base flag making it preferable over the older `-Ttext` flag.
>
> This breaks the start address in i386-pc/kernel.img, which should be 0x9000,
> but became 0x9074.
>
> The error message is:
>
> /usr/bin/grub-install: error: `/usr/lib/grub/i386-pc/kernel.img' is
> miscompiled: its start address is 0x9074 instead of 0x9000: ld.gold bug?.
> --
> main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
> "CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
> putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}

[-- Attachment #1.2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 6052 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v2 1/1] Check linker for --image-base support
@ 2026-01-18 12:21 Hustler One via Grub-devel
  2026-01-19 15:08 ` Daniel Kiper
  0 siblings, 1 reply; 8+ messages in thread
From: Hustler One via Grub-devel @ 2026-01-18 12:21 UTC (permalink / raw)
  To: List; +Cc: nine-ball, Grub Devel


[-- Attachment #1.1: Type: text/plain, Size: 399 bytes --]

I had to revert the commit when updating GRUB to 2.14 on [nixpkgs](https://github.com/NixOS/nixpkgs/pull/481112).

Also, Leah Rowe has also done the same on [Libreboot](https://codeberg.org/libreboot/lbmk/src/branch/master/config/grub/nvme/patches/0010-Revert-configure-Check-linker-for-image-base-support.patch
).

For me, at least, behavior was universally broken on both clang (LLVM21) and GCC.


[-- Attachment #1.2: Type: text/html, Size: 1021 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/1] Check linker for --image-base support
  2026-01-18 12:21 [PATCH v2 1/1] Check linker for --image-base support Hustler One via Grub-devel
@ 2026-01-19 15:08 ` Daniel Kiper
  2026-01-19 23:14   ` Nicholas Vinson
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Kiper @ 2026-01-19 15:08 UTC (permalink / raw)
  To: nine-ball, Nicholas Vinson; +Cc: grub-devel, list, Mike Gilbert

Hi,

Adding Nicholas and Mike...

On Sun, Jan 18, 2026 at 01:21:14PM +0100, Hustler One via Grub-devel wrote:
> I had to revert the commit when updating GRUB to 2.14 on [nixpkgs](https://
> github.com/NixOS/nixpkgs/pull/481112).
>
> Also, Leah Rowe has also done the same on [Libreboot](https://codeberg.org/
> libreboot/lbmk/src/branch/master/config/grub/nvme/patches/0010-Revert-
> configure-Check-linker-for-image-base-support.patch
> ).
>
> For me, at least, behavior was universally broken on both clang (LLVM21) and
> GCC.

It looks it is related to newer linker versions. Older ones work without
any issue. Anyway, sorry for the issues...

Nicholas, may I ask you to investigate this breakage and post a fix?

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/1] Check linker for --image-base support
  2026-01-19 15:08 ` Daniel Kiper
@ 2026-01-19 23:14   ` Nicholas Vinson
  0 siblings, 0 replies; 8+ messages in thread
From: Nicholas Vinson @ 2026-01-19 23:14 UTC (permalink / raw)
  To: Daniel Kiper, nine-ball; +Cc: grub-devel, list, Mike Gilbert



On 1/19/26 10:08, Daniel Kiper wrote:
> Hi,
> 
> Adding Nicholas and Mike...
> 
> On Sun, Jan 18, 2026 at 01:21:14PM +0100, Hustler One via Grub-devel wrote:
>> I had to revert the commit when updating GRUB to 2.14 on [nixpkgs](https://
>> github.com/NixOS/nixpkgs/pull/481112).
>>
>> Also, Leah Rowe has also done the same on [Libreboot](https://codeberg.org/
>> libreboot/lbmk/src/branch/master/config/grub/nvme/patches/0010-Revert-
>> configure-Check-linker-for-image-base-support.patch
>> ).
>>
>> For me, at least, behavior was universally broken on both clang (LLVM21) and
>> GCC.
> 
> It looks it is related to newer linker versions. Older ones work without
> any issue. Anyway, sorry for the issues...
> 
> Nicholas, may I ask you to investigate this breakage and post a fix?

I've already started looking.

I'll let you know when I have something.

Thanks,
Nicholas Vinson

> 
> Daniel


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-01-19 23:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-18 12:21 [PATCH v2 1/1] Check linker for --image-base support Hustler One via Grub-devel
2026-01-19 15:08 ` Daniel Kiper
2026-01-19 23:14   ` Nicholas Vinson
  -- strict thread matches above, loose matches on Subject: below --
2025-11-19  0:38 [PATCH v2 0/1] Check linter " Nicholas Vinson
2025-11-19  0:38 ` [PATCH v2 1/1] Check linker " Nicholas Vinson
2025-11-19 20:27   ` Daniel Kiper
2025-11-25  7:33   ` Gary Lin via Grub-devel
2025-12-01 15:02   ` Christian Hesse via Grub-devel
2025-12-01 15:34     ` cpfeiffer--- via Grub-devel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox