Skip to content

Instantly share code, notes, and snippets.

@kbinani
Last active September 12, 2019 01:22

Revisions

  1. kbinani revised this gist May 17, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion GetModuleFileName.cpp
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,6 @@ std::string GetModuleFileName(const void *module)
    }
    if (!code_ptr) { continue; }
    const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
    if (slide == 0) { break; }
    const uintptr_t start = (const uintptr_t)code_ptr + slide;
    Dl_info info;
    if (dladdr((const void *)start, &info)) {
  2. kbinani revised this gist May 16, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GetModuleFileName.cpp
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    std::string GetModuleFileName(const void *module)
    {
    if (!module) { return std::string(); }
    if (!module) { return std::string(); }
    uint32_t count = _dyld_image_count();
    for (uint32_t i = 0; i < count; ++i) {
    const mach_header *header = _dyld_get_image_header(i);
  3. kbinani created this gist May 16, 2013.
    33 changes: 33 additions & 0 deletions GetModuleFileName.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #include <mach-o/dyld.h>
    #include <mach-o/getsect.h>
    #include <dlfcn.h>
    #include <string>

    std::string GetModuleFileName(const void *module)
    {
    if (!module) { return std::string(); }
    uint32_t count = _dyld_image_count();
    for (uint32_t i = 0; i < count; ++i) {
    const mach_header *header = _dyld_get_image_header(i);
    if (!header) { break; }
    char *code_ptr = NULL;
    if ((header->magic & MH_MAGIC_64) == MH_MAGIC_64) {
    uint64_t size;
    code_ptr = getsectdatafromheader_64((const mach_header_64 *)header, SEG_TEXT, SECT_TEXT, &size);
    } else {
    uint32_t size;
    code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
    }
    if (!code_ptr) { continue; }
    const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
    if (slide == 0) { break; }
    const uintptr_t start = (const uintptr_t)code_ptr + slide;
    Dl_info info;
    if (dladdr((const void *)start, &info)) {
    if (dlopen(info.dli_fname, RTLD_NOW) == module) {
    return std::string(info.dli_fname);
    }
    }
    }
    return std::string();
    }