relocate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008-2011
  4. * Graeme Russ, <graeme.russ@gmail.com>
  5. *
  6. * (C) Copyright 2002
  7. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  8. *
  9. * (C) Copyright 2002
  10. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  11. *
  12. * (C) Copyright 2002
  13. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  14. * Marius Groeger <mgroeger@sysgo.de>
  15. */
  16. #include <common.h>
  17. #include <log.h>
  18. #include <relocate.h>
  19. #include <asm/u-boot-x86.h>
  20. #include <asm/sections.h>
  21. #include <elf.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. int copy_uboot_to_ram(void)
  24. {
  25. size_t len = (uintptr_t)&__data_end - (uintptr_t)&__text_start;
  26. if (gd->flags & GD_FLG_SKIP_RELOC)
  27. return 0;
  28. memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
  29. return 0;
  30. }
  31. int clear_bss(void)
  32. {
  33. ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
  34. size_t len = (uintptr_t)&__bss_end - (uintptr_t)&__bss_start;
  35. if (gd->flags & GD_FLG_SKIP_RELOC)
  36. return 0;
  37. memset((void *)dst_addr, 0x00, len);
  38. return 0;
  39. }
  40. #if CONFIG_IS_ENABLED(X86_64)
  41. static void do_elf_reloc_fixups64(unsigned int text_base, uintptr_t size,
  42. Elf64_Rela *re_src, Elf64_Rela *re_end)
  43. {
  44. Elf64_Addr *offset_ptr_rom, *last_offset = NULL;
  45. Elf64_Addr *offset_ptr_ram;
  46. do {
  47. unsigned long long type = ELF64_R_TYPE(re_src->r_info);
  48. if (type != R_X86_64_RELATIVE) {
  49. printf("%s: unsupported relocation type 0x%llx "
  50. "at %p, ", __func__, type, re_src);
  51. printf("offset = 0x%llx\n", re_src->r_offset);
  52. continue;
  53. }
  54. /* Get the location from the relocation entry */
  55. offset_ptr_rom = (Elf64_Addr *)(uintptr_t)re_src->r_offset;
  56. /* Check that the location of the relocation is in .text */
  57. if (offset_ptr_rom >= (Elf64_Addr *)(uintptr_t)text_base &&
  58. offset_ptr_rom > last_offset) {
  59. /* Switch to the in-RAM version */
  60. offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +
  61. gd->reloc_off);
  62. /* Check that the target points into .text */
  63. if (*offset_ptr_ram >= text_base &&
  64. *offset_ptr_ram <= text_base + size) {
  65. *offset_ptr_ram = gd->reloc_off +
  66. re_src->r_addend;
  67. } else {
  68. debug(" %p: %lx: rom reloc %lx, ram %p, value %lx, limit %lX\n",
  69. re_src, (ulong)re_src->r_info,
  70. (ulong)re_src->r_offset, offset_ptr_ram,
  71. (ulong)*offset_ptr_ram, text_base + size);
  72. }
  73. } else {
  74. debug(" %p: %lx: rom reloc %lx, last %p\n", re_src,
  75. (ulong)re_src->r_info, (ulong)re_src->r_offset,
  76. last_offset);
  77. }
  78. last_offset = offset_ptr_rom;
  79. } while (++re_src < re_end);
  80. }
  81. #else
  82. static void do_elf_reloc_fixups32(unsigned int text_base, uintptr_t size,
  83. Elf32_Rel *re_src, Elf32_Rel *re_end)
  84. {
  85. Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
  86. Elf32_Addr *offset_ptr_ram;
  87. do {
  88. unsigned int type = ELF32_R_TYPE(re_src->r_info);
  89. if (type != R_386_RELATIVE) {
  90. printf("%s: unsupported relocation type 0x%x "
  91. "at %p, ", __func__, type, re_src);
  92. printf("offset = 0x%x\n", re_src->r_offset);
  93. continue;
  94. }
  95. /* Get the location from the relocation entry */
  96. offset_ptr_rom = (Elf32_Addr *)(uintptr_t)re_src->r_offset;
  97. /* Check that the location of the relocation is in .text */
  98. if (offset_ptr_rom >= (Elf32_Addr *)(uintptr_t)text_base &&
  99. offset_ptr_rom > last_offset) {
  100. /* Switch to the in-RAM version */
  101. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  102. gd->reloc_off);
  103. /* Check that the target points into .text */
  104. if (*offset_ptr_ram >= text_base &&
  105. *offset_ptr_ram <= text_base + size) {
  106. *offset_ptr_ram += gd->reloc_off;
  107. } else {
  108. debug(" %p: rom reloc %x, ram %p, value %x, limit %lX\n",
  109. re_src, re_src->r_offset, offset_ptr_ram,
  110. *offset_ptr_ram, text_base + size);
  111. }
  112. } else {
  113. debug(" %p: rom reloc %x, last %p\n", re_src,
  114. re_src->r_offset, last_offset);
  115. }
  116. last_offset = offset_ptr_rom;
  117. } while (++re_src < re_end);
  118. }
  119. #endif
  120. /*
  121. * This function has more error checking than you might expect. Please see
  122. * this commit message for more information:
  123. * 62f7970a x86: Add error checking to x86 relocation code
  124. */
  125. int do_elf_reloc_fixups(void)
  126. {
  127. void *re_src = (void *)(&__rel_dyn_start);
  128. void *re_end = (void *)(&__rel_dyn_end);
  129. uint text_base;
  130. /* The size of the region of u-boot that runs out of RAM. */
  131. uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
  132. if (gd->flags & GD_FLG_SKIP_RELOC)
  133. return 0;
  134. if (re_src == re_end)
  135. panic("No relocation data");
  136. #ifdef CONFIG_SYS_TEXT_BASE
  137. text_base = CONFIG_SYS_TEXT_BASE;
  138. #else
  139. panic("No CONFIG_SYS_TEXT_BASE");
  140. #endif
  141. #if CONFIG_IS_ENABLED(X86_64)
  142. do_elf_reloc_fixups64(text_base, size, re_src, re_end);
  143. #else
  144. do_elf_reloc_fixups32(text_base, size, re_src, re_end);
  145. #endif
  146. return 0;
  147. }