Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created September 17, 2020 20:32
Show Gist options
  • Save peat-psuwit/2ff18dfa2b980d8c3e8feab9ec9ee2dd to your computer and use it in GitHub Desktop.
Save peat-psuwit/2ff18dfa2b980d8c3e8feab9ec9ee2dd to your computer and use it in GitHub Desktop.
Test loader's behavior regarding loaded library at exit
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libpeat-test
LOCAL_SRC_FILES := lib.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := peat-test-app
LOCAL_SRC_FILES := app.c
include $(BUILD_EXECUTABLE)
# Easiest way to run: use LD_LIBRARY_PATH
include $(CLEAR_VARS)
LOCAL_MODULE := peat-test-app-linked
LOCAL_SRC_FILES := app-linked.c
LOCAL_SHARED_LIBRARIES := libpeat-test
include $(BUILD_EXECUTABLE)
#include <stdio.h>
#include "lib.h"
int main(int argc, char **argv) {
printf("from_shared() = %d\n", from_shared());
return 0;
}
#include <stdio.h>
#include <dlfcn.h>
#ifdef LIBHYBRIS
#include <hybris/common/dlfcn.h>
#define dlopen hybris_dlopen
#define dlsym hybris_dlsym
#define dlclose hybris_dlclose
#endif
int main(int argc, char **argv) {
void *lib;
int (*from_shared)();
puts("dlopen()");
lib = dlopen("./libpeat-test.so", RTLD_NOW);
from_shared = dlsym(lib, "from_shared");
printf("from_shared() = %d\n", from_shared());
if (argc >= 2 && argv[1][0] == 'y') {
puts("dlclose()");
dlclose(lib);
puts("return - with dlclose()!");
} else {
puts("return - without dlclose()!");
}
return 0;
}
#include <stdio.h>
#include "lib.h"
class AAA {
public:
AAA() {}
~AAA() { puts("AAA destructed"); }
} aaa;
thread_local class BBB {
public:
BBB() {}
~BBB() { puts("BBB destructed"); }
void nothing() {}
} bbb;
extern "C" int from_shared() {
// Uncomment to trigger thread_local usage
// bbb.nothing();
return 42;
}
__attribute__((destructor))
void libtest_destructor()
{
puts("libtest destructor called");
}
#ifdef __cplusplus
extern "C"
#endif
int from_shared();
libpeat-test.so: lib.h lib.cpp
$(CXX) -g -shared -fPIC -o libpeat-test.so lib.cpp
app: app.c
$(CC) -g -o app app.c -ldl
app-hybris: app.c
$(CC) -g -D LIBHYBRIS -o app-hybris app.c -lhybris-common
# Easiest way to run: use LD_LIBRARY_PATH
app-linked: app-linked.c lib.h
$(CC) -g -o app-linked app-linked.c libpeat-test.so
.PHONY: clean
clean:
rm -f libpeat-test.so app app-hybris app-linked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment