2010-05-14 157 views
50

我知道有可能使用'readelf -d | grep RPATH'来检查shell中给定的二进制文件,但是可以在进程中执行此操作吗?有没有办法检查Linux上当前的rpath?

喜欢的东西(我完全由系统调用):

/* get a copy of current rpath into buffer */ 
    sys_get_current_rpath(&buffer); 

我试图诊断某些犯罪嫌疑人因此链接的问题在我们的代码库,并想检查RPATH这样如果可能的话(我'而不必产生外部脚本)。

回答

38
#include <stdio.h> 
#include <elf.h> 
#include <link.h> 

int main() 
{ 
    const ElfW(Dyn) *dyn = _DYNAMIC; 
    const ElfW(Dyn) *rpath = NULL; 
    const char *strtab = NULL; 
    for (; dyn->d_tag != DT_NULL; ++dyn) { 
    if (dyn->d_tag == DT_RPATH) { 
     rpath = dyn; 
    } else if (dyn->d_tag == DT_STRTAB) { 
     strtab = (const char *)dyn->d_un.d_val; 
    } 
    } 

    if (strtab != NULL && rpath != NULL) { 
    printf("RPATH: %s\n", strtab + rpath->d_un.d_val); 
    } 
    return 0; 
} 
+1

它很棒,但它不适用于$ ORIGIN。 $ ORIGIN不被解释,并按照函数返回。有没有办法添加$ ORIGIN的解释? – 2014-07-10 15:30:06

+1

@Jérôme如果你正在安装'/ proc'的环境中执行,那么展开'$ ORIGIN'就像readlink(“/ proc/self/exe”,...)一样简单,那么NUL终止于最后的斜线。 – 2014-07-11 16:09:51

99

为了记录,这里有几个命令将显示rpath标题。

objdump -x binary-or-library |grep RPATH 

也许一个更好的方式来做到这一点是:

readelf -d binary-or-library |head -20 

第二个命令还列出了其他库之后rpath的直接依赖。

+3

在Ubuntu 15.04上,我必须使用:objdump -x binary-or-library | grep RUNPATH – 2015-08-14 05:20:20

+0

RPATH!= RUNPATH,请参阅http://stackoverflow.com/questions/7967848/use-rpath-but-not-runpath&http ://blog.qt.io/blog/2011/10/28/rpath-and-runpath/ – 2016-10-26 01:10:11

-1

也许你可以使用github.com/NixOS/patchelf的代码,但AFAIK它不是一个图书馆的ATM。

相关问题