2

For some reason, when I give these two variables the "used" attribute, the compiler makes them 8 bytes long. Without it, the compiler optimizes them away as I don't use them (yet). Anyone knows why this is? And are any workarounds for this known?

__attribute__ ((used,section(".booter_version"))) static uint32_t BooterVersion = 2;
__attribute__ ((used,section(".hardware_version"))) static uint32_t HardwareVersion = 3;

Linkerscript part with these sections:

 /* booter version number is placed at the second last 4 bytes of program mem for the booter*/
 .BooterVersion _BOOTER_VERSION_ADDRESS :
 {
    KEEP (*(.booter_version))
  } >kseg0_program_mem
 /* hardware version number is placed at the end of program mem for the booter */
 .HardwareVersion _HARDWARE_VERSION_ADDRESS :
 {
   KEEP (*(.hardware_version))
 } >kseg0_program_mem

Image from the memory map after compiling (in program mem, you see the last two lines for these):

enter image description here

Oscar K
  • 185
  • 1
  • 11
  • 3
    What happens if you put them both in the same section? Maybe there's a minimum section size? – Adrian Mole May 03 '22 at 12:38
  • Nope, no difference. 16 long in total.. Weird.. – Oscar K May 03 '22 at 13:03
  • And by the way, If i place the attributes in main.cpp and remove the used, it stays at four, so eight bytes total for the two. – Oscar K May 03 '22 at 13:03
  • What compiler do you use? This is an interesting question, I looked into the XC32 compiler documentation, but interestingly enough I don't see that variables have the ```used``` attribute. From the documentation is seems that only functions do. Which is interesting on it's own, because you could use this attribute with variables, although the user's guide doesn't mention it. Maybe you are using some other toolchain? – Marcell Juhász May 04 '22 at 07:09
  • Hi Marcell, I use the XC32 compiler set to optimisation level 2. I compile, link and flash the device using Microchips own MPLAB IDE. Which is worth a forum post on its own, but anyhow, I am using everything from Microchip, so the compiler, linker and flashtool. I found it a very interesting problem as well, but I dont know the answer.. – Oscar K May 04 '22 at 07:45
  • And some extra info, You got me to the idea to check if i remove the "used" attribute and just make it volatile to get the same result would fix it. Just tried it. Same problem. Only when I place both the variables in main.cpp instead of another .hpp file or whatever, it will compile to 4 bytes.. Very weird. Aligning on 4 in the linker script doesn't make any differences either. – Oscar K May 04 '22 at 07:50
  • It is also interesting,that if you look at the addresses, ```.BooterVersion``` has the address ```0x9d007ff8``` and ```.HardwareVersion``` has the address ```0x9d007ffc```. Since your PIC is probably byte-addressable, this would mean 4 bytes. And for example between ```.ctors``` and ```.dtors```, there is 8 difference between the addresses, and the size is 8 there as well. I noticed this when I tried to reproduce this, got the same addresses but sizes of 4 bytes. Couldn't this somehow be an error in the tool that generated the memory map? – Marcell Juhász May 04 '22 at 14:00
  • Yeah, I generated the map file as well, and for me the addresses are the same but the sizes are 4 bytes. ```.BooterVersion 0x9d007ff8 0x4 4 .HardwareVersion 0x9d007ffc 0x4 4 ``` So it seems to me that your map file generator cannot subtract :D – Marcell Juhász May 04 '22 at 14:09

0 Answers0