Gentoo Archives: gentoo-user

From: Matti Nykyri <matti.nykyri@×××.fi>
To: "gentoo-user@l.g.o" <gentoo-user@l.g.o>
Subject: Re: [gentoo-user] [OT] How to add library to dynamically linked executable?
Date: Mon, 22 Jun 2015 17:17:04
Message-Id: C4EB8705-90F1-4C1D-B638-66F2FC80D36B@iki.fi
In Reply to: [gentoo-user] [OT] How to add library to dynamically linked executable? by Grant Edwards
1 > On Jun 22, 2015, at 19:13, Grant Edwards <grant.b.edwards@×××××.com> wrote:
2 >
3 > Is there any way to add a library to the list of libraries required by
4 > an ELF dynamically linked executable?
5 >
6 > I have an executable (let's call it "foo") which was written and built
7 > by somebody else [I don't have sources]. It requires the librt to run
8 > on one particular platform, but librt isn't in the exeuctable's list
9 > of libraries. [I'll skip the story of how it ended up this way. It
10 > will be fixed with the next version of that application.]
11 >
12 > # foo
13 > foo: can't resolve symbol 'shm_open'
14 >
15 > If I run it like this, it's fine:
16 >
17 > # LD_PRELOAD=/lib/librt.so.0 foo
18 >
19 > Is there any way to fix the ELF executable file to add librt.so.0 to
20 > its list of libraries?
21 >
22 > I'm aware I can create a shell script that does this:
23 >
24 > #!/bin/sh
25 > export LD_PRELOAD=/lib/librt.so.0
26 > exec foo
27 >
28 > What I'm wondering about is whether there is a way to fix the ELF
29 > executable file itself so as to add librt.so.0 to its list of shared
30 > libraries. I've found chrpath(1), but it only changes the search path
31 > used to look for libraires, not the list of libraries themselves.
32
33 The question you have has multiple solutions to it... Doing it as it should be done is only possible if you have all the needed object files at hand.
34
35 A ready made tool that just adds a shared library to an executable does not exist. But i believe you have a hexeditor, that is able to do it ;)
36
37 I have tryed many of these options and it is quite tricky if you don't have the ELF object-files. There are various tools for you available objcopy etc...
38
39 One option is to make a library that load two libraries e.g. librt and another library needed by the executable. Then hexedit your executable to load the new library you created and it will load the two libraries needed.
40
41 Also you can write a c program that modifies the elf executable, but i don't think you need to for this problem.
42
43 --
44 -Matti

Replies