Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 12 Feb 1999 10:28:33 -0600
From:      "Eric L. Hernes" <erich@lodgenet.com>
To:        hackers@FreeBSD.ORG
Cc:        erich@lodgenet.com
Subject:   dlopen and ELF v. a.out
Message-ID:  <199902121628.KAA29685@jake.lodgenet.com>

next in thread | raw e-mail | index | archive | help
This is a multipart MIME message.

--==_Exmh_-7836634220
Content-Type: text/plain; charset=us-ascii


I've been looking at x11amp, and I've run into a bit of a problem.
The program is set up as a program and a few shared-library plug-ins.
The shared libs all have some common symbol names in global scope,
(obviously) to provide an interface between the program and the plugin.

What happens, though is that the plug-ins aren't very well encapsulated,
so there's big time global symbol polution.  If the plug-ins were contained
in one .c module, I could just declare the offending symbols as static,
and all would be fine.  But the plug-ins are often aggregates where
some symbols need to be seen from different c-sources.

So is there any way to get the linker to export only specific symbols,
and hide the rest (and resolve them internally to the lib)?

Attached is a test prog that illustrates the problem.  There is two
source files, a ldsoTest.c, that is the main program that dlopen()s
three `plug-ins'.  Each plugin is compiled from the same test.c source
with different pre-processor defines to get a different message.
If the programs are compiled a.out, it works as desired.  But if
I build with ELF, it's not right.

Here's the output when I run it: (code attached below)

(ttyp1@jake)$ env OBJFORMAT=aout make
cc -O -pipe -g -shared -o test0.so -DTST_MESSAGE="\"This is test 0"\" test.c
cc -O -pipe -g -shared -o test1.so -DTST_MESSAGE="\"This is test 1"\" test.c
cc -O -pipe -g -shared -o test2.so -DTST_MESSAGE="\"This is test 2"\" test.c
cc -O -pipe -g -o ldsoTest ldsoTest.c
(ttyp1@jake)$ ./ldsoTest
got fp at 0x200170f0
message: This is test 0
got fp at 0x200970f0
message: This is test 1
got fp at 0x200990f0
message: This is test 2
(ttyp1@jake)$ make clean
rm -f test0.so test1.so test2.so ldsoTest
(ttyp1@jake)$ env OBJFORMAT=elf make
cc -O -pipe -g -shared -o test0.so -DTST_MESSAGE="\"This is test 0"\" test.c
cc -O -pipe -g -shared -o test1.so -DTST_MESSAGE="\"This is test 1"\" test.c
cc -O -pipe -g -shared -o test2.so -DTST_MESSAGE="\"This is test 2"\" test.c
cc -O -pipe -g -o ldsoTest ldsoTest.c
(ttyp1@jake)$ ./ldsoTest
got fp at 0x280d2288
message: This is test 0
got fp at 0x280d4288
message: This is test 0
got fp at 0x280d6288
message: This is test 0



--==_Exmh_-7836634220
Content-Type: text/plain ; name="test.c"; charset=us-ascii
Content-Description: test.c
Content-Disposition: attachment; filename="test.c"

#include <stdio.h>

char *msg = TST_MESSAGE;

char *
testFunction(void){
  return msg;
}

--==_Exmh_-7836634220
Content-Type: text/plain ; name="ldsoTest.c"; charset=us-ascii
Content-Description: ldsoTest.c
Content-Disposition: attachment; filename="ldsoTest.c"


#include <dlfcn.h>
#include <stdio.h>
#include <unistd.h>

typedef char *fcn(void);

int
main(int ac, char **av){
  int i;
  char *pn;
  char *cwd=getwd(NULL);
  void *h;
  fcn *fp;
  char *m;

  for(i=0;i<3;i++) {
    asprintf(&pn, "%s/test%d.so", cwd, i);
    h = dlopen(pn, RTLD_NOW);
    if (h) {
      fp = dlsym(h, "testFunction");
      if (fp){
	fprintf(stderr, "got fp at %p\n", fp);
	m = fp();
	fprintf(stderr, "message: %s\n", m);
      }
    } else {
      warn("%s: dlopen failed", pn);
    }
    free(pn);
  }
  return 0;
}

--==_Exmh_-7836634220
Content-Type: text/plain ; name="Makefile"; charset=us-ascii
Content-Description: Makefile
Content-Disposition: attachment; filename="Makefile"


CFLAGS+=-g
BINS=test0.so test1.so test2.so ldsoTest
all: ${BINS}

ldsoTest: ldsoTest.c
	${CC} ${CFLAGS} -o $@ $<

test0.so: test.c
	${CC} ${CFLAGS} -shared -o $@ -DTST_MESSAGE="\"This is test 0"\" test.c

test1.so: test.c
	${CC} ${CFLAGS} -shared -o $@ -DTST_MESSAGE="\"This is test 1"\" test.c

test2.so: test.c
	${CC} ${CFLAGS} -shared -o $@ -DTST_MESSAGE="\"This is test 2"\" test.c



clean:
	rm -f ${BINS}

--==_Exmh_-7836634220
Content-Type: text/plain; charset=us-ascii

Eric L. Hernes
erich@lodgenet.com


--==_Exmh_-7836634220--



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199902121628.KAA29685>