add-cfi.i386.awk 5.8 KB

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