Skip to content

Commit 3579dcd

Browse files
committed
feat(ftrace): load and parse elf stab
1 parent f9b9392 commit 3579dcd

File tree

8 files changed

+188
-6
lines changed

8 files changed

+188
-6
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM docker.io/harsonlau/riscv-tools:latest
22

3-
RUN apt-get update && apt-get install -y llvm-dev
3+
RUN apt-get update && apt-get install -y llvm-dev && apt-get install libelf-dev -y
44

55
RUN rm -rf /var/lib/apt/lists/*

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ all:
77
@echo "-------Build Test-------"
88
@$(MAKE) -C test T=$(T)
99
@echo "-------Start Simulation-------"
10-
@$(SIM) $(TARGET) --batch
10+
@$(SIM) $(T) --batch
1111

1212
debug:
1313
@echo "-------Build Simulator-------"
1414
@$(MAKE) -C sim
1515
@echo "-------Build Test-------"
1616
@$(MAKE) -C test T=$(T)
1717
@echo "-------Start Debugging-------"
18-
@$(SIM) $(TARGET) --debug
18+
@$(SIM) $(T) --debug
1919

2020
itrace:
2121
@echo "-------Build Simulator-------"
2222
@$(MAKE) -C sim
2323
@echo "-------Build Test-------"
2424
@$(MAKE) -C test T=$(T)
2525
@echo "-------Start Debugging-------"
26-
@$(SIM) $(TARGET) --itrace
26+
@$(SIM) $(T) --itrace
2727

2828
unit_test:
2929
@echo "------- Building and Running Simulator Unit Tests -------"

sim/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LD = $(CXX)
2424
COMMON_FLAGS = -g -I./include -Wall $(shell llvm-config --cppflags)
2525
CFLAGS = $(COMMON_FLAGS) -std=gnu11
2626
CXXFLAGS = $(COMMON_FLAGS) -std=c++17
27-
LDFLAGS = $(LLVM_LDFLAGS)
27+
LDFLAGS = $(LLVM_LDFLAGS) -lelf
2828

2929
## 3. Rules
3030
all: $(SIMULATOR)

sim/include/ftrace.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef __FTRACE_H__
2+
#define __FTRACE_H__
3+
4+
#include <stdint.h>
5+
6+
typedef struct {
7+
uint64_t address;
8+
char* name;
9+
} FuncSymbol;
10+
11+
typedef struct {
12+
FuncSymbol* symbols;
13+
int count;
14+
int capacity;
15+
} SymbolTable;
16+
17+
void init_symbol_table(SymbolTable *table);
18+
19+
void add_func_symbol(SymbolTable *table, uint64_t addr, const char *name);
20+
21+
void sort_symbols_by_address(SymbolTable *table);
22+
23+
const char* find_func_name(SymbolTable *table, uint64_t addr);
24+
25+
void free_symbol_table(SymbolTable *table);
26+
27+
#endif

sim/include/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdint.h>
55

66
void load_image(char *filepath);
7+
void load_elf_symbols(char *filepath);
78
uint8_t* guest_to_host(uint64_t vaddr);
89
uint32_t inst_fetch(uint64_t pc);
910
uint64_t mem_read(uint64_t addr, int len);

sim/src/ftrace.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "ftrace.h"
5+
6+
void init_symbol_table(SymbolTable *table) {
7+
if (!table) return;
8+
table->symbols = NULL;
9+
table->count = 0;
10+
table->capacity = 0;
11+
}
12+
13+
void add_func_symbol(SymbolTable *table, uint64_t addr, const char *name) {
14+
if (!table || !name) return;
15+
16+
if (table->count >= table->capacity) {
17+
int new_capacity = (table->capacity == 0) ? 16 : table->capacity * 2;
18+
FuncSymbol* new_symbols = realloc(table->symbols, new_capacity * sizeof(FuncSymbol));
19+
if (!new_symbols) {
20+
perror("Failed to reallocate symbol table");
21+
return;
22+
}
23+
table->symbols = new_symbols;
24+
table->capacity = new_capacity;
25+
}
26+
27+
char *name_copy = strdup(name);
28+
if (!name_copy) {
29+
perror("Failed to duplicate symbol name");
30+
return;
31+
}
32+
33+
table->symbols[table->count].address = addr;
34+
table->symbols[table->count].name = name_copy;
35+
table->count++;
36+
}
37+
38+
static int compare_symbols(const void *a, const void *b) {
39+
FuncSymbol *sym_a = (FuncSymbol *)a;
40+
FuncSymbol *sym_b = (FuncSymbol *)b;
41+
if (sym_a->address < sym_b->address) return -1;
42+
if (sym_a->address > sym_b->address) return 1;
43+
return 0;
44+
}
45+
46+
void sort_symbols_by_address(SymbolTable *table) {
47+
if (!table || table->count == 0) return;
48+
qsort(table->symbols, table->count, sizeof(FuncSymbol), compare_symbols);
49+
}
50+
51+
const char* find_func_name(SymbolTable *table, uint64_t addr) {
52+
if (!table || table->count == 0) return "unknown_function";
53+
54+
int low = 0, high = table->count - 1;
55+
int best_match_idx = -1;
56+
57+
while (low <= high) {
58+
int mid = low + (high - low) / 2;
59+
if (table->symbols[mid].address <= addr) {
60+
best_match_idx = mid;
61+
low = mid + 1;
62+
} else {
63+
high = mid - 1;
64+
}
65+
}
66+
67+
if (best_match_idx != -1) {
68+
return table->symbols[best_match_idx].name;
69+
}
70+
71+
return "unknown_function";
72+
}
73+
74+
75+
void free_symbol_table(SymbolTable *table) {
76+
if (!table) return;
77+
for (int i = 0; i < table->count; ++i) {
78+
free(table->symbols[i].name);
79+
}
80+
free(table->symbols);
81+
table->symbols = NULL;
82+
table->count = 0;
83+
table->capacity = 0;
84+
}

sim/src/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <memory.h>
33
#include <cpu.h>
44
#include <disasm.h>
5+
#include "ftrace.h"
56

67
uint8_t *mem = NULL;
78
int itrace_enabled = 0;
89
LLVMDisasmContextRef disasm_ctx;
10+
SymbolTable *sym_table = NULL;
911

1012
const char *help_string = "Usage: Simulator <img_file> [-d|-b]\n"
1113
"Options:\n"
@@ -16,7 +18,16 @@ int main(int argc, char *argv[]){
1618
mem = (uint8_t *)malloc(MEM_SIZE);
1719
check_mem(mem);
1820
memset(mem, 0, MEM_SIZE);
19-
load_image(argv[1]);
21+
22+
char image_file[128] = "";
23+
sprintf(image_file, "test/build/%s.bin", argv[1]);
24+
load_image(image_file);
25+
26+
char elf_file[128] = "";
27+
sprintf(elf_file, "test/build/%s.elf", argv[1]);
28+
sym_table = malloc(sizeof(SymbolTable));
29+
load_elf_symbols(elf_file);
30+
2031
init_cpu();
2132
if (argc > 2 && strcmp(argv[2], "--debug") == 0) {
2233
debug_loop();

sim/src/memory.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <common.h>
2+
#include <gelf.h>
3+
#include <fcntl.h>
4+
#include "ftrace.h"
25

36
extern uint8_t* mem;
7+
extern SymbolTable *sym_table;
48

59
uint8_t* guest_to_host(uint64_t addr) {return mem + addr - MEM_BASE;}
610

@@ -68,3 +72,58 @@ void load_image(char *filepath){
6872
if(fp) fclose(fp);
6973
return;
7074
}
75+
76+
void load_elf_symbols(const char *file_path) {
77+
int fd;
78+
Elf *elf;
79+
GElf_Ehdr ehdr;
80+
init_symbol_table(sym_table);
81+
82+
if (elf_version(EV_CURRENT) == EV_NONE) {
83+
fprintf(stderr, "ELF library initialization failed: %s\n", elf_errmsg(-1));
84+
exit(1);
85+
}
86+
87+
if ((fd = open(file_path, O_RDONLY, 0)) < 0) {
88+
perror("open");
89+
exit(1);
90+
}
91+
92+
if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
93+
fprintf(stderr, "elf_begin() failed: %s\n", elf_errmsg(-1));
94+
exit(1);
95+
}
96+
97+
if (gelf_getehdr(elf, &ehdr) == NULL) {
98+
fprintf(stderr, "gelf_getehdr() failed: %s\n", elf_errmsg(-1));
99+
exit(1);
100+
}
101+
102+
printf("Entry point: 0x%lx\n", ehdr.e_entry);
103+
104+
Elf_Scn *scn = NULL;
105+
GElf_Shdr shdr;
106+
while ((scn = elf_nextscn(elf, scn)) != NULL) {
107+
gelf_getshdr(scn, &shdr);
108+
109+
if (shdr.sh_type == SHT_SYMTAB) {
110+
Elf_Data *data = elf_getdata(scn, NULL);
111+
int count = shdr.sh_size / shdr.sh_entsize;
112+
113+
for (int i = 0; i < count; ++i) {
114+
GElf_Sym sym;
115+
gelf_getsym(data, i, &sym);
116+
117+
if (GELF_ST_TYPE(sym.st_info) == STT_FUNC) {
118+
const char *name = elf_strptr(elf, shdr.sh_link, sym.st_name);
119+
if (sym_table) {
120+
add_func_symbol(sym_table, sym.st_value, name);
121+
}
122+
}
123+
}
124+
}
125+
}
126+
127+
elf_end(elf);
128+
close(fd);
129+
}

0 commit comments

Comments
 (0)