Skip to content

Commit 34904cc

Browse files
KASLR: add compile-time option to disable randomization
A kernel built with KASLR disabled is more debug-friendly. Edit the gdb initialization files so that .gdbinit contains only commands common to all architectures, while the .gdbinit-* files contain architecture-specific commands (where the kernel symbol offset is set to the value used when KASLR is disabled) and are meant to be used via the `-ix` gdb command line option.
1 parent cce77a6 commit 34904cc

5 files changed

Lines changed: 10 additions & 9 deletions

File tree

.gdbinit

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
set architecture i386:x86-64
2-
31
macro define offsetof(t, f) (size_t)&((t *)0)->f
42
macro define container_of(p, t, f) (t *)((void *)p - offsetof(t, f))
53

64
source tools/nanos_gdb.py
7-
display/i $pc
8-
symbol-file ./output/platform/pc/bin/kernel.elf
9-
target remote :1234
105

.gdbinit-aarch64

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
set architecture aarch64
2-
display/i $pc
2+
file ./output/platform/virt/bin/kernel.elf -o 0xffffffff3fc00000
33
target remote :1234
4-
symbol-file ./output/platform/virt/bin/kernel.elf
54

.gdbinit-riscv64

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
set architecture riscv:rv64
2-
display/i $pc
2+
file ./output/platform/riscv-virt/bin/kernel.elf -o 0xfffffffeffe00000
33
target remote :1234
4-
symbol-file ./output/platform/riscv-virt/bin/kernel.elf
54

.gdbinit-x86-64

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set architecture i386:x86-64
2+
file ./output/platform/pc/bin/kernel.elf -o 0xffffffff7fe00000
3+
target remote :1234
4+

src/kernel/init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ BSS_RO_AFTER_INIT static vector shutdown_completions;
6363
void kaslr(void)
6464
{
6565
extern u8 START, text_end, READONLY_END, bss_start, END;
66+
#ifndef NO_KASLR
6667
u64 ksize = pad(&END - &START, PAGESIZE);
6768
u64 random_offset = random_early_u64() % (KERNEL_LIMIT - KERNEL_BASE - ksize);
69+
#else
70+
u64 random_offset = 0;
71+
#endif
6872
u64 kbase = KERNEL_BASE + (random_offset & ~PAGEMASK);
6973
u64 phys_offset = kernel_phys_offset;
7074
u64 kern_offset = kbase - u64_from_pointer(&START) + phys_offset;

0 commit comments

Comments
 (0)