1 /* Copyright 2019 SiFive Inc. */
2 /* SPDX-License-Identifier: Apache-2.0 */
4 #include <metal/init.h>
7 * These function pointers are created by the linker script
8 * in the .init_array section. The arrays defined by these
9 * and end points are the set of functions defined by instances
10 * of METAL_CONSTRUCTOR() and METAL_DESTRUCTOR().
12 extern metal_constructor_t metal_constructors_start;
13 extern metal_constructor_t metal_constructors_end;
14 extern metal_destructor_t metal_destructors_start;
15 extern metal_destructor_t metal_destructors_end;
17 void metal_init(void) {
18 /* Make sure the constructors only run once */
19 static int init_done = 0;
25 if (&metal_constructors_end <= &metal_constructors_start) {
29 metal_constructor_t *funcptr = &metal_constructors_start;
30 while (funcptr != &metal_constructors_end) {
31 metal_constructor_t func = *funcptr;
39 void metal_fini(void) {
40 /* Make sure the destructors only run once */
41 static int fini_done = 0;
47 if (&metal_destructors_end <= &metal_destructors_start) {
51 metal_destructor_t *funcptr = &metal_destructors_start;
52 while (funcptr != &metal_destructors_end) {
53 metal_destructor_t func = *funcptr;
62 * metal_init_run() and metal_fini_run() are marked weak so that users
63 * can redefine them for their own purposes, including to no-ops
64 * in the case that users don't want the metal constructors or
68 void metal_init_run(void) __attribute__((weak));
69 void metal_init_run(void) { metal_init(); }
71 void metal_fini_run(void) __attribute__((weak));
72 void metal_fini_run(void) { metal_fini(); }