Linux Dynamic Library Concepts

I have discussed this blog at  YouTube video in detail. Please SUBSCRIBE to YouTube channel Embedkari  for further discussion on Embedded Systems.

I discussed about Linux build terminology,tool chain,static and dynamic libraries in the  last blog post.   I strongly recommend you to read earlier blogs for better understanding . If you find something missing, Please provide feedback in comment box so that I can take corrective action.

I also discussed about command nm in my blog .

When to use nm and when readelf/objdump ?

You may rarely required nm command , readelf/objdump is sufficient to fetch required information from executable. As an experiment, Try both nm and readelf in any shared library.

When to use static library ?

If a particular module is compact and is specific to application, It can be compiled as static(*.a) library.

How Dynamic Library concept work ?

There are two possibilities :

  • Dynamically Linked Library : Linux automatically loads the required library if not already loaded.  The address relocation is managed with PLT( Procedure Linkage Table) and GOT(Global Offset Table). You may use following command to display Interpreter :
    • readelf -l  program_name
  • Dynamically Loaded Modules : Application itself load a module at run time and use the same.
How to check the required shared library by any linux application ?

We can use readelf, objdump or ldd

  • readelf -al  program_name | grep NEEDED
  • ldd program_name
How Linux application find required dynamically linked shared libraries?

All library paths are cached in advance to avoid any performance issue when application binary executes. You may check this using ldconfig -p command. ldconfig can also be used to build this cache when a new library is loaded.  Library path is defined at /etc/ld.so.conf   .

One can define  environment variable LD_LIBRARY_PATH also to set the library path. You may check if this was set using env command.Linux will search path mentioned on LD_LIBRARY_PATH first before searching standard library paths. It can be used to test non-standard libraries, backward compatibility,new feature test or relocation of standard library.

Analysis with GDB

Program under discussion is as follows :

#include<stdio.h>
#include<math.h>
void main()
{
   int number;
   float val;
   printf("Please provide number :");
   scanf("%d",&number);
   val=sqrt(number);
   printf("Number=%d, Sqrt value=%f \r\n",number,val);
}
  • Verify  with
    • info sources
    • info file
  • set breakpoint at printf and sqrt functions to analyze at assembly level

Following commands may be useful  from gdb prompt:

  • ni Step one m/c instruction but if it is a function call, execute till return
  • si Step one m/c instruction exactly
  • next step program
  • break Set breakpoint at specified location
  • break *instruction address
  • info b  List breakpoints set
  • disassemble Disassemble a specified memory section
  • list List specified function
  • start Run the program until main
  • run Start program
  • info all registers List all registers and their contents
  • info file : Debugged binary and loaded sections
  • info sources: List of source files
  • continue Continue the program execution
  • help all

References :

Thanks for reading till end. I am trying to improve usability of my  site. Did you find this discussion helpful ? If so,  Please subscribe to YouTube channel Embedkari as well for additional embedded related stuff.

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.