The magic behind shebangs!

These are the notes I took to do my video about shebangs, which is available here:

Como shebangs functionam - youtube short video
Como shebangs functionam - youtube short video

and here:

Como shebangs funcionam - tiktokt video

Unfortunately, all the videos are in portuguese. However, my notes are available bellow and the code is open source and available here: https://github.com/Dpbm/video-shebang.

Enjoy :)

Notes

Calling trace

execve -> do_execveat_common -> create a linux_binprm containing data from it (DEFINE_CLASS -> alloc_bprm ) -> bprm_execve -> exec_binprm -> search_binary_handler -> prepare_binprm -> kernel_read (read the file) ->

it them iterates over a linked list of formats

list_for_each_entry(fmt, &formats, lh) {

the exec.c provides an external function `__register_binfmt` that is used by each type in `binfmt_*.c` to register in the lits, calling like:

core_initcall(init_elf_binfmt);


-> load_binary from struct linux_binprm

if it's a script, it loads `binfmt_script.c` that has the method `load_script` which checks for the shebang

-> open_exec (from exec.c) -> do_open_execat

References