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