xref: /llvm-project/libc/startup/linux/x86_64/start.cpp (revision 2bc994456c5be2ab6d98b94de2349302577a9823)
1 //===-- Implementation of _start for x86_64 -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "src/__support/macros/attributes.h"
9 #include "startup/linux/do_start.h"
10 
_start()11 extern "C" [[noreturn]] void _start() {
12   // This TU is compiled with -fno-omit-frame-pointer. Hence, the previous
13   // value of the base pointer is pushed on to the stack. So, we step over
14   // it (the "+ 1" below) to get to the args.
15   LIBC_NAMESPACE::app.args = reinterpret_cast<LIBC_NAMESPACE::Args *>(
16       reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 1);
17 
18   // The x86_64 ABI requires that the stack pointer is aligned to a 16-byte
19   // boundary. We align it here but we cannot use any local variables created
20   // before the following alignment. Best would be to not create any local
21   // variables before the alignment. Also, note that we are aligning the stack
22   // downwards as the x86_64 stack grows downwards. This ensures that we don't
23   // tread on argc, argv etc.
24   // NOTE: Compiler attributes for alignment do not help here as the stack
25   // pointer on entry to this _start function is controlled by the OS. In fact,
26   // compilers can generate code assuming the alignment as required by the ABI.
27   // If the stack pointers as setup by the OS are already aligned, then the
28   // following code is a NOP.
29   asm volatile("andq $0xfffffffffffffff0, %rsp\n\t");
30   asm volatile("andq $0xfffffffffffffff0, %rbp\n\t");
31 
32   LIBC_NAMESPACE::do_start();
33 }
34