Namelists the nm command


Namelists & the nm command

nm displays the symbol table or namelist of an executable object file. On Solaris 1 systems, the executable object files are in assembler and link editor format. On Solaris 2 systems, objects are in executable and linking format, ELF. Some UNIX systems use the common assembler and link editor format, COFF.

The symbols in an executable object file can be one of several types. For the most part, we are interested in objects ( variables , structures, and arrays) and functions. Using nm, we can get a complete list of all of the symbols, their types, sizes, and other information about each symbol. Using nm along with the UNIX grep command, we can obtain a list of just the objects.

It is important to remember that the kernel is really one great, big program. It is compiled from thousands of individual source files. On a Solaris 1 system, when running nm against /vmunix , you can expect to see over 7000 lines of nm output! On a Solaris 2 system, when running nm on /dev/ksyms (the location of the kernel symbol table for the dynamic kernel), over 10,000 lines is not unusual! Of those, maybe only a third are objects. However, to scale things down for a moment, let's look at a tiny program and see what its symbol table looks like.

A tiny example using Solaris 2

Let's write a small program that includes a small header file. Once compiled, we can take a look at the symbol table. Once we have that, using adb, we will look at the variables we have set in the program.

First, we have our header file, tiny.h .

Example 11-1 tiny.h
 /*  tiny.h  */  #define BESTYEAR 66  struct mustang {    float ragtop;     int leather;     int candyapple; } dreamcar;  struct highway {    int speedlimit;     int smokey; };  float beach_factor = 123.5; 

Next, our little C program, tiny.c .

Example 11-2 tiny.c
 /*  tiny.c  */  #include "tiny.h"  int whistles = 10;  main ()  {    int tickets = 6;     dreamcar.ragtop = 123.5;     dreamcar.leather = 6;     dreamcar.candyapple = 10;  } 

This example, while maybe not the most exciting, demonstrates the relationship between the variables in the .c and .h files and the executable object file's symbol table.

When looking at the UNIX /usr/include files, you will see a lot of structures declared or described; however, not all of these will be immediately defined as actual variables. In tiny.h , we see the structure mustang being declared. In other words, we can see what a mustang structure looks like and what type of data it will contain. We also see that variable dreamcar is defined to be a mustang structure.

A structure type called highway is described or declared; however, it is not referenced in any definitions in either tiny.h or tiny.c . We encounter this type of situation a lot in /usr/include files. We have to dig through other . h files and, all too often, the source itself to discover which structures have been defined for use as a certain type of declared structure.

The difference between "declaring" a value and "defining" it may be a bit fuzzy for some, so let's try a silly analogy just to be on the safe side. You can "declare" to your friends what you would do if you won a million dollars. However, it is not until you actually have the money in your hand that you truly "define" how it is used. Declarations are the simply the descriptions of what might come to be someday. Definitions, in terms of programming, result in actual memory allocation.

Okay, back to the example. Note that tiny.c has a variable that is defined within the main() routine, while another is defined outside of main() . After compilation, which variables from tiny.h and tiny.c do you think will appear in the symbol table of the executable object file called tiny ? Let's take a look!

For this example, we will use nm without any options on a Solaris 2.3 system, so expect to see a lot more than a few lines of output! We will highlight the symbols you are looking for so that they're easier to spot.

Figure 11-1 Using the Solaris 2 nm program to view tiny's symbol table
 Hiya...  cc -o tiny tiny.c  Hiya...  nm tiny  Symbols from tiny:  [Index]   Value      Size    Type  Bind  Other Shndx   Name  [1]              0       0FILE LOCL 0    ABS  tiny  [2]          65748       0SECT LOCL 0    1        [3]          65768       0SECT LOCL 0    2        [4]          66076       0SECT LOCL 0    3        [5]          66780       0SECT LOCL 0    4        [6]          66984       0SECT LOCL 0    5        [7]          66996       0SECT LOCL 0    6        [8]          67032       0SECT LOCL 0    7        [9]          67220       0SECT LOCL 0    8        [10]         67232       0SECT LOCL 0    9        [11]         67244       0SECT LOCL 0    10       [12]        132784       0SECT LOCL 0    11       [13]        132788       0SECT LOCL 0    12       [14]        132924       0SECT LOCL 0    13       [15]        133012       0SECT LOCL 0    14       [16]        133020       0SECT LOCL 0    15       [17]             0       0SECT LOCL 0    16       [18]             0       0SECT LOCL 0    17       [19]             0       0SECT LOCL 0    18       [20]             0       0SECT LOCL 0    19       [21]             0       0SECT LOCL 0    20       [22]             0       0SECT LOCL 0    21       [23]             0       0SECT LOCL 0    22       [24]             0       0SECT LOCL 0    23       [25]             0       0FILE LOCL 0    ABS    crti.s  [26]             0       0FILE LOCL 0    ABS    crt1.s  [27]             0       0FILE LOCL 0    ABS    values-Xt.c  [28]             0       0FILE LOCL 0    ABS  tiny.c  [29]             0       0FILE LOCL 0    ABS    crtn.s  [30]         67032     116FUNC GLOB 0    7      _start  [31]        133016       4OBJT GLOB 0    14  whistles  [32]        133020       4OBJT GLOB 0    15     _environ  [33]        133036       0OBJT GLOB 0    ABS    _end  [34]        132784       0OBJT GLOB 0    ABS    _GLOBAL_OFFSET_TABLE_  [35]        132972       0FUNC GLOB 0    UNDEF  atexit  [36]        132984       0FUNC GLOB 0    UNDEF  exit  [37]         67220       0FUNC GLOB 0    8      _init  [38]        133024      12OBJT GLOB 0    15  dreamcar  [39]        132788       0OBJT GLOB 0    ABS    _DYNAMIC  [40]        132996       0FUNC GLOB 0    UNDEF  _exit  [41]        133020       4OBJT WEAK 0    15     environ  [42]         67148       0NOTY GLOB 0    7      __cg89_used  [43]        133020       0OBJT GLOB 0    ABS    _edata  [44]        132924       0OBJT GLOB 0    ABS    _PROCEDURE_LINKAGE_TABLE_  [45]         67248       0OBJT GLOB 0    ABS    _etext  [46]         67244       4OBJT GLOB 0    10     _lib_version  [47]         67148      72FUNC GLOB 0    7  main  [48]        133012       4OBJT GLOB 0    14  beach_factor  [49]         67232       0FUNC GLOB 0    9      _fini  Hiya... 

Does this nm output match what you expected to see?

The symbols that made it into the symbol table and are shown by the nm command are dreamcar , beach_factor , and whistles . The variable tickets did not end up in the symbol table because it was defined locally within the main() routine.

Note that BESTYEAR is not listed in the namelist because it doesn't exist after compilation. Instead, it's a definition of some constant or fixed value that is only referenced during the preprocessor phase in the compilation of tiny.c . In our example, we didn't even bother to use it.

Highway and mustang , being declarations, are also used only at compilation time. Both describe what structures of those type look like. After compilation, they are no longer needed.

Using adb , we are able to examine the symbol table objects dreamcar , beach_factor , and whistles during execution of our executable object, tiny .

A tiny example using Solaris 1

The default output of the nm command on Solaris 1 is much less informative than that from Solaris 2, though in this example it might appear to be simpler and more to the point. Once you get used to nm' s output, you will most likely choose to use it in piped commands, grep 'ing for the information you are really interested in.

Recompiling tiny.c under SunOS 4.1.3, we get the following.

Figure 11-2 Using the Solaris 1 nm program to view tiny's symbol table
 Hiya on s4-413...  cc -o tiny tiny.c  Hiya on s4-413...  nm tiny  d __DYNAMIC  000040a0 D _edata  000040b0 B _end  D _environ  000024b8 T _etext  T _main  D _beach_factor  000040a0 B _dreamcar  0000409c D _whistles  t crt0.o  T start  t tiny.o  Hiya on s4-413... 

Different flavors of UNIX may have different nm options, so always be sure to read the man page for more information and a list of nm options that you can put to good use.



PANIC. UNIX System Crash Dump Analysis Handbook
PANIC! UNIX System Crash Dump Analysis Handbook (Bk/CD-ROM)
ISBN: 0131493868
EAN: 2147483647
Year: 1994
Pages: 289
Authors: Chris Drake

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net