reloc_x86_64.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* reloc_x86_64.c - position independent x86_64 ELF shared object relocator
  2. Copyright (C) 1999 Hewlett-Packard Co.
  3. Contributed by David Mosberger <davidm@hpl.hp.com>.
  4. Copyright (C) 2005 Intel Co.
  5. Contributed by Fenghua Yu <fenghua.yu@intel.com>.
  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 <elf.h>
  34. #include <link.h> /* get _DYNAMIC decl and ElfW and ELFW macros */
  35. #undef NULL
  36. #define uint64_t efi_uint64_t
  37. #define int64_t efi_int64_t
  38. #define uint32_t efi_uint32_t
  39. #define int32_t efi_int32_t
  40. #define uint16_t efi_uint16_t
  41. #define int16_t efi_int16_t
  42. #define uint8_t efi_uint8_t
  43. #define int8_t efi_int8_t
  44. #undef NULL
  45. #define uint64_t efi_uint64_t
  46. #define int64_t efi_int64_t
  47. #define uint32_t efi_uint32_t
  48. #define int32_t efi_int32_t
  49. #define uint16_t efi_uint16_t
  50. #define int16_t efi_int16_t
  51. #define uint8_t efi_uint8_t
  52. #define int8_t efi_int8_t
  53. #include <efi.h>
  54. #include <efilib.h>
  55. EFI_STATUS _relocate (long ldbase, ElfW(Dyn) *dyn, EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  56. {
  57. long relsz = 0, relent = 0;
  58. ElfW(Rel) *rel = 0;
  59. unsigned long *addr;
  60. int i;
  61. for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
  62. switch (dyn[i].d_tag) {
  63. case DT_RELA:
  64. rel = (ElfW(Rel)*)
  65. ((unsigned long)dyn[i].d_un.d_ptr
  66. + ldbase);
  67. break;
  68. case DT_RELASZ:
  69. relsz = dyn[i].d_un.d_val;
  70. break;
  71. case DT_RELAENT:
  72. relent = dyn[i].d_un.d_val;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. if (!rel && relent == 0)
  79. return EFI_SUCCESS;
  80. if (!rel || relent == 0)
  81. return EFI_LOAD_ERROR;
  82. while (relsz > 0) {
  83. /* apply the relocs */
  84. switch (ELF64_R_TYPE (rel->r_info)) {
  85. case R_X86_64_NONE:
  86. break;
  87. case R_X86_64_RELATIVE:
  88. addr = (unsigned long *)
  89. (ldbase + rel->r_offset);
  90. *addr += ldbase;
  91. break;
  92. default:
  93. break;
  94. }
  95. rel = (ElfW(Rel)*) ((char *) rel + relent);
  96. relsz -= relent;
  97. }
  98. return EFI_SUCCESS;
  99. }