Software discussion

Web software | www.agner.org

 
thread Building a shared library linked against asmlib - George Kola - 2013-02-03
last replythread Building a shared library linked against asmlib - Agner - 2013-02-05
last replythread Building a shared library linked against asmlib - George Kola - 2013-02-05
last replythread Building a shared library linked against asmlib - Agner - 2013-02-06
last reply Building a shared library linked against asmlib - Charles Cummings - 2013-07-10
 
Building a shared library linked against asmlib
Author:  Date: 2013-02-03 21:41
The assembly library of optimization string subroutines is awesome and I have been using it for a few years. A friend of mine asked me to help with some string processing in Erlang -- the string processing is done in C and called from erlang. For this native implemented function (NIF), Erlang requires a shared library of those functions which is loaded at runtime.This is on X86_64. I tried to link libaelf64o.a into the shared library and got relocation error

clang -B/usr/lib/gold-ld/ c_src/strutil_nif.o -shared -L/usr/lib/erlang/lib/erl_interface-3.7.7/lib -lerl_interface -lei -o priv/strutil.so ./c_src/libtre.a ./c_src/libaelf64o.a

/usr/lib/gold-ld/ld: error: ./c_src/libaelf64o.a(strtouplow64.o): requires dynamic R_X86_64_PC32 reloc against 'InstructionSet' which may overflow at runtime; recompile with -fPIC
clang: error: linker command failed with exit code 1 (use -v to see invocation)


The most helpful link I found about the error is stackoverflow.com/questions/8703485/boost-testing-fpic-linking-error and the suggestion is to compile library with -fPIC


Is there a way to link libaelf64o.a into a shared library or something functionally equivalent -- I just want faster string processing and like the string functions in the library.


Thanks,
George

   
Building a shared library linked against asmlib
Author: Agner Date: 2013-02-05 11:00
The error message you get is actually misleading. You have one module calling a function in another module. This function call is indeed self-relative (= R_X86_64_PC32) and thus position-independent. My guess is that the linker wants the function call to go through a Procedure Linkage Table (PLT) in order to make "symbol interposition" possible. This is a mostly unnecessary requirement that makes shared objects unnecessarily slow. See www.agner.org/optimize/optimizing_cpp.pdf chapter 14.12.

I have not seen this error before. Maybe it depends on the linker version. Please try if the linker option -Bsymbolic solves the problem. If not, then you may ask in the gnu mailing list or some other forum if there is a linker switch to suppress this error message and allow the internal function call to use the faster direct call without PLT. Please reply here if you find a solution.

   
Building a shared library linked against asmlib
Author:  Date: 2013-02-05 22:02
Thanks for replying. -Bsymbolic did not solve the problem. I am using 64-bit Ubuntu 12.10. I see that you include position independent version of 32 bit linux library. Is it possible to include the position independent version of 64 bit linux library as well ?

I will post the issue to gnu mailing list and let you know if I find a solution.


Thanks,
George

   
Building a shared library linked against asmlib
Author: Agner Date: 2013-02-06 06:29
George Kola wrote:
Is it possible to include the position independent version of 64 bit linux library as well?

The 64-bit library is position independent, but it is not using a PLT. It may help if there is a way to avoid exporting the InstructionSet function in the shared object so that it doesn't require interposition.

   
Building a shared library linked against asmlib
Author:  Date: 2013-07-10 21:02
Hi -

I believe I've run into the same issue which I've attempted unsuccessfully to resolve. Any suggestions on building a 64-bit shared library would be greatly appreciated as I need it as a language extension to a commercial language (q from kx Systems) so static linkage isn't an option.

I’m able to create the file “libasm.so” (with just A_strstr) on 64-bit Red Hat Enterprise Linux Server release 6.3 without errors and it loads in a c program but I get a seg fault when A_strstr is called. I suspect my build process merely hides the issue, allowing to build and load but not actually call the function.

The seg fault happens on the jmpq instruction –

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7df05c0 in A_strstr () from libasm.so

From gdb:

0x7ffff7df05c0 <A_strstr> jmpq *0x20017a(%rip) # 0x7ffff7ff0740
0x7ffff7df05c6 <A_strstr+6> nopw %cs:0x0(%rax,%rax,1)
0x7ffff7df05d0 <strstrSSE42> movdqu (%rsi),%xmm1
0x7ffff7df05d4 <strstrSSE42+4> pcmpistrm $0xc,(%rdi),%xmm1

Build:

yasm -fELF64 -DUNIX -Worphan-labels -Werror instrset64.asm
yasm -fELF64 -DUNIX -Worphan-labels -Werror strstr64.asm
ld -Bsymbolic -Bsymbolic-functions strstr64.o instrset64.o -o mylib.o
gcc -o libasm.so -Wall -shared -fPIC mylib.o

Versions:

gcc 4.4.6 20120305 (Red Hat 4.4.6-4)
ld 2.20.51.0.2-5.34.el6 20100205
yasm 1.2.0

Thanks,

Charles