Bootloader: Program in flash memory does not start at address 0x00
Hello,
I am using MPLAB X v5.35 and XC8 v2.10 with C99. My controller is a PIC18F25K20.
I am building a bootloader and came across the issue, that the generated code does not start at address 0x00, but at address 0x600, see the following .hex file usage map of a minimal example:
00000000: 1111----1111------------11111111--------------------------------
00000600: 11111111111111111111111-1111111111111111111111111111111111111111 Program starts at 0x600
00000640: 1111111111111111111111111111------------------------------------
00200000: 11111111-------------------------------------------------------- LocID
00300000: 11111111111111-------------------------------------------------- Config
I do not have a code-offset activated. When checking my other projects I saw, that for the larger ones that use ~90% flash, the program starts at 0x00. A smaller projekt at ~10% flash usage has a bit of code after 0x00, then nothing and then resumed at 0x600. The larger ones do not have a gap around 0x600.
Then I generated a bootloader project with MCC et voilà, the code is about 5% and starts at 0x00. That is what I also need. If anyone wants to reproduce: Start a new project, open MCC, select the bootloader generator, generate the minimal amount of necessary modules and generate, then compile. Works straight away. I did not alter any default values.
I based my own bootloader on the MCC bootloader, so a lot of things are actually the same.
In order to eliminate the project config from this problem, I created a completely fresh project and altered nothing on the config. I copied my code over, compiled, but the address starts at 0x600 again. Then I deleted my code and copied the generated code over, the address starts again at 0x00. So it is not a problem in the project config. It should be something in the source code itself.
I also use the default config bytes at address 0x00300000, meaning that I deleted the corresponding header file, to eliminate this from the problem.
Then I noticed the following: If I comment out this part in the generated bootloader, then the address does not start at 0x00 anymore, but at the very end of the flash memory.
asm ("psect intcode,global,reloc=2,class=CODE,delta=1");
asm ("GOTO " str(NEW_INTERRUPT_VECTOR_HIGH));
asm ("psect intcodelo,global,reloc=2,class=CODE,delta=1");
asm ("GOTO " str(NEW_INTERRUPT_VECTOR_LOW));
However, this code is identical in my project and the generated one. Changing the address of the new vectors does not change anything.
I am clueless on how the compiler/linker decides where to place the program in the flash memory.
What do I have to do to make sure, that the program starts at address 0x00?
If more information is needed, please ask. Thanks a lot in advance and best regards.
Update:
I looked at the listing file and saw the following:
...
95 psect smallconst
96 000600 __psmallconst:
97 opt callstack 0
98 000600 00 db 0
99 000601 STR_1:
100 000601 0A db 10
...
psmallconst has something to do with string literals. I removed my only source of string literals (debug) from the project, then my program starts at 0x00. But attention: simply not including is not enough! I have no idea why, but even if the files are completely not included, string literals are generated in the listing file. You need to exclude those files from the project.
I added a random string to the generated project and then also the program does not start at 0x00 anymore, but at 0x600.
I am still not sure how to handle this properly.
post edited by erding - 2021/03/03 09:13:59