libc:

	Description:
		C11 compliant C library. Includes threading TLS support for optimal
			performance, BSD extensions, and some Plan 9 functions.

		Easily portable following the template.

		TLS, standard streams, exit() termination behaviour not enforced, depends
			on what is used and final binary is still C compliant.

		<grp.h> and <pwd.h>, not contained in C library to allow for
			different login semantics (headers also not provided).

		<pthread.h> is provided by separate library, allowing the use of
			different POSIX threads implementation.

		<threads.h> provided as static inline C functions to allow for
			inlining and optimizations.

		malloc(), calloc(), realloc(), etc provided in separate library to allow
			the use of different implementations for different uses.

		libc.a
			- All entry points and static code.

		libc.so
			- No entry points.
			- Dynamic code, still need to find a way to include all non-TLS,
				TLS, standard stream, versions of functions.

		Architecture is selected from the common names: amd64, x86-64, i386, etc.
			Makefile config.mk macro ARCH = amd64

		OS is selected using __OS__ notation defined by POSIX C.
			Makefile config.mk macro: OS = __linux__


	GOALS:
		- Produce clean binaries (minimal required amount of symbols and sections
			in final binary).
		- TLS by default. Also would like to include non-TLS threading as a build
			option.
		- Easily portable following the build template.
		- C11 <stdatomic.h> atomics for minimally lockless threading.
		- Posix threads <pthread.h> is backed by minimal C11
			<threads.h> implementation.
		- Supports C11 _Thread_local for TLS.

	PORTING:
		runtime:
			_start.s:
				- Provide _start as the weak entry point symbol.
				- Entry point into userspace or from dynamic linker.
				- If stack is unaligned (depending on Kernel ABI) align
					the stack pointer here.
				- Must include the following undefined symbols:
					__c_init_main
					__c_call_main
					__c_exit
				- /* FALLTHROUGH */
			init_tls.s:
				- Calls into init_tls() provided in C.
				- Must define the following symbols with linker script:
					__tdata_start
					__tdata_end
					_tbss_start
					__tdata_end
				- /* FALLTHROUGH */
			init_main.s:
				- Initalize argc, argv, and envp according to the userspace
					ABI for the call to main.
				- Usually they are on the stack.
				- Must define __c_init_main as a function in section
					.discard with no allocation.
				- /* FALLTHROUGH */
			init_argv0.s:
				- Initialize argv0.
				- Use the argument for *argv[] in init_main to initialize.
				- Linker can decide program counter relative or absolute
					reference.
				- char *argv0 is provided in argv0.c
				- init_argv0.o and argv0.o are combined later.
				- /* FALLTHROUGH */
			init_environ.s:
				- Initialize environ.
				- Use the argument for *envp[] in init_main to initialize.
				- Linker can decide program counter relative or absolute
					reference.
				- char **environ is provided in environ.c.
				- init_environ.o and environ.o are combined later.
				- /* FALLTHROUGH */
			init_auxv.s:
				- Initialize auxv.
				- Use the argument for *envp[] in init_main to initialize
					by looping until envp == NULL, auxv is next.
				- Linker can decide program counter relative or absolute
					reference.
				- auxv_t *auxv is provided in auxv.c.
				- init_auxv.o and auxv.o are combined later.
				- /* FALLTHROUGH */
			call_main.s:
				- Call C entry point main().
				- Must define __c_call_main as a function in section
					.discard with no allocation.
				- Set return value from main in the first argument.
				- /* FALLTHROUGH */

		exit:
			exit.s:
				- Define the exit() function symbol.
				- Requires __c_exit symbol.
				- /* FALLTHROUGH */
			__c_exit_save_arg.s:
				- Defines __c_exit_save_arg symbol in the section .discard.
				- Save the function argument in a callee saved register.
				- /* FALLTHROUGH */
			call__c_atexit_cleanup.s:
				- Call __c_atexit_cleanup().
				- Requires __c_exit_save_arg and __c_exit_return_arg
					symbols.
				- References global function __c_atexit_cleanup.
				- call__c_atexit_cleanup.c provides global data structure
					used for atexit() function registration.
				- /* FALLTHROUGH */
			call__c_stream_cleanup.s:
				- Call __c_stream_cleanup().
				- Requires __c_exit_save_arg and __c_exit_return_arg
					symbols.
				- References global function __c_stream_cleanup()
				- call__c_stream_cleanup.c provides global data structure
					used for stream accounting.
				- /* FALLTHROUGH */
			__c_exit_return_arg.s:
				- Defines __c_exit_return_arg symbol in the section
					.discard.
				- Return the saved argument.
				- /* FALLTHROUGH */
			_Exit.s:
				- Define the _Exit() function symbol.
				- Requires __c_exit symbol.
				- /* FALLTHROUGH */
			_exit.s:
				- Define the _exit() function symbol.
				- Requires __c_exit symbol.
				- /* FALLTHROUGH */
			__c_exit.s:
				- Define the global __c_exit() function symbol and C exit
					behaviour (cause expected program termination
					identical to _exit()).
				- /* NORETURN */

	NOTE:
		Ensure libc.a is the last library searched in the linker directory path.
			- The linker needs to select the right entry point in libc.a
				depending on what symbols are undefined. If an object file
				or library along the build path uses thread local storage
				the linker needs to choose the entry point that initializes
				the main thread TLS.

	Entry and exit points can't be moved to a shared library without added complexity
	in the build system.
	All binaries will have copies of the entry code although this is negligible.
	Streams are lazily initilized with per stream locks. Need to think of a way to
	build without locks if not using TLS.

	libc.a:
		Contains static routines.

	libc.so:
		Contains dynamic versions of routines and no entry or exit code.
		At link time the executable entry and exit points will be built using
		libc.a.

	Don't use trampolines and lazy initialization/clean-up because the trampolines
		are an unnecessary attack vector.

	- stdio_u.h (u = unlocked)
		- Unlocked versions of stream functions.
		- Potentially unnecessary because of POSIX api (file descriptors).

	- stdio.h:
		- Include way for file streams to share buffers and offsets for reading and writing.

	- Include BSD extensions.

	- err.h (must be included with C library)
		- BSD error and warning printing.
		- Be careful when referencing stderr, use indirection on locks and buffer.
			- That way FILE stderr is not included in symbol table.
		- Include argv0 as the program name (directory removed).
			- Allow caller to initialize via ARGBEGIN? or initialize in crt.s?