Welcome, Guest
Username: Password: Remember me
Discussions for CodeTyphon Studio Installation and Setup.
  • Page:
  • 1

TOPIC:

Strange Behavior With Compiler Optimization 6 years 5 months ago #11077

  • Coldzer0
  • Coldzer0's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 2
  • Thank you received: 0
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

Please Log in or Create an account to join the conversation.

Strange Behavior With Compiler Optimization 6 years 5 months ago #11078

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4508
  • Thank you received: 1100
Thanks Sir
I think it's programming logical Error...

<<i need it to be in the same order>>
if you want the same you must use this Order:
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 = (    // <======================== New Order
      @__begin,
      @algo,
      @ex_trunc,
      @opcodelength,
      @iswin9x,
      @calcjump,
      @apihook,
      @apiunhook,
      @getfieldoffset,
      @getimagefirstsection,
      @getheaderdictionary,
      @getimageordinal,
      @getimagesnapbyordinal,
      @copysections,
      @performbaserelocation,
      @allocmem,
      @strcomp,
      @buildimporttable,
      @getsectionprotection,
      @finalizesections,
      @btmemoryloadlibary,
      @btmemorygetprocaddress,
      @hash,
      @kernel_base,
      @asmmove,
      @movememory,
      @find_function,
      @__getmodulefilenamew_pointer,
      @getmodulefilenamew_new,
      @__getmodulehandlea_pointer,
      @getmodulehandlea_new,
      @__ep_pointer,
      @ep,
      @__pointer,
      @__end);

  fn: array[0..70-36] of string = (     // <========================  New Order
      '__begin',
      'algo',
      'ex_trunc',
      'opcodelength',
      'iswin9x',
      'calcjump',
      'apihook',
      'apiunhook',
      'getfieldoffset',
      'getimagefirstsection',
      'getheaderdictionary',
      'getimageordinal',
      'getimagesnapbyordinal',
      'copysections',
      'performbaserelocation',
      'allocmem',
      'strcomp',
      'buildimporttable',
      'getsectionprotection',
      'finalizesections',
      'btmemoryloadlibary',
      'btmemorygetprocaddress',
      'hash',
      'kernel_base',
      'asmmove',
      'movememory',
      'find_function',
      '__getmodulefilenamew_pointer',
      'getmodulefilenamew_new',
      '__getmodulehandlea_pointer',
      'getmodulehandlea_new',
      '__ep_pointer',
      'ep',
      '__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;                      // <========================  Delete

  for i := Low(f) to High(f) do   // <========================  New Order
 // 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.

Output after your App Reconstruction
__begin: 4247D0
                          algo: 424BA0
                      ex_trunc: 4247E0
                  opcodelength: 424830
                       iswin9x: 424A00
                      calcjump: 424C40
                       apihook: 424CA0
                     apiunhook: 424ED0
                getfieldoffset: 4250B0
          getimagefirstsection: 4250D0
           getheaderdictionary: 425110
               getimageordinal: 425130
         getimagesnapbyordinal: 425150
                  copysections: 425180
         performbaserelocation: 425290
                      allocmem: 4253A0
                       strcomp: 424A10
              buildimporttable: 4253D0
          getsectionprotection: 4255F0
              finalizesections: 425700
            btmemoryloadlibary: 425850
        btmemorygetprocaddress: 425AF0
                          hash: 424A40
                   kernel_base: 424A70
                       asmmove: 424AA0
                    movememory: 425C40
                 find_function: 424AF0
  __getmodulefilenamew_pointer: 424B50
        getmodulefilenamew_new: 425C60
    __getmodulehandlea_pointer: 424B60
          getmodulehandlea_new: 425CC0
                  __ep_pointer: 424B70
                            ep: 425D00
                     __pointer: 424B80
                         __end: 424B90

code_size: 956

Have fun...
It's my first coffee... :)
PilotLogic Architect and Core Programmer
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Strange Behavior With Compiler Optimization 6 years 5 months ago #11079

  • Coldzer0
  • Coldzer0's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 2
  • Thank you received: 0
hello and that was not my question

i don't need to sort the functions


i need the compiled functions to be in order in file and memory

for example :

__begin @ addr = 0x00000001
ex_trunc @ addr = 0x00000002
.....
__end @ addr = 0x00000037


check this forum.lazarus.freepascal.org/index.php/topic,38829.0.html

i was discussing the same problem

and i found that the FPC 3.1.1 have a problem with compiled Functions order

and FPC 3.0.2 works just fine



check the link you may understand my question more



and one more thing why not using latest stable version of FPC (3.0.2)
i really like typhon so much i'm using it for all my projects


so i think stable versions is better than the beta ones

and thanks in advance :D

Please Log in or Create an account to join the conversation.

Strange Behavior With Compiler Optimization 6 years 5 months ago #11080

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4508
  • Thank you received: 1100
No problem Sir
you can use FPC 3.0.2 ... :laugh:
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

Strange Behavior With Compiler Optimization 6 years 5 months ago #11082

  • Mark Chambers
  • Mark Chambers's Avatar
  • Offline
  • New Member
  • New Member
  • Posts: 1
  • Thank you received: 0
Hi, I've read this thread and the Lazarus one also and I'm intrigued - what is it that you are trying to achieve?

Please Log in or Create an account to join the conversation.

  • Page:
  • 1