From efa12a67f95d0637bd00d116bdb193a58d72847c Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Mon, 7 Mar 2022 20:23:39 -0800 Subject: [PATCH] Update OpenExecutable logic to properly check BSD/Linux error conditions --- libc/runtime/openexecutable.S | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/libc/runtime/openexecutable.S b/libc/runtime/openexecutable.S index 7efdcd9c3..8ed1ae5f5 100644 --- a/libc/runtime/openexecutable.S +++ b/libc/runtime/openexecutable.S @@ -121,19 +121,20 @@ OpenExecutable: mov -0x08(%rbp),%eax # __NR_open mov %r14,%rdi mov -0x20(%rbp),%esi # O_RDWR - syscall - mov %eax,%r15d - mov $-1,%eax # check if successful - cmp %eax,%r15d - jne 0f + clc # clear carry flag + syscall + jc .Lohno # bsd error + cmp $-4095,%eax + jae .Lohno # linux error + jmp .Lok -// Open executable in read-only mode. - mov -0x08(%rbp),%eax # __NR_open - mov %r14,%rdi - mov -0x48(%rbp),%esi # O_RDONLY - syscall - mov %eax,%r15d -0: +// Open executable in read-only mode. +.Lohno: mov -0x08(%rbp),%eax # __NR_open + mov %r14,%rdi + mov -0x48(%rbp),%esi # O_RDONLY + syscall + +.Lok: mov %eax,%r15d // Map code segment. mov -0x10(%rbp),%eax # __NR_mmap