[PATCH] kvmtool: builtin-setup: fix embedded ELF binaries size

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



lkvm embeds 2 ELF binaries (guest/pre_init and guest/init) which are written to
the disk during the setup of new virtual machines. The binaries size is
gathered thanks to a symbol generated by ld during the link step
(_binary_%s_size). Unfortunately, the code generated by the compiler doesn't
always match our expectation because the symbol is relocated.

This commit computes the binaries size in a more reliable way thanks to the
subtraction of the end and start addresses of the binaries.
---
 builtin-setup.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/builtin-setup.c b/builtin-setup.c
index 8be8d62..7ecaf04 100644
--- a/builtin-setup.c
+++ b/builtin-setup.c
@@ -123,7 +123,7 @@ static const char *guestfs_symlinks[] = {
 
 #ifdef CONFIG_GUEST_INIT
 static int extract_file(const char *guestfs_name, const char *filename,
-			const void *data, const void *_size)
+			const void *data, const size_t size)
 {
 	char path[PATH_MAX];
 	int fd, ret;
@@ -138,7 +138,7 @@ static int extract_file(const char *guestfs_name, const char *filename,
 		die("Fail to setup %s", path);
 	}
 
-	ret = xwrite(fd, data, (size_t)_size);
+	ret = xwrite(fd, data, size);
 	if (ret < 0)
 		die("Fail to setup %s", path);
 	close(fd);
@@ -147,24 +147,27 @@ static int extract_file(const char *guestfs_name, const char *filename,
 }
 
 extern char _binary_guest_init_start;
-extern char _binary_guest_init_size;
+extern char _binary_guest_init_end;
 extern char _binary_guest_pre_init_start;
-extern char _binary_guest_pre_init_size;
+extern char _binary_guest_pre_init_end;
 
 int kvm_setup_guest_init(const char *guestfs_name)
 {
+	ptrdiff_t size;
 	int err;
 
 #ifdef CONFIG_GUEST_PRE_INIT
+	size = &_binary_guest_pre_init_end - &_binary_guest_pre_init_start;
 	err = extract_file(guestfs_name, "virt/pre_init",
 				&_binary_guest_pre_init_start,
-				&_binary_guest_pre_init_size);
+				size);
 	if (err)
 		return err;
 #endif
+	size = &_binary_guest_init_end - &_binary_guest_init_start;
 	err = extract_file(guestfs_name, "virt/init",
 				&_binary_guest_init_start,
-				&_binary_guest_init_size);
+				size);
 	return err;
 }
 #else
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux