Gentoo Archives: gentoo-user

From: Bryan Gardiner <bog@××××××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] [OT] How to add library to dynamically linked executable?
Date: Mon, 22 Jun 2015 17:35:15
Message-Id: 20150622103450.61931ae2@mion
In Reply to: [gentoo-user] [OT] How to add library to dynamically linked executable? by Grant Edwards
1 On Mon, 22 Jun 2015 16:13:57 +0000 (UTC)
2 Grant Edwards <grant.b.edwards@×××××.com> wrote:
3
4 > Is there any way to add a library to the list of libraries required by
5 > an ELF dynamically linked executable?
6 >
7 > I have an executable (let's call it "foo") which was written and built
8 > by somebody else [I don't have sources]. It requires the librt to run
9 > on one particular platform, but librt isn't in the exeuctable's list
10 > of libraries. [I'll skip the story of how it ended up this way. It
11 > will be fixed with the next version of that application.]
12 >
13 > # foo
14 > foo: can't resolve symbol 'shm_open'
15 >
16 > If I run it like this, it's fine:
17 >
18 > # LD_PRELOAD=/lib/librt.so.0 foo
19 >
20 > Is there any way to fix the ELF executable file to add librt.so.0 to
21 > its list of libraries?
22 >
23 > I'm aware I can create a shell script that does this:
24 >
25 > #!/bin/sh
26 > export LD_PRELOAD=/lib/librt.so.0
27 > exec foo
28 >
29 > What I'm wondering about is whether there is a way to fix the ELF
30 > executable file itself so as to add librt.so.0 to its list of shared
31 > libraries. I've found chrpath(1), but it only changes the search path
32 > used to look for libraires, not the list of libraries themselves.
33 >
34
35 Perhaps elfsh[1] would do the trick. There's an example[2] that
36 demonstrates editing the dynamic section, so you might be able to add
37 an entry that way, though it's not as simple as an "addneeded"
38 command. Although it sounds[3] like elfsh might not be very stable.
39
40 Actually, scratch all that, NixOS's patchelf command[4] supports
41 editing NEEDED entries now, that'll be the easiest way.
42
43 Cheers,
44 Bryan
45
46 [1] http://www.eresi-project.org/wiki/TheELFsh
47 [2] http://www.eresi-project.org/browser/trunk/testsuite/elf/elf-tests.esh
48 [3] https://stackoverflow.com/a/2287737
49 [4] https://github.com/NixOS/patchelf

Replies

Subject Author
[gentoo-user] Re: [OT] How to add library to dynamically linked executable? Grant Edwards <grant.b.edwards@×××××.com>