<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 17, 2013 at 1:50 AM, Alexey Samsonov <span dir="ltr"><<a href="mailto:samsonov@google.com" target="_blank">samsonov@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>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>"g++ -O0" and "clang++ -O0" behave differently: gcc emits both AAA and BBB as</div><div>linker-initialized constants, while Clang initializes BBB during static initialization:</div>
<div>$ g++ -O0 -c str.cc -o a.o ; objdump -d a.o<br></div><div><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 style="white-space:pre-wrap"> </span>55 <span style="white-space:pre-wrap"> </span>push %rbp</div>
<div> 1:<span style="white-space:pre-wrap"> </span>48 89 e5 <span style="white-space:pre-wrap"> </span>mov %rsp,%rbp</div><div> 4:<span style="white-space:pre-wrap"> </span>48 8b 04 25 00 00 00 <span style="white-space:pre-wrap"> </span>mov 0x0,%rax</div>
<div> b:<span style="white-space:pre-wrap"> </span>00 </div><div> c:<span style="white-space:pre-wrap"> </span>48 89 04 25 00 00 00 <span style="white-space:pre-wrap"> </span>mov %rax,0x0</div><div> 13:<span style="white-space:pre-wrap"> </span>00 </div>
<div> 14:<span style="white-space:pre-wrap"> </span>5d <span style="white-space:pre-wrap"> </span>pop %rbp</div><div> 15:<span style="white-space:pre-wrap"> </span>c3 <span style="white-space:pre-wrap"> </span>retq </div>
<div> 16:<span style="white-space:pre-wrap"> </span>66 2e 0f 1f 84 00 00 <span style="white-space:pre-wrap"> </span>nopw %cs:0x0(%rax,%rax,1)</div><div> 1d:<span style="white-space:pre-wrap"> </span>00 00 00 </div>
<div>0000000000000020 <_GLOBAL__I_a>:<br></div><div> 20:<span style="white-space:pre-wrap"> </span>55 <span style="white-space:pre-wrap"> </span>push %rbp</div><div> 21:<span style="white-space:pre-wrap"> </span>48 89 e5 <span style="white-space:pre-wrap"> </span>mov %rsp,%rbp</div>
<div> 24:<span style="white-space:pre-wrap"> </span>e8 d7 ff ff ff <span style="white-space:pre-wrap"> </span>callq 0 <__cxx_global_var_init></div><div> 29:<span style="white-space:pre-wrap"> </span>5d <span style="white-space:pre-wrap"> </span>pop %rbp</div>
<div> 2a:<span style="white-space:pre-wrap"> </span>c3 <span style="white-space:pre-wrap"> </span>retq</div></div><div><br></div><div><br></div><div>Is Clang correct here? I imagine that user who wrote the code might expect the</div>
<div>"constant strings" to go in .rodata and be safely accessible during static initialization.</div><span class=""><font color="#888888"><div></div></font></span></div></div></div><br></blockquote><div><br></div>
<div>clang is correct in the sense that the C++ standard doesn't require the compiler to use static initialization for BBB. [basic.start.init] is the relevant section in the standard; "AAA" doesn't count as a constant expression. You can "fix" this using constexpr.<br>
</div></div><br></div><div class="gmail_extra">-Eli<br></div></div>