make cleanup
This commit is contained in:
131
samples/Makefile
131
samples/Makefile
@@ -3,7 +3,6 @@
|
||||
|
||||
include ../config.mk
|
||||
|
||||
LIBNAME = unicorn
|
||||
UNAME_S := $(shell uname -s)
|
||||
|
||||
# Find GLIB
|
||||
@@ -11,70 +10,34 @@ ifndef GLIB
|
||||
GLIB = $(shell pkg-config --libs glib-2.0)
|
||||
endif
|
||||
|
||||
UNICORN_DEP_LIBS_STATIC += -lpthread -lm $(GLIB)
|
||||
|
||||
# Verbose output?
|
||||
V ?= 0
|
||||
|
||||
INCDIR = ../include
|
||||
SAMPLEDIR = .
|
||||
OBJDIR = .
|
||||
LIBDIR = ..
|
||||
CFLAGS += -Wall -Werror -I../include
|
||||
LDFLAGS += -L..
|
||||
LDLIBS += -lpthread -lunicorn -lm $(GLIB)
|
||||
|
||||
CFLAGS += -Wall -Werror -I$(INCDIR)
|
||||
LDFLAGS += -lpthread -L$(LIBDIR) -l$(LIBNAME)
|
||||
LDFLAGS_STATIC += $(UNICORN_DEP_LIBS_STATIC)
|
||||
|
||||
ifeq ($(CROSS),)
|
||||
CC ?= cc
|
||||
LDFLAGS += -lm $(GLIB)
|
||||
else
|
||||
ifneq ($(CROSS),)
|
||||
CC = $(CROSS)gcc
|
||||
endif
|
||||
|
||||
ifeq ($(UNICORN_ASAN),yes)
|
||||
CC = clang -fsanitize=address -fno-omit-frame-pointer
|
||||
CXX = clang++ -fsanitize=address -fno-omit-frame-pointer
|
||||
CC = clang
|
||||
CXX = clang++
|
||||
AR = llvm-ar
|
||||
LDFLAGS := -fsanitize=address ${LDFLAGS}
|
||||
CFLAGS += -fsanitize=address -fno-omit-frame-pointer
|
||||
endif
|
||||
|
||||
|
||||
#CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
|
||||
#LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
|
||||
|
||||
BIN_EXT =
|
||||
AR_EXT = a
|
||||
|
||||
# Cygwin?
|
||||
ifneq ($(filter CYGWIN%,$(UNAME_S)),)
|
||||
CFLAGS := $(CFLAGS:-fPIC=)
|
||||
LDFLAGS += -lssp
|
||||
LDFLAGS_STATIC += -lssp
|
||||
BIN_EXT = .exe
|
||||
AR_EXT = a
|
||||
LDLIBS += -lssp
|
||||
# mingw?
|
||||
else ifneq ($(filter MINGW%,$(UNAME_S)),)
|
||||
CFLAGS := $(CFLAGS:-fPIC=)
|
||||
BIN_EXT = .exe
|
||||
AR_EXT = lib
|
||||
endif
|
||||
|
||||
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifneq ($(filter MINGW%,$(UNAME_S)),)
|
||||
ARCHIVE = $(LIBDIR)/$(LIBNAME).$(AR_EXT)
|
||||
else ifneq ($(filter CYGWIN%,$(UNAME_S)),)
|
||||
ARCHIVE = $(LIBDIR)/lib$(LIBNAME).$(AR_EXT)
|
||||
else
|
||||
ARCHIVE = $(LIBDIR)/lib$(LIBNAME).$(AR_EXT)
|
||||
#ARCHIVE_X86 = $(LIBDIR)/lib$(LIBNAME)_x86.$(AR_EXT)
|
||||
#ARCHIVE_ARM = $(LIBDIR)/lib$(LIBNAME)_arm.$(AR_EXT)
|
||||
#ARCHIVE_ARM64 = $(LIBDIR)/lib$(LIBNAME)_arm64.$(AR_EXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
.PHONY: all clean clean_bins clean_libs
|
||||
.PHONY: all clean
|
||||
|
||||
UNICORN_ARCHS := $(shell if [ -e ../config.log ]; then cat ../config.log;\
|
||||
else printf "$(UNICORN_ARCHS)"; fi)
|
||||
@@ -89,9 +52,9 @@ endif
|
||||
ifneq (,$(findstring mips,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_mips.c
|
||||
endif
|
||||
ifneq (,$(findstring ppc,$(UNICORN_ARCHS)))
|
||||
#ifneq (,$(findstring ppc,$(UNICORN_ARCHS)))
|
||||
#SOURCES += sample_ppc.c
|
||||
endif
|
||||
#endif
|
||||
ifneq (,$(findstring sparc,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_sparc.c
|
||||
endif
|
||||
@@ -106,73 +69,9 @@ ifneq (,$(findstring m68k,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_m68k.c
|
||||
endif
|
||||
|
||||
OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))
|
||||
OBJS_ELF = $(addprefix $(OBJDIR)/,$(SOURCES:.c=))
|
||||
BINARY = $(addprefix $(SAMPLEDIR)/,$(SOURCES:.c=$(BIN_EXT)))
|
||||
BINS = $(SOURCES:.c=)
|
||||
|
||||
all: $(BINARY)
|
||||
all: $(BINS)
|
||||
|
||||
clean_bins:
|
||||
rm -rf *.o $(OBJS_ELF) $(BINARY) $(SAMPLEDIR)/*.exe $(SAMPLEDIR)/*.static $(OBJDIR)/lib$(LIBNAME)* $(OBJDIR)/$(LIBNAME)*
|
||||
rm -rf sample_x86 sample_arm sample_arm64 sample_mips sample_sparc sample_ppc sample_m68k shellcode mem_apis sample_x86_32_gdt_and_seg_regs sample_batch_reg
|
||||
|
||||
clean_libs:
|
||||
rm -rf libunicorn*.so libunicorn*.lib libunicorn*.dylib unicorn*.dll unicorn*.lib
|
||||
|
||||
clean: clean_bins clean_libs
|
||||
|
||||
$(BINARY): $(OBJS)
|
||||
|
||||
$(SAMPLEDIR)/%$(BIN_EXT): $(OBJDIR)/%.o
|
||||
@mkdir -p $(@D)
|
||||
ifeq ($(V),0)
|
||||
ifeq ($(UNICORN_SHARED),yes)
|
||||
$(call log,LINK,$(notdir $@))
|
||||
@$(link-dynamic)
|
||||
endif
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifneq ($(filter MINGW%,$(UNAME_S)),)
|
||||
$(call log,LINK,$(notdir $(call staticname,$@)))
|
||||
@$(link-static)
|
||||
endif
|
||||
endif
|
||||
else
|
||||
ifeq ($(UNICORN_SHARED),yes)
|
||||
$(link-dynamic)
|
||||
endif
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifneq ($(filter MINGW%,$(UNAME_S)),)
|
||||
$(link-static)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@mkdir -p $(@D)
|
||||
ifeq ($(V),0)
|
||||
$(call log,CC,$(@:$(OBJDIR)/%=%))
|
||||
@$(compile)
|
||||
else
|
||||
$(compile)
|
||||
endif
|
||||
|
||||
|
||||
define link-dynamic
|
||||
$(CC) $< $(LDFLAGS) -o $@
|
||||
endef
|
||||
|
||||
|
||||
define link-static
|
||||
$(CC) $< $(ARCHIVE) $(LDFLAGS_STATIC) -o $(call staticname,$@)
|
||||
endef
|
||||
|
||||
|
||||
staticname = $(subst $(BIN_EXT),,$(1)).static$(BIN_EXT)
|
||||
|
||||
define log
|
||||
@printf " %-7s %s\n" "$(1)" "$(2)"
|
||||
endef
|
||||
|
||||
define compile
|
||||
${CC} ${CFLAGS} -c $< -o $@
|
||||
endef
|
||||
clean:
|
||||
rm -rf *.o $(BINS)
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
|
||||
#define X86_CODE32_JMP_INVALID "\xe9\xe9\xee\xee\xee\x41\x4a" // JMP outside; INC ecx; DEC edx
|
||||
#define X86_CODE32_INOUT "\x41\xE4\x3F\x4a\xE6\x46\x43" // INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx
|
||||
#define X86_CODE32_INC "\x40" // INC eax
|
||||
|
||||
//#define X86_CODE64 "\x41\xBC\x3B\xB0\x28\x2A \x49\x0F\xC9 \x90 \x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9" // <== still crash
|
||||
//#define X86_CODE64 "\x41\xBC\x3B\xB0\x28\x2A\x49\x0F\xC9\x90\x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9"
|
||||
@@ -668,6 +669,105 @@ static void test_i386_inout(void)
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
// emulate code and save/restore the CPU context
|
||||
static void test_i386_context_save(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_context *context;
|
||||
uc_err err;
|
||||
|
||||
int r_eax = 0x1; // EAX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Save/restore CPU context in opaque blob\n");
|
||||
|
||||
// initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// map 8KB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 8 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
if (uc_mem_write(uc, ADDRESS, X86_CODE32_INC, sizeof(X86_CODE32_INC) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_X86_REG_EAX, &r_eax);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
printf(">>> Running emulation for the first time\n");
|
||||
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_INC) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
|
||||
// allocate and save the CPU context
|
||||
printf(">>> Saving CPU context\n");
|
||||
|
||||
err = uc_context_alloc(uc, &context);
|
||||
if (err) {
|
||||
printf("Failed on uc_context_alloc() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = uc_context_save(uc, context);
|
||||
if (err) {
|
||||
printf("Failed on uc_context_save() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// emulate machine code again
|
||||
printf(">>> Running emulation for the second time\n");
|
||||
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_INC) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
|
||||
// restore CPU context
|
||||
err = uc_context_restore(uc, context);
|
||||
if (err) {
|
||||
printf("Failed on uc_context_restore() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> CPU context restored. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
|
||||
// free the CPU context
|
||||
err = uc_context_free(context);
|
||||
if (err) {
|
||||
printf("Failed on uc_context_free() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_x86_64(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
@@ -906,6 +1006,7 @@ int main(int argc, char **argv, char **envp)
|
||||
test_i386();
|
||||
test_i386_map_ptr();
|
||||
test_i386_inout();
|
||||
test_i386_context_save();
|
||||
test_i386_jump();
|
||||
test_i386_loop();
|
||||
test_i386_invalid_mem_read();
|
||||
|
||||
@@ -269,7 +269,7 @@ static void gdt_demo()
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
fprintf(stderr, "%02hhx", buf[i]);
|
||||
fprintf(stderr, "%02x", buf[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user