1

I need some help for compiling with GCC under MinGW.

Say I have two files:

  • File a.c contains two functions, a1 and a2
  • File b.c contains two functions, b1 and b2.

Then I link the two objects into a shared library. The command used are like:

gcc -c a.c
gcc -c b.c
gcc -shared -Wl, --version-script v.ver -Wl, -Map=out.map -Wl, --strip-all -o mydll.dll a.o b.o

File v.ver looks like:

mylib {
   global: a1;
           a2;
   local: *;
}

which is used to control which functions to be exported.

By checking the mapfile I can see that the two functions in b.c are also included into the .text section of the DLL file.

Because this DLL file only exports a1 and a2, and b1 and b2 are only defined in b.c, but never used anywhere. Is there an option I could add in GCC or ld so that b1 and b2 are not built into the DLL file so that I can save some space in the DLL file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rhbc73
  • 739
  • 9
  • 24

1 Answers1

5

Yes, this is possible. To do this, add the following two flags when compiling your C source code to objects:

 -ffunction-sections -fdata-sections

This will generate bigger object files, but will add a lot of information for the linker. When calling the linker add the following flag:

--gc-sections

The linker will now throw away all functions and sections that are not used. Note that this might incur a performance penalty:

Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker create larger object and executable files and are also slower. These options affect code generation. They prevent optimizations by the compiler and assembler using relative locations inside a translation unit since the locations are unknown until link time. An example of such an optimization is relaxing calls to short call instructions. (man gcc)

See also this question: Query on -ffunction-section & -fdata-sections options of gcc for more information.

Cheiron
  • 3,620
  • 4
  • 32
  • 63
  • Thanks Cheiron, actually I tried these options long time on an Unbutu machine and it worked. I remember seeing someone saying these options only work for ELF (linux) executables. I'll have a try tomorrow on MinGW, but is there anyone option to achieve same goal? – rhbc73 Mar 16 '16 at 11:10
  • These are GCC flags and thus should work on any platform, targetting anything, as long as it is not a static library. Well, when thinking longer about that: how would you be sure that these functions are never used? I can write a program linking to your DLL and calling the functions that you want removed, right? So in that sense, why should the functions be removed? – Cheiron Mar 16 '16 at 11:13
  • Wait, never mind: I oversaw the map part. Well, if that part is working correctly, then the linker has no reason to not remove unused functions, so it should work. – Cheiron Mar 16 '16 at 11:19
  • Not working on .so file being made by an Android Toolchain. These options were already there in the toolchain file. When I did a `readelf -a` command for the .so generated for ARMEABI , it also showed the name of all functions which were also not being used. – Shivansh Jagga Oct 09 '17 at 10:32
  • It would be weird if it did work on a .so file, since its a library, so other programs can call functions from that library that are not called by another function in the library. – Cheiron Oct 09 '17 at 11:49
  • wouldn't --gc-sections make more sense when we are linking the library (with all the code from the object file) with some user application object files ? – RohitMat Dec 18 '19 at 08:18
  • Usually you are linking dynamically so that the library does not even end up in the executable, just some code warning the OS that this library should also be loaded on program start. On static linking you could say that these sections should be removed and they probably are. – Cheiron Dec 18 '19 at 11:44