symbol versioning demo
authorAndrew Tridgell <tridge@samba.org>
Mon, 17 Jan 2011 05:47:27 +0000 (16:47 +1100)
committerAndrew Tridgell <tridge@samba.org>
Mon, 17 Jan 2011 05:47:27 +0000 (16:47 +1100)
shlib/Makefile [new file with mode: 0644]
shlib/lib.c [new file with mode: 0644]
shlib/main.c [new file with mode: 0644]
shlib/module.c [new file with mode: 0644]

diff --git a/shlib/Makefile b/shlib/Makefile
new file mode 100644 (file)
index 0000000..3eab8d2
--- /dev/null
@@ -0,0 +1,37 @@
+CFLAGS=-Wall
+
+SONAME1=libversion1.so
+SONAME2=libversion2.so
+SYMVER1=symversion1
+SYMVER2=symversion2
+
+all: main public/module.so
+
+private:
+       mkdir -p private
+
+public:
+       mkdir -p public
+
+vscript1:
+       echo "$(SYMVER1) { global: *; };" > vscript1
+
+vscript2:
+       echo "$(SYMVER2) { global: *; };" > vscript2
+
+public/$(SONAME1): lib.c vscript1 public
+       gcc $(CFLAGS) -Wl,-soname=$(SONAME1) -Wl,--version-script=vscript1 -DLIBVERSION=\"version1\" -fPIC -shared lib.c -o public/$(SONAME1)
+
+private/$(SONAME2): lib.c vscript2 private
+       gcc $(CFLAGS) -Wl,-soname=$(SONAME2) -Wl,--version-script=vscript2 -DLIBVERSION=\"version2\" -fPIC -shared lib.c -o private/$(SONAME2)
+
+public/module.so: module.c private/$(SONAME2)
+       gcc $(CFLAGS) -Wl,--rpath=$(PWD)/private -fPIC -shared module.c -o public/module.so private/$(SONAME2)
+
+main: main.c public/$(SONAME1) public/module.so
+       gcc $(CFLAGS) -Wl,--rpath=$(PWD)/public main.c -o main public/$(SONAME1) -ldl
+
+clean:
+       rm -rf *.o *.so *~ private public main vscript*
+
+
diff --git a/shlib/lib.c b/shlib/lib.c
new file mode 100644 (file)
index 0000000..1e3be52
--- /dev/null
@@ -0,0 +1,3 @@
+const char *libversion(void) {
+       return LIBVERSION;
+}
diff --git a/shlib/main.c b/shlib/main.c
new file mode 100644 (file)
index 0000000..8230c07
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+int main(int argc, const char *argv[])
+{
+       const char *modpath = "public/module.so";
+       void *handle;
+       const char *(*version2)(void);
+       const char *libversion(void);
+
+       printf("Direct: libversion -> %s\n", libversion());
+
+       handle = dlopen(modpath, RTLD_NOW);
+       if (handle == NULL) {
+               printf("Failed to open '%s' - %s\n",
+                      modpath, dlerror());
+               exit(1);
+       }
+
+       version2 = dlsym(handle, "libversion");
+       if (version2 == NULL) {
+               printf("Failed to find libversion in %s\n", modpath);
+               exit(1);
+       }
+
+       printf("Module: libversion -> %s\n", version2());
+       return 0;
+}
diff --git a/shlib/module.c b/shlib/module.c
new file mode 100644 (file)
index 0000000..64d4c46
--- /dev/null
@@ -0,0 +1,8 @@
+
+
+
+const char *libversion(void);
+
+const char *mod_libversion(void) {
+       return libversion();
+}