reloc_aarch64_efi.c 2.7 KB

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