add-cfi.i386.awk 6.1 KB

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