Skip to content

Commit

Permalink
[crt][test] Make ctor_dtor.c robust if DT_INIT/DT_FINI is disabled
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Aug 12, 2021
1 parent b611354 commit c520863
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions compiler-rt/test/crt/ctor_dtor.c
Expand Up @@ -3,16 +3,18 @@
// RUN: %run %t 2>&1 | FileCheck %s

#include <stdio.h>
#include <stdlib.h>

// 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");
Expand All @@ -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() {
Expand Down

0 comments on commit c520863

Please sign in to comment.