[CRIU] [PATCH 2/3] compel: add per-arch handle-elf.c

Dmitry Safonov dsafonov at virtuozzo.com
Fri Apr 29 12:47:54 PDT 2016


Split handle_elf() function from main.c to per-arch.
Rename it to handle_binary not to cross-reference.
Rename generic handle_elf to __handle_elf as with define
not to litter namespace.

Cc: Cyrill Gorcunov <gorcunov at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 compel/Makefile                          |  3 ++-
 compel/arch/aarch64/handle-elf.c         | 20 +++++++++++++++
 compel/arch/aarch64/include/handle-elf.h | 10 ++++++++
 compel/arch/arm/handle-elf.c             | 13 ++++++++++
 compel/arch/arm/include/handle-elf.h     | 10 ++++++++
 compel/arch/ppc64/handle-elf.c           | 20 +++++++++++++++
 compel/arch/ppc64/include/handle-elf.h   |  4 ++-
 compel/arch/x86/handle-elf.c             | 15 ++++++++++++
 compel/arch/x86/include/handle-elf.h     |  7 ++++--
 compel/handle-elf.c                      |  4 +--
 compel/include/piegen.h                  | 30 ++++++++++++++++-------
 compel/main.c                            | 42 +-------------------------------
 12 files changed, 121 insertions(+), 57 deletions(-)
 create mode 100644 compel/arch/aarch64/handle-elf.c
 create mode 100644 compel/arch/aarch64/include/handle-elf.h
 create mode 100644 compel/arch/arm/handle-elf.c
 create mode 100644 compel/arch/arm/include/handle-elf.h
 create mode 100644 compel/arch/ppc64/handle-elf.c
 create mode 100644 compel/arch/x86/handle-elf.c

diff --git a/compel/Makefile b/compel/Makefile
index acba4c3bcb3f..ce5c78363c2c 100644
--- a/compel/Makefile
+++ b/compel/Makefile
@@ -12,13 +12,14 @@ HOSTLDFLAGS	+= $(LDFLAGS)
 hostprogs-y	+= compel
 compel-objs	+= main.o
 compel-objs	+= handle-elf.o
+compel-objs	+= arch/$(ARCH)/handle-elf.o
 
 # Add $(DEFINES) to CFLAGS of compel-objs.
 # We can't do ccflags-y += $(DEFINES)
 # as we need to build handle-elf-32.o
 # with -DCONFIG_X86_32
 define ccflags-defines
-        HOSTCFLAGS_$(1) += $(DEFINES)
+        HOSTCFLAGS_$(notdir $(1)) += $(DEFINES)
 endef
 $(eval $(call map,ccflags-defines,$(compel-objs)))
 
diff --git a/compel/arch/aarch64/handle-elf.c b/compel/arch/aarch64/handle-elf.c
new file mode 100644
index 000000000000..82c282b5f85d
--- /dev/null
+++ b/compel/arch/aarch64/handle-elf.c
@@ -0,0 +1,20 @@
+#include <string.h>
+
+#include "piegen.h"
+#include "handle-elf.h"
+
+int handle_binary(void *mem, size_t size)
+{
+	const unsigned char *elf_ident =
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+		elf_ident_64_le;
+#else
+		elf_ident_64_be;
+#endif
+
+	if (memcmp(mem, elf_ident, sizeof(elf_ident_64_le)) == 0)
+		return handle_elf_aarch64(mem, size);
+
+	pr_err("Unsupported Elf format detected\n");
+	return -1;
+}
diff --git a/compel/arch/aarch64/include/handle-elf.h b/compel/arch/aarch64/include/handle-elf.h
new file mode 100644
index 000000000000..e9c5036f2a08
--- /dev/null
+++ b/compel/arch/aarch64/include/handle-elf.h
@@ -0,0 +1,10 @@
+#ifndef __COMPEL_HANDLE_ELF_H__
+#define __COMPEL_HANDLE_ELF_H__
+
+#include "uapi/elf64-types.h"
+
+#define __handle_elf	handle_elf_aarch64
+
+extern int handle_elf_aarch64(void *mem, size_t size);
+
+#endif /* __COMPEL_HANDLE_ELF_H__ */
diff --git a/compel/arch/arm/handle-elf.c b/compel/arch/arm/handle-elf.c
new file mode 100644
index 000000000000..580ed0e4fa15
--- /dev/null
+++ b/compel/arch/arm/handle-elf.c
@@ -0,0 +1,13 @@
+#include <string.h>
+
+#include "piegen.h"
+#include "handle-elf.h"
+
+int handle_binary(void *mem, size_t size)
+{
+	if (memcmp(mem, elf_ident_32, sizeof(elf_ident_32)) == 0)
+		return handle_elf_arm(mem, size);
+
+	pr_err("Unsupported Elf format detected\n");
+	return -1;
+}
diff --git a/compel/arch/arm/include/handle-elf.h b/compel/arch/arm/include/handle-elf.h
new file mode 100644
index 000000000000..b44ed9f6a839
--- /dev/null
+++ b/compel/arch/arm/include/handle-elf.h
@@ -0,0 +1,10 @@
+#ifndef __COMPEL_HANDLE_ELF_H__
+#define __COMPEL_HANDLE_ELF_H__
+
+#include "uapi/elf32-types.h"
+
+#define __handle_elf	handle_elf_arm
+
+extern int handle_elf_arm(void *mem, size_t size);
+
+#endif /* __COMPEL_HANDLE_ELF_H__ */
diff --git a/compel/arch/ppc64/handle-elf.c b/compel/arch/ppc64/handle-elf.c
new file mode 100644
index 000000000000..9433ef135345
--- /dev/null
+++ b/compel/arch/ppc64/handle-elf.c
@@ -0,0 +1,20 @@
+#include <string.h>
+
+#include "piegen.h"
+#include "handle-elf.h"
+
+int handle_binary(void *mem, size_t size)
+{
+	const unsigned char *elf_ident =
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+		elf_ident_64_le;
+#else
+		elf_ident_64_be;
+#endif
+
+	if (memcmp(mem, elf_ident, sizeof(elf_ident_64_le)) == 0)
+		return handle_elf_ppc64(mem, size);
+
+	pr_err("Unsupported Elf format detected\n");
+	return -1;
+}
diff --git a/compel/arch/ppc64/include/handle-elf.h b/compel/arch/ppc64/include/handle-elf.h
index 203e91b651bd..c6f23123d94d 100644
--- a/compel/arch/ppc64/include/handle-elf.h
+++ b/compel/arch/ppc64/include/handle-elf.h
@@ -4,6 +4,8 @@
 #include "uapi/elf32-types.h"
 
 #define ELF_PPC64
-#define handle_elf	handle_elf_ppc64
+#define __handle_elf	handle_elf_ppc64
+
+extern int handle_elf_ppc64(void *mem, size_t size);
 
 #endif /* __COMPEL_HANDLE_ELF_H__ */
diff --git a/compel/arch/x86/handle-elf.c b/compel/arch/x86/handle-elf.c
new file mode 100644
index 000000000000..5a142c9ef601
--- /dev/null
+++ b/compel/arch/x86/handle-elf.c
@@ -0,0 +1,15 @@
+#include <string.h>
+
+#include "piegen.h"
+#include "handle-elf.h"
+
+int handle_binary(void *mem, size_t size)
+{
+	if (memcmp(mem, elf_ident_32, sizeof(elf_ident_32)) == 0)
+		return handle_elf_x86_32(mem, size);
+	else if (memcmp(mem, elf_ident_64_le, sizeof(elf_ident_64_le)) == 0)
+		return handle_elf_x86_64(mem, size);
+
+	pr_err("Unsupported Elf format detected\n");
+	return -1;
+}
diff --git a/compel/arch/x86/include/handle-elf.h b/compel/arch/x86/include/handle-elf.h
index 75e3c3974771..e02098dbe385 100644
--- a/compel/arch/x86/include/handle-elf.h
+++ b/compel/arch/x86/include/handle-elf.h
@@ -5,14 +5,17 @@
 
 #include "uapi/elf32-types.h"
 #define ELF_X86_32
-#define handle_elf	handle_elf_x86_32
+#define __handle_elf	handle_elf_x86_32
 
 #else /* CONFIG_X86_64 */
 
 #include "uapi/elf64-types.h"
 #define ELF_X86_64
-#define handle_elf	handle_elf_x86_64
+#define __handle_elf	handle_elf_x86_64
 
 #endif
 
+extern int handle_elf_x86_32(void *mem, size_t size);
+extern int handle_elf_x86_64(void *mem, size_t size);
+
 #endif /* __COMPEL_HANDLE_ELF_H__ */
diff --git a/compel/handle-elf.c b/compel/handle-elf.c
index 11f28ab5f18c..70a6d376ef7e 100644
--- a/compel/handle-elf.c
+++ b/compel/handle-elf.c
@@ -6,7 +6,6 @@
 #include <string.h>
 
 #include <fcntl.h>
-#include <elf.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -14,7 +13,6 @@
 
 #include "asm-generic/int.h"
 
-#include "compiler.h"
 #include "piegen.h"
 #include "handle-elf.h"
 
@@ -63,7 +61,7 @@ static int do_relative_toc(long value, uint16_t *location,
 }
 #endif
 
-int handle_elf(void *mem, size_t size)
+int __handle_elf(void *mem, size_t size)
 {
 	const char *symstrings = NULL;
 	Shdr_t *symtab_hdr = NULL;
diff --git a/compel/include/piegen.h b/compel/include/piegen.h
index 0c695c6a7b48..da6ff219b7fe 100644
--- a/compel/include/piegen.h
+++ b/compel/include/piegen.h
@@ -4,6 +4,9 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include <elf.h>
+#include "compiler.h"
+
 typedef struct {
 	char		*input_filename;
 	char		*output_filename;
@@ -17,15 +20,6 @@ typedef struct {
 extern piegen_opt_t opts;
 extern FILE *fout;
 
-#if defined(CONFIG_X86_32) || defined(CONFIG_X86_64)
-extern int handle_elf_x86_32(void *mem, size_t size);
-extern int handle_elf_x86_64(void *mem, size_t size);
-#endif
-
-#if defined(CONFIG_PPC64)
-extern int handle_elf_ppc64(void *mem, size_t size);
-#endif
-
 #define pr_out(fmt, ...)	fprintf(fout, fmt, ##__VA_ARGS__)
 
 #define pr_debug(fmt, ...)	printf("%s: "fmt, opts.stream_name, ##__VA_ARGS__)
@@ -33,4 +27,22 @@ extern int handle_elf_ppc64(void *mem, size_t size);
 #define pr_err(fmt, ...)	fprintf(stderr, "%s: Error (%s:%d): "fmt, opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__)
 #define pr_perror(fmt, ...)	fprintf(stderr, "%s: Error (%s:%d): "fmt ": %m\n", opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__)
 
+extern int handle_binary(void *mem, size_t size);
+
+static const unsigned char __maybe_unused
+elf_ident_32[EI_NIDENT] = {
+	0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+static const unsigned char __maybe_unused
+elf_ident_64_le[EI_NIDENT] = {
+	0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+static const unsigned char __maybe_unused
+elf_ident_64_be[EI_NIDENT] = {
+	0x7f, 0x45, 0x4c, 0x46, 0x02, 0x02, 0x01, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+
 #endif /* __ELFTIL_H__ */
diff --git a/compel/main.c b/compel/main.c
index ead50be1facb..5fe30c31648d 100644
--- a/compel/main.c
+++ b/compel/main.c
@@ -7,13 +7,11 @@
 #include <string.h>
 
 #include <fcntl.h>
-#include <elf.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include "compiler.h"
 #include "piegen.h"
 
 static const char compel_cflags_pie[] = "-fpie -Wa,--noexecstack -fno-stack-protector";
@@ -31,44 +29,6 @@ piegen_opt_t opts = {
 
 FILE *fout;
 
-static int handle_elf(void *mem, size_t size)
-{
-#if defined(CONFIG_X86_32) || defined(CONFIG_X86_64)
-	unsigned char elf_ident_x86_32[EI_NIDENT] = {
-		0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	};
-
-	unsigned char elf_ident_x86_64[EI_NIDENT] = {
-		0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	};
-
-	if (memcmp(mem, elf_ident_x86_32, sizeof(elf_ident_x86_32)) == 0)
-		return handle_elf_x86_32(mem, size);
-	else if (memcmp(mem, elf_ident_x86_64, sizeof(elf_ident_x86_64)) == 0)
-		return handle_elf_x86_64(mem, size);
-#endif
-
-#if defined(CONFIG_PPC64)
-	const unsigned char elf_ident[EI_NIDENT] = {
-#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-                0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-#else
-		0x7f, 0x45, 0x4c, 0x46, 0x02, 0x02, 0x01, 0x00,
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-#endif
-	};
-
-	if (memcmp(mem, elf_ident, sizeof(elf_ident)) == 0)
-		return handle_elf_ppc64(mem, size);
-#endif /* CONFIG_PPC64 */
-
-	pr_err("Unsupported Elf format detected\n");
-	return -1;
-}
-
 static int piegen(void)
 {
 	struct stat st;
@@ -98,7 +58,7 @@ static int piegen(void)
 		goto err;
 	}
 
-	if (handle_elf(mem, st.st_size)) {
+	if (handle_binary(mem, st.st_size)) {
 		close(fd), fd = -1;
 		unlink(opts.output_filename);
 		goto err;
-- 
2.8.0



More information about the CRIU mailing list