llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
270 stars 56 forks source link

Investigate making `text.fixed` for libcalls #275

Open mysterymath opened 9 months ago

mysterymath commented 9 months ago

Presently, the SDK maintains an invariant that all default C readonly sections are always visible in memory, no matter the state of the underlying system. For systems where the whole ROM image of the program is banked as a unit (that is, no portion of it is fixed), this typically requires duplicating the C readonly sections at the same address in each bank. This potentially wastes a ton of space, especially given library functions like printf.

Two requirements justify this invariant: 1) The compiler can insert calls to certain library functions (libcalls). There is no ABI or standard for how or when these will be generated, so there's generally no supported way to prevent the compiler from inserting them. Accordingly, they must always be visibile, since anything written in C may call them. 2) The compiler may rewrite a call from one C library function to another (e.g., printf can become puts). This only requires that C library functions are all equally callable: if any are callable, all are.

Accordingly, we could relax the SDK's invariants by moving the libcalls in the SDK into a special section: .text.fixed and/or .rodata.fixed. These sections would be duplicated across banks, but the main C sections could be placed in a single bank. This would free the other banks for other code and data, while still allowing such code to make libcalls at will.