reloc_32.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Code to process dynamic relocations for PPC32.
  3. *
  4. * Copyrights (C) IBM Corporation, 2011.
  5. * Author: Suzuki Poulose <suzuki@in.ibm.com>
  6. *
  7. * - Based on ppc64 code - reloc_64.S
  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
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <asm/ppc_asm.h>
  15. /* Dynamic section table entry tags */
  16. DT_RELA = 7 /* Tag for Elf32_Rela section */
  17. DT_RELASZ = 8 /* Size of the Rela relocs */
  18. DT_RELAENT = 9 /* Size of one Rela reloc entry */
  19. STN_UNDEF = 0 /* Undefined symbol index */
  20. STB_LOCAL = 0 /* Local binding for the symbol */
  21. R_PPC_ADDR16_LO = 4 /* Lower half of (S+A) */
  22. R_PPC_ADDR16_HI = 5 /* Upper half of (S+A) */
  23. R_PPC_ADDR16_HA = 6 /* High Adjusted (S+A) */
  24. R_PPC_RELATIVE = 22
  25. /*
  26. * r3 = desired final address
  27. */
  28. _GLOBAL(relocate)
  29. mflr r0 /* Save our LR */
  30. bl 0f /* Find our current runtime address */
  31. 0: mflr r12 /* Make it accessible */
  32. mtlr r0
  33. lwz r11, (p_dyn - 0b)(r12)
  34. add r11, r11, r12 /* runtime address of .dynamic section */
  35. lwz r9, (p_rela - 0b)(r12)
  36. add r9, r9, r12 /* runtime address of .rela.dyn section */
  37. lwz r10, (p_st - 0b)(r12)
  38. add r10, r10, r12 /* runtime address of _stext section */
  39. lwz r13, (p_sym - 0b)(r12)
  40. add r13, r13, r12 /* runtime address of .dynsym section */
  41. /*
  42. * Scan the dynamic section for RELA, RELASZ entries
  43. */
  44. li r6, 0
  45. li r7, 0
  46. li r8, 0
  47. 1: lwz r5, 0(r11) /* ELF_Dyn.d_tag */
  48. cmpwi r5, 0 /* End of ELF_Dyn[] */
  49. beq eodyn
  50. cmpwi r5, DT_RELA
  51. bne relasz
  52. lwz r7, 4(r11) /* r7 = rela.link */
  53. b skip
  54. relasz:
  55. cmpwi r5, DT_RELASZ
  56. bne relaent
  57. lwz r8, 4(r11) /* r8 = Total Rela relocs size */
  58. b skip
  59. relaent:
  60. cmpwi r5, DT_RELAENT
  61. bne skip
  62. lwz r6, 4(r11) /* r6 = Size of one Rela reloc */
  63. skip:
  64. addi r11, r11, 8
  65. b 1b
  66. eodyn: /* End of Dyn Table scan */
  67. /* Check if we have found all the entries */
  68. cmpwi r7, 0
  69. beq done
  70. cmpwi r8, 0
  71. beq done
  72. cmpwi r6, 0
  73. beq done
  74. /*
  75. * Work out the current offset from the link time address of .rela
  76. * section.
  77. * cur_offset[r7] = rela.run[r9] - rela.link [r7]
  78. * _stext.link[r12] = _stext.run[r10] - cur_offset[r7]
  79. * final_offset[r3] = _stext.final[r3] - _stext.link[r12]
  80. */
  81. subf r7, r7, r9 /* cur_offset */
  82. subf r12, r7, r10
  83. subf r3, r12, r3 /* final_offset */
  84. subf r8, r6, r8 /* relaz -= relaent */
  85. /*
  86. * Scan through the .rela table and process each entry
  87. * r9 - points to the current .rela table entry
  88. * r13 - points to the symbol table
  89. */
  90. /*
  91. * Check if we have a relocation based on symbol
  92. * r5 will hold the value of the symbol.
  93. */
  94. applyrela:
  95. lwz r4, 4(r9) /* r4 = rela.r_info */
  96. srwi r5, r4, 8 /* ELF32_R_SYM(r_info) */
  97. cmpwi r5, STN_UNDEF /* sym == STN_UNDEF ? */
  98. beq get_type /* value = 0 */
  99. /* Find the value of the symbol at index(r5) */
  100. slwi r5, r5, 4 /* r5 = r5 * sizeof(Elf32_Sym) */
  101. add r12, r13, r5 /* r12 = &__dyn_sym[Index] */
  102. /*
  103. * GNU ld has a bug, where dynamic relocs based on
  104. * STB_LOCAL symbols, the value should be assumed
  105. * to be zero. - Alan Modra
  106. */
  107. /* XXX: Do we need to check if we are using GNU ld ? */
  108. lbz r5, 12(r12) /* r5 = dyn_sym[Index].st_info */
  109. extrwi r5, r5, 4, 24 /* r5 = ELF32_ST_BIND(r5) */
  110. cmpwi r5, STB_LOCAL /* st_value = 0, ld bug */
  111. beq get_type /* We have r5 = 0 */
  112. lwz r5, 4(r12) /* r5 = __dyn_sym[Index].st_value */
  113. get_type:
  114. /* Load the relocation type to r4 */
  115. extrwi r4, r4, 8, 24 /* r4 = ELF32_R_TYPE(r_info) = ((char*)r4)[3] */
  116. /* R_PPC_RELATIVE */
  117. cmpwi r4, R_PPC_RELATIVE
  118. bne hi16
  119. lwz r4, 0(r9) /* r_offset */
  120. lwz r0, 8(r9) /* r_addend */
  121. add r0, r0, r3 /* final addend */
  122. stwx r0, r4, r7 /* memory[r4+r7]) = (u32)r0 */
  123. b nxtrela /* continue */
  124. /* R_PPC_ADDR16_HI */
  125. hi16:
  126. cmpwi r4, R_PPC_ADDR16_HI
  127. bne ha16
  128. lwz r4, 0(r9) /* r_offset */
  129. lwz r0, 8(r9) /* r_addend */
  130. add r0, r0, r3
  131. add r0, r0, r5 /* r0 = (S+A+Offset) */
  132. extrwi r0, r0, 16, 0 /* r0 = (r0 >> 16) */
  133. b store_half
  134. /* R_PPC_ADDR16_HA */
  135. ha16:
  136. cmpwi r4, R_PPC_ADDR16_HA
  137. bne lo16
  138. lwz r4, 0(r9) /* r_offset */
  139. lwz r0, 8(r9) /* r_addend */
  140. add r0, r0, r3
  141. add r0, r0, r5 /* r0 = (S+A+Offset) */
  142. extrwi r5, r0, 1, 16 /* Extract bit 16 */
  143. extrwi r0, r0, 16, 0 /* r0 = (r0 >> 16) */
  144. add r0, r0, r5 /* Add it to r0 */
  145. b store_half
  146. /* R_PPC_ADDR16_LO */
  147. lo16:
  148. cmpwi r4, R_PPC_ADDR16_LO
  149. bne unknown_type
  150. lwz r4, 0(r9) /* r_offset */
  151. lwz r0, 8(r9) /* r_addend */
  152. add r0, r0, r3
  153. add r0, r0, r5 /* r0 = (S+A+Offset) */
  154. extrwi r0, r0, 16, 16 /* r0 &= 0xffff */
  155. /* Fall through to */
  156. /* Store half word */
  157. store_half:
  158. sthx r0, r4, r7 /* memory[r4+r7] = (u16)r0 */
  159. nxtrela:
  160. /*
  161. * We have to flush the modified instructions to the
  162. * main storage from the d-cache. And also, invalidate the
  163. * cached instructions in i-cache which has been modified.
  164. *
  165. * We delay the sync / isync operation till the end, since
  166. * we won't be executing the modified instructions until
  167. * we return from here.
  168. */
  169. dcbst r4,r7
  170. sync /* Ensure the data is flushed before icbi */
  171. icbi r4,r7
  172. unknown_type:
  173. cmpwi r8, 0 /* relasz = 0 ? */
  174. ble done
  175. add r9, r9, r6 /* move to next entry in the .rela table */
  176. subf r8, r6, r8 /* relasz -= relaent */
  177. b applyrela
  178. done:
  179. sync /* Wait for the flush to finish */
  180. isync /* Discard prefetched instructions */
  181. blr
  182. p_dyn: .long __dynamic_start - 0b
  183. p_rela: .long __rela_dyn_start - 0b
  184. p_sym: .long __dynamic_symtab - 0b
  185. p_st: .long _stext - 0b