1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2019 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <string.h>
37 #include <picotls.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40
41 extern char __data_source[];
42 extern char __data_start[];
43 extern char __data_end[];
44 extern char __data_size[];
45 extern char __bss_start[];
46 extern char __bss_end[];
47 extern char __bss_size[];
48 extern char __tls_base[];
49 extern char __tdata_end[];
50 extern char __tls_end[];
51
52 #ifdef __PICOLIBC_CRT_RUNTIME_SIZE
53 #define __data_size (__data_end - __data_start)
54 #define __bss_size (__bss_end - __bss_start)
55 #endif
56
57 /* These two functions must be defined in the architecture-specific
58 * code
59 */
60
61 void
62 _start(void);
63
64 /* This is the application entry point */
65 int
66 main(int, char **);
67
68 #ifdef _HAVE_INITFINI_ARRAY
69 extern void __libc_init_array(void);
70 #endif
71
72 /* After the architecture-specific chip initialization is done, this
73 * function initializes the data and bss segments. Note that a static
74 * block of TLS data is carefully interleaved with the regular data
75 * and bss segments in picolibc.ld so that this one operation
76 * initializes both. Then it runs the application code, starting with
77 * any initialization functions, followed by the main application
78 * entry point and finally any cleanup functions
79 */
80
81 #include <picotls.h>
82 #include <stdio.h>
83 #ifdef CRT0_SEMIHOST
84 #include <semihost.h>
85 #endif
86
87 #ifndef CONSTRUCTORS
88 #define CONSTRUCTORS 1
89 #endif
90
91 static inline void
__start(void)92 __start(void)
93 {
94 memcpy(__data_start, __data_source, (uintptr_t) __data_size);
95 memset(__bss_start, '\0', (uintptr_t) __bss_size);
96 #ifdef PICOLIBC_TLS
97 _set_tls(__tls_base);
98 #endif
99 #if defined(_HAVE_INITFINI_ARRAY) && CONSTRUCTORS
100 __libc_init_array();
101 #endif
102
103 #ifdef CRT0_SEMIHOST
104 #define CMDLINE_LEN 1024
105 #define ARGV_LEN 64
106 static char cmdline[CMDLINE_LEN];
107 static char *argv[ARGV_LEN];
108 int argc = 0;
109
110 if (sys_semihost_get_cmdline(cmdline, sizeof(cmdline)) == 0 &&
111 cmdline[0])
112 {
113 char *c = cmdline;
114
115 while (*c && argc < ARGV_LEN - 1) {
116 argv[argc++] = c;
117 while (*c && *c != ' ')
118 c++;
119 if (!*c)
120 break;
121 *c = '\0';
122 while (*++c == ' ')
123 ;
124 }
125 } else
126 argv[argc++] = "program-name";
127 argv[argc] = NULL;
128 #else
129 #define argv NULL
130 #define argc 0
131 #endif
132
133 int ret = main(argc, argv);
134 #ifdef CRT0_EXIT
135 exit(ret);
136 #else
137 (void) ret;
138 for(;;);
139 #endif
140 }
141