relocate.c 4.2 KB

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