@@ -224,8 +224,18 @@ namespace {
224
224
}
225
225
226
226
// Conformance Cache.
227
-
227
+ # if defined(__APPLE__) && defined(__MACH__)
228
228
static void _initializeCallbacksToInspectDylib ();
229
+ #else
230
+ namespace swift {
231
+ void _swift_initializeCallbacksToInspectDylib (
232
+ void (*fnAddImageBlock)(const uint8_t *, size_t ),
233
+ const char *sectionName);
234
+ }
235
+
236
+ static void _addImageProtocolConformancesBlock (const uint8_t *conformances,
237
+ size_t conformancesSize);
238
+ #endif
229
239
230
240
struct ConformanceState {
231
241
ConcurrentMap<ConformanceCacheEntry> Cache;
@@ -235,7 +245,13 @@ struct ConformanceState {
235
245
ConformanceState () {
236
246
SectionsToScan.reserve (16 );
237
247
pthread_mutex_init (&SectionsToScanLock, nullptr );
248
+ #if defined(__APPLE__) && defined(__MACH__)
238
249
_initializeCallbacksToInspectDylib ();
250
+ #else
251
+ _swift_initializeCallbacksToInspectDylib (
252
+ _addImageProtocolConformancesBlock,
253
+ SWIFT_PROTOCOL_CONFORMANCES_SECTION);
254
+ #endif
239
255
}
240
256
241
257
void cacheSuccess (const void *type, const ProtocolDescriptor *proto,
@@ -295,6 +311,14 @@ static void _addImageProtocolConformancesBlock(const uint8_t *conformances,
295
311
recordsBegin, recordsEnd);
296
312
}
297
313
314
+ #if !defined(__APPLE__) || !defined(__MACH__)
315
+ // Common Structure
316
+ struct InspectArgs {
317
+ void (*fnAddImageBlock)(const uint8_t *, size_t );
318
+ const char *sectionName;
319
+ };
320
+ #endif
321
+
298
322
#if defined(__APPLE__) && defined(__MACH__)
299
323
static void _addImageProtocolConformances (const mach_header *mh,
300
324
intptr_t vmaddr_slide) {
@@ -317,16 +341,27 @@ static void _addImageProtocolConformances(const mach_header *mh,
317
341
318
342
_addImageProtocolConformancesBlock (conformances, conformancesSize);
319
343
}
344
+
345
+ static void _initializeCallbacksToInspectDylib () {
346
+ // Install our dyld callback.
347
+ // Dyld will invoke this on our behalf for all images that have already
348
+ // been loaded.
349
+ _dyld_register_func_for_add_image (_addImageProtocolConformances);
350
+ }
351
+
320
352
#elif defined(__ELF__)
321
353
static int _addImageProtocolConformances (struct dl_phdr_info *info,
322
- size_t size, void * /* data*/ ) {
354
+ size_t size, void *data) {
355
+ // inspectArgs contains addImage*Block function and the section name
356
+ InspectArgs *inspectArgs = reinterpret_cast <InspectArgs *>(data);
357
+
323
358
void *handle;
324
359
if (!info->dlpi_name || info->dlpi_name [0 ] == ' \0 ' ) {
325
360
handle = dlopen (nullptr , RTLD_LAZY);
326
361
} else
327
362
handle = dlopen (info->dlpi_name , RTLD_LAZY | RTLD_NOLOAD);
328
363
auto conformances = reinterpret_cast <const uint8_t *>(
329
- dlsym (handle, SWIFT_PROTOCOL_CONFORMANCES_SECTION ));
364
+ dlsym (handle, inspectArgs-> sectionName ));
330
365
331
366
if (!conformances) {
332
367
// if there are no conformances, don't hold this handle open.
@@ -338,14 +373,29 @@ static int _addImageProtocolConformances(struct dl_phdr_info *info,
338
373
auto conformancesSize = *reinterpret_cast <const uint64_t *>(conformances);
339
374
conformances += sizeof (conformancesSize);
340
375
341
- _addImageProtocolConformancesBlock (conformances, conformancesSize);
376
+ inspectArgs-> fnAddImageBlock (conformances, conformancesSize);
342
377
343
378
dlclose (handle);
344
379
return 0 ;
345
380
}
381
+
382
+ void swift::_swift_initializeCallbacksToInspectDylib (
383
+ void (*fnAddImageBlock)(const uint8_t *, size_t ),
384
+ const char *sectionName) {
385
+ InspectArgs inspectArgs = {fnAddImageBlock, sectionName};
386
+
387
+ // Search the loaded dls. Unlike the above, this only searches the already
388
+ // loaded ones.
389
+ // FIXME: Find a way to have this continue to happen after.
390
+ // rdar://problem/19045112
391
+ dl_iterate_phdr (_addImageProtocolConformances, &inspectArgs);
392
+ }
346
393
#elif defined(__CYGWIN__)
347
394
static int _addImageProtocolConformances (struct dl_phdr_info *info,
348
- size_t size, void * /* data*/ ) {
395
+ size_t size, void *data) {
396
+ InspectArgs *inspectArgs = (InspectArgs *)data;
397
+ // inspectArgs contains addImage*Block function and the section name
398
+
349
399
void *handle;
350
400
if (!info->dlpi_name || info->dlpi_name [0 ] == ' \0 ' ) {
351
401
handle = dlopen (nullptr , RTLD_LAZY);
@@ -354,40 +404,26 @@ static int _addImageProtocolConformances(struct dl_phdr_info *info,
354
404
355
405
unsigned long conformancesSize;
356
406
const uint8_t *conformances =
357
- _swift_getSectionDataPE (handle, SWIFT_PROTOCOL_CONFORMANCES_SECTION ,
407
+ _swift_getSectionDataPE (handle, inspectArgs-> sectionName ,
358
408
&conformancesSize);
359
409
360
- if (!conformances) {
361
- // if there are no conformances, don't hold this handle open.
362
- dlclose (handle);
363
- return 0 ;
364
- }
365
-
366
- _addImageProtocolConformancesBlock (conformances, conformancesSize);
410
+ if (conformances)
411
+ inspectArgs->fnAddImageBlock (conformances, conformancesSize);
367
412
368
413
dlclose (handle);
369
414
return 0 ;
370
415
}
371
- #endif
372
416
373
- static void _initializeCallbacksToInspectDylib () {
374
- #if defined(__APPLE__) && defined(__MACH__)
375
- // Install our dyld callback.
376
- // Dyld will invoke this on our behalf for all images that have already
377
- // been loaded.
378
- _dyld_register_func_for_add_image (_addImageProtocolConformances);
379
- #elif defined(__ELF__)
380
- // Search the loaded dls. Unlike the above, this only searches the already
381
- // loaded ones.
382
- // FIXME: Find a way to have this continue to happen after.
383
- // rdar://problem/19045112
384
- dl_iterate_phdr (_addImageProtocolConformances, nullptr );
385
- #elif defined(__CYGWIN__)
386
- _swift_dl_iterate_phdr (_addImageProtocolConformances, nullptr );
417
+ void swift::_swift_initializeCallbacksToInspectDylib (
418
+ void (*fnAddImageBlock)(const uint8_t *, size_t ),
419
+ const char *sectionName) {
420
+ InspectArgs inspectArgs = {fnAddImageBlock, sectionName};
421
+
422
+ _swift_dl_iterate_phdr (_addImageProtocolConformances, &inspectArgs);
423
+ }
387
424
#else
388
425
# error No known mechanism to inspect dynamic libraries on this platform.
389
426
#endif
390
- }
391
427
392
428
// This variable is used to signal when a cache was generated and
393
429
// it is correct to avoid a new scan.
0 commit comments