Patchwork gcc 4.6.0 and coreboot

login
register
about
Submitter Scott
Date 2011-04-21 00:36:38
Message ID <BC6EEA06A5654D1583053D6AFB1C7F11@m3a78>
Download mbox | patch
Permalink /patch/2896/
State New
Headers show

Comments

Scott - 2011-04-21 00:36:38
Is anyone using gcc 4.6.0 for coreboot builds yet? New with gcc 4.6.0
is warning -Wunused-but-set-variable. Borland compilers offered this
useful warning 25 years ago, but until now others generally do not.

My interest in gcc 4.6.0 is for using the improved link time optimization
to reduce code size and cut boot time. This is especially useful for
projects using the new AMD reference code, where many unused functions
and data can end up in the image. Here is a comaprision with and without
link time optimization:

AMD Persimmon image size, gcc 4.6.0

                         Standard    -flto
fallback/romstage        285072      161464
fallback/coreboot_ram    167885      114976

I built xgcc using svn://coreboot.org/coreboot/trunk/util/crossgcc/buildgcc
with these changes:
1) binutils-2.21.51.tar.bz2
2) gcc-core-4.6.0.tar.bz2
3) patches removed
4) --disable-libquadmath (to avoid a build problem)
5) --with-dwarf2 (makes debug output without modification to the makefiles)
6) --enable-frame-pointer (work around)

Change 6 is most interesting. Gcc 4.6.0 documents a change in default for
frame pointer omission. Building with only -Os (optimize for size) results
in bigger code because of missing frame pointer omission. Building with
'-Os -fomit-frame-pointer' results in a huge code size increase. Combining
-fomit-frame-pointer and -Os seems to disable -Os. Configuring with
--enable-frame-pointer restores the previous behavior and aviods the problem.

With this xgcc, the only changes needed to get through abuild are:
1) Disable -Wall. -Wall enables the new -Wunused-but-set-variable,
which causes a build fail when unused variables are found.

2) Change to crt0.s edit logic to avoid build fails when crt0.s ends up
with a line starting with ".section .section".

To build with link time optimization enabled additional changes in the
attached patch were used. So far I have tested on AMD Persimmon only.

Thanks,
Scott

Patch

Index: Makefile
===================================================================
--- Makefile	(revision 6483)
+++ Makefile	(working copy)
@@ -211,7 +211,7 @@ 
 de$(EMPTY)fine $(1)-objs_$(2)_template
 $(obj)/$$(1).$(1).o: src/$$(1).$(2) $(obj)/config.h $(4)
 	@printf "    CC         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
-	$(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
+	$(CC) $(3) -MMD $$$$(CFLAGS) $$$$(LTO_OPTIMIZE) -c -o $$$$@ $$$$<
 en$(EMPTY)def
 end$(EMPTY)if
 endef
Index: Makefile.inc
===================================================================
--- Makefile.inc	(revision 6483)
+++ Makefile.inc	(working copy)
@@ -64,7 +64,7 @@ 
 	$(CC) -x assembler-with-cpp -E -MMD -MT $$(@) -D__ACPI__ -P -include $(abspath $(obj)/config.h) -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
 	iasl -p $$(obj)/$(1) -tc $$(basename $$@).asl
 	mv $$(obj)/$(1).hex $$(basename $$@).c
-	$(CC) $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $(1)))), -DAmlCode=AmlCode_$$(basename $$(notdir $(1)))) -c -o $$@ $$(basename $$@).c
+	$(CC) $$(CFLAGS) $$(LTO_OPTIMIZE) $$(if $$(subst dsdt,,$$(basename $$(notdir $(1)))), -DAmlCode=AmlCode_$$(basename $$(notdir $(1)))) -c -o $$@ $$(basename $$@).c
 	# keep %.o: %.c rule from catching the temporary .c file after a make clean
 	mv $$(basename $$@).c $$(basename $$@).hex
 endef
@@ -99,8 +99,14 @@ 
 INCLUDES += -Isrc/devices/oprom/include
 # abspath is a workaround for romcc
 INCLUDES += -include $(abspath $(obj)/config.h)
+
+OPTIMIZE :=-Os -fomit-frame-pointer $(CONFIG_EXTRA_OPTIMIZE) #-march=amdfam10
+LTO_OPTIMIZE :=$(OPTIMIZE)
+ifeq ($(CONFIG_LTO_OPTIMIZE),y)
+LTO_OPTIMIZE :=$(LTO_OPTIMIZE) -flto
+endif
 
-CFLAGS = $(INCLUDES) -Os -pipe -g
+CFLAGS = $(INCLUDES) -pipe -g
 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs
 CFLAGS += -Wstrict-aliasing -Wshadow
@@ -110,7 +116,7 @@ 
 ifneq ($(CONFIG_AMD_AGESA),y)
 CFLAGS += -nostdinc 
 endif
-CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
+CFLAGS += -fno-common -ffreestanding -fno-builtin
 
 additional-dirs := $(objutil)/cbfstool $(objutil)/romcc $(objutil)/options
 
@@ -178,7 +184,7 @@ 
 
 $(obj)/%.ramstage.o: $(obj)/%.c $(obj)/config.h $(OPTION_TABLE_H)
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(CC) -MMD $(CFLAGS) -c -o $@ $<
+	$(CC) -MMD $(CFLAGS) $(LTO_OPTIMIZE) -c -o $@ $<
 
 #######################################################################
 # Clean up rules
Index: src/arch/x86/init/ldscript_fallback_cbfs.lb
===================================================================
--- src/arch/x86/init/ldscript_fallback_cbfs.lb	(revision 6483)
+++ src/arch/x86/init/ldscript_fallback_cbfs.lb	(working copy)
@@ -18,11 +18,11 @@ 
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-/* We use ELF as output format. So that we can debug the code in some form. */
+/* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
-OUTPUT_ARCH(i386)
+OUTPUT_ARCH(i386)
 
-TARGET(binary)
+
 SECTIONS
 {
 	. = CONFIG_ROMBASE;
Index: src/arch/x86/Makefile.bootblock.inc
===================================================================
--- src/arch/x86/Makefile.bootblock.inc	(revision 6483)
+++ src/arch/x86/Makefile.bootblock.inc	(working copy)
@@ -76,13 +76,13 @@ 
 $(obj)/coreboot.romstage: $(obj)/coreboot.pre1 $$(romstage-objs) $(obj)/romstage/ldscript.ld
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
 	printf "CONFIG_ROMBASE = 0x0;\nAUTO_XIP_ROM_BASE = 0x0;\n" > $(obj)/location.ld
-	$(CC) -nostdlib -nostartfiles -static -o $(obj)/romstage.elf -L$(obj) -T $(obj)/romstage/ldscript.ld $(romstage-objs)
-	$(OBJCOPY) -O binary $(obj)/romstage.elf $(obj)/romstage.bin
+	$(CC) $(LTO_OPTIMIZE) -nostdlib -nostartfiles -static -o $(obj)/romstage.elf -L$(obj) -T $(obj)/romstage/ldscript.ld $(romstage-objs)
+	$(OBJCOPY) -O binary $(obj)/romstage.elf $(obj)/romstage.bin
 	printf "CONFIG_ROMBASE = 0x" > $(obj)/location.ld
 	$(CBFSTOOL) $(obj)/coreboot.pre1 locate $(obj)/romstage.bin $(CONFIG_CBFS_PREFIX)/romstage $(CONFIG_XIP_ROM_SIZE) > $(obj)/location.txt
 	cat $(obj)/location.txt >> $(obj)/location.ld
 	printf ';\nAUTO_XIP_ROM_BASE = CONFIG_ROMBASE & ~(CONFIG_XIP_ROM_SIZE - 1);\n' >> $(obj)/location.ld
-	$(CC) -nostdlib -nostartfiles -static -o $(obj)/romstage.elf -L$(obj) -T $(obj)/romstage/ldscript.ld $(romstage-objs)
+	$(CC) $(LTO_OPTIMIZE) -nostdlib -nostartfiles -static -o $(obj)/romstage.elf -L$(obj) -T $(obj)/romstage/ldscript.ld $(romstage-objs)
 	$(NM) -n $(obj)/romstage.elf | sort > $(obj)/romstage.map
 	$(OBJCOPY) --only-keep-debug $(obj)/romstage.elf $(obj)/romstage.debug
 	$(OBJCOPY) --strip-debug $(obj)/romstage.elf
Index: src/arch/x86/Makefile.inc
===================================================================
--- src/arch/x86/Makefile.inc	(revision 6483)
+++ src/arch/x86/Makefile.inc	(working copy)
@@ -129,7 +129,7 @@ 
 
 $(obj)/coreboot_ram: $(obj)/coreboot_ram.o $(src)/arch/x86/coreboot_ram.ld #ldoptions
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(CC) -nostdlib -nostartfiles -static -o $@ -L$(obj) -T $(src)/arch/x86/coreboot_ram.ld $(obj)/coreboot_ram.o
+	$(CC) $(LTO_OPTIMIZE) -nostdlib -nostartfiles -static -o $@ -L$(obj) -T $(src)/arch/x86/coreboot_ram.ld $(obj)/coreboot_ram.o $(obj)/coreboot.a
 	$(NM) -n $(obj)/coreboot_ram | sort > $(obj)/coreboot_ram.map
 	$(OBJCOPY) --only-keep-debug $@ $(obj)/coreboot_ram.debug
 	$(OBJCOPY) --strip-debug $@
@@ -225,11 +225,11 @@ 
 
 $(obj)/mainboard/$(MAINBOARDDIR)/ap_romstage.o: $(src)/mainboard/$(MAINBOARDDIR)/ap_romstage.c $(OPTION_TABLE_H)
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(CC) -MMD $(CFLAGS) -I$(src) -D__PRE_RAM__ -I. -I$(obj) -c $< -o $@
+	$(CC) -MMD $(CFLAGS) $(OPTIMIZE) -I$(src) -D__PRE_RAM__ -I. -I$(obj) -c $< -o $@
 
 $(obj)/mainboard/$(MAINBOARDDIR)/romstage.pre.inc: $(src)/mainboard/$(MAINBOARDDIR)/romstage.c $(OPTION_TABLE_H) $(obj)/build.h $(obj)/config.h
 	@printf "    CC         romstage.inc\n"
-	$(CC) -MMD $(CFLAGS) -D__PRE_RAM__ -I$(src) -I. -I$(obj) -c -S $< -o $@
+	$(CC) -MMD $(CFLAGS) $(OPTIMIZE) -D__PRE_RAM__ -I$(src) -I. -I$(obj) -c -S $< -o $@
 
 $(obj)/mainboard/$(MAINBOARDDIR)/romstage.inc: $(obj)/mainboard/$(MAINBOARDDIR)/romstage.pre.inc
 	@printf "    POST       romstage.inc\n"
Index: src/Kconfig
===================================================================
--- src/Kconfig	(revision 6483)
+++ src/Kconfig	(working copy)
@@ -59,6 +59,14 @@ 
 	bool "LLVM/clang"
 endchoice
 
+config LTO_OPTIMIZE
+	bool "Use gcc -flto link time optimization"
+	default n
+	depends on COMPILER_GCC
+	help
+	  Use with gcc 4.6.0 or later to reduce code size by
+	  removing unused functions.
+
 config SCANBUILD_ENABLE
 	bool "Build with scan-build for static analysis"
 	default n