rpath=$ORIGIN not having desired effect?

22

I've got a binary "CeeloPartyServer" that needs to find libFoundation.so at runtime, on a FreeBSD machine. They're both in the same directory. I compile (on another platform, using a cross compiler) CeeloPartyServer using linker flag "-rpath=$ORIGIN".

> readelf -d CeeloPartyServer |grep -i rpath
 0x0000000f (RPATH)                      Library rpath: [$ORIGIN]
> ls
CeeloPartyServer    Contents        Foundation.framework    libFoundation.so
> ./CeeloPartyServer 
/libexec/ld-elf.so.1: Shared object "libFoundation.so" not found, required by "CeeloPartyServer"

Why isn't it finding the library when I try to run it? My exact linker line is: -lm -lmysql -rpath=$ORIGIN. I am pretty sure I don't have to escape \$ or anything like that since my readelf analysis does in fact show that library rpath is set to $ORIGIN. What am I missing?

gcc
linker
freebsd
rpath
asked on Stack Overflow Jun 12, 2011 by Nektarios • edited Jul 17, 2016 by tig

3 Answers

38

I'm assuming you are using gcc and binutils.

If you do

readelf -d CeeloPartyServer | grep ORIGIN

You should get back the RPATH line you found above, but you should also see some entries about flags. The following is from a library that I built.

0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/../lib]
0x000000000000001e (FLAGS)              ORIGIN
0x000000006ffffffb (FLAGS_1)            Flags: ORIGIN

If you aren't seeing some sort of FLAGS entries, you probably haven't told the linker to mark the object as requiring origin processing. With binutils ld, you do this by passing the -z origin flag.

I'm guessing you are using gcc to drive the link though, so in that case you will need to pass flag through the compiler by adding -Wl,-z,origin to your gcc link line.

answered on Stack Overflow Jun 13, 2011 by acm • edited Jan 9, 2016 by acm
12

Depending on how many layers this flag passes through before the linker sees it, you may need to use $$ORIGIN or even \$$ORIGIN. You will know that you have it right when readelf shows an RPATH header that looks like $ORIGIN/../lib or similar. The extra $ and the backslash are just to prevent the $ from being processed by other tools in the chain.

answered on Stack Overflow Jun 15, 2011 by Michael Dillon
3

\$\ORIGIN if you are using chrpath and \$\$ORIGIN if you are providing directly in LDFLAGS

answered on Stack Overflow Jun 28, 2016 by vikram kedlaya

User contributions licensed under CC BY-SA 3.0