Skip to content

Commit b16257b

Browse files
MaskRayDaniel Kiper
authored and
Daniel Kiper
committed
configure: Replace -Wl,-r,-d with -Wl,-r and add -fno-common
In GNU ld and ld.lld, -d is used with -r to allocate space to COMMON symbols. This behavior is presumably to work around legacy projects which inspect relocatable output by themselves and do not handle COMMON symbols. The GRUB does not do this. See llvm/llvm-project#53660 -d is quite useless and ld.lld 15.0.0 will make -d no-op. COMMON symbols have special symbol resolution semantics which can cause surprise (see https://maskray.me/blog/2022-02-06-all-about-common-symbols). GCC<10 and Clang<11 defaulted to -fcommon. Just use -fno-common to avoid COMMON symbols. Signed-off-by: Fangrui Song <[email protected]> Reviewed-by: Daniel Kiper <[email protected]>
1 parent 5e10be4 commit b16257b

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

acinclude.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ link_nopie_needed=no]
430430
AC_MSG_CHECKING([whether linker needs disabling of PIE to work])
431431
AC_LANG_CONFTEST([AC_LANG_SOURCE([[]])])
432432
433-
[if eval "$ac_compile -Wl,-r,-d -nostdlib -Werror -o conftest.o" 2> /dev/null; then]
433+
[if eval "$ac_compile -Wl,-r -nostdlib -Werror -o conftest.o" 2> /dev/null; then]
434434
AC_MSG_RESULT([no])
435435
[# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
436436
rm -f conftest.o

conf/Makefile.common

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CCASFLAGS_KERNEL = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
4141
STRIPFLAGS_KERNEL = -R .rel.dyn -R .reginfo -R .note -R .comment -R .drectve -R .note.gnu.gold-version -R .MIPS.abiflags -R .ARM.exidx
4242

4343
CFLAGS_MODULE = $(CFLAGS_PLATFORM) -ffreestanding
44-
LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-r,-d
44+
LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-r
4545
CPPFLAGS_MODULE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
4646
CCASFLAGS_MODULE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
4747

configure.ac

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fi
8585

8686
# Enable support for "restrict" keyword and other
8787
# features from gnu99 C language standard.
88-
BUILD_CFLAGS="-std=gnu99 $BUILD_CFLAGS"
89-
HOST_CFLAGS="-std=gnu99 $HOST_CFLAGS"
90-
TARGET_CFLAGS="-std=gnu99 $TARGET_CFLAGS"
88+
BUILD_CFLAGS="-std=gnu99 -fno-common $BUILD_CFLAGS"
89+
HOST_CFLAGS="-std=gnu99 -fno-common $HOST_CFLAGS"
90+
TARGET_CFLAGS="-std=gnu99 -fno-common $TARGET_CFLAGS"
9191

9292
# Default HOST_CPPFLAGS
9393
HOST_CPPFLAGS="$HOST_CPPFLAGS -Wall -W"

grub-core/Makefile.core.def

+10-10
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,26 @@ kernel = {
4949

5050
nostrip = emu;
5151

52-
emu_ldflags = '-Wl,-r,-d';
53-
i386_efi_ldflags = '-Wl,-r,-d';
52+
emu_ldflags = '-Wl,-r';
53+
i386_efi_ldflags = '-Wl,-r';
5454
i386_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
55-
x86_64_efi_ldflags = '-Wl,-r,-d';
55+
x86_64_efi_ldflags = '-Wl,-r';
5656
x86_64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
5757

5858
ia64_efi_cflags = '-fno-builtin -fpic -minline-int-divide-max-throughput';
59-
ia64_efi_ldflags = '-Wl,-r,-d';
59+
ia64_efi_ldflags = '-Wl,-r';
6060
ia64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
6161

62-
arm_efi_ldflags = '-Wl,-r,-d';
62+
arm_efi_ldflags = '-Wl,-r';
6363
arm_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
6464

65-
arm64_efi_ldflags = '-Wl,-r,-d';
65+
arm64_efi_ldflags = '-Wl,-r';
6666
arm64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
6767

68-
riscv32_efi_ldflags = '-Wl,-r,-d';
68+
riscv32_efi_ldflags = '-Wl,-r';
6969
riscv32_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
7070

71-
riscv64_efi_ldflags = '-Wl,-r,-d';
71+
riscv64_efi_ldflags = '-Wl,-r';
7272
riscv64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
7373

7474
i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)';
@@ -98,9 +98,9 @@ kernel = {
9898
i386_qemu_cppflags = '-DGRUB_BOOT_MACHINE_LINK_ADDR=$(GRUB_BOOT_MACHINE_LINK_ADDR)';
9999
emu_cflags = '$(CFLAGS_GNULIB)';
100100
emu_cppflags = '$(CPPFLAGS_GNULIB)';
101-
arm_uboot_ldflags = '-Wl,-r,-d';
101+
arm_uboot_ldflags = '-Wl,-r';
102102
arm_uboot_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
103-
arm_coreboot_ldflags = '-Wl,-r,-d';
103+
arm_coreboot_ldflags = '-Wl,-r';
104104
arm_coreboot_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
105105

106106
i386_pc_startup = kern/i386/pc/startup.S;

grub-core/genmod.sh.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ else
8383
for dep in $deps; do echo "char moddep_$dep[] __attribute__ ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
8484

8585
if test -n "$deps"; then
86-
@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r,-d
86+
@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
8787
else
88-
@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r,-d
88+
@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r
8989
fi
9090
rm -f $t1 $t2 $tmpfile
9191
mv $tmpfile2 $tmpfile

0 commit comments

Comments
 (0)