Skip to content

Commit dd19532

Browse files
bmourad01bmourad01
andauthored
Adds some missing functionality to Primus Lisp POSIX (#1489)
* Adds some missing functionality to Primus Lisp POSIX - The thread local storage structure (on SysV x86-64) had some additional data to set up - Added `gets` and `strchrnul` to `stdio` and `string`, respectively Co-authored-by: bmourad01 <bmourad@draper.com>
1 parent 253afc1 commit dd19532

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

plugins/primus_lisp/site-lisp/libc-init.lisp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,28 @@
2424
(set X1 0))
2525

2626

27-
(defun setup-stack-canary ()
27+
(defun setup-thread-local-storage ()
2828
(declare (context (abi "sysv"))
29-
(global program:FS_BASE))
30-
(set FS_BASE (- brk 0x28))
31-
(memory-allocate brk (sizeof ptr_t))
32-
(write-word ptr_t brk 0xDEADBEEFBEAFDEAD)
33-
(+= brk (sizeof ptr_t)))
29+
(global x86-64:FS_BASE))
30+
(let ((tcb-size (+ (* 6 (sizeof ptr_t))
31+
(* 2 (sizeof int)))))
32+
(set FS_BASE brk)
33+
;; tcbhead_t structure
34+
(memory-allocate brk tcb-size 0)
35+
(write-word ptr_t brk brk) ; tcb
36+
(write-word ptr_t (+ brk 0x28) 0xDEADBEEFBEAFDEAD) ; stack_guard
37+
;; ptmalloc structure
38+
(memory-allocate (- brk 0x28) (sizeof ptr_t) 0) ; arena
39+
(memory-allocate (- brk 0x38) (sizeof ptr_t) 0) ; freelist
40+
;; misc
41+
(memory-allocate (- brk 0x40) (sizeof int) 0) ; errno
42+
(+= brk tcb-size)))
3443

3544
(defun init (main argc argv auxv)
3645
"GNU libc initialization stub"
3746
(declare (external "__libc_start_main")
3847
(context (abi "sysv")))
39-
(setup-stack-canary)
48+
(setup-thread-local-storage)
4049
(exit-with (invoke-subroutine main argc argv)))
4150

4251
(defun init (args on-exit main)

plugins/primus_lisp/site-lisp/stdio.lisp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@
130130
(memory-write (+ ptr (min n i)) 0:8)
131131
ptr))))
132132

133+
(defun gets (ptr)
134+
(declare (external "gets"))
135+
(let ((str *standard-input*)
136+
(i 0)
137+
(continue true))
138+
(while continue
139+
(let ((c (fgetc str)))
140+
(if (= c -1)
141+
(set continue false)
142+
(memory-write (+ ptr i) (cast char c))
143+
(set continue (/= c 0xA))
144+
(incr i))))
145+
(memory-write (+ ptr i) 0:8)
146+
ptr))
147+
133148

134149
(defun getchar ()
135150
(declare (external "getchar" "getchar_unlocked"))

plugins/primus_lisp/site-lisp/string.lisp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989
(declare (external "strrchr" "rindex"))
9090
(memrchr p c (+ (strlen p) 1)))
9191

92+
(defun strchrnul (p c)
93+
(declare (external "strchrnul"))
94+
(let ((p (strchr p c)))
95+
(if p p (strchr p 0))))
9296

9397
(defun strpbrk (str set)
9498
(declare (external "strpbrk"))

0 commit comments

Comments
 (0)