fix misinterpretation of operand order in i386 CFI generation
[musl] / tools / add-cfi.i386.awk
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
10 BEGIN {
11   # don't put CFI data in the .eh_frame ELF section (which we don't keep)
12   print ".cfi_sections .debug_frame"
13
14   # only emit CFI directives inside a function
15   in_function = 0
16
17   # emit .loc directives with line numbers from original source
18   printf ".file 1 \"%s\"\n", ARGV[1]
19   line_number = 0
20
21   # used to detect "call label; label:" trick
22   called = ""
23 }
24
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))
29 }
30 function get_reg() {
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)
34 }
35 function get_reg1() {
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)
39 }
40 function get_reg2() {
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)
44 }
45
46 function adjust_sp_offset(delta) {
47   if (in_function)
48     printf ".cfi_adjust_cfa_offset %d\n", delta
49 }
50
51 {
52   line_number = line_number + 1
53
54   # clean the input up before doing anything else
55   # delete comments
56   gsub(/(#|\/\/).*/, "")
57
58   # canonicalize whitespace
59   gsub(/[ \t]+/, " ") # mawk doesn't understand \s
60   gsub(/ *, */, ",")
61   gsub(/ *: */, ": ")
62   gsub(/ $/, "")
63   gsub(/^ /, "")
64 }
65
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
70   if (in_function) {
71     print ".cfi_endproc"
72     in_function = 0
73   }
74 }
75 /^\.type [a-zA-Z0-9_]+,\@function/ {
76   functions[substr($2, 1, length($2)-10)] = 1
77 }
78 # not interested in assembler directives beyond this, just pass them through
79 /^\./ {
80   print
81   next
82 }
83
84 /^[a-zA-Z0-9_]+:/ {
85   label = substr($1, 1, length($1)-1) # drop trailing :
86
87   if (called == label) {
88     # note adjustment of stack pointer from "call label; label:"
89     adjust_sp_offset(4)
90   }
91
92   if (functions[label]) {
93     if (in_function)
94       print ".cfi_endproc"
95
96     in_function = 1
97     print ".cfi_startproc"
98
99     for (register in saved)
100       delete saved[register]
101     for (register in dirty)
102       delete dirty[register]
103   }
104
105   # an instruction may follow on the same line, so continue processing
106 }
107
108 /^$/ { next }
109
110 {
111   called = ""
112   printf ".loc 1 %d\n", line_number
113   print
114 }
115
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
121 #
122 /pushl?/ {
123   if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
124     adjust_sp_offset(2)
125   else
126     adjust_sp_offset(4)
127 }
128 /popl?/ {
129   if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/))
130     adjust_sp_offset(-2)
131   else
132     adjust_sp_offset(-4)
133 }
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()) }
136
137 /call/ {
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)
142 }
143
144 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
145 #
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
151
152   if (in_function) {
153     register = get_reg()
154     if (!saved[register] && !dirty[register]) {
155       printf ".cfi_rel_offset %s,0\n", register
156       saved[register] = 1
157     }
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
176 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
177 # ...then we want to know about it.
178 #
179 function trashed(register) {
180   if (in_function && !saved[register] && !dirty[register]) {
181     printf ".cfi_undefined %s\n", register
182   }
183   dirty[register] = 1
184 }
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)$/ {
189   trashed(get_reg2())
190 }
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") }
196
197 END {
198   if (in_function)
199     print ".cfi_endproc"
200 }