diff --git a/Makefile b/Makefile index 6176f2349..1c8702beb 100644 --- a/Makefile +++ b/Makefile @@ -201,12 +201,9 @@ $(eval $(call gen-built-in,images)) .PHONY: .FORCE +# # Compel get used by CRIU, build it earlier -compel/%: .FORCE - $(Q) $(MAKE) $(build)=compel $@ - -test/compel/%: .FORCE - $(Q) $(MAKE) $(build)=compel $@ +include Makefile.compel # # Next the socket CR library @@ -269,6 +266,7 @@ mrproper: subclean $(Q) $(RM) $(CONFIG_HEADER) $(Q) $(RM) $(SOCCR_CONFIG) $(Q) $(RM) $(VERSION_HEADER) + $(Q) $(RM) $(COMPEL_VERSION_HEADER) $(Q) $(RM) include/common/asm $(Q) $(RM) cscope.* $(Q) $(RM) tags TAGS diff --git a/Makefile.compel b/Makefile.compel new file mode 100644 index 000000000..dd60defbf --- /dev/null +++ b/Makefile.compel @@ -0,0 +1,35 @@ +COMPEL_VERSION_HEADER := compel/include/version.h + +$(COMPEL_VERSION_HEADER): $(SRC_DIR)/Makefile.versions + $(call msg-gen, $(COMPEL_VERSION_HEADER)) + $(E) " GEN " $@ + $(Q) echo "/* Autogenerated, do not edit */" > $(COMPEL_VERSION_HEADER) + $(Q) echo "#ifndef COMPEL_SO_VERSION_H__" >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#define COMPEL_SO_VERSION_H__" >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#define COMPEL_SO_VERSION \"$(COMPEL_SO_VERSION)\"" >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#define COMPEL_SO_VERSION_MAJOR " $(COMPEL_SO_VERSION_MAJOR) >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#define COMPEL_SO_VERSION_MINOR " $(COMPEL_SO_VERSION_MINOR) >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#define COMPEL_SO_VERSION_SUBLEVEL " $(COMPEL_SO_VERSION_SUBLEVEL) >> $(COMPEL_VERSION_HEADER) + $(Q) echo "#endif /* COMPEL_SO_VERSION_H__ */" >> $(COMPEL_VERSION_HEADER) + +# +# Compel itself. +compel/%: $(COMPEL_VERSION_HEADER) $(CONFIG_HEADER) .FORCE + $(Q) $(MAKE) $(build)=compel $@ + +# +# Plugins +compel/plugins/%: $(COMPEL_VERSION_HEADER) $(CONFIG_HEADER) .FORCE + $(Q) $(MAKE) $(build)=compel/plugins $@ + +compel/compel: compel/built-in.o compel/lib.a $(COMPEL_VERSION_HEADER) + $(call msg-link, $@) + $(Q) $(CC) $(CFLAGS) $^ $(WRAPFLAGS) $(LDFLAGS) -rdynamic -o $@ + +# +# And compel library. +LIBCOMPEL_SO := libcompel.so +LIBCOMPEL_SO_CFLAGS += $(CFLAGS) -rdynamic -Wl,-soname,$(LIBCOMPEL_SO).$(COMPEL_SO_VERSION_MAJOR) +compel/$(LIBCOMPEL_SO): compel/lib.a + $(call msg-link, $@) + $(Q) $(CC) -shared $(LIBCOMPEL_SO_CFLAGS) -o $@ -Wl,--whole-archive $^ -Wl,--no-whole-archive $(LDFLAGS) diff --git a/compel/Makefile b/compel/Makefile index 5166e9318..52eb27ea7 100644 --- a/compel/Makefile +++ b/compel/Makefile @@ -1,38 +1,23 @@ include $(SRC_DIR)/Makefile.versions -ccflags-y += -iquote criu/include -ccflags-y += -iquote compel/include -ccflags-y += -iquote compel/arch/$(ARCH)/include -ccflags-y += -DCOMPEL_VERSION=\"$(COMPEL_SO_VERSION_MAJOR).$(COMPEL_SO_VERSION_MINOR)\" - -host-ccflags-y += $(filter-out -pg $(CFLAGS-GCOV),$(ccflags-y)) -HOSTCFLAGS += $(filter-out -pg $(CFLAGS-GCOV),$(WARNINGS) $(DEFINES)) -HOSTLDFLAGS += $(filter-out -pg $(CFLAGS-GCOV),$(LDFLAGS)) -HOSTCFLAGS := $(filter-out -DCONFIG_X86_64,$(HOSTCFLAGS)) -export host-ccflags-y HOSTCFLAGS HOSTLDFLAGS - -hostprogs-y += compel -compel-objs += main.o -compel-objs += handle-elf.o -compel-objs += arch/$(ARCH)/handle-elf.o - -ifeq ($(ARCH),x86) -# Add -DCONFIG_X86_64 or -DCONFIG_X86_32 to HOSTCFLAGS -define ccflags-defines - export HOSTCFLAGS_$(notdir $(1)) += -DCONFIG_X86_64 -endef -$(eval $(call map,ccflags-defines,$(compel-objs))) - -compel-objs += handle-elf-32.o -export HOSTCFLAGS_handle-elf-32.o += -DCONFIG_X86_32 -endif # ARCH == x86 - .PHONY: .FORCE -export compel-objs -test/compel/%: .FORCE - $(Q) $(MAKE) $(build)=test/compel $@ +COMPEL_SO_VERSION := $(COMPEL_SO_VERSION_MAJOR)$(if $(COMPEL_SO_VERSION_MINOR),.$(COMPEL_SO_VERSION_MINOR))$(if $(COMPEL_SO_VERSION_SUBLEVEL),.$(COMPEL_SO_VERSION_SUBLEVEL)) +COMPEL_SO_VERSION_CODE := $(shell expr $(COMPEL_SO_VERSION_MAJOR) \* 65536 \+ $(COMPEL_SO_VERSION_MINOR) \* 256 \+ $(COMPEL_SO_VERSION_SUBLEVEL)) +ccflags-y += -iquote compel/arch/$(ARCH)/src/lib/include +ccflags-y += -iquote compel/include +ccflags-y += -iquote compel/plugins/include +ccflags-y += -iquote $(SRC_DIR)/criu/include +ccflags-y += -fPIC -test: test/compel/test_handle_binary +lib-y += arch/$(ARCH)/src/lib/handle-elf.o +lib-y += src/lib/handle-elf.o +lib-y += src/lib/handle-elf-32.o -.PHONY: test +obj-y += src/main.o + +CFLAGS_handle-elf-32.o += -UCONFIG_X86_64 -DCONFIG_X86_32 +CFLAGS_handle-elf-32.d += -UCONFIG_X86_64 -DCONFIG_X86_32 + +cleanup-y += compel/compel +cleanup-y += compel/libcompel.so diff --git a/compel/arch/aarch64/handle-elf.c b/compel/arch/aarch64/handle-elf.c deleted file mode 100644 index bbd4bf1b0..000000000 --- a/compel/arch/aarch64/handle-elf.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -#include "piegen.h" -#include "uapi/piegen-err.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 -E_NOT_ELF; -} diff --git a/compel/arch/aarch64/include/handle-elf.h b/compel/arch/aarch64/include/handle-elf.h deleted file mode 100644 index 7d7bfb7b3..000000000 --- a/compel/arch/aarch64/include/handle-elf.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __COMPEL_HANDLE_ELF_H__ -#define __COMPEL_HANDLE_ELF_H__ - -#include "uapi/elf64-types.h" - -#define __handle_elf handle_elf_aarch64 -#define arch_is_machine_supported(e_machine) (e_machine == EM_AARCH64) - -extern int handle_elf_aarch64(void *mem, size_t size); - -#endif /* __COMPEL_HANDLE_ELF_H__ */ diff --git a/compel/arch/aarch64/src/lib/handle-elf.c b/compel/arch/aarch64/src/lib/handle-elf.c new file mode 100644 index 000000000..633a382cc --- /dev/null +++ b/compel/arch/aarch64/src/lib/handle-elf.c @@ -0,0 +1,34 @@ +#include + +#include "uapi/compel.h" + +#include "handle-elf.h" +#include "piegen.h" + +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, +}; + +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 -EINVAL; +} diff --git a/compel/arch/aarch64/src/lib/include/handle-elf.h b/compel/arch/aarch64/src/lib/include/handle-elf.h new file mode 100644 index 000000000..0f64b34cb --- /dev/null +++ b/compel/arch/aarch64/src/lib/include/handle-elf.h @@ -0,0 +1,11 @@ +#ifndef COMPEL_HANDLE_ELF_H__ +#define COMPEL_HANDLE_ELF_H__ + +#include "elf64-types.h" + +#define __handle_elf handle_elf_aarch64 +#define arch_is_machine_supported(e_machine) (e_machine == EM_AARCH64) + +extern int handle_elf_aarch64(void *mem, size_t size); + +#endif /* COMPEL_HANDLE_ELF_H__ */ diff --git a/compel/arch/arm/include/handle-elf.h b/compel/arch/arm/include/handle-elf.h deleted file mode 100644 index a465f4273..000000000 --- a/compel/arch/arm/include/handle-elf.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __COMPEL_HANDLE_ELF_H__ -#define __COMPEL_HANDLE_ELF_H__ - -#include "uapi/elf32-types.h" - -#define __handle_elf handle_elf_arm -#define arch_is_machine_supported(e_machine) (e_machine == EM_ARM) - -extern int handle_elf_arm(void *mem, size_t size); - -#endif /* __COMPEL_HANDLE_ELF_H__ */ diff --git a/compel/arch/arm/handle-elf.c b/compel/arch/arm/src/lib/handle-elf.c similarity index 54% rename from compel/arch/arm/handle-elf.c rename to compel/arch/arm/src/lib/handle-elf.c index 2692439c7..e2df0f90d 100644 --- a/compel/arch/arm/handle-elf.c +++ b/compel/arch/arm/src/lib/handle-elf.c @@ -1,8 +1,15 @@ #include -#include "piegen.h" -#include "uapi/piegen-err.h" +#include "uapi/compel.h" + #include "handle-elf.h" +#include "piegen.h" + +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, +}; int handle_binary(void *mem, size_t size) { @@ -10,5 +17,5 @@ int handle_binary(void *mem, size_t size) return handle_elf_arm(mem, size); pr_err("Unsupported Elf format detected\n"); - return -E_NOT_ELF; + return -EINVAL; } diff --git a/compel/arch/arm/src/lib/include/handle-elf.h b/compel/arch/arm/src/lib/include/handle-elf.h new file mode 100644 index 000000000..e5971f37b --- /dev/null +++ b/compel/arch/arm/src/lib/include/handle-elf.h @@ -0,0 +1,11 @@ +#ifndef COMPEL_HANDLE_ELF_H__ +#define COMPEL_HANDLE_ELF_H__ + +#include "elf32-types.h" + +#define __handle_elf handle_elf_arm +#define arch_is_machine_supported(e_machine) (e_machine == EM_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 deleted file mode 100644 index 049e3fba0..000000000 --- a/compel/arch/ppc64/handle-elf.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -#include "piegen.h" -#include "uapi/piegen-err.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 -E_NOT_ELF; -} diff --git a/compel/arch/ppc64/include/handle-elf.h b/compel/arch/ppc64/include/handle-elf.h deleted file mode 100644 index b324debdf..000000000 --- a/compel/arch/ppc64/include/handle-elf.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef __COMPEL_HANDLE_ELF_H__ -#define __COMPEL_HANDLE_ELF_H__ - -#include "uapi/elf64-types.h" - -#define ELF_PPC64 -#define __handle_elf handle_elf_ppc64 -#define arch_is_machine_supported(e_machine) (e_machine == EM_PPC64) - -extern int handle_elf_ppc64(void *mem, size_t size); - -#endif /* __COMPEL_HANDLE_ELF_H__ */ diff --git a/compel/arch/ppc64/src/lib/handle-elf.c b/compel/arch/ppc64/src/lib/handle-elf.c new file mode 100644 index 000000000..6491f2085 --- /dev/null +++ b/compel/arch/ppc64/src/lib/handle-elf.c @@ -0,0 +1,34 @@ +#include + +#include "uapi/compel.h" + +#include "handle-elf.h" +#include "piegen.h" + +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, +}; + +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 -EINVAL; +} diff --git a/compel/arch/ppc64/src/lib/include/handle-elf.h b/compel/arch/ppc64/src/lib/include/handle-elf.h new file mode 100644 index 000000000..1a8217e6b --- /dev/null +++ b/compel/arch/ppc64/src/lib/include/handle-elf.h @@ -0,0 +1,13 @@ +#ifndef COMPEL_HANDLE_ELF_H__ +#define COMPEL_HANDLE_ELF_H__ + +#include "elf64-types.h" + +#define ELF_PPC64 + +#define __handle_elf handle_elf_ppc64 +#define arch_is_machine_supported(e_machine) (e_machine == EM_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 deleted file mode 100644 index 9edb94262..000000000 --- a/compel/arch/x86/handle-elf.c +++ /dev/null @@ -1,16 +0,0 @@ -#include - -#include "piegen.h" -#include "uapi/piegen-err.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 -E_NOT_ELF; -} diff --git a/compel/arch/x86/src/lib/handle-elf.c b/compel/arch/x86/src/lib/handle-elf.c new file mode 100644 index 000000000..7cfbaa1dd --- /dev/null +++ b/compel/arch/x86/src/lib/handle-elf.c @@ -0,0 +1,29 @@ +#include + +#include "uapi/compel.h" + +#include "handle-elf.h" +#include "piegen.h" + +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_32[EI_NIDENT] = { + 0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +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 -EINVAL; +} diff --git a/compel/arch/x86/include/handle-elf.h b/compel/arch/x86/src/lib/include/handle-elf.h similarity index 59% rename from compel/arch/x86/include/handle-elf.h rename to compel/arch/x86/src/lib/include/handle-elf.h index eea94d463..bfc935a4e 100644 --- a/compel/arch/x86/include/handle-elf.h +++ b/compel/arch/x86/src/lib/include/handle-elf.h @@ -1,18 +1,22 @@ -#ifndef __COMPEL_HANDLE_ELF_H__ -#define __COMPEL_HANDLE_ELF_H__ +#ifndef COMPEL_HANDLE_ELF_H__ +#define COMPEL_HANDLE_ELF_H__ #ifdef CONFIG_X86_32 -#include "uapi/elf32-types.h" +#include "elf32-types.h" + #define ELF_X86_32 -#define __handle_elf handle_elf_x86_32 + +#define __handle_elf handle_elf_x86_32 #define arch_is_machine_supported(e_machine) (e_machine == EM_386) #else /* CONFIG_X86_64 */ -#include "uapi/elf64-types.h" +#include "elf64-types.h" + #define ELF_X86_64 -#define __handle_elf handle_elf_x86_64 + +#define __handle_elf handle_elf_x86_64 #define arch_is_machine_supported(e_machine) (e_machine == EM_X86_64) #endif @@ -20,4 +24,4 @@ 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__ */ +#endif /* COMPEL_HANDLE_ELF_H__ */ diff --git a/compel/include/elf32-types.h b/compel/include/elf32-types.h new file mode 100644 index 000000000..b516ba17e --- /dev/null +++ b/compel/include/elf32-types.h @@ -0,0 +1,16 @@ +#ifndef COMPEL_ELF32_TYPES_H__ +#define COMPEL_ELF32_TYPES_H__ + +#define Elf_Ehdr Elf32_Ehdr +#define Elf_Shdr Elf32_Shdr +#define Elf_Sym Elf32_Sym +#define Elf_Rel Elf32_Rel +#define Elf_Rela Elf32_Rela + +#define ELF_ST_TYPE ELF32_ST_TYPE +#define ELF_ST_BIND ELF32_ST_BIND + +#define ELF_R_SYM ELF32_R_SYM +#define ELF_R_TYPE ELF32_R_TYPE + +#endif /* COMPEL_ELF32_TYPES_H__ */ diff --git a/compel/include/elf64-types.h b/compel/include/elf64-types.h new file mode 100644 index 000000000..c4d5f1c72 --- /dev/null +++ b/compel/include/elf64-types.h @@ -0,0 +1,16 @@ +#ifndef COMPEL_ELF64_TYPES_H__ +#define COMPEL_ELF64_TYPES_H__ + +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Shdr Elf64_Shdr +#define Elf_Sym Elf64_Sym +#define Elf_Rel Elf64_Rel +#define Elf_Rela Elf64_Rela + +#define ELF_ST_TYPE ELF64_ST_TYPE +#define ELF_ST_BIND ELF64_ST_BIND + +#define ELF_R_SYM ELF64_R_SYM +#define ELF_R_TYPE ELF64_R_TYPE + +#endif /* COMPEL_ELF64_TYPES_H__ */ diff --git a/compel/include/piegen.h b/compel/include/piegen.h index 736152a2c..f1ed2e13d 100644 --- a/compel/include/piegen.h +++ b/compel/include/piegen.h @@ -1,5 +1,5 @@ -#ifndef __ELFTIL_H__ -#define __ELFTIL_H__ +#ifndef COMPEL_PIEGEN_H__ +#define COMPEL_PIEGEN_H__ #include #include @@ -51,20 +51,4 @@ do { \ 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__ */ +#endif /* COMPEL_PIEGEN_H__ */ diff --git a/compel/include/uapi/elf32-types.h b/compel/include/uapi/elf32-types.h deleted file mode 100644 index 51bf4a24a..000000000 --- a/compel/include/uapi/elf32-types.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __COMPEL_ELF32_TYPES_H__ -#define __COMPEL_ELF32_TYPES_H__ - -#define Ehdr_t Elf32_Ehdr -#define Shdr_t Elf32_Shdr -#define Sym_t Elf32_Sym -#define Rel_t Elf32_Rel -#define Rela_t Elf32_Rela - -#define Off_t Elf32_Off -#define Word_t Elf32_Word -#define Half_t Elf32_Half - -#define ELF_ST_TYPE ELF32_ST_TYPE -#define ELF_ST_BIND ELF32_ST_BIND - -#define ELF_R_SYM ELF32_R_SYM -#define ELF_R_TYPE ELF32_R_TYPE - -#endif /* __COMPEL_ELF32_TYPES_H__ */ diff --git a/compel/include/uapi/elf64-types.h b/compel/include/uapi/elf64-types.h deleted file mode 100644 index d4d6f6f16..000000000 --- a/compel/include/uapi/elf64-types.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __COMPEL_ELF64_TYPES_H__ -#define __COMPEL_ELF64_TYPES_H__ - -#define Ehdr_t Elf64_Ehdr -#define Shdr_t Elf64_Shdr -#define Sym_t Elf64_Sym -#define Rel_t Elf64_Rel -#define Rela_t Elf64_Rela - -#define Off_t Elf64_Off -#define Word_t Elf64_Word -#define Half_t Elf64_Half - -#define ELF_ST_TYPE ELF64_ST_TYPE -#define ELF_ST_BIND ELF64_ST_BIND - -#define ELF_R_SYM ELF64_R_SYM -#define ELF_R_TYPE ELF64_R_TYPE - -#endif /* __COMPEL_ELF64_TYPES_H__ */ diff --git a/compel/include/uapi/piegen-err.h b/compel/include/uapi/piegen-err.h deleted file mode 100644 index f8a2349a1..000000000 --- a/compel/include/uapi/piegen-err.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __PIEGEN_ERR_H__ -#define __PIEGEN_ERR_H__ - -/* Error numbers for piegen. Success is 0, so errors should differ. */ -#define E_UNKNOWN 1 -#define E_NOMEM 2 -#define E_NOT_ELF 3 -#define E_NO_STR_SEC 4 - -#endif /* __PIEGEN_ERR_H__ */ diff --git a/compel/handle-elf-32.c b/compel/src/lib/handle-elf-32.c similarity index 100% rename from compel/handle-elf-32.c rename to compel/src/lib/handle-elf-32.c diff --git a/compel/handle-elf.c b/compel/src/lib/handle-elf.c similarity index 88% rename from compel/handle-elf.c rename to compel/src/lib/handle-elf.c index 0d598468c..3aaa2a2fa 100644 --- a/compel/handle-elf.c +++ b/compel/src/lib/handle-elf.c @@ -11,16 +11,15 @@ #include #include +#include "uapi/compel.h" + #include "asm-generic/int.h" -#include "uapi/piegen-err.h" -#include "piegen.h" #include "handle-elf.h" +#include "piegen.h" -/* TODO: merge with util-vdso.c part in criu header */ /* Check if pointer is out-of-bound */ -static bool -__ptr_oob(const uintptr_t ptr, const uintptr_t start, const size_t size) +static bool __ptr_oob(const uintptr_t ptr, const uintptr_t start, const size_t size) { uintptr_t end = start + size; @@ -29,7 +28,7 @@ __ptr_oob(const uintptr_t ptr, const uintptr_t start, const size_t size) /* Check if pointed structure's end is out-of-bound */ static bool __ptr_struct_end_oob(const uintptr_t ptr, const size_t struct_size, - const uintptr_t start, const size_t size) + const uintptr_t start, const size_t size) { /* the last byte of the structure should be inside [begin, end) */ return __ptr_oob(ptr + struct_size - 1, start, size); @@ -37,7 +36,7 @@ static bool __ptr_struct_end_oob(const uintptr_t ptr, const size_t struct_size, /* Check if pointed structure is out-of-bound */ static bool __ptr_struct_oob(const uintptr_t ptr, const size_t struct_size, - const uintptr_t start, const size_t size) + const uintptr_t start, const size_t size) { return __ptr_oob(ptr, start, size) || __ptr_struct_end_oob(ptr, struct_size, start, size); @@ -73,7 +72,8 @@ static int do_relative_toc(long value, uint16_t *location, } if ((~mask & 0xffff) & value) { - pr_err("bad TOC16 relocation (%ld) (0x%lx)\n", value, (~mask & 0xffff) & value); + pr_err("bad TOC16 relocation (%ld) (0x%lx)\n", + value, (~mask & 0xffff) & value); return -1; } @@ -82,7 +82,7 @@ static int do_relative_toc(long value, uint16_t *location, } #endif -static bool is_header_supported(Ehdr_t *hdr) +static bool is_header_supported(Elf_Ehdr *hdr) { if (!arch_is_machine_supported(hdr->e_machine)) return false; @@ -91,11 +91,11 @@ static bool is_header_supported(Ehdr_t *hdr) return true; } -static const char *get_strings_section(Ehdr_t *hdr, uintptr_t mem, size_t size) +static const char *get_strings_section(Elf_Ehdr *hdr, uintptr_t mem, size_t size) { size_t sec_table_size = ((size_t) hdr->e_shentsize) * hdr->e_shnum; uintptr_t sec_table = mem + hdr->e_shoff; - Shdr_t *secstrings_hdr; + Elf_Shdr *secstrings_hdr; uintptr_t addr; if (__ptr_struct_oob(sec_table, sec_table_size, mem, size)) { @@ -109,7 +109,7 @@ static const char *get_strings_section(Ehdr_t *hdr, uintptr_t mem, size_t size) * (size of section header * index of string section header) */ addr = sec_table + ((size_t) hdr->e_shentsize) * hdr->e_shstrndx; - if (__ptr_struct_oob(addr, sizeof(Shdr_t), + if (__ptr_struct_oob(addr, sizeof(Elf_Shdr), sec_table, sec_table + sec_table_size)) { pr_err("String section header @%#zx is out of [%#zx, %#zx)\n", addr, sec_table, sec_table + sec_table_size); @@ -128,51 +128,55 @@ static const char *get_strings_section(Ehdr_t *hdr, uintptr_t mem, size_t size) return (void*)addr; } +/* + * This name @__handle_elf get renamed into + * @handle_elf_ppc64 or say @handle_elf_x86_64 + * depending on the architecture it's compiled + * under. + */ int __handle_elf(void *mem, size_t size) { const char *symstrings = NULL; - Shdr_t *symtab_hdr = NULL; - Sym_t *symbols = NULL; - Ehdr_t *hdr = mem; + Elf_Shdr *symtab_hdr = NULL; + Elf_Sym *symbols = NULL; + Elf_Ehdr *hdr = mem; - Shdr_t *strtab_hdr = NULL; - Shdr_t **sec_hdrs = NULL; + Elf_Shdr *strtab_hdr = NULL; + Elf_Shdr **sec_hdrs = NULL; const char *secstrings; size_t i, k, nr_gotpcrel = 0; #ifdef ELF_PPC64 s64 toc_offset = 0; #endif - int ret = -E_UNKNOWN; + int ret = -EINVAL; pr_debug("Header\n"); pr_debug("------------\n"); pr_debug("\ttype 0x%x machine 0x%x version 0x%x\n", - (unsigned)hdr->e_type, (unsigned)hdr->e_machine, (unsigned)hdr->e_version); + (unsigned)hdr->e_type, (unsigned)hdr->e_machine, + (unsigned)hdr->e_version); if (!is_header_supported(hdr)) { pr_err("Unsupported header detected\n"); - ret = -E_NOT_ELF; goto err; } sec_hdrs = malloc(sizeof(*sec_hdrs) * hdr->e_shnum); if (!sec_hdrs) { pr_err("No memory for section headers\n"); - ret = -E_NOMEM; + ret = -ENOMEM; goto err; } secstrings = get_strings_section(hdr, (uintptr_t)mem, size); - if (!secstrings) { - ret = -E_NO_STR_SEC; + if (!secstrings) goto err; - } pr_debug("Sections\n"); pr_debug("------------\n"); for (i = 0; i < hdr->e_shnum; i++) { - Shdr_t *sh = mem + hdr->e_shoff + hdr->e_shentsize * i; + Elf_Shdr *sh = mem + hdr->e_shoff + hdr->e_shentsize * i; ptr_func_exit(sh); if (sh->sh_type == SHT_SYMTAB) @@ -221,9 +225,9 @@ int __handle_elf(void *mem, size_t size) pr_out("#include \"%s/types.h\"\n", opts.uapi_dir); for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) { - Sym_t *sym = &symbols[i]; + Elf_Sym *sym = &symbols[i]; const char *name; - Shdr_t *sh_src; + Elf_Shdr *sh_src; ptr_func_exit(sym); name = &symstrings[sym->st_name]; @@ -245,7 +249,8 @@ int __handle_elf(void *mem, size_t size) #endif if (strncmp(name, "__export", 8)) continue; - if ((sym->st_shndx && sym->st_shndx < hdr->e_shnum) || sym->st_shndx == SHN_ABS) { + if ((sym->st_shndx && sym->st_shndx < hdr->e_shnum) || + sym->st_shndx == SHN_ABS) { if (sym->st_shndx == SHN_ABS) { sh_src = NULL; } else { @@ -254,7 +259,8 @@ int __handle_elf(void *mem, size_t size) } pr_out("#define %s%s 0x%lx\n", opts.prefix_name, name, - (unsigned long)(sym->st_value + (sh_src ? sh_src->sh_addr : 0))); + (unsigned long)(sym->st_value + + (sh_src ? sh_src->sh_addr : 0))); } } } @@ -264,8 +270,8 @@ int __handle_elf(void *mem, size_t size) pr_debug("Relocations\n"); pr_debug("------------\n"); for (i = 0; i < hdr->e_shnum; i++) { - Shdr_t *sh = sec_hdrs[i]; - Shdr_t *sh_rel; + Elf_Shdr *sh = sec_hdrs[i]; + Elf_Shdr *sh_rel; if (sh->sh_type != SHT_REL && sh->sh_type != SHT_RELA) continue; @@ -283,11 +289,11 @@ int __handle_elf(void *mem, size_t size) unsigned long place; const char *name; void *where; - Sym_t *sym; + Elf_Sym *sym; union { - Rel_t rel; - Rela_t rela; + Elf_Rel rel; + Elf_Rela rela; } *r = mem + sh->sh_offset + sh->sh_entsize * k; ptr_func_exit(r); @@ -341,7 +347,7 @@ int __handle_elf(void *mem, size_t size) value32 = (s32)sym->st_value; value64 = (s64)sym->st_value; } else { - Shdr_t *sh_src; + Elf_Shdr *sh_src; if ((unsigned)sym->st_shndx > (unsigned)hdr->e_shnum) { pr_err("Unexpected symbol section index %u/%u\n", @@ -356,7 +362,9 @@ int __handle_elf(void *mem, size_t size) } #ifdef ELF_PPC64 -/* Snippet from the OpenPOWER ABI for Linux Supplement: +/* + * Snippet from the OpenPOWER ABI for Linux Supplement: + * * The OpenPOWER ABI uses the three most-significant bits in the symbol * st_other field specifies the number of instructions between a function's * global entry point and local entry point. The global entry point is used @@ -364,6 +372,7 @@ int __handle_elf(void *mem, size_t size) * local entry point is used when r2 is known to already be valid for the * function. A value of zero in these bits asserts that the function does * not use r2. + * * The st_other values have the following meanings: * 0 and 1, the local and global entry points are the same. * 2, the local entry point is at 1 instruction past the global entry point. @@ -408,7 +417,7 @@ int __handle_elf(void *mem, size_t size) case R_PPC64_ADDR32: pr_debug("\t\t\tR_PPC64_ADDR32 at 0x%-4lx val 0x%x\n", place, (unsigned int)(value32 + addend32)); - pr_out(" { .offset = 0x%-8x, .type = PIEGEN_TYPE_INT, " + pr_out(" { .offset = 0x%-8x, .type = COMPEL_TYPE_INT, " " .addend = %-8d, .value = 0x%-16x, " "}, /* R_PPC64_ADDR32 */\n", (unsigned int) place, addend32, value32); @@ -418,7 +427,7 @@ int __handle_elf(void *mem, size_t size) case R_PPC64_REL64: pr_debug("\t\t\tR_PPC64_ADDR64 at 0x%-4lx val 0x%lx\n", place, value64 + addend64); - pr_out("\t{ .offset = 0x%-8x, .type = PIEGEN_TYPE_LONG," + pr_out("\t{ .offset = 0x%-8x, .type = COMPEL_TYPE_LONG," " .addend = %-8ld, .value = 0x%-16lx, " "}, /* R_PPC64_ADDR64 */\n", (unsigned int) place, (long)addend64, (long)value64); @@ -478,13 +487,13 @@ int __handle_elf(void *mem, size_t size) case R_X86_64_32: /* Symbol + Addend (4 bytes) */ case R_X86_64_32S: /* Symbol + Addend (4 bytes) */ pr_debug("\t\t\t\tR_X86_64_32 at 0x%-4lx val 0x%x\n", place, value32); - pr_out(" { .offset = 0x%-8x, .type = PIEGEN_TYPE_INT, " + pr_out(" { .offset = 0x%-8x, .type = COMPEL_TYPE_INT, " ".addend = %-8d, .value = 0x%-16x, }, /* R_X86_64_32 */\n", (unsigned int)place, addend32, value32); break; case R_X86_64_64: /* Symbol + Addend (8 bytes) */ pr_debug("\t\t\t\tR_X86_64_64 at 0x%-4lx val 0x%lx\n", place, (long)value64); - pr_out(" { .offset = 0x%-8x, .type = PIEGEN_TYPE_LONG, " + pr_out(" { .offset = 0x%-8x, .type = COMPEL_TYPE_LONG, " ".addend = %-8ld, .value = 0x%-16lx, }, /* R_X86_64_64 */\n", (unsigned int)place, (long)addend64, (long)value64); break; @@ -504,7 +513,7 @@ int __handle_elf(void *mem, size_t size) break; case R_X86_64_GOTPCREL: /* SymbolOffsetInGot + GOT + Addend - Place (4 bytes) */ pr_debug("\t\t\t\tR_X86_64_GOTPCREL at 0x%-4lx val 0x%x\n", place, value32); - pr_out(" { .offset = 0x%-8x, .type = PIEGEN_TYPE_LONG | PIEGEN_TYPE_GOTPCREL, " + pr_out(" { .offset = 0x%-8x, .type = COMPEL_TYPE_LONG | COMPEL_TYPE_GOTPCREL, " ".addend = %-8d, .value = 0x%-16x, }, /* R_X86_64_GOTPCREL */\n", (unsigned int)place, addend32, value32); nr_gotpcrel++; @@ -514,7 +523,7 @@ int __handle_elf(void *mem, size_t size) #ifdef ELF_X86_32 case R_386_32: /* Symbol + Addend */ pr_debug("\t\t\t\tR_386_32 at 0x%-4lx val 0x%x\n", place, value32 + addend32); - pr_out(" { .offset = 0x%-8x, .type = PIEGEN_TYPE_INT, " + pr_out(" { .offset = 0x%-8x, .type = COMPEL_TYPE_INT, " ".addend = %-4d, .value = 0x%x, },\n", (unsigned int)place, addend32, value32); break; @@ -539,8 +548,8 @@ int __handle_elf(void *mem, size_t size) pr_out("static __maybe_unused const char %s[] = {\n\t", opts.stream_name); - for (i=0, k=0; i < hdr->e_shnum; i++) { - Shdr_t *sh = sec_hdrs[i]; + for (i = 0, k = 0; i < hdr->e_shnum; i++) { + Elf_Shdr *sh = sec_hdrs[i]; unsigned char *shdata; size_t j; @@ -548,19 +557,19 @@ int __handle_elf(void *mem, size_t size) continue; shdata = mem + sh->sh_offset; - pr_debug("Copying section '%s'\n" \ + pr_debug("Copying section '%s'\n" "\tstart:0x%lx (gap:0x%lx) size:0x%lx\n", &secstrings[sh->sh_name], (unsigned long) sh->sh_addr, (unsigned long)(sh->sh_addr - k), (unsigned long) sh->sh_size); /* write 0 in the gap between the 2 sections */ - for (;k < sh->sh_addr; k++) { + for (; k < sh->sh_addr; k++) { if (k && (k % 8) == 0) pr_out("\n\t"); pr_out("0x00,"); } - for (j=0; j < sh->sh_size; j++, k++) { + for (j = 0; j < sh->sh_size; j++, k++) { if (k && (k % 8) == 0) pr_out("\n\t"); pr_out("%#02x,", shdata[j]); diff --git a/compel/main.c b/compel/src/main.c similarity index 99% rename from compel/main.c rename to compel/src/main.c index 6f117cded..ee6ccd53a 100644 --- a/compel/main.c +++ b/compel/src/main.c @@ -12,6 +12,7 @@ #include #include +#include "version.h" #include "piegen.h" static const char compel_cflags_pie[] = "-fpie -Wa,--noexecstack -fno-stack-protector";