relocate.S 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * relocate - common relocation function for ARM U-Boot
  3. *
  4. * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <linux/linkage.h>
  9. /*
  10. * void relocate_code(addr_moni)
  11. *
  12. * This function relocates the monitor code.
  13. *
  14. * NOTE:
  15. * To prevent the code below from containing references with an R_ARM_ABS32
  16. * relocation record type, we never refer to linker-defined symbols directly.
  17. * Instead, we declare literals which contain their relative location with
  18. * respect to relocate_code, and at run time, add relocate_code back to them.
  19. */
  20. ENTRY(relocate_code)
  21. ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
  22. subs r4, r0, r1 /* r4 <- relocation offset */
  23. beq relocate_done /* skip relocation */
  24. ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */
  25. copy_loop:
  26. ldmia r1!, {r10-r11} /* copy from source address [r1] */
  27. stmia r0!, {r10-r11} /* copy to target address [r0] */
  28. cmp r1, r2 /* until source end address [r2] */
  29. blo copy_loop
  30. /*
  31. * fix .rel.dyn relocations
  32. */
  33. ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
  34. ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
  35. fixloop:
  36. ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
  37. and r1, r1, #0xff
  38. cmp r1, #23 /* relative fixup? */
  39. bne fixnext
  40. /* relative fix: increase location by offset */
  41. add r0, r0, r4
  42. ldr r1, [r0]
  43. add r1, r1, r4
  44. str r1, [r0]
  45. fixnext:
  46. cmp r2, r3
  47. blo fixloop
  48. relocate_done:
  49. #ifdef __XSCALE__
  50. /*
  51. * On xscale, icache must be invalidated and write buffers drained,
  52. * even with cache disabled - 4.2.7 of xscale core developer's manual
  53. */
  54. mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
  55. mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
  56. #endif
  57. /* ARMv4- don't know bx lr but the assembler fails to see that */
  58. #ifdef __ARM_ARCH_4__
  59. mov pc, lr
  60. #else
  61. bx lr
  62. #endif
  63. ENDPROC(relocate_code)