Ian Wienand: Exploring $ORIGIN
Anyone who works with building something with a few libraries will
quickly become familiar with rpath's; that is the ability to
store a path inside a binary for finding dependencies at runtime.
Slightly less well known is that the dynamic linker provides some
options for this path specification; one in particular is a path with
the special variable $ORIGIN. ld.so man page has
the following to say about the an $ORIGIN appearing in the
rpath:
Now, wherever you should choose to locate main, as long as
the library is in the right relative location it will be found
correctly. If you are wondering how this works, examining the
back-trace from the linker function that expands it is helpful:
Just from a first look we can divine that this has found the
RPATH header in the dynamic section and has decided to expand
the string $ORIGIN.
At this point it proceeds to do a readlink on
/proc/self/exe to get the path to the currently running
binary, effectively does a basename on it and replaces the
value of $ORIGIN. Interestingly, if this should fail, it
will fall back on the environment variable LD_ORIGIN_PATH for
unknown reasons. This might be useful if you were in a bad situation
where /proc was not mounted and you still had to run your
binary, although you could probably achieve the same thing via the use
of LD_LIBRARY_PATH just as well.
This feature should be used with some caution to avoid turning your
application in a mess that can't be packaged in any standard manner.
One common example of use is probably your java virtual machine to
find its implementation libraries.
There is a small but interesting security issue. If an attacker
can hardlink to your binary that is using $ORIGIN then any
path expansion is now relative to the attackers directory. Hence it
is possible to make the linker read in arbitrary libraries and hence
run arbitrary code. Clearly if your program is running
setuid as someone else, such as root, you've just
given up the keys to the house!
Luckily, the dynamic linker will not let you shoot yourself in the
foot like this, and prevents expansion of the $ORIGIN field
if the program is running setuid.
However, this is a good example of why you might want to keep users
home and temp directories on separate file systems away from where
binaries live.
There is also a similar variable $PLATFORM, which
describes the current running system. This is gained via the
auxiliary vector, which I have written about here and
here.
Setting LD_SHOW_AUXV=1 will allow you to examine this
value.
As a way of a small example, consider the following:ld.so understands the string $ORIGIN (or equivalently $ ORIGIN ) in an rpath specification to mean the directory containing the application executable. Thus, an application located in somedir/app could be compiled with gcc -Wl,-rpath,'$ORI GIN/../lib' so that it finds an associated shared library in somedir/lib no matter where somedir is located in the directory hierarchy.
$ cat Makefile
main: main.c lib/libfoo.so
gcc -g -Wall -o main -Wl,-rpath='$$ORIGIN/lib' -L ./lib -lfoo main.c
lib/libfoo.so: lib/foo.c
gcc -g -Wall -fPIC -shared -o lib/libfoo.so lib/foo.c
$ cat main.c
void function(void);
int main(void)
function();
return 0;
$ cat lib/foo.c
#include <stdio.h>
void function(void)
printf("called!\n");
$ make
gcc -g -Wall -fPIC -shared -o lib/libfoo.so lib/foo.c
gcc -g -Wall -o main -Wl,-rpath='$ORIGIN/lib' -L ./lib -lfoo main.c
$ ./main
called!
$ gdb ./main
(gdb) break _dl_get_origin
Function "_dl_get_origin" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (_dl_get_origin) pending.
(gdb) r
Starting program: /tmp/test-origin/main
Breakpoint 1, _dl_get_origin () at ../sysdeps/unix/sysv/linux/dl-origin.c:37
37 ../sysdeps/unix/sysv/linux/dl-origin.c: No such file or directory.
in ../sysdeps/unix/sysv/linux/dl-origin.c
(gdb) bt
#0 _dl_get_origin () at ../sysdeps/unix/sysv/linux/dl-origin.c:37
#1 0x00007ffff7de61f4 in expand_dynamic_string_token (l=0x7ffff7ffe128,
s=<value optimized out>) at dl-load.c:331
#2 0x00007ffff7de62ea in decompose_rpath (sps=0x7ffff7ffe440,
rpath=0xffffffc0 <Address 0xffffffc0 out of bounds>, l=0x0,
what=0x7ffff7df780a "RPATH") at dl-load.c:554
#3 0x00007ffff7de7051 in _dl_init_paths (llp=0x0) at dl-load.c:722
#4 0x00007ffff7de2124 in dl_main (phdr=0x7ffff7ffe6b8,
phnum=<value optimized out>, user_entry=0x7ffff7ffeb28) at rtld.c:1391
#5 0x00007ffff7df3647 in _dl_sysdep_start (
start_argptr=<value optimized out>, dl_main=0x7ffff7de14e0 <dl_main>)
at ../elf/dl-sysdep.c:243
#6 0x00007ffff7de0423 in _dl_start_final (arg=0x7fffffffe7c0) at rtld.c:338
#7 _dl_start (arg=0x7fffffffe7c0) at rtld.c:564
#8 0x00007ffff7ddfaf8 in _start () from /lib64/ld-linux-x86-64.so.2
$ readelf --dynamic ./main Dynamic section at offset 0x7d8 contains 23 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libfoo.so] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000f (RPATH) Library rpath: [$ORIGIN/lib] 0x000000000000000c (INIT) 0x4004d8 0x000000000000000d (FINI) 0x4006f8
$ readelf --dynamic /usr/lib/jvm/java-6-sun/bin/java Dynamic section at offset 0x9a28 contains 25 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0] 0x0000000000000001 (NEEDED) Shared library: [libjli.so] 0x0000000000000001 (NEEDED) Shared library: [libdl.so.2] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000e (SONAME) Library soname: [lib.so] 0x000000000000000f (RPATH) Library rpath: [$ORIGIN/../lib/amd64/jli:$ORIGIN/../jre/lib/amd64/jli] ...
$ sudo chown root ./main $ sudo chmod +s ./main $ ./main ./main: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
$ LD_SHOW_AUXV=1 ls ... AT_PLATFORM: x86_64 ...
I've not been updating this for a while.
I'm using Google+ and most of my time is spent over there.
Funnily two of my daily web comics recently featured interesting
things you could do with toilet paper:
Since there are probably going to be some questions about this in the near future:
Every once in a while I try out latest Debian/sid
I m listening to quite a few podcasts recently. Here are my current favourites:

Vim's habit of completing the full filename of the first match on :e has always
annoyed me. Rhonda pointed me to wildmode - thanks!
Fixing the Sandbrige MacBook Air display initialization
We left our hero with Debian installed on the MacBook Air, but with
the display getting scrambled as soon as the i915 driver loaded.
As was reported to Matthew, the problem is as simple as a lack of
the right mode for the eDP panel in the machine. This mode is supposed
to come from the panel EDID data, but for some reason the driver
wasn t able to query the EDID data, and so it decided to try some
random panel timings it dug out of the VBT tables, which are generally
supposed to be used by LVDS panels. Apple helpfully stuck valid data
there, but for some other panel one that is 1280x800 pixels instead
of the 1366x768 pixel panel in the MacBook Air.
I heard rumors that some machines would get a black screen when the
i915 driver loaded. I was fortunate my machine simply displayed a
1366x768 subset of the programmed 1280x800 mode. A bit of garbage on
the right side, and a few scanlines missing at the bottom. Quite
workable, especially after I ran fbset -yres 768 to keep the console
in the visible portion of the screen.
DDC failure
Looking through the kernel logs, the Intel driver tries to access the
EDID data and times out, as if DDC is just completely broken. This is
rather unexpected; the eDP spec says that the panel is required to
support DDC and provide EDID. Now, we ve seen a lot of panels which
don t quite live up to the rigorous eDP specifications, but it s a bit
surprising from Apple, who generally do VESA stuff pretty well.
We ve heard reports about panels reporting invalid EDID data, or EDID
data which didn t actually match the panel (causing us to prefer the
VBT data on LVDS machines). But I ve not heard of an eDP panel which
didn t have anything hanging off of the DDC channel.
But X works fine?
During early debugging, I happened to start X up. Much to my surprise,
X came up at the native 1366x768 mode. Digging through the kernel logs
after that, I discovered that EDID was successfully fetched from the
eDP panel while X started up.
At this point, I knew it was all downhill the EDID data was
present, it just wasn t getting picked up during the early part of the
driver initialization when the console mode is initialized.
eDP power management
The CPU is given complete control over the power management of the eDP
panel; sequencing through various power states and waiting appropriate
amounts of time when things change. Given the goal of keeping power
usage as low as possible, this makes a huge amount of sense.
The eDP spec is quite clear though, without power, the panel will not
respond to anything over the aux channel, and that includes EDID
data. The eDP panel power hardware in the Sandybridge chip has a special
mode for dealing with this requirement. If the panel is not displaying
data, you can supply power for the aux channel stuff by setting a
magic bit in the panel power registers.
When initializing the frame buffer, the kernel driver turns off the
panel completely so that it has all of the hardware in a known state
(yeah, this is not optimal, but that s another bug). When X started,
the panel was already running with the console mode.
Given the difference between these two states EDID querying with
the panel off failed, while EDID querying with the panel on worked, it
seemed pretty clear that the panel power wasn t getting managed
correctly. So, it seemed pretty clear that the magic power the panel
bit wasn t getting turned on at the right times.
Getting the power turned on.
I stuck a check inside all of the aux channel communication functions
to see where things were broken. This pointed out several places
missing the panel power calls. This wasn t quite sufficient to get
EDID data flowing. The remaining problem was that the code wasn t
waiting long enough after turning the panel power on before starting
the aux channel communication. A few msleep calls and huzzah! EDID at
boot time and the console had the right mode.
Making it faster
However, it turns out that the driver does this a lot, and the msleeps
required were fairly long the eDP panel wants a 500ms delay from
turning the panel power off before you can turn it back on.
I fix this by simply delaying the panel power off until things were
idle for a long time. Now mode setting goes zipping through, and a
few seconds later, the bit to force panel power on gets turned off.
Getting these bits for yourself
I ve pushed out the code to my (temporary) kernel repository
git://people.freedesktop.org/~keithp/linux
in the fix-edp-vdd-power branch. I d love to hear if you ve tried this
on either a MacBook Air or any other eDP machine from Ironlake onwards.

Two tiny bits:
I used to know guys who would take old computer hardware out in the desert
and shoot it up. I never went, and I've never gratuitously destroyed
a computer in 2 decades of working with the things. Until yesterday when
I picked one up by the monitor and introduced it to the floor.
My regret isn't that I violently destroyed a computer, but that it was my
Mom's computer, and I did it right in front of her, and without even a
story-worthy reason, just out of garden variety frustration. And perhaps
there's some regret that I was actually unsucessful in destroying anything
other than the monitor and some speakers. The computer was salvaged, and my
mom has a new monitor and a working system again.
Sorry Mom & Maggie. To make up for it, I'll humiliate myself with
this recording of "broke mom's computer blues", which will only be
available for a very limited time:
Our friend Susan was going to be in Chicago for a conference, and as we hadn't
seen Chicago yet, and it was on the list of cities we wanted to visit, we made
the semi-spur of the moment decision to have a three day weekend there last
weekend.
We got the Virgin America red-eye flight from SFO, which left on Friday at
around 5pm, and got into Chicago at around 11:30pm, local time. Zoe didn't do
as well as she has previously, and didn't sleep very well on the flight
over, despite us getting lucky and scoring an empty seat between us on an
otherwise fairly full flight. I think she's getting too big to comfortably
sleep in the Ergo baby carrier (or isn't keen on sleeping vertically any
more).
We stayed at the