0900-Correct-nios2-_gp-address-computation.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. From 5eeb7401eed2f26d5fc255de816ca70a2cb9374e Mon Sep 17 00:00:00 2001
  2. From: Sandra Loosemore <sandra@codesourcery.com>
  3. Date: Sun, 27 Dec 2015 12:30:26 -0800
  4. Subject: [PATCH 900/901] Correct nios2 _gp address computation.
  5. 2015-12-27 Sandra Loosemore <sandra@codesourcery.com>
  6. bfd/
  7. * elf32-nios2.c (nios2_elf_assign_gp): Correct computation of _gp
  8. address.
  9. (nios2_elf32_relocate_section): Tidy code for R_NIOS2_GPREL error
  10. messages.
  11. [Romain:
  12. - backport upstream patch on 2.26
  13. - drop bfd/ChangeLog entry]
  14. Signed-off-by: Romain Naour <romain.naour@gmail.com>
  15. ---
  16. bfd/elf32-nios2.c | 31 +++++++++++++++++++++----------
  17. 1 file changed, 21 insertions(+), 10 deletions(-)
  18. diff --git a/bfd/elf32-nios2.c b/bfd/elf32-nios2.c
  19. index 6b29d8b..01ebd6e 100644
  20. --- a/bfd/elf32-nios2.c
  21. +++ b/bfd/elf32-nios2.c
  22. @@ -3086,7 +3086,15 @@ lookup:
  23. case bfd_link_hash_defined:
  24. case bfd_link_hash_defweak:
  25. gp_found = TRUE;
  26. - *pgp = lh->u.def.value;
  27. + {
  28. + asection *sym_sec = lh->u.def.section;
  29. + bfd_vma sym_value = lh->u.def.value;
  30. +
  31. + if (sym_sec->output_section)
  32. + sym_value = (sym_value + sym_sec->output_offset
  33. + + sym_sec->output_section->vma);
  34. + *pgp = sym_value;
  35. + }
  36. break;
  37. case bfd_link_hash_indirect:
  38. case bfd_link_hash_warning:
  39. @@ -3719,7 +3727,6 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  40. struct elf32_nios2_link_hash_entry *eh;
  41. bfd_vma relocation;
  42. bfd_vma gp;
  43. - bfd_vma reloc_address;
  44. bfd_reloc_status_type r = bfd_reloc_ok;
  45. const char *name = NULL;
  46. int r_type;
  47. @@ -3762,12 +3769,6 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  48. if (bfd_link_relocatable (info))
  49. continue;
  50. - if (sec && sec->output_section)
  51. - reloc_address = (sec->output_section->vma + sec->output_offset
  52. - + rel->r_offset);
  53. - else
  54. - reloc_address = 0;
  55. -
  56. if (howto)
  57. {
  58. switch (howto->type)
  59. @@ -3816,6 +3817,15 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  60. /* Turns an absolute address into a gp-relative address. */
  61. if (!nios2_elf_assign_gp (output_bfd, &gp, info))
  62. {
  63. + bfd_vma reloc_address;
  64. +
  65. + if (sec && sec->output_section)
  66. + reloc_address = (sec->output_section->vma
  67. + + sec->output_offset
  68. + + rel->r_offset);
  69. + else
  70. + reloc_address = 0;
  71. +
  72. format = _("global pointer relative relocation at address "
  73. "0x%08x when _gp not defined\n");
  74. sprintf (msgbuf, format, reloc_address);
  75. @@ -3825,7 +3835,7 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  76. else
  77. {
  78. bfd_vma symbol_address = rel->r_addend + relocation;
  79. - relocation = relocation + rel->r_addend - gp;
  80. + relocation = symbol_address - gp;
  81. rel->r_addend = 0;
  82. if (((signed) relocation < -32768
  83. || (signed) relocation > 32767)
  84. @@ -3833,6 +3843,8 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  85. || h->root.type == bfd_link_hash_defined
  86. || h->root.type == bfd_link_hash_defweak))
  87. {
  88. + if (h)
  89. + name = h->root.root.string;
  90. format = _("Unable to reach %s (at 0x%08x) from the "
  91. "global pointer (at 0x%08x) because the "
  92. "offset (%d) is out of the allowed range, "
  93. @@ -3848,7 +3860,6 @@ nios2_elf32_relocate_section (bfd *output_bfd,
  94. rel->r_offset, relocation,
  95. rel->r_addend);
  96. }
  97. -
  98. break;
  99. case R_NIOS2_UJMP:
  100. r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
  101. --
  102. 2.4.3