llvm-mos / llvm-mos-sdk

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

Providing programmatic access to program length #236

Open TheHans255 opened 10 months ago

TheHans255 commented 10 months ago

In my platform linker files, how can I get access to the length of the program, or a program section, in order to write it into the final binary?

I'm continuing work on my Apple //e port, and one thing I'll need to do to get HIRES graphics working with ProDOS programs is write a short ASM boot routine that copies the program to a different location in memory, since ProDOS loads programs on top of the HIRES memory pages by default. Hence, I'll need to write the length of the program somewhere in the binary so that this routine knows exactly what range to copy.

It's also possible that I'll need to write a new, platform-specific version of malloc that provides better access to underlying memory pages (since ProDOS requires that its 1024-byte OS buffers for opening files are page-aligned) and makes better use of the available memory space both before and after the program (in particular, the 6K of memory between the text buffer and the HIRES pages). Hence, I'll need access to the program length there as well so that malloc can properly mark the program area as off-limits.

Note that netiher of these are urgent - I have ways of working around the problems a new malloc would solve, and I can write the ASM routine to simply copy the maximum possible range, including empty parts of RAM.

asiekierka commented 10 months ago

Usually, you do this by defining some symbols:

    __program_start = .;
    /* ... program sections ...*/
    __program_end = .;
    __program_length = __program_end - __program_start;

Then, in code:

extern char __program_start, __program_end, __program_length;
/* ... */
printf("program: %04X - %04X (%d bytes)\n", (uint16_t) &__program_end, (uint16_t) &__program_start, (uint16_t) &__program_length);