diff --git a/plugins/primus_lisp/site-lisp/libc-init.lisp b/plugins/primus_lisp/site-lisp/libc-init.lisp index e376867ff..eaed57d45 100644 --- a/plugins/primus_lisp/site-lisp/libc-init.lisp +++ b/plugins/primus_lisp/site-lisp/libc-init.lisp @@ -24,19 +24,28 @@ (set X1 0)) -(defun setup-stack-canary () +(defun setup-thread-local-storage () (declare (context (abi "sysv")) - (global program:FS_BASE)) - (set FS_BASE (- brk 0x28)) - (memory-allocate brk (sizeof ptr_t)) - (write-word ptr_t brk 0xDEADBEEFBEAFDEAD) - (+= brk (sizeof ptr_t))) + (global x86-64:FS_BASE)) + (let ((tcb-size (+ (* 6 (sizeof ptr_t)) + (* 2 (sizeof int))))) + (set FS_BASE brk) + ;; tcbhead_t structure + (memory-allocate brk tcb-size 0) + (write-word ptr_t brk brk) ; tcb + (write-word ptr_t (+ brk 0x28) 0xDEADBEEFBEAFDEAD) ; stack_guard + ;; ptmalloc structure + (memory-allocate (- brk 0x28) (sizeof ptr_t) 0) ; arena + (memory-allocate (- brk 0x38) (sizeof ptr_t) 0) ; freelist + ;; misc + (memory-allocate (- brk 0x40) (sizeof int) 0) ; errno + (+= brk tcb-size))) (defun init (main argc argv auxv) "GNU libc initialization stub" (declare (external "__libc_start_main") (context (abi "sysv"))) - (setup-stack-canary) + (setup-thread-local-storage) (exit-with (invoke-subroutine main argc argv))) (defun init (args on-exit main) diff --git a/plugins/primus_lisp/site-lisp/stdio.lisp b/plugins/primus_lisp/site-lisp/stdio.lisp index b46dcfd19..9083bbdf1 100644 --- a/plugins/primus_lisp/site-lisp/stdio.lisp +++ b/plugins/primus_lisp/site-lisp/stdio.lisp @@ -130,6 +130,21 @@ (memory-write (+ ptr (min n i)) 0:8) ptr)))) +(defun gets (ptr) + (declare (external "gets")) + (let ((str *standard-input*) + (i 0) + (continue true)) + (while continue + (let ((c (fgetc str))) + (if (= c -1) + (set continue false) + (memory-write (+ ptr i) (cast char c)) + (set continue (/= c 0xA)) + (incr i)))) + (memory-write (+ ptr i) 0:8) + ptr)) + (defun getchar () (declare (external "getchar" "getchar_unlocked")) diff --git a/plugins/primus_lisp/site-lisp/string.lisp b/plugins/primus_lisp/site-lisp/string.lisp index 3c515321d..05008c814 100644 --- a/plugins/primus_lisp/site-lisp/string.lisp +++ b/plugins/primus_lisp/site-lisp/string.lisp @@ -89,6 +89,10 @@ (declare (external "strrchr" "rindex")) (memrchr p c (+ (strlen p) 1))) +(defun strchrnul (p c) + (declare (external "strchrnul")) + (let ((p (strchr p c))) + (if p p (strchr p 0)))) (defun strpbrk (str set) (declare (external "strpbrk"))