JFLib

plat/linux_glibc.rs (Linux)#

Source: lib/plat/linux_glibc.rs

linux_glibc -- a program linked against glibc (--cfg jfgui_glibc; build_rs.py section "glibc",
docs/GPU.md "Linux"): every Linux GPU driver is a glibc library

  _start, glibc_main          the process start through __libc_start_main (ld.so has set up the
                              main thread's TLS and run libc's initialisers by then), then what
                              linux_entry does in a static program
  std_start()                 (feature std: every program with std on Linux is this build) the
                              start from the program's fn main, std's arguments and environment
                              as plat reads them from the initial stack, the end through std
  exit(code)                  plat::exit: glibc's exit (atexit handlers, the destructors of the
                              loaded libraries, stdio); so does returning from the program's main.
                              Crash reports end in rt::os::exit as everywhere: exit_group, no
                              destructor runs inside a signal handler
  at_exit(f)                  f at that normal end, before the handlers registered earlier
  dl_open / dl_sym / dl_close / dl_error    the system's dynamic linker (lib/gpu: libvulkan.so.1);
                              plugins keep their own loader (linux_elfload.rs: they import nothing)
  glibc_version()             the running libc ("2.43")
  glibc_thread_ids()          the calling thread's FS base, pthread_self, &errno (gputest --test glibc)

Threads: linux_thread.rs, its last part: pthread_create on the same mapping as in a static
program.  Everything else of plat stays raw system calls next to glibc, which is fine: they do
not touch errno or glibc's state.  The fault handlers (linux_crash.rs, linux_guard.rs) use
rt_sigaction directly and leave glibc's two internal signals alone.  A driver's own threads have
no alternate signal stack, the report runs on their stack; Mesa's block every signal but SIGSEGV
and SIGSYS, so another fault there (SIGILL in llvmpipe's JIT code) ends the process at once, by the
kernel, without a report.  The allocator (rt::mem) has its own mappings and takes no TLS, so a
driver's thread may call back into it (debug messages); a child after a raw fork only makes system
calls until its exec.  plat::setenv changes JFLib's list, which plat::env and child processes see;
a driver's getenv reads the start environment.
  • fn std_start() -> ! (feature std) the start of a program with std, from its fn main (rt::main!, ui::main! or the program's own): std's arguments and environment as a block like the initial stack, then what glibc_main does, with rt's panic hook first; the end through std::process::exit. With the feature unwind a panic that leaves the app ends it there.

  • fn exit(code: i32) -> ! end the process: glibc's exit (at_exit handlers, the destructors of the loaded libraries); from any thread, not from a signal handler

  • fn at_exit(f: fn()) run f at the normal end of the process (plat::exit, or the program's main returns), not after a crash; before the handlers registered earlier

  • fn dl_open(name: &str) -> Option<usize> open a library through the system's dynamic linker (a name, searched as ld.so does, or a path) and run its initialisers -> its handle

  • unsafe fn dl_sym(handle: usize, name: &[u8]) -> usize the address of a symbol of a library from dl_open (0: none); name ends in NUL

    Safety#

    handle came from dl_open and has not been closed: the dynamic linker reads its structures before it could tell a wrong handle.

  • unsafe fn dl_close(handle: usize) close a library from dl_open

    Safety#

    handle came from dl_open and is closed once; nothing uses what dl_sym found in it any more (the library may be unloaded).

  • fn dl_error() -> String why the calling thread's last dl_open / dl_sym failed ("": it did not, or it was already asked)

  • fn glibc_version() -> String the version of the running glibc ("2.43")

  • fn glibc_thread_ids() -> (usize, usize, usize) the calling thread's FS base, pthread_self() and the address of errno: its thread control block and thread-local storage (a raw clone would have 0 and the creator's)

  • fn glibc_thread_ids() -> (usize, usize, usize) the calling thread's thread pointer (TPIDR_EL0: its TCB), pthread_self() and the address of errno

Source: docs/API.md in the JFLib repository.