A program might rely on functions implemented in vl.c, but implement its own main(). By placing main into a separate source file, there are no complaints about duplicate main()s when linking against vl.o. For example, the virtual-device fuzzer uses a main() provided by libfuzzer, and needs to perform some initialization before running the softmmu initialization. Now, main simply calls three vl.c functions which handle the guest initialization, main loop and cleanup.
Signed-off-by: Alexander Oleinik <alx...@bu.edu> --- Makefile | 1 + Makefile.objs | 2 ++ include/sysemu/sysemu.h | 4 ++++ main.c | 29 +++++++++++++++++++++++++++++ vl.c | 25 +++++++------------------ 5 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 main.c diff --git a/Makefile b/Makefile index b3528617e4..f628783571 100644 --- a/Makefile +++ b/Makefile @@ -463,6 +463,7 @@ $(SOFTMMU_ALL_RULES): $(crypto-obj-y) $(SOFTMMU_ALL_RULES): $(io-obj-y) $(SOFTMMU_ALL_RULES): config-all-devices.mak $(SOFTMMU_ALL_RULES): $(edk2-decompressed) +$(SOFTMMU_ALL_RULES): $(softmmu-main-y) .PHONY: $(TARGET_DIRS_RULES) # The $(TARGET_DIRS_RULES) are of the form SUBDIR/GOAL, so that diff --git a/Makefile.objs b/Makefile.objs index 6a143dcd57..bb1cfa05ef 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -86,6 +86,8 @@ common-obj-$(CONFIG_FDT) += device_tree.o # qapi common-obj-y += qapi/ + +softmmu-main-y = main.o endif ####################################################################### diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h index 44f18eb739..aa204ebbb0 100644 --- a/include/sysemu/sysemu.h +++ b/include/sysemu/sysemu.h @@ -114,6 +114,10 @@ QemuOpts *qemu_get_machine_opts(void); bool defaults_enabled(void); +void main_loop(void); +int qemu_init(int argc, char **argv, char **envp); +void qemu_cleanup(void); + extern QemuOptsList qemu_legacy_drive_opts; extern QemuOptsList qemu_common_drive_opts; extern QemuOptsList qemu_drive_opts; diff --git a/main.c b/main.c new file mode 100644 index 0000000000..279275069d --- /dev/null +++ b/main.c @@ -0,0 +1,29 @@ +#include "qemu/osdep.h" +#include "sysemu/sysemu.h" + +#ifdef CONFIG_SDL +#if defined(__APPLE__) || defined(main) +#include <SDL.h> +int qemu_main(int argc, char **argv, char **envp); +int main(int argc, char **argv) +{ + return qemu_main(argc, argv, NULL); +} +#undef main +#define main qemu_main +#endif +#endif /* CONFIG_SDL */ + +int main(int argc, char **argv, char **envp) +{ + int ret = qemu_init(argc, argv, envp); + if (ret != 0) { + return ret; + } + + main_loop(); + + qemu_cleanup(); + + return 0; +} diff --git a/vl.c b/vl.c index 630f5c5e9c..327510c81f 100644 --- a/vl.c +++ b/vl.c @@ -36,18 +36,6 @@ #include "sysemu/seccomp.h" #include "sysemu/tcg.h" -#ifdef CONFIG_SDL -#if defined(__APPLE__) || defined(main) -#include <SDL.h> -int qemu_main(int argc, char **argv, char **envp); -int main(int argc, char **argv) -{ - return qemu_main(argc, argv, NULL); -} -#undef main -#define main qemu_main -#endif -#endif /* CONFIG_SDL */ #ifdef CONFIG_COCOA #undef main @@ -1794,7 +1782,7 @@ static bool main_loop_should_exit(void) return false; } -static void main_loop(void) +void main_loop(void) { #ifdef CONFIG_PROFILER int64_t ti; @@ -2869,7 +2857,7 @@ static void user_register_global_props(void) global_init_func, NULL, NULL); } -int main(int argc, char **argv, char **envp) +int qemu_init(int argc, char **argv, char **envp) { int i; int snapshot, linux_boot; @@ -4468,7 +4456,7 @@ int main(int argc, char **argv, char **envp) if (vmstate_dump_file) { /* dump and exit */ dump_vmstate_json_to_file(vmstate_dump_file); - return 0; + exit(0); } if (incoming) { @@ -4485,8 +4473,11 @@ int main(int argc, char **argv, char **envp) accel_setup_post(current_machine); os_setup_post(); - main_loop(); + return 0; +} +void qemu_cleanup() +{ gdbserver_cleanup(); /* @@ -4522,6 +4513,4 @@ int main(int argc, char **argv, char **envp) qemu_chr_cleanup(); user_creatable_cleanup(); /* TODO: unref root container, check all devices are ok */ - - return 0; } -- 2.23.0