Reducing the size of .rodata

c++, embedded, linker

Solution

I think your problem may not necessarily be in the rodata section as such - it's just that that's the guy that didn't sit down in time at the muscal chairs event. In other words, rodata in itself isn't what's too large, but the WHOLE IMAGE is too large to fit. The solution would be to look at your entire code, data and rodata in your system, and see if any of it stands out.

Removing unnecessary code (or strings) in general would be the key point here. If there is nothing that can be removed, then you'll have to find a different way to solve the problem. One way may be to compress the code and data, and uncompress it into RAM (assuming there's significantly more RAM than ROM on the target system). This is not an unheard of problem, but it's never really that easy to fix - unless someone has done some really bad coding and added some hundreds of kilobytes of code. If you have a version control system, and you know of a version that does fit, it may be a good thing to check exactly how much spare space there is - if it's suddenly grown a lot, check if someone has added some massive static data structures or some such.

Problem

I'm crosscompiling C++ in Gentoo for an ARM Cortex M3 (Maple Mini), but seem to have hit a roof with the memory resources when linking the elf-file ``` /usr/libexec/gcc/arm-none-eabi/ld: build/maple_mini.elf section `.rodata' will not fit in region `rom' /usr/libexec/gcc/arm-none-eabi/ld: region `rom' overflowed by 1508 bytes ``` This post is a question about how to reduce the size of the contents of .rodata, to be able to complete the linking of the elf-file. I have stripped the included code, and am compiling with the following relevant options. ``` CXXFLAGS = -fno-rtti -fno-exceptions -Os -fdata-sections -ffunction-sections -Wl,-gc-sections ... LDFLAGS = -Wl,-gc-sections -fno-exceptions -fno-rtti ... ``` Still, the .rodata in the map-file (with which I am quite unfamiliar) contains what seems to be some kind of type information for each of the classes in the program. Some map-file excerpts (note. MPU6050 is a SuperSensor<> which is a Sensor<>, in a C++ sense) ``` 0x000000000801d6c0 0x28 .../libsyrup.a(MPU6050.o) 0x000000000801d6c0 _ZTVN5syrup6SensorILi6EEE .rodata._ZTVN5syrup11SuperSensorILi6EEE 0x000000000801d6e8 0x28 .../libsyrup.a(MPU6050.o) 0x000000000801d6e8 _ZTVN5syrup11SuperSensorILi6EEE .rodata._ZTVN5syrup7MPU6050E 0x000000000801d710 0x28 .../libsyrup.a(MPU6050.o) 0x000000000801d710 _ZTVN5syrup7MPU6050E .rodata._ZTVN5syrup6SensorILi1EEE 0x000000000801d738 0x28 .../libsyrup.a(MS5611.o) 0x000000000801d738 _ZTVN5syrup6SensorILi1EEE .rodata._ZTVN5syrup11SuperSensorILi1EEE 0x000000000801d760 0x28 .../libsyrup.a(MS5611.o) 0x000000000801d760 _ZTVN5syrup11SuperSensorILi1EEE ... 0x000000000801ee24 0x6f3 .../libstdc++.a(cp-demangle.o) 0x730 (size before relaxing) *fill* 0x000000000801f517 0x1 .rodata 0x000000000801f518 0x14 .../libgcc.a(unwind-arm.o) .rodata 0x000000000801f52c 0x23c .../libc.a(lib_a-strerror.o) .rodata.str1.4 0x000000000801f768 0x635 .../libc.a(lib_a-strerror.o) 0x63c (size before relaxing) ``` So, lib_a-strerror.o and cp-demangle.o seems to be what takes up most space, although I guess these are quite vital. So, my question is, what further steps can I take to reduce (or restructure the code) the .rodata section, and what exactly is stored there? Any suggestions are welcome! I am fairly new to the deeper workings of compilation and linking, but learning.

Original source