[CRIU] [PATCH 1/3] compel: piegen -- Add ability to write patched binary

Cyrill Gorcunov gorcunov at openvz.org
Tue Sep 27 14:17:21 PDT 2016


Suitable for debug.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 compel/include/piegen.h     |  1 +
 compel/src/lib/handle-elf.c | 12 ++++++++++++
 compel/src/main.c           |  6 +++++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/compel/include/piegen.h b/compel/include/piegen.h
index f1ed2e13d309..721d404cf9a6 100644
--- a/compel/include/piegen.h
+++ b/compel/include/piegen.h
@@ -10,6 +10,7 @@
 typedef struct {
 	char		*input_filename;
 	char		*output_filename;
+	char		*output_patched_filename;	/* for debug only */
 	char		*uapi_dir;
 	char		*stream_name;
 	char		*prefix_name;
diff --git a/compel/src/lib/handle-elf.c b/compel/src/lib/handle-elf.c
index b7e540ed78e0..1ddd5715396d 100644
--- a/compel/src/lib/handle-elf.c
+++ b/compel/src/lib/handle-elf.c
@@ -577,6 +577,18 @@ int __handle_elf(void *mem, size_t size)
 	}
 	pr_out("};\n");
 	free(sec_hdrs);
+
+	if (opts.output_patched_filename) {
+		int patched_fd;
+
+		patched_fd = open(opts.output_patched_filename,
+				  O_RDWR | O_CREAT | O_TRUNC, 0644);
+		if (patched_fd >= 0) {
+			write(patched_fd, mem, size);
+			close(patched_fd);
+		}
+	}
+
 	return 0;
 err:
 	free(sec_hdrs);
diff --git a/compel/src/main.c b/compel/src/main.c
index ee6ccd53acdd..6b5678d58c35 100644
--- a/compel/src/main.c
+++ b/compel/src/main.c
@@ -109,11 +109,12 @@ int main(int argc, char *argv[])
 		},
 	};
 
-	static const char short_opts[] = "a:f:o:s:p:v:r:u:h";
+	static const char short_opts[] = "a:f:o:g:s:p:v:r:u:h";
 	static struct option long_opts[] = {
 		{ "arch",	required_argument,	0, 'a' },
 		{ "file",	required_argument,	0, 'f' },
 		{ "output",	required_argument,	0, 'o' },
+		{ "patched",	required_argument,	0, 'g' },
 		{ "stream",	required_argument,	0, 's' },
 		{ "uapi-dir",	required_argument,	0, 'u' },
 		{ "sym-prefix",	required_argument,	0, 'p' },
@@ -149,6 +150,9 @@ int main(int argc, char *argv[])
 		case 'o':
 			opts.output_filename = optarg;
 			break;
+		case 'g':
+			opts.output_patched_filename = optarg;
+			break;
 		case 'u':
 			opts.uapi_dir = optarg;
 			break;
-- 
2.7.4



More information about the CRIU mailing list