github.com/afumu/libc@v0.0.6/musl/tools/add-cfi.x86_64.awk (about) 1 # Insert GAS CFI directives ("control frame information") into x86-64 asm input 2 3 BEGIN { 4 # don't put CFI data in the .eh_frame ELF section (which we don't keep) 5 print ".cfi_sections .debug_frame" 6 7 # only emit CFI directives inside a function 8 in_function = 0 9 10 # emit .loc directives with line numbers from original source 11 printf ".file 1 \"%s\"\n", ARGV[1] 12 line_number = 0 13 14 # used to detect "call label; label:" trick 15 called = "" 16 } 17 18 function get_const1() { 19 # for instructions with 2 operands, get 1st operand (assuming it is constant) 20 match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/) 21 return parse_const(substr($0, RSTART, RLENGTH-1)) 22 } 23 24 function canonicalize_reg(register) { 25 if (match(register, /^r/)) 26 return register 27 else if (match(register, /^e/)) 28 return "r" substr(register, 2, length(register)-1) 29 else if (match(register, /[hl]$/)) # AH, AL, BH, BL, etc 30 return "r" substr(register, 1, 1) "x" 31 else # AX, BX, CX, etc 32 return "r" register 33 } 34 function get_reg() { 35 # only use if you already know there is 1 and only 1 register 36 match($0, /%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/) 37 return canonicalize_reg(substr($0, RSTART+1, RLENGTH-1)) 38 } 39 function get_reg1() { 40 # for instructions with 2 operands, get 1st operand (assuming it is register) 41 match($0, /%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15),/) 42 return canonicalize_reg(substr($0, RSTART+1, RLENGTH-2)) 43 } 44 function get_reg2() { 45 # for instructions with 2 operands, get 2nd operand (assuming it is register) 46 match($0, /,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/) 47 return canonicalize_reg(substr($0, RSTART+2, RLENGTH-2)) 48 } 49 50 function adjust_sp_offset(delta) { 51 if (in_function) 52 printf ".cfi_adjust_cfa_offset %d\n", delta 53 } 54 55 { 56 line_number = line_number + 1 57 58 # clean the input up before doing anything else 59 # delete comments 60 gsub(/(#|\/\/).*/, "") 61 62 # canonicalize whitespace 63 gsub(/[ \t]+/, " ") # mawk doesn't understand \s 64 gsub(/ *, */, ",") 65 gsub(/ *: */, ": ") 66 gsub(/ $/, "") 67 gsub(/^ /, "") 68 } 69 70 # check for assembler directives which we care about 71 /^\.(section|data|text)/ { 72 # a .cfi_startproc/.cfi_endproc pair should be within the same section 73 # otherwise, clang will choke when generating ELF output 74 if (in_function) { 75 print ".cfi_endproc" 76 in_function = 0 77 } 78 } 79 /^\.type [a-zA-Z0-9_]+,@function/ { 80 functions[substr($2, 1, length($2)-10)] = 1 81 } 82 # not interested in assembler directives beyond this, just pass them through 83 /^\./ { 84 print 85 next 86 } 87 88 /^[a-zA-Z0-9_]+:/ { 89 label = substr($1, 1, length($1)-1) # drop trailing : 90 91 if (called == label) { 92 # note adjustment of stack pointer from "call label; label:" 93 adjust_sp_offset(8) 94 } 95 96 if (functions[label]) { 97 if (in_function) 98 print ".cfi_endproc" 99 100 in_function = 1 101 print ".cfi_startproc" 102 103 for (register in saved) 104 delete saved[register] 105 for (register in dirty) 106 delete dirty[register] 107 } 108 109 # an instruction may follow on the same line, so continue processing 110 } 111 112 /^$/ { next } 113 114 { 115 called = "" 116 printf ".loc 1 %d\n", line_number 117 print 118 } 119 120 # KEEPING UP WITH THE STACK POINTER 121 # %rsp should only be adjusted by pushing/popping or adding/subtracting constants 122 # 123 /pushl?/ { 124 adjust_sp_offset(8) 125 } 126 /popl?/ { 127 adjust_sp_offset(-8) 128 } 129 /addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(-get_const1()) } 130 /subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(get_const1()) } 131 132 /call/ { 133 if (match($0, /call [0-9]+f/)) # "forward" label 134 called = substr($0, RSTART+5, RLENGTH-6) 135 else if (match($0, /call [0-9a-zA-Z_]+/)) 136 called = substr($0, RSTART+5, RLENGTH-5) 137 } 138 139 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME 140 # 141 /pushl? %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { # don't match "push (%reg)" 142 # if a register is being pushed, and its value has not changed since the 143 # beginning of this function, the pushed value can be used when printing 144 # local variables at the next level up the stack 145 # emit '.cfi_rel_offset' for that 146 147 if (in_function) { 148 register = get_reg() 149 if (!saved[register] && !dirty[register]) { 150 printf ".cfi_rel_offset %s,0\n", register 151 saved[register] = 1 152 } 153 } 154 } 155 156 /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\)/ { 157 if (in_function) { 158 register = get_reg() 159 if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%rsp\)/)) { 160 offset = parse_const(substr($0, RSTART, RLENGTH-6)) 161 } else { 162 offset = 0 163 } 164 if (!saved[register] && !dirty[register]) { 165 printf ".cfi_rel_offset %s,%d\n", register, offset 166 saved[register] = 1 167 } 168 } 169 } 170 171 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED 172 # ...then we want to know about it. 173 # 174 function trashed(register) { 175 if (in_function && !saved[register] && !dirty[register]) { 176 printf ".cfi_undefined %s\n", register 177 } 178 dirty[register] = 1 179 } 180 # this does NOT exhaustively check for all possible instructions which could 181 # overwrite a register value inherited from the caller (just the common ones) 182 /mov.*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ { trashed(get_reg2()) } 183 /(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr).*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ { 184 trashed(get_reg2()) 185 } 186 /^i?mul [^,]*$/ { trashed("rax"); trashed("rdx") } 187 /^i?mul.*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ { trashed(get_reg2()) } 188 /^i?div/ { trashed("rax"); trashed("rdx") } 189 190 /(dec|inc|not|neg|pop) %[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/ { trashed(get_reg()) } 191 /cpuid/ { trashed("rax"); trashed("rbx"); trashed("rcx"); trashed("rdx") } 192 193 END { 194 if (in_function) 195 print ".cfi_endproc" 196 }