1
0

add-cfi.i386.awk 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Insert GAS CFI directives ("control frame information") into x86-32 asm input
  2. #
  3. # CFI directives tell the assembler how to generate "stack frame" debug info
  4. # This information can tell a debugger (like gdb) how to find the current stack
  5. # frame at any point in the program code, and how to find the values which
  6. # various registers had at higher points in the call stack
  7. # With this information, the debugger can show a backtrace, and you can move up
  8. # and down the call stack and examine the values of local variables
  9. BEGIN {
  10. # don't put CFI data in the .eh_frame ELF section (which we don't keep)
  11. print ".cfi_sections .debug_frame"
  12. # only emit CFI directives inside a function
  13. in_function = 0
  14. # emit .loc directives with line numbers from original source
  15. printf ".file 1 \"%s\"\n", ARGV[1]
  16. line_number = 0
  17. # used to detect "call label; label:" trick
  18. called = ""
  19. }
  20. function hex2int(str, i) {
  21. str = tolower(str)
  22. for (i = 1; i <= 16; i++) {
  23. char = substr("0123456789abcdef", i, 1)
  24. lookup[char] = i-1
  25. }
  26. result = 0
  27. for (i = 1; i <= length(str); i++) {
  28. result = result * 16
  29. char = substr(str, i, 1)
  30. result = result + lookup[char]
  31. }
  32. return result
  33. }
  34. function parse_const(str) {
  35. sign = sub(/^-/, "", str)
  36. hex = sub(/^0x/, "", str)
  37. if (hex)
  38. n = hex2int(str)
  39. else
  40. n = str+0
  41. return sign ? -n : n
  42. }
  43. function get_const1() {
  44. # for instructions with 2 operands, get 1st operand (assuming it is constant)
  45. match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/)
  46. return parse_const(substr($0, RSTART, RLENGTH-1))
  47. }
  48. function get_reg() {
  49. # only use if you already know there is 1 and only 1 register
  50. match($0, /%e(ax|bx|cx|dx|si|di|bp)/)
  51. return substr($0, RSTART+1, 3)
  52. }
  53. function get_reg1() {
  54. # for instructions with 2 operands, get 1st operand (assuming it is register)
  55. match($0, /%e(ax|bx|cx|dx|si|di|bp),/)
  56. return substr($0, RSTART+1, 3)
  57. }
  58. function get_reg2() {
  59. # for instructions with 2 operands, get 2nd operand (assuming it is register)
  60. match($0, /,%e(ax|bx|cx|dx|si|di|bp)/)
  61. return substr($0, RSTART+RLENGTH-3, 3)
  62. }
  63. function adjust_sp_offset(delta) {
  64. if (in_function)
  65. printf ".cfi_adjust_cfa_offset %d\n", delta
  66. }
  67. {
  68. line_number = line_number + 1
  69. # clean the input up before doing anything else
  70. # delete comments
  71. gsub(/(#|\/\/).*/, "")
  72. # canonicalize whitespace
  73. gsub(/[ \t]+/, " ") # mawk doesn't understand \s
  74. gsub(/ *, */, ",")
  75. gsub(/ *: */, ": ")
  76. gsub(/ $/, "")
  77. gsub(/^ /, "")
  78. }
  79. # check for assembler directives which we care about
  80. /^\.(section|data|text)/ {
  81. # a .cfi_startproc/.cfi_endproc pair should be within the same section
  82. # otherwise, clang will choke when generating ELF output
  83. if (in_function) {
  84. print ".cfi_endproc"
  85. in_function = 0
  86. }
  87. }
  88. /^\.type [a-zA-Z0-9_]+,\@function/ {
  89. functions[substr($2, 1, length($2)-10)] = 1
  90. }
  91. # not interested in assembler directives beyond this, just pass them through
  92. /^\./ {
  93. print
  94. next
  95. }
  96. /^[a-zA-Z0-9_]+:/ {
  97. label = substr($1, 1, length($1)-1) # drop trailing :
  98. if (called == label) {
  99. # note adjustment of stack pointer from "call label; label:"
  100. adjust_sp_offset(4)
  101. }
  102. if (functions[label]) {
  103. if (in_function)
  104. print ".cfi_endproc"
  105. in_function = 1
  106. print ".cfi_startproc"
  107. for (register in saved)
  108. delete saved[register]
  109. for (register in dirty)
  110. delete dirty[register]
  111. }
  112. # an instruction may follow on the same line, so continue processing
  113. }
  114. /^$/ { next }
  115. {
  116. called = ""
  117. printf ".loc 1 %d\n", line_number
  118. print
  119. }
  120. # KEEPING UP WITH THE STACK POINTER
  121. # We do NOT attempt to understand foolish and ridiculous tricks like stashing
  122. # the stack pointer and then using %esp as a scratch register, or bitshifting
  123. # it or taking its square root or anything stupid like that.
  124. # %esp should only be adjusted by pushing/popping or adding/subtracting constants
  125. #
  126. /pushl?/ {
  127. if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
  128. adjust_sp_offset(2)
  129. else
  130. adjust_sp_offset(4)
  131. }
  132. /popl?/ {
  133. if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
  134. adjust_sp_offset(-2)
  135. else
  136. adjust_sp_offset(-4)
  137. }
  138. /addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(-get_const1()) }
  139. /subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(get_const1()) }
  140. /call/ {
  141. if (match($0, /call [0-9]+f/)) # "forward" label
  142. called = substr($0, RSTART+5, RLENGTH-6)
  143. else if (match($0, /call [0-9a-zA-Z_]+/))
  144. called = substr($0, RSTART+5, RLENGTH-5)
  145. }
  146. # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
  147. #
  148. /pushl? %e(ax|bx|cx|dx|si|di|bp)/ { # don't match "push (%reg)"
  149. # if a register is being pushed, and its value has not changed since the
  150. # beginning of this function, the pushed value can be used when printing
  151. # local variables at the next level up the stack
  152. # emit '.cfi_rel_offset' for that
  153. if (in_function) {
  154. register = get_reg()
  155. if (!saved[register] && !dirty[register]) {
  156. printf ".cfi_rel_offset %s,0\n", register
  157. saved[register] = 1
  158. }
  159. }
  160. }
  161. /movl? %e(ax|bx|cx|dx|si|di|bp),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%esp\)/ {
  162. if (in_function) {
  163. register = get_reg()
  164. if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%esp\)/)) {
  165. offset = parse_const(substr($0, RSTART, RLENGTH-6))
  166. } else {
  167. offset = 0
  168. }
  169. if (!saved[register] && !dirty[register]) {
  170. printf ".cfi_rel_offset %s,%d\n", register, offset
  171. saved[register] = 1
  172. }
  173. }
  174. }
  175. # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
  176. # ...then we want to know about it.
  177. #
  178. function trashed(register) {
  179. if (in_function && !saved[register] && !dirty[register]) {
  180. printf ".cfi_undefined %s\n", register
  181. }
  182. dirty[register] = 1
  183. }
  184. # this does NOT exhaustively check for all possible instructions which could
  185. # overwrite a register value inherited from the caller (just the common ones)
  186. /mov.*,%e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg2()) }
  187. /(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr) %e(ax|bx|cx|dx|si|di|bp),/ {
  188. trashed(get_reg1())
  189. }
  190. /i?mul [^,]*$/ { trashed("eax"); trashed("edx") }
  191. /i?mul %e(ax|bx|cx|dx|si|di|bp),/ { trashed(get_reg1()) }
  192. /i?div/ { trashed("eax"); trashed("edx") }
  193. /(dec|inc|not|neg|pop) %e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg()) }
  194. /cpuid/ { trashed("eax"); trashed("ebx"); trashed("ecx"); trashed("edx") }
  195. END {
  196. if (in_function)
  197. print ".cfi_endproc"
  198. }