Linux Dynamic Library Concepts

This article provides information about static, dynamic and shared libraries in linux. Identifying dynamic libraries required by an application. When and how these can be used.

I discussed about Linux build terminology,tool chain,static and dynamic libraries in Linux Development Process. I also discussed about command nm in  Embedded Programming Concepts-DWARF .

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

Useful GDB Commands

  • 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 :

I have discussed this article at  Linux Library working with Examples video.

 

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.

Leave a Reply

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