Skip to contentSkip to conversation

optionrom: fix compilation with mingw docker target

PATCH In review Paolo Bonzini 2 participants 3 messages Aug 9, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 optionrom: fix compilation with mingw docker target

Two fixes are needed. First, mingw does not have -D_FORTIFY_SOURCE, hence --enable-debug disables optimization. This is not acceptable for ROMs, which should override CFLAGS to force inclusion of -O2.

Second, PE stores global constructors and destructors using the following linker script snippet:

     ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
			LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*));  LONG (0);
     ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
			LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*));  LONG (0);

The LONG directives cause the .img files to be 16 bytes too large; the recently added check to signrom.py catches this. To fix this, replace -T and -e options with a linker script.

---
 pc-bios/optionrom/Makefile | 10 +++++++++-
 pc-bios/optionrom/flat.lds |  6 ++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 pc-bios/optionrom/flat.lds
pc-bios/optionrom/Makefile +9 -1
index 9c018ea..8aef152 100644
@@ -9,6 +9,14 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/optionrom)
 
 .PHONY : all clean build-all
 
+# Compiling with no optimization creates ROMs that are too large
+ifeq ($(filter -O%, $(CFLAGS)),)
+override CFLAGS += -O2
+endif
+ifeq ($(filter -O%, $(CFLAGS)),-O0)
+override CFLAGS += -O2
+endif
+
 # Drop -fstack-protector and the like
 QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS)) $(CFLAGS_NOPIE) -ffreestanding
 QEMU_CFLAGS += $(call cc-option, $(QEMU_CFLAGS), -fno-stack-protector)
@@ -51,7 +59,7 @@ endif
 endif
 
 %.img: %.o
-	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m $(LD_EMULATION) -Ttext 0 -e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m $(LD_EMULATION) -T $(SRC_PATH)/pc-bios/optionrom/flat.lds -s -o $@ $<,"  Building $(TARGET_DIR)$@")
 
 %.raw: %.img
 	$(call quiet-command,$(OBJCOPY) -O binary -j .text $< $@,"  Building $(TARGET_DIR)$@")
pc-bios/optionrom/flat.lds new file +6 -0
new file mode 100644
index 0000000..cee2eda
@@ -0,0 +1,6 @@
+SECTIONS
+{
+  . = 0;
+  .text : { *(.text) *(.text.$) }
+}
+ENTRY(_start)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 7 lines · Paolo Bonzini

Two fixes are needed. First, mingw does not have -D_FORTIFY_SOURCE, hence --enable-debug disables optimization. This is not acceptable for ROMs, which should override CFLAGS to force inclusion of -O2.

Second, PE stores global constructors and destructors using the following linker script snippet:

Quoted 8 lines
     ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
                        LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*));  LONG (0);
     ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
                        LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*));  LONG (0);

The LONG directives cause the .img files to be 16 bytes too large; the recently added check to signrom.py catches this. To fix this, replace -T and -e options with a linker script.

...this is really starting to strongly suggest to me that we should not be trying to build our ROM images with whatever random maybe-this-is-an-x86-compiler $CC happens to be...

thanks
-- PMM

#2 lore raw

----- Original Message -----

Quoted 21 lines

From: "Peter Maydell" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Cc: "QEMU Developers" <[email protected]>, "Richard W.M. Jones" <[email protected]>
Sent: Tuesday, August 9, 2016 11:47:17 PM
Subject: Re: [Qemu-devel] [PATCH] optionrom: fix compilation with mingw docker target

Quoted 14 lines · Paolo Bonzini

Two fixes are needed. First, mingw does not have -D_FORTIFY_SOURCE, hence --enable-debug disables optimization. This is not acceptable for ROMs, which should override CFLAGS to force inclusion of -O2.

Second, PE stores global constructors and destructors using the following linker script snippet:

     ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
                        LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*));
                        LONG (0);
     ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
                        LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*));
                        LONG (0);
Quoted 7 lines
Quoted 3 lines

The LONG directives cause the .img files to be 16 bytes too large; the recently added check to signrom.py catches this. To fix this, replace -T and -e options with a linker script.

...this is really starting to strongly suggest to me that we should not be trying to build our ROM images with whatever random maybe-this-is-an-x86-compiler $CC happens to be...

Well, to be precise the problems are in the linker (both the "emulation" thing to this one), not the compiler. That's where most of the variability is between OSes indeed, but the GNU linker is very customizable. So after this patch this is looking a lot like what firmware build systems do (and kvm-unit-tests too), which is in some sense a good sign.

If anything, pc-bios/ should be separately configured and should not use anything from the output of the toplevel configure script. That's a different project and would let everyone compile all ROMs locally just by installing a suitable cross-compiler. It would be nice to have...

Paolo