News for hackers. Stuff that matters.
Re:Actually Portable Executables by matheusmoreira on Monday March 01, 2021 @07:07AM (#26301951)
Not sure why the stack is unaligned there. The way I solved this in liblinux is to just pass the stack pointer to a C function and compute all the other pointers there. That way the compiler deals with the stack stuff.

  _start:
      xorq %rbp,%rbp    /* user code should mark the deepest stack frame
                         * by setting the frame pointer to zero
                         */

      movq %rsp,%rdi

      call liblinux_start

      movq %rax,%rdi
      movq $__NR_exit,%rax
      syscall

  static void *after(void *vector) {
      void **pointer = (void **) vector;
      while (*pointer++ != 0);
      return pointer;
  }

  int liblinux_start(void *stack_pointer) {
      long count;
      char *arguments;
      char *environment;
      struct auxiliary *values;

      count = *((long *) stack_pointer);
      arguments = ((char **) stack_pointer) + 1;
      environment = arguments + count + 1;
      values = after(environment);

      /* start is the main function */
      return start(count, arguments, environment, values);
  }
> Can you share this code? If not, could you document how exactly would you have done this?

Unfortunately I didn't make it that far. I was planning to study how glibc and musl do it just like how I studied their system call implementations. If I start working on this again I'm gonna look this up. I'll need to learn this linking stuff in order to support the kernel vDSO anyway.

There are no comments on this comment yet.