Page 1 of 2

Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 3:32 pm
by alphabetagamma
hi

i found a tutorial to write a bootloader in asm that loads a kernel written in c.

Code: Select all

nasm –f bin –o boot.bin boot.asm
i compiled the bootloader into a binary file and the c source into an obj file.

Code: Select all

gcc -ffreestanding -c -o kernel.o kernel.c
now i want to link the obj and the bin file with the gnu linker. i use the following linker script:

Code: Select all

OUTPUT_FORMAT("binary")
INPUT(kernel.o)
ENTRY(_main)
SECTIONS
{
  .text 0x40000 :
  {
    *(.text)
  }
  .data :
  {
    *(.data)
  }
  .bss :
  {
    *(.bss)
  }
}
i use the following command:

Code: Select all

ld -T link.ld -o kernel.bin
now i get the following error:
ld: PE operations on non PE file.
when i try to link it without the linker script i get the following error:
ld kernel.o -o kernel.bin
kernel.o:kernel.c:(.text+0x12): undefined reference to '__main'
what do i have to do to get this running ? i know i need a binary file of the c-code and then copy boot.bin and kernel.bin together.
the code is in the addition.

system information:
gcc 4.3
nasm-2.05.01
windows vista x64 (yeah i know :P)

Re: Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 5:40 pm
by sdkmvx

Re: Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 6:32 pm
by alphabetagamma
thanks for the reply.
i understand the problem and the solution but i have never worked with cygwin, gcc and bash commands like that.

it would be nice of someone who knows how to configure this cross compiler under windows could show it to me by doing it over remote access. so that i can watch the steps ? write a pm to me if you have time to do it.

Re: Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 6:59 pm
by CodeCat
I'm not sure the instructions in the wiki could really be any clearer. I followed them and it worked perfectly.

Re: Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 7:29 pm
by alphabetagamma
i dont understand the following step:
binutils

cd /usr/src/build-binutils
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
make all
make install
what is binutils-x.xx ? the cygwin bash says no directory or file found...
is it the gcc directory ? i downloaded the following package:
http://ftp.gnu.org/gnu/gcc/gcc-4.3.2/gc ... 3.2.tar.gz
but there ist no binutils directory in it...

Re: Problem with the gnu linker (ld.exe)

Posted: Mon Dec 01, 2008 8:56 pm
by leledumbo
It's a separate package containing assembler, linker, and many other utilities, take 'em here. Your ld is compiled ONLY with capability to handle PE files.

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 3:59 am
by JamesM
Hi,

As part of my tutorial rewrite (still ongoing before anyone asks) I've created an i686-elf cross compiler for cygwin. You can find it here. Just untar it;

Code: Select all

tar -xvzf i686-xcompiler-cygwin.tar.gz
to a suitable location and add that location to your PATH:

Code: Select all

export PATH=/my/location/where/i/extracted/the/files/bin:$PATH
Note the /bin on the end of the pathname.

Hope this helps,

James

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 4:43 am
by alphabetagamma
JamesM wrote:Hi,

As part of my tutorial rewrite (still ongoing before anyone asks) I've created an i686-elf cross compiler for cygwin. You can find it here. Just untar it;

Code: Select all

tar -xvzf i686-xcompiler-cygwin.tar.gz
to a suitable location and add that location to your PATH:

Code: Select all

export PATH=/my/location/where/i/extracted/the/files/bin:$PATH
Note the /bin on the end of the pathname.

Hope this helps,

James
thanks easier then the other tutorial, but how can i run the gcc.exe in cygwin now ? and where do i have to copy my source files to ?

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 5:48 am
by JamesM
Just type "gcc". If you followed my instructions, the cross compiler GCC should be in your path before cygwin's standard one.

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 6:13 am
by alphabetagamma
JamesM wrote:Just type "gcc". If you followed my instructions, the cross compiler GCC should be in your path before cygwin's standard one.
ok, needed to restart cygwin to initialize the path...
but a new problem came up:
i have to compile the assembler file with nasmw the following way

Code: Select all

nasmw -f aout /path/k_entry.asm -o /path/k_entry.o
error:
nasm: fatal: unable to open output file '/path/k_entry.o'
the file exists...so whats the problem here ?

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 6:18 am
by JamesM
Hi,

I'm pretty sure the directory "/path" doesn't actually exist. If you're in the directory where your asm files are, you just write:

Code: Select all

nasmw -f aout k_entry.asm -o k_entry.o
By the way, I'd use "-f elf", instead of "-f aout" if you want full compatibility with your GCC toolchain.

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 6:20 am
by Solar
g1o2k4 wrote:nasm: fatal: unable to open output file '/path/k_entry.o'
the file exists...so whats the problem here ?
Think for a minute: "Unable to open output file", and "file exists"... Double-check if the file really exists ("ls -l /path/k_entry.o"), then check the file access permissions on the file. Try "rm /path/k_entry.o" (which should delete the target file). Can you do that? Do you still get the error after deleting the target file?

All in all I'd recommend trying some user-space programming first, to familarize yourself with the tools. Write some simple test programs - asking the user for his name, reading input, and printing a greeting - stuff like that.

Kernel space programming is hard enough when you know your tools. Using unfamiliar tools in an unfamiliar environment for doing unfamiliar kernel-space work is a bit much at once.

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 6:35 am
by alphabetagamma
Solar wrote:Kernel space programming is hard enough when you know your tools. Using unfamiliar tools in an unfamiliar environment for doing unfamiliar kernel-space work is a bit much at once.
exactly! sag enough that there are no open source ides for that...i just want to go to the step to know how to compile it and then just using c.

back to topic: a new problem. the linker still crashes:

Code: Select all

ld -T link.ld kernel.bin
the linker script is that from above.
error:
ld: cannot perform PE operation on non PE output file 'kernel.bin'


i also tried another source package for a c kernel (addition).
there the linker says:

Code: Select all

ld _t kernel.ld kernel.o k_entry.o
k_entry: file not recognized: File format not recognized

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 6:39 am
by JamesM
Hi,

Please can you paste the output of these three commands:

Code: Select all

ld -v
file k_entry.o
file kernel.o
Thanks,

James

Re: Problem with the gnu linker (ld.exe)

Posted: Tue Dec 02, 2008 7:26 am
by Solar
g1o2k4 wrote:sag enough that there are no open source ides for that...
You can use any IDE you like...?!?