www.ShoppingPodder.com

Leading Computer Shopping,
News and information


Part of the Identityscape.com network...

getxfactor.com jmoodmusic.com smartbusinesschoices.com mintdepot.com lowfaresalways.com evangelicalview.com shoppingpodder.com soproudlywehail.com webnews.ws currenthumor.com

 

 

ld: how to link library to existing elf
   Shopping Podder - the Best of Computer Postings! Forum Index -> Computer Applications  
View previous topic :: View next topic  
Author Message
Ni@m
Guest






PostPosted: Thu Oct 23, 2008 7:01 am    Post subject: ld: how to link library to existing elf Reply with quote

Hi.
Is it possible to link library to elf binary that had been already
linked?
For example I have test.c

#include <stdio.h>

int main(int argc, char **argv)
{
puts("\n[^_^]\n");

return 0;
}

I compile it and link it with gcc test.c -lc -o test

Then I have another one, puts.c
#include <stdio.h>

int puts(const char *s)
{
fputs("\n{\n", stdout);
fputs(s, stdout);
fputs("\n}\n", stdout);
}

Which I compile as a shared lib: gcc puts.c -shared -o puts.so -fPIC

Then I want to link 'test' with puts.so to replace libc's puts. Can I
do this?

I know I can do this on linking stage of test: "gcc test.c -lputs -lc -
Wall -L/tmp" in case I put libs in correct order.
Everybody is welcome to suggest any other solutions on how to change
dynamic symbol table or symbol resolution.

Thank you in advance.
Back to top
Ni@m
Guest






PostPosted: Thu Oct 23, 2008 9:33 am    Post subject: Re: ld: how to link library to existing elf Reply with quote

Quote:
I don't know that there's a way to relink the binary file itself, but
you can use the LD_PRELOAD environment variable to override the library
function.

$ env LD_PRELOAD=./puts.so ./test

Thank you for the reply.

I was thinking about LD_PRELOAD but in this case I have to keep it ENV
all the time. I don't want to use /etc/ld.so.preload because I just
want to override some functions in some tools. Well, probably wrapper
that sets LD_PRELOAD is the best solution if there is no way to
"relink".
Back to top
Nate Eldredge
Guest






PostPosted: Thu Oct 23, 2008 12:22 pm    Post subject: Re: ld: how to link library to existing elf Reply with quote

"Ni@m" <niam.niam@gmail.com> writes:

Quote:
Hi.
Is it possible to link library to elf binary that had been already
linked?
For example I have test.c

#include <stdio.h

int main(int argc, char **argv)
{
puts("\n[^_^]\n");

return 0;
}

I compile it and link it with gcc test.c -lc -o test

Then I have another one, puts.c
#include <stdio.h

int puts(const char *s)
{
fputs("\n{\n", stdout);
fputs(s, stdout);
fputs("\n}\n", stdout);
}

Which I compile as a shared lib: gcc puts.c -shared -o puts.so -fPIC

Then I want to link 'test' with puts.so to replace libc's puts. Can I
do this?

I don't know that there's a way to relink the binary file itself, but
you can use the LD_PRELOAD environment variable to override the library
function.

$ env LD_PRELOAD=./puts.so ./test

{

[^_^]

}
$

See the man page for ld.so for more info.
Back to top
Display posts from previous:   
   Shopping Podder - the Best of Computer Postings! Forum Index -> Computer Applications  
Page 1 of 1
All times are GMT

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum