summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Graf <agraf@suse.de>2011-09-30 19:40:36 +0200
committerhyokeun <hyokeun.jeon@samsung.com>2016-09-06 15:55:31 +0900
commitef7a95c9cdb51be8ddaf190e22c269cd07eb432d (patch)
treec9a71cc631d11cf70641a3ae32d23ff6820a562f
parent5248e7a8a6e58a4cd1f451210a557bfe48da0695 (diff)
downloadqemu-ef7a95c9cdb51be8ddaf190e22c269cd07eb432d.tar.gz
qemu-ef7a95c9cdb51be8ddaf190e22c269cd07eb432d.tar.bz2
qemu-ef7a95c9cdb51be8ddaf190e22c269cd07eb432d.zip
linux-user: add binfmt wrapper for argv[0] handling
When using qemu's linux-user binaries through binfmt, argv[0] gets lost along the execution because qemu only gets passed in the full file name to the executable while argv[0] can be something completely different. This breaks in some subtile situations, such as the grep and make test suites. This patch adds a wrapper binary called qemu-$TARGET-binfmt that can be used with binfmt's P flag which passes the full path _and_ argv[0] to the binfmt handler. The binary would be smart enough to be versatile and only exist in the system once, creating the qemu binary path names from its own argv[0]. However, this seemed like it didn't fit the make system too well, so we're currently creating a new binary for each target archictecture. CC: Reinhard Max <max@suse.de> Signed-off-by: Alexander Graf <agraf@suse.de> [AF: Rebased onto new Makefile infrastructure, twice] [AF: Updated for aarch64 for v2.0.0-rc1] [AF: Rebased onto Makefile changes for v2.1.0-rc0] [AF: Rebased onto script rewrite for v2.7.0-rc2 - to be fixed] Signed-off-by: Andreas Färber <afaerber@suse.de>
-rw-r--r--Makefile.target13
-rw-r--r--linux-user/Makefile.objs2
-rw-r--r--linux-user/binfmt.c42
3 files changed, 57 insertions, 0 deletions
diff --git a/Makefile.target b/Makefile.target
index a440bcb5b..a65c55f37 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -36,6 +36,10 @@ endif
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
STPFILES=
+ifdef CONFIG_LINUX_USER
+PROGS+=$(QEMU_PROG)-binfmt
+endif
+
config-target.h: config-target.h-timestamp
config-target.h-timestamp: config-target.mak
@@ -115,6 +119,8 @@ QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) \
obj-y += linux-user/
obj-y += gdbstub.o thunk.o user-exec.o
+obj-binfmt-y += linux-user/
+
endif #CONFIG_LINUX_USER
#########################################################
@@ -163,7 +169,11 @@ endif # CONFIG_SOFTMMU
# Workaround for http://gcc.gnu.org/PR55489, see configure.
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
+ifdef CONFIG_LINUX_USER
+dummy := $(call unnest-vars,,obj-y obj-binfmt-y)
+else
dummy := $(call unnest-vars,,obj-y)
+endif
all-obj-y := $(obj-y)
target-obj-y :=
@@ -200,6 +210,9 @@ ifdef CONFIG_DARWIN
$(call quiet-command,SetFile -a C $@," SETFILE $(TARGET_DIR)$@")
endif
+$(QEMU_PROG)-binfmt: $(obj-binfmt-y)
+ $(call LINK,$^)
+
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES)," GEN $(TARGET_DIR)$@")
diff --git a/linux-user/Makefile.objs b/linux-user/Makefile.objs
index 8c9305810..607ca2dc9 100644
--- a/linux-user/Makefile.objs
+++ b/linux-user/Makefile.objs
@@ -6,3 +6,5 @@ obj-$(TARGET_HAS_BFLT) += flatload.o
obj-$(TARGET_I386) += vm86.o
obj-$(TARGET_ARM) += arm/nwfpe/
obj-$(TARGET_M68K) += m68k-sim.o
+
+obj-binfmt-y = binfmt.o
diff --git a/linux-user/binfmt.c b/linux-user/binfmt.c
new file mode 100644
index 000000000..cd1f513b3
--- /dev/null
+++ b/linux-user/binfmt.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+int main(int argc, char **argv, char **envp)
+{
+ char *binfmt;
+ char **new_argv;
+
+ /*
+ * Check if our file name ends with -binfmt
+ */
+ binfmt = argv[0] + strlen(argv[0]) - strlen("-binfmt");
+ if (strcmp(binfmt, "-binfmt")) {
+ fprintf(stderr, "%s: Invalid executable name\n", argv[0]);
+ exit(1);
+ }
+ if (argc < 3) {
+ fprintf(stderr, "%s: Please use me through binfmt with P flag\n",
+ argv[0]);
+ exit(1);
+ }
+
+ binfmt[0] = '\0';
+ /* Now argv[0] is the real qemu binary name */
+
+ new_argv = (char **)malloc((argc + 2) * sizeof(*new_argv));
+ if (argc > 3) {
+ memcpy(&new_argv[4], &argv[3], (argc - 3) * sizeof(*new_argv));
+ }
+ new_argv[0] = argv[0];
+ new_argv[1] = (char *)"-0";
+ new_argv[2] = argv[2];
+ new_argv[3] = argv[1];
+ new_argv[argc + 1] = NULL;
+
+ return execve(new_argv[0], new_argv, envp);
+}