Linking against older symbol version in a .so file

前端 未结 11 1840
长发绾君心
长发绾君心 2020-11-27 11:25

Using gcc and ld on x86_64 linux I need to link against a newer version of a library (glibc 2.14) but the executable needs to run on a system with an older version (2.5). Si

11条回答
  •  伪装坚强ぢ
    2020-11-27 11:58

    I had a similar issue. A third party library we use needs the old memcpy@GLIBC_2.2.5. My solution is an extended approach @anight posted.

    I also warp the memcpy command, but i had to use a slightly different approach, since the solution @anight posted did not work for me.

    memcpy_wrap.c:

    #include 
    #include 
    
    asm (".symver wrap_memcpy, memcpy@GLIBC_2.2.5");
    void *wrap_memcpy(void *dest, const void *src, size_t n) {
      return memcpy(dest, src, n);
    }
    

    memcpy_wrap.map:

    GLIBC_2.2.5 {
       memcpy;
    };
    

    Build the wrapper:

    gcc -c memcpy_wrap.c -o memcpy_wrap.o
    

    Now finally when linking the program add

    • -Wl,--version-script memcpy_wrap.map
    • memcpy_wrap.o

    so that you will end up with something like:

    g++  -Wl,--version-script memcpy_wrap.map  memcpy_wrap.o 
    

提交回复
热议问题