-
Notifications
You must be signed in to change notification settings - Fork 0
/
kextlibs_main.c
832 lines (706 loc) · 27.2 KB
/
kextlibs_main.c
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
/*
* Copyright (c) 2006 Apple Computer, 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 <CoreFoundation/CoreFoundation.h>
#include <IOKit/kext/OSKext.h>
#include <IOKit/kext/OSKextPrivate.h>
#include "kextlibs_main.h"
#include "kext_tools_util.h"
/*******************************************************************************
* Local function prototypes and misc grotty bits.
*******************************************************************************/
const char * progname = "(unknown)";
/*******************************************************************************
*******************************************************************************/
int main(int argc, char * const * argv)
{
ExitStatus result = EX_OSERR;
ExitStatus printResult = EX_OSERR;
KextlibsArgs toolArgs;
CFArrayRef kexts = NULL; // must release
const NXArchInfo ** arches = NULL; // must free
KextlibsInfo * libInfo = NULL; // must release contents & free
Boolean libsAreArchSpecific = FALSE;
CFIndex count, i;
CFIndex numArches;
/*****
* Find out what the program was invoked as.
*/
progname = rindex(argv[0], '/');
if (progname) {
progname++; // go past the '/'
} else {
progname = (char *)argv[0];
}
/* Set the OSKext log callback right away.
*/
OSKextSetLogOutputFunction(&tool_log);
result = readArgs(argc, argv, &toolArgs);
if (result != EX_OK) {
if (result == kKextlibsExitHelp) {
result = EX_OK;
}
goto finish;
}
result = EX_OSERR;
/*****
* If necessary or requested, add the extensions folders to the
* BEGINNING of the list of folders.
*/
count = CFArrayGetCount(toolArgs.repositoryURLs);
if (!count || toolArgs.flagSysKexts) {
CFArrayRef osExtFolders = OSKextGetSystemExtensionsFolderURLs(); // do not release
if (!osExtFolders) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Library error - can't get system extensions folders.");
goto finish;
}
count = CFArrayGetCount(osExtFolders);
for (i = 0; i < count; i++) {
CFTypeRef osExtFolder = CFArrayGetValueAtIndex(osExtFolders, i);
CFIndex folderIndex = CFArrayGetFirstIndexOfValue(
toolArgs.repositoryURLs, RANGE_ALL(toolArgs.repositoryURLs),
osExtFolder);
if (folderIndex == kCFNotFound) {
CFArrayInsertValueAtIndex(toolArgs.repositoryURLs, i,
osExtFolder);
}
}
}
kexts = OSKextCreateKextsFromURLs(kCFAllocatorDefault,
toolArgs.repositoryURLs);
if (!kexts) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Can't read kexts from folders.");
goto finish;
}
toolArgs.kextURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault, (u_char *)toolArgs.kextName,
strlen(toolArgs.kextName), /* isDirectory */ true);
if (!toolArgs.kextURL) {
OSKextLogStringError(/* kext */ NULL);
goto finish;
}
toolArgs.theKext = OSKextCreate(kCFAllocatorDefault,toolArgs. kextURL);
if (!toolArgs.theKext) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Can't open %s.", toolArgs.kextName);
goto finish;
}
/* A codeless kext is either a library redirect,
* so we can't advise, or it doesn't need any libraries!
*/
if (!OSKextDeclaresExecutable(toolArgs.theKext)) {
if (OSKextIsLibrary(toolArgs.theKext)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"%s is a library without an executable; "
"defining its OSBundleLibraries (if any) is up to you.",
toolArgs.kextName);
} else {
CFDictionaryRef libs = OSKextGetValueForInfoDictionaryKey(toolArgs.theKext,
CFSTR(kOSBundleLibrariesKey));
if (libs &&
CFDictionaryGetTypeID() == CFGetTypeID(libs) &&
CFDictionaryGetCount(libs)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"%s has no executable and should not declare OSBundleLibraries.",
toolArgs.kextName);
} else {
/* In this one case, the exit status will be EX_OK.
*/
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"%s has no executable and does not need to declare OSBundleLibraries.",
toolArgs.kextName);
result = EX_OK;
}
}
goto finish;
}
arches = OSKextCopyArchitectures(toolArgs.theKext);
if (!arches || !arches[0]) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Can't determine architectures of %s.",
toolArgs.kextName);
goto finish;
}
result = EX_OK;
for (numArches = 0; arches[numArches]; numArches++) {
/* just counting */
}
libInfo = (KextlibsInfo *)malloc(numArches * sizeof(KextlibsInfo));
if (!libInfo) {
OSKextLogMemError();
goto finish;
}
/* Find libraries for the kext for each architecture in the kext.
*/
for (i = 0; i < numArches; i++) {
OSKextSetArchitecture(arches[i]);
libInfo[i].libKexts = OSKextFindLinkDependencies(toolArgs.theKext,
toolArgs.flagNonKPI, toolArgs.flagAllowUnsupported,
&libInfo[i].undefSymbols, &libInfo[i].onedefSymbols,
&libInfo[i].multdefSymbols, &libInfo[i].multdefLibs);
if (!libInfo[i].libKexts) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
}
/* If there's more than 1 arch, see if we have to print arch-specific
* results.
*/
if (numArches >= 2) {
for (i = 0; i < numArches - 1; i++) {
if (!CFEqual(libInfo[i].libKexts, libInfo[i+1].libKexts)) {
libsAreArchSpecific = TRUE;
break;
}
}
}
/* If all the libs are the same for all arches, then just print them
* once at the top of the output. Otherwise, only when doing XML,
* print the arch-specific XML declarations before the diagnostics.
*/
if (!libsAreArchSpecific) {
printResult = printLibs(&toolArgs, NULL, libInfo[0].libKexts,
/* extraNewline? */ TRUE);
/* Higher exit statuses always win.
*/
if (printResult > result) {
result = printResult;
}
} else if (toolArgs.flagXML) {
for (i = 0; i < numArches; i++) {
printResult = printLibs(&toolArgs, arches[i],
libInfo[i].libKexts,
/* extraNewline? */ i + 1 == numArches);
if (printResult > result) {
result = printResult;
}
}
}
/* Down here, for each arch, print arch-specific non-XML library declarations,
* followed by any problems found for that arch.
*/
for (i = 0; i < numArches; i++) {
if (libsAreArchSpecific && !toolArgs.flagXML) {
printResult = printLibs(&toolArgs, arches[i], libInfo[i].libKexts,
/* extraNewline? */ i + 1 == numArches);
if (printResult > result) {
result = printResult;
}
}
printResult = printProblems(&toolArgs, arches[i],
libInfo[i].undefSymbols, libInfo[i].onedefSymbols,
libInfo[i].multdefSymbols, libInfo[i].multdefLibs,
/* printArchFlag */ !libsAreArchSpecific || toolArgs.flagXML,
/* extraNewline? */ i + i < numArches);
if (printResult != EX_OK) {
/* Higher exit statuses always win.
*/
if (printResult > result) {
result = printResult;
}
}
}
finish:
/* Theoretically, we could exit w/o cleaning up. However, 12569152
points out that while clang should know exit() cleans up, its
analyzer *should* treat CF objects differently: releasing them might
have (important, expected) side effects. So we just clean up
everything and return from main, letting the runtime exit(result).
*/
SAFE_FREE(arches);
SAFE_RELEASE(toolArgs.repositoryURLs);
SAFE_RELEASE(toolArgs.kextURL);
SAFE_RELEASE(toolArgs.theKext);
SAFE_RELEASE(kexts); // this is the one clang unexpectedly noticed
if (libInfo) {
for (i = 0; i < numArches; i++) {
SAFE_RELEASE_NULL(libInfo[i].libKexts);
SAFE_RELEASE_NULL(libInfo[i].undefSymbols);
SAFE_RELEASE_NULL(libInfo[i].onedefSymbols);
SAFE_RELEASE_NULL(libInfo[i].multdefSymbols);
SAFE_RELEASE_NULL(libInfo[i].multdefLibs);
}
free(libInfo);
}
return result;
}
/*******************************************************************************
*******************************************************************************/
ExitStatus readArgs(
int argc,
char * const * argv,
KextlibsArgs * toolArgs)
{
ExitStatus result = EX_USAGE;
ExitStatus scratchResult = EX_USAGE;
int optChar = 0;
bzero(toolArgs, sizeof(*toolArgs));
/*****
* Allocate collection objects needed for command line argument processing.
*/
toolArgs->repositoryURLs = CFArrayCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeArrayCallBacks);
if (!toolArgs->repositoryURLs) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
/*****
* Process command-line arguments.
*/
result = EX_USAGE;
while ((optChar = getopt_long_only(argc, argv, kOptChars,
sOptInfo, NULL)) != -1) {
switch (optChar) {
case kOptHelp:
usage(kUsageLevelFull);
result = kKextlibsExitHelp;
goto finish;
break;
case kOptRepository:
scratchResult = addRepository(toolArgs, optarg);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
break;
case kOptCompatible:
toolArgs->flagCompatible = true;
break;
case kOptSystemExtensions:
toolArgs->flagSysKexts = true;
break;
case kOptQuiet:
beQuiet();
break;
case kOptVerbose:
scratchResult = setLogFilterForOpt(argc, argv, /* forceOnFlags */ 0);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
break;
case 0:
switch (longopt) {
case kLongOptXML:
toolArgs->flagXML = true;
break;
case kLongOptAllSymbols:
toolArgs->flagPrintUndefSymbols = true;
toolArgs->flagPrintOnedefSymbols = true;
toolArgs->flagPrintMultdefSymbols = true;
break;
case kLongOptUndefSymbols:
toolArgs->flagPrintUndefSymbols = true;
break;
case kLongOptOnedefSymbols:
toolArgs->flagPrintOnedefSymbols = true;
break;
case kLongOptMultdefSymbols:
toolArgs->flagPrintMultdefSymbols = true;
break;
case kLongOptNonKPI:
toolArgs->flagNonKPI = true;
break;
case kLongOptUnsupported:
toolArgs->flagAllowUnsupported = true;
break;
}
break;
default:
usage(kUsageLevelBrief);
goto finish;
break;
}
}
argc -= optind;
argv += optind;
if (!argv[0]) {
fprintf(stderr, "No kext specified.");
usage(kUsageLevelBrief);
goto finish;
}
scratchResult = checkPath(argv[0], kOSKextBundleExtension,
/* directoryRequired */ TRUE, /* writableRequired */ FALSE);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
toolArgs->kextName = argv[0];
argc--;
argv++;
if (argc) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Too many arguments starting at '%s'.", argv[0]);
usage(kUsageLevelBrief);
goto finish;
}
result = EX_OK;
finish:
return result;
}
/*******************************************************************************
*******************************************************************************/
ExitStatus addRepository(
KextlibsArgs * toolArgs,
const char * path)
{
ExitStatus result = EX_OSERR;
CFURLRef url = NULL; // must release
result = checkPath(path, /* suffix */ NULL,
/* dirRequired? */ TRUE, /* writableRequired? */ FALSE);
if (result != EX_OK) {
goto finish;
}
url = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault, (const UInt8 *)path, strlen(path), true);
if (!url) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Can't create CFURL for '%s'.", path);
goto finish;
}
addToArrayIfAbsent(toolArgs->repositoryURLs, url);
result = EX_OK;
finish:
SAFE_RELEASE(url);
return result;
}
/*******************************************************************************
* This function prints to stdout, as it represents the only real output of
* the program and we want to be able to pipe it into pbcopy.
*******************************************************************************/
ExitStatus printLibs(
KextlibsArgs * toolArgs,
const NXArchInfo * arch,
CFArrayRef libKexts,
Boolean trailingNewlineFlag)
{
ExitStatus result = EX_OSERR;
const NXArchInfo * genericArch = NULL; // do not free
char * libIdentifier = NULL; // must free
CFIndex count, i;
/* Get the generic architecture name for the arch, except for PPC,
* which we no longer support.
*/
if (arch && arch->cputype != CPU_TYPE_POWERPC) {
genericArch = NXGetArchInfoFromCpuType(arch->cputype,
CPU_SUBTYPE_MULTIPLE);
if (!genericArch) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Can't find generic NXArchInfo for %s.",
arch->name);
goto finish;
}
}
count = CFArrayGetCount(libKexts);
/* For XML output, don't print anything if we found no libraries;
* an empty OSBundleLibraries might look like good output to somebody.
*/
if (toolArgs->flagXML) {
if (count) {
fprintf(stdout, "\t<key>OSBundleLibraries%s%s</key>\n",
genericArch ? "_" : "",
genericArch ? genericArch->name : "");
fprintf(stdout, "\t<dict>\n");
}
} else {
if (arch) {
fprintf(stderr, "For %s:\n", arch->name);
} else {
fprintf(stderr, "For all architectures:\n");
}
if (!count) {
fprintf(stdout, " No libraries found.\n");
}
}
for (i = 0; i < count; i++) {
OSKextRef libKext = (OSKextRef)CFArrayGetValueAtIndex(libKexts, i);
OSKextVersion version;
char versCString[kOSKextVersionMaxLength];
SAFE_FREE_NULL(libIdentifier);
libIdentifier = createUTF8CStringForCFString(OSKextGetIdentifier(libKext));
if (toolArgs->flagCompatible) {
version = OSKextGetCompatibleVersion(libKext);
} else {
version = OSKextGetVersion(libKext);
}
if (libIdentifier && version > kOSKextVersionUndefined) {
OSKextVersionGetString(version, versCString, sizeof(versCString));
if (toolArgs->flagXML) {
fprintf(stdout, "\t\t<key>%s</key>\n", libIdentifier);
fprintf(stdout, "\t\t<string>%s</string>\n", versCString);
} else {
fprintf(stdout, " %s = %s\n", libIdentifier, versCString);
}
} else {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Internal error generating library list.");
goto finish;
}
}
if (count && toolArgs->flagXML) {
fprintf(stdout, "\t</dict>\n");
}
if (trailingNewlineFlag) {
fprintf(stderr, "\n");
}
result = kKextlibsExitOK;
finish:
SAFE_FREE(libIdentifier);
return result;
}
/*******************************************************************************
*******************************************************************************/
ExitStatus printProblems(
KextlibsArgs * toolArgs,
const NXArchInfo * arch,
CFDictionaryRef undefSymbols,
CFDictionaryRef onedefSymbols,
CFDictionaryRef multdefSymbols,
CFArrayRef multdefLibs,
Boolean printArchFlag,
Boolean trailingNewlineFlag)
{
ExitStatus result = EX_OSERR;
CFIndex undefCount, onedefCount, multdefCount;
CFIndex count, i;
onedefCount = CFDictionaryGetCount(onedefSymbols);
undefCount = CFDictionaryGetCount(undefSymbols);
multdefCount = CFDictionaryGetCount(multdefSymbols);
if (!toolArgs->flagPrintOnedefSymbols && !undefCount && !multdefCount) {
result = kKextlibsExitOK;
goto finish;
}
if (printArchFlag) {
fprintf(stderr, "For %s:\n", arch->name);
}
if (toolArgs->flagPrintOnedefSymbols) {
fprintf(stderr, " %ld symbol%s found in one library kext each%s\n",
onedefCount,
onedefCount > 1 ? "s" : "",
onedefCount && toolArgs->flagPrintOnedefSymbols ? ":" : ".");
CFDictionaryApplyFunction(onedefSymbols, printOnedefSymbol,
&(toolArgs->flagCompatible));
}
if (undefCount) {
fprintf(stderr, " %ld symbol%s not found in any library kext%s\n",
undefCount,
undefCount > 1 ? "s" : "",
toolArgs->flagPrintUndefSymbols ? ":" : ".");
if (toolArgs->flagPrintUndefSymbols) {
CFDictionaryApplyFunction(undefSymbols, printUndefSymbol, NULL);
}
result = kKextlibsExitUndefineds;
}
if (multdefCount) {
if (toolArgs->flagPrintMultdefSymbols) {
fprintf(stderr, " %ld symbol%s found in more than one library kext:\n",
multdefCount,
multdefCount > 1 ? "s" : "");
CFDictionaryApplyFunction(multdefSymbols, printMultdefSymbol,
&(toolArgs->flagCompatible));
} else {
count = CFArrayGetCount(multdefLibs);
fprintf(stderr, " Multiple symbols found among %ld libraries:\n",
count);
for (i = 0; i < count; i++) {
OSKextRef lib = (OSKextRef)CFArrayGetValueAtIndex(multdefLibs, i);
char * name = NULL; // must free
name = createUTF8CStringForCFString(OSKextGetIdentifier(lib));
if (name) {
fprintf(stderr, "\t%s\n", name);
}
SAFE_FREE(name);
}
}
result = kKextlibsExitMultiples;
}
if (trailingNewlineFlag) {
fprintf(stderr, "\n");
}
if (undefCount || multdefCount) {
goto finish;
}
result = kKextlibsExitOK;
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
void printUndefSymbol(const void * key,
const void * value __unused, void * context __unused)
{
char * cSymbol = NULL; // must free
cSymbol = createUTF8CStringForCFString((CFStringRef)key);
if (cSymbol) {
fprintf(stderr, "\t%s\n", cSymbol);
}
SAFE_FREE(cSymbol);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void printOnedefSymbol(const void * key, const void * value, void * context)
{
Boolean flagCompatible = *(Boolean *)context;
OSKextRef libKext = (OSKextRef)value;
char * cSymbol = NULL; // must free
char * libVers = NULL; // must free
CFURLRef libKextURL;
char libKextName[PATH_MAX];
CFStringRef libVersString = NULL; // do not release
cSymbol = createUTF8CStringForCFString((CFStringRef)key);
if (!cSymbol) {
OSKextLogStringError(/* kext */ NULL);
goto finish;
}
libKextURL = OSKextGetURL(libKext);
if (!CFURLGetFileSystemRepresentation(libKextURL,
/* resolveToBase */ false,
(u_char *)libKextName, PATH_MAX)) {
OSKextLogStringError(/* kext */ NULL);
goto finish;
}
if (flagCompatible) {
libVersString = OSKextGetValueForInfoDictionaryKey(libKext,
CFSTR("OSBundleCompatibleVersion"));
} else {
libVersString = OSKextGetValueForInfoDictionaryKey(libKext,
kCFBundleVersionKey);
}
libVers = createUTF8CStringForCFString(libVersString);
if (!libVers) {
OSKextLogStringError(/* kext */ NULL);
goto finish;
}
fprintf(stderr, " %s in %s (%s%s)\n", cSymbol, libKextName,
flagCompatible ? "compatible version " : "", libVers);
finish:
SAFE_FREE(cSymbol);
SAFE_FREE(libVers);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void printMultdefSymbol(const void * key, const void * value, void * context)
{
char * cSymbol = NULL; // must free
char * libVers = NULL; // must free
CFArrayRef libs = (CFArrayRef)value;
Boolean flagCompatible = *(Boolean *)context;
CFIndex count, i;
cSymbol = createUTF8CStringForCFString((CFStringRef)key);
if (cSymbol) {
fprintf(stderr, " %s: in\n", cSymbol);
}
count = CFArrayGetCount(libs);
for (i = 0; i < count; i++) {
OSKextRef libKext = (OSKextRef)CFArrayGetValueAtIndex(libs, i);
CFURLRef libKextURL;
char libKextName[PATH_MAX];
CFStringRef libVersString = NULL; // do not release
SAFE_FREE_NULL(libVers);
libKextURL = OSKextGetURL(libKext);
if (!CFURLGetFileSystemRepresentation(libKextURL,
/* resolveToBase */ true,
(u_char *)libKextName, PATH_MAX)) {
fprintf(stderr, "string/url conversion error\n");
goto finish;
}
if (flagCompatible) {
libVersString = OSKextGetValueForInfoDictionaryKey(libKext,
CFSTR("OSBundleCompatibleVersion"));
} else {
libVersString = OSKextGetValueForInfoDictionaryKey(libKext,
kCFBundleVersionKey);
}
libVers = createUTF8CStringForCFString(libVersString);
if (!libVers) {
fprintf(stderr, "string/url conversion error\n");
goto finish;
}
fprintf(stderr, " %s (%s%s)\n", libKextName,
flagCompatible ? "compatible version " : "", libVers);
}
finish:
SAFE_FREE(cSymbol);
SAFE_FREE(libVers);
return;
}
/*******************************************************************************
* usage()
*******************************************************************************/
static void usage(UsageLevel usageLevel)
{
fprintf(stderr, "usage: %s [options] kext\n", progname);
if (usageLevel == kUsageLevelBrief) {
fprintf(stderr, "\nuse %s -%s for a list of options\n",
progname, kOptNameHelp);
return;
}
fprintf(stderr, "\n");
// extra newline for spacing
fprintf(stderr, "<kext>: the kext to find libraries for\n");
fprintf(stderr, "-%s <arch>:\n", kOptNameArch);
fprintf(stderr, " resolve for architecture <arch> instead of running kernel's\n");
fprintf(stderr, "-%s: print XML fragment suitable for pasting\n", kOptNameXML);
// fake out compiler for blank line
fprintf(stderr, "\n");
fprintf(stderr, "-%s (-%c):\n",
kOptNameSystemExtensions, kOptSystemExtensions);
fprintf(stderr, " look in the system exensions folder (assumed if no other folders\n"
" specified with %s)\n",kOptNameRepository);
fprintf(stderr, "-%s <directory> (-%c):\n", kOptNameRepository, kOptRepository);
fprintf(stderr, " look in <directory> for library kexts\n");
fprintf(stderr, "%s", "\n");
fprintf(stderr, "-%s:\n", kOptNameAllSymbols);
fprintf(stderr, " list all symbols, found, not found, or found more than once\n");
fprintf(stderr, "-%s:\n", kOptNameOnedefSymbols);
fprintf(stderr, " list all symbols found with the library kext they were found in\n");
fprintf(stderr, "-%s:\n", kOptNameUndefSymbols);
fprintf(stderr, " list all symbols not found in any library\n");
fprintf(stderr, "-%s:\n", kOptNameMultdefSymbols);
fprintf(stderr, " list all symbols found more than once with their library kexts\n");
fprintf(stderr, "%s", "\n");
fprintf(stderr, "-%s (-%c):\n", kOptNameCompatible, kOptCompatible);
fprintf(stderr, " use library kext compatble versions rather than current versions\n");
fprintf(stderr, "-%s:\n", kOptNameUnsupported);
fprintf(stderr, " look in unsupported kexts for symbols\n");
return;
}