在不相关的可执行linux中调用函数 [英] call function in unrelated executable linux
问题描述
如果我有一个指向函数的指针并将其提供给另一个(无关/子)可执行文件,我如何在不产生段错误的情况下调用该函数?
If I have a pointer to a function and I give that to another (Unrelated/Child) Executable, how can I call that function without making a segfault?
目前我可以创建一个函数并为其分配这个内存地址:
At the moment I can create a function and assign it this memory adress:
Dim As Function (ByRef As String) As Integer MyFunction
' get pointer...
MyFunction = FunctionPointer
然后调用 MyFunction 我得到一个段错误(显然是因为我正在调用的函数在另一个可执行文件地址空间中,而我不允许访问)
But then calling MyFunction I get a segfault (Obviously because the function I am calling is in another executables adress space whitch I am not allowed to access)
我该如何解决/解决这个问题?
How can I fix this/Work around it?
推荐答案
如果您可以控制 other 可执行文件(您要从中调用函数的那个),那么将其构建为 PIE
(与位置无关的可执行文件),并将其加载到第一个可执行文件的地址空间中.
If you have control over the other executable (the one you want to call a function from), then build it as a PIE
(position-independent executable), and load it into the first executable's address space.
在 C
中,您将使用 -fPIC -pie
构建,然后使用 dlopen(3)
和 dlsym(3)
.
In C
, you would build with -fPIC -pie
, then use dlopen(3)
and dlsym(3)
.
在BASIC
中,我不知道;-(
这篇关于在不相关的可执行linux中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!