1 year ago

#371990

test-img

philquinn

Global variable symbols is incorrect when I debug a unix-like kernel wrote by myself

code is here at commit #489ee1c

I am writing a unix-like kernel following this tutorial for personal learning. Global variable symbols is incorrect when I debug a unix-like kernel wrote by myself.

I start the kernel using qemu-system-i386 -d cpu_reset -s -S -D ./run.log -drive format=raw,file=os_image -m 8G

there is also a problem that physical memory is only 3GB in code while I set -m 4G.

and start a gdb stoping at init_global_mm_vars() functions

.gdbinit

set arch i386
symbol-file /root/os/2-kernel/kernel.elf
b init_global_mm_vars
target remote localhost:1234

enter image description here enter image description here

You can see that the address of symbol Kernel_Vmm_End is 0x58d4 ,but used in asm is 0x68d4. all above global variable symbols is incorrect.

Why all the global variable symbols go wrong ?

I found that if I don't use link.ld script and just use -Ttext=0,when link and all problems seem gone.

ENTRY(kernel_main) /* Kernel entry label */
OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)

SECTIONS {
    . = 0x0; /* Kernel code is located at 0x0 */

    Kernel_Text_Vmm_Start_p = .; /* Export labels */

    .text : /* Align at 4KB and load at 4KB */
    {
        *(.text) /* All text sections from all files */
    }
    . = ALIGN(0x1000);
    Kernel_Rodata_Vmm_Start_p =.;
    .rodata ALIGN (0x1000)  : AT(ADDR(.rodata))  /* Align at 4KB and load at 4KB */
    {
        *(.rodata) /* All read-only data sections from all files */
    }
    . = ALIGN(0x1000);
    Kernel_Data_Vmm_Start_p =.;
    .data ALIGN (0x1000)  : AT(ADDR(.data)) /* Align at 4KB and load at 4KB */
    {
        *(.data) /* All data sections from all files */
    }
    . = ALIGN(0x1000);
    Kernel_Bss_Vmm_Start_p =.;
    .bss ALIGN (0x1000)  : AT(ADDR(.bss)) /* Align at 4KB and load at 4KB */
    {
        *(COMMON) /* All COMMON sections from all files */
        *(.bss) /* All bss sections from all files */
    }
    . = ALIGN(0x1000);
    Kernel_Vmm_End_p = .;
}

Still have no idea why this ld script goes wrong?

linux-kernel

operating-system

gdb

ld

debug-symbols

0 Answers

Your Answer

Accepted video resources