How to stop GCC from combining string literals that share the same suffix?

半城伤御伤魂 提交于 2019-12-01 18:29:04

问题


GCC seems to do an optimization where it combines string literals that share the same suffix.

For example a C program that contains the two string literals "foo bar" and "bar" can end up in final ELF form (1) the string table changed to have a single string literal "foo bar" and (2) the program changed so that any pointer to "bar" is converted to now point 4 characters into the string "foo bar". Note that, from the point of view of a C program reading only forward, the second literal will still look like "bar".

While I think this is a clever scheme for compressing a collection of strings without adding complexity to the format of an ELF file, for esoteric reasons it is also causing me problems (while post-processing ELF files and doing various analyses on them). How do I turn off this feature of GCC?


回答1:


The ELF string table is built by the assembler and link editor, so it is not a GCC matter, but rather binutils-related. String table merging was introduced in binutils 2.26:

  • Inefficient .strtab implementation
  • Use strtab with GC and suffix merging for .strtab

This caused occasional issues, for example when building powerpc64 kernel modules.

Unfortunately, I'm not aware of a way to disable string table merging in the BFD-based linker (ld.bfd).

However, gold (ld.gold, also part of binutils) only performs string table merging when optimizing (at -O2 and higher; be aware that this is a linker flag). If the binutils assembler merged the string table entries, it will duplicate them again. This means that if your project is compatible with the gold linker, you can use it to address this problem.



来源:https://stackoverflow.com/questions/51551068/how-to-stop-gcc-from-combining-string-literals-that-share-the-same-suffix

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