| 1 |
/* Global constructor/destructor support |
| 2 |
|
| 3 |
Copyright (C) 2024-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software; you can redistribute it and/or modify it |
| 6 |
under the terms of the GNU General Public License as published by the |
| 7 |
Free Software Foundation; either version 3, or (at your option) any |
| 8 |
later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, but |
| 11 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
#include "auto-target.h" |
| 25 |
|
| 26 |
#ifdef HAVE_STDLIB_H |
| 27 |
# include <stdlib.h> |
| 28 |
#endif |
| 29 |
#include "gbl-ctors.h" |
| 30 |
|
| 31 |
extern int atexit (void (*function) (void)); |
| 32 |
|
| 33 |
|
| 34 |
/* Host/device compatibility: '__cxa_finalize'. Dummy; if necessary, |
| 35 |
overridden via libgomp 'target-cxa-dso-dtor.c'. */ |
| 36 |
|
| 37 |
extern void __GCC_offload___cxa_finalize (void *); |
| 38 |
|
| 39 |
void __attribute__((weak)) |
| 40 |
__GCC_offload___cxa_finalize (void *dso_handle __attribute__((unused))) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
/* There are no DSOs; this is the main program. */ |
| 45 |
static void * const __dso_handle = 0; |
| 46 |
|
| 47 |
|
| 48 |
/* Handler functions ('static', in contrast to the 'gbl-ctors.h' |
| 49 |
prototypes). */ |
| 50 |
|
| 51 |
static void __static_do_global_ctors (void); |
| 52 |
|
| 53 |
static void |
| 54 |
__static_do_global_ctors (void) |
| 55 |
{ |
| 56 |
__SIZE_TYPE__ nptrs = (__SIZE_TYPE__) __CTOR_LIST__[0]; |
| 57 |
for (__SIZE_TYPE__ i = nptrs; i >= 1; --i) |
| 58 |
__CTOR_LIST__[i] (); |
| 59 |
} |
| 60 |
|
| 61 |
static void __static_do_global_dtors (void); |
| 62 |
|
| 63 |
static void |
| 64 |
__static_do_global_dtors (void) |
| 65 |
{ |
| 66 |
__GCC_offload___cxa_finalize (__dso_handle); |
| 67 |
|
| 68 |
func_ptr *p = __DTOR_LIST__; |
| 69 |
++p; |
| 70 |
for (; *p; ++p) |
| 71 |
(*p) (); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/* For nvptx target configurations, override the 'crt0.c' dummy. */ |
| 76 |
|
| 77 |
extern void __gbl_ctors (void); |
| 78 |
|
| 79 |
void |
| 80 |
__gbl_ctors (void) |
| 81 |
{ |
| 82 |
__static_do_global_ctors (); |
| 83 |
atexit (__static_do_global_dtors); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* For nvptx offloading configurations, need '.entry' wrappers. */ |
| 88 |
|
| 89 |
# if defined(__nvptx_softstack__) && defined(__nvptx_unisimt__) |
| 90 |
|
| 91 |
/* OpenMP */ |
| 92 |
|
| 93 |
/* See 'crt0.c', 'mgomp.c'. */ |
| 94 |
extern void *__nvptx_stacks[32] __attribute__((shared,nocommon)); |
| 95 |
extern unsigned __nvptx_uni[32] __attribute__((shared,nocommon)); |
| 96 |
|
| 97 |
__attribute__((kernel)) void __do_global_ctors__entry__mgomp (void *); |
| 98 |
|
| 99 |
void |
| 100 |
__do_global_ctors__entry__mgomp (void *nvptx_stacks_0) |
| 101 |
{ |
| 102 |
__nvptx_stacks[0] = nvptx_stacks_0; |
| 103 |
__nvptx_uni[0] = 0; |
| 104 |
|
| 105 |
__static_do_global_ctors (); |
| 106 |
} |
| 107 |
|
| 108 |
__attribute__((kernel)) void __do_global_dtors__entry__mgomp (void *); |
| 109 |
|
| 110 |
void |
| 111 |
__do_global_dtors__entry__mgomp (void *nvptx_stacks_0) |
| 112 |
{ |
| 113 |
__nvptx_stacks[0] = nvptx_stacks_0; |
| 114 |
__nvptx_uni[0] = 0; |
| 115 |
|
| 116 |
__static_do_global_dtors (); |
| 117 |
} |
| 118 |
|
| 119 |
# else |
| 120 |
|
| 121 |
/* OpenACC */ |
| 122 |
|
| 123 |
__attribute__((kernel)) void __do_global_ctors__entry (void); |
| 124 |
|
| 125 |
void |
| 126 |
__do_global_ctors__entry (void) |
| 127 |
{ |
| 128 |
__static_do_global_ctors (); |
| 129 |
} |
| 130 |
|
| 131 |
__attribute__((kernel)) void __do_global_dtors__entry (void); |
| 132 |
|
| 133 |
void |
| 134 |
__do_global_dtors__entry (void) |
| 135 |
{ |
| 136 |
__static_do_global_dtors (); |
| 137 |
} |
| 138 |
|
| 139 |
# endif |
| 140 |
|
| 141 |
|
| 142 |
/* The following symbol just provides a means for the nvptx-tools 'ld' to |
| 143 |
trigger linking in this file. */ |
| 144 |
|
| 145 |
int __trigger_gbl_ctors; |