relocate.c 4.7 KB

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