[PATCH] libgloss, aarch64: Fix off-by-one in exception handler

Alex Coplan <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
There is a catch-all trap handler in the EL3 boot code found in
libgloss/aarch64/cpu-init/rdimon-aem-el3.S.  It makes a call to write
which is equivalent to the following C code:

write(STDERR_FILENO, "Terminated by exception.\n", 26);

the problem is that 26 is the length of the string + 1, the correct
length is 25:

$ python3 -c 'print(len("Terminated by exception.\n"))'
25

Therefore, as things stand, the trailing NUL byte is also written to
stderr; this can be seen by inspecting the output of binaries built with
e.g. -specs=aem-ve.specs before and after this patch is applied, as in
the below:

$ cat run_qemu.sh
#!/bin/bash
qemu-system-aarch64 -machine virt,secure=on -cpu neoverse-v1 -m 2g \
  -nographic -semihosting -device loader,file=$1,cpu-num=0
$ ./run_qemu.sh before.exe 2>&1 | xxd
00000000: 5465 726d 696e 6174 6564 2062 7920 6578  Terminated by ex
00000010: 6365 7074 696f 6e2e 0a00                 ception...
$ ./run_qemu.sh after.exe 2>&1 | xxd
00000000: 5465 726d 696e 6174 6564 2062 7920 6578  Terminated by ex
00000010: 6365 7074 696f 6e2e 0a                   ception..

This simple patch fixes the off-by-one error, passing the correct length
to write in the exception handler.
---

OK to commit?  I'll need someone to commit on my behalf as I don't have
write access.

Thanks,
Alex

---
 libgloss/aarch64/cpu-init/rdimon-aem-el3.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
0001-libgloss-aarch64-Fix-off-by-one-in-exception-handler.patch (text/x-patch, 418 B)
diff --git a/libgloss/aarch64/cpu-init/rdimon-aem-el3.S b/libgloss/aarch64/cpu-init/rdimon-aem-el3.S
index 0296a8054..19dfee4f3 100644
--- a/libgloss/aarch64/cpu-init/rdimon-aem-el3.S
+++ b/libgloss/aarch64/cpu-init/rdimon-aem-el3.S
@@ -68,7 +68,7 @@ lower_a32_fiq:
 lower_a32_serror:
 	mov	x0, 2
 	adr	x1, .LC3
-	mov	x2, 26
+	mov	x2, 25
 	bl	FUNCTION (write)
 	mov	x0,  126
 	b	FUNCTION (exit)		/* Cannot return.  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.