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

    <tr>
        <th>Summary</th>
        <td>
            When linked with LLD, static constructors are only executed if the .o is produced by LLC
        </td>
    </tr>

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

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

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

<pre>
    ## description

When linked with LLD, static constructors and destructors are only executed if the _.o_ files are produced by LLC. If I use the C API then they are not.

This seems related to fact that _ld.bfd_ or _ld.gold_ handle correctly the `.ctors` while _ld.lld_ does not. Yet this is not clear why the problem does not happen when LLC is used, instead of the C API, to produce objects.

## System info

- linux x86_64
- LLVM13

## reproduction

- decompress the attached archive
  - the *.o file is produced using the C API (v13)
  - the *.ll file is produced using the C API (v13)
- check the paths in the shell script, those used are for Fedora
- `sh test.sh`

## output

```
LD.BFD+OBJ produced using the C API
called

LD.LLD+OBJ produced using the C API

LD.LLD+OBJ produced using LLC
called
```
## expected output

```
LD.BFD+OBJ produced using the C API
called

LD.LLD+OBJ produced using the C API
called

LD.LLD+OBJ produced using LLC
called
```
[tc.tar.gz](https://github.com/llvm/llvm-project/files/10073120/tc.tar.gz)

## notes

I'm opened to workarounds (maybe using `LLVMSetSection()` ?) as if the bug is confirmed I'll not be in measure to use the fix before several months. Also explanations on what is the secret sauce added by LLC for the static ctors are welcome.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVclu6zYU_RppQ1jQYMv2QosMCJDCRQukaPFWBiVeWWwo0SCp2O7X95CyHafNQ192BQSJFO9w7nRYa3GqorzAwwTZxsi9k3qI0scovZvef3Q0MCWHVxLsIF3HNpvHKH9g1nEnG9bowTozNk4by_ggvJn3vSGmB3VidKRmdLAgW-Y6YttEb1krFU0ye6PF2OC4PsH8Q8KeW_bMRktB-IHd_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