.mod_term_func entries are no longer run (starting from macOS 12.0)

Originator:iains.gcc
Number:rdar://FB9733712 Date Originated:2021-11-01
Status:open Resolved:
Product:macOS Product Version:12.0
Classification: Reproducible:yes
 
Executables can declare a set of entries to be run before (constructors) and after (destructors) the main body of code.

These entries are placed into special sections in the executable designated .mod_init_func  and .mod_term_func respectively.

in the example code we declare a startup function (__attribute__((constructor))) and a 
shutdown (__attribute__((destructor)).

==== ctor-dtor.c

#include <stdio.h>

__attribute__((constructor))
void startup ()
{
  fprintf(stderr, "startup\n");
}


__attribute__((destructor))
void shutdown ()
{
  fprintf(stderr, "shutdown\n");
}


int main ()
{
  printf ("42\n");
  return 0;
}

=====


On MacOS 11.6 and earlier, the output is:
startup
42
shutdown

On MacOS 12.0 the output is:
startup
42

==== so the destructor is not called.

NOTE: this does NOT show when the compiler is clang since clang attaches destructors to "cxa_atexit()" instead of using the mod term section.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!