4

I am using different versions of libm.a. One that I am playing with is fdlibm's libm.a (from Sun).

The problem is that I feel that my program does not call the functions in fdlibm's libm.a, but calls those of the system's glibc's libm.a.

#include "fdlibm.h"
int main(){
  double x = sin(3);
}

The program is compiled C++ programs(because it has to be linked with other c++ programs):

g++ prog.cpp libm.a

where libm.a is the fdlibm's. (From Sun, http://www.netlib.org/fdlibm/readme)

Question 1

How can I know what does sin actually invoke at run-time? I heard about various tools like objdump, gdb... Which one can be used for my case and how?

Question 2

How can I enforce fdlibm's libm.a be used?

Thanks.

Community
  • 1
  • 1
zell
  • 9,830
  • 10
  • 62
  • 115
  • You are asking what happens when you invoke undefined behavior. Redefining the standard library functions (e.g., `sin`) is undefined behavior. – David Hammen Jun 24 '15 at 13:50
  • @David Thanks for the help. What is your point btw? – zell Jun 24 '15 at 13:52
  • 3
    @David Hammen, could you provide reference proving your statement about UB? –  Jun 24 '15 at 13:53
  • if you are on Linux run `gdb -ex "b sin" -ex "r" --batch ./a.out` and add its output to your question –  Jun 24 '15 at 14:00
  • 1
    As a follow-up to the OP's question, why does the compiler not warn about multiple function definitions in this case? – Carlton Jun 24 '15 at 14:01
  • 2
    @Carlton - it is not an error. `sin` is a libc function and you can provide your own. That is why the order of libs on command line is important –  Jun 24 '15 at 14:03
  • @skwllsp Thanks so much. Would you briefly explain that magic command, maybe as an answer to the question? It may be useful for others, too – zell Jun 24 '15 at 14:09
  • I'd try one of these: http://stackoverflow.com/a/6978802/2508150 – Paolo M Jun 24 '15 at 14:12
  • Why not to inline such function as sin() ? With a lib the overhead for calling the function seems substantial (on a modern CPU) w.r.t. the time for computing sinus. – Serge Rogatch Jun 24 '15 at 14:28
  • @SergeRogatch How can we inline it without modifying the libm.a source? – zell Jun 24 '15 at 14:32

3 Answers3

6

Question 1. I heard about various tools like objdump, gdb.

As with gdb. Create file trace_sin.gdb

$ cat trace_sin.gdb
set confirm off
b sin
commands
bt
c
end
r
quit

And run your program:

$ gdb -q -x trace_sin.gdb   ./a.out Reading symbols from ./a.out...(no
debugging symbols found)...done. Breakpoint 1 at 0x400498

Breakpoint 1, 0x000000314941c760 in sin () from /lib64/libm.so.6
#0  0x000000314941c760 in sin () from /lib64/libm.so.6
#1  0x0000000000400629 in main ()

As you see in my case sin comes from libm

Question 2. How can I enforce fdlibm's libm.a be used?

Just make sure than sin from fdlibm comes before libm's sin

1

I grew tired of linking/deferred loading of the .so version of a library, and somewhere I found that you can achieve a link to a specific libary, by specifying path to the library.

Perhaps this can help with your challenge.

example - I can change this command (and link to SDL2 .so)

$(CC) $(CC_FLAGS)  $<  -o $@  -L../../bag  -lbag_i686 -lSDL2

and achive the same with

$(CC) $(CC_FLAGS)  $<  -o $@  -L../../bag  -lbag_i686 /usr/local/lib/libSDL2.so

Explicitly identifying which lib to use.


On ubuntu, I can use 'locate' to find the full path of a file. It turns out that SDL2 (.so) lands in both /usr/local/lib and /usr/lib/x86_64-linux-gnu. I suppose the x86_64 is more appropriate for my system, and it also links.

2785528
  • 5,438
  • 2
  • 18
  • 20
1

I have used the following simple technique to 'gently specify' (not explicit) a library needed for link. This technique might be appropriate for you.

I had already created several libraries which I had to use, and they were all in one specific path: "/home//cvs-tools/lib1". )

When it came time to use the 1 boost lib I needed, I simply copied the latest libboost_chrono.a into "/home//cvs-tools/lib1". No .so in the way.

And touched my make files so that when I updated boost, rather than me trying to remember all implications, I simply added to my make file the copy of chrono.a to my lib1, and my normal build then updated lib1's copy.

So, by 'gently specific', I mean that a) my make file copied the b) specific COTS library (boost) into c) my lib1 directory, and thus picked up by the same -L.

2785528
  • 5,438
  • 2
  • 18
  • 20