Fixes compile failure if REENTRANT_SYSCALLS_PROVIDED and MISSING_SYSCALL_NAMES defined
[newlib-cygwin.git] / libgloss / cris / lcrt0.c
blobc1406155aee5183867664624d38bc382f9277432
1 /* Support for cris*-axis-linux-gnu and src/sim/cris simulator.
2 Copyright (C) 2000-2005, 2017, 2023, 2024 Axis Communications.
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 2. Neither the name of Axis Communications nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
20 COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
29 #include "linunistd.h"
30 #include "newlib.h"
31 #include <stdlib.h>
33 #ifdef _HAVE_INITFINI_ARRAY
34 #define _init __libc_init_array
35 #define _fini __libc_fini_array
36 #endif
38 extern void exit (int) __attribute ((__noreturn__));
40 __asm__ (".syntax no_register_prefix");
42 #ifdef __ELF__
43 /* This simulator magic for an earlier simulator was supposed to be
44 found two bytes before _start. Let's keep it for sake of
45 compatibility. Trying to emit them with an ordinary const char[]
46 and attribute section makes gcc barf; it doesn't like having the
47 same section attribute for both code and data.
48 The code is supposed to cause a crash if someone jumps to 0. */
49 __asm__
51 " .section .startup,\"ax\",@progbits\n"
52 " .byte 55,55\n"
53 " move.d 0xbadacce5,r9\n"
54 " clear.d [r9]\n"
55 " setf\n"
56 " setf\n"
57 " .previous");
58 #endif
60 __asm__
62 #ifdef __AOUT__
63 " .text\n\t"
64 #elif defined (__ELF__)
65 " .section .startup,\"ax\",@progbits\n"
66 #endif
67 " .global __start\n"
68 "__start:\n"
69 /* SP must be set up by the simulator or the system. */
71 /* Find ARGC, ARGV and ENV. */
72 /* ARGC. */
73 " move.d [sp],r10\n"
75 /* ARGV. */
76 " move.d sp,r11\n"
77 " addq 4,r11\n"
79 /* ENVP. */
80 " move.d sp,r12\n"
81 " addi r10.d,r12\n"
82 " addq 8,r12\n"
84 /* Terminate R9 and R6; we don't have a "console_print_etrax" or system
85 call function. */
86 " clear.d r9\n"
87 " clear.d r6\n"
88 " move.d __start1,r13\n"
89 " jump r13\n"
90 " setf\n"
91 #ifndef __AOUT__
92 /* We rely on a.out not being in .data here. Quite fragile, but
93 covered by e.g. running the GCC test-suite for cris-unknown-aout. */
94 " .previous"
95 #endif
98 extern void _Libctors (void);
99 extern void _Libdtors (void);
101 extern void __init__start (void) __attribute ((weak));
102 extern void __aout__ctors (void) __attribute ((weak));
103 extern int main (int argc, char **argv, char **env);
105 static void start1 (int, char **, char **) __asm__ ("__start1") __attribute ((__used__));
106 static void
107 start1 (int argc, char **argv, char **env)
109 #ifdef __ELF__
110 /* For ELF systems, we call _init and register _fini with atexit. */
112 extern void _init (void);
113 extern void _fini (void);
114 _init ();
115 if (atexit (_fini) != 0)
116 exit (-1);
118 #else
119 /* Constructors which may get here through the ELF .init section, when
120 linking ELF and producing a.out. */
121 if (__init__start)
122 __init__start ();
124 if (__aout__ctors)
125 __aout__ctors ();
127 /* Call constructors in shared libraries. */
128 _Libctors ();
130 if (atexit (_Libdtors) != 0)
131 exit (-1);
132 #endif
134 /* Call the user program. */
135 exit (main (argc, argv, env));