OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [tools/] [amber-elfsplitter.c] - Diff between revs 2 and 61

Show entire file | Details | Blame | View Log

Rev 2 Rev 61
Line 205... Line 205...
 
 
 
 
int main(int argc,char *argv[])
int main(int argc,char *argv[])
{
{
   FILE *infile,*outfile;
   FILE *infile,*outfile;
   unsigned char *buf;
   unsigned char *inbuf;
 
   unsigned char *outbuf;
   int buf_size;
   int buf_size;
   unsigned int length,i;
   unsigned int length,i;
   unsigned int StringSectionOffset;
   unsigned int StringSectionOffset;
   unsigned int StringSectionOffsetFound = 0;
   unsigned int StringSectionOffsetFound = 0;
 
   unsigned int outP;
 
 
   char filename_mem[80], filename_nopath[80];
   char filename_mem[80], filename_nopath[80];
   char tmp[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
   char tmp[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
   FILE *file_mem;
   FILE *file_mem;
   unsigned int j, k;
   unsigned int j, k, last_k;
   int m;
   int m;
   ElfHeader *elfHeader;
   ElfHeader *elfHeader;
   Elf32_Phdr *elfProgram;
   Elf32_Phdr *elfProgram;
   Elf32_Shdr *elfSection;
   Elf32_Shdr *elfSection;
   int stack_ptr16KB_flag=1;  /*  permanently switch on the -sp16k option */
   int stack_ptr16KB_flag=1;  /*  permanently switch on the -sp16k option */
   char* ptr=argv[1];
   char* ptr=argv[1];
 
 
   int infile_size;
   int infile_size;
 
   int boffset;
 
   int max_out = 0;
 
 
   if (argc<2){
   if (argc<2){
      printf("%s ERROR: no input file specified. Quitting\n", argv[0]);
      printf("%s ERROR: no input file specified. Quitting\n", argv[0]);
      exit(1);
      exit(1);
      }
      }
Line 237... Line 241...
      printf("%s ERROR: Can't open %s. Quitting\n", argv[0], argv[1]);
      printf("%s ERROR: Can't open %s. Quitting\n", argv[0], argv[1]);
      exit(1);
      exit(1);
   }
   }
   infile_size = fsize(infile);
   infile_size = fsize(infile);
 
 
   buf=(unsigned char*)malloc(infile_size);
   inbuf =(unsigned char*)malloc(infile_size);
   buf_size=fread(buf,1,infile_size,infile);
   outbuf=(unsigned char*)malloc(infile_size*2);
 
   buf_size=fread(inbuf,1,infile_size,infile);
   fclose(infile);
   fclose(infile);
 
 
   if ( buf_size != infile_size ) {
   if ( buf_size != infile_size ) {
      fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
      fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
      argv[0], argv[1], infile_size, buf_size);
      argv[0], argv[1], infile_size, buf_size);
Line 253... Line 258...
   if ( infile_size > 0x1000000 ) {
   if ( infile_size > 0x1000000 ) {
      fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
      fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
      argv[0], argv[1], infile_size);
      argv[0], argv[1], infile_size);
      }
      }
 
 
   elfHeader=(ElfHeader*)buf;
   elfHeader=(ElfHeader*)inbuf;
 
 
#ifdef DEBUG   
#ifdef DEBUG   
   strncpy(tmp, (char*)elfHeader->e_ident+1, 3);
   strncpy(tmp, (char*)elfHeader->e_ident+1, 3);
   printf("Debug: elfHeader->e_ident= %s\n",tmp);
   printf("Debug: elfHeader->e_ident= %s\n",tmp);
   printf("Debug: elfHeader->e_machine= 0x%x\n",elfHeader->e_machine);
   printf("Debug: elfHeader->e_machine= 0x%x\n",elfHeader->e_machine);
Line 279... Line 284...
#ifdef DEBUG   
#ifdef DEBUG   
   printf("Debug: elfHeader->e_phnum=0x%x\n",elfHeader->e_phnum);
   printf("Debug: elfHeader->e_phnum=0x%x\n",elfHeader->e_phnum);
#endif
#endif
 
 
   for(i=0;i<elfHeader->e_phnum;++i) {
   for(i=0;i<elfHeader->e_phnum;++i) {
      elfProgram           = (Elf32_Phdr*)(buf+elfHeader->e_phoff+elfHeader->e_phentsize*i);
      elfProgram           = (Elf32_Phdr*)(inbuf+elfHeader->e_phoff+elfHeader->e_phentsize*i);
 
 
      length=elfProgram->p_vaddr+elfProgram->p_memsz;
      length=elfProgram->p_vaddr+elfProgram->p_memsz;
#ifdef DEBUG   
#ifdef DEBUG   
      printf("Debug: Program Length=0x%x\n",length);
      printf("Debug: Program Length=0x%x\n",length);
#endif
#endif
Line 292... Line 297...
 
 
   /* Find the location in the file of the string section
   /* Find the location in the file of the string section
      containing the section names
      containing the section names
   */
   */
   for(i=0;i<elfHeader->e_shnum;++i) {
   for(i=0;i<elfHeader->e_shnum;++i) {
      elfSection=(Elf32_Shdr*)(buf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
      elfSection=(Elf32_Shdr*)(inbuf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
      if (elfSection->sh_type == SHT_STRTAB && !StringSectionOffsetFound) {
      if (elfSection->sh_type == SHT_STRTAB && !StringSectionOffsetFound) {
         StringSectionOffset      = elfSection->sh_offset;
         StringSectionOffset      = elfSection->sh_offset;
         StringSectionOffsetFound = 1;
         StringSectionOffsetFound = 1;
         }
         }
      }
      }
 
 
   for(i=0;i<elfHeader->e_shnum;++i) {
   for(i=0;i<elfHeader->e_shnum;++i) {
      elfSection=(Elf32_Shdr*)(buf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
      elfSection=(Elf32_Shdr*)(inbuf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
 
 
 
        /* Get the byte offset and use it to word-align the data */
 
        boffset = elfSection->sh_offset & 3;
 
 
        if (elfSection->sh_type != SHT_NULL) {
        if (elfSection->sh_type != SHT_NULL) {
          printf("// Section name %s\n", (char*)(buf+StringSectionOffset+elfSection->sh_name));
          printf("// Section name %s\n", (char*)(inbuf+StringSectionOffset+elfSection->sh_name));
          printf("//  Type %s, Size 0x%x, Start address 0x%08x, File offset 0x%x\n",
          printf("//  Type %s, Size 0x%x, Start address 0x%08x, File offset 0x%x, boffset %d\n",
              pSHT(elfSection->sh_type),
              pSHT(elfSection->sh_type),
              elfSection->sh_size,
              elfSection->sh_size,
              elfSection->sh_addr,
              elfSection->sh_addr,
              elfSection->sh_offset);
              elfSection->sh_offset,
 
              boffset);
           }
           }
 
 
 
 
        /* section with non-zero bits, can be either text or data */
        /* section with non-zero bits, can be either text or data */
        if (elfSection->sh_type == SHT_PROGBITS && elfSection->sh_size != 0) {
        if (elfSection->sh_type == SHT_PROGBITS && elfSection->sh_size != 0) {
            for (j=0; j<elfSection->sh_size; j=j+4) {
            for (j=0; j<elfSection->sh_size; j++) {
               k = j + elfSection->sh_offset;
               k = j + elfSection->sh_offset;
               printf("@%08x %02x%02x%02x%02x\n",
               outP = elfSection->sh_addr + j;
                      (elfSection->sh_addr + j),        /* use word addresses */
               outbuf[outP] = inbuf[k];
                      buf[k+3], buf[k+2], buf[k+1], buf[k+0]);
               if (outP > max_out) max_out = outP;
               }
               }
           }
           }
 
 
 
 
        if (elfSection->sh_type == SHT_NOBITS && elfSection->sh_size != 0) {
        if (elfSection->sh_type == SHT_NOBITS && elfSection->sh_size != 0) {
            printf("// .bss Dump Zeros\n");
            printf("// .bss Dump Zeros\n");
            for (j=elfSection->sh_offset; j<elfSection->sh_offset+elfSection->sh_size; j=j+4) {
            for (j=0; j<elfSection->sh_size; j++) {
               printf("@%08x 00000000\n",
               outP = j + elfSection->sh_addr;
               (j + elfSection->sh_addr - elfSection->sh_offset)); /* use word addresses */
               outbuf[outP] = 0;
 
               if (outP > max_out) max_out = outP;
 
               }
               }
               }
           }
           }
 
 
 
 
 
   for(j=0;j<max_out+3;j=j+4) {
 
        printf("@%08x %02x%02x%02x%02x\n", j, outbuf[j+3], outbuf[j+2], outbuf[j+1], outbuf[j+0]);
   }
   }
 
 
   free(buf);
   free(inbuf);
 
   free(outbuf);
 
 
   return 0;
   return 0;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.