@@ -44,6 +44,15 @@ swift_reflection_interop_createReflectionContext(
44
44
GetStringLengthFunction GetStringLength,
45
45
GetSymbolAddressFunction GetSymbolAddress);
46
46
47
+ static inline SwiftReflectionInteropContextRef
48
+ swift_reflection_interop_createReflectionContextWithDataLayout (
49
+ void *ReaderContext,
50
+ QueryDataLayoutFunction DataLayout,
51
+ FreeBytesFunction FreeBytes,
52
+ ReadBytesFunction ReadBytes,
53
+ GetStringLengthFunction GetStringLength,
54
+ GetSymbolAddressFunction GetSymbolAddress);
55
+
47
56
// / Add a library handle to the interop context. Returns 1 if the
48
57
// / library was added successfully, 0 if a symbol couldn't be looked up
49
58
// / or the reported metadata version is too old.
@@ -193,6 +202,15 @@ struct SwiftReflectionFunctions {
193
202
ReadBytesFunction ReadBytes,
194
203
GetStringLengthFunction GetStringLength,
195
204
GetSymbolAddressFunction GetSymbolAddress);
205
+
206
+ // Optional creation function that takes a data layout query function.
207
+ SwiftReflectionContextRef (*createReflectionContextWithDataLayout)(
208
+ void *ReaderContext,
209
+ QueryDataLayoutFunction DataLayout,
210
+ FreeBytesFunction FreeBytes,
211
+ ReadBytesFunction ReadBytes,
212
+ GetStringLengthFunction GetStringLength,
213
+ GetSymbolAddressFunction GetSymbolAddress);
196
214
197
215
SwiftReflectionContextRef (*createReflectionContextLegacy)(
198
216
void *ReaderContext,
@@ -298,7 +316,7 @@ struct SwiftReflectionInteropContextLegacyImageRangeList {
298
316
299
317
struct SwiftReflectionInteropContext {
300
318
void *ReaderContext;
301
- uint8_t PointerSize ;
319
+ QueryDataLayoutFunction DataLayout ;
302
320
FreeBytesFunction FreeBytes;
303
321
ReadBytesFunction ReadBytes;
304
322
uint64_t (*GetStringLength)(void *reader_context,
@@ -409,11 +427,12 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
409
427
#ifndef __cplusplus
410
428
#define decltype (x ) void *
411
429
#endif
412
- #define LOAD_NAMED (field, symbol ) do { \
430
+ #define LOAD_NAMED (field, symbol, required ) do { \
413
431
Functions->field = (decltype (Functions->field ))dlsym (Handle, symbol); \
414
- if (Functions->field == NULL ) return 0 ; \
432
+ if (required && Functions->field == NULL ) return 0 ; \
415
433
} while (0 )
416
- #define LOAD (name ) LOAD_NAMED(name, " swift_reflection_" #name)
434
+ #define LOAD (name ) LOAD_NAMED(name, " swift_reflection_" #name, 1 )
435
+ #define LOAD_OPT (name ) LOAD_NAMED(name, " swift_reflection_" #name, 0 )
417
436
418
437
Functions->classIsSwiftMaskPtr =
419
438
(unsigned long long *)dlsym (Handle, " swift_reflection_classIsSwiftMask" );
@@ -425,15 +444,18 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
425
444
int IsLegacy = dlsym (Handle, " swift_reflection_addImage" ) == NULL ;
426
445
427
446
if (IsLegacy) {
428
- LOAD_NAMED (createReflectionContextLegacy, " swift_reflection_createReflectionContext" );
429
- LOAD_NAMED (addReflectionInfoLegacy, " swift_reflection_addReflectionInfo" );
447
+ LOAD_NAMED (createReflectionContextLegacy, " swift_reflection_createReflectionContext" , 1 );
448
+ LOAD_NAMED (addReflectionInfoLegacy, " swift_reflection_addReflectionInfo" , 1 );
430
449
} else {
431
450
LOAD (createReflectionContext);
432
451
LOAD (addReflectionInfo);
433
452
LOAD (addImage);
434
453
LOAD (ownsObject);
435
454
LOAD (ownsAddress);
436
455
LOAD (metadataForObject);
456
+
457
+ // Optional creation function.
458
+ LOAD_OPT (createReflectionContextWithDataLayout);
437
459
}
438
460
439
461
LOAD (destroyReflectionContext);
@@ -508,6 +530,32 @@ swift_reflection_interop_GetSymbolAddressAdapter(
508
530
return Context->GetSymbolAddress (Context->ReaderContext , name, name_length);
509
531
}
510
532
533
+ static inline int
534
+ swift_reflection_interop_minimalDataLayoutQueryFunction4 (
535
+ void *ReaderContext,
536
+ DataLayoutQueryType type,
537
+ void *inBuffer, void *outBuffer) {
538
+ if (type == DLQ_GetPointerSize || type == DLQ_GetSizeSize) {
539
+ uint8_t *result = (uint8_t *)outBuffer;
540
+ *result = 4 ;
541
+ return 1 ;
542
+ }
543
+ return 0 ;
544
+ }
545
+
546
+ static inline int
547
+ swift_reflection_interop_minimalDataLayoutQueryFunction8 (
548
+ void *ReaderContext,
549
+ DataLayoutQueryType type,
550
+ void *inBuffer, void *outBuffer) {
551
+ if (type == DLQ_GetPointerSize || type == DLQ_GetSizeSize) {
552
+ uint8_t *result = (uint8_t *)outBuffer;
553
+ *result = 8 ;
554
+ return 1 ;
555
+ }
556
+ return 0 ;
557
+ }
558
+
511
559
static inline SwiftReflectionInteropContextRef
512
560
swift_reflection_interop_createReflectionContext (
513
561
void *ReaderContext,
@@ -516,12 +564,37 @@ swift_reflection_interop_createReflectionContext(
516
564
ReadBytesFunction ReadBytes,
517
565
GetStringLengthFunction GetStringLength,
518
566
GetSymbolAddressFunction GetSymbolAddress) {
519
-
567
+ QueryDataLayoutFunction DataLayout;
568
+ if (PointerSize == 4 )
569
+ DataLayout = swift_reflection_interop_minimalDataLayoutQueryFunction4;
570
+ else if (PointerSize == 8 )
571
+ DataLayout = swift_reflection_interop_minimalDataLayoutQueryFunction8;
572
+ else
573
+ abort (); // Can't handle sizes other than 4 and 8.
574
+
575
+ return swift_reflection_interop_createReflectionContextWithDataLayout (
576
+ ReaderContext,
577
+ NULL ,
578
+ FreeBytes,
579
+ ReadBytes,
580
+ GetStringLength,
581
+ GetSymbolAddress);
582
+ }
583
+
584
+ static inline SwiftReflectionInteropContextRef
585
+ swift_reflection_interop_createReflectionContextWithDataLayout (
586
+ void *ReaderContext,
587
+ QueryDataLayoutFunction DataLayout,
588
+ FreeBytesFunction FreeBytes,
589
+ ReadBytesFunction ReadBytes,
590
+ GetStringLengthFunction GetStringLength,
591
+ GetSymbolAddressFunction GetSymbolAddress) {
592
+
520
593
SwiftReflectionInteropContextRef ContextRef =
521
594
(SwiftReflectionInteropContextRef)calloc (sizeof (*ContextRef), 1 );
522
595
523
596
ContextRef->ReaderContext = ReaderContext;
524
- ContextRef->PointerSize = PointerSize ;
597
+ ContextRef->DataLayout = DataLayout ;
525
598
ContextRef->FreeBytes = FreeBytes;
526
599
ContextRef->ReadBytes = ReadBytes;
527
600
ContextRef->GetStringLength = GetStringLength;
@@ -548,10 +621,29 @@ swift_reflection_interop_addLibrary(
548
621
swift_reflection_interop_readBytesAdapter,
549
622
swift_reflection_interop_GetStringLengthAdapter,
550
623
swift_reflection_interop_GetSymbolAddressAdapter);
624
+ } else if (Library->Functions .createReflectionContextWithDataLayout ) {
625
+ Library->Context =
626
+ Library->Functions .createReflectionContextWithDataLayout (
627
+ ContextRef->ReaderContext ,
628
+ ContextRef->DataLayout ,
629
+ ContextRef->FreeBytes ,
630
+ ContextRef->ReadBytes ,
631
+ ContextRef->GetStringLength ,
632
+ ContextRef->GetSymbolAddress );
551
633
} else {
634
+ uint8_t PointerSize;
635
+ int result = ContextRef->DataLayout (
636
+ ContextRef->ReaderContext , DLQ_GetPointerSize, NULL , &PointerSize);
637
+ if (!result)
638
+ abort (); // We need the pointer size, can't proceed without it.
639
+
552
640
Library->Context = Library->Functions .createReflectionContext (
553
641
ContextRef->ReaderContext ,
554
- ContextRef->PointerSize , ContextRef->FreeBytes , ContextRef->ReadBytes , ContextRef->GetStringLength , ContextRef->GetSymbolAddress );
642
+ PointerSize,
643
+ ContextRef->FreeBytes ,
644
+ ContextRef->ReadBytes ,
645
+ ContextRef->GetStringLength ,
646
+ ContextRef->GetSymbolAddress );
555
647
}
556
648
}
557
649
return Success;
0 commit comments