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

    <tr>
        <th>Summary</th>
        <td>
            clang creates "weak symbol" whereas gcc produces "unique global sybol" symbol type for same source
        </td>
    </tr>

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

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

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

<pre>
    https://github.com/llvm/llvm-project/issues/22655 Previous question link
We've had this problem many times. To this end, we propose the following solution to optimize this problem.
I hope I can get comments on this plan and whether it is feasible.

on llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
```
void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
...
  if (MAI->hasDotTypeDotSizeDirective())
 OutStreamer->emitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject);
...
}
```
Modify as follows:

```
void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
...
if (MAI->hasDotTypeDotSizeDirective()){
 GlobalValue::LinkageTypes Linkage = GV->getLinkage();
     if(Linkage == GlobalValue::LinkOnceODRLinkage){
 OutStreamer->emitSymbolAttribute(EmittedSym, MCSA_ELF_TypeGnuUniqueObject);
    }else{
 OutStreamer->emitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject);
 }
 }
...
}
```

on /llvm/lib/MC/MCELFStreamer.cpp
```
bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
...
case MCSA_Weak:
    // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
    // We emit a warning for now but may switch to an error in the future.
    if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
 getContext().reportWarning(
          getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
 Symbol->setBinding(ELF::STB_WEAK);
    break;
...
}
```
Modify as follows:
```
bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
...
case MCSA_Weak:
    // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
    // We emit a warning for now but may switch to an error in the future.
 if(Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK  && Symbol->getBinding() != ELF::STB_GNU_UNIQUE){
 getContext().reportWarning(
          getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
 Symbol->setBinding(ELF::STB_WEAK);
      break;
    }
 if(!Symbol->isBindingSet()){
      Symbol->setBinding(ELF::STB_WEAK);
 }
    break;
...
}
```
@MaskRay 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV1tT4zgT_TXKS9e4jBzi-CEPIbeiPgLzTWB4pGS7Y2uxpYzUJmR-_ZZkk8tMdrdmht2HrXUFg0X3UV_OaSvCWlkoxBG7vGKX055oqNRmVDT6Vaqwn_RSne9GJdHGsmjM-JzxeSGpbNIg0zXj86p6efv1YWP0b5gR43NpbYOW8Tnng8tL-GjwRerGwpcGLUmtoJLqmYVTFo4fkfH4BaEUOVApLWyMTiusoRZqByRrtAHc6_Z_qHLGJ7BFZ7XRFoFKhLWuKr2VqgCrq8bjkwa9IVnLr3iCGrSbXkOpNwjXkAkFBRJkuq5RkQXn6-0roUCoHLYlUokGJIG0sEZhZVphh9PeXT5dGWTK-Hyic1ygYnw-tvVHIxWhOXkIss2mAxiE3cc_vmiZw5FTNGbRGGtJi0qnovosjBRphYwPM60swekyMD5efGY8ARZftYBB0EUKINfA-HA5vv7Aolkp7FTT_W6DU00r-RWn0mBG8sVhM564T-t319CKDIoajXN0wax2daqrMZGRaUPOY1ZLIsxXu9q1ZzlZjZ9mN_Mnh3-XtpxIWPRtTCyenq3CUudyvQNhu8566h3V-x8r2g-X7A1hv0fVYBvPjVTPokDnbqF7ABZNYfHZgRdI3WIH9lYscJdcMz48cvJ-53a4UxneTT_toY4i-tU-LlTzoOSX5kxDXYgsnmJl8f32O7PN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