Skip to content

Commit b2e39ae

Browse files
committed
arch/xtensa:add crt0 to initialize environment
we use crt0 inside of start hook in pr #16154, so xtensa also need add it. Signed-off-by: anjiahao <[email protected]>
1 parent 9643ee7 commit b2e39ae

File tree

5 files changed

+172
-2
lines changed

5 files changed

+172
-2
lines changed

Diff for: arch/xtensa/src/Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ $(AOBJS) $(UAOBJS) $(HEAD_AOBJ): %$(OBJEXT): %.S
145145
$(COBJS) $(UCOBJS) $(HEAD_COBJ): %$(OBJEXT): %.c
146146
$(call COMPILE, $<, $@)
147147

148+
$(STARTUP_ELF_OBJS): %$(OBJEXT): %.c
149+
$(Q) $(CC) $(CELFFLAGS) -c common$(DELIM)crt0.c -o crt0$(OBJEXT)
150+
148151
ifeq ($(CONFIG_BUILD_FLAT),y)
149-
$(BIN): $(OBJS)
152+
$(BIN): $(STARTUP_ELF_OBJS) $(OBJS)
150153
$(call ARCHIVE, $@, $(OBJS))
151154
else
152-
$(BIN): $(UOBJS)
155+
$(BIN): $(STARTUP_ELF_OBJS) $(UOBJS)
153156
$(call ARCHIVE, $@, $(UOBJS))
154157
endif
155158

Diff for: arch/xtensa/src/common/Make.defs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
# The start-up, "head", file. May be either a .S or a .c file.
2424

25+
STARTUP_ELF_OBJS = crt0$(OBJEXT)
26+
2527
HEAD_ASRC = xtensa_vectors.S xtensa_window_vector.S xtensa_windowspill.S
2628
HEAD_ASRC += xtensa_int_handlers.S xtensa_user_handler.S
2729

Diff for: arch/xtensa/src/common/crt0.c

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

Diff for: arch/xtensa/src/lx6/Toolchain.defs

+6
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,9 @@ CXXELFFLAGS = $(CXXFLAGS) -fvisibility=hidden -mtext-section-literals
219219

220220
LDELFFLAGS = -r -e __start
221221
LDELFFLAGS += -T $(call CONVERT_PATH,$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)elf$(DELIM)gnu-elf.ld)
222+
ifneq ($(CONFIG_BUILD_KERNEL),y)
223+
# Flat build and protected elf entry point use crt0,
224+
# Kernel build will use apps/import/scripts/crt0
225+
226+
LDELFFLAGS += $(TOPDIR)$(DELIM)arch$(DELIM)xtensa$(DELIM)src$(DELIM)crt0.o
227+
endif

Diff for: arch/xtensa/src/lx7/Toolchain.defs

+6
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,9 @@ CXXELFFLAGS = $(CXXFLAGS) -fvisibility=hidden -mtext-section-literals
223223

224224
LDELFFLAGS = -r -e __start
225225
LDELFFLAGS += -T $(call CONVERT_PATH,$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)elf$(DELIM)gnu-elf.ld)
226+
ifneq ($(CONFIG_BUILD_KERNEL),y)
227+
# Flat build and protected elf entry point use crt0,
228+
# Kernel build will use apps/import/scripts/crt0
229+
230+
LDELFFLAGS += $(TOPDIR)$(DELIM)arch$(DELIM)xtensa$(DELIM)src$(DELIM)crt0.o
231+
endif

0 commit comments

Comments
 (0)