ld: how to link library to existing elf - Linux

This is a discussion on ld: how to link library to existing elf - Linux ; Hi. Is it possible to link library to elf binary that had been already linked? For example I have test.c #include int main(int argc, char **argv) { puts("\n[^_^]\n"); return 0; } I compile it and link it with gcc test.c ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: ld: how to link library to existing elf

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

    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

    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.

  2. Re: ld: how to link library to existing elf

    "Ni@m" writes:

    > Hi.
    > Is it possible to link library to elf binary that had been already
    > linked?
    > For example I have test.c
    >
    > #include
    >
    > 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
    >
    > 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.


  3. Re: ld: how to link library to existing elf

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

+ Reply to Thread