Next: , Previous: Enumerations, Up: Types


5.8 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. Separate stabs are generated for the structure tag, the structure typedef, and the structure instance. The stabs for the tag and the typedef are emitted 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 type descriptor ‘s’ following the ‘16=’ of the type definition narrows the symbol type to structure.

Following the ‘s’ type 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’.

     # 128 is N_LSYM
     .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 stand-alone 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.

If a field is a static member (this is a C++ feature in which a single variable appears to be a field of every structure of a given type) it still starts out with the field name, a colon, and the type, but then instead of a comma, bit position, comma, and bit size, there is a colon followed by the name of the variable which each such field refers to.

If the structure has methods (a C++ feature), they follow the non-method fields; see Cplusplus.