bebbo / gcc

Bebbo's gcc-6-branch for m68k-amigaos
GNU General Public License v2.0
33 stars 11 forks source link

Variables go to wrong section depending on their initialization order #196

Closed mankeli closed 1 year ago

mankeli commented 1 year ago

So two chip memory arrays. Initialized one should go to data section, non-initialized to bss. But if the initialized one is defined first, both of them go to data section. Repro case below, compile with gcc test.c -o test.exe -O3

#include <stdio.h>

#if 0 // hello goes to BSS, mello to DATA
__chip uint16_t hello[65536*16];
__chip uint16_t mello[] = { 1, 2, 3, 4 };
#else // both go to DATA
__chip uint16_t mello[] = { 1, 2, 3, 4 };
__chip uint16_t hello[65536*16];
#endif

int main(void)
{
  for (int i = 0; i < 65536; i++)
    hello[i] = i+999;
        printf("hello %d %p - %d %p\n", hello[0], hello, mello[0], mello);
        return 0;
}
mheyer32 commented 1 year ago

I think this is an optimization. The Amiga data Hunk has a way to say "I have actual data until size X, the rest of the data hunk is filled with zeros". The zeros are not stored in the file. This is as good as a BSS section.

mankeli commented 1 year ago

well, one exe is 2.1MB and the other is 62kB, so even if it's an optimization, it doesn't work. And if there's multiple arrays, the unintialized ones don't seem to be at the end of the data hunk.

I was wondering earlier if the __chip section attribute interferes with the section selection, but now that I found out that the initialization order matters, it seems more unlikely explanation.

bebbo commented 1 year ago

The programs do run, if you have more than 2MB Chip RAM. grafik

There is no direct .chipbss section atm, you only have __chip. The selection is done via hooks which select the section for you - and there is the problem, which will be solved eventually.

=> put all the uninitialized stuff at the end at the start and you get small executables.

mankeli commented 1 year ago

If you have time, could you enlighten me a bit about where these section selection hooks are defined / how the selection process happens? I could take a look at it.

bebbo commented 1 year ago

The hook amiga_named_section now clears the last section on chip, fast or far. This may emit some superfluous section directives, but the switch to .bsschip is now better handled.

mankeli commented 1 year ago

Nice! This seems to produce bit-to-bit exactly same binaries with both code paths. Thank you :)