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

    <tr>
        <th>Summary</th>
        <td>
            Should llvm-mc use section relocations against a section group?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mc
      </td>
    </tr>

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

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

<pre>
    llvm-mc for ELF [prefers relocations against a section to relocations against a symbol](https://github.com/llvm/llvm-project/blob/4b3bc46d1d794b8ed78b75ccd35a6cc30235bf31/llvm/lib/MC/ELFObjectWriter.cpp#L1189) for local symbols (to be precise, a relocation against an STB_LOCAL symbol will be emitted against the symbol's section's STT_SECTION symbol instead of the symbol itself). This can interact badly with COMDAT section groups:

```
$ cat foo.s
.section .text.foo,"axG",@progbits,foo,comdat
foo:
 retq

.text
.globl _start
_start:
        jmp     foo

$ cat otherfoo.s
.section .text.foo,"axG",@progbits,foo,comdat
foo:
 retq

$ llvm-mc --filetype=obj -o foo.o foo.s
$ llvm-mc --filetype=obj -o otherfoo.o otherfoo.s
$ ld.lld otherfoo.o foo.o
ld.lld: error: relocation refers to a discarded section: .text.foo
>>> defined in foo.o
>>> section group signature: foo
>>> prevailing definition is in otherfoo.o
>>> referenced by foo.o:(.text+0x1)
```

This is obviously a contrived example. On the one hand, using section groups with local symbols is pretty weird ([Fangrui's blog post](https://maskray.me/blog/2021-07-25-comdat-and-section-group) links to a [generic ABI discussion thread](https://groups.google.com/g/generic-abi/c/2X6mR-s2zoc) which goes into this). I can't imagine many real world uses (maybe if you put a local symbol in the same COMDAT section as a non-local symbol, but symbols usually go in their own COMDAT section), so maybe this is okay.

On the other hand, MC's justification seems a bit tenuous here. It says:

> // It is always valid to create a relocation with a symbol. It is preferable
> // to use a relocation with a section if that is possible. Using the section
> // allows us to omit some local symbols from the symbol table.

That makes sense for private (`.L` or LLVM's `private` linkage) symbols, but not for other local symbols (LLVM's `internal` linkage), since those will be included in the symbol table even if no relocations target them. Does MC's behavior still make sense in that case?

This isn't particularly important, but I ran into it and was curious what others though. CC @MaskRay in particular.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vltv47wR_TX0y8CCTPkSP_ghceIigLcBvk0vb4shNZK4oUiVpOxVf31BSnacbNqiLw0Mm5E4Zy48c4bovaoN0Y6tHtjqcYZ9aKzb-ZbIoJoJWw47rU_tvJVQWQdPxwOw1UPnqCLnwZG2EoOyxgPWqIwPgOBJxkcQ7L_bMLTCarZ6ZPyuCaHzrLhn_MD4oVah6UUmbcv4ITqefuadsz9JBsYPQlvB-GEpCiGX63JRbrZLcUfl5k5sVlKWxQrXUhY5L1aiKhY3OCrafdszfng6Hl5ExPubU4FcJruO8eK4WNxtGd-mTGPceorUA-N3wYIg6BxJ5YnxPeBNdu_JGfj--vDj-LK_P07WcFZaR1tqVQhUXveGhi6l4Bt_KVtaf399_fH9af_6_PLnC0o0ISzBVjeGoIInXTG-zeC1UR4kGlAmkEMZQGCpBzir0MD-5dvj_ev1aGpn-7Hs-SPLL9_rfPqM__IlSAxQWZv58VF2sc8C_QpZZS3je8Y5_voT4zyul3nnbC1U8Izvx_fStiWGESA-uTgFR-Eft_4T6LSstRUafviAbno0ra_W09_Ptku_Efk2lyl4Gxpy_7cMotdLu8znldIUho5Y8WjFT5jbVEp7W9D_ZnAN3_6WSTItM63L213pe9wwvmTFPZBz1sXFDWGnDg4WEErlJbqSyisHi_ubAo3uiqfxAyVVylAJytx6e3__gWMQ9QVD7yhifoHWOTqh0srUI7BKtspH-Pe8Plul6MlIKkEMUxhRQ-5GDvGH_NeC8e3XvE7fqVuUBytOyvZeD4AgrQlOnagE-oVtpymDF5O6zRqCBk0Z-773MdiPnTQ22UfNUD4mF8IAZ1KujBrCVg8HNLXrVepyoW0NnfXhKyls0b85HLKWRtGrGT_wnC_m-WbOV_ORk3M05XwKZZ5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