This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Remove unused modules?

Hi, I am in the process of creating a library with common code (initialization of peripheral units etc). In most cases, some modules are unused, but they are still included in the .out file. Is it possible to instruct the linker to only include code that is used somewhere?

  • Hi,

    The linker usually works this way: it doesn't include not referenced object file and sections.

    So to disregard the unused code it must be inside a single compilation units (a single c file) composed only of unused code. A reference to only one routine inside the complation unit is enought to force the linker to include the whole text section.

    Wich CGT are you using?

  • That's what I thought, so I was surprised when I saw non-used modules in my project. Currently, I use CGT v6.1.0.

  • Are you using C++? Do you have some global constructor in the unused modules?

    Mybe there is some "hidden reference", such as a variable in a used module initialized with a pointer to a function from an unused one?

  • I use C. I would be surprised if there was any hidden references, as this is a new project which just includes the files from my library. Maybe I am interpreting the .map file wrong? It indicates that functions from the various objects are present.

  • Suppose one library module has two functions in it: one is named called_a_lot() and the other is named never_called().  Further, suppose both functions are in the same .text section in that module.  The main program only calls called_a_lot(), and not never_called().  The linker brings this module in from the library because of called_a_lot().  The function never_called() comes in too, because it is in the same section.

    I think building the library with --gen_func_subsections will help.  That option puts each function in its own subsection.  I can't recall if the linker removes the unneeded never_called() subsection after bringing in the library module.

    Thanks and regards,

    -George

  • Thanks for your help! I understand that if a part of a module is used, the whole module will be included. However, my problem is that unused modules also are included.

    To verify this, I created a new .c file, and wrote a large function (setting the same volatile variable over and over). This caused my build to fail, as the program now was too big for any available section.

    Note that I don't have a proper library, it's just a set of source files that can be included in different projects.

  • If none of the functions or data in a library module is needed by the main program, then that library module should not be brought in by the linker.  If you have a case of the linker doing so, then please submit a test case which allows us to reproduce the problem.  Sending the entire CCS project is the most convenient method.  If that is not possible for some reason, there are other, less convenient, methods.  But you do need to submit everything seen by the linker.

    Thanks and regards,

    -George

  • I assume you are using COFFABI.  In COFFABI, every section from every file you explicitly specify on the command line will be included in the executable by default.  If you were to place this unused function in a library, and then link against the library, the linker would not include it in the executable.

  • George: OK, if we don't resolve this issue soon I will find out if I can send you my project.

    Archaeologist: Output format is set to "legacy COFF" and cannot be changed. If I understand correctly, COFF is the only supported ABI for C2000?

    In summary, based on Archaeologist's information, this behavior is expected. Based on the other information in this thread, the behavior is unexpected. I hope we can clarify what is correct :)

  • Yes, C2000 only supports COFFABI.

    There is no contradiction, just different cases.  You mentioned in your first post that you were "creating a library".  Alberto and George assumed that your object file was already in your library.  In that case, their posts accurately describe the expected behavior.  Later you say "Note that I don't have a proper library;" under that assumption, the expected behavior is different.

  • Ah, that's where I got confused yes. Thanks for clearing that up.

    In conclusion, if I want to only include used parts of code, a library is needed.