Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gcc

elf

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?

like image 495
Daniel Avatar asked Jul 27 '18 05:07

Daniel


1 Answers

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.

like image 177
Florian Weimer Avatar answered Sep 19 '22 20:09

Florian Weimer