首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Windows上将C语言编译成ELF32

在Windows上将C语言编译成ELF32
EN

Stack Overflow用户
提问于 2021-01-16 17:06:17
回答 1查看 603关注 0票数 1

我试图在Windows上将C程序编译成ELF格式,因此我尝试做以下几件事:

output

  • Downloaded

  • 用MinGW gcc -Wall -c test.c -o test.o编译,但没有作为MinGW https://github.com/nativeos/i386-elf-toolchain/releases ( 32位版本)进行测试,而是用"[...]/i386-elf-gcc" -c test.c -o test.o编译,但我得到了错误i386-elf-gcc/libexec/gcc/i386-elf/5.2.0/cc1.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory

我要做的是在这里学习本教程:https://github.com/cfenollosa/os-tutorial,但我只能在汇编和C之间建立实际的链接。我很清楚,本教程是为Linux而不是Windows编写的,但我只是.这么说吧,我在使用Linux时遇到了麻烦。

我不知道需要哪些文件来解决我的问题,下面是:

  1. kernel.c

代码语言:javascript
运行
复制
void my_function() {
}

int main() {
    return 0;
}

  1. kernel_entry.asm

代码语言:javascript
运行
复制
[bits 32]
[extern main]
call main
jmp $

  1. boot.asm

代码语言:javascript
运行
复制
[org 0x7c00]

kernel_offset equ 0x1000

    mov [bootDrive], dl

    mov bp, 0x9000
    mov sp, bp

    call switch_to_pm

    call BEGIN_PM

    jmp $

%include "gdt_init.asm"
%include "switch_32.asm"

bootDrive db 0

[bits 32]
BEGIN_PM:
    call clear_screen
    call kernel_offset

    mov ebx, errorMSG
    call print_string_pm

    jmp $

%include "io/clear.asm"
%include "io/print.asm"


errorMSG db "Something went terribly wrong. Restart the PC to fix it", 0

; bootsector
times 510-($-$$) db 0
dw 0xaa55

其他文件几乎是复制粘贴,所以我不认为有必要包含它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-16 23:47:44

所以,这有点复杂,所以系好安全带。

首先,我使用了NASM和MinGW。下面列出了我的以下操作:

gcc -m32 -c kernel.c -o kernel.o -ffreestanding -nostdlib -nostdinc

  • Linked kernel_entry.o编译kernel.c,用ld -m i386pe -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o

  • Transformed kernel.tmp编译kernel.tmp到bin与objcopy -O binary -j .text kernel.tmp kernel.bin

  • Merged共同引导,用type bootsect.o kernel.bin > drive.bin

编译kernel.bin之后,

  1. 将bootsector.asm组装成普通的 kernel_entry.asm和nasm kernel_entry.asm -f elf32 -o kernel_entry.o
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65752272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档