1 /* Copyright (C) 2017-2020 Free Software Foundation, Inc.
2 
3    This file is free software; you can redistribute it and/or modify it
4    under the terms of the GNU General Public License as published by the
5    Free Software Foundation; either version 3, or (at your option) any
6    later version.
7 
8    This file is distributed in the hope that it will be useful, but
9    WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    General Public License for more details.
12 
13    Under Section 7 of GPL version 3, you are granted additional
14    permissions described in the GCC Runtime Library Exception, version
15    3.1, as published by the Free Software Foundation.
16 
17    You should have received a copy of the GNU General Public License and
18    a copy of the GCC Runtime Library Exception along with this program;
19    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
20    <http://www.gnu.org/licenses/>.  */
21 
22 typedef long long size_t;
23 
24 /* Provide an entry point symbol to silence a linker warning.  */
_start()25 void _start() {}
26 
27 #ifdef USE_NEWLIB_INITFINI
28 
29 extern void __libc_init_array (void) __attribute__((weak));
30 extern void __libc_fini_array (void) __attribute__((weak));
31 
32 __attribute__((amdgpu_hsa_kernel ()))
_init_array()33 void _init_array()
34 {
35   __libc_init_array ();
36 }
37 
38 __attribute__((amdgpu_hsa_kernel ()))
_fini_array()39 void _fini_array()
40 {
41   __libc_fini_array ();
42 }
43 
44 #endif
45 
46 /* These magic symbols are provided by the linker.  */
47 extern void (*__preinit_array_start []) (void) __attribute__((weak));
48 extern void (*__preinit_array_end []) (void) __attribute__((weak));
49 extern void (*__init_array_start []) (void) __attribute__((weak));
50 extern void (*__init_array_end []) (void) __attribute__((weak));
51 extern void (*__fini_array_start []) (void) __attribute__((weak));
52 extern void (*__fini_array_end []) (void) __attribute__((weak));
53 
54 __attribute__((amdgpu_hsa_kernel ()))
_init_array()55 void _init_array()
56 {
57   /* Iterate over all the init routines.  */
58   size_t count;
59   size_t i;
60 
61   count = __preinit_array_end - __preinit_array_start;
62   for (i = 0; i < count; i++)
63     __preinit_array_start[i] ();
64 
65   count = __init_array_end - __init_array_start;
66   for (i = 0; i < count; i++)
67     __init_array_start[i] ();
68 }
69 
70 __attribute__((amdgpu_hsa_kernel ()))
_fini_array()71 void _fini_array()
72 {
73   size_t count;
74   size_t i;
75 
76   count = __fini_array_end - __fini_array_start;
77   for (i = count; i > 0; i--)
78     __fini_array_start[i-1] ();
79 }
80