039-LoongArch-ld-Fix-relocation-error-of-pcrel.patch 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. From 509a2ec6ad3ea7eb3f4cf59538cf636a2126e4c3 Mon Sep 17 00:00:00 2001
  2. From: liuzhensong <[email protected]>
  3. Date: Fri, 2 Sep 2022 16:29:14 +0800
  4. Subject: [PATCH 039/160] LoongArch:ld: Fix relocation error of pcrel.
  5. Patch for branch 2.39.
  6. Need to reduce the address of pc when using
  7. reloction R_LARCH_SOP_PUSH_PCREL.
  8. bfd/
  9. * elfnn-loongarch.c
  10. ---
  11. bfd/elfnn-loongarch.c | 3 +-
  12. ld/testsuite/ld-loongarch-elf/pcrel-const.d | 14 +++++++
  13. ld/testsuite/ld-loongarch-elf/pcrel-const.lds | 14 +++++++
  14. ld/testsuite/ld-loongarch-elf/pcrel-const.s | 12 ++++++
  15. ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++
  16. 5 files changed, 81 insertions(+), 1 deletion(-)
  17. create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.d
  18. create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.lds
  19. create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.s
  20. create mode 100644 ld/testsuite/ld-loongarch-elf/pr.exp
  21. --- a/bfd/elfnn-loongarch.c
  22. +++ b/bfd/elfnn-loongarch.c
  23. @@ -2342,9 +2342,10 @@ loongarch_elf_relocate_section (bfd *out
  24. case R_LARCH_SOP_PUSH_PLT_PCREL:
  25. unresolved_reloc = false;
  26. - if (resolved_to_const)
  27. + if (!is_undefweak && resolved_to_const)
  28. {
  29. relocation += rel->r_addend;
  30. + relocation -= pc;
  31. break;
  32. }
  33. else if (is_undefweak)
  34. --- /dev/null
  35. +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.d
  36. @@ -0,0 +1,14 @@
  37. +#as: -mla-global-with-pcrel
  38. +#objdump: -Drsz
  39. +
  40. +.*:[ ]+file format .*
  41. +
  42. +
  43. +Disassembly of section .text:
  44. +
  45. +.* <foo>:
  46. +#...
  47. +[ ]+8:[ ]+02c04084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+16\(0x10\)
  48. +#...
  49. +0+14 <__sec_end>:
  50. +#pass
  51. --- /dev/null
  52. +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
  53. @@ -0,0 +1,14 @@
  54. +ENTRY(foo);
  55. +SECTIONS
  56. +{
  57. + .text : {
  58. + *(.text*)
  59. + }
  60. +
  61. + .data : {
  62. + __sec_start = .;
  63. + *(.gzdata)
  64. + __sec_end = .;
  65. + }
  66. +}
  67. +PROVIDE(__sec_size = __sec_end);
  68. --- /dev/null
  69. +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
  70. @@ -0,0 +1,12 @@
  71. + .text
  72. + .align 2
  73. + .globl foo
  74. + .type foo, @function
  75. +foo:
  76. + nop
  77. + la.global $r4,__sec_size
  78. + ldptr.w $r4,$r4,0
  79. + jr $r1
  80. + .size foo, .-foo
  81. + .data
  82. + .word 1
  83. --- /dev/null
  84. +++ b/ld/testsuite/ld-loongarch-elf/pr.exp
  85. @@ -0,0 +1,39 @@
  86. +# Expect script for LoongArch ELF linker tests
  87. +# Copyright (C) 2022 Free Software Foundation, Inc.
  88. +#
  89. +# This file is part of the GNU Binutils.
  90. +#
  91. +# This program is free software; you can redistribute it and/or modify
  92. +# it under the terms of the GNU General Public License as published by
  93. +# the Free Software Foundation; either version 3 of the License, or
  94. +# (at your option) any later version.
  95. +#
  96. +# This program is distributed in the hope that it will be useful,
  97. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  98. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  99. +# GNU General Public License for more details.
  100. +#
  101. +# You should have received a copy of the GNU General Public License
  102. +# along with this program; if not, write to the Free Software
  103. +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  104. +# MA 02110-1301, USA.
  105. +#
  106. +
  107. +if ![istarget loongarch64-*-*] {
  108. + return
  109. +}
  110. +
  111. +set link_tests [list \
  112. + [list \
  113. + "pcrel const" \
  114. + "-T pcrel-const.lds" "" \
  115. + "-mla-global-with-pcrel" \
  116. + { pcrel-const.s } \
  117. + [list \
  118. + [list objdump -D pcrel-const.d] \
  119. + ] \
  120. + "pcrel-const" \
  121. + ] \
  122. +]
  123. +
  124. +run_ld_link_tests $link_tests