Search code examples
embeddedld

How to assign address to external symbols from another library without including it in the output of ld?


Here the context :

I'm developing some kind of micro OS for arduino ( SAMD21 MCU ). I would like the boot loader to load the OS (easy part, the official one works, and the OS is linked with a "with-bootloader.ld" script). Then, to load an application from the OS in a similar way the bootloader loads the OS. (easy for the OS part)

Doing so, the OS would not have to be re-programmed and only the application would change.

Here the question :

Since the OS would include most of arduino code, is it possible to link the app to the OS without including the OS in the binary? I mean at the linking step of the app, to replace any references to symbols from the OS by their actual value without re-including the OS code? What would you add to the linker script ?

The goal is to just re-programm the app code at a predefined address without touching to the OS. (the reprogramming stop would be done from the OS code, self-programming the mcu like an vanilla bootloader)


Solution

  • You have at least these options, all seen before:

    1. Provide a fixed interface to all of the OS functions. This might be a vector or any other well-to-remeber address. Each call uses a function number as an index.

      Define the address of this entry point in your app's linker script.

      Declare all OS functions as calls to this entry point with the appropriate function number in a header file.

      Similar alternative: Ask this single function about the address of the indexed OS function.

    2. Provide a jump table into all OS functions. Place it at a fixed address.

      Define the addresses of these jumps in your app's linker script.

      Declare all OS functions in a header file.

    3. Write a script that extracts the addresses of all OS functions out of the map file of the OS.

      Do some research how to include these addresses in the app's linker script. Extend your script to provide the correct syntax. You might even use the script to generate the app's linker script.

      Declare all OS functions in a header file.

    References in the app's sources will be resolved by the definitions in its linker script.