================================================================================
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 = struct_new( "Elf32_Ehdr", 52);


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

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

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


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

   struct_apply(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 (! db_index_find(ADDRESS_RVA, &rva, &a) )
         return(sys_set_lasterr(0));
      if (! db_index_find(STRUCTURE_ID, &a.structure, &s))
         return(sys_set_lasterr(0));

      /* print structure  header */
      snprintf(str, len, "STRUCT %s:\t%s %s bytes\n",
                        s.name, settings->comment, s.size);
      cont = db_index_find(STRUCT_MEMBER_STRUCTURE, &s.id, &m);
      while (cont && m.structure == s.id) {
         db_index_find(DATA_TYPE_ID, &m.type, &t);
         snprintf(tmp, 128, "\t%s %s\n", t.name, m.name);
         strncat(str, tmp, len);
         cont = db_index_next(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

