ld:对非PE输出文件错误无法执行PE操作 [英] ld: cannot perform PE operations on non PE output file error
问题描述
我是操作系统编程的新手,我正在阅读一本书,内容如下:
main(){
char * video_memory = 0xb8000;
* video_memory ='X';
}
为了编译这个名为kernel.c的文件,我在Windows下使用MinGW 7如下:
gcc -freestanding -c kernel.c -o kernel.o
这会创建目标文件kernel.o。但是,以下命令不起作用。
ld -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
我收到以下错误:
ld:无法对非PE输出文件'kernel.bin'执行PE操作
<我无法解决问题。请帮助我。
谢谢
在Ross的帮助下,我成功跳转到内核偏移。但是,我无法从Kernel_entry.asm调用C函数。此外,当我从我的kernel.bin中删除C函数并更改代码时,屏幕上会显示三个奇怪的字符。
Kernel_entry.asm是如下:
[bits 32]
; [extern _start]
mov ebx,MSG_KERNEL_ENTRY
调用print_string_pm
;调用_start
jmp
%includeprint_string_pm.asm
MSG_KERNEL_ENTRY db调用内核入口,0
bootsec.asm如下:
[org 0x7c00]
KERNEL_OFFSET equ 0x1000
mov [BOOT_DRIVE],dl
mov bp,0x9000
mov sp,bp
mov bx,MSG_REAL_MODE
调用print_string
调用load_kernel
调用switch_to_pm
jmp
%include print_string.asm
%includedisk_load.asm
%includegdt.asm
%includeprint_string_pm.asm
%includeswitch_to_pm.asm
%包含clear_screen.asm
[位16]
load_kernel:
mov bx,MSG_LOAD_ KERNEL
调用print_string
mov bx,KERNEL_OFFSET
mov dh,15
mov dl,[BOOT_DRIVE]
调用disk_load
ret
[bits 32]
BEGIN_PM:
;调用clear_screen
mov ebx,MSG_PROT_MODE
调用print_string_pm
调用KERNEL_OFFSET
jmp $
BOOT_DRIVE db 0
MSG_REAL_MODE db16位实模式启动,0
MSG_PROT_MODE db成功切换到32位保护模式,0
MSG_LOAD_KERNEL db将内核加载到内存中,0
次510 - ($ - $$)db 0
dw 0xaa55
所有消息均正确显示,但Kernel_entry.asm中的最后一个显示为三个奇怪的字符。我不明白会发生什么。
您需要做的第一件事是更改函数的名称。如果您将它称为 main
,MinGW版本的GCC将插入对 __ main
的调用来执行初始化。例如:
start(){
char * video_memory = 0xb8000;
* video_memory ='X';
$ / code>
这意味着您还必须编辑 kernel_entry .asm
据此:
[bits 32]
[extern _start]
调用_start
jmp $
接下来编译并组装这两个文件,并链接到这些命令:
ld -T NUL -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.tmp kernel.bin
第一个命令链接将对象作为PECOFF可执行文件,然后第二个命令将其转换为二进制文件。现在将二进制文件与引导加载程序结合起来:
copy / b boot_sect.bin + kernel.bin os-image
I am new to operating system programming and I am reading a book which gives a simple example of kernel as follows:
main() {
char *video_memory = 0xb8000;
*video_memory = 'X';
}
To compile this file which is called kernel.c, I use MinGW under Windows 7 as follows:
gcc -ffreestanding -c kernel.c -o kernel.o
This creates the object file kernel.o. However, following command does not work.
ld -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
I receive the following error:
ld: cannot perform PE operations on non PE output file 'kernel.bin'
I cannot solve the problem. Please help me.
Thank you
After helps of Ross, I succeded to jump to kernel offset. However, I cannot call C function from Kernel_entry.asm. Moreover, when I remove the C function from my kernel.bin and change the code as seen below, Three weird chars are shown on the screen.
Kernel_entry.asm is as follows:
[bits 32]
;[extern _start]
mov ebx, MSG_KERNEL_ENTRY
call print_string_pm
;call _start
jmp $
%include "print_string_pm.asm"
MSG_KERNEL_ENTRY db "Kernel entry is invoked", 0
bootsec.asm is as follows:
[org 0x7c00]
KERNEL_OFFSET equ 0x1000
mov [BOOT_DRIVE], dl
mov bp, 0x9000
mov sp, bp
mov bx, MSG_REAL_MODE
call print_string
call load_kernel
call switch_to_pm
jmp $
%include "print_string.asm"
%include "disk_load.asm"
%include "gdt.asm"
%include "print_string_pm.asm"
%include "switch_to_pm.asm"
%include "clear_screen.asm"
[bits 16]
load_kernel:
mov bx, MSG_LOAD_KERNEL
call print_string
mov bx, KERNEL_OFFSET
mov dh, 15
mov dl, [BOOT_DRIVE]
call disk_load
ret
[bits 32]
BEGIN_PM:
;call clear_screen
mov ebx, MSG_PROT_MODE
call print_string_pm
call KERNEL_OFFSET
jmp $
BOOT_DRIVE db 0
MSG_REAL_MODE db "Started in 16-Bit Real Mode", 0
MSG_PROT_MODE db "Successfully switched to 32-Bit Protected Mode", 0
MSG_LOAD_KERNEL db "Loading Kernel into memory", 0
times 510 - ($ - $$) db 0
dw 0xaa55
All messages are shown correctly but the last one in the Kernel_entry.asm is shown as three weird chars. I don't understand what happens.
The first thing you'll need to is change the name of you function. If you call it main
the MinGW version of GCC will insert a call to __main
to do initialization. For example:
start() {
char *video_memory = 0xb8000;
*video_memory = 'X';
}
This means you'll also have to edit kernel_entry.asm
accordingly:
[bits 32]
[extern _start]
call _start
jmp $
Next compile and assemble the two files as you did before, and link it with these commands:
ld -T NUL -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.tmp kernel.bin
The first command links the objects as a PECOFF executable, and then the second command converts it to a binary. Now combine the binary with the boot loader:
copy /b boot_sect.bin+kernel.bin os-image
这篇关于ld:对非PE输出文件错误无法执行PE操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!