* [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
@ 2024-07-19 9:06 Howard Su
2024-07-19 14:18 ` Thorsten Glaser
0 siblings, 1 reply; 7+ messages in thread
From: Howard Su @ 2024-07-19 9:06 UTC (permalink / raw)
To: musl
[-- Attachment #1: Type: text/plain, Size: 1498 bytes --]
I am working on a project on Alpine OS on ARM. I have hard time to debug a
multi thread application. GDB cannot generate full backtrace.
(gdb) bt
#0 __cp_begin () at src/thread/arm/syscall_cp.s:23
#1 0xb6f1f532 in __syscall_cp_c (nr=7639428, u=<optimized out>,
v=<optimized out>, w=0, x=0, y=-1093370608, z=2)
at src/thread/pthread_cancel.c:33
#2 0xbed47cc0 in ?? ()
I am suggesting that we can add CFI to assembly code to make sure it
generates the symbols to support gdb.
I tried ChatGPT which is able to write cfi directives correctly. Pick the
above thread cancellation point as example. Not perfect but only little
changes needed.
.global __syscall_cp_asm
.type __syscall_cp_asm,%function
__syscall_cp_asm:
.cfi_startproc
mov ip,sp
.cfi_def_cfa_register ip
stmfd sp!,{r4,r5,r6,r7,lr}
.cfi_adjust_cfa_offset 20
.cfi_rel_offset r4, 0
.cfi_rel_offset r5, 4
.cfi_rel_offset r6, 8
.cfi_rel_offset r7, 12
.cfi_rel_offset lr, 16
.global __cp_begin
__cp_begin:
ldr r0,[r0]
cmp r0,#0
blne __cancel
mov r7,r1
mov r0,r2
mov r1,r3
ldmfd ip,{r2,r3,r4,r5,r6}
.cfi_remember_state
.cfi_def_cfa ip, 0
svc 0
.global __cp_end
__cp_end:
.cfi_restore_state
ldmfd sp!,{r4,r5,r6,r7,lr}
.cfi_adjust_cfa_offset -20
.cfi_restore r4
.cfi_restore r5
.cfi_restore r6
.cfi_restore r7
.cfi_restore lr
tst lr,#1
moveq pc,lr
.cfi_return_column lr
bx lr
.cfi_endproc
--
-Howard
[-- Attachment #2: Type: text/html, Size: 2337 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-19 9:06 [musl] [Suggestion] Add cfi directives to assembly code via chatgpt Howard Su
@ 2024-07-19 14:18 ` Thorsten Glaser
2024-07-19 21:40 ` Rich Felker
0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Glaser @ 2024-07-19 14:18 UTC (permalink / raw)
To: musl
Howard Su dixit:
>I tried ChatGPT which is able to write cfi directives correctly.
It is not. It is also a destroyer of the environment and a theft machine
and while I’m not sure musl has such a policy, many OSS projects won’t
permit use of ML/LLM (so-called “AI”) when creating a contribution.
bye,
//mirabilos
--
"Using Lynx is like wearing a really good pair of shades: cuts out
the glare and harmful UV (ultra-vanity), and you feel so-o-o COOL."
-- Henry Nelson, March 1999
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-19 14:18 ` Thorsten Glaser
@ 2024-07-19 21:40 ` Rich Felker
2024-07-22 6:39 ` Howard Su
0 siblings, 1 reply; 7+ messages in thread
From: Rich Felker @ 2024-07-19 21:40 UTC (permalink / raw)
To: Thorsten Glaser; +Cc: musl, Howard Su
On Fri, Jul 19, 2024 at 02:18:11PM +0000, Thorsten Glaser wrote:
> Howard Su dixit:
>
> >I tried ChatGPT which is able to write cfi directives correctly.
>
> It is not. It is also a destroyer of the environment and a theft machine
> and while I’m not sure musl has such a policy, many OSS projects won’t
> permit use of ML/LLM (so-called “AI”) when creating a contribution.
Indeed, while in this case I would guess the amount of code (I have
not read it because I don't want copyright taint) is sufficiently
small and non-creative not to be subject to copyright, in general,
anything output from an LLM (chatgpt or otherwise) is not acceptable
for inclusion in musl because it is plagiarized from some unknown and
undiscoverable subset of billions(?) of works incorporated into the
LLM without authorization by their authors/copyright holders. Anything
submitted for inclusion must have provenance (original authorship or
exiting published material under suitable license) whereby we know it
can be copied and distributed under the project license.
Rich
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-19 21:40 ` Rich Felker
@ 2024-07-22 6:39 ` Howard Su
2024-07-22 21:39 ` Szabolcs Nagy
0 siblings, 1 reply; 7+ messages in thread
From: Howard Su @ 2024-07-22 6:39 UTC (permalink / raw)
To: Rich Felker; +Cc: Thorsten Glaser, musl
[-- Attachment #1: Type: text/plain, Size: 1480 bytes --]
fair enough. Let's put aside that LLM writing the CFI directives. I do have
knowledge about it to hand write it.
Do you think it is a good idea to put effort into adding CFI to those
assembly code, especially the thread_cp part, which blocks my debug.
On Sat, Jul 20, 2024 at 5:40 AM Rich Felker <dalias@libc.org> wrote:
> On Fri, Jul 19, 2024 at 02:18:11PM +0000, Thorsten Glaser wrote:
> > Howard Su dixit:
> >
> > >I tried ChatGPT which is able to write cfi directives correctly.
> >
> > It is not. It is also a destroyer of the environment and a theft machine
> > and while I’m not sure musl has such a policy, many OSS projects won’t
> > permit use of ML/LLM (so-called “AI”) when creating a contribution.
>
> Indeed, while in this case I would guess the amount of code (I have
> not read it because I don't want copyright taint) is sufficiently
> small and non-creative not to be subject to copyright, in general,
> anything output from an LLM (chatgpt or otherwise) is not acceptable
> for inclusion in musl because it is plagiarized from some unknown and
> undiscoverable subset of billions(?) of works incorporated into the
> LLM without authorization by their authors/copyright holders. Anything
> submitted for inclusion must have provenance (original authorship or
> exiting published material under suitable license) whereby we know it
> can be copied and distributed under the project license.
>
> Rich
>
--
-Howard
[-- Attachment #2: Type: text/html, Size: 2161 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-22 6:39 ` Howard Su
@ 2024-07-22 21:39 ` Szabolcs Nagy
2024-07-29 0:19 ` Howard Su
0 siblings, 1 reply; 7+ messages in thread
From: Szabolcs Nagy @ 2024-07-22 21:39 UTC (permalink / raw)
To: Howard Su; +Cc: Rich Felker, Thorsten Glaser, musl
* Howard Su <howard0su@gmail.com> [2024-07-22 14:39:35 +0800]:
> fair enough. Let's put aside that LLM writing the CFI directives. I do have
> knowledge about it to hand write it.
>
> Do you think it is a good idea to put effort into adding CFI to those
> assembly code, especially the thread_cp part, which blocks my debug.
note that there is now support in gas to synthetize the CFI for
manually written x86 asm code with --scfi=experimental
https://sourceware.org/binutils/docs/as.html
the aarch64 support just got committed recently
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=29085f7243e415db0e1617173eb80c441671ba0e
there is no support for arm yet.
meanwhile musl has tools/add-cfi*.awk that you could extend to
support arm, this is simpler than adding the gas support as it
only has to work for musl code.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-22 21:39 ` Szabolcs Nagy
@ 2024-07-29 0:19 ` Howard Su
2024-07-29 8:16 ` Szabolcs Nagy
0 siblings, 1 reply; 7+ messages in thread
From: Howard Su @ 2024-07-29 0:19 UTC (permalink / raw)
To: Howard Su, Rich Felker, Thorsten Glaser, musl
[-- Attachment #1.1: Type: text/plain, Size: 1114 bytes --]
Per suggestion, I added awk script to do so. Please help review. also I
noticed that stack unwind is still not functional.
On Tue, Jul 23, 2024 at 5:39 AM Szabolcs Nagy <nsz@port70.net> wrote:
> * Howard Su <howard0su@gmail.com> [2024-07-22 14:39:35 +0800]:
> > fair enough. Let's put aside that LLM writing the CFI directives. I do
> have
> > knowledge about it to hand write it.
> >
> > Do you think it is a good idea to put effort into adding CFI to those
> > assembly code, especially the thread_cp part, which blocks my debug.
>
> note that there is now support in gas to synthetize the CFI for
> manually written x86 asm code with --scfi=experimental
> https://sourceware.org/binutils/docs/as.html
>
> the aarch64 support just got committed recently
>
> https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=29085f7243e415db0e1617173eb80c441671ba0e
>
> there is no support for arm yet.
>
> meanwhile musl has tools/add-cfi*.awk that you could extend to
> support arm, this is simpler than adding the gas support as it
> only has to work for musl code.
>
>
--
-Howard
[-- Attachment #1.2: Type: text/html, Size: 1924 bytes --]
[-- Attachment #2: 0001-Add-awk-script-to-add-CFI-directives-to-arm.patch --]
[-- Type: text/x-patch, Size: 6589 bytes --]
From fbe4007ac7e6728b6226ca52ccbc5b77571bcece Mon Sep 17 00:00:00 2001
From: Howard Su <howard0su@gmail.com>
Date: Mon, 29 Jul 2024 08:17:31 +0800
Subject: [PATCH] Add awk script to add CFI directives to arm
---
crt/arm/crti.s | 2 -
tools/add-cfi.arm.awk | 220 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 220 insertions(+), 2 deletions(-)
create mode 100644 tools/add-cfi.arm.awk
diff --git a/crt/arm/crti.s b/crt/arm/crti.s
index 18dc1e41..acab83c9 100644
--- a/crt/arm/crti.s
+++ b/crt/arm/crti.s
@@ -2,12 +2,10 @@
.section .init
.global _init
-.type _init,%function
_init:
push {r0,lr}
.section .fini
.global _fini
-.type _fini,%function
_fini:
push {r0,lr}
diff --git a/tools/add-cfi.arm.awk b/tools/add-cfi.arm.awk
new file mode 100644
index 00000000..93b94768
--- /dev/null
+++ b/tools/add-cfi.arm.awk
@@ -0,0 +1,220 @@
+# Insert GAS CFI directives ("control frame information") into arm 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
+}
+
+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(1[0-5]|[0-9])|ip|sp|lr|pc)/)
+ return substr($0, RSTART+1, RLENGTH-1)
+}
+
+function get_reg1() {
+ # only use if you already know there is 1 and only 1 register
+ match($0, /(r(1[0-5]|[0-9])|ip|sp|lr|ip),/)
+ return substr($0, RSTART, RLENGTH-1)
+}
+
+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 (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 }
+
+# KEEPING UP WITH THE STACK POINTER
+# sp should only be adjusted by pushing/popping or adding/subtracting constants
+#
+/pop \{[^\}]+\}/ {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ count = 0
+ for (i in reg_array) {
+ count++
+ }
+
+ adjust_sp_offset(-4 * count)
+}
+
+/ldmfd sp!,\{[^\}]+\}/ {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ count = 0
+ for (i in reg_array) {
+ count++
+ }
+
+ adjust_sp_offset(-4 * count)
+}
+
+/push \{[^\}]+\}/ {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ count = 0
+ for (i in reg_array) {
+ count++
+ }
+
+ adjust_sp_offset(4 * count)
+ offset = 4 * count
+}
+
+/stmfd sp!,\{[^\}]+\}/ {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ count = 0
+ for (i in reg_array) {
+ count++
+ }
+
+ adjust_sp_offset(4 * count)
+ offset = 4 * count
+}
+
+# TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
+#
+/stmfd sp!,\{[^\}]+\}/ {
+ if (in_function) {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ offset = -offset + 4
+ for (i in reg_array) {
+ register = reg_array[i]
+ if (!saved[register] && !dirty[register]) {
+ printf ".cfi_rel_offset %s, %d\n", register, offset
+ saved[register] = 1
+ offset += 4
+ }
+ }
+ }
+}
+
+/push \{[^\}]+\}/ {
+ if (in_function) {
+ match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
+ registers = substr($2, RSTART + 1, RLENGTH-2)
+
+ split(registers, reg_array, ",")
+ offset = -offset + 4
+ for (i in reg_array) {
+ register = reg_array[i]
+ if (!saved[register] && !dirty[register]) {
+ printf ".cfi_rel_offset %s, %d\n", register, offset
+ saved[register] = 1
+ offset += 4
+ }
+ }
+ }
+}
+
+# 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)
+/(ldr|adds|subs|ldr|ldrex|mov) (r(1[0-5]|[0-9])|ip|sp|lr|pc),/ {
+ trashed(get_reg1())
+}
+
+{
+ called = ""
+ printf ".loc 1 %d\n", line_number
+ print
+}
+
+END {
+ if (in_function)
+ print ".cfi_endproc"
+}
--
2.45.2
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [musl] [Suggestion] Add cfi directives to assembly code via chatgpt
2024-07-29 0:19 ` Howard Su
@ 2024-07-29 8:16 ` Szabolcs Nagy
0 siblings, 0 replies; 7+ messages in thread
From: Szabolcs Nagy @ 2024-07-29 8:16 UTC (permalink / raw)
To: Howard Su; +Cc: Rich Felker, Thorsten Glaser, musl
* Howard Su <howard0su@gmail.com> [2024-07-29 08:19:42 +0800]:
> Per suggestion, I added awk script to do so. Please help review. also I
> noticed that stack unwind is still not functional.
you mean you built musl with --enable-debug and a backtrace in
gdb is missing libc symbols? you should try to figure out what
is missing there.. (did you check the annotated asm? did you
previously managed to get unwind work with manual annotation?)
note that within the process unwinding won't work at runtime,
(at least across asm code) arm has a different unwind abi that
does not use dwarf information from eh_frame, and that would
require different annotation.
>
> On Tue, Jul 23, 2024 at 5:39 AM Szabolcs Nagy <nsz@port70.net> wrote:
>
> > * Howard Su <howard0su@gmail.com> [2024-07-22 14:39:35 +0800]:
> > > fair enough. Let's put aside that LLM writing the CFI directives. I do
> > have
> > > knowledge about it to hand write it.
> > >
> > > Do you think it is a good idea to put effort into adding CFI to those
> > > assembly code, especially the thread_cp part, which blocks my debug.
> >
> > note that there is now support in gas to synthetize the CFI for
> > manually written x86 asm code with --scfi=experimental
> > https://sourceware.org/binutils/docs/as.html
> >
> > the aarch64 support just got committed recently
> >
> > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=29085f7243e415db0e1617173eb80c441671ba0e
> >
> > there is no support for arm yet.
> >
> > meanwhile musl has tools/add-cfi*.awk that you could extend to
> > support arm, this is simpler than adding the gas support as it
> > only has to work for musl code.
> >
> >
>
> --
> -Howard
> From fbe4007ac7e6728b6226ca52ccbc5b77571bcece Mon Sep 17 00:00:00 2001
> From: Howard Su <howard0su@gmail.com>
> Date: Mon, 29 Jul 2024 08:17:31 +0800
> Subject: [PATCH] Add awk script to add CFI directives to arm
>
> ---
> crt/arm/crti.s | 2 -
> tools/add-cfi.arm.awk | 220 ++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 220 insertions(+), 2 deletions(-)
> create mode 100644 tools/add-cfi.arm.awk
>
> diff --git a/crt/arm/crti.s b/crt/arm/crti.s
> index 18dc1e41..acab83c9 100644
> --- a/crt/arm/crti.s
> +++ b/crt/arm/crti.s
> @@ -2,12 +2,10 @@
>
> .section .init
> .global _init
> -.type _init,%function
> _init:
> push {r0,lr}
>
> .section .fini
> .global _fini
> -.type _fini,%function
> _fini:
> push {r0,lr}
> diff --git a/tools/add-cfi.arm.awk b/tools/add-cfi.arm.awk
> new file mode 100644
> index 00000000..93b94768
> --- /dev/null
> +++ b/tools/add-cfi.arm.awk
> @@ -0,0 +1,220 @@
> +# Insert GAS CFI directives ("control frame information") into arm 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
> +}
> +
> +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(1[0-5]|[0-9])|ip|sp|lr|pc)/)
> + return substr($0, RSTART+1, RLENGTH-1)
> +}
> +
> +function get_reg1() {
> + # only use if you already know there is 1 and only 1 register
> + match($0, /(r(1[0-5]|[0-9])|ip|sp|lr|ip),/)
> + return substr($0, RSTART, RLENGTH-1)
> +}
> +
> +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 (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 }
> +
> +# KEEPING UP WITH THE STACK POINTER
> +# sp should only be adjusted by pushing/popping or adding/subtracting constants
> +#
> +/pop \{[^\}]+\}/ {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + count = 0
> + for (i in reg_array) {
> + count++
> + }
> +
> + adjust_sp_offset(-4 * count)
> +}
> +
> +/ldmfd sp!,\{[^\}]+\}/ {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + count = 0
> + for (i in reg_array) {
> + count++
> + }
> +
> + adjust_sp_offset(-4 * count)
> +}
> +
> +/push \{[^\}]+\}/ {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + count = 0
> + for (i in reg_array) {
> + count++
> + }
> +
> + adjust_sp_offset(4 * count)
> + offset = 4 * count
> +}
> +
> +/stmfd sp!,\{[^\}]+\}/ {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + count = 0
> + for (i in reg_array) {
> + count++
> + }
> +
> + adjust_sp_offset(4 * count)
> + offset = 4 * count
> +}
> +
> +# TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
> +#
> +/stmfd sp!,\{[^\}]+\}/ {
> + if (in_function) {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + offset = -offset + 4
> + for (i in reg_array) {
> + register = reg_array[i]
> + if (!saved[register] && !dirty[register]) {
> + printf ".cfi_rel_offset %s, %d\n", register, offset
> + saved[register] = 1
> + offset += 4
> + }
> + }
> + }
> +}
> +
> +/push \{[^\}]+\}/ {
> + if (in_function) {
> + match($2, /\{((r(1[0-5]|[0-9])|ip|sp|lr|pc|,)+)\}/)
> + registers = substr($2, RSTART + 1, RLENGTH-2)
> +
> + split(registers, reg_array, ",")
> + offset = -offset + 4
> + for (i in reg_array) {
> + register = reg_array[i]
> + if (!saved[register] && !dirty[register]) {
> + printf ".cfi_rel_offset %s, %d\n", register, offset
> + saved[register] = 1
> + offset += 4
> + }
> + }
> + }
> +}
> +
> +# 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)
> +/(ldr|adds|subs|ldr|ldrex|mov) (r(1[0-5]|[0-9])|ip|sp|lr|pc),/ {
> + trashed(get_reg1())
> +}
> +
> +{
> + called = ""
> + printf ".loc 1 %d\n", line_number
> + print
> +}
> +
> +END {
> + if (in_function)
> + print ".cfi_endproc"
> +}
> --
> 2.45.2
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-07-29 8:16 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-19 9:06 [musl] [Suggestion] Add cfi directives to assembly code via chatgpt Howard Su
2024-07-19 14:18 ` Thorsten Glaser
2024-07-19 21:40 ` Rich Felker
2024-07-22 6:39 ` Howard Su
2024-07-22 21:39 ` Szabolcs Nagy
2024-07-29 0:19 ` Howard Su
2024-07-29 8:16 ` Szabolcs Nagy
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).