Failed to access library variable lazarus / 21-General ---------------------------------------------------------------------- mikerabat · 2024-08-01 07:49 Hi! The following project throws a linker error - (checked on a raspi 4, CodeTyphon IDE. Additional compiler options -fPIC -Xc I stumbled over this when trying to create an apache module on the RPi4... These modules need to export a record but I cannot fill ....  Anything that I'm missing here? How can I fill the record and exort the data? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library exportTest; {$mode objfpc}{$H+} uses  Classes  { you can add units after this }; type  TTestRec = packed record    idx1 : integer;    idx2 : integer;  end; var idxData : TTestRec; export name 'idxData'; procedure InitData(i1, i2 : integer);begin     idxData.idx1 := i1;     idxData.idx2 := i2;end; exports idxData, InitData; begin end.            the think is ... why? Why is it not allowed to change the content of the exported variable at all? ---------------------------------------------------------------------- cdbc · 2024-08-01 09:11 Hi Due to the app / library barrier, if you need to export a variable, do it via a function, e.g.: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library exportTest; {$mode objfpc}{$H+} uses  Classes  { you can add units after this }; type  TTestRec = packed record    idx1 : integer;    idx2 : integer;  end; var _idxData : TTestRec; procedure InitData(i1, i2 : integer);begin     _idxData.idx1 := i1;     _idxData.idx2 := i2;end; function idxData(): TTestRec;begin  Result:= _idxData;end; exports idxData, InitData; begin end.You could put a 'Get' in front of the 'idxData' function name... Regards Benny ---------------------------------------------------------------------- mikerabat · 2024-08-01 09:18 Yes that was exactly my thought. Export a function that updates the internal data (as a library should).. Nevertheless... The lz_FpWeb package exports an Apache module record. And all the Apache modules I inspected do that too. As far as I know Apache accesses the exported record directly to setup it's handler this is where I'm stuck. So... I guess I need to use either fcgi or cgi to create a module for apache then... Or is there another thing that I can do here? ---------------------------------------------------------------------- cdbc · 2024-08-01 09:33 Hi Hmmm, you're sure, it's not a pointer being exported... PRecSomething?!? Regards Benny ---------------------------------------------------------------------- mikerabat · 2024-08-01 09:41 Yes - but the pointer is immediatly dereferenced I guess and it is assumed that the content is a "module" description. Anyhow. I would then go the other way round -> how can I determine the address of the exported data record when loaded into a process? I'm of course not speaking of the @ operator... that would again cause the linker to throw errors :/ Something like the lea opcode in x86 but for 64 bit arm... like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     library exportTest;         {$mode objfpc}{$H+}         uses      Classes    { you can add units after this };         type      TTestRec = packed record        idx1 : integer;        idx2 : integer;      end;     PTestRec = ^TTestRec;         var idxData : TTestRec; export name 'idxData';         procedure InitData(i1, i2 : integer);    var rec : PTestRec;    begin         // load the address into rec         // rec := @idxData; // this fails of course        rec := PTestRec( LoadSharedVariableAddress ); // function that returns the address of the data...                   rec^.idx1 := i1;         rec^.idx2 := i2;    end;         exports idxData, InitData;         begin         end. ---------------------------------------------------------------------- ASerge · 2024-08-01 21:19 Quote from: mikerabat on August 01, 2024, 09:49:13 amHi! The following project throws a linker error - (checked on a raspi 4, CodeTyphon IDE. Additional compiler options -fPIC -Xc I stumbled over this when trying to create an apache module on the RPi4... These modules need to export a record but I cannot fill ....  Anything that I'm missing here? How can I fill the record and exort the data? FPC 3.2.2. Windows x64. There are no problems compiling this code and using exported variable and function. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$APPTYPE CONSOLE} type  TTestRec = packed record    idx1 : integer;    idx2 : integer;  end; var idxData: TTestRec; external 'exportTest.dll'; procedure InitData(i1, i2 : Integer); pascal; external 'exportTest.dll'; begin  InitData(3, 4);  Writeln(idxData.idx1, ' ', idxData.idx2);  Readln;end. ---------------------------------------------------------------------- mikerabat · 2024-08-02 07:25 I know.. on windows this works. I'm having trouble to do it on a Raspberry PI (the package compiles well on FPC Windows but as I wanted to port it to Linux I started getting troubles) My next idea was to use the API dlOpen(nil, 0); dlSym('idxData'); to access the field in the shared file (though I think this is ridiculous and I didn't got that idea running yet)... ---------------------------------------------------------------------- marcov · 2024-08-02 14:19 https://www.freepascal.org/docs-html/3.2.0/prog/progsu152.html ? ---------------------------------------------------------------------- PascalDragon · 2024-08-02 21:28 Quote from: mikerabat on August 02, 2024, 09:25:51 amI know.. on windows this works. I'm having trouble to do it on a Raspberry PI (the package compiles well on FPC Windows but as I wanted to port it to Linux I started getting troubles) My next idea was to use the API dlOpen(nil, 0); dlSym('idxData'); to access the field in the shared file (though I think this is ridiculous and I didn't got that idea running yet)... If I use your example from the first post and use the following program to load the library then it works (at least on x86_64-linux): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program tlibtestprog; {$mode objfpc} uses  DynLibs; type  TInit = procedure(i1, i2: Integer);   TTestRec = packed record    f1, f2: Integer;  end;  PTestRec = ^TTestRec; var  init: TInit;  v: Pointer;  lib: TLibHandle;  rec: PTestRec;begin  lib := LoadLibrary('libtlibtest.so');  if lib = NilHandle then begin    Writeln('Failed to load library');  end else begin    init := TInit(GetProcAddress(lib, 'InitData'));    if not Assigned(init) then begin      Writeln('Failed to retrieve function InitData');    end else begin      init(42, 21);      rec := PTestRec(GetProcAddress(lib, 'idxData'));      if not Assigned(rec) then        Writeln('Failed to retrieve variable idxData')      else        Writeln(rec^.f1, ' ', rec^.f2);    end;    FreeLibrary(lib);  end;end. Code: [Select][sven@Tayet projects]$ LD_LIBRARY_PATH=. ./tlibtestprog 42 21 ---------------------------------------------------------------------- mikerabat · 2024-08-04 08:23 Sorry for not beeing clear enough... I'm having trouble accessing the exported library variabl WITHIN the library aka initializing the variable on shared module load... The problem is not to access the variable in the program that loads the shared lib... kind regards   Mike ---------------------------------------------------------------------- PascalDragon · 2024-08-05 19:55 Quote from: mikerabat on August 04, 2024, 10:23:45 amSorry for not beeing clear enough... I'm having trouble accessing the exported library variabl WITHIN the library aka initializing the variable on shared module load... In that case... initialize it in the begin … end.-block of the library-file? (Or call a suitable function to initialize it from that block) ---------------------------------------------------------------------- H₂SO₄ · 2024-08-06 06:39 Quote from: mikerabat on August 04, 2024, 10:23:45 amI'm having trouble accessing the exported library variabl WITHIN the library aka initializing the variable on shared module load... The problem is not to access the variable in the program that loads the shared lib... Is  InitData  called from non-Pascal code? If that's the case, then you'll need to be careful with the calling conventions. In this minimal example I used  cdecl  and called the function from C: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // File: test.paslibrary test_lib; {$mode objfpc}{$packrecords C} type  TTestRec = record    idx1: Integer;    idx2: Integer;  end; var idxData: TTestRec; cvar; export; procedure InitData(i1, i2: Integer); cdecl;begin  idxData.idx1 := i1;  idxData.idx2 := i2;end; exports idxData, InitData; beginend. Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // File: host.c#include #inlcude  int main(){    void *lib = dlopen("./libtest.so", RTLD_LAZY);    void (*InitData)(int i1, int i2) = dlsym(lib, "InitData");    struct {      int idx1;      int idx2;  } *idxData = dlsym(lib, "idxData");    InitData(42, 777);    printf("idx1 = %d, idx2 = %d\n", idxData->idx1, idxData->idx2);    dlclose(lib);} Compiled with  fpc -fPIC -Xc test.pas && gcc host.c, the output is... Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i idx1 = 42, idx2 = 777 ...as expected. ---------------------------------------------------------------------- mikerabat · 2024-08-06 11:56 Again... sorry for beeing not too clear on the issue. The problem is not accessing the exported library from any executable but rather accessing it WITHIN the library itself aka initialize the record when the module is loaded. And also as I found out it's not a problem on Win but rather a problem on my Raspberry PI. History: I tried to create an apache shared module using fpWeb. Apache expects a variable of type "module" to be exported (and only this) in the shared module.... This record of course needs to be initialized somehow so the fields are valid and the point where this is done is when the module is loaded. I guess I will change my code and see if I can do the same task with fcgi or a simple cgi executable.... ---------------------------------------------------------------------- H₂SO₄ · 2024-08-07 08:16 Quote from: mikerabat on August 06, 2024, 01:56:56 pmApache expects a variable of type "module" to be exported (and only this) in the shared module.... This record of course needs to be initialized somehow so the fields are valid and the point where this is done is when the module is loaded. There are sample Apache modules in FPC's  httpd20  (or another version's) package: go to your FPC installation folder and look at source/packages/httpd20/examples/mod_example.pp. You'll see that to initialize the exported  module  record, the library's main  begin / end.  block is used (as @PascalDragon suggested). ---------------------------------------------------------------------- mikerabat · 2024-08-07 08:27 That is exactly my point! I started with the apache module - compiles fine in Windows and when moved to a raspberry pi the linker started to complain that it's an usafe reference! Then I tried to create a simple shared library and voila the same thing happens. The problem - again - is that it is not allowed to access the exported variable WITHIN the library so I have no chance to intialize it... So the question is - how can I access an exported variable within a shared library on a ARM 64 bit Linux environment... (dlopen, dlsym does not work...) ---------------------------------------------------------------------- MarkMLl · 2024-08-07 08:37 Quote from: Khrys on August 07, 2024, 10:16:20 amYou'll see that to initialize the exported  module  record, the library's main  begin / end.  block is used (as @PascalDragon suggested). And as one of the core developers, his suggestions should not be dismissed casually. So the bottom line is that an Apache module (i.e. a library as distinct from a CGI binary etc.) needs a variable called "module" with all fields pre-initialised before it's loaded. That's a clear case of needing to be done in the library's main block. To summarise: the issue is not one of failing to access the library variable, but the variable not having been initialised at the point it is accessed. MarkMLl ---------------------------------------------------------------------- mikerabat · 2024-08-07 09:20 QuoteTo summarise: the issue is not one of failing to access the library variable, but the variable not having been initialised at the point it is accessed. Actually it's failing to access the variable within the library. The linker complains at any point - even if you would export a function that actually initializes the variable (aka not in the begin end of the shared module code). ---------------------------------------------------------------------- cdbc · 2024-08-07 09:31 Hi How about creating a pointer to your exported variable, in your library and then access this pointer in the begin .. end. block to initialize?!? Like so:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Code: Pascal  [Select][+]// File: test.paslibrary test_lib; {$mode objfpc}{$packrecords C} type  PTestRec = ^TTestRec;  TTestRec = record    idx1: Integer;    idx2: Integer;  end; var   idxData: TTestRec; cvar; export; procedure InitData(prData: pointer); cdecl;begin  PTestRec(prData)^.idx1 := i1;  PTestRec(prData)^.idx2 := i2;end; exports idxData; begin  InitData(@idxData);end.I dunno, just might work... Regards Benny ---------------------------------------------------------------------- mikerabat · 2024-08-07 09:50 Thanks for the suggestion cdbc... I tried that already with no success. Hence my idea to "load" the same library via dlopen and get a pointer to the variable with dlsym but of course that's kinda of a recursion that does not do the trick... I don't know why the linker actually complains here anyway... and only on ARM it turned out. ---------------------------------------------------------------------- MarkMLl · 2024-08-07 10:05 Right. I don't have a running RPi at the moment, which rules me out for practical experimentation. QuoteCodeTyphon IDE. Additional compiler options -fPIC -Xc Try it using either Lazarus or FPC with a makefile or properly- described command line. If you still have problems post the exported ("published") Lazarus project here, and then hands-off to hopefully gave somebody a chance to look at it. I've definitely had separately-linked code (i.e. .so etc.) running on an RPi before now, but looking at some of my stuff: because of the way I generate it (from a .inc describing the interface using a utility I've not published) the way I've done it is to have * A concise main unit declaring appropriate exports. * The main unit imports what it needs from other units, which initialise etc. stuff without problems. hence Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* FPC 2.6.4 on Linux or Solaris FPC 2.6.4 on Linux or Solaris FPC 2.6.4 on Lin *) library Analyzer_Vcd; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,     (* <================    THIS IS REQUIRED!!!!!   =============== *)  {$ENDIF}  Classes, Analyzer_VcdCode; { #todo -oMarkMLl -cLA :  Issue with constant/variable export here. } exports  MagicNumber, State, Trigger, Stop, Rearm, Running; end.  with all the work done in the Analyzer_VcdCode unit. Note that TODO I've got in there, from experience on x86_64 Debian Linux (i.e. I'd expect the linker etc. to be reasonably similar to what's on an RPi). However my recollection of that is that that was strictly related to *exporting* the MagicNumber variable rather than to initialising it. However it looks as though you've hit something which is at least in part platform-specific, and it is by no means outside the bounds of possibility that the CodeTyphon project has applied unpublished patches to the way FPC interacts with the linker. MarkMLl