Help- selective symbol exposure after link

Jason Spielman <jason@interval.net>
22 Jan 1997 23:15:35 -0500

          From comp.compilers

Related articles
Help- selective symbol exposure after link jason@interval.net (Jason Spielman) (1997-01-22)
Re: Help- selective symbol exposure after link kaz@nt.com (1997-01-25)
Re: Help- selective symbol exposure after link maslen@best.com (Thomas M. Maslen) (1997-01-25)
Re: Help- selective symbol exposure after link mac@coos.dartmouth.edu (1997-01-25)
Re: Help- selective symbol exposure after link dlmoore@ix.netcom.com (David L Moore) (1997-01-26)
Re: Help- selective symbol exposure after link whsieh@cs.washington.edu (1997-01-29)
Re: Help- selective symbol exposure after link maslen@best.com (Thomas M. Maslen) (1997-01-29)
[12 later articles]
| List of all articles for this month |
From: Jason Spielman <jason@interval.net>
Newsgroups: comp.compilers,comp.lang.c
Date: 22 Jan 1997 23:15:35 -0500
Organization: Interval Systems, Inc.
Summary: selective symbol exposure during link
Keywords: linker, question, comment

Q: How does one link several object files into a single object file
      or library, where symbols are resolved locally as much as possible,
      and where only selected symbols are exported/exposed/made public?


Here is an example:
File b.c contains a function named b1() which does something simple.
File a.c contains a function named a1() which calls b1();
File main.c include main() which calls a1().
I'd like to create an object (or library) ab.o, composed of
a.o and b.o, which only exposes a1(). With this ab.o, I then
should be able to compile main.c and create an executable.
I tried an incremental link followed by a strip (tried various
combinations) but nothing seems to work. Below are listed the
files and the errors I got during my efforts:


PS. it's on a linux-2.0.18 platform; however I've tried it on various
        other platforms, including sunos-4.1.3, dec-alpha-osf1-v3.2, and
can't get it to work anywhere
PPS. I am a strong advocate of declaring functions static or external
but re-organizing the code to use that mechanism is an unrealistic
option.
PPPS. Please email (jason@interval.net) responses as well as posting as
my news server is a bit flakey and sometimes drops articles
before expiration.


Thanks.


-----------FILE: a.c-------------------
#include <stdio.h>
#include <stdlib.h>


int a1(int arg)
{
printf("a1(%d)\n",arg);
return b1(arg);
}
-----------FILE: b.c--------------------
#include <stdio.h>
#include <stdlib.h>


int b1(int arg)
{
printf("b1(%d)\n",arg);
return arg;
}
-----------FILE: main.c-----------------
#include <stdio.h>
#include <stdlib.h>


int main(int argc,char **argv)
{
a1(987);
return 0;
}
----------SCRIPT--------------------------
> gcc -c a.c
> gcc -c b.c
> ld -r -o ab.o a.o b.o
> nm ab.o
00000000 T a1
00000040 T b1
00000000 t gcc2_compiled.
00000040 t gcc2_compiled.
                  U printf
> strip K -a1 -K printf ab.o
BFD: sta31925: symbol `b1' required but not present
strip: sta31925: No symbols
[I have long maintained that nested link-time scopes are a good idea, but I've
never seen a linker that actually provides them. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.