Skip to content

Commit

Permalink
Rearranging source directories into src/ and test/. Build libegalito.a.
Browse files Browse the repository at this point in the history
The Makefile has been split into parts. There is also a "make .symlinks"
option which makes links to the current build directory.
  • Loading branch information
dwks committed Mar 1, 2017
1 parent 356725c commit 1401bb0
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# root Makefile for egalito
# to change settings, see env.mk

.PHONY: all src test
all: src test
src:
$(MAKE) -C src
test:
$(MAKE) -C test
45 changes: 45 additions & 0 deletions env.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Setup for egalito compilation environment

CC = gcc
CXX = g++
GENERIC_FLAGS = -Wall -Wextra -Wno-unused-parameter -I.
OPT_FLAGS = -g -O2
DEPFLAGS = -MT '$@ $(@:.o=.d)' -MMD -MP
CFLAGS = -std=gnu99 $(GENERIC_FLAGS) $(OPT_FLAGS)
CXXFLAGS = -std=c++11 $(GENERIC_FLAGS) $(OPT_FLAGS)
CLDFLAGS = -lcapstone -Wl,-q
#LDFLAGS = -lcapstone -q
#LDFLAGS2 = `gcc --print-file-name=libstdc++.a` `gcc --print-file-name=libgcc.a`
#LDFLAGS2 = $(shell g++ -print-search-dirs 2>&1 | grep libraries | sed 's/libraries: =/:/; s/:/ -L/g') -lstdc++ --start-group -lgcc -lgcc_eh -lc --end-group

ifdef PROFILE # set PROFILE=1 to enable gprof profiling
CFLAGS += -no-pie -pg
CXXFLAGS += -no-pie -pg
CLDFLAGS += -no-pie -pg
endif

P_ARCH := $(shell uname -m)
ifeq (aarch64,$(P_ARCH))
CFLAGS += -DARCH_AARCH64
CXXFLAGS += -DARCH_AARCH64
BUILDDIR = build_aarch64/
else ifeq (x86_64,$(P_ARCH))
CFLAGS += -DARCH_X86_64
CXXFLAGS += -DARCH_X86_64
BUILDDIR = build_x86_64/
else
$(error Unsupported platform, we only handle aarch64 and x86_64)
endif
CFLAGS += '-DTESTDIR="example/$(BUILDDIR)"'
CXXFLAGS += '-DTESTDIR="example/$(BUILDDIR)"'

ifneq (,$(wildcard /usr/lib/x86_64-linux-gnu))
GLIBCDIR = /usr/lib/x86_64-linux-gnu
else ifneq (,$(wildcard /usr/lib/aarch64-linux-gnu))
GLIBCDIR = /usr/lib/aarch64-linux-gnu
else ifneq (,$(wildcard /usr/lib64))
GLIBCDIR = /usr/lib64
endif
#STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbeginT.o`
STARTFILES = $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o`
ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o
6 changes: 5 additions & 1 deletion src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
*.o
*.d
*.swp
/depend
/.symlinks
/loader
/egalito
/libegalito.a
/ex
/regression/elfgen_test
/regression/output/*
/build_x86_64
/build_aarch64
82 changes: 26 additions & 56 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
# Makefile for egalito

CC = gcc
CXX = g++
GENERIC_FLAGS = -Wall -Wextra -Wno-unused-parameter -I.
OPT_FLAGS = -g -O2
DEPFLAGS = -MT '$@ $(@:.o=.d)' -MMD -MP
CFLAGS = -std=gnu99 $(GENERIC_FLAGS) $(OPT_FLAGS)
CXXFLAGS = -std=c++11 $(GENERIC_FLAGS) $(OPT_FLAGS)
CLDFLAGS = -lcapstone -Wl,-q
#LDFLAGS = -lcapstone -q
#LDFLAGS2 = `gcc --print-file-name=libstdc++.a` `gcc --print-file-name=libgcc.a`
#LDFLAGS2 = $(shell g++ -print-search-dirs 2>&1 | grep libraries | sed 's/libraries: =/:/; s/:/ -L/g') -lstdc++ --start-group -lgcc -lgcc_eh -lc --end-group

ifdef PROFILE # set PROFILE=1 to enable gprof profiling
CFLAGS += -no-pie -pg
CXXFLAGS += -no-pie -pg
CLDFLAGS += -no-pie -pg
endif

P_ARCH := $(shell uname -m)
ifeq (aarch64,$(P_ARCH))
CFLAGS += -DARCH_AARCH64
CXXFLAGS += -DARCH_AARCH64
BUILDDIR = build_aarch64/
else ifeq (x86_64,$(P_ARCH))
CFLAGS += -DARCH_X86_64
CXXFLAGS += -DARCH_X86_64
BUILDDIR = build_x86_64/
else
$(error Unsupported platform, we only handle aarch64 and x86_64)
endif
CFLAGS += '-DTESTDIR="test/$(BUILDDIR)"'
CXXFLAGS += '-DTESTDIR="test/$(BUILDDIR)"'

ifneq (,$(wildcard /usr/lib/x86_64-linux-gnu))
GLIBCDIR = /usr/lib/x86_64-linux-gnu
else ifneq (,$(wildcard /usr/lib/aarch64-linux-gnu))
GLIBCDIR = /usr/lib/aarch64-linux-gnu
else ifneq (,$(wildcard /usr/lib64))
GLIBCDIR = /usr/lib64
endif
#STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbeginT.o`
STARTFILES = $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o`
ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o
include ../env.mk

ANALYSIS_SOURCES = $(wildcard analysis/*.cpp)
BREAK_SOURCES = $(wildcard break/*.cpp)
Expand All @@ -66,14 +24,15 @@ exe-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)))
obj-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)).o)
dep-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)).d)

SOURCES = $(CHUNK_SOURCES) $(ELF_SOURCES) $(TRANSFORM_SOURCES) $(LOG_SOURCES) \
$(PASS_SOURCES) $(DISASM_SOURCES) $(CONDUCTOR_SOURCES) $(ANALYSIS_SOURCES)
TYPICAL_OBJECTS = $(call obj-filename,$(SOURCES))
TARGET_SOURCES = $(SOURCES) $(MAIN_SOURCES)
TYPICAL_SOURCES = $(CHUNK_SOURCES) $(ELF_SOURCES) $(TRANSFORM_SOURCES) \
$(LOG_SOURCES) $(PASS_SOURCES) $(DISASM_SOURCES) $(CONDUCTOR_SOURCES) \
$(ANALYSIS_SOURCES)
TYPICAL_OBJECTS = $(call obj-filename,$(TYPICAL_SOURCES))
TARGET_SOURCES = $(TYPICAL_SOURCES) $(MAIN_SOURCES)
TARGET_OBJECTS = $(call obj-filename,$(TARGET_SOURCES))
LOADER_SOURCES = $(SOURCES) $(BREAK_SOURCES) $(LOAD_SOURCES)
LOADER_SOURCES = $(TYPICAL_SOURCES) $(BREAK_SOURCES) $(LOAD_SOURCES)
LOADER_OBJECTS = $(call obj-filename,$(LOADER_SOURCES))
ITEST_SOURCES = $(SOURCES) $(INTEGRATION_SOURCES)
ITEST_SOURCES = $(TYPICAL_SOURCES) $(INTEGRATION_SOURCES)
ITEST_OBJECTS = $(call obj-filename,$(ITEST_SOURCES))
ALL_SOURCES = $(sort $(TARGET_SOURCES) $(LOADER_SOURCES) $(ITEST_SOURCES))
ALL_OBJECTS = $(call obj-filename,$(ALL_SOURCES))
Expand All @@ -82,15 +41,26 @@ BUILDTREE = $(sort $(dir $(ALL_OBJECTS)))

TARGET = $(BUILDDIR)egalito
LOADER = $(BUILDDIR)loader
ITEST = $(BUILDDIR)itest
LIBRARY = $(BUILDDIR)libegalito.a

OUTPUTS = $(TARGET) $(LOADER) $(LIBRARY)
SYMLINKS = $(notdir $(OUTPUTS))

# Default target
.PHONY: all
all: $(TARGET) $(LOADER) $(ITEST)
all: $(OUTPUTS) .symlinks

$(ALL_OBJECTS): | $(BUILDTREE)
$(BUILDTREE):
mkdir -p $@
@mkdir -p $@

.symlinks: $(OUTPUTS)
@echo making symlinks...
@ln -sf $(BUILDDIR)egalito
@ln -sf $(BUILDDIR)loader
@ln -sf $(BUILDDIR)libegalito.a
@ln -sf ../test/example/$(BUILDDIR) ex
@touch .symlinks

# Dependencies
DEPEND_FILES = $(call dep-filename,$(ALL_SOURCES))
Expand Down Expand Up @@ -119,8 +89,8 @@ $(LOADER): $(LOADER_OBJECTS)
$(CXX) -static -o $@ -pg \
-nostartfiles $(STARTFILES) $^ $(CLDFLAGS) $(ENDFILES) -Wl,--eh-frame-hdr \
-Wl,-Ttext-segment=0x7400000 -Wl,-Trodata-segment=0x7600000
$(ITEST): $(ITEST_OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(CLDFLAGS)
$(LIBRARY): $(TYPICAL_OBJECTS)
$(AR) cr $(LIBRARY) $(TYPICAL_OBJECTS)

# Tests
REGRESSION_SOURCES = $(wildcard regression/*.cpp)
Expand All @@ -134,9 +104,9 @@ $(REGRESSION_TESTS): % : %.o $(TYPICAL_OBJECTS) $(BUILDDIR)regression/elfgenmana

$(REGRESSION_OBJECTS): | $(BUILDDIR)/regression
$(BUILDDIR)/regression:
mkdir -p $@
@mkdir -p $@

# Other targets
.PHONY: clean
clean:
-rm -rf $(BUILDDIR)
-rm -rf $(BUILDDIR) .symlinks $(SYMLINKS) ex
5 changes: 0 additions & 5 deletions src/test/.gitignore

This file was deleted.

5 changes: 5 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.swp
/.symlinks
/itest
/build_x86_64
/build_aarch64
61 changes: 61 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Makefile for egalito test code

include ../env.mk

CFLAGS += -I ../src/
CXXFLAGS += -I ../src/
CLDFLAGS += -L ../src/$(BUILDDIR) -legalito

INTEGRATION_SOURCES = $(wildcard integration/*.cpp)

exe-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)))
obj-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)).o)
dep-filename = $(foreach s,$1,$(BUILDDIR)$(dir $s)$(basename $(notdir $s)).d)

SOURCES = $(INTEGRATION_SOURCES)
ITEST_SOURCES = $(SOURCES) $(INTEGRATION_SOURCES)
ITEST_OBJECTS = $(call obj-filename,$(ITEST_SOURCES))
ALL_SOURCES = $(sort $(ITEST_SOURCES))
ALL_OBJECTS = $(call obj-filename,$(ALL_SOURCES))

BUILDTREE = $(sort $(dir $(ALL_OBJECTS)))

ITEST = $(BUILDDIR)itest

OUTPUTS = $(ITEST)

# Default target
.PHONY: all
all: $(ITEST) .symlinks

$(ALL_OBJECTS): | $(BUILDTREE)
$(BUILDTREE):
@mkdir -p $@

.symlinks: $(OUTPUTS)
@echo making symlinks...
@ln -sf $(BUILDDIR)itest
@touch .symlinks

# Dependencies
DEPEND_FILES = $(call dep-filename,$(ALL_SOURCES))
-include $(DEPEND_FILES)

# Rules
$(BUILDDIR)%.o: %.s
$(CC) -fPIC -c $< -o $@
$(BUILDDIR)%.o: %.S
$(CC) -fPIC $(DEPFLAGS) -c $< -o $@
$(BUILDDIR)%.o: %.c
$(CC) $(CFLAGS) $(DEPFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<
$(BUILDDIR)%.o: %.cpp
$(CXX) $(CXXFLAGS) $(DEPFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<

# Programs and libraries
$(ITEST): $(ITEST_OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(CLDFLAGS)

# Other targets
.PHONY: clean
clean:
-rm -rf $(BUILDDIR) .symlinks
3 changes: 3 additions & 0 deletions test/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.swp
/build_x86_64
/build_aarch64
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions test/integration/itest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "libc_resolve.h"
#include "jumptable.h"

int main() {
LibcResolve::run();
JumpTableIntegration::run();
JumpTableIntegration::run2();

return 0;
}
91 changes: 91 additions & 0 deletions test/integration/jumptable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <iostream>
#include "jumptable.h"
#include "analysis/jumptable.h"
#include "conductor/conductor.h"
#include "log/registry.h"

void JumpTableIntegration::run() {
GroupRegistry::getInstance()->muteAllSettings();
//GroupRegistry::getInstance()->applySetting("disasm", 9);
//GroupRegistry::getInstance()->applySetting("analysis", 9);

try {
ElfMap elf(TESTDIR "jumptable");

Conductor conductor;
conductor.parse(&elf, nullptr);

auto module = conductor.getMainSpace()->getModule();
auto f = module->getChildren()->getNamed()->find("main");
if(testFunction(f, 1)) {
std::cout << "TEST PASSED: found jump table in main\n";
}
}
catch(const char *error) {
std::cout << "TEST FAILED: error: " << error << std::endl;
}
}

void JumpTableIntegration::run2() {
GroupRegistry::getInstance()->muteAllSettings();
//GroupRegistry::getInstance()->applySetting("disasm", 9);
//GroupRegistry::getInstance()->applySetting("analysis", 9);

try {
ElfMap elf(TESTDIR "jumptable");

Conductor conductor;
conductor.parseRecursive(&elf);

auto libc = conductor.getLibraryList()->getLibc();
if(!libc) {
std::cout << "TEST FAILED: can't locate libc.so in depends\n";
return;
}

auto module = libc->getElfSpace()->getModule();
struct {
const char *name;
int expected;
} testCase[] = {
{"parse_expression", 2},
{"trecurse", 0} // this has a tail-recursive call
};
for(size_t i = 0; i < sizeof(testCase)/sizeof(*testCase); i ++) {
auto name = testCase[i].name;
auto f = module->getChildren()->getNamed()->find(name);
if(f) {
if(!testFunction(f, testCase[i].expected)) return;
}
}

std::cout << "TEST PASSED: found test jump tables in libc\n";
}
catch(const char *error) {
std::cout << "TEST FAILED: error: " << error << std::endl;
}
}

bool JumpTableIntegration::testFunction(Function *f, int expected) {
JumpTableSearch jt;
jt.search(f);

if((int)jt.getTableList().size() != expected) {
std::cout << "TEST FAILED: function ["
<< f->getSymbol()->getName() << "]: expected " << expected
<< " jump tables but found "
<< jt.getTableList().size() << std::endl;
return false;
}

auto tableList = jt.getTableList();
for(auto table : tableList) {
std::cout << "found jump table in ["
<< f->getSymbol()->getName() << "] at "
<< std::hex << table->getAddress() << " with "
<< std::dec << table->getEntries()
<< " entries.\n";
}

return true;
}
14 changes: 14 additions & 0 deletions test/integration/jumptable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef EGALITO_INTEGRATION_JUMP_TABLE_H
#define EGALITO_INTEGRATION_JUMP_TABLE_H

class Function;

class JumpTableIntegration {
public:
static void run();
static void run2();
private:
static bool testFunction(Function *f, int expected);
};

#endif
Loading

0 comments on commit 1401bb0

Please sign in to comment.