initpri1.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* initpri1.c -- test constructor priorities.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Copied from the gcc testsuite, where the test was contributed by
  4. Mark Mitchell <mark@codesourcery.com>.
  5. This file is part of gold.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. /* This tests that the linker handles constructor and destructor
  19. priorities correctly. */
  20. #include <stdlib.h>
  21. /* Constructor priorities in attributes were added in gcc 4.3. */
  22. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
  23. int i;
  24. int j;
  25. void c1(void) __attribute__((constructor (500)));
  26. void c2(void) __attribute__((constructor (700)));
  27. void c3(void) __attribute__((constructor (600)));
  28. void c1() {
  29. if (i++ != 0)
  30. abort ();
  31. }
  32. void c2() {
  33. if (i++ != 2)
  34. abort ();
  35. }
  36. void c3() {
  37. if (i++ != 1)
  38. abort ();
  39. }
  40. void d1(void) __attribute__((destructor (500)));
  41. void d2(void) __attribute__((destructor (700)));
  42. void d3(void) __attribute__((destructor (600)));
  43. void d1() {
  44. if (--i != 0)
  45. abort ();
  46. }
  47. void d2() {
  48. if (--i != 2)
  49. abort ();
  50. }
  51. void d3() {
  52. if (j != 4)
  53. abort ();
  54. if (--i != 1)
  55. abort ();
  56. }
  57. void cd4(void) __attribute__((constructor (800), destructor (800)));
  58. void cd4() {
  59. if (i != 3)
  60. abort ();
  61. ++j;
  62. }
  63. void cd5(void) __attribute__((constructor, destructor));
  64. void cd5() {
  65. if (i != 3)
  66. abort();
  67. ++j;
  68. }
  69. int main (void) {
  70. if (i != 3)
  71. return 1;
  72. if (j != 2)
  73. abort ();
  74. return 0;
  75. }
  76. #else /* !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) */
  77. int main (void) {
  78. exit (0);
  79. }
  80. #endif /* !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) */