blob: ab90430dced64897e60c63cc739bd36a9f4cc52e [file] [log] [blame]
Albert ARIBAUD3da0e572013-05-19 01:48:15 +00001/*
2 * relocate - common relocation function for ARM U-Boot
3 *
4 * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Albert ARIBAUD3da0e572013-05-19 01:48:15 +00007 */
8
9#include <linux/linkage.h>
10
11/*
12 * void relocate_code(addr_moni)
13 *
14 * This function relocates the monitor code.
15 *
16 * NOTE:
17 * To prevent the code below from containing references with an R_ARM_ABS32
18 * relocation record type, we never refer to linker-defined symbols directly.
19 * Instead, we declare literals which contain their relative location with
20 * respect to relocate_code, and at run time, add relocate_code back to them.
21 */
22
23ENTRY(relocate_code)
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020024 ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
25 subs r9, r0, r1 /* r9 <- relocation offset */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000026 beq relocate_done /* skip relocation */
Albert ARIBAUDd026dec2013-06-11 14:17:33 +020027 ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000028
29copy_loop:
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020030 ldmia r1!, {r10-r11} /* copy from source address [r1] */
31 stmia r0!, {r10-r11} /* copy to target address [r0] */
32 cmp r1, r2 /* until source end address [r2] */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000033 blo copy_loop
34
35 /*
36 * fix .rel.dyn relocations
37 */
Albert ARIBAUD47bd65e2013-06-11 14:17:34 +020038 ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
39 ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000040fixloop:
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020041 ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
42 and r1, r1, #0xff
43 cmp r1, #23 /* relative fixup? */
44 bne fixnext
45
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000046 /* relative fix: increase location by offset */
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020047 add r0, r0, r9
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000048 ldr r1, [r0]
49 add r1, r1, r9
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000050 str r1, [r0]
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020051fixnext:
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000052 cmp r2, r3
53 blo fixloop
54
55relocate_done:
56
Mike Dunn9dc8fef2013-06-21 09:12:28 -070057#ifdef __XSCALE__
58 /*
59 * On xscale, icache must be invalidated and write buffers drained,
60 * even with cache disabled - 4.2.7 of xscale core developer's manual
61 */
62 mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
63 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
64#endif
65
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000066 /* ARMv4- don't know bx lr but the assembler fails to see that */
67
68#ifdef __ARM_ARCH_4__
69 mov pc, lr
70#else
71 bx lr
72#endif
73
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000074ENDPROC(relocate_code)