Example #1
0
void
OSRuntimeUnloadCPPForSegmentInKmod(
    kernel_segment_command_t * segment,
    kmod_info_t              * kmodInfo)
{

    kernel_section_t * section = NULL;  // do not free
    OSKext           * theKext = NULL;  // must release

    if (gKernelCPPInitialized && kmodInfo) {
        theKext = OSKext::lookupKextWithIdentifier(kmodInfo->name);
    }

    for (section = firstsect(segment);
            section != 0;
            section = nextsect(segment, section)) {

        if (sectionIsDestructor(section)) {
            structor_t * destructors = (structor_t *)section->addr;

            if (destructors) {
                int num_destructors = section->size / sizeof(structor_t);
                int hit_null_destructor = 0;

                for (int i = 0; i < num_destructors; i++) {
                    if (destructors[i]) {
                        (*destructors[i])();
                    } else if (!hit_null_destructor) {
                        hit_null_destructor = 1;
                        OSRuntimeLog(theKext, kOSRuntimeLogSpec,
                                     "Null destructor in kext %s segment %s!",
                                     kmodInfo ? kmodInfo->name : "(unknown)",
                                     section->segname);
                    }
                }
            } /* if (destructors) */
        } /* if (strncmp...) */
    } /* for (section...) */

    OSSafeRelease(theKext);
    return;
}
Example #2
0
void OSRuntimeUnloadCPPForSegment(struct segment_command * segment) {

    struct section * section;

    for (section = firstsect(segment);
         section != 0;
         section = nextsect(segment, section)) {

        if (strcmp(section->sectname, "__destructor") == 0) {
            structor_t * destructors = (structor_t *)section->addr;

            if (destructors) {
                int num_destructors = section->size / sizeof(structor_t);

                for (int i = 0; i < num_destructors; i++) {
                    (*destructors[i])();
                }
            } /* if (destructors) */
        } /* if (strcmp...) */
    } /* for (section...) */

    return;
}
Example #3
0
// 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);
}