|
| 1 | +/**************************************************************************** |
| 2 | + * arch/xtensa/src/common/crt0.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#include <sys/types.h> |
| 30 | +#include <stdlib.h> |
| 31 | + |
| 32 | +#include <nuttx/addrenv.h> |
| 33 | +#include <nuttx/arch.h> |
| 34 | + |
| 35 | +#include <arch/syscall.h> |
| 36 | + |
| 37 | +/**************************************************************************** |
| 38 | + * Public Data |
| 39 | + ****************************************************************************/ |
| 40 | + |
| 41 | +/* Linker defined symbols to .ctors and .dtors */ |
| 42 | + |
| 43 | +extern initializer_t _sctors[]; |
| 44 | +extern initializer_t _ectors[]; |
| 45 | +extern initializer_t _sdtors[]; |
| 46 | +extern initializer_t _edtors[]; |
| 47 | + |
| 48 | +/**************************************************************************** |
| 49 | + * Public Function Prototypes |
| 50 | + ****************************************************************************/ |
| 51 | + |
| 52 | +int main(int argc, char *argv[]); |
| 53 | + |
| 54 | +/**************************************************************************** |
| 55 | + * Private Functions |
| 56 | + ****************************************************************************/ |
| 57 | + |
| 58 | +#ifdef CONFIG_HAVE_CXXINITIALIZE |
| 59 | + |
| 60 | +/**************************************************************************** |
| 61 | + * Name: exec_ctors |
| 62 | + * |
| 63 | + * Description: |
| 64 | + * Call static constructors |
| 65 | + * |
| 66 | + ****************************************************************************/ |
| 67 | + |
| 68 | +static void exec_ctors(void) |
| 69 | +{ |
| 70 | + for (initializer_t *ctor = _sctors; ctor != _ectors; ctor++) |
| 71 | + { |
| 72 | + (*ctor)(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/**************************************************************************** |
| 77 | + * Name: exec_dtors |
| 78 | + * |
| 79 | + * Description: |
| 80 | + * Call static destructors |
| 81 | + * |
| 82 | + ****************************************************************************/ |
| 83 | + |
| 84 | +static void exec_dtors(void) |
| 85 | +{ |
| 86 | + for (initializer_t *dtor = _sdtors; dtor != _edtors; dtor++) |
| 87 | + { |
| 88 | + (*dtor)(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#endif |
| 93 | + |
| 94 | +/**************************************************************************** |
| 95 | + * Public Functions |
| 96 | + ****************************************************************************/ |
| 97 | + |
| 98 | +/**************************************************************************** |
| 99 | + * Name: __start |
| 100 | + * |
| 101 | + * Description: |
| 102 | + * This function is the low level entry point into the main thread of |
| 103 | + * execution of a task. It receives initial control when the task is |
| 104 | + * started and calls main entry point of the newly started task. |
| 105 | + * |
| 106 | + * Input Parameters: |
| 107 | + * argc - The number of parameters being passed. |
| 108 | + * argv - The parameters being passed. These lie in kernel-space memory |
| 109 | + * and will have to be reallocated in user-space memory. |
| 110 | + * |
| 111 | + * Returned Value: |
| 112 | + * This function should not return. It should call the user-mode start-up |
| 113 | + * main() function. If that function returns, this function will call |
| 114 | + * exit. |
| 115 | + * |
| 116 | + ****************************************************************************/ |
| 117 | + |
| 118 | +void __start(int argc, char *argv[]) |
| 119 | +{ |
| 120 | + int ret; |
| 121 | + |
| 122 | + /* Initialize the reserved area at the beginning of the .bss/.data region |
| 123 | + * that is visible to the RTOS. |
| 124 | + */ |
| 125 | + |
| 126 | +#ifdef CONFIG_BUILD_KERNEL |
| 127 | + ARCH_DATA_RESERVE->ar_sigtramp = (addrenv_sigtramp_t)sig_trampoline; |
| 128 | +#endif |
| 129 | + |
| 130 | +#ifdef CONFIG_HAVE_CXXINITIALIZE |
| 131 | + /* Call C++ constructors */ |
| 132 | + |
| 133 | + exec_ctors(); |
| 134 | + |
| 135 | + /* Setup so that C++ destructors called on task exit */ |
| 136 | + |
| 137 | +# if CONFIG_LIBC_MAX_EXITFUNS > 0 |
| 138 | + atexit(exec_dtors); |
| 139 | +# endif |
| 140 | +#endif |
| 141 | + |
| 142 | + /* Call the main() entry point passing argc and argv. */ |
| 143 | + |
| 144 | + ret = main(argc, argv); |
| 145 | + |
| 146 | +#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0 |
| 147 | + exec_dtors(); |
| 148 | +#endif |
| 149 | + |
| 150 | + /* Call exit() if/when the main() returns */ |
| 151 | + |
| 152 | + exit(ret); |
| 153 | +} |
0 commit comments