Stuck with DLL export

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

Post Reply
User avatar
sadko4u
Established Member
Posts: 988
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 361 times

Stuck with DLL export

Post by sadko4u »

Currently I'm stuck with MinGW and DLL exports...

We have file1.cpp:

Code: Select all

extern "C"
{
    __declspec(dllexport)
    int f() { return 1; }

    __declspec(dllexport)
    int g() { return 2; }

    int k() { return 3; }
}
And file2.cpp:

Code: Select all

extern "C"
{
    __declspec(dllexport)
    int l() { return 1; }

    __declspec(dllexport)
    int m() { return 2; }

    int n() { return 3; }
}
We compile them as:

Code: Select all

g++ -o file1.o -c file1.cpp -fPIC
g++ -o file2.o -c file2.cpp -fPIC
Consider the following linking example:

Code: Select all

g++ -o test1.dll file1.o file2.o -shared -fPIC
dumpbin /exports test1.dll
It works as expected, only __declspec(dllexport) symbols are exported:
Image

But merging files into larger object file does not work:

Code: Select all

ld -r -o file3.o file1.o file2.o
g++ -o test2.dll file3.o -shared -fPIC
dumpbin /exports test2.dll
It gives all possible symbols exported in the DLL:
Image


MinGW 8.1.0, tested different builds with the same result.
Any ideas how to make it work properly without using the DEF file?
LSP (Linux Studio Plugins) Developer and Maintainer.
User avatar
sadko4u
Established Member
Posts: 988
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 361 times

Re: Stuck with DLL export

Post by sadko4u »

Image
LSP (Linux Studio Plugins) Developer and Maintainer.
User avatar
sadko4u
Established Member
Posts: 988
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 361 times

Re: Stuck with DLL export

Post by sadko4u »

Finally, found the solution.
The problem was that original *.o files contain the additional .drectve section that holds export directives for the linker.
After processing these files with ld, we become new *.o file with omitted .drectve section.
So, the solution to get the expected result, is to force the ld to keep the .drectve section in the output object file.
Sadly, but there is no special option for ld.
But we can generate the default script for the [/b] ld where we observe the following:

Code: Select all

  /DISCARD/ :
  {
    *(.drectve)
    *(.debug$S)
    *(.debug$T)
    *(.debug$F)
  }
That means, that the .drectve section will be discarded in the output file.
So we need to patch this script:

Code: Select all

  .drectve  :
  {
    *(.drectve)
  }
  /DISCARD/ :
  {
    *(.debug$S)
    *(.debug$T)
    *(.debug$F)
  }
And pass this script as the -T option.
After that, all works fine.
LSP (Linux Studio Plugins) Developer and Maintainer.
Post Reply