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 function canonicalize_reg
(register
) {
32 if (match(register
, /^e
/))
34 else if (match(register
, /[hl
]$
/)) # AH, AL, BH, BL, etc
35 return "e" substr(register
, 1, 1) "x"
36 else # AX, BX, CX, etc
40 # only use if you already know there is 1 and only 1 register
41 match($
0, /%e?
([abcd
][hlx
]|si
|di
|bp
)/)
42 return canonicalize_reg
(substr($
0, RSTART+1, RLENGTH-1))
45 # for instructions with 2 operands, get 1st operand (assuming it is register)
46 match($
0, /%e?
([abcd
][hlx
]|si
|di
|bp
),/)
47 return canonicalize_reg
(substr($
0, RSTART+1, RLENGTH-2))
50 # for instructions with 2 operands, get 2nd operand (assuming it is register)
51 match($
0, /,%e?
([abcd
][hlx
]|si
|di
|bp
)/)
52 return canonicalize_reg
(substr($
0, RSTART+2, RLENGTH-2))
55 function adjust_sp_offset
(delta
) {
57 printf ".cfi_adjust_cfa_offset %d\n", delta
61 line_number = line_number
+ 1
63 # clean the input up before doing anything else
65 gsub(/(#|\/\/).*/, "")
67 # canonicalize whitespace
68 gsub(/[ \t]+/, " ") # mawk doesn't understand \s
75 # check for assembler directives which we care about
76 /^\.
(section
|data
|text
)/ {
77 # a .cfi_startproc/.cfi_endproc pair should be within the same section
78 # otherwise, clang will choke when generating ELF output
84 /^\.type
[a
-zA
-Z0
-9_
]+,\@
function/ {
85 functions
[substr($
2, 1, length($
2)-10)] =
1
87 # not interested in assembler directives beyond this, just pass them through
94 label =
substr($
1, 1, length($
1)-1) # drop trailing :
96 if (called == label
) {
97 # note adjustment of stack pointer from "call label; label:"
101 if (functions
[label
]) {
106 print ".cfi_startproc"
108 for (register in saved
)
109 delete saved
[register
]
110 for (register in dirty
)
111 delete dirty
[register
]
114 # an instruction may follow on the same line, so continue processing
121 printf ".loc 1 %d\n", line_number
125 # KEEPING UP WITH THE STACK POINTER
126 # We do NOT attempt to understand foolish and ridiculous tricks like stashing
127 # the stack pointer and then using %esp as a scratch register, or bitshifting
128 # it or taking its square root or anything stupid like that.
129 # %esp should only be adjusted by pushing/popping or adding/subtracting constants
132 if (match($
0, / %
(ax
|bx
|cx
|dx
|di
|si
|bp
|sp
)/))
138 if (match($
0, / %
(ax
|bx
|cx
|dx
|di
|si
|bp
|sp
)/))
143 /addl? \$
-?
(0x
[0-9a
-fA
-F
]+|[0-9]+),%esp
/ { adjust_sp_offset
(-get_const1
()) }
144 /subl? \$
-?
(0x
[0-9a
-fA
-F
]+|[0-9]+),%esp
/ { adjust_sp_offset
(get_const1
()) }
147 if (match($
0, /call
[0-9]+f
/)) # "forward" label
148 called =
substr($
0, RSTART+5, RLENGTH-6)
149 else if (match($
0, /call
[0-9a
-zA
-Z_
]+/))
150 called =
substr($
0, RSTART+5, RLENGTH-5)
153 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
155 /pushl? %e
(ax
|bx
|cx
|dx
|si
|di
|bp
)/ { # don't match "push (%reg)"
156 # if a register is being pushed, and its value has not changed since the
157 # beginning of this function, the pushed value can be used when printing
158 # local variables at the next level up the stack
159 # emit '.cfi_rel_offset' for that
163 if (!saved
[register
] && !dirty
[register
]) {
164 printf ".cfi_rel_offset %s,0\n", register
170 /movl? %e
(ax
|bx
|cx
|dx
|si
|di
|bp
),-?
(0x
[0-9a
-fA
-F
]+|[0-9]+)?\
(%esp\
)/ {
173 if (match($
0, /-?
(0x
[0-9a
-fA
-F
]+|[0-9]+)\
(%esp\
)/)) {
174 offset = parse_const
(substr($
0, RSTART, RLENGTH-6))
178 if (!saved
[register
] && !dirty
[register
]) {
179 printf ".cfi_rel_offset %s,%d\n", register
, offset
185 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
186 # ...then we want to know about it.
188 function trashed
(register
) {
189 if (in_function
&& !saved
[register
] && !dirty
[register
]) {
190 printf ".cfi_undefined %s\n", register
194 # this does NOT exhaustively check for all possible instructions which could
195 # overwrite a register value inherited from the caller (just the common ones)
196 /mov.
*,%e?
([abcd
][hlx
]|si
|di
|bp
)$
/ { trashed
(get_reg2
()) }
197 /(add
|addl
|sub|subl
|and
|or
|xor
|lea
|sal
|sar
|shl
|shr
).
*,%e?
([abcd
][hlx
]|si
|di
|bp
)$
/ {
200 /^i?mul
[^
,]*$
/ { trashed
("eax"); trashed
("edx") }
201 /^i?mul.
*,%e?
([abcd
][hlx
]|si
|di
|bp
)$
/ { trashed
(get_reg2
()) }
202 /^i?div
/ { trashed
("eax"); trashed
("edx") }
203 /(dec
|inc
|not
|neg
|pop
) %e?
([abcd
][hlx
]|si
|di
|bp
)/ { trashed
(get_reg
()) }
204 /cpuid
/ { trashed
("eax"); trashed
("ebx"); trashed
("ecx"); trashed
("edx") }