relocate.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
  4. */
  5. #include <common.h>
  6. #include <elf.h>
  7. #include <log.h>
  8. #include <asm-generic/sections.h>
  9. extern ulong __image_copy_start;
  10. extern ulong __ivt_start;
  11. extern ulong __ivt_end;
  12. extern ulong __text_end;
  13. DECLARE_GLOBAL_DATA_PTR;
  14. int copy_uboot_to_ram(void)
  15. {
  16. size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
  17. if (gd->flags & GD_FLG_SKIP_RELOC)
  18. return 0;
  19. memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
  20. return 0;
  21. }
  22. int clear_bss(void)
  23. {
  24. ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
  25. size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
  26. memset((void *)dst_addr, 0x00, len);
  27. return 0;
  28. }
  29. /*
  30. * Base functionality is taken from x86 version with added ARC-specifics
  31. */
  32. int do_elf_reloc_fixups(void)
  33. {
  34. Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
  35. Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
  36. if (gd->flags & GD_FLG_SKIP_RELOC)
  37. return 0;
  38. debug("Section .rela.dyn is located at %08x-%08x\n",
  39. (unsigned int)re_src, (unsigned int)re_end);
  40. Elf32_Addr *offset_ptr_rom;
  41. Elf32_Addr *offset_ptr_ram;
  42. do {
  43. /* Get the location from the relocation entry */
  44. offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
  45. /* Check that the location of the relocation is in .text */
  46. if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
  47. offset_ptr_rom < (Elf32_Addr *)&__image_copy_end) {
  48. unsigned int val, do_swap = 0;
  49. /* Switch to the in-RAM version */
  50. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  51. gd->reloc_off);
  52. #ifdef __LITTLE_ENDIAN__
  53. /* If location in ".text" section swap value */
  54. if (((u32)offset_ptr_rom >= (u32)&__text_start &&
  55. (u32)offset_ptr_rom <= (u32)&__text_end)
  56. #if defined(__ARC700__) || defined(__ARC600__)
  57. || ((u32)offset_ptr_rom >= (u32)&__ivt_start &&
  58. (u32)offset_ptr_rom <= (u32)&__ivt_end)
  59. #endif
  60. )
  61. do_swap = 1;
  62. #endif
  63. debug("Patching value @ %08x (relocated to %08x)%s\n",
  64. (unsigned int)offset_ptr_rom,
  65. (unsigned int)offset_ptr_ram,
  66. do_swap ? ", middle-endian encoded" : "");
  67. /*
  68. * Use "memcpy" because target location might be
  69. * 16-bit aligned on ARC so we may need to read
  70. * byte-by-byte. On attempt to read entire word by
  71. * CPU throws an exception
  72. */
  73. memcpy(&val, offset_ptr_ram, sizeof(int));
  74. if (do_swap)
  75. val = (val << 16) | (val >> 16);
  76. /* Check that the target points into executable */
  77. if (val < (unsigned int)&__image_copy_start ||
  78. val > (unsigned int)&__image_copy_end) {
  79. /* TODO: Use panic() instead of debug()
  80. *
  81. * For some reason GCC might generate
  82. * fake relocation even for LD/SC of constant
  83. * inderectly. See an example below:
  84. * ----------------------->8--------------------
  85. * static int setup_mon_len(void)
  86. * {
  87. * gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  88. * return 0;
  89. * }
  90. * ----------------------->8--------------------
  91. *
  92. * And that's what we get in the binary:
  93. * ----------------------->8--------------------
  94. * 10005cb4 <setup_mon_len>:
  95. * 10005cb4: 193c 3f80 0003 2f80 st 0x32f80,[r25,60]
  96. * 10005cb8: R_ARC_RELATIVE *ABS*-0x10000000
  97. * 10005cbc: 7fe0 j_s.d [blink]
  98. * 10005cbe: 700c mov_s r0,0
  99. * ----------------------->8--------------------
  100. */
  101. debug("Relocation target %08x points outside of image\n",
  102. val);
  103. }
  104. val += gd->reloc_off;
  105. if (do_swap)
  106. val = (val << 16) | (val >> 16);
  107. memcpy(offset_ptr_ram, &val, sizeof(int));
  108. }
  109. } while (++re_src < re_end);
  110. return 0;
  111. }