Index: trunk/tools/elf2flt/elfflt.c
===================================================================
--- trunk/tools/elf2flt/elfflt.c	(revision 1505)
+++ trunk/tools/elf2flt/elfflt.c	(revision 1527)
@@ -43,11 +43,15 @@
 char* flat_buf;         // point to buffer of flat file
 struct flat_hdr* flat;  // point to flat_buf, but casted to header
-uint32_t* flat_reloc;   // point to begining of relocation table
-uint32_t* flat_reloc_cur; // ptr to current reloc value (for write_allocate)
-
-uint32_t* flat_import_buf; // point to begining of import table
-uint32_t* flat_import_cur; // ptr to current import value (for write_import)
+
+uint32_t flat_reloc_count;
+reloc_record_t* flat_reloc;   // point to begining of relocation table in memory
+reloc_record_t* flat_reloc_cur; // ptr to current reloc value (for write_allocate)
+
+uint32_t flat_import_count;
+import_record_t* flat_import_buf; // point to begining of import table in memory
+import_record_t* flat_import_cur; // ptr to current import value (for write_import)
 
 char* flag_sym_display=0;  // buffer of flags. [symidx]=0 not_showed_yet, 1 already_shown
+int flag_unsafe_sym=0;      // =1 if one of imported symbol is from stoplist
 
 /*---------------------------------------------------------------------------*/
@@ -195,4 +199,6 @@
 		  continue;
       }
+      if ( stoplist_check(name) )
+	      { flag_unsafe_sym=1; }
 
       ret = apply_import( base_sect, &rela, importidx, &s, relidx);
@@ -252,5 +258,5 @@
 
 static 
-void print_offs(char *prefix, int offs)
+void print_offs(char *prefix, int offs, char* postfix)
 {
     int secoffs = 0;
@@ -258,5 +264,5 @@
     
     if ( !offs ) {
-        printf("%s 0x0\n",prefix);
+        printf("%s 0x0 %s",prefix, postfix);
         return;
     }
@@ -264,9 +270,9 @@
     if ( offs >=flat->entry && offs<flat->data_start )
        { sect="text"; secoffs=flat->entry;}
-    else if  ( offs >=flat->data_start && offs<=flat->data_end )
+    else if  ( offs >=flat->data_start && offs<flat->bss_start )
        { sect="data"; secoffs=flat->data_start;}
-    else if  ( offs >flat->data_end && offs<=flat->bss_end )
-       { sect="bss"; secoffs=flat->data_end+1;}         
-    printf("%s 0x%x (%s+0x%x)\n",prefix, offs,sect,offs-secoffs);
+    else if  ( offs >=flat->bss_start && offs<flat->reloc_start )
+       { sect="bss"; secoffs=flat->bss_start;}         
+    printf("%s 0x%x (%s+0x%x)%s",prefix, offs,sect,offs-secoffs,postfix);
 }
 
@@ -282,5 +288,5 @@
 	}
 
-    if  ( offs >flat->data_end || offs<=flat->data_start )
+    if  ( offs >=flat->bss_start || offs<flat->data_start )
 	  return "";
 
@@ -566,5 +572,7 @@
   memset(flat_buf, 0, flatmainsize+flatrelocsize);
   
-  flat_import_buf=malloc( flatrelocsize );      		//import is subset of full reloc list, so this size is enough
+  //import is subset of full reloc list, so same count is enough
+  // but apply multiplier to take into account difference between sizeofs
+  flat_import_buf=malloc( flatrelocsize* sizeof(import_record_t)/sizeof(reloc_record_t) );      		
   if ( !flat_import_buf) { PRINTERR(stderr, "fail to malloc flat import buf\n"); return ELFFLT_OUTPUT_ERROR;}
   memset(flat_import_buf, 0, flatrelocsize);
@@ -599,18 +607,17 @@
 
   // Initialize flat headers
-  memcpy(flat->magic, FLAT_MAGIC_NUMBER, 4);       // Set magic (CHDK_FLAT)
+  memcpy(flat->magic, FLAT_MAGIC_NUMBER, sizeof(flat->magic));       // Set magic (CHDK_FLAT)
   flat->rev = FLAT_VERSION;
   flat->entry = text.flat_offset;
   flat->data_start = rodata.flat_offset;
-  flat->data_end = bss.flat_offset-1;  
-  flat->bss_end = flatmainsize-1;
+  flat->bss_start = bss.flat_offset;  
   flat->reloc_start = flatmainsize;
-  flat->reloc_count = 0;
+  flat_reloc_count = 0;
 
   //@tsv - this is for debug purpose only
-  flat->filler = data.flat_offset;
+  flat->filler1 = data.flat_offset;
 
   flat->import_start = 0;
-  flat->import_count = 0;
+  flat_import_count = 0;
 
   
@@ -622,4 +629,6 @@
   // _div0_arm hack
   add_div0_arm();
+
+  flag_unsafe_sym = 0;
 
   // Do relocations
@@ -634,7 +643,8 @@
       return ret;
 
-  flat->import_start = flat->reloc_start+flat->reloc_count*sizeof(uint32_t);
-
-
+  if ( flag_unsafe_sym )
+      return ELFFLT_UNSAFE_SYMBOL;
+
+  flat->import_start = flat->reloc_start+flat_reloc_count*sizeof(reloc_record_t);
 
   // Init offsets to the entry symbols
@@ -666,17 +676,24 @@
   }
 
+  // Prepare symbol list
+  flat->symbols_start = flat->import_start+flat_import_count*sizeof(import_record_t);
+  int flat_symtablesize=0;
+  flat->file_size = flat->symbols_start + flat_symtablesize;
+
+
+
   if ( FLAG_DUMP_FLT_HEADERS ) {
 	printf("\nFLT Headers:\n");
 	printf("->entry        0x%x (size %d)\n", flat->entry, flat->data_start - flat->entry );
-	printf("->data_start   0x%x (size %d)\n", flat->data_start,  flat->data_end - flat->data_start + 1 );
-	printf("->data_end     0x%x\n", flat->data_end );
-	printf("->bss_end      0x%x (size %d)\n", flat->bss_end, flat->bss_end - flat->data_end );
-	printf("->reloc_start  0x%x (size %d)\n", flat->reloc_start, flat->reloc_count*4 );
-	printf("->import_start 0x%x (size %d)\n", flat->import_start, flat->import_count*4 );
-
-	print_offs("\n.._module_loader()   =", flat->_module_loader);
-	print_offs(".._module_unloader() = ", flat->_module_unloader);
-	print_offs(".._module_run()      = ", flat->_module_run);
-	print_offs("..MODULE_EXPORT_LIST = ", flat->_module_exportlist);
+	printf("->data_start   0x%x (size %d)\n", flat->data_start,  flat->bss_start - flat->data_start );
+	printf("->bss_start    0x%x (size %d)\n", flat->bss_start,   flat->reloc_start - flat->bss_start );
+	printf("->reloc_start  0x%x (size %d)\n", flat->reloc_start, flat_reloc_count*sizeof(reloc_record_t) );
+	printf("->import_start 0x%x (size %d %d)\n", flat->import_start, flat->symbols_start-flat->import_start, flat_import_count*sizeof(import_record_t) );
+	printf("->symbol_start 0x%x (size %d)\n", flat->symbols_start, flat_symtablesize );
+
+	print_offs("\n.._module_loader()   =", flat->_module_loader,"\n");
+	print_offs(".._module_unloader() = ", flat->_module_unloader,"\n");
+	print_offs(".._module_run()      = ", flat->_module_run,"\n");
+	print_offs("..MODULE_EXPORT_LIST = ", flat->_module_exportlist,"\n");
 
 	printf("\nModule info:\n");
@@ -704,22 +721,24 @@
     dump_section( "FLT_header", flat_buf, sizeof(struct flat_hdr) );
     dump_section( "FLT_text", flat_buf+flat->entry, flat->data_start-flat->entry );
-    dump_section( "FLT_data", flat_buf+flat->data_start, flat->data_end-flat->data_start+1);
-    dump_section( "FLT_bss",  flat_buf+flat->data_end+1, flat->bss_end-flat->data_end );
+    dump_section( "FLT_data", flat_buf+flat->data_start, flat->bss_start-flat->data_start);
+    dump_section( "FLT_bss",  flat_buf+flat->bss_start, flat->reloc_start-flat->bss_start );
 
     printf("\nDump relocations:\n");
-    for( i = 0; i< flat->reloc_count; i++)
+    for( i = 0; i< flat_reloc_count; i++)
     {
-        print_offs("Offs: ",*(int*)(flat_buf+flat->reloc_start+i*4));
+        print_offs("Offs: ",*(int*)(flat_buf+flat->reloc_start+i*sizeof(reloc_record_t)), "\n");
     }
 
     printf("\nDump imports:\n");
-    for( i = 0; i< flat->import_count; i++)
+    for( i = 0; i< flat_import_count; i++)
     {
-		int offs=*(flat_import_buf+i);
-        print_offs("Offs: ",offs);
+		import_record_t* import=&flat_import_buf[i];
+        print_offs("Offs: ",import->offs,"");
+		int addend = *(uint32_t*)(flat_buf+import->offs);
+		printf(" = sym_%d[%s]+0x%x\n",import->importidx,get_import_symbol(import->importidx),addend);
     }
   }
 
-  int filesize = flat->import_start + flat->import_count*sizeof(uint32_t);
+  int filesize = flat->file_size;
 
   printf("\n\nOutput file %s (size=%d bytes)\n",fltfile,filesize);
@@ -727,5 +746,5 @@
   int output_fd = open(fltfile,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0777);
   write(output_fd, flat_buf, flat->import_start);
-  write(output_fd, flat_import_buf, flat->import_count*sizeof(uint32_t));
+  write(output_fd, flat_import_buf, flat_import_count*sizeof(import_record_t));
   close(output_fd);
 
