Recent

Author Topic: Pascal JIT  (Read 4904 times)

tihory

  • New member
  • *
  • Posts: 7
Pascal JIT
« on: May 19, 2021, 12:47:24 pm »
Hi,

I'm interested to implement a Pascal JIT linker, so I implemented a COFF 64bit linker and link object files generated by FPC trunk then link them with DLL dependencies like "kernel32.dll" and "user32.dll".

Project source code is available at https://github.com/tihorygit/TheLinker.

Now I have a problem with linking FPC compiler functions like "fpc_initializeunits". After files linked together, some of these functions point to an invalid location in object memory and cause an access violation exception.

I read Pascal TInternalLinker code but didn't find any solution, is anyone experience with linkers to help me or have a solution?
« Last Edit: May 23, 2021, 12:53:08 pm by tihory »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Pascal JIT
« Reply #1 on: May 19, 2021, 11:31:31 pm »
The function is defined in rtl/inc/system.inc and is included in every system unit implementation:
Code: Bash  [Select][+][-]
  1. $ grep -ir fpc_initializeunits *
  2. inc/compproc.inc:procedure fpc_InitializeUnits; compilerproc;
  3. inc/system.inc:procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  4. inc/system.inc:procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  5. java/jsystem.inc:procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  6. java/jsystem.inc:procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  7. java/jcompproc.inc:procedure fpc_InitializeUnits; compilerproc;
  8. $ grep -ir system.inc *
  9. aix/system.pp:{$I system.inc}
  10. amiga/system.pp:{$I system.inc}
  11. aros/system.pp:{$I system.inc}
  12. atari/system.pp:    {$i system.inc}
  13. beos/system.pp:{$I system.inc}
  14. bsd/system.pp:{$I system.inc}
  15. embedded/system.pp:{$I system.inc}
  16. emx/system.pas:{$I system.inc}
  17. fpmake.pp:          AddInclude('system.inc',AllUnixOSes);
  18. freertos/system.pp:{$I system.inc}
  19. gba/system.pp:{$i system.inc}
  20. go32v2/system.pp:{$I system.inc}
  21. haiku/system.pp:{$I system.inc}
  22. inc/readme:system.inc      OS and Processor independent implementation part of system unit.
  23. java/system.pp:{$i jsystem.inc}
  24. linux/system.pp:{$I system.inc}
  25. macos/system.pp:{$I system.inc}
  26. morphos/system.pp:{$I system.inc}
  27. msdos/system.pp:{$I system.inc}
  28. msxdos/system.pp:{$I system.inc}
  29. nativent/system.pp:{$I system.inc}
  30. nds/system.pp:{$i system.inc}
  31. netware/system.pp:{$I system.inc}
  32. netwlibc/system.pp:{$I system.inc}
  33. os2/system.pas:{$I system.inc}
  34. palmos/system.pp:{$i system.inc}
  35. qnx/system.pp:{$I system.inc}
  36. sinclairql/system.pp:  {$i system.inc}
  37. solaris/system.pp:{$I system.inc}
  38. symbian/system.pp:{$I system.inc}
  39. wasi/system.pp:{$I system.inc}
  40. watcom/system.pp:{$include system.inc}
  41. wii/system.pp:{$i system.inc}
  42. win16/system.pp:{$I system.inc}
  43. win32/system.pp:{$I system.inc}
  44. win64/system.pp:{$I system.inc}
  45. wince/system.pp:{$I system.inc}
  46. zxspectrum/system.pp:{$I system.inc}
  47.  
do notice that it's exported ALL CAPS as FPC_INITIALIZEUNITS, bet your linker is case sensitive, right?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Pascal JIT
« Reply #2 on: May 20, 2021, 09:27:21 am »
do notice that it's exported ALL CAPS as FPC_INITIALIZEUNITS, bet your linker is case sensitive, right?

Linkers are supposed to be. The compilerproc directives leads to the compiler generating a lowercase mangled name (here fpc_initializeunits) which can't be accessed using an external directive, which is why an all uppercase alias (FPC_INITIALIZEUNITS) is added.

tihory

  • New member
  • *
  • Posts: 7
Re: Pascal JIT
« Reply #3 on: May 21, 2021, 02:54:23 am »
Quote
do notice that it's exported ALL CAPS as FPC_INITIALIZEUNITS, bet your linker is case sensitive, right?

Due to the "dumpbin" output, the "FPC_INITIALIZEUNITS" and "fpc_initializeunits" point to the same address in object memory.

Maybe this problem occurs because FPC uses a custom linking script and the "FPC_INITIALIZEUNITS" searches for specific addresses in memory depends on the linking script.
« Last Edit: May 21, 2021, 03:17:52 am by tihory »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Pascal JIT
« Reply #4 on: May 21, 2021, 04:34:32 pm »
Quote
do notice that it's exported ALL CAPS as FPC_INITIALIZEUNITS, bet your linker is case sensitive, right?

Due to the "dumpbin" output, the "FPC_INITIALIZEUNITS" and "fpc_initializeunits" point to the same address in object memory.

Maybe this problem occurs because FPC uses a custom linking script and the "FPC_INITIALIZEUNITS" searches for specific addresses in memory depends on the linking script.

As written above: one is merely an alias of the other. They're indeed expected to be the same.

tihory

  • New member
  • *
  • Posts: 7
Re: Pascal JIT
« Reply #5 on: June 07, 2021, 11:29:09 am »
Hi,
I implemented some other features but postponed development for now.

Features:
  • Link multiple object files together
  • Link against DLL files
  • Support Pascal Mangling for external DLL functions
  • Support function injection

 

TinyPortal © 2005-2018