Bug 22756 - Linker relaxation miscalculates symbol addresses on riscv
Summary: Linker relaxation miscalculates symbol addresses on riscv
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: ---
Assignee: Jim Wilson
Depends on:
Reported: 2018-01-27 23:22 UTC by Andreas Schwab
Modified: 2021-07-21 01:21 UTC (History)
2 users (show)

See Also:
Target: riscv64-*-*
Last reconfirmed:

Testcase (1.83 MB, application/x-bzip)
2018-05-31 21:28 UTC, Andreas Schwab

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Schwab 2018-01-27 23:22:31 UTC
The test program created by the copy_setup proc in testsuite/binutils-all/objcopy.exp is broken by linker relaxation.

$ objdump -d tmpdir/testprog

tmpdir/testprog:     file format elf64-littleriscv

Disassembly of section .plt:

0000000000000690 <.plt>:
 690:   00002397                auipc   t2,0x2
 694:   41c30333                sub     t1,t1,t3
 698:   9883be03                ld      t3,-1656(t2) # 2018 <__TMC_END__>
 69c:   fd430313                addi    t1,t1,-44
 6a0:   98838293                addi    t0,t2,-1656
 6a4:   00135313                srli    t1,t1,0x1
 6a8:   0082b283                ld      t0,8(t0)
 6ac:   000e0067                jr      t3

00000000000006b0 <__libc_start_main@plt>:
 6b0:   00002e17                auipc   t3,0x2
 6b4:   978e3e03                ld      t3,-1672(t3) # 2028 <__libc_start_main@GLIBC_2.27>
 6b8:   000e0367                jalr    t1,t3
 6bc:   00000013                nop

00000000000006c0 <abort@plt>:
 6c0:   00002e17                auipc   t3,0x2
 6c4:   970e3e03                ld      t3,-1680(t3) # 2030 <abort@GLIBC_2.27>
 6c8:   000e0367                jalr    t1,t3
 6cc:   00000013                nop

00000000000006d0 <_exit@plt>:
 6d0:   00002e17                auipc   t3,0x2
 6d4:   968e3e03                ld      t3,-1688(t3) # 2038 <_exit@GLIBC_2.27>
 6d8:   000e0367                jalr    t1,t3
 6dc:   00000013                nop

00000000000006e0 <strlen@plt>:
 6e0:   00002e17                auipc   t3,0x2
 6e4:   960e3e03                ld      t3,-1696(t3) # 2040 <strlen@GLIBC_2.27>
 6e8:   000e0367                jalr    t1,t3
 6ec:   00000013                nop

00000000000006f0 <puts@plt>:
 6f0:   00002e17                auipc   t3,0x2
 6f4:   958e3e03                ld      t3,-1704(t3) # 2048 <puts@GLIBC_2.27>
 6f8:   000e0367                jalr    t1,t3
 6fc:   00000013                nop

0000000000000700 <exit@plt>:
 700:   00002e17                auipc   t3,0x2
 704:   950e3e03                ld      t3,-1712(t3) # 2050 <exit@GLIBC_2.27>
 708:   000e0367                jalr    t1,t3
 70c:   00000013                nop

0000000000000710 <write@plt>:
 710:   00002e17                auipc   t3,0x2
 714:   948e3e03                ld      t3,-1720(t3) # 2058 <write@GLIBC_2.27>
 718:   000e0367                jalr    t1,t3
 71c:   00000013                nop

Disassembly of section .text:

0000000000000720 <_start>:
 720:   02c000ef                jal     ra,74c <_start+0x2c>
 724:   87aa                    mv      a5,a0
 726:   00002517                auipc   a0,0x2
 72a:   94a53503                ld      a0,-1718(a0) # 2070 <__wrap_main@@Base+0x160e>
 72e:   6582                    ld      a1,0(sp)
 730:   0030                    addi    a2,sp,8
 732:   ff017113                andi    sp,sp,-16
 736:   00000697                auipc   a3,0x0
 73a:   35c68693                addi    a3,a3,860 # a92 <__libc_csu_init>
 73e:   00000717                auipc   a4,0x0
 742:   3ac70713                addi    a4,a4,940 # aea <__libc_csu_fini>
 746:   880a                    mv      a6,sp
 748:   f69ff06f                j       6b0 <__libc_start_main@plt>
 74c:   00002197                auipc   gp,0x2
 750:   14418193                addi    gp,gp,324 # 2890 <__global_pointer$>
 754:   8082                    ret

0000000000000756 <deregister_tm_clones>:
 756:   00002517                auipc   a0,0x2
 75a:   8c250513                addi    a0,a0,-1854 # 2018 <__TMC_END__>
 75e:   00002797                auipc   a5,0x2
 762:   8ba78793                addi    a5,a5,-1862 # 2018 <__TMC_END__>
 766:   00a78963                beq     a5,a0,778 <deregister_tm_clones+0x22>
 76a:   00002317                auipc   t1,0x2
 76e:   8fe33303                ld      t1,-1794(t1) # 2068 <_ITM_deregisterTMCloneTable>
 772:   00030363                beqz    t1,778 <deregister_tm_clones+0x22>
 776:   8302                    jr      t1
 778:   8082                    ret

000000000000077a <register_tm_clones>:
 77a:   00002517                auipc   a0,0x2
 77e:   89e50513                addi    a0,a0,-1890 # 2018 <__TMC_END__>
 782:   00002597                auipc   a1,0x2
 786:   89658593                addi    a1,a1,-1898 # 2018 <__TMC_END__>
 78a:   8d89                    sub     a1,a1,a0
 78c:   858d                    srai    a1,a1,0x3
 78e:   03f5d793                srli    a5,a1,0x3f
 792:   95be                    add     a1,a1,a5
 794:   8585                    srai    a1,a1,0x1
 796:   c981                    beqz    a1,7a6 <register_tm_clones+0x2c>
 798:   00002317                auipc   t1,0x2
 79c:   8f033303                ld      t1,-1808(t1) # 2088 <_ITM_registerTMCloneTable>
 7a0:   00030363                beqz    t1,7a6 <register_tm_clones+0x2c>
 7a4:   8302                    jr      t1
 7a6:   8082                    ret

00000000000007a8 <__do_global_dtors_aux>:
 7a8:   00002797                auipc   a5,0x2
 7ac:   8e87c783                lbu     a5,-1816(a5) # 2090 <_edata>
 7b0:   eb85                    bnez    a5,7e0 <__do_global_dtors_aux+0x38>
 7b2:   1141                    addi    sp,sp,-16
 7b4:   e406                    sd      ra,8(sp)
 7b6:   00002797                auipc   a5,0x2
 7ba:   8ca7b783                ld      a5,-1846(a5) # 2080 <__cxa_finalize@GLIBC_2.27>
 7be:   c799                    beqz    a5,7cc <__do_global_dtors_aux+0x24>
 7c0:   00002717                auipc   a4,0x2
 7c4:   84070713                addi    a4,a4,-1984 # 2000 <__data_start>
 7c8:   6308                    ld      a0,0(a4)
 7ca:   9782                    jalr    a5
 7cc:   f8bff0ef                jal     ra,756 <deregister_tm_clones>
 7d0:   60a2                    ld      ra,8(sp)
 7d2:   4785                    li      a5,1
 7d4:   00002717                auipc   a4,0x2
 7d8:   8af70e23                sb      a5,-1860(a4) # 2090 <_edata>
 7dc:   0141                    addi    sp,sp,16
 7de:   8082                    ret
 7e0:   8082                    ret

00000000000007e2 <frame_dummy>:
 7e2:   f99ff06f                j       77a <register_tm_clones>

00000000000007e6 <fn>:
 7e6:   1141                    addi    sp,sp,-16
 7e8:   e422                    sd      s0,8(sp)
 7ea:   0800                    addi    s0,sp,16
 7ec:   478d                    li      a5,3
 7ee:   853e                    mv      a0,a5
 7f0:   6422                    ld      s0,8(sp)
 7f2:   0141                    addi    sp,sp,16
 7f4:   8082                    ret

00000000000007f6 <main>:
 7f6:   1141                    addi    sp,sp,-16
 7f8:   e406                    sd      ra,8(sp)
 7fa:   e022                    sd      s0,0(sp)
 7fc:   0800                    addi    s0,sp,16
 7fe:   00002797                auipc   a5,0x2
 802:   87a7b783                ld      a5,-1926(a5) # 2078 <common@@Base-0x20>
 806:   439c                    lw      a5,0(a5)
 808:   e39d                    bnez    a5,82e <main+0x38>
 80a:   00001797                auipc   a5,0x1
 80e:   7fe78793                addi    a5,a5,2046 # 2008 <global>
 812:   439c                    lw      a5,0(a5)
 814:   873e                    mv      a4,a5
 816:   4785                    li      a5,1
 818:   00f71b63                bne     a4,a5,82e <main+0x38>
 81c:   00001797                auipc   a5,0x1
 820:   7f078793                addi    a5,a5,2032 # 200c <local>
 824:   439c                    lw      a5,0(a5)
 826:   873e                    mv      a4,a5
 828:   4789                    li      a5,2
 82a:   00f70a63                beq     a4,a5,83e <main+0x48>
 82e:   00000517                auipc   a0,0x0
 832:   2d250513                addi    a0,a0,722 # b00 <string+0x8>
 836:   ebbff0ef                jal     ra,6f0 <puts@plt>
 83a:   4785                    li      a5,1
 83c:   a801                    j       84c <main+0x56>
 83e:   00000517                auipc   a0,0x0
 842:   2ca50513                addi    a0,a0,714 # b08 <string+0x10>
 846:   eabff0ef                jal     ra,6f0 <puts@plt>
 84a:   4781                    li      a5,0
 84c:   853e                    mv      a0,a5
 84e:   60a2                    ld      ra,8(sp)
 850:   6402                    ld      s0,0(sp)
 852:   0141                    addi    sp,sp,16
 854:   8082                    ret

0000000000000856 <write_int>:
 856:   7179                    addi    sp,sp,-48
 858:   f406                    sd      ra,40(sp)
 85a:   f022                    sd      s0,32(sp)
 85c:   1800                    addi    s0,sp,48
 85e:   87aa                    mv      a5,a0
 860:   fcb43823                sd      a1,-48(s0)
 864:   fcf42e23                sw      a5,-36(s0)
 868:   fdc42783                lw      a5,-36(s0)
 86c:   2781                    sext.w  a5,a5
 86e:   0207d263                bgez    a5,892 <write_int+0x3c>
 872:   fd043783                ld      a5,-48(s0)
 876:   00178713                addi    a4,a5,1
 87a:   fce43823                sd      a4,-48(s0)
 87e:   02d00713                li      a4,45
 882:   00e78023                sb      a4,0(a5)
 886:   fdc42783                lw      a5,-36(s0)
 88a:   40f007bb                negw    a5,a5
 88e:   fcf42e23                sw      a5,-36(s0)
 892:   fdc42783                lw      a5,-36(s0)
 896:   0007871b                sext.w  a4,a5
 89a:   47a5                    li      a5,9
 89c:   00e7df63                ble     a4,a5,8ba <write_int+0x64>
 8a0:   fdc42703                lw      a4,-36(s0)
 8a4:   47a9                    li      a5,10
 8a6:   02f747bb                divw    a5,a4,a5
 8aa:   2781                    sext.w  a5,a5
 8ac:   fd043583                ld      a1,-48(s0)
 8b0:   853e                    mv      a0,a5
 8b2:   fa5ff0ef                jal     ra,856 <write_int>
 8b6:   fca43823                sd      a0,-48(s0)
 8ba:   fdc42703                lw      a4,-36(s0)
 8be:   47a9                    li      a5,10
 8c0:   02f767bb                remw    a5,a4,a5
 8c4:   2781                    sext.w  a5,a5
 8c6:   0ff7f793                andi    a5,a5,255
 8ca:   0307879b                addiw   a5,a5,48
 8ce:   fef407a3                sb      a5,-17(s0)
 8d2:   fd043783                ld      a5,-48(s0)
 8d6:   00178713                addi    a4,a5,1
 8da:   fce43823                sd      a4,-48(s0)
 8de:   fef44703                lbu     a4,-17(s0)
 8e2:   00e78023                sb      a4,0(a5)
 8e6:   fd043783                ld      a5,-48(s0)
 8ea:   853e                    mv      a0,a5
 8ec:   70a2                    ld      ra,40(sp)
 8ee:   7402                    ld      s0,32(sp)

00000000000008f0 <__wrap_exit>:
 8f0:   6145                    addi    sp,sp,48
 8f2:   8082                    ret
 8f4:   715d                    addi    sp,sp,-80
 8f6:   e486                    sd      ra,72(sp)
 8f8:   e0a2                    sd      s0,64(sp)
 8fa:   0880                    addi    s0,sp,80
 8fc:   87aa                    mv      a5,a0
 8fe:   faf42e23                sw      a5,-68(s0)
 902:   fc840793                addi    a5,s0,-56
 906:   00000717                auipc   a4,0x0
 90a:   20a70713                addi    a4,a4,522 # b10 <string+0x18>
 90e:   6314                    ld      a3,0(a4)
 910:   e394                    sd      a3,0(a5)
 912:   6718                    ld      a4,8(a4)
 914:   e798                    sd      a4,8(a5)
 916:   fc840793                addi    a5,s0,-56
 91a:   853e                    mv      a0,a5
 91c:   dc5ff0ef                jal     ra,6e0 <strlen@plt>
 920:   872a                    mv      a4,a0
 922:   fc840793                addi    a5,s0,-56
 926:   973e                    add     a4,a4,a5
 928:   fbc42783                lw      a5,-68(s0)
 92c:   85ba                    mv      a1,a4
 92e:   853e                    mv      a0,a5
 930:   f27ff0ef                jal     ra,856 <write_int>
 934:   fea43423                sd      a0,-24(s0)
 938:   fe843783                ld      a5,-24(s0)
 93c:   00178713                addi    a4,a5,1
 940:   fee43423                sd      a4,-24(s0)
 944:   4729                    li      a4,10
 946:   00e78023                sb      a4,0(a5)
 94a:   fc840793                addi    a5,s0,-56
 94e:   fe843703                ld      a4,-24(s0)
 952:   40f707b3                sub     a5,a4,a5
 956:   873e                    mv      a4,a5
 958:   fc840793                addi    a5,s0,-56
 95c:   863a                    mv      a2,a4
 95e:   85be                    mv      a1,a5
 960:   4505                    li      a0,1
 962:   dafff0ef                jal     ra,710 <write@plt>
 966:   00001797                auipc   a5,0x1
 96a:   72e78793                addi    a5,a5,1838 # 2094 <done_exit_message>
 96e:   4705                    li      a4,1
 970:   c398                    sw      a4,0(a5)
 972:   fbc42783                lw      a5,-68(s0)
 976:   853e                    mv      a0,a5
 978:   d89ff0ef                jal     ra,700 <exit@plt>
 97c:   0001                    nop
 97e:   00001797                auipc   a5,0x1
 982:   69278793                addi    a5,a5,1682 # 2010 <___constval>
 986:   439c                    lw      a5,0(a5)
 988:   fbfd                    bnez    a5,97e <__wrap_exit+0x8e>
 98a:   0001                    nop
 98c:   60a6                    ld      ra,72(sp)
 98e:   6406                    ld      s0,64(sp)
 990:   6161                    addi    sp,sp,80
 992:   8082                    ret

0000000000000994 <__wrap__exit>:
 994:   715d                    addi    sp,sp,-80
 996:   e486                    sd      ra,72(sp)
 998:   e0a2                    sd      s0,64(sp)
 99a:   0880                    addi    s0,sp,80
 99c:   87aa                    mv      a5,a0
 99e:   faf42e23                sw      a5,-68(s0)
 9a2:   00001797                auipc   a5,0x1
 9a6:   6f278793                addi    a5,a5,1778 # 2094 <done_exit_message>
 9aa:   439c                    lw      a5,0(a5)
 9ac:   e3bd                    bnez    a5,a12 <__wrap__exit+0x7e>
 9ae:   fc840793                addi    a5,s0,-56
 9b2:   00000717                auipc   a4,0x0
 9b6:   15e70713                addi    a4,a4,350 # b10 <string+0x18>
 9ba:   6314                    ld      a3,0(a4)
 9bc:   e394                    sd      a3,0(a5)
 9be:   6718                    ld      a4,8(a4)
 9c0:   e798                    sd      a4,8(a5)
 9c2:   fc840793                addi    a5,s0,-56
 9c6:   853e                    mv      a0,a5
 9c8:   d19ff0ef                jal     ra,6e0 <strlen@plt>
 9cc:   872a                    mv      a4,a0
 9ce:   fc840793                addi    a5,s0,-56
 9d2:   973e                    add     a4,a4,a5
 9d4:   fbc42783                lw      a5,-68(s0)
 9d8:   85ba                    mv      a1,a4
 9da:   853e                    mv      a0,a5
 9dc:   e7bff0ef                jal     ra,856 <write_int>
 9e0:   fea43423                sd      a0,-24(s0)
 9e4:   fe843783                ld      a5,-24(s0)
 9e8:   00178713                addi    a4,a5,1
 9ec:   fee43423                sd      a4,-24(s0)
 9f0:   4729                    li      a4,10
 9f2:   00e78023                sb      a4,0(a5)
 9f6:   fc840793                addi    a5,s0,-56
 9fa:   fe843703                ld      a4,-24(s0)
 9fe:   40f707b3                sub     a5,a4,a5
 a02:   873e                    mv      a4,a5
 a04:   fc840793                addi    a5,s0,-56
 a08:   863a                    mv      a2,a4
 a0a:   85be                    mv      a1,a5
 a0c:   4505                    li      a0,1
 a0e:   d03ff0ef                jal     ra,710 <write@plt>
 a12:   fbc42783                lw      a5,-68(s0)
 a16:   853e                    mv      a0,a5

0000000000000a18 <__wrap_abort>:
 a18:   cb9ff0ef                jal     ra,6d0 <_exit@plt>
 a1c:   0001                    nop
 a1e:   00001797                auipc   a5,0x1
 a22:   5f278793                addi    a5,a5,1522 # 2010 <___constval>
 a26:   439c                    lw      a5,0(a5)
 a28:   fbfd                    bnez    a5,a1e <__wrap_abort+0x6>
 a2a:   0001                    nop
 a2c:   60a6                    ld      ra,72(sp)
 a2e:   6406                    ld      s0,64(sp)
 a30:   6161                    addi    sp,sp,80
 a32:   8082                    ret
 a34:   1141                    addi    sp,sp,-16
 a36:   e406                    sd      ra,8(sp)
 a38:   e022                    sd      s0,0(sp)
 a3a:   0800                    addi    s0,sp,16
 a3c:   4651                    li      a2,20
 a3e:   00000597                auipc   a1,0x0
 a42:   0e258593                addi    a1,a1,226 # b20 <string+0x28>
 a46:   4505                    li      a0,1
 a48:   cc9ff0ef                jal     ra,710 <write@plt>
 a4c:   c75ff0ef                jal     ra,6c0 <abort@plt>
 a50:   0001                    nop
 a52:   00001797                auipc   a5,0x1
 a56:   5be78793                addi    a5,a5,1470 # 2010 <___constval>
 a5a:   439c                    lw      a5,0(a5)
 a5c:   fbfd                    bnez    a5,a52 <__wrap_abort+0x3a>
 a5e:   fbbff0ef                jal     ra,a18 <__wrap_abort>

0000000000000a62 <__wrap_main>:
 a62:   7179                    addi    sp,sp,-48
 a64:   f406                    sd      ra,40(sp)
 a66:   f022                    sd      s0,32(sp)
 a68:   1800                    addi    s0,sp,48
 a6a:   87aa                    mv      a5,a0
 a6c:   feb43023                sd      a1,-32(s0)
 a70:   fcc43c23                sd      a2,-40(s0)
 a74:   fef42623                sw      a5,-20(s0)
 a78:   fec42783                lw      a5,-20(s0)
 a7c:   fd843603                ld      a2,-40(s0)
 a80:   fe043583                ld      a1,-32(s0)
 a84:   853e                    mv      a0,a5
 a86:   d71ff0ef                jal     ra,7f6 <main>
 a8a:   87aa                    mv      a5,a0
 a8c:   853e                    mv      a0,a5
 a8e:   e63ff0ef                jal     ra,8f0 <__wrap_exit>

0000000000000a92 <__libc_csu_init>:
 a92:   7139                    addi    sp,sp,-64
 a94:   f822                    sd      s0,48(sp)
 a96:   f04a                    sd      s2,32(sp)
 a98:   00001417                auipc   s0,0x1
 a9c:   36840413                addi    s0,s0,872 # 1e00 <__frame_dummy_init_array_entry>
 aa0:   00001917                auipc   s2,0x1
 aa4:   36890913                addi    s2,s2,872 # 1e08 <__init_array_end>
 aa8:   40890933                sub     s2,s2,s0
 aac:   fc06                    sd      ra,56(sp)
 aae:   f426                    sd      s1,40(sp)
 ab0:   ec4e                    sd      s3,24(sp)
 ab2:   e852                    sd      s4,16(sp)
 ab4:   e456                    sd      s5,8(sp)
 ab6:   40395913                srai    s2,s2,0x3
 aba:   00090f63                beqz    s2,ad8 <__libc_csu_init+0x46>
 abe:   8ab2                    mv      s5,a2
 ac0:   8a2e                    mv      s4,a1
 ac2:   89aa                    mv      s3,a0
 ac4:   4481                    li      s1,0
 ac6:   601c                    ld      a5,0(s0)
 ac8:   8656                    mv      a2,s5
 aca:   85d2                    mv      a1,s4
 acc:   854e                    mv      a0,s3
 ace:   0485                    addi    s1,s1,1
 ad0:   9782                    jalr    a5
 ad2:   0421                    addi    s0,s0,8
 ad4:   fe9919e3                bne     s2,s1,ac6 <__libc_csu_init+0x34>
 ad8:   70e2                    ld      ra,56(sp)
 ada:   7442                    ld      s0,48(sp)
 adc:   74a2                    ld      s1,40(sp)
 ade:   7902                    ld      s2,32(sp)
 ae0:   69e2                    ld      s3,24(sp)
 ae2:   6a42                    ld      s4,16(sp)
 ae4:   6aa2                    ld      s5,8(sp)
 ae6:   6121                    addi    sp,sp,64
 ae8:   8082                    ret

0000000000000aea <__libc_csu_fini>:
 aea:   8082                    ret

Note how both __wrap_exit and __warp_abort are pointing to the wrong addresses: __wrap_exit should point to 0x8f4, and __wrap_abort should point to 0xa34.
Comment 1 Jim Wilson 2018-01-30 03:29:51 UTC
The function size is wrong too.

The function address/size is modified in riscv_relax_delete_bytes in elfnn-riscv.c.  This goes through the symbol table, and looks for any symbol whose address/size if affected by the deleted bytes, and then adjusts the symbol address/size by the deleted byte count.

The problem here is that for part of the link, for __wrap_exit, we have two identical symbols with the same address and size.  Hence, when we adjust symbols, we adjust __wrap_exit twice, causing us to delete too much from both its address and size.

I don't know why we have two identical symbols yet, I will be looking at that next.
Comment 2 Jim Wilson 2018-02-02 19:38:47 UTC
The problem is with testglue.o, which has both a definition of __wrap_exit, and an undefined reference to exit.  With the --wrap option, the later gets renamed to __wrap_exit, and we now have two references to the same symbol in the symbol table, which violates assumptions in the riscv port.

Looking at other ports, I see that 3 of them have a solution for this, to check for duplicate symbols and ignore them.  The solution is O(N^2) but we only need it when --wrap is used, so perhaps it is OK.  I do prefer to use code which is already used in other ports.

Although looking closer at the code to support deleting bytes during relaxation, it looks like every port is broken in at least one way.  Here is my unverified list:

no wrap symbol support and no symbol size support
no wrap symbol support
O(N^2) solution to wrap symbols, but no symbol size support

Maybe worth a meta bug to track?

Anyways, I have a patch for elfnn-riscv.c based on the elf32-crx.c support, and will commit soon.
Comment 3 cvs-commit@gcc.gnu.org 2018-02-02 20:44:22 UTC
The master branch has been updated by Jim Wilson <wilson@sourceware.org>:


commit 7f02625eb48105e100d2da58091d56978ed041ef
Author: Jim Wilson <jimw@sifive.com>
Date:   Fri Feb 2 12:43:09 2018 -0800

    RISC-V: Fix --wrap and relaxation conflict.
    	PR ld/22756
    	* elfnn-riscv.c (riscv_relax_delete_bytes): New parameter link_info.
    	If link_info->wrap_hash, check for a duplicate symbol and ignore.
    	(_bfd_riscv_relax_call, bfd_riscv_relax_lui, _bfd_riscv_relax_tls_le):
    	Pass new argument to riscv_relax_delete_bytes.
    	(_bfd_riscv_relax_align, _bfd_riscv_relax_delete): Likewise.  Remove
    	ATTRIBUTE_UNUSED from link_info parameter.
Comment 4 Jim Wilson 2018-03-01 01:00:59 UTC
I found another problem with linker relaxation and symbol sizes.

gamma05:2463$ cat tmp.c
extern void sub3 (void);

void __attribute__ ((noinline))
sub2 (void)
  sub3 ();

void __attribute__ ((noinline))
sub1 (void)

main (void)
  sub2 ();
  return 0;
gamma05:2464$ cat tmp2.c
sub3 (void)
gamma05:2465$ riscv32-unknown-linux-gnu-gcc -O2 tmp.c tmp2.c
gamma05:2466$ riscv32-unknown-linux-gnu-nm -S --size-sort a.out
00012018 00000001 b completed.5737
00010352 00000002 T __libc_csu_fini
000102f4 00000002 T sub2
000102f8 00000002 T sub3
00012010 00000004 G _IO_stdin_used
00010240 0000000e T main
00010250 00000026 T _start
000102fa 00000058 T __libc_csu_init
000102f6 fffffffc T sub1

Sub1 has a size of (unsigned) -4 which can't be right.  The problem here is that we update the symbol address first, and then we use the new address when checking to see if the size should be updated also which is wrong.  The size check needs to use the old address.  But since we can't have deleted relocs spanning symbols, we can fix this by adding an "else".

Looking at all ports that handle both address and size updates after deleting bytes, I see that only elf32-msp430.c handles this correctly.  And soon risc-v will get it right also.  The other 7 appear to get this wrong.
Comment 5 cvs-commit@gcc.gnu.org 2018-03-01 22:19:46 UTC
The master branch has been updated by Jim Wilson <wilson@sourceware.org>:


commit 788af978df01c3667be99a1607b774f5fa844113
Author: Jim Wilson <jimw@sifive.com>
Date:   Thu Mar 1 14:17:57 2018 -0800

    RISC-V: Fix symbol size bug when relaxation deletes bytes.
    	PR 22756
    	* elfnn-riscv.c (riscv_relax_delete_bytes): When adjust st_size, use
    	else if instead of if.
Comment 6 Andreas Schwab 2018-05-31 21:28:18 UTC
Created attachment 11048 [details]

This is another test case that goes wrong, see the link file for the linker command.  This is the tail of the objdump -d output:

000000000000ec50 <setkey_r@GLIBC_2.27>:
    ec50:       6406                    ld      s0,64(sp)
    ec52:       74e2                    ld      s1,56(sp)
    ec54:       7942                    ld      s2,48(sp)
    ec56:       79a2                    ld      s3,40(sp)
    ec58:       6161                    addi    sp,sp,80
    ec5a:       8082                    ret
    ec5c:                       jal     ra,10c5c <_crypt_psbox+0x9c>

000000000000ec5e <encrypt_r@GLIBC_2.27>:
    ec5e:       a45f 6785 95be          0x95be6785a45f
    ec64:       80358593                addi    a1,a1,-2045
    ec68:       99f1                    andi    a1,a1,-4
    ec6a:                       j       1dc6a <_crypt_m_sbox+0xaa>

000000000000ec6c <setkey@GLIBC_2.27>:
    ec6c:       ec7f                    0xec7f
    ec6e:       6785                    lui     a5,0x1
    ec70:       963e                    add     a2,a2,a5
    ec72:                       li      a2,0

000000000000ec74 <encrypt@GLIBC_2.27>:
    ec74:       8036                    c.mv    zero,a3
    ec76:       9a71                    andi    a2,a2,-4
    ec78:       f3dff06f                j       ebb4 <do_encrypt_r>
    ec7c:       0001e597                auipc   a1,0x1e
    ec80:       5ec58593                addi    a1,a1,1516 # 2d268 <nr_encrypt_ctx>
    ec84:       eadff06f                j       eb30 <do_setkey_r>
    ec88:       0001e617                auipc   a2,0x1e
    ec8c:       5e060613                addi    a2,a2,1504 # 2d268 <nr_encrypt_ctx>
    ec90:       f25ff06f                j       ebb4 <do_encrypt_r>
Comment 7 Andreas Schwab 2018-06-01 18:37:44 UTC
This simpler patch instead of #c3 fixes both the original problem and the preceding test case.

diff --git c/bfd/elfnn-riscv.c w/bfd/elfnn-riscv.c
index 5f66f4f2d9..9c2b48baf5 100644
--- c/bfd/elfnn-riscv.c
+++ w/bfd/elfnn-riscv.c
@@ -2653,6 +2653,14 @@ riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count)
   for (i = 0; i < symcount; i++)
       struct elf_link_hash_entry *sym_hash = sym_hashes[i];
+      unsigned j;
+      /* Check for symbol aliases, don't adjust the same symbol twice.  */
+      for (j = 0; j < i; j++)
+	if (sym_hashes[j] == sym_hash)
+	  break;
+      if (j < i)
+	continue;
       if ((sym_hash->root.type == bfd_link_hash_defined
 	   || sym_hash->root.type == bfd_link_hash_defweak)
Comment 8 Jim Wilson 2018-06-01 23:05:55 UTC
Linker relaxation that deletes code is O(m*n) where m is the number of relocations and n is the number of symbols.  There have been complaints about this.  This makes the RISC-V linker slower than other targets that don't have this feature.  Checking for duplicate symbols makes this O(m*n^2) which is even worse, so I want to only check for duplicates when necessary.

The only significant difference between my patch and yours is that you are checking for duplicates unconditionally, where I check for them only when wrapped symbols are present.  Debugging the run-time differences between your patch and mine, I see that all of the affected symbols are versioned_hidden.  This happens in the nondeflt_vers code in elflink.c, where it makes the sym_hash entry for foo point to the sym_hash entry for foo@BAR.  This would explain why none of the other ports that delete code during linker relaxation have seen this problem, as they are almost all embedded targets, and symbol versioning is not common outside glibc.

Here is the patch I propose to add to fix this

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index b82e655b7b..a0bdee54b2 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2708,9 +2708,12 @@ riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count,
         call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
         the same symbol (which is __wrap_SYMBOL), but still exist as two
         different symbols in 'sym_hashes', we don't want to adjust
-        the global symbol __wrap_SYMBOL twice.
-        This check is only relevant when symbols are being wrapped.  */
-      if (link_info->wrap_hash != NULL)
+        the global symbol __wrap_SYMBOL twice.  */
+      /* The same problem occurs with symbols that are versioned_hidden, as
+        foo becomes an alias for foo@BAR, and hence they need the same
+        treatment.  */
+      if (link_info->wrap_hash != NULL
+         || sym_hash->versioned == versioned_hidden)
          struct elf_link_hash_entry **cur_sym_hashes;
Comment 9 cvs-commit@gcc.gnu.org 2018-06-03 21:46:31 UTC
The master branch has been updated by Jim Wilson <wilson@sourceware.org>:


commit 137b5cbd204241ec47873c353cf7e41b47182d53
Author: Jim Wilson <jimw@sifive.com>
Date:   Sun Jun 3 14:44:44 2018 -0700

    RISC-V: Fix symbol address problem with versioned symbols.
    	PR ld/22756
    	* elfnn-riscv.c (riscv_relax_delete_bytes): Add versioned_hidden check
    	to code that ignores duplicate symbols.
Comment 10 Nelson Chu 2021-07-21 01:21:18 UTC
The pr28021 is related to this pr, and the new problem is resolved by Michael Matz.  So also marked as resolved and fixed for pr22756.