-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
54 lines (39 loc) · 1.38 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
CC := gcc
CFLAGS := -nostdlib -ffunction-sections -Wl,--gc-sections
LDFLAGS := -nostartfiles -T linker_script.ld
LD := ldd
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
# Get all source files in the directory (including both .c and .S files)
SRC_FILES := $(wildcard $(SRC_DIR)/*.c) $(wildcard $(SRC_DIR)/*.S)
# Create object file names for all source files
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(filter %.c, $(SRC_FILES))) \
$(patsubst $(SRC_DIR)/%.S, $(OBJ_DIR)/%.o, $(filter %.S, $(SRC_FILES)))
# Specify the top-level file (default is "main.c")
TOP_LEVEL ?= main.c
# Specify the final executable name
TARGET := $(BIN_DIR)/$(basename $(notdir $(TOP_LEVEL)))
# Specify the tester path
TEST := test/loader.c
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJ_FILES)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
$(CC) --version | head -n1 > $(BIN_DIR)/versions.txt
$(LD) --version | head -n1 >> $(BIN_DIR)/versions.txt
objcopy --dump-section .text=$(BIN_DIR)/shellcode $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.S
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
inspect: $(BIN_DIR)/shellcode
objdump -D $(TARGET)
objdump -M intel,x86-64 -b binary -D -mi386 $(BIN_DIR)/shellcode
loader: $(BIN_DIR)/shellcode
$(CC) $(TEST) -o $(BIN_DIR)/loader