我一直在使用这教程编写一个操作系统。我所处的部分是引导加载程序完成,C用于编程(然后链接在一起.)。不过,我相信我所遇到的问题与gcc有关。
我为操作系统构建了一个i386-elf交叉编译器。一切正常,我可以执行我的代码--一切正常。除了所有全局变量都初始化为零外,虽然提供了默认值。
int test_var = 1234;
// yes, void main() is correct (the boot-loader will call this)
void main() {} 如果我用GDB调试这段代码,我会得到:(gcc-7.1.0, target: i328-elf)
(gdb) b main
Breakpoint 1 at 0x1554: file src/kernel/main.c, line 11.
(gdb) c
Continuing.
Breakpoint 1, main () at src/kernel/main.c:11
11 void main() {
(gdb) p test_var
$1 = 0如果我在本地机器(gcc-6.3.0, target: x86_64)上运行相同的代码,它会打印1234。
我的问题是:我是不是配置错了gcc,这是我的操作系统中的一个错误,这是一个已知的问题吗?我什么也找不到。
我的整个源代码:链接我使用以下命令编译我的内容:
# ...
i386-elf-gcc -g -ffreestanding -Iinclude/ -c src/kernel/main.c -o out/kernel/main.o
# ...
i386-elf-ld -e 0x1000 -Ttext 0x1000 -o out/kernel.elf out/kernel_entry.o out/kernel/main.o # some other stuff ...
i386-elf-objcopy -O binary out/kernel.elf out/kernel.bin
cat out/boot.bin out/kernel.bin > out/os.bin
qemu-system-i386 -drive "format=raw,file=out/os.bin"编辑: As @EugeneSh。这里提出了一些逻辑,以确保它没有被删除:
#include <cpu/types.h>
#include <cpu/isr.h>
#include <kernel/print.h>
#include <driver/vga.h>
int test_var = 1234;
void main() {
vga_text_init();
switch (test_var) {
case 1234: print("That's correct"); break;
case 0: print("It's zero"); break;
// I don't have a method like atoi() in place, I would use
// GDB to get the value
default: print("It's something else");
}
}遗憾的是它打印了It's zero
发布于 2017-08-17 15:20:57
编译器从不将未初始化的全局变量清除为零,它的逻辑在加载程序内部构建,因此当您为数据段分配内存时,它的大小也包含bss部分。因此,您必须检查bss部分的偏移、对齐和大小与数据段以及memset()它们之间的“0”。
在编写操作系统时,所有的库例程都是不可用的,所以最好使用程序集编写memset()函数。
https://stackoverflow.com/questions/45286875
复制相似问题