Recent

Author Topic: FPC 3.2.2 + NASM Windows 64-Bit Pro + how to: create DLL and EXE of .o files ?  (Read 2436 times)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Now, I have .s and .o Files in the File System.
.s was created by FPC
.o was created by ppas.bat with nasm.exe

ppas.bat runs without errors.
How can I create a DLL or EXE with the .o files ?

because, I have some .a Files, too, and I don't know what to do with.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Now, I get the following Output by gcc.exe:

Code: Bash  [Select][+][-]
  1. C:\fpcupdeluxe\fpc\bin\x86_64-win64\gcc -nostdlib -o test.exe  xmm.o system.o windows.o  ^
  2. global.o locales.o  exceptions.o QApplicationPascal.o rtllibimport.o sysutils.o strutils.o sysinitpas.o ^
  3. rtlunit.o rtllib.o QObjectPascalexport.o dialogs.o ^
  4. -L.\ -limpwindows -lrtllib_dll -limpsystem -limpDialogs -limpQApplicationPascal -limplocales -limprtllibimport ^
  5. -limprtlunit -limpstrutils -limpsysutils -limpexceptions -limpsystem -limpglobal
  6. ld: error: 0-bit reloc in dll
  7. ld: error: 0-bit reloc in dll
  8. ld: error: 0-bit reloc in dll
  9. ld: error: 0-bit reloc in dll
  10. ld: error: 0-bit reloc in dll
  11. ld: error: 0-bit reloc in dll
  12. ld: error: 0-bit reloc in dll
  13. ld: xmm.o:xmm.s:(.text+0x3d1): undefined reference to `_$dll$ntdll$RtlCompareMemory'
  14. ld: xmm.o:xmm.s:(.text+0x3e6): undefined reference to `_$dll$ntdll$RtlCompareMemory'
  15. ld: xmm.o:xmm.s:(.text+0x4a5): undefined reference to `_$dll$kernel32$RtlFillMemory'
« Last Edit: May 10, 2025, 05:18:53 pm by paule32 »
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12262
  • FPC developer.
gcc in the FPC/Lazarus distribution is an empty stub for preprocessing resource files only (and hopefully one of these days that won't even be necessary anymore)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
okay.
After compile a pas files to .s files with -fPIC Option, I could create DLL and EXE.

But, I don't want to use gcc or ld to link .o files.
When I drop the command on the CLI:

Code: Bash  [Select][+][-]
  1. fpc -otest.exe test.o

I get the message:

Code: Bash  [Select][+][-]
  1. test.o(1,2) Fatal: Syntax error, "BEGIN" expected but "identifier D" found
  2. Fatal: Compilation aborted

So, how can I create a EXE from .o File per FPC ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6004
  • Compiler Developer
So, how can I create a EXE from .o File per FPC ?

For FPC at least the main project needs to be Pascal source. Other object files can then be added using {$L objfile.o}. Anything else is not supported and you must use manual linking using the link script instead.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
I use this command:

Code: Bash  [Select][+][-]
  1. fpc -dDLLEXPORT -dLANGDEU -Cn -a -al -Anasmwin64 -sh test_unit.pas

this will create a .S File.
I did then

Code: Bash  [Select][+][-]
  1. dir *.res
  2. dir *.ld

but with no results.

Then I tried the following:

Code: Bash  [Select][+][-]
  1. fpc -dDLLEXPORT -dLANGDEU -Cn -a -al -Anasmwin64 -sh test_unit.pas
  2. nasm.exe -f win64 -o test_unit.o -w-orphan-labels  test_unit.s
  3. fpc -dDLLIMPORT -dLANGDEU -n -B -O3 -Os test.pas

with this code (test.pas):

Code: Pascal  [Select][+][-]
  1. program test;
  2. {$L test_unit.o}
  3.  
  4. begin
  5.   PASCALMAIN;
  6. end.

and this (test_unit.pas):

Code: Pascal  [Select][+][-]
  1. unit test_unit;
  2. interface
  3. procedure PASCALMAIN;
  4. implementation
  5. procedure PASCALMAIN;
  6. begin
  7. { some code }
  8. end;
  9. end.

The Message comes then:

Code: Bash  [Select][+][-]
  1. fpc.exe -dDLLIMPORT -dLANGDEU -n -B -O3 -Os test.pas
  2. test.pas(10,3) Error: Identifier not found "PASCALMAIN"
  3. test.pas(12) Fatal: There were 1 errors compiling module, stopping
  4. Fatal: Compilation aborted
  5. Error: C:\fpcupdeluxe\fpc\bin\x86_64-win64\ppcx64.exe returned an error exitcode
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6004
  • Compiler Developer
You need to make the PASCALMAIN identifier known to the parser:

Code: Pascal  [Select][+][-]
  1. procedure PASCALMAIN; external name 'PASCALMAIN';

(though you'll likely need to use the proper mangled name for your PASCALMAIN unless you provide a public alias)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Thank you. It works.

But I get this errors:

Code: Bash  [Select][+][-]
  1. test.pas(18,1) Error: Undefined symbol: STRUTILS_::=::\_STRINGREPLACE$ANSISTRING$ANSISTRING$ANSISTRING$TREPLACEFLAGS::=::\ANSISTRING
  2. test.pas(18,1) Error: Undefined symbol: DIALOGS_::=::\_SHOWMESSAGE$PCHAR
  3. test.pas(18,1) Fatal: There were 2 errors compiling module, stopping
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6004
  • Compiler Developer
But I get this errors:

Then some of your code uses these functions. You need to make sure that the corresponding object files are linked in as well.

That's why you shouldn't try to do such nonsense, because you're basically reinventing what the compiler already does for you.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
okay. I get it.
Thank you for your help.

I add this Code to the test_unit.pas:

Code: Pascal  [Select][+][-]
  1. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; external RTLDLL;
  2. procedure ShowMessage(msg: PChar); stdcall; external RTLDLL;

but I used the Code again in my StrUtils.pas:

Code: Pascal  [Select][+][-]
  1. unit test_unit;
  2. interface
  3. uses
  4.   Windows, Dialogs, StrUtils;
  5.  
  6. procedure PASCALMAIN
  7.  
  8. implementation
  9.  
  10. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; external RTLDLL;
  11. procedure ShowMessage(msg: PChar); stdcall; external RTLDLL;
  12.  
  13. procedure PASCALMAIN;
  14. begin
  15. { some code }
  16. end;
  17.  
  18. end.

But I have already Code for in StrUtils.pas:

Code: Pascal  [Select][+][-]
  1. unit StrUtils;
  2. interface
  3.  
  4. {$ifdef DLLEXPORT}
  5. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; stdcall; export;
  6. {$endif DLLEXPORT}
  7.  
  8. {$ifdef DLLIMPORT}
  9. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; external RTLDLL;
  10. {$endif DLLIMPORT}
  11.  
  12. implementation
  13. ...
  14. end.

And I compile with:

Code: Bash  [Select][+][-]
  1. fpc -dDLLEXPORT -dLANGDEU -Cn -a -al -Anasmwin64 -sh test_unit.pas
  2. nasm.exe -f win64 -o test_unit.o -w-orphan-labels  test_unit.s
  3. fpc -dDLLIMPORT -dLANGDEU -n -B -O3 -Os test.pas

something magican goes on there - I don't know why ?  :'(
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
when I use the Code below:

Code: Pascal  [Select][+][-]
  1. program test;
  2. {$L test_unit.o}
  3.  
  4. uses
  5.   Windows, Dialogs, SysUtils, StrUtils, Exceptions,
  6.   Locales, global,
  7.   RtlLibImport, QApplicationPascal;
  8.  
  9. procedure PASCALMAINE; external name 'PASCALMAINE';
  10.  
  11. begin
  12.   PASCALMAINE;
  13. end.

I get:

Code: Bash  [Select][+][-]
  1. fpc -dDLLIMPORT -dLANGDEU -n -B -O3 -Os test.pas
  2. test.pas(18,1) Error: Undefined symbol: PASCALMAINE
  3. test.pas(18,1) Fatal: There were 1 errors compiling module, stopping
  4. Fatal: Compilation aborted
  5. Error: C:\fpcupdeluxe\fpc\bin\x86_64-win64\ppcx64.exe returned an error exitcode

But when I use:

Code: Pascal  [Select][+][-]
  1. program test;
  2. {$L test_unit.o}
  3.  
  4. uses
  5.   Windows, Dialogs, SysUtils, StrUtils, Exceptions,
  6.   Locales, global,
  7.   RtlLibImport, QApplicationPascal;
  8.  
  9. procedure PASCALMAIN; external name 'PASCALMAIN';
  10.  
  11. begin
  12.   PASCALMAIN;
  13. end.

The Compilation works.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

munair

  • Hero Member
  • *****
  • Posts: 883
  • compiler developer @SharpBASIC
    • SharpBASIC
When using mixed languages like FPC and NASM to compile into one program, you really should know what you're doing. Obviously, when you get errors like UNDEFINED SYMBOL and you don't know why, then you don't know what you're doing. And what's wrong with LD? I use NASM+LD all the time and it works great linking multiple object files.
It's only logical.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
the Problem is not understanding asm + nasm + ld.
the Problem is, what FPC does internally with the magic things like compilerproc or mangked names.

I wrote:
- when using PASCALMAIN, all is okay
- when using PASCALMAINE, the Problems pop out

so, my conclution is, that magic things goes on there.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
[SOLVED]
« Reply #13 on: May 12, 2025, 12:27:35 pm »
okay. I solved the Problem.
The Problem was on me, that I don't assemble the .pas and .s file.

All okay.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
@munair

how do you doing NASM + LD ?
what is the command line ?
what do you link (.o and .a files) ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018