2011-12-14 22:58:12 +00:00
/ *
* Code t o p r o c e s s d y n a m i c r e l o c a t i o n s f o r P P C 3 2 .
*
* Copyrights ( C ) I B M C o r p o r a t i o n , 2 0 1 1 .
* Author : Suzuki P o u l o s e < s u z u k i @in.ibm.com>
*
* - Based o n p p c64 c o d e - r e l o c _ 6 4 . S
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or
* modify i t u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e
* as p u b l i s h e d b y t h e F r e e S o f t w a r e F o u n d a t i o n ; either version
* 2 of t h e L i c e n s e , o r ( a t y o u r o p t i o n ) a n y l a t e r v e r s i o n .
* /
# include < a s m / p p c _ a s m . h >
/* Dynamic section table entry tags */
DT_ R E L A = 7 / * T a g f o r E l f32 _ R e l a s e c t i o n * /
DT_ R E L A S Z = 8 / * S i z e o f t h e R e l a r e l o c s * /
DT_ R E L A E N T = 9 / * S i z e o f o n e R e l a r e l o c e n t r y * /
STN_ U N D E F = 0 / * U n d e f i n e d s y m b o l i n d e x * /
STB_ L O C A L = 0 / * L o c a l b i n d i n g f o r t h e s y m b o l * /
R_ P P C _ A D D R 1 6 _ L O = 4 / * L o w e r h a l f o f ( S + A ) * /
R_ P P C _ A D D R 1 6 _ H I = 5 / * U p p e r h a l f o f ( S + A ) * /
R_ P P C _ A D D R 1 6 _ H A = 6 / * H i g h A d j u s t e d ( S + A ) * /
R_ P P C _ R E L A T I V E = 2 2
/ *
* r3 = d e s i r e d f i n a l a d d r e s s
* /
_ GLOBAL( r e l o c a t e )
mflr r0 / * S a v e o u r L R * /
bl 0 f / * F i n d o u r c u r r e n t r u n t i m e a d d r e s s * /
0 : mflr r12 / * M a k e i t a c c e s s i b l e * /
mtlr r0
lwz r11 , ( p _ d y n - 0 b ) ( r12 )
add r11 , r11 , r12 / * r u n t i m e a d d r e s s o f . d y n a m i c s e c t i o n * /
lwz r9 , ( p _ r e l a - 0 b ) ( r12 )
add r9 , r9 , r12 / * r u n t i m e a d d r e s s o f . r e l a . d y n s e c t i o n * /
lwz r10 , ( p _ s t - 0 b ) ( r12 )
add r10 , r10 , r12 / * r u n t i m e a d d r e s s o f _ s t e x t s e c t i o n * /
lwz r13 , ( p _ s y m - 0 b ) ( r12 )
add r13 , r13 , r12 / * r u n t i m e a d d r e s s o f . d y n s y m s e c t i o n * /
/ *
* Scan t h e d y n a m i c s e c t i o n f o r R E L A , R E L A S Z e n t r i e s
* /
li r6 , 0
li r7 , 0
li r8 , 0
1 : lwz r5 , 0 ( r11 ) / * E L F _ D y n . d _ t a g * /
cmpwi r5 , 0 / * E n d o f E L F _ D y n [ ] * /
beq e o d y n
cmpwi r5 , D T _ R E L A
bne r e l a s z
lwz r7 , 4 ( r11 ) / * r7 = r e l a . l i n k * /
b s k i p
relasz :
cmpwi r5 , D T _ R E L A S Z
bne r e l a e n t
lwz r8 , 4 ( r11 ) / * r8 = T o t a l R e l a r e l o c s s i z e * /
b s k i p
relaent :
cmpwi r5 , D T _ R E L A E N T
bne s k i p
lwz r6 , 4 ( r11 ) / * r6 = S i z e o f o n e R e l a r e l o c * /
skip :
addi r11 , r11 , 8
b 1 b
eodyn : /* End of Dyn Table scan */
/* Check if we have found all the entries */
cmpwi r7 , 0
beq d o n e
cmpwi r8 , 0
beq d o n e
cmpwi r6 , 0
beq d o n e
/ *
* Work o u t t h e c u r r e n t o f f s e t f r o m t h e l i n k t i m e a d d r e s s o f . r e l a
* section.
* cur_ o f f s e t [ r7 ] = r e l a . r u n [ r9 ] - r e l a . l i n k [ r7 ]
* _ stext. l i n k [ r12 ] = _ s t e x t . r u n [ r10 ] - c u r _ o f f s e t [ r7 ]
* final_ o f f s e t [ r3 ] = _ s t e x t . f i n a l [ r3 ] - _ s t e x t . l i n k [ r12 ]
* /
subf r7 , r7 , r9 / * c u r _ o f f s e t * /
subf r12 , r7 , r10
subf r3 , r12 , r3 / * f i n a l _ o f f s e t * /
subf r8 , r6 , r8 / * r e l a z - = r e l a e n t * /
/ *
* Scan t h r o u g h t h e . r e l a t a b l e a n d p r o c e s s e a c h e n t r y
* r9 - p o i n t s t o t h e c u r r e n t . r e l a t a b l e e n t r y
* r1 3 - p o i n t s t o t h e s y m b o l t a b l e
* /
/ *
* Check i f w e h a v e a r e l o c a t i o n b a s e d o n s y m b o l
* r5 w i l l h o l d t h e v a l u e o f t h e s y m b o l .
* /
applyrela :
lwz r4 , 4 ( r9 ) / * r4 = r e l a . r _ i n f o * /
srwi r5 , r4 , 8 / * E L F 3 2 _ R _ S Y M ( r _ i n f o ) * /
cmpwi r5 , S T N _ U N D E F / * s y m = = S T N _ U N D E F ? * /
beq g e t _ t y p e / * v a l u e = 0 * /
/* Find the value of the symbol at index(r5) */
slwi r5 , r5 , 4 / * r5 = r5 * s i z e o f ( E l f32 _ S y m ) * /
add r12 , r13 , r5 / * r12 = & _ _ d y n _ s y m [ I n d e x ] * /
/ *
* GNU l d h a s a b u g , w h e r e d y n a m i c r e l o c s b a s e d o n
* STB_ L O C A L s y m b o l s , t h e v a l u e s h o u l d b e a s s u m e d
* to b e z e r o . - A l a n M o d r a
* /
/* XXX: Do we need to check if we are using GNU ld ? */
lbz r5 , 1 2 ( r12 ) / * r5 = d y n _ s y m [ I n d e x ] . s t _ i n f o * /
extrwi r5 , r5 , 4 , 2 4 / * r5 = E L F 3 2 _ S T _ B I N D ( r5 ) * /
cmpwi r5 , S T B _ L O C A L / * s t _ v a l u e = 0 , l d b u g * /
beq g e t _ t y p e / * W e h a v e r5 = 0 * /
lwz r5 , 4 ( r12 ) / * r5 = _ _ d y n _ s y m [ I n d e x ] . s t _ v a l u e * /
get_type :
/* Load the relocation type to r4 */
extrwi r4 , r4 , 8 , 2 4 / * r4 = E L F 3 2 _ R _ T Y P E ( r _ i n f o ) = ( ( c h a r * ) r4 ) [ 3 ] * /
/* R_PPC_RELATIVE */
cmpwi r4 , R _ P P C _ R E L A T I V E
bne h i 1 6
lwz r4 , 0 ( r9 ) / * r _ o f f s e t * /
lwz r0 , 8 ( r9 ) / * r _ a d d e n d * /
add r0 , r0 , r3 / * f i n a l a d d e n d * /
stwx r0 , r4 , r7 / * m e m o r y [ r4 + r7 ] ) = ( u 3 2 ) r0 * /
b n x t r e l a / * c o n t i n u e * /
/* R_PPC_ADDR16_HI */
hi16 :
cmpwi r4 , R _ P P C _ A D D R 1 6 _ H I
bne h a16
lwz r4 , 0 ( r9 ) / * r _ o f f s e t * /
lwz r0 , 8 ( r9 ) / * r _ a d d e n d * /
add r0 , r0 , r3
add r0 , r0 , r5 / * r0 = ( S + A + O f f s e t ) * /
extrwi r0 , r0 , 1 6 , 0 / * r0 = ( r0 > > 1 6 ) * /
b s t o r e _ h a l f
/* R_PPC_ADDR16_HA */
ha16 :
cmpwi r4 , R _ P P C _ A D D R 1 6 _ H A
bne l o 1 6
lwz r4 , 0 ( r9 ) / * r _ o f f s e t * /
lwz r0 , 8 ( r9 ) / * r _ a d d e n d * /
add r0 , r0 , r3
add r0 , r0 , r5 / * r0 = ( S + A + O f f s e t ) * /
extrwi r5 , r0 , 1 , 1 6 / * E x t r a c t b i t 1 6 * /
extrwi r0 , r0 , 1 6 , 0 / * r0 = ( r0 > > 1 6 ) * /
add r0 , r0 , r5 / * A d d i t t o r0 * /
b s t o r e _ h a l f
/* R_PPC_ADDR16_LO */
lo16 :
cmpwi r4 , R _ P P C _ A D D R 1 6 _ L O
2013-06-27 09:09:43 +08:00
bne u n k n o w n _ t y p e
2011-12-14 22:58:12 +00:00
lwz r4 , 0 ( r9 ) / * r _ o f f s e t * /
lwz r0 , 8 ( r9 ) / * r _ a d d e n d * /
add r0 , r0 , r3
add r0 , r0 , r5 / * r0 = ( S + A + O f f s e t ) * /
extrwi r0 , r0 , 1 6 , 1 6 / * r0 & = 0 x f f f f * /
/* Fall through to */
/* Store half word */
store_half :
sthx r0 , r4 , r7 / * m e m o r y [ r4 + r7 ] = ( u 1 6 ) r0 * /
nxtrela :
/ *
* We h a v e t o f l u s h t h e m o d i f i e d i n s t r u c t i o n s t o t h e
* main s t o r a g e f r o m t h e d - c a c h e . A n d a l s o , i n v a l i d a t e t h e
* cached i n s t r u c t i o n s i n i - c a c h e w h i c h h a s b e e n m o d i f i e d .
*
* We d e l a y t h e s y n c / i s y n c o p e r a t i o n t i l l t h e e n d , s i n c e
* we w o n ' t b e e x e c u t i n g t h e m o d i f i e d i n s t r u c t i o n s u n t i l
* we r e t u r n f r o m h e r e .
* /
dcbst r4 ,r7
sync / * E n s u r e t h e d a t a i s f l u s h e d b e f o r e i c b i * /
icbi r4 ,r7
2013-06-27 09:09:43 +08:00
unknown_type :
2011-12-14 22:58:12 +00:00
cmpwi r8 , 0 / * r e l a s z = 0 ? * /
ble d o n e
add r9 , r9 , r6 / * m o v e t o n e x t e n t r y i n t h e . r e l a t a b l e * /
subf r8 , r6 , r8 / * r e l a s z - = r e l a e n t * /
b a p p l y r e l a
done :
sync / * W a i t f o r t h e f l u s h t o f i n i s h * /
isync / * D i s c a r d p r e f e t c h e d i n s t r u c t i o n s * /
blr
p_dyn : .long _ _ d y n a m i c _ s t a r t - 0 b
p_rela : .long _ _ r e l a _ d y n _ s t a r t - 0 b
p_sym : .long _ _ d y n a m i c _ s y m t a b - 0 b
p_st : .long _ s t e x t - 0 b