Linking Problem

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.
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Linking Problem

Post by Kieran »

I am having problems when linking my kernel.
I have an application that produces an LD linker script, but once i add a certain file it no longer produces working binary's or coff exe's.

Here is the linker script:

Code: Select all

INPUT("fdd.o")
INPUT("gdt.o")
INPUT("hdd.o")
INPUT("idt.o")
INPUT("irq.o")
INPUT("isrs.o")
INPUT("kbd.o")
INPUT("kprintf.o")
INPUT("main.o")
INPUT("mm.o")
INPUT("paging.o")
INPUT("start.o")
INPUT("timer.o")
INPUT("vga.o")
INPUT("video.o")
INPUT("../lib/libc.a")

OUTPUT_FORMAT("binary")

SECTIONS
{
    .text 0x100000 :	
    {
	code = .; _code = .; __code = .;
	*(.text)
	. = ALIGN(8192);
    }
    .data :
    {
	data = .; _data = .; __data = .;
	*(.data)
	. = ALIGN(8192);
    }
    .bss :
    {
	bss = .; _bss = .; __bss = .;
	*(.bss)
	*(COMMON) 
	. = ALIGN(8192);
    }
    end = .; _end = .; __end = .;
}

The file causing the problem is hdd.c. I am not getting any build warnings or errors from it. I have attached it...

If anyone has any ideas I would be grateful.

Thanks in advance, Kieran
Attachments
HDD.c
(3 KiB) Downloaded 58 times
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

Very weird problem. :roll:

Why don't you try replacing all input files in linker script:

Code: Select all

INPUT("fdd.o")
INPUT("gdt.o")
INPUT("hdd.o")
INPUT("idt.o")
INPUT("irq.o")
INPUT("isrs.o")
INPUT("kbd.o")
INPUT("kprintf.o")
INPUT("main.o")
INPUT("mm.o")
INPUT("paging.o")
INPUT("start.o")
INPUT("timer.o")
INPUT("vga.o")
INPUT("video.o")
INPUT("../lib/libc.a")
with this line in the makefile or batch maker
ld -T link.ld -o kernel.bin *.o ../lib/libc.a

Cheers,
Gonzalo
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Why are you trying to link with libc.a?
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

Hmm, because he might have printf, itoa, and all string functions in libc.a ? :roll:
User avatar
jerryleecooper
Member
Member
Posts: 233
Joined: Mon Aug 06, 2007 6:32 pm
Location: Canada

Post by jerryleecooper »

I don't know a lot about linker scripts, but isn't outputting as "BINARY" format requires that the first function to be called the first to be linked?
And what is it with "coff"?
Do you use optimization? It can cause problems with -ffreestanding in a binary file format.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

gzaloprgm wrote:Hmm, because he might have printf, itoa, and all string functions in libc.a ? :roll:
In a kernel? It's making life harder, imho, to have a libc.a for a kernel.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

pcmattman wrote:
gzaloprgm wrote:Hmm, because he might have printf, itoa, and all string functions in libc.a ? :roll:
In a kernel? It's making life harder, imho, to have a libc.a for a kernel.
To be fair pcmattman, you don't know the purpose of that library for his specific project.

He could be developing a C library for both kernel-land and user-land functions, The BSD's do a similar thing with a few #ifdefs..

I don't see the problem... unless he's linking with his host OS's C library.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Linking Problem

Post by pcmattman »

Brynet-Inc:
Kieran wrote:I am having problems when linking my kernel.
OP: Could you please clarify what that libc.a is? Is it your host OS one or one you have made?

Also, could you please tell us what your compiler and linker command lines are? (And your batch/make files if you have any).
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

libc.a is a library of common functions made by me...
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

In your linker script the object file is spelled "hdd.o", but your attached file is "HDD.c". Check your capitalisations.
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

sorry James, that isn't the problem. All of my files are saved in upper case and they all compile in lower case from the linker script.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

Your linker script doesn't have an .rodata section. Does it help to add the line

Code: Select all

*(.rodata*)
on the next line after *(.text) in the linker script? (GCC stores strings in this section.)
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

I have tried adding the .rodata section into the linker script with no avail...
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post by Wave »

You don't specify an entry point. It probably starts somewhere you don't want it to.
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

Are you using GRUB? If so, the multiboot header has to be in the first 8KB of the kernel. You could try moving INPUT("start.o") to the very top of the linker script.
Post Reply