* * *

Author Topic: Use of +offset(%ebp) for parameters invalid here  (Read 1432 times)

PeterX

  • Full Member
  • ***
  • Posts: 245
Use of +offset(%ebp) for parameters invalid here
« on: June 07, 2017, 04:36:52 pm »
Hi,

I am still looking for a solution to this Lazarus Warning:

Code: Pascal  [Select]
  1. Use of +offset(%ebp) for parameters invalid here

Source of this code seems to be Delphi 5, classes.pas line 1846 :

Code: Pascal  [Select]
  1. class procedure TList.Error(const Msg: string; Data: Integer);
  2.  
  3.   function ReturnAddr: Pointer;
  4.   asm
  5.           MOV     EAX,[EBP+4]
  6.   end;
  7.  
  8. begin
  9.   raise EListError.CreateFmt(Msg, [Data]) at ReturnAddr;
  10. end;


Actually the same code can be found in FPC unit RegExpr.pas in line 4148.
Unit is in  C:\lazarus\fpc\3.0.2\source\packages\regexpr\src

Code: Pascal  [Select]
  1. {$IFDEF reRealExceptionAddr}
  2. {$OPTIMIZATION ON}
  3. // ReturnAddr works correctly only if compiler optimization is ON
  4. // I placed this method at very end of unit because there are no
  5. // way to restore compiler optimization flag ...
  6. {$ENDIF}
  7. procedure TRegExpr.Error (AErrorID : integer);
  8. {$IFDEF reRealExceptionAddr}
  9.  function ReturnAddr : pointer; //###0.938
  10.   asm
  11.    mov  eax,[ebp+4]
  12.   end;
  13. {$ENDIF}
  14. ...

Does anybody know enough about ASM
to be able to solve this for the FPC / Lazarus compiler ?
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Full Member
  • ***
  • Posts: 245
« Last Edit: June 07, 2017, 05:04:58 pm by PeterX »
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

taazz

  • Hero Member
  • *****
  • Posts: 4481
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #2 on: June 07, 2017, 04:56:57 pm »
replace returnAddr with get_caller_addr(get_frame);
eg.
Code: Pascal  [Select]
  1. type
  2.   TEvsCustomControl = class
  3.     class procedure Error(Const aMsg:String; const Params:Array of const);
  4.   end;
  5. implementation
  6. procedure TEvsCustomControl.Error(const aMsg :String; aParams :array of const);
  7. begin
  8.   raise ETBException.CreateFmt(aMsg, aParams) at get_caller_addr(get_frame);
  9. end;
  10.  
  11.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

PeterX

  • Full Member
  • ***
  • Posts: 245
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #3 on: June 07, 2017, 10:39:29 pm »
replace returnAddr with get_caller_addr(get_frame);
eg.
Code: Pascal  [Select]
  1. type
  2.   TEvsCustomControl = class
  3.     class procedure Error(Const aMsg:String; const Params:Array of const);
  4.   end;
  5. implementation
  6. procedure TEvsCustomControl.Error(const aMsg :String; aParams :array of const);
  7. begin
  8.   raise ETBException.CreateFmt(aMsg, aParams) at get_caller_addr(get_frame);
  9. end;
  10.  
  11.  
What's get_frame ?
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5875
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #4 on: June 07, 2017, 10:41:16 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 4752
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #5 on: June 07, 2017, 10:43:53 pm »
A documented function
I can't help, but smile.... ;)
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5875
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #6 on: June 07, 2017, 10:49:22 pm »
The construct as Taazz describes is exactly meant to avoid the need for assembler as PeterX describes.

With the added advantage that the result is more portable, and better understandable/optimizable to the compiler

PeterX

  • Full Member
  • ***
  • Posts: 245
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #7 on: June 08, 2017, 11:39:40 am »
What's get_frame ?

A documented function
The documentation is very .. poor, to me.

I wanted to learn how this assembler code has to be written.

So I searched for the function in other INC files and I found:


x86_64.inc :
Code: Pascal  [Select]
  1. {$IFNDEF INTERNAL_BACKTRACE}
  2. {$define FPC_SYSTEM_HAS_GET_FRAME}
  3. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  4. asm
  5.         movq    %rbp,%rax
  6. end;
  7. {$ENDIF not INTERNAL_BACKTRACE}


i386.inc :
Code: Pascal  [Select]
  1. {$IFNDEF INTERNAL_BACKTRACE}
  2. {$define FPC_SYSTEM_HAS_GET_FRAME}
  3. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  4. asm
  5.         movl    %ebp,%eax
  6. end;
  7. {$ENDIF not INTERNAL_BACKTRACE}
  8.  


i8086.inc :
Code: Pascal  [Select]
  1. {$IFNDEF INTERNAL_BACKTRACE}
  2. {$define FPC_SYSTEM_HAS_GET_FRAME}
  3. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  4. asm
  5.   mov ax, bp
  6. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  7.   mov dx, ss
  8. {$endif}
  9. end;
  10. {$ENDIF not INTERNAL_BACKTRACE}
  11.  


I still do not understand why "Use of +offset(%ebp) for parameters (is) invalid here"
(for Windows / Laz i386 ..)
Code: Pascal  [Select]
  1.   function ReturnAddr: Pointer;
  2.   asm
  3.           MOV     EAX,[EBP+4]
  4.   end;
  5.  
Does anybody know enough about ASM ?
« Last Edit: June 08, 2017, 11:41:59 am by PeterX »
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5875
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #8 on: June 08, 2017, 11:49:55 am »
I think the assembler thinks you are trying to load a parameter (typically at positive offset to BP), while there is no parameter to the function.

I don't think this can be avoided, the code is simply not a proper function but something internal.

Thaddy

  • Hero Member
  • *****
  • Posts: 4752
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #9 on: June 08, 2017, 11:53:41 am »
Well a lot of us know. There's only the small matter that the syntax you are using in the Delphi code is Intel syntax. And your question suggest you are mixing this up with the "+offset(%ebp)" is not Intel syntax, but gnu syntax, which has a different instruction order(reverse). Furthermore there is the issue of how to refer: ESP or EBP? Makes a big difference...
While experimenting to solve this yourself, make sure you have {$ASMMODE INTEL} on.
Also, "offset" is only recently added. It does not work in older versions of FPC.

[edit]
Marco's post crossed, basically I gave the same pointer. (Apart from mixing up syntax).

Also note that the internal assembler in FPC is more strict than BASM:
Code: Pascal  [Select]
  1. function ReturnAddr: Pointer;
  2.   asm
  3.           MOV     EAX,[EBP+4]
  4.   end;
  5.  
Delphi assumes dword size but [] indicates a pointer. You have to specify the size: dword ptr. It will still be delphi compatble. If you rely on an older version, use  lea instead of offset.
« Last Edit: June 08, 2017, 12:04:26 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

PeterX

  • Full Member
  • ***
  • Posts: 245
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #10 on: June 08, 2017, 12:09:57 pm »
While experimenting to solve this yourself, make sure you have {$ASMMODE INTEL} on.

Well, I already added ASMMODE some years ago here,
otherwise it wouldn't compile at all .. :
Code: Pascal  [Select]
  1.   function ReturnAddr: Pointer; assembler;
  2.   // From classes.pas
  3. {$ifdef FPC} {$ASMMODE intel} {$endif}
  4.   asm
  5.     MOV         EAX,[EBP+4] // sysutils.pas says [EBP-4], but this works !
  6.   end;
  7.  

But I'll try the get_frame function in IFDEF first now.
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

taazz

  • Hero Member
  • *****
  • Posts: 4481
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #11 on: June 08, 2017, 12:11:14 pm »
One off topic question in 64bit apps does  +4 becomes +8?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 4752
Re: Use of +offset(%ebp) for parameters invalid here
« Reply #12 on: June 08, 2017, 01:06:52 pm »
One off topic question in 64bit apps does  +4 becomes +8?
Actually + SizeOf(Pointer) is perfectly legal in FPC's implementation of inline ASM...  :D
« Last Edit: June 08, 2017, 01:11:28 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus