Add the qemu-arm-binfmt (qemu-arm64-binfmt)
authorshuai.fu <shuai01.fu@samsung.com>
Mon, 5 Sep 2016 06:35:50 +0000 (14:35 +0800)
committerSoonKyu Park <sk7.park@samsung.com>
Wed, 28 Dec 2016 13:21:24 +0000 (22:21 +0900)
Change-Id: Iee417992cebbba47c5155aaf1f4a47de77ec5a72
Signed-off-by: shuai.fu <shuai01.fu@samsung.com>
Makefile.target
linux-user/binfmt.c [new file with mode: 0644]
packaging/qemu-arm-static.spec

index 9a4985213bc91acf46e384c2c2cc2dccf3eadaad..a2319e124161feb7c0ba306191876e094a0cba3b 100644 (file)
@@ -31,6 +31,10 @@ PROGS+=$(QEMU_PROGW)
 endif
 STPFILES=
 
+ifdef CONFIG_LINUX_USER
+PROGS+=$(QEMU_PROG)-binfmt
+endif
+
 config-target.h: config-target.h-timestamp
 config-target.h-timestamp: config-target.mak
 
@@ -166,6 +170,12 @@ $(QEMU_PROG): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
        $(call LINK,$^)
 endif
 
+$(QEMU_PROG)-binfmt:
+       #pwd
+       #$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o ./linux-user/$@.o $(BUILD_DIR)/linux-user/binfmt.c
+       $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(CFLAGS) $($@-cflags) -c -o ./linux-user/$@.o $(BUILD_DIR)/linux-user/binfmt.c
+       $(call LINK,./linux-user/$@.o)
+
 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/binfmt.c b/linux-user/binfmt.c
new file mode 100644 (file)
index 0000000..d55941a
--- /dev/null
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef __x86_64__
+#define ARCH_NAME "x86_64"
+#elif defined __i386__
+#define ARCH_NAME "i586"
+#endif
+
+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';
+    strcpy(binfmt, "-static");
+    /* Now argv[0] is the real qemu binary name */
+
+#ifdef ARCH_NAME
+    {
+        char *hostbin;
+        char *guestarch;
+        int r;
+
+        guestarch = strrchr(argv[0], '-') ;
+        if (!guestarch) {
+            goto skip;
+        }
+        guestarch++;
+        r = asprintf(&hostbin, "/emul/" ARCH_NAME "-for-%s/%s", guestarch, argv[1]);
+        if (!access(hostbin, X_OK) && (r > 0)) {
+            /*
+             * We found a host binary replacement for the non-host binary. Let's
+             * use that instead!
+             */
+            return execve(hostbin, &argv[2], envp);
+        }
+    }
+skip:
+#endif
+
+    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);
+}
index 4e265f0d6204246b615bcd5e9359dfae5e528804..ed2e4881d998e5eec79fd5aee0a10e2eaa53466c 100644 (file)
@@ -105,6 +105,8 @@ rm -rf ${RPM_BUILD_ROOT}
 %defattr(-, root, root)
 %{_bindir}/qemu-arm-static
 %{_bindir}/qemu-arm64-static
+%{_bindir}/qemu-arm-binfmt
+%{_bindir}/qemu-arm64-binfmt
 %{_sbindir}/*
 
 %changelog