I'm writing a little kernel in assembler. I'm running it in QEMU and have some problems with some bugs. Now I want to debug the kernel with dbg. So I assembled it like so:
$ nasm -g -f elf -o myos.elf myos.asm
$ objcopy --only-keep-debug myos.elf myos.sym
$ objcopy -O binary myos.elf myos.bin
Then I run it in QEMU with:
$ qemu-system-i386 -s -S myos.bin
Then I connect with gdb:
$ gdb
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
0x0000fff0 in ?? ()
symbol-file myos.sym
Reading symbols from /home/sven/Projekte/myos/myos.sym...done.
I have a label named welcome
in my kernel that points to a string. While testing I tried to look at that string, which gave the following result:
(gdb) x/32b welcome
0x1e <welcome>: 0x00 0xf0 0xa5 0xfe 0x00 0xf0 0x87 0xe9
0x26: 0x00 0xf0 0x6e 0xc9 0x00 0xf0 0x6e 0xc9
0x2e: 0x00 0xf0 0x6e 0xc9 0x00 0xf0 0x6e 0xc9
0x36: 0x00 0xf0 0x57 0xef 0x00 0xf0 0x6e
The label is defined like this:
welcome: db "System started. Happy hacking!", 10, 0
So you can see, gdb is pretending welcome starts with a null byte but by definition it's not. However the kernel uses the label correctly, so it doesn't seem like a poblem with my code. Examining other parts of the memory doesn't match the loaded kernel at all.
Does anyone know why the memory of the virtual machine doesn't match the loaded kernel, while the machine still behaves corectly?
Explanation
qemu-system-i386
loads the first byte of an x86 boot sector image file at address0x7c00
at run time.myos.elf
,myos.sym
) mistakenly inform GDB that the code would be loaded at address 0. Thus GDB thinkswelcome
is at0x1e
while it's actually at0x7c1e
.0x7c00
to all addresses in GDB would work but is clumsy:x/32xb (welcome + 0x7c00)
Solution
boot.asm
x86-boot.ld
Build the code with:
dump-welcome.gdb
Sample session:
Thought Process
Most of the 32 bytes you dumped have values ≥ 0x80, i.e. they're not printable ASCII characters. This raises the question: Am I really dumping the right address?
The hex dump of your
welcome
message should be:Using GDB to search for the
welcome
message in memory would have revealed the right address as well:Further Reading