build-sys: clean up flags included in the linker command line

Some of the CFLAGS that are discovered during configure, for example
compiler warnings, are being included on the linker command line because
QEMU_CFLAGS is added to it.  Other flags, such as the -m32, appear twice
because they are included in both QEMU_CFLAGS and LDFLAGS.  All this
leads to confusion with respect to what goes in which Makefile variables
(and we have plenty).

So, introduce QEMU_LDFLAGS for flags discovered by configure, following
the lead of QEMU_CFLAGS, and stop adding to it:

1) options that are already in CFLAGS, for example "-g"

2) duplicate options

At the same time, options that _are_ needed by both compiler and linker
must now be added to both QEMU_CFLAGS and QEMU_LDFLAGS, which is clearer.
This is mostly -fsanitize options.  For now, --extra-cflags has this behavior
(but --extra-cxxflags does not).

Meson will not include CFLAGS on the linker command line, do the same in our
build system as well.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2019-12-11 15:34:27 +01:00
parent 673b0add9e
commit db5adeaa84
5 changed files with 38 additions and 37 deletions

View File

@ -327,7 +327,7 @@ matrix:
- TEST_CMD="" - TEST_CMD=""
before_script: before_script:
- mkdir -p ${BUILD_DIR} && cd ${BUILD_DIR} - mkdir -p ${BUILD_DIR} && cd ${BUILD_DIR}
- ${SRC_DIR}/configure ${CONFIG} --extra-cflags="-g3 -O0 -Wno-error=stringop-truncation -fsanitize=thread -fuse-ld=gold" || { cat config.log && exit 1; } - ${SRC_DIR}/configure ${CONFIG} --extra-cflags="-g3 -O0 -Wno-error=stringop-truncation -fsanitize=thread" --extra-ldflags="-fuse-ld=gold" || { cat config.log && exit 1; }
# Run check-tcg against linux-user # Run check-tcg against linux-user

View File

@ -490,7 +490,7 @@ DTC_CPPFLAGS=-I$(BUILD_DIR)/dtc -I$(SRC_PATH)/dtc -I$(SRC_PATH)/dtc/libfdt
.PHONY: dtc/all .PHONY: dtc/all
dtc/all: .git-submodule-status dtc/libfdt dtc/tests dtc/all: .git-submodule-status dtc/libfdt dtc/tests
$(call quiet-command,$(MAKE) $(DTC_MAKE_ARGS) CPPFLAGS="$(DTC_CPPFLAGS)" CFLAGS="$(DTC_CFLAGS)" LDFLAGS="$(LDFLAGS)" ARFLAGS="$(ARFLAGS)" CC="$(CC)" AR="$(AR)" LD="$(LD)" $(SUBDIR_MAKEFLAGS) libfdt/libfdt.a,) $(call quiet-command,$(MAKE) $(DTC_MAKE_ARGS) CPPFLAGS="$(DTC_CPPFLAGS)" CFLAGS="$(DTC_CFLAGS)" LDFLAGS="$(QEMU_LDFLAGS)" ARFLAGS="$(ARFLAGS)" CC="$(CC)" AR="$(AR)" LD="$(LD)" $(SUBDIR_MAKEFLAGS) libfdt/libfdt.a,)
dtc/%: .git-submodule-status dtc/%: .git-submodule-status
@mkdir -p $@ @mkdir -p $@
@ -517,7 +517,7 @@ slirp/all: .git-submodule-status
BUILD_DIR="$(BUILD_DIR)/slirp" \ BUILD_DIR="$(BUILD_DIR)/slirp" \
PKG_CONFIG="$(PKG_CONFIG)" \ PKG_CONFIG="$(PKG_CONFIG)" \
CC="$(CC)" AR="$(AR)" LD="$(LD)" RANLIB="$(RANLIB)" \ CC="$(CC)" AR="$(AR)" LD="$(LD)" RANLIB="$(RANLIB)" \
CFLAGS="$(QEMU_CFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)") CFLAGS="$(QEMU_CFLAGS) $(CFLAGS)" LDFLAGS="$(QEMU_LDFLAGS)")
# Compatibility gunk to keep make working across the rename of targets # Compatibility gunk to keep make working across the rename of targets
# for recursion, to be removed some time after 4.1. # for recursion, to be removed some time after 4.1.

61
configure vendored
View File

@ -126,7 +126,7 @@ compile_object() {
compile_prog() { compile_prog() {
local_cflags="$1" local_cflags="$1"
local_ldflags="$2" local_ldflags="$2"
do_cc $QEMU_CFLAGS $local_cflags -o $TMPE $TMPC $LDFLAGS $local_ldflags do_cc $QEMU_CFLAGS $local_cflags -o $TMPE $TMPC $QEMU_LDFLAGS $local_ldflags
} }
# symbolically link $1 to $2. Portable version of "ln -sf". # symbolically link $1 to $2. Portable version of "ln -sf".
@ -523,10 +523,11 @@ for opt do
--cpu=*) cpu="$optarg" --cpu=*) cpu="$optarg"
;; ;;
--extra-cflags=*) QEMU_CFLAGS="$QEMU_CFLAGS $optarg" --extra-cflags=*) QEMU_CFLAGS="$QEMU_CFLAGS $optarg"
QEMU_LDFLAGS="$QEMU_LDFLAGS $optarg"
;; ;;
--extra-cxxflags=*) QEMU_CXXFLAGS="$QEMU_CXXFLAGS $optarg" --extra-cxxflags=*) QEMU_CXXFLAGS="$QEMU_CXXFLAGS $optarg"
;; ;;
--extra-ldflags=*) LDFLAGS="$LDFLAGS $optarg" --extra-ldflags=*) QEMU_LDFLAGS="$QEMU_LDFLAGS $optarg"
EXTRA_LDFLAGS="$optarg" EXTRA_LDFLAGS="$optarg"
;; ;;
--enable-debug-info) debug_info="yes" --enable-debug-info) debug_info="yes"
@ -599,7 +600,6 @@ QEMU_INCLUDES="-iquote . -iquote \$(SRC_PATH) -iquote \$(SRC_PATH)/accel/tcg -iq
QEMU_INCLUDES="$QEMU_INCLUDES -iquote \$(SRC_PATH)/disas/libvixl" QEMU_INCLUDES="$QEMU_INCLUDES -iquote \$(SRC_PATH)/disas/libvixl"
if test "$debug_info" = "yes"; then if test "$debug_info" = "yes"; then
CFLAGS="-g $CFLAGS" CFLAGS="-g $CFLAGS"
LDFLAGS="-g $LDFLAGS"
fi fi
# running configure in the source tree? # running configure in the source tree?
@ -845,12 +845,12 @@ Darwin)
LDFLAGS_SHARED="-bundle -undefined dynamic_lookup" LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
if [ "$cpu" = "x86_64" ] ; then if [ "$cpu" = "x86_64" ] ; then
QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS" QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
LDFLAGS="-arch x86_64 $LDFLAGS" QEMU_LDFLAGS="-arch x86_64 $QEMU_LDFLAGS"
fi fi
cocoa="yes" cocoa="yes"
audio_drv_list="coreaudio try-sdl" audio_drv_list="coreaudio try-sdl"
audio_possible_drivers="coreaudio sdl" audio_possible_drivers="coreaudio sdl"
LDFLAGS="-framework CoreFoundation -framework IOKit $LDFLAGS" QEMU_LDFLAGS="-framework CoreFoundation -framework IOKit $QEMU_LDFLAGS"
libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu" libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu"
# Disable attempts to use ObjectiveC features in os/object.h since they # Disable attempts to use ObjectiveC features in os/object.h since they
# won't work when we're compiling with gcc as a C compiler. # won't work when we're compiling with gcc as a C compiler.
@ -1025,7 +1025,7 @@ for opt do
;; ;;
--static) --static)
static="yes" static="yes"
LDFLAGS="-static $LDFLAGS" QEMU_LDFLAGS="-static $QEMU_LDFLAGS"
QEMU_PKG_CONFIG_FLAGS="--static $QEMU_PKG_CONFIG_FLAGS" QEMU_PKG_CONFIG_FLAGS="--static $QEMU_PKG_CONFIG_FLAGS"
;; ;;
--mandir=*) mandir="$optarg" --mandir=*) mandir="$optarg"
@ -1551,42 +1551,42 @@ done
case "$cpu" in case "$cpu" in
ppc) ppc)
CPU_CFLAGS="-m32" CPU_CFLAGS="-m32"
LDFLAGS="-m32 $LDFLAGS" QEMU_LDFLAGS="-m32 $QEMU_LDFLAGS"
;; ;;
ppc64) ppc64)
CPU_CFLAGS="-m64" CPU_CFLAGS="-m64"
LDFLAGS="-m64 $LDFLAGS" QEMU_LDFLAGS="-m64 $QEMU_LDFLAGS"
;; ;;
sparc) sparc)
CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc" CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc"
LDFLAGS="-m32 -mv8plus $LDFLAGS" QEMU_LDFLAGS="-m32 -mv8plus $QEMU_LDFLAGS"
;; ;;
sparc64) sparc64)
CPU_CFLAGS="-m64 -mcpu=ultrasparc" CPU_CFLAGS="-m64 -mcpu=ultrasparc"
LDFLAGS="-m64 $LDFLAGS" QEMU_LDFLAGS="-m64 $QEMU_LDFLAGS"
;; ;;
s390) s390)
CPU_CFLAGS="-m31" CPU_CFLAGS="-m31"
LDFLAGS="-m31 $LDFLAGS" QEMU_LDFLAGS="-m31 $QEMU_LDFLAGS"
;; ;;
s390x) s390x)
CPU_CFLAGS="-m64" CPU_CFLAGS="-m64"
LDFLAGS="-m64 $LDFLAGS" QEMU_LDFLAGS="-m64 $QEMU_LDFLAGS"
;; ;;
i386) i386)
CPU_CFLAGS="-m32" CPU_CFLAGS="-m32"
LDFLAGS="-m32 $LDFLAGS" QEMU_LDFLAGS="-m32 $QEMU_LDFLAGS"
;; ;;
x86_64) x86_64)
# ??? Only extremely old AMD cpus do not have cmpxchg16b. # ??? Only extremely old AMD cpus do not have cmpxchg16b.
# If we truly care, we should simply detect this case at # If we truly care, we should simply detect this case at
# runtime and generate the fallback to serial emulation. # runtime and generate the fallback to serial emulation.
CPU_CFLAGS="-m64 -mcx16" CPU_CFLAGS="-m64 -mcx16"
LDFLAGS="-m64 $LDFLAGS" QEMU_LDFLAGS="-m64 $QEMU_LDFLAGS"
;; ;;
x32) x32)
CPU_CFLAGS="-mx32" CPU_CFLAGS="-mx32"
LDFLAGS="-mx32 $LDFLAGS" QEMU_LDFLAGS="-mx32 $QEMU_LDFLAGS"
;; ;;
# No special flags required for other host CPUs # No special flags required for other host CPUs
esac esac
@ -1967,6 +1967,7 @@ EOF
if do_cc $QEMU_CFLAGS -Werror $flag -c -o $TMPO $TMPC && if do_cc $QEMU_CFLAGS -Werror $flag -c -o $TMPO $TMPC &&
compile_prog "-Werror $flag" ""; then compile_prog "-Werror $flag" ""; then
QEMU_CFLAGS="$QEMU_CFLAGS $flag" QEMU_CFLAGS="$QEMU_CFLAGS $flag"
QEMU_LDFLAGS="$QEMU_LDFLAGS $flag"
sp_on=1 sp_on=1
break break
fi fi
@ -2051,10 +2052,10 @@ EOF
if compile_prog "-fPIE -DPIE" "-pie"; then if compile_prog "-fPIE -DPIE" "-pie"; then
QEMU_CFLAGS="-fPIE -DPIE $QEMU_CFLAGS" QEMU_CFLAGS="-fPIE -DPIE $QEMU_CFLAGS"
LDFLAGS="-pie $LDFLAGS" QEMU_LDFLAGS="-pie $QEMU_LDFLAGS"
pie="yes" pie="yes"
if compile_prog "" "-Wl,-z,relro -Wl,-z,now" ; then if compile_prog "" "-Wl,-z,relro -Wl,-z,now" ; then
LDFLAGS="-Wl,-z,relro -Wl,-z,now $LDFLAGS" QEMU_LDFLAGS="-Wl,-z,relro -Wl,-z,now $QEMU_LDFLAGS"
fi fi
else else
if test "$pie" = "yes"; then if test "$pie" = "yes"; then
@ -5837,7 +5838,7 @@ EOF
hvf='no' hvf='no'
else else
hvf='yes' hvf='yes'
LDFLAGS="-framework Hypervisor $LDFLAGS" QEMU_LDFLAGS="-framework Hypervisor $QEMU_LDFLAGS"
fi fi
fi fi
@ -6071,8 +6072,8 @@ esac
write_c_skeleton write_c_skeleton
if test "$gcov" = "yes" ; then if test "$gcov" = "yes" ; then
CFLAGS="-fprofile-arcs -ftest-coverage -g $CFLAGS" QEMU_CFLAGS="-fprofile-arcs -ftest-coverage -g $QEMU_CFLAGS"
LDFLAGS="-fprofile-arcs -ftest-coverage $LDFLAGS" QEMU_LDFLAGS="-fprofile-arcs -ftest-coverage $QEMU_LDFLAGS"
elif test "$fortify_source" = "yes" ; then elif test "$fortify_source" = "yes" ; then
CFLAGS="-O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 $CFLAGS" CFLAGS="-O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 $CFLAGS"
elif test "$debug" = "no"; then elif test "$debug" = "no"; then
@ -6080,7 +6081,8 @@ elif test "$debug" = "no"; then
fi fi
if test "$have_asan" = "yes"; then if test "$have_asan" = "yes"; then
CFLAGS="-fsanitize=address $CFLAGS" QEMU_CFLAGS="-fsanitize=address $QEMU_CFLAGS"
QEMU_LDFLAGS="-fsanitize=address $QEMU_LDFLAGS"
if test "$have_asan_iface_h" = "no" ; then if test "$have_asan_iface_h" = "no" ; then
echo "ASAN build enabled, but ASAN header missing." \ echo "ASAN build enabled, but ASAN header missing." \
"Without code annotation, the report may be inferior." "Without code annotation, the report may be inferior."
@ -6090,7 +6092,8 @@ if test "$have_asan" = "yes"; then
fi fi
fi fi
if test "$have_ubsan" = "yes"; then if test "$have_ubsan" = "yes"; then
CFLAGS="-fsanitize=undefined $CFLAGS" QEMU_CFLAGS="-fsanitize=undefined $QEMU_CFLAGS"
QEMU_LDFLAGS="-fsanitize=undefined $QEMU_LDFLAGS"
fi fi
########################################## ##########################################
@ -6125,7 +6128,7 @@ fi
if test "$solaris" = "no" ; then if test "$solaris" = "no" ; then
if $ld --version 2>/dev/null | grep "GNU ld" >/dev/null 2>/dev/null ; then if $ld --version 2>/dev/null | grep "GNU ld" >/dev/null 2>/dev/null ; then
LDFLAGS="-Wl,--warn-common $LDFLAGS" QEMU_LDFLAGS="-Wl,--warn-common $QEMU_LDFLAGS"
fi fi
fi fi
@ -6140,7 +6143,7 @@ fi
if test "$mingw32" = "yes" ; then if test "$mingw32" = "yes" ; then
for flag in --dynamicbase --no-seh --nxcompat; do for flag in --dynamicbase --no-seh --nxcompat; do
if ld_has $flag ; then if ld_has $flag ; then
LDFLAGS="-Wl,$flag $LDFLAGS" QEMU_LDFLAGS="-Wl,$flag $QEMU_LDFLAGS"
fi fi
done done
fi fi
@ -6375,7 +6378,7 @@ EOF
update_cxxflags update_cxxflags
if do_cxx $QEMU_CXXFLAGS -o $TMPE $TMPCXX $TMPO $LDFLAGS; then if do_cxx $QEMU_CXXFLAGS -o $TMPE $TMPCXX $TMPO $QEMU_LDFLAGS; then
# C++ compiler $cxx works ok with C compiler $cc # C++ compiler $cxx works ok with C compiler $cc
: :
else else
@ -6427,7 +6430,6 @@ echo "Objective-C compiler $objcc"
echo "ARFLAGS $ARFLAGS" echo "ARFLAGS $ARFLAGS"
echo "CFLAGS $CFLAGS" echo "CFLAGS $CFLAGS"
echo "QEMU_CFLAGS $QEMU_CFLAGS" echo "QEMU_CFLAGS $QEMU_CFLAGS"
echo "LDFLAGS $LDFLAGS"
echo "QEMU_LDFLAGS $QEMU_LDFLAGS" echo "QEMU_LDFLAGS $QEMU_LDFLAGS"
echo "make $make" echo "make $make"
echo "install $install" echo "install $install"
@ -7479,9 +7481,8 @@ if test "$sparse" = "yes" ; then
echo "HOST_CC := REAL_CC=\"\$(HOST_CC)\" cgcc" >> $config_host_mak echo "HOST_CC := REAL_CC=\"\$(HOST_CC)\" cgcc" >> $config_host_mak
echo "QEMU_CFLAGS += -Wbitwise -Wno-transparent-union -Wno-old-initializer -Wno-non-pointer-null" >> $config_host_mak echo "QEMU_CFLAGS += -Wbitwise -Wno-transparent-union -Wno-old-initializer -Wno-non-pointer-null" >> $config_host_mak
fi fi
echo "LDFLAGS=$LDFLAGS" >> $config_host_mak
echo "LDFLAGS_NOPIE=$LDFLAGS_NOPIE" >> $config_host_mak
echo "QEMU_LDFLAGS=$QEMU_LDFLAGS" >> $config_host_mak echo "QEMU_LDFLAGS=$QEMU_LDFLAGS" >> $config_host_mak
echo "LDFLAGS_NOPIE=$LDFLAGS_NOPIE" >> $config_host_mak
echo "LD_REL_FLAGS=$LD_REL_FLAGS" >> $config_host_mak echo "LD_REL_FLAGS=$LD_REL_FLAGS" >> $config_host_mak
echo "LD_I386_EMULATION=$ld_i386_emulation" >> $config_host_mak echo "LD_I386_EMULATION=$ld_i386_emulation" >> $config_host_mak
echo "LIBS+=$LIBS" >> $config_host_mak echo "LIBS+=$LIBS" >> $config_host_mak
@ -7807,7 +7808,7 @@ if test "$target_bsd_user" = "yes" ; then
fi fi
# generate QEMU_CFLAGS/LDFLAGS for targets # generate QEMU_CFLAGS/QEMU_LDFLAGS for targets
cflags="" cflags=""
ldflags="" ldflags=""
@ -7928,7 +7929,7 @@ if test "$TARGET_ARCH" = "s390x" && test "$target_softmmu" = "yes" && \
fi fi
fi fi
echo "LDFLAGS+=$ldflags" >> $config_target_mak echo "QEMU_LDFLAGS+=$ldflags" >> $config_target_mak
echo "QEMU_CFLAGS+=$cflags" >> $config_target_mak echo "QEMU_CFLAGS+=$cflags" >> $config_target_mak
done # for target in $targets done # for target in $targets

View File

@ -5,9 +5,9 @@ qga-vss-dll-obj-y += requester.o provider.o install.o
obj-qga-vss-dll-obj-y = $(addprefix $(obj)/, $(qga-vss-dll-obj-y)) obj-qga-vss-dll-obj-y = $(addprefix $(obj)/, $(qga-vss-dll-obj-y))
$(obj-qga-vss-dll-obj-y): QEMU_CXXFLAGS := $(filter-out -fstack-protector-all -fstack-protector-strong, $(QEMU_CXXFLAGS)) -Wno-unknown-pragmas -Wno-delete-non-virtual-dtor $(obj-qga-vss-dll-obj-y): QEMU_CXXFLAGS := $(filter-out -fstack-protector-all -fstack-protector-strong, $(QEMU_CXXFLAGS)) -Wno-unknown-pragmas -Wno-delete-non-virtual-dtor
$(obj)/qga-vss.dll: LDFLAGS = -shared -Wl,--add-stdcall-alias,--enable-stdcall-fixup -lglib-2.0 -lole32 -loleaut32 -lshlwapi -luuid -lintl -lws2_32 -static QGA_VSS_LDFLAGS = -shared -Wl,--add-stdcall-alias,--enable-stdcall-fixup -lglib-2.0 -lole32 -loleaut32 -lshlwapi -luuid -lintl -lws2_32 -static
$(obj)/qga-vss.dll: $(obj-qga-vss-dll-obj-y) $(SRC_PATH)/$(obj)/qga-vss.def $(obj)/qga-vss.dll: $(obj-qga-vss-dll-obj-y) $(SRC_PATH)/$(obj)/qga-vss.def
$(call quiet-command,$(CXX) -o $@ $(qga-vss-dll-obj-y) $(SRC_PATH)/qga/vss-win32/qga-vss.def $(CXXFLAGS) $(LDFLAGS),"LINK","$(TARGET_DIR)$@") $(call quiet-command,$(CXX) -o $@ $(qga-vss-dll-obj-y) $(SRC_PATH)/qga/vss-win32/qga-vss.def $(CXXFLAGS) $(QGA_VSS_LDFLAGS),"LINK","$(TARGET_DIR)$@")
# rules to build qga-provider.tlb # rules to build qga-provider.tlb

View File

@ -76,7 +76,7 @@ expand-objs = $(strip $(sort $(filter %.o,$1)) \
# must link with the C++ compiler, not the plain C compiler. # must link with the C++ compiler, not the plain C compiler.
LINKPROG = $(or $(CXX),$(CC)) LINKPROG = $(or $(CXX),$(CC))
LINK = $(call quiet-command, $(LINKPROG) $(QEMU_LDFLAGS) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \ LINK = $(call quiet-command, $(LINKPROG) $(CFLAGS) $(QEMU_LDFLAGS) -o $@ \
$(call process-archive-undefs, $1) \ $(call process-archive-undefs, $1) \
$(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@") $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
@ -105,7 +105,7 @@ LINK = $(call quiet-command, $(LINKPROG) $(QEMU_LDFLAGS) $(QEMU_CFLAGS) $(CFLAGS
DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS) module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
%$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED) %$(DSOSUF): QEMU_LDFLAGS += $(LDFLAGS_SHARED)
%$(DSOSUF): %.mo %$(DSOSUF): %.mo
$(call LINK,$^) $(call LINK,$^)
@# Copy to build root so modules can be loaded when program started without install @# Copy to build root so modules can be loaded when program started without install