| View previous topic :: View next topic |
| Author |
Message |
Ni@m Guest
|
Posted: Thu Oct 23, 2008 7:01 am Post subject: ld: how to link library to existing elf |
|
|
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
|
Posted: Thu Oct 23, 2008 9:33 am Post subject: Re: ld: how to link library to existing elf |
|
|
| 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
|
Posted: Thu Oct 23, 2008 12:22 pm Post subject: Re: ld: how to link library to existing elf |
|
|
"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 |
|