| 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 %libc %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 | |
| 18 | struct object; |
| 19 | static int counter; |
| 20 | |
| 21 | void __register_frame_info(const void *fi, struct object *obj) { |
| 22 | printf(format: "__register_frame_info()\n" ); |
| 23 | } |
| 24 | |
| 25 | void __deregister_frame_info(const void *fi) { |
| 26 | printf(format: "__deregister_frame_info()\n" ); |
| 27 | } |
| 28 | |
| 29 | void __attribute__((constructor)) ctor() { |
| 30 | printf(format: "ctor()\n" ); |
| 31 | ++counter; |
| 32 | } |
| 33 | |
| 34 | void __attribute__((destructor)) dtor() { |
| 35 | printf(format: "dtor()\n" ); |
| 36 | if (--counter != 0) |
| 37 | abort(); |
| 38 | } |
| 39 | |
| 40 | int main() { |
| 41 | printf(format: "main()\n" ); |
| 42 | return 0; |
| 43 | } |
| 44 | |