Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Minimum DLL in MinGW?

Tags:

c

mingw

dll

im trying to compile a dll as small as possible, i reduced the section alignmnent and it saved some size, and i also removed the stdlib with (-nostdlib) and all Optimizations on. now i have some questions

  1. "-Wl,-exclude-all-symbols" removes all symbols, but leaves the exports directory there taking a lot of space
  2. if i used an ld-script to bind both .data and .text section together, will that make the .reloc section useless? as far as i know .reloc is used for absolute addresses from .text to .data, if they are both in the same section, all addresses will be relative right?

thanks.

like image 200
killercode Avatar asked Oct 10 '22 05:10

killercode


1 Answers

Don't mess with sections, just use the -fPIC flag to reduce relocations (otherwise references are always absolute on x86). You will still have a .reloc, since it's used for linking. Also, always compile with -Os and -fomit-frame-pointer.

BTW, you should not have a .data section. If you do it's because something is wrong: look for and fix your data references; everything should be constant. If you need to mess with data, first copy it to a buffer provided by the caller, or something like that.

If you can dedicate some time to learning the GCC attributes extension, you will find several attributes that enhance GCC's comprehension of the code thus enabling better optimizations (e.g. pure and const functions). This can cut a lot of bytes.

More importantly, try to give GCC basic hints about how you're using the code, e.g. use static functions whenever appropriate.

GCC will still add some dummy sections which you can eliminate with the strip utility. It doesn't remove everything by default, you need to specify the -s flag to remove all symbols and -R name to remove a section. You can do something similar with objcopy -S -R name ... (just mentioning it because if you're going to do something else with it, you can do it all in one pass).

like image 58
Ismael Luceno Avatar answered Oct 13 '22 11:10

Ismael Luceno