<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61924>61924</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang bug] clang emits truncated DWARF for union under -O1
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jwnhy
      </td>
    </tr>
</table>

<pre>
    Tested on top-of-tree clang/gdb/lldb
Given the following code
```c
int a, b;
union {
  short f0;
 unsigned char f1;
  int f2;
  long f3;
} static g_55 = {65535};
int c() {
  char *d = (char*)&g_55;
  *d = b;
  return 0;
}
int main() { a = g_55.f1; }
```

`clang -O1 -g` will emit incomplete debugging info regarding to `g_55`, making `lldb` fail to read `g_55` and `gdb` report wrong value.
```
Symbol "g_55" is a complex DWARF expression:
     0: DW_OP_addrx 0x201028 -> 255
    [1-byte piece].
```

Here the address `0x201028` just points to `255` in the `.data` section, but it should be `65535`.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVEGPqzgM_jXuxWoVDKT0wKGzbHdvb7W70juOAjE0s2lSJWE68-9XgZm2hychkJ0v9mf7wypGMznmFuoXqLuNmtPZh_bt5s6fm97rz_Zfjok1eofJX7d-3KbAjINVbgI6TboHOlmrexAdiOMf5p0dpjPj6K31N-MmHLzm9RSkWJ9htY1LqIB-wx7Kl9U1O-Mdwv7LRIxnHxKO4o7A2S2cNQ5nFXAsHieYA4705LDeTTiWdw_sO4xJJTPg9FrXCGWXk8m6LmvYd3dcDjQANUCHZzJLRqCjXi9Skx1AR6ADkMwRn3Lfcf2TM3Cag0PxzOiR8qKMe2RFtVzPYXdLmXgH3zv5ZX47l7ng9keB2wmkwJuxFvliEho3-MvVcmLU3M_TlEdj3Ogx8KSCzmbyCFIsZUiR53JR_2U_SLGMWAoclbEZF1jpJzAqt5orKvA1T-0WcvvflZ1590ve_3xeem8RiJY4RGgiKlyZfmD38_j3CfnjGjhG4x2Ux-8-IuYeHrH7-frjr1eldfhA8UGiENTgFsrfker6AYb6pdj2n4nxanhgqLtfE1rff3LgRcQ5LseYK_sOnst7m2PCqzcuxa-e0doFs2ofpNhplVR2RR5Spp5VPic0KSt6thr7BbcqT4ovOhvdlvpQHtSG20I2om4aSbQ5t7rioqmLoaCm1FJSoxutxNiUuhqroZEb05KgUlSiErIQRLteSNXsqS9FWe1rXUMl-KKM3Vn7ftn5MG1MjDO3sjhQtbGqZxuXNUDk-IbLIRDlrRDafGfbz1OESlgTU3xESSbZZX-s2uvnCepuXRCL8iKmMLtB5S2yDnT0Adf_fHaaQ5brZg62Pad0jXnGdMqrxaTz3O8Gf1k2zPv3Z3sN_o2HBHRaOEag01LD_wEAAP__oMhetg">