From c520863abb901912799a1178a6caa9079f6214bf Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 12 Aug 2021 09:31:31 -0700 Subject: [PATCH] [crt][test] Make ctor_dtor.c robust if DT_INIT/DT_FINI is disabled New ports in glibc typically don't define ELF_INITFINI, so DT_INIT/DT_FINI support is disabled. (rhel ppc64le likely patches their glibc this way as well.) musl can disable DT_INIT/DT_FINI via -DNO_LEGACY_INITFINI. So we cannot guarantee ctor()/dtor() will be printed. --- compiler-rt/test/crt/ctor_dtor.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler-rt/test/crt/ctor_dtor.c b/compiler-rt/test/crt/ctor_dtor.c index 69848a01c7ca0..90be0b452eedc 100644 --- a/compiler-rt/test/crt/ctor_dtor.c +++ b/compiler-rt/test/crt/ctor_dtor.c @@ -3,16 +3,18 @@ // RUN: %run %t 2>&1 | FileCheck %s #include +#include // Ensure the various startup functions are called in the proper order. // CHECK: __register_frame_info() -// CHECK-NEXT: ctor() -// CHECK-NEXT: main() -// CHECK-NEXT: dtor() -// CHECK-NEXT: __deregister_frame_info() +/// ctor() is here if ld.so/libc supports DT_INIT/DT_FINI +// CHECK: main() +/// dtor() is here if ld.so/libc supports DT_INIT/DT_FINI +// CHECK: __deregister_frame_info() struct object; +static int counter; void __register_frame_info(const void *fi, struct object *obj) { printf("__register_frame_info()\n"); @@ -24,10 +26,13 @@ void __deregister_frame_info(const void *fi) { void __attribute__((constructor)) ctor() { printf("ctor()\n"); + ++counter; } void __attribute__((destructor)) dtor() { printf("dtor()\n"); + if (--counter != 0) + abort(); } int main() {