initpri3.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* initpri3.c -- test ctor odering when using init_array.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <iant@google.com>.
  4. This file is part of gold.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* This tests that the linker correctly orders .ctor entries when
  18. putting them into .init_array, as is the default. */
  19. #include <assert.h>
  20. int i = 1;
  21. static void
  22. ctor1 (void)
  23. {
  24. assert (i == 1);
  25. i = 2;
  26. }
  27. static void
  28. ctor2 (void)
  29. {
  30. assert (i == 2);
  31. i = 3;
  32. }
  33. static void
  34. dtor1 (void)
  35. {
  36. assert (i == 3);
  37. i = 2;
  38. }
  39. static void
  40. dtor2 (void)
  41. {
  42. assert (i == 2);
  43. i = 1;
  44. }
  45. /* The .ctors section is run in reverse order, the .dtors section in
  46. run in forward order. We give these arrays the "aligned" attribute
  47. because the x86_64 ABI would otherwise give them a 16-byte
  48. alignment, which may leave a hole in the section. */
  49. void (*ctors[]) (void)
  50. __attribute__ ((aligned (4), section (".ctors"))) = {
  51. ctor2,
  52. ctor1
  53. };
  54. void (*dtors[]) (void)
  55. __attribute__ ((aligned (4), section (".dtors"))) = {
  56. dtor1,
  57. dtor2
  58. };
  59. int
  60. main (void)
  61. {
  62. assert (i == 3);
  63. return 0;
  64. }