Loading and executing bundle from memory
Asked Answered
S

1

1

Loading bundle from memory is possible by NSCreateObjectFileImageFromMemory function. Does anyone have successful experience in this area? Does anyone have working sample for this function? My code is as:

text srcPath = "/Applications/TextEdit.app/Contents/MacOS/TextEdit";
data_t data;
data.loadFromFile(srcPath);
void *addr;
kern_return_t err;
NSObjectFileImage img = nil;
NSObjectFileImageReturnCode dyld_err;

err = vm_allocate(mach_task_self(), (vm_address_t *)&addr,
    data.length(), true);
if(err == 0)
{
    //err = vm_write(mach_task_self(), (vm_address_t)addr,
        //(vm_address_t)(char*)data, data.length());
    memcpy(addr, (char*)data, data.length());
    if(err == 0)
        dyld_err =
            NSCreateObjectFileImageFromMemory(addr, data.length(), &img);
        // error is NSObjectFileImageFailure
}

The img variable is null (error is NSObjectFileImageFailure). Why?

Thankyou.

Signac answered 3/8, 2012 at 12:40 Comment(0)
G
0

From the manpage, it looks like only MH_BUNDLE files can be loaded with NSCreateObjectFileImageFromMemory() and friends.

MH_BUNDLE files are explained here.

The MH_BUNDLE file type is the type typically used by code that you load at runtime (typically called bundles or plug-ins). By convention, the file name extension for this format is .bundle.

Note that that manpage is for 10.4 and there does not appear to be a newer version available.

Gilreath answered 3/8, 2012 at 12:47 Comment(2)
Thanks, I also have been created a simple bundle by XCode (has .bundle extension), but not successful.Signac
Ok, I detected problem, 1. MH_BUNDLE files can be loaded and 2. both loader and bundle should have same architecture (both 32 or 64bit).Signac

© 2022 - 2024 — McMap. All rights reserved.