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 get_const1() {
26 # for instructions with 2 operands, get 1st operand (assuming it is constant)
27 match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/)
28 return parse_const(substr($0, RSTART, RLENGTH-1))
31 # only use if you already know there is 1 and only 1 register
32 match($0, /%e(ax|bx|cx|dx|si|di|bp)/)
33 return substr($0, RSTART+1, 3)
36 # for instructions with 2 operands, get 1st operand (assuming it is register)
37 match($0, /%e(ax|bx|cx|dx|si|di|bp),/)
38 return substr($0, RSTART+1, 3)
41 # for instructions with 2 operands, get 2nd operand (assuming it is register)
42 match($0, /,%e(ax|bx|cx|dx|si|di|bp)/)
43 return substr($0, RSTART+RLENGTH-3, 3)
46 function adjust_sp_offset(delta) {
48 printf ".cfi_adjust_cfa_offset %d\n", delta
52 line_number = line_number + 1
54 # clean the input up before doing anything else
56 gsub(/(#|\/\/).*/, "")
58 # canonicalize whitespace
59 gsub(/[ \t]+/, " ") # mawk doesn't understand \s
66 # check for assembler directives which we care about
67 /^\.(section|data|text)/ {
68 # a .cfi_startproc/.cfi_endproc pair should be within the same section
69 # otherwise, clang will choke when generating ELF output
75 /^\.type [a-zA-Z0-9_]+,\@function/ {
76 functions[substr($2, 1, length($2)-10)] = 1
78 # not interested in assembler directives beyond this, just pass them through
85 label = substr($1, 1, length($1)-1) # drop trailing :
87 if (called == label) {
88 # note adjustment of stack pointer from "call label; label:"
92 if (functions[label]) {
97 print ".cfi_startproc"
99 for (register in saved)
100 delete saved[register]
101 for (register in dirty)
102 delete dirty[register]
105 # an instruction may follow on the same line, so continue processing
112 printf ".loc 1 %d\n", line_number
116 # KEEPING UP WITH THE STACK POINTER
117 # We do NOT attempt to understand foolish and ridiculous tricks like stashing
118 # the stack pointer and then using %esp as a scratch register, or bitshifting
119 # it or taking its square root or anything stupid like that.
120 # %esp should only be adjusted by pushing/popping or adding/subtracting constants
123 if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
129 if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
134 /addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(-get_const1()) }
135 /subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(get_const1()) }
138 if (match($0, /call [0-9]+f/)) # "forward" label
139 called = substr($0, RSTART+5, RLENGTH-6)
140 else if (match($0, /call [0-9a-zA-Z_]+/))
141 called = substr($0, RSTART+5, RLENGTH-5)
144 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
146 /pushl? %e(ax|bx|cx|dx|si|di|bp)/ { # don't match "push (%reg)"
147 # if a register is being pushed, and its value has not changed since the
148 # beginning of this function, the pushed value can be used when printing
149 # local variables at the next level up the stack
150 # emit '.cfi_rel_offset' for that
154 if (!saved[register] && !dirty[register]) {
155 printf ".cfi_rel_offset %s,0\n", register
161 /movl? %e(ax|bx|cx|dx|si|di|bp),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%esp\)/ {
164 if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%esp\)/)) {
165 offset = parse_const(substr($0, RSTART, RLENGTH-6))
169 if (!saved[register] && !dirty[register]) {
170 printf ".cfi_rel_offset %s,%d\n", register, offset
176 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
177 # ...then we want to know about it.
179 function trashed(register) {
180 if (in_function && !saved[register] && !dirty[register]) {
181 printf ".cfi_undefined %s\n", register
185 # this does NOT exhaustively check for all possible instructions which could
186 # overwrite a register value inherited from the caller (just the common ones)
187 /mov.*,%e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg2()) }
188 /(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr).*,%e(ax|bx|cx|dx|si|di|bp)$/ {
191 /^i?mul [^,]*$/ { trashed("eax"); trashed("edx") }
192 /^i?mul.*,%e(ax|bx|cx|dx|si|di|bp)$/ { trashed(get_reg2()) }
193 /^i?div/ { trashed("eax"); trashed("edx") }
194 /(dec|inc|not|neg|pop) %e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg()) }
195 /cpuid/ { trashed("eax"); trashed("ebx"); trashed("ecx"); trashed("edx") }