reloc_ia32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* reloc_ia32.c - position independent x86 ELF shared object relocator
  2. Copyright (C) 1999 Hewlett-Packard Co.
  3. Contributed by David Mosberger <davidm@hpl.hp.com>.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following
  12. disclaimer in the documentation and/or other materials
  13. provided with the distribution.
  14. * Neither the name of Hewlett-Packard Co. nor the names of its
  15. contributors may be used to endorse or promote products derived
  16. from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  18. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  19. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  22. BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  23. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  27. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. SUCH DAMAGE.
  30. */
  31. #include <elf.h>
  32. #include <link.h> /* get _DYNAMIC decl and ElfW and ELFW macros */
  33. #undef NULL
  34. #define uint64_t efi_uint64_t
  35. #define int64_t efi_int64_t
  36. #define uint32_t efi_uint32_t
  37. #define int32_t efi_int32_t
  38. #define uint16_t efi_uint16_t
  39. #define int16_t efi_int16_t
  40. #define uint8_t efi_uint8_t
  41. #define int8_t efi_int8_t
  42. #undef NULL
  43. #define uint64_t efi_uint64_t
  44. #define int64_t efi_int64_t
  45. #define uint32_t efi_uint32_t
  46. #define int32_t efi_int32_t
  47. #define uint16_t efi_uint16_t
  48. #define int16_t efi_int16_t
  49. #define uint8_t efi_uint8_t
  50. #define int8_t efi_int8_t
  51. #include <efi.h>
  52. #include <efilib.h>
  53. EFI_STATUS _relocate (long ldbase, ElfW(Dyn) *dyn, EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  54. {
  55. long relsz = 0, relent = 0;
  56. ElfW(Rel) *rel = 0;
  57. unsigned long *addr;
  58. int i;
  59. for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
  60. switch (dyn[i].d_tag) {
  61. case DT_REL:
  62. rel = (ElfW(Rel)*)
  63. ((unsigned long)dyn[i].d_un.d_ptr
  64. + ldbase);
  65. break;
  66. case DT_RELSZ:
  67. relsz = dyn[i].d_un.d_val;
  68. break;
  69. case DT_RELENT:
  70. relent = dyn[i].d_un.d_val;
  71. break;
  72. case DT_RELA:
  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 (ELF32_R_TYPE (rel->r_info)) {
  85. case R_386_NONE:
  86. break;
  87. case R_386_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. }