@@ -2401,10 +2401,10 @@ static void _addImageProtocolConformances(const mach_header *mh,
2401
2401
#include < climits>
2402
2402
#include < cstdio>
2403
2403
#include < unordered_set>
2404
+ #include < unistd.h>
2404
2405
2405
- static void _addImageProtocolConformances (std::string const & name) {
2406
- // FIXME: Android: special case the current executable?
2407
- void *handle = dlopen (name.c_str (), RTLD_LAZY);
2406
+ static void _addImageProtocolConformances (const char * name) {
2407
+ void *handle = dlopen (name, RTLD_LAZY);
2408
2408
if (!handle)
2409
2409
return ; // not a shared library
2410
2410
auto conformances = reinterpret_cast <const uint8_t *>(
@@ -2425,11 +2425,17 @@ static void _addImageProtocolConformances(std::string const& name) {
2425
2425
dlclose (handle);
2426
2426
}
2427
2427
2428
- static void android_iterate_libs (void (*callback)(std::string const & )) {
2428
+ static void android_iterate_libs (void (*callback)(const char * )) {
2429
2429
std::unordered_set<std::string> already;
2430
2430
FILE* f = fopen (" /proc/self/maps" , " r" );
2431
2431
if (!f)
2432
2432
return ;
2433
+ char ownname[PATH_MAX + 1 ];
2434
+ if (readlink (" /proc/self/exe" , ownname, sizeof (ownname)) == -1 ) {
2435
+ fprintf (stderr, " swift: can't find path of executable\n " );
2436
+ ownname[0 ] = ' \0 ' ;
2437
+ }
2438
+
2433
2439
char name[PATH_MAX + 1 ];
2434
2440
char perms[4 + 1 ];
2435
2441
while (fscanf (f, " %*s %4c %*s %*s %*s%*[ ]%[^\n ]" , perms, name) > 0 ) {
@@ -2441,7 +2447,12 @@ static void android_iterate_libs(void (*callback)(std::string const&)) {
2441
2447
if (already.count (name_str) != 0 )
2442
2448
continue ;
2443
2449
already.insert (name_str);
2444
- callback (name_str);
2450
+ const char * libname = name_str.c_str ();
2451
+ if (strcmp (libname, ownname) == 0 ) {
2452
+ // need to pass null if opening main executable
2453
+ libname = nullptr ;
2454
+ }
2455
+ callback (libname);
2445
2456
}
2446
2457
fclose (f);
2447
2458
}
0 commit comments