gcclinkerelfcortex-m

Appending to an ELF file - Cortex M / GCC


I an generating an ELF file for a Cortex M MCU using GCC. I want to know if there is a way of accomplishing the following. I wish to incorporate a new function into the ELF file at a later point in time. The normal way would be to add the source of the new function to the project and rebuild. Is it however possible to simply convert the new function to an object file and add it to the ELF file? The new function will refer functions that are already part of the ELF meaning some sort of linking will also be necessary.

Is something like that possible?


Solution

  • Is something like that possible?

    Everything is possible, given enough effort.

    In practice, what you are asking for is quite hard in the general case (impossible for all practical reasons), but may a bit easier if your original ELF file has been laid out with sufficient gaps between sections, and if your new function is very small.

    Update:

    I am generating the original ELF myself. ... I have full freedom to place things the way I want.

    That certainly makes the problem much much easier.

    1. Estimate the size of your future function, add a reasonable margin.
    2. Put a "stub" function of the required size (fill it with NOPs).
    3. Later, overwrite the NOP instructions with your (relocated) actual code.