Last active
July 30, 2020 19:10
-
-
Save heiner/7c5c02d3d6ac67c3ffc749119a770522 to your computer and use it in GitHub Desktop.
dlreload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.10) | |
| project(dlreload) | |
| add_library(inner SHARED inner.c) | |
| add_library(outer SHARED outer.c) | |
| add_executable(test test.c) | |
| target_link_libraries(test PUBLIC outer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "inner.h" | |
| void init(int i) { | |
| g.i = 666 + i; | |
| g.b = 1; | |
| another_int = i; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef INNER_H | |
| #define INNER_H | |
| #include <stdbool.h> | |
| #include <stdio.h> | |
| typedef struct inner_globals_t { | |
| int i; | |
| bool b; | |
| FILE *f; | |
| } inner_globals; | |
| inner_globals g; | |
| int another_int; | |
| void init(int); | |
| #endif /* INNER_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <dlfcn.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "outer.h" | |
| void* handle = NULL; | |
| void load() { | |
| handle = dlopen("libinner.dylib", RTLD_NOW); | |
| if (!handle) { | |
| fprintf(stderr, "%s\n", dlerror()); | |
| exit(EXIT_FAILURE); | |
| } | |
| dlerror(); /* Clear any existing error */ | |
| void (*init)(int); | |
| init = dlsym(handle, "init"); | |
| int* obj = (int*)dlsym(handle, "another_int"); | |
| printf("in load: another_int: %d\n", *obj); | |
| init(32); | |
| printf("in load after init: another_int: %d\n", *obj); | |
| void* handle2 = dlopen("libinner2.dylib", RTLD_NOW); | |
| init = dlsym(handle2, "init"); | |
| int* obj2 = (int*)dlsym(handle2, "another_int"); | |
| printf("in load, handle2: another_int: %d\n", *obj2); | |
| init(44); | |
| printf("in load, handle2: another_int: %d\n", *obj2); | |
| printf("but handle: another_int: %d\n", *obj); | |
| dlclose(handle); | |
| dlclose(handle2); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef OUTER_H | |
| #define OUTER_H | |
| void load(); | |
| #endif /* OUTER_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "outer.h" | |
| int main(int argc, char **argv) { load(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment