Forcing certain compiler-generated variables into specific ELF sections (with gcc)

痴心易碎 提交于 2019-12-02 19:34:29

One approach, which may be hackier than you'd like, would be to interpose a script to change the section names between compilation and assembly. Eg:

gcc -fdata-sections -S -o test.s test.c
sed 's/^\t.section\t\.rodata\.__func__\.[0-9]*/\t.section .rom_data/' -i test.s
gcc -c test.s

You could also try writing a clang transformation pass to place the __func__ declarations in a section of your choosing, or writing an object file manipulation program using libbfd.

It looks like I answered my own question at the end with the -fdata-sections business, I just didn't understand the GNU Linker enough to see it. I don't actually need the globbing with exclusion as long as I specify the *(.rodata.__func__*) bit first. Any sections that glob matches will get marked as used, so a later glob for *(.rodata*) won't double count them and copy them somewhere else. I don't need to tag them with ROM_STR at all. Cool!

It's important to note that -fdata-sections does actually put each function string into its own .rodata.__func__.1234 section (I'm not sure what pattern the numbers follow). I don't know if anonymous strings also get their own sections; if so, I could use the same linker tricks to capture all of the anonymous strings instead of the ROM_STR section attribute macro, but it would probably be a bad idea. ROM_STR gets used in the LOG macro, so it's guaranteed only to be applied to logging format strings. If I forced all anonymous strings into ROM with a linker trick, that would include normal message data, and I would pay a runtime performance penalty to access it from flash. So I don't know if it's even possible, but its advisability would depend on your specific system requirements.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!