Re: Why are switch statement tables in section .rodata which gets copied to RAM with avr-gcc?

Matthijs Kooijman <[email protected]> Tue, 31 Mar 2020 16:59:37 +0200
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <[email protected]>
Hi Peter,

> It turns out, that avr-gcc or the linker for the Arduino processor that I
> have access to, generate code to copy .rodata to .data in RAM. This is bad,
> and may have been the reason for these PROGMEM tables.
.rodata is copied to RAM, because flash and RAM are not in the same
address space on AVR. This means that, without special treatment, a
pointer to read-only data will be dereferenced the same as a pointer to
read-write data, reading from RAM, so the compiler has no choice but to
copy rodata to RAM.

To circumvent that, data can be marked with PROGMEM, to instruct the
compiler to leave it it flash, but then it is up to the programmer to
distinguish such pointers and use pgm_read_... functions to read them
from flash instead of RAM.

Having said that: Compiler-generated switch tables (just like
vtables, btw), *could* very well be kept in flash by putting them in an
appropriate section and generating the right code for loading data from
them, but I suspect this simply was not implemented (since this is a
rather specialcase for avr, on most other platforms putting data in
.rodata is sufficient to keep it in flash).

Does this answer your question?

Gr.

Matthijs
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEMyF3AetYrDfGJ9el6ZMxy91tJYwFAl6DWtMACgkQ6ZMxy91t
JYwRTw//XU6eYiDKGKTNallAaNpFmZyWDB+AfZwIZ6o8S9/y+IdajDFvQ2Iyf++6
Bou9sF/7nHlKGJsLuxx886BbHQu+D+gPZQ8R0aGkkNFlgiJnw5yCkQZ647veMg8u
wdb+QhsMCii/H0GI6QZbhg3Ph0633FDPiDzN9G2oqlsfIYhzgmxjb4Epr8gn4JYF
ODGHMwrnuPhS7CIsEOpT5wR+1dRJhJv6RIUNUHNiz6ArKnBiE5PAQhJ6/nhj/zm/
+vrkLWamDvJVeASJTCVslEqDZbWpFh0aGVMudE+E7faV5rGmFoQxHtmurRxp/xwu
2aI1bJ2rvP3cnO4ymEjQ51BzW956Pd1NGcF2ekqjdugK3Yy5Yyg7wls0NCB4ng39
jHi6864zyh2FUqd20B2ceht6qNbyCiLUq3DkCYMNHFA3jp2wDforoZ+x9O/k/4lE
VIxFhimGhOkvT0VjMUF1x3jbRLUCwAUDAl+OcU3B2ZqohG2VeFjx5JORk2ln3p/n
O7iHvatG5eLxDZwD1Gx0ulxncBJbxGHh2XrV/rh1pJAKmwWoXDOwQXUOfHhShaIX
CL7o14hxtO+4Tq8jn4ST1Fgux3mcHGCOZIcbkhr0ogyRRErgEKNu7dB/EsfQrT3L
kUEKO2BCXZ+ziJdkD395YltS9NMJt7QrGEszx/EDQG8WszU3gwA=
=ClHY
-----END PGP SIGNATURE-----