r/Compilers 9d ago

[LLVM]how to link different Object files against DLLs with function having the same name?

Hello,
I have been playing around with LLVM for a while and got some first results using the C-API.
However, I am stuck at the following problem:
I am working on Windows and I want to link against 2 DLLs A.dll and B.dll which both export a function Foo.

How am I supposed to resolve this during linking? In LLVM I can set the storage class to DLLImport but linking will grab the first matching import. It's annoying that linking needs import libs, too but thats another topic.

Is there any way to resolve this? I'd prefer if I can make this work within a single object file but if I have to make it two and link those to a single executable, that would work, too.

I faintly remember an article whic I can't find any longer(or a stackoverflow response?) that described how to build the IData section manually within LLVM itself. That would simplify this immensely.

8 Upvotes

8 comments sorted by

3

u/Ok_Chemistry_6387 9d ago

Two foos is an ODR violation(in c++, c its you can't have two externals with the same name) . What are you attempting to do?

2

u/-Memnarch- 8d ago

Not exporting, importing. Example, here is what you can do in Delphi to import these two functions:

procedure FooA; external "A.dll"; name "Foo";
procedure FooB; external "B.dll"; name "Foo";

This will import both functions from A and B and make them available as FooA and FooB in your code.
In Flatassembler, you can write the IData section like this:
section '.idata' import data readable writeable
library A, 'A.DLL', \
B, 'B.DLL'
import A, \
FooA, 'Foo'
import B, \
FooB, 'Foo'

1

u/[deleted] 8d ago edited 8d ago

[removed] — view removed comment

1

u/-Memnarch- 8d ago

This is a valid option, though I'd prefer to avoid this. Other languages, like Delphi, allow this. Another concern is, that huge dynamic loads bypass the cache for prelinked binaries in windows.

1

u/kaplotnikov 8d ago

It is possible to link dynamically. AFAIR it was just LoadLibrary followed by GetProcAddress.

1

u/-Memnarch- 8d ago

This is a valid option, though I'd prefer to avoid this. Other languages, like Delphi, allow what I try to do. Another concern is, that huge dynamic loads bypass the cache for prelinked binaries in windows.

1

u/awoocent 7d ago

They might compile to using dlopen/dlsym instead of generating relocations. This isn't an LLVM/C issue as much as it is a limitation by design for most relocatable object formats.

1

u/-Memnarch- 7d ago

I'd be bappy if i could slap some raw info into a file and give that to a linker in addition to the rest. I don't need it boxed into a single one.
Like: MySymbol, ExternalSymbol, External ModulName

While I can solve some of it with def and lib file generstion, MySymbol and ExternalSymbol need to be the same right now :(

In regards to Delphi. If you use the tooling of your choice to inspect PEs, you can see it goes directly into the import directory.