libk  Check-in [f0f16493ca]

Overview
Comment:make crt behave as a C runtime launcher should; comment code thoroughly
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f0f16493caf8ef1de1c32504d868442cdfbc25c4fea7231295f005a4cae1b674
User & Date: lexi on 2019-08-21 03:44:37
Original Comment: make crt behave properly, hopefully fixing segfault on nixos; comment code thoroughly
Other Links: manifest | tags
Context
2019-08-21
04:24
modify build system to replace extremely fragile error code generation mechanism check-in: d77ef26adf user: lexi tags: trunk
03:44
make crt behave as a C runtime launcher should; comment code thoroughly check-in: f0f16493ca user: lexi tags: trunk
03:10
add assembly output flag to build.sh check-in: 06e32bd3b4 user: lexi tags: trunk
Changes

Modified kcore/boot.rt.c from [6007ea2ce5] to [a5bf4923b7].

     1      1   #include <k/core.h>
     2      2   extern stat entry(kenv);
     3      3   
     4         -stat _boot(unsigned int argc, char** argv) {
            4  +unsigned long long
            5  +_boot(unsigned int argc, /* argument count */
            6  +		char** argv, /* arguments */
            7  +		char** envp /* environment */ ) {
     5      8   	kenv e = { 
     6         -		// todo: determine terminal class and set term vs ansi correctly!
            9  +		/* TODO: determine terminal class and set term vs ansi correctly! */
     7     10   		{ {kiostream_term, 0}, {kiostream_term, 1} }, // chan std
     8     11   		{ {kiostream_closed},  {kiostream_term, 2} }, // chan err
     9         -		argc, argv,
    10         -		null // no environment yet
           12  +		argc, argv, 
           13  +		null // needs parsing
    11     14   	};
    12     15   	return entry(e);
    13     16   }
    14     17   	

Modified kcore/boot.rt.x86.lin.64.s from [c4c48f7422] to [3c036595d7].

     2      2   bits 64
     3      3   %include "../arch/posix/x86.lin.64.s"
     4      4   global _start:function
     5      5   extern _boot
     6      6   extern entry
     7      7   
     8      8   _start:
     9         -	mov rbp, rsp
    10         -	mov rdi, [rbp + 0] ; argc
    11         -	lea rsi, [rbp + 8] ; argv
            9  +	mov rbp, 0 ; zero the stack base ptr.
           10  +	; attempted fix for a difficult-to-
           11  +	; reproduce bug
           12  +
           13  +	mov rdi, 0 ; zero rdi - because argc
           14  +	; is 32 bits long, we have to store
           15  +	; its value in the lower half of rdi.
           16  +	; this ensures that the upper half is
           17  +	; zeroed as well.
           18  +
           19  +	mov edi, [rsp + 0] ; sizeof arguments
           20  +	; first argument to _boot(): argc
           21  +	; this is a 32-bit signed(??) integer
           22  +	; that is equal to the number of
           23  +	; elements in argv (see below). it is
           24  +	; not strictly necessary, because argv
           25  +	; is per spec always null-terminated,
           26  +	; but we pass it just in case.
           27  +
           28  +	lea rsi, [rsp + 8] ; &arguments
           29  +	; 2nd argument to _boot(): ptr to argv
           30  +	; this points to an array of strings
           31  +	; containing the program's command line
           32  +	; arguments. _boot() does not need to 
           33  +	; parse this, but it does need to store
           34  +	; it in the structure passed to main()
           35  +
           36  +	lea rdx, [rsp + 16] ; &environment
           37  +	; third argument to _boot(): ptr to envp
           38  +	; this points to the list of environment
           39  +	; variables for the running program. it
           40  +	; is the responsibility of _boot to parse
           41  +	; this list and arrange it into a set of
           42  +	; legible and useful C arrays.
           43  +
           44  +	mov rax, 0 ; zero out %rax
           45  +	; this is required by the C ABI, and is
           46  +	; reputedly necessary for compatibility
           47  +	; with icc, intel's own proprietary C
           48  +	; compiler.
           49  +
           50  +	call _boot ; invoke the start function
           51  +	; that will set up the runtime and
           52  +	; construct the necessary structures
           53  +	; that will be bassed to libc.
           54  +
           55  +	; boot has returned and left its
           56  +	; return value in the register %rax.
           57  +	; regardless of the size of the
           58  +	; return value of main(), _boot always
           59  +	; returns the system word length.
           60  +
           61  +	mov sys.reg.1, sys.reg.ret ; fill in
           62  +	; the return value as exit's argument
    12     63   
    13         -	call _boot
           64  +	mov sys.reg.0, sys.exit ; set %rax to
           65  +	; the syscall number of exit
    14     66   
    15         -	mov sys.reg.1, sys.reg.ret
    16         -	mov sys.reg.0, sys.exit
    17         -	sys.call
           67  +	sys.call ; invoke the kernel