reloc_riscv_efi.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* reloc_riscv.c - position independent ELF shared object relocator
  3. Copyright (C) 2018 Alexander Graf <agraf@suse.de>
  4. Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  5. Copyright (C) 1999 Hewlett-Packard Co.
  6. Contributed by David Mosberger <davidm@hpl.hp.com>.
  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 <elf.h>
  36. #if __riscv_xlen == 64
  37. #define Elf_Dyn Elf64_Dyn
  38. #define Elf_Rela Elf64_Rela
  39. #define ELF_R_TYPE ELF64_R_TYPE
  40. #else
  41. #define Elf_Dyn Elf32_Dyn
  42. #define Elf_Rela Elf32_Rela
  43. #define ELF_R_TYPE ELF32_R_TYPE
  44. #endif
  45. efi_status_t EFIAPI _relocate(long ldbase, Elf_Dyn *dyn)
  46. {
  47. long relsz = 0, relent = 0;
  48. Elf_Rela *rel = 0;
  49. unsigned long *addr;
  50. int i;
  51. for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
  52. switch (dyn[i].d_tag) {
  53. case DT_RELA:
  54. rel = (Elf_Rela *)((ulong)dyn[i].d_un.d_ptr + ldbase);
  55. break;
  56. case DT_RELASZ:
  57. relsz = dyn[i].d_un.d_val;
  58. break;
  59. case DT_RELAENT:
  60. relent = dyn[i].d_un.d_val;
  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 (ELF_R_TYPE(rel->r_info)) {
  73. case R_RISCV_RELATIVE:
  74. addr = (ulong *)(ldbase + rel->r_offset);
  75. *addr = ldbase + rel->r_addend;
  76. break;
  77. default:
  78. /* Panic */
  79. while (1) ;
  80. }
  81. rel = (Elf_Rela *)((char *)rel + relent);
  82. relsz -= relent;
  83. }
  84. return EFI_SUCCESS;
  85. }