================================================================================
B A S T A R D                                            disassembly environment


                         Bastard  Structure  HOWTO



================================================================================
 Contents

 1. The Representation of Data Structures
 2. Creating Structures
 3. Assigning Members to a Structure
 4. Printing a Structure in the Target Assembly Language
 5. Printing a Structure in the Target High-Level Language


================================================================================
 1. The Representation of Data Structures

   struct structure {  
      unsigned long    id;
      char    name[32];
      unsigned short   size;
   };

   struct struct_member { 
      unsigned long    id;
      unsigned long    type;
      int     size;
      unsigned long    structure;
      int     order;
      char    name[32];
   };


================================================================================
 2. Creating Structures

   int e_struct = DefineStruct( "Elf32_Ehdr", 52);


================================================================================
 3. Assigning Members to a Structure

   d1 = DefineDataType("Elf32_Addr", 4, DT_UNSIGNED);
   d2 = DefineDataType("Elf32_Half", 2, DT_UNSIGNED);
   d3 = DefineDataType("Elf32_Off", 4, DT_UNSIGNED);
   d4 = DefineDataType("Elf32_Sword", 4, DT_SIGNED);
   d5 = DefineDataType("Elf32_Word", 4, DT_UNSIGNED);
   d6 = GetDataType( "unsigned char");

   DefineStructMember(e_struct, d6, 16, 1, "e_ident");
   DefineStructMember(e_struct, d2, 1,  2, "e_type");
   DefineStructMember(e_struct, d2, 1,  3, "e_machine");
   DefineStructMember(e_struct, d5, 1,  4, "e_version");
   DefineStructMember(e_struct, d1, 1,  5, "e_entry");
   DefineStructMember(e_struct, d3, 1,  6, "e_phoff");
   DefineStructMember(e_struct, d3, 1,  7, "e_shoff");
   DefineStructMember(e_struct, d5, 1,  8, "e_flags");
   DefineStructMember(e_struct, d2, 1,  9, "e_ehsize");
   DefineStructMember(e_struct, d2, 1, 10, "e_phentsize");
   DefineStructMember(e_struct, d2, 1, 11, "e_phnum");
   DefineStructMember(e_struct, d2, 1, 12, "e_shentsize");
   DefineStructMember(e_struct, d2, 1, 13, "e_shsnum");
   DefineStructMember(e_struct, d2, 1, 14, "e_shstrndx");


================================================================================
 4. Applying a Structure to an Address

   ApplyStructure(0,e_struct);


================================================================================
 5. Printing a Structure in the Target Assembly Language
  

   int sprint_asm_struct( char *str, int len, long rva ) {
      struct address a;
      struct structure s;
      struct struct_member m;
      struct data_type t;
      char tmp[128];
      int cont;

      if (! DBIndexFind(ADDRESS_RVA, &rva, &a) )
         return(SetLastError(0));
      if (! DBIndexFind(STRUCTURE_ID, &a.structure, &s))
         return(SetLastError(0));

      /* print structure  header */
      snprintf(str, len, "STRUCT %s:\t%s %s bytes\n",
                        s.name, settings->comment, s.size);
      cont = DBIndexFind(STRUCT_MEMBER_STRUCTURE, &s.id, &m);
      while (cont && m.structure == s.id) {
         DBIndexFind(DATA_TYPE_ID, &m.type, &t);
         snprintf(tmp, 128, "\t%s %s\n", t.name, m.name);
         strncat(str, tmp, len);
         cont = DBIndexNext(STRUCT_MEMBER_STRUCTURE, &m);
      }
      /* Print Structure End */
      sprintf(tmp, "%s ENDSTRUCT %s\n", settings->comment, s.name);
      strncat(str, tmp, len);
      return(1);
   }

================================================================================
 6. Printing a Structure in the Target High-Level Language

