Re: [gentoo-user] [OT] How to add library to dynamically linked executable?

2015-06-22 Thread Matti Nykyri
 On Jun 22, 2015, at 19:13, Grant Edwards grant.b.edwa...@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





[gentoo-user] [OT] How to add library to dynamically linked executable?

2015-06-22 Thread Grant Edwards
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.edwardsYow! Okay ... I'm going
  at   home to write the I HATE
  gmail.comRUBIK's CUBE HANDBOOK FOR
   DEAD CAT LOVERS ...




Re: [gentoo-user] [OT] How to add library to dynamically linked executable?

2015-06-22 Thread Bryan Gardiner
On Mon, 22 Jun 2015 16:13:57 + (UTC)
Grant Edwards grant.b.edwa...@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


pgpPTP2uvQQRv.pgp
Description: OpenPGP digital signature