|
19 | 19 | #import <Foundation/Foundation.h>
|
20 | 20 | #import <mach/mach.h>
|
21 | 21 | #import <mach-o/dyld.h>
|
| 22 | +#import <libgen.h> |
| 23 | +#import <stdlib.h> |
| 24 | +#import <string.h> |
22 | 25 |
|
23 | 26 | #import "SwiftRemoteMirrorLegacyInterop.h"
|
24 | 27 |
|
25 | 28 |
|
26 | 29 | void *Load(char *path) {
|
| 30 | + const char *libpath = getenv("DYLD_LIBRARY_PATH"); |
| 31 | + char *ourlibpath = NULL; |
| 32 | + const char *oldlibpath = NULL; |
| 33 | + |
| 34 | + if (libpath && path[0] == '/') { |
| 35 | + // If DYLD_LIBRARY_PATH is set, and the path we've been given is absolute, |
| 36 | + // then prepend the directory part of the path we've been given to it. |
| 37 | + const char *libdir = dirname(path); |
| 38 | + size_t pathlen = strlen(libpath); |
| 39 | + size_t dirlen = strlen(libdir); |
| 40 | + ourlibpath = (char *)malloc(pathlen + dirlen + 2); |
| 41 | + memcpy(ourlibpath, libdir, dirlen); |
| 42 | + ourlibpath[dirlen] = ':'; |
| 43 | + oldlibpath = ourlibpath + dirlen + 1; |
| 44 | + memcpy(ourlibpath + dirlen + 1, libpath, pathlen + 1); |
| 45 | + |
| 46 | + setenv("DYLD_LIBRARY_PATH", ourlibpath, 1); |
| 47 | + } |
| 48 | + |
| 49 | + // Actually open the dylib |
27 | 50 | void *Handle = dlopen(path, RTLD_LOCAL);
|
| 51 | + |
| 52 | + if (ourlibpath) { |
| 53 | + // Reset DYLD_LIBRARY_PATH, if we changed it |
| 54 | + setenv("DYLD_LIBRARY_PATH", oldlibpath, 1); |
| 55 | + free(ourlibpath); |
| 56 | + } |
| 57 | + |
28 | 58 | if (Handle == NULL) {
|
29 | 59 | fprintf(stderr, "loading %s: %s\n", path, dlerror());
|
30 | 60 | exit(1);
|
|
0 commit comments