25
25
#include " SwiftRemoteMirrorLegacyInteropTypes.h"
26
26
#include " SwiftRemoteMirror.h"
27
27
28
+ #include < string.h>
28
29
#include < dlfcn.h>
29
30
#include < mach-o/getsect.h>
30
31
37
38
static inline SwiftReflectionInteropContextRef
38
39
swift_reflection_interop_createReflectionContext (
39
40
void *ReaderContext,
40
- void *LibraryHandle,
41
- void *LegacyLibraryHandle,
42
41
uint8_t PointerSize,
43
42
FreeBytesFunction FreeBytes,
44
43
ReadBytesFunction ReadBytes,
45
44
GetStringLengthFunction GetStringLength,
46
45
GetSymbolAddressFunction GetSymbolAddress);
47
46
47
+ // / Add a library handle to the interop context. Returns 1 if the
48
+ // / library was added successfully, 0 if a symbol couldn't be looked up
49
+ // / or the reported metadata version is too old.
50
+ static inline int
51
+ swift_reflection_interop_addLibrary (
52
+ SwiftReflectionInteropContextRef ContextRef, void *LibraryHandle);
53
+
48
54
static inline void
49
55
swift_reflection_interop_destroyReflectionContext (
50
56
SwiftReflectionInteropContextRef ContextRef);
@@ -283,6 +289,7 @@ struct SwiftReflectionInteropContextLegacyImageRangeList {
283
289
284
290
struct SwiftReflectionInteropContext {
285
291
void *ReaderContext;
292
+ uint8_t PointerSize;
286
293
FreeBytesFunction FreeBytes;
287
294
ReadBytesFunction ReadBytes;
288
295
uint64_t (*GetStringLength)(void *reader_context,
@@ -291,8 +298,7 @@ struct SwiftReflectionInteropContext {
291
298
const char *name,
292
299
uint64_t name_length);
293
300
294
- // Currently we support at most two libraries.
295
- struct SwiftReflectionInteropContextLibrary Libraries[2 ];
301
+ struct SwiftReflectionInteropContextLibrary *Libraries;
296
302
int LibraryCount;
297
303
298
304
struct SwiftReflectionInteropContextFreeList *FreeList;
@@ -381,12 +387,11 @@ swift_reflection_interop_libraryForObject(
381
387
return swift_reflection_interop_libraryForAddress (ContextRef, Metadata);
382
388
}
383
389
384
- static inline void
390
+ static inline int
385
391
swift_reflection_interop_loadFunctions (struct SwiftReflectionInteropContext *Context,
386
- void *Handle,
387
- int IsLegacy) {
392
+ void *Handle) {
388
393
if (Handle == NULL )
389
- return ;
394
+ return 0 ;
390
395
391
396
struct SwiftReflectionInteropContextLibrary *Library = &Context
392
397
->Libraries [Context->LibraryCount ];
@@ -397,14 +402,16 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
397
402
#endif
398
403
#define LOAD_NAMED (field, symbol ) do { \
399
404
Functions->field = (decltype (Functions->field ))dlsym (Handle, symbol); \
400
- if (Functions->field == NULL ) return ; \
405
+ if (Functions->field == NULL ) return 0 ; \
401
406
} while (0 )
402
407
#define LOAD (name ) LOAD_NAMED(name, " swift_reflection_" #name)
403
408
404
409
LOAD (getSupportedMetadataVersion);
405
410
uint16_t version = Functions->getSupportedMetadataVersion ();
406
411
if (version < SWIFT_LEGACY_METADATA_MIN_VERSION)
407
- return ;
412
+ return 0 ;
413
+
414
+ int IsLegacy = dlsym (Handle, " swift_reflection_addImage" ) == NULL ;
408
415
409
416
if (IsLegacy) {
410
417
LOAD_NAMED (createReflectionContextLegacy, " swift_reflection_createReflectionContext" );
@@ -438,7 +445,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
438
445
Library->IsLegacy = IsLegacy;
439
446
Context->LibraryCount ++;
440
447
441
- return ;
448
+ return 1 ;
442
449
443
450
#undef LOAD
444
451
#undef LOAD_NAMED
@@ -470,6 +477,7 @@ swift_reflection_interop_readBytesAdapter(void *reader_context,
470
477
static inline uint8_t
471
478
swift_reflection_interop_getSizeAdapter (void *reader_context) {
472
479
// Legacy library doesn't pay attention to these anyway.
480
+ (void )reader_context;
473
481
return sizeof (void *);
474
482
}
475
483
@@ -492,8 +500,6 @@ swift_reflection_interop_GetSymbolAddressAdapter(
492
500
static inline SwiftReflectionInteropContextRef
493
501
swift_reflection_interop_createReflectionContext (
494
502
void *ReaderContext,
495
- void *LibraryHandle,
496
- void *LegacyLibraryHandle,
497
503
uint8_t PointerSize,
498
504
FreeBytesFunction FreeBytes,
499
505
ReadBytesFunction ReadBytes,
@@ -503,15 +509,27 @@ swift_reflection_interop_createReflectionContext(
503
509
SwiftReflectionInteropContextRef ContextRef =
504
510
(SwiftReflectionInteropContextRef)calloc (sizeof (*ContextRef), 1 );
505
511
506
- swift_reflection_interop_loadFunctions (ContextRef, LibraryHandle, 0 );
507
- swift_reflection_interop_loadFunctions (ContextRef, LegacyLibraryHandle, 1 );
512
+ ContextRef->ReaderContext = ReaderContext;
513
+ ContextRef->PointerSize = PointerSize;
514
+ ContextRef->FreeBytes = FreeBytes;
515
+ ContextRef->ReadBytes = ReadBytes;
516
+ ContextRef->GetStringLength = GetStringLength;
517
+ ContextRef->GetSymbolAddress = GetSymbolAddress;
508
518
509
- if (ContextRef->LibraryCount == 0 ) {
510
- free (ContextRef);
511
- return NULL ;
512
- }
519
+ ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable (NULL , 0 , NULL , NULL );
513
520
514
- FOREACH_LIBRARY {
521
+ return ContextRef;
522
+ }
523
+
524
+ static inline int
525
+ swift_reflection_interop_addLibrary (
526
+ SwiftReflectionInteropContextRef ContextRef, void *LibraryHandle) {
527
+ size_t NewSize = (ContextRef->LibraryCount + 1 ) * sizeof (*ContextRef->Libraries );
528
+ ContextRef->Libraries = realloc (ContextRef->Libraries , NewSize);
529
+ int Success = swift_reflection_interop_loadFunctions (ContextRef, LibraryHandle);
530
+ if (Success) {
531
+ struct SwiftReflectionInteropContextLibrary *Library =
532
+ &ContextRef->Libraries [ContextRef->LibraryCount - 1 ];
515
533
if (Library->IsLegacy ) {
516
534
Library->Context = Library->Functions .createReflectionContextLegacy (
517
535
ContextRef,
@@ -520,20 +538,12 @@ swift_reflection_interop_createReflectionContext(
520
538
swift_reflection_interop_GetStringLengthAdapter,
521
539
swift_reflection_interop_GetSymbolAddressAdapter);
522
540
} else {
523
- Library->Context = Library->Functions .createReflectionContext (ReaderContext,
524
- PointerSize, FreeBytes, ReadBytes, GetStringLength, GetSymbolAddress);
541
+ Library->Context = Library->Functions .createReflectionContext (
542
+ ContextRef->ReaderContext ,
543
+ ContextRef->PointerSize , ContextRef->FreeBytes , ContextRef->ReadBytes , ContextRef->GetStringLength , ContextRef->GetSymbolAddress );
525
544
}
526
545
}
527
-
528
- ContextRef->ReaderContext = ReaderContext;
529
- ContextRef->FreeBytes = FreeBytes;
530
- ContextRef->ReadBytes = ReadBytes;
531
- ContextRef->GetStringLength = GetStringLength;
532
- ContextRef->GetSymbolAddress = GetSymbolAddress;
533
-
534
- ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable (NULL , 0 , NULL , NULL );
535
-
536
- return ContextRef;
546
+ return Success;
537
547
}
538
548
539
549
static inline void
@@ -542,6 +552,7 @@ swift_reflection_interop_destroyReflectionContext(
542
552
FOREACH_LIBRARY {
543
553
Library->Functions .destroyReflectionContext (Library->Context );
544
554
}
555
+ free (ContextRef->Libraries );
545
556
struct SwiftReflectionInteropContextLegacyImageRangeList *LegacyImageRangeList
546
557
= ContextRef->LegacyImageRangeList ;
547
558
while (LegacyImageRangeList != NULL ) {
@@ -668,14 +679,14 @@ swift_reflection_interop_addImageLegacy(
668
679
}
669
680
670
681
info.LocalStartAddress = (uintptr_t )Buf;
671
- info.RemoteStartAddress = ImageStart;
682
+ info.RemoteStartAddress = ( uintptr_t ) ImageStart;
672
683
673
684
Library->Functions .addReflectionInfoLegacy (Library->Context , info);
674
685
675
686
// Find the data segment and add it to our list.
676
687
unsigned long DataSize;
677
688
const uint8_t *DataSegment = getsegmentdata (Header, " __DATA" , &DataSize);
678
- uintptr_t DataSegmentStart = DataSegment - (const uint8_t *)Buf + ImageStart;
689
+ uintptr_t DataSegmentStart = ( uintptr_t )( DataSegment - (const uint8_t *)Buf + ImageStart) ;
679
690
680
691
struct SwiftReflectionInteropContextLegacyImageRangeList *Node =
681
692
(struct SwiftReflectionInteropContextLegacyImageRangeList *)malloc (sizeof (*Node));
@@ -687,12 +698,12 @@ swift_reflection_interop_addImageLegacy(
687
698
// If the buffer needs to be freed, save buffer and free context to free it when the
688
699
// reflection context is destroyed.
689
700
if (ContextRef->FreeBytes != NULL ) {
690
- struct SwiftReflectionInteropContextFreeList *Node =
691
- (struct SwiftReflectionInteropContextFreeList *)malloc (sizeof (*Node ));
692
- Node ->Next = ContextRef->FreeList ;
693
- Node ->Pointer = Buf;
694
- Node ->Context = FreeContext;
695
- ContextRef->FreeList = Node ;
701
+ struct SwiftReflectionInteropContextFreeList *FreeListNode =
702
+ (struct SwiftReflectionInteropContextFreeList *)malloc (sizeof (*FreeListNode ));
703
+ FreeListNode ->Next = ContextRef->FreeList ;
704
+ FreeListNode ->Pointer = Buf;
705
+ FreeListNode ->Context = FreeContext;
706
+ ContextRef->FreeList = FreeListNode ;
696
707
}
697
708
698
709
return 1 ;
@@ -736,7 +747,7 @@ swift_reflection_interop_lookupMetadata(SwiftReflectionInteropContextRef Context
736
747
swift_reflection_interop_libraryForAddress (ContextRef, Metadata);
737
748
if (Library != NULL ) {
738
749
Result.Metadata = Metadata;
739
- Result.Library = LIBRARY_INDEX;
750
+ Result.Library = ( int ) LIBRARY_INDEX;
740
751
}
741
752
return Result;
742
753
}
@@ -762,7 +773,7 @@ swift_reflection_interop_typeRefForInstance(SwiftReflectionInteropContextRef Con
762
773
swift_typeref_t Typeref = Library->Functions .typeRefForInstance (Library->Context ,
763
774
Object);
764
775
Result.Typeref = Typeref;
765
- Result.Library = LIBRARY_INDEX;
776
+ Result.Library = ( int ) LIBRARY_INDEX;
766
777
}
767
778
return Result;
768
779
}
@@ -780,7 +791,7 @@ swift_reflection_interop_typeRefForMangledTypeName(
780
791
continue ;
781
792
782
793
Result.Typeref = Typeref;
783
- Result.Library = LIBRARY_INDEX;
794
+ Result.Library = ( int ) LIBRARY_INDEX;
784
795
return Result;
785
796
}
786
797
@@ -867,7 +878,7 @@ swift_reflection_interop_childOfInstance(SwiftReflectionInteropContextRef Contex
867
878
Result.Offset = LibResult.Offset ;
868
879
Result.Kind = LibResult.Kind ;
869
880
Result.TR .Typeref = LibResult.TR ;
870
- Result.TR .Library = LIBRARY_INDEX;
881
+ Result.TR .Library = ( int ) LIBRARY_INDEX;
871
882
} else {
872
883
Result.Kind = SWIFT_UNKNOWN;
873
884
}
0 commit comments