I agree that would be the best. I believe (but haven’t verified) that MacOS forbids having an executable stack, yet GCC compilers still work. I also read something about this in a recent C standard proposal: Functional Functions - A Comprehensive Proposal Overviewing Blocks, Nested Functions, and Lambdas for C,
NOTE: As of early 2025 in GCC 14, GCC selected a heap-based implementation that got rid of the executable stack. This requires some amount of dynamic allocation in cases where it cannot prove that the function is only passed down, not have its address taken in a meaningful way, or if it is not used immediately (as determined by the optimizer). It can be turned on with
-ftrampoline-impl=heap.
@zaikunzhang, are you strictly limited to GCC 13?
Not all Fortran compilers use the executable stack approach. A brief discussion of the trampoline mechanism can be found here: Is creating nested subroutines/functions considered good practice in Fortran? - #25 by szakharin
Another discussion of issues related to internal procedures is from @sblionel in Doctor Fortran in “Think, Thank, Thunk”,
Historical note: in the past, these thunks were built on the stack, which was very convenient. However the notion of executable code on the stack was also convenient for virus writers so operating systems evolved to disallow this. Other solutions were found, a topic for someone else’s blog.