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

    <tr>
        <th>Summary</th>
        <td>
            clang integrated assembler generates incorrect relocations for mips32
        </td>
    </tr>

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

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

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

<pre>
    When I build openssl with clang 16, the built binary `openssl` just crashes. Then I tried to narrow down to the issue, and it seems it is related to the integrated assembler of clang, because it generates different object from the one generated by gas, and finally linker would complain that "can't find matching R_MIPS_LO16 relocation for R_MIPS_GOT16"

I have preprocessed the problematic assembly file, so it can be easily verified.
[aes-mips.s.zip](https://github.com/llvm/llvm-project/files/12447500/aes-mips.s.zip)

Reproduced steps:
1. `clang -target mipsel-buildroot-linux-musl -fPIC -pthread -mabi=32 -mips2 -Wa,--noexecstack -Qunused-arguments -Wall -O3 -c -o aes-mips.o aes-mips.s`
2. `llvm-readelf -r aes-mips.o`
```
Relocation section '.rel.text' at offset 0x1be0 contains 12 entries:
 Offset     Info    Type                Sym. Value Symbol's Name
00000340  00001405 R_MIPS_HI16            00000000 _gp_disp
00000344  00001406 R_MIPS_LO16            00000000 _gp_disp
00000760  00001405 R_MIPS_HI16            00000000 _gp_disp
00000764  00001406 R_MIPS_LO16            00000000 _gp_disp
00000b38  00001405 R_MIPS_HI16            00000000 _gp_disp
00000b3c  00001406 R_MIPS_LO16            00000000 _gp_disp
00000b80  00001405 R_MIPS_HI16            00000000 _gp_disp
00000b84  00001406 R_MIPS_LO16            00000000 _gp_disp
00000378  00001209 R_MIPS_GOT16           00000000 .rodata
00000798  00001209 R_MIPS_GOT16           00000000 .rodata
00000b50  00001209 R_MIPS_GOT16           00000000 .rodata
00000b98  00001209 R_MIPS_GOT16           00000000 .rodata

Relocation section '.rel.pdr' at offset 0x1c40 contains 7 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name
00000000  00000102 R_MIPS_32              00000000 .text
00000020  00001302 R_MIPS_32              00000340 AES_encrypt
00000040  00000102 R_MIPS_32              00000000 .text
00000060  00001502 R_MIPS_32              00000760 AES_decrypt
00000080  00000102 R_MIPS_32              00000000 .text
000000a0  00001602 R_MIPS_32              00000b38 AES_set_encrypt_key
000000c0  00001702 R_MIPS_32              00000b80 AES_set_decrypt_key
```
3. Note that there 4 `R_MIPS_GOT16` without corresponding `R_MIPS_LO16`

If we tried the assembler from binutils.
1. `mipsel-buildroot-linux-musl-as -march mips2 -mabi 32 -call_nonpic -EL -KPIC  --noexecstack -o gas-aes-mips.o aes-mips.s`
2. `llvm-readelf -r gas-aes-mips.o`
```
Relocation section '.rel.text' at offset 0x1c04 contains 16 entries:
 Offset     Info    Type Sym. Value  Symbol's Name
00000340  00001105 R_MIPS_HI16 00000000   _gp_disp
00000344  00001106 R_MIPS_LO16            00000000 _gp_disp
00000378  00000a09 R_MIPS_GOT16           00000000 .rodata
0000037c  00000a06 R_MIPS_LO16            00000000 .rodata
00000740  00001105 R_MIPS_HI16            00000000 _gp_disp
00000744  00001106 R_MIPS_LO16            00000000 _gp_disp
00000778  00000a09 R_MIPS_GOT16           00000000 .rodata
0000077c  00000a06 R_MIPS_LO16            00000000 .rodata
00000b14  00001105 R_MIPS_HI16            00000000 _gp_disp
00000b18  00001106 R_MIPS_LO16            00000000 _gp_disp
00000b2c  00000a09 R_MIPS_GOT16           00000000 .rodata
00000b30  00000a06 R_MIPS_LO16            00000000 .rodata
00000b60  00001105 R_MIPS_HI16            00000000 _gp_disp
00000b64  00001106 R_MIPS_LO16            00000000 _gp_disp
00000b78  00000a09 R_MIPS_GOT16           00000000 .rodata
00000b7c  00000a06 R_MIPS_LO16            00000000 .rodata

Relocation section '.rel.pdr' at offset 0x1c84 contains 7 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name
00000000  00000102 R_MIPS_32              00000000 .text
00000020  00001002 R_MIPS_32              00000340 AES_encrypt
00000040  00000102 R_MIPS_32              00000000 .text
00000060  00001202 R_MIPS_32              00000740 AES_decrypt
00000080  00000102 R_MIPS_32              00000000 .text
000000a0  00001302 R_MIPS_32              00000b14 AES_set_encrypt_key
000000c0  00001402 R_MIPS_32              00000b60 AES_set_decrypt_key
```
3. For each rodata, there is a pair of LO16 and GOT16.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWF1v27gS_TX0y0ACRcmS_eCHtqnvDW5v022L7WNAUiOLLU0KJNXE--sXlPyVtFunUReLFYpKsWeOzoxnOAfDvVcbg7gi85dkfjXjfWitW7XcttxqxJmw9W71qUUD1yB6pWuwHRrvNdyp0ILU3GwgKwl7BaHFwSSAUIa7HZCS7o1JSeFz7wNIx32LPoWPI2RwCmsIFgx3zt5Bbe9M_DNiKe97jMDc1KACeMStjw_Kg0PNw-g5mJqAGzd8wr3HrdDowDYjvQghUPLeY_TeoMFo6qFWTYMOTQArPqMM0Di7HfCswaNdDWIHG-4PTBpluNY70Mp8QQd3ttc1SLvtNFcGQssDEMYkN4RVIVrXsOVBtsps4P3t_6_ffbh9c5OVMQQreVDWQGPd4av_3HyM2WSEXhH6Yvz_Glr-FaFz2Dkr0fsYeBs_sELjlgclD2HvoFF6SJq3MVjJDQgE5F7pHXxFpxqFdbpHn7_k6JOt6nzq0z9UR-ZXhC3aEDpP8heErQlbb1Roe5FKuyVsrfXXwy3pnI1JI2wdX-kJW2esKKo5pYStH-Gy5Xk872McdS-xBh9wfNfwRZbGmhlrKgncbTBAREGdDLXnrA2JVqa_T7a915A0765fQdKF1iGvIdlyoUh-lTMYXs4g-cQJe5UkxuI9Sh-4_ALJb73pPdYJd5t-iyb4aKY1JDc5JBISC0f2Z4-elHRkyQaWQw7ia1E3kLgzn6NhfBj_7cM-_uAe5XAnrEod6jTgfSCsAh7ANo3HAPQ-E0hBWhO4Mh4yBmhit5yyBTejabyuTWPj_eOuQ3h0fdhtU_id6x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