Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

rpath

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

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).

like image 797
Justicle Avatar asked May 14 '10 17:05

Justicle


People also ask

How do I list an Rpath?

If there is a way to add the rPath tag to the library, that would be the best option. You can create a shell wrapper that would set LD_LIBRARY_PATH , call the app via exec and pass all arguments to the app itself via $@ . You can putt a wrapper in path, call it app for example and call the real app app-bin .

What is Rpath in Linux?

In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries. Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library).

What is Runpath?

Both rpath and runpath are used to specify directories to search for shared libraries(dynamic libraries). If you are not sure what shared libraries is, I have a story written on Static vs Dynamic libraries. Shared libraries are libraries which are not bundles along with the executable. They are loaded at the run time.

Can Rpath be relative?

The RPATH entries for directories contained within the build tree can be made relative to enable relocatable builds and to help achieve reproducible builds by omitting the build directory from the build environment.


2 Answers

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

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.

like image 135
Michael Dillon Avatar answered Sep 30 '22 14:09

Michael Dillon


#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; } 
like image 44
Employed Russian Avatar answered Sep 30 '22 13:09

Employed Russian