reorganize, add sym_lookup

This commit is contained in:
trimill 2024-01-31 21:19:22 -05:00
parent 690210c944
commit 7ce0cd6056
No known key found for this signature in database
GPG key ID: 4F77A16E17E10BCB
6 changed files with 112 additions and 46 deletions

View file

@ -8,9 +8,11 @@ ISO_NAME=os_image.iso
CC=cc CC=cc
LD=ld LD=ld
CFLAGS=-std=c2x -ffreestanding -fno-stack-protector -g -Wall -Wextra -pedantic -lgcc -isystem $(INCLUDE_DIR) CFLAGS+=-std=c2x -ffreestanding -fno-stack-protector -g -Wall -Wextra -pedantic -lgcc -isystem $(INCLUDE_DIR)
CFLAGS+=-DPAGE_SIZE=4096 CFLAGS+=-DPAGE_SIZE=4096
LDFLAGS+=-nmagic -T arch/amd64/linker.ld
C_SRC=$(shell find $(SRC_DIR) -type f -name "*.c") C_SRC=$(shell find $(SRC_DIR) -type f -name "*.c")
C_OBJ=$(patsubst %.c,$(BUILD_DIR)/%.o,$(C_SRC)) C_OBJ=$(patsubst %.c,$(BUILD_DIR)/%.o,$(C_SRC))
@ -37,12 +39,12 @@ $(C_OBJ): $(BUILD_DIR)/%.o : %.c
$(BUILD_DIR)/$(K_BIN_NAME): $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC) $(H_INCLUDE) $(BUILD_DIR)/$(K_BIN_NAME): $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC) $(H_INCLUDE)
@mkdir -p $(@D) @mkdir -p $(@D)
$(LD) -nmagic -o $(BUILD_DIR)/$(K_BIN_NAME) -T $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ) $(LD) $(LDFLAGS) -o $(BUILD_DIR)/$(K_BIN_NAME) $(A_OBJ) $(C_OBJ)
$(BUILD_DIR)/$(ISO_NAME): $(BUILD_DIR)/$(K_BIN_NAME) grub.cfg $(BUILD_DIR)/$(ISO_NAME): $(BUILD_DIR)/$(K_BIN_NAME) grub.cfg
@mkdir -p $(@D) @mkdir -p $(@D)
@mkdir -p $(BUILD_DIR)/iso/boot/grub @mkdir -p $(BUILD_DIR)/iso/boot/grub
cp grub.cfg $(BUILD_DIR)/iso/boot/grub cp arch/amd64/grub.cfg $(BUILD_DIR)/iso/boot/grub
cp $(BUILD_DIR)/$(K_BIN_NAME) $(BUILD_DIR)/iso/boot cp $(BUILD_DIR)/$(K_BIN_NAME) $(BUILD_DIR)/iso/boot
grub-mkrescue -o $(BUILD_DIR)/$(ISO_NAME) $(BUILD_DIR)/iso grub-mkrescue -o $(BUILD_DIR)/$(ISO_NAME) $(BUILD_DIR)/iso

38
arch/amd64/linker.ld Normal file
View file

@ -0,0 +1,38 @@
ENTRY(start)
SECTIONS {
. = 1M;
kernel_start = .;
.boot BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
text_start = .;
.text BLOCK(4K) : ALIGN(4K)
{
*(.text)
}
text_end = .;
.bss BLOCK(4K) : ALIGN(4K)
{
*(.bss)
}
kernel_end = .;
}

68
arch/amd64/sym_lookup.py Executable file
View file

@ -0,0 +1,68 @@
#!/usr/bin/env python3
import subprocess
import bisect
import sys
import dataclasses
@dataclasses.dataclass
class Symbol:
addr: int
size: int
ty: str
scope: str
name: str
readelf = subprocess.run(
["readelf", "-s", "build/kernel.bin"],
capture_output=True,
encoding="UTF-8"
)
symbols = []
text_start = None
text_end = None
for line in readelf.stdout.split("\n")[3:-1]:
parts = list(line.split());
sym = Symbol(
addr=int(parts[1], 16),
size=int(parts[2]),
ty=parts[3],
scope=parts[4],
name=parts[7] if len(parts) >= 8 else "",
)
symbols.append(sym)
if sym.name == "text_start":
text_start = sym.addr
if sym.name == "text_end":
text_end = sym.addr
symbols.sort(key=lambda s: s.addr)
def find_sym(addr):
try:
if addr.startswith("0x") or addr.startswith("0X"):
addr = int(arg[2:], 16)
else:
addr = int(arg, 10)
except:
print(f"Invalid address: {addr}")
return
print(f"Address 0x{addr:016X}:")
idx = bisect.bisect_right(symbols, addr, key=lambda s: s.addr)
for i in range(idx-1, 0, -1):
sym = symbols[i]
if sym.addr < text_start:
break
if sym.addr > text_end:
break
print(f"\t{sym.name:<20} [0x{sym.addr:016X} : 0x{sym.size:04X}] {sym.ty:<7} {sym.scope:<7}")
if sym.ty == "FUNC":
return
print("\t<end of text>")
for arg in sys.argv[1:]:
find_sym(arg.strip())

View file

@ -6,13 +6,9 @@
// in order from top to bottom. returns the number filled (at most len) // in order from top to bottom. returns the number filled (at most len)
size_t backtrace(void **dst, size_t len); size_t backtrace(void **dst, size_t len);
// same as backtrace but with specified instruction and base pointer // same as backtrace but with specified instruction and base pointer
size_t backtrace_ex(void **dst, size_t len, void* ip, void *bp); size_t backtrace_ex(void **dst, size_t len, void* ip, void *bp);
// TODO symbols
//size_t backtrace_symbols(char *const *dst, size_t len);
// Log a backtrace // Log a backtrace
void log_backtrace(); void log_backtrace();

View file

@ -1,38 +0,0 @@
ENTRY(start)
PHDRS {
loadable PT_LOAD FLAGS(7) ;
}
SECTIONS {
. = 1M;
kernel_start = .;
.boot BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.text BLOCK(4K) : ALIGN(4K)
{
*(.text)
}
.bss BLOCK(4K) : ALIGN(4K)
{
*(.bss)
}
.symtab : {
symtab = .;
*(.symtab)
} :loadable
kernel_end = .;
}