Avoid 'multiple definition' error during linking?

The Chazman chaz@devastator.extern.ucsd.edu
Fri Jul 14 10:49:00 GMT 2000


> I'm trying to replace some functions found in libgcc.a (portions of
> fp-bit, specifically).  Trouble is, when I link, I get this:
>
> fp-bit.c(.text+0x320): multiple definition of `__addsf3'
> fp-bit.o:fp-bit.c:30: first defined here
> /usr/lib/gcc-lib/sh-coff/2.95.2/m2/libgcc.a(fp-bit.o): In function
>     `__subsf3':
> fp-bit.c(.text+0x380): multiple definition of `__subsf3'
>
> ... and so on.
>
> I don't get this error when I replace one or more functions in libc or
> libm (I'm using the newlib versions).  Even more strange, if I copy
> fp-bit.c from the gcc sources, modify it, and link it in its entireity,
> things seem to work fine as well.  If I disturb things in the file
> (i.e. remove the portions I'm not modifying, as I did to produce the
> above error messages), I start getting complaints again.

Hi, Bill.  The linker will bring in only the objects (.o files) it needs
from each library, but it has to bring in each object in its entirety.
It can't link just one portion of an object.  So if you copy over all of
fp-bit.c to your libc/libm and make the changes you need, leaving the
unchanged functions present and intact, all of the fp-bit symbols get
resolved from your version, and the linker doesn't need to get fp-bit.o
from libgcc.  If the fp-bit.o in your libc/libm contains only the
functions you changed, and omits the ones you didn't, then the linker
needs both copies.  Here's why.  Let's say fp-bit.c has two functions,
foo() and bar().  Let's say you've modified foo(), but the stock bar()
is fine for your purposes, so your fp-bit.c in your libc/libm just has
your modified foo() and no bar().  Let's say your program calls both
foo() and bar() at some point.  When linking libc or libm, the linker
sees a definition for foo(), which it needs, so it links in your fp-bit.o
and resolves foo.  But bar is still unresolved.  When it gets to libgcc,
it finds a definition for bar in that fp-bit.o, so it links in that
object also.  But because it has to link an entire object, it also gets
a second definition of foo along with the definition of bar it wanted.
This is the multiple definition error you're seeing.  The result is that
if you want to replace a function from a library, you need to replace
the entire object that the function is defined in, thereby providing
your own definitions for all the symbols in that object so that the
version in the library will never need to be linked in.

Hope that explained it well enough...


-------------------------------------------
Carl Miller               Firmware Engineer
chaz@gordian.com              Gordian, Inc.
(714) 850-0205       http://www.gordian.com
(personal: chaz@devastator.extern.ucsd.edu)

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com



More information about the crossgcc mailing list