Subject: bin/6586: Linker failures
To: None <gnats-bugs@gnats.netbsd.org>
From: Olle Bergqvist <ollber@aladdin.netinsight.se>
List: netbsd-bugs
Date: 12/15/1998 15:29:03
>Number:         6586
>Category:       bin
>Synopsis:       ld linker failure
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 15 07:35:01 1998
>Last-Modified:
>Originator:     
>Organization:
	Net Insight 
>Release:        NetBSD 1.3.2
>Environment:
	NetBSD 1.3.2 i386 gcc version 2.7.2.2
	what /usr/bin/ld
	/usr/bin/ld
        ld.c    6.10 (Berkeley) 5/22/91
System: NetBSD aladdin 1.3.2 NetBSD 1.3.2 (CONAN_KERNEL) #2: Sun Oct 18 15:50:37 CEST 1998 ace@conan:/usr/src/sys/arch/i386/compile/CONAN_KERNEL i386


>Description:

	Linker errors are generated if two libraries (ar files) reference each other and only functions in one
        of them is really needed by the main routine. 
	The linker may produce "Undefined symbol" error". Undefined symbol `_lib1_func1' referenced from text segment

        Swapping the library order or make a dummy reference on the link line may fix the problem !

	Link Line : ld -o main main.l lib1.a lib2.a	

>How-To-Repeat:
Makefile 
------------
all : clean main

DEFINES=

lib1.a :
        gcc -c lib1.c
        ar ruv lib1.a lib1.o
        ranlib lib1.a

lib2.a :
        gcc -c lib2.c
        ar ruv lib2.a lib2.o
        ranlib lib2.a

# Reverse the order of lib1.a & lib2.a and it will link
main : lib1.a lib2.a
        gcc -c $(DEFINES) -c main.c
        gcc -o main main.o lib1.a lib2.a

clean :
        rm -rf *.o *.a main
---------------------------------
main.c
---------------------------------
void main(void)
{
        lib2_func1();
}

#ifdef ALWAYS_LINK
void dummy(void)
{
        lib1_func1();
}
#endif
----------------------------------
lib1.c
----------------------------------
void lib1_func1(void)
{
        printf("lib1 func1 \n");
}
-----------------------------------
lib2.c
-----------------------------------
void lib2_func1(void)
{

         printf("Lib2 Func1\n");
}

void lib2_func2(void)
{
        lib1_func1();
}
------------------------------------

Repeat by :
make
	lib2.o: Undefined symbol `_lib1_func1' referenced from text segment
	gmake: *** [main] Error 1

make DEFINES="-DALWAYS_LINK"
      Works great !!

>Fix:
	fix 1.	Reverse the order of lib1.a & lib2.a on the link line
	fix 2.  Make a dummy reference from main() to lib1.c
>Audit-Trail:
>Unformatted: