I was wondering if there is way to remove ALL the unused functions listed in the map file for an embedded project developed in C and using the IAR embedded workbench for ARM IDE, which uses its own compiler and linker:
IAR C/C++ Compiler for ARM 8.30
IAR ELF Linker for ARM 8.30
IAR Assembler for ARM 8.30
I have noticed that not all the functions listed in the map file are the used functions that actually are used at run time, is there any optimization way to remove all unused functions?
For example a third library is used in the project and FuncA() is part of that inside which there might be a switch case and for every case a different static function in called, lets say FuncA1(), FuncA2(), ... FuncAn(). We would enter each case based on the code and usage of the FuncA() so it it obvious that not all of the FuncA1(), FuncA2(), ... FuncAn() functions would be called in the project, however, all of them are listed in the map file.
Is it possible to remove such functions from the map file? If yes how?
Removal of unused functions with external linkage is necessarily a function of the linker rather then the compiler. However a linker is not required to support that and any support is toolchain dependent and may require specific link-time optimisation switches to be applied.
Unused functions with static linkage could be removed by the compiler.
If the functions
FuncAx()have static linkage, but are explicitly referenced in the functionFuncA()with external linkage, then neither the compiler nor the linker should be able to remove the functions, because the compiler has no a-priori knowledge of howFuncA()will be called, and the linker has no reference to functions with static linkage, or necessarily understanding of the language semantics that would make it apparent the switch cases in question are not invoked.It is possible I guess that a sophisticated toolchain with a C language aware linker and with link-time whole program optimisation might remove dead code more aggressively, but that is certainly tool-chain specific. Most linkers are source language agnostic and merely resolve symbols in the object code and in some case remove code to which no link has been made.