+ OSArray * infoDictArray = NULL;// do not release
+ OSObject * parsedXML = NULL;// must release
+ OSDictionary * prelinkInfoDict = NULL;// do not release
+ OSString * errorString = NULL;// must release
+ OSKext * theKernel = NULL;// must release
+ OSData * kernelcacheUUID = NULL;// do not release
+
+ kernel_segment_command_t * prelinkTextSegment = NULL;// see code
+ kernel_segment_command_t * prelinkInfoSegment = NULL;// see code
+
+ /* We make some copies of data, but if anything fails we're basically
+ * going to fail the boot, so these won't be cleaned up on error.
+ */
+ void * prelinkData = NULL;// see code
+ vm_size_t prelinkLength = 0;
+
+
+ OSDictionary * infoDict = NULL;// do not release
+
+ IORegistryEntry * registryRoot = NULL;// do not release
+ OSNumber * prelinkCountObj = NULL;// must release
+
+ u_int i = 0;
+#if NO_KEXTD
+ bool ramDiskBoot;
+ bool developerDevice;
+ bool dontLoad;
+#endif
+ OSData * kaslrOffsets = NULL;
+ unsigned long plk_segSizes[PLK_SEGMENTS];
+ vm_offset_t plk_segAddrs[PLK_SEGMENTS];
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogProgressLevel |
+ kOSKextLogDirectoryScanFlag | kOSKextLogArchiveFlag,
+ "Starting from prelinked kernel.");
+
+ prelinkTextSegment = getsegbyname(kPrelinkTextSegment);
+ if (!prelinkTextSegment) {
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogErrorLevel |
+ kOSKextLogDirectoryScanFlag | kOSKextLogArchiveFlag,
+ "Can't find prelinked kexts' text segment.");
+ goto finish;
+ }
+
+#if KASLR_KEXT_DEBUG
+ unsigned long scratchSize;
+ vm_offset_t scratchAddr;
+
+ IOLog("kaslr: prelinked kernel address info: \n");
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__TEXT", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __TEXT \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__DATA", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __DATA \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LINKEDIT", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __LINKEDIT \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__KLD", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __KLD \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_TEXT", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __PRELINK_TEXT \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+
+ scratchAddr = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_INFO", &scratchSize);
+ IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __PRELINK_INFO \n",
+ (unsigned long)scratchAddr,
+ (unsigned long)(scratchAddr + scratchSize),
+ scratchSize);
+#endif
+
+ prelinkData = (void *) prelinkTextSegment->vmaddr;
+ prelinkLength = prelinkTextSegment->vmsize;
+
+ /* build arrays of plk info for later use */
+ const char ** segNamePtr;
+
+ for (segNamePtr = &plk_segNames[0], i = 0; *segNamePtr && i < PLK_SEGMENTS; segNamePtr++, i++) {
+ plk_segSizes[i] = 0;
+ plk_segAddrs[i] = (vm_offset_t)getsegdatafromheader(&_mh_execute_header, *segNamePtr, &plk_segSizes[i]);
+ }
+
+
+ /* Unserialize the info dictionary from the prelink info section.
+ */
+ parsedXML = OSUnserializeXML((const char *)prelinkInfoSect->addr,
+ &errorString);
+ if (parsedXML) {
+ prelinkInfoDict = OSDynamicCast(OSDictionary, parsedXML);
+ }
+ if (!prelinkInfoDict) {
+ const char * errorCString = "(unknown error)";
+
+ if (errorString && errorString->getCStringNoCopy()) {
+ errorCString = errorString->getCStringNoCopy();
+ } else if (parsedXML) {
+ errorCString = "not a dictionary";
+ }
+ OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
+ "Error unserializing prelink plist: %s.", errorCString);
+ goto finish;
+ }
+
+#if NO_KEXTD
+ /* Check if we should keep developer kexts around.
+ * TODO: Check DeviceTree instead of a boot-arg <rdar://problem/10604201>
+ */
+ developerDevice = true;
+ PE_parse_boot_argn("developer", &developerDevice, sizeof(developerDevice));
+
+ ramDiskBoot = IORamDiskBSDRoot();
+#endif /* NO_KEXTD */
+
+ /* Copy in the kernelcache UUID */
+ kernelcacheUUID = OSDynamicCast(OSData,
+ prelinkInfoDict->getObject(kPrelinkInfoKCIDKey));
+ if (kernelcacheUUID) {
+ if (kernelcacheUUID->getLength() != sizeof(kernelcache_uuid)) {
+ panic("kernelcacheUUID length is %d, expected %lu", kernelcacheUUID->getLength(),
+ sizeof(kernelcache_uuid));
+ } else {
+ kernelcache_uuid_valid = TRUE;
+ memcpy((void *)&kernelcache_uuid, (const void *)kernelcacheUUID->getBytesNoCopy(), kernelcacheUUID->getLength());
+ uuid_unparse_upper(kernelcache_uuid, kernelcache_uuid_string);
+ }
+ }
+
+ infoDictArray = OSDynamicCast(OSArray,
+ prelinkInfoDict->getObject(kPrelinkInfoDictionaryKey));
+ if (!infoDictArray) {
+ OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
+ "The prelinked kernel has no kext info dictionaries");
+ goto finish;
+ }
+
+ /* kaslrOffsets are available use them to slide local relocations */
+ kaslrOffsets = OSDynamicCast(OSData,
+ prelinkInfoDict->getObject(kPrelinkLinkKASLROffsetsKey));
+
+ /* Create dictionary of excluded kexts
+ */
+#ifndef CONFIG_EMBEDDED
+ OSKext::createExcludeListFromPrelinkInfo(infoDictArray);