gmon-start.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Code to enable profiling at program startup.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. Note that people who make modified versions of this file are not
  17. obligated to grant this special exception for their modified
  18. versions; it is their choice whether to do so. The GNU Lesser
  19. General Public License gives permission to release a modified
  20. version without this exception; this exception also makes it
  21. possible to release a modified version which carries forward this
  22. exception.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #include <sys/types.h>
  31. #include <sys/gmon.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <elf-initfini.h>
  35. /* Use __executable_start as the lowest address to keep profiling records
  36. if it provided by the linker. */
  37. extern const char __executable_start[] __attribute__ ((visibility ("hidden")));
  38. extern char etext[];
  39. #if !ELF_INITFINI
  40. /* Instead of defining __gmon_start__ globally in gcrt1.o, we make it
  41. static and just put a pointer to it into the .preinit_array
  42. section. */
  43. # define GMON_START_ARRAY_SECTION ".preinit_array"
  44. #endif
  45. #ifdef GMON_START_ARRAY_SECTION
  46. static void __gmon_start__ (void);
  47. static void (*const gmon_start_initializer) (void)
  48. __attribute__ ((used, section (GMON_START_ARRAY_SECTION))) = &__gmon_start__;
  49. static
  50. #else
  51. /* We cannot use the normal constructor mechanism to call
  52. __gmon_start__ because gcrt1.o appears before crtbegin.o in the link.
  53. Instead crti.o calls it specially. */
  54. extern void __gmon_start__ (void);
  55. #endif
  56. void
  57. __gmon_start__ (void)
  58. {
  59. /* Protect from being called more than once. Since crti.o is linked
  60. into every shared library, each of their init functions will call us. */
  61. static int called;
  62. if (called)
  63. return;
  64. called = 1;
  65. /* Start keeping profiling records. */
  66. __monstartup ((u_long) &__executable_start, (u_long) &etext);
  67. /* Call _mcleanup before exiting; it will write out gmon.out from the
  68. collected data. */
  69. atexit (&_mcleanup);
  70. }