Mark Assembly Objects Non-Executable-Stack With .note.GNU-stack and -z noexecstack
An assembly file without a .note.GNU-stack section makes the linker mark the whole program's stack executable. Add the section or pass -z noexecstack.

You can build a clean C program with every hardening flag, add one small assembly file with a fast routine, link, and find that the whole executable now has an executable stack. Nothing in the build warns you on older toolchains, and the C compiler had nothing to do with it. The cause is a convention in the GNU toolchain: an object file declares whether it needs an executable stack through a section named .note.GNU-stack, and an object that lacks the section is assumed, for compatibility with old code, to need one. This post shows the failure, the three ways to fix it, how each of GNU as, the C preprocessor path and ld handles it and how to check the result. Examples target GCC 15 and binutils 2.45 on x86-64 Linux 6.12.
The failure
A trivial assembly function that returns 42, written in AT&T syntax for the GNU assembler:
.text
.globl answer
.type answer, @function
answer:
movl $42, %eax
ret
.size answer, .-answerA C file that calls it:
#include <stdio.h>
int answer(void);
int main(void)
{
printf("%d\n", answer());
return 0;
}Build both and link with the usual hardening flags, then look at the stack header:
gcc -std=c17 -O2 -c main.c
gcc -c answer.s
gcc -o demo main.o answer.o
readelf -lW demo | grep GNU_STACK GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RWE 0x10RWE means read, write and execute. The stack is executable, and the program carries that property no matter how careful main.c was. With binutils 2.39 and later the link step also prints a warning:
/usr/bin/ld: warning: answer.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linkerBoth messages exist since binutils 2.39, which made the silent default noisy, and a future release is expected to change the default to non-executable. On older linkers the warning is absent and the failure is silent.
Why an object file has an opinion
When the linker combines objects into an executable, it must decide the PT_GNU_STACK flags for the output. Each input contributes a vote through its .note.GNU-stack section, which is empty and has no allocation flags, so it takes no space in memory. The section's own flags carry the answer: no X flag means the object does not need an executable stack, and X means it does. An object with no such section at all is treated as needing one, because code from the pre-2000 era sometimes built trampolines on the stack. C compilers on Linux emit the section automatically with no X flag, which is why C code does not suffer from this, except when a nested function with its address taken forces GCC to emit a trampoline and mark the stack executable, which -Wtrampolines warns about.
The linker takes the strictest answer. One object that lacks the marker, or marks X, makes the output stack executable, and every other object's declaration is overridden. The kernel and the dynamic loader then honor the PT_GNU_STACK header: the main thread's stack and every new thread's stack are created executable.
Fix 1: add the section to the assembly source
The direct fix is to declare the section at the end of each assembly file:
.text
.globl answer
.type answer, @function
answer:
movl $42, %eax
ret
.size answer, .-answer
.section .note.GNU-stack,"",@progbitsThe directive's pieces are the section name, an empty flags string (no a, w or x) and the type @progbits. On targets where @ starts a comment, such as ARM and AArch64 in some assemblers, the type is written %progbits. Using %progbits everywhere is safe, since all GNU assembler targets accept it, so portable sources prefer it:
.section .note.GNU-stack,"",%progbitsRebuild and check:
$ gcc -c answer.s && gcc -o demo main.o answer.o
$ readelf -lW demo | grep GNU_STACK
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10The flags are RW, and the linker warning is gone.
Fix 2: when the file goes through the C preprocessor
Assembly files named with a capital .S are run through the C preprocessor first, which is the usual way to share macros with C code. Guard the directive so the file still assembles on non-ELF targets such as macOS or Windows, which have no such section:
#include "config.h"
.text
.globl answer
answer:
movl $42, %eax
ret
#if defined(__linux__) && defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endifNote that the file starts a line with #include and #if, which the preprocessor handles. This only applies to .S files that the C driver preprocesses, and not to the body of an .s file passed straight to the assembler, where # begins a comment on most targets.
Fix 3: tell the assembler or the linker
If you cannot change the source, for example because the assembly comes from a vendored library or a code generator, two flags override the default.
For the assembler, --noexecstack makes the object carry the non-executable marker without editing the source:
gcc -Wa,--noexecstack -c answer.sFor the linker, -z noexecstack sets the output's stack flags to non-executable regardless of what the inputs voted:
gcc -Wl,-z,noexecstack -o demo main.o answer.oThe second is a blunt instrument, and it is the right one to use as a default in a hardened build, because it prevents a surprise from any object. It does not make code that really needs an executable stack work, and the program then crashes with SIGSEGV when it jumps into the stack. The linker flag has a counterpart, -z execstack, which forces the opposite. Do not use it unless you can name the code that needs it.
The GNU linker documentation lists these in Options under -z, and the assembler's --noexecstack and --execstack are in its command-line options.
Inline assembly and generated code
Inline asm inside a C file is fine, because the C compiler already emitted the section. The trouble starts when the inline assembly switches sections with .section or .pushsection and forgets to restore the previous one, but that affects where the code lands and not the stack note. Code that is generated at build time into .s files, from a template or another tool, needs the directive in its template. Compilers for other languages usually emit the note: Go's linker, Rust's rustc and Clang all produce non-executable-stack objects.
Static libraries are the easy place to miss it. An archive of objects built years ago may contain one that lacks the section, and the linker pulls it in only when a symbol from it is needed, so the executable becomes executable-stack only in some builds. Check an archive's members with readelf -S on each object, or look for the linker warning in the full link log.
Check the result
For an executable, look at the GNU_STACK header as above. For an object file or an archive member, look for the section and its flags:
$ readelf -SW answer.o | grep -E 'GNU-stack|Name'
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 5] .note.GNU-stack PROGBITS 0000000000000000 000040 000000 00 0 0 1An empty Flg column is a non-executable declaration. A flag of X means the object asks for an executable stack, and a missing section means it asks implicitly. Binutils 2.39 and later print the missing-section warning and let you control it with --warn-execstack and --no-warn-execstack, and newer releases also offer a mode that turns it into an error, so a CI log scan for missing .note.GNU-stack is a cheap guard. The hardening-verification script in this series catches the final result with its GNU_STACK check.
For a running process, /proc/<pid>/maps shows rwx on the stack line when it is executable and rw-p when it is not.


