'How to disable stack canaries for specific functions?

With gcc, is it possible to compile with -fstack-protector, but omit for a specific function.

For example, say i have two functions.

void a() {
  ...
}

void b() {
  ...
}

Can I tell the compiler to compile a program that will use a canary before the saved return address of a, but no canary for b?



Solution 1:[1]

You'd have to test if it works (inspect the generated code at Godbolt) but it looks like you can do, for example:

__attribute__ ((no_stack_protector)) void foo () { ... }

no_sanitize looks like an intriguing option, I wonder who uses that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1