relocate.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <elf.h>
  8. #include <asm-generic/sections.h>
  9. extern ulong __image_copy_start;
  10. extern ulong __ivt_end;
  11. DECLARE_GLOBAL_DATA_PTR;
  12. int copy_uboot_to_ram(void)
  13. {
  14. size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
  15. memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
  16. return 0;
  17. }
  18. int clear_bss(void)
  19. {
  20. ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
  21. size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
  22. memset((void *)dst_addr, 0x00, len);
  23. return 0;
  24. }
  25. /*
  26. * Base functionality is taken from x86 version with added ARC-specifics
  27. */
  28. int do_elf_reloc_fixups(void)
  29. {
  30. Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
  31. Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
  32. debug("Section .rela.dyn is located at %08x-%08x\n",
  33. (unsigned int)re_src, (unsigned int)re_end);
  34. Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
  35. Elf32_Addr *offset_ptr_ram;
  36. do {
  37. /* Get the location from the relocation entry */
  38. offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
  39. /* Check that the location of the relocation is in .text */
  40. if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
  41. offset_ptr_rom > last_offset) {
  42. unsigned int val;
  43. /* Switch to the in-RAM version */
  44. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  45. gd->reloc_off);
  46. debug("Patching value @ %08x (relocated to %08x)\n",
  47. (unsigned int)offset_ptr_rom,
  48. (unsigned int)offset_ptr_ram);
  49. /*
  50. * Use "memcpy" because target location might be
  51. * 16-bit aligned on ARC so we may need to read
  52. * byte-by-byte. On attempt to read entire word by
  53. * CPU throws an exception
  54. */
  55. memcpy(&val, offset_ptr_ram, sizeof(int));
  56. #ifdef __LITTLE_ENDIAN__
  57. /* If location in ".text" section swap value */
  58. if ((unsigned int)offset_ptr_rom <
  59. (unsigned int)&__ivt_end)
  60. val = (val << 16) | (val >> 16);
  61. #endif
  62. /* Check that the target points into executable */
  63. if (val >= (unsigned int)&__image_copy_start && val <=
  64. (unsigned int)&__image_copy_end) {
  65. val += gd->reloc_off;
  66. #ifdef __LITTLE_ENDIAN__
  67. /* If location in ".text" section swap value */
  68. if ((unsigned int)offset_ptr_rom <
  69. (unsigned int)&__ivt_end)
  70. val = (val << 16) | (val >> 16);
  71. #endif
  72. memcpy(offset_ptr_ram, &val, sizeof(int));
  73. }
  74. }
  75. last_offset = offset_ptr_rom;
  76. } while (++re_src < re_end);
  77. return 0;
  78. }