Subversion Repositories Kolibri OS

Rev

Rev 7626 | Rev 9680 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7626 Rev 7700
Line 1... Line -...
1
#include "tok.h"
-
 
2
#include "coff.h"
-
 
3
 
-
 
4
#define _OUTPE_
-
 
5
 
-
 
6
/* -----------------------------------------------------------------------
-
 
7
 ‘®§¤ ­¨¥ PE ä®à¬ â 
-
 
8
 ------------------------------------------------------------------------ */
-
 
9
#define SIZESTUB 96
-
 
10
#define STRVERS 0x20	//ᬥ饭¨¥ ⥪áâ  á ­®¬¥à®¬ ¢¥àᨨ
-
 
11
 
-
 
12
char stub[]={0x4D,0x5A,0x50,0x00,0x02,0x00,0x00,0x00,
-
 
13
             0x04,0x00,0x0F,0x00,0xFF,0xFF,0x00,0x00,
-
 
14
						 0xB8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-
 
15
             0x40,0x00,0x1A,0x00,0x00,0x00,0x00,0x00,
-
 
16
 
-
 
17
						 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-
 
18
						 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-
 
19
             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-
 
20
						 0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,
-
 
21
 
-
 
22
						 0xBA,0x0E,0x00,0x0E,0x1F,0xB4,0x09,0xCD,
-
 
23
             0x21,0xB8,0x01,0x4C,0xCD,0x21,0x46,0x6F,
-
 
24
						 0x72,0x20,0x57,0x69,0x6E,0x33,0x32,0x20,
-
 
25
						 0x6F,0x6E,0x6C,0x79,0x21,0x0D,0x0A,0x24};
-
 
26
 
-
 
27
#define SIZESTUB2 12
-
 
28
char stub2[]={0x4D,0x5A,0x53,0x50,0x48,0x49,0x4E,0x58,
-
 
29
              0x20,0x43,0x2D,0x2D};
-
 
30
unsigned int numdll,numapi;
-
 
31
unsigned char FixUpTable=FALSE;	//§ ¯à¥â¨âì ᮧ¤ ­¨¥ â ¡«¨æ¨ Fix UP for Windows
-
 
32
unsigned char WinMonoBlock=TRUE;
-
 
33
int numexport=0;
-
 
34
unsigned long ImageBase=0x400000;
-
 
35
unsigned long vsizeheader=0x1000; //¢¨àâã «ì­ë© à §¬¥à § £®«®¢ª .
-
 
36
unsigned long FILEALIGN=0;//512;	// ¢ëà ¢­¨¢ ­¨¥ ᥪ権 ¢ ä ©«¥
-
 
37
int filingzerope;
-
 
38
 
-
 
39
struct listexport *lexport=NULL;
-
 
40
static unsigned long sizestub;
-
 
41
static unsigned long numrs=1;
-
 
42
 
-
 
43
unsigned long  Align(unsigned long size,unsigned long val)
-
 
44
{
-
 
45
	if(val<2)return size;
-
 
46
	val--;
-
 
47
	return (size+val)&(~val);
-
 
48
}
-
 
49
 
-
 
50
/*
-
 
51
#include 
-
 
52
 
-
 
53
void PrintMem(void *mem)
-
 
54
{
-
 
55
unsigned char *m,*n;
-
 
56
int i,j;
-
 
57
	m=(unsigned char *)mem;
-
 
58
	for(i=0;i<192;){
-
 
59
		n=m;
-
 
60
		for(j=0;j<16;j++,i++){
-
 
61
			printf("%02X ",*m);
-
 
62
			m++;
-
 
63
		}
-
 
64
		for(j=0;j<16;j++){
-
 
65
			char c=*n;
-
 
66
			n++;
-
 
67
			if(c<0x20||c>0x7e)c='.';
-
 
68
			putch(c);
-
 
69
		}
-
 
70
		puts("");
-
 
71
	}
-
 
72
}
-
 
73
 
-
 
74
void CheckMem()
-
 
75
{
-
 
76
	if(listdll!=NULL){	//¥áâì api-¯à®æ
-
 
77
DLLLIST *newdll=listdll;	//­ ç «® ᯨ᪠ DLL
-
 
78
APIPROC *listapi=newdll->list;
-
 
79
idrec *rec=listapi->recapi;
-
 
80
		PrintMem(rec);
-
 
81
	}
-
 
82
} */
-
 
83
 
-
 
84
void AddJmpApi()
-
 
85
{
-
 
86
//¯®¨áª api ¯à®æ¥¤ãà
-
 
87
//᪮à४â¨à®¢ âì  ¤à¥á  ¨§ â ¡«¨æë ¯¥à¥¬¥é¥­¨©
-
 
88
	alignersize+=AlignCD(CS,4);
-
 
89
	if(listdll!=NULL){	//¥áâì api-¯à®æ
-
 
90
		DLLLIST *newdll=listdll;	//­ ç «® ᯨ᪠ DLL
-
 
91
		numdll=numapi=0;
-
 
92
		for(APIPROC *listapi=newdll->list;;){
-
 
93
			unsigned short numapiprocdll=0;	//ç¨á«® ¨á¯®«ì§ã¥¬ëå ¢ ¡¨¡«¨®â¥ª¥ ¯à®æ¥¤ãà
-
 
94
			for(short i=0;inum;i++){	//¯à®¢¥à¨âì ¢á¥ ¯à®æ¥¤ãàë
-
 
95
				idrec *rec=(listapi+i)->recapi;
-
 
96
				unsigned int idnum=rec->recnumber;	//¨¤¥­â¨ä¨ª â®à ᯨ᪠
-
 
97
				char useapi=FALSE;	//ä« £ ¨á¯®«ì§®¢ ­¨ï
-
 
98
				for(unsigned int j=0;j
-
 
99
					if((postbuf+j)->num==idnum){
-
 
100
						if((postbuf+j)->type==CALL_32){	//­ è«¨
-
 
101
							useapi=API_JMP;	//ä« £ ¢§¢¥¤¥¬
-
 
102
							unsigned long hold=outptr-((postbuf+j)->loc+4);	//à áâ®ï­¨¥ ¤® ¢ë§®¢ 
-
 
103
 							*(long *)&output[(postbuf+j)->loc]=hold;	//¨á¯à ¢¨âì
-
 
104
						}
-
 
105
						else if((postbuf+j)->type==CALL_32I){
-
 
106
							useapi=API_FAST;	//ä« £ ¢§¢¥¤¥¬
-
 
107
							numrel++;
-
 
108
						}
-
 
109
					}
-
 
110
				}
-
 
111
				if(useapi==API_JMP){	// ¯à®æ¥¤ãà  ¢ë§ë¢ « áì
-
 
112
					*(short *)&output[outptr]=0x25ff;	//£¥­¥à æ¨ï JMP
-
 
113
					outptr+=2;
-
 
114
					rec->recnumber=outptr;	//⥯¥àì ¢¬¥áâ® ¨¤¥­â. â®çª  ¢å®¤ 
-
 
115
					AddReloc(CS);	//¤®¡ ¢¨âì ¥¥ ¢ â ¡« ¯¥à¥¬¥é¥­¨©
-
 
116
					*(long *)&output[outptr]=0;	// ¤à¥á ¢ë§®¢ 
-
 
117
					outptr+=4;
-
 
118
					numapi++;	//®¡é¥¥ ç¨á«® ¨á¯®«ì§®¢ ­ëå api-¯à®æ¥¤ãà
-
 
119
					numapiprocdll++;	//ç¨á«® ¨á¯®«ì§®¢ ­ëå ¯à®æ¥¤ãà ¢ í⮩ DLL
-
 
120
				}
-
 
121
				else if(useapi==API_FAST){
-
 
122
					numapi++;	//®¡é¥¥ ç¨á«® ¨á¯®«ì§®¢ ­ëå api-¯à®æ¥¤ãà
-
 
123
					numapiprocdll++;	//ç¨á«® ¨á¯®«ì§®¢ ­ëå ¯à®æ¥¤ãà ¢ í⮩ DLL
-
 
124
				}
-
 
125
				rec->recrm=useapi;	//⨯ ¢ë§®¢  api; 0 - not used
-
 
126
				if(rec->newid!=NULL){
-
 
127
					free(rec->newid);	//ᯨ᮪ ¯ à ¬¥â஢ ¡®«ìè¥ ­¥ ­ã¦¥­
-
 
128
					rec->newid=NULL;
-
 
129
				}
-
 
130
			}
-
 
131
			newdll->num=numapiprocdll;	//¨á¯à ¢¨âì ç¨á«® ॠ«ì­® ¨á¯®«ì§ ¯à®æ¥¤ãà
-
 
132
			if(numapiprocdll==0){	//¢ í⮩ ¡¨¡«¨®â¥ª¥ ­¥ ¨á¯®«ì§ ­¨ ®¤¨­ ¢ë§®¢
-
 
133
				free(newdll->list);	//㤠«¨âì ᯨ᮪ ¯à®æ¥¤ãà.
-
 
134
			}
-
 
135
			else numdll++;
-
 
136
			if(newdll->next==NULL)break;	//ª®­¥æ ᯨ᪠
-
 
137
			newdll=newdll->next;
-
 
138
			listapi=newdll->list;
-
 
139
		}
-
 
140
	}
-
 
141
	outptrdata=outptr;
-
 
142
}
-
 
143
 
-
 
144
int MakePE()
-
 
145
{
-
 
146
unsigned short numobj=1;
-
 
147
char *importblock=NULL;
-
 
148
char *relocblock=NULL;
-
 
149
char *exportblock=NULL;
-
 
150
unsigned long psize = 0,vsize=0,sizereloc=0,sizeReloc=0,sizeImport=0,sizeExport=0,
-
 
151
              sizebss=0,sizeRes=0;
-
 
152
unsigned long startsec=0,startsecr=0,startsece=0,startsecres=0;
-
 
153
unsigned int posrel=0,sizeimport=0,startimportname=0,sizeexport=0,sizeres=0,
-
 
154
             startexportname=0;
-
 
155
unsigned int sizehead;	//à §¬¥à § £®«®¢ª 
-
 
156
unsigned int exportnum=0; //­®¬¥à ᥪ樨 íªá¯®àâ 
-
 
157
unsigned int relocnum=0;	//­®¬¥à ᥪ樨 ¯¥à¥¬¥é¥­¨©
-
 
158
unsigned int importnum=0;	//­®¬¥à ᥪ樨 ¨¬¯®àâ 
-
 
159
unsigned int codenum=0;	//­®¬¥à ᥪ樨 ª®¤ 
-
 
160
unsigned int resnum=0;	//­®¬¥à ᥪ樨 à¥áãàᮢ
-
 
161
	if(hout==NULL)return -1;
-
 
162
	if(WinMonoBlock==FALSE){
-
 
163
		vsize=Align(outptr+(wbss==FALSE?postsize:0),OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à ᥪ樨 ª®¤ 
-
 
164
		psize=Align(outptr,FILEALIGN);	//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 ª®¤ 
-
 
165
	}
-
 
166
	else vsize=outptr;
-
 
167
//	sizehead=((postsize&&wbss)?2:1)*sizeof(OBJECT_ENTRY);
-
 
168
	sizehead=numrs*sizeof(OBJECT_ENTRY);
-
 
169
	OBJECT_ENTRY *objentry=(OBJECT_ENTRY *)MALLOC(sizehead);//â ¢«¨æ  ®¡ê¥ªâ®¢
-
 
170
	memset(objentry,0,sizehead);//®ç¨áâ¨âì â ¡«¨æã ®¡ê¥ªâ®¢
-
 
171
//ᥪæ¨ï .bss
-
 
172
	if(wbss){	//¥áâì post ¯¥à¥¬¥­­ë¥
-
 
173
		numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢
-
 
174
		codenum=1;	//­®¬¥à ᥪ樨 ª®¤ 
-
 
175
		strcpy(objentry->name,".bss"); //¨¬ï ᥪ樨
-
 
176
		objentry->vsize=sizebss=Align(postsize,OBJECTALIGN);
-
 
177
		objentry->pOffset=objentry->psize=0;
-
 
178
		objentry->flags=0xC0000080;
-
 
179
		objentry->sectionRVA=vsizeheader;
-
 
180
	}
-
 
181
	strcpy((objentry+codenum)->name,"CODE");	//¥¥ ¨¬ï
-
 
182
	(objentry+codenum)->vsize=vsize;          //à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
183
	(objentry+codenum)->psize=psize;          //à §¬¥à ᥪ樨 ¢ ä ©«¥
-
 
184
	(objentry+codenum)->flags=0xe0000060;     //ä« £ ᥪ樨
-
 
185
	(objentry+codenum)->sectionRVA=vsizeheader+sizebss;//¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
186
//ᥪæ¨ï ¨¬¯®àâ 
-
 
187
	if(numapi!=0){	//¥áâì ¢ë§®¢ë api-¯à®æ¥¤ãà ᮧ¤ ᥪæ¨î ¨¬¯®àâ 
-
 
188
		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
189
			importnum=numobj;
-
 
190
			numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢
-
 
191
		}
-
 
192
		startsec=vsizeheader+vsize+sizebss;	//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
193
			//ॠ«ì­ë© à §¬¥à ᥪ樨
-
 
194
		startimportname=(numdll+1)*20+(numapi+numdll)*(shortimport==0?8:4);
-
 
195
		sizeimport=Align(startimportname,FILEALIGN);	//à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
196
		importblock=(char *)MALLOC(sizeimport);	//¯ ¬ïâì ¯®¤ ­¥¥
-
 
197
		memset(importblock,0,sizeimport);	//®ç¨áâ¨âì ¥¥
-
 
198
		DLLLIST *newdll=listdll;	//â ¡«¨çª  dll á ¨¬¯®àâ¨à㥬묨 ¯à®æ¥¤ãà ¬¨
-
 
199
		unsigned long sn,sn1;
-
 
200
		sn1=sn=(numdll+1)*20;
-
 
201
		for(int i=0;;i++){
-
 
202
			while(newdll->num==0)if((newdll=newdll->next)==NULL)break;//¯à®¯ã᪠­¥¨á¯®«ì§ã¥¬ëå
-
 
203
			if(newdll==NULL)break;	//§ ¢¥àè¨âì 横« ¥á«¨ ᯨ᮪ dll ¯ãáâ
-
 
204
			APIPROC *listapi=newdll->list;	//â ¡«¨çª  ¯à®æ¥¤ãà ¨§ ⥪ã饩 dll
-
 
205
			*(long *)&importblock[i*20+12]=startsec+startimportname;
-
 
206
			*(long *)&importblock[i*20]=(shortimport==0?startsec+sn:0);
-
 
207
			*(long *)&importblock[i*20+16]=startsec+sn+(shortimport==0?(numdll+numapi)*4:0);
-
 
208
			sn+=(newdll->num+1)*4;
-
 
209
			unsigned int lenn=strlen(newdll->name)+1;
-
 
210
			if((lenn+startimportname+1)>=sizeimport){
-
 
211
				sizeimport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨
-
 
212
				importblock=(char *)REALLOC(importblock,sizeimport);
-
 
213
				memset(importblock+sizeimport-FILEALIGN,0,FILEALIGN);
-
 
214
			}
-
 
215
			strcpy(&importblock[startimportname],newdll->name);
-
 
216
			startimportname+=lenn;
-
 
217
			for(int n=0,t=0;nnum;n++,t++){
-
 
218
				while((listapi+t)->recapi->recrm==0)t++;
-
 
219
				idrec *rec=(listapi+t)->recapi;
-
 
220
				unsigned long newadr;
-
 
221
				newadr=ImageBase+startsec+sn1+(shortimport==0?(numdll+numapi)*4:0);
-
 
222
				if(rec->recrm==API_JMP)*(long *)&output[rec->recnumber]=newadr;
-
 
223
				else{
-
 
224
					for(unsigned int j=0;j
-
 
225
						if((postbuf+j)->num==(unsigned long)rec->recnumber&&(postbuf+j)->type==CALL_32I){
-
 
226
 							*(long *)&output[(postbuf+j)->loc]=newadr;	//¨á¯à ¢¨âì
-
 
227
						}
-
 
228
					}
-
 
229
				}
-
 
230
				if(useordinal&&rec->recsib!=-1){
-
 
231
					*(long *)&importblock[sn1]=rec->recsib|0x80000000;
-
 
232
					if(shortimport==0)*(long *)&importblock[sn1+(numdll+numapi)*4]=rec->recsib|0x80000000;
-
 
233
				}
-
 
234
				else{
-
 
235
					if((startimportname%2)==1)importblock[startimportname++]=0;
-
 
236
					*(long *)&importblock[sn1]=startsec+startimportname;
-
 
237
					if(shortimport==0)*(long *)&importblock[sn1+(numdll+numapi)*4]=startsec+startimportname;
-
 
238
					if(rec->recsize!=-1)sprintf((char *)string2,"%s@%u",rec->recid,rec->recsize);
-
 
239
					else strcpy((char *)string2,rec->recid);
-
 
240
					lenn=strlen((char *)string2)+1;
-
 
241
					if((lenn+startimportname+4)>=sizeimport){
-
 
242
						sizeimport+=FILEALIGN;
-
 
243
						importblock=(char *)REALLOC(importblock,sizeimport);
-
 
244
						memset(importblock+sizeimport-FILEALIGN,0,FILEALIGN);
-
 
245
					}
-
 
246
					*(short *)&importblock[startimportname]=0;
-
 
247
					startimportname+=2;
-
 
248
					strcpy(&importblock[startimportname],(char *)string2);
-
 
249
					startimportname+=lenn;
-
 
250
				}
-
 
251
				sn1+=4;
-
 
252
			}
-
 
253
			if(newdll->next==NULL)break;
-
 
254
			newdll=newdll->next;
-
 
255
			sn1+=4;
-
 
256
		}
-
 
257
		importblock[startimportname++]=0;
-
 
258
 
-
 
259
		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
260
			strcpy((objentry+importnum)->name,".idata"); //¨¬ï ᥪ樨
-
 
261
			(objentry+importnum)->vsize=sizeImport=Align(sizeimport,OBJECTALIGN);
-
 
262
			(objentry+importnum)->psize=sizeimport;
-
 
263
			(objentry+importnum)->flags=0xC0000040;
-
 
264
			(objentry+importnum)->sectionRVA=startsec;
-
 
265
		}
-
 
266
		else sizeImport=sizeimport=Align(startimportname,4);
-
 
267
	}
-
 
268
//ᥪæ¨ï íªá¯®àâ 
-
 
269
	if(numexport!=0){
-
 
270
		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
271
			exportnum=numobj;
-
 
272
			numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢
-
 
273
		}
-
 
274
		startsece=vsizeheader+vsize+sizeImport+sizebss;//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
275
		startexportname=sizeof(EXPORT_TABLE)+numexport*10;//ॠ«ì­ë© à §¬¥à ᥪ樨
-
 
276
		sizeexport=Align(startexportname,FILEALIGN);	//à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
277
		exportblock=(char *)MALLOC(sizeexport);	//¯ ¬ïâì ¯®¤ ­¥¥
-
 
278
		memset(exportblock,0,sizeexport);	//®ç¨áâ¨âì ¥¥
-
 
279
		*(long *)&exportblock[12]=startsece+startexportname;// ¤à¥á ¨¬¥­¨ ä ©« 
-
 
280
		*(long *)&exportblock[16]=1;	//Ordinal Base
-
 
281
		*(long *)&exportblock[20]=numexport;	//Num of Functions
-
 
282
		*(long *)&exportblock[24]=numexport;	//Num of Name Pointer
-
 
283
		*(long *)&exportblock[28]=startsece+sizeof(EXPORT_TABLE);//Address Table RVA
-
 
284
		*(long *)&exportblock[32]=startsece+sizeof(EXPORT_TABLE)+numexport*4;//Name Pointers RVA
-
 
285
		*(long *)&exportblock[36]=startsece+sizeof(EXPORT_TABLE)+numexport*8;//Ordinal Table RVA
-
 
286
		char *oname;
-
 
287
		strcpy((char *)string2,(char *)rawfilename);
-
 
288
		oname=strrchr((char *)string2,'\\');
-
 
289
		if(oname==NULL)oname=(char *)string2;
-
 
290
		else oname=oname+1;
-
 
291
		sprintf((char *)string,"%s.%s",oname,outext);
-
 
292
		unsigned int lenn=strlen((char *)string)+1;
-
 
293
		if((lenn+startexportname+1)>=sizeexport){
-
 
294
			sizeexport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨
-
 
295
			exportblock=(char *)REALLOC(exportblock,sizeexport);
-
 
296
			memset(exportblock+sizeexport-FILEALIGN,0,FILEALIGN);
-
 
297
		}
-
 
298
		strcpy(&exportblock[startexportname],(char *)string);
-
 
299
		startexportname+=lenn;
-
 
300
		for(int i=0;i
-
 
301
			*(long *)&exportblock[sizeof(EXPORT_TABLE)+i*4]=(lexport+i)->address+vsizeheader+sizebss;	// ¤à¥á  ä㭪権
-
 
302
			*(long *)&exportblock[sizeof(EXPORT_TABLE)+(numexport+i)*4]=startsece+startexportname;	// ¤à¥á  ¨¬¥­
-
 
303
			*(short *)&exportblock[sizeof(EXPORT_TABLE)+numexport*8+i*2]=(short)i;	//®à¤¨­ «ë ¨¬¥­
-
 
304
			lenn=strlen((lexport+i)->name)+1;
-
 
305
			if((lenn+startexportname+1)>=sizeexport){
-
 
306
				sizeexport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨
-
 
307
				exportblock=(char *)REALLOC(exportblock,sizeexport);
-
 
308
				memset(exportblock+sizeexport-FILEALIGN,0,FILEALIGN);
-
 
309
			}
-
 
310
			strcpy(&exportblock[startexportname],(lexport+i)->name);
-
 
311
			startexportname+=lenn;
-
 
312
		}
-
 
313
		free(lexport);//®á¢®¡®¤¨¬ 㦥 ­¥ ­ã¦­ë© ¡«®ª
-
 
314
		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
315
			strcpy((objentry+exportnum)->name,".edata"); //¨¬ï ᥪ樨
-
 
316
			(objentry+exportnum)->vsize=sizeExport=Align(sizeexport,OBJECTALIGN);
-
 
317
			(objentry+exportnum)->psize=sizeexport;
-
 
318
			(objentry+exportnum)->flags=0x40000040;
-
 
319
			(objentry+exportnum)->sectionRVA=startsece;
-
 
320
		}
-
 
321
		else sizeexport=sizeExport=Align(startexportname,4);
-
 
322
	}
-
 
323
 
-
 
324
	if(numres){	//ᥪæ¨ï à¥áãàᮢ
-
 
325
		if(WinMonoBlock==FALSE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
326
			resnum=numobj;
-
 
327
			numobj++;                     //㢥«¨ç¨âì ç¨á«® ®¡ê¥ªâ®¢
1
#include "tok.h"

#include "coff.h"



#define _OUTPE_



/* -----------------------------------------------------------------------

 ‘®§¤ ­¨¥ PE ä®à¬ â 

 ------------------------------------------------------------------------ */

#define SIZESTUB 96

#define STRVERS 0x20	//ᬥ饭¨¥ ⥪áâ  á ­®¬¥à®¬ ¢¥àᨨ



unsigned char stub[]={0x4D,0x5A,0x50,0x00,0x02,0x00,0x00,0x00,

             0x04,0x00,0x0F,0x00,0xFF,0xFF,0x00,0x00,

						 0xB8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

             0x40,0x00,0x1A,0x00,0x00,0x00,0x00,0x00,



						 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

						 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

						 0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,



						 0xBA,0x0E,0x00,0x0E,0x1F,0xB4,0x09,0xCD,

             0x21,0xB8,0x01,0x4C,0xCD,0x21,0x46,0x6F,

						 0x72,0x20,0x57,0x69,0x6E,0x33,0x32,0x20,

						 0x6F,0x6E,0x6C,0x79,0x21,0x0D,0x0A,0x24};



#define SIZESTUB2 12

char stub2[]={0x4D,0x5A,0x53,0x50,0x48,0x49,0x4E,0x58,

              0x20,0x43,0x2D,0x2D};

unsigned int numdll,numapi;

unsigned char FixUpTable=FALSE;	//§ ¯à¥â¨âì ᮧ¤ ­¨¥ â ¡«¨æ¨ Fix UP for Windows

unsigned char WinMonoBlock=TRUE;

int numexport=0;

unsigned long ImageBase=0x400000;

unsigned long vsizeheader=0x1000; //¢¨àâã «ì­ë© à §¬¥à § £®«®¢ª .

unsigned long FILEALIGN=0;//512;	// ¢ëà ¢­¨¢ ­¨¥ ᥪ権 ¢ ä ©«¥

int filingzerope;



struct listexport *lexport=NULL;

static unsigned long sizestub;

static unsigned long numrs=1;



unsigned long  Align(unsigned long size,unsigned long val)

{

	if(val<2)return size;

	val--;

	return (size+val)&(~val);

}



/*

#include 



void PrintMem(void *mem)

{

unsigned char *m,*n;

int i,j;

	m=(unsigned char *)mem;

	for(i=0;i<192;){

		n=m;

		for(j=0;j<16;j++,i++){

			printf("%02X ",*m);

			m++;

		}

		for(j=0;j<16;j++){

			char c=*n;

			n++;

			if(c<0x20||c>0x7e)c='.';

			putch(c);

		}

		puts("");

	}

}



void CheckMem()

{

	if(listdll!=NULL){	//¥áâì api-¯à®æ

DLLLIST *newdll=listdll;	//­ ç «® ᯨ᪠ DLL

APIPROC *listapi=newdll->list;

idrec *rec=listapi->recapi;

		PrintMem(rec);

	}

} */



void AddJmpApi()

{

//¯®¨áª api ¯à®æ¥¤ãà

//᪮à४â¨à®¢ âì  ¤à¥á  ¨§ â ¡«¨æë ¯¥à¥¬¥é¥­¨©

	alignersize+=AlignCD(CS,4);

	if(listdll!=NULL){	//¥áâì api-¯à®æ

		DLLLIST *newdll=listdll;	//­ ç «® ᯨ᪠ DLL

		numdll=numapi=0;

		for(APIPROC *listapi=newdll->list;;){

			unsigned short numapiprocdll=0;	//ç¨á«® ¨á¯®«ì§ã¥¬ëå ¢ ¡¨¡«¨®â¥ª¥ ¯à®æ¥¤ãà

			for(short i=0;inum;i++){	//¯à®¢¥à¨âì ¢á¥ ¯à®æ¥¤ãàë

				idrec *rec=(listapi+i)->recapi;

				unsigned int idnum=rec->recnumber;	//¨¤¥­â¨ä¨ª â®à ᯨ᪠

				char useapi=FALSE;	//ä« £ ¨á¯®«ì§®¢ ­¨ï

				for(unsigned int j=0;jnum==idnum){

						if((postbuf+j)->type==CALL_32){	//­ è«¨

							useapi=API_JMP;	//ä« £ ¢§¢¥¤¥¬

							unsigned long hold=outptr-((postbuf+j)->loc+4);	//à áâ®ï­¨¥ ¤® ¢ë§®¢ 

 							*(long *)&output[(postbuf+j)->loc]=hold;	//¨á¯à ¢¨âì

						}

						else if((postbuf+j)->type==CALL_32I){

							useapi=API_FAST;	//ä« £ ¢§¢¥¤¥¬

							numrel++;

						}

					}

				}

				if(useapi==API_JMP){	// ¯à®æ¥¤ãà  ¢ë§ë¢ « áì

					*(short *)&output[outptr]=0x25ff;	//£¥­¥à æ¨ï JMP

					outptr+=2;

					rec->recnumber=outptr;	//⥯¥àì ¢¬¥áâ® ¨¤¥­â. â®çª  ¢å®¤ 

					AddReloc(CS);	//¤®¡ ¢¨âì ¥¥ ¢ â ¡« ¯¥à¥¬¥é¥­¨©

					*(long *)&output[outptr]=0;	// ¤à¥á ¢ë§®¢ 

					outptr+=4;

					numapi++;	//®¡é¥¥ ç¨á«® ¨á¯®«ì§®¢ ­ëå api-¯à®æ¥¤ãà

					numapiprocdll++;	//ç¨á«® ¨á¯®«ì§®¢ ­ëå ¯à®æ¥¤ãà ¢ í⮩ DLL

				}

				else if(useapi==API_FAST){

					numapi++;	//®¡é¥¥ ç¨á«® ¨á¯®«ì§®¢ ­ëå api-¯à®æ¥¤ãà

					numapiprocdll++;	//ç¨á«® ¨á¯®«ì§®¢ ­ëå ¯à®æ¥¤ãà ¢ í⮩ DLL

				}

				rec->recrm=useapi;	//⨯ ¢ë§®¢  api; 0 - not used

				if(rec->newid!=NULL){

					free(rec->newid);	//ᯨ᮪ ¯ à ¬¥â஢ ¡®«ìè¥ ­¥ ­ã¦¥­

					rec->newid=NULL;

				}

			}

			newdll->num=numapiprocdll;	//¨á¯à ¢¨âì ç¨á«® ॠ«ì­® ¨á¯®«ì§ ¯à®æ¥¤ãà

			if(numapiprocdll==0){	//¢ í⮩ ¡¨¡«¨®â¥ª¥ ­¥ ¨á¯®«ì§ ­¨ ®¤¨­ ¢ë§®¢

				free(newdll->list);	//㤠«¨âì ᯨ᮪ ¯à®æ¥¤ãà.

			}

			else numdll++;

			if(newdll->next==NULL)break;	//ª®­¥æ ᯨ᪠

			newdll=newdll->next;

			listapi=newdll->list;

		}

	}

	outptrdata=outptr;

}



int MakePE()

{

unsigned short numobj=1;

char *importblock=NULL;

char *relocblock=NULL;

char *exportblock=NULL;

unsigned long psize=0,vsize=0,sizereloc=0,sizeReloc=0,sizeImport=0,sizeExport=0,

              sizebss=0,sizeRes=0;

unsigned long startsec=0,startsecr=0,startsece=0,startsecres=0;

unsigned int posrel=0,sizeimport=0,startimportname=0,sizeexport=0,sizeres=0,

             startexportname=0;

unsigned int sizehead;	//à §¬¥à § £®«®¢ª 

unsigned int exportnum=0; //­®¬¥à ᥪ樨 íªá¯®àâ 

unsigned int relocnum=0;	//­®¬¥à ᥪ樨 ¯¥à¥¬¥é¥­¨©

unsigned int importnum=0;	//­®¬¥à ᥪ樨 ¨¬¯®àâ 

unsigned int codenum=0;	//­®¬¥à ᥪ樨 ª®¤ 

unsigned int resnum=0;	//­®¬¥à ᥪ樨 à¥áãàᮢ

	if(hout==NULL)return -1;

	if(WinMonoBlock==FALSE){

		vsize=Align(outptr+(wbss==FALSE?postsize:0),OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à ᥪ樨 ª®¤ 

		psize=Align(outptr,FILEALIGN);	//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 ª®¤ 

	}

	else vsize=outptr;

//	sizehead=((postsize&&wbss)?2:1)*sizeof(OBJECT_ENTRY);

	sizehead=numrs*sizeof(OBJECT_ENTRY);

	OBJECT_ENTRY *objentry=(OBJECT_ENTRY *)MALLOC(sizehead);//â ¢«¨æ  ®¡ê¥ªâ®¢

	memset(objentry,0,sizehead);//®ç¨áâ¨âì â ¡«¨æã ®¡ê¥ªâ®¢

//ᥪæ¨ï .bss

	if(wbss){	//¥áâì post ¯¥à¥¬¥­­ë¥

		numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢

		codenum=1;	//­®¬¥à ᥪ樨 ª®¤ 

		strcpy(objentry->name,".bss"); //¨¬ï ᥪ樨

		objentry->vsize=sizebss=Align(postsize,OBJECTALIGN);

		objentry->pOffset=objentry->psize=0;

		objentry->flags=0xC0000080;

		objentry->sectionRVA=vsizeheader;

	}

	strcpy((objentry+codenum)->name,"CODE");	//¥¥ ¨¬ï

	(objentry+codenum)->vsize=vsize;          //à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨

	(objentry+codenum)->psize=psize;          //à §¬¥à ᥪ樨 ¢ ä ©«¥

	(objentry+codenum)->flags=0xe0000060;     //ä« £ ᥪ樨

	(objentry+codenum)->sectionRVA=vsizeheader+sizebss;//¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨

//ᥪæ¨ï ¨¬¯®àâ 

	if(numapi!=0){	//¥áâì ¢ë§®¢ë api-¯à®æ¥¤ãà ᮧ¤ ᥪæ¨î ¨¬¯®àâ 

		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			importnum=numobj;

			numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢

		}

		startsec=vsizeheader+vsize+sizebss;	//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨

			//ॠ«ì­ë© à §¬¥à ᥪ樨

		startimportname=(numdll+1)*20+(numapi+numdll)*(shortimport==0?8:4);

		sizeimport=Align(startimportname,FILEALIGN);	//à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨

		importblock=(char *)MALLOC(sizeimport);	//¯ ¬ïâì ¯®¤ ­¥¥

		memset(importblock,0,sizeimport);	//®ç¨áâ¨âì ¥¥

		DLLLIST *newdll=listdll;	//â ¡«¨çª  dll á ¨¬¯®àâ¨à㥬묨 ¯à®æ¥¤ãà ¬¨

		unsigned long sn,sn1;

		sn1=sn=(numdll+1)*20;

		for(int i=0;;i++){

			while(newdll->num==0)if((newdll=newdll->next)==NULL)break;//¯à®¯ã᪠­¥¨á¯®«ì§ã¥¬ëå

			if(newdll==NULL)break;	//§ ¢¥àè¨âì 横« ¥á«¨ ᯨ᮪ dll ¯ãáâ

			APIPROC *listapi=newdll->list;	//â ¡«¨çª  ¯à®æ¥¤ãà ¨§ ⥪ã饩 dll

			*(long *)&importblock[i*20+12]=startsec+startimportname;

			*(long *)&importblock[i*20]=(shortimport==0?startsec+sn:0);

			*(long *)&importblock[i*20+16]=startsec+sn+(shortimport==0?(numdll+numapi)*4:0);

			sn+=(newdll->num+1)*4;

			unsigned int lenn=strlen(newdll->name)+1;

			if((lenn+startimportname+1)>=sizeimport){

				sizeimport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨

				importblock=(char *)REALLOC(importblock,sizeimport);

				memset(importblock+sizeimport-FILEALIGN,0,FILEALIGN);

			}

			strcpy(&importblock[startimportname],newdll->name);

			startimportname+=lenn;

			for(int n=0,t=0;nnum;n++,t++){

				while((listapi+t)->recapi->recrm==0)t++;

				idrec *rec=(listapi+t)->recapi;

				unsigned long newadr;

				newadr=ImageBase+startsec+sn1+(shortimport==0?(numdll+numapi)*4:0);

				if(rec->recrm==API_JMP)*(long *)&output[rec->recnumber]=newadr;

				else{

					for(unsigned int j=0;jnum==(unsigned long)rec->recnumber&&(postbuf+j)->type==CALL_32I){

 							*(long *)&output[(postbuf+j)->loc]=newadr;	//¨á¯à ¢¨âì

						}

					}

				}

				if(useordinal&&rec->recsib!=-1){

					*(long *)&importblock[sn1]=rec->recsib|0x80000000;

					if(shortimport==0)*(long *)&importblock[sn1+(numdll+numapi)*4]=rec->recsib|0x80000000;

				}

				else{

					if((startimportname%2)==1)importblock[startimportname++]=0;

					*(long *)&importblock[sn1]=startsec+startimportname;

					if(shortimport==0)*(long *)&importblock[sn1+(numdll+numapi)*4]=startsec+startimportname;

					if(rec->recsize!=-1)sprintf((char *)string2,"%s@%u",rec->recid,rec->recsize);

					else strcpy((char *)string2,rec->recid);

					lenn=strlen((char *)string2)+1;

					if((lenn+startimportname+4)>=sizeimport){

						sizeimport+=FILEALIGN;

						importblock=(char *)REALLOC(importblock,sizeimport);

						memset(importblock+sizeimport-FILEALIGN,0,FILEALIGN);

					}

					*(short *)&importblock[startimportname]=0;

					startimportname+=2;

					strcpy(&importblock[startimportname],(char *)string2);

					startimportname+=lenn;

				}

				sn1+=4;

			}

			if(newdll->next==NULL)break;

			newdll=newdll->next;

			sn1+=4;

		}

		importblock[startimportname++]=0;



		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			strcpy((objentry+importnum)->name,".idata"); //¨¬ï ᥪ樨

			(objentry+importnum)->vsize=sizeImport=Align(sizeimport,OBJECTALIGN);

			(objentry+importnum)->psize=sizeimport;

			(objentry+importnum)->flags=0xC0000040;

			(objentry+importnum)->sectionRVA=startsec;

		}

		else sizeImport=sizeimport=Align(startimportname,4);

	}

//ᥪæ¨ï íªá¯®àâ 

	if(numexport!=0){

		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			exportnum=numobj;

			numobj++;     //㢥«¨ç¨¢ ¥¬ ç¨á«® ®¡ê¥ªâ®¢

		}

		startsece=vsizeheader+vsize+sizeImport+sizebss;//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨

		startexportname=sizeof(EXPORT_TABLE)+numexport*10;//ॠ«ì­ë© à §¬¥à ᥪ樨

		sizeexport=Align(startexportname,FILEALIGN);	//à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨

		exportblock=(char *)MALLOC(sizeexport);	//¯ ¬ïâì ¯®¤ ­¥¥

		memset(exportblock,0,sizeexport);	//®ç¨áâ¨âì ¥¥

		*(long *)&exportblock[12]=startsece+startexportname;// ¤à¥á ¨¬¥­¨ ä ©« 

		*(long *)&exportblock[16]=1;	//Ordinal Base

		*(long *)&exportblock[20]=numexport;	//Num of Functions

		*(long *)&exportblock[24]=numexport;	//Num of Name Pointer

		*(long *)&exportblock[28]=startsece+sizeof(EXPORT_TABLE);//Address Table RVA

		*(long *)&exportblock[32]=startsece+sizeof(EXPORT_TABLE)+numexport*4;//Name Pointers RVA

		*(long *)&exportblock[36]=startsece+sizeof(EXPORT_TABLE)+numexport*8;//Ordinal Table RVA

		char *oname;

		strcpy((char *)string2,(char *)rawfilename);

		oname=strrchr((char *)string2,'\\');

		if(oname==NULL)oname=(char *)string2;

		else oname=oname+1;

		sprintf((char *)string,"%s.%s",oname,outext);

		unsigned int lenn=strlen((char *)string)+1;

		if((lenn+startexportname+1)>=sizeexport){

			sizeexport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨

			exportblock=(char *)REALLOC(exportblock,sizeexport);

			memset(exportblock+sizeexport-FILEALIGN,0,FILEALIGN);

		}

		strcpy(&exportblock[startexportname],(char *)string);

		startexportname+=lenn;

		for(int i=0;iaddress+vsizeheader+sizebss;	// ¤à¥á  ä㭪権

			*(long *)&exportblock[sizeof(EXPORT_TABLE)+(numexport+i)*4]=startsece+startexportname;	// ¤à¥á  ¨¬¥­

			*(short *)&exportblock[sizeof(EXPORT_TABLE)+numexport*8+i*2]=(short)i;	//®à¤¨­ «ë ¨¬¥­

			lenn=strlen((lexport+i)->name)+1;

			if((lenn+startexportname+1)>=sizeexport){

				sizeexport+=FILEALIGN;	//㢥«¨ç¨âì à §¬¥à ᥪ樨

				exportblock=(char *)REALLOC(exportblock,sizeexport);

				memset(exportblock+sizeexport-FILEALIGN,0,FILEALIGN);

			}

			strcpy(&exportblock[startexportname],(lexport+i)->name);

			startexportname+=lenn;

		}

		free(lexport);//®á¢®¡®¤¨¬ 㦥 ­¥ ­ã¦­ë© ¡«®ª

		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			strcpy((objentry+exportnum)->name,".edata"); //¨¬ï ᥪ樨

			(objentry+exportnum)->vsize=sizeExport=Align(sizeexport,OBJECTALIGN);

			(objentry+exportnum)->psize=sizeexport;

			(objentry+exportnum)->flags=0x40000040;

			(objentry+exportnum)->sectionRVA=startsece;

		}

		else sizeexport=sizeExport=Align(startexportname,4);

	}



	if(numres){	//ᥪæ¨ï à¥áãàᮢ

		if(WinMonoBlock==FALSE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			resnum=numobj;

			numobj++;                     //㢥«¨ç¨âì ç¨á«® ®¡ê¥ªâ®¢

		}

		startsecres=vsizeheader+vsize+sizeImport+sizebss+sizeExport;//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨

		LISTRELOC *resrel;

		if(MakeRes(startsecres,&resrel))free(resrel);

		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

			strcpy((objentry+resnum)->name,".rsrc"); //¨¬ï ᥪ樨

			(objentry+resnum)->vsize=sizeRes=Align(curposbuf,OBJECTALIGN);

			(objentry+resnum)->psize=sizeres=Align(curposbuf,FILEALIGN);

			(objentry+resnum)->flags=0x40000040;

			(objentry+resnum)->sectionRVA=startsecres;

		}

		else sizeres=Align(curposbuf,4);

	}



//ᥪæ¨ï â ¡«¨æ ¯¥à¥¬¥é¥­¨ï

	if((FixUpTable==TRUE&&numrel!=0)/*||numexport!=0*/){//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï

		if(WinMonoBlock==FALSE||dllflag==TRUE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª ¨ íâ® DLL

			relocnum=numobj;

			numobj++;                     //㢥«¨ç¨âì ç¨á«® ®¡ê¥ªâ®¢

		}

		if(WinMonoBlock&&dllflag)startsecr=vsizeheader+

			Align(sizeimport+sizeexport+outptr+(wbss==FALSE?postsize:0)+sizebss+sizeres,OBJECTALIGN);

		else startsecr=vsizeheader+vsize+sizeImport+sizeExport+sizebss+sizeres;	//¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨

		//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 â ¡«¨æë ¯¥à¥¬¥é¥­¨©

		sizereloc=Align(numrel*2+(outptr/4096+1)*10,FILEALIGN);

		sizeReloc=Align(sizereloc,OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à í⮩ ᥪ樨

		relocblock=(char *)MALLOC(sizereloc);	//¯ ¬ïâì ¯®¤ íâã ᥪæ¨î

		memset(relocblock,0,sizereloc);	//®ç¨áâ¨âì ¥¥

		//§ ¯®«­ï¥¬ ᥪæ¨î ¯¥à¥¬¥é¥­¨ï

		unsigned int startrsec=0;	// ¤à¥á ­ ç «  ¡«®ª  ¢ ᥪ樨 ¯¥à¥¬¥é¥­¨ï

		unsigned int startblc=0;	// ¤à¥á ¯¥à¢®£® ¡«®ª 

		posrel=8;

		do{

			unsigned char fr=FALSE;	//ä« £ í«¥¬¥­â 

			for(unsigned int i=0;itype==CALL_32I||

						    ((postbuf+i)->type>=POST_VAR32&&(postbuf+i)->type<=FIX_CODE32))

								&&

	  				(postbuf+i)->loc>=startblc&&(postbuf+i)->loc<(startblc+4096)){

					*(short *)&relocblock[posrel]=(short)((postbuf+i)->loc%4096|0x3000);

					posrel+=2;

					fr=TRUE;

				}

			}

			if(fr!=FALSE){	//¥á«¨ ¡ë«¨ ¯¥à¥¬¥é ¥¬ë¥  ¤à¥á 

				posrel+=posrel%4;	//¢ëà ¢­¨¢ ¥¬

				*(long *)&relocblock[startrsec]=vsizeheader+sizebss+startblc;

				*(long *)&relocblock[startrsec+4]=posrel-startrsec;	//à §¬¥à ªã᪠

				startrsec=posrel;

				posrel+=8;

			}

			startblc+=4096;

		}while(startblcname,".reloc");	//¨¬ï ᥪ樨

			(objentry+relocnum)->vsize=sizeReloc;        //à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨

			(objentry+relocnum)->psize=sizereloc;        //à §¬¥à ᥪ樨 ¢ ä ©«¥

			(objentry+relocnum)->flags=0x52000040;       //ä« £ ᥪ樨

			(objentry+relocnum)->sectionRVA=startsecr;   //¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨

		}

		else sizereloc=Align(posrel,4);

	}

	if(WinMonoBlock){

		psize=sizeimport+sizeexport+(dllflag==FALSE?sizereloc:0)+sizeres;	//à §¬¥à ¤®¯®«­¨â¥«ì­ëå ¤ ­­ëå

		if(wbss==0){

			for(unsigned int i=0;itype==POST_VAR32)

						*(long *)&output[(postbuf+i)->loc]+=psize;

			}

		}

		psize+=outptr;

		(objentry+codenum)->vsize=vsize=Align(psize+(wbss==FALSE?postsize:0),OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à ᥪ樨 ª®¤ 

		filingzerope=(objentry+codenum)->psize=Align(psize,FILEALIGN);	//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 ª®¤ 

		filingzerope-=psize;

		psize=(objentry+codenum)->psize;

		sizeImport=sizeExport=0;

		if(dllflag==FALSE)sizeReloc=0;

	}

	PE_HEADER *peheader=(PE_HEADER *)MALLOC(sizeof(PE_HEADER));

	memset(peheader,0,sizeof(PE_HEADER));

	sizehead=Align(sizeof(PE_HEADER)+sizestub+(numobj+(numres!=0?0:1))*sizeof(OBJECT_ENTRY),FILEALIGN);

#ifdef _WC_

	peheader->sign='EP';

#else

	peheader->sign='PE';

#endif

	peheader->cpu=0x14c;//(chip>=4?(chip>=5?0x14e:0x14D):0x14c);

//	peheader->date_time=0;

	peheader->DLLflag=dllflag;

	peheader->numobj=numobj;

	peheader->NTheadsize=0xe0;

	peheader->flags=(short)(0x818e|(dllflag==0?0:0x2000));

	peheader->Magic=0x10b;

	peheader->LinkVer=(short)((short)ver2*256+ver1);

	peheader->sizecode=psize;

	peheader->sizeuninitdata=postsize;

	{

		unsigned int temp;

		temp=EntryPoint();

		if(temp==0xffffffff)peheader->EntryRVA=0;

		else peheader->EntryRVA=vsizeheader+sizebss+temp;

	}

	peheader->basecode=vsizeheader+sizebss;

	peheader->objAlig=OBJECTALIGN;

	peheader->fileAlig=FILEALIGN;

	peheader->OSver=1;

	peheader->SubSysVer=4;

	peheader->ImageBase=ImageBase;

	peheader->headsize=sizehead;

	peheader->imagesize=vsizeheader+	//à §¬¥à § £®«®¢ª 

			                vsize+	//à §¬¥à ª®¤ 

											sizebss+		//à §¬¥à post ¡«®ª 

											sizeReloc+	//à §¬¥à â ¡«¨æë ¯¥à¥¬¥é¥­¨ï

											sizeImport+//à §¬¥à â ¡«¨æë ¨¬¯®àâ 

											sizeRes+	//à §¬¥à â ¡«¨æë à¥áãàᮢ

											sizeExport;//à §¬¥à â ¡«¨æë íªá¯®àâ 

	peheader->SubSys=(short)(2+wconsole);	//GUIWIN

	peheader->stackRezSize=stacksize*0x10;

	peheader->stackComSize=stacksize;

	peheader->heapRezSize=0x10000;

	peheader->heapComSize=0x1000;//postsize;	//????

	peheader->numRVA=0x10;

	if(!usestub){

		peheader->basedata=12;

		peheader->pCOFF=0x40;

	}

	(objentry+codenum)->pOffset=sizehead;

	if(numapi){

		if(!WinMonoBlock)(objentry+importnum)->pOffset=sizehead+psize;

		peheader->importRVA=startsec;

		peheader->importSize=startimportname;

	}

	if(numexport){

		if(!WinMonoBlock)(objentry+exportnum)->pOffset=sizehead+psize+sizeimport;

		peheader->exportRVA=startsece;

		peheader->exportSize=startexportname;

	}

	if(numres){

		if(!WinMonoBlock)(objentry+resnum)->pOffset=sizehead+psize+sizeimport+sizeexport;

		peheader->resourRVA=startsecres;

		peheader->resourSize=curposbuf;

	}

	if(posrel){

		if(!WinMonoBlock)(objentry+relocnum)->pOffset=sizehead+psize+sizeimport+sizeexport+sizeres;

		else if(dllflag)(objentry+relocnum)->pOffset=sizehead+psize;

		peheader->fixupRVA=startsecr;

		peheader->fixupSize=posrel;

	}

	if(fwrite(peheader,sizeof(PE_HEADER),1,hout)!=1){

errwrite:

		ErrWrite();

		fclose(hout);

		hout=NULL;

		return(-1);

	}

	if(fwrite(objentry,sizeof(OBJECT_ENTRY)*numobj,1,hout)!=1)goto errwrite;

	ChSize(sizehead);

	runfilesize=sizehead+psize;

	outputcodestart=ftell(hout);

	if(fwrite(output,outptr,1,hout)!=1)goto errwrite;	//¡«®ª ª®¤ 

	if(!WinMonoBlock){

		filingzerope=psize-outptr;

		ChSize(runfilesize);

	}

	if(numapi){

		if(fwrite(importblock,sizeimport,1,hout)!=1)goto errwrite;

		free(importblock);

	}

	if(numexport){

		if(fwrite(exportblock,sizeexport,1,hout)!=1)goto errwrite;

		free(exportblock);

	}

	if(numres){

		if(fwrite(resbuf,sizeres,1,hout)!=1)goto errwrite;

		free(resbuf);

	}

	if(posrel){

		if(WinMonoBlock&&dllflag)ChSize(runfilesize);

		if(fwrite(relocblock,sizereloc,1,hout)!=1)goto errwrite;

		free(relocblock);

	}

	if(WinMonoBlock){

		if(dllflag)runfilesize+=sizereloc;

	}

	else runfilesize+=sizereloc+sizeimport+sizeexport+sizeres;

	ChSize(runfilesize);

	free(peheader);

	free(objentry);

	fclose(hout);

	hout=NULL;

	ImageBase+=vsizeheader+sizebss;	//¨§¬ à §¬¥à ¤«ï «¨á⨭£ 

	return 0;

}



void ChSize(long size)

{

char buf[256];

long delta,ssave;

	memset(buf,0,256);

	delta=size-ftell(hout);

	while(delta>0){

		ssave=256;

		if(delta<256)ssave=delta;

		fwrite(buf,ssave,1,hout);

		delta-=ssave;

	}

}



int AlignCD(char segm,int val)	//¢ëà ¢­ïâì ¤ ­­ë¥ ¨«¨ ª®¤

{

unsigned int a;

	a=(segm==DS?outptrdata:outptr)%val;

	if(a==0)val=0;

	else val-=a;

	a=0;

	while(val!=0){

		segm==DS?opd(aligner):op(0x90);

		val--;

		a++;

	}

	return a;

}



void CreatWinStub()

{

	if(!usestub){

		sizestub=SIZESTUB2;

		hout=CreateOutPut(outext,"wb");

		if(fwrite(stub2,SIZESTUB2,1,hout)!=1){

			ErrWrite();

			return;

		}

	}

	else CreatStub(winstub);

//¯®¤áç¨â âì ç¨á«® ᥪ権

	if(wbss){

		if(postsize)numrs++;

		else wbss=FALSE;

	}

	if(WinMonoBlock==FALSE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª

		if(numapi)numrs++;	//¥áâì ¢ë§®¢ë api-¯à®æ¥¤ãà

		if(numexport)numrs++;	//ᮧ¤ âì ᥪæ¨î ¨¬¯®àâ 

		if((FixUpTable==TRUE&&posts)/*||numexport!=0*/)numrs++;	//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï

		if(numres)numrs++;	//à¥áãàáë

	}

	else if(dllflag&&FixUpTable==TRUE&&posts!=0)numrs++;	//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï

//à §¬¥à § £à㧮筮£® ®¡à § 

	vsizeheader=Align(numrs*sizeof(OBJECT_ENTRY)+sizeof(PE_HEADER)+sizestub,0x1000);

}



void ImportName(char *name)

{

FILE *infile;

union{

	PE_HEADER pe;

	OBJECT_ENTRY obj;

};

unsigned long temp;

unsigned long ulexport;	//ᥪæ¨ï á ¥ªá¯®à⮬

unsigned long numobj;	//ç¨á«® ®¡ê¥ªâ®¢

unsigned long posdll;	//¯®§¨æ¨ï ᥪ樨 ¢ ä ©«¥

unsigned long nameadr;	//â ¡«¨æ   ¤à¥á®¢ ¨¬¥­

unsigned long startname;	//­ ç «® ¨¬¥­¨

unsigned long ordinallist,ordinalbase;

unsigned int i,j;

DLLLIST *newdll;

APIPROC *listapi;

	if((infile=fopen(name,"rb"))==NULL){

		ErrOpenFile(name);

		return;

	}

	fseek(infile,0x3C,SEEK_SET);

	if(fread(&temp,4,1,infile)!=1){

errread:

		//fprintf(stderr,"Unable to read from file %s.\n",name);
		printf("Unable to read from file %s.\n",name);
		fclose(infile);

		return;

	}

	fseek(infile,0,SEEK_END);

	if((unsigned long)ftell(infile)<=temp){

		//fprintf(stderr,"Bad file %s.\n",name);
		printf("Bad file %s.\n",name);
		fclose(infile);

		return;

	}

	fseek(infile,temp,SEEK_SET);

	if(fread(&pe,sizeof(PE_HEADER),1,infile)!=1)goto errread;

	if(pe.sign!=

#ifdef _WC_

			'EP'

#else

      'PE'

#endif

					){

		//fprintf(stderr,"For DLL support only format PE.\n");
		printf("For DLL support only format PE.\n");
		
		fclose(infile);

		return;

	}

	if((ulexport=pe.exportRVA)==0){

		//fprintf(stderr,"No export directory on %s.\n",name);
		printf("No export directory on %s.\n",name);
		fclose(infile);

		return;

	}

	numobj=pe.numobj;

	temp=pe.objAlig;

	while(numobj!=0){

		if(fread(&obj,sizeof(OBJECT_ENTRY),1,infile)!=1)goto errread;

		if((obj.sectionRVA+Align(obj.psize,temp))>ulexport)break;

		numobj--;

	}

	if(numobj==0){

		//fprintf(stderr,"Bad object table in %s.\n",name);
		printf("Bad object table in %s.\n",name);
		fclose(infile);

		return;

	}

	posdll=obj.pOffset+ulexport-obj.sectionRVA;

	fseek(infile,posdll+24,SEEK_SET);

	if(fread(&numobj,4,1,infile)!=1)goto errread;

	fseek(infile,posdll+32,SEEK_SET);

	if(fread(&nameadr,4,1,infile)!=1)goto errread;

	if(fread(&ordinallist,4,1,infile)!=1)goto errread;

	nameadr-=ulexport;

	ordinallist-=ulexport;

	fseek(infile,posdll+12,SEEK_SET);

	if(fread(&startname,4,1,infile)!=1)goto errread;

	if(fread(&ordinalbase,4,1,infile)!=1)goto errread;

	fseek(infile,posdll+startname-ulexport,SEEK_SET);

	j=0;

	do{

		if(fread(&string[j],1,1,infile)!=1)goto errread;

	}while(string[j++]!=0);	//¨¬ï ¡¨¡«¨®â¥ª¨

	newdll=FindDLL();

	listapi=newdll->list;



	for(i=0;inum==0)listapi=(APIPROC *)MALLOC(sizeof(APIPROC));	//¯¥à¢ ï ¢ ᯨ᪥

			else listapi=(APIPROC *)REALLOC(listapi,sizeof(APIPROC)*(newdll->num+1));

			(listapi+newdll->num)->recapi=addtotree(itok.name);

			newdll->num++;

		}

		nameadr+=4;

		ordinallist+=2;

	}

	newdll->list=listapi;

	fclose(infile);

}



void CreatStub(char *name)

{

	sizestub=SIZESTUB;

	hout=CreateOutPut(outext,"wb");

	sprintf((char *)(&stub[STRVERS]),"%s%s",compilerstr,__DATE__);

	if(name==NULL){

stdstub:

		if(fwrite(stub,SIZESTUB,1,hout)!=1){

errwrite:

			ErrWrite();

			return;

		}

	}

	else{

EXE_DOS_HEADER exeheader;  // header for EXE format

FILE *stubin;

		if((stubin=fopen(name,"rb"))==NULL){

			ErrOpenFile(name);

			goto stdstub;

		}

		if(fread(&exeheader,sizeof(EXE_DOS_HEADER),1,stubin)!=1){

errread:

			ErrReadStub();

			fclose(stubin);

			goto stdstub;

		}

		if(exeheader.sign!=0x5A4D){

errstub:

			//fprintf(stderr,"File %s can not be stub file.\n",name);
			printf("File %s can not be stub file.\n",name);
			fclose(stubin);

			goto stdstub;

		}

		fseek(stubin,0,SEEK_END);

		sizestub=ftell(stubin);

		unsigned long temp;

		if(exeheader.ofsreloc>=0x40){	//¯à®¢¥àª  çâ® íâ® ­¥ 32-¡¨â­ë© ä ©«

			fseek(stubin,0x3c,SEEK_SET);

			if(fread(&temp,4,1,stubin)!=1)goto errread;

			if(temp=maxsizelistname){

		maxsizelistname+=MAXLISTNAME;

		ListName=(char *)REALLOC(ListName,maxsizelistname);

	}

	strcpy(ListName+sizelistName,name);

	sizelistName+=size+1;

}



void CreatSymbolTable(idrec *ptr)

{

unsigned int i;

	if(ptr!=NULL){

		CreatSymbolTable(ptr->right);

		if(ptr->rectok==tk_apiproc){

			for(unsigned int j=0;jnum==(unsigned long)ptr->recnumber&&((postbuf+j)->type==CALL_32I||(postbuf+j)->type==CALL_32)){

					goto nameext;

				}

			}

		}

		else if((externnum!=0&&ptr->rectok==tk_undefproc&&(ptr->flag&f_extern)!=0)||

				((ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)&&ptr->recsegm>=NOT_DYNAMIC)||

				((ptr->rectok>=tk_bits&&ptr->rectok<=tk_doublevar)||ptr->rectok==tk_structvar)){

nameext:

			if(numsymbol+1>=maxnumsymbol){

				maxnumsymbol+=MAXNUMSYMBOL;

				isymbol=(IMAGE_SYMBOL *)REALLOC(isymbol,maxnumsymbol*sizeof(IMAGE_SYMBOL));

			 	memset(isymbol+maxnumsymbol*sizeof(IMAGE_SYMBOL)-MAXSIZESYMBOL,0,MAXSIZESYMBOL);	//®ç¨áâ¨âì ¥¥

			}

			if(ptr->rectok==tk_apiproc||ptr->rectok==tk_undefproc||

					ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)(isymbol+numsymbol)->Type=32;

			i=strlen(ptr->recid);

			if(i>8){

				(isymbol+numsymbol)->N.Name.Short=0;

				(isymbol+numsymbol)->N.Name.Long=sizelistName+4;

				AddName(ptr->recid,i);

			}

			else strncpy((isymbol+numsymbol)->N.sname,ptr->recid,i);

			if(ptr->rectok!=tk_apiproc&&ptr->rectok!=tk_undefproc&&(ptr->flag&f_extern)==0){

				(isymbol+numsymbol)->Value=ptr->recnumber;

				if(ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)(isymbol+numsymbol)->SectionNumber=(short)(textnum+1);

				else (isymbol+numsymbol)->SectionNumber=(short)((ptr->recpost==0?textnum:bssnum)+1);

			}

			else{

				(NameId+numextern)->num=ptr->recnumber;

				(NameId+numextern)->id=numsymbol;

				numextern++;

				if(numextern>=maxnumnameid){

					maxnumnameid+=MAXNUMSYMBOL;

					NameId=(NAMEID *)REALLOC(NameId,maxnumnameid*sizeof(NAMEID));

				}

			}

			(isymbol+numsymbol)->StorageClass=2;

			numsymbol++;

		}

		CreatSymbolTable(ptr->left);

	}

}



void FreeCoffBuf()

{

	free(ListName);

	free(isymbol);

	free(NameId);

	free(treloc);

	fclose(hout);

	hout=NULL;

}



void IncReloc()

{

	numreloc++;

	if(numreloc>=maxnumreloc){

		maxnumreloc+=MAXNUMRELOC;

		treloc=(IMAGE_RELOCATION *)REALLOC(treloc,maxnumreloc*sizeof(IMAGE_RELOCATION));

	}

}



void CreatRelocTable()

{

	for(int j=0;jVirtualAddress=(postbuf+j)->loc-startptr;

		if((postbuf+j)->type>=CALL_32I&&(postbuf+j)->type<=FIX_CODE32){

			switch((postbuf+j)->type){

				case POST_VAR:

				case POST_VAR32:

					(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;

					(treloc+numreloc)->SymbolTableIndex=segbss;

					break;

				case FIX_VAR:

				case FIX_VAR32:

				case FIX_CODE:

				case FIX_CODE32:

					(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;

					(treloc+numreloc)->SymbolTableIndex=segtext;

					break;

			}

			IncReloc();

		}

		else{

			for(int i=0;inum==(postbuf+j)->num){

					if((postbuf+j)->type==EXT_VAR)(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;

					else (treloc+numreloc)->Type=IMAGE_REL_I386_REL32;

					(treloc+numreloc)->SymbolTableIndex=(NameId+i)->id;

					IncReloc();

					break;

				}

			}

		}



	}

}



int MakeCoff()

{

COFF_HEADER chead = {0};

unsigned long sizehead,curobj,resnum,numresrel,segres,lastoffset,headernum;

OBJECT_ENTRY *objentry;

int i;

LISTRELOC *resrel=NULL;

char *codesecname;

	hout=CreateOutPut("obj","wb");

	chead.cpu=0x14c;

	chead.SizeOfOptionalHeader=0;

	chead.date_time=0;

	chead.Characteristics=0x100;

	/*if(header)*/numrs=2;

//¯®¤áç¨â âì ç¨á«® ᥪ権

	if(wbss){

		if(postsize)numrs++;

		else wbss=FALSE;

	}

	if(numres)numrs++;	//à¥áãàáë

	chead.numobj=numrs;

	sizehead=numrs*sizeof(OBJECT_ENTRY);

	objentry=(OBJECT_ENTRY *)MALLOC(sizehead);//â ¢«¨æ  ®¡ê¥ªâ®¢

	memset(objentry,0,sizehead);//®ç¨áâ¨âì â ¡«¨æã ®¡ê¥ªâ®¢

	curobj=0;

	lastoffset=sizehead+sizeof(COFF_HEADER);

//	if(header){

		strcpy((objentry+curobj)->name,".version");

		sprintf((char *)(&stub[STRVERS]),"%s%s",compilerstr,__DATE__);

		(objentry+curobj)->psize=strlen((char *)(&stub[STRVERS]))+1;

		(objentry+curobj)->pOffset=lastoffset;

		(objentry+curobj)->flags=0x100A00;

		headernum=curobj;

		lastoffset+=(objentry+curobj)->psize;

		curobj++;

//	}

	codesecname=".text";

	if(splitdata==FALSE)codesecname=".codedat";

	strcpy((objentry+curobj)->name,codesecname);

	(objentry+curobj)->psize=outptr;

	(objentry+curobj)->pOffset=lastoffset;

	(objentry+curobj)->flags=0xE0300060;

	lastoffset+=outptr;

	textnum=curobj;

	curobj++;

	if(wbss){

		strcpy((objentry+curobj)->name,".bss");

		(objentry+curobj)->psize=postsize;

		(objentry+curobj)->flags=0xC0300080;

		bssnum=curobj;

		curobj++;

	}

	if(numres){

		strcpy((objentry+curobj)->name,".rsrc$01");

		numresrel=(objentry+curobj)->NumberOfRelocations=MakeRes(0,&resrel);

		(objentry+curobj)->psize=curposbuf;

		(objentry+curobj)->flags=0x40000040;

		resnum=curobj;

	}

	sizelistName=0;numsymbol=0;

	ListName=(char *)MALLOC(MAXLISTNAME);

	isymbol=(IMAGE_SYMBOL *)MALLOC(MAXSIZESYMBOL);

 	memset(isymbol,0,MAXSIZESYMBOL);	//®ç¨áâ¨âì ¥¥

	maxsizelistname=MAXLISTNAME;

	maxnumnameid=maxnumsymbol=MAXNUMSYMBOL;

	NameId=(NAMEID *)MALLOC(MAXSIZENAMEID);

	treloc=(IMAGE_RELOCATION *)MALLOC(sizeof(IMAGE_RELOCATION)*MAXNUMRELOC);

	maxnumreloc=MAXNUMRELOC;

	numreloc=0;

	strcpy(isymbol->N.sname,"@comp.id");

	isymbol->Value=0x141F8E;

	isymbol->SectionNumber=-1;

	isymbol->StorageClass=3;

	strcpy((isymbol+1)->N.sname,".file");

	(isymbol+1)->Value=1;

	(isymbol+1)->SectionNumber=-2;

	(isymbol+1)->StorageClass=0x67;

	i=(strlen(startfileinfo->filename)-1)/sizeof(IMAGE_SYMBOL)+1;

	(isymbol+1)->NumberOfAuxSymbols=i;

	strcpy((isymbol+2)->N.sname,startfileinfo->filename);

	numsymbol=i+2;

	segtext=numsymbol;

	strcpy((isymbol+numsymbol)->N.sname,codesecname);

	(isymbol+numsymbol)->SectionNumber=textnum+1;

	(isymbol+numsymbol)->StorageClass=3;

	(isymbol+numsymbol)->NumberOfAuxSymbols=1;

	numsymbol++;

	(isymbol+numsymbol)->N.Name.Short=outptr;

	numsymbol++;

	if(wbss){

		segbss=numsymbol;

		strcpy((isymbol+numsymbol)->N.sname,".bss");

		(isymbol+numsymbol)->SectionNumber=bssnum+1;

		(isymbol+numsymbol)->StorageClass=3;

		(isymbol+numsymbol)->NumberOfAuxSymbols=1;

		numsymbol++;

		(isymbol+numsymbol)->N.Name.Short=postsize;

		numsymbol++;

		strcpy((isymbol+numsymbol)->N.sname,"DGROUP");

		(isymbol+numsymbol)->SectionNumber=bssnum+1;

		(isymbol+numsymbol)->StorageClass=3;

	}

	strcpy((isymbol+numsymbol)->N.sname,"FLAT");

	(isymbol+numsymbol)->SectionNumber=-1;

	(isymbol+numsymbol)->StorageClass=3;

	numsymbol++;

	if(numres){

		segres=numsymbol;

		strcpy((isymbol+numsymbol)->N.sname,".rsrc$01");

		(isymbol+numsymbol)->StorageClass=3;

		(isymbol+numsymbol)->SectionNumber=resnum+1;

		numsymbol++;

	}

//	if(header){

		strcpy((isymbol+numsymbol)->N.sname,".version");

		(isymbol+numsymbol)->SectionNumber=headernum+1;

		(isymbol+numsymbol)->StorageClass=3;

		numsymbol++;

//	}

	CreatSymbolTable(treestart);

	CreatRelocTable();

	(isymbol+segtext+1)->N.Name.Long=numreloc;

	(objentry+textnum)->NumberOfRelocations=numreloc;

	if(numreloc){

		(objentry+textnum)->PointerToRelocations=lastoffset;

		lastoffset+=sizeof(IMAGE_RELOCATION)*numreloc;

	}

	if(numres){

		(objentry+resnum)->pOffset=lastoffset;

		lastoffset+=curposbuf;

		if(numresrel){

			(objentry+resnum)->PointerToRelocations=lastoffset;

			lastoffset+=sizeof(IMAGE_RELOCATION)*numresrel;

		}

	}

	chead.COFFsize=numsymbol;

	if(numsymbol){

		chead.pCOFF=lastoffset;

	}

	if(fwrite(&chead,sizeof(COFF_HEADER),1,hout)!=1){

errwrite:

		ErrWrite();

		free(objentry);

		if(resrel)free(resrel);

		FreeCoffBuf();

		return(-1);

	}

	if(fwrite(objentry,sizehead,1,hout)!=1)goto errwrite;

//	if(header){

		if(fwrite(&stub[STRVERS],(objentry+headernum)->psize,1,hout)!=1)goto errwrite;

//	}

	if(fwrite(output,outptr,1,hout)!=1)goto errwrite;	//¡«®ª ª®¤ 

	if(numreloc){

		if(fwrite(treloc,numreloc*sizeof(IMAGE_RELOCATION),1,hout)!=1)goto errwrite;

	}

	if(numres){

		if(fwrite(resbuf,curposbuf,1,hout)!=1)goto errwrite;

		free(resbuf);

		if(numresrel){

			IMAGE_RELOCATION *rrel;

			rrel=(IMAGE_RELOCATION *)MALLOC(sizeof(IMAGE_RELOCATION)*numresrel);

			for(i=0;iVirtualAddress=(resrel+i)->val;

				(rrel+i)->Type=IMAGE_REL_I386_DIR32NB;

				(rrel+i)->SymbolTableIndex=segres;

			}

			if(fwrite(rrel,sizeof(IMAGE_RELOCATION)*numresrel,1,hout)!=1)goto errwrite;

			free(rrel);

		}

	}

	if(numsymbol){

		if(fwrite(isymbol,numsymbol*sizeof(IMAGE_SYMBOL),1,hout)!=1)goto errwrite;

		if(sizelistName){

			sizelistName+=4;

			if(fwrite(&sizelistName,4,1,hout)!=1)goto errwrite;

			if(fwrite(ListName,sizelistName-4,1,hout)!=1)goto errwrite;

		}

		else{

			if(fwrite(&sizelistName,4,1,hout)!=1)goto errwrite;

			sizelistName+=4;

		}

	}

	runfilesize=lastoffset+sizelistName;

	free(objentry);

	if(resrel)free(resrel);

	FreeCoffBuf();

	return 0;

}
328
		}
-
 
329
		startsecres=vsizeheader+vsize+sizeImport+sizebss+sizeExport;//­ ç «® ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
330
		LISTRELOC *resrel;
-
 
331
		if(MakeRes(startsecres,&resrel))free(resrel);
-
 
332
		if(!WinMonoBlock){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
333
			strcpy((objentry+resnum)->name,".rsrc"); //¨¬ï ᥪ樨
-
 
334
			(objentry+resnum)->vsize=sizeRes=Align(curposbuf,OBJECTALIGN);
-
 
335
			(objentry+resnum)->psize=sizeres=Align(curposbuf,FILEALIGN);
-
 
336
			(objentry+resnum)->flags=0x40000040;
-
 
337
			(objentry+resnum)->sectionRVA=startsecres;
-
 
338
		}
-
 
339
		else sizeres=Align(curposbuf,4);
-
 
340
	}
-
 
341
 
-
 
342
//ᥪæ¨ï â ¡«¨æ ¯¥à¥¬¥é¥­¨ï
-
 
343
	if((FixUpTable==TRUE&&numrel!=0)/*||numexport!=0*/){//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï
-
 
344
		if(WinMonoBlock==FALSE||dllflag==TRUE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª ¨ íâ® DLL
-
 
345
			relocnum=numobj;
-
 
346
			numobj++;                     //㢥«¨ç¨âì ç¨á«® ®¡ê¥ªâ®¢
-
 
347
		}
-
 
348
		if(WinMonoBlock&&dllflag)startsecr=vsizeheader+
-
 
349
			Align(sizeimport+sizeexport+outptr+(wbss==FALSE?postsize:0)+sizebss+sizeres,OBJECTALIGN);
-
 
350
		else startsecr=vsizeheader+vsize+sizeImport+sizeExport+sizebss+sizeres;	//¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
351
		//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 â ¡«¨æë ¯¥à¥¬¥é¥­¨©
-
 
352
		sizereloc=Align(numrel*2+(outptr/4096+1)*10,FILEALIGN);
-
 
353
		sizeReloc=Align(sizereloc,OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à í⮩ ᥪ樨
-
 
354
		relocblock=(char *)MALLOC(sizereloc);	//¯ ¬ïâì ¯®¤ íâã ᥪæ¨î
-
 
355
		memset(relocblock,0,sizereloc);	//®ç¨áâ¨âì ¥¥
-
 
356
		//§ ¯®«­ï¥¬ ᥪæ¨î ¯¥à¥¬¥é¥­¨ï
-
 
357
		unsigned int startrsec=0;	// ¤à¥á ­ ç «  ¡«®ª  ¢ ᥪ樨 ¯¥à¥¬¥é¥­¨ï
-
 
358
		unsigned int startblc=0;	// ¤à¥á ¯¥à¢®£® ¡«®ª 
-
 
359
		posrel=8;
-
 
360
		do{
-
 
361
			unsigned char fr=FALSE;	//ä« £ í«¥¬¥­â 
-
 
362
			for(unsigned int i=0;i
-
 
363
				if(
-
 
364
						(
-
 
365
						  (postbuf+i)->type==CALL_32I||
-
 
366
						    ((postbuf+i)->type>=POST_VAR32&&(postbuf+i)->type<=FIX_CODE32))
-
 
367
								&&
-
 
368
	  				(postbuf+i)->loc>=startblc&&(postbuf+i)->loc<(startblc+4096)){
-
 
369
					*(short *)&relocblock[posrel]=(short)((postbuf+i)->loc%4096|0x3000);
-
 
370
					posrel+=2;
-
 
371
					fr=TRUE;
-
 
372
				}
-
 
373
			}
-
 
374
			if(fr!=FALSE){	//¥á«¨ ¡ë«¨ ¯¥à¥¬¥é ¥¬ë¥  ¤à¥á 
-
 
375
				posrel+=posrel%4;	//¢ëà ¢­¨¢ ¥¬
-
 
376
				*(long *)&relocblock[startrsec]=vsizeheader+sizebss+startblc;
-
 
377
				*(long *)&relocblock[startrsec+4]=posrel-startrsec;	//à §¬¥à ªã᪠
-
 
378
				startrsec=posrel;
-
 
379
				posrel+=8;
-
 
380
			}
-
 
381
			startblc+=4096;
-
 
382
		}while(startblc
-
 
383
		posrel-=8;
-
 
384
		if(WinMonoBlock==FALSE||dllflag==TRUE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
385
			strcpy((objentry+relocnum)->name,".reloc");	//¨¬ï ᥪ樨
-
 
386
			(objentry+relocnum)->vsize=sizeReloc;        //à §¬¥à ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
387
			(objentry+relocnum)->psize=sizereloc;        //à §¬¥à ᥪ樨 ¢ ä ©«¥
-
 
388
			(objentry+relocnum)->flags=0x52000040;       //ä« £ ᥪ樨
-
 
389
			(objentry+relocnum)->sectionRVA=startsecr;   //¢¨àâã «ì­ë©  ¤à¥á ᥪ樨 ¢ ¯ ¬ïâ¨
-
 
390
		}
-
 
391
		else sizereloc=Align(posrel,4);
-
 
392
	}
-
 
393
	if(WinMonoBlock){
-
 
394
		psize=sizeimport+sizeexport+(dllflag==FALSE?sizereloc:0)+sizeres;	//à §¬¥à ¤®¯®«­¨â¥«ì­ëå ¤ ­­ëå
-
 
395
		if(wbss==0){
-
 
396
			for(unsigned int i=0;i
-
 
397
				if((postbuf+i)->type==POST_VAR32)
-
 
398
						*(long *)&output[(postbuf+i)->loc]+=psize;
-
 
399
			}
-
 
400
		}
-
 
401
		psize+=outptr;
-
 
402
		(objentry+codenum)->vsize=vsize=Align(psize+(wbss==FALSE?postsize:0),OBJECTALIGN);//¢¨àâã «ì­ë© à §¬¥à ᥪ樨 ª®¤ 
-
 
403
		filingzerope=(objentry+codenum)->psize=Align(psize,FILEALIGN);	//䨧¨ç¥áª¨© à §¬¥à ᥪ樨 ª®¤ 
-
 
404
		filingzerope-=psize;
-
 
405
		psize=(objentry+codenum)->psize;
-
 
406
		sizeImport=sizeExport=0;
-
 
407
		if(dllflag==FALSE)sizeReloc=0;
-
 
408
	}
-
 
409
	PE_HEADER *peheader=(PE_HEADER *)MALLOC(sizeof(PE_HEADER));
-
 
410
	memset(peheader,0,sizeof(PE_HEADER));
-
 
411
	sizehead=Align(sizeof(PE_HEADER)+sizestub+(numobj+(numres!=0?0:1))*sizeof(OBJECT_ENTRY),FILEALIGN);
-
 
412
#ifdef _WC_
-
 
413
	peheader->sign='EP';
-
 
414
#else
-
 
415
	peheader->sign='PE';
-
 
416
#endif
-
 
417
	peheader->cpu=0x14c;//(chip>=4?(chip>=5?0x14e:0x14D):0x14c);
-
 
418
//	peheader->date_time=0;
-
 
419
	peheader->DLLflag=dllflag;
-
 
420
	peheader->numobj=numobj;
-
 
421
	peheader->NTheadsize=0xe0;
-
 
422
	peheader->flags=(short)(0x818e|(dllflag==0?0:0x2000));
-
 
423
	peheader->Magic=0x10b;
-
 
424
	peheader->LinkVer=(short)((short)ver2*256+ver1);
-
 
425
	peheader->sizecode=psize;
-
 
426
	peheader->sizeuninitdata=postsize;
-
 
427
	{
-
 
428
		unsigned int temp;
-
 
429
		temp=EntryPoint();
-
 
430
		if(temp==0xffffffff)peheader->EntryRVA=0;
-
 
431
		else peheader->EntryRVA=vsizeheader+sizebss+temp;
-
 
432
	}
-
 
433
	peheader->basecode=vsizeheader+sizebss;
-
 
434
	peheader->objAlig=OBJECTALIGN;
-
 
435
	peheader->fileAlig=FILEALIGN;
-
 
436
	peheader->OSver=1;
-
 
437
	peheader->SubSysVer=4;
-
 
438
	peheader->ImageBase=ImageBase;
-
 
439
	peheader->headsize=sizehead;
-
 
440
	peheader->imagesize=vsizeheader+	//à §¬¥à § £®«®¢ª 
-
 
441
			                vsize+	//à §¬¥à ª®¤ 
-
 
442
											sizebss+		//à §¬¥à post ¡«®ª 
-
 
443
											sizeReloc+	//à §¬¥à â ¡«¨æë ¯¥à¥¬¥é¥­¨ï
-
 
444
											sizeImport+//à §¬¥à â ¡«¨æë ¨¬¯®àâ 
-
 
445
											sizeRes+	//à §¬¥à â ¡«¨æë à¥áãàᮢ
-
 
446
											sizeExport;//à §¬¥à â ¡«¨æë íªá¯®àâ 
-
 
447
	peheader->SubSys=(short)(2+wconsole);	//GUIWIN
-
 
448
	peheader->stackRezSize=stacksize*0x10;
-
 
449
	peheader->stackComSize=stacksize;
-
 
450
	peheader->heapRezSize=0x10000;
-
 
451
	peheader->heapComSize=0x1000;//postsize;	//????
-
 
452
	peheader->numRVA=0x10;
-
 
453
	if(!usestub){
-
 
454
		peheader->basedata=12;
-
 
455
		peheader->pCOFF=0x40;
-
 
456
	}
-
 
457
	(objentry+codenum)->pOffset=sizehead;
-
 
458
	if(numapi){
-
 
459
		if(!WinMonoBlock)(objentry+importnum)->pOffset=sizehead+psize;
-
 
460
		peheader->importRVA=startsec;
-
 
461
		peheader->importSize=startimportname;
-
 
462
	}
-
 
463
	if(numexport){
-
 
464
		if(!WinMonoBlock)(objentry+exportnum)->pOffset=sizehead+psize+sizeimport;
-
 
465
		peheader->exportRVA=startsece;
-
 
466
		peheader->exportSize=startexportname;
-
 
467
	}
-
 
468
	if(numres){
-
 
469
		if(!WinMonoBlock)(objentry+resnum)->pOffset=sizehead+psize+sizeimport+sizeexport;
-
 
470
		peheader->resourRVA=startsecres;
-
 
471
		peheader->resourSize=curposbuf;
-
 
472
	}
-
 
473
	if(posrel){
-
 
474
		if(!WinMonoBlock)(objentry+relocnum)->pOffset=sizehead+psize+sizeimport+sizeexport+sizeres;
-
 
475
		else if(dllflag)(objentry+relocnum)->pOffset=sizehead+psize;
-
 
476
		peheader->fixupRVA=startsecr;
-
 
477
		peheader->fixupSize=posrel;
-
 
478
	}
-
 
479
	if(fwrite(peheader,sizeof(PE_HEADER),1,hout)!=1){
-
 
480
errwrite:
-
 
481
		ErrWrite();
-
 
482
		fclose(hout);
-
 
483
		hout=NULL;
-
 
484
		return(-1);
-
 
485
	}
-
 
486
	if(fwrite(objentry,sizeof(OBJECT_ENTRY)*numobj,1,hout)!=1)goto errwrite;
-
 
487
	ChSize(sizehead);
-
 
488
	runfilesize=sizehead+psize;
-
 
489
	outputcodestart=ftell(hout);
-
 
490
	if(fwrite(output,outptr,1,hout)!=1)goto errwrite;	//¡«®ª ª®¤ 
-
 
491
	if(!WinMonoBlock){
-
 
492
		filingzerope=psize-outptr;
-
 
493
		ChSize(runfilesize);
-
 
494
	}
-
 
495
	if(numapi){
-
 
496
		if(fwrite(importblock,sizeimport,1,hout)!=1)goto errwrite;
-
 
497
		free(importblock);
-
 
498
	}
-
 
499
	if(numexport){
-
 
500
		if(fwrite(exportblock,sizeexport,1,hout)!=1)goto errwrite;
-
 
501
		free(exportblock);
-
 
502
	}
-
 
503
	if(numres){
-
 
504
		if(fwrite(resbuf,sizeres,1,hout)!=1)goto errwrite;
-
 
505
		free(resbuf);
-
 
506
	}
-
 
507
	if(posrel){
-
 
508
		if(WinMonoBlock&&dllflag)ChSize(runfilesize);
-
 
509
		if(fwrite(relocblock,sizereloc,1,hout)!=1)goto errwrite;
-
 
510
		free(relocblock);
-
 
511
	}
-
 
512
	if(WinMonoBlock){
-
 
513
		if(dllflag)runfilesize+=sizereloc;
-
 
514
	}
-
 
515
	else runfilesize+=sizereloc+sizeimport+sizeexport+sizeres;
-
 
516
	ChSize(runfilesize);
-
 
517
	free(peheader);
-
 
518
	free(objentry);
-
 
519
	fclose(hout);
-
 
520
	hout=NULL;
-
 
521
	ImageBase+=vsizeheader+sizebss;	//¨§¬ à §¬¥à ¤«ï «¨á⨭£ 
-
 
522
	return 0;
-
 
523
}
-
 
524
 
-
 
525
void ChSize(long size)
-
 
526
{
-
 
527
char buf[256];
-
 
528
long delta,ssave;
-
 
529
	memset(buf,0,256);
-
 
530
	delta=size-ftell(hout);
-
 
531
	while(delta>0){
-
 
532
		ssave=256;
-
 
533
		if(delta<256)ssave=delta;
-
 
534
		fwrite(buf,ssave,1,hout);
-
 
535
		delta-=ssave;
-
 
536
	}
-
 
537
}
-
 
538
 
-
 
539
int AlignCD(char segm,int val)	//¢ëà ¢­ïâì ¤ ­­ë¥ ¨«¨ ª®¤
-
 
540
{
-
 
541
unsigned int a;
-
 
542
	a=(segm==DS?outptrdata:outptr)%val;
-
 
543
	if(a==0)val=0;
-
 
544
	else val-=a;
-
 
545
	a=0;
-
 
546
	while(val!=0){
-
 
547
		segm==DS?opd(aligner):op(0x90);
-
 
548
		val--;
-
 
549
		a++;
-
 
550
	}
-
 
551
	return a;
-
 
552
}
-
 
553
 
-
 
554
void CreatWinStub()
-
 
555
{
-
 
556
	if(!usestub){
-
 
557
		sizestub=SIZESTUB2;
-
 
558
		hout=CreateOutPut(outext,"wb");
-
 
559
		if(fwrite(stub2,SIZESTUB2,1,hout)!=1){
-
 
560
			ErrWrite();
-
 
561
			return;
-
 
562
		}
-
 
563
	}
-
 
564
	else CreatStub(winstub);
-
 
565
//¯®¤áç¨â âì ç¨á«® ᥪ権
-
 
566
	if(wbss){
-
 
567
		if(postsize)numrs++;
-
 
568
		else wbss=FALSE;
-
 
569
	}
-
 
570
	if(WinMonoBlock==FALSE){	//¥á«¨ ­¥ ¥¤¨­ë© ¡«®ª
-
 
571
		if(numapi)numrs++;	//¥áâì ¢ë§®¢ë api-¯à®æ¥¤ãà
-
 
572
		if(numexport)numrs++;	//ᮧ¤ âì ᥪæ¨î ¨¬¯®àâ 
-
 
573
		if((FixUpTable==TRUE&&posts)/*||numexport!=0*/)numrs++;	//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï
-
 
574
		if(numres)numrs++;	//à¥áãàáë
-
 
575
	}
-
 
576
	else if(dllflag&&FixUpTable==TRUE&&posts!=0)numrs++;	//ᮧ¤ âì ᥪæ¨î ¯¥à¥¬¥é¥­¨ï
-
 
577
//à §¬¥à § £à㧮筮£® ®¡à § 
-
 
578
	vsizeheader=Align(numrs*sizeof(OBJECT_ENTRY)+sizeof(PE_HEADER)+sizestub,0x1000);
-
 
579
}
-
 
580
 
-
 
581
void ImportName(char *name)
-
 
582
{
-
 
583
FILE *infile;
-
 
584
union{
-
 
585
	PE_HEADER pe;
-
 
586
	OBJECT_ENTRY obj;
-
 
587
};
-
 
588
unsigned long temp;
-
 
589
unsigned long ulexport;	//ᥪæ¨ï á ¥ªá¯®à⮬
-
 
590
unsigned long numobj;	//ç¨á«® ®¡ê¥ªâ®¢
-
 
591
unsigned long posdll;	//¯®§¨æ¨ï ᥪ樨 ¢ ä ©«¥
-
 
592
unsigned long nameadr;	//â ¡«¨æ   ¤à¥á®¢ ¨¬¥­
-
 
593
unsigned long startname;	//­ ç «® ¨¬¥­¨
-
 
594
unsigned long ordinallist,ordinalbase;
-
 
595
unsigned int i,j;
-
 
596
DLLLIST *newdll;
-
 
597
APIPROC *listapi;
-
 
598
	if((infile=fopen(name,"rb"))==NULL){
-
 
599
		ErrOpenFile(name);
-
 
600
		return;
-
 
601
	}
-
 
602
	fseek(infile,0x3C,SEEK_SET);
-
 
603
	if(fread(&temp,4,1,infile)!=1){
-
 
604
errread:
-
 
605
		//fprintf(stderr,"Unable to read from file %s.\n",name);
		printf("Unable to read from file %s.\n",name);
-
 
606
		fclose(infile);
-
 
607
		return;
-
 
608
	}
-
 
609
	fseek(infile,0,SEEK_END);
-
 
610
	if((unsigned long)ftell(infile)<=temp){
-
 
611
		//fprintf(stderr,"Bad file %s.\n",name);
		printf("Bad file %s.\n",name);
-
 
612
		fclose(infile);
-
 
613
		return;
-
 
614
	}
-
 
615
	fseek(infile,temp,SEEK_SET);
-
 
616
	if(fread(&pe,sizeof(PE_HEADER),1,infile)!=1)goto errread;
-
 
617
	if(pe.sign!=
-
 
618
#ifdef _WC_
-
 
619
			'EP'
-
 
620
#else
-
 
621
      'PE'
-
 
622
#endif
-
 
623
					){
-
 
624
		//fprintf(stderr,"For DLL support only format PE.\n");
		printf("For DLL support only format PE.\n");
		
-
 
625
		fclose(infile);
-
 
626
		return;
-
 
627
	}
-
 
628
	if((ulexport=pe.exportRVA)==0){
-
 
629
		//fprintf(stderr,"No export directory on %s.\n",name);
		printf("No export directory on %s.\n",name);
-
 
630
		fclose(infile);
-
 
631
		return;
-
 
632
	}
-
 
633
	numobj=pe.numobj;
-
 
634
	temp=pe.objAlig;
-
 
635
	while(numobj!=0){
-
 
636
		if(fread(&obj,sizeof(OBJECT_ENTRY),1,infile)!=1)goto errread;
-
 
637
		if((obj.sectionRVA+Align(obj.psize,temp))>ulexport)break;
-
 
638
		numobj--;
-
 
639
	}
-
 
640
	if(numobj==0){
-
 
641
		//fprintf(stderr,"Bad object table in %s.\n",name);
		printf("Bad object table in %s.\n",name);
-
 
642
		fclose(infile);
-
 
643
		return;
-
 
644
	}
-
 
645
	posdll=obj.pOffset+ulexport-obj.sectionRVA;
-
 
646
	fseek(infile,posdll+24,SEEK_SET);
-
 
647
	if(fread(&numobj,4,1,infile)!=1)goto errread;
-
 
648
	fseek(infile,posdll+32,SEEK_SET);
-
 
649
	if(fread(&nameadr,4,1,infile)!=1)goto errread;
-
 
650
	if(fread(&ordinallist,4,1,infile)!=1)goto errread;
-
 
651
	nameadr-=ulexport;
-
 
652
	ordinallist-=ulexport;
-
 
653
	fseek(infile,posdll+12,SEEK_SET);
-
 
654
	if(fread(&startname,4,1,infile)!=1)goto errread;
-
 
655
	if(fread(&ordinalbase,4,1,infile)!=1)goto errread;
-
 
656
	fseek(infile,posdll+startname-ulexport,SEEK_SET);
-
 
657
	j=0;
-
 
658
	do{
-
 
659
		if(fread(&string[j],1,1,infile)!=1)goto errread;
-
 
660
	}while(string[j++]!=0);	//¨¬ï ¡¨¡«¨®â¥ª¨
-
 
661
	newdll=FindDLL();
-
 
662
	listapi=newdll->list;
-
 
663
 
-
 
664
	for(i=0;i
-
 
665
		fseek(infile,posdll+nameadr,SEEK_SET);
-
 
666
		if(fread(&startname,4,1,infile)!=1)goto errread;
-
 
667
		fseek(infile,posdll+startname-ulexport,SEEK_SET);
-
 
668
		itok.size=-1;
-
 
669
		j=0;
-
 
670
		unsigned char c;
-
 
671
		do{
-
 
672
			if(fread(&c,1,1,infile)!=1)goto errread;
-
 
673
			if(c=='@'){
-
 
674
				itok.name[j]=0;
-
 
675
				break;
-
 
676
			}
-
 
677
			itok.name[j]=c;
-
 
678
			j++;
-
 
679
		}while(j<=IDLENGTH&&c!=0);
-
 
680
		if(c=='@'){
-
 
681
			j=0;
-
 
682
			do{
-
 
683
				if(fread(&c,1,1,infile)!=1)goto errread;
-
 
684
				string[j++]=c;
-
 
685
			}while(isdigit(c));
-
 
686
			itok.size=getnumber(string);
-
 
687
		}
-
 
688
		tok=tk_id;
-
 
689
		searchvar(itok.name);
-
 
690
		if(tok==tk_id){
-
 
691
			fseek(infile,posdll+ordinallist,SEEK_SET);
-
 
692
			itok.sib=0;
-
 
693
			if(fread(&itok.sib,2,1,infile)!=1)goto errread;
-
 
694
			itok.sib+=ordinalbase;
-
 
695
			tok=tk_apiproc;
-
 
696
			itok.number=secondcallnum++;
-
 
697
			itok.segm=NOT_DYNAMIC;
-
 
698
			string[0]=0;
-
 
699
			if(newdll->num==0)listapi=(APIPROC *)MALLOC(sizeof(APIPROC));	//¯¥à¢ ï ¢ ᯨ᪥
-
 
700
			else listapi=(APIPROC *)REALLOC(listapi,sizeof(APIPROC)*(newdll->num+1));
-
 
701
			(listapi+newdll->num)->recapi=addtotree(itok.name);
-
 
702
			newdll->num++;
-
 
703
		}
-
 
704
		nameadr+=4;
-
 
705
		ordinallist+=2;
-
 
706
	}
-
 
707
	newdll->list=listapi;
-
 
708
	fclose(infile);
-
 
709
}
-
 
710
 
-
 
711
void CreatStub(char *name)
-
 
712
{
-
 
713
	sizestub=SIZESTUB;
-
 
714
	hout=CreateOutPut(outext,"wb");
-
 
715
	sprintf(&stub[STRVERS],"%s%s",compilerstr,__DATE__);
-
 
716
	if(name==NULL){
-
 
717
stdstub:
-
 
718
		if(fwrite(stub,SIZESTUB,1,hout)!=1){
-
 
719
errwrite:
-
 
720
			ErrWrite();
-
 
721
			return;
-
 
722
		}
-
 
723
	}
-
 
724
	else{
-
 
725
EXE_DOS_HEADER exeheader;  // header for EXE format
-
 
726
FILE *stubin;
-
 
727
		if((stubin=fopen(name,"rb"))==NULL){
-
 
728
			ErrOpenFile(name);
-
 
729
			goto stdstub;
-
 
730
		}
-
 
731
		if(fread(&exeheader,sizeof(EXE_DOS_HEADER),1,stubin)!=1){
-
 
732
errread:
-
 
733
			ErrReadStub();
-
 
734
			fclose(stubin);
-
 
735
			goto stdstub;
-
 
736
		}
-
 
737
		if(exeheader.sign!=0x5A4D){
-
 
738
errstub:
-
 
739
			//fprintf(stderr,"File %s can not be stub file.\n",name);
			printf("File %s can not be stub file.\n",name);
-
 
740
			fclose(stubin);
-
 
741
			goto stdstub;
-
 
742
		}
-
 
743
		fseek(stubin,0,SEEK_END);
-
 
744
		sizestub=ftell(stubin);
-
 
745
		unsigned long temp;
-
 
746
		if(exeheader.ofsreloc>=0x40){	//¯à®¢¥àª  çâ® íâ® ­¥ 32-¡¨â­ë© ä ©«
-
 
747
			fseek(stubin,0x3c,SEEK_SET);
-
 
748
			if(fread(&temp,4,1,stubin)!=1)goto errread;
-
 
749
			if(temp
-
 
750
				fseek(stubin,temp,SEEK_SET);
-
 
751
				if(fread(&temp,4,1,stubin)!=1)goto errread;
-
 
752
				switch(temp){
-
 
753
#ifdef _WC_
-
 
754
					case 'EP':
-
 
755
					case 'EN':
-
 
756
					case 'EL':
-
 
757
					case 'XL':
-
 
758
#else
-
 
759
					case 'PE':
-
 
760
					case 'NE':
-
 
761
					case 'LE':
-
 
762
					case 'LX':
-
 
763
#endif
-
 
764
						goto errstub;
-
 
765
				}
-
 
766
			}
-
 
767
			exeheader.ofsreloc+=(unsigned short)0x20;
-
 
768
		}
-
 
769
		else exeheader.ofsreloc=0x40;
-
 
770
		//à §¬¥à ä ©« 
-
 
771
		sizestub=Align(sizestub+32,8);
-
 
772
		fseek(stubin,0x20,SEEK_SET);
-
 
773
		exeheader.headsize+=(unsigned short)2;
-
 
774
		if(fwrite(&exeheader,sizeof(EXE_DOS_HEADER),1,hout)!=1)goto errwrite;
-
 
775
		*(unsigned long *)&stub[STRVERS+28]=sizestub;
-
 
776
		if(fwrite(&stub[STRVERS],32,1,hout)!=1)goto errwrite;
-
 
777
		CopyFile(stubin,hout);
-
 
778
		ChSize(sizestub);
-
 
779
	}
-
 
780
}
-
 
781
 
-
 
782
#define MAXLISTNAME 1024
-
 
783
#define MAXNUMSYMBOL 128
-
 
784
#define MAXSIZESYMBOL MAXNUMSYMBOL*sizeof(IMAGE_SYMBOL)
-
 
785
#define MAXNUMRELOC 256
-
 
786
 
-
 
787
char *ListName;
-
 
788
unsigned long sizelistName,maxsizelistname,textnum,bssnum;
-
 
789
IMAGE_SYMBOL *isymbol;
-
 
790
unsigned long numsymbol,maxnumsymbol,maxnumnameid,maxnumreloc,numreloc;
-
 
791
int segtext,segbss;
-
 
792
extern int numextern;
-
 
793
IMAGE_RELOCATION *treloc;
-
 
794
 
-
 
795
struct NAMEID{
-
 
796
	int num;
-
 
797
	int id;
-
 
798
}*NameId;
-
 
799
 
-
 
800
#define MAXSIZENAMEID MAXNUMSYMBOL*sizeof(NAMEID)
-
 
801
 
-
 
802
void AddName(char *name,unsigned long size)
-
 
803
{
-
 
804
	if(sizelistName+size+1>=maxsizelistname){
-
 
805
		maxsizelistname+=MAXLISTNAME;
-
 
806
		ListName=(char *)REALLOC(ListName,maxsizelistname);
-
 
807
	}
-
 
808
	strcpy(ListName+sizelistName,name);
-
 
809
	sizelistName+=size+1;
-
 
810
}
-
 
811
 
-
 
812
void CreatSymbolTable(idrec *ptr)
-
 
813
{
-
 
814
unsigned int i;
-
 
815
	if(ptr!=NULL){
-
 
816
		CreatSymbolTable(ptr->right);
-
 
817
		if(ptr->rectok==tk_apiproc){
-
 
818
			for(unsigned int j=0;j
-
 
819
				if((postbuf+j)->num==(unsigned long)ptr->recnumber&&((postbuf+j)->type==CALL_32I||(postbuf+j)->type==CALL_32)){
-
 
820
					goto nameext;
-
 
821
				}
-
 
822
			}
-
 
823
		}
-
 
824
		else if((externnum!=0&&ptr->rectok==tk_undefproc&&(ptr->flag&f_extern)!=0)||
-
 
825
				((ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)&&ptr->recsegm>=NOT_DYNAMIC)||
-
 
826
				((ptr->rectok>=tk_bits&&ptr->rectok<=tk_doublevar)||ptr->rectok==tk_structvar)){
-
 
827
nameext:
-
 
828
			if(numsymbol+1>=maxnumsymbol){
-
 
829
				maxnumsymbol+=MAXNUMSYMBOL;
-
 
830
				isymbol=(IMAGE_SYMBOL *)REALLOC(isymbol,maxnumsymbol*sizeof(IMAGE_SYMBOL));
-
 
831
			 	memset(isymbol+maxnumsymbol*sizeof(IMAGE_SYMBOL)-MAXSIZESYMBOL,0,MAXSIZESYMBOL);	//®ç¨áâ¨âì ¥¥
-
 
832
			}
-
 
833
			if(ptr->rectok==tk_apiproc||ptr->rectok==tk_undefproc||
-
 
834
					ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)(isymbol+numsymbol)->Type=32;
-
 
835
			i=strlen(ptr->recid);
-
 
836
			if(i>8){
-
 
837
				(isymbol+numsymbol)->N.Name.Short=0;
-
 
838
				(isymbol+numsymbol)->N.Name.Long=sizelistName+4;
-
 
839
				AddName(ptr->recid,i);
-
 
840
			}
-
 
841
			else strncpy((isymbol+numsymbol)->N.sname,ptr->recid,i);
-
 
842
			if(ptr->rectok!=tk_apiproc&&ptr->rectok!=tk_undefproc&&(ptr->flag&f_extern)==0){
-
 
843
				(isymbol+numsymbol)->Value=ptr->recnumber;
-
 
844
				if(ptr->rectok==tk_proc||ptr->rectok==tk_interruptproc)(isymbol+numsymbol)->SectionNumber=(short)(textnum+1);
-
 
845
				else (isymbol+numsymbol)->SectionNumber=(short)((ptr->recpost==0?textnum:bssnum)+1);
-
 
846
			}
-
 
847
			else{
-
 
848
				(NameId+numextern)->num=ptr->recnumber;
-
 
849
				(NameId+numextern)->id=numsymbol;
-
 
850
				numextern++;
-
 
851
				if(numextern>=maxnumnameid){
-
 
852
					maxnumnameid+=MAXNUMSYMBOL;
-
 
853
					NameId=(NAMEID *)REALLOC(NameId,maxnumnameid*sizeof(NAMEID));
-
 
854
				}
-
 
855
			}
-
 
856
			(isymbol+numsymbol)->StorageClass=2;
-
 
857
			numsymbol++;
-
 
858
		}
-
 
859
		CreatSymbolTable(ptr->left);
-
 
860
	}
-
 
861
}
-
 
862
 
-
 
863
void FreeCoffBuf()
-
 
864
{
-
 
865
	free(ListName);
-
 
866
	free(isymbol);
-
 
867
	free(NameId);
-
 
868
	free(treloc);
-
 
869
	fclose(hout);
-
 
870
	hout=NULL;
-
 
871
}
-
 
872
 
-
 
873
void IncReloc()
-
 
874
{
-
 
875
	numreloc++;
-
 
876
	if(numreloc>=maxnumreloc){
-
 
877
		maxnumreloc+=MAXNUMRELOC;
-
 
878
		treloc=(IMAGE_RELOCATION *)REALLOC(treloc,maxnumreloc*sizeof(IMAGE_RELOCATION));
-
 
879
	}
-
 
880
}
-
 
881
 
-
 
882
void CreatRelocTable()
-
 
883
{
-
 
884
	for(int j=0;j
-
 
885
 		(treloc+numreloc)->VirtualAddress=(postbuf+j)->loc-startptr;
-
 
886
		if((postbuf+j)->type>=CALL_32I&&(postbuf+j)->type<=FIX_CODE32){
-
 
887
			switch((postbuf+j)->type){
-
 
888
				case POST_VAR:
-
 
889
				case POST_VAR32:
-
 
890
					(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;
-
 
891
					(treloc+numreloc)->SymbolTableIndex=segbss;
-
 
892
					break;
-
 
893
				case FIX_VAR:
-
 
894
				case FIX_VAR32:
-
 
895
				case FIX_CODE:
-
 
896
				case FIX_CODE32:
-
 
897
					(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;
-
 
898
					(treloc+numreloc)->SymbolTableIndex=segtext;
-
 
899
					break;
-
 
900
			}
-
 
901
			IncReloc();
-
 
902
		}
-
 
903
		else{
-
 
904
			for(int i=0;i
-
 
905
				if((NameId+i)->num==(postbuf+j)->num){
-
 
906
					if((postbuf+j)->type==EXT_VAR)(treloc+numreloc)->Type=IMAGE_REL_I386_DIR32;
-
 
907
					else (treloc+numreloc)->Type=IMAGE_REL_I386_REL32;
-
 
908
					(treloc+numreloc)->SymbolTableIndex=(NameId+i)->id;
-
 
909
					IncReloc();
-
 
910
					break;
-
 
911
				}
-
 
912
			}
-
 
913
		}
-
 
914
 
-
 
915
	}
-
 
916
}
-
 
917
 
-
 
918
int MakeCoff()
-
 
919
{
-
 
920
COFF_HEADER chead;
-
 
921
unsigned long sizehead,curobj,resnum,numresrel,segres,lastoffset,headernum;
-
 
922
OBJECT_ENTRY *objentry;
-
 
923
int i;
-
 
924
LISTRELOC *resrel=NULL;
-
 
925
char *codesecname;
-
 
926
	hout=CreateOutPut("obj","wb");
-
 
927
	chead.cpu=0x14c;
-
 
928
	chead.SizeOfOptionalHeader=0;
-
 
929
	chead.date_time=0;
-
 
930
	chead.Characteristics=0x100;
-
 
931
	/*if(header)*/numrs=2;
-
 
932
//¯®¤áç¨â âì ç¨á«® ᥪ権
-
 
933
	if(wbss){
-
 
934
		if(postsize)numrs++;
-
 
935
		else wbss=FALSE;
-
 
936
	}
-
 
937
	if(numres)numrs++;	//à¥áãàáë
-
 
938
	chead.numobj=numrs;
-
 
939
	sizehead=numrs*sizeof(OBJECT_ENTRY);
-
 
940
	objentry=(OBJECT_ENTRY *)MALLOC(sizehead);//â ¢«¨æ  ®¡ê¥ªâ®¢
-
 
941
	memset(objentry,0,sizehead);//®ç¨áâ¨âì â ¡«¨æã ®¡ê¥ªâ®¢
-
 
942
	curobj=0;
-
 
943
	lastoffset=sizehead+sizeof(COFF_HEADER);
-
 
944
//	if(header){
-
 
945
		strcpy((objentry+curobj)->name,".version");
-
 
946
		sprintf(&stub[STRVERS],"%s%s",compilerstr,__DATE__);
-
 
947
		(objentry+curobj)->psize=strlen(&stub[STRVERS])+1;
-
 
948
		(objentry+curobj)->pOffset=lastoffset;
-
 
949
		(objentry+curobj)->flags=0x100A00;
-
 
950
		headernum=curobj;
-
 
951
		lastoffset+=(objentry+curobj)->psize;
-
 
952
		curobj++;
-
 
953
//	}
-
 
954
	codesecname=".text";
-
 
955
	if(splitdata==FALSE)codesecname=".codedat";
-
 
956
	strcpy((objentry+curobj)->name,codesecname);
-
 
957
	(objentry+curobj)->psize=outptr;
-
 
958
	(objentry+curobj)->pOffset=lastoffset;
-
 
959
	(objentry+curobj)->flags=0xE0300060;
-
 
960
	lastoffset+=outptr;
-
 
961
	textnum=curobj;
-
 
962
	curobj++;
-
 
963
	if(wbss){
-
 
964
		strcpy((objentry+curobj)->name,".bss");
-
 
965
		(objentry+curobj)->psize=postsize;
-
 
966
		(objentry+curobj)->flags=0xC0300080;
-
 
967
		bssnum=curobj;
-
 
968
		curobj++;
-
 
969
	}
-
 
970
	if(numres){
-
 
971
		strcpy((objentry+curobj)->name,".rsrc$01");
-
 
972
		numresrel=(objentry+curobj)->NumberOfRelocations=MakeRes(0,&resrel);
-
 
973
		(objentry+curobj)->psize=curposbuf;
-
 
974
		(objentry+curobj)->flags=0x40000040;
-
 
975
		resnum=curobj;
-
 
976
	}
-
 
977
	sizelistName=0;numsymbol=0;
-
 
978
	ListName=(char *)MALLOC(MAXLISTNAME);
-
 
979
	isymbol=(IMAGE_SYMBOL *)MALLOC(MAXSIZESYMBOL);
-
 
980
 	memset(isymbol,0,MAXSIZESYMBOL);	//®ç¨áâ¨âì ¥¥
-
 
981
	maxsizelistname=MAXLISTNAME;
-
 
982
	maxnumnameid=maxnumsymbol=MAXNUMSYMBOL;
-
 
983
	NameId=(NAMEID *)MALLOC(MAXSIZENAMEID);
-
 
984
	treloc=(IMAGE_RELOCATION *)MALLOC(sizeof(IMAGE_RELOCATION)*MAXNUMRELOC);
-
 
985
	maxnumreloc=MAXNUMRELOC;
-
 
986
	numreloc=0;
-
 
987
	strcpy(isymbol->N.sname,"@comp.id");
-
 
988
	isymbol->Value=0x141F8E;
-
 
989
	isymbol->SectionNumber=-1;
-
 
990
	isymbol->StorageClass=3;
-
 
991
	strcpy((isymbol+1)->N.sname,".file");
-
 
992
	(isymbol+1)->Value=1;
-
 
993
	(isymbol+1)->SectionNumber=-2;
-
 
994
	(isymbol+1)->StorageClass=0x67;
-
 
995
	i=(strlen(startfileinfo->filename)-1)/sizeof(IMAGE_SYMBOL)+1;
-
 
996
	(isymbol+1)->NumberOfAuxSymbols=i;
-
 
997
	strcpy((isymbol+2)->N.sname,startfileinfo->filename);
-
 
998
	numsymbol=i+2;
-
 
999
	segtext=numsymbol;
-
 
1000
	strcpy((isymbol+numsymbol)->N.sname,codesecname);
-
 
1001
	(isymbol+numsymbol)->SectionNumber=textnum+1;
-
 
1002
	(isymbol+numsymbol)->StorageClass=3;
-
 
1003
	(isymbol+numsymbol)->NumberOfAuxSymbols=1;
-
 
1004
	numsymbol++;
-
 
1005
	(isymbol+numsymbol)->N.Name.Short=outptr;
-
 
1006
	numsymbol++;
-
 
1007
	if(wbss){
-
 
1008
		segbss=numsymbol;
-
 
1009
		strcpy((isymbol+numsymbol)->N.sname,".bss");
-
 
1010
		(isymbol+numsymbol)->SectionNumber=bssnum+1;
-
 
1011
		(isymbol+numsymbol)->StorageClass=3;
-
 
1012
		(isymbol+numsymbol)->NumberOfAuxSymbols=1;
-
 
1013
		numsymbol++;
-
 
1014
		(isymbol+numsymbol)->N.Name.Short=postsize;
-
 
1015
		numsymbol++;
-
 
1016
		strcpy((isymbol+numsymbol)->N.sname,"DGROUP");
-
 
1017
		(isymbol+numsymbol)->SectionNumber=bssnum+1;
-
 
1018
		(isymbol+numsymbol)->StorageClass=3;
-
 
1019
	}
-
 
1020
	strcpy((isymbol+numsymbol)->N.sname,"FLAT");
-
 
1021
	(isymbol+numsymbol)->SectionNumber=-1;
-
 
1022
	(isymbol+numsymbol)->StorageClass=3;
-
 
1023
	numsymbol++;
-
 
1024
	if(numres){
-
 
1025
		segres=numsymbol;
-
 
1026
		strcpy((isymbol+numsymbol)->N.sname,".rsrc$01");
-
 
1027
		(isymbol+numsymbol)->StorageClass=3;
-
 
1028
		(isymbol+numsymbol)->SectionNumber=resnum+1;
-
 
1029
		numsymbol++;
-
 
1030
	}
-
 
1031
//	if(header){
-
 
1032
		strcpy((isymbol+numsymbol)->N.sname,".version");
-
 
1033
		(isymbol+numsymbol)->SectionNumber=headernum+1;
-
 
1034
		(isymbol+numsymbol)->StorageClass=3;
-
 
1035
		numsymbol++;
-
 
1036
//	}
-
 
1037
	CreatSymbolTable(treestart);
-
 
1038
	CreatRelocTable();
-
 
1039
	(isymbol+segtext+1)->N.Name.Long=numreloc;
-
 
1040
	(objentry+textnum)->NumberOfRelocations=numreloc;
-
 
1041
	if(numreloc){
-
 
1042
		(objentry+textnum)->PointerToRelocations=lastoffset;
-
 
1043
		lastoffset+=sizeof(IMAGE_RELOCATION)*numreloc;
-
 
1044
	}
-
 
1045
	if(numres){
-
 
1046
		(objentry+resnum)->pOffset=lastoffset;
-
 
1047
		lastoffset+=curposbuf;
-
 
1048
		if(numresrel){
-
 
1049
			(objentry+resnum)->PointerToRelocations=lastoffset;
-
 
1050
			lastoffset+=sizeof(IMAGE_RELOCATION)*numresrel;
-
 
1051
		}
-
 
1052
	}
-
 
1053
	chead.COFFsize=numsymbol;
-
 
1054
	if(numsymbol){
-
 
1055
		chead.pCOFF=lastoffset;
-
 
1056
	}
-
 
1057
	if(fwrite(&chead,sizeof(COFF_HEADER),1,hout)!=1){
-
 
1058
errwrite:
-
 
1059
		ErrWrite();
-
 
1060
		free(objentry);
-
 
1061
		if(resrel)free(resrel);
-
 
1062
		FreeCoffBuf();
-
 
1063
		return(-1);
-
 
1064
	}
-
 
1065
	if(fwrite(objentry,sizehead,1,hout)!=1)goto errwrite;
-
 
1066
//	if(header){
-
 
1067
		if(fwrite(&stub[STRVERS],(objentry+headernum)->psize,1,hout)!=1)goto errwrite;
-
 
1068
//	}
-
 
1069
	if(fwrite(output,outptr,1,hout)!=1)goto errwrite;	//¡«®ª ª®¤ 
-
 
1070
	if(numreloc){
-
 
1071
		if(fwrite(treloc,numreloc*sizeof(IMAGE_RELOCATION),1,hout)!=1)goto errwrite;
-
 
1072
	}
-
 
1073
	if(numres){
-
 
1074
		if(fwrite(resbuf,curposbuf,1,hout)!=1)goto errwrite;
-
 
1075
		free(resbuf);
-
 
1076
		if(numresrel){
-
 
1077
			IMAGE_RELOCATION *rrel;
-
 
1078
			rrel=(IMAGE_RELOCATION *)MALLOC(sizeof(IMAGE_RELOCATION)*numresrel);
-
 
1079
			for(i=0;i
-
 
1080
		 		(rrel+i)->VirtualAddress=(resrel+i)->val;
-
 
1081
				(rrel+i)->Type=IMAGE_REL_I386_DIR32NB;
-
 
1082
				(rrel+i)->SymbolTableIndex=segres;
-
 
1083
			}
-
 
1084
			if(fwrite(rrel,sizeof(IMAGE_RELOCATION)*numresrel,1,hout)!=1)goto errwrite;
-
 
1085
			free(rrel);
-
 
1086
		}
-
 
1087
	}
-
 
1088
	if(numsymbol){
-
 
1089
		if(fwrite(isymbol,numsymbol*sizeof(IMAGE_SYMBOL),1,hout)!=1)goto errwrite;
-
 
1090
		if(sizelistName){
-
 
1091
			sizelistName+=4;
-
 
1092
			if(fwrite(&sizelistName,4,1,hout)!=1)goto errwrite;
-
 
1093
			if(fwrite(ListName,sizelistName-4,1,hout)!=1)goto errwrite;
-
 
1094
		}
-
 
1095
		else{
-
 
1096
			if(fwrite(&sizelistName,4,1,hout)!=1)goto errwrite;
-
 
1097
			sizelistName+=4;
-
 
1098
		}
-
 
1099
	}
-
 
1100
	runfilesize=lastoffset+sizelistName;
-
 
1101
	free(objentry);
-
 
1102
	if(resrel)free(resrel);
-
 
1103
	FreeCoffBuf();
-
 
1104
	return 0;
-
 
1105
}
-
 
1106
>
2