home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 August - Disc 3 / chip_20018103_hu.iso / amiga / chiputil / gg / loadelfwos.lha / LoadElfWOS.lzx / src / symbols.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-01  |  3.3 KB  |  140 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <powerpc/powerpc.h>
  6. #include <proto/powerpc.h>
  7. #include "elf/external.h"
  8. #include "elf/common.h"
  9. #include "symbols.h"
  10. #include "error.h"
  11.  
  12. void print_symbols(PElfObject *);
  13.  
  14. int add_symbol_sect(PElfObject *obj,unsigned long i)
  15. {
  16.     int n;
  17.     int symbolsnum=obj->sections[i].size/sizeof(Elf32_External_Sym);
  18.     Elf32_External_Sym *symtab=(Elf32_External_Sym *)obj->sections[i].elfadr;
  19.     char *strings=obj->sections[obj->sections[i].link].elfadr;
  20.     int undefnum=0,absnum=0,commonnum=0,normalnum=0;
  21.     size_t commonsize=0;
  22.  
  23.     if(!(obj->symbols))
  24.     {
  25.         if(!(obj->symbols=calloc(symbolsnum,sizeof(PSymbol))))
  26.         {
  27.             error_printf("No mem for Symbol Table !");
  28.             return 0L;
  29.         }
  30.     }else{
  31.         error_printf("More than one symbol section ?");
  32.         error_printf("%s",obj->sections[i].name);
  33.         return 0L;
  34.     }
  35.  
  36.     obj->symbolscnt=symbolsnum;
  37.     
  38.     info_printf("Collecting %i Symbols!\n",symbolsnum);
  39.     for(n=1;n<symbolsnum;n++)
  40.     {
  41.         obj->symbols[n].name=strings+symtab[n].st_name;
  42.         obj->symbols[n].value=symtab[n].st_value;
  43.         obj->symbols[n].size=symtab[n].st_size;
  44.         obj->symbols[n].type=symtab[n].st_info;
  45.         obj->symbols[n].sectionindex=symtab[n].st_shndx;
  46.         if(ELF_ST_TYPE(obj->symbols[n].type)==STT_SECTION)
  47.             obj->symbols[n].name=obj->sections[obj->symbols[n].sectionindex].name;
  48.         switch(obj->symbols[n].sectionindex)
  49.         {
  50.             case SHN_UNDEF :
  51.                 error_printf("Undefined Symbol: %s",obj->symbols[n].name);
  52.                 undefnum++;
  53.                 break;
  54.  
  55.             case SHN_ABS :
  56.                 absnum++;
  57.                 break;
  58.  
  59.             case SHN_COMMON :
  60.                 {
  61.                     unsigned int tempalign=obj->symbols[n].value-1;
  62.                     unsigned int tempsize=obj->symbols[n].size;
  63.                     obj->symbols[n].value=(commonsize+tempalign)&(~tempalign);
  64.                     commonsize=obj->symbols[n].value+tempsize;
  65.                     obj->symbols[n].size=tempsize;
  66.                 }
  67.                 commonnum++;
  68.                 break;
  69.  
  70.             default:
  71.                 if(obj->symbols[n].sectionindex>obj->sectcnt)
  72.                 {
  73.                     error_printf("Unknown Sectionindex for Symbol: %s !",obj->symbols[n].name);
  74.                     return 0L;
  75.                 }
  76.                 obj->symbols[n].value+=(unsigned long)obj->sections[obj->symbols[n].sectionindex].virtadr;
  77.                 normalnum++;
  78.         }
  79.     }
  80.  
  81.     if(undefnum>0)
  82.     {
  83.         return 0L;
  84.     }
  85.  
  86.     info_printf("Collected Symbols:\n");
  87.     info_printf("%i undefined\n",undefnum);
  88.     info_printf("%i absolute\n",absnum);
  89.     info_printf("%i common  ",commonnum);
  90.     info_printf("Size: %li\n",commonsize);
  91.     info_printf("%i normal\n",normalnum);
  92.  
  93.     //Create Common Section
  94.  
  95.     if(commonsize)
  96.     {
  97.         if(!(obj->sections[0].virtadr=AllocVec32(commonsize,0L)))    // Probably should use AllocMem32 here
  98.         {
  99.             error_printf("No mem for common section !");
  100.             return 0L;
  101.         }
  102.         memset(obj->sections[0].virtadr,0,commonsize);
  103.     }
  104.  
  105.     obj->sections[0].name=".common";
  106.     obj->sections[0].elfadr=0L;
  107.     obj->sections[0].size=commonsize;
  108.     obj->sections[0].type=SHT_NOBITS;
  109.     obj->sections[0].flags=SHF_ALLOC;
  110.  
  111.     for(i=1;i<symbolsnum;i++)
  112.         if(obj->symbols[i].sectionindex==SHN_COMMON)
  113.         {
  114.             obj->symbols[i].sectionindex=0;
  115.             obj->symbols[i].value+=(unsigned long)obj->sections[0].virtadr;
  116.         }
  117.  
  118.     //print_symbols(obj);
  119.  
  120.     return 1L;
  121. }
  122.  
  123. void print_symbols(PElfObject *obj)
  124. {
  125.     int i;
  126.     for(i=1;i<obj->symbolscnt;i++)
  127.     {
  128.         info_printf("%-30s: %8lx\n",obj->symbols[i].name,obj->symbols[i].value);
  129.     }
  130. }
  131.  
  132. unsigned long get_symbol_by_name(PElfObject *obj,char *name)
  133. {
  134.     int i;
  135.     for(i=1;i<obj->symbolscnt;i++)
  136.         if(strcmp(name,obj->symbols[i].name)==0)
  137.             return obj->symbols[i].value;
  138.     return -1L;
  139. }
  140.