relocate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* Get the location from the relocation entry */
  47. offset_ptr_rom = (Elf64_Addr *)(uintptr_t)re_src->r_offset;
  48. /* Check that the location of the relocation is in .text */
  49. if (offset_ptr_rom >= (Elf64_Addr *)(uintptr_t)text_base &&
  50. offset_ptr_rom > last_offset) {
  51. /* Switch to the in-RAM version */
  52. offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +
  53. gd->reloc_off);
  54. /* Check that the target points into .text */
  55. if (*offset_ptr_ram >= text_base &&
  56. *offset_ptr_ram <= text_base + size) {
  57. *offset_ptr_ram = gd->reloc_off +
  58. re_src->r_addend;
  59. } else {
  60. debug(" %p: %lx: rom reloc %lx, ram %p, value %lx, limit %lX\n",
  61. re_src, (ulong)re_src->r_info,
  62. (ulong)re_src->r_offset, offset_ptr_ram,
  63. (ulong)*offset_ptr_ram, text_base + size);
  64. }
  65. } else {
  66. debug(" %p: %lx: rom reloc %lx, last %p\n", re_src,
  67. (ulong)re_src->r_info, (ulong)re_src->r_offset,
  68. last_offset);
  69. }
  70. last_offset = offset_ptr_rom;
  71. } while (++re_src < re_end);
  72. }
  73. #else
  74. static void do_elf_reloc_fixups32(unsigned int text_base, uintptr_t size,
  75. Elf32_Rel *re_src, Elf32_Rel *re_end)
  76. {
  77. Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
  78. Elf32_Addr *offset_ptr_ram;
  79. do {
  80. /* Get the location from the relocation entry */
  81. offset_ptr_rom = (Elf32_Addr *)(uintptr_t)re_src->r_offset;
  82. /* Check that the location of the relocation is in .text */
  83. if (offset_ptr_rom >= (Elf32_Addr *)(uintptr_t)text_base &&
  84. offset_ptr_rom > last_offset) {
  85. /* Switch to the in-RAM version */
  86. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  87. gd->reloc_off);
  88. /* Check that the target points into .text */
  89. if (*offset_ptr_ram >= text_base &&
  90. *offset_ptr_ram <= text_base + size) {
  91. *offset_ptr_ram += gd->reloc_off;
  92. } else {
  93. debug(" %p: rom reloc %x, ram %p, value %x, limit %lX\n",
  94. re_src, re_src->r_offset, offset_ptr_ram,
  95. *offset_ptr_ram, text_base + size);
  96. }
  97. } else {
  98. debug(" %p: rom reloc %x, last %p\n", re_src,
  99. re_src->r_offset, last_offset);
  100. }
  101. last_offset = offset_ptr_rom;
  102. } while (++re_src < re_end);
  103. }
  104. #endif
  105. /*
  106. * This function has more error checking than you might expect. Please see
  107. * this commit message for more information:
  108. * 62f7970a x86: Add error checking to x86 relocation code
  109. */
  110. int do_elf_reloc_fixups(void)
  111. {
  112. void *re_src = (void *)(&__rel_dyn_start);
  113. void *re_end = (void *)(&__rel_dyn_end);
  114. uint text_base;
  115. /* The size of the region of u-boot that runs out of RAM. */
  116. uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
  117. if (gd->flags & GD_FLG_SKIP_RELOC)
  118. return 0;
  119. if (re_src == re_end)
  120. panic("No relocation data");
  121. #ifdef CONFIG_SYS_TEXT_BASE
  122. text_base = CONFIG_SYS_TEXT_BASE;
  123. #else
  124. panic("No CONFIG_SYS_TEXT_BASE");
  125. #endif
  126. #if CONFIG_IS_ENABLED(X86_64)
  127. do_elf_reloc_fixups64(text_base, size, re_src, re_end);
  128. #else
  129. do_elf_reloc_fixups32(text_base, size, re_src, re_end);
  130. #endif
  131. return 0;
  132. }