[CRIU] [PATCH 3/6] parasite: Build offsets from already linked elf file

Cyrill Gorcunov gorcunov at openvz.org
Tue Feb 14 08:41:58 EST 2012


Offsets to parasite should be built from linked elf
file otherwise some offsets might be wrong.

Moreover, to simplify parasite building procedure do not
compile util-net.c twice, simply make recv_fd to be static
function in header. Having two object files linked into
injectable code is not what we need here.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile           |   16 +++++++++-------
 include/util-net.h |   39 +++++++++++++++++++++++++++++++++++++--
 parasite.lds.S     |   13 ++++++++++++-
 util-net.c         |   36 +-----------------------------------
 4 files changed, 59 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile
index adfc7ea..ad775db 100644
--- a/Makefile
+++ b/Makefile
@@ -49,6 +49,7 @@ SRCS-BLOB	+= $(patsubst %.o,%.c,$(OBJS-BLOB))
 
 HEAD-BLOB-GEN	:= $(patsubst %.o,%-blob.h,$(OBJS-BLOB))
 HEAD-BIN	:= $(patsubst %.o,%.bin,$(OBJS-BLOB))
+HEAD-ELF	:= $(patsubst %.o,%.elf,$(OBJS-BLOB))
 HEAD-LDS	:= $(patsubst %.o,%.lds.S,$(OBJS-BLOB))
 
 ROBJS-BLOB	:= restorer.o
@@ -77,21 +78,21 @@ $(OBJS-BLOB): $(SRCS-BLOB)
 	$(E) "  CC      " $@
 	$(Q) $(CC) -c $(CFLAGS) -fpic $< -o $@
 
-parasite-util-net.o: util-net.c
-	$(E) "  CC      " $@
-	$(Q) $(CC) -c $(CFLAGS) -fpic $< -o $@
+$(HEAD-ELF): $(HEAD-LDS) $(OBJS-BLOB)
+	$(E) "  GEN     " $@
+	$(Q) $(LD) --oformat "elf64-x86-64" -T $(HEAD-LDS) $(OBJS-BLOB) -o $@
 
-$(HEAD-BIN): $(HEAD-LDS) $(OBJS-BLOB) parasite-util-net.o
+$(HEAD-BIN): $(HEAD-LDS) $(HEAD-ELF) $(OBJS-BLOB)
 	$(E) "  GEN     " $@
-	$(Q) $(LD) -T $^ -o $@
+	$(Q) $(LD) --oformat "binary" -T $(HEAD-LDS) $(OBJS-BLOB) -o $@
 
-$(HEAD-BLOB-GEN): $(HEAD-BIN)
+$(HEAD-BLOB-GEN): $(HEAD-BIN) $(HEAD-ELF) $(HEAD-LDS) $(OBJS-BLOB)
 	$(E) "  GEN     " $@
 	$(Q) $(SH) gen-offsets.sh			\
 		parasite_h__				\
 		parasite_blob_offset__			\
 		parasite_blob				\
-		$(OBJS-BLOB)				\
+		$(HEAD-ELF)				\
 		$(HEAD-BIN) > parasite-blob.h
 	$(Q) sync
 
@@ -160,6 +161,7 @@ clean:
 	$(Q) $(RM) -f ./*.img
 	$(Q) $(RM) -f ./*.out
 	$(Q) $(RM) -f ./*.bin
+	$(Q) $(RM) -f ./*.elf
 	$(Q) $(RM) -f ./$(PROGRAM)
 	$(Q) $(RM) -f ./$(HEAD-BLOB-GEN)
 	$(Q) $(RM) -f ./$(RHEAD-BLOB-GEN)
diff --git a/include/util-net.h b/include/util-net.h
index dbcad61..28b0926 100644
--- a/include/util-net.h
+++ b/include/util-net.h
@@ -1,9 +1,44 @@
 #ifndef UTIL_NET_H_
 #define UTIL_NET_H_
 
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "syscall.h"
+
 #define UNIX_PATH_MAX (sizeof(struct sockaddr_un) - \
 			(size_t)((struct sockaddr_un *) 0)->sun_path)
 
 extern int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd);
-extern int recv_fd(int sock);
-#endif
+
+static int recv_fd(int sock)
+{
+	char ccmsg[CMSG_SPACE(sizeof(int))];
+	struct msghdr msg = { };
+	struct iovec iov = { };
+	struct cmsghdr *cmsg;
+	int *cmsg_data;
+	char buf[1];
+	int ret;
+
+	iov.iov_base	= buf;
+	iov.iov_len	= 1;
+
+	msg.msg_iov	= &iov;
+	msg.msg_iovlen	= 1;
+	msg.msg_control	= ccmsg;
+	msg.msg_controllen = sizeof(ccmsg);
+
+	ret = sys_recvmsg(sock, &msg, 0);
+	if (ret < 0)
+		return ret;
+
+	cmsg = CMSG_FIRSTHDR(&msg);
+	if (!cmsg || !cmsg->cmsg_type == SCM_RIGHTS)
+		return -2;
+
+	cmsg_data = (int *)CMSG_DATA(cmsg);
+	return *cmsg_data;
+}
+
+#endif /* UTIL_NET_H_ */
diff --git a/parasite.lds.S b/parasite.lds.S
index 0f3aa32..55b5b78 100644
--- a/parasite.lds.S
+++ b/parasite.lds.S
@@ -1,6 +1,9 @@
-OUTPUT_FORMAT("binary")
 OUTPUT_ARCH(i386:x86-64)
 
+/*
+ * Target should be provided by option
+ */
+
 SECTIONS
 {
 	. = 0;
@@ -10,10 +13,18 @@ SECTIONS
 		. = ALIGN(8);
 	}
 	.data : {
+		*(.parasite.data)
 		*(.data)
 		*(.rodata)
+		*(.rodata.*)
 		*(.bss)
 		*(.parasite.stack)
 		. = ALIGN(8);
 	}
+
+	/* Sections to be discarded */
+	/DISCARD/ : {
+		*(.eh_frame)
+		*(.comment)
+	}
 }
diff --git a/util-net.c b/util-net.c
index dad02c4..f110e09 100644
--- a/util-net.c
+++ b/util-net.c
@@ -1,7 +1,4 @@
-#include <sys/socket.h>
-#include <sys/un.h>
-
-#include "syscall.h"
+#include "util-net.h"
 
 int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
 {
@@ -33,34 +30,3 @@ int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
 
 	return sys_sendmsg(sock, &hdr, 0);
 }
-
-int recv_fd(int sock)
-{
-	char ccmsg[CMSG_SPACE(sizeof(int))];
-	struct msghdr msg = { };
-	struct iovec iov = { };
-	struct cmsghdr *cmsg;
-	int *cmsg_data;
-	char buf[1];
-	int ret;
-
-	iov.iov_base	= buf;
-	iov.iov_len	= 1;
-
-	msg.msg_iov	= &iov;
-	msg.msg_iovlen	= 1;
-	msg.msg_control	= ccmsg;
-	msg.msg_controllen = sizeof(ccmsg);
-
-	ret = sys_recvmsg(sock, &msg, 0);
-	if (ret < 0)
-		return ret;
-
-	cmsg = CMSG_FIRSTHDR(&msg);
-	if (!cmsg || !cmsg->cmsg_type == SCM_RIGHTS)
-		return -2;
-
-	cmsg_data = (int *)CMSG_DATA(cmsg);
-	return *cmsg_data;
-}
-
-- 
1.7.7.6



More information about the CRIU mailing list