Control over symbol exports in MinGW linker

written on Wed 02 July 2014

Below is an example on how to include, or exclude a symbol from the list of exported symbols in a DLL file.

To test it out, we need to compile our object files first:

C:\> g++ -O0 -gdwarf-4 dll\dllmain.cpp -c -o dllmain.o

Linking is done by the g++ driver:

C:\> g++ -Wl,--enable-auto-import -Wl,--out-implib,libsomelib.a -Wl,--exclude-all-symbols -shared dllmain.o -o somelib.dll

Then we can choose which symbols to export directly in the source code:

#include <windows.h>

__declspec(dllexport) void someExportedFunction() {
    MessageBox(NULL, "msgbox", "msgbox", MB_OK);
}

void nonExportedFunction() {
    MessageBox(NULL, "notexported", "notexported", MB_OK);
}

Verification:

C:\libtest> pedump -E somelib.dll

=== EXPORTS ===

# module "somelib.dll"
# flags=0x0  ts="2014-02-20 08:37:48"  version=0.0  ord_base=1
# nFuncs=1  nNames=1

  ORD ENTRY_VA  NAME
  1     1570  _Z20someExportedFunctionv

As you can see, there is no nonExportedFunction symbol defined in the export table of somelib.dll, which matches our goal.

(pedump is a software tool written by Andrey Zaikin, get it from http://pedump.me - but why the hell some AV engines blacklist this domain, I've no idea)

This entry was tagged on #c++ and #windows