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