reloc_32.S 5.2 KB

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