ld Error: PE operations on non PE file. HELP!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MadProgrammer

ld Error: PE operations on non PE file. HELP!

Post by MadProgrammer »

System: WinXP, mingw, ld, gcc

Ok

It is throwing this error, and I believe that I compiled the code correctly.
"ld: PE operations on non PE file."

Here is the compile command line:
g++ -c D:\osdev\kernel\*.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions

Here is the link command line:
ld -T D:\osdev\kernel\link.ld -o kernel.com D:\osdev\kernel\*.o

Here is the linker script:
OUTPUT_FORMAT("binary")
ENTRY(main)
SECTIONS
{
    .text 0x100000 :
    {
        code = .; _code = .; __code = .;
        *(.text)
        . = ALIGN(4096);
    }

    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        . = ALIGN(4096);
    }

    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}

The specified ENTRY is correct. Any ideas on what I am doing wrong?
Chris Giese

RE:ld Error: PE operations on non PE file. HELP!

Post by Chris Giese »

This is a limitation of MinGW. Admittedly, the error message is an improvement -- it used to just crash if you used any output format other than pei-i386. Maybe you can link to PE, then convert to binary using objcopy.

Or use this clever idea from Tim Robinson:
ld --file-alignment 4096 --section-alignment 4096 ...

Now you have a "binary" kernel with a 4096-byte PE header at the start, which you can ignore or skip over.
Post Reply