mailing list of musl libc
 help / color / mirror / code / Atom feed
7eacc180996bf931d60eec4e23d12791ea1b2c88 blob 4941 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
 
# 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 callable from C
  # (blindly emitting a '.cfi_startproc' at the beginning of each file and
  #   '.cfi_endproc' at the end doesn't work)
  in_function = 0
}

function hex2int(str) {
  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 get_const() {
  # only use if you already know there is 1 and only 1 constant
  match($0, /\$[0-9a-fA-F]+/)
  return hex2int(substr($0, RSTART+1, 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, RLENGTH-1)
}
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, RLENGTH-2)
}
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+2, RLENGTH-2)
}

function adjust_sp_offset(delta) {
  if (in_function) {
    printf ".cfi_adjust_cfa_offset %d\n", delta
  }
}

{ print }

/\.type.*,@function/ {
  if (in_function) {
    print ".cfi_endproc"
  }

  print ".cfi_startproc"
  in_function = 1

  for (register in saved)
    delete saved[register]
  for (register in dirty)
    delete dirty[register]
}

# 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?/ { adjust_sp_offset(4) }
/popl?/  { adjust_sp_offset(-4) }
# TODO: can add/sub instructions also specify offset in decimal?
# TODO: can offset be negative?
/addl?\s+\$[0-9a-fA-F]+,%esp/ { adjust_sp_offset(-get_const()) }
/subl?\s+\$[0-9a-fA-F]+,%esp/ { adjust_sp_offset(get_const()) }

# TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
#
/pushl?\s+%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
    }
  }
}

# TODO: this should also understand hex offsets prefixed with 0x or -0x
/movl?\s+%e(ax|bx|cx|dx|si|di|bp),-?[0-9]*\(%esp\)/ {
  if (in_function) {
    register = get_reg()
    if (match($0, /-?[0-9]+\(%esp\)/)) {
      offset = substr($0, RSTART, RLENGTH-6) # decimal, not hex!
    } 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)
# TODO: detect when ax/ah/al/etc. are trashed -- means eax is no longer usable either
/mov.*,%e(ax|bx|cx|dx|si|di|bp)/  { trashed(get_reg2()) }
/(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr)\s+%e(ax|bx|cx|dx|si|di|bp),/ {
  trashed(get_reg1())
}
/i?mul\s+[^,]*$/                    { trashed("eax"); trashed("edx") }
/i?mul\s+%e(ax|bx|cx|dx|si|di|bp),/ { trashed(get_reg1()) }
/^(\w+:)?\s*i?div/                  { trashed("eax"); trashed("edx") }
/(dec|inc|not|neg|pop)\s+%e(ax|bx|cx|dx|si|di|bp)/  { trashed(get_reg()) }
/^(\w+:)\s*cpuid/ { trashed("eax"); trashed("ebx"); trashed("ecx"); trashed("edx") }

END {
  if (in_function) {
    print ".cfi_endproc"
  }
}
debug log:

solving 7eacc18 ...
found 7eacc18 in https://inbox.vuxu.org/musl/1431466124-2848-1-git-send-email-alexinbeijing@gmail.com/

applying [1/1] https://inbox.vuxu.org/musl/1431466124-2848-1-git-send-email-alexinbeijing@gmail.com/
diff --git a/tools/add-cfi.awk.i386 b/tools/add-cfi.awk.i386
new file mode 100644
index 0000000..7eacc18

Checking patch tools/add-cfi.awk.i386...
Applied patch tools/add-cfi.awk.i386 cleanly.

index at:
100644 7eacc180996bf931d60eec4e23d12791ea1b2c88	tools/add-cfi.awk.i386

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).