Subversion Repositories Kolibri OS

Rev

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