void OSRuntimeUnloadCPP(kmod_info_t *ki, void *) { if (ki && ki->address) { struct segment_command * segment; struct mach_header *header; OSSymbol::checkForPageUnload((void *) ki->address, (void *) (ki->address + ki->size)); header = (struct mach_header *)ki->address; segment = firstsegfromheader(header); for (segment = firstsegfromheader(header); segment != 0; segment = nextseg(segment)) { OSRuntimeUnloadCPPForSegment(segment); } } }
// Functions used by the extenTools/kmod library project kern_return_t OSRuntimeInitializeCPP(kmod_info_t *ki, void *) { struct mach_header *header; void *metaHandle; bool load_success; struct segment_command * segment; struct segment_command * failure_segment; if (!ki || !ki->address) return KMOD_RETURN_FAILURE; else header = (struct mach_header *) ki->address; // Tell the meta class system that we are starting the load metaHandle = OSMetaClass::preModLoad(ki->name); assert(metaHandle); if (!metaHandle) return KMOD_RETURN_FAILURE; load_success = true; failure_segment = 0; /* Scan the header for all sections named "__constructor", in any * segment, and invoke the constructors within those sections. */ for (segment = firstsegfromheader(header); segment != 0 && load_success; segment = nextseg(segment)) { struct section * section; /* Record the current segment in the event of a failure. */ failure_segment = segment; for (section = firstsect(segment); section != 0 && load_success; section = nextsect(segment, section)) { if (strcmp(section->sectname, "__constructor") == 0) { structor_t * constructors = (structor_t *)section->addr; if (constructors) { // FIXME: can we break here under the assumption that // section names are unique within a segment? int num_constructors = section->size / sizeof(structor_t); int hit_null_constructor = 0; for (int i = 0; i < num_constructors && OSMetaClass::checkModLoad(metaHandle); i++) { if (constructors[i]) { (*constructors[i])(); } else if (!hit_null_constructor) { hit_null_constructor = 1; printf("Error! Null constructor in segment %s.\n", section->segname); } } load_success = OSMetaClass::checkModLoad(metaHandle); } /* if (constructors) */ } /* if (strcmp...) */ } /* for (section...) */ } /* for (segment...) */ // We failed so call all of the destructors if (!load_success) { /* Scan the header for all sections named "__constructor", in any * segment, and invoke the constructors within those sections. */ for (segment = firstsegfromheader(header); segment != failure_segment && segment != 0; segment = nextseg(segment)) { OSRuntimeUnloadCPPForSegment(segment); } /* for (segment...) */ } return OSMetaClass::postModLoad(metaHandle); }
*********************************************************************/ void OSRuntimeUnloadCPP( kmod_info_t * kmodInfo, void * data __unused) { if (kmodInfo && kmodInfo->address) { kernel_segment_command_t * segment; kernel_mach_header_t * header; OSSymbol::checkForPageUnload((void *)kmodInfo->address, (void *)(kmodInfo->address + kmodInfo->size)); header = (kernel_mach_header_t *)kmodInfo->address; segment = firstsegfromheader(header); for (segment = firstsegfromheader(header); segment != 0; segment = nextsegfromheader(header, segment)) { OSRuntimeUnloadCPPForSegmentInKmod(segment, kmodInfo); } } return; } /********************************************************************* *********************************************************************/ kern_return_t
/* * Return the first segment_command in the header. */ kernel_segment_command_t * firstseg(void) { return firstsegfromheader(&_mh_execute_header); }