有没有办法在 Linux 上检查当前的 rpath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2836330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:01:30  来源:igfitidea点击:

Is there a way to inspect the current rpath on Linux?

linuxrpath

提问by Justicle

I'm aware that it is possible to use readelf -d <elf> | grep RPATHto inspect a given binary from the shell, but is it possible to do this within a process?

我知道可以使用readelf -d <elf> | grep RPATH从 shell 检查给定的二进制文件,但是是否可以在进程中执行此操作?

Something like (my completely made up system call):

类似于(我完全编造的系统调用):

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

I'm trying to diagnose some suspect SO linking issues in our codebase, and would like to inspect the RPATH this way if possible (I'd rather not have to spawn an external script).

我正在尝试在我们的代码库中诊断一些可疑的 SO 链接问题,并希望尽可能以这种方式检查 RPATH(我宁愿不必生成外部脚本)。

采纳答案by Employed Russian

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

回答by Michael Dillon

For the record, here are a couple of commands that will show the rpathheader.

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

objdump -x binary-or-library |grep RPATH

Maybe an even better way to do it is the following:

也许更好的方法如下:

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

The second command also lists the direct dependencies on other libraries followed by rpath.

第二个命令还列出了对其他库的直接依赖,后跟rpath.

回答by Oscar Andreasson

You can also use:

您还可以使用:

chrpath -l binary-or-library