dl_iterate_phdr
来实现的。但我想在Mac上做同样的事情。
在dyld(3)手册中记录的函数(似乎不再在线)似乎提供类似的功能。
以下是该内容:
NAME
_dyld_image_count, _dyld_get_image_header, _dyld_get_image_vmaddr_slide, _dyld_get_image_name, _dyld_register_func_for_add_image, _dyld_register_func_for_remove_image, NSVersionOfRunTimeLibrary, NSVersionOfLinkTimeLibrary _NSGetExecutablePath
SYNOPSIS
#include <mach-o/dyld.h> uint32_t _dyld_image_count(void); const struct mach_header* _dyld_get_image_header(uint32_t image_index); intptr_t _dyld_get_image_vmaddr_slide(uint32_t image_index); const char* _dyld_get_image_name(uint32_t image_index); void _dyld_register_func_for_add_image(void (*func)(const struct mach_header* mh, intptr_t vmaddr_slide)); void _dyld_register_func_for_remove_image(void (*func)(const struct mach_header* mh, intptr_t vmaddr_slide)); int32_t NSVersionOfRunTimeLibrary(const char* libraryName); int32_t NSVersionOfLinkTimeLibrary(const char* libraryName); int _NSGetExecutablePath(char* buf, uint32_t* bufsize);
DESCRIPTION
These routines provide additional introspection of dyld beyond that provided by
dlopen()
anddladdr()
_dyld_image_count()
returns the current number of images mapped in by dyld. Note that using this count to iterate all images is not thread safe, because another thread may be adding or removing images during the iteration.
_dyld_get_image_header()
returns a pointer to the mach header of the image indexed by image_index. Ifimage_index
is out of range, NULL is returned.
_dyld_get_image_vmaddr_slide()
returns the virtural memory address slide amount of the image indexed byimage_index
. Ifimage_index
is out of range zero is returned.
_dyld_get_image_name()
returns the name of the image indexed byimage_index
. The C-string continues to be owned by dyld and should not deleted. Ifimage_index
is out of range NULL is returned.
_dyld_register_func_for_add_image()
registers the specified function to be called when a new image is added (a bundle or a dynamic shared library) to the program. When this function is first registered it is called for once for each image that is currently part of the process.
_dyld_register_func_for_remove_image()
registers the specified function to be called when an image is removed (a bundle or a dynamic shared library) from the process.
NSVersionOfRunTimeLibrary()
returns the current_version number of the currently loaded dylib specifed by the libraryName. The libraryName parameter would be "bar" for /path/libbar.3.dylib and "Foo" for /path/Foo.framework/Versions/A/Foo. This function returns -1 if no such library is loaded.
NSVersionOfLinkTimeLibrary()
returns the current_version number that the main executable was linked against at build time. The libraryName parameter would be "bar" for /path/libbar.3.dylib and "Foo" for /path/Foo.framework/Versions/A/Foo. This function returns -1 if the main executable did not link against the specified library.
_NSGetExecutablePath()
copies the path of the main executable into the bufferbuf
. Thebufsize
parameter should initially be the size of the buffer. This function returns 0 if the path was successfully copied, and *bufsize
is left unchanged. It returns -1 if the buffer is not large enough, and *bufsize
is set to the size required. Note that_NSGetExecutablePath()
will return "a path" to the executable not a "real path" to the executable. That is, the path may be a symbolic link and not the real file. With deep directories the total bufsize needed could be more thanMAXPATHLEN
.
仅为完整性而言:
输入是指向某些静态内容(例如函数)的任意指针,目标是找到库及其部分。
我在这里实现了ptr_is_in_exe
函数:https://github.com/albertz/openlierox/blob/0.59/src/common/Debug_extended_backtrace.cpp
static bool
ptr_is_in_exe(const void *ptr, const struct mach_header *& header, intptr_t& offset, uintptr_t& vmaddr, std::string& image_name)
{
uint32_t i, count = _dyld_image_count();
for (i = 0; i < count; i++) {
header = _dyld_get_image_header(i);
offset = _dyld_get_image_vmaddr_slide(i);
uint32_t j = 0;
struct load_command* cmd = (struct load_command*)((char *)header + sizeof(struct mach_header));
if(header->magic == MH_MAGIC_64)
cmd = (struct load_command*)((char *)header + sizeof(struct mach_header_64));
while (j < header->ncmds) {
if (cmd->cmd == LC_SEGMENT) {
struct segment_command* seg = (struct segment_command*)cmd;
if (((intptr_t)ptr >= (seg->vmaddr + offset)) && ((intptr_t)ptr < (seg->vmaddr + offset + seg->vmsize))) {
vmaddr = seg->vmaddr;
image_name = _dyld_get_image_name(i);
return true;
}
}
if (cmd->cmd == LC_SEGMENT_64) {
struct segment_command_64* seg = (struct segment_command_64*)cmd;
if (((uintptr_t)ptr >= (seg->vmaddr + offset)) && ((uintptr_t)ptr < (seg->vmaddr + offset + seg->vmsize))) {
vmaddr = seg->vmaddr;
image_name = _dyld_get_image_name(i);
return true;
}
}
j++;
cmd = (struct load_command*)((char*)cmd + cmd->cmdsize);
}
}
return false;
}
dladdr()
要简单得多。 - Ken Thomases