2 * Copyright (C) Paul Mackerras 1997.
4 * Adapted for 64 bit LE PowerPC by Andrew Tauferner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
16 RELACOUNT = 0x6ffffff9
19 /* A procedure descriptor used when booting this as a COFF file.
20 * When making COFF, this comes first in the link and we're
23 .globl _zimage_start_opd
25 .long 0x500000, 0, 0, 0
29 p_start: .llong _start
30 p_etext: .llong _etext
31 p_bss_start: .llong __bss_start
34 p_toc: .llong __toc_start + 0x8000 - p_base
35 p_dyn: .llong __dynamic_start - p_base
36 p_rela: .llong __rela_dyn_start - p_base
38 .weak _platform_stack_top
39 p_pstack: .llong _platform_stack_top
43 p_bss_start: .long __bss_start
46 .weak _platform_stack_top
47 p_pstack: .long _platform_stack_top
53 .globl _zimage_start_lib
55 /* Work out the offset between the address we were linked at
56 and the address where we're running. */
58 p_base: mflr r10 /* r10 now points to runtime addr of p_base */
60 /* grab the link address of the dynamic section in r11 */
61 addis r11,r10,(_GLOBAL_OFFSET_TABLE_-p_base)@ha
62 lwz r11,(_GLOBAL_OFFSET_TABLE_-p_base)@l(r11)
64 beq 3f /* if not linked -pie */
65 /* get the runtime address of the dynamic section in r12 */
67 addis r12,r10,(__dynamic_start-p_base)@ha
68 addi r12,r12,(__dynamic_start-p_base)@l
69 subf r11,r11,r12 /* runtime - linktime offset */
71 /* The dynamic section contains a series of tagged entries.
72 * We need the RELA and RELACOUNT entries. */
75 9: lwz r8,0(r12) /* get tag */
77 beq 10f /* end of list */
80 lwz r9,4(r12) /* get RELA pointer in r9 */
82 11: addis r8,r8,(-RELACOUNT)@ha
85 lwz r0,4(r12) /* get RELACOUNT value in r0 */
89 /* The relocation section contains a list of relocations.
90 * We now do the R_PPC_RELATIVE ones, which point to words
91 * which need to be initialized with addend + offset.
92 * The R_PPC_RELATIVE ones come first and there are RELACOUNT
94 10: /* skip relocation if we don't have both */
100 add r9,r9,r11 /* Relocate RELA pointer */
102 2: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */
103 cmpwi r0,22 /* R_PPC_RELATIVE */
105 lwz r12,0(r9) /* reloc->r_offset */
106 lwz r0,8(r9) /* reloc->r_addend */
112 /* Do a cache flush for our text, in case the loader didn't */
113 3: lwz r9,p_start-p_base(r10) /* note: these are relocated now */
114 lwz r8,p_etext-p_base(r10)
124 lwz r9,p_bss_start-p_base(r10)
125 lwz r8,p_end-p_base(r10)
132 /* Possibly set up a custom stack */
133 lwz r8,p_pstack-p_base(r10)
138 stwu r0,-16(r1) /* establish a stack frame */
140 #else /* __powerpc64__ */
141 /* Save the prom pointer at p_prom. */
142 std r5,(p_prom-p_base)(r10)
144 /* Set r2 to the TOC. */
145 ld r2,(p_toc-p_base)(r10)
148 /* Grab the link address of the dynamic section in r11. */
151 beq 3f /* if not linked -pie then no dynamic section */
153 ld r11,(p_dyn-p_base)(r10)
155 ld r9,(p_rela-p_base)(r10)
160 9: ld r6,0(r11) /* get tag */
162 beq 12f /* end of list */
165 ld r7,8(r11) /* get RELA pointer in r7 */
167 10: addis r6,r6,(-RELACOUNT)@ha
170 ld r8,8(r11) /* get RELACOUNT value in r8 */
174 cmpdi r7,0 /* check we have both RELA and RELACOUNT */
179 /* Calcuate the runtime offset. */
182 /* Run through the list of relocations and process the
183 * R_PPC64_RELATIVE ones. */
185 13: ld r0,8(r9) /* ELF64_R_TYPE(reloc->r_info) */
186 cmpdi r0,22 /* R_PPC64_RELATIVE */
188 ld r6,0(r9) /* reloc->r_offset */
189 ld r0,16(r9) /* reloc->r_addend */
195 /* Do a cache flush for our text, in case the loader didn't */
196 3: ld r9,p_start-p_base(r10) /* note: these are relocated now */
197 ld r8,p_etext-p_base(r10)
207 ld r9,p_bss_start-p_base(r10)
208 ld r8,p_end-p_base(r10)
215 /* Possibly set up a custom stack */
216 ld r8,p_pstack-p_base(r10)
221 stdu r0,-16(r1) /* establish a stack frame */
223 #endif /* __powerpc64__ */
224 /* Call platform_init() */
232 #define PROM_FRAME_SIZE 512
233 #define SAVE_GPR(n, base) std n,8*(n)(base)
234 #define REST_GPR(n, base) ld n,8*(n)(base)
235 #define SAVE_2GPRS(n, base) SAVE_GPR(n, base); SAVE_GPR(n+1, base)
236 #define SAVE_4GPRS(n, base) SAVE_2GPRS(n, base); SAVE_2GPRS(n+2, base)
237 #define SAVE_8GPRS(n, base) SAVE_4GPRS(n, base); SAVE_4GPRS(n+4, base)
238 #define SAVE_10GPRS(n, base) SAVE_8GPRS(n, base); SAVE_2GPRS(n+8, base)
239 #define REST_2GPRS(n, base) REST_GPR(n, base); REST_GPR(n+1, base)
240 #define REST_4GPRS(n, base) REST_2GPRS(n, base); REST_2GPRS(n+2, base)
241 #define REST_8GPRS(n, base) REST_4GPRS(n, base); REST_4GPRS(n+4, base)
242 #define REST_10GPRS(n, base) REST_8GPRS(n, base); REST_2GPRS(n+8, base)
244 /* prom handles the jump into and return from firmware. The prom args pointer
250 stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
261 /* remove MSR_LE from msr but keep MSR_SF */
266 /* Load FW address, set LR to label 1, and jump to FW */
272 ld r10,(p_prom-0b)(r10)
277 1: /* Return from OF */
280 /* Restore registers and return. */
283 /* Restore the MSR (back to 64 bits) */
288 /* Restore other registers */
296 addi r1,r1,PROM_FRAME_SIZE