Martins3.github.io icon indicating copy to clipboard operation
Martins3.github.io copied to clipboard

argv -> envp

Open Martins3 opened this issue 3 years ago • 2 comments

int main(int argc, char *argv[]){
  int i = 0 ;
  while(argv[i] != NULL){
    printf("%s", argv[i]);
    i ++;
  }

  i ++;

  while(argv[i] != NULL){
    printf("%s", argv[i]);
    i ++;
  }

  return 0;
}
➜  a ./a.out
./a.outDISPLAY=:0SESSION_MANAGER=local/maritns3-pc:@/tmp/.ICE-unix/1602,unix/maritns3
-pc:/tmp/.ICE-unix/1602PAPERSIZE=a4SSH_AGENT_PID=1539LANGUAGE=LANG=en_US.UTF-8SSH_AUT
H_SOCK=/run/user/1000/keyring/sshXDG_CURRENT_DESKTOP=ubuntu:GNOMEGIO_LAUNCHED_DESKTOP
_FILE_PID=2451LC_IDENTIFICATION=zh_CN.UTF-8PWD=/home/maritns3/aLIBVIRT_DEFAULT_URI=qe
mu:///systemGPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1DESKTOP_SESSION=ubuntu
USER=maritns3XDG_MENU_PREFIX=gnome-HOME=/home/maritns3QT_IM_MODULE=fcitxNO_AT_BRIDGE=
1LC_MEASUREMENT=zh_CN.UTF-8QT4_IM_MODULE=fcitxTERM=xterm-256colorDBUS_SESSION_BUS_ADD
RESS=unix:path=/run/user/1000/busLC_NUMERIC=zh_CN.UTF-8GTK_MODULES=gail:atk-bridge_=/
home/maritns3/a/./a.outWINDOWPATH=2GTK_IM_MODULE=fcitxJOURNAL_STREAM=9:51408QT_ACCESS
IBILITY=1GNOME_DESKTOP_SESSION_ID=this-is-deprecatedMANAGERPID=1133LC_TIME=zh_CN.UTF-
8XDG_SESSION_DESKTOP=ubuntuLOGNAME=maritns3CLUTTER_IM_MODULE=fcitxLC_PAPER=zh_CN.UTF-
8PATH=/home/maritns3/.cargo/bin:/home/maritns3/.cargo/bin:/home/maritns3/.local/bin:/
home/maritns3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/g
ames:/usr/local/games:/snap/bin:/home/maritns3/.antigen/bundles/skywind3000/z.lua:/ho
me/maritns3/.antigen/bundles/zsh-users/zsh-autosuggestions:/home/maritns3/.fzf/bin:/h
ome/maritns3/.cache/vimfiles/.cache/init.vim/.dein/bin/XDG_RUNTIME_DIR=/run/user/1000
XMODIFIERS=@im=fcitxGNOME_SHELL_SESSION_MODE=ubuntuXDG_SESSION_TYPE=x11LC_MONETARY=zh
_CN.UTF-8SHELL=/usr/bin/zshLC_TELEPHONE=zh_CN.UTF-8USERNAME=maritns3XDG_CONFIG_DIRS=/
etc/xdg/xdg-ubuntu:/etc/xdgVTE_VERSION=6003INVOCATION_ID=230ade44340249909c75f72ca1d4
8fc1SHLVL=2XAUTHORITY=/run/user/1000/gdm/XauthorityXDG_DATA_DIRS=/usr/share/ubuntu:/u
sr/local/share/:/usr/share/:/var/lib/snapd/desktopLC_NAME=zh_CN.UTF-8XDG_SESSION_CLAS
S=userLC_ADDRESS=zh_CN.UTF-8GDMSESSION=ubuntuIM_CONFIG_PHASE=1IBUS_DISABLE_SNOOPER=1O
LDPWD=/home/maritns3/aZSH=/home/maritns3/.oh-my-zshPAGER=lessLESS=-RLSCOLORS=Gxfxcxdx
bxegedabagacadLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40
;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=
37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;3
1:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;3
1:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz
=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=
01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=0
1;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;
31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01
;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=0
1;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=
01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=
01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=
01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=0
1;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;3
5:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36
:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36
:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:_ZL_FZF_FLA
G=-e_ZL_MATCH_MODE=1VIMRUNTIME=/usr/share/nvim/runtimeNVIM_LISTEN_ADDRESS=/tmp/nvimd5
AKJC/0VIM=/usr/share/nvimMYVIMRC=/home/maritns3/.SpaceVim/init.vimLC_ALL=LC_MESSAGES=
en_US.UTF-8SPACEVIM_SERVER_ADDRESS=/tmp/spacevim_nvim_serverCOC_VIMCONFIG=/home/marit
ns3/.SpaceVimCOC_DATA_HOME=/home/maritns3/.config/cocVIM_SERVERNAME=/tmp/nvimd5AKJC/0
VIM_EXE=/usr/bin/nvim%
➜  a

Martins3 avatar Oct 28 '20 11:10 Martins3

So, Linux put envp just after the argv(one null as gap)

Martins3 avatar Oct 28 '20 11:10 Martins3

getauxval() and the auxiliary vector

Historically, many UNIX systems have implemented the auxiliary vector feature. In essence, it is a list of key-value pairs that the kernel's ELF binary loader (fs/binfmt_elf.c in the kernel source) constructs when a new executable image is loaded into a process. This list is placed at a specific location in the process's address space; on Linux systems it sits at the high end of the user address space, just above the (downwardly growing) stack, the command-line arguments (argv), and environment variables (environ).

AT_SYSINFO_EHDR: The value is a pointer to the page containing the Virtual Dynamic Shared Object (VDSO) that the kernel creates in order to provide fast implementations of certain system calls. (Some documentation on the VDSO can be found in the kernel source file Documentation/ABI/stable/vdso.)

we can see the key-value pairs in the vector, followed by a pair of zero values that indicate the end of the vector:

  $ od -t d8 /proc/15558/auxv

Martins3 avatar Nov 02 '20 14:11 Martins3