Subversion Repositories Kolibri OS

Rev

Rev 872 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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