Including LD Custom Library

Programming, for all ages and all languages.
Post Reply
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Including LD Custom Library

Post 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
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Including LD Custom Library

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Including LD Custom Library

Post 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
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Including LD Custom Library

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Including LD Custom Library

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

Re: Including LD Custom Library

Post 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)
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Including LD Custom Library

Post 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" :)
Post Reply