Recent

Author Topic: Unwanted Underscores Added To External Names  (Read 1445 times)

Kevan S. Hashemi

  • New Member
  • *
  • Posts: 23
Re: Unwanted Underscores Added To External Names
« Reply #15 on: May 14, 2020, 10:03:54 pm »
Quote
Please do try to use the syntax that I specified:

Nice one Marcov! That works. Setting your solution in my simple example:

Code: Pascal  [Select][+][-]
  1. function answer:integer; cdecl; external 'd.dll' name 'answer';

With this syntax, FPC ends up putting the prefix before the library name. Here is the assembler output:

Code: ASM  [Select][+][-]
  1. call    FPC_IOCHECK
  2. call    _$dll$d$answer
  3. movw    %ax,%cx

That behavior may be a bug. I hope not, because if it's a bug, it may be fixed later. I moved the library to another folder and used FPC's "-k" option to pass the new path to the linker. The compiler found the library just fine. My original declaration was:

Code: Pascal  [Select][+][-]
  1. {$linklib d}
  2. function answer:integer; cdecl; external;

An alternative is:

Code: Pascal  [Select][+][-]
  1. {$linklib d}
  2. function answer:integer; cdecl; external name 'answer';

These won't compile without an "_answer" to link to, but they produce identical assembler code:

Code: ASM  [Select][+][-]
  1. call    FPC_IOCHECK
  2. call    _answer
  3. movw    %ax,%cx

So, I have a syntax that will work on Windows. Thank you for your attention to my problem, and for your patience.

Kevan S. Hashemi

  • New Member
  • *
  • Posts: 23
Re: Unwanted Underscores Added To External Names
« Reply #16 on: May 14, 2020, 10:31:09 pm »
Quote
IMHO, fpc should give more liberty to developers, why add by force that "_"?.

I agree. That would make my code cleaner, and it would bring the FPC "export" sections into harmony with the "external" declarations, as on Linux.

And another thing that's great about the Linux implementation: dynamic libraries not needed at compile time. You don't have to specify the library name or the path name. Just the routine name. It's not as if linking to a dynamic library at compile time is any guarantee that the library will be present at run time. Well, I suppose the link against the dynamic library is a check that the routines exist in the library. Maybe that's worth it.

Anyway, I am now linking to the Tcl libraries on MacOSX, Windows, and Linux, so I'm happy.

Fred vS

  • Hero Member
  • *****
  • Posts: 1871
    • StrumPract is the musicians best friend
Re: Unwanted Underscores Added To External Names
« Reply #17 on: May 14, 2020, 10:45:18 pm »
Quote
Well, I suppose the link against the dynamic library is a check that the routines exist in the library. Maybe that's worth it.

IMHO, it is because if you use a C (gcc, clang, ...) compiler, with C++ code, the library is needed at linking, with the header of the library too.
But it is not the case with fpc, so I dont understand why fpc does all that C things.
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8780
  • FPC developer.
Re: Unwanted Underscores Added To External Names
« Reply #18 on: May 14, 2020, 11:34:30 pm »
Note:

Code: Pascal  [Select][+][-]
  1. {$linklib d}
  2. function answer:integer; cdecl; external;


The above is the linking syntax for symbols that have a unified linker namespace like most open source unixes.  All symbols are just thrown on one big heap and resolved.

The working syntax for windows:
Code: [Select]
    function answer:integer; cdecl; external 'd.dll' name 'answer';

is the syntax for platforms (like Windows, OS X and Solaris) that implement linker namespaces for dynamic libraries, where each symbol has an associated "module" (effectively: DLL/.so). That's why this syntax has the dll name in the declaration.  The _$dll$d$answer is probably a jump to such auto generated import library table, NOT the final jump to the DLL itself. Probably at the _$dll$d$answer  there is an easily (and lazy?) relocatable jmp to the real library address.

GCC/dlltool tries to hide this by adding this to the import libraries, but FPC generates the relevant COFF/PE structures on the fly.

It could be that FPC assumes that if you use the gcc syntax on windows, you really want to link with gcc.


« Last Edit: May 14, 2020, 11:37:08 pm by marcov »

Fred vS

  • Hero Member
  • *****
  • Posts: 1871
    • StrumPract is the musicians best friend
Re: Unwanted Underscores Added To External Names
« Reply #19 on: May 14, 2020, 11:59:41 pm »
Quote
Or, maybe, use LoadLibrary() instead of external...

I just tried with LoadLibrary, using your sample code, for which I thank you again. Even with LoadLibrary, the fact that we declare the function as 'cdecl' is sufficient to make FPC add an underscore to the external name when it tries to look up the routine in the dynamic library.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyMethod=function :integer; cdecl;

So the routine has to have an underscore prefix in the dynamic library, or else there is no way to call it.

Huh, sorry but this one I dont have the same result than you.

For me, on Windows, that code worked without problems:

Library:

Code: Pascal  [Select][+][-]
  1. library d;
  2.     function answer:integer; cdecl;
  3.     begin
  4.             writeln('Hello from d library, answer routine.');
  5.             answer:=42;
  6.     end;
  7.      
  8.     exports answer;
  9.     end.
  10.  

Program:

Code: Pascal  [Select][+][-]
  1.   program m;
  2.    uses dynlibs, SysUtils;
  3.      type
  4.      TMyMethod=function :integer; cdecl;
  5.    
  6.     var
  7.      MyLib: TLibHandle = dynlibs.NilHandle;
  8.      MyMethod: TMyMethod;
  9.     begin
  10.    
  11.     //  MyLib := LoadLibrary(ExtractFilePath(ParamStr(0)) + '\d.dll');
  12.    
  13.      [EDITED] // On Windows, it works too if library is in same dir than exe
  14.      MyLib := LoadLibrary('d.dll');
  15.    
  16.      if MyLib = 0 then Exit;
  17.      MyMethod := TMyMethod(GetProcedureAddress(MyLib, 'answer'));
  18.      writeln('Answer: ',MyMethod);
  19.      FreeLibrary(MyLib);
  20.      MyLib:= DynLibs.NilHandle;
  21.     end.

Result:

Code: Pascal  [Select][+][-]
  1. fred@fiens ~>m.exe
  2. Hello from d library, answer routine.
  3. Answer: 42


(And I am very happy that my lovely LoadLibrary() is still safe.)

Fre;D
« Last Edit: May 15, 2020, 12:32:51 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Kevan S. Hashemi

  • New Member
  • *
  • Posts: 23
Re: Unwanted Underscores Added To External Names
« Reply #20 on: May 15, 2020, 01:06:55 am »
Fred: Darn it, you're right. I just ran it myself. Mine failed because I tried to use a forward slash instead of a backslash on Windows. Should have send you my code so you could have pointed out my error, rather than having you write it yourself. Well, this is great: two solutions to my problem.

Marcov: Thank you for that explanation of the difference between MacOSX/Windows and the Linux handling of dynamic library routines. Now the differences between the compile behavior make more sense.

Both: I really appreciate your help. This has been a good day. I have now proved to myself that I can indeed port all 35k lines of my Pascal code from GNU Pascal to FPC, and have my software package run in 64 bit on all three platforms at the end. I'm celebrating.

Fred vS

  • Hero Member
  • *****
  • Posts: 1871
    • StrumPract is the musicians best friend
Re: Unwanted Underscores Added To External Names
« Reply #21 on: May 15, 2020, 01:34:21 am »
Quote
Both:

Thanks! (yes Marco and I always do good job together).

 ;D
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

jiaxing2

  • Full Member
  • ***
  • Posts: 164
Re: Unwanted Underscores Added To External Names
« Reply #22 on: May 15, 2020, 05:26:00 am »
Quote
Or, maybe, use LoadLibrary() instead of external...

I just tried with LoadLibrary, using your sample code, for which I thank you again. Even with LoadLibrary, the fact that we declare the function as 'cdecl' is sufficient to make FPC add an underscore to the external name when it tries to look up the routine in the dynamic library.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyMethod=function :integer; cdecl;

So the routine has to have an underscore prefix in the dynamic library, or else there is no way to call it.

I don't understand. An underscore is normal for cdecl. I think FPC does everything right. Check your Tcl/Tk dll.

IMHO, fpc should give more liberty to developers, why add by force that "_"?.

It is the same for so number with Unix libraries, the so number is arbitrary deleted, so you cannot link libraries with name 'libmylib.so.2" for example.

We are not all stupid and let us the choice to do things . (even if you find it stupid).

I just tell by my experience. I didn't said anyone stupid or even think so. I'm just confused.

jiaxing2

  • Full Member
  • ***
  • Posts: 164
Re: Unwanted Underscores Added To External Names
« Reply #23 on: May 15, 2020, 05:30:18 am »
The default way works fine, so I think your main problem is in the way you manually hacked together an import lib or so?

I think so, too.

Yeah. After all. This is the case. He used the wrong syntax from the beginning.

p/s: Don't want to insult anyone but... it's just that.

 

TinyPortal © 2005-2018