public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] [OT] How to add library to dynamically linked executable?
@ 2015-06-22 16:13 Grant Edwards
  2015-06-22 17:16 ` Matti Nykyri
  2015-06-22 17:34 ` [gentoo-user] " Bryan Gardiner
  0 siblings, 2 replies; 6+ messages in thread
From: Grant Edwards @ 2015-06-22 16:13 UTC (permalink / raw
  To: gentoo-user

Is there any way to add a library to the list of libraries required by
an ELF dynamically linked executable?

I have an executable (let's call it "foo") which was written and built
by somebody else [I don't have sources].  It requires the librt to run
on one particular platform, but librt isn't in the exeuctable's list
of libraries.  [I'll skip the story of how it ended up this way. It
will be fixed with the next version of that application.]

  # foo
  foo: can't resolve symbol 'shm_open'
  
If I run it like this, it's fine:

  # LD_PRELOAD=/lib/librt.so.0 foo

Is there any way to fix the ELF executable file to add librt.so.0 to
its list of libraries?

I'm aware I can create a shell script that does this:

#!/bin/sh
export LD_PRELOAD=/lib/librt.so.0
exec foo

What I'm wondering about is whether there is a way to fix the ELF
executable file itself so as to add librt.so.0 to its list of shared
libraries.  I've found chrpath(1), but it only changes the search path
used to look for libraires, not the list of libraries themselves.

-- 
Grant Edwards               grant.b.edwards        Yow! Okay ... I'm going
                                  at               home to write the "I HATE
                              gmail.com            RUBIK's CUBE HANDBOOK FOR
                                                   DEAD CAT LOVERS" ...



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] [OT] How to add library to dynamically linked executable?
  2015-06-22 16:13 [gentoo-user] [OT] How to add library to dynamically linked executable? Grant Edwards
@ 2015-06-22 17:16 ` Matti Nykyri
  2015-06-22 23:57   ` [gentoo-user] " walt
  2015-06-22 17:34 ` [gentoo-user] " Bryan Gardiner
  1 sibling, 1 reply; 6+ messages in thread
From: Matti Nykyri @ 2015-06-22 17:16 UTC (permalink / raw
  To: gentoo-user@lists.gentoo.org

> On Jun 22, 2015, at 19:13, Grant Edwards <grant.b.edwards@gmail.com> wrote:
> 
> Is there any way to add a library to the list of libraries required by
> an ELF dynamically linked executable?
> 
> I have an executable (let's call it "foo") which was written and built
> by somebody else [I don't have sources].  It requires the librt to run
> on one particular platform, but librt isn't in the exeuctable's list
> of libraries.  [I'll skip the story of how it ended up this way. It
> will be fixed with the next version of that application.]
> 
>  # foo
>  foo: can't resolve symbol 'shm_open'
> 
> If I run it like this, it's fine:
> 
>  # LD_PRELOAD=/lib/librt.so.0 foo
> 
> Is there any way to fix the ELF executable file to add librt.so.0 to
> its list of libraries?
> 
> I'm aware I can create a shell script that does this:
> 
> #!/bin/sh
> export LD_PRELOAD=/lib/librt.so.0
> exec foo
> 
> What I'm wondering about is whether there is a way to fix the ELF
> executable file itself so as to add librt.so.0 to its list of shared
> libraries.  I've found chrpath(1), but it only changes the search path
> used to look for libraires, not the list of libraries themselves.

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.

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

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

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.

Also you can write a c program that modifies the elf executable, but i don't think you need to for this problem.

-- 
-Matti




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [gentoo-user] [OT] How to add library to dynamically linked executable?
  2015-06-22 16:13 [gentoo-user] [OT] How to add library to dynamically linked executable? Grant Edwards
  2015-06-22 17:16 ` Matti Nykyri
@ 2015-06-22 17:34 ` Bryan Gardiner
  2015-06-22 18:59   ` [gentoo-user] " Grant Edwards
  1 sibling, 1 reply; 6+ messages in thread
From: Bryan Gardiner @ 2015-06-22 17:34 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1850 bytes --]

On Mon, 22 Jun 2015 16:13:57 +0000 (UTC)
Grant Edwards <grant.b.edwards@gmail.com> wrote:

> Is there any way to add a library to the list of libraries required by
> an ELF dynamically linked executable?
> 
> I have an executable (let's call it "foo") which was written and built
> by somebody else [I don't have sources].  It requires the librt to run
> on one particular platform, but librt isn't in the exeuctable's list
> of libraries.  [I'll skip the story of how it ended up this way. It
> will be fixed with the next version of that application.]
> 
>   # foo
>   foo: can't resolve symbol 'shm_open'
>   
> If I run it like this, it's fine:
> 
>   # LD_PRELOAD=/lib/librt.so.0 foo
> 
> Is there any way to fix the ELF executable file to add librt.so.0 to
> its list of libraries?
> 
> I'm aware I can create a shell script that does this:
> 
> #!/bin/sh
> export LD_PRELOAD=/lib/librt.so.0
> exec foo
> 
> What I'm wondering about is whether there is a way to fix the ELF
> executable file itself so as to add librt.so.0 to its list of shared
> libraries.  I've found chrpath(1), but it only changes the search path
> used to look for libraires, not the list of libraries themselves.
> 

Perhaps elfsh[1] would do the trick.  There's an example[2] that
demonstrates editing the dynamic section, so you might be able to add
an entry that way, though it's not as simple as an "addneeded"
command.  Although it sounds[3] like elfsh might not be very stable.

Actually, scratch all that, NixOS's patchelf command[4] supports
editing NEEDED entries now, that'll be the easiest way.

Cheers,
Bryan

[1] http://www.eresi-project.org/wiki/TheELFsh
[2] http://www.eresi-project.org/browser/trunk/testsuite/elf/elf-tests.esh
[3] https://stackoverflow.com/a/2287737
[4] https://github.com/NixOS/patchelf

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [gentoo-user] Re: [OT] How to add library to dynamically linked executable?
  2015-06-22 17:34 ` [gentoo-user] " Bryan Gardiner
@ 2015-06-22 18:59   ` Grant Edwards
  0 siblings, 0 replies; 6+ messages in thread
From: Grant Edwards @ 2015-06-22 18:59 UTC (permalink / raw
  To: gentoo-user

On 2015-06-22, Bryan Gardiner <bog@khumba.net> wrote:

[...]

> Actually, scratch all that, NixOS's patchelf command[4] supports
> editing NEEDED entries now, that'll be the easiest way.

[...]

> [4] https://github.com/NixOS/patchelf

Yes!  That's exactly what I was looking for, but just couldn't come up
with the Google keywords to find it.

Thanks much.

-- 
Grant Edwards               grant.b.edwards        Yow! I'm in direct contact
                                  at               with many advanced fun
                              gmail.com            CONCEPTS.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [gentoo-user] Re: [OT] How to add library to dynamically linked executable?
  2015-06-22 17:16 ` Matti Nykyri
@ 2015-06-22 23:57   ` walt
  2015-06-23  0:31     ` Grant Edwards
  0 siblings, 1 reply; 6+ messages in thread
From: walt @ 2015-06-22 23:57 UTC (permalink / raw
  To: gentoo-user

On 06/22/2015 10:16 AM, Matti Nykyri wrote:
> Also you can write a c program that modifies the elf executable, but
> i don't think you need to for this problem.

There is dev-util/patchelf, which sounds relevant, but I've never used it.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [gentoo-user] Re: [OT] How to add library to dynamically linked executable?
  2015-06-22 23:57   ` [gentoo-user] " walt
@ 2015-06-23  0:31     ` Grant Edwards
  0 siblings, 0 replies; 6+ messages in thread
From: Grant Edwards @ 2015-06-23  0:31 UTC (permalink / raw
  To: gentoo-user

On 2015-06-22, walt <w41ter@gmail.com> wrote:
> On 06/22/2015 10:16 AM, Matti Nykyri wrote:
>> Also you can write a c program that modifies the elf executable, but
>> i don't think you need to for this problem.
>
> There is dev-util/patchelf, which sounds relevant, but I've never used it.

Yep.  The 0.8 version with the ebuild in dev-util is a bit old and
doesn't support the the --add-needed option.  The git version does,
and it worked great.

-- 
Grant





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-06-23  0:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-22 16:13 [gentoo-user] [OT] How to add library to dynamically linked executable? Grant Edwards
2015-06-22 17:16 ` Matti Nykyri
2015-06-22 23:57   ` [gentoo-user] " walt
2015-06-23  0:31     ` Grant Edwards
2015-06-22 17:34 ` [gentoo-user] " Bryan Gardiner
2015-06-22 18:59   ` [gentoo-user] " Grant Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox