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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With