Page 1 of 1

Including LD Custom Library

Posted: Thu Aug 14, 2008 9:45 am
by AJ
Hi,

I know I've had a few toolchain issues before and I know sometimes they've been daft mistakes, but I'm afraid I'm stuck again!

I've developped a library (libos.a) which contains some useful classes and functions for the freestanding environment. This includes a string class, for example. The idea behind the (static) library is that I can use the functions in both my OS and my second stage loader with minimal effort.

The problem is this: Until this point, I have been linking with the following command line in my makefile:

Code: Select all

LDFLAGS:=-T $(SRCBASE)link.ld -nostdinc -nostdlib -o
...
@$(LD) $(LDFLAGS) $(BUILD)$(BINARY) $(OBJ) $(LIBPATH)$(LIB)
Sorry about all those makefile variables, but hopefully anyone familiar with makefiles gets the gist of it! This, of course, links the entire libos.a whether the functions are used or not. So, I tried the following:

Code: Select all

LDFLAGS:=-T $(SRCBASE)link.ld -nostdinc -nostdlib -static -L$(LIBPATH) -l$(LIB) -o
...
@$(LD) $(LDFLAGS) $(BUILD)$(BINARY) $(OBJ)
Which instead uses the libos.a file as a traditional library - only those required functions (Solar's one function per file style) are included in the final binary = smaller binaries! The only problem is that with this second method, all symbols within the library are unresolved. I know that LD is finding my library, because changing -l$(LIB) to -lrandomfile causes LD to say "cannot find -lrandomfile". LIBPATH and LIB makefile variables are also correct because the first example worked.

Removing -nostdlib does not change anything - and according to the LD docs this should make no difference to explicitly declared libraries anyway. The library is archived with ar (flags -vcrs to create an archive index as per ranlib - removing the s argument makes no difference).

Does anyone know what I have missed this time?

Cheers,
Adam

Re: Including LD Custom Library

Posted: Thu Aug 14, 2008 11:47 am
by quok
The problem is because your library is specified before the objects to link. Traditionally, libraries are specified after the objects to link.

You can get around this several ways... if you want to specify -los before $(OBJ), then do it like this:

Code: Select all

--start-group -l$(LIB) $(OBJ) --end-group
.

In my makefile, I just specify $(LDFLAGS) as the last thing on the command line.

Re: Including LD Custom Library

Posted: Thu Aug 14, 2008 1:27 pm
by AJ
Thanks quok - that simple, eh? #-o I can't try it out until tomorrow morning but I'll post again then.

Cheers,
Adam

Re: Including LD Custom Library

Posted: Thu Aug 14, 2008 2:30 pm
by quok
Just in case you're wondering where I got it from: http://sourceware.org/binutils/docs/ld/ ... ml#Options
The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

See the -( option for a way to force the linker to search archives multiple times.

You may list the same archive multiple times on the command line.
The -( option is an alias for --start-group.

Re: Including LD Custom Library

Posted: Fri Aug 15, 2008 1:59 am
by AJ
Brilliant - thanks! My 30KiB for(;;) ; loop (!) has now reduced to a few bytes plus ELF symbolic information (I'm still compiling a debug version at the moment).

Adam

Re: Including LD Custom Library

Posted: Thu Aug 21, 2008 9:08 am
by JamesM
In my makefile, I just specify $(LDFLAGS) as the last thing on the command line.
Traditionally, that is the role that $(LDADD) plays. (LD Additions)

Re: Including LD Custom Library

Posted: Thu Aug 21, 2008 12:00 pm
by quok
JamesM wrote:
In my makefile, I just specify $(LDFLAGS) as the last thing on the command line.
Traditionally, that is the role that $(LDADD) plays. (LD Additions)

Strangely, I've never heard of $(LDADD) before, heh. Thanks for the note about it, now I can be more "traditional" :)