我正在运行 OS X 10.12,并且正在开发一个基于文本的基本操作系统。我开发了一个引导加载程序,看起来运行良好。我唯一的问题是,当我尝试将内核编译为纯二进制文件时,链接器将无法工作。我做了一些研究,我认为这是因为 OS X 运行 Darwin 链接器而不是 GNU 链接器。因此,我下载并安装了 GNU binutils。然而,它仍然行不通...
这是我的内核:
void main() {
// Create pointer to a character and point it to the first cell of video
// memory (i.e. the top-left)
char* video_memory = (char*) 0xb8000;
// At that address, put an x
*video_memory = 'x';
}
这是我尝试编译它的时候:
Hazims-MacBook-Pro:32 bit root# gcc -ffreestanding -c kernel.c -o kernel.o
Hazims-MacBook-Pro:32 bit root# ld -o kernel.bin -T text 0x1000 kernel.o --oformat binary
ld: unknown option: -T
Hazims-MacBook-Pro:32 bit root#
我很想知道如何解决这个问题。感谢您抽出时间。
最佳答案
-T
是 gcc 编译器标志,而不是链接器标志。看看这个:
With these components you can now actually build the final kernel. We use the compiler as the linker as it allows it greater control over the link process. Note that if your kernel is written in C++, you should use the C++ compiler instead.
You can then link your kernel using:
i686-elf-gcc -T linker.ld -o myos.bin -ffreestand -O2 -nostdlib boot.o kernel.o -lgcc
Note: Some tutorials suggest linking with i686-elf-ld rather than the compiler, however this prevents the compiler from performing various tasks during linking.
The file myos.bin is now your kernel (all other files are no longer needed). Note that we are linking against libgcc, which implements various runtime routines that your cross-compiler depends on. Leaving it out will give you problems in the future. If you did not build and install libgcc as part of your cross-compiler, you should go back now and build a cross-compiler with libgcc. The compiler depends on this library and will use it regardless of whether you provide it or not.
这全部直接取自 OSDev ,它非常清楚地记录了整个过程,包括一个简单的内核。
关于c - 如何使用 GNU 链接器而不是 Darwin 链接器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958878/