[libc-commits] [libc] [libc] major refactor of startup library (PR #76092)

via libc-commits libc-commits at lists.llvm.org
Thu Dec 21 15:56:51 PST 2023


================
@@ -159,88 +28,8 @@ extern "C" void _start() {
   // compilers can generate code assuming the alignment as required by the ABI.
   // If the stack pointers as setup by the OS are already aligned, then the
   // following code is a NOP.
-  __asm__ __volatile__("andq $0xfffffffffffffff0, %rsp\n\t");
-  __asm__ __volatile__("andq $0xfffffffffffffff0, %rbp\n\t");
-
-  auto tid = LIBC_NAMESPACE::syscall_impl<long>(SYS_gettid);
-  if (tid <= 0)
-    LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, 1);
-  LIBC_NAMESPACE::main_thread_attrib.tid = static_cast<int>(tid);
-
-  // After the argv array, is a 8-byte long NULL value before the array of env
-  // values. The end of the env values is marked by another 8-byte long NULL
-  // value. We step over it (the "+ 1" below) to get to the env values.
-  uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
-  uint64_t *env_end_marker = env_ptr;
-  app.env_ptr = env_ptr;
-  while (*env_end_marker)
-    ++env_end_marker;
-
-  // Initialize the POSIX global declared in unistd.h
-  environ = reinterpret_cast<char **>(env_ptr);
-
-  // After the env array, is the aux-vector. The end of the aux-vector is
-  // denoted by an AT_NULL entry.
-  Elf64_Phdr *program_hdr_table = nullptr;
-  uintptr_t program_hdr_count = 0;
-  app.auxv_ptr = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
-  for (auto *aux_entry = app.auxv_ptr; aux_entry->id != AT_NULL; ++aux_entry) {
-    switch (aux_entry->id) {
-    case AT_PHDR:
-      program_hdr_table = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
-      break;
-    case AT_PHNUM:
-      program_hdr_count = aux_entry->value;
-      break;
-    case AT_PAGESZ:
-      app.page_size = aux_entry->value;
-      break;
-    default:
-      break; // TODO: Read other useful entries from the aux vector.
-    }
-  }
-
-  app.tls.size = 0;
-  for (uintptr_t i = 0; i < program_hdr_count; ++i) {
-    Elf64_Phdr *phdr = program_hdr_table + i;
-    if (phdr->p_type != PT_TLS)
-      continue;
-    // TODO: p_vaddr value has to be adjusted for static-pie executables.
-    app.tls.address = phdr->p_vaddr;
-    app.tls.size = phdr->p_memsz;
-    app.tls.init_size = phdr->p_filesz;
-    app.tls.align = phdr->p_align;
-  }
-
-  // This descriptor has to be static since its cleanup function cannot
-  // capture the context.
-  static LIBC_NAMESPACE::TLSDescriptor tls;
-  LIBC_NAMESPACE::init_tls(tls);
-  if (tls.size != 0 && !LIBC_NAMESPACE::set_thread_ptr(tls.tp))
-    LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, 1);
-
-  LIBC_NAMESPACE::self.attrib = &LIBC_NAMESPACE::main_thread_attrib;
-  LIBC_NAMESPACE::main_thread_attrib.atexit_callback_mgr =
-      LIBC_NAMESPACE::internal::get_thread_atexit_callback_mgr();
-  // We register the cleanup_tls function to be the last atexit callback to be
-  // invoked. It will tear down the TLS. Other callbacks may depend on TLS (such
-  // as the stack protector canary).
-  LIBC_NAMESPACE::atexit(
-      []() { LIBC_NAMESPACE::cleanup_tls(tls.tp, tls.size); });
-  // We want the fini array callbacks to be run after other atexit
-  // callbacks are run. So, we register them before running the init
-  // array callbacks as they can potentially register their own atexit
-  // callbacks.
-  LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks);
-
-  LIBC_NAMESPACE::call_init_array_callbacks(
-      static_cast<int>(app.args->argc),
-      reinterpret_cast<char **>(app.args->argv),
-      reinterpret_cast<char **>(env_ptr));
-
-  int retval = main(static_cast<int>(app.args->argc),
-                    reinterpret_cast<char **>(app.args->argv),
-                    reinterpret_cast<char **>(env_ptr));
+  LIBC_INLINE_ASM("andq $0xfffffffffffffff0, %rsp\n\t");
----------------
michaelrj-google wrote:

it's probably best not to use `LIBC_INLINE_ASM` here since it's likely we'll be removing that macro soon, see https://github.com/llvm/llvm-project/issues/75956 for more info.

https://github.com/llvm/llvm-project/pull/76092


More information about the libc-commits mailing list