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

    <tr>
        <th>Summary</th>
        <td>
            LLD for Mach-O incorrectly dead-strips merged CFStrings
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld:MachO
      </td>
    </tr>

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

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

<pre>
    #101222 recently landed which enables private symbols to be merged, which includes CFString constants. On a Clang containing that patch, if you build the following C code:

```
$ cat merge.c
void *__CFConstantStringClassReference;
int main(int argc, char *argv[]) {
 return argc ? (long)__builtin___CFStringMakeConstantString("foo")
 : (long)__builtin___CFStringMakeConstantString("bar");
}

$ clang -target arm64-apple-macos11 -O3 -S -o merge.s merge.c
```

It generates the assembly in https://gist.github.com/smeenai/9fe473bcb5c4cb4a2b97eb147b23f671 (I changed a `.set` directive changed to `=` to avoid #104623 and match the object file generated by Clang directly). If you compile and link this assembly:

```
$ llvm-mc --triple=arm64-apple-macos11 --filetype=obj -o merge.o merge.s
$ ld64.lld -arch arm64 -platform_version macos 11 11 -dead_strip -o merge merge.o
```

you'll discover that the second cfstring constant has been incorrectly dead-stripped:

```
$ llvm-objdump -h merge.o | grep cfstring
  3 __cfstring       00000040 0000000000000020 DATA

$ llvm-objdump -h merge | grep cfstring
  3 __cfstring    00000020 0000000100004000 DATA
```

Note the size of 0x20 instead of 0x40 in the output binary. The code in the binary still references the second string though:

```
$ llvm-objdump -d merge
0000000100000470 <_main>:
100000470: 1001dc88     adr     x8, 0x100004000 <__MergedGlobals>
100000474: d503201f     nop
100000478: 11008109     add     w9, w8, #0x20
10000047c: 7100001f     cmp     w0, #0x0
100000480: 1a880120     csel    w0, w9, w8, eq
100000484: d65f03c0 ret
```

Which causes crashes at runtime in an actual program.

This might be a general problem with dead-stripping sections with alt_entry symbols and not something specific to CFStrings, but I haven't experimented further yet.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVt2O27gOfhrlhnAgy45_LnIxTU4OBmjPAKcF9jKQJdpWK0teSc5M9ukXsj2ZSdHtdtcI4kgiP5IfKTLce9UZxD3ZfSC744ZPobdu7wdEw9WmsfK6JyxLacoYA4cCTdBX0NxIlPDcK9EDGt5o9DA6deEBwV-HxmoPwUKDMKDrUBJ2WKWVEXqS6OFw-hycMh0Ia3zgJvgtPBngcNB82Q1cmSgQeh5g5EH0EUa1cLUTNJPSEkKP0Fqt7XMUPICwEkn2QOiR0Nfvgq6fZclyEDwsfm3FsnmxSgJhD-fz4XRY3Vm8O2ju_f-xRYdGIMk-LArKBBi4MoRV8Sd3nYi-iZ67iMNdd1kYJawGUq5a4DBMzsziQLITEFZpazrC6vM5BhSUOUcfFtuf-De894awijDWWksYI6xeUUn28C-RGu5WpNfASHm8Iy-yNecjCdx1GEMdijzh46gxGbiwPk0hecog-QyJXVn19-x-n4H5-zFAhwYdD-jnNHLvcWj0FZSBPoTRxzyyE2GnTvmw7VTop2Yr7EDYaa1Pwk51i3mZNaLZiVw0OWdNXWKT5mXDsrYo00jMY8yL6VACB1LQrcdACgpSORRBXfB2HGw8J9kxHgcLfK2LLKV5wTLgRsIQ63B22DZfUQRolcZbKBKa61rAC7y-ElZv4XEpWmGHMYpHIK3MNwi98rfI_75wtb4MySAgSYJTo0aSHX-YjyQ6Fa5jFLDN17fM3DL0DlMW-VZrCQl3ol_yC8moeWitG84XdF5ZAzM2pGn8JBK5PPvoww361cBPUn61E2Gl1iCVF_aCbrnZkUuPwhoJovX3PQF67qFBNLFvWLcwCtF8MpsfUf4ia7b5KqdhhKS_UUHKA3QOx5vZ9T5BBufzzZXlofOT0_XH68MoHB--PHx_ZX5o8dft3bBXK-lim94Z-xHD_7MBFz7VHwi2BfrCKCjjA3K5rPO4Xup3CuMUoFGGu-sWvvQ4t8_X42UffFBag3vtgP59ulaPQ2-nrv-neZALK8v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