Skip to content

Commit 7252ab4

Browse files
committed
[Runtime] Use dyld to find the image path when possible.
For performance reasons, we want to avoid `dladdr()` when possible. rdar://130954606
1 parent 3bc3f15 commit 7252ab4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

stdlib/public/runtime/Paths.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#if !defined(_WIN32) || defined(__CYGWIN__)
29+
2930
#if __has_include(<sys/stat.h>)
3031
# include <sys/stat.h>
3132

@@ -42,12 +43,21 @@
4243
#include <dlfcn.h>
4344
#endif
4445

46+
#if __has_include(<mach-o/dyld_priv.h>)
47+
#include <mach-o/dyld_priv.h>
48+
#define APPLE_OS_SYSTEM 1
4549
#else
50+
#define APPLE_OS_SYSTEM 0
51+
#endif
52+
53+
#else // defined(_WIN32)
54+
4655
#define WIN32_LEAN_AND_MEAN
4756
#define NOMINMAX
4857
#include <windows.h>
4958
#include <psapi.h>
50-
#endif
59+
60+
#endif // defined(_WIN32)
5161

5262
#include <cerrno>
5363
#include <cstdlib>
@@ -519,7 +529,9 @@ _swift_initRuntimePath(void *) {
519529
#if APPLE_OS_SYSTEM
520530
const char *path = dyld_image_path_containing_address(_swift_initRuntimePath);
521531

522-
runtimePath = ::strdup(path);
532+
// No need to ::strdup() this, as the return value is guaranteed to remain
533+
// valid as long as the library is loaded.
534+
runtimePath = path;
523535
#elif SWIFT_STDLIB_HAS_DLADDR
524536
Dl_info dli;
525537
int ret = ::dladdr((void *)_swift_initRuntimePath, &dli);

0 commit comments

Comments
 (0)