本文介绍了在Mac上相当于dl_iterate_phdr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历所有已加载的共享库,并获取它们的基地址以及文件名.这基本上是Linux上的 dl_iterate_phdr .

I want to iterate through all loaded shared libraries and get their base addresses as well as their file names. This is basically dl_iterate_phdr on Linux.

但是我想在Mac上也做同样的事情.

But I want to do the same for Mac.

推荐答案

dyld(3)手册页(似乎不再在线)中记录的功能似乎提供了类似的功能.

The functions documented in the dyld(3) man page (which no longer seem to be online) seem to provide the analogous functionality.

这里是内容:

 _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

简介

 #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);

说明

这些例程提供了对dyld的额外自省由 dlopen() dladdr()

_dyld_image_count()返回当前映射到的图像数量由dyld.请注意,使用此计数来迭代所有图像不是线程安全,因为其他线程可能正在添加或删除图像在迭代过程中.

_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()返回一个指向由image_index索引的图像.如果 image_index 超出范围,则为NULL返回.

_dyld_get_image_header() returns a pointer to the mach header of the image indexed by image_index. If image_index is out of range, NULL is returned.

_dyld_get_image_vmaddr_slide()返回虚拟内存地址由 image_index 索引的图像的幻灯片数量.如果 image_index 超出范围则返回零.

_dyld_get_image_vmaddr_slide() returns the virtural memory address slide amount of the image indexed by image_index. If image_index is out of range zero is returned.

_dyld_get_image_name()返回索引的图像的名称 image_index .C字符串继续由dyld拥有,应该没有删除.如果 image_index 超出范围,则返回NULL.

_dyld_get_image_name() returns the name of the image indexed by image_index. The C-string continues to be owned by dyld and should not deleted. If image_index is out of range NULL is returned.

_dyld_register_func_for_add_image()注册指定功能添加新图像(捆绑或动态共享)时调用程式库).首次注册此功能时对于当前属于该过程的每个图像,都需要调用一次.

_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()注册指定的删除图像时要调用的函数(捆绑或动态共享库).

_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()返回当前的版本号由libraryName指定的当前加载的dylib.这对于/path/libbar.3.dylib,libraryName参数将为"bar",并且/path/Foo.framework/Versions/A/Foo的"Foo".该函数返回如果未加载此类库,则为-1.

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()返回的当前版本号是主要的可执行文件在构建时就被链接了.libraryName/path/libbar.3.dylib的参数为"bar",参数的为"Foo"/path/Foo.framework/Versions/A/Foo.如果函数返回-1,则此函数返回-1.主可执行文件未链接到指定的库.

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()将主可执行文件的路径复制到缓冲区 buf . bufsize 参数最初应为缓冲区的大小.如果路径是成功复制,并且* bufsize 保持不变.如果缓冲区不够大,它将返回-1,并将* bufsize 设置为所需的大小.注意 _NSGetExecutablePath()将返回可执行文件的路径",而不是可执行文件的真实路径".也就是说,路径可能是一个象征性的链接,而不是真实文件.使用深目录时,总bufsize所需的数量可能超过 MAXPATHLEN .

_NSGetExecutablePath() copies the path of the main executable into the buffer buf. The bufsize 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 than MAXPATHLEN.

这篇关于在Mac上相当于dl_iterate_phdr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 06:09