Skip to content

Commit

Permalink
* Simple test dlopen
Browse files Browse the repository at this point in the history
* Test dlopen with autotools
  • Loading branch information
JuergenReppSIT committed Oct 31, 2024
1 parent 25c7c0f commit a4a5a33
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 7 deletions.
13 changes: 7 additions & 6 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ tss2_HEADERS += $(srcdir)/include/tss2/tss2_tcti_device.h
lib_LTLIBRARIES += $(libtss2_tcti_device)
pkgconfig_DATA += lib/tss2-tcti-device.pc

# tcti library for swtpm
if ENABLE_TCTI_SWTPM
libtss2_tcti_swtpm = src/tss2-tcti/libtss2-tcti-swtpm.la
tss2_HEADERS += $(srcdir)/include/tss2/tss2_tcti_swtpm.h
lib_LTLIBRARIES += $(libtss2_tcti_swtpm)
pkgconfig_DATA += lib/tss2-tcti-swtpm.pc

if HAVE_LD_VERSION_SCRIPT
src_tss2_tcti_libtss2_tcti_device_la_LDFLAGS = -Wl,--version-script=$(srcdir)/lib/tss2-tcti-device.map
endif # HAVE_LD_VERSION_SCRIPT
Expand All @@ -328,12 +335,6 @@ src_tss2_tcti_libtss2_tcti_device_la_SOURCES = \
endif # ENABLE_TCTI_DEVICE
EXTRA_DIST += lib/tss2-tcti-device.map

# tcti library for swtpm
if ENABLE_TCTI_SWTPM
libtss2_tcti_swtpm = src/tss2-tcti/libtss2-tcti-swtpm.la
tss2_HEADERS += $(srcdir)/include/tss2/tss2_tcti_swtpm.h
lib_LTLIBRARIES += $(libtss2_tcti_swtpm)
pkgconfig_DATA += lib/tss2-tcti-swtpm.pc

if HAVE_LD_VERSION_SCRIPT
src_tss2_tcti_libtss2_tcti_swtpm_la_LDFLAGS = -Wl,--version-script=$(srcdir)/lib/tss2-tcti-swtpm.map
Expand Down
1 change: 1 addition & 0 deletions src/tss2-tcti/tcti-libtpms.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ tcti_libtpms_dl(TSS2_TCTI_LIBTPMS_CONTEXT *tcti_libtpms)
const char *names[] = {"libtpms.so", "libtpms.so.0"};

for (size_t i = 0; i < ARRAY_LEN(names); i++) {
LOG_ERROR("XXX 7 %s", names[i]);
tcti_libtpms->libtpms = dlopen(names[i], RTLD_LAZY | RTLD_LOCAL);
if (tcti_libtpms->libtpms != NULL) {
break;
Expand Down
1 change: 1 addition & 0 deletions src/tss2-tcti/tctildr-dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ handle_from_name(const char *file,
LOG_ERROR("TCTI name truncated in transform.");
return TSS2_TCTI_RC_BAD_VALUE;
}
LOG_ERROR("XXX 1 %s", file_xfrm);
*handle = dlopen(file_xfrm, RTLD_NOW);
if (*handle != NULL) {
return TSS2_RC_SUCCESS;
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions test-dlopen/dlopen-autotools/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
lib_LTLIBRARIES = libmyfu1.la
libmyfu1_la_SOURCES = myfu1.c
libmyfu1_la_CFLAGS = -fPIC
libmyfu1_la_LDFLAGS = -version-info 1:0:0 -rpath /usr/local/lib
lib_LTLIBRARIES += libmyfu2.la
libmyfu2_la_SOURCES = myfu2.c
libmyfu2_la_CFLAGS = -fPIC
libmyfu2_la_LDFLAGS = -version-info 1:0:0 -rpath /usr/local/lib

main_LDADD = libmyfu1.la libmyfu2.la -ldl
bin_PROGRAMS = main
main_SOURCES = main.c
main_LDADD += libmyfu1.la
main_LDADD += libmyfu2.la
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions test-dlopen/dlopen-autotools/configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AC_INIT([MyLib], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
LT_INIT
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
23 changes: 23 additions & 0 deletions test-dlopen/dlopen-autotools/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <dlfcn.h>

void myfu() {
printf("Hello from main myfu!\n");
}


int main() {
void *handle;
handle = dlopen("libmyfu2.so", RTLD_NOW);
myfu();
void (*myfu)() = (void (*)()) dlsym(handle, "myfu");
char *error = dlerror();
if (error) {
fprintf(stderr, "Error finding symbol 'myfu': %s\n", error);
dlclose(handle);
return 1;
}
printf("Calling myfu:\n");
myfu();
return 0;
}
6 changes: 6 additions & 0 deletions test-dlopen/dlopen-autotools/myfu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

// Define the external function
void myfu() {
printf("Hello from so myfu!\n");
}
6 changes: 6 additions & 0 deletions test-dlopen/dlopen-autotools/myfu1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

// Define the external function
void myfu() {
printf("Hello from so1 myfu!\n");
}
6 changes: 6 additions & 0 deletions test-dlopen/dlopen-autotools/myfu2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

// Define the external function
void myfu() {
printf("Hello from so2 myfu!\n");
}
8 changes: 8 additions & 0 deletions test-dlopen/dlopen-autotools/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
git clean -xdf
aclocal
libtoolize
autoconf
automake --add-missing
./configure --disable-dependency-tracking
gmake -j
./main
23 changes: 23 additions & 0 deletions test-dlopen/simple-test/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <dlfcn.h>

void myfu() {
printf("Hello from main myfu!\n");
}


int main() {
void *handle;
handle = dlopen("./libmyfu.so", RTLD_NOW);
myfu();
void (*myfu)() = (void (*)()) dlsym(handle, "myfu");
char *error = dlerror();
if (error) {
fprintf(stderr, "Error finding symbol 'myfu': %s\n", error);
dlclose(handle);
return 1;
}
printf("Calling myfu:\n");
myfu();
return 0;
}
2 changes: 2 additions & 0 deletions test-dlopen/simple-test/make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clang -fPIC -shared -o libmyfu.so myfu.c
clang -o main_program main.c -ldl
6 changes: 6 additions & 0 deletions test-dlopen/simple-test/myfu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

// Define the external function
void myfu() {
printf("Hello from so myfu!\n");
}
2 changes: 1 addition & 1 deletion tss2-dlopen/tss2-dlopen-fapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ init_dlhandle(void)
{
if (dlhandle)
return TSS2_RC_SUCCESS;
dlhandle = dlopen(LIB, RTLD_NOW | RTLD_LOCAL);
dlhandle = dlopen(LIB, RTLD_NOW | RTLD_LOCAL);
if (!dlhandle) {
WARN("Library " LIB " not found: %s.", dlerror());
return TSS2_FAPI_RC_NOT_IMPLEMENTED;
Expand Down

0 comments on commit a4a5a33

Please sign in to comment.