Albert ARIBAUD | 3da0e57 | 2013-05-19 01:48:15 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * relocate - common relocation function for ARM U-Boot |
| 3 | * |
| 4 | * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <linux/linkage.h> |
| 26 | |
| 27 | /* |
| 28 | * void relocate_code(addr_moni) |
| 29 | * |
| 30 | * This function relocates the monitor code. |
| 31 | * |
| 32 | * NOTE: |
| 33 | * To prevent the code below from containing references with an R_ARM_ABS32 |
| 34 | * relocation record type, we never refer to linker-defined symbols directly. |
| 35 | * Instead, we declare literals which contain their relative location with |
| 36 | * respect to relocate_code, and at run time, add relocate_code back to them. |
| 37 | */ |
| 38 | |
| 39 | ENTRY(relocate_code) |
| 40 | mov r6, r0 /* save addr of destination */ |
| 41 | |
| 42 | ldr r0, =_start /* r0 <- SRC &_start */ |
| 43 | subs r9, r6, r0 /* r9 <- relocation offset */ |
| 44 | beq relocate_done /* skip relocation */ |
| 45 | mov r1, r6 /* r1 <- scratch for copy loop */ |
| 46 | adr r7, relocate_code /* r7 <- SRC &relocate_code */ |
| 47 | ldr r3, _image_copy_end_ofs /* r3 <- __image_copy_end local ofs */ |
| 48 | add r2, r7, r3 /* r2 <- SRC &__image_copy_end */ |
| 49 | |
| 50 | copy_loop: |
| 51 | ldmia r0!, {r10-r11} /* copy from source address [r0] */ |
| 52 | stmia r1!, {r10-r11} /* copy to target address [r1] */ |
| 53 | cmp r0, r2 /* until source end address [r2] */ |
| 54 | blo copy_loop |
| 55 | |
| 56 | /* |
| 57 | * fix .rel.dyn relocations |
| 58 | */ |
| 59 | ldr r10, _dynsym_start_ofs /* r10 <- __dynsym_start local ofs */ |
| 60 | add r10, r10, r7 /* r10 <- SRC &__dynsym_start */ |
| 61 | ldr r2, _rel_dyn_start_ofs /* r2 <- __rel_dyn_start local ofs */ |
| 62 | add r2, r2, r7 /* r2 <- SRC &__rel_dyn_start */ |
| 63 | ldr r3, _rel_dyn_end_ofs /* r3 <- __rel_dyn_end local ofs */ |
| 64 | add r3, r3, r7 /* r3 <- SRC &__rel_dyn_end */ |
| 65 | fixloop: |
| 66 | ldr r0, [r2] /* r0 <- SRC location to fix up */ |
| 67 | add r0, r0, r9 /* r0 <- DST location to fix up */ |
| 68 | ldr r1, [r2, #4] |
| 69 | and r7, r1, #0xff |
| 70 | cmp r7, #23 /* relative fixup? */ |
| 71 | beq fixrel |
| 72 | cmp r7, #2 /* absolute fixup? */ |
| 73 | beq fixabs |
| 74 | /* ignore unknown type of fixup */ |
| 75 | b fixnext |
| 76 | fixabs: |
| 77 | /* absolute fix: set location to (offset) symbol value */ |
| 78 | mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */ |
| 79 | add r1, r10, r1 /* r1 <- address of symbol in table */ |
| 80 | ldr r1, [r1, #4] /* r1 <- symbol value */ |
| 81 | add r1, r1, r9 /* r1 <- relocated sym addr */ |
| 82 | b fixnext |
| 83 | fixrel: |
| 84 | /* relative fix: increase location by offset */ |
| 85 | ldr r1, [r0] |
| 86 | add r1, r1, r9 |
| 87 | fixnext: |
| 88 | str r1, [r0] |
| 89 | add r2, r2, #8 /* each rel.dyn entry is 8 bytes */ |
| 90 | cmp r2, r3 |
| 91 | blo fixloop |
| 92 | |
| 93 | relocate_done: |
| 94 | |
| 95 | /* ARMv4- don't know bx lr but the assembler fails to see that */ |
| 96 | |
| 97 | #ifdef __ARM_ARCH_4__ |
| 98 | mov pc, lr |
| 99 | #else |
| 100 | bx lr |
| 101 | #endif |
| 102 | |
| 103 | _image_copy_end_ofs: |
| 104 | .word __image_copy_end - relocate_code |
| 105 | _rel_dyn_start_ofs: |
| 106 | .word __rel_dyn_start - relocate_code |
| 107 | _rel_dyn_end_ofs: |
| 108 | .word __rel_dyn_end - relocate_code |
| 109 | _dynsym_start_ofs: |
| 110 | .word __dynsym_start - relocate_code |
| 111 | |
| 112 | ENDPROC(relocate_code) |