Why do I need the-s flag in gcc?

Actually the answer to the question I found:

Remove all symbol table and relocation information from the executable. (https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options)

Found an article about relocation information:

Relocation information

Setup information (information for the linker about which constructs in the object file should be replaced with the addresses of objects defined in other source files)

(http://computer_en_ru.academic.ru/36046/relocation_information)

Then found the answer to the question, what is symbol table such: https://stackoverflow.com/questions/69112/what-is-a-symbol-table

But with my knowledge of English, a holistic view of the problem did not work out. Can someone help you put all the pieces of the puzzle into a single picture?

Author: Дух сообщества, 2017-05-20

1 answers

The compiler puts in the executable file not only what is needed to run (that is, code and data), but also additional (and generally unnecessary) information (for example, the names of all variables and functions, including those that were declared with the static specifier).

This additional information is only needed at the build stage, but the compiler leaves it in the final executable file, perhaps for the convenience of further debugging. The flag is responsible for cutting it out -s.

Take, for example, a program consisting of two source code files:

  1. main.cpp

    #include <iostream>
    #include <string>
    
    std::string partOne("Hello, "); // Глобальная переменная
    
    std::string getString(); // Прототип функции, объявленной в другом файле
    
    int main()
    {
        std::cout << getString() << std::endl;
        return 0;
    }
    
  2. other.cpp

    #include <string>
    
    extern std::string partOne;
    static std::string partTwo("World!");
    
    std::string getString()
    {
        return partOne + partTwo;
    }
    
  3. CMake.txt

    add_executable(example1 main.cpp other.cpp)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto") 
    

    Flag -flto I inserted it to demonstrate that even enabling inter-module optimizations does not allow the compiler to remove any names.

Let's build the project in the Release mode (-DCMAKE_BUILD_TYPE=Release) and let's study the symbol table with the utility nm:

004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
00406000 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e4 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e4 b .bss
004063e4 b .bss
004063e4 b .bss
00406020 b .bss
004063e4 b .bss
004063e4 b .bss
004063e8 b .bss
004063e4 b .bss
004063e8 b .bss
004063e4 b .bss
004063e4 b .bss
004063e8 b .bss
004063e4 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e0 b .bss
004063c0 b .bss
00406038 b .bss
004063e8 b .bss
004063e8 b .bss
004063c0 b .bss
004063e8 b .bss
00406080 b .bss
00406078 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
00406078 b .bss
00406044 b .bss
0040606c b .bss
004063e8 b .bss
00406044 b .bss
00406068 b .bss
00406068 b .bss
00406044 b .bss
00406068 b .bss
00406064 b .bss
00406064 b .bss
00406044 b .bss
00406064 b .bss
00406044 b .bss
00406060 b .bss
00406060 b .bss
00406044 b .bss
0040605c b .bss
00406058 b .bss
00406044 b .bss
00406054 b .bss
00406044 b .bss
00406044 b .bss
004063e8 b .bss
00406044 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
004063e8 b .bss
00408000 d .CRT$XCA
00408004 d .CRT$XCAA
00408008 d .CRT$XCZ
0040802c d .CRT$XDA
00408030 d .CRT$XDZ
0040800c d .CRT$XIA
00408010 d .CRT$XIAA
00408014 d .CRT$XIC
00408018 d .CRT$XIZ
0040801c d .CRT$XLA
00408020 d .CRT$XLC
00408024 d .CRT$XLD
00408028 d .CRT$XLZ
00402914 t .ctors
00402918 t .ctors.65535
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403000 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403020 d .data
00403028 d .data
00403028 d .data
00403020 d .data
00403020 d .data
00403004 d .data
00403020 d .data
00403020 d .data
00403028 d .data
00403028 d .data
00403020 d .data
0040301c d .data
00403028 d .data
0040301c d .data
0040301c d .data
00403028 d .data
00403004 d .data
0040301c d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403004 d .data
0040301c d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403018 d .data
00403028 d .data
00403018 d .data
00403028 d .data
00403028 d .data
00403014 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403004 d .data
00403014 d .data
00403028 d .data
00403014 d .data
00403028 d .data
00403004 d .data
00403014 d .data
00403028 d .data
00403014 d .data
00403004 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403028 d .data
00403014 d .data
00403014 d .data
0040300c d .data
00403004 d .data
00403004 d .data
00403008 d .data
00403008 d .data
00403008 d .data
00403004 d .data
00403008 d .data
00403004 d .data
00403004 d .data
00403004 d .data
0040301c d .data
00403028 d .data
00403008 d .data
00403028 d .data
00403028 d .data
00403004 d .data
00403028 d .data$__security_cookie
0040302c d .data$__security_cookie_complement
0040c000 N .debug_abbrev
0040c014 N .debug_abbrev
0040a020 N .debug_aranges
0040a000 N .debug_aranges
0040e000 N .debug_frame
0040b0b3 N .debug_info
0040b000 N .debug_info
0040d077 N .debug_line
0040d000 N .debug_line
004055d8 r .eh_frame
00405298 r .eh_frame
004058a0 r .eh_frame
004053e0 r .eh_frame
00405000 r .eh_frame
0040543c r .eh_frame
004053b4 r .eh_frame
004054d0 r .eh_frame
004052c4 r .eh_frame
0040521c r .eh_frame
00405128 r .eh_frame
00405304 r .eh_frame
00405360 r .eh_frame
00405564 r .eh_frame
004054fc r .eh_frame
004050c8 r .eh_frame
004056b0 r .eh_frame
004058e0 r .eh_frame
00402928 t .gcc_except_table
00407028 i .idata$2
00407000 i .idata$2
0040703c i .idata$2
00407014 i .idata$2
00407184 i .idata$4
00407120 i .idata$4
00407130 i .idata$4
00407128 i .idata$4
004070d0 i .idata$4
00407064 i .idata$4
00407168 i .idata$4
0040714c i .idata$4
004070f4 i .idata$4
0040708c i .idata$4
00407094 i .idata$4
004070f0 i .idata$4
00407104 i .idata$4
00407070 i .idata$4
004070c8 i .idata$4
0040716c i .idata$4
004070cc i .idata$4
004070a0 i .idata$4
00407080 i .idata$4
004070e0 i .idata$4
00407138 i .idata$4
004070fc i .idata$4
00407068 i .idata$4
0040709c i .idata$4
00407124 i .idata$4
0040717c i .idata$4
00407064 i .idata$4
00407140 i .idata$4
004070ec i .idata$4
004070b8 i .idata$4
00407158 i .idata$4
0040712c i .idata$4
004070a8 i .idata$4
00407114 i .idata$4
00407148 i .idata$4
00407108 i .idata$4
0040707c i .idata$4
0040711c i .idata$4
004070b0 i .idata$4
00407118 i .idata$4
00407134 i .idata$4
0040713c i .idata$4
00407174 i .idata$4
00407148 i .idata$4
00407164 i .idata$4
00407078 i .idata$4
004070bc i .idata$4
00407180 i .idata$4
004070e8 i .idata$4
00407170 i .idata$4
00407084 i .idata$4
00407098 i .idata$4
00407178 i .idata$4
00407090 i .idata$4
004070b4 i .idata$4
004070c4 i .idata$4
0040706c i .idata$4
00407088 i .idata$4
004070d4 i .idata$4
00407078 i .idata$4
004070d8 i .idata$4
004070c0 i .idata$4
0040715c i .idata$4
00407150 i .idata$4
004070d8 i .idata$4
00407110 i .idata$4
0040710c i .idata$4
00407154 i .idata$4
004070f8 i .idata$4
00407074 i .idata$4
004070e4 i .idata$4
00407160 i .idata$4
00407100 i .idata$4
004070ac i .idata$4
00407144 i .idata$4
004070dc i .idata$4
004070a4 i .idata$4
004071a0 i .idata$5
004071a8 i .idata$5
004071d0 i .idata$5
004071f0 i .idata$5
00407234 i .idata$5
004071e4 i .idata$5
00407278 i .idata$5
00407274 i .idata$5
004071fc i .idata$5
004071ac i .idata$5
00407224 i .idata$5
00407190 i .idata$5
004071d8 i .idata$5
00407200 i .idata$5
00407208 i .idata$5
004071b4 i .idata$5
004071ec i .idata$5
0040719c i .idata$5
0040729c i .idata$5
004071f8 i .idata$5
004071e0 i .idata$5
00407240 i .idata$5
004071fc i .idata$5
00407238 i .idata$5
00407258 i .idata$5
004071e8 i .idata$5
004072a4 i .idata$5
00407298 i .idata$5
004071f4 i .idata$5
00407288 i .idata$5
00407260 i .idata$5
0040720c i .idata$5
004072a0 i .idata$5
00407188 i .idata$5
00407280 i .idata$5
00407290 i .idata$5
00407250 i .idata$5
004071b0 i .idata$5
004071cc i .idata$5
004071dc i .idata$5
00407284 i .idata$5
0040722c i .idata$5
0040718c i .idata$5
0040725c i .idata$5
00407264 i .idata$5
00407248 i .idata$5
00407204 i .idata$5
00407228 i .idata$5
00407194 i .idata$5
004071c0 i .idata$5
0040728c i .idata$5
00407188 i .idata$5
004072a8 i .idata$5
0040726c i .idata$5
00407254 i .idata$5
00407214 i .idata$5
004071c4 i .idata$5
004071a4 i .idata$5
004071b8 i .idata$5
00407218 i .idata$5
00407270 i .idata$5
0040719c i .idata$5
00407244 i .idata$5
00407198 i .idata$5
0040724c i .idata$5
00407210 i .idata$5
004071d4 i .idata$5
0040726c i .idata$5
0040723c i .idata$5
0040721c i .idata$5
00407220 i .idata$5
004071bc i .idata$5
004071c8 i .idata$5
00407294 i .idata$5
00407268 i .idata$5
00407230 i .idata$5
0040727c i .idata$5
00407724 i .idata$6
00407562 i .idata$6
004074d0 i .idata$6
004076a8 i .idata$6
004075a4 i .idata$6
00407374 i .idata$6
0040738a i .idata$6
004072f4 i .idata$6
00407540 i .idata$6
0040759c i .idata$6
00407708 i .idata$6
004077b8 i .idata$6
004075ae i .idata$6
004074a2 i .idata$6
004072c0 i .idata$6
004074fe i .idata$6
004075c0 i .idata$6
00407610 i .idata$6
004073ec i .idata$6
00407324 i .idata$6
00407580 i .idata$6
004075fa i .idata$6
0040751a i .idata$6
0040735e i .idata$6
00407480 i .idata$6
00407790 i .idata$6
004075c8 i .idata$6
00407494 i .idata$6
004074e0 i .idata$6
004074ee i .idata$6
00407674 i .idata$6
0040756c i .idata$6
00407418 i .idata$6
0040750a i .idata$6
00407592 i .idata$6
00407664 i .idata$6
004076e0 i .idata$6
004072dc i .idata$6
0040730c i .idata$6
00407478 i .idata$6
00407604 i .idata$6
004073d2 i .idata$6
00407740 i .idata$6
004075e6 i .idata$6
004075b6 i .idata$6
004073fc i .idata$6
004075d2 i .idata$6
0040745a i .idata$6
00407634 i .idata$6
00407440 i .idata$6
0040754a i .idata$6
004072ac i .idata$6
004076cc i .idata$6
004075f0 i .idata$6
0040733c i .idata$6
0040734a i .idata$6
004074be i .idata$6
004073c0 i .idata$6
004076f8 i .idata$6
00407578 i .idata$6
004075dc i .idata$6
0040739a i .idata$6
00407684 i .idata$6
004073ae i .idata$6
004077ac i .idata$6
00407430 i .idata$6
0040752c i .idata$6
00407558 i .idata$6
00407588 i .idata$6
00407910 i .idata$7
004078a8 i .idata$7
004078a4 i .idata$7
004078f0 i .idata$7
004078e0 i .idata$7
004078e8 i .idata$7
0040791c i .idata$7
004078dc i .idata$7
0040786c i .idata$7
004078b0 i .idata$7
004078c4 i .idata$7
00407858 i .idata$7
00407820 i .idata$7
0040788c i .idata$7
00407934 i .idata$7
00407870 i .idata$7
004078cc i .idata$7
00407924 i .idata$7
00407898 i .idata$7
00407890 i .idata$7
004078a0 i .idata$7
00407834 i .idata$7
00407928 i .idata$7
004078bc i .idata$7
004078ac i .idata$7
004078c8 i .idata$7
00407848 i .idata$7
0040789c i .idata$7
00407804 i .idata$7
004078d4 i .idata$7
004078b8 i .idata$7
004077fc i .idata$7
00407830 i .idata$7
004078ec i .idata$7
004078d8 i .idata$7
00407874 i .idata$7
00407868 i .idata$7
00407888 i .idata$7
00407930 i .idata$7
0040790c i .idata$7
0040782c i .idata$7
00407828 i .idata$7
00407938 i .idata$7
00407850 i .idata$7
004078c0 i .idata$7
004078f4 i .idata$7
00407908 i .idata$7
00407800 i .idata$7
00407894 i .idata$7
0040781c i .idata$7
00407860 i .idata$7
004078e4 i .idata$7
0040792c i .idata$7
0040784c i .idata$7
004077f8 i .idata$7
0040785c i .idata$7
00407918 i .idata$7
00407920 i .idata$7
004078b4 i .idata$7
00407840 i .idata$7
0040793c i .idata$7
00407824 i .idata$7
00407838 i .idata$7
00407914 i .idata$7
00407808 i .idata$7
00407844 i .idata$7
00407854 i .idata$7
00407878 i .idata$7
0040783c i .idata$7
004078d0 i .idata$7
00407904 i .idata$7
00407900 i .idata$7
00407864 i .idata$7
00403030 d .jcr
00403030 d .jcr
00404064 r .rdata
004041ac r .rdata
00404078 r .rdata
004042ac r .rdata
004042a4 r .rdata
00404074 r .rdata
00404000 r .rdata
00404730 r .rdata$zzz
00404370 r .rdata$zzz
004043b0 r .rdata$zzz
004043f0 r .rdata$zzz
00404430 r .rdata$zzz
00404470 r .rdata$zzz
004044b0 r .rdata$zzz
004044f0 r .rdata$zzz
00404530 r .rdata$zzz
004048f0 r .rdata$zzz
00404570 r .rdata$zzz
004045b0 r .rdata$zzz
004045f0 r .rdata$zzz
00404630 r .rdata$zzz
00404670 r .rdata$zzz
004046b0 r .rdata$zzz
00404930 r .rdata$zzz
004046f0 r .rdata$zzz
004048b0 r .rdata$zzz
004042f0 r .rdata$zzz
00404870 r .rdata$zzz
00404770 r .rdata$zzz
004047b0 r .rdata$zzz
004047f0 r .rdata$zzz
004042b0 r .rdata$zzz
00404830 r .rdata$zzz
00404330 r .rdata$zzz
00404970 r .rdata_runtime_pseudo_reloc
004026d8 t .text
004027d0 t .text
00402390 t .text
     U .text
     U .text
     U .text
     U .text
004026e0 t .text
00402390 t .text
004027c8 t .text
004027d8 t .text
00402140 t .text
00401500 t .text
00402778 t .text
     U .text
0040270c t .text
004027d8 t .text
00402140 t .text
00402778 t .text
00402780 t .text
00402000 t .text
     U .text
004026d0 t .text
00402788 t .text
0040270c t .text
004027d8 t .text
00401f60 t .text
     U .text
     U .text
00402790 t .text
00401610 t .text
004027a8 t .text
0040270c t .text
     U .text
004027c0 t .text
00401f50 t .text
0040270c t .text
00402798 t .text
00401b10 t .text
     U .text
     U .text
0040270c t .text
00402798 t .text
00401a50 t .text
     U .text
0040270c t .text
00402714 t .text
     U .text
00401a40 t .text
     U .text
00401a40 t .text
00402720 t .text
004018f0 t .text
004027b8 t .text
     U .text
004018f0 t .text
00402740 t .text
004018f0 t .text
00402748 t .text
004018f0 t .text
00402750 t .text
     U .text
00401810 t .text
00402758 t .text
00401810 t .text
00402760 t .text
004017f0 t .text
00402768 t .text
     U .text
004017e0 t .text
004027b0 t .text
00401000 t .text
00401680 t .text
     U .text
00402770 t .text
     U .text
00401700 t .text
00401688 t .text
00401690 t .text
00401690 t .text
004016f8 t .text
00401690 t .text
     U .text
004016f0 t .text
     U .text
004016e8 t .text
00401698 t .text
004016e0 t .text
004016a0 t .text
     U .text
004016d8 t .text
004016a8 t .text
004016d0 t .text
004016a8 t .text
004016c8 t .text
     U .text
     U .text
004016c0 t .text
004016b0 t .text
004016b8 t .text
004027a0 t .text
004027e0 t .text.startup
004028f0 t .text.startup
00409004 d .tls
00409000 d .tls$AAA
0040901c d .tls$ZZZ
00000000 A .weak.___deregister_frame_info._hmod_libgcc
00000000 A .weak.___register_frame_info._hmod_libgcc
00000000 A .weak.__Jv_RegisterClasses._hmod_libgcc
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
00000001 a @feat.00
004021c0 T ____w64_mingwthr_add_key_dtor
00402250 T ____w64_mingwthr_remove_key_dtor
004026e0 T ___chkstk_ms
0040800c D ___crt_xc_end__
00408000 D ___crt_xc_start__
0040801c D ___crt_xi_end__
0040800c D ___crt_xi_start__
0040801c D ___crt_xl_start__
0040802c D ___crt_xp_end__
0040802c D ___crt_xp_start__
0040802c D ___crt_xt_end__
0040802c D ___crt_xt_start__
00402910 T ___CTOR_LIST__
004026d0 T ___deregister_frame_info
00402780 T ___dllonexit
00401f90 T ___do_global_ctors
00401f60 T ___do_global_dtors
00402920 T ___DTOR_LIST__
00401700 t ___dyn_tls_dtor@12
00401750 T ___dyn_tls_init@12
00404074 R ___dyn_tls_init_callback
004050c8 r ___EH_FRAME_BEGIN__
004058e0 r ___FRAME_END__
004015b0 T ___gcc_deregister_frame
00401680 T ___gcc_personality_v0
00401500 T ___gcc_register_frame
00402714 T ___getmainargs
00400000 A ___ImageBase
00403030 d ___JCR_END__
00403030 d ___JCR_LIST__
00000000 A ___lconv_init
00401fe0 T ___main
00402630 T ___mingw_enum_import_library_names
00402500 T ___mingw_GetSectionCount
004024b0 T ___mingw_GetSectionForAddress
00401000 t ___mingw_invalidParameterHandler
00406064 B ___mingw_oldexcpt_handler
00408014 D ___mingw_pinit
00401a50 T ___mingw_raise_matherr
00401aa0 T ___mingw_setusermatherr
004022f0 T ___mingw_TLScallback
004063ec B ___mingw_winmain_hInstance
004063e8 B ___mingw_winmain_lpCmdLine
00403000 D ___mingw_winmain_nShowCmd
004063c8 b ___mingwthr_cs
004063c4 b ___mingwthr_cs_init
00402140 t ___mingwthr_run_key_dtors.part.0
00403010 D ___native_dllmain_reason
004063fc B ___native_startup_lock
00406400 B ___native_startup_state
0040300c D ___native_vcclrit_reason
004063f8 B ___onexitbegin
004063f4 B ___onexitend
004026d8 T ___register_frame_info
00401b10 t ___report_error
004020b0 T ___report_gsfailure
00404970 A ___RUNTIME_PSEUDO_RELOC_LIST__
00404988 R ___RUNTIME_PSEUDO_RELOC_LIST_END__
00403028 D ___security_cookie
0040302c D ___security_cookie_complement
00402000 T ___security_init_cookie
0040270c T ___set_app_type
00402798 T ___setusermatherr
00401610 t ___tcf_0.lto_priv.6
00401620 t ___tcf_0.lto_priv.7
00401650 t ___tcf_1
004017d0 T ___tlregdtor
00409020 D ___tls_end__
00409000 D ___tls_start__
00401180 t ___tmainCRTStartup
00408000 D ___xc_a
00408008 D ___xc_z
0040802c d ___xd_a
00408030 d ___xd_z
0040800c D ___xi_a
00408018 D ___xi_z
0040801c D ___xl_a
00408020 D ___xl_c
00408024 D ___xl_d
00408028 D ___xl_z
00402760 T __amsg_exit
00406414 B __bss_end__
00406000 B __bss_start__
00402758 T __cexit
00403004 D __charmax
00403018 D __CRT_MT
00402910 T __CTOR_LIST__
00403034 D __data_end__
00403000 D __data_start__
004017f0 T __decode_pointer
00000000 A __dll__
00000000 A __dll_characteristics__
00403008 D __dowildcard
00402920 T __DTOR_LIST__
00401800 T __encode_pointer
0040a000 D __end__
00000200 A __file_alignment__
004023d0 T __FindPESection
00402410 T __FindPESectionByName
00402530 T __FindPESectionExec
0040605c B __fmode
00401f50 T __fpreset
00402898 T __fu0___ZSt4cout
00402720 T __get_invalid_parameter_handler
004025a0 T __GetPEImageBase
004027e0 t __GLOBAL__I_65535_0_objects.a_0xd0.3181
004018f0 T __gnu_exception_handler@4
00407014 I __head_lib32_libkernel32_a
00407028 I __head_lib32_libmsvcrt_a
00407000 I __head_libgcc_s_dw2_1_dll
0040703c I __head_libstdc___6_dll
004072ac I __IAT_end__
00407188 I __IAT_start__
00400000 A __image_base__
0040718c I __imp____deregister_frame_info
004071fc I __imp____dllonexit
00407190 I __imp____gcc_personality_v0
00407200 I __imp____getmainargs
00407204 I __imp____initenv
00407208 I __imp____lconv_init
00407194 I __imp____register_frame_info
0040720c I __imp____set_app_type
00407210 I __imp____setusermatherr
00407214 I __imp___acmdln
00407218 I __imp___amsg_exit
0040721c I __imp___cexit
00407220 I __imp___fmode
00403020 D __imp___get_invalid_parameter_handler
00407224 I __imp___initterm
00407228 I __imp___iob
0040722c I __imp___lock
00407230 I __imp___onexit
00403024 D __imp___set_invalid_parameter_handler
00407234 I __imp___unlock
00407188 I __imp___Unwind_Resume
0040726c I __imp___ZNKSt5ctypeIcE13_M_widen_initEv
00407270 I __imp___ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
00407274 I __imp___ZNSo3putEc
00407278 I __imp___ZNSo5flushEv
0040727c I __imp___ZNSs4_Rep10_M_destroyERKSaIcE
00407280 I __imp___ZNSs4_Rep10_M_disposeERKSaIcE
00407284 I __imp___ZNSs6appendERKSs
00407288 I __imp___ZNSsC1EPKcRKSaIcE
0040728c I __imp___ZNSsC1ERKSs
00407290 I __imp___ZNSt8ios_base4InitC1Ev
00407294 I __imp___ZNSt8ios_base4InitD1Ev
00407298 I __imp___ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
0040729c I __imp___ZSt16__throw_bad_castv
004072a0 I __imp___ZSt4cout
004072a0 I __imp___ZSt4cout
004072a4 I __imp___ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
00407238 I __imp__abort
0040723c I __imp__calloc
0040719c I __imp__DeleteCriticalSection@4
004071a0 I __imp__EnterCriticalSection@4
00407240 I __imp__exit
00407244 I __imp__fprintf
00407248 I __imp__free
004071a4 I __imp__FreeLibrary@4
0040724c I __imp__fwrite
004071a8 I __imp__GetCurrentProcess@0
004071ac I __imp__GetCurrentProcessId@0
004071b0 I __imp__GetCurrentThreadId@0
004071b4 I __imp__GetLastError@0
004071b8 I __imp__GetModuleHandleA@4
004071bc I __imp__GetProcAddress@8
004071c0 I __imp__GetStartupInfoA@4
004071c4 I __imp__GetSystemTimeAsFileTime@4
004071c8 I __imp__GetTickCount@0
004071cc I __imp__InitializeCriticalSection@4
004071d0 I __imp__LeaveCriticalSection@4
004071d4 I __imp__LoadLibraryA@4
00407250 I __imp__malloc
00407254 I __imp__memcpy
004071d8 I __imp__QueryPerformanceCounter@4
004071dc I __imp__SetUnhandledExceptionFilter@4
00407258 I __imp__signal
004071e0 I __imp__Sleep@4
0040725c I __imp__strlen
00407260 I __imp__strncmp
004071e4 I __imp__TerminateProcess@8
004071e8 I __imp__TlsGetValue@4
004071ec I __imp__UnhandledExceptionFilter@4
00407264 I __imp__vfprintf
004071f0 I __imp__VirtualProtect@16
004071f4 I __imp__VirtualQuery@12
00402768 T __initterm
004025d0 T __IsNonwritableInCurrentImage
     U __Jv_RegisterClasses
00407878 I __lib32_libkernel32_a_iname
004078f4 I __lib32_libmsvcrt_a_iname
00000000 A __loader_flags__
00402778 T __lock
00000000 A __major_image_version__
00000004 A __major_os_version__
00000004 A __major_subsystem_version__
00401ab0 T __matherr
0040301c D __MINGW_INSTALL_DEBUG_MATHERR
00000000 A __minor_image_version__
00000000 A __minor_os_version__
00000000 A __minor_subsystem_version__
00406060 B __newmode
004077ac I __nm___ZSt4cout
00401cb0 T __pei386_runtime_relocator
00404988 R __rt_psrelocs_end
00000018 A __rt_psrelocs_size
00404970 R __rt_psrelocs_start
00404970 A __RUNTIME_PSEUDO_RELOC_LIST__
00404988 R __RUNTIME_PSEUDO_RELOC_LIST_END__
00001000 A __section_alignment__
00402730 T __set_invalid_parameter_handler
00401a40 T __setargv
00001000 A __size_of_heap_commit__
00100000 A __size_of_heap_reserve__
00001000 A __size_of_stack_commit__
00200000 A __size_of_stack_reserve__
00000003 A __subsystem__
0040901c D __tls_end
00406050 B __tls_index
00409000 D __tls_start
00409004 D __tls_used
00402788 T __unlock
00401688 T __Unwind_Resume
004023b0 T __ValidateImageBase
00402390 t __ValidateImageBase.part.0
00000000 A __Z9getStringv
00406038 b __ZL7partTwo
004016c8 T __ZNKSt5ctypeIcE13_M_widen_initEv
00000000 A __ZNKSt5ctypeIcE8do_widenEc
00401698 T __ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
004016e0 T __ZNSo3putEc
004016d8 T __ZNSo5flushEv
004016a8 T __ZNSs4_Rep10_M_destroyERKSaIcE
00401690 T __ZNSs4_Rep10_M_disposeERKSaIcE
004016f0 T __ZNSs6appendERKSs
004016b0 T __ZNSsC1EPKcRKSaIcE
004016f8 T __ZNSsC1ERKSs
004016b8 T __ZNSt8ios_base4InitC1Ev
004016c0 T __ZNSt8ios_base4InitD1Ev
004016e8 T __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
004016d0 T __ZSt16__throw_bad_castv
004016a0 T __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
00406040 b __ZStL8__ioinit
00000000 A __ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_ERKS6_S8_
004027b8 T _abort
0040601c b _argc
00406010 b _argret
00406018 b _argv
004018d0 T _atexit
004027c0 T _calloc
00404194 r _CSWTCH.5
00000000 A _DeleteCriticalSection@4
00000000 A _EnterCriticalSection@4
00406014 b _envp
00402770 T _exit
00401f50 T _fpreset
004027a0 T _fprintf
004027c8 T _free
00000000 A _FreeLibrary@4
004027a8 T _fwrite
00000000 A _GetCurrentProcess@0
00000000 A _GetCurrentProcessId@0
00000000 A _GetCurrentThreadId@0
00000000 A _GetLastError@0
00000000 A _GetModuleHandleA@4
00000000 A _GetProcAddress@8
00000000 A _GetStartupInfoA@4
00000000 A _GetSystemTimeAsFileTime@4
00000000 A _GetTickCount@0
00406080 b _GS_ContextRecord
004042a4 r _GS_ExceptionPointers
00406360 b _GS_ExceptionRecord
004063e4 b _handler
00406004 b _has_cctor
004063f0 B _hmod_libgcc
00000000 A _InitializeCriticalSection@4
00406078 b _initialized
004063c0 b _key_dtor_list
00000000 A _LeaveCriticalSection@4
00407808 I _libgcc_s_dw2_1_dll_iname
0040793c I _libstdc___6_dll_iname
00000000 A _LoadLibraryA@4
00402850 T _main
004014e0 T _mainCRTStartup
0040600c b _mainret
00402740 T _malloc
00406008 b _managedapp
00401b70 t _mark_section_writable
00406070 b _maxSections
00402750 T _memcpy
00406058 B _mingw_app_type
00402720 t _mingw_get_invalid_parameter_handler
00406054 B _mingw_initcharmax
0040604c B _mingw_initltsdrot_force
00406048 B _mingw_initltsdyn_force
00406044 B _mingw_initltssuo_force
00401810 T _mingw_onexit
00408010 D _mingw_pcinit
00408004 D _mingw_pcppinit
00402730 t _mingw_set_invalid_parameter_handler
004017e0 t _my_lconv_init
00406020 b _obj
00403014 d _p.59329
0040603c b _partOne
00000000 A _partOne
00401010 t _pre_c_init
00401130 t _pre_cpp_init
00000000 A _QueryPerformanceCounter@4
004028f0 t _register_frame_ctor
00000000 A _SetUnhandledExceptionFilter@4
00402790 T _signal
00000000 A _Sleep@4
00406000 b _startinfo
00402748 T _strlen
004027d0 T _strncmp
00406068 b _stUserMathErr
00000000 A _TerminateProcess@8
00406074 b _the_secs
00000000 A _TlsGetValue@4
00000000 A _UnhandledExceptionFilter@4
004027b0 T _vfprintf
00000000 A _VirtualProtect@16
00000000 A _VirtualQuery@12
0040606c b _was_init.60615
004014c0 T _WinMainCRTStartup
004071fc i fthunk
0040719c i fthunk
00407078 i hname
004070d8 i hname

Doesn't it seem like this is somehow too much for a program that doesn't export anything in the spirit of a DLL?

At the same time, we will remember the program size: 52,105 bytes.

Now let's change the CMakeList.txt:

add_executable(example2 main.cpp other.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto -s")

We added the -s flag and changed the name of the executable file. Now after building the program, nm outputs the following:

nm: example2.exe: no symbols

That is, the program does not need any of the 1,001 above-mentioned names to work!

What with volume? 16,896 bytes. That is, we were able to painlessly reduce the size of the program by three times! And this is for two simple object files. When the project becomes more complex, the share of names from the size of the executable file will be even greater.

By the way, have you noticed that most of the characters are taken up by the names of the sections? So, this is a consequence of the difference in the formats of object (ELF) and executable (PE EXE) files when building under MinGW, which does not allow you to combine these names and use them for their intended purpose. The name table also contains a large number of characters from the standard library object files.


When writing this answer, the following software versions were used:

  • CMake 3.4.0,
  • GCC 4.9.2 (i686-posix-dwarf-rev1, Built by MinGW-W64 project),
  • GNU nm (GNU Binutils) 2.24.
 5
Author: Arhadthedev, 2017-05-21 08:47:48