Subversion Repositories Kolibri OS

Rev

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

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