Wednesday, December 11, 2013

GNU Binary Utilities


Below is list of several GNU utilities where we can use these in our day-to-day software development

ld
nm
ldd
ar
c++filt
objdump 
strip
readelf
ranlib
size  
strings
ldconfig 
catchsegv


ldd
Another very useful tool for working with shared libraries is ldd. It tells you which shared libraries an executable program uses. print shared library dependencies  Here's an example:

papaya$ ldd wibble
libstuff.so => libstuff.so (0x400af000)
libm.so.5 => /lib/libm.so.5 (0x400ba000)
libc.so.5 => /lib/libc.so.5 (0x400c3000)

The three fields in each line are the name of the library, the full path to the instance of the library that is used, and where in the virtual address space the library is mapped to.

If ldd outputs not found for a certain library, you are in trouble and won't be able to run the program in question. You will have to search for a copy of that library. Perhaps it is a library shipped with your distribution that you opted not to install, or it is already on your hard disk, but the loader (the part of the system that loads every executable program) cannot find it.
Ex :
ldd  scimAppD
ldd libSCIM_CAPD.so

nm
This command allows you to retrieve information on symbol names inside an object file or executable file. By default, the output gives you a symbol name and its virtual address. What good is that? Suppose you are compiling code and the compiler complains that you have an unresolved symbol _foo. You search all of your source code and cannot find anywhere where you use this symbol. Perhaps it got pulled in from some template or a macro buried in one of the dozens of include files that compiled along with your code.
 The command:

nm -guA *.o | grep foo

shows all the modules that refer to foo. If you want to find out what library defines foo, simply use:

nm -gA /usr/lib/* | grep foo

GNU nm lists the symbols from object files objfile.... If no object files are listed as arguments, nm assumes the file `a.out'. The command "nm" lists symbols contained in the object file or shared library.

ar
The GNU ar program creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).

C++filt
The C++ and Java languages provides function overloading, which means that you can write many functions with the same name (providing each takes parameters of different types). All C++ and Java function names are encoded into a low-level assembly label (this process is known as mangling). The c++filt (1) program does the inverse mapping: it decodes (demangles) low-level names into user-level names so that the linker can keep these overloaded functions from clashing. reverses the C++ symbol name-mangling that gcc(1) does, so if the program uses c++ then you can read the function names.

·       catchsegv - prints out the backtrace of function calls for a program receiving a SIGSEGV. - don't forget to compile with -g if you want file names and line numbers to be accessible.

objdump displays information about one or more object files. The options control what particular information to display. This information is mostly useful to programmers who are working on the compilation tools, as opposed to programmers who just want their program to compile and work.

·       GNU strip discards all symbols from object files objfile. The list of object files may include archives. At least one object file must be given.

·       readelf displays information about one or more ELF format object files. The options control what particular information to display.
·       The GNU size utility lists the section sizes--and the total size--for each of the object or archive files objfile in its argument list. By default, one line of output is generated for each object file or each module in an archive.


·       For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the options below) and are followed by an unprintable character. By default, it only prints the strings from the initialized and loaded sections of object files; for other types of files, it prints the strings from the whole file.

ldconfig
By default, the loader looks only in /lib and /usr/lib. If you have libraries in another directory, create an environment variable LD_LIBRARY_PATH and add the directories separated by colons. If you believe that everything is set up correctly, and the library in question still cannot be found, run the command ldconfig as root, which refreshes the linker system cache.

About the Author

I am currently working as Senior Developer at CISCO Systems, India.
I Pursued M.Tech degree in Computer Science from V.T.U, Bangalore and B.Tech in Computer Science from Nagarjuna University.

My Hobbies are Coding , Watching TV, Listening to music.
Reach me at naresh.master3@gmail.com

No comments:

Post a Comment