Linking Problem With GCC, LD, NASM. [Solved]

Programming, for all ages and all languages.
Post Reply
duggydiggy
Posts: 9
Joined: Wed Jul 01, 2009 1:15 pm

Linking Problem With GCC, LD, NASM. [Solved]

Post by duggydiggy »

Greets All,

I have been trying to write a very simple kernel and following Bran's Kernel Development Tutorial step by step.
I am done with

1.start.asm http://dpaste.com/100953/
2.link.ld http://dpaste.com/100954/
3.main.c http://dpaste.com/100965/
upto step three of the tutorial

Build steps-

nasm -f elf -o start.o start.asm //successful

gcc -Wall -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c //suucessful

gcc -T link.ld -o kernel.bin start.o //usr/lib/gcc/i686-pc-linux-gnu/4.1.2/libgcc_s.so: could not read symbols: File in wrong format
//collect:ld returned 1 exit status

What can be wrong?
and if I link using
ld -T link.ld -o kernel.bin start.o //ld: cannot find code

I google for these errors but in vain.
Please guide me.

Cheers.
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Re: Link not successful !

Post by meh »

Although I haven't been OSDeving for a long time and forgot some of it, I may be able to help a little. I think your problems may lie in your "link.ld" file. Try commenting your " OUTPUT_FORMAT("binary") " line and try changing your "ENTRY(mymain)" to "ENTRY(start)". Remember that I don't know much about this, but that may work.
duggydiggy
Posts: 9
Joined: Wed Jul 01, 2009 1:15 pm

Re: Link not successful !

Post by duggydiggy »

AusZero wrote:Although I haven't been OSDeving for a long time and forgot some of it, I may be able to help a little. I think your problems may lie in your "link.ld" file. Try commenting your " OUTPUT_FORMAT("binary") " line and try changing your "ENTRY(mymain)" to "ENTRY(start)". Remember that I don't know much about this, but that may work.
Edited link.ld

Code: Select all

ENTRY(main)
phys = 0x00100000;
SECTIONS
{
	.text phys : AT(phys)
	{
		code =.;
		*(.text)
		*(.rodata)
		. = ALIGN(4096);
	}
	.data : AT(phys + (data - code))
	{
		data = .;
		*(.data)
		. = ALIGN(4096);
	}
	.bss : AT(phys + (bss - code))
	{
		bss = .;
		*(.bss)
		. = ALIGN(4096);
	}
	end =.;
}

gcc -T link.ld -o kernel.bin start.o //usr/lib/gcc/i686-pc-linux-gnu/bin/ld: cannot find code
//collect:ld returned 1 exit status

What will be the OUPT_FORMAT then??
Still the same error !!

Cheers!!
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Re: Link not successful !

Post by meh »

In my operating system, I didn't have an output format in the "link.ld" file, and it worked without any problems. Also, the "ENTRY(main)" should be "ENTRY(start)".
duggydiggy
Posts: 9
Joined: Wed Jul 01, 2009 1:15 pm

Re: Link not successful !

Post by duggydiggy »

AusZero wrote:In my operating system, I didn't have an output format in the "link.ld" file, and it worked without any problems. Also, the "ENTRY(main)" should be "ENTRY(start)".
I am sorry ,that was silly !!
Well I changed it from main->start but still the same error while linking.

Code: Select all

gcc -T link.ld -o kernel.bin start.o //usr/lib/gcc/i686-pc-linux-gnu/bin/ld: cannot find code
//collect:ld returned 1 exit status
I don't understand why ld is not able to resolve extern code from start.asm,I think in link.ld I have defined the variable..Is there anything you can make out?

Cheers!!
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Re: Link not successful !

Post by meh »

That's all I could think of right now, but I do seem to remember having a problem similar to yours before. Maybe you should wait for someone else to help you, but if I can figure out what your problem is, I'll post it.
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Re: Link not successful !

Post by meh »

Are you linking your "start.o" and your "main.o" files together? In your first post, you're just linking your "start.o" file.
duggydiggy
Posts: 9
Joined: Wed Jul 01, 2009 1:15 pm

Re: Link not successful !

Post by duggydiggy »

somebody please reply :(

Cheers!!
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Link not successful !

Post by gravaera »

I'm not doing my kernel in C, however, I believe you're missing a few necessary command line options to GCC that ensure it doesn't try to link in libraries from the native OS.

Why not look around the wiki (I can't remember where exactly, but I do remember Solar listing a bunch of Comm. Line options for GCC in his makefile tutorial) and see which command line options apply for C?

I know for sure you're missing -ffreestanding, and that it's not purely a good practice to try passing your object file outputs through GCC to get them to the linker...why not just pass the objects to the linker itself?

And there's something wrong with the way yoo go about this whole thing:

First you assemble a relocatable object file, start.o; Then you compile main.o; Then you link (through GCC, which is not really standard practice) together start.o, and nothing else into kernel.bin.

What you should do is link together the relocatable object files to create an executable image like so:

Code: Select all

ld -T ./link.ld -o kernel.bin main.o start.o
If you replace the last line with the ...gcc -T... you should get a link, assuming you get the command line flags in order to stop LD from trying to link in libGCC.

I would recommend taking some time off to understand Linking and do some reading on Relocatable, Shared, and Executable object files. Wikipedia has some good info on that.

-Good luck
gravaera
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
inx
Member
Member
Posts: 142
Joined: Wed Mar 05, 2008 12:52 am

Re: Link not successful !

Post by inx »

The problem is most likely that libgcc_s.so is a shared library. You need to build a cross compiler to an "unknown-elf" (e.g. i386-unknown-elf) target so that you'll have a statically linkable libgcc (libgcc.a). Linking to libgcc_s.so will leave unresolved symbols that a dynamic linker would attach at load time, but your kernel is to be a freestanding executable, ergo no dynamic linker. That's why it's complaining about the library being in the wrong format: it is.

EDIT: Gravaera is correct, also. Two halves of a whole. ;)
duggydiggy
Posts: 9
Joined: Wed Jul 01, 2009 1:15 pm

Re: Linking Problem With GCC, LD, NASM. [Solved]

Post by duggydiggy »

gravaera wrote:I'm not doing my kernel in C, however, I believe you're missing a few necessary command line options to GCC that ensure it doesn't try to link in libraries from the native OS.

Why not look around the wiki (I can't remember where exactly, but I do remember Solar listing a bunch of Comm. Line options for GCC in his makefile tutorial) and see which command line options apply for C?

I know for sure you're missing -ffreestanding, and that it's not purely a good practice to try passing your object file outputs through GCC to get them to the linker...why not just pass the objects to the linker itself?

And there's something wrong with the way yoo go about this whole thing:

First you assemble a relocatable object file, start.o; Then you compile main.o; Then you link (through GCC, which is not really standard practice) together start.o, and nothing else into kernel.bin.

What you should do is link together the relocatable object files to create an executable image like so:

Code: Select all

ld -T ./link.ld -o kernel.bin main.o start.o
If you replace the last line with the ...gcc -T... you should get a link, assuming you get the command line flags in order to stop LD from trying to link in libGCC.

I would recommend taking some time off to understand Linking and do some reading on Relocatable, Shared, and Executable object files. Wikipedia has some good info on that.

-Good luck
gravaera
Thanks for your suggestion.
I will surely follow it.

Cheers!!
Post Reply