Hello all
as the title said i have a Strange Behavior With Compiler Optimization in my code i have
My Unit Code
this code in a new unit not the main one
and i'm using
_begin and
_end to calc the code size in between
code_size := integer ((@_end)) - integer ((@_begin)) - sizeof (dword) ;
the full code
program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, sysutils, shellcode
{ you can add units after this };
var
code_size, i: Integer;
f: array[0..70-36] of Pointer = (
@__begin,
@algo,
@btmemorygetprocaddress,
@btmemoryloadlibary,
@finalizesections,
@getsectionprotection,
@buildimporttable,
@strcomp,
@allocmem,
@performbaserelocation,
@copysections,
@getimagesnapbyordinal,
@getimageordinal,
@getheaderdictionary,
@getimagefirstsection,
@getfieldoffset,
@apiunhook,
@apihook,
@calcjump,
@iswin9x,
@opcodelength,
@ex_trunc,
@__getmodulefilenamew_pointer,
@getmodulefilenamew_new,
@__getmodulehandlea_pointer,
@getmodulehandlea_new,
@__ep_pointer,
@ep,
@kernel_base,
@hash,
@asmmove,
@movememory,
@find_function,
@__pointer,
@__end);
fn: array[0..70-36] of string = (
'__begin',
'algo',
'btmemorygetprocaddress',
'btmemoryloadlibary',
'finalizesections',
'getsectionprotection',
'buildimporttable',
'strcomp',
'allocmem',
'performbaserelocation',
'copysections',
'getimagesnapbyordinal',
'getimageordinal',
'getheaderdictionary',
'getimagefirstsection',
'getfieldoffset',
'apiunhook',
'apihook',
'calcjump',
'iswin9x',
'opcodelength',
'ex_trunc',
'__getmodulefilenamew_pointer',
'getmodulefilenamew_new',
'__getmodulehandlea_pointer',
'getmodulehandlea_new',
'__ep_pointer',
'ep',
'kernel_base',
'hash',
'asmmove',
'movememory',
'find_function',
'__pointer',
'__end');
sl: TStringList;
begin
sl := TStringList.Create;
for i := Low(f) to High(f) do
sl.AddObject(IntToHex(PtrUInt( f[i] ), SizeOf(PtrInt)), TObject(i));
sl.Sort;
for i := 0 to sl.Count-1 do
WriteLn(fn[Integer(sl.Objects[i])]:30,': ',sl[i]);
code_size := integer (@__end) - integer (@__begin) - sizeof (dword);
WriteLn('');
WriteLn('code_size: ', code_size);
ReadLn;
end.
the Result in Delphi
__begin: 421290
algo: 4212A0
ex_trunc: 421340
opcodelength: 421380
iswin9x: 421550
calcjump: 421560
apihook: 4215B0
apiunhook: 4217D0
getfieldoffset: 421990
getimagefirstsection: 4219B0
getheaderdictionary: 4219F0
getimageordinal: 421A10
getimagesnapbyordinal: 421A30
copysections: 421A50
performbaserelocation: 421B50
allocmem: 421C70
strcomp: 421CA0
buildimporttable: 421CD0
getsectionprotection: 421EE0
finalizesections: 421FF0
btmemoryloadlibary: 422130
btmemorygetprocaddress: 4223D0
hash: 422510
kernel_base: 422540
asmmove: 422570
movememory: 4225C0
find_function: 4225E0
__getmodulefilenamew_pointer: 422640
getmodulefilenamew_new: 422650
__getmodulehandlea_pointer: 4226B0
getmodulehandlea_new: 4226C0
__ep_pointer: 422700
ep: 422710
__pointer: 422750
__end: 422760
code_size: 5324
in Delphi it works fine cuz it compile the full unit into code and put it in order and with full asm code for all functions
but in FreePascal
__begin: 424930
ex_trunc: 424940
opcodelength: 424990
iswin9x: 424B60
strcomp: 424B70
hash: 424BA0
kernel_base: 424BD0
asmmove: 424C00
find_function: 424C50
__getmodulefilenamew_pointer: 424CB0
__getmodulehandlea_pointer: 424CC0
__ep_pointer: 424CD0
__pointer: 424CE0
__end: 424CF0
algo: 424D00
calcjump: 424DA0
apihook: 424E00
apiunhook: 425030
getfieldoffset: 425210
getimagefirstsection: 425230
getheaderdictionary: 425270
getimageordinal: 425290
getimagesnapbyordinal: 4252B0
copysections: 4252E0
performbaserelocation: 4253F0
allocmem: 425500
buildimporttable: 425530
getsectionprotection: 425750
finalizesections: 425860
btmemoryloadlibary: 4259B0
btmemorygetprocaddress: 425C50
movememory: 425DA0
getmodulefilenamew_new: 425DC0
getmodulehandlea_new: 425E20
ep: 425E60
code_size: 956
then i try to use ( no optimization )
C:\codetyphon\fpc\fpc32\bin\i386-win32\fpc.exe
-O-
-Pi386
-B
-al
-s
-FiC:\Users\Coldzer0\Desktop\Coldroot\Source-Code\Coldroot_Crypt\lib\i386-win32
-FuC:\Users\Coldzer0\Desktop\Coldroot\Source-Code\Coldroot_Crypt\
-FUC:\Users\Coldzer0\Desktop\Coldroot\Source-Code\Coldroot_Crypt\lib\i386-win32\
Project1.ppr
but the code still in the wrong order
i'm using "-al -s" to keep the assembly ".s" file to check the order of functions before compile
i just need to know if there's a compiler directive or an option so i can have my code compiled in order
and without any optimization
i need it to be in the same order
i search this problem and i found nothing but in gcc there was something like "fno-toplevel-reorder"
i hope i can find help
thanks in advanced