[ILUG] Linking question
Kenn Humborg
kenn at linux.ie
Wed Nov 15 21:57:32 GMT 2000
On Tue, Nov 07, 2000 at 02:14:54PM +0000, Mel wrote:
> On Mon, 6 Nov 2000 hmoreau at digiserve.ie wrote:
>
> > On 6 Nov, Mel wrote:
> > >
> > > here is one for the gcc/g++ kings.
> > >
> > > I have two libs that I want pieces out of either of them. Now, they
> > > decided having the same named functions doing the same thing in two binary
> > > libs would be funny - aren't they smart!
> > >
> >
> > Need to know a little more here. Are the libs shared libs (.so's) or
> > static (.a)? What are the actual libs? What functions or symbols
> > overlap?
> >
>
> They are two static libs that are part of an SDK for music
> encoding/decoding. At some stage both of them have similar functions such
> as frame counters and global constants. I can strip out some of the
> functions but not all of them. I was hoping there was some way you could
> tell the linker thant when there is duplicate symbols, just take them from
> the first library
>
> not looking like it though.
Weak/strong symbol definitions can do what you're talking about. I've never
got this to work correctly with gcc compiling C, but the linker and gcc docs
suggest that this kind of thing should work:
========= main.c
extern void g(void);
void f(void)
{
g();
}
========= lib1.c
void g(void)
{
/* do something */
}
========= lib2.c
__attribute__((weak)) void g(void)
{
/* do something */
}
Now, linking main.o with lib1.o or main.o with lib2.o should give the
expected behaviour. However, linking all three together should result in
the linker selecting the g() from lib1.o, rather than lib2.o. (The above
isn't the right syntax for declaring weak definitions. The interested
reader is referred to the GCC info pages.)
I'm not sure how GNU ld deals with multiple weak definitions without a
strong definition (i.e. both lib1 and lib2 marked weak). The VMS linker
quietly selects the _first_ occurrence of the multiply-defined weak
symbol, so I'd imagine that GNU ld does the same.
In your case, what you need to do is make all the multiply-defined symbols
weak in one of the libraries. You _might_ be able to do this by picking
apart the .a into the constituent .o files and doing some magic with a
linker script. Let me check the info pages...
Hmm... that approach doesn't look promising. There doesn't seem to be
any way to do this via a linker script.
Plan B is a bit trickier... Basically, you'd use the BFD library to
pick through the .a and copy it verbatim to a new .a, but you'd set
the BSF_WEAK bit on any symbol definitions. Now that sounds suspiciously
like something that objcopy could do. man objcopy...
Yup - objcopy --weaken will make all symbol definitions weak. Do this
on the 'second' library. I don't know if objcopy will work on a .a.
You may need to 'ar' it apart first, objcopy each .o and then 'ar' it
back together again.
Later,
Kenn
More information about the ILUG
mailing list