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

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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



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".


"foo bar"


"bar"


"foo bar"


"bar"


4


"foo bar"


"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?





Try -fno-merge-constants?
– n.m.
3 hours ago


-fno-merge-constants





have not tried that yet, but even if it worked, we would still like to merge identical constants of various kinds, but not merge string constants that are not identical but have the same suffix
– Daniel
2 hours ago





I don't think you have an option there if I recall correctly. You will have one reference to a literal, even if you make use of it more than once (e.g. use "foo" multiple times will only result in one literal) -- the option prevents the use of the optimization you describe.
– David C. Rankin
2 hours ago




"foo"





I think you should explain exactly what problem this is causing you. It can never cause any problem for the running code (as long as it doesn't have undefined behavior) and for whatever post-processing you're doing, there might be a better idea.
– Felix Palmen
1 hour ago




1 Answer
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:



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).


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.


ld.gold


-O2






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

c 2ABX6,ic6l6OlaXskRbE04DoLJNrVmodzEg8MAY4XCjX pnwc4S8H TomBSkij
QOlIdt7EPuT pZ,8enESCFODkAay gZFK Yk 5G0Rp8c,obtvkQUhhKb,821cB

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template