问题描述
因此,我正在尝试将C文件编译为.bin,然后在我的第一阶段Bootloader之后将其添加到.img文件。 我在answer用户Michael Petch中找到了这些bash命令:
gcc -g -m32 -c -ffreestanding -o kernel.o kernel.c -lgcc
ld -melf_i386 -Tlinker.ld -nostdlib --nmagic -o kernel.elf kernel.o
objcopy -O binary kernel.elf kernel.bin
并使用了此C代码(取自相同的答案,另存为kernel.c):
/* This code will be placed at the beginning of the object by the linker script */
__asm__ ("jmp _main\r\n");
int main(){
/* Do Stuff Here*/
return 0; /* return back to bootloader */
}
我在cygwin中执行了这些命令,并产生了以下结果:
ld: kernel.o: in function `main':
/cygdrive/d/Work/asm/kernel.c:4: undefined reference to `___main'
objcopy: 'kernel.elf': No such file
OUTPUT_FORMAT(elf32-i386)
ENTRY(_main)
SECTIONS
{
. = 0x9000;
.text : { *(.text.start) *(.text) }
.data : { *(.data) }
.bss : { *(.bss) *(COMMON) }
}
我已经使用objdump
分解了kernel.o文件,其结果在这里:
> objdump -d -j .text kernel.o
kernel.o: file format pe-i386
disassembly of section .text:
00000000 <.text>:
0: eb 00 jmp 2 <_main>
00000002 <_main>:
2: 55 push %ebp
3: 89 e5 mov %esp,%ebp
5: 83 e4 f0 and $0xfffffff0,%esp
8: e8 00 00 00 00 call d <_main+0xb>
d: b8 00 00 00 00 mov $0x0,%eax
12: c9 leave
13: c3 ret
这是gcc -v
的结果,如果有帮助的话:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/10/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-10.2.0/configure --srcdir=/mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-10.2.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --with-gcc-major-version-only --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC)
我在做什么错?这是由cygwin引起的吗?如果是,我还能在Windows上使用其他选项吗? (我尝试过MSVC,但这显然很可怕)
此外,我的引导程序未使用任何.section
伪操作(我不知道如何正确使用伪操作),这将来会引起任何问题,并且可以与已编译的C一起正常工作程序?
解决方法
通过更深入的搜索,可以很容易地发现__main
(内部带有下划线)是程序的实际入口点。
以下两个答案中提到了相同的问题:
两者都要求与-lgcc
选项和libgcc库建立某种形式的连接。
将main
重命名为__main
是可行的,但不建议这样做(内核的入口点显然是由惯例kmain
构成的,如在其他问答中所见)
__main
函数是操作系统在启动程序时调用的函数,它通常包含(例如)对exit()
的调用(如果返回类型为main
,则返回返回代码)是int)和其他一些底层系统调用(可能是特定于系统的,这里需要做更多的研究)
GCC希望您甚至在独立编译中都包含一个__main
函数,因为它是所有应用程序的默认入口点(或者说是我所见的)。