@@ -311,10 +311,10 @@ static void _addImageProtocolConformances(const mach_header *mh,
311311#include < climits>
312312#include < cstdio>
313313#include < unordered_set>
314+ #include < unistd.h>
314315
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);
318318 if (!handle)
319319 return ; // not a shared library
320320 auto conformances = reinterpret_cast <const uint8_t *>(
@@ -335,11 +335,17 @@ static void _addImageProtocolConformances(std::string const& name) {
335335 dlclose (handle);
336336}
337337
338- static void android_iterate_libs (void (*callback)(std::string const & )) {
338+ static void android_iterate_libs (void (*callback)(const char * )) {
339339 std::unordered_set<std::string> already;
340340 FILE* f = fopen (" /proc/self/maps" , " r" );
341341 if (!f)
342342 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+
343349 char name[PATH_MAX + 1 ];
344350 char perms[4 + 1 ];
345351 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&)) {
351357 if (already.count (name_str) != 0 )
352358 continue ;
353359 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);
355366 }
356367 fclose (f);
357368}
0 commit comments