Thread overview
Is there a way to Deferred binding symbol from dynamic library?
Nov 22
Dakota
Nov 23
IchorDev
Nov 26
Dakota
Nov 29
IchorDev
November 22

Try build a dynamic library, a lot function symbol and global symbol need to be deferred binding from runtime app.

It can be done by pass -Wl,-undefined,dynamic_lookup or --unresolved-symbols=dynamic_lookup to linker.

I am look a way to do it from d code, like pragma(linkerDirective, "--unresolved-symbols=dynamic_lookup"), this can be more flexable when I import one module, I get that module linker options. (there is a lot module they have diff symbol options, I need to combine them arbitrarily)

linux work since they allow undefined symbol when link dynamic library.

for macOS, with pragma(linkerDirective, "--unresolved-symbols=dynamic_lookup");, I get error:

lld: error: --unresolved-symbols=dynamic_lookup is not allowed in LC_LINKER_OPTION

I need to use llvm lld -flavor ld64.

November 23

On Friday, 22 November 2024 at 06:13:26 UTC, Dakota wrote:

>

Try build a dynamic library, a lot function symbol and global symbol need to be deferred binding from runtime app.

Why not load the libraries at runtime and look the pointers up with symbol names? Or do you really need the compiler to do it for you?

November 26

On Saturday, 23 November 2024 at 12:09:22 UTC, IchorDev wrote:

>

Why not load the libraries at runtime and look the pointers up with symbol names? Or do you really need the compiler to do it for you?

Thanks for suggestion.

Because I need link some part static c library, they link global var by symbols name, I can not change this part.

What I can do is provide the symbol at runtime, I can not think a way to work around this at runtime. (maybe for function, but not for global vars)

November 29

On Tuesday, 26 November 2024 at 10:34:07 UTC, Dakota wrote:

>

On Saturday, 23 November 2024 at 12:09:22 UTC, IchorDev wrote:

>

Why not load the libraries at runtime and look the pointers up with symbol names? Or do you really need the compiler to do it for you?

Because I need link some part static c library, they link global var by symbols name, I can not change this part.

What I can do is provide the symbol at runtime, I can not think a way to work around this at runtime. (maybe for function, but not for global vars)

I'm pretty sure loading the exported symbols of global variables works the same way, they'd just be pointers to data instead of to code.

__gshared int* foreignLib_some_integer;

//to load:
foreignLib_some_integer = cast(int*)loadSymbol(foreignLib, "foreignLib_some_integer");

//using in your code:
if(*foreignLib_some_integer == 10){
  //etc.
}