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

    <tr>
        <th>Summary</th>
        <td>
            [lld/macho] LLD does not correctly add swift symbols  even when -ObjC flag is added
        </td>
    </tr>

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

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

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

<pre>
    Reproducer: https://github.com/tapthaker/LLD-exec-bad-excess-repro/

>From LD64's source code:

```cpp
template <typename A>
bool Parser<A>::hasObjC2Categories(const uint8_t* fileContent)
{
        const macho_header<P>* header = (const macho_header<P>*)fileContent;
        const uint32_t cmd_count = header->ncmds();
        const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
        const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
        const macho_load_command<P>* cmd = cmds;
        for (uint32_t i = 0; i < cmd_count; ++i) {
                if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
                        const macho_segment_command<P>*   segment = (macho_segment_command<P>*)cmd;
                        const macho_section<P>* sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
                        for (uint32_t si=0; si < segment->nsects(); ++si) {
                                const macho_section<P>* sect = &sectionsStart[si];
                                if ( (sect->size() > 0) 
                                        && (strcmp(sect->sectname(), "__objc_catlist") == 0)
                                        && (strcmp(sect->segname(), "__DATA") == 0) ) {
                                                return true;
                                }
                                // <rdar://problem/34686622> -ObjC option should cause all swift code in static libraries to be loaded
                                if ( (sect->size() > 0)
// ========================================== BELOW IS THE IMPORTANT PART ==================
                                        && (strncmp(sect->sectname(), "__swift", 7) == 0)
                                        && (strcmp(sect->segname(), "__TEXT") == 0) ) {
                                                return true;
                                }
                        }
                }
                cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
                if ( cmd > cmdsEnd )
                        throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
        }
        return false;
}
```
What LLD does:

```cpp
template <class LP> static bool objectHasObjCSection(MemoryBufferRef mb) {
  using SectionHeader = typename LP::section;

  auto *hdr =
      reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
  if (hdr->magic != LP::magic)
    return false;

  if (const auto *c =
          findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
    auto sectionHeaders = ArrayRef<SectionHeader>{
        reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};
    for (const SectionHeader &secHead : sectionHeaders) {
      StringRef sectname(secHead.sectname,
                         strnlen(secHead.sectname, sizeof(secHead.sectname)));
      StringRef segname(secHead.segname,
                        strnlen(secHead.segname, sizeof(secHead.segname)));
      if ((segname == segment_names::data &&
           sectname == section_names::objcCatList) ||
// ========================================== BELOW IS THE IMPORTANT PART ==================
          (segname == segment_names::text &&
           sectname == section_names::swift)) {
        return true;
      }
    }
  }
  return false;
}
```

So, instead of `==`, we should be checking that the section starts with `__swift`.

I'll submit a patch for the fix soon

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzlWEtv2zgQ_jXyhbChSH4efHBsBy3gboPEQPdmUBT1aCXRIKlNs79-Z0i9LDlp0qK7hw0YWyTn8XFmODNyIMLn9QM_SxGWjEvH35BE67OCB8e7gxGnOimDCRM5TDQ964R-Azrv7nDYjfl3zsYBDeGBcaXGEgUhn7tz3I39vJMiJ4fdfOp4C0WUKCXjhImQo4oOnTN37WDns13RPD9nVHPi-Fv9fOYFzTnZOP7ebgdCZOSeSoWwt2YdJPqbhKrPwdettwXWWMiUK8dbMlEoTcq00MuTdrwNidKMb0WheQHTVQVhcVsjWlmGnLJEnBJOQ6PkHpUAs10AXDvSyL5OCrK7mvy-AkTkeydNWB6emCgLbaRaMWOQUcAGHgBBDritzkxQ5M1zWoQdkJYE2Wugr5GjfNSyZAmVdl6f0rsl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