Skip to content

Commit 3a4a59d

Browse files
zhuoweimodocache
authored andcommitted
android: load conformance tables in main executable properly; fixes swiftlang#5
1 parent b79d05c commit 3a4a59d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

stdlib/public/runtime/ProtocolConformance.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -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
}

utils/build-script-impl

-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,6 @@ fi
11001100
if [[ "${DISTCC}" ]] ; then
11011101
BUILD_ARGS="${BUILD_ARGS} -j $(distcc -j)"
11021102
fi
1103-
BUILD_ARGS="${BUILD_ARGS} -j2"
11041103

11051104
case "${CMAKE_GENERATOR}" in
11061105
Ninja)

0 commit comments

Comments
 (0)