Recent

Author Topic: [SOLVED] Question about "cvar"  (Read 6872 times)

440bx

  • Hero Member
  • *****
  • Posts: 4024
[SOLVED] Question about "cvar"
« on: July 03, 2021, 03:55:00 am »
Hello,

Ntdll exports a number of variables which can be imported in FPC using "cvar".

There is a variable named LdrSystemDllInitBlock which is exported in both, the 32bit and 64bit versions of ntdll.  Accessing the variable from a 64bit program works as expected but, from a 32bit program, Windows complains that the variable "_LdrSystemDllInitBlock" (note the extra underscore) is not found in ntdll.

Here is a little test program that operates as expected in 64bit but, causes the "_LdrSystemDllInitBlock entry point not found" in ntdll. 
Code: Pascal  [Select][+][-]
  1. {$APPTYPE       CONSOLE}
  2.  
  3. {$TYPEDADDRESS  ON}
  4.  
  5. {$LONGSTRINGS   OFF}
  6.  
  7. { --------------------------------------------------------------------------- }
  8.  
  9.  
  10. {$ifdef VER90}
  11.   {$FATAL this program can only be compiled with FPC}
  12. {$endif}
  13.  
  14. { --------------------------------------------------------------------------- }
  15.  
  16.  
  17. program _cvars;
  18.  
  19. uses
  20.   sysutils
  21.   ;
  22.  
  23.  
  24. var
  25.   LdrSystemDllInitBlock : DWORD; cvar; external 'ntdll';
  26.  
  27.  
  28. begin
  29.   writeln;
  30.   writeln;
  31.  
  32.   writeln('  address of LdrSystemDllInitBlock is : ',
  33.              '$' + IntToHex(ptruint(@LdrSystemDllInitBlock), 0));
  34.   writeln;
  35.  
  36.   writeln('  value   of LdrSystemDllInitBlock is : ',
  37.              LdrSystemDllInitBlock);
  38.  
  39.   writeln;
  40.   writeln;
  41.   writeln('press ENTER/RETURN to end this program');
  42.   readln;
  43. end.          

My question is: why is it looking for the variable name preceded by an "_" when the program is compiled for 32bit and what are the steps to take to eliminate the problem ?

Thank you for your help.

Attached is a screenshot of the error message and verification that the export is present in the 32bit version of ntdll (without the "_")


« Last Edit: July 03, 2021, 08:36:12 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

tetrastes

  • Sr. Member
  • ****
  • Posts: 481
Re: Question about "cvar"
« Reply #1 on: July 03, 2021, 11:30:49 am »
I don't know why "cvar" adds underscore for 32 bit and does not for 64, but after removing "cvar" from declaration, your code works both for 32 and 64 bit.
« Last Edit: July 03, 2021, 01:40:40 pm by tetrastes »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Question about "cvar"
« Reply #2 on: July 03, 2021, 01:00:31 pm »
Probably for cygwin/mingw use that prefixed local vars with _ on COFF targets.

440bx

  • Hero Member
  • *****
  • Posts: 4024
Re: Question about "cvar"
« Reply #3 on: July 03, 2021, 08:35:55 pm »
after removing "cvar" from declaration, your code works both for 32 and 64 bit.
For some reason, I was under the impression that "cvar" was required to import external variable.  I'm rather pleased to see that it is not. 

tetrastest, thank you very much for mentioning that.  Very useful detail to know.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: [SOLVED] Question about "cvar"
« Reply #4 on: July 04, 2021, 12:54:21 pm »
cvar applies the C mangling to the variable. And that mangling on Windows 32-bit does have a leading _ (on Windows 64-bit Microsoft dropped that). However the Windows API does not use C mangling, thus you must not use cvar for those variables.

440bx

  • Hero Member
  • *****
  • Posts: 4024
Re: [SOLVED] Question about "cvar"
« Reply #5 on: July 04, 2021, 04:00:01 pm »
cvar applies the C mangling to the variable. And that mangling on Windows 32-bit does have a leading _ (on Windows 64-bit Microsoft dropped that). However the Windows API does not use C mangling, thus you must not use cvar for those variables.
Thank you PascalDragon.  I like to understand how something works.  That explanation was very helpful.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

tetrastes

  • Sr. Member
  • ****
  • Posts: 481
Re: [SOLVED] Question about "cvar"
« Reply #6 on: July 05, 2021, 12:57:46 pm »
Yes, the explanation was helpful, but why we read something different in documentation?
https://www.freepascal.org/docs-html/current/prog/progsu147.html
https://www.freepascal.org/docs-html/current/ref/refse22.html
« Last Edit: July 05, 2021, 01:02:39 pm by tetrastes »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: [SOLVED] Question about "cvar"
« Reply #7 on: July 05, 2021, 01:03:03 pm »
Yes, the explanation was helpful, but why we read something different in documentation?
https://www.freepascal.org/docs-html/current/prog/progsu147.html#x186-1890007.1.2
https://www.freepascal.org/docs-html/current/ref/refse22.html#x53-730004.2

Well, the documentation is mostly right (especially the case sensitive part). The only thing that's missing is the mention of the C-prefix (the '_'). Please file a bug report.

440bx

  • Hero Member
  • *****
  • Posts: 4024
Re: [SOLVED] Question about "cvar"
« Reply #8 on: July 05, 2021, 09:41:43 pm »
The only thing that's missing is the mention of the C-prefix (the '_'). Please file a bug report.
Done.  Ticket: 0039164
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: [SOLVED] Question about "cvar"
« Reply #9 on: July 06, 2021, 03:24:17 pm »
Thanks.

 

TinyPortal © 2005-2018