Subversion Repositories Kolibri OS

Rev

Rev 692 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 692 Rev 872
1
; flat assembler core
1
; flat assembler core
2
; Copyright (c) 1999-2007, Tomasz Grysztar.
2
; Copyright (c) 1999-2007, Tomasz Grysztar.
3
; All rights reserved.
3
; All rights reserved.
4
 
4
 
5
formatter:
5
formatter:
6
	cmp	[output_file],0
6
	cmp	[output_file],0
7
	jne	output_path_ok
7
	jne	output_path_ok
8
	push	edi
8
	push	edi
9
	mov	esi,[input_file]
9
	mov	esi,[input_file]
10
	mov	edi,[free_additional_memory]
10
	mov	edi,[free_additional_memory]
11
      copy_output_path:
11
      copy_output_path:
12
	lods	byte [esi]
12
	lods	byte [esi]
13
	cmp	edi,[structures_buffer]
13
	cmp	edi,[structures_buffer]
14
	jae	out_of_memory
14
	jae	out_of_memory
15
	stos	byte [edi]
15
	stos	byte [edi]
16
	or	al,al
16
	or	al,al
17
	jnz	copy_output_path
17
	jnz	copy_output_path
18
	dec	edi
18
	dec	edi
19
	mov	eax,edi
19
	mov	eax,edi
20
      find_extension:
20
      find_extension:
21
	dec	eax
21
	dec	eax
22
	cmp	eax,[free_additional_memory]
22
	cmp	eax,[free_additional_memory]
23
	jb	extension_found
23
	jb	extension_found
24
	cmp	byte [eax],'\'
24
	cmp	byte [eax],'\'
25
	je	extension_found
25
	je	extension_found
26
	cmp	byte [eax],'/'
26
	cmp	byte [eax],'/'
27
	je	extension_found
27
	je	extension_found
28
	cmp	byte [eax],'.'
28
	cmp	byte [eax],'.'
29
	jne	find_extension
29
	jne	find_extension
30
	mov	edi,eax
30
	mov	edi,eax
31
      extension_found:
31
      extension_found:
32
	lea	eax,[edi+9]
32
	lea	eax,[edi+9]
33
	cmp	eax,[structures_buffer]
33
	cmp	eax,[structures_buffer]
34
	jae	out_of_memory
34
	jae	out_of_memory
35
	cmp	[file_extension],0
35
	cmp	[file_extension],0
36
	jne	extension_specified
36
	jne	extension_specified
37
	cmp	[output_format],2
37
	cmp	[output_format],2
38
	je	exe_extension
38
	je	exe_extension
39
	jb	bin_extension
39
	jb	bin_extension
40
	cmp	[output_format],4
40
	cmp	[output_format],4
41
	je	obj_extension
41
	je	obj_extension
42
	cmp	[output_format],5
42
	cmp	[output_format],5
43
	je	o_extension
43
	je	o_extension
44
	cmp	[output_format],3
44
	cmp	[output_format],3
45
	jne	no_extension
45
	jne	no_extension
46
	cmp	[subsystem],1
46
	cmp	[subsystem],1
47
	je	sys_extension
47
	je	sys_extension
48
	bt	[format_flags],8
48
	bt	[format_flags],8
49
	jnc	exe_extension
49
	jnc	exe_extension
50
	mov	eax,'.dll'
50
	mov	eax,'.dll'
51
	jmp	make_extension
51
	jmp	make_extension
52
      sys_extension:
52
      sys_extension:
53
	mov	eax,'.sys'
53
	mov	eax,'.sys'
54
	jmp	make_extension
54
	jmp	make_extension
55
      bin_extension:
55
      bin_extension:
56
	mov	eax,'.bin'
56
	mov	eax,'.bin'
57
	bt	[format_flags],0
57
	bt	[format_flags],0
58
	jnc	make_extension
58
	jnc	make_extension
59
	mov	eax,'.com'
59
	mov	eax,'.com'
60
	jmp	make_extension
60
	jmp	make_extension
61
      obj_extension:
61
      obj_extension:
62
	mov	eax,'.obj'
62
	mov	eax,'.obj'
63
	jmp	make_extension
63
	jmp	make_extension
64
      o_extension:
64
      o_extension:
65
	mov	eax,'.o'
65
	mov	eax,'.o'
66
	bt	[format_flags],0
66
	bt	[format_flags],0
67
	jnc	make_extension
67
	jnc	make_extension
68
      no_extension:
68
      no_extension:
69
	xor	eax,eax
69
	xor	eax,eax
70
	jmp	make_extension
70
	jmp	make_extension
71
      exe_extension:
71
      exe_extension:
72
	mov	eax,'.exe'
72
	mov	eax,'.exe'
73
      make_extension:
73
      make_extension:
74
	xchg	eax,[edi]
74
	xchg	eax,[edi]
75
	scas	dword [edi]
75
	scas	dword [edi]
76
	mov	byte [edi],0
76
	mov	byte [edi],0
77
	scas	byte [edi]
77
	scas	byte [edi]
78
	mov	esi,edi
78
	mov	esi,edi
79
	stos	dword [edi]
79
	stos	dword [edi]
80
	sub	edi,9
80
	sub	edi,9
81
	xor	eax,eax
81
	xor	eax,eax
82
	mov	ebx,characters
82
	mov	ebx,characters
83
      adapt_case:
83
      adapt_case:
84
	mov	al,[esi]
84
	mov	al,[esi]
85
	or	al,al
85
	or	al,al
86
	jz	adapt_next
86
	jz	adapt_next
87
	xlat	byte [ebx]
87
	xlat	byte [ebx]
88
	cmp	al,[esi]
88
	cmp	al,[esi]
89
	je	adapt_ok
89
	je	adapt_ok
90
	sub	byte [edi],20h
90
	sub	byte [edi],20h
91
      adapt_ok:
91
      adapt_ok:
92
	inc	esi
92
	inc	esi
93
      adapt_next:
93
      adapt_next:
94
	inc	edi
94
	inc	edi
95
	cmp	byte [edi],0
95
	cmp	byte [edi],0
96
	jne	adapt_case
96
	jne	adapt_case
97
	jmp	extension_ok
97
	jmp	extension_ok
98
      extension_specified:
98
      extension_specified:
99
	mov	al,'.'
99
	mov	al,'.'
100
	stos	byte [edi]
100
	stos	byte [edi]
101
	mov	esi,[file_extension]
101
	mov	esi,[file_extension]
102
      copy_extension:
102
      copy_extension:
103
	lods	byte [esi]
103
	lods	byte [esi]
104
	stos	byte [edi]
104
	stos	byte [edi]
105
	test	al,al
105
	test	al,al
106
	jnz	copy_extension
106
	jnz	copy_extension
107
	dec	edi
107
	dec	edi
108
      extension_ok:
108
      extension_ok:
109
	mov	esi,edi
109
	mov	esi,edi
110
	lea	ecx,[esi+1]
110
	lea	ecx,[esi+1]
111
	sub	ecx,[free_additional_memory]
111
	sub	ecx,[free_additional_memory]
112
	mov	edi,[structures_buffer]
112
	mov	edi,[structures_buffer]
113
	dec	edi
113
	dec	edi
114
	std
114
	std
115
	rep	movs byte [edi],[esi]
115
	rep	movs byte [edi],[esi]
116
	cld
116
	cld
117
	inc	edi
117
	inc	edi
118
	mov	[structures_buffer],edi
118
	mov	[structures_buffer],edi
119
	mov	[output_file],edi
119
	mov	[output_file],edi
120
	pop	edi
120
	pop	edi
121
      output_path_ok:
121
      output_path_ok:
122
	cmp	[output_format],4
122
	cmp	[output_format],4
123
	je	coff_formatter
123
	je	coff_formatter
124
	cmp	[output_format],5
124
	cmp	[output_format],5
125
	jne	common_formatter
125
	jne	common_formatter
126
	bt	[format_flags],0
126
	bt	[format_flags],0
127
	jnc	elf_formatter
127
	jnc	elf_formatter
128
      common_formatter:
128
      common_formatter:
129
	mov	eax,edi
129
	mov	eax,edi
130
	sub	eax,[code_start]
130
	sub	eax,[code_start]
131
	mov	[real_code_size],eax
131
	mov	[real_code_size],eax
132
	cmp	edi,[undefined_data_end]
132
	cmp	edi,[undefined_data_end]
133
	jne	calculate_code_size
133
	jne	calculate_code_size
134
	mov	edi,[undefined_data_start]
134
	mov	edi,[undefined_data_start]
135
      calculate_code_size:
135
      calculate_code_size:
136
	sub	edi,[code_start]
136
	sub	edi,[code_start]
137
	mov	[code_size],edi
137
	mov	[code_size],edi
138
	mov	[written_size],0
138
	mov	[written_size],0
139
	mov	edx,[output_file]
139
	mov	edx,[output_file]
140
	call	create
140
	call	create
141
	jc	write_failed
141
	jc	write_failed
142
	cmp	[output_format],3
142
	cmp	[output_format],3
143
	jne	stub_written
143
	jne	stub_written
144
	mov	edx,[code_start]
144
	mov	edx,[code_start]
145
	mov	ecx,[stub_size]
145
	mov	ecx,[stub_size]
146
	sub	edx,ecx
146
	sub	edx,ecx
147
	add	[written_size],ecx
147
	add	[written_size],ecx
148
	call	write
148
	call	write
149
      stub_written:
149
      stub_written:
150
	cmp	[output_format],2
150
	cmp	[output_format],2
151
	jne	write_output
151
	jne	write_output
152
	call	write_mz_header
152
	call	write_mz_header
153
      write_output:
153
      write_output:
154
	call	write_code
154
	call	write_code
155
      output_written:
155
      output_written:
156
	call	close
156
	call	close
157
	ret
157
	ret
158
      write_code:
158
      write_code:
159
	mov	eax,[written_size]
159
	mov	eax,[written_size]
160
	mov	[headers_size],eax
160
	mov	[headers_size],eax
161
	mov	edx,[code_start]
161
	mov	edx,[code_start]
162
	mov	ecx,[code_size]
162
	mov	ecx,[code_size]
163
	add	[written_size],ecx
163
	add	[written_size],ecx
164
	call	write
164
	call	write
165
	jc	write_failed
165
	jc	write_failed
166
	ret
166
	ret
167
format_directive:
167
format_directive:
168
	cmp	edi,[code_start]
168
	cmp	edi,[code_start]
169
	jne	unexpected_instruction
169
	jne	unexpected_instruction
170
	cmp	[virtual_data],0
170
	cmp	[virtual_data],0
171
	jne	unexpected_instruction
171
	jne	unexpected_instruction
172
	cmp	[output_format],0
172
	cmp	[output_format],0
173
	jne	unexpected_instruction
173
	jne	unexpected_instruction
174
	lods	byte [esi]
174
	lods	byte [esi]
175
	cmp	al,17h
175
	cmp	al,17h
176
	je	format_prefix
176
	je	format_prefix
177
	cmp	al,18h
177
	cmp	al,18h
178
	jne	invalid_argument
178
	jne	invalid_argument
179
	lods	byte [esi]
179
	lods	byte [esi]
180
      select_format:
180
      select_format:
181
	mov	dl,al
181
	mov	dl,al
182
	shr	al,4
182
	shr	al,4
183
	mov	[output_format],al
183
	mov	[output_format],al
184
	and	edx,0Fh
184
	and	edx,0Fh
185
	or	[format_flags],edx
185
	or	[format_flags],edx
186
	cmp	al,2
186
	cmp	al,2
187
	je	format_mz
187
	je	format_mz
188
	cmp	al,3
188
	cmp	al,3
189
	je	format_pe
189
	je	format_pe
190
	cmp	al,4
190
	cmp	al,4
191
	je	format_coff
191
	je	format_coff
192
	cmp	al,5
192
	cmp	al,5
193
	je	format_elf
193
	je	format_elf
194
      format_defined:
194
      format_defined:
195
	cmp	byte [esi],86h
195
	cmp	byte [esi],86h
196
	jne	instruction_assembled
196
	jne	instruction_assembled
197
	cmp	word [esi+1],'('
197
	cmp	word [esi+1],'('
198
	jne	invalid_argument
198
	jne	invalid_argument
199
	mov	eax,[esi+3]
199
	mov	eax,[esi+3]
200
	add	esi,3+4
200
	add	esi,3+4
201
	mov	[file_extension],esi
201
	mov	[file_extension],esi
202
	lea	esi,[esi+eax+1]
202
	lea	esi,[esi+eax+1]
203
	jmp	instruction_assembled
203
	jmp	instruction_assembled
204
      format_prefix:
204
      format_prefix:
205
	lods	byte [esi]
205
	lods	byte [esi]
206
	mov	ah,al
206
	mov	ah,al
207
	lods	byte [esi]
207
	lods	byte [esi]
208
	cmp	al,18h
208
	cmp	al,18h
209
	jne	invalid_argument
209
	jne	invalid_argument
210
	lods	byte [esi]
210
	lods	byte [esi]
211
	mov	edx,eax
211
	mov	edx,eax
212
	shr	dl,4
212
	shr	dl,4
213
	shr	dh,4
213
	shr	dh,4
214
	cmp	dl,dh
214
	cmp	dl,dh
215
	jne	invalid_argument
215
	jne	invalid_argument
216
	or	al,ah
216
	or	al,ah
217
	jmp	select_format
217
	jmp	select_format
218
entry_directive:
218
entry_directive:
219
	bts	[format_flags],10h
219
	bts	[format_flags],10h
220
	jc	setting_already_specified
220
	jc	setting_already_specified
221
	mov	al,[output_format]
221
	mov	al,[output_format]
222
	cmp	al,2
222
	cmp	al,2
223
	je	mz_entry
223
	je	mz_entry
224
	cmp	al,3
224
	cmp	al,3
225
	je	pe_entry
225
	je	pe_entry
226
	cmp	al,5
226
	cmp	al,5
227
	jne	illegal_instruction
227
	jne	illegal_instruction
228
	bt	[format_flags],0
228
	bt	[format_flags],0
229
	jc	elf_entry
229
	jc	elf_entry
230
	jmp	illegal_instruction
230
	jmp	illegal_instruction
231
stack_directive:
231
stack_directive:
232
	bts	[format_flags],11h
232
	bts	[format_flags],11h
233
	jc	setting_already_specified
233
	jc	setting_already_specified
234
	mov	al,[output_format]
234
	mov	al,[output_format]
235
	cmp	al,2
235
	cmp	al,2
236
	je	mz_stack
236
	je	mz_stack
237
	cmp	al,3
237
	cmp	al,3
238
	je	pe_stack
238
	je	pe_stack
239
	jmp	illegal_instruction
239
	jmp	illegal_instruction
240
heap_directive:
240
heap_directive:
241
	bts	[format_flags],12h
241
	bts	[format_flags],12h
242
	jc	setting_already_specified
242
	jc	setting_already_specified
243
	mov	al,[output_format]
243
	mov	al,[output_format]
244
	cmp	al,2
244
	cmp	al,2
245
	je	mz_heap
245
	je	mz_heap
246
	cmp	al,3
246
	cmp	al,3
247
	je	pe_heap
247
	je	pe_heap
248
	jmp	illegal_instruction
248
	jmp	illegal_instruction
249
segment_directive:
249
segment_directive:
250
	cmp	[virtual_data],0
250
	cmp	[virtual_data],0
251
	jne	illegal_instruction
251
	jne	illegal_instruction
252
	mov	al,[output_format]
252
	mov	al,[output_format]
253
	cmp	al,2
253
	cmp	al,2
254
	je	mz_segment
254
	je	mz_segment
255
	cmp	al,5
255
	cmp	al,5
256
	je	elf_segment
256
	je	elf_segment
257
	jmp	illegal_instruction
257
	jmp	illegal_instruction
258
section_directive:
258
section_directive:
259
	cmp	[virtual_data],0
259
	cmp	[virtual_data],0
260
	jne	illegal_instruction
260
	jne	illegal_instruction
261
	mov	al,[output_format]
261
	mov	al,[output_format]
262
	cmp	al,3
262
	cmp	al,3
263
	je	pe_section
263
	je	pe_section
264
	cmp	al,4
264
	cmp	al,4
265
	je	coff_section
265
	je	coff_section
266
	cmp	al,5
266
	cmp	al,5
267
	je	elf_section
267
	je	elf_section
268
	jmp	illegal_instruction
268
	jmp	illegal_instruction
269
public_directive:
269
public_directive:
270
	mov	al,[output_format]
270
	mov	al,[output_format]
271
	cmp	al,4
271
	cmp	al,4
272
	je	public_allowed
272
	je	public_allowed
273
	cmp	al,5
273
	cmp	al,5
274
	jne	illegal_instruction
274
	jne	illegal_instruction
275
	bt	[format_flags],0
275
	bt	[format_flags],0
276
	jc	illegal_instruction
276
	jc	illegal_instruction
277
      public_allowed:
277
      public_allowed:
278
	mov	[base_code],0C0h
278
	mov	[base_code],0C0h
279
	lods	byte [esi]
279
	lods	byte [esi]
280
	cmp	al,2
280
	cmp	al,2
281
	je	public_label
281
	je	public_label
282
	cmp	al,1Dh
282
	cmp	al,1Dh
283
	jne	invalid_argument
283
	jne	invalid_argument
284
	lods	byte [esi]
284
	lods	byte [esi]
285
	and	al,7
285
	and	al,7
286
	add	[base_code],al
286
	add	[base_code],al
287
	lods	byte [esi]
287
	lods	byte [esi]
288
	cmp	al,2
288
	cmp	al,2
289
	jne	invalid_argument
289
	jne	invalid_argument
290
      public_label:
290
      public_label:
291
	lods	dword [esi]
291
	lods	dword [esi]
292
	cmp	eax,0Fh
292
	cmp	eax,0Fh
293
	jb	invalid_use_of_symbol
293
	jb	invalid_use_of_symbol
294
	je	reserved_word_used_as_symbol
294
	je	reserved_word_used_as_symbol
295
	mov	dx,[current_pass]
295
	mov	dx,[current_pass]
296
	mov	[eax+18],dx
296
	mov	[eax+18],dx
297
	or	byte [eax+8],8
297
	or	byte [eax+8],8
298
	inc	esi
298
	inc	esi
299
	mov	ebx,[free_additional_memory]
299
	mov	ebx,[free_additional_memory]
300
	lea	edx,[ebx+10h]
300
	lea	edx,[ebx+10h]
301
	cmp	edx,[structures_buffer]
301
	cmp	edx,[structures_buffer]
302
	jae	out_of_memory
302
	jae	out_of_memory
303
	mov	[free_additional_memory],edx
303
	mov	[free_additional_memory],edx
304
	mov	[ebx+8],eax
304
	mov	[ebx+8],eax
305
	mov	eax,[current_line]
305
	mov	eax,[current_line]
306
	mov	[ebx+0Ch],eax
306
	mov	[ebx+0Ch],eax
307
	lods	byte [esi]
307
	lods	byte [esi]
308
	cmp	al,86h
308
	cmp	al,86h
309
	jne	invalid_argument
309
	jne	invalid_argument
310
	lods	word [esi]
310
	lods	word [esi]
311
	cmp	ax,'('
311
	cmp	ax,'('
312
	jne	invalid_argument
312
	jne	invalid_argument
313
	mov	[ebx+4],esi
313
	mov	[ebx+4],esi
314
	lods	dword [esi]
314
	lods	dword [esi]
315
	lea	esi,[esi+eax+1]
315
	lea	esi,[esi+eax+1]
316
	mov	al,[base_code]
316
	mov	al,[base_code]
317
	mov	[ebx],al
317
	mov	[ebx],al
318
	jmp	instruction_assembled
318
	jmp	instruction_assembled
319
extrn_directive:
319
extrn_directive:
320
	mov	al,[output_format]
320
	mov	al,[output_format]
321
	cmp	al,4
321
	cmp	al,4
322
	je	extrn_allowed
322
	je	extrn_allowed
323
	cmp	al,5
323
	cmp	al,5
324
	jne	illegal_instruction
324
	jne	illegal_instruction
325
	bt	[format_flags],0
325
	bt	[format_flags],0
326
	jc	illegal_instruction
326
	jc	illegal_instruction
327
      extrn_allowed:
327
      extrn_allowed:
328
	lods	word [esi]
328
	lods	word [esi]
329
	cmp	ax,'('
329
	cmp	ax,'('
330
	jne	invalid_argument
330
	jne	invalid_argument
331
	mov	ebx,esi
331
	mov	ebx,esi
332
	lods	dword [esi]
332
	lods	dword [esi]
333
	lea	esi,[esi+eax+1]
333
	lea	esi,[esi+eax+1]
334
	mov	edx,[free_additional_memory]
334
	mov	edx,[free_additional_memory]
335
	lea	eax,[edx+0Ch]
335
	lea	eax,[edx+0Ch]
336
	cmp	eax,[structures_buffer]
336
	cmp	eax,[structures_buffer]
337
	jae	out_of_memory
337
	jae	out_of_memory
338
	mov	[free_additional_memory],eax
338
	mov	[free_additional_memory],eax
339
	mov	byte [edx],80h
339
	mov	byte [edx],80h
340
	mov	[edx+4],ebx
340
	mov	[edx+4],ebx
341
	lods	byte [esi]
341
	lods	byte [esi]
342
	cmp	al,86h
342
	cmp	al,86h
343
	jne	invalid_argument
343
	jne	invalid_argument
344
	lods	byte [esi]
344
	lods	byte [esi]
345
	cmp	al,2
345
	cmp	al,2
346
	jne	invalid_argument
346
	jne	invalid_argument
347
	lods	dword [esi]
347
	lods	dword [esi]
348
	cmp	eax,0Fh
348
	cmp	eax,0Fh
349
	jb	invalid_use_of_symbol
349
	jb	invalid_use_of_symbol
350
	je	reserved_word_used_as_symbol
350
	je	reserved_word_used_as_symbol
351
	inc	esi
351
	inc	esi
352
	mov	ebx,eax
352
	mov	ebx,eax
353
	xor	ah,ah
353
	xor	ah,ah
354
	lods	byte [esi]
354
	lods	byte [esi]
355
	cmp	al,':'
355
	cmp	al,':'
356
	je	get_extrn_size
356
	je	get_extrn_size
357
	dec	esi
357
	dec	esi
358
	cmp	al,11h
358
	cmp	al,11h
359
	jne	extrn_size_ok
359
	jne	extrn_size_ok
360
      get_extrn_size:
360
      get_extrn_size:
361
	lods	word [esi]
361
	lods	word [esi]
362
	cmp	al,11h
362
	cmp	al,11h
363
	jne	invalid_argument
363
	jne	invalid_argument
364
      extrn_size_ok:
364
      extrn_size_ok:
365
	mov	[address_symbol],edx
365
	mov	[address_symbol],edx
366
	movzx	ecx,ah
366
	movzx	ecx,ah
367
	mov	[edx+8],ecx
367
	mov	[edx+8],ecx
368
	xor	eax,eax
368
	xor	eax,eax
369
	xor	edx,edx
369
	xor	edx,edx
370
	xor	ebp,ebp
370
	xor	ebp,ebp
371
	mov	ch,2
371
	mov	ch,2
372
	test	[format_flags],8
372
	test	[format_flags],8
373
	jz	make_free_label
373
	jz	make_free_label
374
	mov	ch,4
374
	mov	ch,4
375
	jmp	make_free_label
375
	jmp	make_free_label
376
mark_relocation:
376
mark_relocation:
377
	cmp	[value_type],0
377
	cmp	[value_type],0
378
	je	relocation_ok
378
	je	relocation_ok
379
	cmp	[virtual_data],0
379
	cmp	[virtual_data],0
380
	jne	relocation_ok
380
	jne	relocation_ok
381
	cmp	[output_format],2
381
	cmp	[output_format],2
382
	je	mark_mz_relocation
382
	je	mark_mz_relocation
383
	cmp	[output_format],3
383
	cmp	[output_format],3
384
	je	mark_pe_relocation
384
	je	mark_pe_relocation
385
	cmp	[output_format],4
385
	cmp	[output_format],4
386
	je	mark_coff_relocation
386
	je	mark_coff_relocation
387
	cmp	[output_format],5
387
	cmp	[output_format],5
388
	je	mark_elf_relocation
388
	je	mark_elf_relocation
389
      relocation_ok:
389
      relocation_ok:
390
	ret
390
	ret
391
close_pass:
391
close_pass:
392
	mov	al,[output_format]
392
	mov	al,[output_format]
393
	cmp	al,3
393
	cmp	al,3
394
	je	close_pe
394
	je	close_pe
395
	cmp	al,4
395
	cmp	al,4
396
	je	close_coff
396
	je	close_coff
397
	cmp	al,5
397
	cmp	al,5
398
	je	close_elf
398
	je	close_elf
399
	ret
399
	ret
400
 
400
 
401
format_mz:
401
format_mz:
402
	mov	edx,[additional_memory]
402
	mov	edx,[additional_memory]
403
	push	edi
403
	push	edi
404
	mov	edi,edx
404
	mov	edi,edx
405
	mov	ecx,1Ch shr 2
405
	mov	ecx,1Ch shr 2
406
	xor	eax,eax
406
	xor	eax,eax
407
	rep	stos dword [edi]
407
	rep	stos dword [edi]
408
	mov	[free_additional_memory],edi
408
	mov	[free_additional_memory],edi
409
	pop	edi
409
	pop	edi
410
	mov	word [edx+0Ch],0FFFFh
410
	mov	word [edx+0Ch],0FFFFh
411
	mov	word [edx+10h],1000h
411
	mov	word [edx+10h],1000h
412
	mov	[code_type],16
412
	mov	[code_type],16
413
	jmp	format_defined
413
	jmp	format_defined
414
mark_mz_relocation:
414
mark_mz_relocation:
415
	push	eax ebx
415
	push	eax ebx
416
	inc	[number_of_relocations]
416
	inc	[number_of_relocations]
417
	mov	ebx,[free_additional_memory]
417
	mov	ebx,[free_additional_memory]
418
	mov	eax,edi
418
	mov	eax,edi
419
	sub	eax,[code_start]
419
	sub	eax,[code_start]
420
	mov	[ebx],ax
420
	mov	[ebx],ax
421
	shr	eax,16
421
	shr	eax,16
422
	shl	ax,12
422
	shl	ax,12
423
	mov	[ebx+2],ax
423
	mov	[ebx+2],ax
424
	cmp	word [ebx],0FFFFh
424
	cmp	word [ebx],0FFFFh
425
	jne	mz_relocation_ok
425
	jne	mz_relocation_ok
426
	inc	word [ebx+2]
426
	inc	word [ebx+2]
427
	sub	word [ebx],10h
427
	sub	word [ebx],10h
428
      mz_relocation_ok:
428
      mz_relocation_ok:
429
	add	ebx,4
429
	add	ebx,4
430
	cmp	ebx,[structures_buffer]
430
	cmp	ebx,[structures_buffer]
431
	jae	out_of_memory
431
	jae	out_of_memory
432
	mov	[free_additional_memory],ebx
432
	mov	[free_additional_memory],ebx
433
	pop	ebx eax
433
	pop	ebx eax
434
	ret
434
	ret
435
mz_segment:
435
mz_segment:
436
	lods	byte [esi]
436
	lods	byte [esi]
437
	cmp	al,2
437
	cmp	al,2
438
	jne	invalid_argument
438
	jne	invalid_argument
439
	lods	dword [esi]
439
	lods	dword [esi]
440
	cmp	eax,0Fh
440
	cmp	eax,0Fh
441
	jb	invalid_use_of_symbol
441
	jb	invalid_use_of_symbol
442
	je	reserved_word_used_as_symbol
442
	je	reserved_word_used_as_symbol
443
	inc	esi
443
	inc	esi
444
	mov	ebx,eax
444
	mov	ebx,eax
445
	mov	eax,edi
445
	mov	eax,edi
446
	sub	eax,[code_start]
446
	sub	eax,[code_start]
447
	mov	ecx,0Fh
447
	mov	ecx,0Fh
448
	add	eax,0Fh
448
	add	eax,0Fh
449
	and	eax,1111b
449
	and	eax,1111b
450
	sub	ecx,eax
450
	sub	ecx,eax
451
	mov	edx,edi
451
	mov	edx,edi
452
	xor	al,al
452
	xor	al,al
453
	rep	stos byte [edi]
453
	rep	stos byte [edi]
454
	mov	dword [org_origin],edi
454
	mov	dword [org_origin],edi
455
	mov	dword [org_origin+4],0
455
	mov	dword [org_origin+4],0
456
	mov	[org_registers],0
456
	mov	[org_registers],0
457
	mov	[org_start],edi
457
	mov	[org_start],edi
458
	mov	eax,edx
458
	mov	eax,edx
459
	call	undefined_data
459
	call	undefined_data
460
	mov	eax,edi
460
	mov	eax,edi
461
	sub	eax,[code_start]
461
	sub	eax,[code_start]
462
	shr	eax,4
462
	shr	eax,4
463
	cmp	eax,10000h
463
	cmp	eax,10000h
464
	jae	value_out_of_range
464
	jae	value_out_of_range
465
	mov	edx,eax
465
	mov	edx,eax
466
	mov	al,16
466
	mov	al,16
467
	cmp	byte [esi],13h
467
	cmp	byte [esi],13h
468
	jne	segment_type_ok
468
	jne	segment_type_ok
469
	inc	esi
469
	inc	esi
470
	lods	byte [esi]
470
	lods	byte [esi]
471
      segment_type_ok:
471
      segment_type_ok:
472
	mov	[code_type],al
472
	mov	[code_type],al
473
	mov	eax,edx
473
	mov	eax,edx
474
	mov	cx,0100h
474
	mov	cx,0100h
475
	xor	edx,edx
475
	xor	edx,edx
476
	xor	ebp,ebp
476
	xor	ebp,ebp
477
	mov	[address_symbol],edx
477
	mov	[address_symbol],edx
478
	jmp	make_free_label
478
	jmp	make_free_label
479
mz_entry:
479
mz_entry:
480
	lods	byte [esi]
480
	lods	byte [esi]
481
	cmp	al,'('
481
	cmp	al,'('
482
	jne	invalid_argument
482
	jne	invalid_argument
483
	call	get_word_value
483
	call	get_word_value
484
	cmp	[value_type],1
484
	cmp	[value_type],1
485
	je	initial_cs_ok
485
	je	initial_cs_ok
486
	cmp	[error_line],0
486
	cmp	[error_line],0
487
	jne	initial_cs_ok
487
	jne	initial_cs_ok
488
	mov	eax,[current_line]
488
	mov	eax,[current_line]
489
	mov	[error_line],eax
489
	mov	[error_line],eax
490
	mov	[error],invalid_address
490
	mov	[error],invalid_address
491
      initial_cs_ok:
491
      initial_cs_ok:
492
	mov	edx,[additional_memory]
492
	mov	edx,[additional_memory]
493
	mov	[edx+16h],ax
493
	mov	[edx+16h],ax
494
	lods	byte [esi]
494
	lods	byte [esi]
495
	cmp	al,':'
495
	cmp	al,':'
496
	jne	invalid_argument
496
	jne	invalid_argument
497
	lods	byte [esi]
497
	lods	byte [esi]
498
	cmp	al,'('
498
	cmp	al,'('
499
	jne	invalid_argument
499
	jne	invalid_argument
500
	ja	invalid_address
500
	ja	invalid_address
501
	call	get_word_value
501
	call	get_word_value
502
	cmp	[value_type],0
502
	cmp	[value_type],0
503
	jne	invalid_use_of_symbol
503
	jne	invalid_use_of_symbol
504
	mov	edx,[additional_memory]
504
	mov	edx,[additional_memory]
505
	mov	[edx+14h],ax
505
	mov	[edx+14h],ax
506
	jmp	instruction_assembled
506
	jmp	instruction_assembled
507
mz_stack:
507
mz_stack:
508
	lods	byte [esi]
508
	lods	byte [esi]
509
	cmp	al,'('
509
	cmp	al,'('
510
	jne	invalid_argument
510
	jne	invalid_argument
511
	call	get_word_value
511
	call	get_word_value
512
	cmp	byte [esi],':'
512
	cmp	byte [esi],':'
513
	je	stack_pointer
513
	je	stack_pointer
514
	cmp	ax,10h
514
	cmp	ax,10h
515
	jb	invalid_value
515
	jb	invalid_value
516
	cmp	[value_type],0
516
	cmp	[value_type],0
517
	jne	invalid_use_of_symbol
517
	jne	invalid_use_of_symbol
518
	mov	edx,[additional_memory]
518
	mov	edx,[additional_memory]
519
	mov	[edx+10h],ax
519
	mov	[edx+10h],ax
520
	jmp	instruction_assembled
520
	jmp	instruction_assembled
521
      stack_pointer:
521
      stack_pointer:
522
	cmp	[value_type],1
522
	cmp	[value_type],1
523
	je	initial_ss_ok
523
	je	initial_ss_ok
524
	cmp	[error_line],0
524
	cmp	[error_line],0
525
	jne	initial_ss_ok
525
	jne	initial_ss_ok
526
	mov	eax,[current_line]
526
	mov	eax,[current_line]
527
	mov	[error_line],eax
527
	mov	[error_line],eax
528
	mov	[error],invalid_address
528
	mov	[error],invalid_address
529
      initial_ss_ok:
529
      initial_ss_ok:
530
	mov	edx,[additional_memory]
530
	mov	edx,[additional_memory]
531
	mov	[edx+0Eh],ax
531
	mov	[edx+0Eh],ax
532
	lods	byte [esi]
532
	lods	byte [esi]
533
	cmp	al,':'
533
	cmp	al,':'
534
	jne	invalid_argument
534
	jne	invalid_argument
535
	lods	byte [esi]
535
	lods	byte [esi]
536
	cmp	al,'('
536
	cmp	al,'('
537
	jne	invalid_argument
537
	jne	invalid_argument
538
	call	get_word_value
538
	call	get_word_value
539
	cmp	[value_type],0
539
	cmp	[value_type],0
540
	jne	invalid_use_of_symbol
540
	jne	invalid_use_of_symbol
541
	mov	edx,[additional_memory]
541
	mov	edx,[additional_memory]
542
	mov	[edx+10h],ax
542
	mov	[edx+10h],ax
543
	bts	[format_flags],4
543
	bts	[format_flags],4
544
	jmp	instruction_assembled
544
	jmp	instruction_assembled
545
mz_heap:
545
mz_heap:
546
	cmp	[output_format],2
546
	cmp	[output_format],2
547
	jne	illegal_instruction
547
	jne	illegal_instruction
548
	lods	byte [esi]
548
	lods	byte [esi]
549
	call	get_size_operator
549
	call	get_size_operator
550
	cmp	ah,1
550
	cmp	ah,1
551
	je	invalid_value
551
	je	invalid_value
552
	cmp	ah,2
552
	cmp	ah,2
553
	ja	invalid_value
553
	ja	invalid_value
554
	cmp	al,'('
554
	cmp	al,'('
555
	jne	invalid_argument
555
	jne	invalid_argument
556
	call	get_word_value
556
	call	get_word_value
557
	cmp	[value_type],0
557
	cmp	[value_type],0
558
	jne	invalid_use_of_symbol
558
	jne	invalid_use_of_symbol
559
	mov	edx,[additional_memory]
559
	mov	edx,[additional_memory]
560
	mov	[edx+0Ch],ax
560
	mov	[edx+0Ch],ax
561
	jmp	instruction_assembled
561
	jmp	instruction_assembled
562
write_mz_header:
562
write_mz_header:
563
	mov	edx,[additional_memory]
563
	mov	edx,[additional_memory]
564
	bt	[format_flags],4
564
	bt	[format_flags],4
565
	jc	mz_stack_ok
565
	jc	mz_stack_ok
566
	mov	eax,[real_code_size]
566
	mov	eax,[real_code_size]
567
	dec	eax
567
	dec	eax
568
	shr	eax,4
568
	shr	eax,4
569
	inc	eax
569
	inc	eax
570
	mov	[edx+0Eh],ax
570
	mov	[edx+0Eh],ax
571
	shl	eax,4
571
	shl	eax,4
572
	movzx	ecx,word [edx+10h]
572
	movzx	ecx,word [edx+10h]
573
	add	eax,ecx
573
	add	eax,ecx
574
	mov	[real_code_size],eax
574
	mov	[real_code_size],eax
575
      mz_stack_ok:
575
      mz_stack_ok:
576
	mov	edi,[free_additional_memory]
576
	mov	edi,[free_additional_memory]
577
	mov	eax,[number_of_relocations]
577
	mov	eax,[number_of_relocations]
578
	shl	eax,2
578
	shl	eax,2
579
	add	eax,1Ch
579
	add	eax,1Ch
580
	sub	edi,eax
580
	sub	edi,eax
581
	xchg	edi,[free_additional_memory]
581
	xchg	edi,[free_additional_memory]
582
	mov	ecx,0Fh
582
	mov	ecx,0Fh
583
	add	eax,0Fh
583
	add	eax,0Fh
584
	and	eax,1111b
584
	and	eax,1111b
585
	sub	ecx,eax
585
	sub	ecx,eax
586
	xor	al,al
586
	xor	al,al
587
	rep	stos byte [edi]
587
	rep	stos byte [edi]
588
	sub	edi,[free_additional_memory]
588
	sub	edi,[free_additional_memory]
589
	mov	ecx,edi
589
	mov	ecx,edi
590
	shr	edi,4
590
	shr	edi,4
591
	mov	word [edx],'MZ' 	; signature
591
	mov	word [edx],'MZ' 	; signature
592
	mov	[edx+8],di		; header size in paragraphs
592
	mov	[edx+8],di		; header size in paragraphs
593
	mov	eax,[number_of_relocations]
593
	mov	eax,[number_of_relocations]
594
	mov	[edx+6],ax		; number of relocation entries
594
	mov	[edx+6],ax		; number of relocation entries
595
	mov	eax,[code_size]
595
	mov	eax,[code_size]
596
	add	eax,ecx
596
	add	eax,ecx
597
	mov	esi,eax
597
	mov	esi,eax
598
	shr	esi,9
598
	shr	esi,9
599
	and	eax,1FFh
599
	and	eax,1FFh
600
	inc	si
600
	inc	si
601
	or	ax,ax
601
	or	ax,ax
602
	jnz	mz_size_ok
602
	jnz	mz_size_ok
603
	mov	ax,200h
603
	dec	si
604
	dec	si
-
 
605
      mz_size_ok:
604
      mz_size_ok:
606
	mov	[edx+2],ax		; number of bytes in last page
605
	mov	[edx+2],ax		; number of bytes in last page
607
	mov	[edx+4],si		; number of pages
606
	mov	[edx+4],si		; number of pages
608
	mov	eax,[real_code_size]
607
	mov	eax,[real_code_size]
609
	dec	eax
608
	dec	eax
610
	shr	eax,4
609
	shr	eax,4
611
	inc	eax
610
	inc	eax
612
	mov	esi,[code_size]
611
	mov	esi,[code_size]
613
	dec	esi
612
	dec	esi
614
	shr	esi,4
613
	shr	esi,4
615
	inc	esi
614
	inc	esi
616
	sub	eax,esi
615
	sub	eax,esi
617
	mov	[edx+0Ah],ax		; minimum memory in addition to code
616
	mov	[edx+0Ah],ax		; minimum memory in addition to code
618
	add	[edx+0Ch],ax		; maximum memory in addition to code
617
	add	[edx+0Ch],ax		; maximum memory in addition to code
619
	salc
618
	salc
620
	mov	ah,al
619
	mov	ah,al
621
	or	[edx+0Ch],ax
620
	or	[edx+0Ch],ax
622
	mov	word [edx+18h],1Ch	; offset of relocation table
621
	mov	word [edx+18h],1Ch	; offset of relocation table
623
	add	[written_size],ecx
622
	add	[written_size],ecx
624
	call	write
623
	call	write
625
	jc	write_failed
624
	jc	write_failed
626
	ret
625
	ret
627
 
626
 
628
make_stub:
627
make_stub:
629
	mov	[stub_file],edx
628
	mov	[stub_file],edx
630
	or	edx,edx
629
	or	edx,edx
631
	jnz	stub_from_file
630
	jnz	stub_from_file
632
	push	esi
631
	push	esi
633
	mov	edx,edi
632
	mov	edx,edi
634
	xor	eax,eax
633
	xor	eax,eax
635
	mov	ecx,20h
634
	mov	ecx,20h
636
	rep	stos dword [edi]
635
	rep	stos dword [edi]
637
	mov	eax,40h+default_stub_end-default_stub
636
	mov	eax,40h+default_stub_end-default_stub
638
	mov	cx,100h+default_stub_end-default_stub
637
	mov	cx,100h+default_stub_end-default_stub
639
	mov	word [edx],'MZ'
638
	mov	word [edx],'MZ'
640
	mov	word [edx+4],1
639
	mov	word [edx+4],1
641
	mov	word [edx+2],ax
640
	mov	word [edx+2],ax
642
	mov	word [edx+8],4
641
	mov	word [edx+8],4
643
	mov	word [edx+0Ah],10h
642
	mov	word [edx+0Ah],10h
644
	mov	word [edx+0Ch],0FFFFh
643
	mov	word [edx+0Ch],0FFFFh
645
	mov	word [edx+10h],cx
644
	mov	word [edx+10h],cx
646
	mov	word [edx+3Ch],ax
645
	mov	word [edx+3Ch],ax
647
	mov	word [edx+18h],40h
646
	mov	word [edx+18h],40h
648
	lea	edi,[edx+40h]
647
	lea	edi,[edx+40h]
649
	mov	esi,default_stub
648
	mov	esi,default_stub
650
	mov	ecx,default_stub_end-default_stub
649
	mov	ecx,default_stub_end-default_stub
651
	rep	movs byte [edi],[esi]
650
	rep	movs byte [edi],[esi]
652
	pop	esi
651
	pop	esi
653
	jmp	stub_ok
652
	jmp	stub_ok
654
      default_stub:
653
      default_stub:
655
	use16
654
	use16
656
	push	cs
655
	push	cs
657
	pop	ds
656
	pop	ds
658
	mov	dx,stub_message-default_stub
657
	mov	dx,stub_message-default_stub
659
	mov	ah,9
658
	mov	ah,9
660
	int	21h
659
	int	21h
661
	mov	ax,4C01h
660
	mov	ax,4C01h
662
	int	21h
661
	int	21h
663
      stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
662
      stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
664
	rq	1
663
	rq	1
665
      default_stub_end:
664
      default_stub_end:
666
	use32
665
	use32
667
      stub_from_file:
666
      stub_from_file:
668
	push	esi
667
	push	esi
669
	mov	esi,edx
668
	mov	esi,edx
670
	call	open_binary_file
669
	call	open_binary_file
671
	mov	edx,edi
670
	mov	edx,edi
672
	mov	ecx,1Ch
671
	mov	ecx,1Ch
673
	mov	esi,edx
672
	mov	esi,edx
674
	call	read
673
	call	read
675
	jc	binary_stub
674
	jc	binary_stub
676
	cmp	word [esi],'MZ'
675
	cmp	word [esi],'MZ'
677
	jne	binary_stub
676
	jne	binary_stub
678
	add	edi,1Ch
677
	add	edi,1Ch
679
	movzx	ecx,word [esi+6]
678
	movzx	ecx,word [esi+6]
680
	add	ecx,11b
679
	add	ecx,11b
681
	and	ecx,not 11b
680
	and	ecx,not 11b
682
	add	ecx,(40h-1Ch) shr 2
681
	add	ecx,(40h-1Ch) shr 2
683
	lea	eax,[edi+ecx*4]
682
	lea	eax,[edi+ecx*4]
684
	cmp	edi,[display_buffer]
683
	cmp	edi,[display_buffer]
685
	jae	out_of_memory
684
	jae	out_of_memory
686
	xor	eax,eax
685
	xor	eax,eax
687
	rep	stos dword [edi]
686
	rep	stos dword [edi]
688
	mov	edx,40h
687
	mov	edx,40h
689
	xchg	dx,[esi+18h]
688
	xchg	dx,[esi+18h]
690
	xor	al,al
689
	xor	al,al
691
	call	lseek
690
	call	lseek
692
	movzx	ecx,word [esi+6]
691
	movzx	ecx,word [esi+6]
693
	shl	ecx,2
692
	shl	ecx,2
694
	lea	edx,[esi+40h]
693
	lea	edx,[esi+40h]
695
	call	read
694
	call	read
696
	mov	edx,edi
695
	mov	edx,edi
697
	sub	edx,esi
696
	sub	edx,esi
698
	shr	edx,4
697
	shr	edx,4
699
	xchg	dx,[esi+8]
698
	xchg	dx,[esi+8]
700
	shl	edx,4
699
	shl	edx,4
701
	xor	al,al
700
	xor	al,al
702
	call	lseek
701
	call	lseek
703
	movzx	ecx,word [esi+4]
702
	movzx	ecx,word [esi+4]
704
	dec	ecx
703
	dec	ecx
705
	shl	ecx,9
704
	shl	ecx,9
706
	movzx	edx,word [esi+2]
705
	movzx	edx,word [esi+2]
707
	add	ecx,edx
706
	test	edx,edx
-
 
707
	jnz	stub_header_size_ok
-
 
708
	mov	dx,200h
-
 
709
     stub_header_size_ok:
-
 
710
	add	ecx,edx
708
	mov	edx,edi
711
	mov	edx,edi
709
	sub	ecx,eax
712
	sub	ecx,eax
710
	je	read_stub_code
713
	je	read_stub_code
711
	jb	stub_code_ok
714
	jb	stub_code_ok
712
	push	ecx
715
	push	ecx
713
	dec	ecx
716
	dec	ecx
714
	shr	ecx,3
717
	shr	ecx,3
715
	inc	ecx
718
	inc	ecx
716
	shl	ecx,1
719
	shl	ecx,1
717
	lea	eax,[edi+ecx*4]
720
	lea	eax,[edi+ecx*4]
718
	cmp	eax,[display_buffer]
721
	cmp	eax,[display_buffer]
719
	jae	out_of_memory
722
	jae	out_of_memory
720
	xor	eax,eax
723
	xor	eax,eax
721
	rep	stos dword [edi]
724
	rep	stos dword [edi]
722
	pop	ecx
725
	pop	ecx
723
     read_stub_code:
726
     read_stub_code:
724
	call	read
727
	call	read
725
     stub_code_ok:
728
     stub_code_ok:
726
	call	close
729
	call	close
727
	mov	edx,edi
730
	mov	edx,edi
728
	sub	edx,esi
731
	sub	edx,esi
729
	mov	ax,dx
732
	mov	ax,dx
730
	and	ax,1FFh
733
	and	ax,1FFh
731
	mov	[esi+2],ax
734
	mov	[esi+2],ax
732
	dec	edx
735
	dec	edx
733
	shr	edx,9
736
	shr	edx,9
734
	inc	edx
737
	inc	edx
735
	mov	[esi+4],dx
738
	mov	[esi+4],dx
736
	mov	eax,edi
739
	mov	eax,edi
737
	sub	eax,esi
740
	sub	eax,esi
738
	mov	[esi+3Ch],eax
741
	mov	[esi+3Ch],eax
739
	pop	esi
742
	pop	esi
740
      stub_ok:
743
      stub_ok:
741
	ret
744
	ret
742
      binary_stub:
745
      binary_stub:
743
	mov	esi,edi
746
	mov	esi,edi
744
	mov	ecx,40h shr 2
747
	mov	ecx,40h shr 2
745
	xor	eax,eax
748
	xor	eax,eax
746
	rep	stos dword [edi]
749
	rep	stos dword [edi]
747
	mov	al,2
750
	mov	al,2
748
	xor	edx,edx
751
	xor	edx,edx
749
	call	lseek
752
	call	lseek
750
	push	eax
753
	push	eax
751
	xor	al,al
754
	xor	al,al
752
	xor	edx,edx
755
	xor	edx,edx
753
	call	lseek
756
	call	lseek
754
	mov	ecx,[esp]
757
	mov	ecx,[esp]
755
	add	ecx,40h+111b
758
	add	ecx,40h+111b
756
	and	ecx,not 111b
759
	and	ecx,not 111b
757
	mov	ax,cx
760
	mov	ax,cx
758
	and	ax,1FFh
761
	and	ax,1FFh
759
	mov	[esi+2],ax
762
	mov	[esi+2],ax
760
	lea	eax,[ecx+1FFh]
763
	lea	eax,[ecx+1FFh]
761
	shr	eax,9
764
	shr	eax,9
762
	mov	[esi+4],ax
765
	mov	[esi+4],ax
763
	mov	[esi+3Ch],ecx
766
	mov	[esi+3Ch],ecx
764
	sub	ecx,40h
767
	sub	ecx,40h
765
	mov	eax,10000h
768
	mov	eax,10000h
766
	sub	eax,ecx
769
	sub	eax,ecx
767
	jbe	binary_heap_ok
770
	jbe	binary_heap_ok
768
	shr	eax,4
771
	shr	eax,4
769
	mov	[esi+0Ah],ax
772
	mov	[esi+0Ah],ax
770
      binary_heap_ok:
773
      binary_heap_ok:
771
	mov	word [esi],'MZ'
774
	mov	word [esi],'MZ'
772
	mov	word [esi+8],4
775
	mov	word [esi+8],4
773
	mov	ax,0FFFFh
776
	mov	ax,0FFFFh
774
	mov	[esi+0Ch],ax
777
	mov	[esi+0Ch],ax
775
	dec	ax
778
	dec	ax
776
	mov	[esi+10h],ax
779
	mov	[esi+10h],ax
777
	sub	ax,0Eh
780
	sub	ax,0Eh
778
	mov	[esi+0Eh],ax
781
	mov	[esi+0Eh],ax
779
	mov	[esi+16h],ax
782
	mov	[esi+16h],ax
780
	mov	word [esi+14h],100h
783
	mov	word [esi+14h],100h
781
	mov	word [esi+18h],40h
784
	mov	word [esi+18h],40h
782
	mov	eax,[display_buffer]
785
	mov	eax,[display_buffer]
783
	sub	eax,ecx
786
	sub	eax,ecx
784
	cmp	edi,eax
787
	cmp	edi,eax
785
	jae	out_of_memory
788
	jae	out_of_memory
786
	mov	edx,edi
789
	mov	edx,edi
787
	shr	ecx,2
790
	shr	ecx,2
788
	xor	eax,eax
791
	xor	eax,eax
789
	rep	stos dword [edi]
792
	rep	stos dword [edi]
790
	pop	ecx
793
	pop	ecx
791
	call	read
794
	call	read
792
	call	close
795
	call	close
793
	pop	esi
796
	pop	esi
794
	ret
797
	ret
795
 
798
 
796
format_pe:
799
format_pe:
797
	xor	edx,edx
800
	xor	edx,edx
798
	mov	[machine],14Ch
801
	mov	[machine],14Ch
799
	mov	[subsystem],3
802
	mov	[subsystem],3
800
	mov	[subsystem_version],3 + 10 shl 16
803
	mov	[subsystem_version],3 + 10 shl 16
801
	mov	[image_base],400000h
804
	mov	[image_base],400000h
802
	test	[format_flags],8
805
	test	[format_flags],8
803
	jz	pe_settings
806
	jz	pe_settings
804
	mov	[machine],8664h
807
	mov	[machine],8664h
805
	mov	[subsystem_version],5 + 0 shl 16
808
	mov	[subsystem_version],5 + 0 shl 16
806
	mov	[image_base_high],0
809
	mov	[image_base_high],0
807
      pe_settings:
810
      pe_settings:
808
	cmp	byte [esi],84h
811
	cmp	byte [esi],84h
809
	je	get_stub_name
812
	je	get_stub_name
810
	cmp	byte [esi],80h
813
	cmp	byte [esi],80h
811
	je	get_pe_base
814
	je	get_pe_base
812
	cmp	byte [esi],1Bh
815
	cmp	byte [esi],1Bh
813
	jne	pe_settings_ok
816
	jne	pe_settings_ok
814
	lods	byte [esi]
817
	lods	byte [esi]
815
	lods	byte [esi]
818
	lods	byte [esi]
816
	test	al,80h+40h
819
	test	al,80h+40h
817
	jz	subsystem_setting
820
	jz	subsystem_setting
818
	cmp	al,80h
821
	cmp	al,80h
819
	je	dll_flag
822
	je	dll_flag
820
	cmp	al,81h
823
	cmp	al,81h
821
	je	wdm_flag
824
	je	wdm_flag
822
	jmp	pe_settings
825
	jmp	pe_settings
823
      dll_flag:
826
      dll_flag:
824
	bts	[format_flags],8
827
	bts	[format_flags],8
825
	jc	setting_already_specified
828
	jc	setting_already_specified
826
	jmp	pe_settings
829
	jmp	pe_settings
827
      wdm_flag:
830
      wdm_flag:
828
	bts	[format_flags],9
831
	bts	[format_flags],9
829
	jc	setting_already_specified
832
	jc	setting_already_specified
830
	jmp	pe_settings
833
	jmp	pe_settings
831
      subsystem_setting:
834
      subsystem_setting:
832
	bts	[format_flags],7
835
	bts	[format_flags],7
833
	jc	setting_already_specified
836
	jc	setting_already_specified
834
	and	ax,3Fh
837
	and	ax,3Fh
835
	mov	[subsystem],ax
838
	mov	[subsystem],ax
836
	cmp	byte [esi],'('
839
	cmp	byte [esi],'('
837
	jne	pe_settings
840
	jne	pe_settings
838
	inc	esi
841
	inc	esi
839
	cmp	byte [esi],'.'
842
	cmp	byte [esi],'.'
840
	jne	invalid_value
843
	jne	invalid_value
841
	inc	esi
844
	inc	esi
842
	push	edx
845
	push	edx
843
	cmp	byte [esi+11],0
846
	cmp	byte [esi+11],0
844
	jne	invalid_value
847
	jne	invalid_value
845
	cmp	byte [esi+10],2
848
	cmp	byte [esi+10],2
846
	ja	invalid_value
849
	ja	invalid_value
847
	mov	dx,[esi+8]
850
	mov	dx,[esi+8]
848
	cmp	dx,8000h
851
	cmp	dx,8000h
849
	je	zero_version
852
	je	zero_version
850
	mov	eax,[esi+4]
853
	mov	eax,[esi+4]
851
	cmp	dx,7
854
	cmp	dx,7
852
	jg	invalid_value
855
	jg	invalid_value
853
	mov	cx,7
856
	mov	cx,7
854
	sub	cx,dx
857
	sub	cx,dx
855
	mov	eax,[esi+4]
858
	mov	eax,[esi+4]
856
	shr	eax,cl
859
	shr	eax,cl
857
	mov	ebx,eax
860
	mov	ebx,eax
858
	shr	ebx,24
861
	shr	ebx,24
859
	cmp	bl,100
862
	cmp	bl,100
860
	jae	invalid_value
863
	jae	invalid_value
861
	and	eax,0FFFFFFh
864
	and	eax,0FFFFFFh
862
	mov	ecx,100
865
	mov	ecx,100
863
	mul	ecx
866
	mul	ecx
864
	shrd	eax,edx,24
867
	shrd	eax,edx,24
865
	jnc	version_value_ok
868
	jnc	version_value_ok
866
	inc	eax
869
	inc	eax
867
      version_value_ok:
870
      version_value_ok:
868
	shl	eax,16
871
	shl	eax,16
869
	mov	ax,bx
872
	mov	ax,bx
870
	jmp	subsystem_version_ok
873
	jmp	subsystem_version_ok
871
      zero_version:
874
      zero_version:
872
	xor	eax,eax
875
	xor	eax,eax
873
      subsystem_version_ok:
876
      subsystem_version_ok:
874
	pop	edx
877
	pop	edx
875
	add	esi,13
878
	add	esi,13
876
	mov	[subsystem_version],eax
879
	mov	[subsystem_version],eax
877
	jmp	pe_settings
880
	jmp	pe_settings
878
      get_pe_base:
881
      get_pe_base:
879
	bts	[format_flags],10
882
	bts	[format_flags],10
880
	jc	setting_already_specified
883
	jc	setting_already_specified
881
	lods	word [esi]
884
	lods	word [esi]
882
	cmp	ah,'('
885
	cmp	ah,'('
883
	jne	invalid_argument
886
	jne	invalid_argument
884
	cmp	byte [esi],'.'
887
	cmp	byte [esi],'.'
885
	je	invalid_value
888
	je	invalid_value
886
	push	edx edi
889
	push	edx edi
887
	add	edi,[stub_size]
890
	add	edi,[stub_size]
888
	test	[format_flags],8
891
	test	[format_flags],8
889
	jnz	get_pe64_base
892
	jnz	get_pe64_base
890
	call	get_dword_value
893
	call	get_dword_value
891
	mov	[image_base],eax
894
	mov	[image_base],eax
892
	jmp	pe_base_ok
895
	jmp	pe_base_ok
893
      get_pe64_base:
896
      get_pe64_base:
894
	call	get_qword_value
897
	call	get_qword_value
895
	mov	[image_base],eax
898
	mov	[image_base],eax
896
	mov	[image_base_high],edx
899
	mov	[image_base_high],edx
897
      pe_base_ok:
900
      pe_base_ok:
898
	pop	edi edx
901
	pop	edi edx
899
	cmp	[value_type],0
902
	cmp	[value_type],0
900
	jne	invalid_use_of_symbol
903
	jne	invalid_use_of_symbol
901
	cmp	byte [esi],84h
904
	cmp	byte [esi],84h
902
	jne	pe_settings_ok
905
	jne	pe_settings_ok
903
      get_stub_name:
906
      get_stub_name:
904
	lods	byte [esi]
907
	lods	byte [esi]
905
	lods	word [esi]
908
	lods	word [esi]
906
	cmp	ax,'('
909
	cmp	ax,'('
907
	jne	invalid_argument
910
	jne	invalid_argument
908
	lods	dword [esi]
911
	lods	dword [esi]
909
	mov	edx,esi
912
	mov	edx,esi
910
	add	esi,eax
913
	add	esi,eax
911
	inc	esi
914
	inc	esi
912
      pe_settings_ok:
915
      pe_settings_ok:
913
	mov	ebp,[stub_size]
916
	mov	ebp,[stub_size]
914
	or	ebp,ebp
917
	or	ebp,ebp
915
	jz	make_pe_stub
918
	jz	make_pe_stub
916
	cmp	edx,[stub_file]
919
	cmp	edx,[stub_file]
917
	je	pe_stub_ok
920
	je	pe_stub_ok
918
	sub	edi,[stub_size]
921
	sub	edi,[stub_size]
919
	mov	[code_start],edi
922
	mov	[code_start],edi
920
      make_pe_stub:
923
      make_pe_stub:
921
	call	make_stub
924
	call	make_stub
922
	mov	eax,edi
925
	mov	eax,edi
923
	sub	eax,[code_start]
926
	sub	eax,[code_start]
924
	mov	[stub_size],eax
927
	mov	[stub_size],eax
925
	mov	[code_start],edi
928
	mov	[code_start],edi
926
	mov	ebp,eax
929
	mov	ebp,eax
927
      pe_stub_ok:
930
      pe_stub_ok:
928
	mov	edx,edi
931
	mov	edx,edi
929
	mov	ecx,18h+0E0h
932
	mov	ecx,18h+0E0h
930
	test	[format_flags],8
933
	test	[format_flags],8
931
	jz	zero_pe_header
934
	jz	zero_pe_header
932
	add	ecx,10h
935
	add	ecx,10h
933
      zero_pe_header:
936
      zero_pe_header:
934
	add	ebp,ecx
937
	add	ebp,ecx
935
	shr	ecx,2
938
	shr	ecx,2
936
	xor	eax,eax
939
	xor	eax,eax
937
	rep	stos dword [edi]
940
	rep	stos dword [edi]
938
	mov	word [edx],'PE' 	; signature
941
	mov	word [edx],'PE' 	; signature
939
	mov	ax,[machine]
942
	mov	ax,[machine]
940
	mov	word [edx+4],ax
943
	mov	word [edx+4],ax
941
	mov	dword [edx+38h],1000h	; section alignment
944
	mov	dword [edx+38h],1000h	; section alignment
942
	mov	dword [edx+3Ch],200h	; file alignment
945
	mov	dword [edx+3Ch],200h	; file alignment
943
	mov	word [edx+40h],1	; OS version
946
	mov	word [edx+40h],1	; OS version
944
	mov	eax,[subsystem_version]
947
	mov	eax,[subsystem_version]
945
	mov	[edx+48h],eax
948
	mov	[edx+48h],eax
946
	mov	ax,[subsystem]
949
	mov	ax,[subsystem]
947
	mov	[edx+5Ch],ax
950
	mov	[edx+5Ch],ax
948
	cmp	ax,1
951
	cmp	ax,1
949
	jne	pe_alignment_ok
952
	jne	pe_alignment_ok
950
	mov	eax,20h
953
	mov	eax,20h
951
	mov	dword [edx+38h],eax
954
	mov	dword [edx+38h],eax
952
	mov	dword [edx+3Ch],eax
955
	mov	dword [edx+3Ch],eax
953
      pe_alignment_ok:
956
      pe_alignment_ok:
954
	mov	word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
957
	mov	word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
955
	test	[format_flags],8
958
	test	[format_flags],8
956
	jnz	init_pe64_specific
959
	jnz	init_pe64_specific
957
	mov	dword [edx+14h],0E0h	; size of optional header
960
	mov	dword [edx+14h],0E0h	; size of optional header
958
	mov	dword [edx+16h],10B010Eh; flags and magic value
961
	mov	dword [edx+16h],10B010Eh; flags and magic value
959
	mov	eax,[image_base]
962
	mov	eax,[image_base]
960
	mov	dword [edx+34h],eax
963
	mov	dword [edx+34h],eax
961
	mov	dword [edx+60h],1000h	; stack reserve
964
	mov	dword [edx+60h],1000h	; stack reserve
962
	mov	dword [edx+64h],1000h	; stack commit
965
	mov	dword [edx+64h],1000h	; stack commit
963
	mov	dword [edx+68h],10000h	; heap reserve
966
	mov	dword [edx+68h],10000h	; heap reserve
964
	mov	dword [edx+6Ch],0	; heap commit
967
	mov	dword [edx+6Ch],0	; heap commit
965
	mov	dword [edx+74h],16	; number of directories
968
	mov	dword [edx+74h],16	; number of directories
966
	jmp	pe_header_ok
969
	jmp	pe_header_ok
967
      init_pe64_specific:
970
      init_pe64_specific:
968
	mov	dword [edx+14h],0F0h	; size of optional header
971
	mov	dword [edx+14h],0F0h	; size of optional header
969
	mov	dword [edx+16h],20B002Eh; flags and magic value
972
	mov	dword [edx+16h],20B002Eh; flags and magic value
970
	mov	eax,[image_base]
973
	mov	eax,[image_base]
971
	mov	dword [edx+30h],eax
974
	mov	dword [edx+30h],eax
972
	mov	eax,[image_base_high]
975
	mov	eax,[image_base_high]
973
	mov	dword [edx+34h],eax
976
	mov	dword [edx+34h],eax
974
	mov	dword [edx+60h],1000h	; stack reserve
977
	mov	dword [edx+60h],1000h	; stack reserve
975
	mov	dword [edx+68h],1000h	; stack commit
978
	mov	dword [edx+68h],1000h	; stack commit
976
	mov	dword [edx+70h],10000h	; heap reserve
979
	mov	dword [edx+70h],10000h	; heap reserve
977
	mov	dword [edx+78h],0	; heap commit
980
	mov	dword [edx+78h],0	; heap commit
978
	mov	dword [edx+84h],16	; number of directories
981
	mov	dword [edx+84h],16	; number of directories
979
      pe_header_ok:
982
      pe_header_ok:
980
	bsf	ecx,[edx+3Ch]
983
	bsf	ecx,[edx+3Ch]
981
	imul	ebx,[number_of_sections],28h
984
	imul	ebx,[number_of_sections],28h
982
	or	ebx,ebx
985
	or	ebx,ebx
983
	jnz	reserve_space_for_section_headers
986
	jnz	reserve_space_for_section_headers
984
	mov	ebx,28h
987
	mov	ebx,28h
985
      reserve_space_for_section_headers:
988
      reserve_space_for_section_headers:
986
	add	ebx,ebp
989
	add	ebx,ebp
987
	dec	ebx
990
	dec	ebx
988
	shr	ebx,cl
991
	shr	ebx,cl
989
	inc	ebx
992
	inc	ebx
990
	shl	ebx,cl
993
	shl	ebx,cl
991
	sub	ebx,ebp
994
	sub	ebx,ebp
992
	mov	ecx,ebx
995
	mov	ecx,ebx
993
	mov	eax,[display_buffer]
996
	mov	eax,[display_buffer]
994
	sub	eax,ecx
997
	sub	eax,ecx
995
	cmp	edi,eax
998
	cmp	edi,eax
996
	jae	out_of_memory
999
	jae	out_of_memory
997
	shr	ecx,2
1000
	shr	ecx,2
998
	xor	eax,eax
1001
	xor	eax,eax
999
	rep	stos dword [edi]
1002
	rep	stos dword [edi]
1000
	mov	eax,edi
1003
	mov	eax,edi
1001
	sub	eax,[code_start]
1004
	sub	eax,[code_start]
1002
	add	eax,[stub_size]
1005
	add	eax,[stub_size]
1003
	mov	[edx+54h],eax		; size of headers
1006
	mov	[edx+54h],eax		; size of headers
1004
	mov	ecx,[edx+38h]
1007
	mov	ecx,[edx+38h]
1005
	dec	ecx
1008
	dec	ecx
1006
	add	eax,ecx
1009
	add	eax,ecx
1007
	not	ecx
1010
	not	ecx
1008
	and	eax,ecx
1011
	and	eax,ecx
1009
	bt	[format_flags],8
1012
	bt	[format_flags],8
1010
	jc	pe_entry_init_ok
1013
	jc	pe_entry_init_ok
1011
	mov	[edx+28h],eax		; entry point rva
1014
	mov	[edx+28h],eax		; entry point rva
1012
      pe_entry_init_ok:
1015
      pe_entry_init_ok:
1013
	mov	[number_of_sections],0
1016
	mov	[number_of_sections],0
1014
	movzx	ebx,word [edx+14h]
1017
	movzx	ebx,word [edx+14h]
1015
	lea	ebx,[edx+18h+ebx]
1018
	lea	ebx,[edx+18h+ebx]
1016
	mov	[current_section],ebx
1019
	mov	[current_section],ebx
1017
	mov	dword [ebx],'.fla'
1020
	mov	dword [ebx],'.fla'
1018
	mov	dword [ebx+4],'t'
1021
	mov	dword [ebx+4],'t'
1019
	mov	[ebx+14h],edi
1022
	mov	[ebx+14h],edi
1020
	mov	[ebx+0Ch],eax
1023
	mov	[ebx+0Ch],eax
1021
	mov	dword [ebx+24h],0E0000060h
1024
	mov	dword [ebx+24h],0E0000060h
1022
	xor	ecx,ecx
1025
	xor	ecx,ecx
1023
	not	eax
1026
	not	eax
1024
	not	ecx
1027
	not	ecx
1025
	add	eax,1
1028
	add	eax,1
1026
	adc	ecx,0
1029
	adc	ecx,0
1027
	add	eax,edi
1030
	add	eax,edi
1028
	adc	ecx,0
1031
	adc	ecx,0
1029
	test	[format_flags],8
1032
	test	[format_flags],8
1030
	jnz	pe64_org
1033
	jnz	pe64_org
1031
	sub	eax,[edx+34h]
1034
	sub	eax,[edx+34h]
1032
	sbb	ecx,0
1035
	sbb	ecx,0
1033
	mov	bl,2
1036
	mov	bl,2
1034
	mov	[code_type],32
1037
	mov	[code_type],32
1035
	jmp	pe_org_ok
1038
	jmp	pe_org_ok
1036
      pe64_org:
1039
      pe64_org:
1037
	sub	eax,[edx+30h]
1040
	sub	eax,[edx+30h]
1038
	sbb	ecx,[edx+34h]
1041
	sbb	ecx,[edx+34h]
1039
	mov	bl,4
1042
	mov	bl,4
1040
	mov	[code_type],64
1043
	mov	[code_type],64
1041
      pe_org_ok:
1044
      pe_org_ok:
1042
	bt	[resolver_flags],0
1045
	bt	[resolver_flags],0
1043
	jc	pe_labels_type_ok
1046
	jc	pe_labels_type_ok
1044
	xor	bl,bl
1047
	xor	bl,bl
1045
      pe_labels_type_ok:
1048
      pe_labels_type_ok:
1046
	mov	[labels_type],bl
1049
	mov	[labels_type],bl
1047
	mov	dword [org_origin],eax
1050
	mov	dword [org_origin],eax
1048
	mov	dword [org_origin+4],ecx
1051
	mov	dword [org_origin+4],ecx
1049
	mov	[org_registers],0
1052
	mov	[org_registers],0
1050
	mov	[org_start],edi
1053
	mov	[org_start],edi
1051
	bt	[format_flags],8
1054
	bt	[format_flags],8
1052
	jnc	dll_flag_ok
1055
	jnc	dll_flag_ok
1053
	or	dword [edx+16h],2000h
1056
	or	dword [edx+16h],2000h
1054
      dll_flag_ok:
1057
      dll_flag_ok:
1055
	bt	[format_flags],9
1058
	bt	[format_flags],9
1056
	jnc	wdm_flag_ok
1059
	jnc	wdm_flag_ok
1057
	or	word [edx+5Eh],2000h
1060
	or	word [edx+5Eh],2000h
1058
      wdm_flag_ok:
1061
      wdm_flag_ok:
1059
	jmp	format_defined
1062
	jmp	format_defined
1060
pe_section:
1063
pe_section:
1061
	call	close_pe_section
1064
	call	close_pe_section
1062
	bts	[format_flags],5
1065
	bts	[format_flags],5
1063
	lea	ecx,[ebx+28h]
1066
	lea	ecx,[ebx+28h]
1064
	add	edx,[edx+54h]
1067
	add	edx,[edx+54h]
1065
	sub	edx,[stub_size]
1068
	sub	edx,[stub_size]
1066
	cmp	ecx,edx
1069
	cmp	ecx,edx
1067
	jbe	new_section
1070
	jbe	new_section
1068
	lea	ebx,[edx-28h]
1071
	lea	ebx,[edx-28h]
1069
	or	[next_pass_needed],-1
1072
	or	[next_pass_needed],-1
1070
	push	edi
1073
	push	edi
1071
	mov	edi,ebx
1074
	mov	edi,ebx
1072
	mov	ecx,28h shr 4
1075
	mov	ecx,28h shr 4
1073
	xor	eax,eax
1076
	xor	eax,eax
1074
	rep	stos dword [edi]
1077
	rep	stos dword [edi]
1075
	pop	edi
1078
	pop	edi
1076
      new_section:
1079
      new_section:
1077
	mov	[ebx+0Ch],eax
1080
	mov	[ebx+0Ch],eax
1078
	lods	word [esi]
1081
	lods	word [esi]
1079
	cmp	ax,'('
1082
	cmp	ax,'('
1080
	jne	invalid_argument
1083
	jne	invalid_argument
1081
	lea	edx,[esi+4]
1084
	lea	edx,[esi+4]
1082
	mov	ecx,[esi]
1085
	mov	ecx,[esi]
1083
	lea	esi,[esi+4+ecx+1]
1086
	lea	esi,[esi+4+ecx+1]
1084
	cmp	ecx,8
1087
	cmp	ecx,8
1085
	ja	name_too_long
1088
	ja	name_too_long
1086
	xor	eax,eax
1089
	xor	eax,eax
1087
	mov	[ebx],eax
1090
	mov	[ebx],eax
1088
	mov	[ebx+4],eax
1091
	mov	[ebx+4],eax
1089
	push	esi edi
1092
	push	esi edi
1090
	mov	edi,ebx
1093
	mov	edi,ebx
1091
	mov	esi,edx
1094
	mov	esi,edx
1092
	rep	movs byte [edi],[esi]
1095
	rep	movs byte [edi],[esi]
1093
	pop	edi esi
1096
	pop	edi esi
1094
	mov	dword [ebx+24h],0
1097
	mov	dword [ebx+24h],0
1095
	mov	[ebx+14h],edi
1098
	mov	[ebx+14h],edi
1096
	mov	edx,[code_start]
1099
	mov	edx,[code_start]
1097
	mov	eax,edi
1100
	mov	eax,edi
1098
	xor	ecx,ecx
1101
	xor	ecx,ecx
1099
	sub	eax,[ebx+0Ch]
1102
	sub	eax,[ebx+0Ch]
1100
	sbb	ecx,0
1103
	sbb	ecx,0
1101
	test	[format_flags],8
1104
	test	[format_flags],8
1102
	jnz	pe64_section_org
1105
	jnz	pe64_section_org
1103
	sub	eax,[edx+34h]
1106
	sub	eax,[edx+34h]
1104
	sbb	ecx,0
1107
	sbb	ecx,0
1105
	mov	[labels_type],2
1108
	mov	[labels_type],2
1106
	mov	[code_type],32
1109
	mov	[code_type],32
1107
	bt	[resolver_flags],0
1110
	bt	[resolver_flags],0
1108
	jc	pe_section_org_ok
1111
	jc	pe_section_org_ok
1109
	mov	[labels_type],0
1112
	mov	[labels_type],0
1110
	jmp	pe_section_org_ok
1113
	jmp	pe_section_org_ok
1111
      pe64_section_org:
1114
      pe64_section_org:
1112
	sub	eax,[edx+30h]
1115
	sub	eax,[edx+30h]
1113
	sbb	ecx,[edx+34h]
1116
	sbb	ecx,[edx+34h]
1114
	mov	[labels_type],4
1117
	mov	[labels_type],4
1115
	mov	[code_type],64
1118
	mov	[code_type],64
1116
	bt	[resolver_flags],0
1119
	bt	[resolver_flags],0
1117
	jc	pe_section_org_ok
1120
	jc	pe_section_org_ok
1118
	mov	[labels_type],0
1121
	mov	[labels_type],0
1119
      pe_section_org_ok:
1122
      pe_section_org_ok:
1120
	mov	dword [org_origin],eax
1123
	mov	dword [org_origin],eax
1121
	mov	dword [org_origin+4],ecx
1124
	mov	dword [org_origin+4],ecx
1122
	mov	[org_registers],0
1125
	mov	[org_registers],0
1123
	mov	[org_start],edi
1126
	mov	[org_start],edi
1124
      get_section_flags:
1127
      get_section_flags:
1125
	lods	byte [esi]
1128
	lods	byte [esi]
1126
	cmp	al,1Ah
1129
	cmp	al,1Ah
1127
	je	set_directory
1130
	je	set_directory
1128
	cmp	al,19h
1131
	cmp	al,19h
1129
	je	section_flag
1132
	je	section_flag
1130
	dec	esi
1133
	dec	esi
1131
	jmp	instruction_assembled
1134
	jmp	instruction_assembled
1132
      set_directory:
1135
      set_directory:
1133
	movzx	eax,byte [esi]
1136
	movzx	eax,byte [esi]
1134
	inc	esi
1137
	inc	esi
1135
	mov	ecx,ebx
1138
	mov	ecx,ebx
1136
	test	[format_flags],8
1139
	test	[format_flags],8
1137
	jnz	pe64_directory
1140
	jnz	pe64_directory
1138
	xchg	ecx,[edx+78h+eax*8]
1141
	xchg	ecx,[edx+78h+eax*8]
1139
	mov	dword [edx+78h+eax*8+4],-1
1142
	mov	dword [edx+78h+eax*8+4],-1
1140
	jmp	pe_directory_set
1143
	jmp	pe_directory_set
1141
      pe64_directory:
1144
      pe64_directory:
1142
	xchg	ecx,[edx+88h+eax*8]
1145
	xchg	ecx,[edx+88h+eax*8]
1143
	mov	dword [edx+88h+eax*8+4],-1
1146
	mov	dword [edx+88h+eax*8+4],-1
1144
      pe_directory_set:
1147
      pe_directory_set:
1145
	or	ecx,ecx
1148
	or	ecx,ecx
1146
	jnz	data_already_defined
1149
	jnz	data_already_defined
1147
	push	ebx edx
1150
	push	ebx edx
1148
	call	generate_pe_data
1151
	call	generate_pe_data
1149
	pop	edx ebx
1152
	pop	edx ebx
1150
	jmp	get_section_flags
1153
	jmp	get_section_flags
1151
      section_flag:
1154
      section_flag:
1152
	lods	byte [esi]
1155
	lods	byte [esi]
1153
	cmp	al,9
1156
	cmp	al,9
1154
	je	invalid_argument
1157
	je	invalid_argument
1155
	cmp	al,11
1158
	cmp	al,11
1156
	je	invalid_argument
1159
	je	invalid_argument
1157
	mov	cl,al
1160
	mov	cl,al
1158
	mov	eax,1
1161
	mov	eax,1
1159
	shl	eax,cl
1162
	shl	eax,cl
1160
	test	dword [ebx+24h],eax
1163
	test	dword [ebx+24h],eax
1161
	jnz	setting_already_specified
1164
	jnz	setting_already_specified
1162
	or	dword [ebx+24h],eax
1165
	or	dword [ebx+24h],eax
1163
	jmp	get_section_flags
1166
	jmp	get_section_flags
1164
      close_pe_section:
1167
      close_pe_section:
1165
	mov	ebx,[current_section]
1168
	mov	ebx,[current_section]
1166
	mov	edx,[code_start]
1169
	mov	edx,[code_start]
1167
	mov	eax,edi
1170
	mov	eax,edi
1168
	sub	eax,[ebx+14h]
1171
	sub	eax,[ebx+14h]
1169
	jnz	finish_section
1172
	jnz	finish_section
1170
	bt	[format_flags],5
1173
	bt	[format_flags],5
1171
	jc	finish_section
1174
	jc	finish_section
1172
	mov	eax,[ebx+0Ch]
1175
	mov	eax,[ebx+0Ch]
1173
	ret
1176
	ret
1174
      finish_section:
1177
      finish_section:
1175
	mov	[ebx+8],eax
1178
	mov	[ebx+8],eax
1176
	cmp	edi,[undefined_data_end]
1179
	cmp	edi,[undefined_data_end]
1177
	jne	align_section
1180
	jne	align_section
1178
	cmp	dword [edx+38h],1000h
1181
	cmp	dword [edx+38h],1000h
1179
	jb	align_section
1182
	jb	align_section
1180
	mov	edi,[undefined_data_start]
1183
	mov	edi,[undefined_data_start]
1181
      align_section:
1184
      align_section:
1182
	mov	[undefined_data_end],0
1185
	mov	[undefined_data_end],0
1183
	mov	ebp,edi
1186
	mov	ebp,edi
1184
	sub	ebp,[ebx+14h]
1187
	sub	ebp,[ebx+14h]
1185
	mov	ecx,[edx+3Ch]
1188
	mov	ecx,[edx+3Ch]
1186
	dec	ecx
1189
	dec	ecx
1187
	lea	eax,[ebp+ecx]
1190
	lea	eax,[ebp+ecx]
1188
	not	ecx
1191
	not	ecx
1189
	and	eax,ecx
1192
	and	eax,ecx
1190
	mov	[ebx+10h],eax
1193
	mov	[ebx+10h],eax
1191
	sub	eax,ebp
1194
	sub	eax,ebp
1192
	mov	ecx,eax
1195
	mov	ecx,eax
1193
	xor	al,al
1196
	xor	al,al
1194
	rep	stos byte [edi]
1197
	rep	stos byte [edi]
1195
	mov	eax,[code_start]
1198
	mov	eax,[code_start]
1196
	sub	eax,[stub_size]
1199
	sub	eax,[stub_size]
1197
	sub	[ebx+14h],eax
1200
	sub	[ebx+14h],eax
1198
	mov	eax,[ebx+8]
1201
	mov	eax,[ebx+8]
1199
	or	eax,eax
1202
	or	eax,eax
1200
	jz	udata_ok
1203
	jz	udata_ok
1201
	cmp	dword [ebx+10h],0
1204
	cmp	dword [ebx+10h],0
1202
	jne	udata_ok
1205
	jne	udata_ok
1203
	or	byte [ebx+24h],80h
1206
	or	byte [ebx+24h],80h
1204
      udata_ok:
1207
      udata_ok:
1205
	mov	ecx,[edx+38h]
1208
	mov	ecx,[edx+38h]
1206
	dec	ecx
1209
	dec	ecx
1207
	add	eax,ecx
1210
	add	eax,ecx
1208
	not	ecx
1211
	not	ecx
1209
	and	eax,ecx
1212
	and	eax,ecx
1210
	add	eax,[ebx+0Ch]
1213
	add	eax,[ebx+0Ch]
1211
	add	ebx,28h
1214
	add	ebx,28h
1212
	mov	[current_section],ebx
1215
	mov	[current_section],ebx
1213
	inc	word [number_of_sections]
1216
	inc	word [number_of_sections]
1214
	jz	format_limitations_exceeded
1217
	jz	format_limitations_exceeded
1215
	ret
1218
	ret
1216
data_directive:
1219
data_directive:
1217
	cmp	[output_format],3
1220
	cmp	[output_format],3
1218
	jne	illegal_instruction
1221
	jne	illegal_instruction
1219
	lods	byte [esi]
1222
	lods	byte [esi]
1220
	cmp	al,1Ah
1223
	cmp	al,1Ah
1221
	je	predefined_data_type
1224
	je	predefined_data_type
1222
	cmp	al,'('
1225
	cmp	al,'('
1223
	jne	invalid_argument
1226
	jne	invalid_argument
1224
	call	get_byte_value
1227
	call	get_byte_value
1225
	cmp	al,16
1228
	cmp	al,16
1226
	jb	data_type_ok
1229
	jb	data_type_ok
1227
	jmp	invalid_value
1230
	jmp	invalid_value
1228
      predefined_data_type:
1231
      predefined_data_type:
1229
	movzx	eax,byte [esi]
1232
	movzx	eax,byte [esi]
1230
	inc	esi
1233
	inc	esi
1231
      data_type_ok:
1234
      data_type_ok:
1232
	mov	ebx,[current_section]
1235
	mov	ebx,[current_section]
1233
	mov	ecx,edi
1236
	mov	ecx,edi
1234
	sub	ecx,[ebx+14h]
1237
	sub	ecx,[ebx+14h]
1235
	add	ecx,[ebx+0Ch]
1238
	add	ecx,[ebx+0Ch]
1236
	mov	edx,[code_start]
1239
	mov	edx,[code_start]
1237
	test	[format_flags],8
1240
	test	[format_flags],8
1238
	jnz	pe64_data
1241
	jnz	pe64_data
1239
	xchg	ecx,[edx+78h+eax*8]
1242
	xchg	ecx,[edx+78h+eax*8]
1240
	jmp	init_pe_data
1243
	jmp	init_pe_data
1241
      pe64_data:
1244
      pe64_data:
1242
	xchg	ecx,[edx+88h+eax*8]
1245
	xchg	ecx,[edx+88h+eax*8]
1243
      init_pe_data:
1246
      init_pe_data:
1244
	or	ecx,ecx
1247
	or	ecx,ecx
1245
	jnz	data_already_defined
1248
	jnz	data_already_defined
1246
	call	allocate_structure_data
1249
	call	allocate_structure_data
1247
	mov	word [ebx],data_directive-assembler
1250
	mov	word [ebx],data_directive-assembler
1248
	mov	[ebx+2],al
1251
	mov	[ebx+2],al
1249
	mov	edx,[current_line]
1252
	mov	edx,[current_line]
1250
	mov	[ebx+4],edx
1253
	mov	[ebx+4],edx
1251
	call	generate_pe_data
1254
	call	generate_pe_data
1252
	jmp	instruction_assembled
1255
	jmp	instruction_assembled
1253
      end_data:
1256
      end_data:
1254
	cmp	[output_format],3
1257
	cmp	[output_format],3
1255
	jne	illegal_instruction
1258
	jne	illegal_instruction
1256
	call	find_structure_data
1259
	call	find_structure_data
1257
	jc	unexpected_instruction
1260
	jc	unexpected_instruction
1258
	movzx	eax,byte [ebx+2]
1261
	movzx	eax,byte [ebx+2]
1259
	mov	edx,[current_section]
1262
	mov	edx,[current_section]
1260
	mov	ecx,edi
1263
	mov	ecx,edi
1261
	sub	ecx,[edx+14h]
1264
	sub	ecx,[edx+14h]
1262
	add	ecx,[edx+0Ch]
1265
	add	ecx,[edx+0Ch]
1263
	mov	edx,[code_start]
1266
	mov	edx,[code_start]
1264
	test	[format_flags],8
1267
	test	[format_flags],8
1265
	jnz	end_pe64_data
1268
	jnz	end_pe64_data
1266
	sub	ecx,[edx+78h+eax*8]
1269
	sub	ecx,[edx+78h+eax*8]
1267
	mov	[edx+78h+eax*8+4],ecx
1270
	mov	[edx+78h+eax*8+4],ecx
1268
	jmp	remove_structure_data
1271
	jmp	remove_structure_data
1269
      end_pe64_data:
1272
      end_pe64_data:
1270
	sub	ecx,[edx+88h+eax*8]
1273
	sub	ecx,[edx+88h+eax*8]
1271
	mov	[edx+88h+eax*8+4],ecx
1274
	mov	[edx+88h+eax*8+4],ecx
1272
	jmp	remove_structure_data
1275
	jmp	remove_structure_data
1273
pe_entry:
1276
pe_entry:
1274
	lods	byte [esi]
1277
	lods	byte [esi]
1275
	cmp	al,'('
1278
	cmp	al,'('
1276
	jne	invalid_argument
1279
	jne	invalid_argument
1277
	cmp	byte [esi],'.'
1280
	cmp	byte [esi],'.'
1278
	je	invalid_value
1281
	je	invalid_value
1279
	test	[format_flags],8
1282
	test	[format_flags],8
1280
	jnz	pe64_entry
1283
	jnz	pe64_entry
1281
	call	get_dword_value
1284
	call	get_dword_value
1282
	mov	bl,2
1285
	mov	bl,2
1283
	bt	[resolver_flags],0
1286
	bt	[resolver_flags],0
1284
	jc	check_pe_entry_label_type
1287
	jc	check_pe_entry_label_type
1285
	xor	bl,bl
1288
	xor	bl,bl
1286
      check_pe_entry_label_type:
1289
      check_pe_entry_label_type:
1287
	cmp	[value_type],bl
1290
	cmp	[value_type],bl
1288
	je	pe_entry_ok
1291
	je	pe_entry_ok
1289
	cmp	[error_line],0
1292
	cmp	[error_line],0
1290
	jne	pe_entry_ok
1293
	jne	pe_entry_ok
1291
	mov	edx,[current_line]
1294
	mov	edx,[current_line]
1292
	mov	[error_line],edx
1295
	mov	[error_line],edx
1293
	mov	[error],invalid_address
1296
	mov	[error],invalid_address
1294
      pe_entry_ok:
1297
      pe_entry_ok:
1295
	mov	edx,[code_start]
1298
	mov	edx,[code_start]
1296
	sub	eax,[edx+34h]
1299
	sub	eax,[edx+34h]
1297
	mov	[edx+28h],eax
1300
	mov	[edx+28h],eax
1298
	jmp	instruction_assembled
1301
	jmp	instruction_assembled
1299
      pe64_entry:
1302
      pe64_entry:
1300
	call	get_qword_value
1303
	call	get_qword_value
1301
	mov	bl,4
1304
	mov	bl,4
1302
	bt	[resolver_flags],0
1305
	bt	[resolver_flags],0
1303
	jc	check_pe64_entry_label_type
1306
	jc	check_pe64_entry_label_type
1304
	xor	bl,bl
1307
	xor	bl,bl
1305
      check_pe64_entry_label_type:
1308
      check_pe64_entry_label_type:
1306
	cmp	[value_type],bl
1309
	cmp	[value_type],bl
1307
	je	pe64_entry_type_ok
1310
	je	pe64_entry_type_ok
1308
	cmp	[error_line],0
1311
	cmp	[error_line],0
1309
	jne	pe64_entry_type_ok
1312
	jne	pe64_entry_type_ok
1310
	mov	edx,[current_line]
1313
	mov	edx,[current_line]
1311
	mov	[error_line],edx
1314
	mov	[error_line],edx
1312
	mov	[error],invalid_address
1315
	mov	[error],invalid_address
1313
      pe64_entry_type_ok:
1316
      pe64_entry_type_ok:
1314
	mov	ecx,[code_start]
1317
	mov	ecx,[code_start]
1315
	sub	eax,[ecx+30h]
1318
	sub	eax,[ecx+30h]
1316
	sbb	edx,[ecx+34h]
1319
	sbb	edx,[ecx+34h]
1317
	jz	pe64_entry_range_ok
1320
	jz	pe64_entry_range_ok
1318
	mov	edx,[current_line]
1321
	mov	edx,[current_line]
1319
	mov	[error_line],edx
1322
	mov	[error_line],edx
1320
	mov	[error],value_out_of_range
1323
	mov	[error],value_out_of_range
1321
      pe64_entry_range_ok:
1324
      pe64_entry_range_ok:
1322
	mov	[ecx+28h],eax
1325
	mov	[ecx+28h],eax
1323
	jmp	instruction_assembled
1326
	jmp	instruction_assembled
1324
pe_stack:
1327
pe_stack:
1325
	lods	byte [esi]
1328
	lods	byte [esi]
1326
	cmp	al,'('
1329
	cmp	al,'('
1327
	jne	invalid_argument
1330
	jne	invalid_argument
1328
	cmp	byte [esi],'.'
1331
	cmp	byte [esi],'.'
1329
	je	invalid_value
1332
	je	invalid_value
1330
	test	[format_flags],8
1333
	test	[format_flags],8
1331
	jnz	pe64_stack
1334
	jnz	pe64_stack
1332
	call	get_dword_value
1335
	call	get_dword_value
1333
	cmp	[value_type],0
1336
	cmp	[value_type],0
1334
	jne	invalid_use_of_symbol
1337
	jne	invalid_use_of_symbol
1335
	mov	edx,[code_start]
1338
	mov	edx,[code_start]
1336
	mov	[edx+60h],eax
1339
	mov	[edx+60h],eax
1337
	cmp	byte [esi],','
1340
	cmp	byte [esi],','
1338
	jne	default_stack_commit
1341
	jne	default_stack_commit
1339
	lods	byte [esi]
1342
	lods	byte [esi]
1340
	lods	byte [esi]
1343
	lods	byte [esi]
1341
	cmp	al,'('
1344
	cmp	al,'('
1342
	jne	invalid_argument
1345
	jne	invalid_argument
1343
	cmp	byte [esi],'.'
1346
	cmp	byte [esi],'.'
1344
	je	invalid_value
1347
	je	invalid_value
1345
	call	get_dword_value
1348
	call	get_dword_value
1346
	cmp	[value_type],0
1349
	cmp	[value_type],0
1347
	jne	invalid_use_of_symbol
1350
	jne	invalid_use_of_symbol
1348
	mov	edx,[code_start]
1351
	mov	edx,[code_start]
1349
	mov	[edx+64h],eax
1352
	mov	[edx+64h],eax
1350
	cmp	eax,[edx+60h]
1353
	cmp	eax,[edx+60h]
1351
	ja	value_out_of_range
1354
	ja	value_out_of_range
1352
	jmp	instruction_assembled
1355
	jmp	instruction_assembled
1353
      default_stack_commit:
1356
      default_stack_commit:
1354
	mov	dword [edx+64h],1000h
1357
	mov	dword [edx+64h],1000h
1355
	mov	eax,[edx+60h]
1358
	mov	eax,[edx+60h]
1356
	cmp	eax,1000h
1359
	cmp	eax,1000h
1357
	ja	instruction_assembled
1360
	ja	instruction_assembled
1358
	mov	dword [edx+64h],eax
1361
	mov	dword [edx+64h],eax
1359
	jmp	instruction_assembled
1362
	jmp	instruction_assembled
1360
      pe64_stack:
1363
      pe64_stack:
1361
	call	get_qword_value
1364
	call	get_qword_value
1362
	cmp	[value_type],0
1365
	cmp	[value_type],0
1363
	jne	invalid_use_of_symbol
1366
	jne	invalid_use_of_symbol
1364
	mov	ecx,[code_start]
1367
	mov	ecx,[code_start]
1365
	mov	[ecx+60h],eax
1368
	mov	[ecx+60h],eax
1366
	mov	[ecx+64h],edx
1369
	mov	[ecx+64h],edx
1367
	cmp	byte [esi],','
1370
	cmp	byte [esi],','
1368
	jne	default_pe64_stack_commit
1371
	jne	default_pe64_stack_commit
1369
	lods	byte [esi]
1372
	lods	byte [esi]
1370
	lods	byte [esi]
1373
	lods	byte [esi]
1371
	cmp	al,'('
1374
	cmp	al,'('
1372
	jne	invalid_argument
1375
	jne	invalid_argument
1373
	cmp	byte [esi],'.'
1376
	cmp	byte [esi],'.'
1374
	je	invalid_value
1377
	je	invalid_value
1375
	call	get_qword_value
1378
	call	get_qword_value
1376
	cmp	[value_type],0
1379
	cmp	[value_type],0
1377
	jne	invalid_use_of_symbol
1380
	jne	invalid_use_of_symbol
1378
	mov	ecx,[code_start]
1381
	mov	ecx,[code_start]
1379
	mov	[ecx+68h],eax
1382
	mov	[ecx+68h],eax
1380
	mov	[ecx+6Ch],edx
1383
	mov	[ecx+6Ch],edx
1381
	cmp	edx,[ecx+64h]
1384
	cmp	edx,[ecx+64h]
1382
	ja	value_out_of_range
1385
	ja	value_out_of_range
1383
	jb	instruction_assembled
1386
	jb	instruction_assembled
1384
	cmp	eax,[ecx+60h]
1387
	cmp	eax,[ecx+60h]
1385
	ja	value_out_of_range
1388
	ja	value_out_of_range
1386
	jmp	instruction_assembled
1389
	jmp	instruction_assembled
1387
      default_pe64_stack_commit:
1390
      default_pe64_stack_commit:
1388
	mov	dword [ecx+68h],1000h
1391
	mov	dword [ecx+68h],1000h
1389
	cmp	dword [ecx+64h],0
1392
	cmp	dword [ecx+64h],0
1390
	jne	instruction_assembled
1393
	jne	instruction_assembled
1391
	mov	eax,[ecx+60h]
1394
	mov	eax,[ecx+60h]
1392
	cmp	eax,1000h
1395
	cmp	eax,1000h
1393
	ja	instruction_assembled
1396
	ja	instruction_assembled
1394
	mov	dword [ecx+68h],eax
1397
	mov	dword [ecx+68h],eax
1395
	jmp	instruction_assembled
1398
	jmp	instruction_assembled
1396
pe_heap:
1399
pe_heap:
1397
	lods	byte [esi]
1400
	lods	byte [esi]
1398
	cmp	al,'('
1401
	cmp	al,'('
1399
	jne	invalid_argument
1402
	jne	invalid_argument
1400
	cmp	byte [esi],'.'
1403
	cmp	byte [esi],'.'
1401
	je	invalid_value
1404
	je	invalid_value
1402
	test	[format_flags],8
1405
	test	[format_flags],8
1403
	jnz	pe64_heap
1406
	jnz	pe64_heap
1404
	call	get_dword_value
1407
	call	get_dword_value
1405
	cmp	[value_type],0
1408
	cmp	[value_type],0
1406
	jne	invalid_use_of_symbol
1409
	jne	invalid_use_of_symbol
1407
	mov	edx,[code_start]
1410
	mov	edx,[code_start]
1408
	mov	[edx+68h],eax
1411
	mov	[edx+68h],eax
1409
	cmp	byte [esi],','
1412
	cmp	byte [esi],','
1410
	jne	instruction_assembled
1413
	jne	instruction_assembled
1411
	lods	byte [esi]
1414
	lods	byte [esi]
1412
	lods	byte [esi]
1415
	lods	byte [esi]
1413
	cmp	al,'('
1416
	cmp	al,'('
1414
	jne	invalid_argument
1417
	jne	invalid_argument
1415
	cmp	byte [esi],'.'
1418
	cmp	byte [esi],'.'
1416
	je	invalid_value
1419
	je	invalid_value
1417
	call	get_dword_value
1420
	call	get_dword_value
1418
	cmp	[value_type],0
1421
	cmp	[value_type],0
1419
	jne	invalid_use_of_symbol
1422
	jne	invalid_use_of_symbol
1420
	mov	edx,[code_start]
1423
	mov	edx,[code_start]
1421
	mov	[edx+6Ch],eax
1424
	mov	[edx+6Ch],eax
1422
	cmp	eax,[edx+68h]
1425
	cmp	eax,[edx+68h]
1423
	ja	value_out_of_range
1426
	ja	value_out_of_range
1424
	jmp	instruction_assembled
1427
	jmp	instruction_assembled
1425
      pe64_heap:
1428
      pe64_heap:
1426
	call	get_qword_value
1429
	call	get_qword_value
1427
	cmp	[value_type],0
1430
	cmp	[value_type],0
1428
	jne	invalid_use_of_symbol
1431
	jne	invalid_use_of_symbol
1429
	mov	ecx,[code_start]
1432
	mov	ecx,[code_start]
1430
	mov	[ecx+70h],eax
1433
	mov	[ecx+70h],eax
1431
	mov	[ecx+74h],edx
1434
	mov	[ecx+74h],edx
1432
	cmp	byte [esi],','
1435
	cmp	byte [esi],','
1433
	jne	instruction_assembled
1436
	jne	instruction_assembled
1434
	lods	byte [esi]
1437
	lods	byte [esi]
1435
	lods	byte [esi]
1438
	lods	byte [esi]
1436
	cmp	al,'('
1439
	cmp	al,'('
1437
	jne	invalid_argument
1440
	jne	invalid_argument
1438
	cmp	byte [esi],'.'
1441
	cmp	byte [esi],'.'
1439
	je	invalid_value
1442
	je	invalid_value
1440
	call	get_qword_value
1443
	call	get_qword_value
1441
	cmp	[value_type],0
1444
	cmp	[value_type],0
1442
	jne	invalid_use_of_symbol
1445
	jne	invalid_use_of_symbol
1443
	mov	ecx,[code_start]
1446
	mov	ecx,[code_start]
1444
	mov	[ecx+78h],eax
1447
	mov	[ecx+78h],eax
1445
	mov	[ecx+7Ch],edx
1448
	mov	[ecx+7Ch],edx
1446
	cmp	edx,[ecx+74h]
1449
	cmp	edx,[ecx+74h]
1447
	ja	value_out_of_range
1450
	ja	value_out_of_range
1448
	jb	instruction_assembled
1451
	jb	instruction_assembled
1449
	cmp	eax,[edx+70h]
1452
	cmp	eax,[edx+70h]
1450
	ja	value_out_of_range
1453
	ja	value_out_of_range
1451
	jmp	instruction_assembled
1454
	jmp	instruction_assembled
1452
mark_pe_relocation:
1455
mark_pe_relocation:
1453
	push	eax ebx
1456
	push	eax ebx
1454
	test	[format_flags],8
1457
	test	[format_flags],8
1455
	jz	check_pe32_relocation_type
1458
	jz	check_pe32_relocation_type
1456
	cmp	[value_type],4
1459
	cmp	[value_type],4
1457
	je	pe_relocation_type_ok
1460
	je	pe_relocation_type_ok
1458
      check_pe32_relocation_type:
1461
      check_pe32_relocation_type:
1459
	cmp	[value_type],2
1462
	cmp	[value_type],2
1460
	je	pe_relocation_type_ok
1463
	je	pe_relocation_type_ok
1461
	cmp	[error_line],0
1464
	cmp	[error_line],0
1462
	jne	pe_relocation_type_ok
1465
	jne	pe_relocation_type_ok
1463
	mov	eax,[current_line]
1466
	mov	eax,[current_line]
1464
	mov	[error_line],eax
1467
	mov	[error_line],eax
1465
	mov	[error],invalid_use_of_symbol
1468
	mov	[error],invalid_use_of_symbol
1466
      pe_relocation_type_ok:
1469
      pe_relocation_type_ok:
1467
	mov	ebx,[current_section]
1470
	mov	ebx,[current_section]
1468
	mov	eax,edi
1471
	mov	eax,edi
1469
	sub	eax,[ebx+14h]
1472
	sub	eax,[ebx+14h]
1470
	add	eax,[ebx+0Ch]
1473
	add	eax,[ebx+0Ch]
1471
	mov	ebx,[free_additional_memory]
1474
	mov	ebx,[free_additional_memory]
1472
	inc	[number_of_relocations]
1475
	inc	[number_of_relocations]
1473
	jz	invalid_use_of_symbol
1476
	jz	invalid_use_of_symbol
1474
	add	ebx,5
1477
	add	ebx,5
1475
	cmp	ebx,[structures_buffer]
1478
	cmp	ebx,[structures_buffer]
1476
	jae	out_of_memory
1479
	jae	out_of_memory
1477
	mov	[free_additional_memory],ebx
1480
	mov	[free_additional_memory],ebx
1478
	mov	[ebx-5],eax
1481
	mov	[ebx-5],eax
1479
	cmp	[value_type],2
1482
	cmp	[value_type],2
1480
	je	fixup_32bit
1483
	je	fixup_32bit
1481
	mov	byte [ebx-1],0Ah
1484
	mov	byte [ebx-1],0Ah
1482
	jmp	fixup_ok
1485
	jmp	fixup_ok
1483
      fixup_32bit:
1486
      fixup_32bit:
1484
	mov	byte [ebx-1],3
1487
	mov	byte [ebx-1],3
1485
      fixup_ok:
1488
      fixup_ok:
1486
	pop	ebx eax
1489
	pop	ebx eax
1487
	ret
1490
	ret
1488
generate_pe_data:
1491
generate_pe_data:
1489
	cmp	al,2
1492
	cmp	al,2
1490
	je	make_pe_resource
1493
	je	make_pe_resource
1491
	cmp	al,5
1494
	cmp	al,5
1492
	je	make_pe_fixups
1495
	je	make_pe_fixups
1493
	ret
1496
	ret
1494
make_pe_fixups:
1497
make_pe_fixups:
1495
	bts	[resolver_flags],0
1498
	bts	[resolver_flags],0
1496
	jc	pe_relocatable_ok
1499
	jc	pe_relocatable_ok
1497
	or	[next_pass_needed],-1
1500
	or	[next_pass_needed],-1
1498
     pe_relocatable_ok:
1501
     pe_relocatable_ok:
1499
	push	esi
1502
	push	esi
1500
	mov	ecx,[number_of_relocations]
1503
	mov	ecx,[number_of_relocations]
1501
	mov	esi,[free_additional_memory]
1504
	mov	esi,[free_additional_memory]
1502
	lea	eax,[ecx*5]
1505
	lea	eax,[ecx*5]
1503
	sub	esi,eax
1506
	sub	esi,eax
1504
	mov	[free_additional_memory],esi
1507
	mov	[free_additional_memory],esi
1505
	or	[number_of_relocations],-1
1508
	or	[number_of_relocations],-1
1506
	xor	edx,edx
1509
	xor	edx,edx
1507
	mov	ebp,edi
1510
	mov	ebp,edi
1508
      make_fixups:
1511
      make_fixups:
1509
	cmp	[esi],edx
1512
	cmp	[esi],edx
1510
	jb	store_fixup
1513
	jb	store_fixup
1511
	mov	eax,edi
1514
	mov	eax,edi
1512
	sub	eax,ebp
1515
	sub	eax,ebp
1513
	test	eax,11b
1516
	test	eax,11b
1514
	jz	fixups_block
1517
	jz	fixups_block
1515
	xor	ax,ax
1518
	xor	ax,ax
1516
	stos	word [edi]
1519
	stos	word [edi]
1517
	add	dword [ebx],2
1520
	add	dword [ebx],2
1518
      fixups_block:
1521
      fixups_block:
1519
	mov	eax,edx
1522
	mov	eax,edx
1520
	add	edx,1000h
1523
	add	edx,1000h
1521
	cmp	[esi],edx
1524
	cmp	[esi],edx
1522
	jae	fixups_block
1525
	jae	fixups_block
1523
	stos	dword [edi]
1526
	stos	dword [edi]
1524
	mov	ebx,edi
1527
	mov	ebx,edi
1525
	mov	eax,8
1528
	mov	eax,8
1526
	stos	dword [edi]
1529
	stos	dword [edi]
1527
      store_fixup:
1530
      store_fixup:
1528
	jecxz	fixups_done
1531
	jecxz	fixups_done
1529
	add	dword [ebx],2
1532
	add	dword [ebx],2
1530
	mov	ah,[esi+1]
1533
	mov	ah,[esi+1]
1531
	and	ah,0Fh
1534
	and	ah,0Fh
1532
	mov	al,[esi+4]
1535
	mov	al,[esi+4]
1533
	shl	al,4
1536
	shl	al,4
1534
	or	ah,al
1537
	or	ah,al
1535
	mov	al,[esi]
1538
	mov	al,[esi]
1536
	stos	word [edi]
1539
	stos	word [edi]
1537
	add	esi,5
1540
	add	esi,5
1538
	loop	make_fixups
1541
	loop	make_fixups
1539
      fixups_done:
1542
      fixups_done:
1540
	pop	esi
1543
	pop	esi
1541
	ret
1544
	ret
1542
make_pe_resource:
1545
make_pe_resource:
1543
	cmp	byte [esi],82h
1546
	cmp	byte [esi],82h
1544
	jne	resource_done
1547
	jne	resource_done
1545
	inc	esi
1548
	inc	esi
1546
	lods	word [esi]
1549
	lods	word [esi]
1547
	cmp	ax,'('
1550
	cmp	ax,'('
1548
	jne	invalid_argument
1551
	jne	invalid_argument
1549
	lods	dword [esi]
1552
	lods	dword [esi]
1550
	mov	edx,esi
1553
	mov	edx,esi
1551
	lea	esi,[esi+eax+1]
1554
	lea	esi,[esi+eax+1]
1552
	cmp	[next_pass_needed],0
1555
	cmp	[next_pass_needed],0
1553
	je	resource_from_file
1556
	je	resource_from_file
1554
	cmp	[current_pass],0
1557
	cmp	[current_pass],0
1555
	jne	reserve_space_for_resource
1558
	jne	reserve_space_for_resource
1556
	mov	[resource_size],0
1559
	mov	[resource_size],0
1557
      reserve_space_for_resource:
1560
      reserve_space_for_resource:
1558
	add	edi,[resource_size]
1561
	add	edi,[resource_size]
1559
	cmp	edi,[display_buffer]
1562
	cmp	edi,[display_buffer]
1560
	ja	out_of_memory
1563
	ja	out_of_memory
1561
	jmp	resource_done
1564
	jmp	resource_done
1562
      resource_from_file:
1565
      resource_from_file:
1563
	push	esi
1566
	push	esi
1564
	mov	esi,edx
1567
	mov	esi,edx
1565
	call	open_binary_file
1568
	call	open_binary_file
1566
	push	ebx
1569
	push	ebx
1567
	mov	esi,[free_additional_memory]
1570
	mov	esi,[free_additional_memory]
1568
	lea	eax,[esi+20h]
1571
	lea	eax,[esi+20h]
1569
	cmp	eax,[structures_buffer]
1572
	cmp	eax,[structures_buffer]
1570
	ja	out_of_memory
1573
	ja	out_of_memory
1571
	mov	edx,esi
1574
	mov	edx,esi
1572
	mov	ecx,20h
1575
	mov	ecx,20h
1573
	call	read
1576
	call	read
1574
	jc	invalid_file_format
1577
	jc	invalid_file_format
1575
	xor	eax,eax
1578
	xor	eax,eax
1576
	cmp	[esi],eax
1579
	cmp	[esi],eax
1577
	jne	invalid_file_format
1580
	jne	invalid_file_format
1578
	mov	ax,0FFFFh
1581
	mov	ax,0FFFFh
1579
	cmp	[esi+8],eax
1582
	cmp	[esi+8],eax
1580
	jne	invalid_file_format
1583
	jne	invalid_file_format
1581
	cmp	[esi+12],eax
1584
	cmp	[esi+12],eax
1582
	jne	invalid_file_format
1585
	jne	invalid_file_format
1583
	mov	eax,20h
1586
	mov	eax,20h
1584
	cmp	[esi+4],eax
1587
	cmp	[esi+4],eax
1585
	jne	invalid_file_format
1588
	jne	invalid_file_format
1586
      read_resource_headers:
1589
      read_resource_headers:
1587
	test	eax,11b
1590
	test	eax,11b
1588
	jz	resource_file_alignment_ok
1591
	jz	resource_file_alignment_ok
1589
	mov	edx,4
1592
	mov	edx,4
1590
	and	eax,11b
1593
	and	eax,11b
1591
	sub	edx,eax
1594
	sub	edx,eax
1592
	mov	al,1
1595
	mov	al,1
1593
	call	lseek
1596
	call	lseek
1594
      resource_file_alignment_ok:
1597
      resource_file_alignment_ok:
1595
	mov	[esi],eax
1598
	mov	[esi],eax
1596
	lea	edx,[esi+12]
1599
	lea	edx,[esi+12]
1597
	mov	ecx,8
1600
	mov	ecx,8
1598
	call	read
1601
	call	read
1599
	jc	resource_headers_ok
1602
	jc	resource_headers_ok
1600
	mov	ecx,[esi+16]
1603
	mov	ecx,[esi+16]
1601
	add	[esi],ecx
1604
	add	[esi],ecx
1602
	lea	edx,[esi+20]
1605
	lea	edx,[esi+20]
1603
	sub	ecx,8
1606
	sub	ecx,8
1604
	mov	[esi+16],ecx
1607
	mov	[esi+16],ecx
1605
	lea	eax,[edx+ecx]
1608
	lea	eax,[edx+ecx]
1606
	cmp	eax,[structures_buffer]
1609
	cmp	eax,[structures_buffer]
1607
	ja	out_of_memory
1610
	ja	out_of_memory
1608
	call	read
1611
	call	read
1609
	jc	invalid_file_format
1612
	jc	invalid_file_format
1610
	mov	edx,[esi]
1613
	mov	edx,[esi]
1611
	add	edx,[esi+12]
1614
	add	edx,[esi+12]
1612
	mov	eax,[esi+16]
1615
	mov	eax,[esi+16]
1613
	lea	ecx,[esi+20]
1616
	lea	ecx,[esi+20]
1614
	lea	esi,[ecx+eax]
1617
	lea	esi,[ecx+eax]
1615
	add	ecx,2
1618
	add	ecx,2
1616
	cmp	word [ecx-2],0FFFFh
1619
	cmp	word [ecx-2],0FFFFh
1617
	je	resource_header_type_ok
1620
	je	resource_header_type_ok
1618
      check_resource_header_type:
1621
      check_resource_header_type:
1619
	cmp	ecx,esi
1622
	cmp	ecx,esi
1620
	jae	invalid_file_format
1623
	jae	invalid_file_format
1621
	cmp	word [ecx],0
1624
	cmp	word [ecx],0
1622
	je	resource_header_type_ok
1625
	je	resource_header_type_ok
1623
	add	ecx,2
1626
	add	ecx,2
1624
	jmp	check_resource_header_type
1627
	jmp	check_resource_header_type
1625
      resource_header_type_ok:
1628
      resource_header_type_ok:
1626
	add	ecx,2
1629
	add	ecx,2
1627
	cmp	word [ecx],0FFFFh
1630
	cmp	word [ecx],0FFFFh
1628
	je	resource_header_name_ok
1631
	je	resource_header_name_ok
1629
      check_resource_header_name:
1632
      check_resource_header_name:
1630
	cmp	ecx,esi
1633
	cmp	ecx,esi
1631
	jae	invalid_file_format
1634
	jae	invalid_file_format
1632
	cmp	word [ecx],0
1635
	cmp	word [ecx],0
1633
	je	resource_header_name_ok
1636
	je	resource_header_name_ok
1634
	add	ecx,2
1637
	add	ecx,2
1635
	jmp	check_resource_header_name
1638
	jmp	check_resource_header_name
1636
      resource_header_name_ok:
1639
      resource_header_name_ok:
1637
	xor	al,al
1640
	xor	al,al
1638
	call	lseek
1641
	call	lseek
1639
	jmp	read_resource_headers
1642
	jmp	read_resource_headers
1640
      resource_headers_ok:
1643
      resource_headers_ok:
1641
	xor	eax,eax
1644
	xor	eax,eax
1642
	mov	[esi],eax
1645
	mov	[esi],eax
1643
	mov	[resource_data],edi
1646
	mov	[resource_data],edi
1644
	lea	eax,[edi+16]
1647
	lea	eax,[edi+16]
1645
	cmp	eax,[display_buffer]
1648
	cmp	eax,[display_buffer]
1646
	jae	out_of_memory
1649
	jae	out_of_memory
1647
	xor	eax,eax
1650
	xor	eax,eax
1648
	stos	dword [edi]
1651
	stos	dword [edi]
1649
	call	make_timestamp
1652
	call	make_timestamp
1650
	stos	dword [edi]
1653
	stos	dword [edi]
1651
	xor	eax,eax
1654
	xor	eax,eax
1652
	stos	dword [edi]
1655
	stos	dword [edi]
1653
	stos	dword [edi]
1656
	stos	dword [edi]
1654
	xor	ebx,ebx
1657
	xor	ebx,ebx
1655
      make_type_name_directory:
1658
      make_type_name_directory:
1656
	mov	esi,[free_additional_memory]
1659
	mov	esi,[free_additional_memory]
1657
	xor	edx,edx
1660
	xor	edx,edx
1658
      find_type_name:
1661
      find_type_name:
1659
	cmp	dword [esi],0
1662
	cmp	dword [esi],0
1660
	je	type_name_ok
1663
	je	type_name_ok
1661
	add	esi,20
1664
	add	esi,20
1662
	cmp	word [esi],0FFFFh
1665
	cmp	word [esi],0FFFFh
1663
	je	check_next_type_name
1666
	je	check_next_type_name
1664
	or	ebx,ebx
1667
	or	ebx,ebx
1665
	jz	check_this_type_name
1668
	jz	check_this_type_name
1666
	xor	ecx,ecx
1669
	xor	ecx,ecx
1667
      compare_with_previous_type_name:
1670
      compare_with_previous_type_name:
1668
	mov	ax,[esi+ecx]
1671
	mov	ax,[esi+ecx]
1669
	cmp	ax,[ebx+ecx]
1672
	cmp	ax,[ebx+ecx]
1670
	ja	check_this_type_name
1673
	ja	check_this_type_name
1671
	jb	check_next_type_name
1674
	jb	check_next_type_name
1672
	add	ecx,2
1675
	add	ecx,2
1673
	mov	ax,[esi+ecx]
1676
	mov	ax,[esi+ecx]
1674
	or	ax,[ebx+ecx]
1677
	or	ax,[ebx+ecx]
1675
	jnz	compare_with_previous_type_name
1678
	jnz	compare_with_previous_type_name
1676
	jmp	check_next_type_name
1679
	jmp	check_next_type_name
1677
      check_this_type_name:
1680
      check_this_type_name:
1678
	or	edx,edx
1681
	or	edx,edx
1679
	jz	type_name_found
1682
	jz	type_name_found
1680
	xor	ecx,ecx
1683
	xor	ecx,ecx
1681
      compare_with_current_type_name:
1684
      compare_with_current_type_name:
1682
	mov	ax,[esi+ecx]
1685
	mov	ax,[esi+ecx]
1683
	cmp	ax,[edx+ecx]
1686
	cmp	ax,[edx+ecx]
1684
	ja	check_next_type_name
1687
	ja	check_next_type_name
1685
	jb	type_name_found
1688
	jb	type_name_found
1686
	add	ecx,2
1689
	add	ecx,2
1687
	mov	ax,[esi+ecx]
1690
	mov	ax,[esi+ecx]
1688
	or	ax,[edx+ecx]
1691
	or	ax,[edx+ecx]
1689
	jnz	compare_with_current_type_name
1692
	jnz	compare_with_current_type_name
1690
	jmp	same_type_name
1693
	jmp	same_type_name
1691
      type_name_found:
1694
      type_name_found:
1692
	mov	edx,esi
1695
	mov	edx,esi
1693
      same_type_name:
1696
      same_type_name:
1694
	mov	[esi-16],edi
1697
	mov	[esi-16],edi
1695
      check_next_type_name:
1698
      check_next_type_name:
1696
	mov	eax,[esi-4]
1699
	mov	eax,[esi-4]
1697
	add	esi,eax
1700
	add	esi,eax
1698
	jmp	find_type_name
1701
	jmp	find_type_name
1699
      type_name_ok:
1702
      type_name_ok:
1700
	or	edx,edx
1703
	or	edx,edx
1701
	jz	type_name_directory_done
1704
	jz	type_name_directory_done
1702
	mov	ebx,edx
1705
	mov	ebx,edx
1703
      make_type_name_entry:
1706
      make_type_name_entry:
1704
	mov	eax,[resource_data]
1707
	mov	eax,[resource_data]
1705
	inc	word [eax+12]
1708
	inc	word [eax+12]
1706
	lea	eax,[edi+8]
1709
	lea	eax,[edi+8]
1707
	cmp	eax,[display_buffer]
1710
	cmp	eax,[display_buffer]
1708
	jae	out_of_memory
1711
	jae	out_of_memory
1709
	mov	eax,ebx
1712
	mov	eax,ebx
1710
	stos	dword [edi]
1713
	stos	dword [edi]
1711
	xor	eax,eax
1714
	xor	eax,eax
1712
	stos	dword [edi]
1715
	stos	dword [edi]
1713
	jmp	make_type_name_directory
1716
	jmp	make_type_name_directory
1714
      type_name_directory_done:
1717
      type_name_directory_done:
1715
	mov	ebx,-1
1718
	mov	ebx,-1
1716
      make_type_id_directory:
1719
      make_type_id_directory:
1717
	mov	esi,[free_additional_memory]
1720
	mov	esi,[free_additional_memory]
1718
	mov	edx,10000h
1721
	mov	edx,10000h
1719
      find_type_id:
1722
      find_type_id:
1720
	cmp	dword [esi],0
1723
	cmp	dword [esi],0
1721
	je	type_id_ok
1724
	je	type_id_ok
1722
	add	esi,20
1725
	add	esi,20
1723
	cmp	word [esi],0FFFFh
1726
	cmp	word [esi],0FFFFh
1724
	jne	check_next_type_id
1727
	jne	check_next_type_id
1725
	movzx	eax,word [esi+2]
1728
	movzx	eax,word [esi+2]
1726
	cmp	eax,ebx
1729
	cmp	eax,ebx
1727
	jle	check_next_type_id
1730
	jle	check_next_type_id
1728
	cmp	eax,edx
1731
	cmp	eax,edx
1729
	jg	check_next_type_id
1732
	jg	check_next_type_id
1730
	mov	edx,eax
1733
	mov	edx,eax
1731
	mov	[esi-16],edi
1734
	mov	[esi-16],edi
1732
      check_next_type_id:
1735
      check_next_type_id:
1733
	mov	eax,[esi-4]
1736
	mov	eax,[esi-4]
1734
	add	esi,eax
1737
	add	esi,eax
1735
	jmp	find_type_id
1738
	jmp	find_type_id
1736
      type_id_ok:
1739
      type_id_ok:
1737
	cmp	edx,10000h
1740
	cmp	edx,10000h
1738
	je	type_id_directory_done
1741
	je	type_id_directory_done
1739
	mov	ebx,edx
1742
	mov	ebx,edx
1740
      make_type_id_entry:
1743
      make_type_id_entry:
1741
	mov	eax,[resource_data]
1744
	mov	eax,[resource_data]
1742
	inc	word [eax+14]
1745
	inc	word [eax+14]
1743
	lea	eax,[edi+8]
1746
	lea	eax,[edi+8]
1744
	cmp	eax,[display_buffer]
1747
	cmp	eax,[display_buffer]
1745
	jae	out_of_memory
1748
	jae	out_of_memory
1746
	mov	eax,ebx
1749
	mov	eax,ebx
1747
	stos	dword [edi]
1750
	stos	dword [edi]
1748
	xor	eax,eax
1751
	xor	eax,eax
1749
	stos	dword [edi]
1752
	stos	dword [edi]
1750
	jmp	make_type_id_directory
1753
	jmp	make_type_id_directory
1751
      type_id_directory_done:
1754
      type_id_directory_done:
1752
	mov	esi,[resource_data]
1755
	mov	esi,[resource_data]
1753
	add	esi,10h
1756
	add	esi,10h
1754
	mov	ecx,[esi-4]
1757
	mov	ecx,[esi-4]
1755
	or	cx,cx
1758
	or	cx,cx
1756
	jz	resource_directories_ok
1759
	jz	resource_directories_ok
1757
      make_resource_directories:
1760
      make_resource_directories:
1758
	push	ecx
1761
	push	ecx
1759
	push	edi
1762
	push	edi
1760
	mov	edx,edi
1763
	mov	edx,edi
1761
	sub	edx,[resource_data]
1764
	sub	edx,[resource_data]
1762
	bts	edx,31
1765
	bts	edx,31
1763
	mov	[esi+4],edx
1766
	mov	[esi+4],edx
1764
	lea	eax,[edi+16]
1767
	lea	eax,[edi+16]
1765
	cmp	eax,[display_buffer]
1768
	cmp	eax,[display_buffer]
1766
	jae	out_of_memory
1769
	jae	out_of_memory
1767
	xor	eax,eax
1770
	xor	eax,eax
1768
	stos	dword [edi]
1771
	stos	dword [edi]
1769
	call	make_timestamp
1772
	call	make_timestamp
1770
	stos	dword [edi]
1773
	stos	dword [edi]
1771
	xor	eax,eax
1774
	xor	eax,eax
1772
	stos	dword [edi]
1775
	stos	dword [edi]
1773
	stos	dword [edi]
1776
	stos	dword [edi]
1774
	mov	ebp,esi
1777
	mov	ebp,esi
1775
	xor	ebx,ebx
1778
	xor	ebx,ebx
1776
      make_resource_name_directory:
1779
      make_resource_name_directory:
1777
	mov	esi,[free_additional_memory]
1780
	mov	esi,[free_additional_memory]
1778
	xor	edx,edx
1781
	xor	edx,edx
1779
      find_resource_name:
1782
      find_resource_name:
1780
	cmp	dword [esi],0
1783
	cmp	dword [esi],0
1781
	je	resource_name_ok
1784
	je	resource_name_ok
1782
	push	esi
1785
	push	esi
1783
	cmp	[esi+4],ebp
1786
	cmp	[esi+4],ebp
1784
	jne	check_next_resource_name
1787
	jne	check_next_resource_name
1785
	add	esi,20
1788
	add	esi,20
1786
	call	skip_resource_name
1789
	call	skip_resource_name
1787
	cmp	word [esi],0FFFFh
1790
	cmp	word [esi],0FFFFh
1788
	je	check_next_resource_name
1791
	je	check_next_resource_name
1789
	or	ebx,ebx
1792
	or	ebx,ebx
1790
	jz	check_this_resource_name
1793
	jz	check_this_resource_name
1791
	xor	ecx,ecx
1794
	xor	ecx,ecx
1792
      compare_with_previous_resource_name:
1795
      compare_with_previous_resource_name:
1793
	mov	ax,[esi+ecx]
1796
	mov	ax,[esi+ecx]
1794
	cmp	ax,[ebx+ecx]
1797
	cmp	ax,[ebx+ecx]
1795
	ja	check_this_resource_name
1798
	ja	check_this_resource_name
1796
	jb	check_next_resource_name
1799
	jb	check_next_resource_name
1797
	add	ecx,2
1800
	add	ecx,2
1798
	mov	ax,[esi+ecx]
1801
	mov	ax,[esi+ecx]
1799
	or	ax,[ebx+ecx]
1802
	or	ax,[ebx+ecx]
1800
	jnz	compare_with_previous_resource_name
1803
	jnz	compare_with_previous_resource_name
1801
	jmp	check_next_resource_name
1804
	jmp	check_next_resource_name
1802
      skip_resource_name:
1805
      skip_resource_name:
1803
	cmp	word [esi],0FFFFh
1806
	cmp	word [esi],0FFFFh
1804
	jne	skip_unicode_string
1807
	jne	skip_unicode_string
1805
	add	esi,4
1808
	add	esi,4
1806
	ret
1809
	ret
1807
      skip_unicode_string:
1810
      skip_unicode_string:
1808
	add	esi,2
1811
	add	esi,2
1809
	cmp	word [esi-2],0
1812
	cmp	word [esi-2],0
1810
	jne	skip_unicode_string
1813
	jne	skip_unicode_string
1811
	ret
1814
	ret
1812
      check_this_resource_name:
1815
      check_this_resource_name:
1813
	or	edx,edx
1816
	or	edx,edx
1814
	jz	resource_name_found
1817
	jz	resource_name_found
1815
	xor	ecx,ecx
1818
	xor	ecx,ecx
1816
      compare_with_current_resource_name:
1819
      compare_with_current_resource_name:
1817
	mov	ax,[esi+ecx]
1820
	mov	ax,[esi+ecx]
1818
	cmp	ax,[edx+ecx]
1821
	cmp	ax,[edx+ecx]
1819
	ja	check_next_resource_name
1822
	ja	check_next_resource_name
1820
	jb	resource_name_found
1823
	jb	resource_name_found
1821
	add	ecx,2
1824
	add	ecx,2
1822
	mov	ax,[esi+ecx]
1825
	mov	ax,[esi+ecx]
1823
	or	ax,[edx+ecx]
1826
	or	ax,[edx+ecx]
1824
	jnz	compare_with_current_resource_name
1827
	jnz	compare_with_current_resource_name
1825
	jmp	same_resource_name
1828
	jmp	same_resource_name
1826
      resource_name_found:
1829
      resource_name_found:
1827
	mov	edx,esi
1830
	mov	edx,esi
1828
      same_resource_name:
1831
      same_resource_name:
1829
	mov	eax,[esp]
1832
	mov	eax,[esp]
1830
	mov	[eax+8],edi
1833
	mov	[eax+8],edi
1831
      check_next_resource_name:
1834
      check_next_resource_name:
1832
	pop	esi
1835
	pop	esi
1833
	mov	eax,[esi+16]
1836
	mov	eax,[esi+16]
1834
	lea	esi,[esi+20+eax]
1837
	lea	esi,[esi+20+eax]
1835
	jmp	find_resource_name
1838
	jmp	find_resource_name
1836
      resource_name_ok:
1839
      resource_name_ok:
1837
	or	edx,edx
1840
	or	edx,edx
1838
	jz	resource_name_directory_done
1841
	jz	resource_name_directory_done
1839
	mov	ebx,edx
1842
	mov	ebx,edx
1840
      make_resource_name_entry:
1843
      make_resource_name_entry:
1841
	mov	eax,[esp]
1844
	mov	eax,[esp]
1842
	inc	word [eax+12]
1845
	inc	word [eax+12]
1843
	lea	eax,[edi+8]
1846
	lea	eax,[edi+8]
1844
	cmp	eax,[display_buffer]
1847
	cmp	eax,[display_buffer]
1845
	jae	out_of_memory
1848
	jae	out_of_memory
1846
	mov	eax,ebx
1849
	mov	eax,ebx
1847
	stos	dword [edi]
1850
	stos	dword [edi]
1848
	xor	eax,eax
1851
	xor	eax,eax
1849
	stos	dword [edi]
1852
	stos	dword [edi]
1850
	jmp	make_resource_name_directory
1853
	jmp	make_resource_name_directory
1851
      resource_name_directory_done:
1854
      resource_name_directory_done:
1852
	mov	ebx,-1
1855
	mov	ebx,-1
1853
      make_resource_id_directory:
1856
      make_resource_id_directory:
1854
	mov	esi,[free_additional_memory]
1857
	mov	esi,[free_additional_memory]
1855
	mov	edx,10000h
1858
	mov	edx,10000h
1856
      find_resource_id:
1859
      find_resource_id:
1857
	cmp	dword [esi],0
1860
	cmp	dword [esi],0
1858
	je	resource_id_ok
1861
	je	resource_id_ok
1859
	push	esi
1862
	push	esi
1860
	cmp	[esi+4],ebp
1863
	cmp	[esi+4],ebp
1861
	jne	check_next_resource_id
1864
	jne	check_next_resource_id
1862
	add	esi,20
1865
	add	esi,20
1863
	call	skip_resource_name
1866
	call	skip_resource_name
1864
	cmp	word [esi],0FFFFh
1867
	cmp	word [esi],0FFFFh
1865
	jne	check_next_resource_id
1868
	jne	check_next_resource_id
1866
	movzx	eax,word [esi+2]
1869
	movzx	eax,word [esi+2]
1867
	cmp	eax,ebx
1870
	cmp	eax,ebx
1868
	jle	check_next_resource_id
1871
	jle	check_next_resource_id
1869
	cmp	eax,edx
1872
	cmp	eax,edx
1870
	jg	check_next_resource_id
1873
	jg	check_next_resource_id
1871
	mov	edx,eax
1874
	mov	edx,eax
1872
	mov	eax,[esp]
1875
	mov	eax,[esp]
1873
	mov	[eax+8],edi
1876
	mov	[eax+8],edi
1874
      check_next_resource_id:
1877
      check_next_resource_id:
1875
	pop	esi
1878
	pop	esi
1876
	mov	eax,[esi+16]
1879
	mov	eax,[esi+16]
1877
	lea	esi,[esi+20+eax]
1880
	lea	esi,[esi+20+eax]
1878
	jmp	find_resource_id
1881
	jmp	find_resource_id
1879
      resource_id_ok:
1882
      resource_id_ok:
1880
	cmp	edx,10000h
1883
	cmp	edx,10000h
1881
	je	resource_id_directory_done
1884
	je	resource_id_directory_done
1882
	mov	ebx,edx
1885
	mov	ebx,edx
1883
      make_resource_id_entry:
1886
      make_resource_id_entry:
1884
	mov	eax,[esp]
1887
	mov	eax,[esp]
1885
	inc	word [eax+14]
1888
	inc	word [eax+14]
1886
	lea	eax,[edi+8]
1889
	lea	eax,[edi+8]
1887
	cmp	eax,[display_buffer]
1890
	cmp	eax,[display_buffer]
1888
	jae	out_of_memory
1891
	jae	out_of_memory
1889
	mov	eax,ebx
1892
	mov	eax,ebx
1890
	stos	dword [edi]
1893
	stos	dword [edi]
1891
	xor	eax,eax
1894
	xor	eax,eax
1892
	stos	dword [edi]
1895
	stos	dword [edi]
1893
	jmp	make_resource_id_directory
1896
	jmp	make_resource_id_directory
1894
      resource_id_directory_done:
1897
      resource_id_directory_done:
1895
	pop	eax
1898
	pop	eax
1896
	mov	esi,ebp
1899
	mov	esi,ebp
1897
	pop	ecx
1900
	pop	ecx
1898
	add	esi,8
1901
	add	esi,8
1899
	dec	cx
1902
	dec	cx
1900
	jnz	make_resource_directories
1903
	jnz	make_resource_directories
1901
      resource_directories_ok:
1904
      resource_directories_ok:
1902
	shr	ecx,16
1905
	shr	ecx,16
1903
	jnz	make_resource_directories
1906
	jnz	make_resource_directories
1904
	mov	esi,[resource_data]
1907
	mov	esi,[resource_data]
1905
	add	esi,10h
1908
	add	esi,10h
1906
	movzx	eax,word [esi-4]
1909
	movzx	eax,word [esi-4]
1907
	movzx	edx,word [esi-2]
1910
	movzx	edx,word [esi-2]
1908
	add	eax,edx
1911
	add	eax,edx
1909
	lea	esi,[esi+eax*8]
1912
	lea	esi,[esi+eax*8]
1910
	push	edi			; address of language directories
1913
	push	edi			; address of language directories
1911
      update_resource_directories:
1914
      update_resource_directories:
1912
	cmp	esi,[esp]
1915
	cmp	esi,[esp]
1913
	je	resource_directories_updated
1916
	je	resource_directories_updated
1914
	add	esi,10h
1917
	add	esi,10h
1915
	mov	ecx,[esi-4]
1918
	mov	ecx,[esi-4]
1916
	or	cx,cx
1919
	or	cx,cx
1917
	jz	language_directories_ok
1920
	jz	language_directories_ok
1918
      make_language_directories:
1921
      make_language_directories:
1919
	push	ecx
1922
	push	ecx
1920
	push	edi
1923
	push	edi
1921
	mov	edx,edi
1924
	mov	edx,edi
1922
	sub	edx,[resource_data]
1925
	sub	edx,[resource_data]
1923
	bts	edx,31
1926
	bts	edx,31
1924
	mov	[esi+4],edx
1927
	mov	[esi+4],edx
1925
	lea	eax,[edi+16]
1928
	lea	eax,[edi+16]
1926
	cmp	eax,[display_buffer]
1929
	cmp	eax,[display_buffer]
1927
	jae	out_of_memory
1930
	jae	out_of_memory
1928
	xor	eax,eax
1931
	xor	eax,eax
1929
	stos	dword [edi]
1932
	stos	dword [edi]
1930
	call	make_timestamp
1933
	call	make_timestamp
1931
	stos	dword [edi]
1934
	stos	dword [edi]
1932
	xor	eax,eax
1935
	xor	eax,eax
1933
	stos	dword [edi]
1936
	stos	dword [edi]
1934
	stos	dword [edi]
1937
	stos	dword [edi]
1935
	mov	ebp,esi
1938
	mov	ebp,esi
1936
	mov	ebx,-1
1939
	mov	ebx,-1
1937
      make_language_id_directory:
1940
      make_language_id_directory:
1938
	mov	esi,[free_additional_memory]
1941
	mov	esi,[free_additional_memory]
1939
	mov	edx,10000h
1942
	mov	edx,10000h
1940
      find_language_id:
1943
      find_language_id:
1941
	cmp	dword [esi],0
1944
	cmp	dword [esi],0
1942
	je	language_id_ok
1945
	je	language_id_ok
1943
	push	esi
1946
	push	esi
1944
	cmp	[esi+8],ebp
1947
	cmp	[esi+8],ebp
1945
	jne	check_next_language_id
1948
	jne	check_next_language_id
1946
	add	esi,20
1949
	add	esi,20
1947
	mov	eax,esi
1950
	mov	eax,esi
1948
	call	skip_resource_name
1951
	call	skip_resource_name
1949
	call	skip_resource_name
1952
	call	skip_resource_name
1950
	neg	eax
1953
	neg	eax
1951
	add	eax,esi
1954
	add	eax,esi
1952
	and	eax,11b
1955
	and	eax,11b
1953
	add	esi,eax
1956
	add	esi,eax
1954
      get_language_id:
1957
      get_language_id:
1955
	movzx	eax,word [esi+6]
1958
	movzx	eax,word [esi+6]
1956
	cmp	eax,ebx
1959
	cmp	eax,ebx
1957
	jle	check_next_language_id
1960
	jle	check_next_language_id
1958
	cmp	eax,edx
1961
	cmp	eax,edx
1959
	jge	check_next_language_id
1962
	jge	check_next_language_id
1960
	mov	edx,eax
1963
	mov	edx,eax
1961
	mov	eax,[esp]
1964
	mov	eax,[esp]
1962
	mov	[current_offset],eax
1965
	mov	[current_offset],eax
1963
      check_next_language_id:
1966
      check_next_language_id:
1964
	pop	esi
1967
	pop	esi
1965
	mov	eax,[esi+16]
1968
	mov	eax,[esi+16]
1966
	lea	esi,[esi+20+eax]
1969
	lea	esi,[esi+20+eax]
1967
	jmp	find_language_id
1970
	jmp	find_language_id
1968
      language_id_ok:
1971
      language_id_ok:
1969
	cmp	edx,10000h
1972
	cmp	edx,10000h
1970
	je	language_id_directory_done
1973
	je	language_id_directory_done
1971
	mov	ebx,edx
1974
	mov	ebx,edx
1972
      make_language_id_entry:
1975
      make_language_id_entry:
1973
	mov	eax,[esp]
1976
	mov	eax,[esp]
1974
	inc	word [eax+14]
1977
	inc	word [eax+14]
1975
	lea	eax,[edi+8]
1978
	lea	eax,[edi+8]
1976
	cmp	eax,[display_buffer]
1979
	cmp	eax,[display_buffer]
1977
	jae	out_of_memory
1980
	jae	out_of_memory
1978
	mov	eax,ebx
1981
	mov	eax,ebx
1979
	stos	dword [edi]
1982
	stos	dword [edi]
1980
	mov	eax,[current_offset]
1983
	mov	eax,[current_offset]
1981
	stos	dword [edi]
1984
	stos	dword [edi]
1982
	jmp	make_language_id_directory
1985
	jmp	make_language_id_directory
1983
      language_id_directory_done:
1986
      language_id_directory_done:
1984
	pop	eax
1987
	pop	eax
1985
	mov	esi,ebp
1988
	mov	esi,ebp
1986
	pop	ecx
1989
	pop	ecx
1987
	add	esi,8
1990
	add	esi,8
1988
	dec	cx
1991
	dec	cx
1989
	jnz	make_language_directories
1992
	jnz	make_language_directories
1990
      language_directories_ok:
1993
      language_directories_ok:
1991
	shr	ecx,16
1994
	shr	ecx,16
1992
	jnz	make_language_directories
1995
	jnz	make_language_directories
1993
	jmp	update_resource_directories
1996
	jmp	update_resource_directories
1994
      resource_directories_updated:
1997
      resource_directories_updated:
1995
	mov	esi,[resource_data]
1998
	mov	esi,[resource_data]
1996
	push	edi
1999
	push	edi
1997
      make_name_strings:
2000
      make_name_strings:
1998
	add	esi,10h
2001
	add	esi,10h
1999
	movzx	eax,word [esi-2]
2002
	movzx	eax,word [esi-2]
2000
	movzx	ecx,word [esi-4]
2003
	movzx	ecx,word [esi-4]
2001
	add	eax,ecx
2004
	add	eax,ecx
2002
	lea	eax,[esi+eax*8]
2005
	lea	eax,[esi+eax*8]
2003
	push	eax
2006
	push	eax
2004
	or	ecx,ecx
2007
	or	ecx,ecx
2005
	jz	string_entries_processed
2008
	jz	string_entries_processed
2006
      process_string_entries:
2009
      process_string_entries:
2007
	push	ecx
2010
	push	ecx
2008
	mov	edx,edi
2011
	mov	edx,edi
2009
	sub	edx,[resource_data]
2012
	sub	edx,[resource_data]
2010
	bts	edx,31
2013
	bts	edx,31
2011
	xchg	[esi],edx
2014
	xchg	[esi],edx
2012
	mov	ebx,edi
2015
	mov	ebx,edi
2013
	xor	ax,ax
2016
	xor	ax,ax
2014
	stos	word [edi]
2017
	stos	word [edi]
2015
      copy_string_data:
2018
      copy_string_data:
2016
	lea	eax,[edi+2]
2019
	lea	eax,[edi+2]
2017
	cmp	eax,[display_buffer]
2020
	cmp	eax,[display_buffer]
2018
	jae	out_of_memory
2021
	jae	out_of_memory
2019
	mov	ax,[edx]
2022
	mov	ax,[edx]
2020
	or	ax,ax
2023
	or	ax,ax
2021
	jz	string_data_copied
2024
	jz	string_data_copied
2022
	stos	word [edi]
2025
	stos	word [edi]
2023
	inc	word [ebx]
2026
	inc	word [ebx]
2024
	add	edx,2
2027
	add	edx,2
2025
	jmp	copy_string_data
2028
	jmp	copy_string_data
2026
      string_data_copied:
2029
      string_data_copied:
2027
	add	esi,8
2030
	add	esi,8
2028
	pop	ecx
2031
	pop	ecx
2029
	loop	process_string_entries
2032
	loop	process_string_entries
2030
      string_entries_processed:
2033
      string_entries_processed:
2031
	pop	esi
2034
	pop	esi
2032
	cmp	esi,[esp]
2035
	cmp	esi,[esp]
2033
	jb	make_name_strings
2036
	jb	make_name_strings
2034
	mov	eax,edi
2037
	mov	eax,edi
2035
	sub	eax,[resource_data]
2038
	sub	eax,[resource_data]
2036
	test	al,11b
2039
	test	al,11b
2037
	jz	resource_strings_alignment_ok
2040
	jz	resource_strings_alignment_ok
2038
	xor	ax,ax
2041
	xor	ax,ax
2039
	stos	word [edi]
2042
	stos	word [edi]
2040
      resource_strings_alignment_ok:
2043
      resource_strings_alignment_ok:
2041
	pop	edx
2044
	pop	edx
2042
	pop	ebx			; address of language directories
2045
	pop	ebx			; address of language directories
2043
	mov	ebp,edi
2046
	mov	ebp,edi
2044
      update_language_directories:
2047
      update_language_directories:
2045
	add	ebx,10h
2048
	add	ebx,10h
2046
	movzx	eax,word [ebx-2]
2049
	movzx	eax,word [ebx-2]
2047
	movzx	ecx,word [ebx-4]
2050
	movzx	ecx,word [ebx-4]
2048
	add	ecx,eax
2051
	add	ecx,eax
2049
      make_data_records:
2052
      make_data_records:
2050
	push	ecx
2053
	push	ecx
2051
	mov	esi,edi
2054
	mov	esi,edi
2052
	sub	esi,[resource_data]
2055
	sub	esi,[resource_data]
2053
	xchg	esi,[ebx+4]
2056
	xchg	esi,[ebx+4]
2054
	lea	eax,[edi+16]
2057
	lea	eax,[edi+16]
2055
	cmp	eax,[display_buffer]
2058
	cmp	eax,[display_buffer]
2056
	jae	out_of_memory
2059
	jae	out_of_memory
2057
	mov	eax,esi
2060
	mov	eax,esi
2058
	stos	dword [edi]
2061
	stos	dword [edi]
2059
	mov	eax,[esi+12]
2062
	mov	eax,[esi+12]
2060
	stos	dword [edi]
2063
	stos	dword [edi]
2061
	xor	eax,eax
2064
	xor	eax,eax
2062
	stos	dword [edi]
2065
	stos	dword [edi]
2063
	stos	dword [edi]
2066
	stos	dword [edi]
2064
	pop	ecx
2067
	pop	ecx
2065
	add	ebx,8
2068
	add	ebx,8
2066
	loop	make_data_records
2069
	loop	make_data_records
2067
	cmp	ebx,edx
2070
	cmp	ebx,edx
2068
	jb	update_language_directories
2071
	jb	update_language_directories
2069
	pop	ebx			; file handle
2072
	pop	ebx			; file handle
2070
	mov	esi,ebp
2073
	mov	esi,ebp
2071
	mov	ebp,edi
2074
	mov	ebp,edi
2072
      update_data_records:
2075
      update_data_records:
2073
	push	ebp
2076
	push	ebp
2074
	mov	ecx,edi
2077
	mov	ecx,edi
2075
	mov	eax,[current_section]
2078
	mov	eax,[current_section]
2076
	sub	ecx,[eax+14h]
2079
	sub	ecx,[eax+14h]
2077
	add	ecx,[eax+0Ch]
2080
	add	ecx,[eax+0Ch]
2078
	xchg	ecx,[esi]
2081
	xchg	ecx,[esi]
2079
	mov	edx,[ecx]
2082
	mov	edx,[ecx]
2080
	xor	al,al
2083
	xor	al,al
2081
	call	lseek
2084
	call	lseek
2082
	mov	edx,edi
2085
	mov	edx,edi
2083
	mov	ecx,[esi+4]
2086
	mov	ecx,[esi+4]
2084
	add	edi,ecx
2087
	add	edi,ecx
2085
	cmp	edi,[display_buffer]
2088
	cmp	edi,[display_buffer]
2086
	ja	out_of_memory
2089
	ja	out_of_memory
2087
	call	read
2090
	call	read
2088
	mov	eax,edi
2091
	mov	eax,edi
2089
	sub	eax,[resource_data]
2092
	sub	eax,[resource_data]
2090
	and	eax,11b
2093
	and	eax,11b
2091
	jz	resource_data_alignment_ok
2094
	jz	resource_data_alignment_ok
2092
	mov	ecx,4
2095
	mov	ecx,4
2093
	sub	ecx,eax
2096
	sub	ecx,eax
2094
	xor	al,al
2097
	xor	al,al
2095
	rep	stos byte [edi]
2098
	rep	stos byte [edi]
2096
      resource_data_alignment_ok:
2099
      resource_data_alignment_ok:
2097
	pop	ebp
2100
	pop	ebp
2098
	add	esi,16
2101
	add	esi,16
2099
	cmp	esi,ebp
2102
	cmp	esi,ebp
2100
	jb	update_data_records
2103
	jb	update_data_records
2101
	pop	esi
2104
	pop	esi
2102
	call	close
2105
	call	close
2103
	mov	eax,edi
2106
	mov	eax,edi
2104
	sub	eax,[resource_data]
2107
	sub	eax,[resource_data]
2105
	mov	[resource_size],eax
2108
	mov	[resource_size],eax
2106
      resource_done:
2109
      resource_done:
2107
	ret
2110
	ret
2108
close_pe:
2111
close_pe:
2109
	call	close_pe_section
2112
	call	close_pe_section
2110
	mov	edx,[code_start]
2113
	mov	edx,[code_start]
2111
	mov	[edx+50h],eax
2114
	mov	[edx+50h],eax
2112
	call	make_timestamp
2115
	call	make_timestamp
2113
	mov	edx,[code_start]
2116
	mov	edx,[code_start]
2114
	mov	[edx+8],eax
2117
	mov	[edx+8],eax
2115
	mov	eax,[number_of_relocations]
2118
	mov	eax,[number_of_relocations]
2116
	cmp	eax,-1
2119
	cmp	eax,-1
2117
	je	pe_relocations_ok
2120
	je	pe_relocations_ok
2118
	shl	eax,2
2121
	shl	eax,2
2119
	sub	[free_additional_memory],eax
2122
	sub	[free_additional_memory],eax
2120
	btr	[resolver_flags],0
2123
	btr	[resolver_flags],0
2121
	jnc	pe_relocations_ok
2124
	jnc	pe_relocations_ok
2122
	or	[next_pass_needed],-1
2125
	or	[next_pass_needed],-1
2123
      pe_relocations_ok:
2126
      pe_relocations_ok:
2124
	mov	eax,[number_of_sections]
2127
	mov	eax,[number_of_sections]
2125
	mov	[edx+6],ax
2128
	mov	[edx+6],ax
2126
	imul	eax,28h
2129
	imul	eax,28h
2127
	movzx	ecx,word [edx+14h]
2130
	movzx	ecx,word [edx+14h]
2128
	lea	eax,[eax+18h+ecx]
2131
	lea	eax,[eax+18h+ecx]
2129
	add	eax,[stub_size]
2132
	add	eax,[stub_size]
2130
	mov	ecx,[edx+3Ch]
2133
	mov	ecx,[edx+3Ch]
2131
	dec	ecx
2134
	dec	ecx
2132
	add	eax,ecx
2135
	add	eax,ecx
2133
	not	ecx
2136
	not	ecx
2134
	and	eax,ecx
2137
	and	eax,ecx
2135
	cmp	eax,[edx+54h]
2138
	cmp	eax,[edx+54h]
2136
	je	pe_sections_ok
2139
	je	pe_sections_ok
2137
	or	[next_pass_needed],-1
2140
	or	[next_pass_needed],-1
2138
      pe_sections_ok:
2141
      pe_sections_ok:
2139
	xor	ecx,ecx
2142
	xor	ecx,ecx
2140
	add	edx,78h
2143
	add	edx,78h
2141
	test	[format_flags],8
2144
	test	[format_flags],8
2142
	jz	process_directories
2145
	jz	process_directories
2143
	add	edx,10h
2146
	add	edx,10h
2144
      process_directories:
2147
      process_directories:
2145
	mov	eax,[edx+ecx*8]
2148
	mov	eax,[edx+ecx*8]
2146
	or	eax,eax
2149
	or	eax,eax
2147
	jz	directory_ok
2150
	jz	directory_ok
2148
	cmp	dword [edx+ecx*8+4],-1
2151
	cmp	dword [edx+ecx*8+4],-1
2149
	jne	directory_ok
2152
	jne	directory_ok
2150
      section_data:
2153
      section_data:
2151
	mov	ebx,[edx+ecx*8]
2154
	mov	ebx,[edx+ecx*8]
2152
	mov	eax,[ebx+0Ch]
2155
	mov	eax,[ebx+0Ch]
2153
	mov	[edx+ecx*8],eax 	; directory rva
2156
	mov	[edx+ecx*8],eax 	; directory rva
2154
	mov	eax,[ebx+8]
2157
	mov	eax,[ebx+8]
2155
	mov	[edx+ecx*8+4],eax	; directory size
2158
	mov	[edx+ecx*8+4],eax	; directory size
2156
      directory_ok:
2159
      directory_ok:
2157
	inc	cl
2160
	inc	cl
2158
	cmp	cl,10h
2161
	cmp	cl,10h
2159
	jb	process_directories
2162
	jb	process_directories
2160
	mov	ebx,[code_start]
2163
	mov	ebx,[code_start]
2161
	sub	ebx,[stub_size]
2164
	sub	ebx,[stub_size]
2162
	mov	ecx,edi
2165
	mov	ecx,edi
2163
	sub	ecx,ebx
2166
	sub	ecx,ebx
2164
	mov	ebp,ecx
2167
	mov	ebp,ecx
2165
	shr	ecx,1
2168
	shr	ecx,1
2166
	xor	eax,eax
2169
	xor	eax,eax
2167
	cdq
2170
	cdq
2168
      calculate_checksum:
2171
      calculate_checksum:
2169
	mov	dx,[ebx]
2172
	mov	dx,[ebx]
2170
	add	eax,edx
2173
	add	eax,edx
2171
	mov	dx,ax
2174
	mov	dx,ax
2172
	shr	eax,16
2175
	shr	eax,16
2173
	add	eax,edx
2176
	add	eax,edx
2174
	add	ebx,2
2177
	add	ebx,2
2175
	loop	calculate_checksum
2178
	loop	calculate_checksum
2176
	add	eax,ebp
2179
	add	eax,ebp
2177
	mov	ebx,[code_start]
2180
	mov	ebx,[code_start]
2178
	mov	[ebx+58h],eax
2181
	mov	[ebx+58h],eax
2179
	ret
2182
	ret
2180
 
2183
 
2181
format_coff:
2184
format_coff:
2182
	mov	eax,[additional_memory]
2185
	mov	eax,[additional_memory]
2183
	mov	[symbols_stream],eax
2186
	mov	[symbols_stream],eax
2184
	mov	ebx,eax
2187
	mov	ebx,eax
2185
	add	eax,20h
2188
	add	eax,20h
2186
	cmp	eax,[structures_buffer]
2189
	cmp	eax,[structures_buffer]
2187
	jae	out_of_memory
2190
	jae	out_of_memory
2188
	mov	[free_additional_memory],eax
2191
	mov	[free_additional_memory],eax
2189
	xor	eax,eax
2192
	xor	eax,eax
2190
	mov	[ebx],al
2193
	mov	[ebx],al
2191
	mov	[ebx+4],eax
2194
	mov	[ebx+4],eax
2192
	mov	[ebx+8],edi
2195
	mov	[ebx+8],edi
2193
	mov	al,4
2196
	mov	al,4
2194
	mov	[ebx+10h],eax
2197
	mov	[ebx+10h],eax
2195
	mov	al,60h
2198
	mov	al,60h
2196
	bt	[format_flags],0
2199
	bt	[format_flags],0
2197
	jnc	flat_section_flags_ok
2200
	jnc	flat_section_flags_ok
2198
	or	eax,0E0000000h
2201
	or	eax,0E0000000h
2199
      flat_section_flags_ok:
2202
      flat_section_flags_ok:
2200
	mov	dword [ebx+14h],eax
2203
	mov	dword [ebx+14h],eax
2201
	mov	[current_section],ebx
2204
	mov	[current_section],ebx
2202
	mov	[number_of_sections],0
2205
	mov	[number_of_sections],0
2203
	mov	dword [org_origin],edi
2206
	mov	dword [org_origin],edi
2204
	mov	dword [org_origin+4],0
2207
	mov	dword [org_origin+4],0
2205
	mov	[org_registers],0
2208
	mov	[org_registers],0
2206
	mov	[org_start],edi
2209
	mov	[org_start],edi
2207
	mov	[org_symbol],ebx
2210
	mov	[org_symbol],ebx
2208
	mov	[labels_type],2
2211
	mov	[labels_type],2
2209
	mov	[code_type],32
2212
	mov	[code_type],32
2210
	test	[format_flags],8
2213
	test	[format_flags],8
2211
	jz	format_defined
2214
	jz	format_defined
2212
	mov	[labels_type],4
2215
	mov	[labels_type],4
2213
	mov	[code_type],64
2216
	mov	[code_type],64
2214
	jmp	format_defined
2217
	jmp	format_defined
2215
coff_section:
2218
coff_section:
2216
	call	close_coff_section
2219
	call	close_coff_section
2217
	mov	ebx,[free_additional_memory]
2220
	mov	ebx,[free_additional_memory]
2218
	lea	eax,[ebx+20h]
2221
	lea	eax,[ebx+20h]
2219
	cmp	eax,[structures_buffer]
2222
	cmp	eax,[structures_buffer]
2220
	jae	out_of_memory
2223
	jae	out_of_memory
2221
	mov	[free_additional_memory],eax
2224
	mov	[free_additional_memory],eax
2222
	mov	[current_section],ebx
2225
	mov	[current_section],ebx
2223
	inc	[number_of_sections]
2226
	inc	[number_of_sections]
2224
	xor	eax,eax
2227
	xor	eax,eax
2225
	mov	[ebx],al
2228
	mov	[ebx],al
2226
	mov	[ebx+8],edi
2229
	mov	[ebx+8],edi
2227
	mov	dword [org_origin],edi
2230
	mov	dword [org_origin],edi
2228
	mov	dword [org_origin+4],0
2231
	mov	dword [org_origin+4],0
2229
	mov	[org_registers],0
2232
	mov	[org_registers],0
2230
	mov	[org_start],edi
2233
	mov	[org_start],edi
2231
	mov	[org_symbol],ebx
2234
	mov	[org_symbol],ebx
2232
	mov	[labels_type],2
2235
	mov	[labels_type],2
2233
	test	[format_flags],8
2236
	test	[format_flags],8
2234
	jz	coff_labels_type_ok
2237
	jz	coff_labels_type_ok
2235
	mov	[labels_type],4
2238
	mov	[labels_type],4
2236
      coff_labels_type_ok:
2239
      coff_labels_type_ok:
2237
	mov	[ebx+10h],eax
2240
	mov	[ebx+10h],eax
2238
	mov	[ebx+14h],eax
2241
	mov	[ebx+14h],eax
2239
	lods	word [esi]
2242
	lods	word [esi]
2240
	cmp	ax,'('
2243
	cmp	ax,'('
2241
	jne	invalid_argument
2244
	jne	invalid_argument
2242
	mov	[ebx+4],esi
2245
	mov	[ebx+4],esi
2243
	mov	ecx,[esi]
2246
	mov	ecx,[esi]
2244
	lea	esi,[esi+4+ecx+1]
2247
	lea	esi,[esi+4+ecx+1]
2245
	cmp	ecx,8
2248
	cmp	ecx,8
2246
	ja	name_too_long
2249
	ja	name_too_long
2247
      coff_section_flags:
2250
      coff_section_flags:
2248
	cmp	byte [esi],1Ch
2251
	cmp	byte [esi],1Ch
2249
	je	coff_section_alignment
2252
	je	coff_section_alignment
2250
	cmp	byte [esi],19h
2253
	cmp	byte [esi],19h
2251
	jne	coff_section_settings_ok
2254
	jne	coff_section_settings_ok
2252
	inc	esi
2255
	inc	esi
2253
	lods	byte [esi]
2256
	lods	byte [esi]
2254
	bt	[format_flags],0
2257
	bt	[format_flags],0
2255
	jc	coff_section_flag_ok
2258
	jc	coff_section_flag_ok
2256
	cmp	al,7
2259
	cmp	al,7
2257
	ja	invalid_argument
2260
	ja	invalid_argument
2258
      coff_section_flag_ok:
2261
      coff_section_flag_ok:
2259
	mov	cl,al
2262
	mov	cl,al
2260
	mov	eax,1
2263
	mov	eax,1
2261
	shl	eax,cl
2264
	shl	eax,cl
2262
	test	dword [ebx+14h],eax
2265
	test	dword [ebx+14h],eax
2263
	jnz	setting_already_specified
2266
	jnz	setting_already_specified
2264
	or	dword [ebx+14h],eax
2267
	or	dword [ebx+14h],eax
2265
	jmp	coff_section_flags
2268
	jmp	coff_section_flags
2266
      coff_section_alignment:
2269
      coff_section_alignment:
2267
	bt	[format_flags],0
2270
	bt	[format_flags],0
2268
	jnc	invalid_argument
2271
	jnc	invalid_argument
2269
	inc	esi
2272
	inc	esi
2270
	lods	byte [esi]
2273
	lods	byte [esi]
2271
	or	al,al
2274
	or	al,al
2272
	jnz	invalid_argument
2275
	jnz	invalid_argument
2273
	lods	byte [esi]
2276
	lods	byte [esi]
2274
	cmp	al,'('
2277
	cmp	al,'('
2275
	jne	invalid_argument
2278
	jne	invalid_argument
2276
	cmp	byte [esi],'.'
2279
	cmp	byte [esi],'.'
2277
	je	invalid_value
2280
	je	invalid_value
2278
	push	ebx
2281
	push	ebx
2279
	call	get_dword_value
2282
	call	get_dword_value
2280
	pop	ebx
2283
	pop	ebx
2281
	cmp	[value_type],0
2284
	cmp	[value_type],0
2282
	jne	invalid_use_of_symbol
2285
	jne	invalid_use_of_symbol
2283
	mov	edx,eax
2286
	mov	edx,eax
2284
	dec	edx
2287
	dec	edx
2285
	test	eax,edx
2288
	test	eax,edx
2286
	jnz	invalid_value
2289
	jnz	invalid_value
2287
	or	eax,eax
2290
	or	eax,eax
2288
	jz	invalid_value
2291
	jz	invalid_value
2289
	cmp	eax,2000h
2292
	cmp	eax,2000h
2290
	ja	invalid_value
2293
	ja	invalid_value
2291
	bsf	edx,eax
2294
	bsf	edx,eax
2292
	inc	edx
2295
	inc	edx
2293
	shl	edx,20
2296
	shl	edx,20
2294
	or	[ebx+14h],edx
2297
	or	[ebx+14h],edx
2295
	xchg	[ebx+10h],eax
2298
	xchg	[ebx+10h],eax
2296
	or	eax,eax
2299
	or	eax,eax
2297
	jnz	setting_already_specified
2300
	jnz	setting_already_specified
2298
	jmp	coff_section_flags
2301
	jmp	coff_section_flags
2299
      coff_section_settings_ok:
2302
      coff_section_settings_ok:
2300
	cmp	dword [ebx+10h],0
2303
	cmp	dword [ebx+10h],0
2301
	jne	instruction_assembled
2304
	jne	instruction_assembled
2302
	mov	dword [ebx+10h],4
2305
	mov	dword [ebx+10h],4
2303
	bt	[format_flags],0
2306
	bt	[format_flags],0
2304
	jnc	instruction_assembled
2307
	jnc	instruction_assembled
2305
	or	dword [ebx+14h],300000h
2308
	or	dword [ebx+14h],300000h
2306
	jmp	instruction_assembled
2309
	jmp	instruction_assembled
2307
      close_coff_section:
2310
      close_coff_section:
2308
	mov	ebx,[current_section]
2311
	mov	ebx,[current_section]
2309
	mov	eax,edi
2312
	mov	eax,edi
2310
	mov	edx,[ebx+8]
2313
	mov	edx,[ebx+8]
2311
	sub	eax,edx
2314
	sub	eax,edx
2312
	mov	[ebx+0Ch],eax
2315
	mov	[ebx+0Ch],eax
2313
	xor	eax,eax
2316
	xor	eax,eax
2314
	xchg	[undefined_data_end],eax
2317
	xchg	[undefined_data_end],eax
2315
	cmp	eax,edi
2318
	cmp	eax,edi
2316
	jne	coff_section_ok
2319
	jne	coff_section_ok
2317
	cmp	edx,[undefined_data_start]
2320
	cmp	edx,[undefined_data_start]
2318
	jne	coff_section_ok
2321
	jne	coff_section_ok
2319
	mov	edi,edx
2322
	mov	edi,edx
2320
	or	byte [ebx+14h],80h
2323
	or	byte [ebx+14h],80h
2321
      coff_section_ok:
2324
      coff_section_ok:
2322
	ret
2325
	ret
2323
mark_coff_relocation:
2326
mark_coff_relocation:
2324
	cmp	[value_type],3
2327
	cmp	[value_type],3
2325
	je	coff_relocation_relative
2328
	je	coff_relocation_relative
2326
	push	ebx eax
2329
	push	ebx eax
2327
	test	[format_flags],8
2330
	test	[format_flags],8
2328
	jnz	coff_64bit_relocation
2331
	jnz	coff_64bit_relocation
2329
	mov	al,6
2332
	mov	al,6
2330
	jmp	coff_relocation
2333
	jmp	coff_relocation
2331
      coff_64bit_relocation:
2334
      coff_64bit_relocation:
2332
	mov	al,1
2335
	mov	al,1
2333
	cmp	[value_type],4
2336
	cmp	[value_type],4
2334
	je	coff_relocation
2337
	je	coff_relocation
2335
	mov	al,2
2338
	mov	al,2
2336
	jmp	coff_relocation
2339
	jmp	coff_relocation
2337
      coff_relocation_relative:
2340
      coff_relocation_relative:
2338
	push	ebx
2341
	push	ebx
2339
	bt	[format_flags],0
2342
	bt	[format_flags],0
2340
	jnc	relative_ok
2343
	jnc	relative_ok
2341
	mov	ebx,[current_section]
2344
	mov	ebx,[current_section]
2342
	mov	ebx,[ebx+8]
2345
	mov	ebx,[ebx+8]
2343
	sub	ebx,edi
2346
	sub	ebx,edi
2344
	sub	eax,ebx
2347
	sub	eax,ebx
2345
	add	eax,4
2348
	add	eax,4
2346
      relative_ok:
2349
      relative_ok:
2347
	push	eax
2350
	push	eax
2348
	mov	al,20
2351
	mov	al,20
2349
	test	[format_flags],8
2352
	test	[format_flags],8
2350
	jnz	relative_coff_64bit_relocation
2353
	jnz	relative_coff_64bit_relocation
2351
	cmp	[labels_type],2
2354
	cmp	[labels_type],2
2352
	jne	invalid_use_of_symbol
2355
	jne	invalid_use_of_symbol
2353
	jmp	coff_relocation
2356
	jmp	coff_relocation
2354
      relative_coff_64bit_relocation:
2357
      relative_coff_64bit_relocation:
2355
	mov	al,4
2358
	mov	al,4
2356
	cmp	[labels_type],4
2359
	cmp	[labels_type],4
2357
	jne	invalid_use_of_symbol
2360
	jne	invalid_use_of_symbol
2358
      coff_relocation:
2361
      coff_relocation:
2359
	mov	ebx,[free_additional_memory]
2362
	mov	ebx,[free_additional_memory]
2360
	add	ebx,0Ch
2363
	add	ebx,0Ch
2361
	cmp	ebx,[structures_buffer]
2364
	cmp	ebx,[structures_buffer]
2362
	jae	out_of_memory
2365
	jae	out_of_memory
2363
	mov	[free_additional_memory],ebx
2366
	mov	[free_additional_memory],ebx
2364
	mov	byte [ebx-0Ch],al
2367
	mov	byte [ebx-0Ch],al
2365
	mov	eax,[current_section]
2368
	mov	eax,[current_section]
2366
	mov	eax,[eax+8]
2369
	mov	eax,[eax+8]
2367
	neg	eax
2370
	neg	eax
2368
	add	eax,edi
2371
	add	eax,edi
2369
	mov	[ebx-0Ch+4],eax
2372
	mov	[ebx-0Ch+4],eax
2370
	mov	eax,[symbol_identifier]
2373
	mov	eax,[symbol_identifier]
2371
	mov	[ebx-0Ch+8],eax
2374
	mov	[ebx-0Ch+8],eax
2372
	pop	eax ebx
2375
	pop	eax ebx
2373
	ret
2376
	ret
2374
close_coff:
2377
close_coff:
2375
	call	close_coff_section
2378
	call	close_coff_section
2376
	cmp	[next_pass_needed],0
2379
	cmp	[next_pass_needed],0
2377
	je	coff_closed
2380
	je	coff_closed
2378
	mov	eax,[symbols_stream]
2381
	mov	eax,[symbols_stream]
2379
	mov	[free_additional_memory],eax
2382
	mov	[free_additional_memory],eax
2380
      coff_closed:
2383
      coff_closed:
2381
	ret
2384
	ret
2382
coff_formatter:
2385
coff_formatter:
2383
	sub	edi,[code_start]
2386
	sub	edi,[code_start]
2384
	mov	[code_size],edi
2387
	mov	[code_size],edi
2385
	call	prepare_default_section
2388
	call	prepare_default_section
2386
	mov	edi,[free_additional_memory]
2389
	mov	edi,[free_additional_memory]
2387
	mov	ebx,edi
2390
	mov	ebx,edi
2388
	mov	ecx,28h shr 2
2391
	mov	ecx,28h shr 2
2389
	imul	ecx,[number_of_sections]
2392
	imul	ecx,[number_of_sections]
2390
	add	ecx,14h shr 2
2393
	add	ecx,14h shr 2
2391
	lea	eax,[edi+ecx*4]
2394
	lea	eax,[edi+ecx*4]
2392
	cmp	eax,[structures_buffer]
2395
	cmp	eax,[structures_buffer]
2393
	jae	out_of_memory
2396
	jae	out_of_memory
2394
	xor	eax,eax
2397
	xor	eax,eax
2395
	rep	stos dword [edi]
2398
	rep	stos dword [edi]
2396
	mov	word [ebx],14Ch
2399
	mov	word [ebx],14Ch
2397
	test	[format_flags],8
2400
	test	[format_flags],8
2398
	jz	coff_magic_ok
2401
	jz	coff_magic_ok
2399
	mov	word [ebx],8664h
2402
	mov	word [ebx],8664h
2400
      coff_magic_ok:
2403
      coff_magic_ok:
2401
	mov	word [ebx+12h],104h
2404
	mov	word [ebx+12h],104h
2402
	bt	[format_flags],0
2405
	bt	[format_flags],0
2403
	jnc	coff_flags_ok
2406
	jnc	coff_flags_ok
2404
	or	byte [ebx+12h],80h
2407
	or	byte [ebx+12h],80h
2405
      coff_flags_ok:
2408
      coff_flags_ok:
2406
	push	ebx
2409
	push	ebx
2407
	call	make_timestamp
2410
	call	make_timestamp
2408
	pop	ebx
2411
	pop	ebx
2409
	mov	[ebx+4],eax
2412
	mov	[ebx+4],eax
2410
	mov	eax,[number_of_sections]
2413
	mov	eax,[number_of_sections]
2411
	mov	[ebx+2],ax
2414
	mov	[ebx+2],ax
2412
	mov	esi,[symbols_stream]
2415
	mov	esi,[symbols_stream]
2413
	xor	eax,eax
2416
	xor	eax,eax
2414
	xor	ecx,ecx
2417
	xor	ecx,ecx
2415
      enumerate_symbols:
2418
      enumerate_symbols:
2416
	cmp	esi,[free_additional_memory]
2419
	cmp	esi,[free_additional_memory]
2417
	je	symbols_enumerated
2420
	je	symbols_enumerated
2418
	mov	dl,[esi]
2421
	mov	dl,[esi]
2419
	or	dl,dl
2422
	or	dl,dl
2420
	jz	enumerate_section
2423
	jz	enumerate_section
2421
	cmp	dl,0C0h
2424
	cmp	dl,0C0h
2422
	jae	enumerate_public
2425
	jae	enumerate_public
2423
	cmp	dl,80h
2426
	cmp	dl,80h
2424
	jae	enumerate_extrn
2427
	jae	enumerate_extrn
2425
	add	esi,0Ch
2428
	add	esi,0Ch
2426
	jmp	enumerate_symbols
2429
	jmp	enumerate_symbols
2427
      enumerate_section:
2430
      enumerate_section:
2428
	mov	edx,eax
2431
	mov	edx,eax
2429
	shl	edx,8
2432
	shl	edx,8
2430
	mov	[esi],edx
2433
	mov	[esi],edx
2431
	inc	eax
2434
	inc	eax
2432
	inc	ecx
2435
	inc	ecx
2433
	mov	[esi+1Eh],cx
2436
	mov	[esi+1Eh],cx
2434
	add	esi,20h
2437
	add	esi,20h
2435
	jmp	enumerate_symbols
2438
	jmp	enumerate_symbols
2436
      enumerate_public:
2439
      enumerate_public:
2437
	mov	edx,eax
2440
	mov	edx,eax
2438
	shl	edx,8
2441
	shl	edx,8
2439
	mov	dl,[esi]
2442
	mov	dl,[esi]
2440
	mov	[esi],edx
2443
	mov	[esi],edx
2441
	mov	edx,[esi+8]
2444
	mov	edx,[esi+8]
2442
	add	esi,10h
2445
	add	esi,10h
2443
	inc	eax
2446
	inc	eax
2444
	cmp	byte [edx+11],2
2447
	cmp	byte [edx+11],2
2445
	jne	enumerate_symbols
2448
	jne	enumerate_symbols
2446
	mov	edx,[edx+20]
2449
	mov	edx,[edx+20]
2447
	cmp	byte [edx],0C0h
2450
	cmp	byte [edx],0C0h
2448
	jae	enumerate_symbols
2451
	jae	enumerate_symbols
2449
	cmp	byte [edx],80h
2452
	cmp	byte [edx],80h
2450
	jb	enumerate_symbols
2453
	jb	enumerate_symbols
2451
	inc	eax
2454
	inc	eax
2452
	jmp	enumerate_symbols
2455
	jmp	enumerate_symbols
2453
      enumerate_extrn:
2456
      enumerate_extrn:
2454
	mov	edx,eax
2457
	mov	edx,eax
2455
	shl	edx,8
2458
	shl	edx,8
2456
	mov	dl,[esi]
2459
	mov	dl,[esi]
2457
	mov	[esi],edx
2460
	mov	[esi],edx
2458
	add	esi,0Ch
2461
	add	esi,0Ch
2459
	inc	eax
2462
	inc	eax
2460
	jmp	enumerate_symbols
2463
	jmp	enumerate_symbols
2461
      prepare_default_section:
2464
      prepare_default_section:
2462
	mov	ebx,[symbols_stream]
2465
	mov	ebx,[symbols_stream]
2463
	cmp	dword [ebx+0Ch],0
2466
	cmp	dword [ebx+0Ch],0
2464
	jne	default_section_ok
2467
	jne	default_section_ok
2465
	cmp	[number_of_sections],0
2468
	cmp	[number_of_sections],0
2466
	je	default_section_ok
2469
	je	default_section_ok
2467
	mov	edx,ebx
2470
	mov	edx,ebx
2468
      find_references_to_default_section:
2471
      find_references_to_default_section:
2469
	cmp	ebx,[free_additional_memory]
2472
	cmp	ebx,[free_additional_memory]
2470
	jne	check_reference
2473
	jne	check_reference
2471
	add	[symbols_stream],20h
2474
	add	[symbols_stream],20h
2472
	ret
2475
	ret
2473
      check_reference:
2476
      check_reference:
2474
	mov	al,[ebx]
2477
	mov	al,[ebx]
2475
	or	al,al
2478
	or	al,al
2476
	jz	skip_other_section
2479
	jz	skip_other_section
2477
	cmp	al,0C0h
2480
	cmp	al,0C0h
2478
	jae	check_public_reference
2481
	jae	check_public_reference
2479
	cmp	al,80h
2482
	cmp	al,80h
2480
	jae	next_reference
2483
	jae	next_reference
2481
	cmp	edx,[ebx+8]
2484
	cmp	edx,[ebx+8]
2482
	je	default_section_ok
2485
	je	default_section_ok
2483
      next_reference:
2486
      next_reference:
2484
	add	ebx,0Ch
2487
	add	ebx,0Ch
2485
	jmp	find_references_to_default_section
2488
	jmp	find_references_to_default_section
2486
      check_public_reference:
2489
      check_public_reference:
2487
	mov	eax,[ebx+8]
2490
	mov	eax,[ebx+8]
2488
	add	ebx,10h
2491
	add	ebx,10h
2489
	test	byte [eax+8],1
2492
	test	byte [eax+8],1
2490
	jz	find_references_to_default_section
2493
	jz	find_references_to_default_section
2491
	mov	cx,[current_pass]
2494
	mov	cx,[current_pass]
2492
	cmp	cx,[eax+16]
2495
	cmp	cx,[eax+16]
2493
	jne	find_references_to_default_section
2496
	jne	find_references_to_default_section
2494
	cmp	edx,[eax+20]
2497
	cmp	edx,[eax+20]
2495
	je	default_section_ok
2498
	je	default_section_ok
2496
	jmp	find_references_to_default_section
2499
	jmp	find_references_to_default_section
2497
      skip_other_section:
2500
      skip_other_section:
2498
	add	ebx,20h
2501
	add	ebx,20h
2499
	jmp	find_references_to_default_section
2502
	jmp	find_references_to_default_section
2500
      default_section_ok:
2503
      default_section_ok:
2501
	inc	[number_of_sections]
2504
	inc	[number_of_sections]
2502
	ret
2505
	ret
2503
      symbols_enumerated:
2506
      symbols_enumerated:
2504
	mov	[ebx+0Ch],eax
2507
	mov	[ebx+0Ch],eax
2505
	mov	ebp,edi
2508
	mov	ebp,edi
2506
	sub	ebp,ebx
2509
	sub	ebp,ebx
2507
	push	ebp
2510
	push	ebp
2508
	lea	edi,[ebx+14h]
2511
	lea	edi,[ebx+14h]
2509
	mov	esi,[symbols_stream]
2512
	mov	esi,[symbols_stream]
2510
      find_section:
2513
      find_section:
2511
	cmp	esi,[free_additional_memory]
2514
	cmp	esi,[free_additional_memory]
2512
	je	sections_finished
2515
	je	sections_finished
2513
	mov	al,[esi]
2516
	mov	al,[esi]
2514
	or	al,al
2517
	or	al,al
2515
	jz	section_found
2518
	jz	section_found
2516
	add	esi,0Ch
2519
	add	esi,0Ch
2517
	cmp	al,0C0h
2520
	cmp	al,0C0h
2518
	jb	find_section
2521
	jb	find_section
2519
	add	esi,4
2522
	add	esi,4
2520
	jmp	find_section
2523
	jmp	find_section
2521
      section_found:
2524
      section_found:
2522
	push	esi edi
2525
	push	esi edi
2523
	mov	esi,[esi+4]
2526
	mov	esi,[esi+4]
2524
	or	esi,esi
2527
	or	esi,esi
2525
	jz	default_section
2528
	jz	default_section
2526
	mov	ecx,[esi]
2529
	mov	ecx,[esi]
2527
	add	esi,4
2530
	add	esi,4
2528
	rep	movs byte [edi],[esi]
2531
	rep	movs byte [edi],[esi]
2529
	jmp	section_name_ok
2532
	jmp	section_name_ok
2530
      default_section:
2533
      default_section:
2531
	mov	al,'.'
2534
	mov	al,'.'
2532
	stos	byte [edi]
2535
	stos	byte [edi]
2533
	mov	eax,'flat'
2536
	mov	eax,'flat'
2534
	stos	dword [edi]
2537
	stos	dword [edi]
2535
      section_name_ok:
2538
      section_name_ok:
2536
	pop	edi esi
2539
	pop	edi esi
2537
	mov	eax,[esi+0Ch]
2540
	mov	eax,[esi+0Ch]
2538
	mov	[edi+10h],eax
2541
	mov	[edi+10h],eax
2539
	mov	eax,[esi+14h]
2542
	mov	eax,[esi+14h]
2540
	mov	[edi+24h],eax
2543
	mov	[edi+24h],eax
2541
	test	al,80h
2544
	test	al,80h
2542
	jnz	section_ptr_ok
2545
	jnz	section_ptr_ok
2543
	mov	eax,[esi+8]
2546
	mov	eax,[esi+8]
2544
	sub	eax,[code_start]
2547
	sub	eax,[code_start]
2545
	add	eax,ebp
2548
	add	eax,ebp
2546
	mov	[edi+14h],eax
2549
	mov	[edi+14h],eax
2547
      section_ptr_ok:
2550
      section_ptr_ok:
2548
	mov	ebx,[code_start]
2551
	mov	ebx,[code_start]
2549
	mov	edx,[code_size]
2552
	mov	edx,[code_size]
2550
	add	ebx,edx
2553
	add	ebx,edx
2551
	add	edx,ebp
2554
	add	edx,ebp
2552
	xor	ecx,ecx
2555
	xor	ecx,ecx
2553
	add	esi,20h
2556
	add	esi,20h
2554
      find_relocations:
2557
      find_relocations:
2555
	cmp	esi,[free_additional_memory]
2558
	cmp	esi,[free_additional_memory]
2556
	je	section_relocations_done
2559
	je	section_relocations_done
2557
	mov	al,[esi]
2560
	mov	al,[esi]
2558
	or	al,al
2561
	or	al,al
2559
	jz	section_relocations_done
2562
	jz	section_relocations_done
2560
	cmp	al,80h
2563
	cmp	al,80h
2561
	jb	add_relocation
2564
	jb	add_relocation
2562
	cmp	al,0C0h
2565
	cmp	al,0C0h
2563
	jb	next_relocation
2566
	jb	next_relocation
2564
	add	esi,10h
2567
	add	esi,10h
2565
	jmp	find_relocations
2568
	jmp	find_relocations
2566
      add_relocation:
2569
      add_relocation:
2567
	lea	eax,[ebx+0Ah]
2570
	lea	eax,[ebx+0Ah]
2568
	cmp	eax,[display_buffer]
2571
	cmp	eax,[display_buffer]
2569
	ja	out_of_memory
2572
	ja	out_of_memory
2570
	mov	eax,[esi+4]
2573
	mov	eax,[esi+4]
2571
	mov	[ebx],eax
2574
	mov	[ebx],eax
2572
	mov	eax,[esi+8]
2575
	mov	eax,[esi+8]
2573
	mov	eax,[eax]
2576
	mov	eax,[eax]
2574
	shr	eax,8
2577
	shr	eax,8
2575
	mov	[ebx+4],eax
2578
	mov	[ebx+4],eax
2576
	movzx	ax,byte [esi]
2579
	movzx	ax,byte [esi]
2577
	mov	[ebx+8],ax
2580
	mov	[ebx+8],ax
2578
	add	ebx,0Ah
2581
	add	ebx,0Ah
2579
	inc	ecx
2582
	inc	ecx
2580
      next_relocation:
2583
      next_relocation:
2581
	add	esi,0Ch
2584
	add	esi,0Ch
2582
	jmp	find_relocations
2585
	jmp	find_relocations
2583
      section_relocations_done:
2586
      section_relocations_done:
2584
	cmp	ecx,10000h
2587
	cmp	ecx,10000h
2585
	jb	section_relocations_count_16bit
2588
	jb	section_relocations_count_16bit
2586
	bt	[format_flags],0
2589
	bt	[format_flags],0
2587
	jnc	format_limitations_exceeded
2590
	jnc	format_limitations_exceeded
2588
	mov	word [edi+20h],0FFFFh
2591
	mov	word [edi+20h],0FFFFh
2589
	or	dword [edi+24h],1000000h
2592
	or	dword [edi+24h],1000000h
2590
	mov	[edi+18h],edx
2593
	mov	[edi+18h],edx
2591
	push	esi edi
2594
	push	esi edi
2592
	push	ecx
2595
	push	ecx
2593
	lea	esi,[ebx-1]
2596
	lea	esi,[ebx-1]
2594
	add	ebx,0Ah
2597
	add	ebx,0Ah
2595
	lea	edi,[ebx-1]
2598
	lea	edi,[ebx-1]
2596
	imul	ecx,0Ah
2599
	imul	ecx,0Ah
2597
	std
2600
	std
2598
	rep	movs byte [edi],[esi]
2601
	rep	movs byte [edi],[esi]
2599
	cld
2602
	cld
2600
	pop	ecx
2603
	pop	ecx
2601
	inc	esi
2604
	inc	esi
2602
	inc	ecx
2605
	inc	ecx
2603
	mov	[esi],ecx
2606
	mov	[esi],ecx
2604
	xor	eax,eax
2607
	xor	eax,eax
2605
	mov	[esi+4],eax
2608
	mov	[esi+4],eax
2606
	mov	[esi+8],ax
2609
	mov	[esi+8],ax
2607
	pop	edi esi
2610
	pop	edi esi
2608
	jmp	section_relocations_ok
2611
	jmp	section_relocations_ok
2609
      section_relocations_count_16bit:
2612
      section_relocations_count_16bit:
2610
	mov	[edi+20h],cx
2613
	mov	[edi+20h],cx
2611
	jcxz	section_relocations_ok
2614
	jcxz	section_relocations_ok
2612
	mov	[edi+18h],edx
2615
	mov	[edi+18h],edx
2613
      section_relocations_ok:
2616
      section_relocations_ok:
2614
	sub	ebx,[code_start]
2617
	sub	ebx,[code_start]
2615
	mov	[code_size],ebx
2618
	mov	[code_size],ebx
2616
	add	edi,28h
2619
	add	edi,28h
2617
	jmp	find_section
2620
	jmp	find_section
2618
      sections_finished:
2621
      sections_finished:
2619
	mov	edx,[free_additional_memory]
2622
	mov	edx,[free_additional_memory]
2620
	mov	ebx,[code_size]
2623
	mov	ebx,[code_size]
2621
	add	ebp,ebx
2624
	add	ebp,ebx
2622
	mov	[edx+8],ebp
2625
	mov	[edx+8],ebp
2623
	add	ebx,[code_start]
2626
	add	ebx,[code_start]
2624
	mov	edi,ebx
2627
	mov	edi,ebx
2625
	mov	ecx,[edx+0Ch]
2628
	mov	ecx,[edx+0Ch]
2626
	imul	ecx,12h shr 1
2629
	imul	ecx,12h shr 1
2627
	xor	eax,eax
2630
	xor	eax,eax
2628
	shr	ecx,1
2631
	shr	ecx,1
2629
	jnc	zero_symbols_table
2632
	jnc	zero_symbols_table
2630
	stos	word [edi]
2633
	stos	word [edi]
2631
      zero_symbols_table:
2634
      zero_symbols_table:
2632
	rep	stos dword [edi]
2635
	rep	stos dword [edi]
2633
	mov	edx,edi
2636
	mov	edx,edi
2634
	stos	dword [edi]
2637
	stos	dword [edi]
2635
	mov	esi,[symbols_stream]
2638
	mov	esi,[symbols_stream]
2636
      make_symbols_table:
2639
      make_symbols_table:
2637
	cmp	esi,[free_additional_memory]
2640
	cmp	esi,[free_additional_memory]
2638
	je	symbols_table_ok
2641
	je	symbols_table_ok
2639
	mov	al,[esi]
2642
	mov	al,[esi]
2640
	cmp	al,0C0h
2643
	cmp	al,0C0h
2641
	jae	add_public_symbol
2644
	jae	add_public_symbol
2642
	cmp	al,80h
2645
	cmp	al,80h
2643
	jae	add_extrn_symbol
2646
	jae	add_extrn_symbol
2644
	or	al,al
2647
	or	al,al
2645
	jz	add_section_symbol
2648
	jz	add_section_symbol
2646
	add	esi,0Ch
2649
	add	esi,0Ch
2647
	jmp	make_symbols_table
2650
	jmp	make_symbols_table
2648
      add_section_symbol:
2651
      add_section_symbol:
2649
	call	store_symbol_name
2652
	call	store_symbol_name
2650
	movzx	eax,word [esi+1Eh]
2653
	movzx	eax,word [esi+1Eh]
2651
	mov	[ebx+0Ch],ax
2654
	mov	[ebx+0Ch],ax
2652
	mov	byte [ebx+10h],3
2655
	mov	byte [ebx+10h],3
2653
	add	esi,20h
2656
	add	esi,20h
2654
	add	ebx,12h
2657
	add	ebx,12h
2655
	jmp	make_symbols_table
2658
	jmp	make_symbols_table
2656
      add_extrn_symbol:
2659
      add_extrn_symbol:
2657
	call	store_symbol_name
2660
	call	store_symbol_name
2658
	mov	byte [ebx+10h],2
2661
	mov	byte [ebx+10h],2
2659
	add	esi,0Ch
2662
	add	esi,0Ch
2660
	add	ebx,12h
2663
	add	ebx,12h
2661
	jmp	make_symbols_table
2664
	jmp	make_symbols_table
2662
      add_public_symbol:
2665
      add_public_symbol:
2663
	call	store_symbol_name
2666
	call	store_symbol_name
2664
	mov	eax,[esi+0Ch]
2667
	mov	eax,[esi+0Ch]
2665
	mov	[current_line],eax
2668
	mov	[current_line],eax
2666
	mov	eax,[esi+8]
2669
	mov	eax,[esi+8]
2667
	test	byte [eax+8],1
2670
	test	byte [eax+8],1
2668
	jz	undefined_coff_public
2671
	jz	undefined_coff_public
2669
	mov	cx,[current_pass]
2672
	mov	cx,[current_pass]
2670
	cmp	cx,[eax+16]
2673
	cmp	cx,[eax+16]
2671
	jne	undefined_coff_public
2674
	jne	undefined_coff_public
2672
	mov	cl,[eax+11]
2675
	mov	cl,[eax+11]
2673
	or	cl,cl
2676
	or	cl,cl
2674
	jz	public_constant
2677
	jz	public_constant
2675
	test	[format_flags],8
2678
	test	[format_flags],8
2676
	jnz	check_64bit_public_symbol
2679
	jnz	check_64bit_public_symbol
2677
	cmp	cl,2
2680
	cmp	cl,2
2678
	je	public_symbol_type_ok
2681
	je	public_symbol_type_ok
2679
	jmp	invalid_use_of_symbol
2682
	jmp	invalid_use_of_symbol
2680
      undefined_coff_public:
2683
      undefined_coff_public:
2681
	mov	eax,[eax+24]
2684
	mov	eax,[eax+24]
2682
	mov	[error_info],eax
2685
	mov	[error_info],eax
2683
	jmp	undefined_symbol
2686
	jmp	undefined_symbol
2684
      check_64bit_public_symbol:
2687
      check_64bit_public_symbol:
2685
	cmp	cl,4
2688
	cmp	cl,4
2686
	jne	invalid_use_of_symbol
2689
	jne	invalid_use_of_symbol
2687
      public_symbol_type_ok:
2690
      public_symbol_type_ok:
2688
	mov	ecx,[eax+20]
2691
	mov	ecx,[eax+20]
2689
	cmp	byte [ecx],80h
2692
	cmp	byte [ecx],80h
2690
	je	alias_symbol
2693
	je	alias_symbol
2691
	cmp	byte [ecx],0
2694
	cmp	byte [ecx],0
2692
	jne	invalid_use_of_symbol
2695
	jne	invalid_use_of_symbol
2693
	mov	cx,[ecx+1Eh]
2696
	mov	cx,[ecx+1Eh]
2694
	mov	[ebx+0Ch],cx
2697
	mov	[ebx+0Ch],cx
2695
      public_symbol_section_ok:
2698
      public_symbol_section_ok:
2696
	cmp	dword [eax+4],0
2699
	cmp	dword [eax+4],0
2697
	je	store_public_symbol
2700
	je	store_public_symbol
2698
	cmp	dword [eax+4],-1
2701
	cmp	dword [eax+4],-1
2699
	jne	value_out_of_range
2702
	jne	value_out_of_range
2700
	bt	dword [eax],31
2703
	bt	dword [eax],31
2701
	jnc	value_out_of_range
2704
	jnc	value_out_of_range
2702
      store_public_symbol:
2705
      store_public_symbol:
2703
	mov	eax,[eax]
2706
	mov	eax,[eax]
2704
	mov	[ebx+8],eax
2707
	mov	[ebx+8],eax
2705
	mov	al,2
2708
	mov	al,2
2706
	cmp	byte [esi],0C0h
2709
	cmp	byte [esi],0C0h
2707
	je	store_symbol_class
2710
	je	store_symbol_class
2708
	inc	al
2711
	inc	al
2709
	cmp	byte [esi],0C1h
2712
	cmp	byte [esi],0C1h
2710
	je	store_symbol_class
2713
	je	store_symbol_class
2711
	mov	al,105
2714
	mov	al,105
2712
      store_symbol_class:
2715
      store_symbol_class:
2713
	mov	byte [ebx+10h],al
2716
	mov	byte [ebx+10h],al
2714
	add	esi,10h
2717
	add	esi,10h
2715
	add	ebx,12h
2718
	add	ebx,12h
2716
	jmp	make_symbols_table
2719
	jmp	make_symbols_table
2717
      alias_symbol:
2720
      alias_symbol:
2718
	bt	[format_flags],0
2721
	bt	[format_flags],0
2719
	jnc	invalid_use_of_symbol
2722
	jnc	invalid_use_of_symbol
2720
	mov	ecx,[eax]
2723
	mov	ecx,[eax]
2721
	or	ecx,[eax+4]
2724
	or	ecx,[eax+4]
2722
	jnz	invalid_use_of_symbol
2725
	jnz	invalid_use_of_symbol
2723
	mov	byte [ebx+10h],69h
2726
	mov	byte [ebx+10h],69h
2724
	mov	byte [ebx+11h],1
2727
	mov	byte [ebx+11h],1
2725
	add	ebx,12h
2728
	add	ebx,12h
2726
	mov	ecx,[eax+20]
2729
	mov	ecx,[eax+20]
2727
	mov	ecx,[ecx]
2730
	mov	ecx,[ecx]
2728
	shr	ecx,8
2731
	shr	ecx,8
2729
	mov	[ebx],ecx
2732
	mov	[ebx],ecx
2730
	mov	byte [ebx+4],3
2733
	mov	byte [ebx+4],3
2731
	add	esi,10h
2734
	add	esi,10h
2732
	add	ebx,12h
2735
	add	ebx,12h
2733
	jmp	make_symbols_table
2736
	jmp	make_symbols_table
2734
      public_constant:
2737
      public_constant:
2735
	mov	word [ebx+0Ch],0FFFFh
2738
	mov	word [ebx+0Ch],0FFFFh
2736
	jmp	public_symbol_section_ok
2739
	jmp	public_symbol_section_ok
2737
      symbols_table_ok:
2740
      symbols_table_ok:
2738
	mov	eax,edi
2741
	mov	eax,edi
2739
	sub	eax,edx
2742
	sub	eax,edx
2740
	mov	[edx],eax
2743
	mov	[edx],eax
2741
	sub	edi,[code_start]
2744
	sub	edi,[code_start]
2742
	mov	[code_size],edi
2745
	mov	[code_size],edi
2743
	mov	[written_size],0
2746
	mov	[written_size],0
2744
	mov	edx,[output_file]
2747
	mov	edx,[output_file]
2745
	call	create
2748
	call	create
2746
	jc	write_failed
2749
	jc	write_failed
2747
	mov	edx,[free_additional_memory]
2750
	mov	edx,[free_additional_memory]
2748
	pop	ecx
2751
	pop	ecx
2749
	add	[written_size],ecx
2752
	add	[written_size],ecx
2750
	call	write
2753
	call	write
2751
	jc	write_failed
2754
	jc	write_failed
2752
	jmp	write_output
2755
	jmp	write_output
2753
      store_symbol_name:
2756
      store_symbol_name:
2754
	push	esi
2757
	push	esi
2755
	mov	esi,[esi+4]
2758
	mov	esi,[esi+4]
2756
	or	esi,esi
2759
	or	esi,esi
2757
	jz	default_name
2760
	jz	default_name
2758
	lods	dword [esi]
2761
	lods	dword [esi]
2759
	mov	ecx,eax
2762
	mov	ecx,eax
2760
	cmp	ecx,8
2763
	cmp	ecx,8
2761
	ja	add_string
2764
	ja	add_string
2762
	push	edi
2765
	push	edi
2763
	mov	edi,ebx
2766
	mov	edi,ebx
2764
	rep	movs byte [edi],[esi]
2767
	rep	movs byte [edi],[esi]
2765
	pop	edi esi
2768
	pop	edi esi
2766
	ret
2769
	ret
2767
      default_name:
2770
      default_name:
2768
	mov	dword [ebx],'.fla'
2771
	mov	dword [ebx],'.fla'
2769
	mov	dword [ebx+4],'t'
2772
	mov	dword [ebx+4],'t'
2770
	pop	esi
2773
	pop	esi
2771
	ret
2774
	ret
2772
      add_string:
2775
      add_string:
2773
	mov	eax,edi
2776
	mov	eax,edi
2774
	sub	eax,edx
2777
	sub	eax,edx
2775
	mov	[ebx+4],eax
2778
	mov	[ebx+4],eax
2776
	inc	ecx
2779
	inc	ecx
2777
	rep	movs byte [edi],[esi]
2780
	rep	movs byte [edi],[esi]
2778
	pop	esi
2781
	pop	esi
2779
	ret
2782
	ret
2780
 
2783
 
2781
format_elf:
2784
format_elf:
2782
	test	[format_flags],8
2785
	test	[format_flags],8
2783
	jnz	format_elf64
2786
	jnz	format_elf64
2784
	mov	edx,edi
2787
	mov	edx,edi
2785
	mov	ecx,34h shr 2
2788
	mov	ecx,34h shr 2
2786
	lea	eax,[edi+ecx*4]
2789
	lea	eax,[edi+ecx*4]
2787
	cmp	eax,[display_buffer]
2790
	cmp	eax,[display_buffer]
2788
	jae	out_of_memory
2791
	jae	out_of_memory
2789
	xor	eax,eax
2792
	xor	eax,eax
2790
	rep	stos dword [edi]
2793
	rep	stos dword [edi]
2791
	mov	dword [edx],7Fh + 'ELF' shl 8
2794
	mov	dword [edx],7Fh + 'ELF' shl 8
2792
	mov	al,1
2795
	mov	al,1
2793
	mov	[edx+4],al
2796
	mov	[edx+4],al
2794
	mov	[edx+5],al
2797
	mov	[edx+5],al
2795
	mov	[edx+6],al
2798
	mov	[edx+6],al
2796
	mov	[edx+14h],al
2799
	mov	[edx+14h],al
2797
	mov	byte [edx+12h],3
2800
	mov	byte [edx+12h],3
2798
	mov	byte [edx+28h],34h
2801
	mov	byte [edx+28h],34h
2799
	mov	byte [edx+2Eh],28h
2802
	mov	byte [edx+2Eh],28h
2800
	mov	[code_type],32
2803
	mov	[code_type],32
2801
	cmp	word [esi],1D19h
2804
	cmp	word [esi],1D19h
2802
	je	format_elf_exe
2805
	je	format_elf_exe
2803
	mov	[labels_type],2
2806
	mov	[labels_type],2
2804
      elf_header_ok:
2807
      elf_header_ok:
2805
	mov	byte [edx+10h],1
2808
	mov	byte [edx+10h],1
2806
	mov	eax,[additional_memory]
2809
	mov	eax,[additional_memory]
2807
	mov	[symbols_stream],eax
2810
	mov	[symbols_stream],eax
2808
	mov	ebx,eax
2811
	mov	ebx,eax
2809
	add	eax,20h
2812
	add	eax,20h
2810
	cmp	eax,[structures_buffer]
2813
	cmp	eax,[structures_buffer]
2811
	jae	out_of_memory
2814
	jae	out_of_memory
2812
	mov	[free_additional_memory],eax
2815
	mov	[free_additional_memory],eax
2813
	xor	eax,eax
2816
	xor	eax,eax
2814
	mov	[current_section],ebx
2817
	mov	[current_section],ebx
2815
	mov	[number_of_sections],eax
2818
	mov	[number_of_sections],eax
2816
	mov	dword [org_origin],edi
2819
	mov	dword [org_origin],edi
2817
	mov	dword [org_origin+4],eax
2820
	mov	dword [org_origin+4],eax
2818
	mov	[org_registers],eax
2821
	mov	[org_registers],eax
2819
	mov	[org_start],edi
2822
	mov	[org_start],edi
2820
	mov	[org_symbol],ebx
2823
	mov	[org_symbol],ebx
2821
	mov	[ebx],al
2824
	mov	[ebx],al
2822
	mov	[ebx+4],eax
2825
	mov	[ebx+4],eax
2823
	mov	[ebx+8],edi
2826
	mov	[ebx+8],edi
2824
	mov	al,111b
2827
	mov	al,111b
2825
	mov	[ebx+14h],eax
2828
	mov	[ebx+14h],eax
2826
	mov	al,4
2829
	mov	al,4
2827
	mov	[ebx+10h],eax
2830
	mov	[ebx+10h],eax
2828
	test	[format_flags],8
2831
	test	[format_flags],8
2829
	jz	format_defined
2832
	jz	format_defined
2830
	mov	byte [ebx+10h],8
2833
	mov	byte [ebx+10h],8
2831
	jmp	format_defined
2834
	jmp	format_defined
2832
      format_elf64:
2835
      format_elf64:
2833
	mov	edx,edi
2836
	mov	edx,edi
2834
	mov	ecx,40h shr 2
2837
	mov	ecx,40h shr 2
2835
	lea	eax,[edi+ecx*4]
2838
	lea	eax,[edi+ecx*4]
2836
	cmp	eax,[display_buffer]
2839
	cmp	eax,[display_buffer]
2837
	jae	out_of_memory
2840
	jae	out_of_memory
2838
	xor	eax,eax
2841
	xor	eax,eax
2839
	rep	stos dword [edi]
2842
	rep	stos dword [edi]
2840
	mov	dword [edx],7Fh + 'ELF' shl 8
2843
	mov	dword [edx],7Fh + 'ELF' shl 8
2841
	mov	al,1
2844
	mov	al,1
2842
	mov	[edx+5],al
2845
	mov	[edx+5],al
2843
	mov	[edx+6],al
2846
	mov	[edx+6],al
2844
	mov	[edx+14h],al
2847
	mov	[edx+14h],al
2845
	mov	byte [edx+4],2
2848
	mov	byte [edx+4],2
2846
	mov	byte [edx+12h],62
2849
	mov	byte [edx+12h],62
2847
	mov	byte [edx+34h],40h
2850
	mov	byte [edx+34h],40h
2848
	mov	byte [edx+3Ah],40h
2851
	mov	byte [edx+3Ah],40h
2849
	mov	[code_type],64
2852
	mov	[code_type],64
2850
	cmp	word [esi],1D19h
2853
	cmp	word [esi],1D19h
2851
	je	format_elf64_exe
2854
	je	format_elf64_exe
2852
	mov	[labels_type],4
2855
	mov	[labels_type],4
2853
	jmp	elf_header_ok
2856
	jmp	elf_header_ok
2854
elf_section:
2857
elf_section:
2855
	bt	[format_flags],0
2858
	bt	[format_flags],0
2856
	jc	illegal_instruction
2859
	jc	illegal_instruction
2857
	call	close_coff_section
2860
	call	close_coff_section
2858
	mov	ebx,[free_additional_memory]
2861
	mov	ebx,[free_additional_memory]
2859
	lea	eax,[ebx+20h]
2862
	lea	eax,[ebx+20h]
2860
	cmp	eax,[structures_buffer]
2863
	cmp	eax,[structures_buffer]
2861
	jae	out_of_memory
2864
	jae	out_of_memory
2862
	mov	[free_additional_memory],eax
2865
	mov	[free_additional_memory],eax
2863
	mov	[current_section],ebx
2866
	mov	[current_section],ebx
2864
	inc	word [number_of_sections]
2867
	inc	word [number_of_sections]
2865
	jz	format_limitations_exceeded
2868
	jz	format_limitations_exceeded
2866
	xor	eax,eax
2869
	xor	eax,eax
2867
	mov	[ebx],al
2870
	mov	[ebx],al
2868
	mov	[ebx+8],edi
2871
	mov	[ebx+8],edi
2869
	mov	dword [org_origin],edi
2872
	mov	dword [org_origin],edi
2870
	mov	dword [org_origin+4],0
2873
	mov	dword [org_origin+4],0
2871
	mov	[org_registers],0
2874
	mov	[org_registers],0
2872
	mov	[org_start],edi
2875
	mov	[org_start],edi
2873
	mov	[org_symbol],ebx
2876
	mov	[org_symbol],ebx
2874
	test	[format_flags],8
2877
	test	[format_flags],8
2875
	jnz	elf64_labels_type
2878
	jnz	elf64_labels_type
2876
	mov	[labels_type],2
2879
	mov	[labels_type],2
2877
	jmp	elf_labels_type_ok
2880
	jmp	elf_labels_type_ok
2878
      elf64_labels_type:
2881
      elf64_labels_type:
2879
	mov	[labels_type],4
2882
	mov	[labels_type],4
2880
      elf_labels_type_ok:
2883
      elf_labels_type_ok:
2881
	mov	[ebx+10h],eax
2884
	mov	[ebx+10h],eax
2882
	mov	al,10b
2885
	mov	al,10b
2883
	mov	[ebx+14h],eax
2886
	mov	[ebx+14h],eax
2884
	lods	word [esi]
2887
	lods	word [esi]
2885
	cmp	ax,'('
2888
	cmp	ax,'('
2886
	jne	invalid_argument
2889
	jne	invalid_argument
2887
	mov	[ebx+4],esi
2890
	mov	[ebx+4],esi
2888
	mov	ecx,[esi]
2891
	mov	ecx,[esi]
2889
	lea	esi,[esi+4+ecx+1]
2892
	lea	esi,[esi+4+ecx+1]
2890
      elf_section_flags:
2893
      elf_section_flags:
2891
	cmp	byte [esi],1Ch
2894
	cmp	byte [esi],1Ch
2892
	je	elf_section_alignment
2895
	je	elf_section_alignment
2893
	cmp	byte [esi],19h
2896
	cmp	byte [esi],19h
2894
	jne	elf_section_settings_ok
2897
	jne	elf_section_settings_ok
2895
	inc	esi
2898
	inc	esi
2896
	lods	byte [esi]
2899
	lods	byte [esi]
2897
	sub	al,28
2900
	sub	al,28
2898
	xor	al,11b
2901
	xor	al,11b
2899
	test	al,not 10b
2902
	test	al,not 10b
2900
	jnz	invalid_argument
2903
	jnz	invalid_argument
2901
	mov	cl,al
2904
	mov	cl,al
2902
	mov	al,1
2905
	mov	al,1
2903
	shl	al,cl
2906
	shl	al,cl
2904
	test	byte [ebx+14h],al
2907
	test	byte [ebx+14h],al
2905
	jnz	setting_already_specified
2908
	jnz	setting_already_specified
2906
	or	byte [ebx+14h],al
2909
	or	byte [ebx+14h],al
2907
	jmp	elf_section_flags
2910
	jmp	elf_section_flags
2908
      elf_section_alignment:
2911
      elf_section_alignment:
2909
	inc	esi
2912
	inc	esi
2910
	lods	byte [esi]
2913
	lods	byte [esi]
2911
	or	al,al
2914
	or	al,al
2912
	jnz	invalid_argument
2915
	jnz	invalid_argument
2913
	lods	byte [esi]
2916
	lods	byte [esi]
2914
	cmp	al,'('
2917
	cmp	al,'('
2915
	jne	invalid_argument
2918
	jne	invalid_argument
2916
	cmp	byte [esi],'.'
2919
	cmp	byte [esi],'.'
2917
	je	invalid_value
2920
	je	invalid_value
2918
	push	ebx
2921
	push	ebx
2919
	call	get_dword_value
2922
	call	get_dword_value
2920
	pop	ebx
2923
	pop	ebx
2921
	cmp	[value_type],0
2924
	cmp	[value_type],0
2922
	jne	invalid_use_of_symbol
2925
	jne	invalid_use_of_symbol
2923
	mov	edx,eax
2926
	mov	edx,eax
2924
	dec	edx
2927
	dec	edx
2925
	test	eax,edx
2928
	test	eax,edx
2926
	jnz	invalid_value
2929
	jnz	invalid_value
2927
	or	eax,eax
2930
	or	eax,eax
2928
	jz	invalid_value
2931
	jz	invalid_value
2929
	xchg	[ebx+10h],eax
2932
	xchg	[ebx+10h],eax
2930
	or	eax,eax
2933
	or	eax,eax
2931
	jnz	setting_already_specified
2934
	jnz	setting_already_specified
2932
	jmp	elf_section_flags
2935
	jmp	elf_section_flags
2933
      elf_section_settings_ok:
2936
      elf_section_settings_ok:
2934
	cmp	dword [ebx+10h],0
2937
	cmp	dword [ebx+10h],0
2935
	jne	instruction_assembled
2938
	jne	instruction_assembled
2936
	mov	dword [ebx+10h],4
2939
	mov	dword [ebx+10h],4
2937
	test	[format_flags],8
2940
	test	[format_flags],8
2938
	jz	instruction_assembled
2941
	jz	instruction_assembled
2939
	mov	byte [ebx+10h],8
2942
	mov	byte [ebx+10h],8
2940
	jmp	instruction_assembled
2943
	jmp	instruction_assembled
2941
mark_elf_relocation:
2944
mark_elf_relocation:
2942
	cmp	[value_type],3
2945
	cmp	[value_type],3
2943
	je	elf_relocation_relative
2946
	je	elf_relocation_relative
2944
	cmp	[value_type],7
2947
	cmp	[value_type],7
2945
	je	elf_relocation_relative
2948
	je	elf_relocation_relative
2946
	push	ebx eax
2949
	push	ebx eax
2947
	cmp	[value_type],5
2950
	cmp	[value_type],5
2948
	je	elf_gotoff_relocation
2951
	je	elf_gotoff_relocation
2949
	ja	invalid_use_of_symbol
2952
	ja	invalid_use_of_symbol
2950
	mov	al,1			; R_386_32 / R_AMD64_64
2953
	mov	al,1			; R_386_32 / R_AMD64_64
2951
	test	[format_flags],8
2954
	test	[format_flags],8
2952
	jz	coff_relocation
2955
	jz	coff_relocation
2953
	cmp	[value_type],4
2956
	cmp	[value_type],4
2954
	je	coff_relocation
2957
	je	coff_relocation
2955
	mov	al,11			; R_AMD64_32S
2958
	mov	al,11			; R_AMD64_32S
2956
	jmp	coff_relocation
2959
	jmp	coff_relocation
2957
      elf_gotoff_relocation:
2960
      elf_gotoff_relocation:
2958
	test	[format_flags],8
2961
	test	[format_flags],8
2959
	jnz	invalid_use_of_symbol
2962
	jnz	invalid_use_of_symbol
2960
	mov	al,9			; R_386_GOTOFF
2963
	mov	al,9			; R_386_GOTOFF
2961
	jmp	coff_relocation
2964
	jmp	coff_relocation
2962
      elf_relocation_relative:
2965
      elf_relocation_relative:
2963
	cmp	[labels_type],0
2966
	cmp	[labels_type],0
2964
	je	invalid_use_of_symbol
2967
	je	invalid_use_of_symbol
2965
	push	ebx
2968
	push	ebx
2966
	mov	ebx,[current_section]
2969
	mov	ebx,[current_section]
2967
	mov	ebx,[ebx+8]
2970
	mov	ebx,[ebx+8]
2968
	sub	ebx,edi
2971
	sub	ebx,edi
2969
	sub	eax,ebx
2972
	sub	eax,ebx
2970
	push	eax
2973
	push	eax
2971
	mov	al,2			; R_386_PC32 / R_AMD64_PC32
2974
	mov	al,2			; R_386_PC32 / R_AMD64_PC32
2972
	cmp	[value_type],3
2975
	cmp	[value_type],3
2973
	je	coff_relocation
2976
	je	coff_relocation
2974
	mov	al,4			; R_386_PLT32 / R_AMD64_PLT32
2977
	mov	al,4			; R_386_PLT32 / R_AMD64_PLT32
2975
	jmp	coff_relocation
2978
	jmp	coff_relocation
2976
close_elf:
2979
close_elf:
2977
	bt	[format_flags],0
2980
	bt	[format_flags],0
2978
	jc	close_elf_exe
2981
	jc	close_elf_exe
2979
	call	close_coff_section
2982
	call	close_coff_section
2980
	cmp	[next_pass_needed],0
2983
	cmp	[next_pass_needed],0
2981
	je	elf_closed
2984
	je	elf_closed
2982
	mov	eax,[symbols_stream]
2985
	mov	eax,[symbols_stream]
2983
	mov	[free_additional_memory],eax
2986
	mov	[free_additional_memory],eax
2984
      elf_closed:
2987
      elf_closed:
2985
	ret
2988
	ret
2986
elf_formatter:
2989
elf_formatter:
2987
	push	edi
2990
	push	edi
2988
	call	prepare_default_section
2991
	call	prepare_default_section
2989
	mov	esi,[symbols_stream]
2992
	mov	esi,[symbols_stream]
2990
	mov	edi,[free_additional_memory]
2993
	mov	edi,[free_additional_memory]
2991
	xor	eax,eax
2994
	xor	eax,eax
2992
	mov	ecx,4
2995
	mov	ecx,4
2993
	rep	stos dword [edi]
2996
	rep	stos dword [edi]
2994
	test	[format_flags],8
2997
	test	[format_flags],8
2995
	jz	find_first_section
2998
	jz	find_first_section
2996
	mov	ecx,2
2999
	mov	ecx,2
2997
	rep	stos dword [edi]
3000
	rep	stos dword [edi]
2998
      find_first_section:
3001
      find_first_section:
2999
	mov	al,[esi]
3002
	mov	al,[esi]
3000
	or	al,al
3003
	or	al,al
3001
	jz	first_section_found
3004
	jz	first_section_found
3002
	cmp	al,0C0h
3005
	cmp	al,0C0h
3003
	jb	skip_other_symbol
3006
	jb	skip_other_symbol
3004
	add	esi,4
3007
	add	esi,4
3005
      skip_other_symbol:
3008
      skip_other_symbol:
3006
	add	esi,0Ch
3009
	add	esi,0Ch
3007
	jmp	find_first_section
3010
	jmp	find_first_section
3008
      first_section_found:
3011
      first_section_found:
3009
	mov	ebx,esi
3012
	mov	ebx,esi
3010
	mov	ebp,esi
3013
	mov	ebp,esi
3011
	add	esi,20h
3014
	add	esi,20h
3012
	xor	ecx,ecx
3015
	xor	ecx,ecx
3013
	xor	edx,edx
3016
	xor	edx,edx
3014
      find_next_section:
3017
      find_next_section:
3015
	cmp	esi,[free_additional_memory]
3018
	cmp	esi,[free_additional_memory]
3016
	je	make_section_symbol
3019
	je	make_section_symbol
3017
	mov	al,[esi]
3020
	mov	al,[esi]
3018
	or	al,al
3021
	or	al,al
3019
	jz	make_section_symbol
3022
	jz	make_section_symbol
3020
	cmp	al,0C0h
3023
	cmp	al,0C0h
3021
	jae	skip_public
3024
	jae	skip_public
3022
	cmp	al,80h
3025
	cmp	al,80h
3023
	jae	skip_extrn
3026
	jae	skip_extrn
3024
	or	byte [ebx+14h],40h
3027
	or	byte [ebx+14h],40h
3025
      skip_extrn:
3028
      skip_extrn:
3026
	add	esi,0Ch
3029
	add	esi,0Ch
3027
	jmp	find_next_section
3030
	jmp	find_next_section
3028
      skip_public:
3031
      skip_public:
3029
	add	esi,10h
3032
	add	esi,10h
3030
	jmp	find_next_section
3033
	jmp	find_next_section
3031
      make_section_symbol:
3034
      make_section_symbol:
3032
	mov	eax,edi
3035
	mov	eax,edi
3033
	xchg	eax,[ebx+4]
3036
	xchg	eax,[ebx+4]
3034
	stos	dword [edi]
3037
	stos	dword [edi]
3035
	test	[format_flags],8
3038
	test	[format_flags],8
3036
	jnz	elf64_section_symbol
3039
	jnz	elf64_section_symbol
3037
	xor	eax,eax
3040
	xor	eax,eax
3038
	stos	dword [edi]
3041
	stos	dword [edi]
3039
	stos	dword [edi]
3042
	stos	dword [edi]
3040
	call	store_section_index
3043
	call	store_section_index
3041
	jmp	section_symbol_ok
3044
	jmp	section_symbol_ok
3042
      store_section_index:
3045
      store_section_index:
3043
	inc	ecx
3046
	inc	ecx
3044
	mov	eax,ecx
3047
	mov	eax,ecx
3045
	shl	eax,8
3048
	shl	eax,8
3046
	mov	[ebx],eax
3049
	mov	[ebx],eax
3047
	inc	dx
3050
	inc	dx
3048
	jz	format_limitations_exceeded
3051
	jz	format_limitations_exceeded
3049
	mov	eax,edx
3052
	mov	eax,edx
3050
	shl	eax,16
3053
	shl	eax,16
3051
	mov	al,3
3054
	mov	al,3
3052
	test	byte [ebx+14h],40h
3055
	test	byte [ebx+14h],40h
3053
	jz	section_index_ok
3056
	jz	section_index_ok
3054
	or	ah,-1
3057
	or	ah,-1
3055
	inc	dx
3058
	inc	dx
3056
	jz	format_limitations_exceeded
3059
	jz	format_limitations_exceeded
3057
      section_index_ok:
3060
      section_index_ok:
3058
	stos	dword [edi]
3061
	stos	dword [edi]
3059
	ret
3062
	ret
3060
      elf64_section_symbol:
3063
      elf64_section_symbol:
3061
	call	store_section_index
3064
	call	store_section_index
3062
	xor	eax,eax
3065
	xor	eax,eax
3063
	stos	dword [edi]
3066
	stos	dword [edi]
3064
	stos	dword [edi]
3067
	stos	dword [edi]
3065
	stos	dword [edi]
3068
	stos	dword [edi]
3066
	stos	dword [edi]
3069
	stos	dword [edi]
3067
      section_symbol_ok:
3070
      section_symbol_ok:
3068
	mov	ebx,esi
3071
	mov	ebx,esi
3069
	add	esi,20h
3072
	add	esi,20h
3070
	cmp	ebx,[free_additional_memory]
3073
	cmp	ebx,[free_additional_memory]
3071
	jne	find_next_section
3074
	jne	find_next_section
3072
	inc	dx
3075
	inc	dx
3073
	jz	format_limitations_exceeded
3076
	jz	format_limitations_exceeded
3074
	mov	[current_section],edx
3077
	mov	[current_section],edx
3075
	mov	esi,[symbols_stream]
3078
	mov	esi,[symbols_stream]
3076
      find_other_symbols:
3079
      find_other_symbols:
3077
	cmp	esi,[free_additional_memory]
3080
	cmp	esi,[free_additional_memory]
3078
	je	elf_symbol_table_ok
3081
	je	elf_symbol_table_ok
3079
	mov	al,[esi]
3082
	mov	al,[esi]
3080
	or	al,al
3083
	or	al,al
3081
	jz	skip_section
3084
	jz	skip_section
3082
	cmp	al,0C0h
3085
	cmp	al,0C0h
3083
	jae	make_public_symbol
3086
	jae	make_public_symbol
3084
	cmp	al,80h
3087
	cmp	al,80h
3085
	jae	make_extrn_symbol
3088
	jae	make_extrn_symbol
3086
	add	esi,0Ch
3089
	add	esi,0Ch
3087
	jmp	find_other_symbols
3090
	jmp	find_other_symbols
3088
      skip_section:
3091
      skip_section:
3089
	add	esi,20h
3092
	add	esi,20h
3090
	jmp	find_other_symbols
3093
	jmp	find_other_symbols
3091
      make_public_symbol:
3094
      make_public_symbol:
3092
	mov	eax,[esi+0Ch]
3095
	mov	eax,[esi+0Ch]
3093
	mov	[current_line],eax
3096
	mov	[current_line],eax
3094
	cmp	byte [esi],0C0h
3097
	cmp	byte [esi],0C0h
3095
	jne	invalid_argument
3098
	jne	invalid_argument
3096
	mov	ebx,[esi+8]
3099
	mov	ebx,[esi+8]
3097
	test	byte [ebx+8],1
3100
	test	byte [ebx+8],1
3098
	jz	undefined_public
3101
	jz	undefined_public
3099
	mov	ax,[current_pass]
3102
	mov	ax,[current_pass]
3100
	cmp	ax,[ebx+16]
3103
	cmp	ax,[ebx+16]
3101
	jne	undefined_public
3104
	jne	undefined_public
3102
	mov	dl,[ebx+11]
3105
	mov	dl,[ebx+11]
3103
	or	dl,dl
3106
	or	dl,dl
3104
	jz	public_absolute
3107
	jz	public_absolute
3105
	mov	eax,[ebx+20]
3108
	mov	eax,[ebx+20]
3106
	cmp	byte [eax],0
3109
	cmp	byte [eax],0
3107
	jne	invalid_use_of_symbol
3110
	jne	invalid_use_of_symbol
3108
	mov	eax,[eax+4]
3111
	mov	eax,[eax+4]
3109
	test	[format_flags],8
3112
	test	[format_flags],8
3110
	jnz	elf64_public
3113
	jnz	elf64_public
3111
	cmp	dl,2
3114
	cmp	dl,2
3112
	jne	invalid_use_of_symbol
3115
	jne	invalid_use_of_symbol
3113
	mov	dx,[eax+0Eh]
3116
	mov	dx,[eax+0Eh]
3114
	jmp	section_for_public_ok
3117
	jmp	section_for_public_ok
3115
      undefined_public:
3118
      undefined_public:
3116
	mov	eax,[ebx+24]
3119
	mov	eax,[ebx+24]
3117
	mov	[error_info],eax
3120
	mov	[error_info],eax
3118
	jmp	undefined_symbol
3121
	jmp	undefined_symbol
3119
      elf64_public:
3122
      elf64_public:
3120
	cmp	dl,4
3123
	cmp	dl,4
3121
	jne	invalid_use_of_symbol
3124
	jne	invalid_use_of_symbol
3122
	mov	dx,[eax+6]
3125
	mov	dx,[eax+6]
3123
	jmp	section_for_public_ok
3126
	jmp	section_for_public_ok
3124
      public_absolute:
3127
      public_absolute:
3125
	mov	dx,0FFF1h
3128
	mov	dx,0FFF1h
3126
      section_for_public_ok:
3129
      section_for_public_ok:
3127
	mov	eax,[esi+4]
3130
	mov	eax,[esi+4]
3128
	stos	dword [edi]
3131
	stos	dword [edi]
3129
	test	[format_flags],8
3132
	test	[format_flags],8
3130
	jnz	elf64_public_symbol
3133
	jnz	elf64_public_symbol
3131
	call	get_public_value
3134
	call	get_public_value
3132
	stos	dword [edi]
3135
	stos	dword [edi]
3133
	xor	eax,eax
3136
	xor	eax,eax
3134
	mov	al,[ebx+10]
3137
	mov	al,[ebx+10]
3135
	stos	dword [edi]
3138
	stos	dword [edi]
3136
	mov	eax,edx
3139
	mov	eax,edx
3137
	shl	eax,16
3140
	shl	eax,16
3138
	mov	al,10h
3141
	mov	al,10h
3139
	cmp	byte [ebx+10],0
3142
	cmp	byte [ebx+10],0
3140
	je	elf_public_function
3143
	je	elf_public_function
3141
	or	al,1
3144
	or	al,1
3142
	jmp	store_elf_public_info
3145
	jmp	store_elf_public_info
3143
      elf_public_function:
3146
      elf_public_function:
3144
	or	al,2
3147
	or	al,2
3145
      store_elf_public_info:
3148
      store_elf_public_info:
3146
	stos	dword [edi]
3149
	stos	dword [edi]
3147
	jmp	public_symbol_ok
3150
	jmp	public_symbol_ok
3148
      elf64_public_symbol:
3151
      elf64_public_symbol:
3149
	mov	eax,edx
3152
	mov	eax,edx
3150
	shl	eax,16
3153
	shl	eax,16
3151
	mov	al,10h
3154
	mov	al,10h
3152
	cmp	byte [ebx+10],0
3155
	cmp	byte [ebx+10],0
3153
	je	elf64_public_function
3156
	je	elf64_public_function
3154
	or	al,1
3157
	or	al,1
3155
	jmp	store_elf64_public_info
3158
	jmp	store_elf64_public_info
3156
      elf64_public_function:
3159
      elf64_public_function:
3157
	or	al,2
3160
	or	al,2
3158
      store_elf64_public_info:
3161
      store_elf64_public_info:
3159
	stos	dword [edi]
3162
	stos	dword [edi]
3160
	call	get_public_value
3163
	call	get_public_value
3161
	stos	dword [edi]
3164
	stos	dword [edi]
3162
	xor	eax,eax
3165
	xor	eax,eax
3163
	stos	dword [edi]
3166
	stos	dword [edi]
3164
	mov	al,[ebx+10]
3167
	mov	al,[ebx+10]
3165
	stos	dword [edi]
3168
	stos	dword [edi]
3166
	xor	al,al
3169
	xor	al,al
3167
	stos	dword [edi]
3170
	stos	dword [edi]
3168
      public_symbol_ok:
3171
      public_symbol_ok:
3169
	inc	ecx
3172
	inc	ecx
3170
	mov	eax,ecx
3173
	mov	eax,ecx
3171
	shl	eax,8
3174
	shl	eax,8
3172
	mov	al,0C0h
3175
	mov	al,0C0h
3173
	mov	[esi],eax
3176
	mov	[esi],eax
3174
	add	esi,10h
3177
	add	esi,10h
3175
	jmp	find_other_symbols
3178
	jmp	find_other_symbols
3176
      get_public_value:
3179
      get_public_value:
3177
	mov	eax,[ebx]
3180
	mov	eax,[ebx]
3178
	cmp	dword [ebx+4],0
3181
	cmp	dword [ebx+4],0
3179
	je	public_value_ok
3182
	je	public_value_ok
3180
	cmp	dword [ebx+4],-1
3183
	cmp	dword [ebx+4],-1
3181
	jne	value_out_of_range
3184
	jne	value_out_of_range
3182
	bt	eax,31
3185
	bt	eax,31
3183
	jnc	value_out_of_range
3186
	jnc	value_out_of_range
3184
      public_value_ok:
3187
      public_value_ok:
3185
	ret
3188
	ret
3186
      make_extrn_symbol:
3189
      make_extrn_symbol:
3187
	mov	eax,[esi+4]
3190
	mov	eax,[esi+4]
3188
	stos	dword [edi]
3191
	stos	dword [edi]
3189
	test	[format_flags],8
3192
	test	[format_flags],8
3190
	jnz	elf64_extrn_symbol
3193
	jnz	elf64_extrn_symbol
3191
	xor	eax,eax
3194
	xor	eax,eax
3192
	stos	dword [edi]
3195
	stos	dword [edi]
3193
	mov	eax,[esi+8]
3196
	mov	eax,[esi+8]
3194
	stos	dword [edi]
3197
	stos	dword [edi]
3195
	mov	eax,10h
3198
	mov	eax,10h
3196
	stos	dword [edi]
3199
	stos	dword [edi]
3197
	jmp	extrn_symbol_ok
3200
	jmp	extrn_symbol_ok
3198
      elf64_extrn_symbol:
3201
      elf64_extrn_symbol:
3199
	mov	eax,10h
3202
	mov	eax,10h
3200
	stos	dword [edi]
3203
	stos	dword [edi]
3201
	xor	al,al
3204
	xor	al,al
3202
	stos	dword [edi]
3205
	stos	dword [edi]
3203
	stos	dword [edi]
3206
	stos	dword [edi]
3204
	mov	eax,[esi+8]
3207
	mov	eax,[esi+8]
3205
	stos	dword [edi]
3208
	stos	dword [edi]
3206
	xor	eax,eax
3209
	xor	eax,eax
3207
	stos	dword [edi]
3210
	stos	dword [edi]
3208
      extrn_symbol_ok:
3211
      extrn_symbol_ok:
3209
	inc	ecx
3212
	inc	ecx
3210
	mov	eax,ecx
3213
	mov	eax,ecx
3211
	shl	eax,8
3214
	shl	eax,8
3212
	mov	al,80h
3215
	mov	al,80h
3213
	mov	[esi],eax
3216
	mov	[esi],eax
3214
	add	esi,0Ch
3217
	add	esi,0Ch
3215
	jmp	find_other_symbols
3218
	jmp	find_other_symbols
3216
      elf_symbol_table_ok:
3219
      elf_symbol_table_ok:
3217
	mov	edx,edi
3220
	mov	edx,edi
3218
	mov	ebx,[free_additional_memory]
3221
	mov	ebx,[free_additional_memory]
3219
	xor	al,al
3222
	xor	al,al
3220
	stos	byte [edi]
3223
	stos	byte [edi]
3221
	add	edi,16
3224
	add	edi,16
3222
	mov	[edx+1],edx
3225
	mov	[edx+1],edx
3223
	add	ebx,10h
3226
	add	ebx,10h
3224
	test	[format_flags],8
3227
	test	[format_flags],8
3225
	jz	make_string_table
3228
	jz	make_string_table
3226
	add	ebx,8
3229
	add	ebx,8
3227
      make_string_table:
3230
      make_string_table:
3228
	cmp	ebx,edx
3231
	cmp	ebx,edx
3229
	je	elf_string_table_ok
3232
	je	elf_string_table_ok
3230
	test	[format_flags],8
3233
	test	[format_flags],8
3231
	jnz	make_elf64_string
3234
	jnz	make_elf64_string
3232
	cmp	byte [ebx+0Dh],0
3235
	cmp	byte [ebx+0Dh],0
3233
	je	rel_prefix_ok
3236
	je	rel_prefix_ok
3234
	mov	byte [ebx+0Dh],0
3237
	mov	byte [ebx+0Dh],0
3235
	mov	eax,'.rel'
3238
	mov	eax,'.rel'
3236
	stos	dword [edi]
3239
	stos	dword [edi]
3237
      rel_prefix_ok:
3240
      rel_prefix_ok:
3238
	mov	esi,edi
3241
	mov	esi,edi
3239
	sub	esi,edx
3242
	sub	esi,edx
3240
	xchg	esi,[ebx]
3243
	xchg	esi,[ebx]
3241
	add	ebx,10h
3244
	add	ebx,10h
3242
      make_elf_string:
3245
      make_elf_string:
3243
	or	esi,esi
3246
	or	esi,esi
3244
	jz	default_string
3247
	jz	default_string
3245
	lods	dword [esi]
3248
	lods	dword [esi]
3246
	mov	ecx,eax
3249
	mov	ecx,eax
3247
	rep	movs byte [edi],[esi]
3250
	rep	movs byte [edi],[esi]
3248
	xor	al,al
3251
	xor	al,al
3249
	stos	byte [edi]
3252
	stos	byte [edi]
3250
	jmp	make_string_table
3253
	jmp	make_string_table
3251
      make_elf64_string:
3254
      make_elf64_string:
3252
	cmp	byte [ebx+5],0
3255
	cmp	byte [ebx+5],0
3253
	je	elf64_rel_prefix_ok
3256
	je	elf64_rel_prefix_ok
3254
	mov	byte [ebx+5],0
3257
	mov	byte [ebx+5],0
3255
	mov	eax,'.rel'
3258
	mov	eax,'.rel'
3256
	stos	dword [edi]
3259
	stos	dword [edi]
3257
	mov	al,'a'
3260
	mov	al,'a'
3258
	stos	byte [edi]
3261
	stos	byte [edi]
3259
      elf64_rel_prefix_ok:
3262
      elf64_rel_prefix_ok:
3260
	mov	esi,edi
3263
	mov	esi,edi
3261
	sub	esi,edx
3264
	sub	esi,edx
3262
	xchg	esi,[ebx]
3265
	xchg	esi,[ebx]
3263
	add	ebx,18h
3266
	add	ebx,18h
3264
	jmp	make_elf_string
3267
	jmp	make_elf_string
3265
      default_string:
3268
      default_string:
3266
	mov	eax,'.fla'
3269
	mov	eax,'.fla'
3267
	stos	dword [edi]
3270
	stos	dword [edi]
3268
	mov	ax,'t'
3271
	mov	ax,'t'
3269
	stos	word [edi]
3272
	stos	word [edi]
3270
	jmp	make_string_table
3273
	jmp	make_string_table
3271
      elf_string_table_ok:
3274
      elf_string_table_ok:
3272
	mov	[edx+1+8],edi
3275
	mov	[edx+1+8],edi
3273
	mov	ebx,[code_start]
3276
	mov	ebx,[code_start]
3274
	mov	eax,edi
3277
	mov	eax,edi
3275
	sub	eax,[free_additional_memory]
3278
	sub	eax,[free_additional_memory]
3276
	test	[format_flags],8
3279
	test	[format_flags],8
3277
	jnz	finish_elf64_header
3280
	jnz	finish_elf64_header
3278
	mov	[ebx+20h],eax
3281
	mov	[ebx+20h],eax
3279
	mov	eax,[current_section]
3282
	mov	eax,[current_section]
3280
	inc	ax
3283
	inc	ax
3281
	jz	format_limitations_exceeded
3284
	jz	format_limitations_exceeded
3282
	mov	[ebx+32h],ax
3285
	mov	[ebx+32h],ax
3283
	inc	ax
3286
	inc	ax
3284
	jz	format_limitations_exceeded
3287
	jz	format_limitations_exceeded
3285
	mov	[ebx+30h],ax
3288
	mov	[ebx+30h],ax
3286
	jmp	elf_header_finished
3289
	jmp	elf_header_finished
3287
      finish_elf64_header:
3290
      finish_elf64_header:
3288
	mov	[ebx+28h],eax
3291
	mov	[ebx+28h],eax
3289
	mov	eax,[current_section]
3292
	mov	eax,[current_section]
3290
	inc	ax
3293
	inc	ax
3291
	jz	format_limitations_exceeded
3294
	jz	format_limitations_exceeded
3292
	mov	[ebx+3Eh],ax
3295
	mov	[ebx+3Eh],ax
3293
	inc	ax
3296
	inc	ax
3294
	jz	format_limitations_exceeded
3297
	jz	format_limitations_exceeded
3295
	mov	[ebx+3Ch],ax
3298
	mov	[ebx+3Ch],ax
3296
      elf_header_finished:
3299
      elf_header_finished:
3297
	xor	eax,eax
3300
	xor	eax,eax
3298
	mov	ecx,10
3301
	mov	ecx,10
3299
	rep	stos dword [edi]
3302
	rep	stos dword [edi]
3300
	test	[format_flags],8
3303
	test	[format_flags],8
3301
	jz	elf_null_section_ok
3304
	jz	elf_null_section_ok
3302
	mov	ecx,6
3305
	mov	ecx,6
3303
	rep	stos dword [edi]
3306
	rep	stos dword [edi]
3304
      elf_null_section_ok:
3307
      elf_null_section_ok:
3305
	mov	esi,ebp
3308
	mov	esi,ebp
3306
	xor	ecx,ecx
3309
	xor	ecx,ecx
3307
      make_section_entry:
3310
      make_section_entry:
3308
	mov	ebx,edi
3311
	mov	ebx,edi
3309
	mov	eax,[esi+4]
3312
	mov	eax,[esi+4]
3310
	mov	eax,[eax]
3313
	mov	eax,[eax]
3311
	stos	dword [edi]
3314
	stos	dword [edi]
3312
	mov	eax,1
3315
	mov	eax,1
3313
	cmp	dword [esi+0Ch],0
3316
	cmp	dword [esi+0Ch],0
3314
	je	bss_section
3317
	je	bss_section
3315
	test	byte [esi+14h],80h
3318
	test	byte [esi+14h],80h
3316
	jz	section_type_ok
3319
	jz	section_type_ok
3317
      bss_section:
3320
      bss_section:
3318
	mov	al,8
3321
	mov	al,8
3319
      section_type_ok:
3322
      section_type_ok:
3320
	stos	dword [edi]
3323
	stos	dword [edi]
3321
	mov	eax,[esi+14h]
3324
	mov	eax,[esi+14h]
3322
	and	al,3Fh
3325
	and	al,3Fh
3323
	call	store_elf_machine_word
3326
	call	store_elf_machine_word
3324
	xor	eax,eax
3327
	xor	eax,eax
3325
	call	store_elf_machine_word
3328
	call	store_elf_machine_word
3326
	mov	eax,[esi+8]
3329
	mov	eax,[esi+8]
3327
	mov	[image_base],eax
3330
	mov	[image_base],eax
3328
	sub	eax,[code_start]
3331
	sub	eax,[code_start]
3329
	call	store_elf_machine_word
3332
	call	store_elf_machine_word
3330
	mov	eax,[esi+0Ch]
3333
	mov	eax,[esi+0Ch]
3331
	call	store_elf_machine_word
3334
	call	store_elf_machine_word
3332
	xor	eax,eax
3335
	xor	eax,eax
3333
	stos	dword [edi]
3336
	stos	dword [edi]
3334
	stos	dword [edi]
3337
	stos	dword [edi]
3335
	mov	eax,[esi+10h]
3338
	mov	eax,[esi+10h]
3336
	call	store_elf_machine_word
3339
	call	store_elf_machine_word
3337
	xor	eax,eax
3340
	xor	eax,eax
3338
	call	store_elf_machine_word
3341
	call	store_elf_machine_word
3339
	inc	ecx
3342
	inc	ecx
3340
	add	esi,20h
3343
	add	esi,20h
3341
	xchg	edi,[esp]
3344
	xchg	edi,[esp]
3342
	mov	ebp,edi
3345
	mov	ebp,edi
3343
      convert_relocations:
3346
      convert_relocations:
3344
	cmp	esi,[free_additional_memory]
3347
	cmp	esi,[free_additional_memory]
3345
	je	relocations_converted
3348
	je	relocations_converted
3346
	mov	al,[esi]
3349
	mov	al,[esi]
3347
	or	al,al
3350
	or	al,al
3348
	jz	relocations_converted
3351
	jz	relocations_converted
3349
	cmp	al,80h
3352
	cmp	al,80h
3350
	jb	make_relocation_entry
3353
	jb	make_relocation_entry
3351
	cmp	al,0C0h
3354
	cmp	al,0C0h
3352
	jb	relocation_entry_ok
3355
	jb	relocation_entry_ok
3353
	add	esi,10h
3356
	add	esi,10h
3354
	jmp	convert_relocations
3357
	jmp	convert_relocations
3355
      make_relocation_entry:
3358
      make_relocation_entry:
3356
	test	[format_flags],8
3359
	test	[format_flags],8
3357
	jnz	make_elf64_relocation_entry
3360
	jnz	make_elf64_relocation_entry
3358
	mov	eax,[esi+4]
3361
	mov	eax,[esi+4]
3359
	stos	dword [edi]
3362
	stos	dword [edi]
3360
	mov	eax,[esi+8]
3363
	mov	eax,[esi+8]
3361
	mov	eax,[eax]
3364
	mov	eax,[eax]
3362
	mov	al,[esi]
3365
	mov	al,[esi]
3363
	stos	dword [edi]
3366
	stos	dword [edi]
3364
	jmp	relocation_entry_ok
3367
	jmp	relocation_entry_ok
3365
      make_elf64_relocation_entry:
3368
      make_elf64_relocation_entry:
3366
	mov	eax,[esi+4]
3369
	mov	eax,[esi+4]
3367
	stos	dword [edi]
3370
	stos	dword [edi]
3368
	xor	eax,eax
3371
	xor	eax,eax
3369
	stos	dword [edi]
3372
	stos	dword [edi]
3370
	movzx	eax,byte [esi]
3373
	movzx	eax,byte [esi]
3371
	stos	dword [edi]
3374
	stos	dword [edi]
3372
	mov	eax,[esi+8]
3375
	mov	eax,[esi+8]
3373
	mov	eax,[eax]
3376
	mov	eax,[eax]
3374
	shr	eax,8
3377
	shr	eax,8
3375
	stos	dword [edi]
3378
	stos	dword [edi]
3376
	xor	eax,eax
3379
	xor	eax,eax
3377
	stos	dword [edi]
3380
	stos	dword [edi]
3378
	stos	dword [edi]
3381
	stos	dword [edi]
3379
      relocation_entry_ok:
3382
      relocation_entry_ok:
3380
	add	esi,0Ch
3383
	add	esi,0Ch
3381
	jmp	convert_relocations
3384
	jmp	convert_relocations
3382
      store_elf_machine_word:
3385
      store_elf_machine_word:
3383
	stos	dword [edi]
3386
	stos	dword [edi]
3384
	test	[format_flags],8
3387
	test	[format_flags],8
3385
	jz	elf_machine_word_ok
3388
	jz	elf_machine_word_ok
3386
	mov	dword [edi],0
3389
	mov	dword [edi],0
3387
	add	edi,4
3390
	add	edi,4
3388
      elf_machine_word_ok:
3391
      elf_machine_word_ok:
3389
	ret
3392
	ret
3390
      relocations_converted:
3393
      relocations_converted:
3391
	cmp	edi,ebp
3394
	cmp	edi,ebp
3392
	xchg	edi,[esp]
3395
	xchg	edi,[esp]
3393
	je	rel_section_ok
3396
	je	rel_section_ok
3394
	mov	eax,[ebx]
3397
	mov	eax,[ebx]
3395
	sub	eax,4
3398
	sub	eax,4
3396
	test	[format_flags],8
3399
	test	[format_flags],8
3397
	jz	store_relocations_name_offset
3400
	jz	store_relocations_name_offset
3398
	dec	eax
3401
	dec	eax
3399
      store_relocations_name_offset:
3402
      store_relocations_name_offset:
3400
	stos	dword [edi]
3403
	stos	dword [edi]
3401
	test	[format_flags],8
3404
	test	[format_flags],8
3402
	jnz	rela_section
3405
	jnz	rela_section
3403
	mov	eax,9
3406
	mov	eax,9
3404
	jmp	store_relocations_type
3407
	jmp	store_relocations_type
3405
      rela_section:
3408
      rela_section:
3406
	mov	eax,4
3409
	mov	eax,4
3407
      store_relocations_type:
3410
      store_relocations_type:
3408
	stos	dword [edi]
3411
	stos	dword [edi]
3409
	xor	al,al
3412
	xor	al,al
3410
	call	store_elf_machine_word
3413
	call	store_elf_machine_word
3411
	call	store_elf_machine_word
3414
	call	store_elf_machine_word
3412
	mov	eax,ebp
3415
	mov	eax,ebp
3413
	sub	eax,[code_start]
3416
	sub	eax,[code_start]
3414
	call	store_elf_machine_word
3417
	call	store_elf_machine_word
3415
	mov	eax,[esp]
3418
	mov	eax,[esp]
3416
	sub	eax,ebp
3419
	sub	eax,ebp
3417
	call	store_elf_machine_word
3420
	call	store_elf_machine_word
3418
	mov	eax,[current_section]
3421
	mov	eax,[current_section]
3419
	stos	dword [edi]
3422
	stos	dword [edi]
3420
	mov	eax,ecx
3423
	mov	eax,ecx
3421
	stos	dword [edi]
3424
	stos	dword [edi]
3422
	inc	ecx
3425
	inc	ecx
3423
	test	[format_flags],8
3426
	test	[format_flags],8
3424
	jnz	finish_elf64_rela_section
3427
	jnz	finish_elf64_rela_section
3425
	mov	eax,4
3428
	mov	eax,4
3426
	stos	dword [edi]
3429
	stos	dword [edi]
3427
	mov	al,8
3430
	mov	al,8
3428
	stos	dword [edi]
3431
	stos	dword [edi]
3429
	jmp	rel_section_ok
3432
	jmp	rel_section_ok
3430
      finish_elf64_rela_section:
3433
      finish_elf64_rela_section:
3431
	mov	eax,8
3434
	mov	eax,8
3432
	stos	dword [edi]
3435
	stos	dword [edi]
3433
	xor	al,al
3436
	xor	al,al
3434
	stos	dword [edi]
3437
	stos	dword [edi]
3435
	mov	al,24
3438
	mov	al,24
3436
	stos	dword [edi]
3439
	stos	dword [edi]
3437
	xor	al,al
3440
	xor	al,al
3438
	stos	dword [edi]
3441
	stos	dword [edi]
3439
      rel_section_ok:
3442
      rel_section_ok:
3440
	cmp	esi,[free_additional_memory]
3443
	cmp	esi,[free_additional_memory]
3441
	jne	make_section_entry
3444
	jne	make_section_entry
3442
	pop	eax
3445
	pop	eax
3443
	mov	ebx,[code_start]
3446
	mov	ebx,[code_start]
3444
	sub	eax,ebx
3447
	sub	eax,ebx
3445
	mov	[code_size],eax
3448
	mov	[code_size],eax
3446
	mov	ecx,20h
3449
	mov	ecx,20h
3447
	test	[format_flags],8
3450
	test	[format_flags],8
3448
	jz	adjust_elf_section_headers_offset
3451
	jz	adjust_elf_section_headers_offset
3449
	mov	ecx,28h
3452
	mov	ecx,28h
3450
      adjust_elf_section_headers_offset:
3453
      adjust_elf_section_headers_offset:
3451
	add	[ebx+ecx],eax
3454
	add	[ebx+ecx],eax
3452
	mov	eax,1
3455
	mov	eax,1
3453
	stos	dword [edi]
3456
	stos	dword [edi]
3454
	mov	al,2
3457
	mov	al,2
3455
	stos	dword [edi]
3458
	stos	dword [edi]
3456
	xor	al,al
3459
	xor	al,al
3457
	call	store_elf_machine_word
3460
	call	store_elf_machine_word
3458
	call	store_elf_machine_word
3461
	call	store_elf_machine_word
3459
	mov	eax,[code_size]
3462
	mov	eax,[code_size]
3460
	call	store_elf_machine_word
3463
	call	store_elf_machine_word
3461
	mov	eax,[edx+1]
3464
	mov	eax,[edx+1]
3462
	sub	eax,[free_additional_memory]
3465
	sub	eax,[free_additional_memory]
3463
	call	store_elf_machine_word
3466
	call	store_elf_machine_word
3464
	mov	eax,[current_section]
3467
	mov	eax,[current_section]
3465
	inc	eax
3468
	inc	eax
3466
	stos	dword [edi]
3469
	stos	dword [edi]
3467
	mov	eax,[number_of_sections]
3470
	mov	eax,[number_of_sections]
3468
	inc	eax
3471
	inc	eax
3469
	stos	dword [edi]
3472
	stos	dword [edi]
3470
	test	[format_flags],8
3473
	test	[format_flags],8
3471
	jnz	finish_elf64_sym_section
3474
	jnz	finish_elf64_sym_section
3472
	mov	eax,4
3475
	mov	eax,4
3473
	stos	dword [edi]
3476
	stos	dword [edi]
3474
	mov	al,10h
3477
	mov	al,10h
3475
	stos	dword [edi]
3478
	stos	dword [edi]
3476
	jmp	sym_section_ok
3479
	jmp	sym_section_ok
3477
      finish_elf64_sym_section:
3480
      finish_elf64_sym_section:
3478
	mov	eax,8
3481
	mov	eax,8
3479
	stos	dword [edi]
3482
	stos	dword [edi]
3480
	xor	al,al
3483
	xor	al,al
3481
	stos	dword [edi]
3484
	stos	dword [edi]
3482
	mov	al,18h
3485
	mov	al,18h
3483
	stos	dword [edi]
3486
	stos	dword [edi]
3484
	xor	al,al
3487
	xor	al,al
3485
	stos	dword [edi]
3488
	stos	dword [edi]
3486
      sym_section_ok:
3489
      sym_section_ok:
3487
	mov	al,1+8
3490
	mov	al,1+8
3488
	stos	dword [edi]
3491
	stos	dword [edi]
3489
	mov	al,3
3492
	mov	al,3
3490
	stos	dword [edi]
3493
	stos	dword [edi]
3491
	xor	al,al
3494
	xor	al,al
3492
	call	store_elf_machine_word
3495
	call	store_elf_machine_word
3493
	call	store_elf_machine_word
3496
	call	store_elf_machine_word
3494
	mov	eax,[edx+1]
3497
	mov	eax,[edx+1]
3495
	sub	eax,[free_additional_memory]
3498
	sub	eax,[free_additional_memory]
3496
	add	eax,[code_size]
3499
	add	eax,[code_size]
3497
	call	store_elf_machine_word
3500
	call	store_elf_machine_word
3498
	mov	eax,[edx+1+8]
3501
	mov	eax,[edx+1+8]
3499
	sub	eax,[edx+1]
3502
	sub	eax,[edx+1]
3500
	call	store_elf_machine_word
3503
	call	store_elf_machine_word
3501
	xor	eax,eax
3504
	xor	eax,eax
3502
	stos	dword [edi]
3505
	stos	dword [edi]
3503
	stos	dword [edi]
3506
	stos	dword [edi]
3504
	mov	al,1
3507
	mov	al,1
3505
	call	store_elf_machine_word
3508
	call	store_elf_machine_word
3506
	xor	eax,eax
3509
	xor	eax,eax
3507
	call	store_elf_machine_word
3510
	call	store_elf_machine_word
3508
	mov	eax,'tab'
3511
	mov	eax,'tab'
3509
	mov	dword [edx+1],'.sym'
3512
	mov	dword [edx+1],'.sym'
3510
	mov	[edx+1+4],eax
3513
	mov	[edx+1+4],eax
3511
	mov	dword [edx+1+8],'.str'
3514
	mov	dword [edx+1+8],'.str'
3512
	mov	[edx+1+8+4],eax
3515
	mov	[edx+1+8+4],eax
3513
	mov	[written_size],0
3516
	mov	[written_size],0
3514
	mov	edx,[output_file]
3517
	mov	edx,[output_file]
3515
	call	create
3518
	call	create
3516
	jc	write_failed
3519
	jc	write_failed
3517
	call	write_code
3520
	call	write_code
3518
	mov	ecx,edi
3521
	mov	ecx,edi
3519
	mov	edx,[free_additional_memory]
3522
	mov	edx,[free_additional_memory]
3520
	sub	ecx,edx
3523
	sub	ecx,edx
3521
	add	[written_size],ecx
3524
	add	[written_size],ecx
3522
	call	write
3525
	call	write
3523
	jc	write_failed
3526
	jc	write_failed
3524
	jmp	output_written
3527
	jmp	output_written
3525
 
3528
 
3526
format_elf_exe:
3529
format_elf_exe:
3527
	add	esi,2
3530
	add	esi,2
3528
	or	[format_flags],1
3531
	or	[format_flags],1
3529
	mov	[image_base],8048000h
3532
	mov	[image_base],8048000h
3530
	cmp	byte [esi],80h
3533
	cmp	byte [esi],80h
3531
	jne	elf_exe_base_ok
3534
	jne	elf_exe_base_ok
3532
	lods	word [esi]
3535
	lods	word [esi]
3533
	cmp	ah,'('
3536
	cmp	ah,'('
3534
	jne	invalid_argument
3537
	jne	invalid_argument
3535
	cmp	byte [esi],'.'
3538
	cmp	byte [esi],'.'
3536
	je	invalid_value
3539
	je	invalid_value
3537
	push	edx
3540
	push	edx
3538
	call	get_dword_value
3541
	call	get_dword_value
3539
	cmp	[value_type],0
3542
	cmp	[value_type],0
3540
	jne	invalid_use_of_symbol
3543
	jne	invalid_use_of_symbol
3541
	mov	[image_base],eax
3544
	mov	[image_base],eax
3542
	pop	edx
3545
	pop	edx
3543
      elf_exe_base_ok:
3546
      elf_exe_base_ok:
3544
	mov	byte [edx+10h],2
3547
	mov	byte [edx+10h],2
3545
	mov	byte [edx+2Ah],20h
3548
	mov	byte [edx+2Ah],20h
3546
	mov	ebx,edi
3549
	mov	ebx,edi
3547
	mov	ecx,20h shr 2
3550
	mov	ecx,20h shr 2
3548
	cmp	[current_pass],0
3551
	cmp	[current_pass],0
3549
	je	init_elf_segments
3552
	je	init_elf_segments
3550
	imul	ecx,[number_of_sections]
3553
	imul	ecx,[number_of_sections]
3551
      init_elf_segments:
3554
      init_elf_segments:
3552
	xor	eax,eax
3555
	xor	eax,eax
3553
	rep	stos dword [edi]
3556
	rep	stos dword [edi]
3554
	mov	[number_of_sections],0
3557
	mov	[number_of_sections],0
3555
	mov	byte [ebx],1
3558
	mov	byte [ebx],1
3556
	mov	word [ebx+1Ch],1000h
3559
	mov	word [ebx+1Ch],1000h
3557
	mov	byte [ebx+18h],111b
3560
	mov	byte [ebx+18h],111b
3558
	mov	eax,edi
3561
	mov	eax,edi
3559
	sub	eax,[code_start]
3562
	sub	eax,[code_start]
3560
	mov	[ebx+4],eax
3563
	mov	[ebx+4],eax
3561
	add	eax,[image_base]
3564
	add	eax,[image_base]
3562
	mov	[ebx+8],eax
3565
	mov	[ebx+8],eax
3563
	mov	[ebx+0Ch],eax
3566
	mov	[ebx+0Ch],eax
3564
	mov	[edx+18h],eax
3567
	mov	[edx+18h],eax
3565
	xor	edx,edx
3568
	xor	edx,edx
3566
	not	eax
3569
	not	eax
3567
	not	edx
3570
	not	edx
3568
	add	eax,1
3571
	add	eax,1
3569
	adc	edx,0
3572
	adc	edx,0
3570
	add	eax,edi
3573
	add	eax,edi
3571
	adc	edx,0
3574
	adc	edx,0
3572
	mov	dword [org_origin],eax
3575
	mov	dword [org_origin],eax
3573
	mov	dword [org_origin+4],edx
3576
	mov	dword [org_origin+4],edx
3574
	mov	[org_registers],0
3577
	mov	[org_registers],0
3575
	mov	[org_start],edi
3578
	mov	[org_start],edi
3576
	mov	[symbols_stream],edi
3579
	mov	[symbols_stream],edi
3577
	jmp	format_defined
3580
	jmp	format_defined
3578
      format_elf64_exe:
3581
      format_elf64_exe:
3579
	add	esi,2
3582
	add	esi,2
3580
	or	[format_flags],1
3583
	or	[format_flags],1
3581
	mov	[image_base],400000h
3584
	mov	[image_base],400000h
3582
	mov	[image_base_high],0
3585
	mov	[image_base_high],0
3583
	cmp	byte [esi],80h
3586
	cmp	byte [esi],80h
3584
	jne	elf64_exe_base_ok
3587
	jne	elf64_exe_base_ok
3585
	lods	word [esi]
3588
	lods	word [esi]
3586
	cmp	ah,'('
3589
	cmp	ah,'('
3587
	jne	invalid_argument
3590
	jne	invalid_argument
3588
	cmp	byte [esi],'.'
3591
	cmp	byte [esi],'.'
3589
	je	invalid_value
3592
	je	invalid_value
3590
	push	edx
3593
	push	edx
3591
	call	get_qword_value
3594
	call	get_qword_value
3592
	cmp	[value_type],0
3595
	cmp	[value_type],0
3593
	jne	invalid_use_of_symbol
3596
	jne	invalid_use_of_symbol
3594
	mov	[image_base],eax
3597
	mov	[image_base],eax
3595
	mov	[image_base_high],edx
3598
	mov	[image_base_high],edx
3596
	pop	edx
3599
	pop	edx
3597
      elf64_exe_base_ok:
3600
      elf64_exe_base_ok:
3598
	mov	byte [edx+10h],2
3601
	mov	byte [edx+10h],2
3599
	mov	byte [edx+36h],38h
3602
	mov	byte [edx+36h],38h
3600
	mov	ebx,edi
3603
	mov	ebx,edi
3601
	mov	ecx,38h shr 2
3604
	mov	ecx,38h shr 2
3602
	cmp	[current_pass],0
3605
	cmp	[current_pass],0
3603
	je	init_elf64_segments
3606
	je	init_elf64_segments
3604
	imul	ecx,[number_of_sections]
3607
	imul	ecx,[number_of_sections]
3605
      init_elf64_segments:
3608
      init_elf64_segments:
3606
	xor	eax,eax
3609
	xor	eax,eax
3607
	rep	stos dword [edi]
3610
	rep	stos dword [edi]
3608
	mov	[number_of_sections],0
3611
	mov	[number_of_sections],0
3609
	mov	byte [ebx],1
3612
	mov	byte [ebx],1
3610
	mov	word [ebx+30h],1000h
3613
	mov	word [ebx+30h],1000h
3611
	mov	byte [ebx+4],111b
3614
	mov	byte [ebx+4],111b
3612
	push	edx
3615
	push	edx
3613
	mov	eax,edi
3616
	mov	eax,edi
3614
	sub	eax,[code_start]
3617
	sub	eax,[code_start]
3615
	mov	[ebx+8],eax
3618
	mov	[ebx+8],eax
3616
	xor	edx,edx
3619
	xor	edx,edx
3617
	add	eax,[image_base]
3620
	add	eax,[image_base]
3618
	adc	edx,[image_base_high]
3621
	adc	edx,[image_base_high]
3619
	mov	[ebx+10h],eax
3622
	mov	[ebx+10h],eax
3620
	mov	[ebx+10h+4],edx
3623
	mov	[ebx+10h+4],edx
3621
	mov	[ebx+18h],eax
3624
	mov	[ebx+18h],eax
3622
	mov	[ebx+18h+4],edx
3625
	mov	[ebx+18h+4],edx
3623
	pop	ebx
3626
	pop	ebx
3624
	mov	[ebx+18h],eax
3627
	mov	[ebx+18h],eax
3625
	mov	[ebx+18h+4],edx
3628
	mov	[ebx+18h+4],edx
3626
	not	eax
3629
	not	eax
3627
	not	edx
3630
	not	edx
3628
	add	eax,1
3631
	add	eax,1
3629
	adc	edx,0
3632
	adc	edx,0
3630
	add	eax,edi
3633
	add	eax,edi
3631
	adc	edx,0
3634
	adc	edx,0
3632
	mov	dword [org_origin],eax
3635
	mov	dword [org_origin],eax
3633
	mov	dword [org_origin+4],edx
3636
	mov	dword [org_origin+4],edx
3634
	mov	[org_registers],0
3637
	mov	[org_registers],0
3635
	mov	[org_start],edi
3638
	mov	[org_start],edi
3636
	mov	[symbols_stream],edi
3639
	mov	[symbols_stream],edi
3637
	jmp	format_defined
3640
	jmp	format_defined
3638
elf_entry:
3641
elf_entry:
3639
	lods	byte [esi]
3642
	lods	byte [esi]
3640
	cmp	al,'('
3643
	cmp	al,'('
3641
	jne	invalid_argument
3644
	jne	invalid_argument
3642
	cmp	byte [esi],'.'
3645
	cmp	byte [esi],'.'
3643
	je	invalid_value
3646
	je	invalid_value
3644
	test	[format_flags],8
3647
	test	[format_flags],8
3645
	jnz	elf64_entry
3648
	jnz	elf64_entry
3646
	call	get_dword_value
3649
	call	get_dword_value
3647
	cmp	[value_type],0
3650
	cmp	[value_type],0
3648
	jne	invalid_use_of_symbol
3651
	jne	invalid_use_of_symbol
3649
	mov	edx,[code_start]
3652
	mov	edx,[code_start]
3650
	mov	[edx+18h],eax
3653
	mov	[edx+18h],eax
3651
	jmp	instruction_assembled
3654
	jmp	instruction_assembled
3652
      elf64_entry:
3655
      elf64_entry:
3653
	call	get_qword_value
3656
	call	get_qword_value
3654
	cmp	[value_type],0
3657
	cmp	[value_type],0
3655
	jne	invalid_use_of_symbol
3658
	jne	invalid_use_of_symbol
3656
	mov	ebx,[code_start]
3659
	mov	ebx,[code_start]
3657
	mov	[ebx+18h],eax
3660
	mov	[ebx+18h],eax
3658
	mov	[ebx+1Ch],edx
3661
	mov	[ebx+1Ch],edx
3659
	jmp	instruction_assembled
3662
	jmp	instruction_assembled
3660
elf_segment:
3663
elf_segment:
3661
	bt	[format_flags],0
3664
	bt	[format_flags],0
3662
	jnc	illegal_instruction
3665
	jnc	illegal_instruction
3663
	test	[format_flags],8
3666
	test	[format_flags],8
3664
	jnz	elf64_segment
3667
	jnz	elf64_segment
3665
	call	close_elf_segment
3668
	call	close_elf_segment
3666
	push	eax
3669
	push	eax
3667
	mov	ebx,[number_of_sections]
3670
	mov	ebx,[number_of_sections]
3668
	shl	ebx,5
3671
	shl	ebx,5
3669
	add	ebx,[code_start]
3672
	add	ebx,[code_start]
3670
	add	ebx,34h
3673
	add	ebx,34h
3671
	cmp	ebx,[symbols_stream]
3674
	cmp	ebx,[symbols_stream]
3672
	jb	new_elf_segment
3675
	jb	new_elf_segment
3673
	mov	ebx,[symbols_stream]
3676
	mov	ebx,[symbols_stream]
3674
	sub	ebx,20h
3677
	sub	ebx,20h
3675
	push	edi
3678
	push	edi
3676
	mov	edi,ebx
3679
	mov	edi,ebx
3677
	mov	ecx,20h shr 2
3680
	mov	ecx,20h shr 2
3678
	xor	eax,eax
3681
	xor	eax,eax
3679
	rep	stos dword [edi]
3682
	rep	stos dword [edi]
3680
	pop	edi
3683
	pop	edi
3681
	or	[next_pass_needed],-1
3684
	or	[next_pass_needed],-1
3682
      new_elf_segment:
3685
      new_elf_segment:
3683
	mov	byte [ebx],1
3686
	mov	byte [ebx],1
3684
	mov	word [ebx+1Ch],1000h
3687
	mov	word [ebx+1Ch],1000h
3685
      elf_segment_flags:
3688
      elf_segment_flags:
3686
	cmp	byte [esi],19h
3689
	cmp	byte [esi],19h
3687
	jne	elf_segment_flags_ok
3690
	jne	elf_segment_flags_ok
3688
	lods	word [esi]
3691
	lods	word [esi]
3689
	sub	ah,28
3692
	sub	ah,28
3690
	jbe	invalid_argument
3693
	jbe	invalid_argument
3691
	cmp	ah,1
3694
	cmp	ah,1
3692
	je	mark_elf_segment_flag
3695
	je	mark_elf_segment_flag
3693
	cmp	ah,3
3696
	cmp	ah,3
3694
	ja	invalid_argument
3697
	ja	invalid_argument
3695
	xor	ah,1
3698
	xor	ah,1
3696
	cmp	ah,2
3699
	cmp	ah,2
3697
	je	mark_elf_segment_flag
3700
	je	mark_elf_segment_flag
3698
	inc	ah
3701
	inc	ah
3699
      mark_elf_segment_flag:
3702
      mark_elf_segment_flag:
3700
	test	[ebx+18h],ah
3703
	test	[ebx+18h],ah
3701
	jnz	setting_already_specified
3704
	jnz	setting_already_specified
3702
	or	[ebx+18h],ah
3705
	or	[ebx+18h],ah
3703
	jmp	elf_segment_flags
3706
	jmp	elf_segment_flags
3704
      elf_segment_flags_ok:
3707
      elf_segment_flags_ok:
3705
	mov	eax,edi
3708
	mov	eax,edi
3706
	sub	eax,[code_start]
3709
	sub	eax,[code_start]
3707
	mov	[ebx+4],eax
3710
	mov	[ebx+4],eax
3708
	pop	edx
3711
	pop	edx
3709
	and	eax,0FFFh
3712
	and	eax,0FFFh
3710
	add	edx,eax
3713
	add	edx,eax
3711
	mov	[ebx+8],edx
3714
	mov	[ebx+8],edx
3712
	mov	[ebx+0Ch],edx
3715
	mov	[ebx+0Ch],edx
3713
	mov	eax,edx
3716
	mov	eax,edx
3714
	xor	edx,edx
3717
	xor	edx,edx
3715
	not	eax
3718
	not	eax
3716
	not	edx
3719
	not	edx
3717
	add	eax,1
3720
	add	eax,1
3718
	adc	edx,0
3721
	adc	edx,0
3719
	add	eax,edi
3722
	add	eax,edi
3720
	adc	edx,0
3723
	adc	edx,0
3721
	mov	dword [org_origin],eax
3724
	mov	dword [org_origin],eax
3722
	mov	dword [org_origin+4],edx
3725
	mov	dword [org_origin+4],edx
3723
	mov	[org_registers],0
3726
	mov	[org_registers],0
3724
	mov	[org_start],edi
3727
	mov	[org_start],edi
3725
	inc	[number_of_sections]
3728
	inc	[number_of_sections]
3726
	jmp	instruction_assembled
3729
	jmp	instruction_assembled
3727
      close_elf_segment:
3730
      close_elf_segment:
3728
	cmp	[number_of_sections],0
3731
	cmp	[number_of_sections],0
3729
	jne	finish_elf_segment
3732
	jne	finish_elf_segment
3730
	cmp	edi,[symbols_stream]
3733
	cmp	edi,[symbols_stream]
3731
	jne	first_elf_segment_ok
3734
	jne	first_elf_segment_ok
3732
	push	edi
3735
	push	edi
3733
	mov	edi,[code_start]
3736
	mov	edi,[code_start]
3734
	add	edi,34h
3737
	add	edi,34h
3735
	mov	ecx,20h shr 2
3738
	mov	ecx,20h shr 2
3736
	xor	eax,eax
3739
	xor	eax,eax
3737
	rep	stos dword [edi]
3740
	rep	stos dword [edi]
3738
	pop	edi
3741
	pop	edi
3739
	mov	eax,[image_base]
3742
	mov	eax,[image_base]
3740
	ret
3743
	ret
3741
      first_elf_segment_ok:
3744
      first_elf_segment_ok:
3742
	inc	[number_of_sections]
3745
	inc	[number_of_sections]
3743
      finish_elf_segment:
3746
      finish_elf_segment:
3744
	mov	ebx,[number_of_sections]
3747
	mov	ebx,[number_of_sections]
3745
	dec	ebx
3748
	dec	ebx
3746
	shl	ebx,5
3749
	shl	ebx,5
3747
	add	ebx,[code_start]
3750
	add	ebx,[code_start]
3748
	add	ebx,34h
3751
	add	ebx,34h
3749
	mov	eax,edi
3752
	mov	eax,edi
3750
	sub	eax,[code_start]
3753
	sub	eax,[code_start]
3751
	sub	eax,[ebx+4]
3754
	sub	eax,[ebx+4]
3752
	mov	edx,edi
3755
	mov	edx,edi
3753
	cmp	edi,[undefined_data_end]
3756
	cmp	edi,[undefined_data_end]
3754
	jne	elf_segment_size_ok
3757
	jne	elf_segment_size_ok
3755
	mov	edi,[undefined_data_start]
3758
	mov	edi,[undefined_data_start]
3756
      elf_segment_size_ok:
3759
      elf_segment_size_ok:
3757
	mov	[ebx+14h],eax
3760
	mov	[ebx+14h],eax
3758
	add	eax,edi
3761
	add	eax,edi
3759
	sub	eax,edx
3762
	sub	eax,edx
3760
	mov	[ebx+10h],eax
3763
	mov	[ebx+10h],eax
3761
	mov	eax,[ebx+8]
3764
	mov	eax,[ebx+8]
3762
	add	eax,[ebx+14h]
3765
	add	eax,[ebx+14h]
3763
	add	eax,0FFFh
3766
	add	eax,0FFFh
3764
	and	eax,not 0FFFh
3767
	and	eax,not 0FFFh
3765
	ret
3768
	ret
3766
      elf64_segment:
3769
      elf64_segment:
3767
	call	close_elf64_segment
3770
	call	close_elf64_segment
3768
	push	eax edx
3771
	push	eax edx
3769
	mov	ebx,[number_of_sections]
3772
	mov	ebx,[number_of_sections]
3770
	imul	ebx,38h
3773
	imul	ebx,38h
3771
	add	ebx,[code_start]
3774
	add	ebx,[code_start]
3772
	add	ebx,40h
3775
	add	ebx,40h
3773
	cmp	ebx,[symbols_stream]
3776
	cmp	ebx,[symbols_stream]
3774
	jb	new_elf64_segment
3777
	jb	new_elf64_segment
3775
	mov	ebx,[symbols_stream]
3778
	mov	ebx,[symbols_stream]
3776
	sub	ebx,38h
3779
	sub	ebx,38h
3777
	push	edi
3780
	push	edi
3778
	mov	edi,ebx
3781
	mov	edi,ebx
3779
	mov	ecx,38h shr 2
3782
	mov	ecx,38h shr 2
3780
	xor	eax,eax
3783
	xor	eax,eax
3781
	rep	stos dword [edi]
3784
	rep	stos dword [edi]
3782
	pop	edi
3785
	pop	edi
3783
	or	[next_pass_needed],-1
3786
	or	[next_pass_needed],-1
3784
      new_elf64_segment:
3787
      new_elf64_segment:
3785
	mov	byte [ebx],1
3788
	mov	byte [ebx],1
3786
	mov	word [ebx+30h],1000h
3789
	mov	word [ebx+30h],1000h
3787
      elf64_segment_flags:
3790
      elf64_segment_flags:
3788
	cmp	byte [esi],19h
3791
	cmp	byte [esi],19h
3789
	jne	elf64_segment_flags_ok
3792
	jne	elf64_segment_flags_ok
3790
	lods	word [esi]
3793
	lods	word [esi]
3791
	sub	ah,28
3794
	sub	ah,28
3792
	jbe	invalid_argument
3795
	jbe	invalid_argument
3793
	cmp	ah,1
3796
	cmp	ah,1
3794
	je	mark_elf64_segment_flag
3797
	je	mark_elf64_segment_flag
3795
	cmp	ah,3
3798
	cmp	ah,3
3796
	ja	invalid_argument
3799
	ja	invalid_argument
3797
	xor	ah,1
3800
	xor	ah,1
3798
	cmp	ah,2
3801
	cmp	ah,2
3799
	je	mark_elf64_segment_flag
3802
	je	mark_elf64_segment_flag
3800
	inc	ah
3803
	inc	ah
3801
      mark_elf64_segment_flag:
3804
      mark_elf64_segment_flag:
3802
	test	[ebx+4],ah
3805
	test	[ebx+4],ah
3803
	jnz	setting_already_specified
3806
	jnz	setting_already_specified
3804
	or	[ebx+4],ah
3807
	or	[ebx+4],ah
3805
	jmp	elf64_segment_flags
3808
	jmp	elf64_segment_flags
3806
      elf64_segment_flags_ok:
3809
      elf64_segment_flags_ok:
3807
	mov	ecx,edi
3810
	mov	ecx,edi
3808
	sub	ecx,[code_start]
3811
	sub	ecx,[code_start]
3809
	mov	[ebx+8],ecx
3812
	mov	[ebx+8],ecx
3810
	pop	edx eax
3813
	pop	edx eax
3811
	and	ecx,0FFFh
3814
	and	ecx,0FFFh
3812
	add	eax,ecx
3815
	add	eax,ecx
3813
	adc	edx,0
3816
	adc	edx,0
3814
	mov	[ebx+10h],eax
3817
	mov	[ebx+10h],eax
3815
	mov	[ebx+10h+4],edx
3818
	mov	[ebx+10h+4],edx
3816
	mov	[ebx+18h],eax
3819
	mov	[ebx+18h],eax
3817
	mov	[ebx+18h+4],edx
3820
	mov	[ebx+18h+4],edx
3818
	not	eax
3821
	not	eax
3819
	not	edx
3822
	not	edx
3820
	add	eax,1
3823
	add	eax,1
3821
	adc	edx,0
3824
	adc	edx,0
3822
	add	eax,edi
3825
	add	eax,edi
3823
	adc	edx,0
3826
	adc	edx,0
3824
	mov	dword [org_origin],eax
3827
	mov	dword [org_origin],eax
3825
	mov	dword [org_origin+4],edx
3828
	mov	dword [org_origin+4],edx
3826
	mov	[org_registers],0
3829
	mov	[org_registers],0
3827
	mov	[org_start],edi
3830
	mov	[org_start],edi
3828
	inc	[number_of_sections]
3831
	inc	[number_of_sections]
3829
	jmp	instruction_assembled
3832
	jmp	instruction_assembled
3830
      close_elf64_segment:
3833
      close_elf64_segment:
3831
	cmp	[number_of_sections],0
3834
	cmp	[number_of_sections],0
3832
	jne	finish_elf64_segment
3835
	jne	finish_elf64_segment
3833
	cmp	edi,[symbols_stream]
3836
	cmp	edi,[symbols_stream]
3834
	jne	first_elf64_segment_ok
3837
	jne	first_elf64_segment_ok
3835
	push	edi
3838
	push	edi
3836
	mov	edi,[code_start]
3839
	mov	edi,[code_start]
3837
	add	edi,40h
3840
	add	edi,40h
3838
	mov	ecx,38h shr 2
3841
	mov	ecx,38h shr 2
3839
	xor	eax,eax
3842
	xor	eax,eax
3840
	rep	stos dword [edi]
3843
	rep	stos dword [edi]
3841
	pop	edi
3844
	pop	edi
3842
	mov	eax,[image_base]
3845
	mov	eax,[image_base]
3843
	mov	edx,[image_base_high]
3846
	mov	edx,[image_base_high]
3844
	ret
3847
	ret
3845
      first_elf64_segment_ok:
3848
      first_elf64_segment_ok:
3846
	inc	[number_of_sections]
3849
	inc	[number_of_sections]
3847
      finish_elf64_segment:
3850
      finish_elf64_segment:
3848
	mov	ebx,[number_of_sections]
3851
	mov	ebx,[number_of_sections]
3849
	dec	ebx
3852
	dec	ebx
3850
	imul	ebx,38h
3853
	imul	ebx,38h
3851
	add	ebx,[code_start]
3854
	add	ebx,[code_start]
3852
	add	ebx,40h
3855
	add	ebx,40h
3853
	mov	eax,edi
3856
	mov	eax,edi
3854
	sub	eax,[code_start]
3857
	sub	eax,[code_start]
3855
	sub	eax,[ebx+8]
3858
	sub	eax,[ebx+8]
3856
	mov	edx,edi
3859
	mov	edx,edi
3857
	cmp	edi,[undefined_data_end]
3860
	cmp	edi,[undefined_data_end]
3858
	jne	elf64_segment_size_ok
3861
	jne	elf64_segment_size_ok
3859
	mov	edi,[undefined_data_start]
3862
	mov	edi,[undefined_data_start]
3860
      elf64_segment_size_ok:
3863
      elf64_segment_size_ok:
3861
	mov	[ebx+28h],eax
3864
	mov	[ebx+28h],eax
3862
	add	eax,edi
3865
	add	eax,edi
3863
	sub	eax,edx
3866
	sub	eax,edx
3864
	mov	[ebx+20h],eax
3867
	mov	[ebx+20h],eax
3865
	mov	eax,[ebx+10h]
3868
	mov	eax,[ebx+10h]
3866
	mov	edx,[ebx+10h+4]
3869
	mov	edx,[ebx+10h+4]
3867
	add	eax,[ebx+28h]
3870
	add	eax,[ebx+28h]
3868
	adc	edx,0
3871
	adc	edx,0
3869
	sub	eax,1
3872
	sub	eax,1
3870
	sbb	edx,0
3873
	sbb	edx,0
3871
	shrd	eax,edx,12
3874
	shrd	eax,edx,12
3872
	shr	edx,12
3875
	shr	edx,12
3873
	add	eax,1
3876
	add	eax,1
3874
	adc	edx,0
3877
	adc	edx,0
3875
	shld	edx,eax,12
3878
	shld	edx,eax,12
3876
	shl	eax,12
3879
	shl	eax,12
3877
	ret
3880
	ret
3878
close_elf_exe:
3881
close_elf_exe:
3879
	test	[format_flags],8
3882
	test	[format_flags],8
3880
	jnz	close_elf64_exe
3883
	jnz	close_elf64_exe
3881
	call	close_elf_segment
3884
	call	close_elf_segment
3882
	mov	edx,[code_start]
3885
	mov	edx,[code_start]
3883
	mov	eax,[number_of_sections]
3886
	mov	eax,[number_of_sections]
3884
	mov	byte [edx+1Ch],34h
3887
	mov	byte [edx+1Ch],34h
3885
	mov	[edx+2Ch],ax
3888
	mov	[edx+2Ch],ax
3886
	shl	eax,5
3889
	shl	eax,5
3887
	add	eax,edx
3890
	add	eax,edx
3888
	add	eax,34h
3891
	add	eax,34h
3889
	cmp	eax,[symbols_stream]
3892
	cmp	eax,[symbols_stream]
3890
	je	elf_exe_ok
3893
	je	elf_exe_ok
3891
	or	[next_pass_needed],-1
3894
	or	[next_pass_needed],-1
3892
      elf_exe_ok:
3895
      elf_exe_ok:
3893
	ret
3896
	ret
3894
      close_elf64_exe:
3897
      close_elf64_exe:
3895
	call	close_elf64_segment
3898
	call	close_elf64_segment
3896
	mov	edx,[code_start]
3899
	mov	edx,[code_start]
3897
	mov	eax,[number_of_sections]
3900
	mov	eax,[number_of_sections]
3898
	mov	byte [edx+20h],40h
3901
	mov	byte [edx+20h],40h
3899
	mov	[edx+38h],ax
3902
	mov	[edx+38h],ax
3900
	imul	eax,38h
3903
	imul	eax,38h
3901
	add	eax,edx
3904
	add	eax,edx
3902
	add	eax,40h
3905
	add	eax,40h
3903
	cmp	eax,[symbols_stream]
3906
	cmp	eax,[symbols_stream]
3904
	je	elf64_exe_ok
3907
	je	elf64_exe_ok
3905
	or	[next_pass_needed],-1
3908
	or	[next_pass_needed],-1
3906
      elf64_exe_ok:
3909
      elf64_exe_ok:
3907
	ret
3910
	ret