-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyldAPIsInLibSystem.cpp
2057 lines (1703 loc) · 56 KB
/
dyldAPIsInLibSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
*
* Copyright (c) 2004-2012 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <stddef.h>
#include <string.h>
#include <malloc/malloc.h>
#include <sys/mman.h>
#include <execinfo.h>
#include <System/sys/csr.h>
#include <crt_externs.h>
#include <Availability.h>
#if !TARGET_OS_DRIVERKIT
#include <vproc_priv.h>
#endif
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <System/sys/codesign.h>
#include <mach-o/dyld_images.h>
#include <mach-o/dyld.h>
#include <mach-o/dyld_priv.h>
#include "dyld_cache_format.h"
#include "objc-shared-cache.h"
#include "ImageLoader.h"
#include "dyldLock.h"
#include "APIs.h"
#include "AllImages.h"
#include "StartGlue.h"
#include "Tracing.h"
// this was in dyld_priv.h but it is no longer exported
extern "C" {
const struct dyld_all_image_infos* _dyld_get_all_image_infos() __attribute__((visibility("hidden")));
}
extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
extern "C" void __cxa_finalize(const void *dso);
extern "C" void __cxa_finalize_ranges(const struct __cxa_range_t ranges[], int count);
//
// private interface between libSystem.dylib and dyld
//
extern "C" int _dyld_func_lookup(const char* dyld_func_name, void **address);
#if TARGET_OS_IOS && !TARGET_OS_SIMULATOR
namespace dyld3 {
extern int compatFuncLookup(const char* name, void** address) __API_AVAILABLE(ios(13.0));
}
extern "C" void setLookupFunc(void*);
#endif
extern bool gUseDyld3;
// deprecated APIs are still availble on Mac OS X, but not on iPhone OS
#if __IPHONE_OS_VERSION_MIN_REQUIRED || TARGET_OS_DRIVERKIT
#define DEPRECATED_APIS_SUPPORTED 0
#else
#define DEPRECATED_APIS_SUPPORTED 1
#endif
/*
* names_match() takes an install_name from an LC_LOAD_DYLIB command and a
* libraryName (which is -lx or -framework Foo argument passed to the static
* link editor for the same library) and determines if they match. This depends
* on conventional use of names including major versioning.
*/
static
bool
names_match(
const char *install_name,
const char* libraryName)
{
const char *basename;
unsigned long n;
/*
* Conventional install names have these forms:
* /System/Library/Frameworks/AppKit.framework/Versions/A/Appkit
* /Local/Library/Frameworks/AppKit.framework/Appkit
* /lib/libsys_s.A.dylib
* /usr/lib/libsys_s.dylib
*/
basename = strrchr(install_name, '/');
if(basename == NULL)
basename = install_name;
else
basename++;
/*
* By checking the base name matching the library name we take care
* of the -framework cases.
*/
if(strcmp(basename, libraryName) == 0)
return true;
/*
* Now check the base name for "lib" if so proceed to check for the
* -lx case dealing with a possible .X.dylib and a .dylib extension.
*/
if(strncmp(basename, "lib", 3) ==0){
n = strlen(libraryName);
if(strncmp(basename+3, libraryName, n) == 0){
if(strncmp(basename+3+n, ".dylib", 6) == 0)
return true;
if(basename[3+n] == '.' &&
basename[3+n+1] != '\0' &&
strncmp(basename+3+n+2, ".dylib", 6) == 0)
return true;
}
}
return false;
}
#if DEPRECATED_APIS_SUPPORTED
void NSInstallLinkEditErrorHandlers(
const NSLinkEditErrorHandlers* handlers)
{
if ( gUseDyld3 )
return dyld3::NSInstallLinkEditErrorHandlers(handlers);
DYLD_LOCK_THIS_BLOCK;
typedef void (*ucallback_t)(const char* symbol_name);
typedef NSModule (*mcallback_t)(NSSymbol s, NSModule old, NSModule newhandler);
typedef void (*lcallback_t)(NSLinkEditErrors c, int errorNumber,
const char* fileName, const char* errorString);
static void (*p)(ucallback_t undefined, mcallback_t multiple, lcallback_t linkEdit) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_install_handlers", (void**)&p);
mcallback_t m = handlers->multiple;
p(handlers->undefined, m, handlers->linkEdit);
}
const char*
NSNameOfModule(
NSModule module)
{
if ( gUseDyld3 )
return dyld3::NSNameOfModule(module);
DYLD_LOCK_THIS_BLOCK;
static const char* (*p)(NSModule module) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSNameOfModule", (void**)&p);
return(p(module));
}
const char*
NSLibraryNameForModule(
NSModule module)
{
if ( gUseDyld3 )
return dyld3::NSLibraryNameForModule(module);
DYLD_LOCK_THIS_BLOCK;
static const char* (*p)(NSModule module) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLibraryNameForModule", (void**)&p);
return(p(module));
}
bool
NSIsSymbolNameDefined(
const char* symbolName)
{
if ( gUseDyld3 )
return dyld3::NSIsSymbolNameDefined(symbolName);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(const char* symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefined", (void**)&p);
return(p(symbolName));
}
bool
NSIsSymbolNameDefinedWithHint(
const char* symbolName,
const char* libraryNameHint)
{
if ( gUseDyld3 )
return dyld3::NSIsSymbolNameDefinedWithHint(symbolName, libraryNameHint);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(const char* symbolName,
const char* libraryNameHint) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefinedWithHint", (void**)&p);
return(p(symbolName, libraryNameHint));
}
bool
NSIsSymbolNameDefinedInImage(
const struct mach_header *image,
const char* symbolName)
{
if ( gUseDyld3 )
return dyld3::NSIsSymbolNameDefinedInImage(image, symbolName);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(const struct mach_header *image,
const char* symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefinedInImage", (void**)&p);
return(p(image, symbolName));
}
NSSymbol
NSLookupAndBindSymbol(
const char* symbolName)
{
if ( gUseDyld3 )
return dyld3::NSLookupAndBindSymbol(symbolName);
DYLD_LOCK_THIS_BLOCK;
static NSSymbol (*p)(const char* symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupAndBindSymbol", (void**)&p);
return(p(symbolName));
}
NSSymbol
NSLookupAndBindSymbolWithHint(
const char* symbolName,
const char* libraryNameHint)
{
if ( gUseDyld3 )
return dyld3::NSLookupAndBindSymbolWithHint(symbolName, libraryNameHint);
DYLD_LOCK_THIS_BLOCK;
static NSSymbol (*p)(const char* symbolName,
const char* libraryNameHint) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupAndBindSymbolWithHint", (void**)&p);
return(p(symbolName, libraryNameHint));
}
NSSymbol
NSLookupSymbolInModule(
NSModule module,
const char* symbolName)
{
if ( gUseDyld3 )
return dyld3::NSLookupSymbolInModule(module, symbolName);
DYLD_LOCK_THIS_BLOCK;
static NSSymbol (*p)(NSModule module, const char* symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupSymbolInModule", (void**)&p);
return(p(module, symbolName));
}
NSSymbol
NSLookupSymbolInImage(
const struct mach_header *image,
const char* symbolName,
uint32_t options)
{
if ( gUseDyld3 )
return dyld3::NSLookupSymbolInImage(image, symbolName, options);
DYLD_LOCK_THIS_BLOCK;
static NSSymbol (*p)(const struct mach_header *image,
const char* symbolName,
uint32_t options) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupSymbolInImage", (void**)&p);
return(p(image, symbolName, options));
}
const char*
NSNameOfSymbol(
NSSymbol symbol)
{
if ( gUseDyld3 )
return dyld3::NSNameOfSymbol(symbol);
DYLD_LOCK_THIS_BLOCK;
static char * (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSNameOfSymbol",(void**)&p);
return(p(symbol));
}
void *
NSAddressOfSymbol(
NSSymbol symbol)
{
if ( gUseDyld3 )
return dyld3::NSAddressOfSymbol(symbol);
DYLD_LOCK_THIS_BLOCK;
static void * (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddressOfSymbol", (void**)&p);
return(p(symbol));
}
NSModule
NSModuleForSymbol(
NSSymbol symbol)
{
if ( gUseDyld3 )
return dyld3::NSModuleForSymbol(symbol);
DYLD_LOCK_THIS_BLOCK;
static NSModule (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSModuleForSymbol", (void**)&p);
return(p(symbol));
}
bool
NSAddLibrary(
const char* pathName)
{
if ( gUseDyld3 )
return dyld3::NSAddLibrary(pathName);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(const char* pathName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddLibrary", (void**)&p);
return(p(pathName));
}
bool
NSAddLibraryWithSearching(
const char* pathName)
{
if ( gUseDyld3 )
return dyld3::NSAddLibrary(pathName);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(const char* pathName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddLibraryWithSearching", (void**)&p);
return(p(pathName));
}
const struct mach_header *
NSAddImage(
const char* image_name,
uint32_t options)
{
if ( gUseDyld3 )
return dyld3::NSAddImage(image_name, options);
DYLD_LOCK_THIS_BLOCK;
static const struct mach_header * (*p)(const char* image_name,
uint32_t options) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddImage", (void**)&p);
return(p(image_name, options));
}
#endif // DEPRECATED_APIS_SUPPORTED
/*
* This routine returns the current version of the named shared library the
* executable it was built with. The libraryName parameter is the same as the
* -lx or -framework Foo argument passed to the static link editor when building
* the executable (with -lx it would be "x" and with -framework Foo it would be
* "Foo"). If this the executable was not built against the specified library
* it returns -1. It should be noted that if this only returns the value the
* current version of the named shared library the executable was built with
* and not a list of current versions that dependent libraries and bundles the
* program is using were built with.
*/
int32_t NSVersionOfLinkTimeLibrary(const char* libraryName)
{
if ( gUseDyld3 )
return dyld3::NSVersionOfLinkTimeLibrary(libraryName);
// Lazily call _NSGetMachExecuteHeader() and cache result
#if __LP64__
static mach_header_64* mh = NULL;
#else
static mach_header* mh = NULL;
#endif
if ( mh == NULL )
mh = _NSGetMachExecuteHeader();
#if __LP64__
const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header_64));
#else
const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header));
#endif
for(uint32_t i = 0; i < mh->ncmds; i++){
switch ( lc->cmd ) {
case LC_LOAD_DYLIB:
case LC_LOAD_WEAK_DYLIB:
case LC_LOAD_UPWARD_DYLIB:
const dylib_command* dl = (dylib_command *)lc;
const char* install_name = (char*)dl + dl->dylib.name.offset;
if ( names_match(install_name, libraryName) )
return dl->dylib.current_version;
break;
}
lc = (load_command*)((char*)lc + lc->cmdsize);
}
return (-1);
}
/*
* This routine returns the current version of the named shared library the
* program it is running against. The libraryName parameter is the same as
* would be static link editor using the -lx or -framework Foo flags (with -lx
* it would be "x" and with -framework Foo it would be "Foo"). If the program
* is not using the specified library it returns -1.
*/
int32_t NSVersionOfRunTimeLibrary(const char* libraryName)
{
if ( gUseDyld3 )
return dyld3::NSVersionOfRunTimeLibrary(libraryName);
uint32_t n = _dyld_image_count();
for(uint32_t i = 0; i < n; i++){
const mach_header* mh = _dyld_get_image_header(i);
if ( mh == NULL )
continue;
if ( mh->filetype != MH_DYLIB )
continue;
#if __LP64__
const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header_64));
#else
const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header));
#endif
for(uint32_t j = 0; j < mh->ncmds; j++){
if ( lc->cmd == LC_ID_DYLIB ) {
const dylib_command* dl = (dylib_command*)lc;
const char* install_name = (char *)dl + dl->dylib.name.offset;
if ( names_match(install_name, libraryName) )
return dl->dylib.current_version;
}
lc = (load_command*)((char*)lc + lc->cmdsize);
}
}
return (-1);
}
#if TARGET_OS_WATCH
uint32_t dyld_get_program_sdk_watch_os_version()
{
if (gUseDyld3)
return dyld3::dyld_get_program_sdk_watch_os_version();
__block uint32_t retval = 0;
__block bool versionFound = false;
dyld3::dyld_get_image_versions((mach_header*)_NSGetMachExecuteHeader(), ^(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version) {
if (versionFound) return;
if (dyld_get_base_platform(platform) == PLATFORM_WATCHOS) {
versionFound = true;
retval = sdk_version;
}
});
return retval;
}
uint32_t dyld_get_program_min_watch_os_version()
{
if (gUseDyld3)
return dyld3::dyld_get_program_min_watch_os_version();
__block uint32_t retval = 0;
__block bool versionFound = false;
dyld3::dyld_get_image_versions((mach_header*)_NSGetMachExecuteHeader(), ^(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version) {
if (versionFound) return;
if (dyld_get_base_platform(platform) == PLATFORM_WATCHOS) {
versionFound = true;
retval = min_version;
}
});
return retval;
}
#endif
#if TARGET_OS_BRIDGE
uint32_t dyld_get_program_sdk_bridge_os_version()
{
if (gUseDyld3)
return dyld3::dyld_get_program_sdk_bridge_os_version();
__block uint32_t retval = 0;
__block bool versionFound = false;
dyld3::dyld_get_image_versions((mach_header*)_NSGetMachExecuteHeader(), ^(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version) {
if (versionFound) return;
if (dyld_get_base_platform(platform) == PLATFORM_BRIDGEOS) {
versionFound = true;
retval = sdk_version;
}
});
return retval;
}
uint32_t dyld_get_program_min_bridge_os_version()
{
if (gUseDyld3)
return dyld3::dyld_get_program_min_bridge_os_version();
__block uint32_t retval = 0;
__block bool versionFound = false;
dyld3::dyld_get_image_versions((mach_header*)_NSGetMachExecuteHeader(), ^(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version) {
if (versionFound) return;
if (dyld_get_base_platform(platform) == PLATFORM_BRIDGEOS) {
versionFound = true;
retval = min_version;
}
});
return retval;
}
#endif
/*
* Returns the sdk version (encode as nibble XXXX.YY.ZZ) the
* specified binary was built against.
*
* First looks for LC_VERSION_MIN_* in binary and if sdk field is
* not zero, return that value.
* Otherwise, looks for the libSystem.B.dylib the binary linked
* against and uses a table to convert that to an sdk version.
*/
uint32_t dyld_get_sdk_version(const mach_header* mh)
{
return dyld3::dyld_get_sdk_version(mh);
}
uint32_t dyld_get_program_sdk_version()
{
return dyld3::dyld_get_sdk_version((mach_header*)_NSGetMachExecuteHeader());
}
uint32_t dyld_get_min_os_version(const struct mach_header* mh)
{
return dyld3::dyld_get_min_os_version(mh);
}
uint32_t dyld_get_program_min_os_version()
{
return dyld3::dyld_get_min_os_version((mach_header*)_NSGetMachExecuteHeader());
}
bool _dyld_get_image_uuid(const struct mach_header* mh, uuid_t uuid)
{
if ( gUseDyld3 )
return dyld3::_dyld_get_image_uuid(mh, uuid);
const load_command* startCmds = NULL;
if ( mh->magic == MH_MAGIC_64 )
startCmds = (load_command*)((char *)mh + sizeof(mach_header_64));
else if ( mh->magic == MH_MAGIC )
startCmds = (load_command*)((char *)mh + sizeof(mach_header));
else
return false; // not a mach-o file, or wrong endianness
const load_command* const cmdsEnd = (load_command*)((char*)startCmds + mh->sizeofcmds);
const load_command* cmd = startCmds;
for(uint32_t i = 0; i < mh->ncmds; ++i) {
const load_command* nextCmd = (load_command*)((char *)cmd + cmd->cmdsize);
if ( (cmd->cmdsize < 8) || (nextCmd > cmdsEnd) || (nextCmd < startCmds)) {
return false;
}
if ( cmd->cmd == LC_UUID ) {
const uuid_command* uuidCmd = (uuid_command*)cmd;
memcpy(uuid, uuidCmd->uuid, 16);
return true;
}
cmd = nextCmd;
}
bzero(uuid, 16);
return false;
}
dyld_platform_t dyld_get_active_platform(void) {
if (gUseDyld3) { return dyld3::dyld_get_active_platform(); }
if (_dyld_get_all_image_infos()->version >= 16) { return (dyld_platform_t)_dyld_get_all_image_infos()->platform; }
__block dyld_platform_t result;
// FIXME: Remove this once we only care about version 16 or greater all image infos
dyld3::dyld_get_image_versions((mach_header*)_NSGetMachExecuteHeader(), ^(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version) {
result = platform;
});
return result;
}
dyld_platform_t dyld_get_base_platform(dyld_platform_t platform) {
return dyld3::dyld_get_base_platform(platform);
}
bool dyld_is_simulator_platform(dyld_platform_t platform) {
return dyld3::dyld_is_simulator_platform(platform);
}
bool dyld_sdk_at_least(const struct mach_header* mh, dyld_build_version_t version) {
return dyld3::dyld_sdk_at_least(mh, version);
}
bool dyld_minos_at_least(const struct mach_header* mh, dyld_build_version_t version) {
return dyld3::dyld_minos_at_least(mh, version);
}
bool dyld_program_sdk_at_least(dyld_build_version_t version) {
return dyld3::dyld_sdk_at_least((mach_header*)_NSGetMachExecuteHeader(),version);
}
bool dyld_program_minos_at_least(dyld_build_version_t version) {
return dyld3::dyld_minos_at_least((mach_header*)_NSGetMachExecuteHeader(), version);
}
// Function that walks through the load commands and calls the internal block for every version found
// Intended as a fallback for very complex (and rare) version checks, or for tools that need to
// print our everything for diagnostic reasons
void dyld_get_image_versions(const struct mach_header* mh, void (^callback)(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version)) {
dyld3::dyld_get_image_versions(mh, callback);
}
#if DEPRECATED_APIS_SUPPORTED
/*
* NSCreateObjectFileImageFromFile() creates an NSObjectFileImage for the
* specified file name if the file is a correct Mach-O file that can be loaded
* with NSloadModule(). For return codes of NSObjectFileImageFailure and
* NSObjectFileImageFormat an error message is printed to stderr. All
* other codes cause no printing.
*/
NSObjectFileImageReturnCode
NSCreateObjectFileImageFromFile(
const char* pathName,
NSObjectFileImage *objectFileImage)
{
if ( gUseDyld3 )
return dyld3::NSCreateObjectFileImageFromFile(pathName, objectFileImage);
DYLD_LOCK_THIS_BLOCK;
static NSObjectFileImageReturnCode (*p)(const char*, NSObjectFileImage*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSCreateObjectFileImageFromFile", (void**)&p);
return p(pathName, objectFileImage);
}
/*
* NSCreateObjectFileImageFromMemory() creates an NSObjectFileImage for the
* object file mapped into memory at address of size length if the object file
* is a correct Mach-O file that can be loaded with NSloadModule(). For return
* codes of NSObjectFileImageFailure and NSObjectFileImageFormat an error
* message is printed to stderr. All other codes cause no printing.
*/
NSObjectFileImageReturnCode
NSCreateObjectFileImageFromMemory(
const void* address,
size_t size,
NSObjectFileImage *objectFileImage)
{
// <rdar://problem/51812762> NSCreatObjectFileImageFromMemory fail opaquely if Hardened runtime is enabled
uint32_t flags;
if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
if ( (flags & (CS_ENFORCEMENT|CS_KILL)) == (CS_ENFORCEMENT|CS_KILL) ) {
//fprintf(stderr, "dyld: warning: NSCreatObjectFileImageFromMemory() cannot be used in harden process 0x%08X\n", flags);
return NSObjectFileImageAccess;
}
}
if ( gUseDyld3 )
return dyld3::NSCreateObjectFileImageFromMemory(address, size, objectFileImage);
DYLD_LOCK_THIS_BLOCK;
static NSObjectFileImageReturnCode (*p)(const void*, size_t, NSObjectFileImage*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSCreateObjectFileImageFromMemory", (void**)&p);
return p(address, size, objectFileImage);
}
#if OBSOLETE_DYLD_API
/*
* NSCreateCoreFileImageFromFile() creates an NSObjectFileImage for the
* specified core file name if the file is a correct Mach-O core file.
* For return codes of NSObjectFileImageFailure and NSObjectFileImageFormat
* an error message is printed to stderr. All other codes cause no printing.
*/
NSObjectFileImageReturnCode
NSCreateCoreFileImageFromFile(
const char* pathName,
NSObjectFileImage *objectFileImage)
{
DYLD_LOCK_THIS_BLOCK;
static NSObjectFileImageReturnCode (*p)(const char*, NSObjectFileImage*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSCreateCoreFileImageFromFile", (void**)&p);
return p(pathName, objectFileImage);
}
#endif
bool
NSDestroyObjectFileImage(
NSObjectFileImage objectFileImage)
{
if ( gUseDyld3 )
return dyld3::NSDestroyObjectFileImage(objectFileImage);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(NSObjectFileImage) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSDestroyObjectFileImage", (void**)&p);
return p(objectFileImage);
}
NSModule
NSLinkModule(
NSObjectFileImage objectFileImage,
const char* moduleName,
uint32_t options)
{
if ( gUseDyld3 )
return dyld3::NSLinkModule(objectFileImage, moduleName, options);
DYLD_LOCK_THIS_BLOCK;
static NSModule (*p)(NSObjectFileImage, const char*, unsigned long) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLinkModule", (void**)&p);
return p(objectFileImage, moduleName, options);
}
/*
* NSSymbolDefinitionCountInObjectFileImage() returns the number of symbol
* definitions in the NSObjectFileImage.
*/
uint32_t
NSSymbolDefinitionCountInObjectFileImage(
NSObjectFileImage objectFileImage)
{
if ( gUseDyld3 )
return dyld3::NSSymbolDefinitionCountInObjectFileImage(objectFileImage);
DYLD_LOCK_THIS_BLOCK;
static uint32_t (*p)(NSObjectFileImage) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSSymbolDefinitionCountInObjectFileImage", (void**)&p);
return p(objectFileImage);
}
/*
* NSSymbolDefinitionNameInObjectFileImage() returns the name of the i'th
* symbol definitions in the NSObjectFileImage. If the ordinal specified is
* outside the range [0..NSSymbolDefinitionCountInObjectFileImage], NULL will
* be returned.
*/
const char*
NSSymbolDefinitionNameInObjectFileImage(
NSObjectFileImage objectFileImage,
uint32_t ordinal)
{
if ( gUseDyld3 )
return dyld3::NSSymbolDefinitionNameInObjectFileImage(objectFileImage, ordinal);
DYLD_LOCK_THIS_BLOCK;
static const char* (*p)(NSObjectFileImage, uint32_t) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSSymbolDefinitionNameInObjectFileImage", (void**)&p);
return p(objectFileImage, ordinal);
}
/*
* NSSymbolReferenceCountInObjectFileImage() returns the number of references
* to undefined symbols the NSObjectFileImage.
*/
uint32_t
NSSymbolReferenceCountInObjectFileImage(
NSObjectFileImage objectFileImage)
{
if ( gUseDyld3 )
return dyld3::NSSymbolReferenceCountInObjectFileImage(objectFileImage);
DYLD_LOCK_THIS_BLOCK;
static uint32_t (*p)(NSObjectFileImage) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSSymbolReferenceCountInObjectFileImage", (void**)&p);
return p(objectFileImage);
}
/*
* NSSymbolReferenceNameInObjectFileImage() returns the name of the i'th
* undefined symbol in the NSObjectFileImage. If the ordinal specified is
* outside the range [0..NSSymbolReferenceCountInObjectFileImage], NULL will be
* returned.
*/
const char*
NSSymbolReferenceNameInObjectFileImage(
NSObjectFileImage objectFileImage,
uint32_t ordinal,
bool *tentative_definition) /* can be NULL */
{
if ( gUseDyld3 )
return dyld3::NSSymbolReferenceNameInObjectFileImage(objectFileImage, ordinal, tentative_definition);
DYLD_LOCK_THIS_BLOCK;
static const char* (*p)(NSObjectFileImage, uint32_t, bool*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSSymbolReferenceNameInObjectFileImage", (void**)&p);
return p(objectFileImage, ordinal, tentative_definition);
}
/*
* NSIsSymbolDefinedInObjectFileImage() returns TRUE if the specified symbol
* name has a definition in the NSObjectFileImage and FALSE otherwise.
*/
bool
NSIsSymbolDefinedInObjectFileImage(
NSObjectFileImage objectFileImage,
const char* symbolName)
{
if ( gUseDyld3 )
return dyld3::NSIsSymbolDefinedInObjectFileImage(objectFileImage, symbolName);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(NSObjectFileImage, const char*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolDefinedInObjectFileImage", (void**)&p);
return p(objectFileImage, symbolName);
}
/*
* NSGetSectionDataInObjectFileImage() returns a pointer to the section contents
* in the NSObjectFileImage for the specified segmentName and sectionName if
* it exists and it is not a zerofill section. If not it returns NULL. If
* the parameter size is not NULL the size of the section is also returned
* indirectly through that pointer.
*/
void *
NSGetSectionDataInObjectFileImage(
NSObjectFileImage objectFileImage,
const char* segmentName,
const char* sectionName,
unsigned long *size) /* can be NULL */
{
if ( gUseDyld3 )
return dyld3::NSGetSectionDataInObjectFileImage(objectFileImage, segmentName, sectionName, size);
DYLD_LOCK_THIS_BLOCK;
static void* (*p)(NSObjectFileImage, const char*, const char*, unsigned long*) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSGetSectionDataInObjectFileImage", (void**)&p);
return p(objectFileImage, segmentName, sectionName, size);
}
void
NSLinkEditError(
NSLinkEditErrors *c,
int *errorNumber,
const char* *fileName,
const char* *errorString)
{
if ( gUseDyld3 )
return dyld3::NSLinkEditError(c, errorNumber, fileName, errorString);
DYLD_LOCK_THIS_BLOCK;
static void (*p)(NSLinkEditErrors *c,
int *errorNumber,
const char* *fileName,
const char* *errorString) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_link_edit_error", (void**)&p);
if(p != NULL)
p(c, errorNumber, fileName, errorString);
}
bool
NSUnLinkModule(
NSModule module,
uint32_t options)
{
if ( gUseDyld3 )
return dyld3::NSUnLinkModule(module, options);
DYLD_LOCK_THIS_BLOCK;
static bool (*p)(NSModule module, uint32_t options) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_unlink_module", (void**)&p);
return p(module, options);
}
#if OBSOLETE_DYLD_API
NSModule
NSReplaceModule(
NSModule moduleToReplace,
NSObjectFileImage newObjectFileImage,
uint32_t options)
{
return(NULL);
}
#endif
#endif // DEPRECATED_APIS_SUPPORTED
/*
*_NSGetExecutablePath copies the path of the executable into the buffer and
* returns 0 if the path was successfully copied in the provided buffer. If the
* buffer is not large enough, -1 is returned and the expected buffer size is
* copied in *bufsize. Note that _NSGetExecutablePath will return "a path" to
* the executable not a "real path" to the executable. That is the path may be
* a symbolic link and not the real file. And with deep directories the total
* bufsize needed could be more than MAXPATHLEN.
*/
int
_NSGetExecutablePath(
char *buf,
uint32_t *bufsize)
{
if ( gUseDyld3 )
return dyld3::_NSGetExecutablePath(buf, bufsize);
DYLD_NO_LOCK_THIS_BLOCK;
static int (*p)(char *buf, uint32_t *bufsize) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld__NSGetExecutablePath", (void**)&p);
return(p(buf, bufsize));
}