]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/testsuite/initpri1.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gold / testsuite / initpri1.c
CommitLineData
2fd32231
ILT
1/* initpri1.c -- test constructor priorities.
2
250d07de 3 Copyright (C) 2007-2021 Free Software Foundation, Inc.
2fd32231
ILT
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,
a6a22b83 22 MA 02110-1301, USA. */
2fd32231 23
a6a22b83
ILT
24/* This tests that the linker handles constructor and destructor
25 priorities correctly. */
2fd32231
ILT
26
27#include <stdlib.h>
28
a6a22b83
ILT
29/* Constructor priorities in attributes were added in gcc 4.3. */
30#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
31
2fd32231
ILT
32int i;
33int j;
34
bb6f53d3
ILT
35void c1(void) __attribute__((constructor (500)));
36void c2(void) __attribute__((constructor (700)));
37void c3(void) __attribute__((constructor (600)));
2fd32231
ILT
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
bb6f53d3
ILT
54void d1(void) __attribute__((destructor (500)));
55void d2(void) __attribute__((destructor (700)));
56void d3(void) __attribute__((destructor (600)));
2fd32231
ILT
57
58void d1() {
59 if (--i != 0)
60 abort ();
61}
62
63void d2() {
64 if (--i != 2)
65 abort ();
66}
67
68void d3() {
ab794b6b 69 if (j != 4)
2fd32231
ILT
70 abort ();
71 if (--i != 1)
72 abort ();
73}
74
bb6f53d3 75void cd4(void) __attribute__((constructor (800), destructor (800)));
2fd32231
ILT
76
77void cd4() {
78 if (i != 3)
79 abort ();
80 ++j;
81}
82
bb6f53d3 83void cd5(void) __attribute__((constructor, destructor));
ab794b6b
ILT
84
85void cd5() {
86 if (i != 3)
87 abort();
88 ++j;
89}
90
bb6f53d3 91int main (void) {
2fd32231
ILT
92 if (i != 3)
93 return 1;
ab794b6b 94 if (j != 2)
2fd32231
ILT
95 abort ();
96 return 0;
97}
a6a22b83
ILT
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)) */