|
| 1 | +<!--===- docs/FAQ.md |
| 2 | +
|
| 3 | + Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | + See https://llvm.org/LICENSE.txt for license information. |
| 5 | + SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +
|
| 7 | +--> |
| 8 | + |
| 9 | +# Frequently Asked Questions (FAQ) |
| 10 | + |
| 11 | +```{contents} |
| 12 | +--- |
| 13 | +local: |
| 14 | +--- |
| 15 | +``` |
| 16 | + |
| 17 | +## Driver |
| 18 | + |
| 19 | +### Why do I get a warning or an error about an executable stack? |
| 20 | + |
| 21 | +This occurs because Flang's implementation of pointers to internal procedures requires an executable stack. |
| 22 | + |
| 23 | +An internal procedure has a "host scope", which is the scope in which it is contained. |
| 24 | +It can access variables defined in that host scope. |
| 25 | +When an internal procedure is referenced from outside its host scope (for example, via a procedure pointer), the implementation must ensure that it can still access variables from the host scope. |
| 26 | +To achieve this, the current implementation of Flang generates a small piece of code, called a "trampoline", on the stack. |
| 27 | +When the procedure is called, this trampoline is executed. |
| 28 | +The trampoline is on the stack, so the stack itself must be executable. |
| 29 | +For a more detailed explanation of trampolines, please refer to the [design document](InternalProcedureTrampolines.md). |
| 30 | + |
| 31 | +An executable stack increases the risk and impact of certain classes of security vulnerabilities, such as [stack buffer overflows](https://llsoftsec.github.io/llsoftsecbook/#stack-buffer-overflows). |
| 32 | +Therefore, modern linkers often issue a warning or an error if an executable stack is not explicitly requested by the developer. |
| 33 | +For instance, the GNU Linker (`ld`) issues a warning while the LLVM Linker (`lld`) emits an error. |
| 34 | + |
| 35 | +```{note} |
| 36 | +The trampoline code generated by Flang is not itself a security risk. |
| 37 | +The risk comes from the possibility of executing malicious code that an attacker has placed on the stack. |
| 38 | +You should determine whether such risks are appropriate for your software. |
| 39 | +``` |
| 40 | + |
| 41 | +When you use the Flang driver (the `flang` command) to generate executables, you can instruct the linker to enable an executable stack with the `-Wl,-z,execstack` or `-Xlinker -zexecstack` flag. |
| 42 | + |
| 43 | +```console |
| 44 | +$ flang src.f90 -fuse-ld=ld |
| 45 | +/path/to/ld: warning: src.o: requires executable stack (because the .note.GNU-stack section is executable) |
| 46 | +$ flang src.f90 -fuse-ld=ld -Wl,-z,execstack |
| 47 | + |
| 48 | +$ flang src.f90 -fuse-ld=lld |
| 49 | +ld.lld: error: src.o: requires an executable stack, but -z execstack is not specified |
| 50 | +flang-22: error: linker command failed with exit code 1 (use -v to see invocation) |
| 51 | +$ flang src.f90 -fuse-ld=lld -Wl,-z,execstack |
| 52 | +``` |
0 commit comments