Problem with the gnu linker (ld.exe)

Programming, for all ages and all languages.
alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

Problem with the gnu linker (ld.exe)

Post 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)
Attachments
kernel.c
the c-kernel
(215 Bytes) Downloaded 216 times
boot.asm
the bootloader that jumps to the c kernel.
(980 Bytes) Downloaded 263 times
sdkmvx
Posts: 1
Joined: Mon Dec 01, 2008 5:39 pm

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

Post by sdkmvx »

alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

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

Post 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.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

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

Post by CodeCat »

I'm not sure the instructions in the wiki could really be any clearer. I followed them and it worked perfectly.
alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

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

Post 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...
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

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

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

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

Post 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
alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

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

Post 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 ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

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

Post by JamesM »

Just type "gcc". If you followed my instructions, the cross compiler GCC should be in your path before cygwin's standard one.
alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

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

Post 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 ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

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

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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.
Every good solution is obvious once you've found it.
alphabetagamma
Posts: 15
Joined: Mon Dec 01, 2008 3:13 pm

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

Post 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
Attachments
kernel2.zip
(1.88 KiB) Downloaded 201 times
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

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

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post by Solar »

g1o2k4 wrote:sag enough that there are no open source ides for that...
You can use any IDE you like...?!?
Every good solution is obvious once you've found it.
Post Reply