brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 25da25a Raw
34 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8#include "src/__support/macros/attributes.h"9#include "startup/linux/do_start.h"10 11extern "C" [[noreturn]] void _start() {12  // This TU is compiled with -fno-omit-frame-pointer. Hence, the previous13  // value of the base pointer is pushed on to the stack. So, we step over14  // 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-byte19  // boundary. We align it here but we cannot use any local variables created20  // before the following alignment. Best would be to not create any local21  // variables before the alignment. Also, note that we are aligning the stack22  // downwards as the x86_64 stack grows downwards. This ensures that we don't23  // tread on argc, argv etc.24  // NOTE: Compiler attributes for alignment do not help here as the stack25  // 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 the28  // 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