Commit 392469b6 authored by Dmitry Safonov's avatar Dmitry Safonov Committed by Andrei Vagin

compel: add per-arch handle-elf.c

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@openvz.org>
Signed-off-by: 's avatarDmitry Safonov <dsafonov@virtuozzo.com>
Acked-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 427f68f0
......@@ -12,13 +12,14 @@ HOSTLDFLAGS += $(filter-out -pg $(CFLAGS-GCOV),$(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)))
......
#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;
}
#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__ */
#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;
}
#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__ */
#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;
}
......@@ -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__ */
#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;
}
......@@ -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__ */
......@@ -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 "common/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;
......
......@@ -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__ */
......@@ -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 "common/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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment