@@ -312,10 +312,10 @@ static void _addImageProtocolConformances(const mach_header *mh,
312312#include < climits>
313313#include < cstdio>
314314#include < unordered_set>
315+ #include < unistd.h>
315316
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);
319319 if (!handle)
320320 return ; // not a shared library
321321 auto conformances = reinterpret_cast <const uint8_t *>(
@@ -336,11 +336,17 @@ static void _addImageProtocolConformances(std::string const& name) {
336336 dlclose (handle);
337337}
338338
339- static void android_iterate_libs (void (*callback)(std::string const & )) {
339+ static void android_iterate_libs (void (*callback)(const char * )) {
340340 std::unordered_set<std::string> already;
341341 FILE* f = fopen (" /proc/self/maps" , " r" );
342342 if (!f)
343343 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+
344350 char name[PATH_MAX + 1 ];
345351 char perms[4 + 1 ];
346352 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&)) {
352358 if (already.count (name_str) != 0 )
353359 continue ;
354360 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);
356367 }
357368 fclose (f);
358369}
0 commit comments