Home > Articles > Operating Systems, Server > Solaris

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

4.5 Information about Applications

4.5.1 Reporting Library Linkage (ldd)

The ldd utility reports the shared libraries that are linked into an application. This is useful for acquiring information, but it should not have any effect on performance (unless the wrong version of a library is selected somehow).

The output in Example 4.50 shows the library the application is searching for on the left, and the library that has been located on the right.

Example 4.50. Output from ldd Showing the Linking of a Particular Application

$ ldd ap27
         libm.so.1 =>     /usr/lib/libm.so.1
         libc.so.1 =>     /usr/lib/libc.so.1
         /usr/platform/SUNW,Sun-Blade-2500/lib/libc_psr.so.1

Passing the -r option to ldd will cause it to check both the objects that are linked into the application and the particular function calls that are required. It will report whether the application is missing a library, and it will report the functions that are missing.

The output shown in Example 4.51 is from the -r option passed to ldd. There are two items of interest. First, ldd reports that it is unable to locate the libsunmath library, which is Sun's library of additional mathematical functions. Under this option, ldd reports the two function calls that it is unable to locate, and these function calls correspond to square root calls for single-precision floating-point, and for long integers.

Example 4.51. The -r Option for ldd

$ ldd -r someapp
         libdl.so.1 =>    /usr/lib/libdl.so.1
         libnsl.so.1 =>   /usr/lib/libnsl.so.1
         libgen.so.1 =>   /usr/lib/libgen.so.1
         libm.so.1 =>     /usr/lib/libm.so.1
         libc.so.1 =>     /usr/lib/libc.so.1
         libsocket.so.1 =>        /usr/lib/libsocket.so.1
         libsunmath.so.1 =>       (file not found)
         libelf.so.1 =>   /usr/lib/libelf.so.1
         libmp.so.2 =>    /usr/lib/libmp.so.2
         /usr/platform/SUNW,Sun-Blade-1000/lib/libc_psr.so.1
         symbol not found: sqrtf
(someapp)
         symbol not found: sqrtl
(someapp)

The paths where libraries are located are hard-coded into the application at link time. I will cover the procedure for doing this in more detail in Section 7.2.6 of Chapter 7. At runtime, it is possible to use the LD_LIBRARY_PATH environment variable to override where the application finds libraries, or to assist the application in locating a particular library. So, for the case in Example 4.51, if the LD_LIBRARY_PATH variable were set to point to a directory containing libsunmath.so, ldd would report that the application used that version of the library. Example 4.52 shows an example of setting the LD_LIBRARY_PATH environment variable under csh. Of course, you can use the same environment variable to change where the application loads all its libraries from, so be careful when setting it and do not rely on it as the default mechanism to enable an application locating its libraries at deployment.

Example 4.52. Example of Setting the LD_LIBRARY_PATH Variable

$ setenv LD_LIBRARY_PATH /export/home/my_libraries/

The LD_LIBRARY_PATH environment variable will override the search path for both 32-bit and 64-bit applications. To explicitly set search paths for these two application types you can use the environment variables LD_LIBRARY_PATH_32 and LD_LIBRARY_PATH_64.

It is also possible to set the LD_PRELOAD environment variable to specify a library that is to be loaded before the application. This enables the use of a different library in addition to the one shipped with the application. This can be a useful way to debug the application's interactions with libraries. I will cover this in more detail in Section 7.2.10 of Chapter 7.

The -u option will request that ldd report any libraries that are linked to the application but not used. In Example 4.53, both libm (the math library) and libsocket (the sockets library) are linked into the application but not actually used.

Example 4.53. Example of ldd -u to Check for Unused Libraries

$ ldd -u ./myapp
         libdl.so.1 =>    /usr/lib/libdl.so.1
         libnsl.so.1 =>   /usr/lib/libnsl.so.1
         libm.so.1 =>     /usr/lib/libm.so.1
         libc.so.1 =>     /usr/lib/libc.so.1
         libsocket.so.1 =>        /usr/lib/libsocket.so.1
         /usr/platform/SUNW,Sun-Blade-1000/lib/libc_psr.so.1

    unused object=/usr/lib/libm.so.1
    unused object=/usr/lib/libsocket.so.1

Another useful option for ldd is the -i flag. This requests that ldd report the order in which the libraries will be initialized. The output from ldd shown in Example 4.54 indicates that libc is initialized first, and libsocket is initialized last.

Example 4.54. Example of ldd -i Output

$ ldd -i ./thisapp
         libdl.so.1 =>    /usr/lib/libdl.so.1
         libnsl.so.1 =>   /usr/lib/libnsl.so.1
         libm.so.1 =>     /usr/lib/libm.so.1
         libc.so.1 =>     /usr/lib/libc.so.1
         libsocket.so.1 =>        /usr/lib/libsocket.so.1
         libmp.so.2 =>    /usr/lib/libmp.so.2
         /usr/platform/SUNW,Sun-Blade-1000/lib/libc_psr.so.1

    init object=/usr/lib/libc.so.1
    init object=/usr/lib/libmp.so.2
    init object=/usr/lib/libnsl.so.1
    init object=/usr/lib/libsocket.so.1

4.5.2 Reporting the Type of Contents Held in a File (file)

The file tool reports on the type of a particular file. It can be useful for situations when it is necessary to check whether a particular application is a script wrapper for the actual real application, or the real application. Another way this tool can help is in determining on what type of processor a given application will run. Recall that the isalist tool from Section 4.2.5 reported the processor's architecture; the file tool will report the architecture an application requires. For a given application to run on a particular machine, the processor needs to support the application's architecture.

Example 4.55 shows file being run on an application. The binary is 32-bit and requires at least a v8plus architecture to run.

Example 4.55. Example of Running file on an Application

$ file a.out
a.out:          ELF 32-bit MSB executable SPARC32PLUS Version 1, V8+ Required, dynamically linked, not stripped

The file command is often useful when examining files or libraries to determine why linking failed with an error reporting an attempt to link 32-bit and 64-bit objects.

4.5.3 Reporting Symbols in a File (nm)

The nm tool reports the symbols defined inside a library, object file, or executable. Typically, this tool will dump out a lot of information. The useful information is usually the names of routines defined in the file, and the names of routines the file requires. If the file has been stripped (using the strip utility), no information is reported. A snippet of example output from nm is shown in Example 4.56.

Example 4.56. Short Sample of Output from nm

$ nm a.out
a.out:
[Index]   Value      Size    Type  Bind  Other Shndx   Name
[45]    |    133144|       4|OBJT |WEAK |0    |15     |environ
[60]    |    132880|       0|FUNC |GLOB |0    |UNDEF  |exit
[37]    |     67164|      40|FUNC |LOCL |0    |8      |foo
[53]    |     67204|      48|FUNC |GLOB |0    |8      |main
[42]    |    132904|       0|FUNC |GLOB |0    |UNDEF  |printf
...

The output from nm shown in Example 4.56 indicates that a.out defines a couple of routines, such as main and foo, but depends on libraries to provide the routines exit and printf.

4.5.4 Reporting Library Version Information (pvs)

It is possible to define multiple versions of a library in a single library file. This is an important mechanism to allow older applications to run with newer versions of a library. The older library API is still available, and the older applications will link to these versions. The newer API is also present, and the newer applications will link to this.

The pvs utility prints out information about the functions and versions of those functions that a library exports, or the library versions that a library or executable requires. By default, pvs will report both the definitions in the library and the requirements of the library.

Example 4.57 shows pvs reporting the versions of the libraries that the ls executable requires.

Example 4.57. Libraries Required by the ls Command

% pvs /bin/ls
         libc.so.1 (SUNW_1.19, SUNWprivate_1.1);

The -r option, for displaying only the requirements of the file, can be used to show that libc.so.1 requires libdl.so.1, as demonstrated in Example 4.58.

Example 4.58. Requirements of libc.so.1

% pvs -r /usr/lib/libc.so.1
         libdl.so.1 (SUNW_1.4, SUNWprivate_1.1);

The -d option shows the versions defined in the library. Example 4.59 shows part of the output of the versions defined in libc.so.1.

Example 4.59. Versions Defined in libc.so.1

% pvs -d /usr/lib/libc.so.1
         libc.so.1;
         SUNW_1.21.2;
         SUNW_1.21.1;
         SUNW_1.21;
         SUNW_1.20.4;
....

It is also possible to list the symbols defined in a library using the -s flag. Part of the output of this for libdl.so.1 is shown in Example 4.60.

Example 4.60. Versions of Functions Exported by libdl.so.1

$ pvs -ds /usr/lib/libdl.so.1
        libdl.so.1:
                _DYNAMIC;
                _edata;
                _etext;
                _end;
                _PROCEDURE_LINKAGE_TABLE_;
        SUNW_1.4:
                dladdr1;
        SUNW_1.3:
        SUNW_1.2:
        SUNW_1.1:
                dlmopen;
                dldump;
                dlinfo;
...

4.5.5 Examining the Disassembly of an Application, Library, or Object (dis)

The dis utility will disassemble libraries, applications, and object files. An example of this is shown in Example 4.61.

Example 4.61. Example of Using dis

$ /usr/ccs/bin/dis a.out
                  ****    DISASSEMBLER  ****


disassembly for a.out

section .text
_start()
        10694:  bc 10 20 00        clr          %fp
        10698:  e0 03 a0 40        ld           [%sp + 0x40], %l0
        1069c:  13 00 00 83        sethi        %hi(0x20c00), %o1
        106a0:  e0 22 61 8c        st           %l0, [%o1 + 0x18c]
        106a4:  a2 03 a0 44        add          %sp, 0x44, %l1
...

4.5.6 Reporting the Size of the Various Segments in an Application, Library, or Object (size)

The size utility prints the size in bytes of the various segments in an application, library, or object file. When used without parameters the command reports the size of the text (executable code), data (initialized data), and bss (uninitialized data). The -f flag reports the name of each allocatable segment together with its size in bytes. The -n flag also reports the nonloadable segments (these segments contain metadata such as debug information). An example is shown in Example 4.62.

Example 4.62. Using the size Command

% size a.out
3104 + 360 + 8 = 3472
% size -fn a.out
17(.interp) + 304(.hash) + 592(.dynsym) + 423(.dynstr) + 48(.SUNW_version) +
12(.rela.data) + 72(.rela.plt) + 1388(.text) + 16(.init) + 12(.fini) + 4(.rodata) +
4(.got) + 124(.plt) + 184(.dynamic) + 48(.data) + 8(.bss) + 1152(.symtab) +
525(.strtab) + 248(.debug_info) + 53(.debug_line) + 26(.debug_abbrev) + 650(.comment) +
184(.shstrtab) = 6094

4.5.7 Reporting Metadata Held in a File (dumpstabs, dwarfdump, elfdump, dump, and mcs)

It is possible to extract information about how an application was built using the dumpstabs utility, which is shipped with the compiler. This utility reports a lot of information, but the most useful is the command line that was passed to the compiler. Two other utilities serve a similar purpose: dwarfdump, which reports the data for applications built with the dwarf debug format, and elfdump which reports similar information for object files. All three utilities can take various flags to specify the level of detail, but by default, dumpstabs and elfdump print out all information, whereas dwarfdump does not report anything for versions earlier than Sun Studio 11 (in these cases, use the -a flag to print all the information). Applications built with the Sun Studio 10 compiler (and earlier) default to the stabs format, so dumpstabs is the appropriate command to use. In Sun Studio 11, the C compiler switched to using dwarf format. In Sun Studio 12, all the compilers default to using dwarf format.

Example 4.63 shows an example of building a file using Sun Studio 10, and then using dumpstabs and grep to extract the compile line used to build the file. In general, a lot of information is reported by dumpstabs, so passing the output through grep and searching for either the name of the file or the CMDLINE marker will reduce the output substantially.

Example 4.63. Example of Searching for the Command Line for a Compiler Using Sun Studio 10

$ cc -fast -o test test.c
$ dumpstabs test | grep test.c
 36:                   test.c  00000000  00000000  LOCAL    FILE  ABS
   0:  .stabs "test.c",N_UNDF,0x0,0x3,0xb8
   2:  .stabs "/export/home; /opt/SUNWspro/prod/bin/cc -fast -c test.c",N_CMDLINE,0x0,0x0,0x0

A similar set of actions for Sun Studio 11 and dwarfdump is shown in Example 4.64.

Example 4.64. Example of Searching for the Command Line for a Compiler Using Sun Studio 11

$ cc -fast -o test test.c
$ dwarfdump test | grep command_line
        DW_AT_SUN_command_line   /opt/SUNWspro/prod/bin/cc -fast -c  test.c
< 13>   DW_AT_SUN_command_line   DW_FORM_string

It is also possible to use the dump command with the -sv option to extract most of the information from an executable. This will dump all the sections in an executable, printing those that are text in text format and the other sections as hexadecimal. An example of the output from dump is shown in Example 4.65. The actual output from the command runs to a number of pages, and Example 4.65 shows only a small part of this output.

Example 4.65. Example of Output from dump

$ dump -sv a.out

a.out:

.interp:
        2f 75 73 72 2f 6c 69 62 2f 6c 64 2e 73 6f 2e 31 00

.hash:
        00 00 00 95 00 00 00 8e 00 00 00 00 00 00 00 00 00 00 00
....
     **** STRING TABLE INFORMATION ****
.strtab:
   <offset>     Name
   <0>
   <1>          a.out
   <7>          crti.s
   <14>         crt1.s
   <21>         __get_exit_frame_monitor_ptr
...
.stab.indexstr:
   <offset>     Name
   <115>        /tmp/;/opt/SUNWspro/prod/bin/f90 -g -qoption f90comp -
h.XAzwWCA01y4\$DCK. test.f90
...

The mcs tool, which is shipped with Solaris, manipulates the comments section in elf files. The -p option will print the comments. It is possible to delete the comments section using -d, or append more strings using -a. The comments section often holds details of the compiler version used and the header files included. An example of manipulating the comments section is shown in Example 4.66. The initial comments section shows the version information for the compiler, together with details of the header files included at compile time. Using the mcs flag -a, it is possible to append another comment to the file.

Example 4.66. Manipulating the Comments Section Using mcs

$ cc -O code.c
$ mcs -p a.out
a.out:

cg: Sun Compiler Common 11 2005/10/13
cg: Sun Compiler Common 11 2005/10/13
@(#)stdio.h     1.84    04/09/28 SMI
@(#)feature_tests.h     1.25    07/02/02 SMI
...
ld: Software Generation Utilities - Solaris Link Editors: 5.10-1.486
$ mcs -a "Hello" a.out
$ mcs -p a.out
a.out:

...
ld: Software Generation Utilities - Solaris Link Editors: 5.10-1.486
Hello
  • + Share This
  • 🔖 Save To Your Account