[RFC PATCH 2/2] Makefile: use xxd for converting guest/init

Andre Przywara
2015-06-23T15:06:12Z

Currently we use ld to convert the static guest/init binary back
into an object file, which we can embed as a binary blob into our
lkvm binary. This works fine as long as compiler and linker use the
same ELF target format, which seems to be not true for most of the
MIPS toolchains.
Use a different approach instead, which converts the guest/init
binary into a C array, from which the compiler generates an .o
representation. As the compiler is now the same, this naturally links
together fine on all architectures.
We use the "xxd" tool for generating a C array representation out of
the binary file. If this turns out to be not widely installed (it
seems to be part of the vim package in most distributions), we could
think about switching to a scripted implementation using "od" or some
printf trickery.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
Makefile | 4 ++--
builtin-run.c | 8 ++++----
builtin-setup.c | 8 ++++----
3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index b9480ff..7f2a0ea 100644
--- a/Makefile
+++ b/Makefile
@@ -365,8 +365,8 @@ $(PROGRAM_ALIAS): $(PROGRAM)
$(Q) ln -f $< $@

$(GUEST_OBJS): $(GUEST_INIT)
- $(E) " LINK " $@
- $(Q) $(LD) $(LDFLAGS) -r -b binary -o $@ $<
+ $(E) " CONVERT " $@
+ $(Q) xxd -i $< | $(CC) -c -x c - -o $@

$(GUEST_INIT): guest/init.c
$(E) " CC " $@
diff --git a/builtin-run.c b/builtin-run.c
index 1ee75ad..0a48663 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -59,8 +59,8 @@ static int kvm_run_wrapper;

bool do_debug_print = false;

-extern char _binary_guest_init_start;
-extern char _binary_guest_init_size;
+extern char guest_init;
+extern char guest_init_len;

static const char * const run_usage[] = {
"lkvm run [<options>] [<kernel image>]",
@@ -354,8 +354,8 @@ static int kvm_setup_guest_init(struct kvm *kvm)
char *data;

/* Setup /virt/init */
- size = (size_t)&_binary_guest_init_size;
- data = (char *)&_binary_guest_init_start;
+ size = (size_t)&guest_init_len;
+ data = (char *)&guest_init;
snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs);
remove(tmp);
fd = open(tmp, O_CREAT | O_WRONLY, 0755);
diff --git a/builtin-setup.c b/builtin-setup.c
index 8b45c56..fd7ca54 100644
--- a/builtin-setup.c
+++ b/builtin-setup.c
@@ -16,8 +16,8 @@
#include <sys/mman.h>
#include <fcntl.h>

-extern char _binary_guest_init_start;
-extern char _binary_guest_init_size;
+extern char guest_init;
+extern char guest_init_len;

static const char *instance_name;

@@ -131,8 +131,8 @@ static int copy_init(const char *guestfs_name)
int fd, ret;
char *data;

- size = (size_t)&_binary_guest_init_size;
- data = (char *)&_binary_guest_init_start;
+ size = (size_t)&guest_init;
+ data = (char *)&guest_init_len;
snprintf(path, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), guestfs_name);
remove(path);
fd = open(path, O_CREAT | O_WRONLY, 0755);
--
2.3.5