I'm currently writing a custom Ethernet bootloader for a STM32F103C8T6 with a WizNet W5500 Ethernet controller. I want to keep the bootloader code as separate as possible from user code.
The problem is that the WizNet library occupies about 25% of all available ROM (only 64k), so I don't want to include the library both in the bootloader and in the user code but instead include it in the bootloader only and access the library from user code.
My current idea for an approach would be:
In the bootloader:
- Include the necessary WizNet .h files (and do Ethernet init and bootloader foo)
- Use a struct/array to store all already used libarary functions as pointers at a predefined location in RAM
In the user code:
- Still include the WizNet .h files, but use the stored function pointers to "overwrite" the default library function calls, or tell the linker it doesn't have to link them in to save ROM space. (That's the thing I don't know how to do exactly)
But there are some open requirements I don't know how to solve:
- Not all functions of the library are called in the bootloader, so they won't get linked in.
So in the user code only the already existing functions should be called via the function pointers.
If a library function is called in user corde that was not needed in the bootloader, the compiler/linker has to put that function into the user code. - When the library gets compiled inside the user code, it should internally also use the function pointers where possible.
- Ideally the names of the functions should stay the same, so the user code stays compatible regardless of using the existing functions via function pointer, or using the library stand-alone.
I don't understand C linking enough to make that work. Maybe my approach is plain wrong and there is a better way, or maybe it's not possible at all. I hope someone is able to help me here and that I could describe my issue clearly enough.
Some additional constraints / information:
- The user code should always be able to find the function pointers, even if the bootloader version changes (it gets recompiled and the library function addresses change)
- I don't want to simply include all the library functions, because that would waste ROM aswell.
- Both bootloader and user code should reside in the ROM (e.g. for fallback to bootloader, if a fault occurs in user code)
- The bootloader can update the user code, but leave itself untouched.
- (Optional) I'd like to not modify the original WizNet library too much.