Skip to content

Instantly share code, notes, and snippets.

@jevinskie
Created March 14, 2025 21:28
Show Gist options
  • Save jevinskie/e6a0c2f3b53a71a086f814c5c1bcc8b8 to your computer and use it in GitHub Desktop.
Save jevinskie/e6a0c2f3b53a71a086f814c5c1bcc8b8 to your computer and use it in GitHub Desktop.
XNU image activation stack layout

exec_copyout_strings

In kern_exec.c

Copy out the strings segment to user space. The strings segment is put on a preinitialized stack frame.

Note

The strings segment layout is backward, from the beginning of the top of the stack to consume the minimal amount of space possible; the returned stack pointer points to the end of the area consumed (stacks grow downward).

  • argc is an int
  • arg[i] are pointers
  • env[i] are pointers
  • the 0's are (void *)NULL's

Stack frame layout

     +-------------+ <- p->user_stack
     |     16b     |
     +-------------+
     | STRING AREA |
     |      :      |
     |      :      |
     |      :      |
     +- -- -- -- --+
     |  PATH AREA  |
     +-------------+
     |      0      |
     +-------------+
     |  applev[n]  |
     +-------------+
            :
            :
     +-------------+
     |  applev[1]  |
     +-------------+
     | exec_path / |
     |  applev[0]  |
     +-------------+
     |      0      |
     +-------------+
     |    env[n]   |
     +-------------+
            :
            :
     +-------------+
     |    env[0]   |
     +-------------+
     |      0      |
     +-------------+
     | arg[argc-1] |
     +-------------+
            :
            :
     +-------------+
     |    arg[0]   |
     +-------------+
     |     argc    |
sp-> +-------------+

Although technically a part of the STRING AREA, we treat the PATH AREA as a separate entity.

This allows us to align the beginning of the PATH AREA to a pointer boundary so that the exec_path, env[i], and argv[i] pointers which preceed it on the stack are properly aligned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment