Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "tok.h"
  2.  
  3. #include "le.h"
  4.  
  5.  
  6.  
  7. #define _OUTLE_
  8.  
  9.  
  10.  
  11. unsigned short swapshort(unsigned int var)
  12.  
  13. {
  14.  
  15. unsigned short h,l;
  16.  
  17.         l=var>>8;
  18.  
  19.         h=var<<8;
  20.  
  21.         return l|h;
  22.  
  23. }
  24.  
  25.  
  26.  
  27. int MakeBin32()
  28.  
  29. {
  30.  
  31.         hout=CreateOutPut(outext,"wb");
  32.  
  33.         if(fwrite(output,outptr,1,hout)!=1){
  34.  
  35.                 ErrWrite();
  36.  
  37.                 fclose(hout);
  38.  
  39.                 hout=NULL;
  40.  
  41.                 return(-1);
  42.  
  43.         }
  44.  
  45.         return 0;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. unsigned int EntryParamStr()
  52.  
  53. {
  54.  
  55. ITOK btok;
  56.  
  57. int bb=tk_id;
  58.  
  59. unsigned char *buf=(unsigned char *)"__bufcomstr";
  60.  
  61.         btok.number=0;
  62.  
  63.         searchtree(&btok,&bb,buf);
  64.  
  65.         if(bb==tk_id)btok.number=0;
  66.  
  67.         else{
  68.  
  69.                 btok.number+=outptrdata;
  70.  
  71.                 if((outptrdata%2)==1)btok.number++;     /* alignment of entire post data block manditory */
  72.  
  73.         }
  74.  
  75.         return btok.number;
  76.  
  77. }
  78.  
  79.  
  80.  
  81. int MakeMEOS()
  82.  
  83. {
  84.  
  85. MEOSheader hdr;
  86.  
  87.         hout=CreateOutPut(outext,"wb");
  88.  
  89.         strcpy((char *)hdr.sign,"MENUET01");
  90.  
  91.         hdr.vers=1;
  92.  
  93.         hdr.start=EntryPoint();
  94.  
  95.         hdr.size=outptr;
  96.  
  97.         hdr.alloc_mem=Align(outptr+postsize+stacksize,16);
  98.  
  99.         hdr.esp=hdr.alloc_mem-4;
  100.  
  101.         hdr.I_Param=EntryParamStr();
  102.  
  103.         hdr.I_Icon=0;
  104.  
  105.         memcpy(output+startptr,&hdr,sizeof(MEOSheader));
  106.  
  107.         if(fwrite(output,outptr,1,hout)!=1){
  108.  
  109.                 ErrWrite();
  110.  
  111.                 fclose(hout);
  112.  
  113.                 hout=NULL;
  114.  
  115.                 return(-1);
  116.  
  117.         }
  118.  
  119.         return 0;
  120.  
  121. }
  122.  
  123.  
  124.  
  125. int MakeLE()
  126.  
  127. {
  128.  
  129. LE_Header hdr;
  130.  
  131. Object_Table ot[2];
  132.  
  133. Page_Map_Table *pmt;
  134.  
  135. unsigned int i;
  136.  
  137. int headerofs;
  138.  
  139. int headsize=sizeof(LE_Header);
  140.  
  141. unsigned long *fpto;
  142.  
  143. int sizeb;
  144.  
  145.         CreatStub(stubfile);
  146.  
  147.         memset(&hdr,0,sizeof(LE_Header));
  148.  
  149. #ifdef _WC_
  150.  
  151.         hdr.Signature='EL';
  152.  
  153. #else
  154.  
  155.         hdr.Signature='LE';
  156.  
  157. #endif
  158.  
  159.         hdr.CPU_Type=i80386;
  160.  
  161.         hdr.Target_OS=1;
  162.  
  163.         hdr.Type_Flags=0x200;
  164.  
  165.         hdr.Number_Of_Memory_Pages=outptr/0x1000+1;
  166.  
  167.         hdr.Initial_CS=1;
  168.  
  169.         hdr.Initial_EI=EntryPoint();
  170.  
  171.         hdr.Initial_SS=2;
  172.  
  173.         hdr.Initial_ESP=stacksize;
  174.  
  175.         hdr.Memory_Page_Size=0x1000;
  176.  
  177.         hdr.Bytes_On_Last_Page=outptr%0x1000;
  178.  
  179.         hdr.Object_Table_Offset=sizeof(LE_Header);
  180.  
  181.         hdr.Object_Table_Entries=2;
  182.  
  183.         headerofs=ftell(hout);
  184.  
  185.         if(fwrite(&hdr,sizeof(LE_Header),1,hout)!=1){
  186.  
  187. errwrite:
  188.  
  189.                 ErrWrite();
  190.  
  191.                 fclose(hout);
  192.  
  193.                 hout=NULL;
  194.  
  195.                 return(-1);
  196.  
  197.         }
  198.  
  199.  
  200.  
  201.         ot[0].Virtual_Segment_Size=outptr+postsize;//hdr.Initial_ESP;
  202.  
  203.         ot[0].Relocation_Base_Address=0x10000;
  204.  
  205.         ot[0].ObjTableFlags=0x2045;
  206.  
  207.         ot[0].Page_MAP_Index=1;
  208.  
  209.         ot[0].Page_MAP_Entries=hdr.Number_Of_Memory_Pages;
  210.  
  211.         ot[1].Virtual_Segment_Size=hdr.Initial_ESP;
  212.  
  213.         ot[1].Relocation_Base_Address=Align(outptr+postsize,0x10000)+0x10000;
  214.  
  215.         ot[1].ObjTableFlags=0x2043;
  216.  
  217.         ot[1].Page_MAP_Index=2;
  218.  
  219.         ot[0].Reserved=ot[1].Reserved=ot[1].Page_MAP_Entries=0;
  220.  
  221.         if(fwrite(&ot,sizeof(Object_Table)*2,1,hout)!=1)goto errwrite;
  222.  
  223.         hdr.Object_Page_Map_Table_Offset=headsize+=sizeof(Object_Table)*2;
  224.  
  225.  
  226.  
  227.         sizeb=sizeof(Page_Map_Table)*hdr.Number_Of_Memory_Pages;
  228.  
  229.         pmt=(Page_Map_Table *)MALLOC(sizeb);
  230.  
  231.         for(i=0;i<hdr.Number_Of_Memory_Pages;i++){
  232.  
  233.                 (pmt+i)->High_Page_Number=swapshort(((i+1)/256));
  234.  
  235.                 (pmt+i)->Low_Page_Number=(unsigned char)((i+1)%256);
  236.  
  237.                 (pmt+i)->FLAGS=0;
  238.  
  239.         }
  240.  
  241.         if(fwrite(pmt,sizeb,1,hout)!=1)goto errwrite;
  242.  
  243.         free(pmt);
  244.  
  245.         hdr.Resource_Table_Offset=hdr.Resident_Names_Table_Offset=headsize+=sizeb;
  246.  
  247.  
  248.  
  249.         i=strlen(rawfilename);
  250.  
  251.         string2[0]=(unsigned char)i;
  252.  
  253.         strcpy((char *)&string2[1],rawfilename);
  254.  
  255.         *(long *)&string2[i+2]=0;
  256.  
  257.         if(fwrite(string2,i+5,1,hout)!=1)goto errwrite;
  258.  
  259.  
  260.  
  261.         hdr.Entry_Table_Offset=headsize+=i+4;
  262.  
  263.         headsize++;
  264.  
  265.         sizeb=sizeof(unsigned long)*(hdr.Number_Of_Memory_Pages+1);
  266.  
  267.         fpto=(unsigned long *)MALLOC(sizeb);
  268.  
  269.         hdr.Fixup_Page_Table_Offset=headsize;
  270.  
  271.         memset(fpto,0,sizeb);
  272.  
  273. //      for(i=0;i<(hdr.Number_Of_Memory_Pages+1);i++){
  274.  
  275. //              fpto[i]=0;
  276.  
  277. //      }
  278.  
  279.         if(fwrite(fpto,sizeb,1,hout)!=1)goto errwrite;
  280.  
  281.         headsize+=sizeb;
  282.  
  283.  
  284.  
  285. //Fixup_Record_Table
  286.  
  287. int sizefixpage=0;
  288.  
  289.         hdr.Fixup_Record_Table_Offset=headsize;
  290.  
  291.         for(i=0;i<hdr.Number_Of_Memory_Pages;i++){
  292.  
  293.                 unsigned int startblc=i*4096;   // ¤à¥á ¯¥à¢®£® ¡«®ª 
  294.  
  295.                 for(unsigned int j=0;j<posts;j++){      //®¡å®¤¨¬ ¢áî â ¡«¨æã post
  296.  
  297.                         if(((postbuf+j)->type==CALL_32I||
  298.  
  299.             ((postbuf+j)->type>=POST_VAR32&&(postbuf+j)->type<=FIX_CODE32))&&
  300.  
  301.                         (postbuf+j)->loc>=startblc&&(postbuf+j)->loc<(startblc+4096)){
  302.  
  303.                                 int sizerec;
  304.  
  305.                                 string2[0]=7;
  306.  
  307.                                 *(unsigned short *)&string2[2]=(unsigned short)((postbuf+j)->loc-startblc);
  308.  
  309.                                 string2[4]=1;
  310.  
  311.                                 unsigned int val=*(unsigned long *)&output[(postbuf+j)->loc];
  312.  
  313.                                 if(val<65536){
  314.  
  315.                                         string2[1]=0;
  316.  
  317.                                         *(unsigned short *)&string2[5]=(unsigned short)val;
  318.  
  319.                                         sizerec=7;
  320.  
  321.                                 }
  322.  
  323.                                 else{
  324.  
  325.                                         string2[1]=0x10;
  326.  
  327.                                         *(unsigned long *)&string2[5]=val;
  328.  
  329.                                         sizerec=9;
  330.  
  331.                                 }
  332.  
  333.                                 sizefixpage+=sizerec;
  334.  
  335.                                 if(fwrite(string2,sizerec,1,hout)!=1)goto errwrite;
  336.  
  337.                         }
  338.  
  339.                         fpto[i+1]=sizefixpage;
  340.  
  341.                 }
  342.  
  343.         }
  344.  
  345.         headsize+=sizefixpage;
  346.  
  347.  
  348.  
  349. //Imported_Procedure_Name_Table
  350.  
  351.         hdr.Imported_Module_Names_Table_Offset=
  352.  
  353.                         hdr.Imported_Procedure_Name_Table_Offset=headsize;
  354.  
  355.         string2[0]=0;
  356.  
  357.         if(fwrite(string2,1,1,hout)!=1)goto errwrite;
  358.  
  359.         headsize++;
  360.  
  361.  
  362.  
  363.  
  364.  
  365.         hdr.Fixup_Section_Size=headsize-hdr.Fixup_Page_Table_Offset;//9+sizefixpage;
  366.  
  367.         hdr.Loader_Section_Size=headsize-sizeof(LE_Header);
  368.  
  369.         hdr.Automatic_Data_Object=2;
  370.  
  371.         hdr.Data_Pages_Offset=Align(headsize+headerofs,16/*1024*/);
  372.  
  373.         ChSize(hdr.Data_Pages_Offset);
  374.  
  375.  
  376.  
  377.         fseek(hout,headerofs,SEEK_SET);
  378.  
  379.         if(fwrite(&hdr,sizeof(LE_Header),1,hout)!=1)goto errwrite;
  380.  
  381.         fseek(hout,headerofs+hdr.Fixup_Page_Table_Offset,SEEK_SET);
  382.  
  383.         if(fwrite(fpto,sizeof(unsigned long)*(hdr.Number_Of_Memory_Pages+1),1,hout)!=1)goto errwrite;
  384.  
  385.         free(fpto);
  386.  
  387.  
  388.  
  389.         fseek(hout,0,SEEK_END);
  390.  
  391.         return 0;
  392.  
  393. }
  394.  
  395.