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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <proto/exec.h>
  6. #include <powerpc/powerpc.h>
  7. #include <proto/powerpc.h>
  8. #include "loadelf.h"
  9. #include "util.h"
  10. #include "error.h"
  11.  
  12. struct Library *PowerPCBase;
  13.  
  14. int main(int argc,char *argv[])
  15. {
  16.     struct PPCArgs Regs;
  17.     PElfObject *obj=0L;
  18.     char args[256]={0};
  19.     FILE *elffile=0L;
  20.     size_t elfsize=0L;
  21.     void *elfptr=0L;
  22.     int i=1;
  23.     
  24.             
  25.     if(argc>1)
  26.     {
  27.         if(!(elffile=fopen(argv[1],"rb")))
  28.         {
  29.             error_printf("Error opening %s : %s !",argv[1],strerror(errno));
  30.             return -10L;
  31.         }
  32.     
  33.         if(!(elfsize=filelength(elffile)))
  34.         {
  35.             error_printf("Elffile size 0 ?");
  36.             fclose(elffile);
  37.             return -10L;
  38.         }
  39.         
  40.         if(!(elfptr=malloc(elfsize)))
  41.         {
  42.             error_printf("No memory for elffile !");
  43.             fclose(elffile);
  44.             return -10L;
  45.         }
  46.         
  47.         if(fread(elfptr,1,elfsize,elffile)!=elfsize)
  48.         {
  49.             error_printf("Error reading Elffile: %s !",strerror(errno));
  50.             free(elfptr);
  51.             fclose(elffile);
  52.             return -10L;
  53.         }
  54.     
  55.         fclose(elffile);
  56.         
  57.         if((PowerPCBase=OpenLibrary("powerpc.library",14)))
  58.         {        
  59.             if((obj=alloc_elfobject(elfptr)))
  60.             {
  61.     
  62.                 free(elfptr);        //Save some memory! This works here, but can be dangerous in other contexts.
  63.     
  64.                 while((argc-i)>0)
  65.                 {
  66.                     strcat(args,argv[i]);
  67.                     strcat(args," ");
  68.                     i++;
  69.                 }
  70.  
  71.                 //printf("Starting program at %p\n",obj->sections[1].virtadr);
  72.  
  73.                 Regs.PP_Code=obj->sections[1].virtadr;
  74.                 Regs.PP_Offset=0L;
  75.                 Regs.PP_Flags=0L;
  76.                 Regs.PP_Stack=0L;
  77.                 Regs.PP_StackSize=0L;
  78.                 Regs.PP_Regs[0]=(ULONG)args;
  79.                 Regs.PP_Regs[1]=(ULONG)PowerPCBase;
  80.                 RunPPC(&Regs);
  81.             }
  82.             else
  83.             {
  84.                 printf("Failed to load : %s \n",argv[1]);
  85.                 free(elfptr);
  86.                 CloseLibrary(PowerPCBase);
  87.                 return -10L;
  88.             }
  89.  
  90.             free_elfobject(obj);
  91.             CloseLibrary(PowerPCBase);
  92.         }
  93.         else
  94.         {
  95.             free(elfptr);
  96.             printf("Could not open powerpc.library V14+\n");
  97.             return -10L;
  98.         }
  99.  
  100.     }else{
  101.         printf("Usage: LoadElfWOS program.elf [parameters]\n");
  102.     }
  103.  
  104.     return(Regs.PP_Regs[0]);
  105. }
  106.