Forum > Unix

Dynamically linked executables...possible?

<< < (2/2)

TCH:

--- Quote from: trev on October 14, 2021, 01:44:43 am ---I don't have time to go through your code or understand the issue and you do not mention the OS, but you can look at what I wrote with code examples for macOS (UNIX) in this Wiki article.

--- End quote ---
Thank you, but what you've covered in your article (creating a library and calling it's functions) is already working in my test setup. What does not is the "callback" mechanism, when the library calls a function of the dynamically linked executable. (The statical - where i simply give the function's address to the library - is working.)


--- Quote from: y.ivanov on October 14, 2021, 10:17:41 am ---Isn't it that the 'caller' variable was declared as pointer to fn in mylib, while the 'caller' in mytest is just a function, not a var?
--- End quote ---
It has to be a var which points to a function, since the function does not exists in the library. It works in C, if the executable is linked dynamically. This is in the C lib:
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---extern int caller(int *ptr);int mylib_d(int *d); int mylib_d(int *d){        *d += 5;        return caller(d);}And this is in the C executable:
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int caller(int *ptr){        *ptr += 5;        return *ptr + 5;}This approach also works in the statically linked Pascal executable, although i have to set that variable manually via a setter function of the library.
--- Quote from: y.ivanov on October 14, 2021, 10:17:41 am ---Have you set the LD_LIBRARY_PATH env variable?
--- End quote ---
No, but it is not needed in this case, as i used the full path to the library, not just it's name.

y.ivanov:

--- Quote from: TCH on October 14, 2021, 11:41:06 am ---*snip*


--- Quote from: y.ivanov on October 14, 2021, 10:17:41 am ---Isn't it that the 'caller' variable was declared as pointer to fn in mylib, while the 'caller' in mytest is just a function, not a var?
--- End quote ---
It has to be a var which points to a function, since the function does not exists in the library. It works in C, if the executable is linked dynamically. This is in the C lib:
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---extern int caller(int *ptr);int mylib_d(int *d); int mylib_d(int *d){        *d += 5;        return caller(d);}And this is in the C executable:
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int caller(int *ptr){        *ptr += 5;        return *ptr + 5;}This approach also works in the statically linked Pascal executable, although i have to set that variable manually via a setter function of the library.
--- Quote from: y.ivanov on October 14, 2021, 10:17:41 am ---Have you set the LD_LIBRARY_PATH env variable?
--- End quote ---
No, but it is not needed in this case, as i used the full path to the library, not just it's name.

--- End quote ---
I'm a bit confused!

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---extern int caller(int *ptr);Is not a variable definition. But:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var caller: TCaller; external name 'caller'; is.

Also, objdump -T mytest  doesn't show caller as a dynamic symbol.

Use:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---exports caller; into mytest.pas and:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    //type TCaller = function (ptr: pinteger): integer; cdecl;         //var caller: TCaller; external name 'caller';        function caller(ptr: pinteger): integer; cdecl; external name 'caller'; into mylib.pas.

Otherwise, you'll get an EAccessViolation.

TCH:

--- Quote from: y.ivanov on October 14, 2021, 01:08:24 pm ---I'm a bit confused!

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---extern int caller(int *ptr);Is not a variable definition. But:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var caller: TCaller; external name 'caller'; is.
--- End quote ---
You're right, i confused it. In the statical version it was.
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int (*caller)(int *ptr);int mylib_d(int *d);void mylib_set_caller(int (*caller_ptr)(int*)); int mylib_d(int *d){        *d += 5;        return (*caller)(d);} void mylib_set_caller(int (*caller_ptr)(int*)){        caller = caller_ptr;}
--- Quote from: y.ivanov on October 14, 2021, 01:08:24 pm ---Also, objdump -T mytest  doesn't show caller as a dynamic symbol.
--- End quote ---
Weird. It did for me.
--- Quote from: y.ivanov on October 14, 2021, 01:08:24 pm ---Use:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---exports caller; into mytest.pas and:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    //type TCaller = function (ptr: pinteger): integer; cdecl;         //var caller: TCaller; external name 'caller';        function caller(ptr: pinteger): integer; cdecl; external name 'caller'; into mylib.pas.

Otherwise, you'll get an EAccessViolation.
--- End quote ---
Thank you, it works perfectly!

y.ivanov:

--- Quote from: TCH on October 14, 2021, 01:59:32 pm ---*snip*
Thank you, it works perfectly!

--- End quote ---
You're welcome!

Yet I'm wondering why such weird back-ref to the 'caller' used? I would pass the pointer to it as a parameter to the mylib_d().

TCH:

--- Quote from: y.ivanov on October 14, 2021, 02:27:25 pm ---
--- Quote from: TCH on October 14, 2021, 01:59:32 pm ---*snip*
Thank you, it works perfectly!

--- End quote ---
You're welcome!

Yet I'm wondering why such weird back-ref to the 'caller' used? I would pass the pointer to it as a parameter to the mylib_d().

--- End quote ---
I am experimenting with libraries and i wanted to try out the dynamically linked executable setup too. I have a version where i pass the function's pointer to the library.

Navigation

[0] Message Index

[*] Previous page

Go to full version