LD Linker - too manu arguments?

Programming, for all ages and all languages.
Post Reply
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

LD Linker - too manu arguments?

Post by mangaluve »

Im using DJGPP/Nasm in Windows. To link my project, I use

Code: Select all

ld -T link.ld -o kernelcode.bin file1.o file2.o
and so on, where link.ld is

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
  .text 0x1000 :
  {
    *(.text)
  }
  .data :
  {
    *(.data)
  }
  .bss :
  {
    *(.bss)
  }
}
However, as I get more files, it seems like I cannot give more than 9 files as argument to ld. How can I solve this so I can link an arbitrary number of o-files?
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: LD Linker - too manu arguments?

Post by Creature »

You can try putting the object files you want to link inside the linker script instead of passing them to ld on the command line.

Add this below the SECTIONS for example:

Code: Select all

INPUT
(		
ObjFile1.o
ObjFile2.o
ObjFile3.o
ObjFile4.o
ObjFile5.o
/* ... */
)
I do it this way and have more than 9 object files and it works perfectly, let me know if this works.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: LD Linker - too manu arguments?

Post by Combuster »

The real problem is DOS - it has a maximum command line size of about 255 characters, and therefore, so does DJGPP.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: LD Linker - too manu arguments?

Post by AJ »

Hi,

If you have that many source files, I would strongly suggest considering a make file. You'll be glad of it once your project expands more. See the Makefile tutorial on the wiki for more information.

As an alternative (that's not so flexible), you could always consider using file patterns for linking:

Code: Select all

ld -T link.ld -o kernelcode.bin *.o
If you are relying on a flat binary format, you may have to ensure that you link the file with your entry point first, so that the entry point is at the start of the file. For ELF, this won't matter because the file format itself defines an entry point.

Cheers,
Adam
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: LD Linker - too manu arguments?

Post by mangaluve »

Thanks for the replies! I tried the last suggestion, linking *.o, and it works perfectly, although I think Im gonna look into make files!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: LD Linker - too manu arguments?

Post by Solar »

Unfortunately, all that 'make' does is passing the commands to the shell (i.e., the DOS box), so you'd be back to square one (unless I'm severely mistaken).

What you can do is using 'ar' to build a linker library, effectively collecting all the *.o files in one *.a file, one by one. (Don't know the exact command line options to do this, but it's possible.) The resulting *.a file can be passed to the linker as a single argument, and the linker will take from that linker library whatever *.o files it needs.
Every good solution is obvious once you've found it.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: LD Linker - too manu arguments?

Post by AJ »

Cygwin, anyone? :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: LD Linker - too manu arguments?

Post by Solar »

*cough*

Second day of staying at home, caring for the wife (flu) and catering for the kids, and already my brain is guacamole mush. :-D

Yes, of course Cygwin is the way to go.
Every good solution is obvious once you've found it.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: LD Linker - too manu arguments?

Post by AJ »

Solar wrote:Second day of staying at home, caring for the wife (flu) and catering for the kids, and already my brain is guacamole mush. :-D
You have my sympathy! I went through all that that last week with my wife and boy. Then, to add insult to injury, you catch the damn thing yourself :(
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: LD Linker - too manu arguments?

Post by salil_bhagurkar »

Alternatively:

Code: Select all

ld -T link.ld -o kernelcode.bin @files.txt
and files.txt can contain "file1.o file2.o"
Post Reply