title: Linux C program: How to find the library to which a function belongs
body: <p>Say at runtime, I want to find out where a function "printf" is defined. How would I do this?
  My first attempt was to print out the address of "printf" and compare it against the virtual address mapping of the process:</p>
  
  <p>my program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  void main()
  {
      printf("address of printf is 0x%X\n", printf);
      printf("pid is  %d\n", getpid());
      while (1);
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre class="lang-none prettyprint-override"><code>-bash-4.1$ ./a &amp;
  [1] 28837
  -bash-4.1$ address of printf is 0x4003F8
  pid is  28837
  </code></pre>
  
  <p>However, this says the function is defined in my own program! </p>
  
  <pre class="lang-none prettyprint-override"><code>-bash-4.1$ head /proc/28837/maps 
  00400000-00401000 r-xp 00000000 08:06 6946857                            /data2/temp/del/a      &lt;&lt;&lt;&lt;&lt;&lt;&lt; Address 0x4003F8 is in my own program?
  00600000-00601000 rw-p 00000000 08:06 6946857                            /data2/temp/del/a
  397ec00000-397ec20000 r-xp 00000000 08:11 55837039                       /lib64/ld-2.12.so
  397ee1f000-397ee20000 r--p 0001f000 08:11 55837039                       /lib64/ld-2.12.so
  397ee20000-397ee21000 rw-p 00020000 08:11 55837039                       /lib64/ld-2.12.so
  397ee21000-397ee22000 rw-p 00000000 00:00 0 
  397f000000-397f18a000 r-xp 00000000 08:11 55837204                       /lib64/libc-2.12.so
  397f18a000-397f38a000 ---p 0018a000 08:11 55837204                       /lib64/libc-2.12.so
  397f38a000-397f38e000 r--p 0018a000 08:11 55837204                       /lib64/libc-2.12.so
  397f38e000-397f38f000 rw-p 0018e000 08:11 55837204                       /lib64/libc-2.12.so
  </code></pre>
  
  <p>Shouldnt it be a call into libc? How do I find out where this "printf" or any other function came from?</p>