From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7834 Path: news.gmane.org!not-for-mail From: Alex Dowad Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v6] Build process uses script to add CFI directives to x86 asm Date: Sun, 31 May 2015 10:20:51 +0200 Message-ID: <1433060451-23934-1-git-send-email-alexinbeijing@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1433060489 32567 80.91.229.3 (31 May 2015 08:21:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 31 May 2015 08:21:29 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7846-gllmg-musl=m.gmane.org@lists.openwall.com Sun May 31 10:21:29 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 1YyyUq-0001cX-14 for gllmg-musl@m.gmane.org; Sun, 31 May 2015 10:21:28 +0200 Original-Received: (qmail 22500 invoked by uid 550); 31 May 2015 08:21:26 -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 22405 invoked from network); 31 May 2015 08:21:17 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=klq+Y8klVrHTGSck/ncZokuB8jXa3kpozYwpJsJKFT8=; b=b0kjV2A3sZ4soJNo/a/nPROL0SzJM2M/TnPDdV0IANgoYlgM6/HT6yg7ybSXOe69aR j8QOBfTmbm7E29mfI0NzZxUbKG8DJY8HvmAdqYSn4sMpMBz827y22QEmN/UeH4FwgGUt 6Zpoyg0gB4U+rhzSvTXQCWuoGLIoaJ2xm6Mr+CNEH2SZ5LGl1O7iUjnZGQnmlzojF8cB eXvOuZ7gWlXQF0vsu+6KA4WAkmG94hYuQeOp+nBaVCoxwsftOY1woaaIhQGMD15xNaXn TQPJtk1ynocjkkHKvz8SEZTlkXS8MlIVs97p9bzPmRdrluXS/onOwrImWZ1QM0yBuiWD 6cTQ== X-Received: by 10.180.94.39 with SMTP id cz7mr10354594wib.66.1433060466422; Sun, 31 May 2015 01:21:06 -0700 (PDT) X-Mailer: git-send-email 2.0.0.GIT Xref: news.gmane.org gmane.linux.lib.musl.general:7834 Archived-At: Some functions implemented in asm need to use EBP for purposes other than acting as a frame pointer. (Notably, it is used for the 6th argument to syscalls with 6 arguments.) Without frame pointers, GDB can only show backtraces if it gets CFI information from a .debug_frame or .eh_frame ELF section. Rather than littering our asm with ugly .cfi directives, use an awk script to insert them in the right places during the build process, so GDB can keep track of where the current stack frame is relative to the stack pointer. This means GDB can produce beautiful stack traces at any given point when single-stepping through asm functions. Additionally, when registers are saved on the stack and later overwritten, emit .cfi directives so GDB will know where they were saved relative to the stack pointer. This way, when you look back up the stack from within an asm function, you can still reliably print the values of local variables in the caller. If this awk script were to understand every possible wild and crazy contortion that an asm programmer can do with the stack and registers, and always emit the exact .cfi directives needed for GDB to know what the register values were in the preceding stack frame, it would necessarily be as complex as a full x86 emulator. That way lies madness. Hence, we assume that the stack pointer will _only_ ever be adjusted using push/pop or else add/sub with a constant. We do not attempt to detect every possible way that a register value could be saved for later use, just the simple and common ways. Thanks to Szabolcs Nagy for suggesting numerous improvements to this code. --- Dear muslers, 1. The AWK script in this patch is now compatible with mawk (I've tested it) 2. Some code has been added to detect "call Nf; N:" and adjust the CFA offset accordingly. Apologies, but I am not adjusting the Makefile rules for building subarch files, because there is no "add-cfi" script for any arch which has subarchs. Efforts to improve the Makefile rules for $(ARCH)/%.s using an aswrap script or something similar have not yielded anything better than this. If anyone wants to contribute something better, that will be appreciated. ASFLAGS has been introduced because clang chokes on the -g flag when assembling the preprocessed asm files. As it turns out, nothing in CCFLAGS is actually needed (or useful) for asm. So ASFLAGS is currently empty. However, people who want to use their own, customized build flags can use it when editing config.mak. Otherwise, all the problems with debugging binaries generated by clang are due to bugs in clang. I've just submitted one patch to llvm-commits to improve their generation of CFI data for C/C++ code (actually anything which compiles to LLVM IR), and hope to submit more. To give you an idea of LLVM's current level of support for debugging asm directives, allow me to quote lib/MC/MCParser/AsmParser.cpp:2842-2846: int64_t LineNumber = getTok().getIntVal(); (void)LineNumber; Lex(); // FIXME: Do something with the .line. Thanks, Alex Dowad Makefile | 12 ++- configure | 20 +++++ tools/add-cfi.i386.awk | 225 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 tools/add-cfi.i386.awk diff --git a/Makefile b/Makefile index 3bd7b4d..4da1857 100644 --- a/Makefile +++ b/Makefile @@ -118,7 +118,11 @@ $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s)))) $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<) %.o: $(ARCH)/%.s - $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $< +ifeq ($(ADD_CFI),yes) + LC_ALL=C mawk -f tools/add-cfi.$(ARCH).awk $< | $(CC) $(ASFLAGS) -x assembler -c -o $@ - +else + $(CC) $(ASFLAGS) -c -o $@ $< +endif %.o: %.c $(GENH) $(IMPH) $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $< @@ -127,7 +131,11 @@ $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s)))) $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<) %.lo: $(ARCH)/%.s - $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $< +ifeq ($(ADD_CFI),yes) + LC_ALL=C mawk -f tools/add-cfi.$(ARCH).awk $< | $(CC) $(ASFLAGS) -x assembler -c -o $@ - +else + $(CC) $(ASFLAGS) -c -o $@ $< +endif %.lo: %.c $(GENH) $(IMPH) $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $< diff --git a/configure b/configure index 7b29ae4..5d15a2a 100755 --- a/configure +++ b/configure @@ -116,6 +116,7 @@ CFLAGS_NOSSP= CFLAGS_TRY= LDFLAGS_AUTO= LDFLAGS_TRY= +ASFLAGS= OPTIMIZE_GLOBS= prefix=/usr/local/musl exec_prefix='$(prefix)' @@ -327,6 +328,23 @@ tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns test "$debug" = yes && CFLAGS_AUTO=-g # +# Preprocess asm files to add extra debugging information if debug is +# enabled, our assembler supports the needed directives, and the +# preprocessing script has been written for our architecture. +# +printf "checking whether we should preprocess assembly to add debugging information... " +if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO" && + test -f "tools/add-cfi.$ARCH.awk" && + echo ".cfi_startproc +.cfi_endproc" | $CC -x assembler -c -o /dev/null - +then + ADD_CFI=yes +else + ADD_CFI=no +fi +printf "%s\n" "$ADD_CFI" + +# # Possibly add a -O option to CFLAGS and select modules to optimize with # -O3 based on the status of --enable-optimize and provided CFLAGS. # @@ -577,9 +595,11 @@ CFLAGS_MEMOPS = $CFLAGS_MEMOPS CFLAGS_NOSSP = $CFLAGS_NOSSP CPPFLAGS = $CPPFLAGS LDFLAGS = $LDFLAGS_AUTO $LDFLAGS +ASFLAGS = $ASFLAGS CROSS_COMPILE = $CROSS_COMPILE LIBCC = $LIBCC OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS +ADD_CFI = $ADD_CFI EOF test "x$static" = xno && echo "STATIC_LIBS =" test "x$shared" = xno && echo "SHARED_LIBS =" diff --git a/tools/add-cfi.i386.awk b/tools/add-cfi.i386.awk new file mode 100644 index 0000000..1d7846c --- /dev/null +++ b/tools/add-cfi.i386.awk @@ -0,0 +1,225 @@ +# Insert GAS CFI directives ("control frame information") into x86-32 asm input +# +# CFI directives tell the assembler how to generate "stack frame" debug info +# This information can tell a debugger (like gdb) how to find the current stack +# frame at any point in the program code, and how to find the values which +# various registers had at higher points in the call stack +# With this information, the debugger can show a backtrace, and you can move up +# and down the call stack and examine the values of local variables + +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 hex2int(str, i) { + str = tolower(str) + + for (i = 1; i <= 16; i++) { + char = substr("0123456789abcdef", i, 1) + lookup[char] = i-1 + } + + result = 0 + for (i = 1; i <= length(str); i++) { + result = result * 16 + char = substr(str, i, 1) + result = result + lookup[char] + } + return result +} + +function parse_const(str) { + sign = sub(/^-/, "", str) + hex = sub(/^0x/, "", str) + if (hex) + n = hex2int(str) + else + n = str+0 + return sign ? -n : n +} + +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, /%e(ax|bx|cx|dx|si|di|bp)/) + return substr($0, RSTART+1, 3) +} +function get_reg1() { + # for instructions with 2 operands, get 1st operand (assuming it is register) + match($0, /%e(ax|bx|cx|dx|si|di|bp),/) + return substr($0, RSTART+1, 3) +} +function get_reg2() { + # for instructions with 2 operands, get 2nd operand (assuming it is register) + match($0, /,%e(ax|bx|cx|dx|si|di|bp)/) + return substr($0, RSTART+RLENGTH-3, 3) +} + +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/ { + if (in_function) { + print ".cfi_endproc" + in_function = 0 + } +} +/^\.globa?l +[a-zA-Z0-9_]+/ { + globals[$2] = 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(4) + } + + if (globals[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 +# We do NOT attempt to understand foolish and ridiculous tricks like stashing +# the stack pointer and then using %esp as a scratch register, or bitshifting +# it or taking its square root or anything stupid like that. +# %esp should only be adjusted by pushing/popping or adding/subtracting constants +# +/pushl?/ { + if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/)) + adjust_sp_offset(2) + else + adjust_sp_offset(4) +} +/popl?/ { + if (match($0, / %(ax|bx|cx|dx|di|si|bp|sp)/)) + adjust_sp_offset(-2) + else + adjust_sp_offset(-4) +} +/addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { adjust_sp_offset(-get_const1()) } +/subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%esp/ { 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? %e(ax|bx|cx|dx|si|di|bp)/ { # 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? %e(ax|bx|cx|dx|si|di|bp),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%esp\)/ { + if (in_function) { + register = get_reg() + if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%esp\)/)) { + 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.*,%e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg2()) } +/(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr) %e(ax|bx|cx|dx|si|di|bp),/ { + trashed(get_reg1()) +} +/i?mul [^,]*$/ { trashed("eax"); trashed("edx") } +/i?mul %e(ax|bx|cx|dx|si|di|bp),/ { trashed(get_reg1()) } +/i?div/ { trashed("eax"); trashed("edx") } +/(dec|inc|not|neg|pop) %e(ax|bx|cx|dx|si|di|bp)/ { trashed(get_reg()) } +/cpuid/ { trashed("eax"); trashed("ebx"); trashed("ecx"); trashed("edx") } + +END { + if (in_function) + print ".cfi_endproc" +} -- 2.0.0.GIT