-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pbinary.e
3071 lines (2937 loc) · 142 KB
/
pbinary.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- pbinary.e
-- =========
--
--constant datab4code = 01 --DEV (could/should be a format option?)
-- Implements the messy details of actually creating 32/64-bit PE/ELF executable files.
-- Executable files contain mountains of obscure and often historical/obsolete details. This
-- program implements all that technical complexity in the simplest and clearest way I know,
-- and I am more than reasonably confident you will fail to find anything simpler than this.
-- In any case, it certainly helps to sequester this lot away from the rest of the system.
-- The demo program filedump.exw can and should be used to diagnose problems with the output,
-- and I strongly recommend you to familiarise yourself with the operation of that before you
-- even think about reading any of the code below.
--
-- Interface: see CreateExecutable() below.
--
-- DLL files could also (theoretically) be created, but I have not finalised the export
-- settings required (DLLMain, etc?), and in fact it might not need any changes to the
-- compiler at all, just a set of rules of the form "you must have this, that, etc".
--
-- work in progress:
-- MZ (done)
-- PE/RVA (done)
-- sections (done)
-- imports (done)
-- exports (done, triggered false positive)
-- relocations (done, cleared that FP)
-- resources (done)
-- data (sort of)
-- code (sort of)
-- thunk references need to be a linked list in code.... (done, ish)
--
-- Cross platform operation:
-- On Windows, you can create a windows PE format executable or a Linux ELF format binary
-- simply by changing(/inserting) a format directive in the program source. However, on
-- Linux, unless you run the Windows version of Phix on Wine, PE format executables may
-- have incorrect timestamps and checksums, because various system routines (GetSystemTime
-- and SystemTimeToFileTime in kernel32.dll, and CheckSumMappedFile in imagehlp.dll) are
-- not available. They may work just fine, they may trigger false positives in your virus
-- scanner, and given both the freely available workaround, plus the need to perform such
-- virus scanning, and in most cases build a Windows Installer, I do not plan on expending
-- any further effort trying to improve upon that situation.
-- TRY2:
-- Note about cross platform operation:
-- While the Windows versions of Phix can create both PE and ELF binaries (and running said
-- on Wine is the suggested workaround to this), the native Linux versions will struggle to
-- set correct timestamps and checksums if asked to create PE binaries, since that is done
-- using routines in kernel32.dll and imagehlp.dll. Otherwise you should be able to use any
-- version to create any other version, eg a 32bit PE p.exe can create 64bit ELF binaries.
--
constant M32 = 32,
M64 = 64
constant BYTE = 1,
WORD = 2*BYTE, -- (suppresses unused warning)
DWORD = 4,
QWORD = 8
function stringify(sequence s)
string res
integer ch
if string(s) then
res = s
else
res = repeat(' ',length(s))
for i=1 to length(s) do
ch = s[i]
res[i] = ch
end for
end if
return res
end function
--DEV:
--function SetField(string res, integer offset, integer dsize, atom v)
function SetField(sequence res, integer offset, integer dsize, atom v)
--
-- Breakup v into dsize bytes (little endian) in res at offset.
-- Note that offset as passed is 0-based, adjusted here(+1) to index res.
-- dsize is BYTE(1), WORD(2), DWORD(4), or QWORD(8)
-- This routine, btw, does not distinguish between -1 and +4294967295 in v.
-- Also note this relies on PBR semantics for performance, ie res=SetField(res,..),
-- where res is a local sequence, as opposed to a file-level or global sequence.
--
--DEV fixme
--!/**/ #isginfo{res,0b1000,MIN,MAX,integer,-2} -- verify this is a string
for i=1 to dsize do
res[offset+i] = and_bits(v,#FF)
v = floor(v/#100)
end for
return res
end function
constant mul = {#1,
#100,
#10000,
#1000000,
#100000000,
#10000000000,
#1000000000000,
#100000000000000}
--DEV
--function GetField(string res, integer offset, integer dsize)
function GetField(sequence res, integer offset, integer dsize)
--
-- Reassemble dsize bytes in res at offset to an unsigned int/atom result.
-- Note that offset as passed is 0-based, adjusted here(+1) to index res.
-- dsize is BYTE(1), WORD(2), DWORD(4), or QWORD(8)
--
atom v = 0
for i=1 to dsize do
v += res[offset+i]*mul[i]
end for
return v
end function
--DEV integer userip, rather then integer machine?
--function fixup(string res, sequence relocations, integer machine, integer userip, integer Base)
function fixup(sequence res, sequence relocations, integer machine, integer userip, integer Base, integer traceit=0) --DEV
--
-- res can be code or data (or possibly something else)
-- relocations contains a list of addresses of dwords (even on M64) that contain
-- (just) an offset from the start of the target section; now that we know the
-- virtual address where that section is going to be, update them appropriately.
-- Note that all entries in relocations (as passed here) refer to the same target
-- section (as well, of course, as all being somewhere in res).
-- machine should be M32 or M64
-- rip should be 0 for all M32 use and 1 for M64/code. [DEV?]
-- Note that for M32, Base is the virtual address (start) of whatever section the
-- relocations target (which can be the same section we are currently patching),
-- whereas for M64 it is either (data-code) or (code-data), depending on whether
-- we are relocating data references in the code section or vice versa (and it
-- makes no sense to relocate references to the same section with RIP). [DEV or 0?]
-- Also note this routine is used for both PE and ELF formats.
--
integer addr
atom v, vhi
integer lres = length(res) -- temp/debug
--DEV fixme
--!/**/ #isginfo{res,0b1000,MIN,MAX,integer,-2} -- verify this is a string
for i=1 to length(relocations) do
addr = relocations[i]
v = GetField(res,addr,DWORD)
if machine=M64 then
--DEV 5/12/14... (maybe we only need this for data section?) [DONE, 6/12/14, and undone immediately!]
vhi = GetField(res,addr+4,DWORD)
-- vhi = 0
if traceit then
-- printf(1,"fixup %08x: %08x, %08x [%d, %d, %d]\n",{addr,v,vhi,vhi=#80000000,vhi,#80000000})
end if
end if
-- if machine=M32 then
if userip=0 then
v += Base
else -- M64, RIP addressing
-- if userip then
v += Base-(addr+4)
-- else
-- v += Base
end if
--DEV not DLL... (must be done after loading)
if machine=M32 then
if and_bits(v,#80000000) then
v = floor(v/4)+#20000000
end if
else -- machine=64
-- (assumes <=4GB executables)
-- DEV not convinced this will be right for -ve offsets, btw
--DEV
-- if vhi=#80000000 then
if vhi/#100=#800000 then
-- res = SetField(res,addr+4,DWORD,#40000000)
res = SetField(res,addr+4,DWORD,vhi/2)
v = floor(v/4)
--DEV (3/1/16) try: (no help)
-- elsif and_bits(vhi,#80000000) then
-- ?9/0
-- elsif and_bits(v,#80000000) then
-- res = SetField(res,addr+4,DWORD,#40000000)
-- v = floor(v/4)+#20000000
end if
end if
res = SetField(res,addr,DWORD,v)
end for
return res
end function
--DEV/erm
--/*
for i=1 to length(relocations) do
addr = relocations[i]
v = GetField(res,addr,DWORD)
if machine=M32 then
v += Base
if and_bits(v,#80000000) then
v = floor(v/4)+#20000000
end if
else -- M64, RIP addressing
-- if userip then
if and_bits(v,#80000000) then
if v!=#80000000 then ?9/0 end if
-- v = floor(v/4)+#20000000
v = #40000000
res = SetField(res,addr,DWORD,v)
else
if v!=0 then ?9/0 end if
end if
addr += 4
v = GetField(res,addr,DWORD)
v += Base-(addr+4)
-- else
-- v += Base
end if
--DEV not DLL... (must be done after loading)
res = SetField(res,addr,DWORD,v)
end for
--*/
-- PE routines
-- ===========
constant GUI = 2,
CUI = 3
--DEV not linux friendly...
include builtins\timestamp.ew -- SYSTEMTIMEtoDateTimeStamp
atom TimeDateStamp
constant FileAlignment = #200,
SectionAlignment = #1000
--function RoundToDwordAlignment(integer v)
-- return floor((v+3)/4)*4
--end function
function RoundToFileAlignment(integer v)
return floor((v+FileAlignment-1)/FileAlignment)*FileAlignment
end function
function RoundToSectionAlignment(integer v)
return floor((v+SectionAlignment-1)/SectionAlignment)*SectionAlignment
end function
function pad_to(string s, integer wordsize)
integer padding = and_bits(length(s),wordsize-1)
if padding then
s &= stringify(repeat('\0',wordsize-padding))
end if
return s
end function
constant
IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
-- IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004, -- (deprecated)
-- IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008, -- (deprecated)
-- IMAGE_FILE_AGGRESIVE_WS_TRIM = 0x0010, -- (obsolete)
IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
-- 0x0040, -- (reserved for future use)
-- IMAGE_FILE_BYTES_REVERSED_LO = 0x0080, -- (deprecated)
IMAGE_FILE_32BIT_MACHINE = 0x0100,
IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
-- IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
-- IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
-- IMAGE_FILE_SYSTEM = 0x1000,
IMAGE_FILE_DLL = 0x2000,
-- IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
-- IMAGE_FILE_BYTES_REVERSED_HI = 0x8000, -- (deprecated)
$
constant -- (excludes OBJ-only settings)
-- IMAGE_SCN_SCALE_INDEX = 0x00000001,
IMAGE_SCN_CNT_CODE = 0x00000020, -- Section contains code.
IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040, -- Section contains initialized data.
-- IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080, -- Section contains uninitialized data.
-- IMAGE_SCN_LNK_OTHER = 0x00000100,
-- IMAGE_SCN_NO_DEFER_SPEC_EXC = 0x00004000,
-- IMAGE_SCN_GPREL = 0x00008000,
-- IMAGE_SCN_MEM_FARDATA = 0x00008000,
-- IMAGE_SCN_MEM_LOCKED = 0x00040000,
-- IMAGE_SCN_MEM_PRELOAD = 0x00080000,
-- IMAGE_SCN_LNK_NRELOC_OVFL = 0x01000000,
IMAGE_SCN_MEM_DISCARDABLE = 0x02000000,
-- IMAGE_SCN_MEM_NOT_CACHED = 0x04000000,
-- IMAGE_SCN_MEM_NOT_PAGED = 0x08000000,
-- IMAGE_SCN_MEM_SHARED = 0x10000000,
IMAGE_SCN_MEM_EXECUTE = 0x20000000,
IMAGE_SCN_MEM_READ = 0x40000000,
IMAGE_SCN_MEM_WRITE = 0x80000000,
$
constant
-- 0x0001, -- ? process attachment notify? \
-- 0x0002, -- ? thread detachment notify? } Reserved, must be zero
-- 0x0004, -- ? thread attachment notify? /
-- 0x0008, -- ? process detachment notify? /
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040,
-- IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY = 0x0080,
-- IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100,
-- IMAGE_DLLCHARACTERISTICS_NO_ISOLATION = 0x0200,
-- IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400,
-- IMAGE_DLLCHARACTERISTICS_NO_BIND = 0x0800,
-- IMAGE_DLLCHARACTERISTICS_WDM_DRIVER = 0x2000,
-- IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000,
$
sequence sectionNames = {},
sectionCharacteristics = {}
procedure DefineSectionCharacteristics(string name, atom flags)
sectionNames = append(sectionNames,name)
sectionCharacteristics = append(sectionCharacteristics,flags)
end procedure
-- DefineSectionCharacteristics(".idata",IMAGE_SCN_MEM_READ+IMAGE_SCN_CNT_INITIALIZED_DATA) -- +write?
DefineSectionCharacteristics(".idata",IMAGE_SCN_MEM_READ+IMAGE_SCN_MEM_WRITE+IMAGE_SCN_CNT_INITIALIZED_DATA)
DefineSectionCharacteristics(".edata",IMAGE_SCN_MEM_READ+IMAGE_SCN_CNT_INITIALIZED_DATA)
DefineSectionCharacteristics(".reloc",IMAGE_SCN_MEM_READ+IMAGE_SCN_CNT_INITIALIZED_DATA+IMAGE_SCN_MEM_DISCARDABLE)
DefineSectionCharacteristics(".pdata",IMAGE_SCN_MEM_READ+IMAGE_SCN_CNT_INITIALIZED_DATA)
DefineSectionCharacteristics(".data",IMAGE_SCN_MEM_READ+IMAGE_SCN_MEM_WRITE+IMAGE_SCN_CNT_INITIALIZED_DATA)
--DEV lets have .code rather than .text:
DefineSectionCharacteristics(".text",IMAGE_SCN_CNT_CODE+IMAGE_SCN_MEM_EXECUTE+IMAGE_SCN_MEM_READ)
-- DefineSectionCharacteristics(".code",IMAGE_SCN_CNT_CODE+IMAGE_SCN_MEM_EXECUTE+IMAGE_SCN_MEM_READ)
DefineSectionCharacteristics(".rsrc",IMAGE_SCN_MEM_READ+IMAGE_SCN_CNT_INITIALIZED_DATA)
function mzHeader()
-- (same for 32 and 64 bit PE executables)
sequence res
--string res
--puts(1,"warning: e_magic of ZZ line 312, pbinary.e\n")
res = stringify( -- offset name size value desc
{'M','Z', -- 00000000,e_magic, x2, MZ, signature (should be "MZ")
-- {'Z','Z', -- 00000000,e_magic, x2, MZ, signature (should be "MZ")
#80,#00, -- 00000002,e_cblp, h2, 0080h, Bytes in last block of file
1,0, -- 00000004,e_cp, 2, 1, Blocks in file
0,0, -- 00000006,e_crlc, 2, 0, Relocations
4,0, -- 00000008,e_cparhdr, 2, 4, Size of header in paragraphs
#10,#00, -- 0000000A,e_minalloc, h2, 0010h, Minimum extra paragraphs needed
#FF,#FF, -- 0000000C,e_maxalloc, h2, FFFFh, Maximum extra paragraphs needed
0,0, -- 0000000E,e_ss, h2, 0000h, Initial (relative) SS value [ignore]
#40,#01, -- 00000010,e_sp, h2, 0140h, Initial SP value [ignore]
0,0, -- 00000012,e_csum, h2, 0000h, Checksum
0,0, -- 00000014,e_ip, h2, 0000h, Initial IP value [ignore]
0,0, -- 00000016,e_cs, h2, 0000h, Initial (relative) CS value [ignore]
#40,#00, -- 00000018,e_lfarlc, h2, 0040h, File address of relocation table
0,0, -- 0000001A,e_ovno, h2, 0000h, Overlay number
0,0,0,0, -- 0000001C,e_res, h8, 0h, Reserved words
0,0,0,0,
0,0, -- 00000024,e_oemid, h2, 0000h, OEM identifier (for e_oeminfo)
0,0, -- 00000026,e_oeminfo, h2, 0000h, OEM information; e_oemid specific
0,0,0,0, -- 00000028,e_res2, h20,0h, Reserved words
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
#80,0,0,0, -- 0000003C,e_lfanew, h4, 00000080h,File address of new exe header
#0E, -- 00000040,push cs,h1,0Eh,
#1F, -- 00000041,pop ds,h1,1Fh,(ie data segment:=code segment)
#BA,#0E,#00, -- 00000042,mov dx,0x000E,h3,BA0E00h,(ie ds:dx is text string below)
#B4,#09, -- 00000045,mov ah,0x09,h2,B409h,(write to stdout)
#CD,#21, -- 00000047,int 0x21,h2,CD21h,
#B8,#01,#4C, -- 00000049,mov ax,0x4C01,h3,B8014Ch,(exit)
#CD,#21, -- 0000004C,int 0x21,h2,CD21h,
'T','h','i','s',' ','p','r','o','g','r','a','m',' ','c','a','n','n','o','t',' ',
'b','e',' ','r','u','n',' ','i','n',' ','D','O','S',' ','m','o','d','e','.',#0D,#0A,'$',
0,0,0,0,
0,0,0,0})
return res
end function
sequence inames
function by_importname(integer i, integer j)
return compare(inames[i],inames[j])
end function
sequence enames
function by_exportname(integer i, integer j)
return compare(enames[i],enames[j])
end function
constant HighLow = #3000
integer actual_reloc_size = 0
function relocate(sequence relocations, sequence ridx, sequence Bases)
integer Base
integer first
integer n
integer PageRVA
integer BlockSize
string block
integer bidx
integer TypeOffset
string res = ""
integer lastRVA = 0
if Bases[1]>Bases[2] then
relocations = reverse(relocations)
ridx = reverse(ridx)
Bases = reverse(Bases)
-- elsif length(relocations)=3 then
-- relocations = {relocations[3],relocations[1],relocations[2]}
-- ridx = {ridx[3],ridx[1],ridx[2]}
-- Bases = {Bases[3],Bases[1],Bases[2]}
end if
for i=1 to length(ridx) do
Base = Bases[i]
for j=1 to length(ridx[i]) do
{first,n,PageRVA} = ridx[i][j]
PageRVA = PageRVA*#1000+Base
-- if PageRVA<=lastRVA then ?9/0 end if
if PageRVA<=lastRVA then ?"pbinary.e: 9/0 line 420\n" end if
lastRVA = PageRVA
BlockSize = 8+n*2
actual_reloc_size += BlockSize
--?{actual_reloc_size,n,BlockSize}
block = repeat(' ',BlockSize)
if and_bits(n,1) then
BlockSize += 2
end if
block = SetField(block,#0,DWORD,PageRVA)
block = SetField(block,#4,DWORD,BlockSize)
bidx = 8
for k=first to first+n-1 do
integer rik = and_bits(relocations[i][k],#0FFF)
-- if rik<0 or rik>#0FFF then ?9/0 end if
TypeOffset = rik+HighLow
block = SetField(block,bidx,WORD,TypeOffset)
bidx += 2
end for
res &= block
if and_bits(n,1) then
res &= repeat('\0',2)
actual_reloc_size += 2
end if
end for
end for
return res
end function
--DEV (for listing)
--25/4/16 (pHeap using mmap)
--global integer BaseOfData2, SizeOfData2
--global integer BaseOfCode2, SizeOfCode2
global atom BaseOfData2, SizeOfData2
global atom BaseOfCode2, SizeOfCode2
--global integer IB
global integer ImageBase2
--with trace
function peHeader(integer machine, integer subsystem, integer subvers, sequence imports, sequence exports, sequence relocations, integer datalen, integer codelen, integer resourcelen)
integer BaseOfData
integer BaseOfCode
--integer BaseOfImports
integer resourceBase
integer SizeOfImage
integer importRVA
integer importBase
--
-- machine should be M32 or M64
-- subsystem should be CUI or GUI
-- imports should be eg {{"kernel32.dll",{"GetLastError","FormatMessageA","LocalFree"},{#17,#4C,#58}},
-- {"user32.dll",{"MessageBoxA"},{#69}}},
-- or (rarely) {} if no imports whatsoever are required
-- Routine names get automatically [tag]sorted per dll, as required.
-- The {#17,#4C,#58} and {#69} are link chain starts, not used here.
-- exports should be eg {"errormsg.dll",{"ShowErrorMessage","ShowLastError"},{#0000000C,#0000004E}}
-- (where #C,#4E are the corresponding offsets into the code section)
-- (the names and offsets are automatically [tag]sorted, as required)
-- or {} if not a dll
--datab4code: (and this description is out-of-date/incomplete anyway)
-- relocations should be {} or {{code_relocations},{data_relocations}}
-- relocations should be {} or {{data_relocations},{code_relocations}}
-- where each entry is a HighLow offset into the
-- appropriate section. Should only be required
-- for 32-bit DLLs, not exes, not 64-bit DLLs.
-- Also, cmiiw, the resource section does not
-- need or get relocations, even if logically
-- the DataRVA therein ought to be adjusted.
-- For data, code, and resources, this routine only needs to know the lengths (pre-padding).
--
-- Returns: A sequence containing the MZ and PE headers, RVA (Relative Virtual Address) table,
-- the completed section table, and any required imports, exports, and relocations,
-- suitable for writing directly to a file (previously opened in binary mode).
-- Also sets BaseOfData, BaseOfCode, and resourceBase, which will likely need to be
-- applied to the revelant block(s). [Hopefully without changing the size of them!]
-- Calling routine is responsible for writing data, code, and resources, with
-- appropriate padding (which should /not/ be included in the lengths passed).
--
--DEV:
--sequence res
string res
sequence sections -- {{RVAdx,name,VirtualSize,VirtualAddress}}
integer RVAaddr
--sequence names
integer thunks,
thunkstart,
isize = 0, -- imports
ibase,
esize = 0, -- exports
elen,
ebase,
xsize = 0, -- exceptions (PE64 only)
xbase,
rsize = 0, -- relocs/resources
rbase,
prevbase,
newbase,
-- dsize,
-- csize,
RVAtable,
SizeOfData,
SizeOfCode,
padding
sequence thunk
sequence ridx
integer RelativeVirtualAddress,
VirtualSize,
VirtualAddress,
k
integer HeaderCharacteristics
sequence itags
sequence ithunks
sequence etags
integer ImageBase
--DEV:
sequence section
string name
integer ln
integer SizeOfRawData
atom v
--integer symidx
--object tmp
integer expected_reloc_size
--puts(1,"peHeader: entry\n")
ImageBase = #00400000 -- DEV what about DLLs?
ImageBase2 = ImageBase
HeaderCharacteristics = IMAGE_FILE_EXECUTABLE_IMAGE +
-- IMAGE_FILE_LINE_NUMS_STRIPPED +
-- IMAGE_FILE_LOCAL_SYMS_STRIPPED +
IMAGE_FILE_DEBUG_STRIPPED
sections = {}
RVAaddr = #1000
SizeOfImage = 0 -- (+length(MZ/PE/etc) done last)
if length(imports) then
thunks = 0
isize = (length(imports)+1)*20
itags = repeat(0,length(imports))
ithunks = repeat(0,length(imports))
for i=1 to length(imports) do
isize += length(imports[i][1])+1
-- 5/10/14:
if and_bits(isize,1) then isize += 1 end if
inames = imports[i][2]
ithunks[i] = tagset(length(inames)) -- (replaced with thunk addrs later)
itags[i] = custom_sort(routine_id("by_importname"),ithunks[i])
thunks += length(inames)+1
for j=1 to length(inames) do
-- isize += 2+length(inames[itags[i][j]])+1 -- (not necessary)
isize += 2+length(inames[j])+1
-- 5/10/14:
if and_bits(isize,1) then isize += 1 end if
end for
end for
if machine=M32 then
isize += thunks*4
else --machine=M64
isize += thunks*8
end if
ibase = RVAaddr
RVAaddr += RoundToSectionAlignment(isize)
sections = append(sections,{2,".idata",isize,ibase})
-- BaseOfImports = RVAaddr (untried)
-- BaseOfImports = ibase
SizeOfImage += RoundToFileAlignment(isize)
else
ithunks = {}
end if
if length(exports) then
enames = exports[2]
etags = custom_sort(routine_id("by_exportname"),tagset(length(exports[2])))
elen = length(exports[2])
esize = #28+elen*10+length(exports[1])+1
-- 5/10/14:
if and_bits(esize,1) then esize += 1 end if
for i=1 to elen do
esize += length(exports[2][i])+1
-- 5/10/14:
if and_bits(esize,1) then esize += 1 end if
end for
ebase = RVAaddr
RVAaddr += RoundToSectionAlignment(esize)
sections = append(sections,{1,".edata",esize,ebase})
SizeOfImage += RoundToFileAlignment(esize)
HeaderCharacteristics += IMAGE_FILE_DLL
if machine=M64 then
HeaderCharacteristics += IMAGE_FILE_RELOCS_STRIPPED
end if
else
HeaderCharacteristics += IMAGE_FILE_RELOCS_STRIPPED
end if
if gexch!=0 then -- global exception handler (PE64 only)
-- ?9/0
xsize = #0C
xbase = RVAaddr
RVAaddr += RoundToSectionAlignment(xsize)
sections = append(sections,{4,".pdata",xsize,xbase})
SizeOfImage += RoundToFileAlignment(xsize)
end if
if length(relocations) then
-- (only needed for 32-bit dlls)
if equal(relocations,{{},{}}) then ?9/0 end if
rsize = 10
ridx = {{},{}}
for i=1 to length(relocations) do
if length(relocations[i]) then
prevbase = floor(relocations[i][1]/#1000)
ridx[i] = append(ridx[i],{1,1,prevbase})
for j=2 to length(relocations[i]) do
newbase = floor(relocations[i][j]/#1000)
if newbase=prevbase then
ridx[i][$][2] += 1
rsize += 2
else
if and_bits(ridx[i][$][2],1) then
rsize += 2
end if
--?{rsize}
ridx[i] = append(ridx[i],{j,1,newbase})
rsize += 10
prevbase = newbase
end if
end for
if and_bits(ridx[i][$][2],1) then
rsize += 2
end if
--?rsize
end if
----??rsize+=8??
if i=1 then rsize+=10 end if
end for
rbase = RVAaddr
RVAaddr += RoundToSectionAlignment(rsize)
sections = append(sections,{6,".reloc",rsize,rbase})
expected_reloc_size = rsize
SizeOfImage += RoundToFileAlignment(rsize)
end if
--if datab4code then
sections = append(sections,{0,".data",datalen,RVAaddr})
BaseOfData = RVAaddr
BaseOfData2 = BaseOfData
SizeOfData2 = datalen
RVAaddr += RoundToSectionAlignment(datalen)
SizeOfImage += RoundToFileAlignment(datalen)
-- sections = append(sections,{0,".code",codelen,RVAaddr})
sections = append(sections,{0,".text",codelen,RVAaddr})
BaseOfCode = RVAaddr
BaseOfCode2 = BaseOfCode
SizeOfCode2 = codelen
RVAaddr += RoundToSectionAlignment(codelen)
SizeOfImage += RoundToFileAlignment(codelen)
--else
-- sections = append(sections,{0,".text",codelen,RVAaddr})
-- BaseOfCode = RVAaddr
--BaseOfCode2 = BaseOfCode
--SizeOfCode2 = codelen
-- RVAaddr += RoundToSectionAlignment(codelen)
-- SizeOfImage += RoundToFileAlignment(codelen)
-- sections = append(sections,{0,".data",datalen,RVAaddr})
-- BaseOfData = RVAaddr
--BaseOfData2 = BaseOfData
--SizeOfData2 = datalen
-- RVAaddr += RoundToSectionAlignment(datalen)
-- SizeOfImage += RoundToFileAlignment(datalen)
--end if
resourceBase = RVAaddr
if resourcelen!=0 then
rsize = resourcelen
-- aside: we are reusing rsize; in SizeOfData calculations
-- below, we want this (rsize/resources) and /not/
-- relocations (which/as that is "discardable").
sections = append(sections,{3,".rsrc",rsize,RVAaddr})
RVAaddr += RoundToSectionAlignment(rsize)
SizeOfImage += RoundToFileAlignment(rsize)
end if
-- SizeOfImage = RVAaddr
--?SizeOfImage
integer SizeOfImage2 = RVAaddr
--?res
res = mzHeader()
--?res
-- Note: The following offsets assume e_lfanew is #80. If you are writing a program which reads
--- exe/dll files, then (for example) Subsystem is at (e_lfanew)+#5C rather than #DC.
if machine=M32 then
res &= stringify( -- offset name size value desc
{'P','E',0,0, -- 00000080,signature, x4, PE\0\0, should be "PE\0\0"
#4C,#01, -- 00000084,machine, h2, 014Ch, i386
0,0, -- 00000086,sections, 2, 3, Number of sections
0,0,0,0, -- 00000088,DateTimeStamp, h4, 52EFE35Fh, 03 February 2014, 18:43:43 (ish)
0,0,0,0, -- 0000008C,PointerToSymbolTable, h4, 00000000h,
0,0,0,0, -- 00000090,NumberOfSymbols, h4, 00000000h,
#E0,#00, -- 00000094,SizeOfOptionalHeader, h2, 00E0h,
#0F,#01, -- 00000096,Characteristics, h2, 010Fh, 32BIT+EXEC-RELOCS-LINES-LOCALS
#0B,#01, -- 00000098,Magic, h2, 010Bh, 32bit
#01, -- 0000009A,MajorLinkerVersion, h1, 01h, 1.70
#46, -- 0000009B,MinorLinkerVersion, h1, 46h,
0,0,0,0, -- 0000009C,SizeOfCode, h4, 00000200h,
0,0,0,0, -- 000000A0,SizeOfInitializedData, h4, 00000400h,
0,0,0,0, -- 000000A4,SizeOfUninitializedData, h4, 00000000h,
0,0,0,0, -- 000000A8,AddressOfEntryPoint, h4, 00002000h,
0,0,0,0, -- 000000AC,BaseOfCode, h4, 00002000h,
0,0,0,0, -- 000000B0,BaseOfData, h4, 00001000h,
0,0,#40,0, -- 000000B4,ImageBase, h4, 00400000h,
0,#10,0,0, -- 000000B8,SectionAlignment, h4, 00001000h,
0,#02,0,0, -- 000000BC,FileAlignment, h4, 00000200h,
1,0, -- 000000C0,MajorOperatingSystemVersion,h2, 0001h, 1.0
0,0, -- 000000C2,MinorOperatingSystemVersion,h2, 0000h,
0,0, -- 000000C4,MajorImageVersion, h2, 0000h, 0.0
0,0, -- 000000C6,MinorImageVersion, h2, 0000h,
4,0, -- 000000C8,MajorSubsystemVersion, h2, 0004h, 4.0
0,0, -- 000000CA,MinorSubsystemVersion, h2, 0000h,
0,0,0,0, -- 000000CC,Win32VersionValue, h4, 00000000h,
0,0,0,0, -- 000000D0,SizeOfImage, h4, 00004000h,
0,2,0,0, -- 000000D4,SizeOfHeaders, h4, 00000200h,
0,0,0,0, -- 000000D8,CheckSum, h4, 000025F4h,
2,0, -- 000000DC,Subsystem, h2, 0002h, 2=GUI,3=CUI
0,0, -- 000000DE,DllCharacteristics, h2, 0000h,
0,#10,0,0, -- 000000E0,SizeOfStackReserve, h4, 00001000h,
0,#10,0,0, -- 000000E4,SizeOfStackCommit, h4, 00001000h,
0,0,#01,0, -- 000000E8,SizeOfHeapReserve, h4, 00010000h,
0,0,0,0, -- 000000EC,SizeOfHeapCommit, h4, 00000000h,
0,0,0,0, -- 000000F0,LoaderFlags, h4, 00000000h,
16,0,0,0, -- 000000F4,NumberOfRvaAndSizes, 4, 16,
0,0,0,0, -- 000000F8,RelativeVirtualAddress[1], h4, 00000000h, export
0,0,0,0, -- 000000FC,RVASize[1], h4, 00000000h,
0,0,0,0, -- 00000100,RelativeVirtualAddress[2], h4, 00003000h, import
0,0,0,0, -- 00000104,RVASize[2], h4, 000000B0h,
0,0,0,0, -- 00000108,RelativeVirtualAddress[3], h4, 00000000h, resource
0,0,0,0, -- 0000010C,RVASize[3], h4, 00000000h,
0,0,0,0, -- 00000110,RelativeVirtualAddress[4], h4, 00000000h, exception
0,0,0,0, -- 00000114,RVASize[4], h4, 00000000h,
0,0,0,0, -- 00000118,RelativeVirtualAddress[5], h4, 00000000h, security
0,0,0,0, -- 0000011C,RVASize[5], h4, 00000000h,
0,0,0,0, -- 00000120,RelativeVirtualAddress[6], h4, 00000000h, basereloc
0,0,0,0, -- 00000124,RVASize[6], h4, 00000000h,
0,0,0,0, -- 00000128,RelativeVirtualAddress[7], h4, 00000000h, debug
0,0,0,0, -- 0000012C,RVASize[7], h4, 00000000h,
0,0,0,0, -- 00000130,RelativeVirtualAddress[8], h4, 00000000h, architecture
0,0,0,0, -- 00000134,RVASize[8], h4, 00000000h,
0,0,0,0, -- 00000138,RelativeVirtualAddress[9], h4, 00000000h, globalptr
0,0,0,0, -- 0000013C,RVASize[9], h4, 00000000h,
0,0,0,0, -- 00000140,RelativeVirtualAddress[10], h4, 00000000h, TLS table
0,0,0,0, -- 00000144,RVASize[10], h4, 00000000h,
0,0,0,0, -- 00000148,RelativeVirtualAddress[11], h4, 00000000h, loadconfig
0,0,0,0, -- 0000014C,RVASize[11], h4, 00000000h,
0,0,0,0, -- 00000150,RelativeVirtualAddress[12], h4, 00000000h, boundimport
0,0,0,0, -- 00000154,RVASize[12], h4, 00000000h,
0,0,0,0, -- 00000158,RelativeVirtualAddress[13], h4, 00000000h, IAT
0,0,0,0, -- 0000015C,RVASize[13], h4, 00000000h,
0,0,0,0, -- 00000160,RelativeVirtualAddress[14], h4, 00000000h, delayimport
0,0,0,0, -- 00000164,RVASize[14], h4, 00000000h,
0,0,0,0, -- 00000168,RelativeVirtualAddress[15], h4, 00000000h, comdescriptor
0,0,0,0, -- 0000016C,RVASize[15], h4, 00000000h,
0,0,0,0, -- 00000170,RelativeVirtualAddress[16], h4, 00000000h, IDD 16
0,0,0,0}) -- 00000174,RVASize[16], h4, 00000000h,
if length(res)!=#178 then ?9/0 end if
if GetField(res,#B4,DWORD)!=ImageBase then ?9/0 end if
RVAtable = #F8
res = SetField(res,#B0,DWORD,BaseOfData)
--DEV/SUG:
-- if and_bits(HeaderCharacteristics,IMAGE_FILE_DLL)!=0 then
-- res = SetField(res,#34,DWORD,#100000000)
-- end if
HeaderCharacteristics += IMAGE_FILE_32BIT_MACHINE
elsif machine=M64 then
res &= stringify( -- offset name size value desc
{'P','E',0,0, -- 00000080,signature, x4, PE\0\0, should be "PE\0\0"
#64,#86, -- 00000084,machine, h2, 8664h, amd64
3,0, -- 00000086,sections, 2, 3, Number of sections
0,0,0,0, -- 00000088,DateTimeStamp, h4, 52EFE35Fh, 03 February 2014, 18:43:43 (ish)
0,0,0,0, -- 0000008C,PointerToSymbolTable, h4, 00000000h,
0,0,0,0, -- 00000090,NumberOfSymbols, h4, 00000000h,
#F0,#00, -- 00000094,SizeOfOptionalHeader, h2, 00F0h,
#2F,#00, -- 00000096,Characteristics, h2, 002Fh, EXEC-RELOCS-LINES-LOCALS+LARGE_ADDR
#0B,#02, -- 00000098,Magic, h2, 020Bh, 64bit
#01, -- 0000009A,MajorLinkerVersion, h1, 01h, 1.70
#46, -- 0000009B,MinorLinkerVersion, h1, 46h,
0,0,0,0, -- 0000009C,SizeOfCode, h4, 00000200h,
0,0,0,0, -- 000000A0,SizeOfInitializedData, h4, 00000400h,
0,0,0,0, -- 000000A4,SizeOfUninitializedData, h4, 00000000h,
0,0,0,0, -- 000000A8,AddressOfEntryPoint, h4, 00001000h,
0,0,0,0, -- 000000AC,BaseOfCode, h4, 00001000h,
0,0,#40,0,0,0,0,0, -- 000000B0,ImageBase, h0, 0000000000400000h,
0,#10,0,0, -- 000000B8,SectionAlignment, h4, 00001000h,
0,#02,0,0, -- 000000BC,FileAlignment, h4, 00000200h,
1,0, -- 000000C0,MajorOperatingSystemVersion,h2, 0001h, 1.0
0,0, -- 000000C2,MinorOperatingSystemVersion,h2, 0000h,
0,0, -- 000000C4,MajorImageVersion, h2, 0000h, 0.0
0,0, -- 000000C6,MinorImageVersion, h2, 0000h,
5,0, -- 000000C8,MajorSubsystemVersion, h2, 0005h, 5.0
0,0, -- 000000CA,MinorSubsystemVersion, h2, 0000h,
0,0,0,0, -- 000000CC,Win32VersionValue, h4, 00000000h,
0,0,0,0, -- 000000D0,SizeOfImage, h4, 00004000h,
0,2,0,0, -- 000000D4,SizeOfHeaders, h4, 00000200h,
0,0,0,0, -- 000000D8,CheckSum, h4, 000025F4h,
2,0, -- 000000DC,Subsystem, h2, 0002h, 2=GUI,3=CUI
0,0, -- 000000DE,DllCharacteristics, h2, 0000h,
0,#10,0,0,0,0,0,0, -- 000000E0,SizeOfStackReserve, h8, 0000000000001000h,
0,#10,0,0,0,0,0,0, -- 000000E8,SizeOfStackCommit, h8, 0000000000001000h,
0,0,#01,0,0,0,0,0, -- 000000F0,SizeOfHeapReserve, h8, 0000000000010000h,
0,0,0,0,0,0,0,0, -- 000000F8,SizeOfHeapCommit, h8, 0000000000000000h,
0,0,0,0, -- 00000100,LoaderFlags, h4, 00000000h,
16,0,0,0, -- 00000104,NumberOfRvaAndSizes, 4, 16,
0,0,0,0, -- 00000108,RelativeVirtualAddress[1], h4, 00000000h, export
0,0,0,0, -- 0000010C,RVASize[1], h4, 00000000h,
0,0,0,0, -- 00000110,RelativeVirtualAddress[2], h4, 00003000h, import
0,0,0,0, -- 00000114,RVASize[2], h4, 00000189h,
0,0,0,0, -- 00000118,RelativeVirtualAddress[3], h4, 00000000h, resource
0,0,0,0, -- 0000011C,RVASize[3], h4, 00000000h,
0,0,0,0, -- 00000120,RelativeVirtualAddress[4], h4, 00000000h, exception
0,0,0,0, -- 00000124,RVASize[4], h4, 00000000h,
0,0,0,0, -- 00000128,RelativeVirtualAddress[5], h4, 00000000h, security
0,0,0,0, -- 0000012C,RVASize[5], h4, 00000000h,
0,0,0,0, -- 00000130,RelativeVirtualAddress[6], h4, 00000000h, basereloc
0,0,0,0, -- 00000134,RVASize[6], h4, 00000000h,
0,0,0,0, -- 00000138,RelativeVirtualAddress[7], h4, 00000000h, debug
0,0,0,0, -- 0000013C,RVASize[7], h4, 00000000h,
0,0,0,0, -- 00000140,RelativeVirtualAddress[8], h4, 00000000h, architecture
0,0,0,0, -- 00000144,RVASize[8], h4, 00000000h,
0,0,0,0, -- 00000148,RelativeVirtualAddress[9], h4, 00000000h, globalptr
0,0,0,0, -- 0000014C,RVASize[9], h4, 00000000h,
0,0,0,0, -- 00000150,RelativeVirtualAddress[10], h4, 00000000h, TLS table
0,0,0,0, -- 00000154,RVASize[10], h4, 00000000h,
0,0,0,0, -- 00000158,RelativeVirtualAddress[11], h4, 00000000h, loadconfig
0,0,0,0, -- 0000015C,RVASize[11], h4, 00000000h,
0,0,0,0, -- 00000160,RelativeVirtualAddress[12], h4, 00000000h, boundimport
0,0,0,0, -- 00000164,RVASize[12], h4, 00000000h,
0,0,0,0, -- 00000168,RelativeVirtualAddress[13], h4, 00000000h, IAT
0,0,0,0, -- 0000016C,RVASize[13], h4, 00000000h,
0,0,0,0, -- 00000170,RelativeVirtualAddress[14], h4, 00000000h, delayimport
0,0,0,0, -- 00000174,RVASize[14], h4, 00000000h,
0,0,0,0, -- 00000178,RelativeVirtualAddress[15], h4, 00000000h, comdescriptor
0,0,0,0, -- 0000017C,RVASize[15], h4, 00000000h,
0,0,0,0, -- 00000180,RelativeVirtualAddress[16], h4, 00000000h, IDD 16
0,0,0,0}) -- 00000184,RVASize[16], h4, 00000000h,
if length(res)!=#188 then ?9/0 end if
if GetField(res,#B0,QWORD)!=ImageBase then ?9/0 end if
RVAtable = #108
--DEV/SUG:
-- if and_bits(HeaderCharacteristics,IMAGE_FILE_DLL)!=0 then
-- res = SetField(res,#30,QWORD,#100000000)
-- end if
HeaderCharacteristics += IMAGE_FILE_LARGE_ADDRESS_AWARE
else
?9/0
end if
--?res
for i=1 to length(sections) do
k = sections[i][1]
if k then
RelativeVirtualAddress = sections[i][4]
VirtualSize = sections[i][3]
res = SetField(res,RVAtable+(k-1)*8,DWORD,RelativeVirtualAddress)
res = SetField(res,RVAtable+(k-1)*8+4,DWORD,VirtualSize)
end if
end for
SizeOfData = RoundToFileAlignment(datalen) + -- data
RoundToFileAlignment(isize) + -- import section
RoundToFileAlignment(esize) -- export section
if resourcelen then
SizeOfData += RoundToFileAlignment(rsize) -- resources [/not/ relocations]
end if
SizeOfCode = RoundToFileAlignment(codelen)
res = SetField(res,#86,WORD,length(sections))
if GetField(res,#B8,DWORD)!=SectionAlignment then ?9/0 end if
res = SetField(res,#88,DWORD,TimeDateStamp)
res = SetField(res,#96,WORD,HeaderCharacteristics)
res = SetField(res,#9C,DWORD,SizeOfCode)
res = SetField(res,#A0,DWORD,SizeOfData)
res = SetField(res,#A8,DWORD,BaseOfCode) -- (EntryPoint is always the same as BaseOfCode)
res = SetField(res,#AC,DWORD,BaseOfCode)
if GetField(res,#B8,DWORD)!=SectionAlignment then ?9/0 end if
if GetField(res,#BC,DWORD)!=FileAlignment then ?9/0 end if
res = SetField(res,#C8,DWORD,subvers)
res = SetField(res,#D0,DWORD,SizeOfImage2)
res = SetField(res,#DC,WORD,subsystem)
if and_bits(HeaderCharacteristics,IMAGE_FILE_DLL)!=0 then
res = SetField(res,#DE,WORD,IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE)
end if
-- The section table starts at #178 (32-bit) or #188 (64-bit), each entry is #28 bytes long
integer PointerToRawData = RoundToFileAlignment(length(res)+length(sections)*#28)
-- importBase = PointerToRawData
importBase = #1000
if length(imports) then
if machine=M32 then
importRVA = #1000-PointerToRawData
else
importRVA = 0-PointerToRawData
end if
end if
--puts(1,"13\n")
--?res
for i=1 to length(sections) do
--DEV:
-- sequence section
-- string name
-- integer ln
-- integer SizeOfRawData
section = stringify( -- offset name size value desc
{0,0,0,0,0,0,0,0, -- 00000000,Name[1], x8,.data\0\0\0,
0,0,0,0, -- 00000008,VirtualSize,h4,0000001Dh,(not really used)
0,0,0,0, -- 0000000C,VirtualAddress,h4,00001000h,
0,0,0,0, -- 00000010,SizeOfRawData,h4,00000200h,
0,0,0,0, -- 00000014,PointerToRawData,h4,00000200h,
0,0,0,0, -- 00000018,PointerToRelocations,h4,00000000h,
0,0,0,0, -- 0000001C,PointerToLinenumbers,h4,00000000h,
0,0, -- 00000020,NumberOfRelocations,h2,0000h,
0,0, -- 00000022,NumberOfLinenumbers,h2,0000h,
0,0,0,0}) -- 00000024,Characteristics,h4,C0000040h,read+write+init
{?,name,VirtualSize,VirtualAddress} = sections[i]
ln = length(name)
if ln>8 then ?9/0 end if
section[1..ln] = name
if name=".pdata" then
VirtualSize = #18
end if
section = SetField(section,#08,DWORD,VirtualSize)
section = SetField(section,#0C,DWORD,VirtualAddress)
SizeOfRawData = RoundToFileAlignment(VirtualSize)
section = SetField(section,#10,DWORD,SizeOfRawData)
section = SetField(section,#14,DWORD,PointerToRawData)
v = sectionCharacteristics[find(name,sectionNames)]
section = SetField(section,#24,DWORD,v)
PointerToRawData += SizeOfRawData
res &= section
end for
padding = RoundToFileAlignment(length(res))-length(res)
if padding then
res &= stringify(repeat(0,padding))
end if
-- SizeOfImage += RoundToFileAlignment(length(res))
SizeOfImage += length(res) -- (+length(MZ/PE/etc), as promised. This is now the final value.)
--?SizeOfImage
if length(imports) then
thunkstart = (length(imports)+1)*20
isize = thunkstart
if machine=M32 then
isize += thunks*4
else --machine=M64
isize += thunks*8
end if
for i=1 to length(imports)+1 do
-- sequence section
section = stringify( -- offset name size value desc
{0,0,0,0, -- 00000000,OriginalFirstThunk, h4, 00000000h, Hint Name Array
0,0,0,0, -- 00000004,TimeDateStamp, h4, 00000000h,
0,0,0,0, -- 00000008,ForwarderChain, h4, 00000000h,
0,0,0,0, -- 0000000C,Name, h4, 0000303Ch, [0x0000063C:KERNEL32.DLL]
0,0,0,0}) -- 00000010,FirstThunk, h4, 00003064h, Hint Name Array, see 0x00000664
if i<=length(imports) then -- (not the terminating null block)
section = SetField(section,#0C,DWORD,isize+ibase)
isize += length(imports[i][1])+1
-- 5/10/14:
if and_bits(isize,1) then isize += 1 end if
section = SetField(section,#10,DWORD,thunkstart+ibase)
if machine=M32 then
thunkstart += (length(imports[i][2])+1)*4
else
thunkstart += (length(imports[i][2])+1)*8
end if
end if
res &= section
end for
-- thunks
for i=1 to length(imports) do
for j=1 to length(imports[i][2])+1 do
if machine=M32 then
thunk = stringify({0,0,0,0})
else
thunk = stringify({0,0,0,0,0,0,0,0})