Files
clang-p2996/libc/startup/linux/x86_64/start.cpp
Schrodinger ZHU Yifan 2bc994456c [libc] major refactor of startup library (#76092)
* separate initialization routines into _start and do_start for all
architectures.
* lift do_start as a separate object library to avoid code duplication.
* (addtionally) address the problem of building hermetic libc with
-fstack-pointer-*

The `crt1.o` is now a merged result of three components:

```
___
  |___ x86_64
  |      |_______ start.cpp.o    <- _start (loads process initial stack and aligns stack pointer)
  |      |_______ tls.cpp.o      <- init_tls, cleanup_tls, set_thread_pointer (TLS related routines) 
  |___ do_start.cpp.o            <- do_start (sets up global variables and invokes the main function) 
```
2024-01-04 12:51:14 -08:00

34 lines
1.7 KiB
C++

//===-- Implementation of _start for x86_64 -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/__support/macros/attributes.h"
#include "startup/linux/do_start.h"
extern "C" [[noreturn]] void _start() {
// This TU is compiled with -fno-omit-frame-pointer. Hence, the previous
// value of the base pointer is pushed on to the stack. So, we step over
// it (the "+ 1" below) to get to the args.
LIBC_NAMESPACE::app.args = reinterpret_cast<LIBC_NAMESPACE::Args *>(
reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 1);
// The x86_64 ABI requires that the stack pointer is aligned to a 16-byte
// boundary. We align it here but we cannot use any local variables created
// before the following alignment. Best would be to not create any local
// variables before the alignment. Also, note that we are aligning the stack
// downwards as the x86_64 stack grows downwards. This ensures that we don't
// tread on argc, argv etc.
// NOTE: Compiler attributes for alignment do not help here as the stack
// pointer on entry to this _start function is controlled by the OS. In fact,
// 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");
LIBC_NAMESPACE::do_start();
}