Skip to contentSkip to conversation

Shared Library Module Support

RFC PATCH Superseded Fam Zheng 6 participants 19 messages Sep 5, 2013 – Sep 6, 2013

Subsystems (2)

v1 of 20 Superseded Latest is v20 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover Shared Library Module Support

This series implements feature of shared object building as described in:

http://wiki.qemu.org/Features/Modules

It's achieved in three steps, with extra bonus to change curl to a shared library module in the end (only to demonstrate the usage, no "make install" support of .so files yet).

1. Allow per object cflags and libs:

[01/06] make.rule: fix $(obj) to a real relative path [02/06] rule.mak: allow per object cflags and libs

2. Rules for building .so:

[03/06] Makefile: define curl cflags and libs with object

3. Code to load module. All .so files are scanned and loaded when program starts:

[04/06] Makefile: introduce common-obj-m and block-obj-m for DSO

4. curl adoption:

[05/06] module: load modules at start
[06/06] curl: build as shared library

Fam Zheng (6):
make.rule: fix $(obj) to a real relative path rule.mak: allow per object cflags and libs
Makefile: define curl cflags and libs with object
Makefile: introduce common-obj-m and block-obj-m for DSO
module: load modules at start
curl: build as shared library

 Makefile              | 24 +++++++++++++++++++++---
 Makefile.objs         | 10 +++++++++-
 Makefile.target       |  3 ++-
 block/Makefile.objs   |  3 ++-
 configure             | 28 ++++++++++++++++------------
 include/qemu/module.h |  2 ++
 qemu-img.c            |  2 ++
 qemu-io.c             |  1 +
 qemu-nbd.c            |  1 +
 rules.mak             | 20 ++++++++++++++------
 scripts/create_config |  3 +++
 util/Makefile.objs    |  2 ++
 util/module.c         | 40 ++++++++++++++++++++++++++++++++++++++++
 vl.c                  |  1 +
 14 files changed, 116 insertions(+), 24 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

RFC PATCH 1/6 make.rule: fix $(obj) to a real relative path

Makefile.target includes rule.mak and unnested common-obj-y, then prefix them with '../', this will ignore object specific QEMU_CFLAGS in subdir Makefile.objs:

    $(obj)/curl.o: QEMU_CFLAGS += $(CURL_CFLAGS)

Because $(obj) here is './block', instead of '../block'. This doesn't hurt compiling because we basically build all .o from top Makefile, before entering Makefile.target, but it will affact arriving per-object libs support.

The starting point of $(obj) is fixed before including ./Makefile.objs, to get consistency with nested Makefile rules in target rule and variable definition.

---
 Makefile.target | 3 ++-
 rules.mak       | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)
Makefile.target +2 -1
index 9a49852..b35e7c1 100644
@@ -144,12 +144,13 @@ endif # CONFIG_SOFTMMU
 %/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
 
 nested-vars += obj-y
+obj := ..
 
 # This resolves all nested paths, so it must come last
 include $(SRC_PATH)/Makefile.objs
 
 all-obj-y = $(obj-y)
-all-obj-y += $(addprefix ../, $(common-obj-y))
+all-obj-y += $(addprefix $(obj)/, $(common-obj-y))
 
 ifndef CONFIG_HAIKU
 LIBS+=-lm
rules.mak +3 -3
index 4499745..5758137 100644
@@ -103,7 +103,6 @@ clean: clean-timestamp
 
 # magic to descend into other directories
 
-obj := .
 old-nested-dirs :=
 
 define push-var
@@ -119,9 +118,10 @@ endef
 
 define unnest-dir
 $(foreach var,$(nested-vars),$(call push-var,$(var),$1/))
-$(eval obj := $(obj)/$1)
+$(eval old-obj := $(obj))
+$(eval obj := $(if $(obj),$(obj)/$1,$1))
 $(eval include $(SRC_PATH)/$1/Makefile.objs)
-$(eval obj := $(patsubst %/$1,%,$(obj)))
+$(eval obj := $(old-obj))
 $(foreach var,$(nested-vars),$(call pop-var,$(var),$1/))
 endef
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [Qemu-devel] [RFC PATCH 1/6] make.rule: fix $(obj) to a real relative path

Il 05/09/2013 12:20, Fam Zheng ha scritto:

Quoted 27 lines

Makefile.target includes rule.mak and unnested common-obj-y, then prefix them with '../', this will ignore object specific QEMU_CFLAGS in subdir Makefile.objs:

    $(obj)/curl.o: QEMU_CFLAGS += $(CURL_CFLAGS)

Because $(obj) here is './block', instead of '../block'. This doesn't hurt compiling because we basically build all .o from top Makefile, before entering Makefile.target, but it will affact arriving per-object libs support.

The starting point of $(obj) is fixed before including ./Makefile.objs, to get consistency with nested Makefile rules in target rule and variable definition.

---
 Makefile.target | 3 ++-
 rules.mak       | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)
Makefile.target +0 -0
index 9a49852..b35e7c1 100644
@@ -144,12 +144,13 @@ endif # CONFIG_SOFTMMU
 %/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)

Makefile.target endif # CONFIG_SOFTMMU · line 153

Quoted 10 lines
Makefile.target +2 -1
 
 nested-vars += obj-y
+obj := ..
 
 # This resolves all nested paths, so it must come last
 include $(SRC_PATH)/Makefile.objs
 
 all-obj-y = $(obj-y)
-all-obj-y += $(addprefix ../, $(common-obj-y))
+all-obj-y += $(addprefix $(obj)/, $(common-obj-y))

The bug is clearly there, but I'm not sure this is correct.

"obj" is the path to the object file. obj-y files are built in the target directory, thus the extra ".." should apply only to common-obj-y; $(obj-y) should not be prefixed. So perhaps you need to:

1) move the nested-vars and unnest-vars call from Makefile.objs to Makefile. also move this line:

   QEMU_CFLAGS+=$(GLIB_CFLAGS)

which has no business in Makefile.objs. With this change, Makefile.objs is just a list, like other */Makefile.objs files.

2) remove the additional complication where common-obj-y is including block-obj-y, just add it to all-obj-y manually.

3) in Makefile.target, do this:

block-obj-y = ../
common-obj-y = ../
nested-vars = obj-y block-obj-y common-obj-y dummy := $(call unnest-vars)
all-obj-y = $(obj-y) $(common-obj-y) $(block-obj-y)

(Can all be done in a single patch).

?

Quoted 16 lines
Makefile.target +0 -0
 ifndef CONFIG_HAIKU
 LIBS+=-lm
rules.mak +0 -1
index 4499745..5758137 100644
@@ -103,7 +103,6 @@ clean: clean-timestamp
 
 # magic to descend into other directories
 
-obj := .
 old-nested-dirs :=
 
 define push-var

rules.mak endef · line 125

Quoted 10 lines
rules.mak +3 -2
@@ -119,9 +118,10 @@ endef
 
 define unnest-dir
 $(foreach var,$(nested-vars),$(call push-var,$(var),$1/))
-$(eval obj := $(obj)/$1)
+$(eval old-obj := $(obj))
+$(eval obj := $(if $(obj),$(obj)/$1,$1))
 $(eval include $(SRC_PATH)/$1/Makefile.objs)
-$(eval obj := $(patsubst %/$1,%,$(obj)))
+$(eval obj := $(old-obj))
 $(foreach var,$(nested-vars),$(call pop-var,$(var),$1/))

old-obj doesn't work if you have multiple levels of nesting. But you can call the variable something like obj-parent-$1 instead to solve this problem.

However, please clean it up afterwards with "$(eval obj-parent-$1 := )".

Paolo

Quoted 3 lines
rules.mak +0 -0
 endef
 
author
#3 lore raw

RFC PATCH 2/6 rule.mak: allow per object cflags and libs

In reply to Fam Zheng

Adds extract-libs in LINK to expand any "per object libs", the syntax to define such a libs options is like:

        $(obj)/curl.o-libs = $(CURL_LIBS)

in block/Makefile.objs.

Similarly,

        $(obj)foo.o-cflags = $(FOO_CFLAGS)

is also supported.

---
 rules.mak | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
rules.mak +5 -3
index 5758137..8276421 100644
@@ -17,15 +17,17 @@ QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(*D)/$(*F).d
 # Same as -I$(SRC_PATH) -I., but for the nested source/object directories
 QEMU_INCLUDES += -I$(<D) -I$(@D)
 
+extract-libs = $(strip $(foreach o,$1,$($(obj-base)$o-libs)))
+
 %.o: %.c
-	$(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<,"  CC    $(TARGET_DIR)$@")
+	$(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"  CC    $(TARGET_DIR)$@")
 %.o: %.rc
 	$(call quiet-command,$(WINDRES) -I. -o $@ $<,"  RC    $(TARGET_DIR)$@")
 
 ifeq ($(LIBTOOL),)
 LINK = $(call quiet-command,$(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
        $(sort $(filter %.o, $1)) $(filter-out %.o, $1) $(version-obj-y) \
-       $(LIBS),"  LINK  $(TARGET_DIR)$@")
+       $(call extract-libs,$^) $(LIBS),"  LINK  $(TARGET_DIR)$@")
 else
 LIBTOOL += $(if $(V),,--quiet)
 %.lo: %.c
@@ -41,7 +43,7 @@ LINK = $(call quiet-command,\
        $(sort $(filter %.o, $1)) $(filter-out %.o, $1) \
        $(if $(filter %.lo %.la,$^),$(version-lobj-y),$(version-obj-y)) \
        $(if $(filter %.lo %.la,$^),$(LIBTOOLFLAGS)) \
-       $(LIBS),$(if $(filter %.lo %.la,$^),"lt LINK ", "  LINK  ")"$(TARGET_DIR)$@")
+       $(call extract-libs,$^) $(LIBS),$(if $(filter %.lo %.la,$^),"lt LINK ", "  LINK  ")"$(TARGET_DIR)$@")
 endif
 
 %.asm: %.S
Signature · 1 line
1.8.3.1
author
#4 lore raw

RFC PATCH 3/6 Makefile: define curl cflags and libs with object

In reply to Fam Zheng

We have per object cflags and libs support now, move CURL_CFLAGS and CURL_LIBS from global option variables to a per object basis.

---
 block/Makefile.objs | 3 ++-
 configure           | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)
block/Makefile.objs +2 -1
index 4cf9aa4..b1e1520 100644
@@ -23,4 +23,5 @@ common-obj-y += commit.o
 common-obj-y += mirror.o
 common-obj-y += backup.o
 
-$(obj)/curl.o: QEMU_CFLAGS+=$(CURL_CFLAGS)
+$(obj)/curl.o-cflags := $(CURL_CFLAGS)
+$(obj)/curl.o-libs := $(CURL_LIBS)
configure +1 -2
index 0a55c20..48e14b8 100755
@@ -2199,8 +2199,6 @@ EOF
   curl_libs=`$curlconfig --libs 2>/dev/null`
   if compile_prog "$curl_cflags" "$curl_libs" ; then
     curl=yes
-    libs_tools="$curl_libs $libs_tools"
-    libs_softmmu="$curl_libs $libs_softmmu"
   else
     if test "$curl" = "yes" ; then
       feature_not_found "curl"
@@ -3878,6 +3876,7 @@ fi
 if test "$curl" = "yes" ; then
   echo "CONFIG_CURL=y" >> $config_host_mak
   echo "CURL_CFLAGS=$curl_cflags" >> $config_host_mak
+  echo "CURL_LIBS=$curl_libs" >> $config_host_mak
 fi
 if test "$brlapi" = "yes" ; then
   echo "CONFIG_BRLAPI=y" >> $config_host_mak
Signature · 1 line
1.8.3.1
author
#5 lore raw

RFC PATCH 4/6 Makefile: introduce common-obj-m and block-obj-m for DSO

In reply to Fam Zheng

Add necessary rules and flags for shared object generation. common-obj-m will include block-obj-m, as common-obj-y for block-obj-y. The rules introduced here are:

QEMU_CFLAGS += -shared -fPIC, for all %.o of shared objects.

1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS += -shared -fPIC". Then "linked" to %.mo, which is an incremental object with "ln -r". This step is for consistency with %.mod case and has no effect.

2) %.mod in $(common-obj-m) is generated by "ln -r", with all its dependencies (multiple *.o) as input. Which means the list of dependency objects must be ruled out in each sub-Makefile.objs like:

$(obj)/foo.mod: $(addprefix $(obj)/,bar.o baz.o qux.o)

Notice that $(obj)/ is required for both target and dependency in the rule.

---
 Makefile      | 24 +++++++++++++++++++++---
 Makefile.objs | 10 +++++++++-
 configure     |  3 +++
 rules.mak     |  6 ++++++
 4 files changed, 39 insertions(+), 4 deletions(-)
Makefile +21 -3
index 806946e..130a497 100644
@@ -56,7 +56,7 @@ Makefile: ;
 configure: ;
 
 .PHONY: all clean cscope distclean dvi html info install install-doc \
-	pdf recurse-all speed test dist
+	pdf recurse-all speed test dist modules
 
 $(call set-vpath, $(SRC_PATH))
 
@@ -121,7 +121,22 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
 include $(SRC_PATH)/libcacard/Makefile
 endif
 
-all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
+all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
+
+mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
+          $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))
+mod-obj-y = $(patsubst %.mo,%.mo.o,$(filter %.mo,$(common-obj-y)))
+
+# Generate rules for single file modules (%.mo: %.o).
+# For multi file modules, dependencies should be listed explicitly in
+# Makefile.objs
+$(foreach o,$(mod-obj-m) $(mod-obj-y),$(eval \
+	$(patsubst %$(DSOSUF),%.mo,$o): $(patsubst %.c,%.o,$(wildcard $(patsubst %$(DSOSUF),%.c,$o))) \
+	))
+
+modules: $(mod-obj-m)
+modules: BUILD_DYNAMIC = 1
+modules: QEMU_CFLAGS += -shared -fPIC
 
 config-host.h: config-host.h-timestamp
 config-host.h-timestamp: config-host.mak
@@ -155,7 +170,7 @@ subdir-dtc:dtc/libfdt dtc/tests
 dtc/%:
 	mkdir -p $@
 
-$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
+$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(common-obj-m)
 
 ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
 romsubdir-%:
@@ -235,6 +250,9 @@ clean:
 	rm -f qemu-options.def
 	find . -name '*.[oda]' -type f -exec rm -f {} +
 	find . -name '*.l[oa]' -type f -exec rm -f {} +
+	find . -name '*'$(DSOSUF) -type f -exec rm -f {} +
+	find . -name '*.mo' -type f -exec rm -f {} +
+
 	rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 	rm -Rf .libs
 	rm -f qemu-img-cmds.h
Makefile.objs +9 -1
index f46a4cd..2f178af 100644
@@ -19,6 +19,8 @@ block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
 block-obj-y += qemu-coroutine-sleep.o
 block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 
+block-obj-m = block/
+
 ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
 # Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
 # only pull in the actual virtio-9p device if we also enabled virtio.
@@ -83,6 +85,9 @@ common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
 
 common-obj-y += qmp-marshal.o
 common-obj-y += qmp.o hmp.o
+
+common-obj-m = $(block-obj-m)
+
 endif
 
 ######################################################################
@@ -121,5 +126,8 @@ nested-vars += \
 	util-obj-y \
 	qga-obj-y \
 	block-obj-y \
-	common-obj-y
+	block-obj-m \
+	common-obj-y \
+	common-obj-m
+
 dummy := $(call unnest-vars)
configure +3 -0
index 48e14b8..8f0a882 100755
@@ -190,6 +190,7 @@ mingw32="no"
 gcov="no"
 gcov_tool="gcov"
 EXESUF=""
+DSOSUF=".so"
 prefix="/usr/local"
 mandir="\${prefix}/share/man"
 datadir="\${prefix}/share"
@@ -580,6 +581,7 @@ fi
 
 if test "$mingw32" = "yes" ; then
   EXESUF=".exe"
+  DSOSUF=".dll"
   QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
   # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
   QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
@@ -4165,6 +4167,7 @@ echo "LIBTOOLFLAGS=$LIBTOOLFLAGS" >> $config_host_mak
 echo "LIBS+=$LIBS" >> $config_host_mak
 echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
+echo "DSOSUF=$DSOSUF" >> $config_host_mak
 echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
 echo "POD2MAN=$POD2MAN" >> $config_host_mak
 echo "TRANSLATE_OPT_CFLAGS=$TRANSLATE_OPT_CFLAGS" >> $config_host_mak
rules.mak +6 -0
index 8276421..090eee1 100644
@@ -58,6 +58,12 @@ endif
 %.o: %.dtrace
 	$(call quiet-command,dtrace -o $@ -G -s $<, "  GEN   $(TARGET_DIR)$@")
 
+%$(DSOSUF) %.mo.o: %.mo
+	$(call quiet-command,$(LD) $< -o $@ -shared,"  LD[M] $(TARGET_DIR)$@")
+
+%.mo:
+	$(call quiet-command,$(LD) $(sort $^) -r -o $@,"  LD    $(TARGET_DIR)$@")
+
 %$(EXESUF): %.o
 	$(call LINK,$^)
 
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Quoted 4 lines · Fam Zheng

1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS += -shared -fPIC". Then "linked" to %.mo, which is an incremental object with "ln -r". This step is for consistency with %.mod case and has no effect.

As a general rule, you should avoid ld -r unless you know exactly what that implies for the given machine. This is the sort of thing that's highly likely to work on x86 while failing elsewhere.

E.g. ARM and PPC ld would like to add trampolines for direct calls to reach the PLT entry. If you create one object that's too large then that's no longer possible.

E.g. MIPS and Alpha ld require that any one input object file reference less than 64K worth of got entries. Every separate object file can address its own 64K segment of the got. If you combine too many objectsyou could overflow the got subsegments.

If our modules are small enough, we may never see any of these, but...

If you really require a single input file to ld for pattern matching purposes, I highly recommend an ar file, and then use --whole-archive to force the entire contents of the .a to be included. This preserves the original translation-unit boundaries for ld.

r~

#7 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Il 05/09/2013 21:24, Richard Henderson ha scritto:

Quoted 19 lines · Fam Zheng
Quoted 4 lines

1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS += -shared -fPIC". Then "linked" to %.mo, which is an incremental object with "ln -r". This step is for consistency with %.mod case and has no effect.

As a general rule, you should avoid ld -r unless you know exactly what that implies for the given machine. This is the sort of thing that's highly likely to work on x86 while failing elsewhere.

E.g. ARM and PPC ld would like to add trampolines for direct calls to reach the PLT entry. If you create one object that's too large then that's no longer possible.

E.g. MIPS and Alpha ld require that any one input object file reference less than 64K worth of got entries. Every separate object file can address its own 64K segment of the got. If you combine too many objectsyou could overflow the got subsegments.

Quoted 6 lines

If our modules are small enough, we may never see any of these, but...

If you really require a single input file to ld for pattern matching purposes, I highly recommend an ar file, and then use --whole-archive to force the entire contents of the .a to be included. This preserves the original translation-unit boundaries for ld.

Yes, this is the reason why I suggested using libtool for this. It increases the cost of compilation by essentially doing it twice, on the other hand you can simply use .la convenience libraries and libtool will be able to link it transparently into either an executable or a shared library.

Libtool used to be really bad, but most performance problems have been solved.

Paolo

#8 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Quoted 2 lines · Paolo Bonzini

Libtool used to be really bad, but most performance problems have been solved.

How about the "it silently creates things in dot-directories, recompiles things at random times when it really shouldn't, and is an enormous insane shell script" problems? The positives would have to be really really strong before we let libtool into the codebase...

-- PMM

#9 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Il 05/09/2013 23:45, Peter Maydell ha scritto:

Quoted 9 lines · Paolo Bonzini
Quoted 2 lines

Libtool used to be really bad, but most performance problems have been solved.

How about the "it silently creates things in dot-directories, recompiles things at random times when it really shouldn't, and is an enormous insane shell script" problems? The positives would have to be really really strong before we let libtool into the codebase...

How about "we'd break compilation on your favorite OS"? :) Darwin ld doesn't support --whole-archive, it needs a different incantation. Relatively recent Solaris now has it, but Apple still holds the bastions.

Paolo

author
#10 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

In reply to Richard Henderson
Quoted 19 lines · Richard Henderson
Quoted 4 lines · Fam Zheng

1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS += -shared -fPIC". Then "linked" to %.mo, which is an incremental object with "ln -r". This step is for consistency with %.mod case and has no effect.

As a general rule, you should avoid ld -r unless you know exactly what that implies for the given machine. This is the sort of thing that's highly likely to work on x86 while failing elsewhere.

E.g. ARM and PPC ld would like to add trampolines for direct calls to reach the PLT entry. If you create one object that's too large then that's no longer possible.

E.g. MIPS and Alpha ld require that any one input object file reference less than 64K worth of got entries. Every separate object file can address its own 64K segment of the got. If you combine too many objectsyou could overflow the got subsegments.

Quoted 7 lines

If our modules are small enough, we may never see any of these, but...

If you really require a single input file to ld for pattern matching purposes, I highly recommend an ar file, and then use --whole-archive to force the entire contents of the .a to be included. This preserves the original translation-unit boundaries for ld.

OK. Thanks for pointing out. The problem with ar file is that --whole-archive needs to be done specificly to modules .a, other libs shouldn't be linked with this flag. I'll drop this step by changing my pattern matching approach in Makefile, and just compile and link without such intermediate object file.

Fam

#11 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Quoted 3 lines · Fam Zheng

OK. Thanks for pointing out. The problem with ar file is that --whole-archive needs to be done specificly to modules .a, other libs shouldn't be linked with this flag.

See also:

  --whole-archive foo.a --no-whole-archive

r~

author
#12 lore raw

Re: [Qemu-devel] [RFC PATCH 4/6] Makefile: introduce common-obj-m and block-obj-m for DSO

Quoted 8 lines · Richard Henderson
Quoted 3 lines · Fam Zheng

OK. Thanks for pointing out. The problem with ar file is that --whole-archive needs to be done specificly to modules .a, other libs shouldn't be linked with this flag.

See also:

  --whole-archive foo.a --no-whole-archive

Yes, this is a way to do it, but will complicates the link command, and may be more error prone. I expanded foo.mo to its contained .o for static linking in the next revision that's coming up.

Thanks,

Fam

author
#13 lore raw

RFC PATCH 5/6 module: load modules at start

In reply to Fam Zheng

Add module_load_all to load all DSO modules under:

    /usr/lib/qemu/block/
    /usr/lib/qemu/net/
    /usr/lib/qemu/ui/

when starting process.

Requires gmodule-2.0 from glib.

---
 configure             | 20 +++++++++++---------
 include/qemu/module.h |  2 ++
 qemu-img.c            |  2 ++
 qemu-io.c             |  1 +
 qemu-nbd.c            |  1 +
 scripts/create_config |  3 +++
 util/Makefile.objs    |  2 ++
 util/module.c         | 40 ++++++++++++++++++++++++++++++++++++++++
 vl.c                  |  1 +
 9 files changed, 63 insertions(+), 9 deletions(-)
configure +11 -9
index 8f0a882..975853e 100755
@@ -2238,15 +2238,17 @@ if test "$mingw32" = yes; then
 else
     glib_req_ver=2.12
 fi
-if $pkg_config --atleast-version=$glib_req_ver gthread-2.0 > /dev/null 2>&1
-then
-    glib_cflags=`$pkg_config --cflags gthread-2.0 2>/dev/null`
-    glib_libs=`$pkg_config --libs gthread-2.0 2>/dev/null`
-    LIBS="$glib_libs $LIBS"
-    libs_qga="$glib_libs $libs_qga"
-else
-    error_exit "glib-$glib_req_ver required to compile QEMU"
-fi
+for i in gthread-2.0 gmodule-2.0; do
+    if $pkg_config --atleast-version=$glib_req_ver $i > /dev/null 2>&1
+    then
+        glib_cflags=`$pkg_config --cflags $i 2>/dev/null`
+        glib_libs=`$pkg_config --libs $i 2>/dev/null`
+        LIBS="$glib_libs $LIBS"
+        libs_qga="$glib_libs $libs_qga"
+    else
+        error_exit "glib-$glib_req_ver required to compile QEMU"
+    fi
+done
 
 ##########################################
 # pixman support probe
include/qemu/module.h +2 -0
index c4ccd57..d3500be 100644
@@ -37,4 +37,6 @@ void register_module_init(void (*fn)(void), module_init_type type);
 
 void module_call_init(module_init_type type);
 
+void module_load_all(void);
+
 #endif
qemu-img.c +2 -0
index b9a848d..e761027 100644
@@ -34,6 +34,7 @@
 #include <getopt.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include "qemu/module.h"
 
 #ifdef _WIN32
 #include <windows.h>
@@ -2328,6 +2329,7 @@ int main(int argc, char **argv)
 
     error_set_progname(argv[0]);
 
+    module_load_all();
     qemu_init_main_loop();
     bdrv_init();
     if (argc < 2)
qemu-io.c +1 -0
index d54dc86..d02e8f5 100644
@@ -398,6 +398,7 @@ int main(int argc, char **argv)
         exit(1);
     }
 
+    module_load_all();
     qemu_init_main_loop();
     bdrv_init();
 
qemu-nbd.c +1 -0
index f044546..ecea543 100644
@@ -558,6 +558,7 @@ int main(int argc, char **argv)
         snprintf(sockpath, 128, SOCKET_PATH, basename(device));
     }
 
+    module_load_all();
     qemu_init_main_loop();
     bdrv_init();
     atexit(bdrv_close_all);
scripts/create_config +3 -0
index b1adbf5..3ecb789 100755
@@ -104,6 +104,9 @@ case $line in
     value=${line#*=}
     echo "#define $name $value"
     ;;
+ DSOSUF=*)
+    echo "#define HOST_DSOSUF \"${line#*=}\""
+    ;;
 esac
 
 done # read
util/Makefile.objs +2 -0
index dc72ab0..33e56b0 100644
@@ -11,3 +11,5 @@ util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o
 util-obj-y += qemu-option.o qemu-progress.o
 util-obj-y += hexdump.o
 util-obj-y += crc32c.o
+
+$(obj)/module.o-libs := -lglib
util/module.c +40 -0
index 7acc33d..29d3a4f 100644
@@ -13,6 +13,8 @@
  * GNU GPL, version 2 or (at your option) any later version.
  */
 
+#include <gmodule.h>
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/queue.h"
 #include "qemu/module.h"
@@ -79,3 +81,41 @@ void module_call_init(module_init_type type)
         e->init();
     }
 }
+
+void module_load_all(void)
+{
+    static const char *module_dir_list[] = {
+        "/usr/lib/qemu/block/",
+        "/usr/lib/qemu/net/",
+        "/usr/lib/qemu/ui/",
+        NULL
+    };
+    const char **path;
+    const char *dsosuf = HOST_DSOSUF;
+    char fname[1024];
+    int suf_len = strlen(dsosuf);
+    DIR *dp;
+    struct dirent *ep = NULL;
+    GModule *g_module;
+    for (path = &module_dir_list[0]; *path != NULL; path++) {
+        dp = opendir(*path);
+        if (!dp) {
+            fprintf(stderr, "Failed to open dir %s\n", *path);
+        }
+        for (ep = readdir(dp); ep; ep = readdir(dp)) {
+            int len = strlen(ep->d_name);
+            if (len > suf_len &&
+                !strcmp(&ep->d_name[len - suf_len], dsosuf)) {
+                pstrcpy(fname, sizeof(fname), *path);
+                pstrcat(fname, sizeof(fname), ep->d_name);
+                g_module = g_module_open(fname, G_MODULE_BIND_LAZY);
+                if (!g_module) {
+                    fprintf(stderr, "Failed to open module file %s\n",
+                            g_module_error());
+                    continue;
+                }
+                printf("Loaded module %s\n", fname);
+            }
+        }
+    }
+}
vl.c +1 -0
index dfbc071..c72e5a8 100644
@@ -3873,6 +3873,7 @@ int main(int argc, char **argv, char **envp)
     }
     loc_set_none();
 
+    module_load_all();
     if (qemu_init_main_loop()) {
         fprintf(stderr, "qemu_init_main_loop failed\n");
         exit(1);
Signature · 1 line
1.8.3.1
#14 lore raw

Re: [Qemu-devel] [RFC PATCH 5/6] module: load modules at start

Quoted 5 lines · Fam Zheng

Add module_load_all to load all DSO modules under:

    /usr/lib/qemu/block/
    /usr/lib/qemu/net/
    /usr/lib/qemu/ui/

when starting process.

This should probably be based on a define with the prefix set at configure time.

Adding directories from command-line arguments into that list would also be nice.

Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
author
#15 lore raw

Re: [Qemu-devel] [RFC PATCH 5/6] module: load modules at start

Quoted 10 lines · Lluís Vilanova
Quoted 5 lines · Fam Zheng

Add module_load_all to load all DSO modules under:

    /usr/lib/qemu/block/
    /usr/lib/qemu/net/
    /usr/lib/qemu/ui/

when starting process.

This should probably be based on a define with the prefix set at configure time.

Yes, will fix.

Quoted 3 lines

Adding directories from command-line arguments into that list would also be nice.

Of course, but I'd like to keep this series focusing on essential framework and leave it for follow-ups.

Thanks,

Fam

#16 lore raw

Re: [Qemu-devel] [RFC PATCH 5/6] module: load modules at start

In reply to Fam Zheng
Quoted 5 lines · Fam Zheng

Add module_load_all to load all DSO modules under:

    /usr/lib/qemu/block/
    /usr/lib/qemu/net/
    /usr/lib/qemu/ui/

when starting process.

NACK.

This is wrong, as has been mentioned already. For example, you can't expect to load ui/* from qemu-img, because qemu-img provides no symbols needed to link ui stuff.

Much better approach is to hook this stuff into object/module registration framework, in module.c. And either load every object of a given kind at the init time, or try to load it at lookup time if the requested object isn't found in current list.

I implemented this approach in my tree more than a month ago.

Thanks,

/mjt

author
#17 lore raw

RFC PATCH 6/6 curl: build as shared library

In reply to Fam Zheng

Produce block/curl.so with --enable-curl. "make install" is not installing it yet, manually copy it to /usr/lib/qemu/block/curl.so to make it loaded.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index 975853e..ac81a7c 100755
@@ -3878,7 +3878,7 @@ if test "$bswap_h" = "yes" ; then
   echo "CONFIG_MACHINE_BSWAP_H=y" >> $config_host_mak
 fi
 if test "$curl" = "yes" ; then
-  echo "CONFIG_CURL=y" >> $config_host_mak
+  echo "CONFIG_CURL=m" >> $config_host_mak
   echo "CURL_CFLAGS=$curl_cflags" >> $config_host_mak
   echo "CURL_LIBS=$curl_libs" >> $config_host_mak
 fi
Signature · 1 line
1.8.3.1
author
#18 lore raw
In reply to Fam Zheng
Quoted 8 lines · Fam Zheng

This series implements feature of shared object building as described in:

http://wiki.qemu.org/Features/Modules

It's achieved in three steps, with extra bonus to change curl to a shared library module in the end (only to demonstrate the usage, no "make install" support of .so files yet).

Quoted 8 lines

1. Allow per object cflags and libs:

[01/06] make.rule: fix $(obj) to a real relative path [02/06] rule.mak: allow per object cflags and libs

2. Rules for building .so:

[03/06] Makefile: define curl cflags and libs with object

Sorry, misleading. 04 should be this step, 03, 06 is for curl enablement.

Quoted 6 lines

3. Code to load module. All .so files are scanned and loaded when program starts:

[04/06] Makefile: introduce common-obj-m and block-obj-m for DSO

And this should be patch 05.

Quoted 5 lines

4. curl adoption:

[05/06] module: load modules at start
[06/06] curl: build as shared library