1 # Insert GAS CFI directives ("control frame information") into x86-32 asm input
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
11 # don't put CFI data in the .eh_frame ELF section (which we don't keep)
12 print ".cfi_sections .debug_frame"
14 # only emit CFI directives inside a function
17 # emit .loc directives with line numbers from original source
18 printf ".file 1 \"%s\"\n", ARGV[1]
21 # used to detect "call label; label:" trick
25 function hex2int(str, i) {
28 for (i = 1; i <= 16; i++) {
29 char = substr("0123456789abcdef", i, 1)
34 for (i = 1; i <= length(str); i++) {
36 char = substr(str, i, 1)
37 result = result + lookup[char]
42 function parse_const(str) {
43 sign = sub(/^-/, "", str)
44 hex = sub(/^0x/, "", str)
52 function get_const1() {
53 # for instructions with 2 operands, get 1st operand (assuming it is constant)
54 match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/)
55 return parse_const(substr($0, RSTART, RLENGTH-1))
58 # only use if you already know there is 1 and only 1 register
59 match($0, /%e(ax|bx|cx|dx|si|di|bp)/)
60 return substr($0, RSTART+1, 3)
63 # for instructions with 2 operands, get 1st operand (assuming it is register)
64 match($0, /%e(ax|bx|cx|dx|si|di|bp),/)
65 return substr($0, RSTART+1, 3)
68 # for instructions with 2 operands, get 2nd operand (assuming it is register)
69 match($0, /,%e(ax|bx|cx|dx|si|di|bp)/)
70 return substr($0, RSTART+RLENGTH-3, 3)
73 function adjust_sp_offset(delta) {
75 printf ".cfi_adjust_cfa_offset %d\n", delta
79 line_number = line_number + 1
81 # clean the input up before doing anything else
83 gsub(/(#|\/\/).*/, "")
85 # canonicalize whitespace
86 gsub(/[ \t]+/, " ") # mawk doesn't understand \s
93 # check for assembler directives which we care about
94 /^\.(section|data|text)/ {
95 # a .cfi_startproc/.cfi_endproc pair should be within the same section
96 # otherwise, clang will choke when generating ELF output
102 /^\.type [a-zA-Z0-9_]+,\@function/ {
103 functions[substr($2, 1, length($2)-10)] = 1
105 # not interested in assembler directives beyond this, just pass them through
112 label = substr($1, 1, length($1)-1) # drop trailing :
114 if (called == label) {
115 # note adjustment of stack pointer from "call label; label:"
119 if (functions[label]) {
124 print ".cfi_startproc"
126 for (register in saved)
127 delete saved[register]
128 for (register in dirty)
129 delete dirty[register]
132 # an instruction may follow on the same line, so continue processing
139 printf ".loc 1 %d\n", line_number
143 # KEEPING UP WITH THE STACK POINTER
144 # We do NOT attempt to understand foolish and ridiculous tricks like stashing
145 # the stack pointer and then using %esp as a scratch register, or bitshifting
146 # it or taking its square root or anything stupid like that.
147 # %esp should only be adjusted by pushing/popping or adding/subtracting constants
150 if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
156 if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
161 /addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(-get_const1()) }
162 /subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(get_const1()) }
165 if (match($0, /call [0-9]+f/)) # "forward" label
166 called = substr($0, RSTART+5, RLENGTH-6)
167 else if (match($0, /call [0-9a-zA-Z_]+/))
168 called = substr($0, RSTART+5, RLENGTH-5)
171 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
173 /pushl? %e(ax|bx|cx|dx|si|di|bp)/ { # don't match "push (%reg)"
174 # if a register is being pushed, and its value has not changed since the
175 # beginning of this function, the pushed value can be used when printing
176 # local variables at the next level up the stack
177 # emit '.cfi_rel_offset' for that
181 if (!saved[register] && !dirty[register]) {
182 printf ".cfi_rel_offset %s,0\n", register
188 /movl? %e(ax|bx|cx|dx|si|di|bp),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%esp\)/ {
191 if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%esp\)/)) {
192 offset = parse_const(substr($0, RSTART, RLENGTH-6))
196 if (!saved[register] && !dirty[register]) {
197 printf ".cfi_rel_offset %s,%d\n", register, offset
203 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
204 # ...then we want to know about it.
206 function trashed(register) {
207 if (in_function && !saved[register] && !dirty[register]) {
208 printf ".cfi_undefined %s\n", register
212 # this does NOT exhaustively check for all possible instructions which could
213 # overwrite a register value inherited from the caller (just the common ones)
214 /mov.*,%e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg2()) }
215 /(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr) %e(ax|bx|cx|dx|si|di|bp),/ {
218 /i?mul [^,]*$/ { trashed("eax"); trashed("edx") }
219 /i?mul %e(ax|bx|cx|dx|si|di|bp),/ { trashed(get_reg1()) }
220 /i?div/ { trashed("eax"); trashed("edx") }
221 /(dec|inc|not|neg|pop) %e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg()) }
222 /cpuid/ { trashed("eax"); trashed("ebx"); trashed("ecx"); trashed("edx") }