@@ -311,10 +311,10 @@ static void _addImageProtocolConformances(const mach_header *mh,
311
311
#include < climits>
312
312
#include < cstdio>
313
313
#include < unordered_set>
314
+ #include < unistd.h>
314
315
315
- static void _addImageProtocolConformances (std::string const & name) {
316
- // FIXME: Android: special case the current executable?
317
- void *handle = dlopen (name.c_str (), RTLD_LAZY);
316
+ static void _addImageProtocolConformances (const char *name) {
317
+ void *handle = dlopen (name, RTLD_LAZY);
318
318
if (!handle)
319
319
return ; // not a shared library
320
320
auto conformances = reinterpret_cast <const uint8_t *>(
@@ -335,11 +335,17 @@ static void _addImageProtocolConformances(std::string const& name) {
335
335
dlclose (handle);
336
336
}
337
337
338
- static void android_iterate_libs (void (*callback)(std::string const & )) {
338
+ static void android_iterate_libs (void (*callback)(const char * )) {
339
339
std::unordered_set<std::string> already;
340
340
FILE* f = fopen (" /proc/self/maps" , " r" );
341
341
if (!f)
342
342
return ;
343
+ char ownname[PATH_MAX + 1 ];
344
+ if (readlink (" /proc/self/exe" , ownname, sizeof (ownname)) == -1 ) {
345
+ fprintf (stderr, " swift: can't find path of executable\n " );
346
+ ownname[0 ] = ' \0 ' ;
347
+ }
348
+
343
349
char name[PATH_MAX + 1 ];
344
350
char perms[4 + 1 ];
345
351
while (fscanf (f, " %*s %4c %*s %*s %*s%*[ ]%[^\n ]" , perms, name) > 0 ) {
@@ -351,7 +357,12 @@ static void android_iterate_libs(void (*callback)(std::string const&)) {
351
357
if (already.count (name_str) != 0 )
352
358
continue ;
353
359
already.insert (name_str);
354
- callback (name_str);
360
+ const char * libname = name_str.c_str ();
361
+ if (strcmp (libname, ownname) == 0 ) {
362
+ // need to pass null if opening main executable
363
+ libname = nullptr ;
364
+ }
365
+ callback (libname);
355
366
}
356
367
fclose (f);
357
368
}
0 commit comments