0

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?

NEO
  • 1
  • 3
  • It could be that the library functions call them. It could be that they are in an object file where one of the functions is used and some or all of the others are not. – Jonathan Leffler Jan 17 '21 at 17:33
  • @JonathanLeffler thanks, none of the library functions call it. So, you are saying that all of the functions belong to an object file would be listed in the map file regarless of the fact that which one is actually called in the project? – NEO Jan 17 '21 at 17:44
  • @NEO I done really follow your argument in the second paragraph. Can you make it clearer or provide a concrete example of code and its associated map file? – Clifford Jan 17 '21 at 17:56
  • It is toolchain dependent rather than a C language issue - what is your toolchain? – Clifford Jan 17 '21 at 18:46
  • IAR is a company, not a toolchain. They make toolchains for more than one architecture. That said I am not familiar with IAR. However re-reading your argument (which is still unclear) I think your assumption that these functions can be removed is flawed. I'll update my answer, but I may misunderstand you argument - you did not include a code example. – Clifford Jan 17 '21 at 19:21
  • The toolchain information should be in the question, not a comment. You may get IAR specific answers in that case. – Clifford Jan 17 '21 at 19:30
  • Your question is really unclear in that case - I think I will give up trying to understand it. You seem to be saying that what you expect to be happening _is_ happening, then asking _why_? Most questions ask that when the _unexpected_ happens. I did not mean "argument" in the sense of a "quarrel" BTW, rather simply a "proposition" - definition 3 of 6 at https://www.dictionary.com/browse/argument?s=t – Clifford Jan 17 '21 at 19:44
  • I doubt IAR's AVR toolchain behaves identically to their AVR toolchain, so specifying the toolchain including its version number is relevant unless you want the kind of general answer I have already given. Also IAR is not even an IDE. IAR Embedded Workbench is an IDE. – Clifford Jan 20 '21 at 08:00
  • Then IAR is not a company IAR Systems is! Let’s not “argue” on the obvious matters. you are right toolchian information should be in the question, so I edited my question and removed unnecessary comments. – NEO Jan 20 '21 at 14:54

1 Answers1

2

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.

We could enter each case based on the code and the function that calls FuncA() so it it obvious that not all of the FuncA1(), FuncA2(), ... FuncAn() functions would be called

If the functions FuncAx() have static linkage, but are explicitly referenced in the function FuncA() 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 how FuncA() 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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • those (FuncA1()...FuncAn) are static functions called in an external function that is FuncA(), funcA() is indeed called in the project. – NEO Jan 17 '21 at 19:02
  • @NEO Again it may be tool chain specific. Functions with static linkage can be removed by the compiler rather than the linker. – Clifford Jan 17 '21 at 19:15
  • This is really vendor specific. e.g. DIAB compiler allows to place all functions and variables into separate ELF object sections, so the linker can then later remove "unused sections". In your IAR, not sure, which optimizations are there and if the compiler can really detect in that switch-case, which paths (and their function calls) are never used. But maybe the --mfc (MultiFileCompilation into single object can help the compiler here? – kesselhaus Jan 20 '21 at 01:48
  • @kesselhaus Your comment should be attached to the question rather than this answer, or you should post an answer. I already said it was toolchain specific. The toolchain is not mentioned in the question, and this is necessarily a general used answer. – Clifford Jan 20 '21 at 07:56
  • @Clifford: Sorry, I thought it was to short for an answer, and it fit better as additional info to your answer, than to add to the comments about if IAR is a toolchain or a company. – kesselhaus Jan 20 '21 at 21:52