Background
Pointers to internal procedures are implemented using “trampolines”. Trampolines are segments of code that pass host-associated variables to the internal procedures, and they are currently generated on the stack. This means that code on the stack must be executable, which can increase security risks (e.g., enabling buffer overflow attacks). In fact, the GNU Linker in Binutils 2.39 or higher emits a warning message for this, and the LLVM Linker rejects this unless users explicitly allow it.
$ flang src.f90 -fuse-ld=ld
/path/to/ld: warning: src.o: requires executable stack (because the .note.GNU-stack section is executable)
$ flang src.f90 -fuse-ld=lld
ld.lld: error: src.o: requires an executable stack, but -z execstack is not specified
flang-22: error: linker command failed with exit code 1 (use -v to see invocation)
Please note that I have not checked whether Windows has a similar issue.
Problem
The problem is that these messages do not seem sufficient for users to understand what is happening. I suspect users would have the following questions when encountering these messages:
- Why is an executable stack required? Is it okay to enable it?
- What should I do to link binaries with
lld?flang src.f90 -fuse-ld=lld -z execstackreturns another error.
I doubt users can easily find answers to these questions at the moment.
Solutions
I have two possible solutions for this issue.
-
Implementing a warning flag
-Wtrampolinesas follows:$ flang src.f90 -Wtrampolines src.f90:nn:nn: warning: trampoline generated for pointer to inner subprogram 'inn', which requires an executable stack [-Wtrampolines] call sub(inn) ^ src.f90:nn:nn: note: 'inn' is declared here subroutine inn ^ /path/to/ld: warning: src.o: requires executable stack (because the .note.GNU-stack section is executable)- GFortran has the same command-line option, but it is not enabled by default. Considering the background, I believe Flang should enable it by default.
- However, there is a possibility of false-positive messages being output.
- I’m thinking of implementing this in the Flang frontend, but it might be better to implement it in the LLVM backend. This is because redundant trampolines are removed during the DSE pass there. (e.g., Compiler Explorer)
-
Improving documentation
- Illustrate the following points:
- The reason why these messages are emitted
- How to handle this issue
- The influence of an executable stack
- IIRC, there are two related documents: the support status and the design document.
- I’m wondering whether adding the above points to these documents would be appropriate.
- Clang has an FAQ page. I believe Flang could follow this.
- My concern is that the current top page of the Flang documentation is not suitable for accommodating a link to an FAQ. Specifically, there are no sections for user guidance. Perhaps, we should rearrange the Table of Contents.
- Illustrate the following points:
My current plan is to create an FAQ page and include a link to it on the support status page. However, I’m not sure if this is the best approach. I would appreciate your comments on this.