reloc_loongarch64.c 3.0 KB

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