https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192
--- Comment #15 from Matt Whitlock <gcc at mattwhitlock dot name> ---
(In reply to Jakub Jelinek from comment #14)
> This doesn't really look like a good idea to me. Instead, perhaps ld's
> --gc-sections or new special option should just remove unused string
> literals from mergeable sections.
I believe (I've read, but I haven't verified) that Gold already does this.
> With your patch, I bet you lose e.g. all tail merging.
Tail merging still works fine.
> Consider:
> const char *used1 () { return "foo bar baz blah blah"; }
> in one TU and
> const char *used2 () { return "bar baz blah blah"; }
> in another.
Okay, I'll use your example.
$ echo 'const char *used1 () { return "foo bar baz blah blah"; }' > tu1.c
$ echo 'const char *used2 () { return "bar baz blah blah"; }' > tu2.c
$ cat > main.c <<EOF
extern const char * used1(), * used2();
int main() { puts(used1()); puts(used2()); return 0; }
EOF
$ gcc -c -fdata-sections -fmerge-constants -o tu1.o tu1.c
$ gcc -c -fdata-sections -fmerge-constants -o tu2.o tu2.c
$ gcc -c -fdata-sections -fmerge-constants -o main.o main.c
$ objdump -s tu1.o tu2.o | fgrep -A2 .rodata
Contents of section .rodata.str1.1.b4d3fd7d:
0000 666f6f20 62617220 62617a20 626c6168 foo bar baz blah
0010 20626c61 6800 blah.
--
Contents of section .rodata.str1.1.a07ea0c2:
0000 62617220 62617a20 626c6168 20626c61 bar baz blah bla
0010 6800 h.
$ gcc -Wl,--gc-sections -o proof main.o tu1.o tu2.o
$ ./proof
foo bar baz blah blah
bar baz blah blah
$ objdump -s proof | fgrep -A2 .rodata
Contents of section .rodata:
40061d 666f6f20 62617220 62617a20 626c6168 foo bar baz blah
40062d 20626c61 6800 blah.
As you can see, tail merging across translation units works fine.