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