1// REQUIRES: crt
2
3// RUN: %clang -fno-use-init-array -g -c %s -o %t.o
4// RUN: %clang -o %t -no-pie -nostdlib %crt1 %crti %crtbegin %t.o -lc %libgcc %crtend %crtn
5// RUN: %run %t 2>&1 | FileCheck %s
6
7#include <stdio.h>
8#include <stdlib.h>
9
10// Ensure the various startup functions are called in the proper order.
11
12// CHECK: __register_frame_info()
13/// ctor() is here if ld.so/libc supports DT_INIT/DT_FINI
14// CHECK: main()
15/// dtor() is here if ld.so/libc supports DT_INIT/DT_FINI
16// CHECK: __deregister_frame_info()
17
18struct object;
19static int counter;
20
21void __register_frame_info(const void *fi, struct object *obj) {
22 printf(format: "__register_frame_info()\n");
23}
24
25void __deregister_frame_info(const void *fi) {
26 printf(format: "__deregister_frame_info()\n");
27}
28
29void __attribute__((constructor)) ctor() {
30 printf(format: "ctor()\n");
31 ++counter;
32}
33
34void __attribute__((destructor)) dtor() {
35 printf(format: "dtor()\n");
36 if (--counter != 0)
37 abort();
38}
39
40int main() {
41 printf(format: "main()\n");
42 return 0;
43}
44

source code of compiler-rt/test/builtins/Unit/ctor_dtor.c