From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8606 Path: news.gmane.org!not-for-mail From: Alex Dowad Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 5/5] Add script to add CFI directives to asm files in debug builds of x86_64 Date: Fri, 2 Oct 2015 13:32:36 +0200 Message-ID: <1443785556-27473-5-git-send-email-alexinbeijing@gmail.com> References: <1443785556-27473-1-git-send-email-alexinbeijing@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1443785686 31913 80.91.229.3 (2 Oct 2015 11:34:46 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 2 Oct 2015 11:34:46 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8618-gllmg-musl=m.gmane.org@lists.openwall.com Fri Oct 02 13:34:45 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Zhybr-0005JX-4m for gllmg-musl@m.gmane.org; Fri, 02 Oct 2015 13:34:43 +0200 Original-Received: (qmail 20265 invoked by uid 550); 2 Oct 2015 11:34:36 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 15779 invoked from network); 2 Oct 2015 11:33:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=x1A1Bular4ZAkAfV6qsn/rrFF8NWhw6X1rAeOQTJK2U=; b=CEQs1dwGY09nHncE03b9UMgaaVhVDKaV+SA8Xt5USbeGma4GbsXw+Od/3sEj84Jekv LNwaBXYsqDtBTjFYoCwj76bVsI/01F6xZKVMWvFFcngNEx9AHybxyVewKGmsQa6Tb1Al DGgq+v1grQj/OKMX/Y6r/Mu19FUqyHOo+4p2ggR6h3KuGieTGJ7fL97oE+jiydsfIsDS Wjly79obyZCWsqHt0wnNSGLLahGMpMF+me7qCpYXKFOHIvAOq/okiTt6QyY7+UfuMIil gR0MXiDKsb6FA0PX2KUxGzoWEn7rJEYynY1LxxN3z9yMLNqK0CNOybIvhlAmMwtrN0Qz dwLw== X-Received: by 10.180.108.162 with SMTP id hl2mr3544824wib.89.1443785570480; Fri, 02 Oct 2015 04:32:50 -0700 (PDT) X-Mailer: git-send-email 2.0.0.GIT In-Reply-To: <1443785556-27473-1-git-send-email-alexinbeijing@gmail.com> Xref: news.gmane.org gmane.linux.lib.musl.general:8606 Archived-At: This makes debugging more pleasant for the odd times when you have to dig into files implemented in x86_64 assembly. It turns out that the parts we care about are almost exactly like i386, except for the register names! --- tools/add-cfi.x86_64.awk | 185 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 tools/add-cfi.x86_64.awk diff --git a/tools/add-cfi.x86_64.awk b/tools/add-cfi.x86_64.awk new file mode 100644 index 0000000..8c82ea3 --- /dev/null +++ b/tools/add-cfi.x86_64.awk @@ -0,0 +1,185 @@ +# Insert GAS CFI directives ("control frame information") into x86-64 asm input + +BEGIN { + # don't put CFI data in the .eh_frame ELF section (which we don't keep) + print ".cfi_sections .debug_frame" + + # only emit CFI directives inside a function + in_function = 0 + + # emit .loc directives with line numbers from original source + printf ".file 1 \"%s\"\n", ARGV[1] + line_number = 0 + + # used to detect "call label; label:" trick + called = "" +} + +function get_const1() { + # for instructions with 2 operands, get 1st operand (assuming it is constant) + match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/) + return parse_const(substr($0, RSTART, RLENGTH-1)) +} +function get_reg() { + # only use if you already know there is 1 and only 1 register + match($0, /%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/) + return substr($0, RSTART+1, RLENGTH-1) +} +function get_reg1() { + # for instructions with 2 operands, get 1st operand (assuming it is register) + match($0, /%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15),/) + return substr($0, RSTART+1, RLENGTH-2) +} +function get_reg2() { + # for instructions with 2 operands, get 2nd operand (assuming it is register) + match($0, /,%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/) + return substr($0, RSTART+2, RLENGTH-2) +} + +function adjust_sp_offset(delta) { + if (in_function) + printf ".cfi_adjust_cfa_offset %d\n", delta +} + +{ + line_number = line_number + 1 + + # clean the input up before doing anything else + # delete comments + gsub(/(#|\/\/).*/, "") + + # canonicalize whitespace + gsub(/[ \t]+/, " ") # mawk doesn't understand \s + gsub(/ *, */, ",") + gsub(/ *: */, ": ") + gsub(/ $/, "") + gsub(/^ /, "") +} + +# check for assembler directives which we care about +/^\.(section|data|text)/ { + # a .cfi_startproc/.cfi_endproc pair should be within the same section + # otherwise, clang will choke when generating ELF output + if (in_function) { + print ".cfi_endproc" + in_function = 0 + } +} +/^\.type [a-zA-Z0-9_]+,\@function/ { + functions[substr($2, 1, length($2)-10)] = 1 +} +# not interested in assembler directives beyond this, just pass them through +/^\./ { + print + next +} + +/^[a-zA-Z0-9_]+:/ { + label = substr($1, 1, length($1)-1) # drop trailing : + + if (called == label) { + # note adjustment of stack pointer from "call label; label:" + adjust_sp_offset(8) + } + + if (functions[label]) { + if (in_function) + print ".cfi_endproc" + + in_function = 1 + print ".cfi_startproc" + + for (register in saved) + delete saved[register] + for (register in dirty) + delete dirty[register] + } + + # an instruction may follow on the same line, so continue processing +} + +/^$/ { next } + +{ + called = "" + printf ".loc 1 %d\n", line_number + print +} + +# KEEPING UP WITH THE STACK POINTER +# %rsp should only be adjusted by pushing/popping or adding/subtracting constants +# +/pushl?/ { + adjust_sp_offset(8) +} +/popl?/ { + adjust_sp_offset(-8) +} +/addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(-get_const1()) } +/subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(get_const1()) } + +/call/ { + if (match($0, /call [0-9]+f/)) # "forward" label + called = substr($0, RSTART+5, RLENGTH-6) + else if (match($0, /call [0-9a-zA-Z_]+/)) + called = substr($0, RSTART+5, RLENGTH-5) +} + +# TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME +# +/pushl? %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { # don't match "push (%reg)" + # if a register is being pushed, and its value has not changed since the + # beginning of this function, the pushed value can be used when printing + # local variables at the next level up the stack + # emit '.cfi_rel_offset' for that + + if (in_function) { + register = get_reg() + if (!saved[register] && !dirty[register]) { + printf ".cfi_rel_offset %s,0\n", register + saved[register] = 1 + } + } +} + +/movl? %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%rsp\)/ { + if (in_function) { + register = get_reg() + if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%rsp\)/)) { + offset = parse_const(substr($0, RSTART, RLENGTH-6)) + } else { + offset = 0 + } + if (!saved[register] && !dirty[register]) { + printf ".cfi_rel_offset %s,%d\n", register, offset + saved[register] = 1 + } + } +} + +# IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED +# ...then we want to know about it. +# +function trashed(register) { + if (in_function && !saved[register] && !dirty[register]) { + printf ".cfi_undefined %s\n", register + } + dirty[register] = 1 +} +# this does NOT exhaustively check for all possible instructions which could +# overwrite a register value inherited from the caller (just the common ones) +/mov.*,%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { trashed(get_reg2()) } +/(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr).*,%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { + trashed(get_reg2()) +} +/^i?mul [^,]*$/ { trashed("rax"); trashed("rdx") } +/^i?mul.*,%r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { trashed(get_reg2()) } +/^i?div/ { trashed("rax"); trashed("rdx") } + +/(dec|inc|not|neg|pop) %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { trashed(get_reg()) } +/cpuid/ { trashed("rax"); trashed("rbx"); trashed("rcx"); trashed("rdx") } + +END { + if (in_function) + print ".cfi_endproc" +} -- 2.0.0.GIT