initfini.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. */
  5. #include <stdint.h>
  6. #ifndef USE_LIBC_INITFINI
  7. #define USE_LIBC_INITFINI 0
  8. #endif
  9. #if USE_LIBC_INITFINI
  10. /*
  11. * The _init() and _fini() will be called respectively when use __libc_init_array()
  12. * and __libc_fnit_array() in libc.a to perform constructor and destructor handling.
  13. * The dummy versions of these functions should be provided.
  14. */
  15. void _init(void)
  16. {
  17. }
  18. void _fini(void)
  19. {
  20. }
  21. #else
  22. /* These magic symbols are provided by the linker. */
  23. extern void (*__preinit_array_start[])(void) __attribute__((weak));
  24. extern void (*__preinit_array_end[])(void) __attribute__((weak));
  25. extern void (*__init_array_start[])(void) __attribute__((weak));
  26. extern void (*__init_array_end[])(void) __attribute__((weak));
  27. /*
  28. * The __libc_init_array()/__libc_fnit_array() function is used to do global
  29. * constructor/destructor and can NOT be compilied to generate the code coverage
  30. * data. We have the function attribute to be 'no_profile_instrument_function'
  31. * to prevent been instrumented for coverage analysis when GCOV=1 is applied.
  32. */
  33. /* Iterate over all the init routines. */
  34. void __libc_init_array(void) __attribute__((no_profile_instrument_function));
  35. void __libc_init_array(void)
  36. {
  37. uint32_t count;
  38. uint32_t i;
  39. count = __preinit_array_end - __preinit_array_start;
  40. for (i = 0; i < count; i++) {
  41. __preinit_array_start[i]();
  42. }
  43. count = __init_array_end - __init_array_start;
  44. for (i = 0; i < count; i++) {
  45. __init_array_start[i]();
  46. }
  47. }
  48. extern void (*__fini_array_start[])(void) __attribute__((weak));
  49. extern void (*__fini_array_end[])(void) __attribute__((weak));
  50. /* Run all the cleanup routines. */
  51. void __libc_fini_array(void) __attribute__((no_profile_instrument_function));
  52. void __libc_fini_array(void)
  53. {
  54. uint32_t count;
  55. uint32_t i;
  56. count = __fini_array_end - __fini_array_start;
  57. for (i = count; i > 0; i--) {
  58. __fini_array_start[i - 1]();
  59. }
  60. }
  61. #endif