c++ - 以编程方式获取共享库中的函数名称

标签 c++ c linux shared-libraries dlopen

当我使用 dl_open() 时,我能否以编程方式从共享库(仅限 Linux)中获取所有函数名称的列表?

我想要这样的东西:

std::vector<std::string> list_all_functions(void *dl) { 
   //... what can I do here?
}

int main() {
    void * dl = dl_open("./mylib.so", RTLD_NOW);
    auto functions = list_all_functions(dl);
    //...
    dl_close(dl);
    return 0;
}

示例库(mylib.so)

标题(.h):

extern "C" {
    int sum (int a, int b);
}

来源(.c):

int sum (int a, int b) { return a + b; }

我知道的肮脏技巧:使用 nmobjdump 实用程序

最佳答案

更新 |长话短说:

我实际上找到了更短的方法:

    auto library = dlopen("/path/to/lib.so", RTLD_LAZY | RTLD_GLOBAL);
    const char * libname = "lib.so";
    struct link_map * map = nullptr;
    dlinfo(library, RTLD_DI_LINKMAP, &map);

    Elf64_Sym * symtab = nullptr;
    char * strtab = nullptr;
    int symentries = 0;
    for (auto section = map->l_ld; section->d_tag != DT_NULL; ++section)
    {
        if (section->d_tag == DT_SYMTAB)
        {
            symtab = (Elf64_Sym *)section->d_un.d_ptr;
        }
        if (section->d_tag == DT_STRTAB)
        {
            strtab = (char*)section->d_un.d_ptr;
        }
        if (section->d_tag == DT_SYMENT)
        {
            symentries = section->d_un.d_val;
        }
    }
    int size = strtab - (char *)symtab;
    for (int k = 0; k < size / symentries; ++k)
    {
        auto sym = &symtab[k];
        // If sym is function
        if (ELF64_ST_TYPE(symtab[k].st_info) == STT_FUNC)
        {
            //str is name of each symbol
            auto str = &strtab[sym->st_name];
            printf("%s\n", str);
        }
    }

我相信作者不再需要这个,但也许有人需要实际代码,这里是(基于之前的回答)

首先,我们需要为 dl_iterate_phdr() 回调:

static int callback(struct dl_phdr_info *info, size_t size, void *data)
{
    // data is copy of 2nd arg in dl_iterate_phdr
    // you can use it for your lib name as I did
    const char * libname = (const char *)data;

    // if current elf's name contains your lib
    if (strstr(info->dlpi_name, libname))
    {

        printf("loaded %s from: %s\n", libname, info->dlpi_name);

        for (int j = 0; j < info->dlpi_phnum; j++)
        {
            // we need to save dyanmic section since it contains symbolic table
            if (info->dlpi_phdr[j].p_type == PT_DYNAMIC)
            {
                Elf64_Sym * symtab = nullptr;
                char * strtab = nullptr;
                int symentries = 0;
                auto dyn = (Elf64_Dyn *)(info->dlpi_addr + info->dlpi_phdr[j].p_vaddr);
                for (int k = 0; k < info->dlpi_phdr[j].p_memsz / sizeof(Elf64_Dyn); ++k)
                {
                    if (dyn[k].d_tag == DT_SYMTAB)
                    {
                        symtab = (Elf64_Sym *)dyn[k].d_un.d_ptr;
                    }
                    if (dyn[k].d_tag == DT_STRTAB)
                    {
                        strtab = (char*)dyn[k].d_un.d_ptr;
                    }
                    if (dyn[k].d_tag == DT_SYMENT)
                    {
                        symentries = dyn[k].d_un.d_val;
                    }
                }
                int size = strtab - (char *)symtab;
                // for each string in table
                for (int k = 0; k < size / symentries; ++k)
                {
                    auto sym = &symtab[k];
                    auto str = &strtab[sym->st_name];
                    printf("%s\n", str);
                }
                break;
            }
        }
    }
    return 0;
}

接下来,我们调用dl_iterate_phdr():

int main()
{
    auto library = dlopen("/path/to/library.so", RTLD_LAZY | RTLD_GLOBAL);
    const char * libname = "library.so";
    dl_iterate_phdr(callback, (void*)libname);
    return 0;
}

如果您需要将这些名称存储在某个地方,您可以将一个指针传递给您的容器,用强制转换恢复它并写在那里。

对于我的示例库:

#include "simple_lib.h"

#include <cstdio>

void __attribute__ ((constructor)) initLibrary(void)
{
    printf("Library is initialized\n");
}
void __attribute__ ((destructor)) cleanUpLibrary(void)
{

    printf("Library is exited\n");
}

void make_number()
{
    printf("1\n");
}

打印这个:

Library is initialized

_ITM_deregisterTMCloneTable
puts
__gmon_start__
_ITM_registerTMCloneTable
__cxa_finalize
_Z11initLibraryv
make_number
_Z14cleanUpLibraryv
Library is exited

关于c++ - 以编程方式获取共享库中的函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54888147/

相关文章:

c - AVR 使用 struct 或 #define 定义引脚

c++ - "const& X"是什么意思,其中 X 是方法原型(prototype)中的结构?

c++ - 在 MFC/Visual C++ 中覆盖 OnCancel 按钮

c++ - std::map(和系列)查找性能问题

c - 如何找到地址空间中的漏洞?

c - 我正在尝试向每个新行添加数字

c++ - 缓存对象应该直接从文件系统读取吗?

linux - 从数据声明变量,并在一行代码中对数据运行多个命令

linux - 在不编写任何代码的情况下,是否有一个命令可以检索当前 shell 的亲和性中可用核心的数量?

java - 使用替代品在 centos 上安装 java