     START-INFO-DIR-ENTRY
     * Stabs::                       The "stabs" debugging information format.
     END-INFO-DIR-ENTRY

This document describes the stabs debugging symbol tables.

Copyright 1992, 1993 Free Software Foundation, Inc. Contributed by Cygnus Support.  Written by Julia Menapace.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy or distribute modified versions of this manual under the terms of the GPL (for which purpose this text may be regarded as a program in the language TeX).

The "stabs" representation of debugging information
***************************************************

This document describes the stabs debugging format.

Overview of stabs
*****************

Stabs refers to a format for information that describes a program to a debugger.  This format was apparently invented by the University of California at Berkeley, for the pdx Pascal debugger; the format has spread widely since then.

This document is one of the few published sources of documentation on stabs.  It is believed to be comprehensive for stabs used by C.  The lists of symbol descriptors (see Symbol descriptors) and type descriptors (see Type descriptors) are believed to be completely comprehensive.  Stabs for COBOL-specific features and for variant records (used by Pascal and Modula-2) are poorly documented here.

Other sources of information on stabs are Dbx and Dbxtool Interfaces, 2nd edition, by Sun, 1988, and AIX Version 3.2 Files Reference, Fourth Edition, September 1992, "dbx Stabstring Grammar" in the a.out section, page 2-31.  This document is believed to incorporate the information from those two sources except where it explictly directs you to them for more information.

Overview of debugging information flow
======================================

The GNU C compiler compiles C source in a .c file into assembly language in a .s file, which the assembler translates into a .o file, which the linker combines with other .o files and libraries to produce an executable file.

With the -g option, GCC puts in the .s file additional debugging information, which is slightly transformed by the assembler and linker, and carried through into the final executable.  This debugging information describes features of the source file like line numbers, the types and scopes of variables, and function names, parameters, and scopes.

For some object file formats, the debugging information is encapsulated in assembler directives known collectively as stab (symbol table) directives, which are interspersed with the generated code.  Stabs are the native format for debugging information in the a.out and XCOFF object file formats.  The GNU tools can also emit stabs in the COFF and ECOFF object file formats.

The assembler adds the information from stabs to the symbol information it places by default in the symbol table and the string table of the .o file it is building.  The linker consolidates the .o files into one executable file, with one symbol table and one string table.  Debuggers use the symbol and string tables in the executable as a source of debugging information about the program.

Overview of stab format
=======================

There are three overall formats for stab assembler directives, differentiated by the first word of the stab.  The name of the directive describes which combination of four possible data fields follows.  It is either .stabs (string), .stabn (number), or .stabd (dot).  IBM's XCOFF assembler uses .stabx (and some other directives such as .file and .bi) instead of .stabs, .stabn or .stabd.

The overall format of each class of stab is:

     .stabs "STRING",TYPE,0,DESC,VALUE
     .stabn TYPE,0,DESC,VALUE
     .stabd TYPE,0,DESC
     .stabx "STRING",VALUE,TYPE,SDB-TYPE

For .stabn and .stabd, there is no STRING (the n_strx field is zero; see Symbol tables).  For .stabd, the VALUE field is implicit and has the value of the current file location.  For .stabx, the SDB-TYPE field is unused for stabs and can always be set to zero.

The number in the TYPE field gives some basic information about which type of stab this is (or whether it is a stab, as opposed to an ordinary symbol).  Each valid type number defines a different stab type; further, the stab type defines the exact interpretation of, and possible values for, any remaining STRING, DESC, or VALUE fields present in the stab.  See Stab types, for a list in numeric order of the valid TYPE field values for stab directives.

The .stabs STRING field
=======================

For .stabs the STRING field holds the meat of the debugging information.  The generally unstructured nature of this field is what makes stabs extensible.  For some stab types the STRING field contains only a name.  For other stab types the contents can be a great deal more complex.

The overall format is of the STRING field is:

     "NAME:SYMBOL-DESCRIPTOR TYPE-INFORMATION"

NAME is the name of the symbol represented by the stab. NAME can be omitted, which means the stab represents an unnamed object.  For example, :t10=*2 defines type 10 as a pointer to type 2, but does not give the type a name.  Omitting the NAME field is supported by AIX dbx and GDB after about version 4.8, but not other debuggers.  GCC sometimes uses a single space as the name instead of omitting the name altogether; apparently that is supported by most debuggers.

The SYMBOL-DESCRIPTOR following the : is an alphabetic character that tells more specifically what kind of symbol the stab represents. If the SYMBOL-DESCRIPTOR is omitted, but type information follows, then the stab represents a local variable.  For a list of symbol descriptors, see Symbol descriptors.  The c symbol descriptor is an exception in that it is not followed by type information.  See Constants.

TYPE-INFORMATION is either a TYPE-NUMBER, or TYPE-NUMBER=.  A TYPE-NUMBER alone is a type reference, referring directly to a type that has already been defined.

The TYPE-NUMBER= form is a type definition, where the number represents a new type which is about to be defined.  The type definition may refer to other types by number, and those type numbers may be followed by = and nested definitions.

In a type definition, if the character that follows the equals sign is non-numeric then it is a TYPE-DESCRIPTOR, and tells what kind of type is about to be defined.  Any other values following the TYPE-DESCRIPTOR vary, depending on the TYPE-DESCRIPTOR. See Type descriptors, for a list of TYPE-DESCRIPTOR values.  If a number follows the = then the number is a TYPE-REFERENCE. For a full description of types, Types.

There is an AIX extension for type attributes.  Following the = are any number of type attributes.  Each one starts with @ and ends with ;.  Debuggers, including AIX's dbx and GDB 4.10, skip any type attributes they do not recognize.  GDB 4.9 and other versions of dbx may not do this.  Because of a conflict with C++ (see Cplusplus), new attributes should not be defined which begin with a digit, (, or -; GDB may be unable to distinguish those from the C++ type descriptor @.  The attributes are:

aBOUNDARY
     BOUNDARY is an integer specifying the alignment.  I assume it applies to all variables of this type.

sSIZE
     Size in bits of a variable of this type.

pINTEGER
     Pointer class (for checking).  Not sure what this means, or how INTEGER is interpreted.

P
     Indicate this is a packed type, meaning that structure fields or array elements are placed more closely in memory, to save memory at the expense of speed.

All of this can make the STRING field quite long.  All versions of GDB, and some versions of dbx, can handle arbitrarily long strings.  But many versions of dbx cretinously limit the strings to about 80 characters, so compilers which must work with such dbx's need to split the .stabs directive into several .stabs directives.  Each stab duplicates exactly all but the STRING field.  The STRING field of every stab except the last is marked as continued with a double-backslash at the end.  Removing the backslashes and concatenating the STRING fields of each stab produces the original, long string.

A simple example in C source
============================

To get the flavor of how stabs describe source information for a C program, let's look at the simple program:

     main()
     {
             printf("Hello world");
     }

When compiled with -g, the program above yields the following .s file.  Line numbers have been added to make it easier to refer to parts of the .s file in the description of the stabs that follows.

The simple example at the assembly level
========================================

This simple ``hello world'' example demonstrates several of the stab types used to describe C language source files.

     1  gcc2_compiled.:
     2  .stabs "/cygint/s1/users/jcm/play/",100,0,0,Ltext0
     3  .stabs "hello.c",100,0,0,Ltext0
     4  .text
     5  Ltext0:
     6  .stabs "int:t1=r1;-2147483648;2147483647;",128,0,0,0
     7  .stabs "char:t2=r2;0;127;",128,0,0,0
     8  .stabs "long int:t3=r1;-2147483648;2147483647;",128,0,0,0
     9  .stabs "unsigned int:t4=r1;0;-1;",128,0,0,0
     10 .stabs "long unsigned int:t5=r1;0;-1;",128,0,0,0
     11 .stabs "short int:t6=r1;-32768;32767;",128,0,0,0
     12 .stabs "long long int:t7=r1;0;-1;",128,0,0,0
     13 .stabs "short unsigned int:t8=r1;0;65535;",128,0,0,0
     14 .stabs "long long unsigned int:t9=r1;0;-1;",128,0,0,0
     15 .stabs "signed char:t10=r1;-128;127;",128,0,0,0
     16 .stabs "unsigned char:t11=r1;0;255;",128,0,0,0
     17 .stabs "float:t12=r1;4;0;",128,0,0,0
     18 .stabs "double:t13=r1;8;0;",128,0,0,0
     19 .stabs "long double:t14=r1;8;0;",128,0,0,0
     20 .stabs "void:t15=15",128,0,0,0
     21      .align 4
     22 LC0:
     23      .ascii "Hello, world!\12\0"
     24      .align 4
     25      .global _main
     26      .proc 1
     27 _main:
     28 .stabn 68,0,4,LM1
     29 LM1:
     30      !#PROLOGUE# 0
     31      save %sp,-136,%sp
     32      !#PROLOGUE# 1
     33      call ___main,0
     34      nop
     35 .stabn 68,0,5,LM2
     36 LM2:
     37 LBB2:
     38      sethi %hi(LC0),%o1
     39      or %o1,%lo(LC0),%o0
     40      call _printf,0
     41      nop
     42 .stabn 68,0,6,LM3
     43 LM3:
     44 LBE2:
     45 .stabn 68,0,6,LM4
     46 LM4:
     47 L1:
     48      ret
     49      restore
     50 .stabs "main:F1",36,0,0,_main
     51 .stabn 192,0,0,LBB2
     52 .stabn 224,0,0,LBE2

Encoding the structure of the program
*************************************

The elements of the program structure that stabs encode include the name of the main function, the names of the source and include files, the line numbers, procedure names and types, and the beginnings and ends of blocks of code.

Main program
============

 -- .stabs: N_MAIN
     Most languages allow the main program to have any name.  The N_MAIN stab type tells the debugger the name that is used in this program.  Only the STRING field is significant; it is the name of a function which is the main program.  Most C compilers do not use this stab (they expect the debugger to assume that the name is main), but some C compilers emit an N_MAIN stab for the main function.

Paths and names of the source files
===================================

 -- .stabs: N_SO
     Before any other stabs occur, there must be a stab specifying the source file.  This information is contained in a symbol of stab type N_SO; the STRING field contains the name of the file.  The VALUE of the symbol is the start address of the portion of the text section corresponding to that file.

     With the Sun Solaris2 compiler, the DESC field contains a source-language code.

     Some compilers (for example, GCC2 and SunOS4 /bin/cc) also include the directory in which the source was compiled, in a second N_SO symbol preceding the one containing the file name.  This symbol can be distinguished by the fact that it ends in a slash.  Code from the cfront C++ compiler can have additional N_SO symbols for nonexistent source files after the N_SO for the real source file; these are believed to contain no useful information.

     For example:

          .stabs "/cygint/s1/users/jcm/play/",100,0,0,Ltext0     # 100 is N_SO
          .stabs "hello.c",100,0,0,Ltext0
                  .text
          Ltext0:

Instead of N_SO symbols, XCOFF uses a .file assembler directive which assembles to a standard COFF .file symbol; explaining this in detail is outside the scope of this document.

Names of include files
======================

There are several schemes for dealing with include files: the traditional N_SOL approach, Sun's N_BINCL approach, and the XCOFF C_BINCL approach (which despite the similar name has little in common with N_BINCL).

 -- .stabs: N_SOL
     An N_SOL symbol specifies which include file subsequent symbols refer to.  The STRING field is the name of the file and the VALUE is the text address corresponding to the start of the previous include file and the start of this one.  To specify the main source file again, use an N_SOL symbol with the name of the main source file.

 -- .stabs: N_BINCL
 -- .stabs: N_EINCL
 -- N_EXCL:
     On Suns, an N_BINCL symbol specifies the start of an include file. In an object file, only the STRING is significant; the Sun linker puts data into some of the other fields.  The end of the include file is marked by an N_EINCL symbol (which has no STRING field).  In an object file, there is no significant data in the N_EINCL symbol; the Sun linker puts data into some of the fields. N_BINCL and N_EINCL can be nested.

     If the linker detects that two source files have identical stabs with an N_BINCL and N_EINCL pair (as will generally be the case for a header file), then it only puts out the stabs once.  Each additional occurance is replaced by an N_EXCL symbol.  I believe the Sun (SunOS4, not sure about Solaris) linker is the only one which supports this feature.

 -- .bi: C_BINCL
 -- .ei: C_EINCL
     For the start of an include file in XCOFF, use the .bi assembler directive, which generates a C_BINCL symbol.  A .ei directive, which generates a C_EINCL symbol, denotes the end of the include file.  Both directives are followed by the name of the source file in quotes, which becomes the STRING for the symbol. The VALUE of each symbol, produced automatically by the assembler and linker, is the offset into the executable of the beginning (inclusive, as you'd expect) or end (inclusive, as you would not expect) of the portion of the COFF line table that corresponds to this include file.  C_BINCL and C_EINCL do not nest.

Line numbers
============

 -- .stabn: N_SLINE
     An N_SLINE symbol represents the start of a source line.  The DESC field contains the line number and the VALUE field contains the code address for the start of that source line.  On most machines the address is absolute; for Sun's stabs-in-ELF, it is relative to the function in which the N_SLINE symbol occurs.

 -- .stabn: N_DSLINE
 -- .stabn: N_BSLINE
     GNU documents N_DSLINE and N_BSLINE symbols for line numbers in the data or bss segments, respectively.  They are identical to N_SLINE but are relocated differently by the linker.  They were intended to be used to describe the source location of a variable declaration, but I believe that GCC2 actually puts the line number in the DESC field of the stab for the variable itself.  GDB has been ignoring these symbols (unless they contain a STRING field) since at least GDB 3.5.

For single source lines that generate discontiguous code, such as flow of control statements, there may be more than one line number entry for the same source line.  In this case there is a line number entry at the start of each code range, each with the same line number.

XCOFF uses COFF line numbers, which are outside the scope of this document.

Procedures
==========

 -- .stabs: N_FUN
     All of the following stabs use the N_FUN symbol type.

     A function is represented by an F symbol descriptor for a global (extern) function, and f for a static (local) function.  (The next N_SLINE symbol gives the line number of the start of the function.)  The VALUE field is the address of the start of the function (absolute for a.out; relative to the start of the file for Sun's stabs-in-ELF).  The type information of the stab represents the return type of the function; thus foo:f5 means that foo is a function returning type 5.

     The type information of the stab is optionally followed by type information for each argument, with each argument preceded by ;. An argument type of 0 means that additional arguments are being passed, whose types and number may vary (... in ANSI C).  This extension is used by Sun's Solaris compiler.  GDB has tolerated it (parsed the syntax, if not necessarily used the information) since at least version 4.8; I don't know whether all versions of dbx tolerate it.  The argument types given here are not redundant with the symbols for the arguments themselves (see Parameters); they are the types of the arguments as they are passed, before any conversions might take place. For example, if a C function which is declared without a prototype takes a float argument, the value is passed as a double but then converted to a float.  Debuggers need to use the types given in the arguments when printing values, but if calling the function they need to use the types given in the symbol defining the function.

     If the return type and types of arguments of a function which is defined in another source file are specified (i.e., a function prototype in ANSI C), traditionally compilers emit no stab; the only way for the debugger to find the information is if the source file where the function is defined was also compiled with debugging symbols.  As an extension the Solaris compiler uses symbol descriptor P followed by the return type of the function, followed by the arguments, each preceded by ;, as in a stab with symbol descriptor f or F. This use of symbol descriptor P can be distinguished from its use for register parameters (see Register parameters) by the fact that it has symbol type N_FUN.

     The AIX documentation also defines symbol descriptor J as an internal function.  I assume this means a function nested within another function.  It also says symbol descriptor m is a module in Modula-2 or extended Pascal.

     Procedures (functions which do not return values) are represented as functions returning the void type in C.  I don't see why this couldn't be used for all languages (inventing a void type for this purpose if necessary), but the AIX documentation defines I, P, and Q for internal, global, and static procedures, respectively. These symbol descriptors are unusual in that they are not followed by type information.

     Here is an exploded summary (with whitespace added for clarity):

          .stabs "NAME:
                  DESC  (global proc F)
                  RETURN_TYPE_REF  (int)
                 ",N_FUN, NIL, NIL,
                 ADDRESS

     Going back to our ``hello world'' example program,

          48      ret
          49      restore

     The .stabs entry after this code fragment shows the NAME of the procedure (main); the type descriptor DESC (F, for a global procedure); a reference to the predefined type int for the return type; and the starting ADDRESS of the procedure.

          50 .stabs "main:F1",36,0,0,_main

     The stab representing a procedure is located immediately following the code of the procedure.  This stab is in turn directly followed by a group of other stabs describing elements of the procedure.  These other stabs describe the procedure's parameters, its block local variables, and its block structure.

Nested procedures
=================

For any of the N_FUN symbol descriptors, after the symbol descriptor and the type information is optionally a scope specifier. This consists of a comma, the name of the procedure, another comma, and the name of the enclosing procedure.  The first name is local to the scope specified, and seems to be redundant with the name of the symbol (before the :).  This feature is used by GCC, and presumably Pascal, Modula-2, etc., compilers, for nested functions.

If procedures are nested more than one level deep, only the immediately containing scope is specified.  For example, this code:

     int
     foo (int x)
     {
       int bar (int y)
         {
           int baz (int z)
             {
               return x + y + z;
             }
           return baz (x + 2 * y);
         }
       return x + bar (3 * x);
     }

produces the stabs:

     .stabs "baz:f1,baz,bar",36,0,0,_baz.15         # 36 is N_FUN
     .stabs "bar:f1,bar,foo",36,0,0,_bar.12
     .stabs "foo:F1",36,0,0,_foo

Block structure
===============

 -- .stabn: N_LBRAC
 -- .stabn: N_RBRAC
     The program's block structure is represented by the N_LBRAC (left brace) and the N_RBRAC (right brace) stab types.  The variables defined inside a block precede the N_LBRAC symbol for most compilers, including GCC.  Other compilers, such as the Convex, Acorn RISC machine, and Sun acc compilers, put the variables after the N_LBRAC symbol.  The VALUE fields of the N_LBRAC and N_RBRAC symbols are the start and end addresses of the code of the block, respectively.  For most machines, they are relative to the starting address of this source file.  For the Gould NP1, they are absolute.  For Sun's stabs-in-ELF, they are relative to the function in which they occur.

     The N_LBRAC and N_RBRAC stabs that describe the block scope of a procedure are located after the N_FUN stab that represents the procedure itself.

     Sun documents the DESC field of N_LBRAC and N_RBRAC symbols as containing the nesting level of the block. However, dbx seems to not care, and GCC always sets DESC to zero.

Constants
*********

The c symbol descriptor indicates that this stab represents a constant.  This symbol descriptor is an exception to the general rule that symbol descriptors are followed by type information.  Instead, it is followed by = and one of the following:

b VALUE
     Boolean constant.  VALUE is a numeric value; I assume it is 0 for false or 1 for true.

c VALUE
     Character constant.  VALUE is the numeric value of the constant.

e TYPE-INFORMATION , VALUE
     Constant whose value can be represented as integral. TYPE-INFORMATION is the type of the constant, as it would appear after a symbol descriptor (see String field).  VALUE is the numeric value of the constant.  GDB 4.9 does not actually get the right value if VALUE does not fit in a host int, but it does not do anything violent, and future debuggers could be extended to accept integers of any size (whether unsigned or not).  This constant type is usually documented as being only for enumeration constants, but GDB has never imposed that restriction; I don't know about other debuggers.

i VALUE
     Integer constant.  VALUE is the numeric value.  The type is some sort of generic integer type (for GDB, a host int); to specify the type explicitly, use e instead.

r VALUE
     Real constant.  VALUE is the real value, which can be INF (optionally preceded by a sign) for infinity, QNAN for a quiet NaN (not-a-number), or SNAN for a signalling NaN.  If it is a normal number the format is that accepted by the C library function atof.

s STRING
     String constant.  STRING is a string enclosed in either ' (in which case ' characters within the string are represented as \' or " (in which case " characters within the string are represented as \").

S TYPE-INFORMATION , ELEMENTS , BITS , PATTERN
     Set constant.  TYPE-INFORMATION is the type of the constant, as it would appear after a symbol descriptor (see String field). ELEMENTS is the number of elements in the set (does this means how many bits of PATTERN are actually used, which would be redundant with the type, or perhaps the number of bits set in PATTERN?  I don't get it), BITS is the number of bits in the constant (meaning it specifies the length of PATTERN, I think), and PATTERN is a hexadecimal representation of the set.  AIX documentation refers to a limit of 32 bytes, but I see no reason why this limit should exist.  This form could probably be used for arbitrary constants, not just sets; the only catch is that PATTERN should be understood to be target, not host, byte order and format.

The boolean, character, string, and set constants are not supported by GDB 4.9, but it ignores them.  GDB 4.8 and earlier gave an error message and refused to read symbols from the file containing the constants.

The above information is followed by ;.

Variables
*********

Different types of stabs describe the various ways that variables can be allocated: on the stack, globally, in registers, in common blocks, statically, or as arguments to a function.

Automatic variables allocated on the stack
==========================================

If a variable's scope is local to a function and its lifetime is only as long as that function executes (C calls such variables automatic), it can be allocated in a register (see Register variables) or on the stack.

 -- .stabs: N_LSYM
     Each variable allocated on the stack has a stab of type N_LSYM, with the symbol descriptor omitted.  Since type information should begin with a digit, -, or (, only those characters precluded from being used for symbol descriptors.  However, the Acorn RISC machine (ARM) is said to get this wrong: it puts out a mere type definition here, without the preceding TYPE-NUMBER=.  This is a bad idea; there is no guarantee that type descriptors are distinct from symbol descriptors.

     The VALUE of the stab is the offset of the variable within the local variables.  On most machines this is an offset from the frame pointer and is negative.  The location of the stab specifies which block it is defined in; see Block structure.

     For example, the following C code:

          int
          main ()
          {
            int x;
          }

     produces the following stabs:

          .stabs "main:F1",36,0,0,_main   # 36 is N_FUN
          .stabs "x:1",128,0,0,-12        # 128 is N_LSYM
          .stabn 192,0,0,LBB2             # 192 is N_LBRAC
          .stabn 224,0,0,LBE2             # 224 is N_RBRAC

     See Procedures for more information on the N_FUN stab, and Block structure for more information on the N_LBRAC and N_RBRAC stabs.

Global variables
================

 -- .stabs: N_GSYM
     A variable whose scope is not specific to just one source file is represented by the G symbol descriptor.  These stabs use the N_GSYM stab type.  The type information for the stab (see String field) gives the type of the variable.

     For example, the following source code:

          char g_foo = 'c';

     yields the following assembly code:

          .stabs "g_foo:G2",32,0,0,0     # 32 is N_GSYM
               .global _g_foo
               .data
          _g_foo:
               .byte 99

     The address of the variable represented by the N_GSYM is not contained in the N_GSYM stab.  The debugger gets this information from the external symbol for the global variable.  In the example above, the .global _g_foo and _g_foo: lines tell the assembler to produce an external symbol.

Register variables
==================

 -- .stabn: N_RSYM
     Register variables have their own stab type, N_RSYM, and their own symbol descriptor, r.  The stab's VALUE field contains the number of the register where the variable data will be stored.

     AIX defines a separate symbol descriptor d for floating point registers.  This seems unnecessary; why not just just give floating point registers different register numbers?  I have not verified whether the compiler actually uses d.

     If the register is explicitly allocated to a global variable, but not initialized, as in:

          register int g_bar asm ("%g5");

     then the stab may be emitted at the end of the object file, with the other bss symbols.

Common blocks
=============

A common block is a statically allocated section of memory which can be referred to by several source files.  It may contain several variables. I believe Fortran is the only language with this feature.

 -- .stabs: N_BCOMM
 -- .stabs: N_ECOMM
     A N_BCOMM stab begins a common block and an N_ECOMM stab ends it.  The only field that is significant in these two stabs is the STRING, which names a normal (non-debugging) symbol that gives the address of the common block.

 -- .stabn: N_ECOML
     Each stab between the N_BCOMM and the N_ECOMM specifies a member of that common block; its VALUE is the offset within the common block of that variable.  The N_ECOML stab type is documented for this purpose, but Sun's Fortran compiler uses N_GSYM instead.  The test case I looked at had a common block local to a function and it used the V symbol descriptor; I assume one would use S if not local to a function (that is, if a common block can be anything other than local to a function).

Static variables
================

Initialized static variables are represented by the S and V symbol descriptors.  S means file scope static, and V means procedure scope static.

 -- .stabs: N_STSYM
 -- .stabs: N_LCSYM
     In a.out files, N_STSYM means the data segment, N_FUN means the text segment, and N_LCSYM means the bss segment.

     For example, the source lines:

          static const int var_const = 5;
          static int var_init = 2;
          static int var_noinit;

     yield the following stabs:

          .stabs "var_const:S1",36,0,0,_var_const      # 36 is N_FUN
          ...
          .stabs "var_init:S1",38,0,0,_var_init        # 38 is N_STSYM
          ...
          .stabs "var_noinit:S1",40,0,0,_var_noinit    # 40 is N_LCSYM

In XCOFF files, each symbol has a section number, so the stab type need not indicate the segment.

In ECOFF files, the storage class is used to specify the section, so the stab type need not indicate the segment.

Parameters
==========

Actual parameters to a function are represented by a stab (or sometimes two; see below) for each parameter.  The stabs are in the order in which the debugger should print the parameters (i.e., the order in which the parameters are declared in the source file).  The exact form of the stab depends on how the parameter is being passed.

 -- .stabs: N_PSYM
     Parameters passed on the stack use the symbol descriptor p and the N_PSYM symbol type.  The VALUE of the symbol is an offset used to locate the parameter on the stack; its exact meaning is machine-dependent, but on most machines it is an offset from the frame pointer.

     As a simple example, the code:

          main (argc, argv)
               int argc;
               char **argv;

     produces the stabs:

          .stabs "main:F1",36,0,0,_main                 # 36 is N_FUN
          .stabs "argc:p1",160,0,0,68                   # 160 is N_PSYM
          .stabs "argv:p20=*21=*2",160,0,0,72

     The type definition of argv is interesting because it contains several type definitions.  Type 21 is pointer to type 2 (char) and argv (type 20) is pointer to type 21.

Passing parameters in registers
-------------------------------

If the parameter is passed in a register, then traditionally there are two symbols for each argument:

     .stabs "arg:p1" . . .       ; N_PSYM
     .stabs "arg:r1" . . .       ; N_RSYM

Debuggers use the second one to find the value, and the first one to know that it is an argument.

 -- .stabs: C_RPSYM
     Because that approach is kind of ugly, some compilers use symbol descriptor P or R to indicate an argument which is in a register.  Symbol type C_RPSYM is used with R and N_RSYM is used with P.  The symbol VALUE field is the register number.  P and R mean the same thing; the difference is that P is a GNU invention and R is an IBM (XCOFF) invention.  As of version 4.9, GDB should handle either one.

There is at least one case where GCC uses a p and r pair rather than P; this is where the argument is passed in the argument list and then loaded into a register.

According to the AIX documentation, symbol descriptor D is for a parameter passed in a floating point register.  This seems unnecessary---why not just use R with a register number which indicates that it's a floating point register?  I haven't verified whether the system actually does what the documentation indicates.

On the sparc and hppa, for a P symbol whose type is a structure or union, the register contains the address of the structure.  On the sparc, this is also true of a p and r pair (using Sun cc) or a p symbol.  However, if a (small) structure is really in a register, r is used.  And, to top it all off, on the hppa it might be a structure which was passed on the stack and loaded into a register and for which there is a p and r pair!  I believe that symbol descriptor i is supposed to deal with this case (it is said to mean "value parameter by reference, indirect access"; I don't know the source for this information), but I don't know details or what compilers or debuggers use it, if any (not GDB or GCC). It is not clear to me whether this case needs to be dealt with differently than parameters passed by reference (see Reference parameters).

Storing parameters as local variables
-------------------------------------

There is a case similar to an argument in a register, which is an argument that is actually stored as a local variable.  Sometimes this happens when the argument was passed in a register and then the compiler stores it as a local variable.  If possible, the compiler should claim that it's in a register, but this isn't always done.

Some compilers use the pair of symbols approach described above (ARG:p followed by ARG:); this includes GCC1 (not GCC2) on the sparc when passing a small structure and GCC2 (sometimes) when the argument type is float and it is passed as a double and converted to float by the prologue (in the latter case the type of the ARG:p symbol is double and the type of the ARG: symbol is float).  GCC, at least on the 960, uses a single p symbol descriptor for an argument which is stored as a local variable but uses N_LSYM instead of N_PSYM.  In this case, the VALUE of the symbol is an offset relative to the local variables for that function, not relative to the arguments; on some machines those are the same thing, but not on all.

Passing parameters by reference
-------------------------------

If the parameter is passed by reference (e.g., Pascal VAR parameters), then the symbol descriptor is v if it is in the argument list, or a if it in a register.  Other than the fact that these contain the address of the parameter rather than the parameter itself, they are identical to p and R, respectively.  I believe a is an AIX invention; v is supported by all stabs-using systems as far as I know.

Passing conformant array parameters
-----------------------------------

Conformant arrays are a feature of Modula-2, and perhaps other languages, in which the size of an array parameter is not known to the called function until run-time.  Such parameters have two stabs: a x for the array itself, and a C, which represents the size of the array.  The VALUE of the x stab is the offset in the argument list where the address of the array is stored (it this right? it is a guess); the VALUE of the C stab is the offset in the argument list where the size of the array (in elements? in bytes?) is stored.

Defining types
**************

The examples so far have described types as references to previously defined types, or defined in terms of subranges of or pointers to previously defined types.  This chapter describes the other type descriptors that may follow the = in a type definition.

Builtin types
=============

Certain types are built in (int, short, void, float, etc.); the debugger recognizes these types and knows how to handle them.  Thus, don't be surprised if some of the following ways of specifying builtin types do not specify everything that a debugger would need to know about the type---in some cases they merely specify enough information to distinguish the type from other types.

The traditional way to define builtin types is convolunted, so new ways have been invented to describe them.  Sun's acc uses special builtin type descriptors (b and R), and IBM uses negative type numbers.  GDB accepts all three ways, as of version 4.8; dbx just accepts the traditional builtin types and perhaps one of the other two formats.  The following sections describe each of these formats.

Traditional builtin types
-------------------------

This is the traditional, convoluted method for defining builtin types. There are several classes of such type definitions: integer, floating point, and void.

Traditional integer types
.........................

Often types are defined as subranges of themselves.  If the bounding values fit within an int, then they are given normally.  For example:

     .stabs "int:t1=r1;-2147483648;2147483647;",128,0,0,0    # 128 is N_LSYM
     .stabs "char:t2=r2;0;127;",128,0,0,0

Builtin types can also be described as subranges of int:

     .stabs "unsigned short:t6=r1;0;65535;",128,0,0,0

If the lower bound of a subrange is 0 and the upper bound is -1, the type is an unsigned integral type whose bounds are too big to describe in an int.  Traditionally this is only used for unsigned int and unsigned long:

     .stabs "unsigned int:t4=r1;0;-1;",128,0,0,0
     .stabs "long long int:t7=r1;0;-1;",128,0,0,0

For larger types, GCC 2.4.5 puts out bounds in octal, with a leading 0.  In this case a negative bound consists of a number which is a 1 bit followed by a bunch of 0 bits, and a positive bound is one in which a bunch of bits are 1. All known versions of dbx and GDB version 4 accept this, but GDB 3.5 refuses to read the whole file containing such symbols.  So GCC 2.3.3 did not output the proper size for these types.

If the lower bound of a subrange is 0 and the upper bound is negative, the type is an unsigned integral type whose size in bytes is the absolute value of the upper bound.  I believe this is a Convex convention for unsigned long long.

If the lower bound of a subrange is negative and the upper bound is 0, the type is a signed integral type whose size in bytes is the absolute value of the lower bound.  I believe this is a Convex convention for long long.  To distinguish this from a legitimate subrange, the type should be a subrange of itself.  I'm not sure whether this is the case for Convex.

Traditional other types
.......................

If the upper bound of a subrange is 0 and the lower bound is positive, the type is a floating point type, and the lower bound of the subrange indicates the number of bytes in the type:

     .stabs "float:t12=r1;4;0;",128,0,0,0
     .stabs "double:t13=r1;8;0;",128,0,0,0

However, GCC writes long double the same way it writes double, so there is no way to distinguish.

     .stabs "long double:t14=r1;8;0;",128,0,0,0

Complex types are defined the same way as floating-point types; there is no way to distinguish a single-precision complex from a double-precision floating-point type.

The C void type is defined as itself:

     .stabs "void:t15=15",128,0,0,0

I'm not sure how a boolean type is represented.

Defining builtin types using builtin type descriptors
-----------------------------------------------------

This is the method used by Sun's acc for defining builtin types. These are the type descriptors to define builtin types:

b SIGNED CHAR-FLAG WIDTH ; OFFSET ; NBITS ;
     Define an integral type.  SIGNED is u for unsigned or s for signed.  CHAR-FLAG is c which indicates this is a character type, or is omitted.  I assume this is to distinguish an integral type from a character type of the same size, for example it might make sense to set it for the C type wchar_t so the debugger can print such variables differently (Solaris does not do this).  Sun sets it on the C types signed char and unsigned char which arguably is wrong.  WIDTH and OFFSET appear to be for small objects stored in larger ones, for example a short in an int register.  WIDTH is normally the number of bytes in the type.  OFFSET seems to always be zero.  NBITS is the number of bits in the type.

     Note that type descriptor b used for builtin types conflicts with its use for Pascal space types (see Miscellaneous types); they can be distinguished because the character following the type descriptor will be a digit, (, or - for a Pascal space type, or u or s for a builtin type.

w
     Documented by AIX to define a wide character type, but their compiler actually uses negative type numbers (see Negative type numbers).

R FP-TYPE ; BYTES ;
     Define a floating point type.  FP-TYPE has one of the following values:

     1 (NF_SINGLE)
          IEEE 32-bit (single precision) floating point format.

     2 (NF_DOUBLE)
          IEEE 64-bit (double precision) floating point format.

     3 (NF_COMPLEX)

     4 (NF_COMPLEX16)

     5 (NF_COMPLEX32)
          These are for complex numbers.  A comment in the GDB source describes them as Fortran complex, double complex, and complex*16, respectively, but what does that mean?  (i.e., Single precision?  Double precison?).

     6 (NF_LDOUBLE)
          long double.  This should probably only be used for Sun format long double, and new codes should be used for other floating point formats (NF_DOUBLE can be used if a long double is really just an IEEE double, of course).

     BYTES is the number of bytes occupied by the type.  This allows a debugger to perform some operations with the type even if it doesn't understand FP-TYPE.

g TYPE-INFORMATION ; NBITS
     Documented by AIX to define a floating type, but their compiler actually uses negative type numbers (see Negative type numbers).

c TYPE-INFORMATION ; NBITS
     Documented by AIX to define a complex type, but their compiler actually uses negative type numbers (see Negative type numbers).

The C void type is defined as a signed integral type 0 bits long:

     .stabs "void:t19=bs0;0;0",128,0,0,0

The Solaris compiler seems to omit the trailing semicolon in this case. Getting sloppy in this way is not a swift move because if a type is embedded in a more complex expression it is necessary to be able to tell where it ends.

I'm not sure how a boolean type is represented.

Negative type numbers
---------------------

This is the method used in XCOFF for defining builtin types. Since the debugger knows about the builtin types anyway, the idea of negative type numbers is simply to give a special type number which indicates the builtin type.  There is no stab defining these types.

I'm not sure whether anyone has tried to define what this means if int can be other than 32 bits (or if other types can be other than their customary size).  If int has exactly one size for each architecture, then it can be handled easily enough, but if the size of int can vary according the compiler options, then it gets hairy. The best way to do this would be to define separate negative type numbers for 16-bit int and 32-bit int; therefore I have indicated below the customary size (and other format information) for each type.  The information below is currently correct because AIX on the RS6000 is the only system which uses these type numbers.  If these type numbers start to get used on other systems, I suspect the correct thing to do is to define a new number in cases where a type does not have the size and format indicated below (or avoid negative type numbers in these cases).

Part of the definition of the negative type number is the name of the type.  Types with identical size and format but different names have different negative type numbers.

-1
     int, 32 bit signed integral type.

-2
     char, 8 bit type holding a character.   Both GDB and dbx on AIX treat this as signed.  GCC uses this type whether char is signed or not, which seems like a bad idea.  The AIX compiler (xlc) seems to avoid this type; it uses -5 instead for char.

-3
     short, 16 bit signed integral type.

-4
     long, 32 bit signed integral type.

-5
     unsigned char, 8 bit unsigned integral type.

-6
     signed char, 8 bit signed integral type.

-7
     unsigned short, 16 bit unsigned integral type.

-8
     unsigned int, 32 bit unsigned integral type.

-9
     unsigned, 32 bit unsigned integral type.

-10
     unsigned long, 32 bit unsigned integral type.

-11
     void, type indicating the lack of a value.

-12
     float, IEEE single precision.

-13
     double, IEEE double precision.

-14
     long double, IEEE double precision.  The compiler claims the size will increase in a future release, and for binary compatibility you have to avoid using long double.  I hope when they increase it they use a new negative type number.

-15
     integer.  32 bit signed integral type.

-16
     boolean.  32 bit type.  How is the truth value encoded?  Is it the least significant bit or is it a question of whether the whole value is zero or non-zero?

-17
     short real.  IEEE single precision.

-18
     real.  IEEE double precision.

-19
     stringptr.  See Strings.

-20
     character, 8 bit unsigned character type.

-21
     logical*1, 8 bit type.  This Fortran type has a split personality in that it is used for boolean variables, but can also be used for unsigned integers.  0 is false, 1 is true, and other values are non-boolean.

-22
     logical*2, 16 bit type.  This Fortran type has a split personality in that it is used for boolean variables, but can also be used for unsigned integers.  0 is false, 1 is true, and other values are non-boolean.

-23
     logical*4, 32 bit type.  This Fortran type has a split personality in that it is used for boolean variables, but can also be used for unsigned integers.  0 is false, 1 is true, and other values are non-boolean.

-24
     logical, 32 bit type.  This Fortran type has a split personality in that it is used for boolean variables, but can also be used for unsigned integers.  0 is false, 1 is true, and other values are non-boolean.

-25
     complex.  A complex type consisting of two IEEE single-precision floating point values.

-26
     complex.  A complex type consisting of two IEEE double-precision floating point values.

-27
     integer*1, 8 bit signed integral type.

-28
     integer*2, 16 bit signed integral type.

-29
     integer*4, 32 bit signed integral type.

-30
     wchar.  Wide character, 16 bits wide, unsigned (what format? Unicode?).

Miscellaneous types
===================

These type descriptors are for types that are built into languages and are derived from the fundamental types.

b TYPE-INFORMATION ; BYTES
     Pascal space type.  This is documented by IBM; what does it mean?

     This use of the b type descriptor can be distinguished from its use for builtin integral types (see Builtin type descriptors) because the character following the type descriptor is always a digit, (, or -.

B TYPE-INFORMATION
     A volatile-qualified version of TYPE-INFORMATION.  This is a Sun extension.  References and stores to a variable with a volatile-qualified type must not be optimized or cached; they must occur as the user specifies them.

d TYPE-INFORMATION
     File of type TYPE-INFORMATION.  As far as I know this is only used by Pascal.

k TYPE-INFORMATION
     A const-qualified version of TYPE-INFORMATION.  This is a Sun extension.  A variable with a const-qualified type cannot be modified.

M TYPE-INFORMATION ; LENGTH
     Multiple instance type.  The type seems to composed of LENGTH repetitions of TYPE-INFORMATION, for example character*3 is represented by M-2;3, where -2 is a reference to a character type (see Negative type numbers).  I'm not sure how this differs from an array.  This appears to be a Fortran feature. LENGTH is a bound, like those in range types; see Subranges.

S TYPE-INFORMATION
     Pascal set type.  TYPE-INFORMATION must be a small type such as an enumeration or a subrange, and the type is a bitmask whose length is specified by the number of elements in TYPE-INFORMATION.

* TYPE-INFORMATION
     Pointer to TYPE-INFORMATION.

Cross-references to other types
===============================

A type can be used before it is defined; one common way to deal with that situation is just to use a type reference to a type which has not yet been defined.

Another way is with the x type descriptor, which is followed by s for a structure tag, u for a union tag, or e for a enumerator tag, followed by the name of the tag, followed by :. For example, the following C declarations:

     struct foo;
     struct foo *bar;

produce:

     .stabs "bar:G16=*17=xsfoo:",32,0,0,0

Not all debuggers support the x type descriptor, so on some machines GCC does not use it.  I believe that for the above example it would just emit a reference to type 17 and never define it, but I haven't verified that.

Modula-2 imported types, at least on AIX, use the i type descriptor, which is followed by the name of the module from which the type is imported, followed by :, followed by the name of the type.  There is then optionally a comma followed by type information for the type.  This differs from merely naming the type (see Typedefs) in that it identifies the module; I don't understand whether the name of the type given here is always just the same as the name we are giving it, or whether this type descriptor is used with a nameless stab (see String field), or what.  The symbol ends with ;.

Subrange types
==============

The r type descriptor defines a type as a subrange of another type.  It is followed by type information for the type of which it is a subrange, a semicolon, an integral lower bound, a semicolon, an integral upper bound, and a semicolon.  The AIX documentation does not specify the trailing semicolon, in an effort to specify array indexes more cleanly, but a subrange which is not an array index has always included a trailing semicolon (see Arrays).

Instead of an integer, either bound can be one of the following:

A OFFSET
     The bound is passed by reference on the stack at offset OFFSET from the argument list.  See Parameters, for more information on such offsets.

T OFFSET
     The bound is passed by value on the stack at offset OFFSET from the argument list.

a REGISTER-NUMBER
     The bound is pased by reference in register number REGISTER-NUMBER.

t REGISTER-NUMBER
     The bound is passed by value in register number REGISTER-NUMBER.

J
     There is no bound.

Subranges are also used for builtin types; see Traditional builtin types.

Array types
===========

Arrays use the a type descriptor.  Following the type descriptor is the type of the index and the type of the array elements.  If the index type is a range type, it ends in a semicolon; otherwise (for example, if it is a type reference), there does not appear to be any way to tell where the types are separated.  In an effort to clean up this mess, IBM documents the two types as being separated by a semicolon, and a range type as not ending in a semicolon (but this is not right for range types which are not array indexes, see Subranges).  I think probably the best solution is to specify that a semicolon ends a range type, and that the index type and element type of an array are separated by a semicolon, but that if the index type is a range type, the extra semicolon can be omitted.  GDB (at least through version 4.9) doesn't support any kind of index type other than a range anyway; I'm not sure about dbx.

It is well established, and widely used, that the type of the index, unlike most types found in the stabs, is merely a type definition, not type information (see String field) (that is, it need not start with TYPE-NUMBER= if it is defining a new type).  According to a comment in GDB, this is also true of the type of the array elements; it gives ar1;1;10;ar1;1;10;4 as a legitimate way to express a two dimensional array.  According to AIX documentation, the element type must be type information.  GDB accepts either.

The type of the index is often a range type, expressed as the letter r and some parameters.  It defines the size of the array.  In the example below, the range r1;0;2; defines an index type which is a subrange of type 1 (integer), with a lower bound of 0 and an upper bound of 2.  This defines the valid range of subscripts of a three-element C array.

For example, the definition:

     char char_vec[3] = {'a','b','c'};

produces the output:

     .stabs "char_vec:G19=ar1;0;2;2",32,0,0,0
          .global _char_vec
          .align 4
     _char_vec:
          .byte 97
          .byte 98
          .byte 99

If an array is packed, the elements are spaced more closely than normal, saving memory at the expense of speed.  For example, an array of 3-byte objects might, if unpacked, have each element aligned on a 4-byte boundary, but if packed, have no padding. One way to specify that something is packed is with type attributes (see String field).  In the case of arrays, another is to use the P type descriptor instead of a.  Other than specifying a packed array, P is identical to a.

An open array is represented by the A type descriptor followed by type information specifying the type of the array elements.

An N-dimensional dynamic array is represented by

     D DIMENSIONS ; TYPE-INFORMATION

DIMENSIONS is the number of dimensions; TYPE-INFORMATION specifies the type of the array elements.

A subarray of an N-dimensional array is represented by

     E DIMENSIONS ; TYPE-INFORMATION

DIMENSIONS is the number of dimensions; TYPE-INFORMATION specifies the type of the array elements.

Strings
=======

Some languages, like C or the original Pascal, do not have string types, they just have related things like arrays of characters.  But most Pascals and various other languages have string types, which are indicated as follows:

n TYPE-INFORMATION ; BYTES
     BYTES is the maximum length.  I'm not sure what TYPE-INFORMATION is; I suspect that it means that this is a string of TYPE-INFORMATION (thus allowing a string of integers, a string of wide characters, etc., as well as a string of characters).  Not sure what the format of this type is.  This is an AIX feature.

z TYPE-INFORMATION ; BYTES
     Just like n except that this is a gstring, not an ordinary string.  I don't know the difference.

N
     Pascal Stringptr.  What is this?  This is an AIX feature.

Enumerations
============

Enumerations are defined with the e type descriptor.

The source line below declares an enumeration type at file scope. The type definition is located after the N_RBRAC that marks the end of the previous procedure's block scope, and before the N_FUN that marks the beginning of the next procedure's block scope.  Therefore it does not describe a block local symbol, but a file local one.

The source line:

     enum e_places {first,second=3,last};

generates the following stab:

     .stabs "e_places:T22=efirst:0,second:3,last:4,;",128,0,0,0

The symbol descriptor (T) says that the stab describes a structure, enumeration, or union tag.  The type descriptor e, following the 22= of the type definition narrows it down to an enumeration type.  Following the e is a list of the elements of the enumeration.  The format is NAME:VALUE,.  The list of elements ends with a ;.

There is no standard way to specify the size of an enumeration type; it is determined by the architecture (normally all enumerations types are 32 bits).  There should be a way to specify an enumeration type of another size; type attributes would be one way to do this.  See Stabs format.

Structures
==========

The encoding of structures in stabs can be shown with an example.

The following source code declares a structure tag and defines an instance of the structure in global scope. Then a typedef equates the structure tag with a new type.  Seperate stabs are generated for the structure tag, the structure typedef, and the structure instance.  The stabs for the tag and the typedef are emited when the definitions are encountered.  Since the structure elements are not initialized, the stab and code for the structure variable itself is located at the end of the program in the bss section.

     struct s_tag {
       int   s_int;
       float s_float;
       char  s_char_vec[8];
       struct s_tag* s_next;
     } g_an_s;

     typedef struct s_tag s_typedef;

The structure tag has an N_LSYM stab type because, like the enumeration, the symbol has file scope.  Like the enumeration, the symbol descriptor is T, for enumeration, structure, or tag type. The symbol descriptor s following the 16= of the type definition narrows the symbol type to structure.

Following the structure symbol descriptor is the number of bytes the structure occupies, followed by a description of each structure element. The structure element descriptions are of the form NAME:TYPE, BIT OFFSET FROM THE START OF THE STRUCT, NUMBER OF BITS IN THE ELEMENT.

     .stabs "name:sym_desc(struct tag) Type_def(16)=type_desc(struct type)
             struct_bytes
             elem_name:type_ref(int),bit_offset,field_bits;
             elem_name:type_ref(float),bit_offset,field_bits;
             elem_name:type_def(17)=type_desc(array)
             index_type(range of int from 0 to 7);
             element_type(char),bit_offset,field_bits;;",
            N_LSYM,NIL,NIL,NIL

     .stabs "s_tag:T16=s20s_int:1,0,32;s_float:12,32,32;
             s_char_vec:17=ar1;0;7;2,64,64;s_next:18=*16,128,32;;",128,0,0,0

In this example, the first two structure elements are previously defined types.  For these, the type following the NAME: part of the element description is a simple type reference.  The other two structure elements are new types.  In this case there is a type definition embedded after the NAME:.  The type definition for the array element looks just like a type definition for a standalone array. The s_next field is a pointer to the same kind of structure that the field is an element of.  So the definition of structure type 16 contains a type definition for an element which is a pointer to type 16.

Giving a type a name
====================

To give a type a name, use the t symbol descriptor.  The type is specified by the type information (see String field) for the stab. For example,

     .stabs "s_typedef:t16",128,0,0,0

specifies that s_typedef refers to type number 16.  Such stabs have symbol type N_LSYM (or C_DECL on AIX).

If you are specifying the tag name for a structure, union, or enumeration, use the T symbol descriptor instead.  I believe C is the only language with this feature.

If the type is an opaque type (I believe this is a Modula-2 feature), AIX provides a type descriptor to specify it.  The type descriptor is o and is followed by a name.  I don't know what the name means---is it always the same as the name of the type, or is this type descriptor used with a nameless stab (see String field)?  There optionally follows a comma followed by type information which defines the type of this type.  If omitted, a semicolon is used in place of the comma and the type information, and the type is much like a generic pointer type---it has a known size but little else about it is specified.

Unions
======

     union u_tag {
       int  u_int;
       float u_float;
       char* u_char;
     } an_u;

This code generates a stab for a union tag and a stab for a union variable.  Both use the N_LSYM stab type.  If a union variable is scoped locally to the procedure in which it is defined, its stab is located immediately preceding the N_LBRAC for the procedure's block start.

The stab for the union tag, however, is located preceding the code for the procedure in which it is defined.  The stab type is N_LSYM.  This would seem to imply that the union type is file scope, like the struct type s_tag.  This is not true.  The contents and position of the stab for u_type do not convey any infomation about its procedure local scope.

     .stabs "name:sym_desc(union tag)type_def(22)=type_desc(union)
             byte_size(4)
             elem_name:type_ref(int),bit_offset(0),bit_size(32);
             elem_name:type_ref(float),bit_offset(0),bit_size(32);
             elem_name:type_ref(ptr to char),bit_offset(0),bit_size(32);;"
            N_LSYM, NIL, NIL, NIL

     .stabs "u_tag:T23=u4u_int:1,0,32;u_float:12,0,32;u_char:21,0,32;;",
            128,0,0,0

The symbol descriptor T, following the name: means that the stab describes an enumeration, structure, or union tag.  The type descriptor u, following the 23= of the type definition, narrows it down to a union type definition.  Following the u is the number of bytes in the union.  After that is a list of union element descriptions.  Their format is NAME:TYPE, BIT OFFSET INTO THE UNION, NUMBER OF BYTES FOR THE ELEMENT;.

The stab for the union variable is:

     .stabs "name:type_ref(u_tag)", N_LSYM, NIL, NIL, frame_ptr_offset

     .stabs "an_u:23",128,0,0,-20

Function types
==============

Various types can be defined for function variables.  These types are not used in defining functions (see Procedures); they are used for things like pointers to functions.

The simple, traditional, type is type descriptor f is followed by type information for the return type of the function, followed by a semicolon.

This does not deal with functions for which the number and types of the parameters are part of the type, as in Modula-2 or ANSI C.  AIX provides extensions to specify these, using the f, F, p, and R type descriptors.

First comes the type descriptor.  If it is f or F, this type involves a function, and the type information for the return type of the function follows, followed by a comma.  Then comes the number of parameters to the function and a semicolon.  Then, for each parameter, there is the name of the parameter followed by a colon (this is only present for type descriptors R and F which represent Pascal function or procedure parameters), type information for the parameter, a comma, 0 if passed by reference or 1 if passed by value, and a semicolon.  The type definition ends with a semicolon.

For example, this variable definition:

     int (*g_pf)();

generates the following code:

     .stabs "g_pf:G24=*25=f1",32,0,0,0
         .common _g_pf,4,"bss"

The variable defines a new type, 24, which is a pointer to another new type, 25, which is a function returning int.

Symbol information in symbol tables
***********************************

This chapter describes the format of symbol table entries and how stab assembler directives map to them.  It also describes the transformations that the assembler and linker make on data from stabs.

Symbol table format
===================

Each time the assembler encounters a stab directive, it puts each field of the stab into a corresponding field in a symbol table entry of its output file.  If the stab contains a STRING field, the symbol table entry for that stab points to a string table entry containing the string data from the stab.  Assembler labels become relocatable addresses.  Symbol table entries in a.out have the format:

     struct internal_nlist {
       unsigned long n_strx;         /* index into string table of name */
       unsigned char n_type;         /* type of symbol */
       unsigned char n_other;        /* misc info (usually empty) */
       unsigned short n_desc;        /* description field */
       bfd_vma n_value;              /* value of symbol */
     };

For .stabs directives, the n_strx field holds the offset in bytes from the start of the string table to the string table entry containing the STRING field.  For other classes of stabs (.stabn and .stabd) this field is zero.

Symbol table entries with n_type field values greater than 0x1f originated as stabs generated by the compiler (with one random exception).  The other entries were placed in the symbol table of the executable by the assembler or the linker.

Transformations on symbol tables
================================

The linker concatenates object files and does fixups of externally defined symbols.

You can see the transformations made on stab data by the assembler and linker by examining the symbol table after each pass of the build.  To do this, use nm -ap, which dumps the symbol table, including debugging information, unsorted.  For stab entries the columns are: VALUE, OTHER, DESC, TYPE, STRING.  For assembler and linker symbols, the columns are: VALUE, TYPE, STRING.

Where the VALUE field of a stab contains a frame pointer offset, or a register number, that VALUE is unchanged by the rest of the build.

Where the VALUE field of a stab contains an assembly language label, it is transformed by each build step.  The assembler turns it into a relocatable address and the linker turns it into an absolute address.

Transformations on static variables
-----------------------------------

This source line defines a static variable at file scope:

     static int s_g_repeat

The following stab describes the symbol:

     .stabs "s_g_repeat:S1",38,0,0,_s_g_repeat

The assembler transforms the stab into this symbol table entry in the .o file.  The location is expressed as a data segment offset.

     00000084 - 00 0000 STSYM s_g_repeat:S1

In the symbol table entry from the executable, the linker has made the relocatable address absolute.

     0000e00c - 00 0000 STSYM s_g_repeat:S1

Transformations on global variables
-----------------------------------

Stabs for global variables do not contain location information. In this case, the debugger finds location information in the assembler or linker symbol table entry describing the variable.  The source line:

     char g_foo = 'c';

generates the stab:

     .stabs "g_foo:G2",32,0,0,0

The variable is represented by two symbol table entries in the object file (see below).  The first one originated as a stab.  The second one is an external symbol.  The upper case D signifies that the n_type field of the symbol table contains 7, N_DATA with local linkage.  The VALUE field is empty for the stab entry.  For the linker symbol, it contains the relocatable address corresponding to the variable.

     00000000 - 00 0000  GSYM g_foo:G2
     00000080 D _g_foo

These entries as transformed by the linker.  The linker symbol table entry now holds an absolute address:

     00000000 - 00 0000  GSYM g_foo:G2
     ...
     0000e008 D _g_foo

GNU C++ stabs
*************

Type descriptors added for C++ descriptions:

#
     method type (## if minimal debug)

@
     Member (class and variable) type.  It is followed by type information for the offset basetype, a comma, and type information for the type of the field being pointed to.  (FIXME: this is acknowledged to be gibberish.  Can anyone say what really goes here?).

     Note that there is a conflict between this and type attributes (see String field); both use type descriptor @. Fortunately, the @ type descriptor used in this C++ sense always will be followed by a digit, (, or -, and type attributes never start with those things.

Basic types for C++
===================

<< the examples that follow are based on a01.C >>

C++ adds two more builtin types to the set defined for C.  These are the unknown type and the vtable record type.  The unknown type, type 16, is defined in terms of itself like the void type.

The vtable record type, type 17, is defined as a structure type and then as a structure tag.  The structure has four fields: delta, index, pfn, and delta2.  pfn is the function pointer.

<< In boilerplate $vtbl_ptr_type, what are the fields delta, index, and delta2 used for? >>

This basic type is present in all C++ programs even if there are no virtual methods defined.

     .stabs "struct_name:sym_desc(type)type_def(17)=type_desc(struct)struct_bytes(8)
             elem_name(delta):type_ref(short int),bit_offset(0),field_bits(16);
             elem_name(index):type_ref(short int),bit_offset(16),field_bits(16);
             elem_name(pfn):type_def(18)=type_desc(ptr to)type_ref(void),
                                         bit_offset(32),field_bits(32);
             elem_name(delta2):type_def(short int);bit_offset(32),field_bits(16);;"
             N_LSYM, NIL, NIL

     .stabs "$vtbl_ptr_type:t17=s8
             delta:6,0,16;index:6,16,16;pfn:18=*15,32,32;delta2:6,32,16;;"
             ,128,0,0,0

     .stabs "name:sym_dec(struct tag)type_ref($vtbl_ptr_type)",N_LSYM,NIL,NIL,NIL

     .stabs "$vtbl_ptr_type:T17",128,0,0,0

Simple class definition
=======================

The stabs describing C++ language features are an extension of the stabs describing C.  Stabs representing C++ class types elaborate extensively on the stab format used to describe structure types in C. Stabs representing class type variables look just like stabs representing C language variables.

Consider the following very simple class definition.

     class baseA {
     public:
             int Adat;
             int Ameth(int in, char other);
     };

The class baseA is represented by two stabs.  The first stab describes the class as a structure type.  The second stab describes a structure tag of the class type.  Both stabs are of stab type N_LSYM.  Since the stab is not located between an N_FUN and an N_LBRAC stab this indicates that the class is defined at file scope.  If it were, then the N_LSYM would signify a local variable.

A stab describing a C++ class type is similar in format to a stab describing a C struct, with each class member shown as a field in the structure.  The part of the struct format describing fields is expanded to include extra information relevent to C++ class members. In addition, if the class has multiple base classes or virtual functions the struct format outside of the field parts is also augmented.

In this simple example the field part of the C++ class stab representing member data looks just like the field part of a C struct stab.  The section on protections describes how its format is sometimes extended for member data.

The field part of a C++ class stab representing a member function differs substantially from the field part of a C struct stab.  It still begins with name: but then goes on to define a new type number for the member function, describe its return type, its argument types, its protection level, any qualifiers applied to the method definition, and whether the method is virtual or not.  If the method is virtual then the method description goes on to give the vtable index of the method, and the type number of the first base class defining the method.

When the field name is a method name it is followed by two colons rather than one.  This is followed by a new type definition for the method. This is a number followed by an equal sign and the type descriptor #, indicating a method type, and a second #, indicating that this is the minimal type of method definition used by GCC2, not larger method definitions used by earlier versions of GCC.  This is followed by a type reference showing the return type of the method and a semi-colon.

The format of an overloaded operator method name differs from that of other methods.  It is op$::OPERATOR-NAME. where OPERATOR-NAME is the operator name such as + or +=. The name ends with a period, and any characters except the period can occur in the OPERATOR-NAME string.

The next part of the method description represents the arguments to the method, preceeded by a colon and ending with a semi-colon.  The types of the arguments are expressed in the same way argument types are expressed in C++ name mangling.  In this example an int and a char map to ic.

This is followed by a number, a letter, and an asterisk or period, followed by another semicolon.  The number indicates the protections that apply to the member function.  Here the 2 means public.  The letter encodes any qualifier applied to the method definition.  In this case, A means that it is a normal function definition.  The dot shows that the method is not virtual.  The sections that follow elaborate further on these fields and describe the additional information present for virtual methods.

     .stabs "class_name:sym_desc(type)type_def(20)=type_desc(struct)struct_bytes(4)
             field_name(Adat):type(int),bit_offset(0),field_bits(32);

             method_name(Ameth)::type_def(21)=type_desc(method)return_type(int);
             :arg_types(int char);
             protection(public)qualifier(normal)virtual(no);;"
             N_LSYM,NIL,NIL,NIL

     .stabs "baseA:t20=s4Adat:1,0,32;Ameth::21=##1;:ic;2A.;;",128,0,0,0

     .stabs "class_name:sym_desc(struct tag)",N_LSYM,NIL,NIL,NIL

     .stabs "baseA:T20",128,0,0,0

Class instance
==============

As shown above, describing even a simple C++ class definition is accomplished by massively extending the stab format used in C to describe structure types.  However, once the class is defined, C stabs with no modifications can be used to describe class instances.  The following source:

     main () {
             baseA AbaseA;
     }

yields the following stab describing the class instance.  It looks no different from a standard C stab describing a local variable.

     .stabs "name:type_ref(baseA)", N_LSYM, NIL, NIL, frame_ptr_offset

     .stabs "AbaseA:20",128,0,0,-20

Method defintion
================

The class definition shown above declares Ameth.  The C++ source below defines Ameth:

     int
     baseA::Ameth(int in, char other)
     {
             return in;
     };

This method definition yields three stabs following the code of the method.  One stab describes the method itself and following two describe its parameters.  Although there is only one formal argument all methods have an implicit argument which is the this pointer.  The this pointer is a pointer to the object on which the method was called.  Note that the method name is mangled to encode the class name and argument types.  Name mangling is described in the ARM (The Annotated C++ Reference Manual, by Ellis and Stroustrup, ISBN 0-201-51459-1); gpcompare.texi in Cygnus GCC distributions describes the differences between GNU mangling and ARM mangling.

     .stabs "name:symbol_desriptor(global function)return_type(int)",
             N_FUN, NIL, NIL, code_addr_of_method_start

     .stabs "Ameth__5baseAic:F1",36,0,0,_Ameth__5baseAic

Here is the stab for the this pointer implicit argument.  The name of the this pointer is always this.  Type 19, the this pointer is defined as a pointer to type 20, baseA, but a stab defining baseA has not yet been emited.  Since the compiler knows it will be emited shortly, here it just outputs a cross reference to the undefined symbol, by prefixing the symbol name with xs.

     .stabs "name:sym_desc(register param)type_def(19)=
             type_desc(ptr to)type_ref(baseA)=
             type_desc(cross-reference to)baseA:",N_RSYM,NIL,NIL,register_number

     .stabs "this:P19=*20=xsbaseA:",64,0,0,8

The stab for the explicit integer argument looks just like a parameter to a C function.  The last field of the stab is the offset from the argument pointer, which in most systems is the same as the frame pointer.

     .stabs "name:sym_desc(value parameter)type_ref(int)",
             N_PSYM,NIL,NIL,offset_from_arg_ptr

     .stabs "in:p1",160,0,0,72

<< The examples that follow are based on A1.C >>

Protections
===========

In the simple class definition shown above all member data and functions were publicly accessable.  The example that follows contrasts public, protected and privately accessable fields and shows how these protections are encoded in C++ stabs.

Protections for class member data are signified by two characters embedded in the stab defining the class type.  These characters are located after the name: part of the string.  /0 means private, /1 means protected, and /2 means public.  If these characters are omited this means that the member is public.  The following C++ source:

     class all_data {
     private:
             int   priv_dat;
     protected:
             char  prot_dat;
     public:
             float pub_dat;
     };

generates the following stab to describe the class type all_data.

     .stabs "class_name:sym_desc(type)type_def(19)=type_desc(struct)struct_bytes
             data_name:/protection(private)type_ref(int),bit_offset,num_bits;
             data_name:/protection(protected)type_ref(char),bit_offset,num_bits;
             data_name:(/num omited, private)type_ref(float),bit_offset,num_bits;;"
             N_LSYM,NIL,NIL,NIL

     .stabs "all_data:t19=s12
             priv_dat:/01,0,32;prot_dat:/12,32,8;pub_dat:12,64,32;;",128,0,0,0

Protections for member functions are signified by one digit embeded in the field part of the stab describing the method.  The digit is 0 if private, 1 if protected and 2 if public.  Consider the C++ class definition below:

     class all_methods {
     private:
             int   priv_meth(int in){return in;};
     protected:
             char  protMeth(char in){return in;};
     public:
             float pubMeth(float in){return in;};
     };

It generates the following stab.  The digit in question is to the left of an A in each case.  Notice also that in this case two symbol descriptors apply to the class name struct tag and struct type.

     .stabs "class_name:sym_desc(struct tag&type)type_def(21)=
             sym_desc(struct)struct_bytes(1)
             meth_name::type_def(22)=sym_desc(method)returning(int);
             :args(int);protection(private)modifier(normal)virtual(no);
             meth_name::type_def(23)=sym_desc(method)returning(char);
             :args(char);protection(protected)modifier(normal)virual(no);
             meth_name::type_def(24)=sym_desc(method)returning(float);
             :args(float);protection(public)modifier(normal)virtual(no);;",
             N_LSYM,NIL,NIL,NIL

     .stabs "all_methods:Tt21=s1priv_meth::22=##1;:i;0A.;protMeth::23=##2;:c;1A.;
             pubMeth::24=##12;:f;2A.;;",128,0,0,0

Method modifiers (const, volatile, const volatile)
==================================================

<< based on a6.C >>

In the class example described above all the methods have the normal modifier.  This method modifier information is located just after the protection information for the method.  This field has four possible character values.  Normal methods use A, const methods use B, volatile methods use C, and const volatile methods use D.  Consider the class definition below:

     class A {
     public:
             int ConstMeth (int arg) const { return arg; };
             char VolatileMeth (char arg) volatile { return arg; };
             float ConstVolMeth (float arg) const volatile {return arg; };
     };

This class is described by the following stab:

     .stabs "class(A):sym_desc(struct)type_def(20)=type_desc(struct)struct_bytes(1)
             meth_name(ConstMeth)::type_def(21)sym_desc(method)
             returning(int);:arg(int);protection(public)modifier(const)virtual(no);
             meth_name(VolatileMeth)::type_def(22)=sym_desc(method)
             returning(char);:arg(char);protection(public)modifier(volatile)virt(no)
             meth_name(ConstVolMeth)::type_def(23)=sym_desc(method)
             returning(float);:arg(float);protection(public)modifer(const volatile)
             virtual(no);;", ...

     .stabs "A:T20=s1ConstMeth::21=##1;:i;2B.;VolatileMeth::22=##2;:c;2C.;
                  ConstVolMeth::23=##12;:f;2D.;;",128,0,0,0

Virtual methods
===============

<< The following examples are based on a4.C >>

The presence of virtual methods in a class definition adds additional data to the class description.  The extra data is appended to the description of the virtual method and to the end of the class description.  Consider the class definition below:

     class A {
     public:
             int Adat;
             virtual int A_virt (int arg) { return arg; };
     };

This results in the stab below describing class A.  It defines a new type (20) which is an 8 byte structure.  The first field of the class struct is Adat, an integer, starting at structure offset 0 and occupying 32 bits.

The second field in the class struct is not explicitly defined by the C++ class definition but is implied by the fact that the class contains a virtual method.  This field is the vtable pointer.  The name of the vtable pointer field starts with $vf and continues with a type reference to the class it is part of.  In this example the type reference for class A is 20 so the name of its vtable pointer field is $vf20, followed by the usual colon.

Next there is a type definition for the vtable pointer type (21). This is in turn defined as a pointer to another new type (22).

Type 22 is the vtable itself, which is defined as an array, indexed by a range of integers between 0 and 1, and whose elements are of type 17.  Type 17 was the vtable record type defined by the boilerplate C++ type definitions, as shown earlier.

The bit offset of the vtable pointer field is 32.  The number of bits in the field are not specified when the field is a vtable pointer.

Next is the method definition for the virtual member function A_virt. Its description starts out using the same format as the non-virtual member functions described above, except instead of a dot after the A there is an asterisk, indicating that the function is virtual. Since is is virtual some addition information is appended to the end of the method description.

The first number represents the vtable index of the method.  This is a 32 bit unsigned number with the high bit set, followed by a semi-colon.

The second number is a type reference to the first base class in the inheritence hierarchy defining the virtual member function.  In this case the class stab describes a base class so the virtual function is not overriding any other definition of the method.  Therefore the reference is to the type number of the class that the stab is describing (20).

This is followed by three semi-colons.  One marks the end of the current sub-section, one marks the end of the method field, and the third marks the end of the struct definition.

For classes containing virtual functions the very last section of the string part of the stab holds a type reference to the first base class.  This is preceeded by ~% and followed by a final semi-colon.

     .stabs "class_name(A):type_def(20)=sym_desc(struct)struct_bytes(8)
             field_name(Adat):type_ref(int),bit_offset(0),field_bits(32);
             field_name(A virt func ptr):type_def(21)=type_desc(ptr to)type_def(22)=
             sym_desc(array)index_type_ref(range of int from 0 to 1);
             elem_type_ref(vtbl elem type),
             bit_offset(32);
             meth_name(A_virt)::typedef(23)=sym_desc(method)returning(int);
             :arg_type(int),protection(public)normal(yes)virtual(yes)
             vtable_index(1);class_first_defining(A);;;~%first_base(A);",
             N_LSYM,NIL,NIL,NIL

     .stabs "A:t20=s8Adat:1,0,32;$vf20:21=*22=ar1;0;1;17,32;
             A_virt::23=##1;:i;2A*-2147483647;20;;;~%20;",128,0,0,0

Inheritence
===========

Stabs describing C++ derived classes include additional sections that describe the inheritence hierarchy of the class.  A derived class stab also encodes the number of base classes.  For each base class it tells if the base class is virtual or not, and if the inheritence is private or public.  It also gives the offset into the object of the portion of the object corresponding to each base class.

This additional information is embeded in the class stab following the number of bytes in the struct.  First the number of base classes appears bracketed by an exclamation point and a comma.

Then for each base type there repeats a series: two digits, a number, a comma, another number, and a semi-colon.

The first of the two digits is 1 if the base class is virtual and 0 if not.  The second digit is 2 if the derivation is public and 0 if not.

The number following the first two digits is the offset from the start of the object to the part of the object pertaining to the base class.

After the comma, the second number is a type_descriptor for the base type.  Finally a semi-colon ends the series, which repeats for each base class.

The source below defines three base classes A, B, and C and the derived class D.

     class A {
     public:
             int Adat;
             virtual int A_virt (int arg) { return arg; };
     };

     class B {
     public:
             int B_dat;
             virtual int B_virt (int arg) {return arg; };
     };

     class C {
     public:
             int Cdat;
             virtual int C_virt (int arg) {return arg; };
     };

     class D : A, virtual B, public C {
     public:
             int Ddat;
             virtual int A_virt (int arg ) { return arg+1; };
             virtual int B_virt (int arg)  { return arg+2; };
             virtual int C_virt (int arg)  { return arg+3; };
             virtual int D_virt (int arg)  { return arg; };
     };

Class stabs similar to the ones described earlier are generated for each base class.

     .stabs "A:T20=s8Adat:1,0,32;$vf20:21=*22=ar1;0;1;17,32;
             A_virt::23=##1;:i;2A*-2147483647;20;;;~%20;",128,0,0,0

     .stabs "B:Tt25=s8Bdat:1,0,32;$vf25:21,32;B_virt::26=##1;
             :i;2A*-2147483647;25;;;~%25;",128,0,0,0

     .stabs "C:Tt28=s8Cdat:1,0,32;$vf28:21,32;C_virt::29=##1;
             :i;2A*-2147483647;28;;;~%28;",128,0,0,0

In the stab describing derived class D below, the information about the derivation of this class is encoded as follows.

     .stabs "derived_class_name:symbol_descriptors(struct tag&type)=
             type_descriptor(struct)struct_bytes(32)!num_bases(3),
             base_virtual(no)inheritence_public(no)base_offset(0),
             base_class_type_ref(A);
             base_virtual(yes)inheritence_public(no)base_offset(NIL),
             base_class_type_ref(B);
             base_virtual(no)inheritence_public(yes)base_offset(64),
             base_class_type_ref(C); ...

     .stabs "D:Tt31=s32!3,000,20;100,25;0264,28;$vb25:24,128;Ddat:
             1,160,32;A_virt::32=##1;:i;2A*-2147483647;20;;B_virt:
             :32:i;2A*-2147483647;25;;C_virt::32:i;2A*-2147483647;
             28;;D_virt::32:i;2A*-2147483646;31;;;~%20;",128,0,0,0

Virtual base classes
====================

A derived class object consists of a concatination in memory of the data areas defined by each base class, starting with the leftmost and ending with the rightmost in the list of base classes.  The exception to this rule is for virtual inheritence.  In the example above, class D inherits virtually from base class B.  This means that an instance of a D object will not contain its own B part but merely a pointer to a B part, known as a virtual base pointer.

In a derived class stab, the base offset part of the derivation information, described above, shows how the base class parts are ordered.  The base offset for a virtual base class is always given as 0. Notice that the base offset for B is given as 0 even though B is not the first base class.  The first base class A starts at offset 0.

The field information part of the stab for class D describes the field which is the pointer to the virtual base class B. The vbase pointer name is $vb followed by a type reference to the virtual base class. Since the type id for B in this example is 25, the vbase pointer name is $vb25.

     .stabs "D:Tt31=s32!3,000,20;100,25;0264,28;$vb25:24,128;Ddat:1,
            160,32;A_virt::32=##1;:i;2A*-2147483647;20;;B_virt::32:i;
            2A*-2147483647;25;;C_virt::32:i;2A*-2147483647;28;;D_virt:
            :32:i;2A*-2147483646;31;;;~%20;",128,0,0,0

Following the name and a semicolon is a type reference describing the type of the virtual base class pointer, in this case 24.  Type 24 was defined earlier as the type of the B class this pointer.  The this pointer for a class is a pointer to the class type.

     .stabs "this:P24=*25=xsB:",64,0,0,8

Finally the field offset part of the vbase pointer field description shows that the vbase pointer is the first field in the D object, before any data fields defined by the class.  The layout of a D class object is a follows, Adat at 0, the vtable pointer for A at 32, Cdat at 64, the vtable pointer for C at 96, the virtual base pointer for B at 128, and Ddat at 160.

Static members
==============

The data area for a class is a concatenation of the space used by the data members of the class.  If the class has virtual methods, a vtable pointer follows the class data.  The field offset part of each field description in the class stab shows this ordering.

<< How is this reflected in stabs?  See Cygnus bug #677 for some info.  >>

Table of stab types
*******************

The following are all the possible values for the stab TYPE field, for a.out files, in numeric order.  This does not apply to XCOFF.

The symbolic names are defined in the file include/aout/stabs.def.

Non-stab symbol types
=====================

The following types are used by the linker and assembler, not by stab directives.  Since this document does not attempt to describe aspects of object file format other than the debugging format, no details are given.

0x0     N_UNDF
     Undefined symbol

0x2     N_ABS
     File scope absolute symbol

0x3     N_ABS | N_EXT
     External absolute symbol

0x4     N_TEXT
     File scope text symbol

0x5     N_TEXT | N_EXT
     External text symbol

0x6     N_DATA
     File scope data symbol

0x7     N_DATA | N_EXT
     External data symbol

0x8     N_BSS
     File scope BSS symbol

0x9     N_BSS | N_EXT
     External BSS symbol

0x0c    N_FN_SEQ
     Same as N_FN, for Sequent compilers

0x0a    N_INDR
     Symbol is indirected to another symbol

0x12    N_COMM
     Common---visible after shared library dynamic link

0x14 N_SETA
     Absolute set element

0x16 N_SETT
     Text segment set element

0x18 N_SETD
     Data segment set element

0x1a N_SETB
     BSS segment set element

0x1c N_SETV
     Pointer to set vector

0x1e N_WARNING
     Print a warning message during linking

0x1f    N_FN
     File name of a .o file

Stab symbol types
=================

The following symbol types indicate that this is a stab.  This is the full list of stab numbers, including stab types that are used in languages other than C.

0x20     N_GSYM
     Global symbol; see Global variables.

0x22     N_FNAME
     Function name (for BSD Fortran); see N_FNAME.

0x24     N_FUN
     Function name (see Procedures) or text segment variable (see Statics).

0x26 N_STSYM
     Data segment file-scope variable; see Statics.

0x28 N_LCSYM
     BSS segment file-scope variable; see Statics.

0x2a N_MAIN
     Name of main routine; see Main program.

0x2c N_ROSYM
     Variable in .rodata section; see Statics.

0x30     N_PC
     Global symbol (for Pascal); see N_PC.

0x32     N_NSYMS
     Number of symbols (according to Ultrix V4.0); see N_NSYMS.

0x34     N_NOMAP
     No DST map; see N_NOMAP.

0x38 N_OBJ
     Object file (Solaris2).

0x3c N_OPT
     Debugger options (Solaris2).

0x40     N_RSYM
     Register variable; see Register variables.

0x42     N_M2C
     Modula-2 compilation unit; see N_M2C.

0x44     N_SLINE
     Line number in text segment; see Line numbers.

0x46     N_DSLINE
     Line number in data segment; see Line numbers.

0x48     N_BSLINE
     Line number in bss segment; see Line numbers.

0x48     N_BROWS
     Sun source code browser, path to .cb file; see N_BROWS.

0x4a     N_DEFD
     GNU Modula2 definition module dependency; see N_DEFD.

0x4c N_FLINE
     Function start/body/end line numbers (Solaris2).

0x50     N_EHDECL
     GNU C++ exception variable; see N_EHDECL.

0x50     N_MOD2
     Modula2 info "for imc" (according to Ultrix V4.0); see N_MOD2.

0x54     N_CATCH
     GNU C++ catch clause; see N_CATCH.

0x60     N_SSYM
     Structure of union element; see N_SSYM.

0x62 N_ENDM
     Last stab for module (Solaris2).

0x64     N_SO
     Path and name of source file; see Source files.

0x80 N_LSYM
     Stack variable (see Stack variables) or type (see Typedefs).

0x82     N_BINCL
     Beginning of an include file (Sun only); see Include files.

0x84     N_SOL
     Name of include file; see Include files.

0xa0     N_PSYM
     Parameter variable; see Parameters.

0xa2     N_EINCL
     End of an include file; see Include files.

0xa4     N_ENTRY
     Alternate entry point; see N_ENTRY.

0xc0     N_LBRAC
     Beginning of a lexical block; see Block structure.

0xc2     N_EXCL
     Place holder for a deleted include file; see Include files.

0xc4     N_SCOPE
     Modula2 scope information (Sun linker); see N_SCOPE.

0xe0     N_RBRAC
     End of a lexical block; see Block structure.

0xe2     N_BCOMM
     Begin named common block; see Common blocks.

0xe4     N_ECOMM
     End named common block; see Common blocks.

0xe8     N_ECOML
     Member of a common block; see Common blocks.

0xea N_WITH
     Pascal with statement: type,,0,0,offset (Solaris2).

0xf0     N_NBTEXT
     Gould non-base registers; see Gould.

0xf2     N_NBDATA
     Gould non-base registers; see Gould.

0xf4     N_NBBSS
     Gould non-base registers; see Gould.

0xf6     N_NBSTS
     Gould non-base registers; see Gould.

0xf8     N_NBLCS
     Gould non-base registers; see Gould.

Table of symbol descriptors
***************************

These tell in the .stabs STRING field what kind of symbol the stab represents.  They follow the colon which follows the symbol name.  See String field, for more information about their use.

DIGIT
(
-
     Variable on the stack; see Stack variables.

a
     Parameter passed by reference in register; see Reference parameters.

c
     Constant; see Constants.

C
     Conformant array bound (Pascal, maybe other languages); Reference parameters.  Name of a caught exception (GNU C++).  These can be distinguished because the latter uses N_CATCH and the former uses another symbol type.

d
     Floating point register variable; see Register variables.

D
     Parameter in floating point register; see Register parameters.

f
     File scope function; see Procedures.

F
     Global function; see Procedures.

G
     Global variable; see Global variables.

i
     See Register parameters.

I
     Internal (nested) procedure; see Nested procedures.

J
     Internal (nested) function; see Nested procedures.

L
     Label name (documented by AIX, no further information known).

m
     Module; see Procedures.

p
     Argument list parameter; see Parameters.

pP
     See Parameters.

pF
     Fortran Function parameter; see Parameters.

P
     Unfortunately, three separate meanings have been independently invented for this symbol descriptor.  At least the GNU and Sun uses can be distinguished by the symbol type.  Global Procedure (AIX) (symbol type used unknown); see Procedures.  Register parameter (GNU) (symbol type N_PSYM); see Parameters.  Prototype of function referenced by this file (Sun acc) (symbol type N_FUN).

Q
     Static Procedure; see Procedures.

R
     Register parameter; see Register parameters.

r
     Register variable; see Register variables.

S
     File scope variable; see Statics.

t
     Type name; see Typedefs.

T
     Enumeration, structure, or union tag; see Typedefs.

v
     Parameter passed by reference; see Reference parameters.

V
     Procedure scope static variable; see Statics.

x
     Conformant array; see Conformant arrays.

X
     Function return variable; see Parameters.

Table of type descriptors
*************************

These tell in the .stabs STRING field what kind of type is being defined.  They follow the type number and an equals sign. See String field, for more information about their use.

DIGIT
(
     Type reference; see String field.

-
     Reference to builtin type; see Negative type numbers.

#
     Method (C++); see Cplusplus.

*
     Pointer; see Miscellaneous types.

&
     Reference (C++).

@
     Type Attributes (AIX); see String field.  Member (class and variable) type (GNU C++); see Cplusplus.

a
     Array; see Arrays.

A
     Open array; see Arrays.

b
     Pascal space type (AIX); see Miscellaneous types.  Builtin integer type (Sun); see Builtin type descriptors.

B
     Volatile-qualified type; see Miscellaneous types.

c
     Complex builtin type; see Builtin type descriptors.

C
     COBOL Picture type.  See AIX documentation for details.

d
     File type; see Miscellaneous types.

D
     N-dimensional dynamic array; see Arrays.

e
     Enumeration type; see Enumerations.

E
     N-dimensional subarray; see Arrays.

f
     Function type; see Function types.

F
     Pascal function parameter; see Function types

g
     Builtin floating point type; see Builtin type descriptors.

G
     COBOL Group.  See AIX documentation for details.

i
     Imported type; see Cross-references.

k
     Const-qualified type; see Miscellaneous types.

K
     COBOL File Descriptor.  See AIX documentation for details.

M
     Multiple instance type; see Miscellaneous types.

n
     String type; see Strings.

N
     Stringptr; see Strings.

o
     Opaque type; see Typedefs.

p
     Procedure; see Function types.

P
     Packed array; see Arrays.

r
     Range type; see Subranges.

R
     Builtin floating type; see Builtin type descriptors (Sun).  Pascal subroutine parameter; see Function types (AIX).  Detecting this conflict is possible with careful parsing (hint: a Pascal subroutine parameter type will always contain a comma, and a builtin type descriptor never will).

s
     Structure type; see Structures.

S
     Set type; see Miscellaneous types.

u
     Union; see Unions.

v
     Variant record.  This is a Pascal and Modula-2 feature which is like a union within a struct in C.  See AIX documentation for details.

w
     Wide character; see Builtin type descriptors.

x
     Cross-reference; see Cross-references.

z
     gstring; see Strings.

Expanded reference by stab type
*******************************

For a full list of stab types, and cross-references to where they are described, see Stab types.  This appendix just duplicates certain information from the main body of this document; eventually the information will all be in one place.

Format of an entry:

The first line is the symbol type (see include/aout/stab.def).

The second line describes the language constructs the symbol type represents.

The third line is the stab format with the significant stab fields named and the rest NIL.

Subsequent lines expand upon the meaning and possible values for each significant stab field.  # stands in for the type descriptor.

Finally, any further information.

N_FNAME
=======

 -- .stabs: N_FNAME
     Function name (for BSD Fortran).

          "name" -> "function_name"

     Only the STRING field is significant.  The location of the symbol is obtained from the corresponding extern symbol.

N_PC
====

 -- .stabs: N_PC
     Global symbol (for Pascal).

          "name" -> "symbol_name"  <<?>>
          value  -> supposedly the line number (stab.def is skeptical)

          stabdump.c says:

          global pascal symbol: name,,0,subtype,line
          << subtype? >>

N_NSYMS
=======

 -- .stabn: N_NSYMS
     Number of symbols (according to Ultrix V4.0).

                  0, files,,funcs,lines (stab.def)

N_NOMAP
=======

 -- .stabs: N_NOMAP
     No DST map for symbol (according to Ultrix V4.0).  I think this means a variable has been optimized out.

                  name, ,0,type,ignored (stab.def)

N_M2C
=====

 -- .stabs: N_M2C
     Modula-2 compilation unit.

          "string" -> "unit_name,unit_time_stamp[,code_time_stamp]"
          desc   -> unit_number
          value  -> 0 (main unit)
                    1 (any other unit)

N_BROWS
=======

 -- .stabs: N_BROWS
     Sun source code browser, path to .cb file

     <<?>> "path to associated .cb file"

     Note: TYPE field value overlaps with N_BSLINE.

N_DEFD
======

 -- .stabn: N_DEFD
     GNU Modula2 definition module dependency.

     GNU Modula-2 definition module dependency.  VALUE is the modification time of the definition file.  OTHER is non-zero if it is imported with the GNU M2 keyword %INITIALIZE.  Perhaps N_M2C can be used if there are enough empty fields?

N_EHDECL
========

 -- .stabs: N_EHDECL
     GNU C++ exception variable <<?>>.

     "STRING is variable name"

     Note: conflicts with N_MOD2.

N_MOD2
======

 -- .stab?: N_MOD2
     Modula2 info "for imc" (according to Ultrix V4.0)

     Note: conflicts with N_EHDECL  <<?>>

N_CATCH
=======

 -- .stabn: N_CATCH
     GNU C++ catch clause

     GNU C++ catch clause.  value is its address.  desc is nonzero if this entry is immediately followed by a CAUGHT stab saying what exception was caught.  Multiple CAUGHT stabs means that multiple exceptions can be caught here.  If desc is 0, it means all exceptions are caught here.

N_SSYM
======

 -- .stabn: N_SSYM
     Structure or union element.

     value is offset in the structure.

     <<?looking at structs and unions in C I didn't see these>>

N_ENTRY
=======

 -- .stabn: N_ENTRY
     Alternate entry point. value is its address. <<?>>

N_SCOPE
=======

 -- .stab?: N_SCOPE
     Modula2 scope information (Sun linker) <<?>>

Non-base registers on Gould systems
===================================

 -- .stab?: N_NBTEXT
 -- .stab?: N_NBDATA
 -- .stab?: N_NBBSS
 -- .stab?: N_NBSTS
 -- .stab?: N_NBLCS
     These are used on Gould systems for non-base registers syms.

     However, the following values are not the values used by Gould; they are the values which GNU has been documenting for these values for a long time, without actually checking what Gould uses.  I include these values only because perhaps some someone actually did something with the GNU information (I hope not, why GNU knowingly assigned wrong values to these in the header file is a complete mystery to me).

          240    0xf0     N_NBTEXT  ??
          242    0xf2     N_NBDATA  ??
          244    0xf4     N_NBBSS   ??
          246    0xf6     N_NBSTS   ??
          248    0xf8     N_NBLCS   ??

N_LENG
======

 -- .stabn: N_LENG
     Second symbol entry containing a length-value for the preceding entry. The VALUE is the length.

Questions and anomalies
***********************

  * For GNU C stabs defining local and global variables (N_LSYM and N_GSYM), the DESC field is supposed to contain the source line number on which the variable is defined.  In reality the DESC field is always 0.  (This behavior is defined in dbxout.c and putting a line number in DESC is controlled by #ifdef WINNING_GDB, which defaults to false). GDB supposedly uses this information if you say list VAR.  In reality, VAR can be a variable defined in the program and GDB says function VAR not defined.

  * In GNU C stabs, there seems to be no way to differentiate tag types: structures, unions, and enums (symbol descriptor T) and typedefs (symbol descriptor t) defined at file scope from types defined locally to a procedure or other more local scope.  They all use the N_LSYM stab type.  Types defined at procedure scope are emited after the N_RBRAC of the preceding function and before the code of the procedure in which they are defined.  This is exactly the same as types defined in the source file between the two procedure bodies. GDB overcompensates by placing all types in block #1, the block for symbols of file scope.  This is true for default, -ansi and -traditional compiler options. (Bugs gcc/1063, gdb/1066.)

  * What ends the procedure scope?  Is it the proc block's N_RBRAC or the next N_FUN?  (I believe its the first.)

  * Global variable stabs don't have location information.  This comes from the external symbol for the same variable.  The external symbol has a leading underbar on the _name of the variable and the stab does not.  How do we know these two symbol table entries are talking about the same symbol when their names are different? (Answer: the debugger knows that external symbols have leading underbars).

Differences between GNU stabs in a.out and GNU stabs in XCOFF
*************************************************************

The AIX/RS6000 native object file format is XCOFF with stabs.  This appendix only covers those differences which are not covered in the main body of this document.

  * BSD a.out stab types correspond to AIX XCOFF storage classes. In general the mapping is N_STABTYPE becomes C_STABTYPE. Some stab types in a.out are not supported in XCOFF; most of these use C_DECL.

     Exception: initialised static N_STSYM and un-initialized static N_LCSYM both map to the C_STSYM storage class.  But the destinction is preserved because in XCOFF N_STSYM and N_LCSYM must be emited in a named static block.  Begin the block with .bs s[RW] data_section_name for N_STSYM or .bs s bss_section_name for N_LCSYM.  End the block with .es.

  * If the XCOFF stab is an N_FUN (C_FUN) then follow the string field with ,. instead of just ,.

I think that's it for .s file differences.  They could stand to be better presented.  This is just a list of what I have noticed so far. There are a lot of differences in the information in the symbol tables of the executable and object files.

Mapping of a.out stab types to XCOFF storage classes:

     stab type       storage class
     -------------------------------
     N_GSYM          C_GSYM
     N_FNAME         unknown
     N_FUN           C_FUN
     N_STSYM         C_STSYM
     N_LCSYM         C_STSYM
     N_MAIN          unkown
     N_PC            unknown
     N_RSYM          C_RSYM
     unknown         C_RPSYM
     N_M2C           unknown
     N_SLINE         unknown
     N_DSLINE        unknown
     N_BSLINE        unknown
     N_BROWSE        unchanged
     N_CATCH         unknown
     N_SSYM          unknown
     N_SO            unknown
     N_LSYM          C_LSYM
     various         C_DECL
     N_BINCL         unknown
     N_SOL           unknown
     N_PSYM          C_PSYM
     N_EINCL         unknown
     N_ENTRY         C_ENTRY
     N_LBRAC         unknown
     N_EXCL          unknown
     N_SCOPE         unknown
     N_RBRAC         unknown
     N_BCOMM         C_BCOMM
     N_ECOMM         C_ECOMM
     N_ECOML         C_ECOML

     N_LENG          unknown

Differences between GNU stabs and Sun native stabs
**************************************************

  * GNU C stabs define all types, file or procedure scope, as N_LSYM.  Sun doc talks about using N_GSYM too.

  * Sun C stabs use type number pairs in the format (A,B) where A is a number starting with 1 and incremented for each sub-source file in the compilation.  B is a number starting with 1 and incremented for each new type defined in the compilation.  GNU C stabs use the type number alone, with no source file number.

Using stabs with the ELF object file format
*******************************************

The ELF object file format allows tools to create object files with custom sections containing any arbitrary data.  To use stabs in ELF object files, the tools create two custom sections, a section named .stab which contains an array of fixed length structures, one struct per stab, and a section named .stabstr containing all the variable length strings that are referenced by stabs in the .stab section.  The byte order of the stabs binary data matches the byte order of the ELF file itself, as determined from the EI_DATA field in the e_ident member of the ELF header.

The first stab in the .stab section for each source file is synthetic, generated entirely by the assembler, with no corresponding .stab directive as input to the assembler.  This stab contains the following fields:

n_strx
     Offset in the .stabstr section to the source filename.

n_type
     N_UNDF.

n_other
     Unused field, always zero.

n_desc
     Count of upcoming symbols, i.e., the number of remaining stabs for this source file.

n_value
     Size of the string table fragment associated with this source file, in bytes.

The .stabstr section always starts with a null byte (so that string offsets of zero reference a null string), followed by random length strings, each of which is null byte terminated.

The ELF section header for the .stab section has its sh_link member set to the section number of the .stabstr section, and the .stabstr section has its ELF section header sh_type member set to SHT_STRTAB to mark it as a string table.

Because the linker does not process the .stab section in any special way, none of the addresses in the n_value field of the stabs are relocated by the linker.  Instead they are relative to the source file (or some entity smaller than a source file, like a function).  To find the address of each section corresponding to a given source file, the (compiler? assembler?) puts out symbols giving the address of each section for a given source file.  Since these are normal ELF symbols, the linker can relocate them correctly.  They are named Bbss.bss for the bss section, Ddata.data for the data section, and Drodata.rodata for the rodata section.  I haven't yet figured out how the debugger gets the address for the text section.