1/*
2 * Code to process dynamic relocations in the kernel.
3 *
4 * Copyright 2008 Paul Mackerras, IBM Corp.
5 *
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.
10 */
11
12#include <asm/ppc_asm.h>
13
14RELA = 7
15RELACOUNT = 0x6ffffff9
16R_PPC64_RELATIVE = 22
17
18/*
19 * r3 = desired final address of kernel
20 */
21_GLOBAL(relocate)
22	mflr	r0
23	bcl	20,31,$+4
240:	mflr	r12		/* r12 has runtime addr of label 0 */
25	mtlr	r0
26	ld	r11,(p_dyn - 0b)(r12)
27	add	r11,r11,r12	/* r11 has runtime addr of .dynamic section */
28	ld	r9,(p_rela - 0b)(r12)
29	add	r9,r9,r12	/* r9 has runtime addr of .rela.dyn section */
30	ld	r10,(p_st - 0b)(r12)
31	add	r10,r10,r12	/* r10 has runtime addr of _stext */
32
33	/*
34	 * Scan the dynamic section for the RELA and RELACOUNT entries.
35	 */
36	li	r7,0
37	li	r8,0
381:	ld	r6,0(r11)	/* get tag */
39	cmpdi	r6,0
40	beq	4f		/* end of list */
41	cmpdi	r6,RELA
42	bne	2f
43	ld	r7,8(r11)	/* get RELA pointer in r7 */
44	b	3f
452:	addis	r6,r6,(-RELACOUNT)@ha
46	cmpdi	r6,RELACOUNT@l
47	bne	3f
48	ld	r8,8(r11)	/* get RELACOUNT value in r8 */
493:	addi	r11,r11,16
50	b	1b
514:	cmpdi	r7,0		/* check we have both RELA and RELACOUNT */
52	cmpdi	cr1,r8,0
53	beq	6f
54	beq	cr1,6f
55
56	/*
57	 * Work out linktime address of _stext and hence the
58	 * relocation offset to be applied.
59	 * cur_offset [r7] = rela.run [r9] - rela.link [r7]
60	 * _stext.link [r10] = _stext.run [r10] - cur_offset [r7]
61	 * final_offset [r3] = _stext.final [r3] - _stext.link [r10]
62	 */
63	subf	r7,r7,r9	/* cur_offset */
64	subf	r10,r7,r10
65	subf	r3,r10,r3	/* final_offset */
66
67	/*
68	 * Run through the list of relocations and process the
69	 * R_PPC64_RELATIVE ones.
70	 */
71	mtctr	r8
725:	lwz	r0,12(9)	/* ELF64_R_TYPE(reloc->r_info) */
73	cmpwi	r0,R_PPC64_RELATIVE
74	bne	6f
75	ld	r6,0(r9)	/* reloc->r_offset */
76	ld	r0,16(r9)	/* reloc->r_addend */
77	add	r0,r0,r3
78	stdx	r0,r7,r6
79	addi	r9,r9,24
80	bdnz	5b
81
826:	blr
83
84.balign 8
85p_dyn:	.llong	__dynamic_start - 0b
86p_rela:	.llong	__rela_dyn_start - 0b
87p_st:	.llong	_stext - 0b
88
89