Skip to contentSkip to conversation

Use linker script to remove unnecessary sections

PATCH In review Bean 4 participants 7 messages Dec 14, 2007 – Dec 16, 2007

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 Use linker script to remove unnecessary sections

Hi,

This patch use customized linker script to build *.img and *.mod files, it should remove unnecessary sections created by the compiler.

2007-12-14 Bean <[email protected]>

* conf/i386-pc.rmk (COMMON_LDFLAGS): Use ldscript to link.

* aclocal.m4 (grub_PROG_OBJCOPY_ABSOLUTE): Test ldscript.

* configure.ac : Remove the '--build-id=none' flag.

* ldscript : New file.

Index: conf/i386-pc.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.94

i386-pc.rmk +1 -1
diff -u -p -r1.94 i386-pc.rmk
@@ -2,10 +2,10 @@
 
 COMMON_ASFLAGS = -nostdinc -fno-builtin -m32
 COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32
-COMMON_LDFLAGS = -m32 -nostdlib
+COMMON_LDFLAGS = -m32 -nostdlib -T "${srcdir}/ldscript"
 
 # Images.
 pkgdata_IMAGES = boot.img diskboot.img kernel.img pxeboot.img

Index: aclocal.m4
===================================================================
RCS file: /sources/grub/grub2/aclocal.m4,v
retrieving revision 1.7

aclocal.m4 +1 -2
diff -u -p -r1.7 aclocal.m4
@@ -57,7 +57,7 @@ else
 fi
 grub_cv_prog_objcopy_absolute=yes
 for link_addr in 2000 8000 7C00; do
-  if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} ${LDFLAGS} -nostdlib -Wl,-N
-Wl,-Ttext -Wl,$link_addr conftest.o -o conftest.exec]); then :
+  if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -nostdlib -Wl,-N -Wl,-T

"-Wl,${srcdir}/ldscript" -Wl,-Ttext -Wl,$link_addr conftest.o -o conftest.exec]); then :
else

     AC_MSG_ERROR([${CC-cc} cannot link at address $link_addr])

fi
Index: configure.ac
===================================================================
RCS file: /sources/grub/grub2/configure.ac,v
retrieving revision 1.40

configure.ac +0 -10
diff -u -p -r1.40 configure.ac
@@ -229,16 +229,6 @@ if test "x$target_m32" = x1; then
   TARGET_LDFLAGS="$TARGET_LDFLAGS -m32"
 fi
 
-AC_MSG_CHECKING([whether the linker accepts `--build-id=none'])
-save_LDFLAGS="$LDFLAGS"
-LDFLAGS="$LDFLAGS -Wl,--build-id=none"
-AC_TRY_LINK(, , build_id_flag=yes, build_id_flag=no)
-AC_MSG_RESULT([$build_id_flag])
-LDFLAGS="$save_LDFLAGS"
-if test "x$build_id_flag" = xyes; then
-  TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,--build-id=none"
-fi
-
 #
 # Compiler features.
 #

Index: ldscript
===================================================================
RCS file: /sources/grub/grub2/ldscript,v

ldscript new file +12 -0
diff -Nu ldscript
@@ -0,0 +1,12 @@
+SECTIONS
+{
+  .text : { *(.text) } =0x90909090
+  .rodata : { *(.rodata .rodata.*) }
+  .data : { *(.data) }
+  PROVIDE(__bss_start = .);
+  .bss : { *(.bss) *(COMMON) }
+  PROVIDE(end = .);
+  .modname : { *(.modname) }
+  .moddep : { *(.moddep) }
+  /DISCARD/ : { *(*) }
+}
Signature · 1 line
Bean
#1 lore raw

Quoting Bean <[email protected]>:

Quoted 4 lines

Hi,

This patch use customized linker script to build *.img and *.mod files, it should remove unnecessary sections created by the compiler.

Thanks! Looks good to me. Actually, if we stop using objcopy, we don't need to test it at all. Or we can reuse the test to test ld in place of objcopy.

I tend to prefer the removal of grub_PROG_OBJCOPY_ABSOLUTE, as it's designed to find a specific bug in some old version of objcopy, and if we discover any issues with ld (I hope not), we'll have to use different tests to detect them.

We may want to use architecture specific ld scripts, but that can be corrected in further patches.

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 11 lines · Bean

...
diff -u -p -r1.7 aclocal.m4

aclocal.m4 +1 -2
@@ -57,7 +57,7 @@ else
 fi
 grub_cv_prog_objcopy_absolute=yes
 for link_addr in 2000 8000 7C00; do
-  if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} ${LDFLAGS} -nostdlib -Wl,-N
-Wl,-Ttext -Wl,$link_addr conftest.o -o conftest.exec]); then :
+  if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -nostdlib -Wl,-N -Wl,-T
Quoted 3 lines

"-Wl,${srcdir}/ldscript" -Wl,-Ttext -Wl,$link_addr conftest.o -o conftest.exec]); then :

Selecting the builtin "-N" script first has no effect if a custom script is specified later. Therefore, "-Wl,-N" is IMO no longer necessary.

Quoted 2 lines · Pavel Roskin

...
We may want to use architecture specific ld scripts, but that can be corrected in further patches.

See "[PATCH] Build on Cygwin" for a working example on how to select a platform specific linker script in configure.ac. This can be easily extended, e.g. test more specific scripts first:

for f in \
${target_cpu}-${platform}-${target_os} \
${target_cpu}-${platform} \
${target_cpu}; do
if test -f "${srcdir}/conf/$f-img-ld.sc"; then ...

Christian

#3 lore raw
In reply to Bean
Quoted 6 lines · Bean

Hi,

This patch use customized linker script to build *.img and *.mod files, it should remove unnecessary sections created by the compiler.

Quoted 7 lines

2007-12-14 Bean <[email protected]>

* conf/i386-pc.rmk (COMMON_LDFLAGS): Use ldscript to link.

* aclocal.m4 (grub_PROG_OBJCOPY_ABSOLUTE): Test ldscript.

* configure.ac : Remove the '--build-id=none' flag.

This probably breaks other targets than i386-pc. linuxbios and ieee1275 both use ELF.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)
#4 lore raw

Quoting Robert Millan <[email protected]>:

Quoted 9 lines · Bean
Quoted 8 lines

Hi,

This patch use customized linker script to build *.img and *.mod files, it should remove unnecessary sections created by the compiler.

2007-12-14 Bean <[email protected]>

Quoted 8 lines
Quoted 5 lines

* conf/i386-pc.rmk (COMMON_LDFLAGS): Use ldscript to link.

* aclocal.m4 (grub_PROG_OBJCOPY_ABSOLUTE): Test ldscript.

* configure.ac : Remove the '--build-id=none' flag.

This probably breaks other targets than i386-pc. linuxbios and ieee1275 both use ELF.

But they don't use objcopy, they use strip. The goal is to switch i386-pc from the buggy and simple objcopy to more powerful and flexible ld. The same could be done to replace strip for other platforms while keeping ELF format for the output.

I agree that we should avoid naming highly target-specific linker scripts in a generic way. "i386-pc.ld" might be a better name. The linker script for modules perhaps could be generic, but this is for *.img files.

Signature · 2 lines
Regards,
Pavel Roskin
#5 lore raw
Quoted 3 lines · Pavel Roskin

I agree that we should avoid naming highly target-specific linker scripts in a generic way. "i386-pc.ld" might be a better name.

or i386/pc/ld (more consistent with the rest of grub)

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)
#6 lore raw
Quoted 8 lines · Robert Millan
Quoted 2 lines · Pavel Roskin

I agree that we should avoid naming highly target-specific linker scripts in a generic way. "i386-pc.ld" might be a better name.

or i386/pc/ld (more consistent with the rest of grub)

or "i386/pc/img.ld" (ld script to produce .img files).

Should this dir structure reside in "conf" ? This scheme is not yet used for the *mk files in this directory.

Christian