reloc_mips64el.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* reloc_mips64el.c - position independent MIPS64 ELF shared object relocator
  2. Copyright (C) 2014 Linaro Ltd. <[email protected]>
  3. Copyright (C) 1999 Hewlett-Packard Co.
  4. Contributed by David Mosberger <[email protected]>.
  5. Copyright (C) 2017 Lemote Co.
  6. Contributed by Heiher <[email protected]>
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. * Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer in the documentation and/or other materials
  16. provided with the distribution.
  17. * Neither the name of Hewlett-Packard Co. nor the names of its
  18. contributors may be used to endorse or promote products derived
  19. from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  21. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  25. BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  26. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  30. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. SUCH DAMAGE.
  33. */
  34. #include <efi.h>
  35. #include <efilib.h>
  36. #include <elf.h>
  37. EFI_STATUS _relocate (long ldbase, Elf64_Dyn *dyn,
  38. EFI_HANDLE image EFI_UNUSED,
  39. EFI_SYSTEM_TABLE *systab EFI_UNUSED)
  40. {
  41. long relsz = 0, relent = 0, gotsz = 0;
  42. Elf64_Rel *rel = 0;
  43. unsigned long *addr = 0;
  44. int i;
  45. for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
  46. switch (dyn[i].d_tag) {
  47. case DT_REL:
  48. rel = (Elf64_Rel*)
  49. ((unsigned long)dyn[i].d_un.d_ptr
  50. + ldbase);
  51. break;
  52. case DT_RELSZ:
  53. relsz = dyn[i].d_un.d_val;
  54. break;
  55. case DT_RELENT:
  56. relent = dyn[i].d_un.d_val;
  57. break;
  58. case DT_PLTGOT:
  59. addr = (unsigned long *)
  60. ((unsigned long)dyn[i].d_un.d_ptr
  61. + ldbase);
  62. break;
  63. case DT_MIPS_LOCAL_GOTNO:
  64. gotsz = dyn[i].d_un.d_val;
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. if ((!rel && relent == 0) && (!addr && gotsz == 0))
  71. return EFI_SUCCESS;
  72. if ((!rel && relent != 0) || (!addr && gotsz != 0))
  73. return EFI_LOAD_ERROR;
  74. while (gotsz > 0) {
  75. *addr += ldbase;
  76. addr += 1;
  77. gotsz --;
  78. }
  79. while (relsz > 0) {
  80. /* apply the relocs */
  81. switch (ELF64_R_TYPE (swap_uint64 (rel->r_info))) {
  82. case R_MIPS_NONE:
  83. break;
  84. case (R_MIPS_64 << 8) | R_MIPS_REL32:
  85. addr = (unsigned long *)
  86. (ldbase + rel->r_offset);
  87. *addr += ldbase;
  88. break;
  89. default:
  90. break;
  91. }
  92. rel = (Elf64_Rel*) ((char *) rel + relent);
  93. relsz -= relent;
  94. }
  95. return EFI_SUCCESS;
  96. }