<div dir="ltr"><div style>Hi cfe-dev!</div><div><br></div>Consider the following code:<div><div>$ cat str.cc</div><div>extern const char* const AAA;</div><div>extern const char* const BBB;</div><div>const char* const AAA = "aaa";<br>
</div><div>const char* const BBB = AAA;</div><div><br></div><div style>"g++ -O0" and "clang++ -O0" behave differently: gcc emits both AAA and BBB as</div><div style>linker-initialized constants, while Clang initializes BBB during static initialization:</div>
<div style>$ g++ -O0 -c str.cc -o a.o ; objdump -d a.o<br></div><div style><div>a.o: file format elf64-x86-64</div><div><div>$ ./bin/clang++ -O0 -c str.cc -o a.o ; objdump -d a.o</div><div>a.o: file format elf64-x86-64<br>
</div><div>Disassembly of section .text.startup:<br></div><div>0000000000000000 <__cxx_global_var_init>:<br></div><div> 0:<span class="" style="white-space:pre"> </span>55 <span class="" style="white-space:pre"> </span>push %rbp</div>
<div> 1:<span class="" style="white-space:pre"> </span>48 89 e5 <span class="" style="white-space:pre"> </span>mov %rsp,%rbp</div><div> 4:<span class="" style="white-space:pre"> </span>48 8b 04 25 00 00 00 <span class="" style="white-space:pre"> </span>mov 0x0,%rax</div>
<div> b:<span class="" style="white-space:pre"> </span>00 </div><div> c:<span class="" style="white-space:pre"> </span>48 89 04 25 00 00 00 <span class="" style="white-space:pre"> </span>mov %rax,0x0</div><div> 13:<span class="" style="white-space:pre"> </span>00 </div>
<div> 14:<span class="" style="white-space:pre"> </span>5d <span class="" style="white-space:pre"> </span>pop %rbp</div><div> 15:<span class="" style="white-space:pre"> </span>c3 <span class="" style="white-space:pre"> </span>retq </div>
<div> 16:<span class="" style="white-space:pre"> </span>66 2e 0f 1f 84 00 00 <span class="" style="white-space:pre"> </span>nopw %cs:0x0(%rax,%rax,1)</div><div> 1d:<span class="" style="white-space:pre"> </span>00 00 00 </div>
<div>0000000000000020 <_GLOBAL__I_a>:<br></div><div> 20:<span class="" style="white-space:pre"> </span>55 <span class="" style="white-space:pre"> </span>push %rbp</div><div> 21:<span class="" style="white-space:pre"> </span>48 89 e5 <span class="" style="white-space:pre"> </span>mov %rsp,%rbp</div>
<div> 24:<span class="" style="white-space:pre"> </span>e8 d7 ff ff ff <span class="" style="white-space:pre"> </span>callq 0 <__cxx_global_var_init></div><div> 29:<span class="" style="white-space:pre"> </span>5d <span class="" style="white-space:pre"> </span>pop %rbp</div>
<div> 2a:<span class="" style="white-space:pre"> </span>c3 <span class="" style="white-space:pre"> </span>retq</div></div><div><br></div><div><br></div><div style>Is Clang correct here? I imagine that user who wrote the code might expect the</div>
<div style>"constant strings" to go in .rodata and be safely accessible during static initialization.</div><div><br></div></div><div>-- <br><div>Alexey Samsonov, MSK</div>
</div></div></div>